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 
25 import com.android.internal.annotations.VisibleForTesting;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.function.Consumer;
30 
31 public abstract class CurrentUserTracker {
32     private final UserReceiver mUserReceiver;
33 
34     private Consumer<Integer> mCallback = this::onUserSwitched;
35 
CurrentUserTracker(Context context)36     public CurrentUserTracker(Context context) {
37         this(UserReceiver.getInstance(context));
38     }
39 
40     @VisibleForTesting
CurrentUserTracker(UserReceiver receiver)41     CurrentUserTracker(UserReceiver receiver) {
42         mUserReceiver = receiver;
43     }
44 
getCurrentUserId()45     public int getCurrentUserId() {
46         return mUserReceiver.getCurrentUserId();
47     }
48 
startTracking()49     public void startTracking() {
50         mUserReceiver.addTracker(mCallback);
51     }
52 
stopTracking()53     public void stopTracking() {
54         mUserReceiver.removeTracker(mCallback);
55     }
56 
onUserSwitched(int newUserId)57     public abstract void onUserSwitched(int newUserId);
58 
59     @VisibleForTesting
60     static class UserReceiver extends BroadcastReceiver {
61         private static UserReceiver sInstance;
62 
63         private Context mAppContext;
64         private boolean mReceiverRegistered;
65         private int mCurrentUserId;
66 
67         private List<Consumer<Integer>> mCallbacks = new ArrayList<>();
68 
69         @VisibleForTesting
UserReceiver(Context context)70         UserReceiver(Context context) {
71             mAppContext = context.getApplicationContext();
72         }
73 
getInstance(Context context)74         static UserReceiver getInstance(Context context) {
75             if (sInstance == null) {
76                 sInstance = new UserReceiver(context);
77             }
78             return sInstance;
79         }
80 
81         @Override
onReceive(Context context, Intent intent)82         public void onReceive(Context context, Intent intent) {
83             if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
84                 notifyUserSwitched(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
85             }
86         }
87 
getCurrentUserId()88         public int getCurrentUserId() {
89             return mCurrentUserId;
90         }
91 
addTracker(Consumer<Integer> callback)92         private void addTracker(Consumer<Integer> callback) {
93             if (!mCallbacks.contains(callback)) {
94                 mCallbacks.add(callback);
95             }
96             if (!mReceiverRegistered) {
97                 mCurrentUserId = ActivityManager.getCurrentUser();
98                 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
99                 mAppContext.registerReceiver(this, filter);
100                 mReceiverRegistered = true;
101             }
102         }
103 
removeTracker(Consumer<Integer> callback)104         private void removeTracker(Consumer<Integer> callback) {
105             if (mCallbacks.contains(callback)) {
106                 mCallbacks.remove(callback);
107                 if (mCallbacks.size() == 0 && mReceiverRegistered) {
108                     mAppContext.unregisterReceiver(this);
109                     mReceiverRegistered = false;
110                 }
111             }
112         }
113 
notifyUserSwitched(int newUserId)114         private void notifyUserSwitched(int newUserId) {
115             if (mCurrentUserId != newUserId) {
116                 mCurrentUserId = newUserId;
117                 List<Consumer<Integer>> callbacks = new ArrayList<>(mCallbacks);
118                 for (Consumer<Integer> consumer : callbacks) {
119                     // Accepting may modify this list
120                     if (mCallbacks.contains(consumer)) {
121                         consumer.accept(newUserId);
122                     }
123                 }
124             }
125         }
126     }
127 }
128