blob: 005206fcd14c560b96d30cdddf43e24aca825b59 [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
dooyoung.hwange9556a82016-11-11 17:23:00 +090025import java.util.ArrayList;
26import java.util.List;
27import java.util.function.Consumer;
Michael Wright0087a142013-02-05 16:29:39 -080028
dooyoung.hwange9556a82016-11-11 17:23:00 +090029public abstract class CurrentUserTracker {
30 private final UserReceiver mUserReceiver;
31
32 private Consumer<Integer> mCallback = this::onUserSwitched;
Michael Wright0087a142013-02-05 16:29:39 -080033
34 public CurrentUserTracker(Context context) {
dooyoung.hwange9556a82016-11-11 17:23:00 +090035 mUserReceiver = UserReceiver.getInstance(context);
Michael Wright0087a142013-02-05 16:29:39 -080036 }
37
38 public int getCurrentUserId() {
dooyoung.hwange9556a82016-11-11 17:23:00 +090039 return mUserReceiver.getCurrentUserId();
Michael Wright0087a142013-02-05 16:29:39 -080040 }
41
Jorim Jaggif056e352014-05-11 23:09:22 +020042 public void startTracking() {
dooyoung.hwange9556a82016-11-11 17:23:00 +090043 mUserReceiver.addTracker(mCallback);
Jorim Jaggif056e352014-05-11 23:09:22 +020044 }
45
Michael Wright0087a142013-02-05 16:29:39 -080046 public void stopTracking() {
dooyoung.hwange9556a82016-11-11 17:23:00 +090047 mUserReceiver.removeTracker(mCallback);
Michael Wright0087a142013-02-05 16:29:39 -080048 }
49
50 public abstract void onUserSwitched(int newUserId);
dooyoung.hwange9556a82016-11-11 17:23:00 +090051
52 private static class UserReceiver extends BroadcastReceiver {
53 private static UserReceiver sInstance;
54
55 private Context mAppContext;
56 private boolean mReceiverRegistered;
57 private int mCurrentUserId;
58
59 private List<Consumer<Integer>> mCallbacks = new ArrayList<>();
60
61 private UserReceiver(Context context) {
62 mAppContext = context.getApplicationContext();
63 }
64
65 static UserReceiver getInstance(Context context) {
66 if (sInstance == null) {
67 sInstance = new UserReceiver(context);
68 }
69 return sInstance;
70 }
71
72 @Override
73 public void onReceive(Context context, Intent intent) {
74 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
75 notifyUserSwitched(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
76 }
77 }
78
79 public int getCurrentUserId() {
80 return mCurrentUserId;
81 }
82
83 private void addTracker(Consumer<Integer> callback) {
84 if (!mCallbacks.contains(callback)) {
85 mCallbacks.add(callback);
86 }
87 if (!mReceiverRegistered) {
88 mCurrentUserId = ActivityManager.getCurrentUser();
89 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
90 mAppContext.registerReceiver(this, filter);
91 mReceiverRegistered = true;
92 }
93 }
94
95 private void removeTracker(Consumer<Integer> callback) {
96 if (mCallbacks.contains(callback)) {
97 mCallbacks.remove(callback);
98 if (mCallbacks.size() == 0 && mReceiverRegistered) {
99 mAppContext.unregisterReceiver(this);
100 mReceiverRegistered = false;
101 }
102 }
103 }
104
105 private void notifyUserSwitched(int newUserId) {
106 if (mCurrentUserId != newUserId) {
107 mCurrentUserId = newUserId;
108 for (Consumer<Integer> consumer : mCallbacks) {
109 consumer.accept(newUserId);
110 }
111 }
112 }
113 }
Michael Wright0087a142013-02-05 16:29:39 -0800114}