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 17 package com.android.systemui.qs.tiles; 18 19 import android.content.Intent; 20 import android.provider.Settings; 21 import android.service.quicksettings.Tile; 22 import android.widget.Switch; 23 24 import com.android.internal.logging.MetricsLogger; 25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 26 import com.android.systemui.Dependency; 27 import com.android.systemui.R; 28 import com.android.systemui.qs.QSHost; 29 import com.android.systemui.plugins.qs.QSTile.BooleanState; 30 import com.android.systemui.qs.tileimpl.QSTileImpl; 31 import com.android.systemui.statusbar.phone.ManagedProfileController; 32 33 /** Quick settings tile: Work profile on/off */ 34 public class WorkModeTile extends QSTileImpl<BooleanState> implements 35 ManagedProfileController.Callback { 36 private final Icon mIcon = ResourceIcon.get(R.drawable.ic_signal_workmode_disable); 37 38 private final ManagedProfileController mProfileController; 39 40 public WorkModeTile(QSHost host) { 41 super(host); 42 mProfileController = Dependency.get(ManagedProfileController.class); 43 } 44 45 @Override 46 public BooleanState newTileState() { 47 return new BooleanState(); 48 } 49 50 @Override 51 public void handleSetListening(boolean listening) { 52 if (listening) { 53 mProfileController.addCallback(this); 54 } else { 55 mProfileController.removeCallback(this); 56 } 57 } 58 59 @Override 60 public Intent getLongClickIntent() { 61 return new Intent(Settings.ACTION_MANAGED_PROFILE_SETTINGS); 62 } 63 64 @Override 65 public void handleClick() { 66 mProfileController.setWorkModeEnabled(!mState.value); 67 } 68 69 @Override 70 public boolean isAvailable() { 71 return mProfileController.hasActiveProfile(); 72 } 73 74 @Override 75 public void onManagedProfileChanged() { 76 refreshState(mProfileController.isWorkModeEnabled()); 77 } 78 79 @Override 80 public void onManagedProfileRemoved() { 81 mHost.removeTile(getTileSpec()); 82 } 83 84 @Override 85 public CharSequence getTileLabel() { 86 return mContext.getString(R.string.quick_settings_work_mode_label); 87 } 88 89 @Override 90 protected void handleUpdateState(BooleanState state, Object arg) { 91 if (state.slash == null) { 92 state.slash = new SlashState(); 93 } 94 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 state.icon = mIcon; 103 if (state.value) { 104 state.slash.isSlashed = false; 105 state.contentDescription = mContext.getString( 106 R.string.accessibility_quick_settings_work_mode_on); 107 } else { 108 state.slash.isSlashed = true; 109 state.contentDescription = mContext.getString( 110 R.string.accessibility_quick_settings_work_mode_off); 111 } 112 state.expandedAccessibilityClassName = 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