Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 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 | |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame^] | 25 | import com.android.internal.annotations.VisibleForTesting; |
| 26 | |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | import java.util.function.Consumer; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 30 | |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 31 | public abstract class CurrentUserTracker { |
| 32 | private final UserReceiver mUserReceiver; |
| 33 | |
| 34 | private Consumer<Integer> mCallback = this::onUserSwitched; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 35 | |
| 36 | public CurrentUserTracker(Context context) { |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame^] | 37 | this(UserReceiver.getInstance(context)); |
| 38 | } |
| 39 | |
| 40 | @VisibleForTesting |
| 41 | CurrentUserTracker(UserReceiver receiver) { |
| 42 | mUserReceiver = receiver; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public int getCurrentUserId() { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 46 | return mUserReceiver.getCurrentUserId(); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jorim Jaggi | f056e35 | 2014-05-11 23:09:22 +0200 | [diff] [blame] | 49 | public void startTracking() { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 50 | mUserReceiver.addTracker(mCallback); |
Jorim Jaggi | f056e35 | 2014-05-11 23:09:22 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 53 | public void stopTracking() { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 54 | mUserReceiver.removeTracker(mCallback); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public abstract void onUserSwitched(int newUserId); |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 58 | |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame^] | 59 | @VisibleForTesting |
| 60 | static class UserReceiver extends BroadcastReceiver { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 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 | |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame^] | 69 | @VisibleForTesting |
| 70 | UserReceiver(Context context) { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 71 | 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 Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame^] | 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 | } |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 127 | } |