1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.settings;
18 
19 import android.app.ActivityManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.UserHandle;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.systemui.broadcast.BroadcastDispatcher;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.function.Consumer;
32 
33 public abstract class CurrentUserTracker {
34     private final UserReceiver mUserReceiver;
35 
36     private Consumer<Integer> mCallback = this::onUserSwitched;
37 
CurrentUserTracker(BroadcastDispatcher broadcastDispatcher)38     public CurrentUserTracker(BroadcastDispatcher broadcastDispatcher) {
39         this(UserReceiver.getInstance(broadcastDispatcher));
40     }
41 
42     @VisibleForTesting
CurrentUserTracker(UserReceiver receiver)43     CurrentUserTracker(UserReceiver receiver) {
44         mUserReceiver = receiver;
45     }
46 
getCurrentUserId()47     public int getCurrentUserId() {
48         return mUserReceiver.getCurrentUserId();
49     }
50 
startTracking()51     public void startTracking() {
52         mUserReceiver.addTracker(mCallback);
53     }
54 
stopTracking()55     public void stopTracking() {
56         mUserReceiver.removeTracker(mCallback);
57     }
58 
onUserSwitched(int newUserId)59     public abstract void onUserSwitched(int newUserId);
60 
61     @VisibleForTesting
62     static class UserReceiver extends BroadcastReceiver {
63         private static UserReceiver sInstance;
64 
65         private boolean mReceiverRegistered;
66         private int mCurrentUserId;
67         private final BroadcastDispatcher mBroadcastDispatcher;
68 
69         private List<Consumer<Integer>> mCallbacks = new ArrayList<>();
70 
71         @VisibleForTesting
UserReceiver(BroadcastDispatcher broadcastDispatcher)72         UserReceiver(BroadcastDispatcher broadcastDispatcher) {
73             mBroadcastDispatcher = broadcastDispatcher;
74         }
75 
getInstance(BroadcastDispatcher broadcastDispatcher)76         static UserReceiver getInstance(BroadcastDispatcher broadcastDispatcher) {
77             if (sInstance == null) {
78                 sInstance = new UserReceiver(broadcastDispatcher);
79             }
80             return sInstance;
81         }
82 
83         @Override
onReceive(Context context, Intent intent)84         public void onReceive(Context context, Intent intent) {
85             if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
86                 notifyUserSwitched(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
87             }
88         }
89 
getCurrentUserId()90         public int getCurrentUserId() {
91             return mCurrentUserId;
92         }
93 
addTracker(Consumer<Integer> callback)94         private void addTracker(Consumer<Integer> callback) {
95             if (!mCallbacks.contains(callback)) {
96                 mCallbacks.add(callback);
97             }
98             if (!mReceiverRegistered) {
99                 mCurrentUserId = ActivityManager.getCurrentUser();
100                 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
101                 mBroadcastDispatcher.registerReceiver(this, filter, null,
102                         UserHandle.ALL);
103                 mReceiverRegistered = true;
104             }
105         }
106 
removeTracker(Consumer<Integer> callback)107         private void removeTracker(Consumer<Integer> callback) {
108             if (mCallbacks.contains(callback)) {
109                 mCallbacks.remove(callback);
110                 if (mCallbacks.size() == 0 && mReceiverRegistered) {
111                     mBroadcastDispatcher.unregisterReceiver(this);
112                     mReceiverRegistered = false;
113                 }
114             }
115         }
116 
notifyUserSwitched(int newUserId)117         private void notifyUserSwitched(int newUserId) {
118             if (mCurrentUserId != newUserId) {
119                 mCurrentUserId = newUserId;
120                 List<Consumer<Integer>> callbacks = new ArrayList<>(mCallbacks);
121                 for (Consumer<Integer> consumer : callbacks) {
122                     // Accepting may modify this list
123                     if (mCallbacks.contains(consumer)) {
124                         consumer.accept(newUserId);
125                     }
126                 }
127             }
128         }
129     }
130 }
131