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

Commit b9da4e46 authored by Fyodor Kupolov's avatar Fyodor Kupolov
Browse files

Handle stopped users in onCleanupUser

This will also take care of the case when the user is removed.

Previously CE database wasn't closed after user was stopped.

Bug: 35988361
Test: Create u10 - switch - add account - switch to u0 - stop u10
Test: Create u10 - switch - add account - switch to u0 - remove u10
Change-Id: I6df61a8b1d00b28f2985d60861dd1b74e3e5942b
parent 352b0f05
Loading
Loading
Loading
Loading
+7 −34
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityThread;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.INotificationManager;
import android.app.Notification;
@@ -64,7 +63,6 @@ import android.content.pm.Signature;
import android.content.pm.UserInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.Environment;
@@ -80,7 +78,6 @@ import android.os.StrictMode;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
@@ -113,7 +110,6 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
@@ -158,6 +154,11 @@ public class AccountManagerService
        public void onUnlockUser(int userHandle) {
            mService.onUnlockUser(userHandle);
        }

        @Override
        public void onCleanupUser(int userHandle) {
            mService.onCleanupUser(userHandle);
        }
    }

    final Context mContext;
@@ -303,18 +304,6 @@ public class AccountManagerService
            }
        }, intentFilter);

        IntentFilter userFilter = new IntentFilter();
        userFilter.addAction(Intent.ACTION_USER_REMOVED);
        mContext.registerReceiverAsUser(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (Intent.ACTION_USER_REMOVED.equals(action)) {
                    onUserRemoved(intent);
                }
            }
        }, UserHandle.ALL, userFilter, null, null);

        injector.addLocalService(new AccountManagerInternalImpl());

        // Need to cancel account request notifications if the update/install can access the account
@@ -1133,16 +1122,12 @@ public class AccountManagerService
        }
    }

    private void onUserRemoved(Intent intent) {
        int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
        if (userId < 1) return;

    private void onCleanupUser(int userId) {
        Log.i(TAG, "onCleanupUser " + userId);
        UserAccounts accounts;
        boolean userUnlocked;
        synchronized (mUsers) {
            accounts = mUsers.get(userId);
            mUsers.remove(userId);
            userUnlocked = mLocalUnlockedUsers.get(userId);
            mLocalUnlockedUsers.delete(userId);
        }
        if (accounts != null) {
@@ -1150,18 +1135,6 @@ public class AccountManagerService
                accounts.accountsDb.close();
            }
        }
        Log.i(TAG, "Removing database files for user " + userId);
        File dbFile = new File(mInjector.getDeDatabaseName(userId));

        AccountsDb.deleteDbFileWarnIfFailed(dbFile);
        // Remove CE file if user is unlocked, or FBE is not enabled
        boolean fbeEnabled = StorageManager.isFileEncryptedNativeOrEmulated();
        if (!fbeEnabled || userUnlocked) {
            File ceDb = new File(mInjector.getCeDatabaseName(userId));
            if (ceDb.exists()) {
                AccountsDb.deleteDbFileWarnIfFailed(ceDb);
            }
        }
    }

    @VisibleForTesting