|
1 |
| -import { FlutterCommon } from './common'; |
| 1 | +import { Utils, fromObject } from '@nativescript/core'; |
| 2 | + |
| 3 | +import { FlutterChannelType, FlutterCommon } from './common'; |
2 | 4 |
|
3 | 5 | function makeFragmentName(viewId: number): string {
|
4 | 6 | return 'android:flutter:' + viewId;
|
5 | 7 | }
|
6 | 8 |
|
| 9 | +let didInit = false; |
| 10 | +let flutterEngine: io.flutter.embedding.engine.FlutterEngine; |
| 11 | +let channel: io.flutter.plugin.common.MethodChannel; |
| 12 | +let listener; |
| 13 | +let MethodCallClazz: java.lang.Class<io.flutter.plugin.common.MethodCall>; |
| 14 | +let argumentsMethod; |
| 15 | +const channelMethods = new Map(); |
| 16 | +export function init() { |
| 17 | + if (!didInit) { |
| 18 | + // todo remove remove after fixing runtime; |
| 19 | + MethodCallClazz = java.lang.Class.forName('io.flutter.plugin.common.MethodCall'); |
| 20 | + argumentsMethod = MethodCallClazz.getDeclaredMethod('arguments', []); |
| 21 | + |
| 22 | + flutterEngine = new io.flutter.embedding.engine.FlutterEngine(Utils.android.getApplicationContext()); |
| 23 | + flutterEngine.getDartExecutor().executeDartEntrypoint(io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint.createDefault()); |
| 24 | + io.flutter.embedding.engine.FlutterEngineCache.getInstance().put('default_nativescript', flutterEngine); |
| 25 | + io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister.registerGeneratedPlugins(flutterEngine); |
| 26 | + channel = new io.flutter.plugin.common.MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), 'nativescript'); |
| 27 | + listener = new io.flutter.plugin.common.MethodChannel.MethodCallHandler({ |
| 28 | + onMethodCall(call: io.flutter.plugin.common.MethodCall, result: io.flutter.plugin.common.MethodChannel.Result) { |
| 29 | + const method = channelMethods.get(call.method); |
| 30 | + if (method) { |
| 31 | + const args = argumentsMethod.invoke(call, null); //call.arguments(); |
| 32 | + const methodArgs = args ? Utils.dataDeserialize(args) : null; |
| 33 | + |
| 34 | + method(methodArgs).then((value) => { |
| 35 | + console.log('value:', value); |
| 36 | + result.success(value); |
| 37 | + }); |
| 38 | + } else { |
| 39 | + result.notImplemented(); |
| 40 | + } |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + channel.setMethodCallHandler(listener); |
| 45 | + |
| 46 | + didInit = true; |
| 47 | + } |
| 48 | +} |
| 49 | + |
7 | 50 | export class Flutter extends FlutterCommon {
|
8 | 51 | _fragment;
|
9 | 52 | _androidViewId: number = -1;
|
| 53 | + _activityCallbacks: android.app.Application.ActivityLifecycleCallbacks; |
| 54 | + |
| 55 | + static _events = fromObject({}); |
| 56 | + static get events() { |
| 57 | + return this._events; |
| 58 | + } |
| 59 | + |
10 | 60 | createNativeView(): Object {
|
11 | 61 | return new android.widget.FrameLayout(this._context);
|
12 | 62 | }
|
13 | 63 |
|
| 64 | + _channel: FlutterChannelType; |
| 65 | + |
| 66 | + //@ts-ignore |
| 67 | + get channel(): FlutterChannelType { |
| 68 | + return this._channel; |
| 69 | + } |
| 70 | + |
| 71 | + set channel(value: FlutterChannelType) { |
| 72 | + if (this._channel) { |
| 73 | + Object.keys(this._channel).forEach((key) => { |
| 74 | + channelMethods.delete(key); |
| 75 | + }); |
| 76 | + } |
| 77 | + this._channel = value; |
| 78 | + if (value) { |
| 79 | + Object.keys(value).forEach((key) => { |
| 80 | + const val = value[key]; |
| 81 | + channelMethods.set(key, val); |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + |
14 | 86 | initNativeView(): void {
|
15 | 87 | super.initNativeView();
|
16 | 88 | if (this._androidViewId < 0) {
|
17 | 89 | this._androidViewId = android.view.View.generateViewId();
|
18 | 90 | }
|
19 | 91 |
|
| 92 | + /* |
| 93 | + const ref = new WeakRef(this); |
| 94 | + Application.android.on('activityNewIntent', args =>{ |
| 95 | + this._fragment?.onNewIntent?.(args.intent) |
| 96 | + }); |
| 97 | + Application.android.on('activityRequestPermissions', args =>{ |
| 98 | + this._fragment?.onRequestPermissionsResult( |
| 99 | + args.requestCode, |
| 100 | + args.permissions, |
| 101 | + args.grantResults |
| 102 | + ); |
| 103 | + }); |
| 104 | + this._activityCallbacks = new android.app.Application.ActivityLifecycleCallbacks(<any>{ |
| 105 | + onActivityPostResumed(param0, param1?) { |
| 106 | + const owner = ref.get(); |
| 107 | + if(owner){ |
| 108 | + owner._fragment?.onPostResume?.(); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | +
|
| 113 | + (Application.android.startActivity as android.app.Activity).registerActivityLifecycleCallbacks( |
| 114 | + this._activityCallbacks |
| 115 | + ); |
| 116 | +
|
| 117 | + */ |
| 118 | + |
20 | 119 | this.nativeViewProtected.setId(this._androidViewId);
|
21 | 120 |
|
22 | 121 | const fm = this._getFragmentManager() as androidx.fragment.app.FragmentManager;
|
23 | 122 |
|
24 |
| - this._fragment = io.flutter.embedding.android.FlutterFragment.createDefault(); |
| 123 | + //this._fragment = io.flutter.embedding.android.FlutterFragment.createDefault() as any; |
| 124 | + |
| 125 | + this._fragment = io.flutter.embedding.android.FlutterFragment.withCachedEngine('default_nativescript').build() as any; |
25 | 126 |
|
26 | 127 | const name = makeFragmentName(this._androidViewId);
|
27 | 128 |
|
28 | 129 | const tr = fm.beginTransaction();
|
29 | 130 |
|
30 |
| - tr.replace(this._androidViewId, this._fragment, name); |
| 131 | + tr.replace(this._androidViewId, this._fragment as any, name); |
| 132 | + |
| 133 | + tr.commit(); |
| 134 | + } |
| 135 | + |
| 136 | + invoke(name: string, args?: Array<any>, callback?: (value?: any) => void) { |
| 137 | + if (callback) { |
| 138 | + const result = new io.flutter.plugin.common.MethodChannel.Result({ |
| 139 | + success(param0: any) {}, |
| 140 | + error(param0: string, param1: string, param2: any): void {}, |
| 141 | + notImplemented(): void {}, |
| 142 | + }); |
| 143 | + |
| 144 | + // this.notify({ |
| 145 | + // eventName: Flutter.invokeResultEvent, |
| 146 | + // object: this, |
| 147 | + // data: result, |
| 148 | + // }); |
31 | 149 |
|
32 |
| - tr.commitAllowingStateLoss(); |
| 150 | + channel.invokeMethod(name, args, result); |
| 151 | + } else { |
| 152 | + channel.invokeMethod(name, Utils.dataSerialize(args, true)); |
| 153 | + } |
33 | 154 | }
|
34 | 155 | }
|
0 commit comments