|  | 
|  | 1 | +package com.annimon.androidplugindemo; | 
|  | 2 | + | 
|  | 3 | +import android.content.Context; | 
|  | 4 | +import android.content.Intent; | 
|  | 5 | +import android.content.pm.ApplicationInfo; | 
|  | 6 | +import android.content.pm.PackageManager; | 
|  | 7 | +import android.content.res.Resources; | 
|  | 8 | +import android.os.Bundle; | 
|  | 9 | +import android.support.v7.app.AppCompatActivity; | 
|  | 10 | +import android.support.v7.widget.Toolbar; | 
|  | 11 | +import android.view.View; | 
|  | 12 | +import android.widget.TextView; | 
|  | 13 | +import java.lang.reflect.Constructor; | 
|  | 14 | +import java.lang.reflect.Field; | 
|  | 15 | +import java.lang.reflect.Method; | 
|  | 16 | +import java.util.List; | 
|  | 17 | +import dalvik.system.DexClassLoader; | 
|  | 18 | + | 
|  | 19 | +public class MainActivity extends AppCompatActivity implements View.OnClickListener { | 
|  | 20 | + | 
|  | 21 | +    private static final String CATEGORY_PLUGIN = "com.annimon.plugin.PLUGIN_APPLICATION"; | 
|  | 22 | +    private static final String PLUGIN_PACKAGE = "com.annimon.plugin"; | 
|  | 23 | + | 
|  | 24 | +    private Toolbar toolbar; | 
|  | 25 | +    private TextView infoTextView; | 
|  | 26 | + | 
|  | 27 | +    @Override | 
|  | 28 | +    protected void onCreate(Bundle savedInstanceState) { | 
|  | 29 | +        super.onCreate(savedInstanceState); | 
|  | 30 | +        setContentView(R.layout.activity_main); | 
|  | 31 | + | 
|  | 32 | +        infoTextView = (TextView) findViewById(R.id.info); | 
|  | 33 | +        toolbar = (Toolbar) findViewById(R.id.toolbar); | 
|  | 34 | +        setSupportActionBar(toolbar); | 
|  | 35 | + | 
|  | 36 | +        int[] ids = {R.id.launch_plugin_activity, R.id.invoke_code, R.id.invoke_library}; | 
|  | 37 | +        for (int id : ids) { | 
|  | 38 | +            findViewById(id).setOnClickListener(this); | 
|  | 39 | +        } | 
|  | 40 | +    } | 
|  | 41 | + | 
|  | 42 | +    @Override | 
|  | 43 | +    public void onClick(View v) { | 
|  | 44 | +        switch (v.getId()) { | 
|  | 45 | +            case R.id.launch_plugin_activity: | 
|  | 46 | +                launchPluginActivity(); | 
|  | 47 | +                break; | 
|  | 48 | +            case R.id.invoke_code: | 
|  | 49 | +                invokeCode(); | 
|  | 50 | +                break; | 
|  | 51 | +            case R.id.invoke_library: | 
|  | 52 | +                invokeLibrary(); | 
|  | 53 | +                break; | 
|  | 54 | +        } | 
|  | 55 | +    } | 
|  | 56 | + | 
|  | 57 | +    /* | 
|  | 58 | +     * Checks if plugin application is installed. | 
|  | 59 | +     */ | 
|  | 60 | +    private boolean isPluginInstalled() { | 
|  | 61 | +        if (!PackageUtil.isApplicationInstalled(this, PLUGIN_PACKAGE)) { | 
|  | 62 | +            infoTextView.setText(R.string.plugin_not_installed); | 
|  | 63 | +            return false; | 
|  | 64 | +        } | 
|  | 65 | +        return true; | 
|  | 66 | +    } | 
|  | 67 | + | 
|  | 68 | +    /* | 
|  | 69 | +     * Gets information about plugin application (label, icon, resources), then launch activity. | 
|  | 70 | +     */ | 
|  | 71 | +    private void launchPluginActivity() { | 
|  | 72 | +        PackageManager pm = getApplicationContext().getPackageManager(); | 
|  | 73 | + | 
|  | 74 | +        // Search plugin applications | 
|  | 75 | +        Intent queryIntent = new Intent(Intent.ACTION_MAIN); | 
|  | 76 | +        queryIntent.addCategory(CATEGORY_PLUGIN); | 
|  | 77 | +        final List<ApplicationInfo> infos = PackageUtil.filterApplicationsByIntent(pm, queryIntent); | 
|  | 78 | +        if (infos.isEmpty()) { | 
|  | 79 | +            infoTextView.setText(R.string.plugin_not_installed); | 
|  | 80 | +            return; | 
|  | 81 | +        } | 
|  | 82 | +        // Get first app | 
|  | 83 | +        ApplicationInfo appInfo = infos.get(0); | 
|  | 84 | + | 
|  | 85 | +        // Get simple information from ApplicationInfo | 
|  | 86 | +        StringBuilder info = new StringBuilder(); | 
|  | 87 | +        info.append("Label: ").append(appInfo.loadLabel(pm)).append("\n"); | 
|  | 88 | +        info.append("Package name: ").append(appInfo.packageName).append("\n"); | 
|  | 89 | +        info.append("Source dir: ").append(appInfo.sourceDir).append("\n"); | 
|  | 90 | +        info.append("Target SDK Version: ").append(appInfo.targetSdkVersion).append("\n"); | 
|  | 91 | + | 
|  | 92 | +        toolbar.setLogo(appInfo.loadIcon(pm)); | 
|  | 93 | + | 
|  | 94 | +        // Get resources | 
|  | 95 | +        Resources pluginResources = PackageUtil.getResources(pm, appInfo); | 
|  | 96 | +        if (pluginResources != null) { | 
|  | 97 | +            // Get string resource | 
|  | 98 | +            int plugin_text = pluginResources.getIdentifier("plugin_text", "string", appInfo.packageName); | 
|  | 99 | +            if (plugin_text > 0) { | 
|  | 100 | +                info.append("Plugin text: ").append(pluginResources.getString(plugin_text)).append("\n"); | 
|  | 101 | +            } | 
|  | 102 | + | 
|  | 103 | +            // Get color resource | 
|  | 104 | +            int colorPrimaryDark = pluginResources.getIdentifier("colorPrimaryDark", "color", appInfo.packageName); | 
|  | 105 | +            if (colorPrimaryDark > 0) { | 
|  | 106 | +                toolbar.setBackgroundColor(pluginResources.getColor(colorPrimaryDark)); | 
|  | 107 | +            } | 
|  | 108 | +        } | 
|  | 109 | +        infoTextView.setText(info); | 
|  | 110 | + | 
|  | 111 | +        // Start Activity | 
|  | 112 | +        Intent activityIntent = pm.getLaunchIntentForPackage(appInfo.packageName); | 
|  | 113 | +        startActivity(activityIntent); | 
|  | 114 | +    } | 
|  | 115 | + | 
|  | 116 | +    /** | 
|  | 117 | +     * Invokes code from plugin application in separate ClassLoader. | 
|  | 118 | +     */ | 
|  | 119 | +    private void invokeCode() { | 
|  | 120 | +        if (!isPluginInstalled()) return; | 
|  | 121 | + | 
|  | 122 | +        Context pluginContext = PackageUtil.getPackageContext(this, PLUGIN_PACKAGE); | 
|  | 123 | +        if (pluginContext == null) return; | 
|  | 124 | +        ClassLoader classLoader = pluginContext.getClassLoader(); | 
|  | 125 | + | 
|  | 126 | +        StringBuilder info = new StringBuilder(); | 
|  | 127 | +        try { | 
|  | 128 | +            // Invoke class via Reflection API | 
|  | 129 | +            Class<?> pluginClass = classLoader.loadClass(PLUGIN_PACKAGE + ".Plugin"); | 
|  | 130 | + | 
|  | 131 | +            Field goldenRatio = pluginClass.getDeclaredField("GOLDEN_RATIO"); | 
|  | 132 | +            info.append("GOLDEN_RATIO: ").append(goldenRatio.getDouble(null)).append("\n"); | 
|  | 133 | + | 
|  | 134 | +            Method sum = pluginClass.getDeclaredMethod("sum", int.class, int.class); | 
|  | 135 | +            info.append("sum(42, 280): ").append(sum.invoke(null, 42, 280)).append("\n"); | 
|  | 136 | + | 
|  | 137 | +            Method reverse = pluginClass.getDeclaredMethod("reverse", String.class); | 
|  | 138 | +            info.append("reverse(\"abcdefgh\"): ").append(reverse.invoke(null, "abcdefgh")); | 
|  | 139 | +        } catch (Exception e) { | 
|  | 140 | +            info.append("Unable to retrieve data, ").append(e.toString()); | 
|  | 141 | +        } | 
|  | 142 | +        infoTextView.setText(info); | 
|  | 143 | +    } | 
|  | 144 | + | 
|  | 145 | +    /** | 
|  | 146 | +     * Extracts library.apk to internal storage, then invoke code. | 
|  | 147 | +     */ | 
|  | 148 | +    private void invokeLibrary() { | 
|  | 149 | +        final String filename = "library.apk"; | 
|  | 150 | +        String path = IOUtil.extractAssetToInternalStorage(this, filename); | 
|  | 151 | +        if (path == null) { | 
|  | 152 | +            infoTextView.setText("Unable to extract " + filename + " to internal storage"); | 
|  | 153 | +            return; | 
|  | 154 | +        } | 
|  | 155 | + | 
|  | 156 | +        StringBuilder info = new StringBuilder(); | 
|  | 157 | +        try { | 
|  | 158 | +            DexClassLoader dexClassLoader = new DexClassLoader( | 
|  | 159 | +                    path, getFilesDir().getAbsolutePath(), null, | 
|  | 160 | +                    getClassLoader()); | 
|  | 161 | +            final Class<?> infoClass = dexClassLoader.loadClass("plugin.Info"); | 
|  | 162 | + | 
|  | 163 | +            // Invoke default constructor | 
|  | 164 | +            Object defaultObject = infoClass.newInstance(); | 
|  | 165 | +            Method getInfo = infoClass.getDeclaredMethod("getInfo"); | 
|  | 166 | +            String defaultInfo = (String) getInfo.invoke(defaultObject); | 
|  | 167 | +            info.append("new Info().getInfo(): ").append(defaultInfo).append("\n"); | 
|  | 168 | + | 
|  | 169 | +            // Invoke constructor with argument | 
|  | 170 | +            Constructor constructor = infoClass.getDeclaredConstructor(String.class); | 
|  | 171 | +            Object testObject = constructor.newInstance("Test"); | 
|  | 172 | +            String testInfo = (String) getInfo.invoke(testObject); | 
|  | 173 | +            info.append("new Info(\"Test\").getInfo(): ").append(testInfo); | 
|  | 174 | +        } catch (Exception e) { | 
|  | 175 | +            info.append("Unable to retrieve data, ").append(e.toString()); | 
|  | 176 | +        } | 
|  | 177 | +        infoTextView.setText(info); | 
|  | 178 | +    } | 
|  | 179 | +} | 
0 commit comments