WorkModeTile.java revision 38bb832e7d1238d85760de75efa17e8bc2e2ccec
1/* 2 * Copyright (C) 2015 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 17package com.android.systemui.qs.tiles; 18 19import android.content.Intent; 20import android.provider.Settings; 21import android.service.quicksettings.Tile; 22import android.widget.Switch; 23 24import com.android.internal.logging.MetricsLogger; 25import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 26import com.android.systemui.Dependency; 27import com.android.systemui.R; 28import com.android.systemui.qs.QSTile; 29import com.android.systemui.statusbar.phone.ManagedProfileController; 30 31/** Quick settings tile: Work profile on/off */ 32public class WorkModeTile extends QSTile<QSTile.BooleanState> implements 33 ManagedProfileController.Callback { 34 private final AnimationIcon mEnable = 35 new AnimationIcon(R.drawable.ic_signal_workmode_enable_animation, 36 R.drawable.ic_signal_workmode_disable); 37 private final AnimationIcon mDisable = 38 new AnimationIcon(R.drawable.ic_signal_workmode_disable_animation, 39 R.drawable.ic_signal_workmode_enable); 40 41 private final ManagedProfileController mProfileController; 42 43 public WorkModeTile(Host host) { 44 super(host); 45 mProfileController = Dependency.get(ManagedProfileController.class); 46 } 47 48 @Override 49 public BooleanState newTileState() { 50 return new BooleanState(); 51 } 52 53 @Override 54 public void setListening(boolean listening) { 55 if (listening) { 56 mProfileController.addCallback(this); 57 } else { 58 mProfileController.removeCallback(this); 59 } 60 } 61 62 @Override 63 public Intent getLongClickIntent() { 64 return new Intent(Settings.ACTION_MANAGED_PROFILE_SETTINGS); 65 } 66 67 @Override 68 public void handleClick() { 69 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); 70 mProfileController.setWorkModeEnabled(!mState.value); 71 } 72 73 @Override 74 public boolean isAvailable() { 75 return mProfileController.hasActiveProfile(); 76 } 77 78 @Override 79 public void onManagedProfileChanged() { 80 refreshState(mProfileController.isWorkModeEnabled()); 81 } 82 83 @Override 84 public void onManagedProfileRemoved() { 85 mHost.removeTile(getTileSpec()); 86 } 87 88 @Override 89 public CharSequence getTileLabel() { 90 return mContext.getString(R.string.quick_settings_work_mode_label); 91 } 92 93 @Override 94 protected void handleUpdateState(BooleanState state, Object arg) { 95 if (arg instanceof Boolean) { 96 state.value = (Boolean) arg; 97 } else { 98 state.value = mProfileController.isWorkModeEnabled(); 99 } 100 101 state.label = mContext.getString(R.string.quick_settings_work_mode_label); 102 if (state.value) { 103 state.icon = mEnable; 104 state.contentDescription = mContext.getString( 105 R.string.accessibility_quick_settings_work_mode_on); 106 } else { 107 state.icon = mDisable; 108 state.contentDescription = mContext.getString( 109 R.string.accessibility_quick_settings_work_mode_off); 110 } 111 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName 112 = Switch.class.getName(); 113 state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; 114 } 115 116 @Override 117 public int getMetricsCategory() { 118 return MetricsEvent.QS_WORKMODE; 119 } 120 121 @Override 122 protected String composeChangeAnnouncement() { 123 if (mState.value) { 124 return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_on); 125 } else { 126 return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_off); 127 } 128 } 129} 130