From effce2e720c695cd9b337f6211b510f16f3d57e3 Mon Sep 17 00:00:00 2001 From: MrSimple <bboyfeiyu@gmail.com> Date: Wed, 6 May 2015 18:09:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=87=8D=E6=9E=84Proxy=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ryg/dynamicload/DLBasePluginActivity.java | 66 ++++----- .../DLBasePluginFragmentActivity.java | 22 +-- .../ryg/dynamicload/DLBasePluginService.java | 13 +- .../lib/src/com/ryg/dynamicload/DLPlugin.java | 5 +- .../com/ryg/dynamicload/DLProxyActivity.java | 12 +- .../dynamicload/DLProxyFragmentActivity.java | 11 +- .../com/ryg/dynamicload/DLProxyService.java | 75 +++++------ .../dynamicload/internal/DLAttachable.java | 21 ++- .../internal/DLServiceAttachable.java | 29 ---- .../internal/DLServiceProxyImpl.java | 57 -------- .../DLActivityProxy.java} | 82 +++--------- .../ryg/dynamicload/proxy/DLBaseProxy.java | 125 ++++++++++++++++++ .../ryg/dynamicload/proxy/DLServiceProxy.java | 25 ++++ DynamicLoadApk/lib/src/com/ryg/utils/LOG.java | 1 - 14 files changed, 275 insertions(+), 269 deletions(-) delete mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceAttachable.java delete mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceProxyImpl.java rename DynamicLoadApk/lib/src/com/ryg/dynamicload/{internal/DLProxyImpl.java => proxy/DLActivityProxy.java} (59%) create mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLBaseProxy.java create mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java index 926c68a..31158dc 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java @@ -89,7 +89,8 @@ public void onCreate(Bundle savedInstanceState) { mPluginManager = DLPluginManager.getInstance(that); Log.d(TAG, "onCreate: from= " - + (mFrom == DLConstants.FROM_INTERNAL ? "DLConstants.FROM_INTERNAL" : "FROM_EXTERNAL")); + + (mFrom == DLConstants.FROM_INTERNAL ? "DLConstants.FROM_INTERNAL" + : "FROM_EXTERNAL")); } @Override @@ -385,7 +386,7 @@ public int startPluginActivityForResult(DLIntent dlIntent, int requestCode) { } return mPluginManager.startPluginActivityForResult(that, dlIntent, requestCode); } - + public int startPluginService(DLIntent dlIntent) { if (mFrom == DLConstants.FROM_EXTERNAL) { if (dlIntent.getPluginPackage() == null) { @@ -394,7 +395,7 @@ public int startPluginService(DLIntent dlIntent) { } return mPluginManager.startPluginService(that, dlIntent); } - + public int bindPluginService(DLIntent dlIntent, ServiceConnection conn, int flags) { if (mFrom == DLConstants.FROM_EXTERNAL) { if (dlIntent.getPluginPackage() == null) { @@ -403,41 +404,42 @@ public int bindPluginService(DLIntent dlIntent, ServiceConnection conn, int flag } return mPluginManager.bindPluginService(that, dlIntent, conn, flags); } - + public int unBindPluginService(DLIntent dlIntent, ServiceConnection conn) { if (mFrom == DLConstants.FROM_EXTERNAL) { if (dlIntent.getPluginPackage() == null) - dlIntent.setPluginPackage(mPluginPackage.packageName); + dlIntent.setPluginPackage(mPluginPackage.packageName); } return mPluginManager.unBindPluginService(that, dlIntent, conn); } -// /** -// * 直接调用that.startService -// * that 可能有两种情况 -// * 1.指向this -// * 2.指向DLProxyActivity -// */ -// public ComponentName startService(Intent service) { -// return that.startService(service); -// } -// -// @Override -// public boolean stopService(Intent name) { -// // TODO Auto-generated method stub -// return super.stopService(name); -// } -// -// @Override -// public boolean bindService(Intent service, ServiceConnection conn, int flags) { -// // TODO Auto-generated method stub -// return super.bindService(service, conn, flags); -// } -// -// @Override -// public void unbindService(ServiceConnection conn) { -// // TODO Auto-generated method stub -// super.unbindService(conn); -// } + // /** + // * 直接调用that.startService + // * that 可能有两种情况 + // * 1.指向this + // * 2.指向DLProxyActivity + // */ + // public ComponentName startService(Intent service) { + // return that.startService(service); + // } + // + // @Override + // public boolean stopService(Intent name) { + // // TODO Auto-generated method stub + // return super.stopService(name); + // } + // + // @Override + // public boolean bindService(Intent service, ServiceConnection conn, int + // flags) { + // // TODO Auto-generated method stub + // return super.bindService(service, conn, flags); + // } + // + // @Override + // public void unbindService(ServiceConnection conn) { + // // TODO Auto-generated method stub + // super.unbindService(conn); + // } } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java index 3b4357d..03e8c6e 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java @@ -15,6 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.ryg.dynamicload; import android.app.Activity; @@ -83,7 +84,8 @@ public void onCreate(Bundle savedInstanceState) { mPluginManager = DLPluginManager.getInstance(that); Log.d(TAG, "onCreate: from= " - + (mFrom == DLConstants.FROM_INTERNAL ? "DLConstants.FROM_INTERNAL" : "FROM_EXTERNAL")); + + (mFrom == DLConstants.FROM_INTERNAL ? "DLConstants.FROM_INTERNAL" + : "FROM_EXTERNAL")); } @Override @@ -357,8 +359,9 @@ public boolean onOptionsItemSelected(MenuItem item) { /** * @param dlIntent - * @return may be {@link #START_RESULT_SUCCESS}, {@link #START_RESULT_NO_PKG}, - * {@link #START_RESULT_NO_CLASS}, {@link #START_RESULT_TYPE_ERROR} + * @return may be {@link #START_RESULT_SUCCESS}, + * {@link #START_RESULT_NO_PKG}, {@link #START_RESULT_NO_CLASS}, + * {@link #START_RESULT_TYPE_ERROR} */ public int startPluginActivity(DLIntent dlIntent) { return startPluginActivityForResult(dlIntent, -1); @@ -366,8 +369,9 @@ public int startPluginActivity(DLIntent dlIntent) { /** * @param dlIntent - * @return may be {@link #START_RESULT_SUCCESS}, {@link #START_RESULT_NO_PKG}, - * {@link #START_RESULT_NO_CLASS}, {@link #START_RESULT_TYPE_ERROR} + * @return may be {@link #START_RESULT_SUCCESS}, + * {@link #START_RESULT_NO_PKG}, {@link #START_RESULT_NO_CLASS}, + * {@link #START_RESULT_TYPE_ERROR} */ public int startPluginActivityForResult(DLIntent dlIntent, int requestCode) { if (mFrom == DLConstants.FROM_EXTERNAL) { @@ -377,7 +381,7 @@ public int startPluginActivityForResult(DLIntent dlIntent, int requestCode) { } return mPluginManager.startPluginActivityForResult(that, dlIntent, requestCode); } - + public int startPluginService(DLIntent dlIntent) { if (mFrom == DLConstants.FROM_EXTERNAL) { if (dlIntent.getPluginPackage() == null) { @@ -386,7 +390,7 @@ public int startPluginService(DLIntent dlIntent) { } return mPluginManager.startPluginService(that, dlIntent); } - + public int bindPluginService(DLIntent dlIntent, ServiceConnection conn, int flags) { if (mFrom == DLConstants.FROM_EXTERNAL) { if (dlIntent.getPluginPackage() == null) { @@ -395,11 +399,11 @@ public int bindPluginService(DLIntent dlIntent, ServiceConnection conn, int flag } return mPluginManager.bindPluginService(that, dlIntent, conn, flags); } - + public int unBindPluginService(DLIntent dlIntent, ServiceConnection conn) { if (mFrom == DLConstants.FROM_EXTERNAL) { if (dlIntent.getPluginPackage() == null) - dlIntent.setPluginPackage(mPluginPackage.packageName); + dlIntent.setPluginPackage(mPluginPackage.packageName); } return mPluginManager.unBindPluginService(that, dlIntent, conn); } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java index eaad418..8b85970 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java @@ -31,13 +31,12 @@ public class DLBasePluginService extends Service implements DLServicePlugin { public static final String TAG = "DLBasePluginService"; private Service mProxyService; - private DLPluginPackage mPluginPackage; + protected DLPluginPackage mPluginPackage; protected Service that = this; protected int mFrom = DLConstants.FROM_INTERNAL; @Override public void attach(Service proxyService, DLPluginPackage pluginPackage) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " attach"); mProxyService = proxyService; mPluginPackage = pluginPackage; @@ -51,65 +50,55 @@ protected boolean isInternalCall() { @Override public IBinder onBind(Intent intent) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onBind"); return null; } @Override public void onCreate() { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onStartCommand"); return 0; } @Override public void onDestroy() { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onDestroy"); } @Override public void onConfigurationChanged(Configuration newConfig) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onConfigurationChanged"); } @Override public void onLowMemory() { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onLowMemory"); } @Override public void onTrimMemory(int level) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onTrimMemory"); } @Override public boolean onUnbind(Intent intent) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onUnbind"); return false; } @Override public void onRebind(Intent intent) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onRebind"); } @Override public void onTaskRemoved(Intent rootIntent) { - // TODO Auto-generated method stub LOG.d(TAG, TAG + " onTaskRemoved"); } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLPlugin.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLPlugin.java index e5458eb..47b9333 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLPlugin.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLPlugin.java @@ -26,9 +26,9 @@ import android.view.MotionEvent; import android.view.WindowManager.LayoutParams; -import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.dynamicload.internal.DLAttachable; -public interface DLPlugin { +public interface DLPlugin extends DLAttachable<Activity>{ public void onCreate(Bundle savedInstanceState); public void onStart(); @@ -38,7 +38,6 @@ public interface DLPlugin { public void onPause(); public void onStop(); public void onDestroy(); - public void attach(Activity proxyActivity, DLPluginPackage pluginPackage); public void onSaveInstanceState(Bundle outState); public void onNewIntent(Intent intent); public void onRestoreInstanceState(Bundle savedInstanceState); diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java index 92d8814..255a64a 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java @@ -33,12 +33,14 @@ import com.ryg.dynamicload.internal.DLAttachable; import com.ryg.dynamicload.internal.DLPluginManager; -import com.ryg.dynamicload.internal.DLProxyImpl; +import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.dynamicload.proxy.DLActivityProxy; -public class DLProxyActivity extends Activity implements DLAttachable { +public class DLProxyActivity extends Activity + implements DLAttachable<DLPlugin> { protected DLPlugin mRemoteActivity; - private DLProxyImpl impl = new DLProxyImpl(this); + private DLActivityProxy impl = new DLActivityProxy(this); @Override protected void onCreate(Bundle savedInstanceState) { @@ -47,7 +49,7 @@ protected void onCreate(Bundle savedInstanceState) { } @Override - public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager) { + public void attach(DLPlugin remoteActivity, DLPluginPackage pluginPackage) { mRemoteActivity = remoteActivity; } @@ -172,7 +174,7 @@ public boolean onOptionsItemSelected(MenuItem item) { mRemoteActivity.onOptionsItemSelected(item); return super.onOptionsItemSelected(item); } - + @Override public ComponentName startService(Intent service) { return super.startService(service); diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyFragmentActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyFragmentActivity.java index 1e77cf8..5762dc3 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyFragmentActivity.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyFragmentActivity.java @@ -31,13 +31,14 @@ import android.view.WindowManager.LayoutParams; import com.ryg.dynamicload.internal.DLAttachable; -import com.ryg.dynamicload.internal.DLPluginManager; -import com.ryg.dynamicload.internal.DLProxyImpl; +import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.dynamicload.proxy.DLActivityProxy; -public class DLProxyFragmentActivity extends FragmentActivity implements DLAttachable { +public class DLProxyFragmentActivity extends FragmentActivity + implements DLAttachable<DLPlugin> { protected DLPlugin mRemoteActivity; - private DLProxyImpl impl = new DLProxyImpl(this); + private DLActivityProxy impl = new DLActivityProxy(this); @Override protected void onCreate(Bundle savedInstanceState) { @@ -46,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) { } @Override - public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager) { + public void attach(DLPlugin remoteActivity, DLPluginPackage pluginPackage) { mRemoteActivity = remoteActivity; } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java index 186d4d8..99f2abc 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java @@ -1,8 +1,5 @@ -package com.ryg.dynamicload; -import com.ryg.dynamicload.internal.DLPluginManager; -import com.ryg.dynamicload.internal.DLServiceAttachable; -import com.ryg.dynamicload.internal.DLServiceProxyImpl; +package com.ryg.dynamicload; import android.annotation.TargetApi; import android.app.Service; @@ -12,114 +9,104 @@ import android.os.IBinder; import android.util.Log; -public class DLProxyService extends Service implements DLServiceAttachable{ - +import com.ryg.dynamicload.internal.DLAttachable; +import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.dynamicload.proxy.DLServiceProxy; + +public class DLProxyService extends Service implements DLAttachable<DLServicePlugin> { + private static final String TAG = "DLProxyService"; - private DLServiceProxyImpl mImpl = new DLServiceProxyImpl(this); + private DLServiceProxy mImpl = new DLServiceProxy(this); private DLServicePlugin mRemoteService; - private DLPluginManager mPluginManager; - + @Override public IBinder onBind(Intent intent) { - // TODO Auto-generated method stub Log.d(TAG, TAG + " onBind"); - //判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 + // 判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 if (mRemoteService == null) { - mImpl.init(intent); + mImpl.onCreate(intent); } return mRemoteService.onBind(intent); } @Override public void onCreate() { - // TODO Auto-generated method stub super.onCreate(); Log.d(TAG, TAG + " onCreate"); - } - -// @Override -// public void onStart(Intent intent, int startId) { -// // TODO Auto-generated method stub -// super.onStart(intent, startId); -// Log.d(TAG, TAG + " onStart"); -// -// } - + } + + // @Override + // public void onStart(Intent intent, int startId) { + // // TODO Auto-generated method stub + // super.onStart(intent, startId); + // Log.d(TAG, TAG + " onStart"); + // + // } + @Override public int onStartCommand(Intent intent, int flags, int startId) { - // TODO Auto-generated method stub Log.d(TAG, TAG + " onStartCommand " + this.toString()); - //判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 + // 判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 if (mRemoteService == null) { - mImpl.init(intent); + mImpl.onCreate(intent); } super.onStartCommand(intent, flags, startId); return mRemoteService.onStartCommand(intent, flags, startId); -// return super.onStartCommand(intent, flags, startId); } - + @Override public void onDestroy() { - // TODO Auto-generated method stub mRemoteService.onDestroy(); super.onDestroy(); Log.d(TAG, TAG + " onDestroy"); } - + @Override public void onConfigurationChanged(Configuration newConfig) { - // TODO Auto-generated method stub mRemoteService.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig); Log.d(TAG, TAG + " onConfigurationChanged"); } - + @Override public void onLowMemory() { - // TODO Auto-generated method stub mRemoteService.onLowMemory(); super.onLowMemory(); Log.d(TAG, TAG + " onLowMemory"); } - + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onTrimMemory(int level) { - // TODO Auto-generated method stub mRemoteService.onTrimMemory(level); super.onTrimMemory(level); Log.d(TAG, TAG + " onTrimMemory"); } - + @Override public boolean onUnbind(Intent intent) { - // TODO Auto-generated method stub Log.d(TAG, TAG + " onUnbind"); super.onUnbind(intent); return mRemoteService.onUnbind(intent); } - + @Override public void onRebind(Intent intent) { - // TODO Auto-generated method stub mRemoteService.onRebind(intent); super.onRebind(intent); Log.d(TAG, TAG + " onRebind"); } - + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onTaskRemoved(Intent rootIntent) { - // TODO Auto-generated method stub mRemoteService.onTaskRemoved(rootIntent); super.onTaskRemoved(rootIntent); Log.d(TAG, TAG + " onTaskRemoved"); } @Override - public void attach(DLServicePlugin remoteService, DLPluginManager pluginManager) { - // TODO Auto-generated method stub + public void attach(DLServicePlugin remoteService, DLPluginPackage pluginPackage) { mRemoteService = remoteService; - mPluginManager = pluginManager; } } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLAttachable.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLAttachable.java index 8457f20..d88a8a5 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLAttachable.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLAttachable.java @@ -18,22 +18,21 @@ package com.ryg.dynamicload.internal; -import com.ryg.dynamicload.DLPlugin; - /** - * @author mrsimple + * 这是一个通用的attach接口,所有的组件都使用该接口即可 */ -public interface DLAttachable { +public interface DLAttachable<T> { /** * when the proxy impl ( {@see DLProxyImpl#launchTargetActivity()} ) launch - * the plugin activity , dl will call this method to attach the proxy activity - * and pluginManager to the plugin activity. the proxy activity will load - * the plugin's resource, so the proxy activity is a resource delegate for - * plugin activity. + * the plugin activity , dl will call this method to attach the proxy + * activity and pluginManager to the plugin activity. the proxy activity + * will load the plugin's resource, so the proxy activity is a resource + * delegate for plugin activity. * - * @param proxyActivity a instance of DLPlugin, {@see DLBasePluginActivity} - * and {@see DLBasePluginFragmentActivity} + * @param proxyComponent a instance of DLPlugin, {@see DLBasePluginActivity} + * and {@see DLBasePluginFragmentActivity}, {@see + * DLBasePluginService} * @param pluginManager DLPluginManager instance, manager the plugins */ - public void attach(DLPlugin proxyActivity, DLPluginManager pluginManager); + public void attach(T proxyComponent, DLPluginPackage pluginManager); } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceAttachable.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceAttachable.java deleted file mode 100644 index b14ca72..0000000 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceAttachable.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2014 singwhatiwanna(任玉刚) <singwhatiwanna@gmail.com> - * - * collaborator:田啸,宋思宇,Mr.Simple - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.ryg.dynamicload.internal; - -import com.ryg.dynamicload.DLServicePlugin; - -/** - * @author mrsimple - */ -public interface DLServiceAttachable { - - public void attach(DLServicePlugin remoteService, DLPluginManager pluginManager); -} diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceProxyImpl.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceProxyImpl.java deleted file mode 100644 index c90de93..0000000 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServiceProxyImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.ryg.dynamicload.internal; - -import java.lang.reflect.Constructor; - -import com.ryg.dynamicload.DLServicePlugin; -import com.ryg.utils.DLConfigs; -import com.ryg.utils.DLConstants; - -import android.annotation.TargetApi; -import android.app.Service; -import android.content.Intent; -import android.os.Build; -import android.os.Bundle; -import android.util.Log; - -public class DLServiceProxyImpl { - - private static final String TAG = "DLServiceProxyImpl"; - private Service mProxyService; - private DLServicePlugin mRemoteService; - - public DLServiceProxyImpl(Service service) { - mProxyService = service; - } - - @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) - public void init(Intent intent) { - // set the extra's class loader - intent.setExtrasClassLoader(DLConfigs.sPluginClassloader); - - String packageName = intent.getStringExtra(DLConstants.EXTRA_PACKAGE); - String clazz = intent.getStringExtra(DLConstants.EXTRA_CLASS); - Log.d(TAG, "clazz=" + clazz + " packageName=" + packageName); - - DLPluginManager pluginManager = DLPluginManager.getInstance(mProxyService); - DLPluginPackage pluginPackage = pluginManager.getPackage(packageName); - - try { - Class<?> localClass = pluginPackage.classLoader.loadClass(clazz); - Constructor<?> localConstructor = localClass.getConstructor(new Class[] {}); - Object instance = localConstructor.newInstance(new Object[] {}); - mRemoteService = (DLServicePlugin) instance; - ((DLServiceAttachable) mProxyService).attach(mRemoteService, pluginManager); - Log.d(TAG, "instance = " + instance); - // attach the proxy activity and plugin package to the - // mPluginActivity - mRemoteService.attach(mProxyService, pluginPackage); - - Bundle bundle = new Bundle(); - bundle.putInt(DLConstants.FROM, DLConstants.FROM_EXTERNAL); - mRemoteService.onCreate(); - } catch (Exception e) { - e.printStackTrace(); - } - } - -} diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLProxyImpl.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLActivityProxy.java similarity index 59% rename from DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLProxyImpl.java rename to DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLActivityProxy.java index ffe5468..a28a37b 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLProxyImpl.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLActivityProxy.java @@ -16,9 +16,8 @@ * limitations under the License. */ -package com.ryg.dynamicload.internal; +package com.ryg.dynamicload.proxy; -import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; @@ -31,10 +30,6 @@ import android.util.Log; import com.ryg.dynamicload.DLPlugin; -import com.ryg.utils.DLConfigs; -import com.ryg.utils.DLConstants; - -import java.lang.reflect.Constructor; /** * This is a plugin activity proxy, the proxy will create the plugin activity @@ -43,40 +38,30 @@ * * @author mrsimple */ -public class DLProxyImpl { - - private static final String TAG = "DLProxyImpl"; - - private String mClass; - private String mPackageName; - - private DLPluginPackage mPluginPackage; - private DLPluginManager mPluginManager; +public class DLActivityProxy extends DLBaseProxy<Activity, DLPlugin> { private AssetManager mAssetManager; private Resources mResources; private Theme mTheme; private ActivityInfo mActivityInfo; - private Activity mProxyActivity; - protected DLPlugin mPluginActivity; - public ClassLoader mPluginClassLoader; +// public ClassLoader mPluginClassLoader; - public DLProxyImpl(Activity activity) { - mProxyActivity = activity; + public DLActivityProxy(Activity activity) { + mProxyComponent = activity; } private void initializeActivityInfo() { PackageInfo packageInfo = mPluginPackage.packageInfo; if ((packageInfo.activities != null) && (packageInfo.activities.length > 0)) { - if (mClass == null) { - mClass = packageInfo.activities[0].name; + if (mPluginClazz == null) { + mPluginClazz = packageInfo.activities[0].name; } - //Finals 修复主题BUG + // Finals 修复主题BUG int defaultTheme = packageInfo.applicationInfo.theme; for (ActivityInfo a : packageInfo.activities) { - if (a.name.equals(mClass)) { + if (a.name.equals(mPluginClazz)) { mActivityInfo = a; // Finals ADD 修复主题没有配置的时候插件异常 if (mActivityInfo.theme == 0) { @@ -99,9 +84,10 @@ private void initializeActivityInfo() { private void handleActivityInfo() { Log.d(TAG, "handleActivityInfo, theme=" + mActivityInfo.theme); if (mActivityInfo.theme > 0) { - mProxyActivity.setTheme(mActivityInfo.theme); + mProxyComponent.setTheme(mActivityInfo.theme); } - Theme superTheme = mProxyActivity.getTheme(); + + Theme superTheme = mProxyComponent.getTheme(); mTheme = mResources.newTheme(); mTheme.setTo(superTheme); // Finals适配三星以及部分加载XML出现异常BUG @@ -110,47 +96,16 @@ private void handleActivityInfo() { } catch (Exception e) { e.printStackTrace(); } - // TODO: handle mActivityInfo.launchMode here in the future. } - public void onCreate(Intent intent) { - - // set the extra's class loader - intent.setExtrasClassLoader(DLConfigs.sPluginClassloader); - - mPackageName = intent.getStringExtra(DLConstants.EXTRA_PACKAGE); - mClass = intent.getStringExtra(DLConstants.EXTRA_CLASS); - Log.d(TAG, "mClass=" + mClass + " mPackageName=" + mPackageName); - - mPluginManager = DLPluginManager.getInstance(mProxyActivity); - mPluginPackage = mPluginManager.getPackage(mPackageName); + @Override + protected void init(Intent intent) { + super.init(intent); mAssetManager = mPluginPackage.assetManager; mResources = mPluginPackage.resources; - initializeActivityInfo(); handleActivityInfo(); - launchTargetActivity(); - } - - @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) - protected void launchTargetActivity() { - try { - Class<?> localClass = getClassLoader().loadClass(mClass); - Constructor<?> localConstructor = localClass.getConstructor(new Class[] {}); - Object instance = localConstructor.newInstance(new Object[] {}); - mPluginActivity = (DLPlugin) instance; - ((DLAttachable) mProxyActivity).attach(mPluginActivity, mPluginManager); - Log.d(TAG, "instance = " + instance); - // attach the proxy activity and plugin package to the mPluginActivity - mPluginActivity.attach(mProxyActivity, mPluginPackage); - - Bundle bundle = new Bundle(); - bundle.putInt(DLConstants.FROM, DLConstants.FROM_EXTERNAL); - mPluginActivity.onCreate(bundle); - } catch (Exception e) { - e.printStackTrace(); - } } public ClassLoader getClassLoader() { @@ -170,6 +125,11 @@ public Theme getTheme() { } public DLPlugin getRemoteActivity() { - return mPluginActivity; + return mPlugin; + } + + @Override + protected void callPluginOnCreate(Bundle bundle) { + mPlugin.onCreate(bundle); } } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLBaseProxy.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLBaseProxy.java new file mode 100644 index 0000000..e17d7af --- /dev/null +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLBaseProxy.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2014 singwhatiwanna(任玉刚) <singwhatiwanna@gmail.com> + * + * collaborator:田啸,宋思宇,Mr.Simple + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ryg.dynamicload.proxy; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +import com.ryg.dynamicload.internal.DLAttachable; +import com.ryg.dynamicload.internal.DLPluginManager; +import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.utils.DLConfigs; +import com.ryg.utils.DLConstants; + +import java.lang.reflect.Constructor; + +/** + * 插件代理类基类 + * + * @author mrsimple + * @param <P> 组件代理类型,例如DLProxyActivity,DLProxyService等 + * @param <T> Plugin类型,例如{@see DLPlugin},{@see DLServicePlugin} + */ +@SuppressWarnings("unchecked") +public abstract class DLBaseProxy<P extends Context, T> { + + /** + * 组件类型,例如Service,Activity等 + */ + protected P mProxyComponent; + + /** + * 插件组件,例如DLBasePluginActivity的子类、DLBasePluginService子类等 + */ + protected T mPlugin; + + // 插件的包名 + protected String mPackageName; + // 插件的Service的类名 + protected String mPluginClazz; + // 插件管理器 + protected DLPluginManager mPluginManager; + // 插件DLPluginPackage + protected DLPluginPackage mPluginPackage; + // Log's TAG + protected final String TAG = this.getClass().getName(); + + /** + * 创建组件,并且调用组件的onCreate方法 + * + * @param intent + */ + public final void onCreate(Intent intent) { + try { + // 1、初始化插件包名、类名等属性 + init(intent); + // 2、反射构造插件对象 + mPlugin = createPlugin(mPluginPackage.classLoader); + // 3、调用插件代理对象的attach方法,将插件注入到代理对象中 + attachPluginToProxy(); + // 4、调用插件的attach、onCreate方法启动插件 + callOnCreate(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + protected void init(Intent intent) { + // set the extra's class loader + intent.setExtrasClassLoader(DLConfigs.sPluginClassloader); + // 插件的包名 + mPackageName = intent.getStringExtra(DLConstants.EXTRA_PACKAGE); + // 插件的Service的类名 + mPluginClazz = intent.getStringExtra(DLConstants.EXTRA_CLASS); + mPluginManager = DLPluginManager.getInstance(mProxyComponent); + mPluginPackage = mPluginManager.getPackage(mPackageName); + } + + protected T createPlugin(ClassLoader classLoader) + throws Exception { + Log.d(TAG, "clazz=" + mPluginClazz + " packageName=" + mPackageName); + Class<?> localClass = classLoader.loadClass(mPluginClazz); + Constructor<?> localConstructor = localClass.getConstructor(new Class[] {}); + return (T) localConstructor.newInstance(new Object[] {}); + } + + /** + * 调用代理对象的attach + */ + private void attachPluginToProxy() { + ((DLAttachable<T>) mProxyComponent).attach(mPlugin, mPluginPackage); + } + + /** + * 调用插件的attach和onCreate函数,启动插件 + */ + private void callOnCreate() { + // 调用插件的attach + ((DLAttachable<P>) mPlugin).attach(mProxyComponent, mPluginPackage); + Bundle bundle = new Bundle(); + bundle.putInt(DLConstants.FROM, DLConstants.FROM_EXTERNAL); + // 调用插件的onCreate + callPluginOnCreate(bundle); + } + + protected abstract void callPluginOnCreate(Bundle bundle); + +} diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java new file mode 100644 index 0000000..aac43f7 --- /dev/null +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java @@ -0,0 +1,25 @@ + +package com.ryg.dynamicload.proxy; + +import android.app.Service; +import android.os.Bundle; + +import com.ryg.dynamicload.DLServicePlugin; + +/** + * 服务代理类 + * + * @author mrsimple + */ +public class DLServiceProxy extends DLBaseProxy<Service, DLServicePlugin> { + + public DLServiceProxy(Service service) { + mProxyComponent = service; + } + + @Override + protected void callPluginOnCreate(Bundle dBundle) { + mPlugin.onCreate(); + } + +} diff --git a/DynamicLoadApk/lib/src/com/ryg/utils/LOG.java b/DynamicLoadApk/lib/src/com/ryg/utils/LOG.java index b961621..743392c 100644 --- a/DynamicLoadApk/lib/src/com/ryg/utils/LOG.java +++ b/DynamicLoadApk/lib/src/com/ryg/utils/LOG.java @@ -1,6 +1,5 @@ package com.ryg.utils; -import android.os.Debug; import android.util.Log; public class LOG { From 25633320ff15f0501b69c14299d760859302c854 Mon Sep 17 00:00:00 2001 From: MrSimple <bboyfeiyu@gmail.com> Date: Thu, 7 May 2015 14:44:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=88=86=E5=8C=85?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E6=9F=90=E4=BA=9B=E7=B1=BB=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E7=BB=93=E6=9E=84=E6=9B=B4=E6=B8=85=E6=99=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ryg/dynamicload/DLBasePluginActivity.java | 3 +- .../DLBasePluginFragmentActivity.java | 3 +- .../ryg/dynamicload/DLBasePluginService.java | 1 + .../com/ryg/dynamicload/DLProxyActivity.java | 183 -------------- .../com/ryg/dynamicload/DLProxyService.java | 112 --------- .../DLActivityPlugin.java} | 6 +- .../dynamicload/internal/DLPluginManager.java | 12 +- .../{ => internal}/DLServicePlugin.java | 4 +- .../dynamicload/loader/DLActivityLoader.java | 134 +++++++++++ .../DLBaseLoader.java} | 13 +- .../dynamicload/loader/DLServiceLoader.java | 25 ++ .../dynamicload/proxy/DLActivityProxy.java | 227 +++++++++++------- .../DLFragmentActivityProxy.java} | 15 +- .../ryg/dynamicload/proxy/DLServiceProxy.java | 112 ++++++++- .../sample/main/main-host/AndroidManifest.xml | 6 +- 15 files changed, 431 insertions(+), 425 deletions(-) delete mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java delete mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java rename DynamicLoadApk/lib/src/com/ryg/dynamicload/{DLPlugin.java => internal/DLActivityPlugin.java} (92%) rename DynamicLoadApk/lib/src/com/ryg/dynamicload/{ => internal}/DLServicePlugin.java (90%) create mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLActivityLoader.java rename DynamicLoadApk/lib/src/com/ryg/dynamicload/{proxy/DLBaseProxy.java => loader/DLBaseLoader.java} (91%) create mode 100644 DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLServiceLoader.java rename DynamicLoadApk/lib/src/com/ryg/dynamicload/{DLProxyFragmentActivity.java => proxy/DLFragmentActivityProxy.java} (90%) diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java index 31158dc..b247f33 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginActivity.java @@ -38,6 +38,7 @@ import android.view.Window; import android.view.WindowManager; +import com.ryg.dynamicload.internal.DLActivityPlugin; import com.ryg.dynamicload.internal.DLIntent; import com.ryg.dynamicload.internal.DLPluginManager; import com.ryg.dynamicload.internal.DLPluginPackage; @@ -49,7 +50,7 @@ * @see {@link DLBasePluginActivity.that} * @author renyugang */ -public class DLBasePluginActivity extends Activity implements DLPlugin { +public class DLBasePluginActivity extends Activity implements DLActivityPlugin { private static final String TAG = "DLBasePluginActivity"; diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java index 03e8c6e..9d35717 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginFragmentActivity.java @@ -40,12 +40,13 @@ import android.view.Window; import android.view.WindowManager; +import com.ryg.dynamicload.internal.DLActivityPlugin; import com.ryg.dynamicload.internal.DLIntent; import com.ryg.dynamicload.internal.DLPluginManager; import com.ryg.dynamicload.internal.DLPluginPackage; import com.ryg.utils.DLConstants; -public class DLBasePluginFragmentActivity extends FragmentActivity implements DLPlugin { +public class DLBasePluginFragmentActivity extends FragmentActivity implements DLActivityPlugin { private static final String TAG = "DLBasePluginFragmentActivity"; diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java index 8b85970..51255db 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLBasePluginService.java @@ -24,6 +24,7 @@ import android.os.IBinder; import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.dynamicload.internal.DLServicePlugin; import com.ryg.utils.DLConstants; import com.ryg.utils.LOG; diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java deleted file mode 100644 index 255a64a..0000000 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyActivity.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright (C) 2014 singwhatiwanna(任玉刚) <singwhatiwanna@gmail.com> - * - * collaborator:田啸,宋思宇,Mr.Simple - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.ryg.dynamicload; - -import android.app.Activity; -import android.content.ComponentName; -import android.content.Intent; -import android.content.res.AssetManager; -import android.content.res.Resources; -import android.content.res.Resources.Theme; -import android.os.Bundle; -import android.view.KeyEvent; -import android.view.Menu; -import android.view.MenuItem; -import android.view.MotionEvent; -import android.view.WindowManager.LayoutParams; - -import com.ryg.dynamicload.internal.DLAttachable; -import com.ryg.dynamicload.internal.DLPluginManager; -import com.ryg.dynamicload.internal.DLPluginPackage; -import com.ryg.dynamicload.proxy.DLActivityProxy; - -public class DLProxyActivity extends Activity - implements DLAttachable<DLPlugin> { - - protected DLPlugin mRemoteActivity; - private DLActivityProxy impl = new DLActivityProxy(this); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - impl.onCreate(getIntent()); - } - - @Override - public void attach(DLPlugin remoteActivity, DLPluginPackage pluginPackage) { - mRemoteActivity = remoteActivity; - } - - @Override - public AssetManager getAssets() { - return impl.getAssets() == null ? super.getAssets() : impl.getAssets(); - } - - @Override - public Resources getResources() { - return impl.getResources() == null ? super.getResources() : impl.getResources(); - } - - @Override - public Theme getTheme() { - return impl.getTheme() == null ? super.getTheme() : impl.getTheme(); - } - - @Override - public ClassLoader getClassLoader() { - return impl.getClassLoader(); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - mRemoteActivity.onActivityResult(requestCode, resultCode, data); - super.onActivityResult(requestCode, resultCode, data); - } - - @Override - protected void onStart() { - mRemoteActivity.onStart(); - super.onStart(); - } - - @Override - protected void onRestart() { - mRemoteActivity.onRestart(); - super.onRestart(); - } - - @Override - protected void onResume() { - mRemoteActivity.onResume(); - super.onResume(); - } - - @Override - protected void onPause() { - mRemoteActivity.onPause(); - super.onPause(); - } - - @Override - protected void onStop() { - mRemoteActivity.onStop(); - super.onStop(); - } - - @Override - protected void onDestroy() { - mRemoteActivity.onDestroy(); - super.onDestroy(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - mRemoteActivity.onSaveInstanceState(outState); - super.onSaveInstanceState(outState); - } - - @Override - protected void onRestoreInstanceState(Bundle savedInstanceState) { - mRemoteActivity.onRestoreInstanceState(savedInstanceState); - super.onRestoreInstanceState(savedInstanceState); - } - - @Override - protected void onNewIntent(Intent intent) { - mRemoteActivity.onNewIntent(intent); - super.onNewIntent(intent); - } - - @Override - public void onBackPressed() { - mRemoteActivity.onBackPressed(); - super.onBackPressed(); - } - - @Override - public boolean onTouchEvent(MotionEvent event) { - super.onTouchEvent(event); - return mRemoteActivity.onTouchEvent(event); - } - - @Override - public boolean onKeyUp(int keyCode, KeyEvent event) { - super.onKeyUp(keyCode, event); - return mRemoteActivity.onKeyUp(keyCode, event); - } - - @Override - public void onWindowAttributesChanged(LayoutParams params) { - mRemoteActivity.onWindowAttributesChanged(params); - super.onWindowAttributesChanged(params); - } - - @Override - public void onWindowFocusChanged(boolean hasFocus) { - mRemoteActivity.onWindowFocusChanged(hasFocus); - super.onWindowFocusChanged(hasFocus); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - mRemoteActivity.onCreateOptionsMenu(menu); - return super.onCreateOptionsMenu(menu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - mRemoteActivity.onOptionsItemSelected(item); - return super.onOptionsItemSelected(item); - } - - @Override - public ComponentName startService(Intent service) { - return super.startService(service); - } - -} diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java deleted file mode 100644 index 99f2abc..0000000 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyService.java +++ /dev/null @@ -1,112 +0,0 @@ - -package com.ryg.dynamicload; - -import android.annotation.TargetApi; -import android.app.Service; -import android.content.Intent; -import android.content.res.Configuration; -import android.os.Build; -import android.os.IBinder; -import android.util.Log; - -import com.ryg.dynamicload.internal.DLAttachable; -import com.ryg.dynamicload.internal.DLPluginPackage; -import com.ryg.dynamicload.proxy.DLServiceProxy; - -public class DLProxyService extends Service implements DLAttachable<DLServicePlugin> { - - private static final String TAG = "DLProxyService"; - private DLServiceProxy mImpl = new DLServiceProxy(this); - private DLServicePlugin mRemoteService; - - @Override - public IBinder onBind(Intent intent) { - Log.d(TAG, TAG + " onBind"); - // 判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 - if (mRemoteService == null) { - mImpl.onCreate(intent); - } - return mRemoteService.onBind(intent); - } - - @Override - public void onCreate() { - super.onCreate(); - Log.d(TAG, TAG + " onCreate"); - } - - // @Override - // public void onStart(Intent intent, int startId) { - // // TODO Auto-generated method stub - // super.onStart(intent, startId); - // Log.d(TAG, TAG + " onStart"); - // - // } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - Log.d(TAG, TAG + " onStartCommand " + this.toString()); - // 判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 - if (mRemoteService == null) { - mImpl.onCreate(intent); - } - super.onStartCommand(intent, flags, startId); - return mRemoteService.onStartCommand(intent, flags, startId); - } - - @Override - public void onDestroy() { - mRemoteService.onDestroy(); - super.onDestroy(); - Log.d(TAG, TAG + " onDestroy"); - } - - @Override - public void onConfigurationChanged(Configuration newConfig) { - mRemoteService.onConfigurationChanged(newConfig); - super.onConfigurationChanged(newConfig); - Log.d(TAG, TAG + " onConfigurationChanged"); - } - - @Override - public void onLowMemory() { - mRemoteService.onLowMemory(); - super.onLowMemory(); - Log.d(TAG, TAG + " onLowMemory"); - } - - @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) - @Override - public void onTrimMemory(int level) { - mRemoteService.onTrimMemory(level); - super.onTrimMemory(level); - Log.d(TAG, TAG + " onTrimMemory"); - } - - @Override - public boolean onUnbind(Intent intent) { - Log.d(TAG, TAG + " onUnbind"); - super.onUnbind(intent); - return mRemoteService.onUnbind(intent); - } - - @Override - public void onRebind(Intent intent) { - mRemoteService.onRebind(intent); - super.onRebind(intent); - Log.d(TAG, TAG + " onRebind"); - } - - @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) - @Override - public void onTaskRemoved(Intent rootIntent) { - mRemoteService.onTaskRemoved(rootIntent); - super.onTaskRemoved(rootIntent); - Log.d(TAG, TAG + " onTaskRemoved"); - } - - @Override - public void attach(DLServicePlugin remoteService, DLPluginPackage pluginPackage) { - mRemoteService = remoteService; - } -} diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLPlugin.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLActivityPlugin.java similarity index 92% rename from DynamicLoadApk/lib/src/com/ryg/dynamicload/DLPlugin.java rename to DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLActivityPlugin.java index 47b9333..eac72d2 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLPlugin.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLActivityPlugin.java @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.ryg.dynamicload; +package com.ryg.dynamicload.internal; import android.app.Activity; import android.content.Intent; @@ -26,9 +26,7 @@ import android.view.MotionEvent; import android.view.WindowManager.LayoutParams; -import com.ryg.dynamicload.internal.DLAttachable; - -public interface DLPlugin extends DLAttachable<Activity>{ +public interface DLActivityPlugin extends DLAttachable<Activity>{ public void onCreate(Bundle savedInstanceState); public void onStart(); diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLPluginManager.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLPluginManager.java index cbfb355..10a3fba 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLPluginManager.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLPluginManager.java @@ -38,9 +38,9 @@ import com.ryg.dynamicload.DLBasePluginActivity; import com.ryg.dynamicload.DLBasePluginFragmentActivity; import com.ryg.dynamicload.DLBasePluginService; -import com.ryg.dynamicload.DLProxyActivity; -import com.ryg.dynamicload.DLProxyFragmentActivity; -import com.ryg.dynamicload.DLProxyService; +import com.ryg.dynamicload.proxy.DLActivityProxy; +import com.ryg.dynamicload.proxy.DLFragmentActivityProxy; +import com.ryg.dynamicload.proxy.DLServiceProxy; import com.ryg.utils.DLConstants; import com.ryg.utils.SoLibManager; @@ -399,9 +399,9 @@ private String getPluginActivityFullPath(DLIntent dlIntent, DLPluginPackage plug private Class<? extends Activity> getProxyActivityClass(Class<?> clazz) { Class<? extends Activity> activityClass = null; if (DLBasePluginActivity.class.isAssignableFrom(clazz)) { - activityClass = DLProxyActivity.class; + activityClass = DLActivityProxy.class; } else if (DLBasePluginFragmentActivity.class.isAssignableFrom(clazz)) { - activityClass = DLProxyFragmentActivity.class; + activityClass = DLFragmentActivityProxy.class; } return activityClass; @@ -410,7 +410,7 @@ private Class<? extends Activity> getProxyActivityClass(Class<?> clazz) { private Class<? extends Service> getProxyServiceClass(Class<?> clazz) { Class<? extends Service> proxyServiceClass = null; if (DLBasePluginService.class.isAssignableFrom(clazz)) { - proxyServiceClass = DLProxyService.class; + proxyServiceClass = DLServiceProxy.class; } // 后续可能还有IntentService,待补充 diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLServicePlugin.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServicePlugin.java similarity index 90% rename from DynamicLoadApk/lib/src/com/ryg/dynamicload/DLServicePlugin.java rename to DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServicePlugin.java index 69a61f3..45f7f55 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLServicePlugin.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLServicePlugin.java @@ -1,4 +1,4 @@ -package com.ryg.dynamicload; +package com.ryg.dynamicload.internal; import android.app.Service; @@ -6,8 +6,6 @@ import android.content.res.Configuration; import android.os.IBinder; -import com.ryg.dynamicload.internal.DLPluginPackage; - public interface DLServicePlugin { public void onCreate(); diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLActivityLoader.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLActivityLoader.java new file mode 100644 index 0000000..5be2d23 --- /dev/null +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLActivityLoader.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2014 singwhatiwanna(任玉刚) <singwhatiwanna@gmail.com> + * + * collaborator:田啸,宋思宇,Mr.Simple + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ryg.dynamicload.loader; + +import android.app.Activity; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageInfo; +import android.content.res.AssetManager; +import android.content.res.Resources; +import android.content.res.Resources.Theme; +import android.os.Build; +import android.os.Bundle; +import android.util.Log; + +import com.ryg.dynamicload.internal.DLActivityPlugin; + +/** + * This is a plugin activity proxy, the proxy will create the plugin activity + * with reflect, and then call the plugin activity's attach、onCreate method, at + * this time, the plugin activity is running. + * + * @author mrsimple + */ +public class DLActivityLoader extends DLBaseLoader<Activity, DLActivityPlugin> { + + private AssetManager mAssetManager; + private Resources mResources; + private Theme mTheme; + + private ActivityInfo mActivityInfo; + + public DLActivityLoader(Activity activity) { + mProxyComponent = activity; + } + + private void initializeActivityInfo() { + PackageInfo packageInfo = mPluginPackage.packageInfo; + if ((packageInfo.activities != null) && (packageInfo.activities.length > 0)) { + if (mPluginClazz == null) { + mPluginClazz = packageInfo.activities[0].name; + } + + // Finals 修复主题BUG + int defaultTheme = packageInfo.applicationInfo.theme; + for (ActivityInfo a : packageInfo.activities) { + if (a.name.equals(mPluginClazz)) { + 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) { + mProxyComponent.setTheme(mActivityInfo.theme); + } + + Theme superTheme = mProxyComponent.getTheme(); + mTheme = mResources.newTheme(); + mTheme.setTo(superTheme); + // Finals适配三星以及部分加载XML出现异常BUG + try { + mTheme.applyStyle(mActivityInfo.theme, true); + } catch (Exception e) { + e.printStackTrace(); + } + // TODO: handle mActivityInfo.launchMode here in the future. + } + + @Override + protected void init(Intent intent) { + super.init(intent); + mAssetManager = mPluginPackage.assetManager; + mResources = mPluginPackage.resources; + initializeActivityInfo(); + handleActivityInfo(); + } + + public ClassLoader getClassLoader() { + return mPluginPackage.classLoader; + } + + public AssetManager getAssets() { + return mAssetManager; + } + + public Resources getResources() { + return mResources; + } + + public Theme getTheme() { + return mTheme; + } + + public DLActivityPlugin getRemoteActivity() { + return mPlugin; + } + + @Override + protected void callPluginOnCreate(Bundle bundle) { + mPlugin.onCreate(bundle); + } +} diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLBaseProxy.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLBaseLoader.java similarity index 91% rename from DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLBaseProxy.java rename to DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLBaseLoader.java index e17d7af..924759b 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLBaseProxy.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLBaseLoader.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package com.ryg.dynamicload.proxy; +package com.ryg.dynamicload.loader; import android.content.Context; import android.content.Intent; @@ -32,14 +32,14 @@ import java.lang.reflect.Constructor; /** - * 插件代理类基类 + * 插件加载器基类,负责构建、加载插件组件,并且与代理组件建立关联关系 * * @author mrsimple * @param <P> 组件代理类型,例如DLProxyActivity,DLProxyService等 * @param <T> Plugin类型,例如{@see DLPlugin},{@see DLServicePlugin} */ @SuppressWarnings("unchecked") -public abstract class DLBaseProxy<P extends Context, T> { +public abstract class DLBaseLoader<P extends Context, T> { /** * 组件类型,例如Service,Activity等 @@ -112,7 +112,7 @@ private void attachPluginToProxy() { * 调用插件的attach和onCreate函数,启动插件 */ private void callOnCreate() { - // 调用插件的attach + // 调用插件的attach,将Proxy组件注入到插件对象中 ((DLAttachable<P>) mPlugin).attach(mProxyComponent, mPluginPackage); Bundle bundle = new Bundle(); bundle.putInt(DLConstants.FROM, DLConstants.FROM_EXTERNAL); @@ -120,6 +120,11 @@ private void callOnCreate() { callPluginOnCreate(bundle); } + /** + * 调用插件的onCreate方法 + * + * @param bundle 额外的数据 + */ protected abstract void callPluginOnCreate(Bundle bundle); } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLServiceLoader.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLServiceLoader.java new file mode 100644 index 0000000..12676ff --- /dev/null +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/loader/DLServiceLoader.java @@ -0,0 +1,25 @@ + +package com.ryg.dynamicload.loader; + +import android.app.Service; +import android.os.Bundle; + +import com.ryg.dynamicload.internal.DLServicePlugin; + +/** + * 服务代理类 + * + * @author mrsimple + */ +public class DLServiceLoader extends DLBaseLoader<Service, DLServicePlugin> { + + public DLServiceLoader(Service service) { + mProxyComponent = service; + } + + @Override + protected void callPluginOnCreate(Bundle dBundle) { + mPlugin.onCreate(); + } + +} diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLActivityProxy.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLActivityProxy.java index a28a37b..6dec201 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLActivityProxy.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLActivityProxy.java @@ -19,117 +19,166 @@ package com.ryg.dynamicload.proxy; import android.app.Activity; +import android.content.ComponentName; import android.content.Intent; -import android.content.pm.ActivityInfo; -import android.content.pm.PackageInfo; import android.content.res.AssetManager; import android.content.res.Resources; import android.content.res.Resources.Theme; -import android.os.Build; import android.os.Bundle; -import android.util.Log; +import android.view.KeyEvent; +import android.view.Menu; +import android.view.MenuItem; +import android.view.MotionEvent; +import android.view.WindowManager.LayoutParams; -import com.ryg.dynamicload.DLPlugin; +import com.ryg.dynamicload.internal.DLActivityPlugin; +import com.ryg.dynamicload.internal.DLAttachable; +import com.ryg.dynamicload.internal.DLPluginManager; +import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.dynamicload.loader.DLActivityLoader; -/** - * This is a plugin activity proxy, the proxy will create the plugin activity - * with reflect, and then call the plugin activity's attach、onCreate method, at - * this time, the plugin activity is running. - * - * @author mrsimple - */ -public class DLActivityProxy extends DLBaseProxy<Activity, DLPlugin> { - - private AssetManager mAssetManager; - private Resources mResources; - private Theme mTheme; - - private ActivityInfo mActivityInfo; -// public ClassLoader mPluginClassLoader; - - public DLActivityProxy(Activity activity) { - mProxyComponent = activity; - } - - private void initializeActivityInfo() { - PackageInfo packageInfo = mPluginPackage.packageInfo; - if ((packageInfo.activities != null) && (packageInfo.activities.length > 0)) { - if (mPluginClazz == null) { - mPluginClazz = packageInfo.activities[0].name; - } - - // Finals 修复主题BUG - int defaultTheme = packageInfo.applicationInfo.theme; - for (ActivityInfo a : packageInfo.activities) { - if (a.name.equals(mPluginClazz)) { - 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) { - mProxyComponent.setTheme(mActivityInfo.theme); - } - - Theme superTheme = mProxyComponent.getTheme(); - mTheme = mResources.newTheme(); - mTheme.setTo(superTheme); - // Finals适配三星以及部分加载XML出现异常BUG - try { - mTheme.applyStyle(mActivityInfo.theme, true); - } catch (Exception e) { - e.printStackTrace(); - } - // TODO: handle mActivityInfo.launchMode here in the future. - } - - @Override - protected void init(Intent intent) { - super.init(intent); - mAssetManager = mPluginPackage.assetManager; - mResources = mPluginPackage.resources; - initializeActivityInfo(); - handleActivityInfo(); +public class DLActivityProxy extends Activity + implements DLAttachable<DLActivityPlugin> { + + protected DLActivityPlugin mRemoteActivity; + private DLActivityLoader impl = new DLActivityLoader(this); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + impl.onCreate(getIntent()); } - public ClassLoader getClassLoader() { - return mPluginPackage.classLoader; + @Override + public void attach(DLActivityPlugin remoteActivity, DLPluginPackage pluginPackage) { + mRemoteActivity = remoteActivity; } + @Override public AssetManager getAssets() { - return mAssetManager; + return impl.getAssets() == null ? super.getAssets() : impl.getAssets(); } + @Override public Resources getResources() { - return mResources; + return impl.getResources() == null ? super.getResources() : impl.getResources(); } + @Override public Theme getTheme() { - return mTheme; + return impl.getTheme() == null ? super.getTheme() : impl.getTheme(); + } + + @Override + public ClassLoader getClassLoader() { + return impl.getClassLoader(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + mRemoteActivity.onActivityResult(requestCode, resultCode, data); + super.onActivityResult(requestCode, resultCode, data); } - public DLPlugin getRemoteActivity() { - return mPlugin; + @Override + protected void onStart() { + mRemoteActivity.onStart(); + super.onStart(); + } + + @Override + protected void onRestart() { + mRemoteActivity.onRestart(); + super.onRestart(); + } + + @Override + protected void onResume() { + mRemoteActivity.onResume(); + super.onResume(); + } + + @Override + protected void onPause() { + mRemoteActivity.onPause(); + super.onPause(); + } + + @Override + protected void onStop() { + mRemoteActivity.onStop(); + super.onStop(); + } + + @Override + protected void onDestroy() { + mRemoteActivity.onDestroy(); + super.onDestroy(); } @Override - protected void callPluginOnCreate(Bundle bundle) { - mPlugin.onCreate(bundle); + protected void onSaveInstanceState(Bundle outState) { + mRemoteActivity.onSaveInstanceState(outState); + super.onSaveInstanceState(outState); } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + mRemoteActivity.onRestoreInstanceState(savedInstanceState); + super.onRestoreInstanceState(savedInstanceState); + } + + @Override + protected void onNewIntent(Intent intent) { + mRemoteActivity.onNewIntent(intent); + super.onNewIntent(intent); + } + + @Override + public void onBackPressed() { + mRemoteActivity.onBackPressed(); + super.onBackPressed(); + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + super.onTouchEvent(event); + return mRemoteActivity.onTouchEvent(event); + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + super.onKeyUp(keyCode, event); + return mRemoteActivity.onKeyUp(keyCode, event); + } + + @Override + public void onWindowAttributesChanged(LayoutParams params) { + mRemoteActivity.onWindowAttributesChanged(params); + super.onWindowAttributesChanged(params); + } + + @Override + public void onWindowFocusChanged(boolean hasFocus) { + mRemoteActivity.onWindowFocusChanged(hasFocus); + super.onWindowFocusChanged(hasFocus); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + mRemoteActivity.onCreateOptionsMenu(menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + mRemoteActivity.onOptionsItemSelected(item); + return super.onOptionsItemSelected(item); + } + + @Override + public ComponentName startService(Intent service) { + return super.startService(service); + } + } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyFragmentActivity.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLFragmentActivityProxy.java similarity index 90% rename from DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyFragmentActivity.java rename to DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLFragmentActivityProxy.java index 5762dc3..e3487fc 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/DLProxyFragmentActivity.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLFragmentActivityProxy.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package com.ryg.dynamicload; +package com.ryg.dynamicload.proxy; import android.content.Intent; import android.content.res.AssetManager; @@ -30,15 +30,16 @@ import android.view.MotionEvent; import android.view.WindowManager.LayoutParams; +import com.ryg.dynamicload.internal.DLActivityPlugin; import com.ryg.dynamicload.internal.DLAttachable; import com.ryg.dynamicload.internal.DLPluginPackage; -import com.ryg.dynamicload.proxy.DLActivityProxy; +import com.ryg.dynamicload.loader.DLActivityLoader; -public class DLProxyFragmentActivity extends FragmentActivity - implements DLAttachable<DLPlugin> { +public class DLFragmentActivityProxy extends FragmentActivity + implements DLAttachable<DLActivityPlugin> { - protected DLPlugin mRemoteActivity; - private DLActivityProxy impl = new DLActivityProxy(this); + protected DLActivityPlugin mRemoteActivity; + private DLActivityLoader impl = new DLActivityLoader(this); @Override protected void onCreate(Bundle savedInstanceState) { @@ -47,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) { } @Override - public void attach(DLPlugin remoteActivity, DLPluginPackage pluginPackage) { + public void attach(DLActivityPlugin remoteActivity, DLPluginPackage pluginPackage) { mRemoteActivity = remoteActivity; } diff --git a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java index aac43f7..60c13eb 100644 --- a/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java +++ b/DynamicLoadApk/lib/src/com/ryg/dynamicload/proxy/DLServiceProxy.java @@ -1,25 +1,113 @@ package com.ryg.dynamicload.proxy; +import android.annotation.TargetApi; import android.app.Service; -import android.os.Bundle; +import android.content.Intent; +import android.content.res.Configuration; +import android.os.Build; +import android.os.IBinder; +import android.util.Log; -import com.ryg.dynamicload.DLServicePlugin; +import com.ryg.dynamicload.internal.DLAttachable; +import com.ryg.dynamicload.internal.DLPluginPackage; +import com.ryg.dynamicload.internal.DLServicePlugin; +import com.ryg.dynamicload.loader.DLServiceLoader; -/** - * 服务代理类 - * - * @author mrsimple - */ -public class DLServiceProxy extends DLBaseProxy<Service, DLServicePlugin> { +public class DLServiceProxy extends Service implements DLAttachable<DLServicePlugin> { - public DLServiceProxy(Service service) { - mProxyComponent = service; + private static final String TAG = "DLProxyService"; + private DLServiceLoader mImpl = new DLServiceLoader(this); + private DLServicePlugin mRemoteService; + + @Override + public IBinder onBind(Intent intent) { + Log.d(TAG, TAG + " onBind"); + // 判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 + if (mRemoteService == null) { + mImpl.onCreate(intent); + } + return mRemoteService.onBind(intent); + } + + @Override + public void onCreate() { + super.onCreate(); + Log.d(TAG, TAG + " onCreate"); + } + + // @Override + // public void onStart(Intent intent, int startId) { + // // TODO Auto-generated method stub + // super.onStart(intent, startId); + // Log.d(TAG, TAG + " onStart"); + // + // } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Log.d(TAG, TAG + " onStartCommand " + this.toString()); + // 判断是否存在插件Service,如果存在,则不进行Service插件的构造工作 + if (mRemoteService == null) { + mImpl.onCreate(intent); + } + super.onStartCommand(intent, flags, startId); + return mRemoteService.onStartCommand(intent, flags, startId); + } + + @Override + public void onDestroy() { + mRemoteService.onDestroy(); + super.onDestroy(); + Log.d(TAG, TAG + " onDestroy"); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + mRemoteService.onConfigurationChanged(newConfig); + super.onConfigurationChanged(newConfig); + Log.d(TAG, TAG + " onConfigurationChanged"); } @Override - protected void callPluginOnCreate(Bundle dBundle) { - mPlugin.onCreate(); + public void onLowMemory() { + mRemoteService.onLowMemory(); + super.onLowMemory(); + Log.d(TAG, TAG + " onLowMemory"); } + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) + @Override + public void onTrimMemory(int level) { + mRemoteService.onTrimMemory(level); + super.onTrimMemory(level); + Log.d(TAG, TAG + " onTrimMemory"); + } + + @Override + public boolean onUnbind(Intent intent) { + Log.d(TAG, TAG + " onUnbind"); + super.onUnbind(intent); + return mRemoteService.onUnbind(intent); + } + + @Override + public void onRebind(Intent intent) { + mRemoteService.onRebind(intent); + super.onRebind(intent); + Log.d(TAG, TAG + " onRebind"); + } + + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) + @Override + public void onTaskRemoved(Intent rootIntent) { + mRemoteService.onTaskRemoved(rootIntent); + super.onTaskRemoved(rootIntent); + Log.d(TAG, TAG + " onTaskRemoved"); + } + + @Override + public void attach(DLServicePlugin remoteService, DLPluginPackage pluginPackage) { + mRemoteService = remoteService; + } } diff --git a/DynamicLoadApk/sample/main/main-host/AndroidManifest.xml b/DynamicLoadApk/sample/main/main-host/AndroidManifest.xml index 3339156..875fa4a 100644 --- a/DynamicLoadApk/sample/main/main-host/AndroidManifest.xml +++ b/DynamicLoadApk/sample/main/main-host/AndroidManifest.xml @@ -51,7 +51,7 @@ </intent-filter> </activity> <activity - android:name="com.ryg.dynamicload.DLProxyActivity" + android:name="com.ryg.dynamicload.proxy.DLActivityProxy" android:label="@string/app_name" > <intent-filter> <action android:name="com.ryg.dynamicload.proxy.activity.VIEW" /> @@ -60,7 +60,7 @@ </intent-filter> </activity> <activity - android:name="com.ryg.dynamicload.DLProxyFragmentActivity" + android:name="com.ryg.dynamicload.proxy.DLFragmentActivityProxy" android:label="@string/app_name" > <intent-filter> <action android:name="com.ryg.dynamicload.proxy.fragmentactivity.VIEW" /> @@ -68,7 +68,7 @@ <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> - <service android:name="com.ryg.dynamicload.DLProxyService" > + <service android:name="com.ryg.dynamicload.proxy.DLServiceProxy" > <!-- <intent-filter >