Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue#22 for lab #49

Open
wants to merge 2 commits into
base: lab
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added DynamicLoadApk/.DS_Store
Binary file not shown.
Binary file added DynamicLoadApk/lib/.DS_Store
Binary file not shown.
Binary file added DynamicLoadApk/lib/src/.DS_Store
Binary file not shown.
Binary file added DynamicLoadApk/lib/src/com/.DS_Store
Binary file not shown.
Binary file added DynamicLoadApk/lib/src/com/ryg/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class DLProxyActivity extends Activity implements DLProxy {

protected DLPlugin mRemoteActivity;
private DLProxyImpl impl = new DLProxyImpl(this);
private DLPluginManager mPluginManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -48,7 +47,6 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager) {
mRemoteActivity = remoteActivity;
mPluginManager = pluginManager;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ryg.dynamicload;

import android.content.Intent;
Expand All @@ -23,6 +24,7 @@
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -37,18 +39,18 @@ public class DLProxyFragmentActivity extends FragmentActivity implements DLProxy

protected DLPlugin mRemoteActivity;
private DLProxyImpl impl = new DLProxyImpl(this);
private DLPluginManager mPluginManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("", "fragment : intent id : " + getIntent().toString());
Log.e("", "### proxy fragment : " + getIntent().getClass().getName());
impl.onCreate(getIntent());
}

@Override
public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager) {
mRemoteActivity = remoteActivity;
mPluginManager = pluginManager;
}

@Override
Expand Down
49 changes: 49 additions & 0 deletions DynamicLoadApk/lib/src/com/ryg/dynamicload/internal/DLConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2015 singwhatiwanna(任玉刚) <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.ryg.dynamicload.internal;

/**
* DL配置类,用于存储DL插件系统唯一的配置信息
*
* @author mrsimple
*/
public final class DLConfig {

/**
* plugin的ClassLoader, 默认设置为DL的ClassLoader,在用户传递Parcelable数据时会替换成Plugin的
* classLoader.
*/
public ClassLoader mPluginClassLoader = DLConfig.class.getClassLoader();
public final float DL_VERSION = 1.0f;

private static final DLConfig sConfig = new DLConfig();

private DLConfig() {
}

public static DLConfig getConfig() {
return sConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ryg.dynamicload.internal;

package com.ryg.dynamicload.internal;

import android.content.Intent;
import android.os.Parcel;
Expand Down Expand Up @@ -77,6 +77,16 @@ public String getDexPath() {
return mDexPath;
}

@Override
public Intent putExtra(String name, Parcelable value) {
ClassLoader pluginLoader = value.getClass().getClassLoader();
if (getExtras() == null) {
putExtra("dl-verson", DLConfig.getConfig().DL_VERSION);
}
DLConfig.getConfig().mPluginClassLoader = pluginLoader;
getExtras().setClassLoader(pluginLoader);
return super.putExtra(name, value);
}

public static final Parcelable.Creator<DLIntent> CREATOR = new Parcelable.Creator<DLIntent>() {

Expand Down Expand Up @@ -114,4 +124,10 @@ public void readFromParcel(Parcel in) {
super.readFromParcel(in);
}

@Override
public String toString() {
return "DLIntent [mDexPath=" + mDexPath + ", mPluginPackage=" + mPluginPackage
+ ", mPluginClass=" + mPluginClass + ", hashcode : " + this.hashCode() + " ]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ryg.dynamicload.internal;

import java.io.File;
Expand Down Expand Up @@ -46,26 +47,29 @@ public class DLPluginManager {
private static final String TAG = "DLPluginManager";

/**
* return value of {@link #startPluginActivity(Activity, DLIntent)} start success
* return value of {@link #startPluginActivity(Activity, DLIntent)} start
* success
*/
public static final int START_RESULT_SUCCESS = 0;

/**
* return value of {@link #startPluginActivity(Activity, DLIntent)} package not found
* return value of {@link #startPluginActivity(Activity, DLIntent)} package
* not found
*/
public static final int START_RESULT_NO_PKG = 1;

/**
* return value of {@link #startPluginActivity(Activity, DLIntent)} class not found
* return value of {@link #startPluginActivity(Activity, DLIntent)} class
* not found
*/
public static final int START_RESULT_NO_CLASS = 2;

/**
* return value of {@link #startPluginActivity(Activity, DLIntent)} class type error
* return value of {@link #startPluginActivity(Activity, DLIntent)} class
* type error
*/
public static final int START_RESULT_TYPE_ERROR = 3;


private static volatile DLPluginManager sInstance;
private Context mContext;
private final HashMap<String, DLPluginPackage> mPackagesHolder = new HashMap<String, DLPluginPackage>();
Expand All @@ -90,10 +94,12 @@ public static DLPluginManager getInstance(Context context) {
/**
* Load a apk. Before start a plugin Activity, we should do this first.<br/>
* NOTE : will only be called by host apk.
*
* @param dexPath
*/
public DLPluginPackage loadApk(String dexPath) {
// when loadApk is called by host apk, we assume that plugin is invoked by host.
// when loadApk is called by host apk, we assume that plugin is invoked
// by host.
mFrom = DLConstants.FROM_EXTERNAL;

PackageInfo packageInfo = mContext.getPackageManager().
Expand Down Expand Up @@ -127,7 +133,8 @@ public void reLoadApk(String dexPath, String packageName) {
private DexClassLoader createDexClassLoader(String dexPath) {
File dexOutputDir = mContext.getDir("dex", Context.MODE_PRIVATE);
final String dexOutputPath = dexOutputDir.getAbsolutePath();
DexClassLoader loader = new DexClassLoader(dexPath, dexOutputPath, null, mContext.getClassLoader());
DexClassLoader loader = new DexClassLoader(dexPath, dexOutputPath, null,
mContext.getClassLoader());
return loader;
}

Expand Down Expand Up @@ -173,6 +180,7 @@ private Resources createResources(AssetManager assetManager) {
public void launchPluginActivity(DLIntent dlIntent) {
Intent service = new Intent(mContext, DLIntentService.class);
service.setAction(DLConstants.ACTION_LAUNCH_PLUGIN);
service.setExtrasClassLoader(DLConfig.getConfig().mPluginClassLoader);
service.putExtra(DLConstants.EXTRA_DLINTENT, dlIntent);
mContext.startService(service);
}
Expand All @@ -188,11 +196,16 @@ public int startPluginActivity(Context context, DLIntent dlIntent) {
* @param context
* @param dlIntent
* @param requestCode
* @return One of below: {@link #START_RESULT_SUCCESS} {@link #START_RESULT_NO_PKG}
* {@link #START_RESULT_NO_CLASS} {@link #START_RESULT_TYPE_ERROR}
* @return One of below: {@link #START_RESULT_SUCCESS}
* {@link #START_RESULT_NO_PKG} {@link #START_RESULT_NO_CLASS}
* {@link #START_RESULT_TYPE_ERROR}
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public int startPluginActivityForResult(Context context, DLIntent dlIntent, int requestCode) {

// 设置 Extras ClassLoader
dlIntent.setExtrasClassLoader(DLConfig.getConfig().mPluginClassLoader);

if (mFrom == DLConstants.FROM_INTERNAL) {
dlIntent.setClassName(context, dlIntent.getPluginClass());
performStartActivityForResult(context, dlIntent, requestCode);
Expand Down Expand Up @@ -236,13 +249,19 @@ public int startPluginActivityForResult(Context context, DLIntent dlIntent, int

Intent intent = new Intent();
intent.setClass(mContext, activityClass);
if (dlIntent.getExtras() != null) {
intent.putExtras(dlIntent.getExtras());
}
intent.putExtra(DLConstants.EXTRA_CLASS, className);
intent.putExtra(DLConstants.EXTRA_PACKAGE, packageName);
intent.putExtra(DLConstants.EXTRA_DEX_PATH, dlIntent.getDexPath());
intent.setFlags(dlIntent.getFlags());

// 将dlIntent中的参数设置到intent中
if (dlIntent.getExtras() != null) {
// 设置ClassLoader,避免出现通过Intent传递Parcelable参数时出现Class Not Found异常
// dlIntent.setExtrasClassLoader(dlIntent.getPathClassLoader());
intent.setExtrasClassLoader(DLConfig.getConfig().mPluginClassLoader);
intent.putExtras(dlIntent.getExtras());
}

Log.d(TAG, "launch " + className);
performStartActivityForResult(context, intent, requestCode);
return START_RESULT_SUCCESS;
Expand All @@ -259,6 +278,7 @@ private void performStartActivityForResult(Context context, Intent intent, int r
/**
* verify plugin, return false means verify plugin failed,the plugin can not
* be loaded.
*
* @param context
* @param pluginPath
* @param packageInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ryg.dynamicload.internal;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -84,6 +85,11 @@ private void handleActivityInfo() {
}

public void onCreate(Intent intent) {

// 在获取数据之前设置Extras ClassLoader,避免传输自定义的Parcelable对象时出现Class Not
// Found的问题.
intent.setExtrasClassLoader(DLConfig.getConfig().mPluginClassLoader);
// 获取数据
mPackageName = intent.getStringExtra(DLConstants.EXTRA_PACKAGE);
mClass = intent.getStringExtra(DLConstants.EXTRA_CLASS);
mDexPath = intent.getStringExtra(DLConstants.EXTRA_DEX_PATH);
Expand Down
7 changes: 2 additions & 5 deletions DynamicLoadApk/lib/src/com/ryg/utils/DLUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.ryg.utils;

import java.util.List;

import android.R.anim;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
Expand All @@ -18,8 +15,8 @@

import com.ryg.dynamicload.DLBasePluginActivity;
import com.ryg.dynamicload.DLBasePluginFragmentActivity;
import com.ryg.dynamicload.internal.DLPluginManager;
import com.ryg.dynamicload.internal.DLPluginPackage;

import java.util.List;

public class DLUtils {
private static final String TAG = "DLUtils";
Expand Down
Binary file added DynamicLoadApk/sample/.DS_Store
Binary file not shown.
Binary file added DynamicLoadApk/sample/main/.DS_Store
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions DynamicLoadApk/sample/main/main-host/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="lib" path="libs/android-support-v4.jar"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
2 changes: 1 addition & 1 deletion DynamicLoadApk/sample/main/main-host/.project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>main-host</name>
<name>dl-main-host</name>
<comment></comment>
<projects>
</projects>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading