Search in sources :

Example 1 with IPrivilegedService

use of org.fdroid.fdroid.privileged.IPrivilegedService in project fdroidclient by f-droid.

the class PrivilegedInstaller method installPackageInternal.

@Override
protected void installPackageInternal(final Uri localApkUri, final Uri canonicalUri) {
    ServiceConnection mServiceConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName name, IBinder service) {
            IPrivilegedService privService = IPrivilegedService.Stub.asInterface(service);
            IPrivilegedCallback callback = new IPrivilegedCallback.Stub() {

                @Override
                public void handleResult(String packageName, int returnCode) throws RemoteException {
                    if (returnCode == INSTALL_SUCCEEDED) {
                        sendBroadcastInstall(canonicalUri, ACTION_INSTALL_COMPLETE);
                    } else {
                        sendBroadcastInstall(canonicalUri, ACTION_INSTALL_INTERRUPTED, "Error " + returnCode + ": " + INSTALL_RETURN_CODES.get(returnCode));
                    }
                }
            };
            try {
                boolean hasPermissions = privService.hasPrivilegedPermissions();
                if (!hasPermissions) {
                    sendBroadcastInstall(canonicalUri, ACTION_INSTALL_INTERRUPTED, context.getString(R.string.system_install_denied_permissions));
                    return;
                }
                privService.installPackage(localApkUri, ACTION_INSTALL_REPLACE_EXISTING, null, callback);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException", e);
                sendBroadcastInstall(canonicalUri, ACTION_INSTALL_INTERRUPTED, "connecting to privileged service failed");
            }
        }

        public void onServiceDisconnected(ComponentName name) {
        }
    };
    Intent serviceIntent = new Intent(PRIVILEGED_EXTENSION_SERVICE_INTENT);
    serviceIntent.setPackage(PRIVILEGED_EXTENSION_PACKAGE_NAME);
    context.getApplicationContext().bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) ComponentName(android.content.ComponentName) Intent(android.content.Intent) IPrivilegedService(org.fdroid.fdroid.privileged.IPrivilegedService) RemoteException(android.os.RemoteException) IPrivilegedCallback(org.fdroid.fdroid.privileged.IPrivilegedCallback)

Example 2 with IPrivilegedService

use of org.fdroid.fdroid.privileged.IPrivilegedService in project fdroidclient by f-droid.

the class PrivilegedInstaller method uninstallPackage.

@Override
protected void uninstallPackage() {
    ServiceConnection mServiceConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName name, IBinder service) {
            IPrivilegedService privService = IPrivilegedService.Stub.asInterface(service);
            IPrivilegedCallback callback = new IPrivilegedCallback.Stub() {

                @Override
                public void handleResult(String packageName, int returnCode) throws RemoteException {
                    if (returnCode == DELETE_SUCCEEDED) {
                        sendBroadcastUninstall(ACTION_UNINSTALL_COMPLETE);
                    } else {
                        sendBroadcastUninstall(ACTION_UNINSTALL_INTERRUPTED, "Error " + returnCode + ": " + UNINSTALL_RETURN_CODES.get(returnCode));
                    }
                }
            };
            try {
                boolean hasPermissions = privService.hasPrivilegedPermissions();
                if (!hasPermissions) {
                    sendBroadcastUninstall(ACTION_UNINSTALL_INTERRUPTED, context.getString(R.string.system_install_denied_permissions));
                    return;
                }
                privService.deletePackage(apk.packageName, 0, callback);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException", e);
                sendBroadcastUninstall(ACTION_UNINSTALL_INTERRUPTED, "connecting to privileged service failed");
            }
        }

        public void onServiceDisconnected(ComponentName name) {
        }
    };
    Intent serviceIntent = new Intent(PRIVILEGED_EXTENSION_SERVICE_INTENT);
    serviceIntent.setPackage(PRIVILEGED_EXTENSION_PACKAGE_NAME);
    context.getApplicationContext().bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) ComponentName(android.content.ComponentName) Intent(android.content.Intent) IPrivilegedService(org.fdroid.fdroid.privileged.IPrivilegedService) RemoteException(android.os.RemoteException) IPrivilegedCallback(org.fdroid.fdroid.privileged.IPrivilegedCallback)

Example 3 with IPrivilegedService

use of org.fdroid.fdroid.privileged.IPrivilegedService in project fdroidclient by f-droid.

the class InstalledAppProviderService method compareToPackageManager.

/**
 * Make sure that {@link InstalledAppProvider}, our database of installed apps,
 * is in sync with what the {@link PackageManager} tells us is installed. Once
 * completed, the relevant {@link android.content.ContentProvider}s will be
 * notified of any changes to installed statuses.  The packages are processed
 * in alphabetically order so that "{@code android}" is processed first.  That
 * is always present and signed by the system key, so it is the source of the
 * system key for comparing all packages.
 * <p>
 * The installed app cache could get out of sync, e.g. if F-Droid crashed/ or
 * ran out of battery half way through responding to {@link Intent#ACTION_PACKAGE_ADDED}.
 * This method returns immediately, and will continue to work in an
 * {@link JobIntentService}.  It doesn't really matter where we put this in the
 * bootstrap process, because it runs in its own thread, at the lowest priority:
 * {@link Process#THREAD_PRIORITY_LOWEST}.
 * <p>
 * APKs installed in {@code /system} will often have zeroed out timestamps, like
 * 2008-01-01 (ziptime) or 2009-01-01.  So instead anything older than 2010 every
 * time since we have no way to know whether an APK wasn't changed as part of an
 * OTA update.  An OTA update could change the APK without changing the
 * {@link PackageInfo#versionCode} or {@link PackageInfo#lastUpdateTime}.
 *
 * @see <a href="https://gitlab.com/fdroid/fdroidclient/issues/819>issue #819</a>
 */
public static void compareToPackageManager(final Context context) {
    Utils.debugLog(TAG, "Comparing package manager to our installed app cache.");
    if (Build.VERSION.SDK_INT >= 29 && PrivilegedInstaller.isExtensionInstalledCorrectly(context) == PrivilegedInstaller.IS_EXTENSION_INSTALLED_YES) {
        ServiceConnection mServiceConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IPrivilegedService privService = IPrivilegedService.Stub.asInterface(service);
                List<PackageInfo> packageInfoList = null;
                try {
                    packageInfoList = privService.getInstalledPackages(PackageManager.GET_SIGNATURES);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                compareToPackageManager(context, packageInfoList);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
            // Nothing to tear down from onServiceConnected
            }
        };
        Intent serviceIntent = new Intent(PrivilegedInstaller.PRIVILEGED_EXTENSION_SERVICE_INTENT);
        serviceIntent.setPackage(PrivilegedInstaller.PRIVILEGED_EXTENSION_PACKAGE_NAME);
        context.getApplicationContext().bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
    } else {
        compareToPackageManager(context, null);
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) PackageInfo(android.content.pm.PackageInfo) ComponentName(android.content.ComponentName) Intent(android.content.Intent) IPrivilegedService(org.fdroid.fdroid.privileged.IPrivilegedService) RemoteException(android.os.RemoteException)

Aggregations

ComponentName (android.content.ComponentName)3 Intent (android.content.Intent)3 ServiceConnection (android.content.ServiceConnection)3 IBinder (android.os.IBinder)3 RemoteException (android.os.RemoteException)3 IPrivilegedService (org.fdroid.fdroid.privileged.IPrivilegedService)3 IPrivilegedCallback (org.fdroid.fdroid.privileged.IPrivilegedCallback)2 PackageInfo (android.content.pm.PackageInfo)1