Quick setting tile to toggle work mode for managed profiles
Status bar will also show a different badge icon when managed profile
is in quiet mode i.e. work mode is off. The tile is invisible for now
until the full feature lands.
Bug: 22541941
Change-Id: I53f33ea346cd9215ecee2ca42de137af61e4c8a2
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/WorkModeTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/WorkModeTile.java
new file mode 100644
index 0000000..7605f3b8
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/WorkModeTile.java
@@ -0,0 +1,216 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.qs.tiles;
+
+import android.app.ActivityManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.UserInfo;
+import android.os.IUserManager;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.util.ArraySet;
+import android.util.Log;
+
+import com.android.internal.logging.MetricsLogger;
+import com.android.systemui.R;
+import com.android.systemui.qs.QSTile;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/** Quick settings tile: Work profile on/off */
+public class WorkModeTile extends QSTile<QSTile.BooleanState> {
+ private final AnimationIcon mEnable =
+ new AnimationIcon(R.drawable.ic_signal_workmode_enable_animation);
+ private final AnimationIcon mDisable =
+ new AnimationIcon(R.drawable.ic_signal_workmode_disable_animation);
+
+ private boolean mListening;
+
+ private UserManager mUserManager;
+ private List<UserInfo> mProfiles;
+
+ public WorkModeTile(Host host) {
+ super(host);
+ mUserManager = UserManager.get(mContext);
+ mProfiles = new LinkedList<UserInfo>();
+ }
+
+ @Override
+ protected BooleanState newTileState() {
+ return new BooleanState();
+ }
+
+ @Override
+ public void handleClick() {
+ MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
+ setWorkModeEnabled(!mState.value);
+ }
+
+ private void reloadManagedProfiles(int userHandle) {
+ synchronized (mProfiles) {
+ mProfiles.clear();
+
+ if (userHandle == UserHandle.USER_CURRENT) {
+ userHandle = ActivityManager.getCurrentUser();
+ }
+ for (UserInfo ui : mUserManager.getEnabledProfiles(userHandle)) {
+ if (ui.isManagedProfile()) {
+ mProfiles.add(ui);
+ }
+ }
+ }
+ }
+
+ private boolean hasActiveProfile() {
+ synchronized (mProfiles) {
+ return mProfiles.size() > 0;
+ }
+ }
+
+ private boolean isWorkModeEnabled() {
+ synchronized (mProfiles) {
+ for (UserInfo ui : mProfiles) {
+ if (ui.isQuietModeEnabled()) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+
+ private void refreshQuietModeState(boolean backgroundRefresh) {
+ if (backgroundRefresh) {
+ refreshState(isWorkModeEnabled() ? UserBoolean.BACKGROUND_TRUE
+ : UserBoolean.BACKGROUND_FALSE);
+ } else {
+ refreshState(isWorkModeEnabled() ? UserBoolean.USER_TRUE : UserBoolean.USER_FALSE);
+ }
+ }
+
+ @Override
+ protected void handleUpdateState(BooleanState state, Object arg) {
+ if (!hasActiveProfile()) {
+ state.visible = false;
+ state.value = false;
+ return;
+ }
+
+ final boolean userInitialized;
+ if (arg instanceof UserBoolean) {
+ state.value = ((UserBoolean) arg).value;
+ userInitialized = ((UserBoolean) arg).userInitiated;
+ } else {
+ state.value = isWorkModeEnabled();
+ userInitialized = false;
+ }
+
+ state.visible = true;
+ final AnimationIcon icon;
+ state.label = mContext.getString(R.string.quick_settings_work_mode_label);
+ if (state.value) {
+ icon = mEnable;
+ state.contentDescription = mContext.getString(
+ R.string.accessibility_quick_settings_work_mode_on);
+ } else {
+ icon = mDisable;
+ state.contentDescription = mContext.getString(
+ R.string.accessibility_quick_settings_work_mode_off);
+ }
+ icon.setAllowAnimation(userInitialized);
+ state.icon = icon;
+ }
+
+ @Override
+ public int getMetricsCategory() {
+ return MetricsLogger.QS_WORKMODE;
+ }
+
+ @Override
+ protected String composeChangeAnnouncement() {
+ if (mState.value) {
+ return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_on);
+ } else {
+ return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_off);
+ }
+ }
+
+ @Override
+ public void setListening(boolean listening) {
+ if (mListening == listening) {
+ return;
+ }
+ mListening = listening;
+ if (listening) {
+ reloadManagedProfiles(UserHandle.USER_CURRENT);
+
+ final IntentFilter filter = new IntentFilter();
+ filter.addAction(Intent.ACTION_USER_SWITCHED);
+ filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
+ filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
+ filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED);
+ mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null);
+ } else {
+ mContext.unregisterReceiver(mReceiver);
+ }
+ }
+
+ private void setWorkModeEnabled(boolean enabled) {
+ synchronized (mProfiles) {
+ for (UserInfo ui : mProfiles) {
+ mUserManager.setQuietModeEnabled(ui.id, !enabled);
+ }
+ }
+ }
+
+ private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ final String action = intent.getAction();
+ final int targetUser;
+ final boolean isBackgroundRefresh;
+ switch (action) {
+ case Intent.ACTION_USER_SWITCHED:
+ targetUser = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
+ UserHandle.USER_CURRENT);
+ isBackgroundRefresh = true;
+ break;
+ case Intent.ACTION_MANAGED_PROFILE_ADDED:
+ case Intent.ACTION_MANAGED_PROFILE_REMOVED:
+ targetUser = UserHandle.USER_CURRENT;
+ isBackgroundRefresh = true;
+ break;
+ case Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED:
+ targetUser = UserHandle.USER_CURRENT;
+ isBackgroundRefresh = false;
+ break;
+ default:
+ targetUser = UserHandle.USER_NULL;
+ isBackgroundRefresh = false;
+ }
+ if (targetUser != UserHandle.USER_NULL) {
+ reloadManagedProfiles(targetUser);
+ refreshQuietModeState(isBackgroundRefresh);
+ }
+ }
+ };
+}