FragmentActivity代理实现? 插件化设计一 主要演示插件化来发整体架构设计(代理模式 - 静态代理)
首先分析角色: 目标接口:IPlugin目标接口 目标对象:PluginFragmentActivity(继承FragmentActivity) 代理对象:ProxyPluginFragmentActivity(自定义的FragmentActivity) 第一步:PluginFragmentActivity目标对象具体的功能复制代码
//代理实现类public class ProxyImpl { private static final String TAG = ProxyImpl.class.getSimpleName(); private String mClass; private String mPackageName; private PluginPackageInfo mPluginPackage; private PluginManager mPluginManager; private AssetManager mAssetManager; private Resources mResources; private Theme mTheme; private ActivityInfo mActivityInfo; private Activity mProxyActivity; protected IPlugin mPluginActivity; public ClassLoader mPluginClassLoader; public ProxyImpl(Activity activity) { mProxyActivity = activity; } private void initActivityInfo() { PackageInfo packageInfo = mPluginPackage.packageInfo; if ((packageInfo.activities != null) && (packageInfo.activities.length > 0)) { if (mClass == null) { mClass = packageInfo.activities[0].name; } // Finals 修复主题BUG int defaultTheme = packageInfo.applicationInfo.theme; for (ActivityInfo a : packageInfo.activities) { if (a.name.equals(mClass)) { mActivityInfo = a; // Finals ADD 修复主题没有配置的时候插件异常 if (mActivityInfo.theme == 0) { if (defaultTheme != 0) { mActivityInfo.theme = defaultTheme; } else { if (Build.VERSION.SDK_INT >= 14) { mActivityInfo.theme = android.R.style.Theme_DeviceDefault; } else { mActivityInfo.theme = android.R.style.Theme; } } } } } } } private void handleActivityInfo() { Log.d(TAG, "handleActivityInfo, theme=" + mActivityInfo.theme); if (mActivityInfo.theme > 0) { mProxyActivity.setTheme(mActivityInfo.theme); } Theme superTheme = mProxyActivity.getTheme(); mTheme = mResources.newTheme(); mTheme.setTo(superTheme); // Finals适配三星以及部分加载XML出现异常BUG try { mTheme.applyStyle(mActivityInfo.theme, true); } catch (Exception e) { e.printStackTrace(); } } public void onCreate(Intent intent) { intent.setExtrasClassLoader(PluginConfigs.sPluginClassloader); mPackageName = intent.getStringExtra(PluginConstants.EXTRA_PACKAGE); mClass = intent.getStringExtra(PluginConstants.EXTRA_CLASS); Log.d(TAG, "mClass=" + mClass + " mPackageName=" + mPackageName); mPluginManager = PluginManager.getInstance(mProxyActivity); mPluginPackage = mPluginManager.getPackage(mPackageName); mAssetManager = mPluginPackage.assetManager; mResources = mPluginPackage.resources; initActivityInfo(); handleActivityInfo(); launchTargetActivity(); } /** * 启动插件程序(实际上是启动代理对象Activity) */ protected void launchTargetActivity() { try { // 加载插件程序(说白了mClass就是PluginActivity) Class localClass = getClassLoader().loadClass(mClass); Constructor localConstructor = localClass .getConstructor(new Class[] {}); Object instance = localConstructor.newInstance(new Object[] {}); mPluginActivity = (IPlugin) instance; /** * 在调用目标对象的时候,我们的代理对象持有目标对象的引用 */ ((IAttachable) mProxyActivity).attach(mPluginActivity, mPluginManager); Log.d(TAG, "instance = " + instance); // attach the proxy activity and plugin package to the // mPluginActivity // 插件程序传入代理对象 mPluginActivity.attach(mProxyActivity, mPluginPackage); /** * 明确了PluginActivityon Create() 判断当前这个Activity是主程序的Activity。还是插件程序的Activity */ Bundle bundle = new Bundle(); bundle.putInt(PluginConstants.FROM, PluginConstants.FROM_EXTERNAL); mPluginActivity.onCreate(bundle); } catch (Exception e) { e.printStackTrace(); } } public ClassLoader getClassLoader() { return mPluginPackage.classLoader; } public AssetManager getAssets() { return mAssetManager; } public Resources getResources() { return mResources; } public Theme getTheme() { return mTheme; } public IPlugin getRemoteActivity() { return mPluginActivity; }}复制代码
Activity可以被实例化 但是系统就不会进行管理(生命周期)
第一步:PluginFragmentActivity目标对象具体的功能 第二步:ProxyPluginFragmentActivity代理对象具体功能实现
3.Apk动态解析加载流程原理分析? 第一步:主程序加载插件程序 第二步:主程序启动插件流程分析