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

Bundle间通信RemoteTransactor

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

定义PreviewRemote实现IRemote接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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

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

Bundle间远程调用PreviewRemote

1
2
3
4
5
6
7
8
9
10
11
12
13
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而直接加载,则会报错

1
2
3
4
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);
}
});
}
});
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :