Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.assist; |
| 18 | |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 22 | import android.os.SystemClock; |
| 23 | import android.provider.DeviceConfig; |
| 24 | import android.provider.Settings; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 25 | |
| 26 | import androidx.annotation.Nullable; |
| 27 | |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 28 | import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 29 | import com.android.systemui.Dependency; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 30 | import com.android.systemui.assist.AssistHandleBehaviorController.BehaviorController; |
| 31 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
| 32 | import com.android.systemui.recents.OverviewProxyService; |
| 33 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 34 | import com.android.systemui.shared.system.QuickStepContract; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 35 | import com.android.systemui.shared.system.TaskStackChangeListener; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.StatusBarState; |
| 37 | |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 38 | import java.util.concurrent.TimeUnit; |
| 39 | |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 40 | /** |
| 41 | * Assistant handle behavior that hides the handles when the phone is dozing or in immersive mode, |
| 42 | * shows the handles when on lockscreen, and shows the handles temporarily when changing tasks or |
| 43 | * entering overview. |
| 44 | */ |
| 45 | final class AssistHandleReminderExpBehavior implements BehaviorController { |
| 46 | |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 47 | private static final String LEARNING_TIME_ELAPSED_KEY = "reminder_exp_learning_time_elapsed"; |
| 48 | private static final String LEARNING_EVENT_COUNT_KEY = "reminder_exp_learning_event_count"; |
| 49 | private static final long DEFAULT_LEARNING_TIME_MS = TimeUnit.DAYS.toMillis(3); |
| 50 | private static final int DEFAULT_LEARNING_COUNT = 3; |
| 51 | |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 52 | private final StatusBarStateController.StateListener mStatusBarStateListener = |
| 53 | new StatusBarStateController.StateListener() { |
| 54 | @Override |
| 55 | public void onStateChanged(int newState) { |
| 56 | handleStatusBarStateChanged(newState); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void onDozingChanged(boolean isDozing) { |
| 61 | handleDozingChanged(isDozing); |
| 62 | } |
| 63 | }; |
| 64 | private final TaskStackChangeListener mTaskStackChangeListener = |
| 65 | new TaskStackChangeListener() { |
| 66 | @Override |
| 67 | public void onTaskMovedToFront(int taskId) { |
| 68 | handleTaskStackTopChanged(taskId); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onTaskCreated(int taskId, ComponentName componentName) { |
| 73 | handleTaskStackTopChanged(taskId); |
| 74 | } |
| 75 | }; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 76 | private final OverviewProxyService.OverviewProxyListener mOverviewProxyListener = |
| 77 | new OverviewProxyService.OverviewProxyListener() { |
| 78 | @Override |
| 79 | public void onOverviewShown(boolean fromHome) { |
| 80 | handleOverviewShown(); |
| 81 | } |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public void onSystemUiStateChanged(int sysuiStateFlags) { |
| 85 | handleSystemUiStateChanged(sysuiStateFlags); |
| 86 | } |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 89 | private final StatusBarStateController mStatusBarStateController; |
| 90 | private final ActivityManagerWrapper mActivityManagerWrapper; |
| 91 | private final OverviewProxyService mOverviewProxyService; |
| 92 | |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 93 | private boolean mOnLockscreen; |
| 94 | private boolean mIsDozing; |
| 95 | private int mRunningTaskId; |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 96 | private boolean mIsNavBarHidden; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 97 | |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 98 | /** Whether user has learned the gesture. */ |
| 99 | private boolean mIsLearned; |
| 100 | private long mLastLearningTimestamp; |
| 101 | /** Uptime while in this behavior. */ |
| 102 | private long mLearningTimeElapsed; |
| 103 | /** Number of successful Assistant invocations while in this behavior. */ |
| 104 | private int mLearningCount; |
| 105 | |
| 106 | @Nullable private Context mContext; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 107 | @Nullable private AssistHandleCallbacks mAssistHandleCallbacks; |
| 108 | |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 109 | AssistHandleReminderExpBehavior() { |
| 110 | mStatusBarStateController = Dependency.get(StatusBarStateController.class); |
| 111 | mActivityManagerWrapper = ActivityManagerWrapper.getInstance(); |
| 112 | mOverviewProxyService = Dependency.get(OverviewProxyService.class); |
| 113 | } |
| 114 | |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 115 | @Override |
| 116 | public void onModeActivated(Context context, AssistHandleCallbacks callbacks) { |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 117 | mContext = context; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 118 | mAssistHandleCallbacks = callbacks; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 119 | mOnLockscreen = onLockscreen(mStatusBarStateController.getState()); |
| 120 | mIsDozing = mStatusBarStateController.isDozing(); |
| 121 | mStatusBarStateController.addCallback(mStatusBarStateListener); |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 122 | ActivityManager.RunningTaskInfo runningTaskInfo = mActivityManagerWrapper.getRunningTask(); |
| 123 | mRunningTaskId = runningTaskInfo == null ? 0 : runningTaskInfo.taskId; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 124 | mActivityManagerWrapper.registerTaskStackListener(mTaskStackChangeListener); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 125 | mOverviewProxyService.addCallback(mOverviewProxyListener); |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 126 | |
| 127 | mLearningTimeElapsed = Settings.Secure.getLong( |
| 128 | context.getContentResolver(), LEARNING_TIME_ELAPSED_KEY, /* default = */ 0); |
| 129 | mLearningCount = Settings.Secure.getInt( |
| 130 | context.getContentResolver(), LEARNING_EVENT_COUNT_KEY, /* default = */ 0); |
| 131 | mLastLearningTimestamp = SystemClock.uptimeMillis(); |
| 132 | |
| 133 | callbackForCurrentState(/* justUnlocked = */ false); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onModeDeactivated() { |
| 138 | mAssistHandleCallbacks = null; |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 139 | if (mContext != null) { |
| 140 | Settings.Secure.putLong( |
| 141 | mContext.getContentResolver(), LEARNING_TIME_ELAPSED_KEY, mLearningTimeElapsed); |
| 142 | Settings.Secure.putInt( |
| 143 | mContext.getContentResolver(), LEARNING_EVENT_COUNT_KEY, mLearningCount); |
| 144 | mContext = null; |
| 145 | } |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 146 | mStatusBarStateController.removeCallback(mStatusBarStateListener); |
| 147 | mActivityManagerWrapper.unregisterTaskStackListener(mTaskStackChangeListener); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 148 | mOverviewProxyService.removeCallback(mOverviewProxyListener); |
| 149 | } |
| 150 | |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 151 | @Override |
| 152 | public void onAssistantGesturePerformed() { |
| 153 | if (mContext == null) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | Settings.Secure.putLong( |
| 158 | mContext.getContentResolver(), LEARNING_EVENT_COUNT_KEY, ++mLearningCount); |
| 159 | } |
| 160 | |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 161 | private static boolean isNavBarHidden(int sysuiStateFlags) { |
| 162 | return (sysuiStateFlags & QuickStepContract.SYSUI_STATE_NAV_BAR_HIDDEN) != 0; |
| 163 | } |
| 164 | |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 165 | private void handleStatusBarStateChanged(int newState) { |
| 166 | boolean onLockscreen = onLockscreen(newState); |
| 167 | if (mOnLockscreen == onLockscreen) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | mOnLockscreen = onLockscreen; |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 172 | callbackForCurrentState(!onLockscreen); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private void handleDozingChanged(boolean isDozing) { |
| 176 | if (mIsDozing == isDozing) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | mIsDozing = isDozing; |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 181 | callbackForCurrentState(/* justUnlocked = */ false); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | private void handleTaskStackTopChanged(int taskId) { |
| 185 | if (mRunningTaskId == taskId) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | mRunningTaskId = taskId; |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 190 | callbackForCurrentState(/* justUnlocked = */ false); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 193 | private void handleSystemUiStateChanged(int sysuiStateFlags) { |
| 194 | boolean isNavBarHidden = isNavBarHidden(sysuiStateFlags); |
| 195 | if (mIsNavBarHidden == isNavBarHidden) { |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 199 | mIsNavBarHidden = isNavBarHidden; |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 200 | callbackForCurrentState(/* justUnlocked = */ false); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private void handleOverviewShown() { |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 204 | callbackForCurrentState(/* justUnlocked = */ false); |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | private boolean onLockscreen(int statusBarState) { |
| 208 | return statusBarState == StatusBarState.KEYGUARD |
| 209 | || statusBarState == StatusBarState.SHADE_LOCKED; |
| 210 | } |
| 211 | |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 212 | private void callbackForCurrentState(boolean justUnlocked) { |
| 213 | updateLearningStatus(); |
| 214 | |
| 215 | if (mIsLearned) { |
| 216 | callbackForLearnedState(justUnlocked); |
| 217 | } else { |
| 218 | callbackForUnlearnedState(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private void callbackForLearnedState(boolean justUnlocked) { |
| 223 | if (mAssistHandleCallbacks == null) { |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | if (mIsDozing || mIsNavBarHidden || mOnLockscreen) { |
| 228 | mAssistHandleCallbacks.hide(); |
| 229 | } else if (justUnlocked) { |
| 230 | mAssistHandleCallbacks.showAndGo(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private void callbackForUnlearnedState() { |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 235 | if (mAssistHandleCallbacks == null) { |
| 236 | return; |
| 237 | } |
| 238 | |
Govinda Wasserman | 2c11340 | 2019-05-24 14:11:24 -0400 | [diff] [blame] | 239 | if (mIsDozing || mIsNavBarHidden) { |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 240 | mAssistHandleCallbacks.hide(); |
| 241 | } else if (mOnLockscreen) { |
| 242 | mAssistHandleCallbacks.showAndStay(); |
| 243 | } else { |
| 244 | mAssistHandleCallbacks.showAndGo(); |
| 245 | } |
| 246 | } |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 247 | |
| 248 | private void updateLearningStatus() { |
| 249 | if (mContext == null) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | long currentTimestamp = SystemClock.uptimeMillis(); |
| 254 | mLearningTimeElapsed += currentTimestamp - mLastLearningTimestamp; |
| 255 | mLastLearningTimestamp = currentTimestamp; |
| 256 | Settings.Secure.putLong( |
| 257 | mContext.getContentResolver(), LEARNING_TIME_ELAPSED_KEY, mLearningTimeElapsed); |
| 258 | |
| 259 | mIsLearned = |
| 260 | mLearningCount >= getLearningCount() || mLearningTimeElapsed >= getLearningTimeMs(); |
| 261 | } |
| 262 | |
| 263 | private long getLearningTimeMs() { |
| 264 | return DeviceConfig.getLong( |
| 265 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 266 | SystemUiDeviceConfigFlags.ASSIST_HANDLES_LEARN_TIME_MS, |
| 267 | DEFAULT_LEARNING_TIME_MS); |
| 268 | } |
| 269 | |
| 270 | private int getLearningCount() { |
| 271 | return DeviceConfig.getInt( |
| 272 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 273 | SystemUiDeviceConfigFlags.ASSIST_HANDLES_LEARN_COUNT, |
| 274 | DEFAULT_LEARNING_COUNT); |
| 275 | } |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 276 | } |