Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 1 | /** |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 2 | * Copyright (C) 2014 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.volume; |
| 18 | |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 20 | import android.animation.LayoutTransition.TransitionListener; |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 21 | import android.app.ActivityManager; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 24 | import android.content.SharedPreferences; |
| 25 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
John Spurlock | 814ee6ab | 2015-05-18 11:16:38 -0400 | [diff] [blame] | 26 | import android.content.res.Configuration; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 27 | import android.net.Uri; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 28 | import android.os.AsyncTask; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.Looper; |
| 31 | import android.os.Message; |
| 32 | import android.provider.Settings; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 33 | import android.provider.Settings.Global; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 34 | import android.service.notification.Condition; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 35 | import android.service.notification.ZenModeConfig; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 36 | import android.service.notification.ZenModeConfig.ZenRule; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 37 | import android.text.TextUtils; |
John Spurlock | 8be53ea | 2015-05-29 12:04:21 -0400 | [diff] [blame] | 38 | import android.text.format.DateFormat; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 39 | import android.text.format.DateUtils; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 40 | import android.util.ArraySet; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 41 | import android.util.AttributeSet; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 42 | import android.util.Log; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 43 | import android.util.MathUtils; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
| 45 | import android.view.View; |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 46 | import android.view.ViewGroup; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 47 | import android.widget.CompoundButton; |
| 48 | import android.widget.CompoundButton.OnCheckedChangeListener; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 49 | import android.widget.FrameLayout; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 50 | import android.widget.ImageView; |
| 51 | import android.widget.LinearLayout; |
| 52 | import android.widget.RadioButton; |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 53 | import android.widget.RadioGroup; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 54 | import android.widget.TextView; |
| 55 | |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 56 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 57 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 58 | import com.android.systemui.Prefs; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 59 | import com.android.systemui.R; |
| 60 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 61 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 62 | import java.io.FileDescriptor; |
| 63 | import java.io.PrintWriter; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 64 | import java.util.Arrays; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 65 | import java.util.Calendar; |
| 66 | import java.util.GregorianCalendar; |
John Spurlock | 8be53ea | 2015-05-29 12:04:21 -0400 | [diff] [blame] | 67 | import java.util.Locale; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 68 | import java.util.Objects; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 69 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 70 | public class ZenModePanel extends FrameLayout { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 71 | private static final String TAG = "ZenModePanel"; |
| 72 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 73 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 74 | public static final int STATE_MODIFY = 0; |
| 75 | public static final int STATE_AUTO_RULE = 1; |
| 76 | public static final int STATE_OFF = 2; |
| 77 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 78 | private static final int SECONDS_MS = 1000; |
| 79 | private static final int MINUTES_MS = 60 * SECONDS_MS; |
| 80 | |
Julia Reynolds | 0ca82fe | 2015-11-05 11:08:02 -0500 | [diff] [blame] | 81 | private static final int[] MINUTE_BUCKETS = ZenModeConfig.MINUTE_BUCKETS; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 82 | private static final int MIN_BUCKET_MINUTES = MINUTE_BUCKETS[0]; |
| 83 | private static final int MAX_BUCKET_MINUTES = MINUTE_BUCKETS[MINUTE_BUCKETS.length - 1]; |
| 84 | private static final int DEFAULT_BUCKET_INDEX = Arrays.binarySearch(MINUTE_BUCKETS, 60); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 85 | private static final int FOREVER_CONDITION_INDEX = 0; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 86 | private static final int COUNTDOWN_CONDITION_INDEX = 1; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 87 | private static final int COUNTDOWN_ALARM_CONDITION_INDEX = 2; |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 88 | private static final int COUNTDOWN_CONDITION_COUNT = 2; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 89 | |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 90 | public static final Intent ZEN_SETTINGS |
| 91 | = new Intent(Settings.ACTION_ZEN_MODE_SETTINGS); |
| 92 | public static final Intent ZEN_PRIORITY_SETTINGS |
| 93 | = new Intent(Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 94 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 95 | private static final long TRANSITION_DURATION = 300; |
| 96 | |
John Spurlock | 7f1df5e | 2014-05-31 19:11:40 -0400 | [diff] [blame] | 97 | private final Context mContext; |
Muyuan Li | 94fa1df | 2016-03-23 12:09:38 -0700 | [diff] [blame] | 98 | protected final LayoutInflater mInflater; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 99 | private final H mHandler = new H(); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 100 | private final ZenPrefs mPrefs; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 101 | private final TransitionHelper mTransitionHelper = new TransitionHelper(); |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 102 | private final Uri mForeverId; |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 103 | private final ConfigurableTexts mConfigurableTexts; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 104 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 105 | private String mTag = TAG + "/" + Integer.toHexString(System.identityHashCode(this)); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 106 | |
Muyuan Li | 8303bd2 | 2016-03-23 20:08:26 -0700 | [diff] [blame] | 107 | protected SegmentedButtons mZenButtons; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 108 | private View mZenIntroduction; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 109 | private TextView mZenIntroductionMessage; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 110 | private View mZenIntroductionConfirm; |
John Spurlock | d896323 | 2015-06-08 16:26:12 -0400 | [diff] [blame] | 111 | private TextView mZenIntroductionCustomize; |
Muyuan Li | 94fa1df | 2016-03-23 12:09:38 -0700 | [diff] [blame] | 112 | protected LinearLayout mZenConditions; |
John Spurlock | 8be53ea | 2015-05-29 12:04:21 -0400 | [diff] [blame] | 113 | private TextView mZenAlarmWarning; |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 114 | private RadioGroup mZenRadioGroup; |
| 115 | private LinearLayout mZenRadioGroupContent; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 116 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 117 | private Callback mCallback; |
| 118 | private ZenModeController mController; |
John Spurlock | ad680d4 | 2015-01-30 15:48:15 -0500 | [diff] [blame] | 119 | private boolean mCountdownConditionSupported; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 120 | private boolean mRequestingConditions; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 121 | private Condition mExitCondition; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 122 | private int mBucketIndex = -1; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 123 | private boolean mExpanded; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 124 | private boolean mHidden; |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 125 | private int mSessionZen; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 126 | private int mAttachedZen; |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 127 | private boolean mAttached; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 128 | private Condition mSessionExitCondition; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 129 | private Condition[] mConditions; |
| 130 | private Condition mTimeCondition; |
John Spurlock | cbd7a31 | 2015-06-21 10:46:28 -0400 | [diff] [blame] | 131 | private boolean mVoiceCapable; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 132 | |
Muyuan Li | 8b27090 | 2016-06-10 10:26:45 -0700 | [diff] [blame] | 133 | protected int mZenModeConditionLayoutId; |
| 134 | protected int mZenModeButtonLayoutId; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 135 | private View mEmpty; |
| 136 | private TextView mEmptyText; |
| 137 | private ImageView mEmptyIcon; |
| 138 | private View mAutoRule; |
| 139 | private TextView mAutoTitle; |
| 140 | private int mState = STATE_MODIFY; |
| 141 | private ViewGroup mEdit; |
Muyuan Li | 8b27090 | 2016-06-10 10:26:45 -0700 | [diff] [blame] | 142 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 143 | public ZenModePanel(Context context, AttributeSet attrs) { |
| 144 | super(context, attrs); |
John Spurlock | 7f1df5e | 2014-05-31 19:11:40 -0400 | [diff] [blame] | 145 | mContext = context; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 146 | mPrefs = new ZenPrefs(); |
Jason Monk | b2eb67b | 2017-07-11 10:13:31 -0400 | [diff] [blame] | 147 | mInflater = LayoutInflater.from(mContext); |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 148 | mForeverId = Condition.newId(mContext).appendPath("forever").build(); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 149 | mConfigurableTexts = new ConfigurableTexts(mContext); |
John Spurlock | cbd7a31 | 2015-06-21 10:46:28 -0400 | [diff] [blame] | 150 | mVoiceCapable = Util.isVoiceCapable(mContext); |
Muyuan Li | 8b27090 | 2016-06-10 10:26:45 -0700 | [diff] [blame] | 151 | mZenModeConditionLayoutId = R.layout.zen_mode_condition; |
| 152 | mZenModeButtonLayoutId = R.layout.zen_mode_button; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 153 | if (DEBUG) Log.d(mTag, "new ZenModePanel"); |
| 154 | } |
| 155 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 156 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 157 | pw.println("ZenModePanel state:"); |
| 158 | pw.print(" mCountdownConditionSupported="); pw.println(mCountdownConditionSupported); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 159 | pw.print(" mRequestingConditions="); pw.println(mRequestingConditions); |
| 160 | pw.print(" mAttached="); pw.println(mAttached); |
| 161 | pw.print(" mHidden="); pw.println(mHidden); |
| 162 | pw.print(" mExpanded="); pw.println(mExpanded); |
| 163 | pw.print(" mSessionZen="); pw.println(mSessionZen); |
| 164 | pw.print(" mAttachedZen="); pw.println(mAttachedZen); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 165 | pw.print(" mConfirmedPriorityIntroduction="); |
| 166 | pw.println(mPrefs.mConfirmedPriorityIntroduction); |
| 167 | pw.print(" mConfirmedSilenceIntroduction="); |
| 168 | pw.println(mPrefs.mConfirmedSilenceIntroduction); |
John Spurlock | cbd7a31 | 2015-06-21 10:46:28 -0400 | [diff] [blame] | 169 | pw.print(" mVoiceCapable="); pw.println(mVoiceCapable); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 170 | mTransitionHelper.dump(fd, pw, args); |
| 171 | } |
| 172 | |
Muyuan Li | 8303bd2 | 2016-03-23 20:08:26 -0700 | [diff] [blame] | 173 | protected void createZenButtons() { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 174 | mZenButtons = findViewById(R.id.zen_buttons); |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 175 | mZenButtons.addButton(R.string.interruption_level_none_twoline, |
John Spurlock | bd1abe6 | 2015-06-10 17:18:38 -0400 | [diff] [blame] | 176 | R.string.interruption_level_none_with_warning, |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 177 | Global.ZEN_MODE_NO_INTERRUPTIONS); |
| 178 | mZenButtons.addButton(R.string.interruption_level_alarms_twoline, |
John Spurlock | a1c7ffe | 2015-06-08 15:34:05 -0400 | [diff] [blame] | 179 | R.string.interruption_level_alarms, |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 180 | Global.ZEN_MODE_ALARMS); |
| 181 | mZenButtons.addButton(R.string.interruption_level_priority_twoline, |
John Spurlock | a1c7ffe | 2015-06-08 15:34:05 -0400 | [diff] [blame] | 182 | R.string.interruption_level_priority, |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 183 | Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 184 | mZenButtons.setCallback(mZenButtonsCallback); |
Muyuan Li | 8303bd2 | 2016-03-23 20:08:26 -0700 | [diff] [blame] | 185 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 186 | |
Muyuan Li | 8303bd2 | 2016-03-23 20:08:26 -0700 | [diff] [blame] | 187 | @Override |
| 188 | protected void onFinishInflate() { |
| 189 | super.onFinishInflate(); |
| 190 | createZenButtons(); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 191 | mZenIntroduction = findViewById(R.id.zen_introduction); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 192 | mZenIntroductionMessage = findViewById(R.id.zen_introduction_message); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 193 | mZenIntroductionConfirm = findViewById(R.id.zen_introduction_confirm); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 194 | mZenIntroductionConfirm.setOnClickListener(v -> confirmZenIntroduction()); |
| 195 | mZenIntroductionCustomize = findViewById(R.id.zen_introduction_customize); |
| 196 | mZenIntroductionCustomize.setOnClickListener(v -> { |
| 197 | confirmZenIntroduction(); |
| 198 | if (mCallback != null) { |
| 199 | mCallback.onPrioritySettings(); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 200 | } |
| 201 | }); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 202 | mConfigurableTexts.add(mZenIntroductionCustomize, R.string.zen_priority_customize_button); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 203 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 204 | mZenConditions = findViewById(R.id.zen_conditions); |
| 205 | mZenAlarmWarning = findViewById(R.id.zen_alarm_warning); |
| 206 | mZenRadioGroup = findViewById(R.id.zen_radio_buttons); |
| 207 | mZenRadioGroupContent = findViewById(R.id.zen_radio_buttons_content); |
| 208 | |
| 209 | mEdit = findViewById(R.id.edit_container); |
| 210 | |
| 211 | mEmpty = findViewById(android.R.id.empty); |
| 212 | mEmpty.setVisibility(INVISIBLE); |
| 213 | mEmptyText = mEmpty.findViewById(android.R.id.title); |
| 214 | mEmptyIcon = mEmpty.findViewById(android.R.id.icon); |
| 215 | |
| 216 | mAutoRule = findViewById(R.id.auto_rule); |
| 217 | mAutoTitle = mAutoRule.findViewById(android.R.id.title); |
| 218 | mAutoRule.setVisibility(INVISIBLE); |
| 219 | } |
| 220 | |
| 221 | public void setEmptyState(int icon, int text) { |
| 222 | mEmptyIcon.post(() -> { |
| 223 | mEmptyIcon.setImageResource(icon); |
| 224 | mEmptyText.setText(text); |
| 225 | }); |
| 226 | } |
| 227 | |
| 228 | public void setAutoText(CharSequence text) { |
| 229 | mAutoTitle.post(() -> mAutoTitle.setText(text)); |
| 230 | } |
| 231 | |
| 232 | public void setState(int state) { |
| 233 | if (mState == state) return; |
| 234 | transitionFrom(getView(mState), getView(state)); |
| 235 | mState = state; |
| 236 | } |
| 237 | |
| 238 | private void transitionFrom(View from, View to) { |
| 239 | from.post(() -> { |
| 240 | // TODO: Better transitions |
| 241 | to.setAlpha(0); |
| 242 | to.setVisibility(VISIBLE); |
| 243 | to.bringToFront(); |
| 244 | to.animate().cancel(); |
| 245 | to.animate().alpha(1) |
| 246 | .setDuration(TRANSITION_DURATION) |
| 247 | .withEndAction(() -> from.setVisibility(INVISIBLE)) |
| 248 | .start(); |
| 249 | }); |
| 250 | } |
| 251 | |
| 252 | private View getView(int state) { |
| 253 | switch (state) { |
| 254 | case STATE_AUTO_RULE: |
| 255 | return mAutoRule; |
| 256 | case STATE_OFF: |
| 257 | return mEmpty; |
| 258 | default: |
| 259 | return mEdit; |
| 260 | } |
John Spurlock | 814ee6ab | 2015-05-18 11:16:38 -0400 | [diff] [blame] | 261 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 262 | |
John Spurlock | 814ee6ab | 2015-05-18 11:16:38 -0400 | [diff] [blame] | 263 | @Override |
| 264 | protected void onConfigurationChanged(Configuration newConfig) { |
| 265 | super.onConfigurationChanged(newConfig); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 266 | mConfigurableTexts.update(); |
John Spurlock | 814ee6ab | 2015-05-18 11:16:38 -0400 | [diff] [blame] | 267 | if (mZenButtons != null) { |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 268 | mZenButtons.update(); |
Yongjiang Wu | cf398419 | 2016-02-20 18:28:10 +0800 | [diff] [blame] | 269 | } |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 270 | } |
| 271 | |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 272 | private void confirmZenIntroduction() { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 273 | final String prefKey = prefKeyForConfirmation(getSelectedZen(Global.ZEN_MODE_OFF)); |
| 274 | if (prefKey == null) return; |
| 275 | if (DEBUG) Log.d(TAG, "confirmZenIntroduction " + prefKey); |
| 276 | Prefs.putBoolean(mContext, prefKey, true); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 277 | mHandler.sendEmptyMessage(H.UPDATE_WIDGETS); |
| 278 | } |
| 279 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 280 | private static String prefKeyForConfirmation(int zen) { |
| 281 | switch (zen) { |
| 282 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 283 | return Prefs.Key.DND_CONFIRMED_PRIORITY_INTRODUCTION; |
| 284 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
| 285 | return Prefs.Key.DND_CONFIRMED_SILENCE_INTRODUCTION; |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 286 | case Global.ZEN_MODE_ALARMS: |
| 287 | return Prefs.Key.DND_CONFIRMED_ALARM_INTRODUCTION; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 288 | default: |
| 289 | return null; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 290 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Jason Monk | 035a936 | 2017-04-20 15:51:16 -0400 | [diff] [blame] | 293 | private void onAttach() { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 294 | setExpanded(true); |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 295 | mAttached = true; |
Jason Monk | 035a936 | 2017-04-20 15:51:16 -0400 | [diff] [blame] | 296 | mAttachedZen = mController.getZen(); |
| 297 | ZenRule manualRule = mController.getManualRule(); |
| 298 | mExitCondition = manualRule != null ? manualRule.condition : null; |
| 299 | if (DEBUG) Log.d(mTag, "onAttach " + mAttachedZen + " " + manualRule); |
| 300 | handleUpdateManualRule(manualRule); |
| 301 | mZenButtons.setSelectedValue(mAttachedZen, false); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 302 | mSessionZen = mAttachedZen; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 303 | mTransitionHelper.clear(); |
Julia Reynolds | b44ef7e | 2016-06-02 15:29:16 -0400 | [diff] [blame] | 304 | mController.addCallback(mZenCallback); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 305 | setSessionExitCondition(copy(mExitCondition)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 306 | updateWidgets(); |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 307 | setRequestingConditions(!mHidden); |
Jason Monk | 2d77c0f | 2017-05-30 16:08:50 -0400 | [diff] [blame] | 308 | ensureSelection(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Jason Monk | 035a936 | 2017-04-20 15:51:16 -0400 | [diff] [blame] | 311 | private void onDetach() { |
| 312 | if (DEBUG) Log.d(mTag, "onDetach"); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 313 | setExpanded(false); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 314 | checkForAttachedZenChange(); |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 315 | mAttached = false; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 316 | mAttachedZen = -1; |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 317 | mSessionZen = -1; |
Julia Reynolds | b44ef7e | 2016-06-02 15:29:16 -0400 | [diff] [blame] | 318 | mController.removeCallback(mZenCallback); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 319 | setSessionExitCondition(null); |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 320 | setRequestingConditions(false); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 321 | mTransitionHelper.clear(); |
| 322 | } |
| 323 | |
Jason Monk | 035a936 | 2017-04-20 15:51:16 -0400 | [diff] [blame] | 324 | @Override |
| 325 | public void onVisibilityAggregated(boolean isVisible) { |
| 326 | super.onVisibilityAggregated(isVisible); |
| 327 | if (isVisible == mAttached) return; |
| 328 | if (isVisible) { |
| 329 | onAttach(); |
| 330 | } else { |
| 331 | onDetach(); |
| 332 | } |
| 333 | } |
| 334 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 335 | private void setSessionExitCondition(Condition condition) { |
| 336 | if (Objects.equals(condition, mSessionExitCondition)) return; |
| 337 | if (DEBUG) Log.d(mTag, "mSessionExitCondition=" + getConditionId(condition)); |
| 338 | mSessionExitCondition = condition; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 339 | } |
| 340 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 341 | public void setHidden(boolean hidden) { |
| 342 | if (mHidden == hidden) return; |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 343 | if (DEBUG) Log.d(mTag, "hidden=" + hidden); |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 344 | mHidden = hidden; |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 345 | setRequestingConditions(mAttached && !mHidden); |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 346 | updateWidgets(); |
| 347 | } |
| 348 | |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 349 | private void checkForAttachedZenChange() { |
| 350 | final int selectedZen = getSelectedZen(-1); |
| 351 | if (DEBUG) Log.d(mTag, "selectedZen=" + selectedZen); |
| 352 | if (selectedZen != mAttachedZen) { |
| 353 | if (DEBUG) Log.d(mTag, "attachedZen: " + mAttachedZen + " -> " + selectedZen); |
| 354 | if (selectedZen == Global.ZEN_MODE_NO_INTERRUPTIONS) { |
| 355 | mPrefs.trackNoneSelected(); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 360 | private void setExpanded(boolean expanded) { |
| 361 | if (expanded == mExpanded) return; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 362 | if (DEBUG) Log.d(mTag, "setExpanded " + expanded); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 363 | mExpanded = expanded; |
| 364 | updateWidgets(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 365 | fireExpanded(); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** Start or stop requesting relevant zen mode exit conditions */ |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 369 | private void setRequestingConditions(final boolean requesting) { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 370 | if (mRequestingConditions == requesting) return; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 371 | if (DEBUG) Log.d(mTag, "setRequestingConditions " + requesting); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 372 | mRequestingConditions = requesting; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 373 | if (mRequestingConditions) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 374 | mTimeCondition = parseExistingTimeCondition(mContext, mExitCondition); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 375 | if (mTimeCondition != null) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 376 | mBucketIndex = -1; |
| 377 | } else { |
| 378 | mBucketIndex = DEFAULT_BUCKET_INDEX; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 379 | mTimeCondition = ZenModeConfig.toTimeCondition(mContext, |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 380 | MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 381 | } |
| 382 | if (DEBUG) Log.d(mTag, "Initial bucket index: " + mBucketIndex); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 383 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 384 | mConditions = null; // reset conditions |
| 385 | handleUpdateConditions(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 386 | } else { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 387 | hideAllConditions(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 388 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Muyuan Li | 94fa1df | 2016-03-23 12:09:38 -0700 | [diff] [blame] | 391 | protected void addZenConditions(int count) { |
| 392 | for (int i = 0; i < count; i++) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 393 | final View rb = mInflater.inflate(mZenModeButtonLayoutId, mEdit, false); |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 394 | rb.setId(i); |
| 395 | mZenRadioGroup.addView(rb); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 396 | final View rbc = mInflater.inflate(mZenModeConditionLayoutId, mEdit, false); |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 397 | rbc.setId(i + count); |
| 398 | mZenRadioGroupContent.addView(rbc); |
Muyuan Li | 94fa1df | 2016-03-23 12:09:38 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 402 | public void init(ZenModeController controller) { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 403 | mController = controller; |
John Spurlock | ad680d4 | 2015-01-30 15:48:15 -0500 | [diff] [blame] | 404 | mCountdownConditionSupported = mController.isCountdownConditionSupported(); |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 405 | final int countdownDelta = mCountdownConditionSupported ? COUNTDOWN_CONDITION_COUNT : 0; |
John Spurlock | ad680d4 | 2015-01-30 15:48:15 -0500 | [diff] [blame] | 406 | final int minConditions = 1 /*forever*/ + countdownDelta; |
Muyuan Li | 94fa1df | 2016-03-23 12:09:38 -0700 | [diff] [blame] | 407 | addZenConditions(minConditions); |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 408 | mSessionZen = getSelectedZen(-1); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 409 | handleUpdateManualRule(mController.getManualRule()); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 410 | if (DEBUG) Log.d(mTag, "init mExitCondition=" + mExitCondition); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 411 | hideAllConditions(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 412 | } |
| 413 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 414 | private void setExitCondition(Condition exitCondition) { |
John Spurlock | 25c3421 | 2014-11-12 09:16:49 -0500 | [diff] [blame] | 415 | if (Objects.equals(mExitCondition, exitCondition)) return; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 416 | mExitCondition = exitCondition; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 417 | if (DEBUG) Log.d(mTag, "mExitCondition=" + getConditionId(mExitCondition)); |
John Spurlock | 89f060a | 2014-07-16 21:03:15 -0400 | [diff] [blame] | 418 | updateWidgets(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 419 | } |
| 420 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 421 | private static Uri getConditionId(Condition condition) { |
| 422 | return condition != null ? condition.id : null; |
| 423 | } |
| 424 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 425 | private Uri getRealConditionId(Condition condition) { |
| 426 | return isForever(condition) ? null : getConditionId(condition); |
| 427 | } |
| 428 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 429 | private static boolean sameConditionId(Condition lhs, Condition rhs) { |
| 430 | return lhs == null ? rhs == null : rhs != null && lhs.id.equals(rhs.id); |
| 431 | } |
| 432 | |
| 433 | private static Condition copy(Condition condition) { |
| 434 | return condition == null ? null : condition.copy(); |
| 435 | } |
| 436 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 437 | public void setCallback(Callback callback) { |
| 438 | mCallback = callback; |
| 439 | } |
| 440 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 441 | private void handleUpdateManualRule(ZenRule rule) { |
| 442 | final int zen = rule != null ? rule.zenMode : Global.ZEN_MODE_OFF; |
| 443 | handleUpdateZen(zen); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 444 | final Condition c = rule == null ? null |
| 445 | : rule.condition != null ? rule.condition |
| 446 | : createCondition(rule.conditionId); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 447 | handleExitConditionChanged(c); |
| 448 | } |
| 449 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 450 | private Condition createCondition(Uri conditionId) { |
| 451 | if (ZenModeConfig.isValidCountdownConditionId(conditionId)) { |
| 452 | long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); |
| 453 | int mins = (int) ((time - System.currentTimeMillis() + DateUtils.MINUTE_IN_MILLIS / 2) |
| 454 | / DateUtils.MINUTE_IN_MILLIS); |
| 455 | Condition c = ZenModeConfig.toTimeCondition(mContext, time, mins, |
| 456 | ActivityManager.getCurrentUser(), false); |
| 457 | return c; |
| 458 | } |
Jason Monk | 2d77c0f | 2017-05-30 16:08:50 -0400 | [diff] [blame] | 459 | // If there is a manual rule, but it has no condition listed then it is forever. |
| 460 | return forever(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 461 | } |
| 462 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 463 | private void handleUpdateZen(int zen) { |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 464 | if (mSessionZen != -1 && mSessionZen != zen) { |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 465 | mSessionZen = zen; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 466 | } |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 467 | mZenButtons.setSelectedValue(zen, false /* fromClick */); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 468 | updateWidgets(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 469 | handleUpdateConditions(); |
| 470 | if (mExpanded) { |
| 471 | final Condition selected = getSelectedCondition(); |
| 472 | if (!Objects.equals(mExitCondition, selected)) { |
| 473 | select(selected); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 478 | private void handleExitConditionChanged(Condition exitCondition) { |
| 479 | setExitCondition(exitCondition); |
| 480 | if (DEBUG) Log.d(mTag, "handleExitConditionChanged " + mExitCondition); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 481 | if (exitCondition == null) return; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 482 | final int N = getVisibleConditions(); |
| 483 | for (int i = 0; i < N; i++) { |
| 484 | final ConditionTag tag = getConditionTagAt(i); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 485 | if (tag != null && sameConditionId(tag.condition, mExitCondition)) { |
| 486 | bind(exitCondition, mZenRadioGroupContent.getChildAt(i), i); |
Jason Monk | 2d77c0f | 2017-05-30 16:08:50 -0400 | [diff] [blame] | 487 | tag.rb.setChecked(true); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 488 | return; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 489 | } |
| 490 | } |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 491 | if (mCountdownConditionSupported && ZenModeConfig.isValidCountdownConditionId( |
| 492 | exitCondition.id)) { |
| 493 | bind(exitCondition, mZenRadioGroupContent.getChildAt(COUNTDOWN_CONDITION_INDEX), |
| 494 | COUNTDOWN_CONDITION_INDEX); |
Jason Monk | 2d77c0f | 2017-05-30 16:08:50 -0400 | [diff] [blame] | 495 | getConditionTagAt(COUNTDOWN_CONDITION_INDEX).rb.setChecked(true); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 496 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 497 | } |
| 498 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 499 | private Condition getSelectedCondition() { |
| 500 | final int N = getVisibleConditions(); |
| 501 | for (int i = 0; i < N; i++) { |
| 502 | final ConditionTag tag = getConditionTagAt(i); |
| 503 | if (tag != null && tag.rb.isChecked()) { |
| 504 | return tag.condition; |
| 505 | } |
| 506 | } |
| 507 | return null; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | private int getSelectedZen(int defValue) { |
| 511 | final Object zen = mZenButtons.getSelectedValue(); |
| 512 | return zen != null ? (Integer) zen : defValue; |
| 513 | } |
| 514 | |
| 515 | private void updateWidgets() { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 516 | if (mTransitionHelper.isTransitioning()) { |
| 517 | mTransitionHelper.pendingUpdateWidgets(); |
| 518 | return; |
| 519 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 520 | final int zen = getSelectedZen(Global.ZEN_MODE_OFF); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 521 | final boolean zenImportant = zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 522 | final boolean zenNone = zen == Global.ZEN_MODE_NO_INTERRUPTIONS; |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 523 | final boolean zenAlarm = zen == Global.ZEN_MODE_ALARMS; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 524 | final boolean introduction = (zenImportant && !mPrefs.mConfirmedPriorityIntroduction |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 525 | || zenNone && !mPrefs.mConfirmedSilenceIntroduction |
| 526 | || zenAlarm && !mPrefs.mConfirmedAlarmIntroduction); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 527 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 528 | mZenButtons.setVisibility(mHidden ? GONE : VISIBLE); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 529 | mZenIntroduction.setVisibility(introduction ? VISIBLE : GONE); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 530 | if (introduction) { |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 531 | int message = zenImportant |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 532 | ? R.string.zen_priority_introduction |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 533 | : zenAlarm |
| 534 | ? R.string.zen_alarms_introduction |
| 535 | : mVoiceCapable |
| 536 | ? R.string.zen_silence_introduction_voice |
| 537 | : R.string.zen_silence_introduction; |
| 538 | mConfigurableTexts.add(mZenIntroductionMessage, message); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 539 | mConfigurableTexts.update(); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 540 | mZenIntroductionCustomize.setVisibility(zenImportant ? VISIBLE : GONE); |
| 541 | } |
John Spurlock | 8be53ea | 2015-05-29 12:04:21 -0400 | [diff] [blame] | 542 | final String warning = computeAlarmWarningText(zenNone); |
| 543 | mZenAlarmWarning.setVisibility(warning != null ? VISIBLE : GONE); |
| 544 | mZenAlarmWarning.setText(warning); |
| 545 | } |
| 546 | |
| 547 | private String computeAlarmWarningText(boolean zenNone) { |
| 548 | if (!zenNone) { |
| 549 | return null; |
| 550 | } |
| 551 | final long now = System.currentTimeMillis(); |
| 552 | final long nextAlarm = mController.getNextAlarm(); |
| 553 | if (nextAlarm < now) { |
| 554 | return null; |
| 555 | } |
| 556 | int warningRes = 0; |
| 557 | if (mSessionExitCondition == null || isForever(mSessionExitCondition)) { |
| 558 | warningRes = R.string.zen_alarm_warning_indef; |
| 559 | } else { |
| 560 | final long time = ZenModeConfig.tryParseCountdownConditionId(mSessionExitCondition.id); |
| 561 | if (time > now && nextAlarm < time) { |
| 562 | warningRes = R.string.zen_alarm_warning; |
| 563 | } |
| 564 | } |
| 565 | if (warningRes == 0) { |
| 566 | return null; |
| 567 | } |
| 568 | final boolean soon = (nextAlarm - now) < 24 * 60 * 60 * 1000; |
| 569 | final boolean is24 = DateFormat.is24HourFormat(mContext, ActivityManager.getCurrentUser()); |
| 570 | final String skeleton = soon ? (is24 ? "Hm" : "hma") : (is24 ? "EEEHm" : "EEEhma"); |
| 571 | final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); |
| 572 | final CharSequence formattedTime = DateFormat.format(pattern, nextAlarm); |
| 573 | final int templateRes = soon ? R.string.alarm_template : R.string.alarm_template_far; |
| 574 | final String template = getResources().getString(templateRes, formattedTime); |
| 575 | return getResources().getString(warningRes, template); |
John Spurlock | 89f060a | 2014-07-16 21:03:15 -0400 | [diff] [blame] | 576 | } |
| 577 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 578 | private static Condition parseExistingTimeCondition(Context context, Condition condition) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 579 | if (condition == null) return null; |
| 580 | final long time = ZenModeConfig.tryParseCountdownConditionId(condition.id); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 581 | if (time == 0) return null; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 582 | final long now = System.currentTimeMillis(); |
| 583 | final long span = time - now; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 584 | if (span <= 0 || span > MAX_BUCKET_MINUTES * MINUTES_MS) return null; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 585 | return ZenModeConfig.toTimeCondition(context, |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 586 | time, Math.round(span / (float) MINUTES_MS), ActivityManager.getCurrentUser(), |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 587 | false /*shortVersion*/); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 588 | } |
| 589 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 590 | private void handleUpdateConditions() { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 591 | if (mTransitionHelper.isTransitioning()) { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 592 | return; |
| 593 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 594 | final int conditionCount = mConditions == null ? 0 : mConditions.length; |
| 595 | if (DEBUG) Log.d(mTag, "handleUpdateConditions conditionCount=" + conditionCount); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 596 | // forever |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 597 | bind(forever(), mZenRadioGroupContent.getChildAt(FOREVER_CONDITION_INDEX), |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 598 | FOREVER_CONDITION_INDEX); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 599 | // countdown |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 600 | if (mCountdownConditionSupported && mTimeCondition != null) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 601 | bind(mTimeCondition, mZenRadioGroupContent.getChildAt(COUNTDOWN_CONDITION_INDEX), |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 602 | COUNTDOWN_CONDITION_INDEX); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 603 | } |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 604 | // countdown until alarm |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 605 | if (mCountdownConditionSupported) { |
| 606 | Condition nextAlarmCondition = getTimeUntilNextAlarmCondition(); |
| 607 | if (nextAlarmCondition != null) { |
Julia Reynolds | c3960f6 | 2016-06-02 14:04:31 -0400 | [diff] [blame] | 608 | mZenRadioGroup.getChildAt( |
| 609 | COUNTDOWN_ALARM_CONDITION_INDEX).setVisibility(View.VISIBLE); |
| 610 | mZenRadioGroupContent.getChildAt( |
| 611 | COUNTDOWN_ALARM_CONDITION_INDEX).setVisibility(View.VISIBLE); |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 612 | bind(nextAlarmCondition, |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 613 | mZenRadioGroupContent.getChildAt(COUNTDOWN_ALARM_CONDITION_INDEX), |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 614 | COUNTDOWN_ALARM_CONDITION_INDEX); |
Julia Reynolds | c3960f6 | 2016-06-02 14:04:31 -0400 | [diff] [blame] | 615 | } else { |
| 616 | mZenRadioGroup.getChildAt(COUNTDOWN_ALARM_CONDITION_INDEX).setVisibility(View.GONE); |
| 617 | mZenRadioGroupContent.getChildAt( |
| 618 | COUNTDOWN_ALARM_CONDITION_INDEX).setVisibility(View.GONE); |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 619 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 620 | } |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 621 | // ensure something is selected |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 622 | if (mExpanded) { |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 623 | ensureSelection(); |
| 624 | } |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 625 | mZenConditions.setVisibility(mSessionZen != Global.ZEN_MODE_OFF ? View.VISIBLE : View.GONE); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 626 | } |
| 627 | |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 628 | private Condition forever() { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 629 | return new Condition(mForeverId, foreverSummary(mContext), "", "", 0 /*icon*/, |
| 630 | Condition.STATE_TRUE, 0 /*flags*/); |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 631 | } |
| 632 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 633 | private static String foreverSummary(Context context) { |
| 634 | return context.getString(com.android.internal.R.string.zen_mode_forever); |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 635 | } |
| 636 | |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 637 | // Returns a time condition if the next alarm is within the next week. |
| 638 | private Condition getTimeUntilNextAlarmCondition() { |
| 639 | GregorianCalendar weekRange = new GregorianCalendar(); |
| 640 | final long now = weekRange.getTimeInMillis(); |
| 641 | setToMidnight(weekRange); |
Dan Sandler | 12d3393 | 2015-12-29 15:45:39 -0500 | [diff] [blame] | 642 | weekRange.add(Calendar.DATE, 6); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 643 | final long nextAlarmMs = mController.getNextAlarm(); |
| 644 | if (nextAlarmMs > 0) { |
| 645 | GregorianCalendar nextAlarm = new GregorianCalendar(); |
| 646 | nextAlarm.setTimeInMillis(nextAlarmMs); |
| 647 | setToMidnight(nextAlarm); |
| 648 | |
| 649 | if (weekRange.compareTo(nextAlarm) >= 0) { |
Julia Reynolds | 5edde5f | 2016-11-11 14:30:51 -0500 | [diff] [blame] | 650 | return ZenModeConfig.toTimeCondition(mContext, nextAlarmMs, |
| 651 | Math.round((nextAlarmMs - now) / (float) MINUTES_MS), |
| 652 | ActivityManager.getCurrentUser(), true); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | return null; |
| 656 | } |
| 657 | |
| 658 | private void setToMidnight(Calendar calendar) { |
| 659 | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| 660 | calendar.set(Calendar.MINUTE, 0); |
| 661 | calendar.set(Calendar.SECOND, 0); |
| 662 | calendar.set(Calendar.MILLISECOND, 0); |
| 663 | } |
| 664 | |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 665 | private ConditionTag getConditionTagAt(int index) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 666 | return (ConditionTag) mZenRadioGroupContent.getChildAt(index).getTag(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 667 | } |
| 668 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 669 | private int getVisibleConditions() { |
| 670 | int rt = 0; |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 671 | final int N = mZenRadioGroupContent.getChildCount(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 672 | for (int i = 0; i < N; i++) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 673 | rt += mZenRadioGroupContent.getChildAt(i).getVisibility() == VISIBLE ? 1 : 0; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 674 | } |
| 675 | return rt; |
| 676 | } |
| 677 | |
| 678 | private void hideAllConditions() { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 679 | final int N = mZenRadioGroupContent.getChildCount(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 680 | for (int i = 0; i < N; i++) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 681 | mZenRadioGroupContent.getChildAt(i).setVisibility(GONE); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
John Spurlock | 3e07701 | 2014-11-29 13:22:21 -0500 | [diff] [blame] | 685 | private void ensureSelection() { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 686 | // are we left without anything selected? if so, set a default |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 687 | final int visibleConditions = getVisibleConditions(); |
| 688 | if (visibleConditions == 0) return; |
| 689 | for (int i = 0; i < visibleConditions; i++) { |
| 690 | final ConditionTag tag = getConditionTagAt(i); |
| 691 | if (tag != null && tag.rb.isChecked()) { |
| 692 | if (DEBUG) Log.d(mTag, "Not selecting a default, checked=" + tag.condition); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 693 | return; |
| 694 | } |
| 695 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 696 | final ConditionTag foreverTag = getConditionTagAt(FOREVER_CONDITION_INDEX); |
| 697 | if (foreverTag == null) return; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 698 | if (DEBUG) Log.d(mTag, "Selecting a default"); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 699 | final int favoriteIndex = mPrefs.getMinuteIndex(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 700 | if (mExitCondition != null && mExitCondition.equals(mTimeCondition)) { |
| 701 | getConditionTagAt(COUNTDOWN_CONDITION_INDEX).rb.setChecked(true); |
Jason Monk | 035a936 | 2017-04-20 15:51:16 -0400 | [diff] [blame] | 702 | } else if (favoriteIndex == -1 || !mCountdownConditionSupported || |
| 703 | mAttachedZen != Global.ZEN_MODE_OFF) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 704 | foreverTag.rb.setChecked(true); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 705 | } else { |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 706 | mTimeCondition = ZenModeConfig.toTimeCondition(mContext, |
| 707 | MINUTE_BUCKETS[favoriteIndex], ActivityManager.getCurrentUser()); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 708 | mBucketIndex = favoriteIndex; |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 709 | bind(mTimeCondition, mZenRadioGroupContent.getChildAt(COUNTDOWN_CONDITION_INDEX), |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 710 | COUNTDOWN_CONDITION_INDEX); |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 711 | getConditionTagAt(COUNTDOWN_CONDITION_INDEX).rb.setChecked(true); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 715 | private static boolean isCountdown(Condition c) { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 716 | return c != null && ZenModeConfig.isValidCountdownConditionId(c.id); |
| 717 | } |
| 718 | |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 719 | private boolean isForever(Condition c) { |
| 720 | return c != null && mForeverId.equals(c.id); |
| 721 | } |
| 722 | |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 723 | private void bind(final Condition condition, final View row, final int rowId) { |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 724 | if (condition == null) throw new IllegalArgumentException("condition must not be null"); |
| 725 | final boolean enabled = condition.state == Condition.STATE_TRUE; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 726 | final ConditionTag tag = |
| 727 | row.getTag() != null ? (ConditionTag) row.getTag() : new ConditionTag(); |
| 728 | row.setTag(tag); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 729 | final boolean first = tag.rb == null; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 730 | if (tag.rb == null) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 731 | tag.rb = (RadioButton) mZenRadioGroup.getChildAt(rowId); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 732 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 733 | tag.condition = condition; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 734 | final Uri conditionId = getConditionId(tag.condition); |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 735 | if (DEBUG) Log.d(mTag, "bind i=" + mZenRadioGroupContent.indexOfChild(row) + " first=" |
| 736 | + first + " condition=" + conditionId); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 737 | tag.rb.setEnabled(enabled); |
| 738 | tag.rb.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 739 | @Override |
| 740 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 741 | if (mExpanded && isChecked) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 742 | tag.rb.setChecked(true); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 743 | if (DEBUG) Log.d(mTag, "onCheckedChanged " + conditionId); |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 744 | MetricsLogger.action(mContext, MetricsEvent.QS_DND_CONDITION_SELECT); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 745 | select(tag.condition); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 746 | announceConditionSelection(tag); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | }); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 750 | |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 751 | if (tag.lines == null) { |
| 752 | tag.lines = row.findViewById(android.R.id.content); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 753 | } |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 754 | if (tag.line1 == null) { |
| 755 | tag.line1 = (TextView) row.findViewById(android.R.id.text1); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 756 | mConfigurableTexts.add(tag.line1); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 757 | } |
| 758 | if (tag.line2 == null) { |
| 759 | tag.line2 = (TextView) row.findViewById(android.R.id.text2); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 760 | mConfigurableTexts.add(tag.line2); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 761 | } |
John Spurlock | 0b68850 | 2014-12-22 15:13:30 -0500 | [diff] [blame] | 762 | final String line1 = !TextUtils.isEmpty(condition.line1) ? condition.line1 |
| 763 | : condition.summary; |
| 764 | final String line2 = condition.line2; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 765 | tag.line1.setText(line1); |
| 766 | if (TextUtils.isEmpty(line2)) { |
| 767 | tag.line2.setVisibility(GONE); |
| 768 | } else { |
| 769 | tag.line2.setVisibility(VISIBLE); |
| 770 | tag.line2.setText(line2); |
| 771 | } |
| 772 | tag.lines.setEnabled(enabled); |
| 773 | tag.lines.setAlpha(enabled ? 1 : .4f); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 774 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 775 | final ImageView button1 = (ImageView) row.findViewById(android.R.id.button1); |
| 776 | button1.setOnClickListener(new OnClickListener() { |
| 777 | @Override |
| 778 | public void onClick(View v) { |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 779 | onClickTimeButton(row, tag, false /*down*/, rowId); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 780 | } |
| 781 | }); |
| 782 | |
| 783 | final ImageView button2 = (ImageView) row.findViewById(android.R.id.button2); |
| 784 | button2.setOnClickListener(new OnClickListener() { |
| 785 | @Override |
| 786 | public void onClick(View v) { |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 787 | onClickTimeButton(row, tag, true /*up*/, rowId); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 788 | } |
| 789 | }); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 790 | tag.lines.setOnClickListener(new OnClickListener() { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 791 | @Override |
| 792 | public void onClick(View v) { |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 793 | tag.rb.setChecked(true); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 794 | } |
| 795 | }); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 796 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 797 | final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 798 | if (rowId != COUNTDOWN_ALARM_CONDITION_INDEX && time > 0) { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 799 | button1.setVisibility(VISIBLE); |
| 800 | button2.setVisibility(VISIBLE); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 801 | if (mBucketIndex > -1) { |
| 802 | button1.setEnabled(mBucketIndex > 0); |
| 803 | button2.setEnabled(mBucketIndex < MINUTE_BUCKETS.length - 1); |
| 804 | } else { |
| 805 | final long span = time - System.currentTimeMillis(); |
| 806 | button1.setEnabled(span > MIN_BUCKET_MINUTES * MINUTES_MS); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 807 | final Condition maxCondition = ZenModeConfig.toTimeCondition(mContext, |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 808 | MAX_BUCKET_MINUTES, ActivityManager.getCurrentUser()); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 809 | button2.setEnabled(!Objects.equals(condition.summary, maxCondition.summary)); |
| 810 | } |
| 811 | |
| 812 | button1.setAlpha(button1.isEnabled() ? 1f : .5f); |
| 813 | button2.setAlpha(button2.isEnabled() ? 1f : .5f); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 814 | } else { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 815 | button1.setVisibility(GONE); |
| 816 | button2.setVisibility(GONE); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 817 | } |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 818 | // wire up interaction callbacks for newly-added condition rows |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 819 | if (first) { |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 820 | Interaction.register(tag.rb, mInteractionCallback); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 821 | Interaction.register(tag.lines, mInteractionCallback); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 822 | Interaction.register(button1, mInteractionCallback); |
| 823 | Interaction.register(button2, mInteractionCallback); |
| 824 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 825 | row.setVisibility(VISIBLE); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | private void announceConditionSelection(ConditionTag tag) { |
| 829 | final int zen = getSelectedZen(Global.ZEN_MODE_OFF); |
| 830 | String modeText; |
| 831 | switch(zen) { |
| 832 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 833 | modeText = mContext.getString(R.string.interruption_level_priority); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 834 | break; |
| 835 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 836 | modeText = mContext.getString(R.string.interruption_level_none); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 837 | break; |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 838 | case Global.ZEN_MODE_ALARMS: |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 839 | modeText = mContext.getString(R.string.interruption_level_alarms); |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 840 | break; |
| 841 | default: |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | announceForAccessibility(mContext.getString(R.string.zen_mode_and_condition, modeText, |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 845 | tag.line1.getText())); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 846 | } |
| 847 | |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 848 | private void onClickTimeButton(View row, ConditionTag tag, boolean up, int rowId) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 849 | MetricsLogger.action(mContext, MetricsEvent.QS_DND_TIME, up); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 850 | Condition newCondition = null; |
| 851 | final int N = MINUTE_BUCKETS.length; |
| 852 | if (mBucketIndex == -1) { |
| 853 | // not on a known index, search for the next or prev bucket by time |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 854 | final Uri conditionId = getConditionId(tag.condition); |
| 855 | final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 856 | final long now = System.currentTimeMillis(); |
| 857 | for (int i = 0; i < N; i++) { |
| 858 | int j = up ? i : N - 1 - i; |
| 859 | final int bucketMinutes = MINUTE_BUCKETS[j]; |
| 860 | final long bucketTime = now + bucketMinutes * MINUTES_MS; |
| 861 | if (up && bucketTime > time || !up && bucketTime < time) { |
| 862 | mBucketIndex = j; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 863 | newCondition = ZenModeConfig.toTimeCondition(mContext, |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 864 | bucketTime, bucketMinutes, ActivityManager.getCurrentUser(), |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 865 | false /*shortVersion*/); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 866 | break; |
| 867 | } |
| 868 | } |
| 869 | if (newCondition == null) { |
| 870 | mBucketIndex = DEFAULT_BUCKET_INDEX; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 871 | newCondition = ZenModeConfig.toTimeCondition(mContext, |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 872 | MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 873 | } |
| 874 | } else { |
| 875 | // on a known index, simply increment or decrement |
| 876 | mBucketIndex = Math.max(0, Math.min(N - 1, mBucketIndex + (up ? 1 : -1))); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 877 | newCondition = ZenModeConfig.toTimeCondition(mContext, |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 878 | MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 879 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 880 | mTimeCondition = newCondition; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 881 | bind(mTimeCondition, row, rowId); |
Julia Reynolds | 656f986 | 2016-06-01 11:37:29 -0400 | [diff] [blame] | 882 | tag.rb.setChecked(true); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 883 | select(mTimeCondition); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 884 | announceConditionSelection(tag); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 885 | } |
| 886 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 887 | private void select(final Condition condition) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 888 | if (DEBUG) Log.d(mTag, "select " + condition); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 889 | if (mSessionZen == -1 || mSessionZen == Global.ZEN_MODE_OFF) { |
| 890 | if (DEBUG) Log.d(mTag, "Ignoring condition selection outside of manual zen"); |
| 891 | return; |
| 892 | } |
| 893 | final Uri realConditionId = getRealConditionId(condition); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 894 | if (mController != null) { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 895 | AsyncTask.execute(new Runnable() { |
| 896 | @Override |
| 897 | public void run() { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 898 | mController.setZen(mSessionZen, realConditionId, TAG + ".selectCondition"); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 899 | } |
| 900 | }); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 901 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 902 | setExitCondition(condition); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 903 | if (realConditionId == null) { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 904 | mPrefs.setMinuteIndex(-1); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 905 | } else if (isCountdown(condition) && mBucketIndex != -1) { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 906 | mPrefs.setMinuteIndex(mBucketIndex); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 907 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 908 | setSessionExitCondition(copy(condition)); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 909 | } |
| 910 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 911 | private void fireInteraction() { |
| 912 | if (mCallback != null) { |
| 913 | mCallback.onInteraction(); |
| 914 | } |
| 915 | } |
| 916 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 917 | private void fireExpanded() { |
| 918 | if (mCallback != null) { |
| 919 | mCallback.onExpanded(mExpanded); |
| 920 | } |
| 921 | } |
| 922 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 923 | private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() { |
| 924 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 925 | public void onManualRuleChanged(ZenRule rule) { |
| 926 | mHandler.obtainMessage(H.MANUAL_RULE_CHANGED, rule).sendToTarget(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 927 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 928 | }; |
| 929 | |
| 930 | private final class H extends Handler { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 931 | private static final int MANUAL_RULE_CHANGED = 2; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 932 | private static final int UPDATE_WIDGETS = 3; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 933 | |
| 934 | private H() { |
| 935 | super(Looper.getMainLooper()); |
| 936 | } |
| 937 | |
| 938 | @Override |
| 939 | public void handleMessage(Message msg) { |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 940 | switch (msg.what) { |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 941 | case MANUAL_RULE_CHANGED: handleUpdateManualRule((ZenRule) msg.obj); break; |
| 942 | case UPDATE_WIDGETS: updateWidgets(); break; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | public interface Callback { |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 948 | void onPrioritySettings(); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 949 | void onInteraction(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 950 | void onExpanded(boolean expanded); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 951 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 952 | |
| 953 | // used as the view tag on condition rows |
| 954 | private static class ConditionTag { |
| 955 | RadioButton rb; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 956 | View lines; |
| 957 | TextView line1; |
| 958 | TextView line2; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 959 | Condition condition; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 960 | } |
| 961 | |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 962 | private final class ZenPrefs implements OnSharedPreferenceChangeListener { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 963 | private final int mNoneDangerousThreshold; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 964 | |
| 965 | private int mMinuteIndex; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 966 | private int mNoneSelected; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 967 | private boolean mConfirmedPriorityIntroduction; |
| 968 | private boolean mConfirmedSilenceIntroduction; |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 969 | private boolean mConfirmedAlarmIntroduction; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 970 | |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 971 | private ZenPrefs() { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 972 | mNoneDangerousThreshold = mContext.getResources() |
| 973 | .getInteger(R.integer.zen_mode_alarm_warning_threshold); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 974 | Prefs.registerListener(mContext, this); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 975 | updateMinuteIndex(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 976 | updateNoneSelected(); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 977 | updateConfirmedPriorityIntroduction(); |
| 978 | updateConfirmedSilenceIntroduction(); |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 979 | updateConfirmedAlarmIntroduction(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | public void trackNoneSelected() { |
| 983 | mNoneSelected = clampNoneSelected(mNoneSelected + 1); |
| 984 | if (DEBUG) Log.d(mTag, "Setting none selected: " + mNoneSelected + " threshold=" |
| 985 | + mNoneDangerousThreshold); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 986 | Prefs.putInt(mContext, Prefs.Key.DND_NONE_SELECTED, mNoneSelected); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | public int getMinuteIndex() { |
| 990 | return mMinuteIndex; |
| 991 | } |
| 992 | |
| 993 | public void setMinuteIndex(int minuteIndex) { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 994 | minuteIndex = clampIndex(minuteIndex); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 995 | if (minuteIndex == mMinuteIndex) return; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 996 | mMinuteIndex = clampIndex(minuteIndex); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 997 | if (DEBUG) Log.d(mTag, "Setting favorite minute index: " + mMinuteIndex); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 998 | Prefs.putInt(mContext, Prefs.Key.DND_FAVORITE_BUCKET_INDEX, mMinuteIndex); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | @Override |
| 1002 | public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
| 1003 | updateMinuteIndex(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 1004 | updateNoneSelected(); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1005 | updateConfirmedPriorityIntroduction(); |
| 1006 | updateConfirmedSilenceIntroduction(); |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 1007 | updateConfirmedAlarmIntroduction(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | private void updateMinuteIndex() { |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1011 | mMinuteIndex = clampIndex(Prefs.getInt(mContext, |
| 1012 | Prefs.Key.DND_FAVORITE_BUCKET_INDEX, DEFAULT_BUCKET_INDEX)); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 1013 | if (DEBUG) Log.d(mTag, "Favorite minute index: " + mMinuteIndex); |
| 1014 | } |
| 1015 | |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 1016 | private int clampIndex(int index) { |
| 1017 | return MathUtils.constrain(index, -1, MINUTE_BUCKETS.length - 1); |
| 1018 | } |
| 1019 | |
| 1020 | private void updateNoneSelected() { |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1021 | mNoneSelected = clampNoneSelected(Prefs.getInt(mContext, |
| 1022 | Prefs.Key.DND_NONE_SELECTED, 0)); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 1023 | if (DEBUG) Log.d(mTag, "None selected: " + mNoneSelected); |
| 1024 | } |
| 1025 | |
| 1026 | private int clampNoneSelected(int noneSelected) { |
| 1027 | return MathUtils.constrain(noneSelected, 0, Integer.MAX_VALUE); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 1028 | } |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1029 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1030 | private void updateConfirmedPriorityIntroduction() { |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1031 | final boolean confirmed = Prefs.getBoolean(mContext, |
| 1032 | Prefs.Key.DND_CONFIRMED_PRIORITY_INTRODUCTION, false); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1033 | if (confirmed == mConfirmedPriorityIntroduction) return; |
| 1034 | mConfirmedPriorityIntroduction = confirmed; |
| 1035 | if (DEBUG) Log.d(mTag, "Confirmed priority introduction: " |
| 1036 | + mConfirmedPriorityIntroduction); |
| 1037 | } |
| 1038 | |
| 1039 | private void updateConfirmedSilenceIntroduction() { |
| 1040 | final boolean confirmed = Prefs.getBoolean(mContext, |
| 1041 | Prefs.Key.DND_CONFIRMED_SILENCE_INTRODUCTION, false); |
| 1042 | if (confirmed == mConfirmedSilenceIntroduction) return; |
| 1043 | mConfirmedSilenceIntroduction = confirmed; |
| 1044 | if (DEBUG) Log.d(mTag, "Confirmed silence introduction: " |
| 1045 | + mConfirmedSilenceIntroduction); |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1046 | } |
Julia Reynolds | d7a00aa | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 1047 | |
| 1048 | private void updateConfirmedAlarmIntroduction() { |
| 1049 | final boolean confirmed = Prefs.getBoolean(mContext, |
| 1050 | Prefs.Key.DND_CONFIRMED_ALARM_INTRODUCTION, false); |
| 1051 | if (confirmed == mConfirmedAlarmIntroduction) return; |
| 1052 | mConfirmedAlarmIntroduction = confirmed; |
| 1053 | if (DEBUG) Log.d(mTag, "Confirmed alarm introduction: " |
| 1054 | + mConfirmedAlarmIntroduction); |
| 1055 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 1056 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 1057 | |
Muyuan Li | 8303bd2 | 2016-03-23 20:08:26 -0700 | [diff] [blame] | 1058 | protected final SegmentedButtons.Callback mZenButtonsCallback = new SegmentedButtons.Callback() { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 1059 | @Override |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 1060 | public void onSelected(final Object value, boolean fromClick) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1061 | if (value != null && mZenButtons.isShown() && isAttachedToWindow()) { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1062 | final int zen = (Integer) value; |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 1063 | if (fromClick) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 1064 | MetricsLogger.action(mContext, MetricsEvent.QS_DND_ZEN_SELECT, zen); |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 1065 | } |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1066 | if (DEBUG) Log.d(mTag, "mZenButtonsCallback selected=" + zen); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1067 | final Uri realConditionId = getRealConditionId(mSessionExitCondition); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1068 | AsyncTask.execute(new Runnable() { |
| 1069 | @Override |
| 1070 | public void run() { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1071 | mController.setZen(zen, realConditionId, TAG + ".selectZen"); |
| 1072 | if (zen != Global.ZEN_MODE_OFF) { |
| 1073 | Prefs.putInt(mContext, Prefs.Key.DND_FAVORITE_ZEN, zen); |
| 1074 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1075 | } |
| 1076 | }); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 1077 | } |
| 1078 | } |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 1079 | |
| 1080 | @Override |
| 1081 | public void onInteraction() { |
| 1082 | fireInteraction(); |
| 1083 | } |
| 1084 | }; |
| 1085 | |
| 1086 | private final Interaction.Callback mInteractionCallback = new Interaction.Callback() { |
| 1087 | @Override |
| 1088 | public void onInteraction() { |
| 1089 | fireInteraction(); |
| 1090 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 1091 | }; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1092 | |
| 1093 | private final class TransitionHelper implements TransitionListener, Runnable { |
| 1094 | private final ArraySet<View> mTransitioningViews = new ArraySet<View>(); |
| 1095 | |
| 1096 | private boolean mTransitioning; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1097 | private boolean mPendingUpdateWidgets; |
| 1098 | |
| 1099 | public void clear() { |
| 1100 | mTransitioningViews.clear(); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 1101 | mPendingUpdateWidgets = false; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1105 | pw.println(" TransitionHelper state:"); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1106 | pw.print(" mPendingUpdateWidgets="); pw.println(mPendingUpdateWidgets); |
| 1107 | pw.print(" mTransitioning="); pw.println(mTransitioning); |
| 1108 | pw.print(" mTransitioningViews="); pw.println(mTransitioningViews); |
| 1109 | } |
| 1110 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1111 | public void pendingUpdateWidgets() { |
| 1112 | mPendingUpdateWidgets = true; |
| 1113 | } |
| 1114 | |
| 1115 | public boolean isTransitioning() { |
| 1116 | return !mTransitioningViews.isEmpty(); |
| 1117 | } |
| 1118 | |
| 1119 | @Override |
| 1120 | public void startTransition(LayoutTransition transition, |
| 1121 | ViewGroup container, View view, int transitionType) { |
| 1122 | mTransitioningViews.add(view); |
| 1123 | updateTransitioning(); |
| 1124 | } |
| 1125 | |
| 1126 | @Override |
| 1127 | public void endTransition(LayoutTransition transition, |
| 1128 | ViewGroup container, View view, int transitionType) { |
| 1129 | mTransitioningViews.remove(view); |
| 1130 | updateTransitioning(); |
| 1131 | } |
| 1132 | |
| 1133 | @Override |
| 1134 | public void run() { |
| 1135 | if (DEBUG) Log.d(mTag, "TransitionHelper run" |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 1136 | + " mPendingUpdateWidgets=" + mPendingUpdateWidgets); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1137 | if (mPendingUpdateWidgets) { |
| 1138 | updateWidgets(); |
| 1139 | } |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 1140 | mPendingUpdateWidgets = false; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | private void updateTransitioning() { |
| 1144 | final boolean transitioning = isTransitioning(); |
| 1145 | if (mTransitioning == transitioning) return; |
| 1146 | mTransitioning = transitioning; |
| 1147 | if (DEBUG) Log.d(mTag, "TransitionHelper mTransitioning=" + mTransitioning); |
| 1148 | if (!mTransitioning) { |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 1149 | if (mPendingUpdateWidgets) { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1150 | mHandler.post(this); |
| 1151 | } else { |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 1152 | mPendingUpdateWidgets = false; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1157 | } |