Java Code Examples for android.content.pm.PackageInstaller#Session

The following examples show how to use android.content.pm.PackageInstaller#Session . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DummyActivity.java    From Shelter with Do What The F*ck You Want To Public License 6 votes vote down vote up
private void actionInstallPackageQ(Uri uri) throws IOException {
    PackageInstaller pi = getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    int sessionId = pi.createSession(params);

    // Show the progress dialog first
    pi.registerSessionCallback(new InstallationProgressListener(this, pi, sessionId));

    PackageInstaller.Session session = pi.openSession(sessionId);
    doInstallPackageQ(uri, session, () -> {
        // We have finished piping the streams, show the progress as 10%
        session.setStagingProgress(0.1f);

        // Commit the session
        Intent intent = new Intent(this, DummyActivity.class);
        intent.setAction(PACKAGEINSTALLER_CALLBACK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        session.commit(pendingIntent.getIntentSender());
    });
}
 
Example 2
Source File: PackageInstallationUtils.java    From android-testdpc with Apache License 2.0 6 votes vote down vote up
public static boolean installPackage(Context context, InputStream in, String packageName)
        throws IOException {
    final PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    final PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);
    // set params
    final int sessionId = packageInstaller.createSession(params);
    final PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    final OutputStream out = session.openWrite("TestDPC", 0, -1);
    final byte[] buffer = new byte[65536];
    int c;
    while ((c = in.read(buffer)) != -1) {
        out.write(buffer, 0, c);
    }
    session.fsync(out);
    in.close();
    out.close();

    session.commit(createInstallIntentSender(context, sessionId));
    return true;
}
 
Example 3
Source File: VersionFixer.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private static boolean installPackage(Context context, byte[] buffer, String packageName)
        throws IOException {
    final PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    final PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);

    final int sessionId = packageInstaller.createSession(params);
    final PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    final OutputStream out = session.openWrite("COSU", 0, -1);
    out.write(buffer, 0, buffer.length);

    session.fsync(out);
    out.close();
    session.commit(createIntentSender(context, sessionId));
    return true;
}
 
Example 4
Source File: VersionFixer.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private static boolean installPackage(Context context, InputStream in, String packageName)
        throws IOException {
    final PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    final PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);

    final int sessionId = packageInstaller.createSession(params);
    final PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    final OutputStream out = session.openWrite("COSU", 0, -1);
    final byte[] buffer = new byte[65536];
    int bytes_read;
    while ((bytes_read = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytes_read);
    }
    session.fsync(out);
    in.close();
    out.close();

    session.commit(createIntentSender(context, sessionId));
    return true;
}
 
Example 5
Source File: VersionFixer.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private static boolean installPackage(Context context, byte[] buffer, String packageName)
        throws IOException {
    final PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    final PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);

    final int sessionId = packageInstaller.createSession(params);
    final PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    final OutputStream out = session.openWrite("COSU", 0, -1);
    out.write(buffer, 0, buffer.length);

    session.fsync(out);
    out.close();
    session.commit(createIntentSender(context, sessionId));
    return true;
}
 
Example 6
Source File: VersionFixer.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private static boolean installPackage(Context context, InputStream in, String packageName)
        throws IOException {
    final PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    final PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);

    final int sessionId = packageInstaller.createSession(params);
    final PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    final OutputStream out = session.openWrite("COSU", 0, -1);
    final byte[] buffer = new byte[65536];
    int bytes_read;
    while ((bytes_read = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytes_read);
    }
    session.fsync(out);
    in.close();
    out.close();

    session.commit(createIntentSender(context, sessionId));
    return true;
}
 
Example 7
Source File: InstallerPrivilegedSession.java    From YalpStore with GNU General Public License v2.0 6 votes vote down vote up
private void writeFileToSession(File file, PackageInstaller.Session session) throws IOException {
    InputStream in = context.getContentResolver().openInputStream(FileProvider.getUriForFile(
        context,
        BuildConfig.APPLICATION_ID + ".fileprovider",
        file
    ));
    OutputStream out = session.openWrite(file.getName(), 0, file.length());
    try {
        int c;
        byte[] buffer = new byte[65536];
        while ((c = in.read(buffer)) != -1) {
            out.write(buffer, 0, c);
        }
        session.fsync(out);
    } finally {
        com.github.yeriomin.yalpstore.Util.closeSilently(in);
        com.github.yeriomin.yalpstore.Util.closeSilently(out);
    }
}
 
Example 8
Source File: InstallerPrivilegedSession.java    From YalpStore with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void install(App app) {
    registerReceiver();
    PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    sessionParams.setAppPackageName(app.getPackageName());
    sessionParams.setAppLabel(app.getDisplayName());
    PackageInstaller.Session session = null;
    try {
        int sessionId = packageInstaller.createSession(sessionParams);
        session = packageInstaller.openSession(sessionId);
        for (File file: Paths.getApkAndSplits(context, app.getPackageName(), app.getVersionCode())) {
            writeFileToSession(file, session);
        }
        session.commit(getIntentSender(sessionId));
    } catch (IOException e) {
        fail(e, app.getPackageName());
    } finally {
        com.github.yeriomin.yalpstore.Util.closeSilently(session);
    }
}
 
Example 9
Source File: MainActivity.java    From splitapkinstall with Apache License 2.0 6 votes vote down vote up
private int doCommitSession(int sessionId, boolean logSuccess) throws RemoteException {
    PackageInstaller.Session session = null;
    try {
        try {
            session = packageInstaller.openSession(sessionId);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Intent callbackIntent = new Intent(getApplicationContext(), APKInstallService.class);
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, callbackIntent, 0);
        session.commit(pendingIntent.getIntentSender());
        session.close();

        System.out.println("install request sent");

        Log.d(TAG, "doCommitSession: " + packageInstaller.getMySessions());
        Log.d(TAG,
                "doCommitSession: after session commit ");
        return 1;
    } finally {
        session.close();
    }
}
 
Example 10
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private int doRemoveSplit(int sessionId, String splitName, boolean logSuccess)
        throws RemoteException {
    final PrintWriter pw = getOutPrintWriter();
    PackageInstaller.Session session = null;
    try {
        session = new PackageInstaller.Session(
                mInterface.getPackageInstaller().openSession(sessionId));
        session.removeSplit(splitName);

        if (logSuccess) {
            pw.println("Success");
        }
        return 0;
    } catch (IOException e) {
        pw.println("Error: failed to remove split; " + e.getMessage());
        return 1;
    } finally {
        IoUtils.closeQuietly(session);
    }
}
 
Example 11
Source File: RootlessSAIPackageInstaller.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("DefaultLocale")
@Override
protected void installApkFiles(ApkSource aApkSource) {
    cleanOldSessions();

    PackageInstaller.Session session = null;
    try (ApkSource apkSource = aApkSource) {
        PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
        sessionParams.setInstallLocation(PreferencesHelper.getInstance(getContext()).getInstallLocation());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            sessionParams.setInstallReason(PackageManager.INSTALL_REASON_USER);

        int sessionID = mPackageInstaller.createSession(sessionParams);
        mSessionsMap.put(sessionID, getOngoingInstallation().getId());

        session = mPackageInstaller.openSession(sessionID);
        int currentApkFile = 0;
        while (apkSource.nextApk()) {
            try (InputStream inputStream = apkSource.openApkInputStream(); OutputStream outputStream = session.openWrite(String.format("%d.apk", currentApkFile++), 0, apkSource.getApkLength())) {
                IOUtils.copyStream(inputStream, outputStream);
                session.fsync(outputStream);
            }
        }

        Intent callbackIntent = new Intent(getContext(), RootlessSAIPIService.class);
        PendingIntent pendingIntent = PendingIntent.getService(getContext(), 0, callbackIntent, 0);
        session.commit(pendingIntent.getIntentSender());
    } catch (Exception e) {
        Log.w(TAG, e);
        dispatchCurrentSessionUpdate(InstallationStatus.INSTALLATION_FAILED, getContext().getString(R.string.installer_error_rootless, Utils.throwableToString(e)));
        installationCompleted();
    } finally {
        if (session != null)
            session.close();
    }
}
 
Example 12
Source File: DummyActivity.java    From Shelter with Do What The F*ck You Want To Public License 5 votes vote down vote up
private void doInstallPackageQ(Uri uri, PackageInstaller.Session session, Runnable callback) {
    new Thread(() -> {
        try (InputStream is = getContentResolver().openInputStream(uri);
             OutputStream os = session.openWrite(UUID.randomUUID().toString(), 0, is.available())
        ) {
            Utility.pipe(is, os);
            session.fsync(os);
        } catch (IOException e) {

        }

        runOnUiThread(callback);
    }).start();
}
 
Example 13
Source File: InstallActivity.java    From XAPKInstaller with Apache License 2.0 5 votes vote down vote up
@TargetApi(21)
private void commitSession(PackageInstaller.Session session) {

    // Create an install status receiver.

    Intent intent = new Intent(this, InstallActivity.class);
    intent.setAction(PACKAGE_INSTALLED_ACTION);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    IntentSender statusReceiver = pendingIntent.getIntentSender();

    // Commit the session (this will start the installation workflow).
    session.commit(statusReceiver);

}
 
Example 14
Source File: InstallActivity.java    From XAPKInstaller with Apache License 2.0 5 votes vote down vote up
@TargetApi(21)
private void addApkToInstallSession(String filePath, PackageInstaller.Session session)
        throws IOException {
    // It's recommended to pass the file size to openWrite(). Otherwise installation may fail
    // if the disk is almost full.
    try (OutputStream packageInSession = session.openWrite(getFileName(filePath), 0, new File(filePath).length());
         InputStream is = new BufferedInputStream(new FileInputStream(filePath))) {
        byte[] buffer = new byte[16384];
        int n;
        while ((n = is.read(buffer)) >= 0) {
            packageInSession.write(buffer, 0, n);
        }
    }
}
 
Example 15
Source File: InstallActivity.java    From XAPKInstaller with Apache License 2.0 5 votes vote down vote up
@TargetApi(21)
private PackageInstaller.Session initSession() throws IOException {
    PackageInstaller.Session session = null;
    PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    int sessionId = 0;
    sessionId = packageInstaller.createSession(params);
    session = packageInstaller.openSession(sessionId);

    return session;
}
 
Example 16
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private int doAbandonSession(int sessionId, boolean logSuccess) throws RemoteException {
    final PrintWriter pw = getOutPrintWriter();
    PackageInstaller.Session session = null;
    try {
        session = new PackageInstaller.Session(
                mInterface.getPackageInstaller().openSession(sessionId));
        session.abandon();
        if (logSuccess) {
            pw.println("Success");
        }
        return 0;
    } finally {
        IoUtils.closeQuietly(session);
    }
}
 
Example 17
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private int doCommitSession(int sessionId, boolean logSuccess) throws RemoteException {
    final PrintWriter pw = getOutPrintWriter();
    PackageInstaller.Session session = null;
    try {
        session = new PackageInstaller.Session(
                mInterface.getPackageInstaller().openSession(sessionId));

        // Sanity check that all .dm files match an apk.
        // (The installer does not support standalone .dm files and will not process them.)
        try {
            DexMetadataHelper.validateDexPaths(session.getNames());
        } catch (IllegalStateException | IOException e) {
            pw.println("Warning [Could not validate the dex paths: " + e.getMessage() + "]");
        }

        final LocalIntentReceiver receiver = new LocalIntentReceiver();
        session.commit(receiver.getIntentSender());

        final Intent result = receiver.getResult();
        final int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS,
                PackageInstaller.STATUS_FAILURE);
        if (status == PackageInstaller.STATUS_SUCCESS) {
            if (logSuccess) {
                pw.println("Success");
            }
        } else {
            pw.println("Failure ["
                    + result.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) + "]");
        }
        return status;
    } finally {
        IoUtils.closeQuietly(session);
    }
}
 
Example 18
Source File: RootlessSaiPackageInstaller.java    From SAI with GNU General Public License v3.0 4 votes vote down vote up
private void install(String sessionId, SaiPiSessionParams params) {
    PackageInstaller.Session session = null;
    String appTempName = null;
    try (ApkSource apkSource = params.apkSource()) {
        appTempName = apkSource.getAppName();
        if (appTempName != null)
            mSessionIdToAppTempName.put(sessionId, appTempName);

        setSessionState(sessionId, new SaiPiSessionState.Builder(sessionId, SaiPiSessionStatus.INSTALLING).appTempName(appTempName).build());

        PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
        sessionParams.setInstallLocation(PreferencesHelper.getInstance(getContext()).getInstallLocation());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            sessionParams.setInstallReason(PackageManager.INSTALL_REASON_USER);

        int androidSessionId = mPackageInstaller.createSession(sessionParams);
        mAndroidPiSessionIdToSaiPiSessionId.put(androidSessionId, sessionId);

        session = mPackageInstaller.openSession(androidSessionId);
        int currentApkFile = 0;
        while (apkSource.nextApk()) {
            try (InputStream inputStream = apkSource.openApkInputStream(); OutputStream outputStream = session.openWrite(String.format("%d.apk", currentApkFile++), 0, apkSource.getApkLength())) {
                IOUtils.copyStream(inputStream, outputStream);
                session.fsync(outputStream);
            }
        }

        Intent callbackIntent = new Intent(RootlessSaiPiBroadcastReceiver.ACTION_DELIVER_PI_EVENT);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, callbackIntent, 0);
        session.commit(pendingIntent.getIntentSender());
    } catch (Exception e) {
        Log.w(TAG, e);
        if (session != null)
            session.abandon();

        setSessionState(sessionId, new SaiPiSessionState.Builder(sessionId, SaiPiSessionStatus.INSTALLATION_FAILED).appTempName(appTempName).error(e.getLocalizedMessage(), Utils.throwableToString(e)).build());
    } finally {
        if (session != null)
            session.close();
    }
}