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 | |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | import java.util.function.Consumer; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 28 | |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 29 | public abstract class CurrentUserTracker { |
| 30 | private final UserReceiver mUserReceiver; |
| 31 | |
| 32 | private Consumer<Integer> mCallback = this::onUserSwitched; |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 33 | |
| 34 | public CurrentUserTracker(Context context) { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 35 | mUserReceiver = UserReceiver.getInstance(context); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public int getCurrentUserId() { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 39 | return mUserReceiver.getCurrentUserId(); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Jorim Jaggi | f056e35 | 2014-05-11 23:09:22 +0200 | [diff] [blame] | 42 | public void startTracking() { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 43 | mUserReceiver.addTracker(mCallback); |
Jorim Jaggi | f056e35 | 2014-05-11 23:09:22 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 46 | public void stopTracking() { |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 47 | mUserReceiver.removeTracker(mCallback); |
Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public abstract void onUserSwitched(int newUserId); |
dooyoung.hwang | e9556a8 | 2016-11-11 17:23:00 +0900 | [diff] [blame] | 51 | |
| 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 Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 114 | } |