Atlas通过RemoteTransactor加载并运行远程Bundle

Bundle间通信RemoteTransactor

以调用图片及视频预览插件为例

定义PreviewRemote实现IRemote接口

public class PreviewRemote implements IRemote {

    public PreviewRemote() {
        RuntimeVariables.androidApplication.registerComponentCallbacks(new ComponentCallbacks2() {
            @Override
            public void onTrimMemory(int level) {
                if (level == ContentProvider.TRIM_MEMORY_UI_HIDDEN) {
                    HostTransactor remote = HostTransactor.get(PreviewRemote.this);
                    if (remote != null) {
                        remote.call("SLEEP_NOTIFY", null, null);
                    }
                }
            }

            @Override
            public void onConfigurationChanged(Configuration configuration) {

            }

            @Override
            public void onLowMemory() {

            }
        });
    }

    @Override
    public Bundle call(String commandName, Bundle bundle, IResponse callback) {
        NLog.i(Constants.TAG, "PreviewRemote.call");
        return null;
    }

    @Override
    public <T> T getRemoteInterface(Class<T> clazz, Bundle bundle) {
        NLog.i(Constants.TAG, "PreviewRemote.getRemoteInterface");
        if (clazz == IPreviewBridge.class) {
            T instance = (T) new IPreviewBridge() {
                @Override
                public void startImagePreview(Activity activity, @NonNull List imgUrls) {
                    GPreviewBuilder.from(activity)
                            .setData(imgUrls)
                            .setCurrentIndex(0)
                            .setSingleFling(true)
                            .setType(GPreviewBuilder.IndicatorType.Dot)
                            .start();
                }

                @Override
                public void startImagePreview(Fragment fragment, @NonNull List imgUrls) {
                    GPreviewBuilder.from(fragment)
                            .setData(imgUrls)
                            .setCurrentIndex(0)
                            .setSingleFling(true)
                            .setType(GPreviewBuilder.IndicatorType.Dot)
                            .start();
                }
            };
            return instance;
        }
        return null;
    }
}

注册PreviewRemote

<meta-data android:name="atlas.transaction.intent.action.START_PREVIEW" android:value="me.yezhou.lib.preview.remote.PreviewRemote"/>

Bundle间远程调用PreviewRemote

RemoteFactory.requestRemote(RemoteTransactor.class, activity, new Intent("atlas.transaction.intent.action.START_PREVIEW"),
        new RemoteFactory.OnRemoteStateListener<RemoteTransactor>() {
            @Override
            public void onRemotePrepared(RemoteTransactor iRemote) {
                IPreviewBridge<IPreviewInfo> previewBridge = iRemote.getRemoteInterface(IPreviewBridge.class,null);
                previewBridge.startImagePreview(activity, previewInfoList);
            }

            @Override
            public void onFailed(String s) {
                NLog.e(Constants.TAG, s);
            }
        });

加载并运行远程Bundle

Bundle间通过RemoteTransactor通信已经实现,但是如果未安装远程Bundle而直接加载,则会报错

E/BundleInstaller: me.yezhou.lib.preview-->[me.yezhou.lib.preview], thread=Thread[bundle_installer,5,main]
E/Framework: restoreExisted: me.yezhou.lib.preview| 3dfyw7sljnuzq| null
E/BundleInstaller: notify finish [me.yezhou.lib.preview]
E/yezhou: install bundle failed: me.yezhou.lib.preview

因此,首先需要加载并运行远程Bundle

  1. 触发加载远程Bundle,若Bundle不存在导致加载失败,则提交服务器请求远程Bundle下载
  2. 远程Bundle下载完成,立即安装
  3. 再次触发加载并运行Bundle
public static void startPreview(final Activity activity, final List<IPreviewInfo> previewInfoList) {
    RemoteFactory.requestRemote(RemoteTransactor.class, activity, new Intent("atlas.transaction.intent.action.START_PREVIEW"),
            new RemoteFactory.OnRemoteStateListener<RemoteTransactor>() {
                @Override
                public void onRemotePrepared(RemoteTransactor iRemote) {
                    IPreviewBridge<IPreviewInfo> previewBridge = iRemote.getRemoteInterface(IPreviewBridge.class,null);
                    previewBridge.startImagePreview(activity, previewInfoList);
                }

                @Override
                public void onFailed(String s) {
                    NLog.e(Constants.TAG, s);
                    AppBundleHelper.bundleExplicitly(activity, "preview", "me.yezhou.lib", new AtlasBundleLoadedListener() {
                        @Override
                        public void onBundleLoaded() {
                            startPreview(activity, previewInfoList);
                        }
                    });
                }
            });
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/atlas-loads-and-runs-remote-bundles-through-remotetransactor/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Atlas通过RemoteTransactor加载并运行远程Bundle
Bundle间通信RemoteTransactor 以调用图片及视频预览插件为例 定义PreviewRemote实现IRemote接口 public class PreviewRemote implements IRemote { pub……
<<上一篇
下一篇>>
文章目录
关闭
目 录