Android Open Source - dynamic-load-apk D L Proxy Impl






From Project

Back to project page dynamic-load-apk.

License

The source code is released under:

Apache License

If you think the Android project dynamic-load-apk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 singwhatiwanna(???) <singwhatiwanna@gmail.com>
 */*w  w w .j  ava 2s  .  c  o  m*/
 * collaborator:??,????
 *
 * 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 java.lang.reflect.Constructor;

import android.annotation.TargetApi;
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.DLPlugin;
import com.ryg.utils.DLConstants;

public class DLProxyImpl {

    private static final String TAG = "DLProxyImpl";

    private String mClass;
    private String mPackageName;

    private DLPluginPackage mPluginPackage;
    private DLPluginManager mPluginManager;

    private AssetManager mAssetManager;
    private Resources mResources;
    private Theme mTheme;

    private ActivityInfo mActivityInfo;
    private Activity mActivity;
    protected DLPlugin mRemoteActivity;

    public DLProxyImpl(Activity activity) {
        mActivity = activity;
    }

    private void initializeActivityInfo() {
        PackageInfo packageInfo = mPluginPackage.packageInfo;
        if ((packageInfo.activities != null) && (packageInfo.activities.length > 0)) {
            if (mClass == null) {
                mClass = packageInfo.activities[0].name;
            }
            for (ActivityInfo a : packageInfo.activities) {
                if (a.name.equals(mClass)) {
                    mActivityInfo = a;
                }
            }
        }
    }

    private void handleActivityInfo() {
        Log.d(TAG, "handleActivityInfo, theme=" + mActivityInfo.theme);
        if (mActivityInfo.theme > 0) {
            mActivity.setTheme(mActivityInfo.theme);
        }
        Theme superTheme = mActivity.getTheme();
        mTheme = mResources.newTheme();
        mTheme.setTo(superTheme);

        // TODO: handle mActivityInfo.launchMode here in the future.
    }

    public void onCreate(Intent intent) {
        mPackageName = intent.getStringExtra(DLConstants.EXTRA_PACKAGE);
        mClass = intent.getStringExtra(DLConstants.EXTRA_CLASS);
        Log.d(TAG, "mClass=" + mClass + " mPackageName=" + mPackageName);

        mPluginManager = DLPluginManager.getInstance(mActivity);
        mPluginPackage = mPluginManager.getPackage(mPackageName);
        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[] {});
            mRemoteActivity = (DLPlugin) instance;
            ((DLProxy) mActivity).attach(mRemoteActivity, mPluginManager);
            Log.d(TAG, "instance = " + instance);

            mRemoteActivity.attach(mActivity, mPluginPackage);

            Bundle bundle = new Bundle();
            bundle.putInt(DLConstants.FROM, DLConstants.FROM_EXTERNAL);
            mRemoteActivity.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 DLPlugin getRemoteActivity() {
        return mRemoteActivity;
    }

    public interface DLProxy {
        public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager);
    }
}




Java Source Code List

com.ryg.dynamicload.DLBasePluginActivity.java
com.ryg.dynamicload.DLBasePluginFragmentActivity.java
com.ryg.dynamicload.DLPlugin.java
com.ryg.dynamicload.DLProxyActivity.java
com.ryg.dynamicload.DLProxyFragmentActivity.java
com.ryg.dynamicload.internal.DLIntent.java
com.ryg.dynamicload.internal.DLPluginManager.java
com.ryg.dynamicload.internal.DLPluginPackage.java
com.ryg.dynamicload.internal.DLProxyImpl.java
com.ryg.dynamicload.sample.docommon.HostInterfaceManager.java
com.ryg.dynamicload.sample.docommon.HostInterface.java
com.ryg.dynamicload.sample.doihost.MainActivity.java
com.ryg.dynamicload.sample.doihost.component.HostComponent.java
com.ryg.dynamicload.sample.doihost.interfacee.HostInterfaceImp.java
com.ryg.dynamicload.sample.doiplugin.MainActivity.java
com.ryg.dynamicload.sample.mainhost.MainActivity.java
com.ryg.dynamicload.sample.mainhost.TestHostClass.java
com.ryg.dynamicload.sample.mainplugin.MainActivity.java
com.ryg.dynamicload.sample.mainplugin.ResourceConstants.java
com.ryg.dynamicload.sample.mainplugin.TestButton.java
com.ryg.dynamicload.sample.mainplugin.TestFragmentActivity.java
com.ryg.dynamicload.sample.mainplugin.TestFragment.java
com.ryg.dynamicload.sample.mainpluginb.MainActivity.java
com.ryg.utils.DLConstants.java
com.ryg.utils.DLUtils.java