Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8a2fc2a3 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Improve SplitInstallService with callbacks

parent c0ba5a55
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
/*
 * Copyright ECORP SAS 2022-2023
 *
 * 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 foundation.e.splitinstall;

import foundation.e.splitinstall.ISplitInstallSystemServiceCallback;

/**
* Interface towards the SplitInstallService.
*/
@@ -12,5 +30,5 @@ interface ISplitInstallService {
     *                     for.
     * @param modulePath: the path of the split module apk we want to install.
     */
    oneway void installSplitModule(in String packageName, in String modulePath);
    oneway void installSplitModule(in String packageName, in String modulePath, in ISplitInstallSystemServiceCallback callback);
}
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright ECORP SAS 2022-2023
 *
 * 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 foundation.e.splitinstall;

import android.os.Bundle;

interface ISplitInstallSystemServiceCallback {
    void onStartInstall(int sessionId);
    void onInstalled(int sessionId);
    void onError(int errorCode);
}
+3 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ android_app {

    srcs: [
	"java/**/*.kt",
	"kotlin/**/*.kt",
    ],

    static_libs: [
+61 −27
Original line number Diff line number Diff line
/*
 * Copyright ECORP SAS 2022
 * Copyright ECORP SAS 2022-2023
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -22,19 +22,32 @@ import android.content.Intent
import android.content.pm.PackageInstaller
import android.util.Log
import foundation.e.splitinstall.ISplitInstallService
import foundation.e.splitinstall.ISplitInstallSystemServiceCallback
import java.io.File
import android.os.Handler
import android.os.Bundle

class SplitInstallBinder(
    val applicationContext: Context
    val applicationContext: Context,
    val handler: Handler
) : ISplitInstallService.Stub() {

    private var packageName: String? = null
    private var modulePath: String? = null

    private val sessionCallbacks = HashMap<String, PackageInstaller.SessionCallback>()

    companion object {
        const val TAG = SplitInstallService.TAG
    }

    override fun installSplitModule(packageName: String, modulePath: String) {
    override fun installSplitModule(packageName: String, modulePath: String, callback: ISplitInstallSystemServiceCallback) {

        Log.i(TAG, "installing $modulePath")
        this.packageName = packageName
        this.modulePath = modulePath

        handler.post {
            Log.i(TAG, "about to install $modulePath")
            val packageManager = applicationContext.packageManager
            val packageInstaller = packageManager.packageInstaller
            val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_INHERIT_EXISTING)
@@ -46,6 +59,25 @@ class SplitInstallBinder(
            val sessionId = packageInstaller.createSession(params)
            val session = packageInstaller.openSession(sessionId)

            sessionCallbacks[modulePath] = object : PackageInstaller.SessionCallback() {
                override fun onActiveChanged(sessionId: Int, active: Boolean) {
                    callback.onStartInstall(0)
                }

                override fun onBadgingChanged(sessionId: Int) { }

                override fun onCreated(sessionId: Int) { }

                override fun onFinished(sessionId: Int, success: Boolean) {
                    callback.onInstalled(0)
                    applicationContext.packageManager.packageInstaller.unregisterSessionCallback(this)
                }

                override fun onProgressChanged(sessionId: Int, progress: Float) { }
            }

            packageInstaller.registerSessionCallback(sessionCallbacks[modulePath])

            try {
                syncFile(session, File(modulePath))

@@ -53,9 +85,10 @@ class SplitInstallBinder(
                callbackIntent.action = SplitInstallBroadcastReceiver.MODULE_INSTALLED_INTENT_ACTION

                val flags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
            val servicePendingIntent =
                val pendingIntent =
                    PendingIntent.getBroadcast(applicationContext, sessionId, callbackIntent, flags)
            session.commit(servicePendingIntent.intentSender)

                session.commit(pendingIntent.intentSender)
            } catch (e: Exception) {
                session.abandon()
                throw e
@@ -63,6 +96,7 @@ class SplitInstallBinder(
                session.close()
            }
        }
    }

    private fun syncFile(session: PackageInstaller.Session, file: File) {
        val inputStream = file.inputStream()
+12 −2
Original line number Diff line number Diff line
/*
 * Copyright ECORP SAS 2022
 * Copyright ECORP SAS 2022-2023
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ import android.os.Build
import android.os.IBinder
import android.util.Log
import foundation.e.splitinstall.service.R
import android.os.Handler;
import android.os.HandlerThread;

import java.io.File

@@ -38,7 +40,15 @@ class SplitInstallService : Service() {
        const val TAG = "SplitInstallSysService"
    }

    override fun onCreate() {
	super.onCreate()
    }

    override fun onBind(intent: Intent?): IBinder? {
        return SplitInstallBinder(applicationContext)
        val thread = HandlerThread("InstallThread")
        thread.start()
        val handler = Handler(thread.getLooper())

        return SplitInstallBinder(applicationContext, handler)
    }
}
+1 −1

File changed.

Contains only whitespace changes.

+5 −5

File changed.

Contains only whitespace changes.

Loading