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

Commit 0cd474f6 authored by Felipe Leme's avatar Felipe Leme
Browse files

Added UserManagerInternal.UserLifecycleListener

Test: manual verification on automotive
Test: adb shell dumpsys user | grep "user lifecycle"

Bug: 155913815

Change-Id: I8a9e3e2008dd03545e1f0a8562754040d87813da
parent 2923b4eb
Loading
Loading
Loading
Loading
+20 −0
Original line number Original line Diff line number Diff line
@@ -56,6 +56,20 @@ public abstract class UserManagerInternal {
        void onUserRestrictionsChanged(int userId, Bundle newRestrictions, Bundle prevRestrictions);
        void onUserRestrictionsChanged(int userId, Bundle newRestrictions, Bundle prevRestrictions);
    }
    }


    /**
     * Listener for user lifecycle events.
     *
     * <p><b>NOTE: </b>implementations MUST not block the current thread.
     */
    public interface UserLifecycleListener {

        /** Called when a new user is created. */
        default void onUserCreated(UserInfo user) {}

        /** Called when an existing user is removed. */
        default void onUserRemoved(UserInfo user) {}
    }

    /**
    /**
     * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to set
     * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to set
     * restrictions enforced by the user.
     * restrictions enforced by the user.
@@ -97,6 +111,12 @@ public abstract class UserManagerInternal {
    /** Remove a {@link UserRestrictionsListener}. */
    /** Remove a {@link UserRestrictionsListener}. */
    public abstract void removeUserRestrictionsListener(UserRestrictionsListener listener);
    public abstract void removeUserRestrictionsListener(UserRestrictionsListener listener);


    /** Adds a {@link UserLifecycleListener}. */
    public abstract void addUserLifecycleListener(UserLifecycleListener listener);

    /** Removes a {@link UserLifecycleListener}. */
    public abstract void removeUserLifecycleListener(UserLifecycleListener listener);

    /**
    /**
     * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to update
     * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to update
     * whether the device is managed by device owner.
     * whether the device is managed by device owner.
+42 −1
Original line number Original line Diff line number Diff line
@@ -110,6 +110,7 @@ import com.android.server.LocalServices;
import com.android.server.LockGuard;
import com.android.server.LockGuard;
import com.android.server.SystemService;
import com.android.server.SystemService;
import com.android.server.am.UserState;
import com.android.server.am.UserState;
import com.android.server.pm.UserManagerInternal.UserLifecycleListener;
import com.android.server.pm.UserManagerInternal.UserRestrictionsListener;
import com.android.server.pm.UserManagerInternal.UserRestrictionsListener;
import com.android.server.storage.DeviceStorageMonitorInternal;
import com.android.server.storage.DeviceStorageMonitorInternal;
import com.android.server.utils.TimingsTraceAndSlog;
import com.android.server.utils.TimingsTraceAndSlog;
@@ -438,6 +439,9 @@ public class UserManagerService extends IUserManager.Stub {
    private final ArrayList<UserRestrictionsListener> mUserRestrictionsListeners =
    private final ArrayList<UserRestrictionsListener> mUserRestrictionsListeners =
            new ArrayList<>();
            new ArrayList<>();


    @GuardedBy("mUserRemovedListeners")
    private final ArrayList<UserLifecycleListener> mUserLifecycleListeners = new ArrayList<>();

    private final LockPatternUtils mLockPatternUtils;
    private final LockPatternUtils mLockPatternUtils;


    private final String ACTION_DISABLE_QUIET_MODE_AFTER_UNLOCK =
    private final String ACTION_DISABLE_QUIET_MODE_AFTER_UNLOCK =
@@ -3645,6 +3649,14 @@ public class UserManagerService extends IUserManager.Stub {
    }
    }


    private void dispatchUserAdded(@NonNull UserInfo userInfo) {
    private void dispatchUserAdded(@NonNull UserInfo userInfo) {
        // Notify internal listeners first...
        synchronized (mUserLifecycleListeners) {
            for (int i = 0; i < mUserLifecycleListeners.size(); i++) {
                mUserLifecycleListeners.get(i).onUserCreated(userInfo);
            }
        }

        //...then external ones
        Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
        Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
        addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
        addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
        // Also, add the UserHandle for mainline modules which can't use the @hide
        // Also, add the UserHandle for mainline modules which can't use the @hide
@@ -4030,13 +4042,19 @@ public class UserManagerService extends IUserManager.Stub {
            user = getUserInfoLU(userId);
            user = getUserInfoLU(userId);
        }
        }
        if (user != null && user.preCreated) {
        if (user != null && user.preCreated) {
            Slog.i(LOG_TAG, "Removing a precreated user with user id: " + userId);
            Slog.i(LOG_TAG, "Removing a pre-created user with user id: " + userId);
            // Don't want to fire ACTION_USER_REMOVED, so cleanup the state and exit early.
            // Don't want to fire ACTION_USER_REMOVED, so cleanup the state and exit early.
            LocalServices.getService(ActivityTaskManagerInternal.class).onUserStopped(userId);
            LocalServices.getService(ActivityTaskManagerInternal.class).onUserStopped(userId);
            removeUserState(userId);
            removeUserState(userId);
            return;
            return;
        }
        }


        synchronized (mUserLifecycleListeners) {
            for (int i = 0; i < mUserLifecycleListeners.size(); i++) {
                mUserLifecycleListeners.get(i).onUserRemoved(user);
            }
        }

        // Let other services shutdown any activity and clean up their state before completely
        // Let other services shutdown any activity and clean up their state before completely
        // wiping the user's system directory and removing from the user list
        // wiping the user's system directory and removing from the user list
        final long ident = Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();
@@ -4973,6 +4991,15 @@ public class UserManagerService extends IUserManager.Stub {
            pw.println("  System user allocations: " + mUser0Allocations.get());
            pw.println("  System user allocations: " + mUser0Allocations.get());
        }
        }


        pw.println();
        pw.println("Number of listeners for");
        synchronized (mUserRestrictionsListeners) {
            pw.println("  restrictions: " + mUserRestrictionsListeners.size());
        }
        synchronized (mUserLifecycleListeners) {
            pw.println("  user lifecycle events: " + mUserLifecycleListeners.size());
        }

        // Dump UserTypes
        // Dump UserTypes
        pw.println();
        pw.println();
        pw.println("User types version: " + mUserTypeVersion);
        pw.println("User types version: " + mUserTypeVersion);
@@ -5090,6 +5117,20 @@ public class UserManagerService extends IUserManager.Stub {
            }
            }
        }
        }


        @Override
        public void addUserLifecycleListener(UserLifecycleListener listener) {
            synchronized (mUserLifecycleListeners) {
                mUserLifecycleListeners.add(listener);
            }
        }

        @Override
        public void removeUserLifecycleListener(UserLifecycleListener listener) {
            synchronized (mUserLifecycleListeners) {
                mUserLifecycleListeners.remove(listener);
            }
        }

        @Override
        @Override
        public void setDeviceManaged(boolean isManaged) {
        public void setDeviceManaged(boolean isManaged) {
            synchronized (mUsersLock) {
            synchronized (mUsersLock) {