John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 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 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 19 | import static android.provider.Settings.Global.ZEN_MODE_ALARMS; |
| 20 | import static android.provider.Settings.Global.ZEN_MODE_OFF; |
| 21 | |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 23 | import android.app.Dialog; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.SharedPreferences; |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 29 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 30 | import android.content.pm.ApplicationInfo; |
| 31 | import android.content.pm.PackageManager; |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 32 | import android.net.Uri; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 33 | import android.os.UserManager; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 34 | import android.provider.Settings; |
| 35 | import android.provider.Settings.Global; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 36 | import android.service.notification.ZenModeConfig; |
| 37 | import android.service.notification.ZenModeConfig.ZenRule; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 38 | import android.service.quicksettings.Tile; |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 39 | import android.text.TextUtils; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 40 | import android.util.Slog; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 41 | import android.view.LayoutInflater; |
| 42 | import android.view.View; |
| 43 | import android.view.View.OnAttachStateChangeListener; |
| 44 | import android.view.ViewGroup; |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 45 | import android.view.WindowManager; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 46 | import android.widget.Switch; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 47 | import android.widget.Toast; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 48 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 49 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 50 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 51 | import com.android.settingslib.notification.EnableZenModeDialog; |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 52 | import com.android.systemui.Prefs; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 53 | import com.android.systemui.R; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 54 | import com.android.systemui.SysUIToast; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 55 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 56 | import com.android.systemui.plugins.qs.DetailAdapter; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 57 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 58 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 59 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 60 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 61 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 62 | import com.android.systemui.volume.ZenModePanel; |
| 63 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 64 | import javax.inject.Inject; |
| 65 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 66 | /** Quick settings tile: Do not disturb **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 67 | public class DndTile extends QSTileImpl<BooleanState> { |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 68 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 69 | private static final Intent ZEN_SETTINGS = |
| 70 | new Intent(Settings.ACTION_ZEN_MODE_SETTINGS); |
| 71 | |
| 72 | private static final Intent ZEN_PRIORITY_SETTINGS = |
| 73 | new Intent(Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 74 | |
| 75 | private static final String ACTION_SET_VISIBLE = "com.android.systemui.dndtile.SET_VISIBLE"; |
| 76 | private static final String EXTRA_VISIBLE = "visible"; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 77 | |
| 78 | private final ZenModeController mController; |
| 79 | private final DndDetailAdapter mDetailAdapter; |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 80 | private final ActivityStarter mActivityStarter; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 81 | |
| 82 | private boolean mListening; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 83 | private boolean mShowingDetail; |
Yuta Yamada | 99fd277 | 2016-10-06 17:40:19 +0900 | [diff] [blame] | 84 | private boolean mReceiverRegistered; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 85 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 86 | @Inject |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 87 | public DndTile(QSHost host, ZenModeController zenModeController, |
| 88 | ActivityStarter activityStarter) { |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 89 | super(host); |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 90 | mController = zenModeController; |
| 91 | mActivityStarter = activityStarter; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 92 | mDetailAdapter = new DndDetailAdapter(); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 93 | mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_SET_VISIBLE)); |
Yuta Yamada | 99fd277 | 2016-10-06 17:40:19 +0900 | [diff] [blame] | 94 | mReceiverRegistered = true; |
Jason Monk | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 95 | mController.observe(getLifecycle(), mZenCallback); |
Yuta Yamada | 99fd277 | 2016-10-06 17:40:19 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void handleDestroy() { |
| 100 | super.handleDestroy(); |
| 101 | if (mReceiverRegistered) { |
| 102 | mContext.unregisterReceiver(mReceiver); |
| 103 | mReceiverRegistered = false; |
| 104 | } |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public static void setVisible(Context context, boolean visible) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 108 | Prefs.putBoolean(context, Prefs.Key.DND_TILE_VISIBLE, visible); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public static boolean isVisible(Context context) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 112 | return Prefs.getBoolean(context, Prefs.Key.DND_TILE_VISIBLE, false /* defaultValue */); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 113 | } |
| 114 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 115 | public static void setCombinedIcon(Context context, boolean combined) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 116 | Prefs.putBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, combined); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public static boolean isCombinedIcon(Context context) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 120 | return Prefs.getBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, |
| 121 | false /* defaultValue */); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 122 | } |
| 123 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 124 | @Override |
| 125 | public DetailAdapter getDetailAdapter() { |
| 126 | return mDetailAdapter; |
| 127 | } |
| 128 | |
| 129 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 130 | public BooleanState newTileState() { |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 131 | return new BooleanState(); |
| 132 | } |
| 133 | |
| 134 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 135 | public Intent getLongClickIntent() { |
| 136 | return ZEN_SETTINGS; |
| 137 | } |
| 138 | |
| 139 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 140 | protected void handleClick() { |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 141 | // Zen is currently on |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 142 | if (mState.value) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 143 | mController.setZen(ZEN_MODE_OFF, null, TAG); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 144 | } else { |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 145 | showDetail(true); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 150 | public void showDetail(boolean show) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 151 | int zenDuration = Settings.Secure.getInt(mContext.getContentResolver(), |
| 152 | Settings.Secure.ZEN_DURATION, 0); |
| 153 | boolean showOnboarding = Settings.Secure.getInt(mContext.getContentResolver(), |
Beverly | 6c60b90 | 2018-08-17 10:10:46 -0400 | [diff] [blame] | 154 | Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0) != 0 |
| 155 | && Settings.Secure.getInt(mContext.getContentResolver(), |
| 156 | Settings.Secure.ZEN_SETTINGS_UPDATED, 0) != 1; |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 157 | if (showOnboarding) { |
| 158 | // don't show on-boarding again or notification ever |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 159 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 160 | Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0); |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 161 | // turn on DND |
| 162 | mController.setZen(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, TAG); |
| 163 | // show on-boarding screen |
| 164 | Intent intent = new Intent(Settings.ZEN_MODE_ONBOARDING); |
| 165 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 166 | mActivityStarter.postStartActivityDismissingKeyguard(intent, 0); |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 167 | } else { |
| 168 | switch (zenDuration) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 169 | case Settings.Secure.ZEN_DURATION_PROMPT: |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 170 | mUiHandler.post(() -> { |
| 171 | Dialog mDialog = new EnableZenModeDialog(mContext).createDialog(); |
| 172 | mDialog.getWindow().setType( |
| 173 | WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
| 174 | SystemUIDialog.setShowForAllUsers(mDialog, true); |
| 175 | SystemUIDialog.registerDismissListener(mDialog); |
| 176 | SystemUIDialog.setWindowOnTop(mDialog); |
| 177 | mUiHandler.post(() -> mDialog.show()); |
| 178 | mHost.collapsePanels(); |
| 179 | }); |
| 180 | break; |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 181 | case Settings.Secure.ZEN_DURATION_FOREVER: |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 182 | mController.setZen(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, TAG); |
| 183 | break; |
| 184 | default: |
| 185 | Uri conditionId = ZenModeConfig.toTimeCondition(mContext, zenDuration, |
| 186 | ActivityManager.getCurrentUser(), true).id; |
| 187 | mController.setZen(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, |
| 188 | conditionId, TAG); |
| 189 | } |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 190 | } |
Beverly | 312ad02 | 2018-01-24 15:07:48 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 194 | protected void handleSecondaryClick() { |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 195 | if (mController.isVolumeRestricted()) { |
| 196 | // Collapse the panels, so the user can see the toast. |
| 197 | mHost.collapsePanels(); |
| 198 | SysUIToast.makeText(mContext, mContext.getString( |
| 199 | com.android.internal.R.string.error_message_change_not_allowed), |
| 200 | Toast.LENGTH_LONG).show(); |
| 201 | return; |
| 202 | } |
Jason Monk | 3c3b650 | 2017-07-27 15:41:26 -0400 | [diff] [blame] | 203 | if (!mState.value) { |
| 204 | // Because of the complexity of the zen panel, it needs to be shown after |
| 205 | // we turn on zen below. |
| 206 | mController.addCallback(new ZenModeController.Callback() { |
| 207 | @Override |
| 208 | public void onZenChanged(int zen) { |
| 209 | mController.removeCallback(this); |
| 210 | showDetail(true); |
| 211 | } |
| 212 | }); |
Beverly | 3e906c5 | 2018-01-22 10:49:43 -0500 | [diff] [blame] | 213 | mController.setZen(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, TAG); |
Jason Monk | 3c3b650 | 2017-07-27 15:41:26 -0400 | [diff] [blame] | 214 | } else { |
| 215 | showDetail(true); |
| 216 | } |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 220 | public CharSequence getTileLabel() { |
| 221 | return mContext.getString(R.string.quick_settings_dnd_label); |
| 222 | } |
| 223 | |
| 224 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 225 | protected void handleUpdateState(BooleanState state, Object arg) { |
Bruno Martins | c154e19 | 2018-03-17 13:55:42 +0000 | [diff] [blame] | 226 | if (mController == null) return; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 227 | final int zen = arg instanceof Integer ? (Integer) arg : mController.getZen(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 228 | final boolean newValue = zen != ZEN_MODE_OFF; |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 229 | final boolean valueChanged = state.value != newValue; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 230 | if (state.slash == null) state.slash = new SlashState(); |
Jason Monk | 2db4619 | 2017-02-01 17:18:22 -0500 | [diff] [blame] | 231 | state.dualTarget = true; |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 232 | state.value = newValue; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 233 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 234 | state.slash.isSlashed = !state.value; |
Beverly | 7999ba9 | 2018-01-24 10:07:06 -0500 | [diff] [blame] | 235 | state.label = getTileLabel(); |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 236 | state.secondaryLabel = TextUtils.emptyIfNull(ZenModeConfig.getDescription(mContext, |
| 237 | zen != Global.ZEN_MODE_OFF, mController.getConfig(), false)); |
Amin Shaikh | 551ff20 | 2019-03-28 09:29:15 -0400 | [diff] [blame] | 238 | state.icon = ResourceIcon.get(com.android.internal.R.drawable.ic_qs_dnd); |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 239 | checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_ADJUST_VOLUME); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 240 | switch (zen) { |
| 241 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 242 | state.contentDescription = |
| 243 | mContext.getString(R.string.accessibility_quick_settings_dnd) + ", " |
Beverly | 92ed74f | 2018-03-15 14:50:40 -0400 | [diff] [blame] | 244 | + state.secondaryLabel; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 245 | break; |
| 246 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 247 | state.contentDescription = |
| 248 | mContext.getString(R.string.accessibility_quick_settings_dnd) + ", " + |
| 249 | mContext.getString(R.string.accessibility_quick_settings_dnd_none_on) |
| 250 | + ", " + state.secondaryLabel; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 251 | break; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 252 | case ZEN_MODE_ALARMS: |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 253 | state.contentDescription = |
| 254 | mContext.getString(R.string.accessibility_quick_settings_dnd) + ", " + |
| 255 | mContext.getString(R.string.accessibility_quick_settings_dnd_alarms_on) |
| 256 | + ", " + state.secondaryLabel; |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 257 | break; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 258 | default: |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 259 | state.contentDescription = mContext.getString( |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 260 | R.string.accessibility_quick_settings_dnd); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 261 | break; |
| 262 | } |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 263 | if (valueChanged) { |
| 264 | fireToggleStateChanged(state.value); |
| 265 | } |
Jason Monk | 28df4ed | 2017-03-23 10:25:37 -0400 | [diff] [blame] | 266 | state.dualLabelContentDescription = mContext.getResources().getString( |
| 267 | R.string.accessibility_quick_settings_open_settings, getTileLabel()); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 268 | state.expandedAccessibilityClassName = Switch.class.getName(); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 272 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 273 | return MetricsEvent.QS_DND; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 277 | protected String composeChangeAnnouncement() { |
| 278 | if (mState.value) { |
| 279 | return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_on); |
| 280 | } else { |
| 281 | return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_off); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 286 | public void handleSetListening(boolean listening) { |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 287 | if (mListening == listening) return; |
| 288 | mListening = listening; |
| 289 | if (mListening) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 290 | Prefs.registerListener(mContext, mPrefListener); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 291 | } else { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 292 | Prefs.unregisterListener(mContext, mPrefListener); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 296 | @Override |
| 297 | public boolean isAvailable() { |
| 298 | return isVisible(mContext); |
| 299 | } |
| 300 | |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 301 | private final OnSharedPreferenceChangeListener mPrefListener |
| 302 | = new OnSharedPreferenceChangeListener() { |
| 303 | @Override |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 304 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, |
| 305 | @Prefs.Key String key) { |
| 306 | if (Prefs.Key.DND_TILE_COMBINED_ICON.equals(key) || |
| 307 | Prefs.Key.DND_TILE_VISIBLE.equals(key)) { |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 308 | refreshState(); |
| 309 | } |
| 310 | } |
| 311 | }; |
| 312 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 313 | private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() { |
| 314 | public void onZenChanged(int zen) { |
| 315 | refreshState(zen); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 316 | if (isShowingDetail()) { |
| 317 | mDetailAdapter.updatePanel(); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onConfigChanged(ZenModeConfig config) { |
| 323 | if (isShowingDetail()) { |
| 324 | mDetailAdapter.updatePanel(); |
| 325 | } |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 326 | } |
| 327 | }; |
| 328 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 329 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 330 | @Override |
| 331 | public void onReceive(Context context, Intent intent) { |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 332 | final boolean visible = intent.getBooleanExtra(EXTRA_VISIBLE, false); |
| 333 | setVisible(mContext, visible); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 334 | refreshState(); |
| 335 | } |
| 336 | }; |
| 337 | |
| 338 | private final class DndDetailAdapter implements DetailAdapter, OnAttachStateChangeListener { |
| 339 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 340 | private ZenModePanel mZenPanel; |
| 341 | private boolean mAuto; |
| 342 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 343 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 344 | public CharSequence getTitle() { |
| 345 | return mContext.getString(R.string.quick_settings_dnd_label); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public Boolean getToggleState() { |
| 350 | return mState.value; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public Intent getSettingsIntent() { |
| 355 | return ZEN_SETTINGS; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public void setToggleState(boolean state) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 360 | MetricsLogger.action(mContext, MetricsEvent.QS_DND_TOGGLE, state); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 361 | if (!state) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 362 | mController.setZen(ZEN_MODE_OFF, null, TAG); |
| 363 | mAuto = false; |
| 364 | } else { |
Beverly | 3e906c5 | 2018-01-22 10:49:43 -0500 | [diff] [blame] | 365 | mController.setZen(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, TAG); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 370 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 371 | return MetricsEvent.QS_DND_DETAILS; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 375 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 376 | mZenPanel = convertView != null ? (ZenModePanel) convertView |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 377 | : (ZenModePanel) LayoutInflater.from(context).inflate( |
| 378 | R.layout.zen_mode_panel, parent, false); |
| 379 | if (convertView == null) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 380 | mZenPanel.init(mController); |
| 381 | mZenPanel.addOnAttachStateChangeListener(this); |
| 382 | mZenPanel.setCallback(mZenModePanelCallback); |
Evan Laird | bcdd046 | 2017-05-15 12:51:34 -0400 | [diff] [blame] | 383 | mZenPanel.setEmptyState(R.drawable.ic_qs_dnd_detail_empty, R.string.dnd_is_off); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 384 | } |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 385 | updatePanel(); |
| 386 | return mZenPanel; |
| 387 | } |
| 388 | |
| 389 | private void updatePanel() { |
| 390 | if (mZenPanel == null) return; |
| 391 | mAuto = false; |
| 392 | if (mController.getZen() == ZEN_MODE_OFF) { |
| 393 | mZenPanel.setState(ZenModePanel.STATE_OFF); |
| 394 | } else { |
| 395 | ZenModeConfig config = mController.getConfig(); |
| 396 | String summary = ""; |
| 397 | if (config.manualRule != null && config.manualRule.enabler != null) { |
| 398 | summary = getOwnerCaption(config.manualRule.enabler); |
| 399 | } |
| 400 | for (ZenRule automaticRule : config.automaticRules.values()) { |
| 401 | if (automaticRule.isAutomaticActive()) { |
| 402 | if (summary.isEmpty()) { |
| 403 | summary = mContext.getString(R.string.qs_dnd_prompt_auto_rule, |
| 404 | automaticRule.name); |
| 405 | } else { |
| 406 | summary = mContext.getString(R.string.qs_dnd_prompt_auto_rule_app); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | if (summary.isEmpty()) { |
| 411 | mZenPanel.setState(ZenModePanel.STATE_MODIFY); |
| 412 | } else { |
| 413 | mAuto = true; |
| 414 | mZenPanel.setState(ZenModePanel.STATE_AUTO_RULE); |
| 415 | mZenPanel.setAutoText(summary); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | private String getOwnerCaption(String owner) { |
| 421 | final PackageManager pm = mContext.getPackageManager(); |
| 422 | try { |
| 423 | final ApplicationInfo info = pm.getApplicationInfo(owner, 0); |
| 424 | if (info != null) { |
| 425 | final CharSequence seq = info.loadLabel(pm); |
| 426 | if (seq != null) { |
| 427 | final String str = seq.toString().trim(); |
| 428 | return mContext.getString(R.string.qs_dnd_prompt_app, str); |
| 429 | } |
| 430 | } |
| 431 | } catch (Throwable e) { |
| 432 | Slog.w(TAG, "Error loading owner caption", e); |
| 433 | } |
| 434 | return ""; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public void onViewAttachedToWindow(View v) { |
| 439 | mShowingDetail = true; |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public void onViewDetachedFromWindow(View v) { |
| 444 | mShowingDetail = false; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 445 | mZenPanel = null; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 446 | } |
| 447 | } |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 448 | |
| 449 | private final ZenModePanel.Callback mZenModePanelCallback = new ZenModePanel.Callback() { |
| 450 | @Override |
| 451 | public void onPrioritySettings() { |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 452 | mActivityStarter.postStartActivityDismissingKeyguard( |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 453 | ZEN_PRIORITY_SETTINGS, 0); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public void onInteraction() { |
| 458 | // noop |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void onExpanded(boolean expanded) { |
| 463 | // noop |
| 464 | } |
| 465 | }; |
| 466 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 467 | } |