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

Commit e7b9618e authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Don't cancel by UID when an app is disabled or uninstalled

Instead, cancel by UID/package.

Bug: 64536115
Test: cts-tradefed run cts-dev --skip-device-info --skip-preconditions --skip-system-status-check com.android.compatibility.common.tradefed.targetprep.NetworkConnectivityChecker -a armeabi-v7a -l INFO -m CtsJobSchedulerTestCases
Test: adb shell pm disable com.google.android.hiddenmenu and
    adb shell pm enable com.google.android.hiddenmenu and check logcat
Change-Id: I8f50c459cf321ac43fd2a6696cb8d4c593accd67
parent 5285e053
Loading
Loading
Loading
Loading
+14 −13
Original line number Diff line number Diff line
@@ -501,11 +501,12 @@ public final class JobSchedulerService extends com.android.server.SystemService
            if (DEBUG) {
                Slog.d(TAG, "Receieved: " + action);
            }
            final String pkgName = getPackageName(intent);
            final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1);

            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
                // Purge the app's jobs if the whole package was just disabled.  When this is
                // the case the component name will be a bare package name.
                final String pkgName = getPackageName(intent);
                final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
                if (pkgName != null && pkgUid != -1) {
                    final String[] changedComponents = intent.getStringArrayExtra(
                            Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
@@ -525,7 +526,8 @@ public final class JobSchedulerService extends com.android.server.SystemService
                                            Slog.d(TAG, "Removing jobs for package " + pkgName
                                                    + " in user " + userId);
                                        }
                                        cancelJobsForUid(pkgUid, "app package state changed");
                                        cancelJobsForPackageAndUid(pkgName, pkgUid,
                                                "app disabled");
                                    }
                                } catch (RemoteException|IllegalArgumentException e) {
                                    /*
@@ -554,7 +556,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
                    if (DEBUG) {
                        Slog.d(TAG, "Removing jobs for uid: " + uidRemoved);
                    }
                    cancelJobsForUid(uidRemoved, "app uninstalled");
                    cancelJobsForPackageAndUid(pkgName, uidRemoved, "app uninstalled");
                }
            } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
@@ -565,8 +567,6 @@ public final class JobSchedulerService extends com.android.server.SystemService
            } else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
                // Has this package scheduled any jobs, such that we will take action
                // if it were to be force-stopped?
                final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
                final String pkgName = intent.getData().getSchemeSpecificPart();
                if (pkgUid != -1) {
                    List<JobStatus> jobsForUid;
                    synchronized (mLock) {
@@ -585,13 +585,11 @@ public final class JobSchedulerService extends com.android.server.SystemService
                }
            } else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) {
                // possible force-stop
                final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
                final String pkgName = intent.getData().getSchemeSpecificPart();
                if (pkgUid != -1) {
                    if (DEBUG) {
                        Slog.d(TAG, "Removing jobs for pkg " + pkgName + " at uid " + pkgUid);
                    }
                    cancelJobsForPackageAndUid(pkgName, pkgUid);
                    cancelJobsForPackageAndUid(pkgName, pkgUid, "app force stopped");
                }
            }
        }
@@ -762,13 +760,17 @@ public final class JobSchedulerService extends com.android.server.SystemService
        }
    }

    void cancelJobsForPackageAndUid(String pkgName, int uid) {
    void cancelJobsForPackageAndUid(String pkgName, int uid, String reason) {
        if ("android".equals(pkgName)) {
            Slog.wtfStack(TAG, "Can't cancel all jobs for system package");
            return;
        }
        synchronized (mLock) {
            final List<JobStatus> jobsForUid = mJobs.getJobsByUid(uid);
            for (int i = jobsForUid.size() - 1; i >= 0; i--) {
                final JobStatus job = jobsForUid.get(i);
                if (job.getSourcePackageName().equals(pkgName)) {
                    cancelJobImplLocked(job, null, "app force stopped");
                    cancelJobImplLocked(job, null, reason);
                }
            }
        }
@@ -783,8 +785,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
     */
    public void cancelJobsForUid(int uid, String reason) {
        if (uid == Process.SYSTEM_UID) {
            // This really shouldn't happen.
            Slog.wtfStack(TAG, "cancelJobsForUid() called for system uid");
            Slog.wtfStack(TAG, "Can't cancel all jobs for system uid");
            return;
        }
        synchronized (mLock) {