blob: f921eb9a49a658f20256c0002c95cfa8a8e54603 [file] [log] [blame]
Rubin Xu1205fb12015-11-04 17:45:03 +00001/*
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
Jason Monk76c67aa2016-02-19 14:49:42 -050019import android.content.Intent;
20import android.provider.Settings;
Jason Monk32508852017-01-18 09:17:13 -050021import android.service.quicksettings.Tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040022import android.widget.Switch;
23
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010024import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Rubin Xu1205fb12015-11-04 17:45:03 +000025import com.android.systemui.R;
Jason Monk702e2eb2017-03-03 16:53:44 -050026import com.android.systemui.plugins.qs.QSTile.BooleanState;
Tony Make9861b42018-03-02 17:24:10 +000027import com.android.systemui.qs.QSHost;
Jason Monk702e2eb2017-03-03 16:53:44 -050028import com.android.systemui.qs.tileimpl.QSTileImpl;
Jason Monkc3f42c12016-02-05 12:33:13 -050029import com.android.systemui.statusbar.phone.ManagedProfileController;
Rubin Xu1205fb12015-11-04 17:45:03 +000030
Jason Monk5d577202018-12-26 15:43:06 -050031import javax.inject.Inject;
32
Rubin Xu1205fb12015-11-04 17:45:03 +000033/** Quick settings tile: Work profile on/off */
Jason Monk702e2eb2017-03-03 16:53:44 -050034public class WorkModeTile extends QSTileImpl<BooleanState> implements
Jason Monkc3f42c12016-02-05 12:33:13 -050035 ManagedProfileController.Callback {
Evan Laird351e4442017-05-12 12:52:09 -040036 private final Icon mIcon = ResourceIcon.get(R.drawable.ic_signal_workmode_disable);
Rubin Xu1205fb12015-11-04 17:45:03 +000037
Jason Monkc3f42c12016-02-05 12:33:13 -050038 private final ManagedProfileController mProfileController;
Rubin Xu1205fb12015-11-04 17:45:03 +000039
Jason Monk5d577202018-12-26 15:43:06 -050040 @Inject
Jason Monkc0e0e2b2018-12-26 16:47:41 -050041 public WorkModeTile(QSHost host, ManagedProfileController managedProfileController) {
Rubin Xu1205fb12015-11-04 17:45:03 +000042 super(host);
Jason Monkc0e0e2b2018-12-26 16:47:41 -050043 mProfileController = managedProfileController;
Jason Monkfa452ef2018-12-26 17:26:10 -050044 mProfileController.observe(getLifecycle(), this);
Rubin Xu1205fb12015-11-04 17:45:03 +000045 }
46
47 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050048 public BooleanState newTileState() {
Rubin Xu1205fb12015-11-04 17:45:03 +000049 return new BooleanState();
50 }
51
52 @Override
Jason Monk1c6116c2017-09-06 17:33:01 -040053 public void handleSetListening(boolean listening) {
Jason Monkc3f42c12016-02-05 12:33:13 -050054 }
55
56 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050057 public Intent getLongClickIntent() {
Ricky Wai38bb8322017-02-01 15:23:50 +000058 return new Intent(Settings.ACTION_MANAGED_PROFILE_SETTINGS);
Jason Monk76c67aa2016-02-19 14:49:42 -050059 }
60
61 @Override
Rubin Xu1205fb12015-11-04 17:45:03 +000062 public void handleClick() {
Jason Monkc3f42c12016-02-05 12:33:13 -050063 mProfileController.setWorkModeEnabled(!mState.value);
Rubin Xu1205fb12015-11-04 17:45:03 +000064 }
65
Jason Monkc3f42c12016-02-05 12:33:13 -050066 @Override
67 public boolean isAvailable() {
68 return mProfileController.hasActiveProfile();
Rubin Xu1205fb12015-11-04 17:45:03 +000069 }
70
Jason Monkc3f42c12016-02-05 12:33:13 -050071 @Override
72 public void onManagedProfileChanged() {
73 refreshState(mProfileController.isWorkModeEnabled());
Rubin Xu1205fb12015-11-04 17:45:03 +000074 }
75
Jason Monkc3f42c12016-02-05 12:33:13 -050076 @Override
77 public void onManagedProfileRemoved() {
78 mHost.removeTile(getTileSpec());
arangelov5fe95982018-08-17 18:43:31 +010079 mHost.unmarkTileAsAutoAdded(getTileSpec());
Rubin Xu1205fb12015-11-04 17:45:03 +000080 }
81
82 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040083 public CharSequence getTileLabel() {
Adam Lesinski6cd41552018-03-02 11:40:50 -080084 return mContext.getString(R.string.quick_settings_work_mode_label);
Jason Monk39c98e62016-03-16 09:18:35 -040085 }
86
87 @Override
Rubin Xu1205fb12015-11-04 17:45:03 +000088 protected void handleUpdateState(BooleanState state, Object arg) {
Tony Make9861b42018-03-02 17:24:10 +000089 if (!isAvailable()) {
90 onManagedProfileRemoved();
91 }
92
Evan Laird351e4442017-05-12 12:52:09 -040093 if (state.slash == null) {
94 state.slash = new SlashState();
95 }
96
Jason Monk66239fb2015-12-21 14:27:00 -050097 if (arg instanceof Boolean) {
98 state.value = (Boolean) arg;
Rubin Xu1205fb12015-11-04 17:45:03 +000099 } else {
Jason Monkc3f42c12016-02-05 12:33:13 -0500100 state.value = mProfileController.isWorkModeEnabled();
Rubin Xu1205fb12015-11-04 17:45:03 +0000101 }
102
Evan Laird351e4442017-05-12 12:52:09 -0400103 state.icon = mIcon;
Rubin Xu1205fb12015-11-04 17:45:03 +0000104 if (state.value) {
Evan Laird351e4442017-05-12 12:52:09 -0400105 state.slash.isSlashed = false;
Rubin Xu1205fb12015-11-04 17:45:03 +0000106 state.contentDescription = mContext.getString(
107 R.string.accessibility_quick_settings_work_mode_on);
108 } else {
Evan Laird351e4442017-05-12 12:52:09 -0400109 state.slash.isSlashed = true;
Rubin Xu1205fb12015-11-04 17:45:03 +0000110 state.contentDescription = mContext.getString(
111 R.string.accessibility_quick_settings_work_mode_off);
112 }
Tony Mak83533f42018-02-22 18:22:32 +0000113 state.label = mContext.getString(R.string.quick_settings_work_mode_label);
Jason Monk702e2eb2017-03-03 16:53:44 -0500114 state.expandedAccessibilityClassName = Switch.class.getName();
Jason Monk32508852017-01-18 09:17:13 -0500115 state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Rubin Xu1205fb12015-11-04 17:45:03 +0000116 }
117
118 @Override
119 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500120 return MetricsEvent.QS_WORKMODE;
Rubin Xu1205fb12015-11-04 17:45:03 +0000121 }
122
123 @Override
124 protected String composeChangeAnnouncement() {
125 if (mState.value) {
126 return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_on);
127 } else {
128 return mContext.getString(R.string.accessibility_quick_settings_work_mode_changed_off);
129 }
130 }
Rubin Xu1205fb12015-11-04 17:45:03 +0000131}