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

Commit c1a4e5dc authored by Fred Quintana's avatar Fred Quintana
Browse files

Make the AccountManagerService clear old grants when the package

that the grants refer to is no longer installed.

Bug: 3425856
Change-Id: I6ec057415c8f35be91c8434ff10e6cf5c1fc47bb
parent 5381b467
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -211,9 +211,44 @@ public class AccountManagerService
        mSimWatcher = new SimWatcher(mContext);
        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();