Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.keyguard; |
| 18 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 19 | import android.annotation.AnyThread; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager; |
| 21 | import android.app.AlarmManager; |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 22 | import android.app.PendingIntent; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 28 | import android.graphics.Typeface; |
| 29 | import android.graphics.drawable.Icon; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 30 | import android.icu.text.DateFormat; |
| 31 | import android.icu.text.DisplayContext; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 32 | import android.media.MediaMetadata; |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 33 | import android.media.session.PlaybackState; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
| 35 | import android.os.Handler; |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 36 | import android.os.Trace; |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 37 | import android.provider.Settings; |
| 38 | import android.service.notification.ZenModeConfig; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 39 | import android.text.TextUtils; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 40 | import android.text.style.StyleSpan; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 41 | |
Lucas Dupin | 64d1e12 | 2018-11-12 14:30:36 -0800 | [diff] [blame] | 42 | import androidx.core.graphics.drawable.IconCompat; |
| 43 | import androidx.slice.Slice; |
| 44 | import androidx.slice.SliceProvider; |
| 45 | import androidx.slice.builders.ListBuilder; |
| 46 | import androidx.slice.builders.ListBuilder.RowBuilder; |
| 47 | import androidx.slice.builders.SliceAction; |
| 48 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 49 | import com.android.internal.annotations.VisibleForTesting; |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 50 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 51 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 52 | import com.android.systemui.R; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 53 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 54 | import com.android.systemui.statusbar.NotificationMediaManager; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 55 | import com.android.systemui.statusbar.policy.NextAlarmController; |
| 56 | import com.android.systemui.statusbar.policy.NextAlarmControllerImpl; |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 57 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 58 | import com.android.systemui.statusbar.policy.ZenModeControllerImpl; |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 59 | import com.android.systemui.util.wakelock.SettableWakeLock; |
| 60 | import com.android.systemui.util.wakelock.WakeLock; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 61 | |
| 62 | import java.util.Date; |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 63 | import java.util.HashSet; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 64 | import java.util.Locale; |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 65 | import java.util.TimeZone; |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 66 | import java.util.concurrent.TimeUnit; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Simple Slice provider that shows the current date. |
| 70 | */ |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 71 | public class KeyguardSliceProvider extends SliceProvider implements |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 72 | NextAlarmController.NextAlarmChangeCallback, ZenModeController.Callback, |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 73 | NotificationMediaManager.MediaListener, StatusBarStateController.StateListener { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 74 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 75 | private static final StyleSpan BOLD_STYLE = new StyleSpan(Typeface.BOLD); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 76 | public static final String KEYGUARD_SLICE_URI = "content://com.android.systemui.keyguard/main"; |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 77 | private static final String KEYGUARD_HEADER_URI = |
| 78 | "content://com.android.systemui.keyguard/header"; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 79 | public static final String KEYGUARD_DATE_URI = "content://com.android.systemui.keyguard/date"; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 80 | public static final String KEYGUARD_NEXT_ALARM_URI = |
| 81 | "content://com.android.systemui.keyguard/alarm"; |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 82 | public static final String KEYGUARD_DND_URI = "content://com.android.systemui.keyguard/dnd"; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 83 | public static final String KEYGUARD_MEDIA_URI = |
| 84 | "content://com.android.systemui.keyguard/media"; |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 85 | public static final String KEYGUARD_ACTION_URI = |
| 86 | "content://com.android.systemui.keyguard/action"; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 87 | |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Only show alarms that will ring within N hours. |
| 90 | */ |
| 91 | @VisibleForTesting |
| 92 | static final int ALARM_VISIBILITY_HOURS = 12; |
| 93 | |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 94 | private static KeyguardSliceProvider sInstance; |
| 95 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 96 | protected final Uri mSliceUri; |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 97 | protected final Uri mHeaderUri; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 98 | protected final Uri mDateUri; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 99 | protected final Uri mAlarmUri; |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 100 | protected final Uri mDndUri; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 101 | protected final Uri mMediaUri; |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 102 | private final Date mCurrentTime = new Date(); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 103 | private final Handler mHandler; |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 104 | private final AlarmManager.OnAlarmListener mUpdateNextAlarm = this::updateNextAlarm; |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 105 | private final HashSet<Integer> mMediaInvisibleStates; |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 106 | private final Object mMediaToken = new Object(); |
Lucas Dupin | ba91479 | 2019-06-20 14:07:57 -0700 | [diff] [blame] | 107 | @VisibleForTesting |
| 108 | protected SettableWakeLock mMediaWakeLock; |
| 109 | @VisibleForTesting |
| 110 | protected ZenModeController mZenModeController; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 111 | private String mDatePattern; |
| 112 | private DateFormat mDateFormat; |
| 113 | private String mLastText; |
| 114 | private boolean mRegistered; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 115 | private String mNextAlarm; |
| 116 | private NextAlarmController mNextAlarmController; |
Lucas Dupin | ba91479 | 2019-06-20 14:07:57 -0700 | [diff] [blame] | 117 | @VisibleForTesting |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 118 | protected AlarmManager mAlarmManager; |
Lucas Dupin | ba91479 | 2019-06-20 14:07:57 -0700 | [diff] [blame] | 119 | @VisibleForTesting |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 120 | protected ContentResolver mContentResolver; |
| 121 | private AlarmManager.AlarmClockInfo mNextAlarmInfo; |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 122 | private PendingIntent mPendingIntent; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 123 | protected NotificationMediaManager mMediaManager; |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 124 | private StatusBarStateController mStatusBarStateController; |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 125 | private CharSequence mMediaTitle; |
| 126 | private CharSequence mMediaArtist; |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 127 | protected boolean mDozing; |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 128 | private boolean mMediaIsVisible; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Receiver responsible for time ticking and updating the date format. |
| 132 | */ |
| 133 | @VisibleForTesting |
| 134 | final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 135 | @Override |
| 136 | public void onReceive(Context context, Intent intent) { |
| 137 | final String action = intent.getAction(); |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 138 | if (Intent.ACTION_DATE_CHANGED.equals(action)) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 139 | synchronized (this) { |
| 140 | updateClockLocked(); |
| 141 | } |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 142 | } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 143 | synchronized (this) { |
| 144 | cleanDateFormatLocked(); |
| 145 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | }; |
| 149 | |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 150 | @VisibleForTesting |
| 151 | final KeyguardUpdateMonitorCallback mKeyguardUpdateMonitorCallback = |
| 152 | new KeyguardUpdateMonitorCallback() { |
| 153 | @Override |
| 154 | public void onTimeChanged() { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 155 | synchronized (this) { |
| 156 | updateClockLocked(); |
| 157 | } |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void onTimeZoneChanged(TimeZone timeZone) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 162 | synchronized (this) { |
| 163 | cleanDateFormatLocked(); |
| 164 | } |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 165 | } |
| 166 | }; |
| 167 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 168 | public KeyguardSliceProvider() { |
| 169 | this(new Handler()); |
| 170 | } |
| 171 | |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 172 | public static KeyguardSliceProvider getAttachedInstance() { |
| 173 | return KeyguardSliceProvider.sInstance; |
| 174 | } |
| 175 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 176 | @VisibleForTesting |
| 177 | KeyguardSliceProvider(Handler handler) { |
| 178 | mHandler = handler; |
| 179 | mSliceUri = Uri.parse(KEYGUARD_SLICE_URI); |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 180 | mHeaderUri = Uri.parse(KEYGUARD_HEADER_URI); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 181 | mDateUri = Uri.parse(KEYGUARD_DATE_URI); |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 182 | mAlarmUri = Uri.parse(KEYGUARD_NEXT_ALARM_URI); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 183 | mDndUri = Uri.parse(KEYGUARD_DND_URI); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 184 | mMediaUri = Uri.parse(KEYGUARD_MEDIA_URI); |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 185 | |
| 186 | mMediaInvisibleStates = new HashSet<>(); |
| 187 | mMediaInvisibleStates.add(PlaybackState.STATE_NONE); |
| 188 | mMediaInvisibleStates.add(PlaybackState.STATE_STOPPED); |
| 189 | mMediaInvisibleStates.add(PlaybackState.STATE_PAUSED); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 192 | /** |
| 193 | * Initialize dependencies that don't exist during {@link android.content.ContentProvider} |
| 194 | * instantiation. |
| 195 | * |
| 196 | * @param mediaManager {@link NotificationMediaManager} singleton. |
| 197 | * @param statusBarStateController {@link StatusBarStateController} singleton. |
| 198 | */ |
| 199 | public void initDependencies( |
| 200 | NotificationMediaManager mediaManager, |
| 201 | StatusBarStateController statusBarStateController) { |
| 202 | mMediaManager = mediaManager; |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 203 | mMediaManager.addCallback(this); |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 204 | mStatusBarStateController = statusBarStateController; |
| 205 | mStatusBarStateController.addCallback(this); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @AnyThread |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 209 | @Override |
| 210 | public Slice onBindSlice(Uri sliceUri) { |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 211 | Trace.beginSection("KeyguardSliceProvider#onBindSlice"); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 212 | Slice slice; |
| 213 | synchronized (this) { |
| 214 | ListBuilder builder = new ListBuilder(getContext(), mSliceUri, ListBuilder.INFINITY); |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 215 | if (needsMediaLocked()) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 216 | addMediaLocked(builder); |
| 217 | } else { |
| 218 | builder.addRow(new RowBuilder(mDateUri).setTitle(mLastText)); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 219 | } |
Lucas Dupin | c954824 | 2019-04-11 10:59:13 -0700 | [diff] [blame] | 220 | addNextAlarmLocked(builder); |
| 221 | addZenModeLocked(builder); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 222 | addPrimaryActionLocked(builder); |
| 223 | slice = builder.build(); |
| 224 | } |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 225 | Trace.endSection(); |
| 226 | return slice; |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 229 | protected boolean needsMediaLocked() { |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 230 | return !TextUtils.isEmpty(mMediaTitle) && mMediaIsVisible && mDozing; |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 233 | protected void addMediaLocked(ListBuilder listBuilder) { |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 234 | if (TextUtils.isEmpty(mMediaTitle)) { |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 235 | return; |
| 236 | } |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 237 | listBuilder.setHeader(new ListBuilder.HeaderBuilder(mHeaderUri).setTitle(mMediaTitle)); |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 238 | |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 239 | if (!TextUtils.isEmpty(mMediaArtist)) { |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 240 | RowBuilder albumBuilder = new RowBuilder(mMediaUri); |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 241 | albumBuilder.setTitle(mMediaArtist); |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 242 | |
| 243 | Icon mediaIcon = mMediaManager == null ? null : mMediaManager.getMediaIcon(); |
| 244 | IconCompat mediaIconCompat = mediaIcon == null ? null |
| 245 | : IconCompat.createFromIcon(getContext(), mediaIcon); |
| 246 | if (mediaIconCompat != null) { |
| 247 | albumBuilder.addEndItem(mediaIconCompat, ListBuilder.ICON_IMAGE); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 248 | } |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 249 | |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 250 | listBuilder.addRow(albumBuilder); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | protected void addPrimaryActionLocked(ListBuilder builder) { |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 255 | // Add simple action because API requires it; Keyguard handles presenting |
| 256 | // its own slices so this action + icon are actually never used. |
Jason Monk | 1045e0b | 2018-08-06 09:42:10 -0400 | [diff] [blame] | 257 | IconCompat icon = IconCompat.createWithResource(getContext(), |
| 258 | R.drawable.ic_access_alarms_big); |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 259 | SliceAction action = SliceAction.createDeeplink(mPendingIntent, icon, |
Jason Monk | 1045e0b | 2018-08-06 09:42:10 -0400 | [diff] [blame] | 260 | ListBuilder.ICON_IMAGE, mLastText); |
Jason Monk | 1045e0b | 2018-08-06 09:42:10 -0400 | [diff] [blame] | 261 | RowBuilder primaryActionRow = new RowBuilder(Uri.parse(KEYGUARD_ACTION_URI)) |
| 262 | .setPrimaryAction(action); |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 263 | builder.addRow(primaryActionRow); |
| 264 | } |
| 265 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 266 | protected void addNextAlarmLocked(ListBuilder builder) { |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 267 | if (TextUtils.isEmpty(mNextAlarm)) { |
| 268 | return; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 269 | } |
Jason Monk | 1045e0b | 2018-08-06 09:42:10 -0400 | [diff] [blame] | 270 | IconCompat alarmIcon = IconCompat.createWithResource(getContext(), |
| 271 | R.drawable.ic_access_alarms_big); |
| 272 | RowBuilder alarmRowBuilder = new RowBuilder(mAlarmUri) |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 273 | .setTitle(mNextAlarm) |
Jason Monk | 1045e0b | 2018-08-06 09:42:10 -0400 | [diff] [blame] | 274 | .addEndItem(alarmIcon, ListBuilder.ICON_IMAGE); |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 275 | builder.addRow(alarmRowBuilder); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 278 | /** |
| 279 | * Add zen mode (DND) icon to slice if it's enabled. |
| 280 | * @param builder The slice builder. |
| 281 | */ |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 282 | protected void addZenModeLocked(ListBuilder builder) { |
Lucas Dupin | 627ad37 | 2018-11-27 10:28:25 +0100 | [diff] [blame] | 283 | if (!isDndOn()) { |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 284 | return; |
| 285 | } |
Jason Monk | 1045e0b | 2018-08-06 09:42:10 -0400 | [diff] [blame] | 286 | RowBuilder dndBuilder = new RowBuilder(mDndUri) |
Lucas Dupin | 1d5e910 | 2018-05-21 17:58:05 -0700 | [diff] [blame] | 287 | .setContentDescription(getContext().getResources() |
| 288 | .getString(R.string.accessibility_quick_settings_dnd)) |
Amin Shaikh | ad29e93 | 2019-03-08 11:26:12 -0500 | [diff] [blame] | 289 | .addEndItem( |
| 290 | IconCompat.createWithResource(getContext(), R.drawable.stat_sys_dnd), |
| 291 | ListBuilder.ICON_IMAGE); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 292 | builder.addRow(dndBuilder); |
| 293 | } |
| 294 | |
| 295 | /** |
Lucas Dupin | 627ad37 | 2018-11-27 10:28:25 +0100 | [diff] [blame] | 296 | * Return true if DND is enabled. |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 297 | */ |
Lucas Dupin | 627ad37 | 2018-11-27 10:28:25 +0100 | [diff] [blame] | 298 | protected boolean isDndOn() { |
| 299 | return mZenModeController.getZen() != Settings.Global.ZEN_MODE_OFF; |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 302 | @Override |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 303 | public boolean onCreateSliceProvider() { |
Lucas Dupin | ba91479 | 2019-06-20 14:07:57 -0700 | [diff] [blame] | 304 | synchronized (this) { |
| 305 | KeyguardSliceProvider oldInstance = KeyguardSliceProvider.sInstance; |
| 306 | if (oldInstance != null) { |
| 307 | oldInstance.onDestroy(); |
| 308 | } |
| 309 | |
| 310 | mAlarmManager = getContext().getSystemService(AlarmManager.class); |
| 311 | mContentResolver = getContext().getContentResolver(); |
| 312 | mNextAlarmController = new NextAlarmControllerImpl(getContext()); |
| 313 | mNextAlarmController.addCallback(this); |
| 314 | mZenModeController = new ZenModeControllerImpl(getContext(), mHandler); |
| 315 | mZenModeController.addCallback(this); |
| 316 | mDatePattern = getContext().getString(R.string.system_ui_aod_date_pattern); |
| 317 | mPendingIntent = PendingIntent.getActivity(getContext(), 0, new Intent(), 0); |
| 318 | mMediaWakeLock = new SettableWakeLock(WakeLock.createPartial(getContext(), "media"), |
| 319 | "media"); |
| 320 | KeyguardSliceProvider.sInstance = this; |
| 321 | registerClockUpdate(); |
| 322 | updateClockLocked(); |
| 323 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 324 | return true; |
| 325 | } |
| 326 | |
Lucas Dupin | ba91479 | 2019-06-20 14:07:57 -0700 | [diff] [blame] | 327 | @VisibleForTesting |
| 328 | protected void onDestroy() { |
| 329 | synchronized (this) { |
| 330 | mNextAlarmController.removeCallback(this); |
| 331 | mZenModeController.removeCallback(this); |
| 332 | mMediaWakeLock.setAcquired(false); |
| 333 | mAlarmManager.cancel(mUpdateNextAlarm); |
| 334 | if (mRegistered) { |
| 335 | mRegistered = false; |
| 336 | getKeyguardUpdateMonitor().removeCallback(mKeyguardUpdateMonitorCallback); |
| 337 | getContext().unregisterReceiver(mIntentReceiver); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 342 | @Override |
| 343 | public void onZenChanged(int zen) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 344 | notifyChange(); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void onConfigChanged(ZenModeConfig config) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 349 | notifyChange(); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 352 | private void updateNextAlarm() { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 353 | synchronized (this) { |
| 354 | if (withinNHoursLocked(mNextAlarmInfo, ALARM_VISIBILITY_HOURS)) { |
| 355 | String pattern = android.text.format.DateFormat.is24HourFormat(getContext(), |
| 356 | ActivityManager.getCurrentUser()) ? "HH:mm" : "h:mm"; |
| 357 | mNextAlarm = android.text.format.DateFormat.format(pattern, |
| 358 | mNextAlarmInfo.getTriggerTime()).toString(); |
| 359 | } else { |
| 360 | mNextAlarm = ""; |
| 361 | } |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 362 | } |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 363 | notifyChange(); |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 366 | private boolean withinNHoursLocked(AlarmManager.AlarmClockInfo alarmClockInfo, int hours) { |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 367 | if (alarmClockInfo == null) { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | long limit = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(hours); |
| 372 | return mNextAlarmInfo.getTriggerTime() <= limit; |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 375 | /** |
| 376 | * Registers a broadcast receiver for clock updates, include date, time zone and manually |
| 377 | * changing the date/time via the settings app. |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 378 | */ |
Lucas Dupin | ba91479 | 2019-06-20 14:07:57 -0700 | [diff] [blame] | 379 | @VisibleForTesting |
| 380 | protected void registerClockUpdate() { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 381 | synchronized (this) { |
| 382 | if (mRegistered) { |
| 383 | return; |
| 384 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 385 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 386 | IntentFilter filter = new IntentFilter(); |
| 387 | filter.addAction(Intent.ACTION_DATE_CHANGED); |
| 388 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 389 | getContext().registerReceiver(mIntentReceiver, filter, null /* permission*/, |
| 390 | null /* scheduler */); |
| 391 | getKeyguardUpdateMonitor().registerCallback(mKeyguardUpdateMonitorCallback); |
| 392 | mRegistered = true; |
| 393 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | @VisibleForTesting |
| 397 | boolean isRegistered() { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 398 | synchronized (this) { |
| 399 | return mRegistered; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 403 | protected void updateClockLocked() { |
| 404 | final String text = getFormattedDateLocked(); |
| 405 | if (!text.equals(mLastText)) { |
| 406 | mLastText = text; |
| 407 | notifyChange(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | protected String getFormattedDateLocked() { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 412 | if (mDateFormat == null) { |
| 413 | final Locale l = Locale.getDefault(); |
| 414 | DateFormat format = DateFormat.getInstanceForSkeleton(mDatePattern, l); |
| 415 | format.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE); |
| 416 | mDateFormat = format; |
| 417 | } |
| 418 | mCurrentTime.setTime(System.currentTimeMillis()); |
| 419 | return mDateFormat.format(mCurrentTime); |
| 420 | } |
| 421 | |
| 422 | @VisibleForTesting |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 423 | void cleanDateFormatLocked() { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 424 | mDateFormat = null; |
| 425 | } |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 426 | |
| 427 | @Override |
| 428 | public void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 429 | synchronized (this) { |
| 430 | mNextAlarmInfo = nextAlarm; |
| 431 | mAlarmManager.cancel(mUpdateNextAlarm); |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 432 | |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 433 | long triggerAt = mNextAlarmInfo == null ? -1 : mNextAlarmInfo.getTriggerTime() |
| 434 | - TimeUnit.HOURS.toMillis(ALARM_VISIBILITY_HOURS); |
| 435 | if (triggerAt > 0) { |
| 436 | mAlarmManager.setExact(AlarmManager.RTC, triggerAt, "lock_screen_next_alarm", |
| 437 | mUpdateNextAlarm, mHandler); |
| 438 | } |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 439 | } |
| 440 | updateNextAlarm(); |
Lucas Dupin | 9cc40de | 2017-12-19 16:09:32 -0800 | [diff] [blame] | 441 | } |
Lucas Dupin | 7e171e2 | 2018-12-20 11:29:35 -0800 | [diff] [blame] | 442 | |
| 443 | @VisibleForTesting |
| 444 | protected KeyguardUpdateMonitor getKeyguardUpdateMonitor() { |
| 445 | return KeyguardUpdateMonitor.getInstance(getContext()); |
| 446 | } |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 447 | |
Lucas Dupin | 76527ee | 2019-02-20 10:46:23 -0800 | [diff] [blame] | 448 | /** |
| 449 | * Called whenever new media metadata is available. |
| 450 | * @param metadata New metadata. |
| 451 | */ |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 452 | @Override |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 453 | public void onMetadataOrStateChanged(MediaMetadata metadata, @PlaybackState.State int state) { |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 454 | synchronized (this) { |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 455 | boolean nextVisible = !mMediaInvisibleStates.contains(state); |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 456 | mHandler.removeCallbacksAndMessages(mMediaToken); |
| 457 | if (mMediaIsVisible && !nextVisible) { |
| 458 | // We need to delay this event for a few millis when stopping to avoid jank in the |
| 459 | // animation. The media app might not send its update when buffering, and the slice |
| 460 | // would end up without a header for 0.5 second. |
| 461 | mMediaWakeLock.setAcquired(true); |
| 462 | mHandler.postDelayed(() -> { |
| 463 | updateMediaStateLocked(metadata, state); |
| 464 | mMediaWakeLock.setAcquired(false); |
| 465 | }, mMediaToken, 2000); |
| 466 | } else { |
| 467 | mMediaWakeLock.setAcquired(false); |
| 468 | updateMediaStateLocked(metadata, state); |
Lucas Dupin | 6b40d5e | 2019-05-15 19:47:11 -0700 | [diff] [blame] | 469 | } |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 470 | } |
Lucas Dupin | 9027bda | 2019-05-28 14:34:47 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | private void updateMediaStateLocked(MediaMetadata metadata, @PlaybackState.State int state) { |
| 474 | boolean nextVisible = !mMediaInvisibleStates.contains(state); |
| 475 | CharSequence title = null; |
| 476 | if (metadata != null) { |
| 477 | title = metadata.getText(MediaMetadata.METADATA_KEY_TITLE); |
| 478 | if (TextUtils.isEmpty(title)) { |
| 479 | title = getContext().getResources().getString(R.string.music_controls_no_title); |
| 480 | } |
| 481 | } |
| 482 | CharSequence artist = metadata == null ? null : metadata.getText( |
| 483 | MediaMetadata.METADATA_KEY_ARTIST); |
| 484 | |
| 485 | if (nextVisible == mMediaIsVisible && TextUtils.equals(title, mMediaTitle) |
| 486 | && TextUtils.equals(artist, mMediaArtist)) { |
| 487 | return; |
| 488 | } |
| 489 | mMediaTitle = title; |
| 490 | mMediaArtist = artist; |
| 491 | mMediaIsVisible = nextVisible; |
Lucas Dupin | 76527ee | 2019-02-20 10:46:23 -0800 | [diff] [blame] | 492 | notifyChange(); |
Lucas Dupin | 6a03a9f | 2018-12-20 17:13:52 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | protected void notifyChange() { |
| 496 | mContentResolver.notifyChange(mSliceUri, null /* observer */); |
| 497 | } |
Lucas Dupin | 3d560e4 | 2019-01-07 13:52:44 -0800 | [diff] [blame] | 498 | |
| 499 | @Override |
| 500 | public void onDozingChanged(boolean isDozing) { |
| 501 | final boolean notify; |
| 502 | synchronized (this) { |
| 503 | boolean neededMedia = needsMediaLocked(); |
| 504 | mDozing = isDozing; |
| 505 | notify = neededMedia != needsMediaLocked(); |
| 506 | } |
| 507 | if (notify) { |
| 508 | notifyChange(); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void onStateChanged(int newState) { |
| 514 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 515 | } |