Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.shared.system; |
| 18 | |
Gustav Sennton | 5a4fc21 | 2019-02-28 16:12:27 +0000 | [diff] [blame] | 19 | import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED; |
Winson Chung | b68213b | 2018-02-01 15:38:02 -0800 | [diff] [blame] | 20 | import static android.app.ActivityManager.LOCK_TASK_MODE_NONE; |
Matthew Ng | 6ff33b7 | 2018-02-27 13:47:38 -0800 | [diff] [blame] | 21 | import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 22 | import static android.app.ActivityManager.RECENT_IGNORE_UNAVAILABLE; |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 23 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 24 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
| 25 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY; |
| 26 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 27 | |
| 28 | import android.annotation.NonNull; |
Riddle Hsu | 440f88b | 2019-11-06 22:17:35 +0800 | [diff] [blame] | 29 | import android.app.Activity; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 30 | import android.app.ActivityManager; |
| 31 | import android.app.ActivityManager.RecentTaskInfo; |
Sunny Goyal | f5b03e0 | 2019-07-12 10:38:37 -0700 | [diff] [blame] | 32 | import android.app.ActivityManager.RunningTaskInfo; |
Tracy Zhou | 8089ffa | 2019-07-30 17:30:43 -0700 | [diff] [blame] | 33 | import android.app.ActivityManager.TaskSnapshot; |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 34 | import android.app.ActivityOptions; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 35 | import android.app.ActivityTaskManager; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 36 | import android.app.AppGlobals; |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 37 | import android.app.IAssistDataReceiver; |
Sunny Goyal | f5b03e0 | 2019-07-12 10:38:37 -0700 | [diff] [blame] | 38 | import android.app.WindowConfiguration; |
Winson Chung | b68213b | 2018-02-01 15:38:02 -0800 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 40 | import android.content.Context; |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 41 | import android.content.Intent; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 42 | import android.content.pm.ActivityInfo; |
| 43 | import android.content.pm.ApplicationInfo; |
| 44 | import android.content.pm.PackageManager; |
| 45 | import android.content.pm.UserInfo; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 46 | import android.graphics.Bitmap; |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 47 | import android.graphics.Rect; |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 48 | import android.os.Bundle; |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 49 | import android.os.Handler; |
Sunny Goyal | 5067909 | 2018-04-04 14:41:07 -0700 | [diff] [blame] | 50 | import android.os.IBinder; |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 51 | import android.os.Looper; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 52 | import android.os.RemoteException; |
Sunny Goyal | 5067909 | 2018-04-04 14:41:07 -0700 | [diff] [blame] | 53 | import android.os.ServiceManager; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 54 | import android.os.UserHandle; |
Winson Chung | b68213b | 2018-02-01 15:38:02 -0800 | [diff] [blame] | 55 | import android.provider.Settings; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 56 | import android.util.Log; |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 57 | import android.view.IRecentsAnimationController; |
| 58 | import android.view.IRecentsAnimationRunner; |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 59 | import android.view.RemoteAnimationTarget; |
Sunny Goyal | 5067909 | 2018-04-04 14:41:07 -0700 | [diff] [blame] | 60 | |
| 61 | import com.android.internal.app.IVoiceInteractionManagerService; |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 62 | import com.android.systemui.shared.recents.model.Task; |
| 63 | import com.android.systemui.shared.recents.model.Task.TaskKey; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 64 | import com.android.systemui.shared.recents.model.ThumbnailData; |
| 65 | |
| 66 | import java.util.ArrayList; |
| 67 | import java.util.List; |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 68 | import java.util.concurrent.Future; |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 69 | import java.util.function.Consumer; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 70 | |
| 71 | public class ActivityManagerWrapper { |
| 72 | |
| 73 | private static final String TAG = "ActivityManagerWrapper"; |
| 74 | |
| 75 | private static final ActivityManagerWrapper sInstance = new ActivityManagerWrapper(); |
| 76 | |
Winson Chung | e3dd59c | 2018-04-02 15:28:40 -0700 | [diff] [blame] | 77 | // Should match the values in PhoneWindowManager |
| 78 | public static final String CLOSE_SYSTEM_WINDOWS_REASON_RECENTS = "recentapps"; |
Andy Wickham | 709825d | 2019-10-16 15:01:46 -0700 | [diff] [blame] | 79 | public static final String CLOSE_SYSTEM_WINDOWS_REASON_HOME_KEY = "homekey"; |
Winson Chung | e3dd59c | 2018-04-02 15:28:40 -0700 | [diff] [blame] | 80 | |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 81 | private final PackageManager mPackageManager; |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 82 | private final BackgroundExecutor mBackgroundExecutor; |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 83 | private final TaskStackChangeListeners mTaskStackChangeListeners; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 84 | |
| 85 | private ActivityManagerWrapper() { |
| 86 | final Context context = AppGlobals.getInitialApplication(); |
| 87 | mPackageManager = context.getPackageManager(); |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 88 | mBackgroundExecutor = BackgroundExecutor.get(); |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 89 | mTaskStackChangeListeners = new TaskStackChangeListeners(Looper.getMainLooper()); |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | public static ActivityManagerWrapper getInstance() { |
| 93 | return sInstance; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return the current user's id. |
| 98 | */ |
| 99 | public int getCurrentUserId() { |
| 100 | UserInfo ui; |
| 101 | try { |
| 102 | ui = ActivityManager.getService().getCurrentUser(); |
| 103 | return ui != null ? ui.id : 0; |
| 104 | } catch (RemoteException e) { |
| 105 | throw e.rethrowFromSystemServer(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 110 | * @return the top running task (can be {@code null}). |
| 111 | */ |
| 112 | public ActivityManager.RunningTaskInfo getRunningTask() { |
Winson Chung | e5ab017 | 2020-03-21 23:00:24 -0700 | [diff] [blame] | 113 | return getRunningTask(false /* filterVisibleRecents */); |
Tony Wickham | fb63fe8 | 2018-01-16 12:14:06 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Winson Chung | e5ab017 | 2020-03-21 23:00:24 -0700 | [diff] [blame] | 116 | /** |
| 117 | * @return the top running task filtering only for tasks that can be visible in the recent tasks |
| 118 | * list (can be {@code null}). |
| 119 | */ |
| 120 | public ActivityManager.RunningTaskInfo getRunningTask(boolean filterOnlyVisibleRecents) { |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 121 | // Note: The set of running tasks from the system is ordered by recency |
| 122 | try { |
| 123 | List<ActivityManager.RunningTaskInfo> tasks = |
Winson Chung | e5ab017 | 2020-03-21 23:00:24 -0700 | [diff] [blame] | 124 | ActivityTaskManager.getService().getFilteredTasks(1, filterOnlyVisibleRecents); |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 125 | if (tasks.isEmpty()) { |
| 126 | return null; |
| 127 | } |
| 128 | return tasks.get(0); |
| 129 | } catch (RemoteException e) { |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 135 | * @return a list of the recents tasks. |
| 136 | */ |
| 137 | public List<RecentTaskInfo> getRecentTasks(int numTasks, int userId) { |
| 138 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 139 | return ActivityTaskManager.getService().getRecentTasks(numTasks, |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 140 | RECENT_IGNORE_UNAVAILABLE, userId).getList(); |
| 141 | } catch (RemoteException e) { |
| 142 | Log.e(TAG, "Failed to get recent tasks", e); |
| 143 | return new ArrayList<>(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @return the task snapshot for the given {@param taskId}. |
| 149 | */ |
Peter Kalauskas | 4dc0460 | 2020-02-12 18:49:03 -0800 | [diff] [blame] | 150 | public @NonNull ThumbnailData getTaskThumbnail(int taskId, boolean isLowResolution) { |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 151 | ActivityManager.TaskSnapshot snapshot = null; |
| 152 | try { |
Peter Kalauskas | 4dc0460 | 2020-02-12 18:49:03 -0800 | [diff] [blame] | 153 | snapshot = ActivityTaskManager.getService().getTaskSnapshot(taskId, isLowResolution); |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 154 | } catch (RemoteException e) { |
| 155 | Log.w(TAG, "Failed to retrieve task snapshot", e); |
| 156 | } |
| 157 | if (snapshot != null) { |
| 158 | return new ThumbnailData(snapshot); |
| 159 | } else { |
| 160 | return new ThumbnailData(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
Riddle Hsu | 440f88b | 2019-11-06 22:17:35 +0800 | [diff] [blame] | 165 | * Removes the outdated snapshot of home task. |
| 166 | */ |
| 167 | public void invalidateHomeTaskSnapshot(final Activity homeActivity) { |
| 168 | mBackgroundExecutor.submit(new Runnable() { |
| 169 | @Override |
| 170 | public void run() { |
| 171 | try { |
| 172 | ActivityTaskManager.getService().invalidateHomeTaskSnapshot( |
| 173 | homeActivity.getActivityToken()); |
| 174 | } catch (RemoteException e) { |
| 175 | Log.w(TAG, "Failed to invalidate home snapshot", e); |
| 176 | } |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | /** |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 182 | * @return the activity label, badging if necessary. |
| 183 | */ |
| 184 | public String getBadgedActivityLabel(ActivityInfo info, int userId) { |
| 185 | return getBadgedLabel(info.loadLabel(mPackageManager).toString(), userId); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @return the application label, badging if necessary. |
| 190 | */ |
| 191 | public String getBadgedApplicationLabel(ApplicationInfo appInfo, int userId) { |
| 192 | return getBadgedLabel(appInfo.loadLabel(mPackageManager).toString(), userId); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @return the content description for a given task, badging it if necessary. The content |
| 197 | * description joins the app and activity labels. |
| 198 | */ |
| 199 | public String getBadgedContentDescription(ActivityInfo info, int userId, |
| 200 | ActivityManager.TaskDescription td) { |
| 201 | String activityLabel; |
| 202 | if (td != null && td.getLabel() != null) { |
| 203 | activityLabel = td.getLabel(); |
| 204 | } else { |
| 205 | activityLabel = info.loadLabel(mPackageManager).toString(); |
| 206 | } |
| 207 | String applicationLabel = info.applicationInfo.loadLabel(mPackageManager).toString(); |
| 208 | String badgedApplicationLabel = getBadgedLabel(applicationLabel, userId); |
| 209 | return applicationLabel.equals(activityLabel) |
| 210 | ? badgedApplicationLabel |
| 211 | : badgedApplicationLabel + " " + activityLabel; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @return the given label for a user, badging if necessary. |
| 216 | */ |
| 217 | private String getBadgedLabel(String label, int userId) { |
| 218 | if (userId != UserHandle.myUserId()) { |
| 219 | label = mPackageManager.getUserBadgedLabel(label, new UserHandle(userId)).toString(); |
| 220 | } |
| 221 | return label; |
| 222 | } |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 223 | |
| 224 | /** |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 225 | * Starts the recents activity. The caller should manage the thread on which this is called. |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 226 | */ |
Sunny Goyal | bba378e | 2018-08-22 12:50:26 -0700 | [diff] [blame] | 227 | public void startRecentsActivity(Intent intent, final AssistDataReceiver assistDataReceiver, |
| 228 | final RecentsAnimationListener animationHandler, final Consumer<Boolean> resultCallback, |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 229 | Handler resultCallbackHandler) { |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 230 | try { |
| 231 | IAssistDataReceiver receiver = null; |
| 232 | if (assistDataReceiver != null) { |
| 233 | receiver = new IAssistDataReceiver.Stub() { |
| 234 | public void onHandleAssistData(Bundle resultData) { |
| 235 | assistDataReceiver.onHandleAssistData(resultData); |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 236 | } |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 237 | public void onHandleAssistScreenshot(Bitmap screenshot) { |
| 238 | assistDataReceiver.onHandleAssistScreenshot(screenshot); |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 239 | } |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 240 | }; |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 241 | } |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 242 | IRecentsAnimationRunner runner = null; |
| 243 | if (animationHandler != null) { |
| 244 | runner = new IRecentsAnimationRunner.Stub() { |
lumark | 5428446 | 2019-03-05 20:44:27 +0800 | [diff] [blame] | 245 | @Override |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 246 | public void onAnimationStart(IRecentsAnimationController controller, |
Winson Chung | d585219 | 2019-09-06 17:20:28 -0700 | [diff] [blame] | 247 | RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, |
| 248 | Rect homeContentInsets, Rect minimizedHomeBounds) { |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 249 | final RecentsAnimationControllerCompat controllerCompat = |
| 250 | new RecentsAnimationControllerCompat(controller); |
| 251 | final RemoteAnimationTargetCompat[] appsCompat = |
| 252 | RemoteAnimationTargetCompat.wrap(apps); |
Winson Chung | d585219 | 2019-09-06 17:20:28 -0700 | [diff] [blame] | 253 | final RemoteAnimationTargetCompat[] wallpapersCompat = |
| 254 | RemoteAnimationTargetCompat.wrap(wallpapers); |
Winson Chung | 584d652 | 2018-02-07 23:57:38 +0000 | [diff] [blame] | 255 | animationHandler.onAnimationStart(controllerCompat, appsCompat, |
Winson Chung | d585219 | 2019-09-06 17:20:28 -0700 | [diff] [blame] | 256 | wallpapersCompat, homeContentInsets, minimizedHomeBounds); |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
lumark | 5428446 | 2019-03-05 20:44:27 +0800 | [diff] [blame] | 259 | @Override |
Tracy Zhou | 8089ffa | 2019-07-30 17:30:43 -0700 | [diff] [blame] | 260 | public void onAnimationCanceled(TaskSnapshot taskSnapshot) { |
Tracy Zhou | f36321fc | 2019-07-31 12:16:40 -0700 | [diff] [blame] | 261 | animationHandler.onAnimationCanceled( |
Tracy Zhou | 8089ffa | 2019-07-30 17:30:43 -0700 | [diff] [blame] | 262 | taskSnapshot != null ? new ThumbnailData(taskSnapshot) : null); |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 263 | } |
lumark | 04bceb9 | 2020-03-07 00:03:33 +0800 | [diff] [blame^] | 264 | |
| 265 | @Override |
| 266 | public void onTaskAppeared(RemoteAnimationTarget app) { |
| 267 | animationHandler.onTaskAppeared(new RemoteAnimationTargetCompat(app)); |
| 268 | } |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 269 | }; |
| 270 | } |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 271 | ActivityTaskManager.getService().startRecentsActivity(intent, receiver, runner); |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 272 | if (resultCallback != null) { |
| 273 | resultCallbackHandler.post(new Runnable() { |
| 274 | @Override |
| 275 | public void run() { |
| 276 | resultCallback.accept(true); |
| 277 | } |
| 278 | }); |
| 279 | } |
| 280 | } catch (Exception e) { |
| 281 | if (resultCallback != null) { |
| 282 | resultCallbackHandler.post(new Runnable() { |
| 283 | @Override |
| 284 | public void run() { |
| 285 | resultCallback.accept(false); |
| 286 | } |
| 287 | }); |
| 288 | } |
| 289 | } |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 293 | * Cancels the remote recents animation started from {@link #startRecentsActivity}. |
| 294 | */ |
Winson Chung | 6a38fca | 2018-03-28 17:57:09 -0700 | [diff] [blame] | 295 | public void cancelRecentsAnimation(boolean restoreHomeStackPosition) { |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 296 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 297 | ActivityTaskManager.getService().cancelRecentsAnimation(restoreHomeStackPosition); |
Winson Chung | ddf6297 | 2018-02-12 11:10:04 -0800 | [diff] [blame] | 298 | } catch (RemoteException e) { |
| 299 | Log.e(TAG, "Failed to cancel recents animation", e); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 304 | * Starts a task from Recents. |
| 305 | * |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 306 | * @see {@link #startActivityFromRecentsAsync(TaskKey, ActivityOptions, int, int, Consumer, Handler)} |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 307 | */ |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 308 | public void startActivityFromRecentsAsync(Task.TaskKey taskKey, ActivityOptions options, |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 309 | Consumer<Boolean> resultCallback, Handler resultCallbackHandler) { |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 310 | startActivityFromRecentsAsync(taskKey, options, WINDOWING_MODE_UNDEFINED, |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 311 | ACTIVITY_TYPE_UNDEFINED, resultCallback, resultCallbackHandler); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Starts a task from Recents. |
| 316 | * |
| 317 | * @param resultCallback The result success callback |
| 318 | * @param resultCallbackHandler The handler to receive the result callback |
| 319 | */ |
Sunny Goyal | bba378e | 2018-08-22 12:50:26 -0700 | [diff] [blame] | 320 | public void startActivityFromRecentsAsync(final Task.TaskKey taskKey, ActivityOptions options, |
| 321 | int windowingMode, int activityType, final Consumer<Boolean> resultCallback, |
| 322 | final Handler resultCallbackHandler) { |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 323 | if (taskKey.windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) { |
| 324 | // We show non-visible docked tasks in Recents, but we always want to launch |
| 325 | // them in the fullscreen stack. |
| 326 | if (options == null) { |
| 327 | options = ActivityOptions.makeBasic(); |
| 328 | } |
| 329 | options.setLaunchWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY); |
| 330 | } else if (windowingMode != WINDOWING_MODE_UNDEFINED |
| 331 | || activityType != ACTIVITY_TYPE_UNDEFINED) { |
| 332 | if (options == null) { |
| 333 | options = ActivityOptions.makeBasic(); |
| 334 | } |
| 335 | options.setLaunchWindowingMode(windowingMode); |
| 336 | options.setLaunchActivityType(activityType); |
| 337 | } |
| 338 | final ActivityOptions finalOptions = options; |
| 339 | |
Jorim Jaggi | d6f0a5c | 2019-07-08 16:53:08 +0200 | [diff] [blame] | 340 | |
| 341 | boolean result = false; |
| 342 | try { |
| 343 | result = startActivityFromRecents(taskKey.id, finalOptions); |
| 344 | } catch (Exception e) { |
| 345 | // Fall through |
| 346 | } |
| 347 | final boolean finalResult = result; |
| 348 | if (resultCallback != null) { |
| 349 | resultCallbackHandler.post(new Runnable() { |
| 350 | @Override |
| 351 | public void run() { |
| 352 | resultCallback.accept(finalResult); |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 353 | } |
Jorim Jaggi | d6f0a5c | 2019-07-08 16:53:08 +0200 | [diff] [blame] | 354 | }); |
| 355 | } |
Winson Chung | b05b398 | 2017-11-01 18:02:43 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 359 | * Starts a task from Recents synchronously. |
| 360 | */ |
| 361 | public boolean startActivityFromRecents(int taskId, ActivityOptions options) { |
| 362 | try { |
| 363 | Bundle optsBundle = options == null ? null : options.toBundle(); |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 364 | ActivityTaskManager.getService().startActivityFromRecents(taskId, optsBundle); |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 365 | return true; |
| 366 | } catch (Exception e) { |
| 367 | return false; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 372 | * Moves an already resumed task to the side of the screen to initiate split screen. |
| 373 | */ |
| 374 | public boolean setTaskWindowingModeSplitScreenPrimary(int taskId, int createMode, |
| 375 | Rect initialBounds) { |
| 376 | try { |
| 377 | return ActivityTaskManager.getService().setTaskWindowingModeSplitScreenPrimary(taskId, |
Evan Rosky | 73a7fe9 | 2019-11-18 18:28:01 -0800 | [diff] [blame] | 378 | true /* onTop */); |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 379 | } catch (RemoteException e) { |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 385 | * Registers a task stack listener with the system. |
| 386 | * This should be called on the main thread. |
| 387 | */ |
| 388 | public void registerTaskStackListener(TaskStackChangeListener listener) { |
| 389 | synchronized (mTaskStackChangeListeners) { |
| 390 | mTaskStackChangeListeners.addListener(ActivityManager.getService(), listener); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Unregisters a task stack listener with the system. |
| 396 | * This should be called on the main thread. |
| 397 | */ |
| 398 | public void unregisterTaskStackListener(TaskStackChangeListener listener) { |
| 399 | synchronized (mTaskStackChangeListeners) { |
| 400 | mTaskStackChangeListeners.removeListener(listener); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /** |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 405 | * Requests that the system close any open system windows (including other SystemUI). |
| 406 | */ |
Matt Pietal | 777c82d | 2019-06-11 15:47:10 -0400 | [diff] [blame] | 407 | public Future<?> closeSystemWindows(final String reason) { |
| 408 | return mBackgroundExecutor.submit(new Runnable() { |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 409 | @Override |
| 410 | public void run() { |
| 411 | try { |
| 412 | ActivityManager.getService().closeSystemDialogs(reason); |
| 413 | } catch (RemoteException e) { |
| 414 | Log.w(TAG, "Failed to close system windows", e); |
| 415 | } |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 416 | } |
| 417 | }); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Removes a task by id. |
| 422 | */ |
Sunny Goyal | bba378e | 2018-08-22 12:50:26 -0700 | [diff] [blame] | 423 | public void removeTask(final int taskId) { |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 424 | mBackgroundExecutor.submit(new Runnable() { |
| 425 | @Override |
| 426 | public void run() { |
| 427 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 428 | ActivityTaskManager.getService().removeTask(taskId); |
Winson Chung | 1c5aeff | 2017-11-08 17:32:28 -0800 | [diff] [blame] | 429 | } catch (RemoteException e) { |
| 430 | Log.w(TAG, "Failed to remove task=" + taskId, e); |
| 431 | } |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 432 | } |
| 433 | }); |
| 434 | } |
| 435 | |
| 436 | /** |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 437 | * Removes all the recent tasks. |
| 438 | */ |
| 439 | public void removeAllRecentTasks() { |
| 440 | mBackgroundExecutor.submit(new Runnable() { |
| 441 | @Override |
| 442 | public void run() { |
| 443 | try { |
| 444 | ActivityTaskManager.getService().removeAllVisibleRecentTasks(); |
| 445 | } catch (RemoteException e) { |
| 446 | Log.w(TAG, "Failed to remove all tasks", e); |
| 447 | } |
| 448 | } |
| 449 | }); |
| 450 | } |
| 451 | |
| 452 | /** |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 453 | * Cancels the current window transtion to/from Recents for the given task id. |
| 454 | */ |
| 455 | public void cancelWindowTransition(int taskId) { |
| 456 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 457 | ActivityTaskManager.getService().cancelTaskWindowTransition(taskId); |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 458 | } catch (RemoteException e) { |
| 459 | Log.w(TAG, "Failed to cancel window transition for task=" + taskId, e); |
| 460 | } |
| 461 | } |
Winson Chung | b68213b | 2018-02-01 15:38:02 -0800 | [diff] [blame] | 462 | |
| 463 | /** |
Matthew Ng | 6ff33b7 | 2018-02-27 13:47:38 -0800 | [diff] [blame] | 464 | * @return whether screen pinning is active. |
| 465 | */ |
| 466 | public boolean isScreenPinningActive() { |
| 467 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 468 | return ActivityTaskManager.getService().getLockTaskModeState() == LOCK_TASK_MODE_PINNED; |
Matthew Ng | 6ff33b7 | 2018-02-27 13:47:38 -0800 | [diff] [blame] | 469 | } catch (RemoteException e) { |
| 470 | return false; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * @return whether screen pinning is enabled. |
| 476 | */ |
| 477 | public boolean isScreenPinningEnabled() { |
| 478 | final ContentResolver cr = AppGlobals.getInitialApplication().getContentResolver(); |
| 479 | return Settings.System.getInt(cr, Settings.System.LOCK_TO_APP_ENABLED, 0) != 0; |
| 480 | } |
| 481 | |
| 482 | /** |
Winson Chung | b68213b | 2018-02-01 15:38:02 -0800 | [diff] [blame] | 483 | * @return whether there is currently a locked task (ie. in screen pinning). |
| 484 | */ |
| 485 | public boolean isLockToAppActive() { |
| 486 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 487 | return ActivityTaskManager.getService().getLockTaskModeState() != LOCK_TASK_MODE_NONE; |
Winson Chung | b68213b | 2018-02-01 15:38:02 -0800 | [diff] [blame] | 488 | } catch (RemoteException e) { |
| 489 | return false; |
| 490 | } |
| 491 | } |
Sunny Goyal | 5067909 | 2018-04-04 14:41:07 -0700 | [diff] [blame] | 492 | |
| 493 | /** |
Gustav Sennton | 5a4fc21 | 2019-02-28 16:12:27 +0000 | [diff] [blame] | 494 | * @return whether lock task mode is active in kiosk-mode (not screen pinning). |
| 495 | */ |
| 496 | public boolean isLockTaskKioskModeActive() { |
| 497 | try { |
| 498 | return ActivityTaskManager.getService().getLockTaskModeState() == LOCK_TASK_MODE_LOCKED; |
| 499 | } catch (RemoteException e) { |
| 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
Sunny Goyal | 5067909 | 2018-04-04 14:41:07 -0700 | [diff] [blame] | 505 | * Shows a voice session identified by {@code token} |
| 506 | * @return true if the session was shown, false otherwise |
| 507 | */ |
| 508 | public boolean showVoiceSession(IBinder token, Bundle args, int flags) { |
| 509 | IVoiceInteractionManagerService service = IVoiceInteractionManagerService.Stub.asInterface( |
| 510 | ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); |
| 511 | if (service == null) { |
| 512 | return false; |
| 513 | } |
| 514 | try { |
| 515 | return service.showSessionFromSession(token, args, flags); |
| 516 | } catch (RemoteException e) { |
| 517 | return false; |
| 518 | } |
| 519 | } |
Evan Rosky | 575c68c | 2018-11-15 18:54:25 -0800 | [diff] [blame] | 520 | |
| 521 | /** |
| 522 | * Returns true if the system supports freeform multi-window. |
| 523 | */ |
| 524 | public boolean supportsFreeformMultiWindow(Context context) { |
| 525 | final boolean freeformDevOption = Settings.Global.getInt(context.getContentResolver(), |
| 526 | Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, 0) != 0; |
| 527 | return ActivityTaskManager.supportsMultiWindow(context) |
| 528 | && (context.getPackageManager().hasSystemFeature( |
| 529 | PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT) |
| 530 | || freeformDevOption); |
| 531 | } |
Sunny Goyal | f5b03e0 | 2019-07-12 10:38:37 -0700 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * Returns true if the running task represents the home task |
| 535 | */ |
| 536 | public static boolean isHomeTask(RunningTaskInfo info) { |
| 537 | return info.configuration.windowConfiguration.getActivityType() |
| 538 | == WindowConfiguration.ACTIVITY_TYPE_HOME; |
| 539 | } |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 540 | } |