Susheel Tickoo
Susheel Tickoo

Reputation: 547

Restriction of Targeting API 28 on IPackageInstallObserver and IPackageDeleteObserver

I am using below classes to install other applications through my code:

android.content.pm.IPackageDeleteObserver
android.content.pm.IPackageInstallObserver

And my application is compiling against target API 27 , considering google links link1 ,link2 and link3

I believe if an app is using private API's from grey-list of target API level they are going to become blacklisted for that app.

Now the API's that i am using are added in grey list of API 28, but as long as i have target API level 27 i am able to use them.

The problem is if i move to Target API 28, these API's are going to be blacklisted for my app.

My question is , has anyone else faced the same problem and if there is any solution around this ?

I have raised a ticket in Google issue tracker as well, but noting has been updated on the same since a long time.

Upvotes: 0

Views: 950

Answers (2)

Stanislav Vlasic
Stanislav Vlasic

Reputation: 41

Answer from Maximiliano Gonzalez actually works when converted to Java code and built as system app (as part of firmware) with uuid system. Here's code:

    private void installPackage(File file) throws IOException {
    PackageInstaller pi = getApplicationContext().getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams sp = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    int sessionId = pi.createSession(sp);
    PackageInstaller.Session session = pi.openSession(sessionId);

    long sizeBytes = 0;
    if (file.isFile()) {
        sizeBytes = file.length();
    }

    InputStream input;
    OutputStream out;

    input = new FileInputStream(file.getPath());
    out = session.openWrite("app_store_session", 0, sizeBytes);

    long total = 0;
    byte[] buffer = new byte[65536];

    int len;

    do {
        len = input.read(buffer);
        if (len != -1) {
            total += len;
            out.write(buffer, 0, len);
        }

    } while (len != -1);

    session.fsync(out);
    input.close();
    out.close();

    PendingIntent broadCastTest = PendingIntent.getBroadcast(getApplicationContext(),
                                    sessionId, new Intent(ACTION_INSTALL_COMPLETE),
                                    PendingIntent.FLAG_UPDATE_CURRENT);
    session.commit(broadCastTest.getIntentSender());
    session.close();
}

Upvotes: 1

Maximiliano Gonzalez
Maximiliano Gonzalez

Reputation: 11

I also had the same issue using android.content.pm in API 28. I was able to install packages using this function:

val ACTION_INSTALL_COMPLETE = "com.example.myinstallexample.INSTALL_COMPLETE"


fun installPackage(file: File) {

            val pi = Application.instance.applicationContext.getPackageManager().getPackageInstaller()
            val sessionId =
                pi.createSession(PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL))
            val session = pi.openSession(sessionId)
            var sizeBytes: Long = 0

            if (file.isFile()) {
                sizeBytes = file.length();
            }

            var input: InputStream? = null
            var out: OutputStream? = null

            input = FileInputStream(file.path)

            out = session.openWrite("app_store_session", 0, sizeBytes);

            var total = 0

            val buffer = ByteArray(65536)

            var len: Int

            while(input.read(buffer).also { len = it } != -1) {
                total += len;
                out.write(buffer, 0, len);
            }

            session.fsync(out);

            input.close()

            out.close();

            val broadCastTest = PendingIntent.getBroadcast(
                Application.instance.applicationContext,
                sessionId,
                Intent(ACTION_INSTALL_COMPLETE),
                PendingIntent.FLAG_UPDATE_CURRENT
            )

            session.commit(broadCastTest.getIntentSender());

            session.close();

}

Upvotes: 1

Related Questions