Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.statusbar.phone; |
| 16 | |
| 17 | import android.app.ActivityManager; |
| 18 | import android.app.StatusBarManager; |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.content.pm.UserInfo; |
| 24 | import android.os.UserHandle; |
| 25 | import android.os.UserManager; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.LinkedList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | public class ManagedProfileControllerImpl implements ManagedProfileController { |
| 32 | |
| 33 | private final List<Callback> mCallbacks = new ArrayList<>(); |
| 34 | |
| 35 | private final Context mContext; |
| 36 | private final UserManager mUserManager; |
| 37 | private final LinkedList<UserInfo> mProfiles; |
| 38 | private boolean mListening; |
| 39 | private int mCurrentUser; |
| 40 | |
| 41 | public ManagedProfileControllerImpl(QSTileHost host) { |
| 42 | mContext = host.getContext(); |
| 43 | mUserManager = UserManager.get(mContext); |
| 44 | mProfiles = new LinkedList<UserInfo>(); |
| 45 | } |
| 46 | |
| 47 | public void addCallback(Callback callback) { |
| 48 | mCallbacks.add(callback); |
| 49 | if (mCallbacks.size() == 1) { |
| 50 | setListening(true); |
| 51 | } |
| 52 | callback.onManagedProfileChanged(); |
| 53 | } |
| 54 | |
| 55 | public void removeCallback(Callback callback) { |
| 56 | if (mCallbacks.remove(callback) && mCallbacks.size() == 0) { |
| 57 | setListening(false); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public void setWorkModeEnabled(boolean enableWorkMode) { |
| 62 | synchronized (mProfiles) { |
| 63 | for (UserInfo ui : mProfiles) { |
| 64 | if (enableWorkMode) { |
| 65 | if (!mUserManager.trySetQuietModeDisabled(ui.id, null)) { |
| 66 | StatusBarManager statusBarManager = (StatusBarManager) mContext |
| 67 | .getSystemService(android.app.Service.STATUS_BAR_SERVICE); |
| 68 | statusBarManager.collapsePanels(); |
| 69 | } |
| 70 | } else { |
| 71 | mUserManager.setQuietModeEnabled(ui.id, true); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private void reloadManagedProfiles() { |
| 78 | synchronized (mProfiles) { |
| 79 | boolean hadProfile = mProfiles.size() > 0; |
| 80 | int user = ActivityManager.getCurrentUser(); |
| 81 | mProfiles.clear(); |
| 82 | |
| 83 | for (UserInfo ui : mUserManager.getEnabledProfiles(user)) { |
| 84 | if (ui.isManagedProfile()) { |
| 85 | mProfiles.add(ui); |
| 86 | } |
| 87 | } |
| 88 | if (mProfiles.size() == 0 && hadProfile && (user == mCurrentUser)) { |
| 89 | for (Callback callback : mCallbacks) { |
| 90 | callback.onManagedProfileRemoved(); |
| 91 | } |
| 92 | } |
| 93 | mCurrentUser = user; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public boolean hasActiveProfile() { |
| 98 | if (!mListening) reloadManagedProfiles(); |
| 99 | synchronized (mProfiles) { |
| 100 | return mProfiles.size() > 0; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public boolean isWorkModeEnabled() { |
| 105 | if (!mListening) reloadManagedProfiles(); |
| 106 | synchronized (mProfiles) { |
| 107 | for (UserInfo ui : mProfiles) { |
| 108 | if (ui.isQuietModeEnabled()) { |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private void setListening(boolean listening) { |
| 117 | mListening = listening; |
| 118 | if (listening) { |
| 119 | reloadManagedProfiles(); |
| 120 | |
| 121 | final IntentFilter filter = new IntentFilter(); |
| 122 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
| 123 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 124 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 125 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); |
| 126 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
| 127 | mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null); |
| 128 | } else { |
| 129 | mContext.unregisterReceiver(mReceiver); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 134 | @Override |
| 135 | public void onReceive(Context context, Intent intent) { |
| 136 | reloadManagedProfiles(); |
| 137 | for (Callback callback : mCallbacks) { |
| 138 | callback.onManagedProfileChanged(); |
| 139 | } |
| 140 | } |
| 141 | }; |
| 142 | } |