360 plugins learning notes external plugins

Keywords: Android

This article introduces how to use the external plug-in of RePlugin, which uses the simulation configuration in official demo2.

Step 1: package the project that needs to be an external plug-in to generate demo1.apk

Step 2: copy the apk to the host project assets/xxx /. The name of XXX is arbitrary and consistent

Here is the same as the official demo, as shown in the figure:

Step 3: call the code where the download Plug-in is triggered

    //Determine whether the demo1 plug-in is installed. If not, download and install it. If it is installed, skip to the page
    if (RePlugin.isPluginInstalled("com.test.qby.myapplication")) {
            RePlugin.startActivity(this, RePlugin.createIntent("com.test.qby.myapplication", "com.test.qby.myapplication.plugin.PluginActivity"))
    } else {
            Toast.makeText(this@MainActivity, "Downloading plug-in, please try again after downloading!", Toast.LENGTH_SHORT).show()
            val pd = ProgressDialog.show(this@MainActivity, "Installing...", "Please wait...", true, true)
            Thread {
                downApk()
                pd.dismiss()
            }.start()
    }

    /**
     * Simulate installation or upgrade (overwrite installation) external plug-ins
     * To facilitate the demonstration, the external plug-in is temporarily placed in the assets/external directory of the Host
     */
    private fun downApk() {
        val demo1Apk = "demo1.apk"
        val demo1apkPath = "external" + File.separator + demo1Apk

        // Does the file already exist? Delete directly
        val pluginFilePath = filesDir.absolutePath + File.separator + demo1Apk
        val pluginFile = File(pluginFilePath)
        if (pluginFile.exists()) {
            FileUtils.deleteQuietly(pluginFile)
        }

        // Start copying
        copyAssetsFileToAppFiles(demo1apkPath, demo1Apk)
        var info: PluginInfo? = null
        if (pluginFile.exists()) {
            info = RePlugin.install(pluginFilePath)
        }

        runOnUiThread({
            if (info != null) {
                Toast.makeText(this@MainActivity, "install success!", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@MainActivity, "install external plugin failed", Toast.LENGTH_SHORT).show()
            }
        })
    }

    /**
     * Copy the contents of a file from the assets directory
     * @param  assetFileName assets Apk source file path under directory
     * @param  newFileName Copy to the file name under the directory / data / data / package [name / files /
     */
    private fun copyAssetsFileToAppFiles(assetFileName: String, newFileName: String) {
        var ins: InputStream? = null
        var fos: FileOutputStream? = null
        val buffsize = 1024
        try {
            ins = this.assets.open(assetFileName)
            val allLength = ins!!.available()
            fos = this.openFileOutput(newFileName, Context.MODE_PRIVATE)
            var byteCount = 0
            var length = 0
            val buffer = ByteArray(buffsize)
            while (byteCount != -1) {
                fos!!.write(buffer, 0, byteCount)
                byteCount = ins!!.read(buffer)
                length += byteCount
                //Download progress
                Log.e("MainActivity","Total length="+ Formatter.formatFileSize(this,allLength.toLong())+"Download progress="+Formatter.formatFileSize(this,length.toLong()))
            }
            fos!!.flush()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                ins!!.close()
                fos!!.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }
    }

In this way, the download of the external plug-in and the jump of the page have been completed. In practical application, it should be downloaded from the server.

Note: at this time, only the plug-in package name com.test.qby.myapplication can be used for page Jump.

RePlugin also provides another way to jump: [alias]

1. Configure plug-in alias and plug-in version in plug-in manifest file

<meta-data
            android:name="com.qihoo360.plugin.name"
            android:value="plugin1"/>

 <meta-data
            android:name="com.qihoo360.plugin.version.var"
            android:value="100"/>

Modify the value value to modify the plug-in alias, plug-in version and version number.

Official tip:

Detailed reference [plug in information]

2. Repeat step 1 and step 2

3. Host jump code

RePlugin.startActivity(this, RePlugin.createIntent("plugin1", "com.test.qby.myapplication.plugin.PluginActivity"))

Of course, you can still use the package name to jump.

Above only personal learning records, if there are omissions or errors, welcome to leave a message!

Posted by bagnallc on Wed, 01 Apr 2020 00:18:29 -0700