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