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

Commit 082aa9cd authored by Fred Quintana's avatar Fred Quintana Committed by Android (Google) Code Review
Browse files

Merge "Make the AccountManagerService clear old grants when the package that...

Merge "Make the AccountManagerService clear old grants when the package that the grants refer to is no longer installed."
parents bb579480 c1a4e5dc
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -209,9 +209,44 @@ public class AccountManagerService

        sThis.set(this);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addDataScheme("package");
        mContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context1, Intent intent) {
                purgeOldGrants();
            }
        }, intentFilter);
        purgeOldGrants();

        validateAccountsAndPopulateCache();
    }

    private void purgeOldGrants() {
        synchronized (mCacheLock) {
            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            final Cursor cursor = db.query(TABLE_GRANTS,
                    new String[]{GRANTS_GRANTEE_UID},
                    null, null, GRANTS_GRANTEE_UID, null, null);
            try {
                while (cursor.moveToNext()) {
                    final int uid = cursor.getInt(0);
                    final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null;
                    if (packageExists) {
                        continue;
                    }
                    Log.d(TAG, "deleting grants for UID " + uid
                            + " because its package is no longer installed");
                    db.delete(TABLE_GRANTS, GRANTS_GRANTEE_UID + "=?",
                            new String[]{Integer.toString(uid)});
                }
            } finally {
                cursor.close();
            }
        }
    }

    private void validateAccountsAndPopulateCache() {
        boolean accountDeleted = false;
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();