Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -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.content.BroadcastReceiver; |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.content.IntentFilter; |
| 22 | import android.content.pm.UserInfo; |
| 23 | import android.os.UserHandle; |
| 24 | import android.os.UserManager; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.LinkedList; |
| 28 | import java.util.List; |
| 29 | |
| 30 | public class ManagedProfileController { |
| 31 | |
| 32 | private final List<Callback> mCallbacks = new ArrayList<>(); |
| 33 | |
| 34 | private final Context mContext; |
| 35 | private final UserManager mUserManager; |
| 36 | private final LinkedList<UserInfo> mProfiles; |
| 37 | private boolean mListening; |
| 38 | private int mCurrentUser; |
| 39 | |
| 40 | public ManagedProfileController(QSTileHost host) { |
| 41 | mContext = host.getContext(); |
| 42 | mUserManager = UserManager.get(mContext); |
| 43 | mProfiles = new LinkedList<UserInfo>(); |
| 44 | } |
| 45 | |
| 46 | public void addCallback(Callback callback) { |
| 47 | mCallbacks.add(callback); |
| 48 | if (mCallbacks.size() == 1) { |
| 49 | setListening(true); |
| 50 | } |
| 51 | callback.onManagedProfileChanged(); |
| 52 | } |
| 53 | |
| 54 | public void removeCallback(Callback callback) { |
| 55 | if (mCallbacks.remove(callback) && mCallbacks.size() == 0) { |
| 56 | setListening(false); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void setWorkModeEnabled(boolean enabled) { |
| 61 | synchronized (mProfiles) { |
| 62 | for (UserInfo ui : mProfiles) { |
| 63 | mUserManager.setQuietModeEnabled(ui.id, !enabled); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private void reloadManagedProfiles() { |
| 69 | synchronized (mProfiles) { |
| 70 | boolean hadProfile = mProfiles.size() > 0; |
| 71 | int user = ActivityManager.getCurrentUser(); |
| 72 | mProfiles.clear(); |
| 73 | |
| 74 | for (UserInfo ui : mUserManager.getEnabledProfiles(user)) { |
| 75 | if (ui.isManagedProfile()) { |
| 76 | mProfiles.add(ui); |
| 77 | } |
| 78 | } |
| 79 | if (mProfiles.size() == 0 && hadProfile && (user == mCurrentUser)) { |
| 80 | for (Callback callback : mCallbacks) { |
| 81 | callback.onManagedProfileRemoved(); |
| 82 | } |
| 83 | } |
| 84 | mCurrentUser = user; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public boolean hasActiveProfile() { |
| 89 | if (!mListening) reloadManagedProfiles(); |
| 90 | synchronized (mProfiles) { |
| 91 | return mProfiles.size() > 0; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public boolean isWorkModeEnabled() { |
| 96 | if (!mListening) reloadManagedProfiles(); |
| 97 | synchronized (mProfiles) { |
| 98 | for (UserInfo ui : mProfiles) { |
| 99 | if (ui.isQuietModeEnabled()) { |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private void setListening(boolean listening) { |
| 108 | mListening = listening; |
| 109 | if (listening) { |
| 110 | reloadManagedProfiles(); |
| 111 | |
| 112 | final IntentFilter filter = new IntentFilter(); |
| 113 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
| 114 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 115 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 116 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED); |
| 117 | mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null); |
| 118 | } else { |
| 119 | mContext.unregisterReceiver(mReceiver); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 124 | @Override |
| 125 | public void onReceive(Context context, Intent intent) { |
| 126 | reloadManagedProfiles(); |
| 127 | for (Callback callback : mCallbacks) { |
| 128 | callback.onManagedProfileChanged(); |
| 129 | } |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | public interface Callback { |
| 134 | void onManagedProfileChanged(); |
| 135 | void onManagedProfileRemoved(); |
| 136 | } |
| 137 | } |