blob: 0b89dc1070e12f7682aee20d0c15af9972dac5fa [file] [log] [blame]
Michael Wright0087a142013-02-05 16:29:39 -08001/*
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
17package com.android.systemui.settings;
18
19import android.app.ActivityManager;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24
Selim Cinekfc9af342017-05-05 14:45:11 -070025import com.android.internal.annotations.VisibleForTesting;
26
dooyoung.hwange9556a82016-11-11 17:23:00 +090027import java.util.ArrayList;
28import java.util.List;
29import java.util.function.Consumer;
Michael Wright0087a142013-02-05 16:29:39 -080030
dooyoung.hwange9556a82016-11-11 17:23:00 +090031public abstract class CurrentUserTracker {
32 private final UserReceiver mUserReceiver;
33
34 private Consumer<Integer> mCallback = this::onUserSwitched;
Michael Wright0087a142013-02-05 16:29:39 -080035
36 public CurrentUserTracker(Context context) {
Selim Cinekfc9af342017-05-05 14:45:11 -070037 this(UserReceiver.getInstance(context));
38 }
39
40 @VisibleForTesting
41 CurrentUserTracker(UserReceiver receiver) {
42 mUserReceiver = receiver;
Michael Wright0087a142013-02-05 16:29:39 -080043 }
44
45 public int getCurrentUserId() {
dooyoung.hwange9556a82016-11-11 17:23:00 +090046 return mUserReceiver.getCurrentUserId();
Michael Wright0087a142013-02-05 16:29:39 -080047 }
48
Jorim Jaggif056e352014-05-11 23:09:22 +020049 public void startTracking() {
dooyoung.hwange9556a82016-11-11 17:23:00 +090050 mUserReceiver.addTracker(mCallback);
Jorim Jaggif056e352014-05-11 23:09:22 +020051 }
52
Michael Wright0087a142013-02-05 16:29:39 -080053 public void stopTracking() {
dooyoung.hwange9556a82016-11-11 17:23:00 +090054 mUserReceiver.removeTracker(mCallback);
Michael Wright0087a142013-02-05 16:29:39 -080055 }
56
57 public abstract void onUserSwitched(int newUserId);
dooyoung.hwange9556a82016-11-11 17:23:00 +090058
Selim Cinekfc9af342017-05-05 14:45:11 -070059 @VisibleForTesting
60 static class UserReceiver extends BroadcastReceiver {
dooyoung.hwange9556a82016-11-11 17:23:00 +090061 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
Selim Cinekfc9af342017-05-05 14:45:11 -070069 @VisibleForTesting
70 UserReceiver(Context context) {
dooyoung.hwange9556a82016-11-11 17:23:00 +090071 mAppContext = context.getApplicationContext();
72 }
73
74 static UserReceiver getInstance(Context context) {
75 if (sInstance == null) {
76 sInstance = new UserReceiver(context);
77 }
78 return sInstance;
79 }
80
81 @Override
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
88 public int getCurrentUserId() {
89 return mCurrentUserId;
90 }
91
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
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
114 private void notifyUserSwitched(int newUserId) {
115 if (mCurrentUserId != newUserId) {
116 mCurrentUserId = newUserId;
Selim Cinekfc9af342017-05-05 14:45:11 -0700117 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 }
dooyoung.hwange9556a82016-11-11 17:23:00 +0900123 }
124 }
125 }
126 }
Michael Wright0087a142013-02-05 16:29:39 -0800127}