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