blob: c91ee8e2f2869e97543993a9717289176b5f9bd5 [file] [log] [blame]
Wale Ogunwale65ebd952018-04-25 15:41:44 -07001/*
2 * Copyright (C) 2018 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
Wale Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070018
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070019import static android.Manifest.permission.BIND_VOICE_INTERACTION;
20import static android.Manifest.permission.CHANGE_CONFIGURATION;
21import static android.Manifest.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS;
22import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070023import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070024import static android.Manifest.permission.READ_FRAME_BUFFER;
25import static android.Manifest.permission.REMOVE_TASKS;
26import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Wale Ogunwalea6191b42018-05-09 07:41:32 -070027import static android.Manifest.permission.STOP_APP_SWITCHES;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070028import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
Evan Rosky4505b352018-09-06 11:20:40 -070029import static android.app.ActivityManagerInternal.ALLOW_FULL_ONLY;
Yunfan Chen79b96062018-10-17 12:45:23 -070030import static android.app.ActivityTaskManager.INVALID_TASK_ID;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070031import static android.app.ActivityTaskManager.RESIZE_MODE_PRESERVE_WINDOW;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070032import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
33import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070034import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
35import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
37import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070039import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale31913b52018-10-13 08:29:31 -070040import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070041import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Wale Ogunwale31913b52018-10-13 08:29:31 -070042import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale214f3482018-10-04 11:00:47 -070043import static android.content.pm.ApplicationInfo.FLAG_FACTORY_TEST;
Wale Ogunwale342fbe92018-10-09 08:44:10 -070044import static android.content.pm.ConfigurationInfo.GL_ES_VERSION_UNDEFINED;
Wale Ogunwalea6191b42018-05-09 07:41:32 -070045import static android.content.pm.PackageManager.FEATURE_ACTIVITIES_ON_SECONDARY_DISPLAYS;
46import static android.content.pm.PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT;
Evan Rosky4505b352018-09-06 11:20:40 -070047import static android.content.pm.PackageManager.FEATURE_PC;
Wale Ogunwalea6191b42018-05-09 07:41:32 -070048import static android.content.pm.PackageManager.FEATURE_PICTURE_IN_PICTURE;
Wale Ogunwaled0412b32018-05-08 09:25:50 -070049import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalea6191b42018-05-09 07:41:32 -070050import static android.content.res.Configuration.UI_MODE_TYPE_TELEVISION;
51import static android.os.Build.VERSION_CODES.N;
Wale Ogunwale214f3482018-10-04 11:00:47 -070052import static android.os.FactoryTest.FACTORY_TEST_HIGH_LEVEL;
53import static android.os.FactoryTest.FACTORY_TEST_LOW_LEVEL;
54import static android.os.FactoryTest.FACTORY_TEST_OFF;
Wale Ogunwale31913b52018-10-13 08:29:31 -070055import static android.os.Process.FIRST_APPLICATION_UID;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070056import static android.os.Process.SYSTEM_UID;
Evan Rosky4505b352018-09-06 11:20:40 -070057import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Wale Ogunwalea6191b42018-05-09 07:41:32 -070058import static android.provider.Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT;
59import static android.provider.Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES;
60import static android.provider.Settings.Global.DEVELOPMENT_FORCE_RTL;
Evan Rosky4505b352018-09-06 11:20:40 -070061import static android.provider.Settings.Global.HIDE_ERROR_DIALOGS;
62import static android.provider.Settings.System.FONT_SCALE;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070063import static android.service.voice.VoiceInteractionSession.SHOW_SOURCE_APPLICATION;
Alison Cichowlas3e340502018-08-07 17:15:01 -040064import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070065import static android.view.Display.DEFAULT_DISPLAY;
66import static android.view.Display.INVALID_DISPLAY;
Wale Ogunwale6767eae2018-05-03 15:52:51 -070067import static android.view.WindowManager.TRANSIT_NONE;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070068import static android.view.WindowManager.TRANSIT_TASK_IN_PLACE;
Evan Rosky4505b352018-09-06 11:20:40 -070069
Yunfan Chen79b96062018-10-17 12:45:23 -070070import static com.android.server.am.ActivityManagerService.ANR_TRACE_DIR;
71import static com.android.server.am.ActivityManagerService.MY_PID;
72import static com.android.server.am.ActivityManagerService.STOCK_PM_FLAGS;
73import static com.android.server.am.ActivityManagerService.dumpStackTraces;
Wale Ogunwale31913b52018-10-13 08:29:31 -070074import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.CONFIG_WILL_CHANGE;
75import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.CONTROLLER;
76import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.CURRENT_TRACKER;
77import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.Controller.IS_A_MONKEY;
78import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.GLOBAL_CONFIGURATION;
79import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.GOING_TO_SLEEP;
80import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.HEAVY_WEIGHT_PROC;
81import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.HOME_PROC;
82import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.LAUNCHING_ACTIVITY;
83import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.PREVIOUS_PROC;
Yunfan Chen279f5582018-12-12 15:24:50 -080084import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.PREVIOUS_PROC_VISIBLE_TIME_MS;
Wale Ogunwale31913b52018-10-13 08:29:31 -070085import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.SCREEN_COMPAT_PACKAGES;
Yunfan Chen279f5582018-12-12 15:24:50 -080086import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.ScreenCompatPackage.MODE;
87import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.ScreenCompatPackage.PACKAGE;
Wale Ogunwale59507092018-10-29 09:00:30 -070088import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_DESTROYING;
89import static com.android.server.wm.ActivityStackSupervisor.DEFER_RESUME;
Wale Ogunwale59507092018-10-29 09:00:30 -070090import static com.android.server.wm.ActivityStackSupervisor.ON_TOP;
91import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
92import static com.android.server.wm.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
93import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ALL;
94import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
95import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_FOCUS;
96import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_IMMERSIVE;
97import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_LOCKTASK;
98import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STACK;
99import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
100import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
101import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_VISIBILITY;
102import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFIGURATION;
103import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_FOCUS;
104import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_IMMERSIVE;
105import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_LOCKTASK;
106import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STACK;
107import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SWITCH;
108import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_VISIBILITY;
109import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
110import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Evan Rosky4505b352018-09-06 11:20:40 -0700111import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_CONTENT;
112import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_DATA;
113import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_RECEIVER_EXTRAS;
114import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_STRUCTURE;
wilsonshihe7903ea2018-09-26 16:17:59 +0800115import static com.android.server.wm.ActivityTaskManagerService.H.REPORT_TIME_TRACKER_MSG;
116import static com.android.server.wm.ActivityTaskManagerService.UiHandler.DISMISS_DIALOG_UI_MSG;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700117import static com.android.server.wm.RecentsAnimationController.REORDER_KEEP_IN_PLACE;
118import static com.android.server.wm.RecentsAnimationController.REORDER_MOVE_TO_ORIGINAL_POSITION;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800119import static com.android.server.wm.RootActivityContainer.MATCH_TASK_IN_STACKS_ONLY;
120import static com.android.server.wm.RootActivityContainer.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS;
wilsonshihe7903ea2018-09-26 16:17:59 +0800121import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
122import static com.android.server.wm.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
123import static com.android.server.wm.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700124
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700125import android.Manifest;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700126import android.annotation.NonNull;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700127import android.annotation.Nullable;
128import android.annotation.UserIdInt;
129import android.app.Activity;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700130import android.app.ActivityManager;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700131import android.app.ActivityManagerInternal;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700132import android.app.ActivityOptions;
133import android.app.ActivityTaskManager;
Wale Ogunwalef6733932018-06-27 05:14:34 -0700134import android.app.ActivityThread;
135import android.app.AlertDialog;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700136import android.app.AppGlobals;
Evan Rosky4505b352018-09-06 11:20:40 -0700137import android.app.Dialog;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700138import android.app.IActivityController;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700139import android.app.IActivityTaskManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700140import android.app.IApplicationThread;
141import android.app.IAssistDataReceiver;
Wale Ogunwale53783742018-09-16 10:21:51 -0700142import android.app.INotificationManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700143import android.app.ITaskStackListener;
Wale Ogunwale53783742018-09-16 10:21:51 -0700144import android.app.Notification;
145import android.app.NotificationManager;
146import android.app.PendingIntent;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700147import android.app.PictureInPictureParams;
148import android.app.ProfilerInfo;
149import android.app.RemoteAction;
150import android.app.WaitResult;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700151import android.app.WindowConfiguration;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700152import android.app.admin.DevicePolicyCache;
153import android.app.assist.AssistContent;
154import android.app.assist.AssistStructure;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700155import android.app.usage.UsageStatsManagerInternal;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700156import android.content.ActivityNotFoundException;
157import android.content.ComponentName;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700158import android.content.ContentResolver;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700159import android.content.Context;
Evan Rosky4505b352018-09-06 11:20:40 -0700160import android.content.DialogInterface;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700161import android.content.IIntentSender;
162import android.content.Intent;
163import android.content.pm.ActivityInfo;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700164import android.content.pm.ApplicationInfo;
Yunfan Chen75157d72018-07-27 14:47:21 +0900165import android.content.pm.ConfigurationInfo;
Evan Rosky4505b352018-09-06 11:20:40 -0700166import android.content.pm.IPackageManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700167import android.content.pm.PackageManager;
Evan Rosky4505b352018-09-06 11:20:40 -0700168import android.content.pm.PackageManagerInternal;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700169import android.content.pm.ParceledListSlice;
170import android.content.pm.ResolveInfo;
Wale Ogunwale53783742018-09-16 10:21:51 -0700171import android.content.res.CompatibilityInfo;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700172import android.content.res.Configuration;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700173import android.content.res.Resources;
Evan Rosky4505b352018-09-06 11:20:40 -0700174import android.database.ContentObserver;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700175import android.graphics.Bitmap;
176import android.graphics.Point;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700177import android.graphics.Rect;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700178import android.metrics.LogMaker;
179import android.net.Uri;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700180import android.os.Binder;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700181import android.os.Build;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700182import android.os.Bundle;
Wale Ogunwale214f3482018-10-04 11:00:47 -0700183import android.os.FactoryTest;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700184import android.os.FileUtils;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700185import android.os.Handler;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700186import android.os.IBinder;
Evan Rosky4505b352018-09-06 11:20:40 -0700187import android.os.IUserManager;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700188import android.os.LocaleList;
Riddle Hsud93a6c42018-11-29 21:50:06 +0800189import android.os.Looper;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700190import android.os.Message;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700191import android.os.PersistableBundle;
Evan Rosky4505b352018-09-06 11:20:40 -0700192import android.os.PowerManager;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700193import android.os.PowerManagerInternal;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700194import android.os.RemoteException;
Evan Rosky4505b352018-09-06 11:20:40 -0700195import android.os.ServiceManager;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700196import android.os.StrictMode;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700197import android.os.SystemClock;
198import android.os.SystemProperties;
Evan Rosky4505b352018-09-06 11:20:40 -0700199import android.os.Trace;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700200import android.os.UpdateLock;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700201import android.os.UserHandle;
Evan Rosky4505b352018-09-06 11:20:40 -0700202import android.os.UserManager;
203import android.os.WorkSource;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700204import android.os.storage.IStorageManager;
205import android.os.storage.StorageManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700206import android.provider.Settings;
207import android.service.voice.IVoiceInteractionSession;
208import android.service.voice.VoiceInteractionManagerInternal;
Kiyoung Kim0fe161d2018-12-20 18:26:10 +0900209import android.sysprop.DisplayProperties;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700210import android.telecom.TelecomManager;
211import android.text.TextUtils;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700212import android.text.format.Time;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700213import android.util.ArrayMap;
214import android.util.EventLog;
215import android.util.Log;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700216import android.util.Slog;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700217import android.util.SparseArray;
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700218import android.util.SparseIntArray;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700219import android.util.StatsLog;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700220import android.util.TimeUtils;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700221import android.util.proto.ProtoOutputStream;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700222import android.view.IRecentsAnimationRunner;
223import android.view.RemoteAnimationAdapter;
224import android.view.RemoteAnimationDefinition;
Evan Rosky4505b352018-09-06 11:20:40 -0700225import android.view.WindowManager;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700226
Evan Rosky4505b352018-09-06 11:20:40 -0700227import com.android.internal.R;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700228import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700229import com.android.internal.app.AssistUtils;
Evan Rosky4505b352018-09-06 11:20:40 -0700230import com.android.internal.app.IAppOpsService;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700231import com.android.internal.app.IVoiceInteractor;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700232import com.android.internal.app.ProcessMap;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700233import com.android.internal.logging.MetricsLogger;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700234import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwale53783742018-09-16 10:21:51 -0700235import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
236import com.android.internal.notification.SystemNotificationChannels;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700237import com.android.internal.os.TransferPipe;
Evan Rosky4505b352018-09-06 11:20:40 -0700238import com.android.internal.os.logging.MetricsLoggerWrapper;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700239import com.android.internal.policy.IKeyguardDismissCallback;
240import com.android.internal.policy.KeyguardDismissCallback;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700241import com.android.internal.util.ArrayUtils;
242import com.android.internal.util.FastPrintWriter;
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700243import com.android.internal.util.Preconditions;
Wale Ogunwale53783742018-09-16 10:21:51 -0700244import com.android.internal.util.function.pooled.PooledLambda;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700245import com.android.server.AttributeCache;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700246import com.android.server.LocalServices;
247import com.android.server.SystemService;
Evan Rosky4505b352018-09-06 11:20:40 -0700248import com.android.server.SystemServiceManager;
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800249import com.android.server.UiThread;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700250import com.android.server.Watchdog;
Wale Ogunwale59507092018-10-29 09:00:30 -0700251import com.android.server.am.ActivityManagerService;
252import com.android.server.am.ActivityManagerServiceDumpActivitiesProto;
253import com.android.server.am.ActivityManagerServiceDumpProcessesProto;
254import com.android.server.am.AppTimeTracker;
255import com.android.server.am.BaseErrorDialog;
256import com.android.server.am.EventLogTags;
257import com.android.server.am.PendingIntentController;
258import com.android.server.am.PendingIntentRecord;
259import com.android.server.am.UserState;
Kiyoung Kim0fe161d2018-12-20 18:26:10 +0900260import com.android.server.appop.AppOpsService;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700261import com.android.server.firewall.IntentFirewall;
Evan Rosky4505b352018-09-06 11:20:40 -0700262import com.android.server.pm.UserManagerService;
263import com.android.server.uri.UriGrantsManagerInternal;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700264import com.android.server.vr.VrManagerInternal;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700265
Wale Ogunwale31913b52018-10-13 08:29:31 -0700266import java.io.BufferedReader;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700267import java.io.File;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700268import java.io.FileDescriptor;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700269import java.io.FileOutputStream;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700270import java.io.FileReader;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700271import java.io.IOException;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700272import java.io.PrintWriter;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700273import java.io.StringWriter;
Riddle Hsua0536432019-02-16 00:38:59 +0800274import java.lang.annotation.ElementType;
275import java.lang.annotation.Retention;
276import java.lang.annotation.RetentionPolicy;
277import java.lang.annotation.Target;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700278import java.lang.ref.WeakReference;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700279import java.text.DateFormat;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700280import java.util.ArrayList;
Wale Ogunwale27c48ae2018-10-25 19:01:01 -0700281import java.util.Arrays;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700282import java.util.Date;
Alison Cichowlas3e340502018-08-07 17:15:01 -0400283import java.util.HashMap;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700284import java.util.HashSet;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700285import java.util.List;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700286import java.util.Locale;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700287import java.util.Map;
288import java.util.Set;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700289
290/**
291 * System service for managing activities and their containers (task, stacks, displays,... ).
292 *
293 * {@hide}
294 */
295public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
Wale Ogunwale98875612018-10-12 07:53:02 -0700296 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityTaskManagerService" : TAG_ATM;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700297 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700298 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
299 private static final String TAG_IMMERSIVE = TAG + POSTFIX_IMMERSIVE;
300 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
301 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
302 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700303 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700304
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700305 // How long we wait until we timeout on key dispatching.
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700306 public static final int KEY_DISPATCHING_TIMEOUT_MS = 5 * 1000;
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700307 // How long we wait until we timeout on key dispatching during instrumentation.
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700308 static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT_MS = 60 * 1000;
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700309
Wale Ogunwale98875612018-10-12 07:53:02 -0700310 /** Used to indicate that an app transition should be animated. */
311 static final boolean ANIMATE = true;
312
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700313 /** Hardware-reported OpenGLES version. */
314 final int GL_ES_VERSION;
315
Wale Ogunwale31913b52018-10-13 08:29:31 -0700316 public static final String DUMP_ACTIVITIES_CMD = "activities" ;
317 public static final String DUMP_ACTIVITIES_SHORT_CMD = "a" ;
318 public static final String DUMP_LASTANR_CMD = "lastanr" ;
319 public static final String DUMP_LASTANR_TRACES_CMD = "lastanr-traces" ;
320 public static final String DUMP_STARTER_CMD = "starter" ;
321 public static final String DUMP_CONTAINERS_CMD = "containers" ;
322 public static final String DUMP_RECENTS_CMD = "recents" ;
323 public static final String DUMP_RECENTS_SHORT_CMD = "r" ;
324
Wale Ogunwale64258362018-10-16 15:13:37 -0700325 /** This activity is not being relaunched, or being relaunched for a non-resize reason. */
326 public static final int RELAUNCH_REASON_NONE = 0;
327 /** This activity is being relaunched due to windowing mode change. */
328 public static final int RELAUNCH_REASON_WINDOWING_MODE_RESIZE = 1;
329 /** This activity is being relaunched due to a free-resize operation. */
330 public static final int RELAUNCH_REASON_FREE_RESIZE = 2;
331
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700332 Context mContext;
Wale Ogunwale64258362018-10-16 15:13:37 -0700333
Wale Ogunwalef6733932018-06-27 05:14:34 -0700334 /**
335 * This Context is themable and meant for UI display (AlertDialogs, etc.). The theme can
336 * change at runtime. Use mContext for non-UI purposes.
337 */
338 final Context mUiContext;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700339 final ActivityThread mSystemThread;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700340 H mH;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700341 UiHandler mUiHandler;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700342 ActivityManagerInternal mAmInternal;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700343 UriGrantsManagerInternal mUgmInternal;
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700344 private PackageManagerInternal mPmInternal;
Wale Ogunwaleb73f3962018-11-20 07:58:22 -0800345 @VisibleForTesting
346 final ActivityTaskManagerInternal mInternal;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700347 PowerManagerInternal mPowerManagerInternal;
348 private UsageStatsManagerInternal mUsageStatsInternal;
349
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700350 PendingIntentController mPendingIntentController;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700351 IntentFirewall mIntentFirewall;
352
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700353 /* Global service lock used by the package the owns this service. */
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800354 final WindowManagerGlobalLock mGlobalLock = new WindowManagerGlobalLock();
Riddle Hsud7088f82019-01-30 13:04:50 +0800355 /**
356 * It is the same instance as {@link mGlobalLock}, just declared as a type that the
357 * locked-region-code-injection does't recognize it. It is used to skip wrapping priority
358 * booster for places that are already in the scope of another booster (e.g. computing oom-adj).
359 *
360 * @see WindowManagerThreadPriorityBooster
361 */
362 final Object mGlobalLockWithoutBoost = mGlobalLock;
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700363 ActivityStackSupervisor mStackSupervisor;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800364 RootActivityContainer mRootActivityContainer;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700365 WindowManagerService mWindowManager;
Wale Ogunwalef6733932018-06-27 05:14:34 -0700366 private UserManagerService mUserManager;
367 private AppOpsService mAppOpsService;
Wale Ogunwalebff2df42018-10-18 17:09:19 -0700368 /** All active uids in the system. */
Riddle Hsua0536432019-02-16 00:38:59 +0800369 private final MirrorActiveUids mActiveUids = new MirrorActiveUids();
Wale Ogunwale9de19442018-10-18 19:05:03 -0700370 private final SparseArray<String> mPendingTempWhitelist = new SparseArray<>();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700371 /** All processes currently running that might have a window organized by name. */
372 final ProcessMap<WindowProcessController> mProcessNames = new ProcessMap<>();
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700373 /** All processes we currently have running mapped by pid */
374 final SparseArray<WindowProcessController> mPidMap = new SparseArray<>();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700375 /** This is the process holding what we currently consider to be the "home" activity. */
376 WindowProcessController mHomeProcess;
Wale Ogunwale53783742018-09-16 10:21:51 -0700377 /** The currently running heavy-weight process, if any. */
378 WindowProcessController mHeavyWeightProcess = null;
Wale Ogunwale214f3482018-10-04 11:00:47 -0700379 boolean mHasHeavyWeightFeature;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700380 /**
381 * This is the process holding the activity the user last visited that is in a different process
382 * from the one they are currently in.
383 */
384 WindowProcessController mPreviousProcess;
385 /** The time at which the previous process was last visible. */
386 long mPreviousProcessVisibleTime;
387
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700388 /** List of intents that were used to start the most recent tasks. */
389 private RecentTasks mRecentTasks;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700390 /** State of external calls telling us if the device is awake or asleep. */
391 private boolean mKeyguardShown = false;
392
393 // Wrapper around VoiceInteractionServiceManager
394 private AssistUtils mAssistUtils;
395
396 // VoiceInteraction session ID that changes for each new request except when
397 // being called for multi-window assist in a single session.
398 private int mViSessionId = 1000;
399
400 // How long to wait in getAssistContextExtras for the activity and foreground services
401 // to respond with the result.
402 private static final int PENDING_ASSIST_EXTRAS_TIMEOUT = 500;
403
404 // How long top wait when going through the modern assist (which doesn't need to block
405 // on getting this result before starting to launch its UI).
406 private static final int PENDING_ASSIST_EXTRAS_LONG_TIMEOUT = 2000;
407
408 // How long to wait in getAutofillAssistStructure() for the activity to respond with the result.
409 private static final int PENDING_AUTOFILL_ASSIST_STRUCTURE_TIMEOUT = 2000;
410
Alison Cichowlas3e340502018-08-07 17:15:01 -0400411 // Permission tokens are used to temporarily granted a trusted app the ability to call
412 // #startActivityAsCaller. A client is expected to dump its token after this time has elapsed,
413 // showing any appropriate error messages to the user.
414 private static final long START_AS_CALLER_TOKEN_TIMEOUT =
415 10 * MINUTE_IN_MILLIS;
416
417 // How long before the service actually expires a token. This is slightly longer than
418 // START_AS_CALLER_TOKEN_TIMEOUT, to provide a buffer so clients will rarely encounter the
419 // expiration exception.
420 private static final long START_AS_CALLER_TOKEN_TIMEOUT_IMPL =
421 START_AS_CALLER_TOKEN_TIMEOUT + 2 * 1000;
422
423 // How long the service will remember expired tokens, for the purpose of providing error
424 // messaging when a client uses an expired token.
425 private static final long START_AS_CALLER_TOKEN_EXPIRED_TIMEOUT =
426 START_AS_CALLER_TOKEN_TIMEOUT_IMPL + 20 * MINUTE_IN_MILLIS;
427
428 // Activity tokens of system activities that are delegating their call to
429 // #startActivityByCaller, keyed by the permissionToken granted to the delegate.
430 final HashMap<IBinder, IBinder> mStartActivitySources = new HashMap<>();
431
432 // Permission tokens that have expired, but we remember for error reporting.
433 final ArrayList<IBinder> mExpiredStartAsCallerTokens = new ArrayList<>();
434
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700435 private final ArrayList<PendingAssistExtras> mPendingAssistExtras = new ArrayList<>();
436
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700437 // Keeps track of the active voice interaction service component, notified from
438 // VoiceInteractionManagerService
439 ComponentName mActiveVoiceInteractionServiceComponent;
440
Wale Ogunwalee2172292018-10-25 10:11:10 -0700441 VrController mVrController;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700442 KeyguardController mKeyguardController;
443 private final ClientLifecycleManager mLifecycleManager;
444 private TaskChangeNotificationController mTaskChangeNotificationController;
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700445 /** The controller for all operations related to locktask. */
446 private LockTaskController mLockTaskController;
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700447 private ActivityStartController mActivityStartController;
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700448
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700449 boolean mSuppressResizeConfigChanges;
450
451 private final UpdateConfigurationResult mTmpUpdateConfigurationResult =
452 new UpdateConfigurationResult();
453
454 static final class UpdateConfigurationResult {
455 // Configuration changes that were updated.
456 int changes;
457 // If the activity was relaunched to match the new configuration.
458 boolean activityRelaunched;
459
460 void reset() {
461 changes = 0;
462 activityRelaunched = false;
463 }
464 }
465
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700466 /** Current sequencing integer of the configuration, for skipping old configurations. */
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700467 private int mConfigurationSeq;
468 // To cache the list of supported system locales
469 private String[] mSupportedSystemLocales = null;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700470
471 /**
472 * Temp object used when global and/or display override configuration is updated. It is also
473 * sent to outer world instead of {@link #getGlobalConfiguration} because we don't trust
474 * anyone...
475 */
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700476 private Configuration mTempConfig = new Configuration();
477
Wale Ogunwalef6733932018-06-27 05:14:34 -0700478 /** Temporary to avoid allocations. */
479 final StringBuilder mStringBuilder = new StringBuilder(256);
480
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700481 // Amount of time after a call to stopAppSwitches() during which we will
482 // prevent further untrusted switches from happening.
483 private static final long APP_SWITCH_DELAY_TIME = 5 * 1000;
484
485 /**
486 * The time at which we will allow normal application switches again,
487 * after a call to {@link #stopAppSwitches()}.
488 */
Wale Ogunwalef6733932018-06-27 05:14:34 -0700489 private long mAppSwitchesAllowedTime;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700490 /**
491 * This is set to true after the first switch after mAppSwitchesAllowedTime
492 * is set; any switches after that will clear the time.
493 */
Wale Ogunwalef6733932018-06-27 05:14:34 -0700494 private boolean mDidAppSwitch;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700495
496 IActivityController mController = null;
497 boolean mControllerIsAMonkey = false;
498
Wale Ogunwale214f3482018-10-04 11:00:47 -0700499 final int mFactoryTest;
500
501 /** Used to control how we initialize the service. */
502 ComponentName mTopComponent;
503 String mTopAction = Intent.ACTION_MAIN;
504 String mTopData;
505
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800506 /** Profiling app information. */
507 String mProfileApp = null;
508 WindowProcessController mProfileProc = null;
509 ProfilerInfo mProfilerInfo = null;
510
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700511 /**
Wale Ogunwale31913b52018-10-13 08:29:31 -0700512 * Dump of the activity state at the time of the last ANR. Cleared after
513 * {@link WindowManagerService#LAST_ANR_LIFETIME_DURATION_MSECS}
514 */
515 String mLastANRState;
516
517 /**
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700518 * Used to retain an update lock when the foreground activity is in
519 * immersive mode.
520 */
Wale Ogunwalef6733932018-06-27 05:14:34 -0700521 private final UpdateLock mUpdateLock = new UpdateLock("immersive");
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700522
523 /**
524 * Packages that are being allowed to perform unrestricted app switches. Mapping is
525 * User -> Type -> uid.
526 */
527 final SparseArray<ArrayMap<String, Integer>> mAllowAppSwitchUids = new SparseArray<>();
528
529 /** The dimensions of the thumbnails in the Recents UI. */
Wale Ogunwalef6733932018-06-27 05:14:34 -0700530 private int mThumbnailWidth;
531 private int mThumbnailHeight;
532 private float mFullscreenThumbnailScale;
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700533
534 /**
535 * Flag that indicates if multi-window is enabled.
536 *
537 * For any particular form of multi-window to be enabled, generic multi-window must be enabled
538 * in {@link com.android.internal.R.bool#config_supportsMultiWindow} config or
539 * {@link Settings.Global#DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES} development option set.
540 * At least one of the forms of multi-window must be enabled in order for this flag to be
541 * initialized to 'true'.
542 *
543 * @see #mSupportsSplitScreenMultiWindow
544 * @see #mSupportsFreeformWindowManagement
545 * @see #mSupportsPictureInPicture
546 * @see #mSupportsMultiDisplay
547 */
548 boolean mSupportsMultiWindow;
549 boolean mSupportsSplitScreenMultiWindow;
550 boolean mSupportsFreeformWindowManagement;
551 boolean mSupportsPictureInPicture;
552 boolean mSupportsMultiDisplay;
553 boolean mForceResizableActivities;
554
555 final List<ActivityTaskManagerInternal.ScreenObserver> mScreenObservers = new ArrayList<>();
556
557 // VR Vr2d Display Id.
558 int mVr2dDisplayId = INVALID_DISPLAY;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700559
Wale Ogunwalef6733932018-06-27 05:14:34 -0700560 /**
561 * Set while we are wanting to sleep, to prevent any
562 * activities from being started/resumed.
563 *
564 * TODO(b/33594039): Clarify the actual state transitions represented by mSleeping.
565 *
566 * Currently mSleeping is set to true when transitioning into the sleep state, and remains true
567 * while in the sleep state until there is a pending transition out of sleep, in which case
568 * mSleeping is set to false, and remains false while awake.
569 *
570 * Whether mSleeping can quickly toggled between true/false without the device actually
571 * display changing states is undefined.
572 */
573 private boolean mSleeping = false;
574
575 /**
576 * The process state used for processes that are running the top activities.
577 * This changes between TOP and TOP_SLEEPING to following mSleeping.
578 */
579 int mTopProcessState = ActivityManager.PROCESS_STATE_TOP;
580
581 // Whether we should show our dialogs (ANR, crash, etc) or just perform their default action
582 // automatically. Important for devices without direct input devices.
583 private boolean mShowDialogs = true;
584
585 /** Set if we are shutting down the system, similar to sleeping. */
586 boolean mShuttingDown = false;
587
588 /**
589 * We want to hold a wake lock while running a voice interaction session, since
590 * this may happen with the screen off and we need to keep the CPU running to
591 * be able to continue to interact with the user.
592 */
593 PowerManager.WakeLock mVoiceWakeLock;
594
595 /**
596 * Set while we are running a voice interaction. This overrides sleeping while it is active.
597 */
598 IVoiceInteractionSession mRunningVoice;
599
600 /**
601 * The last resumed activity. This is identical to the current resumed activity most
602 * of the time but could be different when we're pausing one activity before we resume
603 * another activity.
604 */
605 ActivityRecord mLastResumedActivity;
606
607 /**
608 * The activity that is currently being traced as the active resumed activity.
609 *
610 * @see #updateResumedAppTrace
611 */
612 private @Nullable ActivityRecord mTracedResumedActivity;
613
614 /** If non-null, we are tracking the time the user spends in the currently focused app. */
615 AppTimeTracker mCurAppTimeTracker;
616
Wale Ogunwale008163e2018-07-23 23:11:08 -0700617 private AppWarnings mAppWarnings;
618
Wale Ogunwale53783742018-09-16 10:21:51 -0700619 /**
620 * Packages that the user has asked to have run in screen size
621 * compatibility mode instead of filling the screen.
622 */
623 CompatModePackages mCompatModePackages;
624
Wale Ogunwalef6733932018-06-27 05:14:34 -0700625 private FontScaleSettingObserver mFontScaleSettingObserver;
626
Michal Karpinski4026cae2019-02-12 11:51:47 +0000627 private String mDeviceOwnerPackageName;
628
Wale Ogunwalef6733932018-06-27 05:14:34 -0700629 private final class FontScaleSettingObserver extends ContentObserver {
630 private final Uri mFontScaleUri = Settings.System.getUriFor(FONT_SCALE);
631 private final Uri mHideErrorDialogsUri = Settings.Global.getUriFor(HIDE_ERROR_DIALOGS);
632
633 public FontScaleSettingObserver() {
634 super(mH);
635 final ContentResolver resolver = mContext.getContentResolver();
636 resolver.registerContentObserver(mFontScaleUri, false, this, UserHandle.USER_ALL);
637 resolver.registerContentObserver(mHideErrorDialogsUri, false, this,
638 UserHandle.USER_ALL);
639 }
640
641 @Override
642 public void onChange(boolean selfChange, Uri uri, @UserIdInt int userId) {
643 if (mFontScaleUri.equals(uri)) {
644 updateFontScaleIfNeeded(userId);
645 } else if (mHideErrorDialogsUri.equals(uri)) {
646 synchronized (mGlobalLock) {
647 updateShouldShowDialogsLocked(getGlobalConfiguration());
648 }
649 }
650 }
651 }
652
Riddle Hsua0536432019-02-16 00:38:59 +0800653 /** Indicates that the method may be invoked frequently or is sensitive to performance. */
654 @Target(ElementType.METHOD)
655 @Retention(RetentionPolicy.SOURCE)
656 @interface HotPath {
657 int NONE = 0;
658 int OOM_ADJUSTMENT = 1;
659 int LRU_UPDATE = 2;
660 int PROCESS_CHANGE = 3;
661 int caller() default NONE;
662 }
663
Charles Chen8d98dd22018-12-26 17:36:54 +0800664 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
665 public ActivityTaskManagerService(Context context) {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700666 mContext = context;
Wale Ogunwale214f3482018-10-04 11:00:47 -0700667 mFactoryTest = FactoryTest.getMode();
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700668 mSystemThread = ActivityThread.currentActivityThread();
669 mUiContext = mSystemThread.getSystemUiContext();
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700670 mLifecycleManager = new ClientLifecycleManager();
Wale Ogunwaleb73f3962018-11-20 07:58:22 -0800671 mInternal = new LocalService();
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700672 GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", GL_ES_VERSION_UNDEFINED);
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700673 }
674
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700675 public void onSystemReady() {
676 synchronized (mGlobalLock) {
677 mHasHeavyWeightFeature = mContext.getPackageManager().hasSystemFeature(
678 PackageManager.FEATURE_CANT_SAVE_STATE);
679 mAssistUtils = new AssistUtils(mContext);
680 mVrController.onSystemReady();
681 mRecentTasks.onSystemReadyLocked();
Garfield Tan891146c2018-10-09 12:14:00 -0700682 mStackSupervisor.onSystemReady();
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700683 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700684 }
685
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700686 public void onInitPowerManagement() {
687 synchronized (mGlobalLock) {
688 mStackSupervisor.initPowerManagement();
689 final PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
690 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
691 mVoiceWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*voice*");
692 mVoiceWakeLock.setReferenceCounted(false);
693 }
Wale Ogunwalef6733932018-06-27 05:14:34 -0700694 }
695
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700696 public void installSystemProviders() {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700697 mFontScaleSettingObserver = new FontScaleSettingObserver();
698 }
699
Wale Ogunwale59507092018-10-29 09:00:30 -0700700 public void retrieveSettings(ContentResolver resolver) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700701 final boolean freeformWindowManagement =
702 mContext.getPackageManager().hasSystemFeature(FEATURE_FREEFORM_WINDOW_MANAGEMENT)
703 || Settings.Global.getInt(
704 resolver, DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, 0) != 0;
705
706 final boolean supportsMultiWindow = ActivityTaskManager.supportsMultiWindow(mContext);
707 final boolean supportsPictureInPicture = supportsMultiWindow &&
708 mContext.getPackageManager().hasSystemFeature(FEATURE_PICTURE_IN_PICTURE);
709 final boolean supportsSplitScreenMultiWindow =
710 ActivityTaskManager.supportsSplitScreenMultiWindow(mContext);
711 final boolean supportsMultiDisplay = mContext.getPackageManager()
712 .hasSystemFeature(FEATURE_ACTIVITIES_ON_SECONDARY_DISPLAYS);
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700713 final boolean forceRtl = Settings.Global.getInt(resolver, DEVELOPMENT_FORCE_RTL, 0) != 0;
714 final boolean forceResizable = Settings.Global.getInt(
715 resolver, DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, 0) != 0;
Garfield Tane0846042018-07-26 13:42:04 -0700716 final boolean isPc = mContext.getPackageManager().hasSystemFeature(FEATURE_PC);
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700717
718 // Transfer any global setting for forcing RTL layout, into a System Property
Kiyoung Kim0fe161d2018-12-20 18:26:10 +0900719 DisplayProperties.debug_force_rtl(forceRtl);
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700720
721 final Configuration configuration = new Configuration();
722 Settings.System.getConfiguration(resolver, configuration);
723 if (forceRtl) {
724 // This will take care of setting the correct layout direction flags
725 configuration.setLayoutDirection(configuration.locale);
726 }
727
728 synchronized (mGlobalLock) {
729 mForceResizableActivities = forceResizable;
730 final boolean multiWindowFormEnabled = freeformWindowManagement
731 || supportsSplitScreenMultiWindow
732 || supportsPictureInPicture
733 || supportsMultiDisplay;
734 if ((supportsMultiWindow || forceResizable) && multiWindowFormEnabled) {
735 mSupportsMultiWindow = true;
736 mSupportsFreeformWindowManagement = freeformWindowManagement;
737 mSupportsSplitScreenMultiWindow = supportsSplitScreenMultiWindow;
738 mSupportsPictureInPicture = supportsPictureInPicture;
739 mSupportsMultiDisplay = supportsMultiDisplay;
740 } else {
741 mSupportsMultiWindow = false;
742 mSupportsFreeformWindowManagement = false;
743 mSupportsSplitScreenMultiWindow = false;
744 mSupportsPictureInPicture = false;
745 mSupportsMultiDisplay = false;
746 }
747 mWindowManager.setForceResizableTasks(mForceResizableActivities);
748 mWindowManager.setSupportsPictureInPicture(mSupportsPictureInPicture);
Garfield Tane0846042018-07-26 13:42:04 -0700749 mWindowManager.setSupportsFreeformWindowManagement(mSupportsFreeformWindowManagement);
750 mWindowManager.setIsPc(isPc);
Garfield Tanb5910b42019-03-14 14:50:59 -0700751 mWindowManager.mRoot.onSettingsRetrieved();
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700752 // This happens before any activities are started, so we can change global configuration
753 // in-place.
754 updateConfigurationLocked(configuration, null, true);
755 final Configuration globalConfig = getGlobalConfiguration();
756 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Initial config: " + globalConfig);
757
758 // Load resources only after the current configuration has been set.
759 final Resources res = mContext.getResources();
760 mThumbnailWidth = res.getDimensionPixelSize(
761 com.android.internal.R.dimen.thumbnail_width);
762 mThumbnailHeight = res.getDimensionPixelSize(
763 com.android.internal.R.dimen.thumbnail_height);
764
765 if ((globalConfig.uiMode & UI_MODE_TYPE_TELEVISION) == UI_MODE_TYPE_TELEVISION) {
766 mFullscreenThumbnailScale = (float) res
767 .getInteger(com.android.internal.R.integer.thumbnail_width_tv) /
768 (float) globalConfig.screenWidthDp;
769 } else {
770 mFullscreenThumbnailScale = res.getFraction(
771 com.android.internal.R.fraction.thumbnail_fullscreen_scale, 1, 1);
772 }
773 }
774 }
775
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800776 public WindowManagerGlobalLock getGlobalLock() {
777 return mGlobalLock;
778 }
779
Yunfan Chen585f2932019-01-29 16:04:45 +0900780 /** For test purpose only. */
781 @VisibleForTesting
782 public ActivityTaskManagerInternal getAtmInternal() {
783 return mInternal;
784 }
785
Riddle Hsud93a6c42018-11-29 21:50:06 +0800786 public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController,
787 Looper looper) {
788 mH = new H(looper);
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700789 mUiHandler = new UiHandler();
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700790 mIntentFirewall = intentFirewall;
Wale Ogunwale53783742018-09-16 10:21:51 -0700791 final File systemDir = SystemServiceManager.ensureSystemDir();
792 mAppWarnings = new AppWarnings(this, mUiContext, mH, mUiHandler, systemDir);
793 mCompatModePackages = new CompatModePackages(this, systemDir, mH);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700794 mPendingIntentController = intentController;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700795
796 mTempConfig.setToDefaults();
797 mTempConfig.setLocales(LocaleList.getDefault());
798 mConfigurationSeq = mTempConfig.seq = 1;
799 mStackSupervisor = createStackSupervisor();
Wale Ogunwaled32da472018-11-16 07:19:28 -0800800 mRootActivityContainer = new RootActivityContainer(this);
801 mRootActivityContainer.onConfigurationChanged(mTempConfig);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700802
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700803 mTaskChangeNotificationController =
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700804 new TaskChangeNotificationController(mGlobalLock, mStackSupervisor, mH);
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700805 mLockTaskController = new LockTaskController(mContext, mStackSupervisor, mH);
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700806 mActivityStartController = new ActivityStartController(this);
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700807 mRecentTasks = createRecentTasks();
808 mStackSupervisor.setRecentTasks(mRecentTasks);
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700809 mVrController = new VrController(mGlobalLock);
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700810 mKeyguardController = mStackSupervisor.getKeyguardController();
811 }
812
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700813 public void onActivityManagerInternalAdded() {
814 synchronized (mGlobalLock) {
815 mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
816 mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
817 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700818 }
819
Yunfan Chen75157d72018-07-27 14:47:21 +0900820 int increaseConfigurationSeqLocked() {
821 mConfigurationSeq = Math.max(++mConfigurationSeq, 1);
822 return mConfigurationSeq;
823 }
824
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700825 protected ActivityStackSupervisor createStackSupervisor() {
826 final ActivityStackSupervisor supervisor = new ActivityStackSupervisor(this, mH.getLooper());
827 supervisor.initialize();
828 return supervisor;
829 }
830
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700831 public void setWindowManager(WindowManagerService wm) {
832 synchronized (mGlobalLock) {
833 mWindowManager = wm;
834 mLockTaskController.setWindowManager(wm);
835 mStackSupervisor.setWindowManager(wm);
Wale Ogunwaled32da472018-11-16 07:19:28 -0800836 mRootActivityContainer.setWindowManager(wm);
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700837 }
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700838 }
839
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700840 public void setUsageStatsManager(UsageStatsManagerInternal usageStatsManager) {
841 synchronized (mGlobalLock) {
842 mUsageStatsInternal = usageStatsManager;
843 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700844 }
845
Wale Ogunwalef6733932018-06-27 05:14:34 -0700846 UserManagerService getUserManager() {
847 if (mUserManager == null) {
848 IBinder b = ServiceManager.getService(Context.USER_SERVICE);
849 mUserManager = (UserManagerService) IUserManager.Stub.asInterface(b);
850 }
851 return mUserManager;
852 }
853
854 AppOpsService getAppOpsService() {
855 if (mAppOpsService == null) {
856 IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
857 mAppOpsService = (AppOpsService) IAppOpsService.Stub.asInterface(b);
858 }
859 return mAppOpsService;
860 }
861
862 boolean hasUserRestriction(String restriction, int userId) {
863 return getUserManager().hasUserRestriction(restriction, userId);
864 }
865
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700866 protected RecentTasks createRecentTasks() {
867 return new RecentTasks(this, mStackSupervisor);
868 }
869
870 RecentTasks getRecentTasks() {
871 return mRecentTasks;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700872 }
873
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700874 ClientLifecycleManager getLifecycleManager() {
875 return mLifecycleManager;
876 }
877
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700878 ActivityStartController getActivityStartController() {
879 return mActivityStartController;
880 }
881
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700882 TaskChangeNotificationController getTaskChangeNotificationController() {
883 return mTaskChangeNotificationController;
884 }
885
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700886 LockTaskController getLockTaskController() {
887 return mLockTaskController;
888 }
889
Yunfan Chen75157d72018-07-27 14:47:21 +0900890 /**
891 * Return the global configuration used by the process corresponding to the input pid. This is
892 * usually the global configuration with some overrides specific to that process.
893 */
894 Configuration getGlobalConfigurationForCallingPid() {
895 final int pid = Binder.getCallingPid();
Yunfan Chenc2ff6cf2018-12-04 16:56:21 -0800896 return getGlobalConfigurationForPid(pid);
897 }
898
899 /**
900 * Return the global configuration used by the process corresponding to the given pid.
901 */
902 Configuration getGlobalConfigurationForPid(int pid) {
Yunfan Chen75157d72018-07-27 14:47:21 +0900903 if (pid == MY_PID || pid < 0) {
904 return getGlobalConfiguration();
905 }
906 synchronized (mGlobalLock) {
907 final WindowProcessController app = mPidMap.get(pid);
908 return app != null ? app.getConfiguration() : getGlobalConfiguration();
909 }
910 }
911
912 /**
913 * Return the device configuration info used by the process corresponding to the input pid.
914 * The value is consistent with the global configuration for the process.
915 */
916 @Override
917 public ConfigurationInfo getDeviceConfigurationInfo() {
918 ConfigurationInfo config = new ConfigurationInfo();
919 synchronized (mGlobalLock) {
920 final Configuration globalConfig = getGlobalConfigurationForCallingPid();
921 config.reqTouchScreen = globalConfig.touchscreen;
922 config.reqKeyboardType = globalConfig.keyboard;
923 config.reqNavigation = globalConfig.navigation;
924 if (globalConfig.navigation == Configuration.NAVIGATION_DPAD
925 || globalConfig.navigation == Configuration.NAVIGATION_TRACKBALL) {
926 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
927 }
928 if (globalConfig.keyboard != Configuration.KEYBOARD_UNDEFINED
929 && globalConfig.keyboard != Configuration.KEYBOARD_NOKEYS) {
930 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
931 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700932 config.reqGlEsVersion = GL_ES_VERSION;
Yunfan Chen75157d72018-07-27 14:47:21 +0900933 }
934 return config;
935 }
936
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700937 private void start() {
Wale Ogunwale53783742018-09-16 10:21:51 -0700938 LocalServices.addService(ActivityTaskManagerInternal.class, mInternal);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700939 }
940
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700941 public static final class Lifecycle extends SystemService {
942 private final ActivityTaskManagerService mService;
943
944 public Lifecycle(Context context) {
945 super(context);
946 mService = new ActivityTaskManagerService(context);
947 }
948
949 @Override
950 public void onStart() {
951 publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700952 mService.start();
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700953 }
954
Garfield Tan891146c2018-10-09 12:14:00 -0700955 @Override
956 public void onUnlockUser(int userId) {
957 synchronized (mService.getGlobalLock()) {
Garfield Tan0d407f42019-03-07 11:47:01 -0800958 mService.mStackSupervisor.onUserUnlocked(userId);
Garfield Tan891146c2018-10-09 12:14:00 -0700959 }
960 }
961
962 @Override
963 public void onCleanupUser(int userId) {
964 synchronized (mService.getGlobalLock()) {
965 mService.mStackSupervisor.mLaunchParamsPersister.onCleanupUser(userId);
966 }
967 }
968
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700969 public ActivityTaskManagerService getService() {
970 return mService;
971 }
972 }
973
974 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700975 public final int startActivity(IApplicationThread caller, String callingPackage,
976 Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
977 int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
978 return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
979 resultWho, requestCode, startFlags, profilerInfo, bOptions,
980 UserHandle.getCallingUserId());
981 }
982
983 @Override
984 public final int startActivities(IApplicationThread caller, String callingPackage,
985 Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle bOptions,
986 int userId) {
987 final String reason = "startActivities";
Wale Ogunwalea6191b42018-05-09 07:41:32 -0700988 enforceNotIsolatedCaller(reason);
989 userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, reason);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700990 // TODO: Switch to user app stacks here.
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000991 return getActivityStartController().startActivities(caller, -1, 0, -1, callingPackage,
992 intents, resolvedTypes, resultTo, SafeActivityOptions.fromBundle(bOptions), userId,
993 reason, null /* originatingPendingIntent */,
994 false /* allowBackgroundActivityStart */);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700995 }
996
997 @Override
998 public int startActivityAsUser(IApplicationThread caller, String callingPackage,
999 Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
1000 int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
1001 return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
1002 resultWho, requestCode, startFlags, profilerInfo, bOptions, userId,
1003 true /*validateIncomingUser*/);
1004 }
1005
1006 int startActivityAsUser(IApplicationThread caller, String callingPackage,
1007 Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
1008 int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId,
1009 boolean validateIncomingUser) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001010 enforceNotIsolatedCaller("startActivityAsUser");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001011
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001012 userId = getActivityStartController().checkTargetUser(userId, validateIncomingUser,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001013 Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser");
1014
1015 // TODO: Switch to user app stacks here.
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001016 return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001017 .setCaller(caller)
1018 .setCallingPackage(callingPackage)
1019 .setResolvedType(resolvedType)
1020 .setResultTo(resultTo)
1021 .setResultWho(resultWho)
1022 .setRequestCode(requestCode)
1023 .setStartFlags(startFlags)
1024 .setProfilerInfo(profilerInfo)
1025 .setActivityOptions(bOptions)
1026 .setMayWait(userId)
1027 .execute();
1028
1029 }
1030
1031 @Override
1032 public int startActivityIntentSender(IApplicationThread caller, IIntentSender target,
1033 IBinder whitelistToken, Intent fillInIntent, String resolvedType, IBinder resultTo,
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001034 String resultWho, int requestCode, int flagsMask, int flagsValues, Bundle bOptions) {
1035 enforceNotIsolatedCaller("startActivityIntentSender");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001036 // Refuse possible leaked file descriptors
1037 if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
1038 throw new IllegalArgumentException("File descriptors passed in Intent");
1039 }
1040
1041 if (!(target instanceof PendingIntentRecord)) {
1042 throw new IllegalArgumentException("Bad PendingIntent object");
1043 }
1044
1045 PendingIntentRecord pir = (PendingIntentRecord)target;
1046
1047 synchronized (mGlobalLock) {
1048 // If this is coming from the currently resumed activity, it is
1049 // effectively saying that app switches are allowed at this point.
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001050 final ActivityStack stack = getTopDisplayFocusedStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001051 if (stack.mResumedActivity != null &&
1052 stack.mResumedActivity.info.applicationInfo.uid == Binder.getCallingUid()) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001053 mAppSwitchesAllowedTime = 0;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001054 }
1055 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001056 return pir.sendInner(0, fillInIntent, resolvedType, whitelistToken, null, null,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001057 resultTo, resultWho, requestCode, flagsMask, flagsValues, bOptions);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001058 }
1059
1060 @Override
1061 public boolean startNextMatchingActivity(IBinder callingActivity, Intent intent,
1062 Bundle bOptions) {
1063 // Refuse possible leaked file descriptors
1064 if (intent != null && intent.hasFileDescriptors()) {
1065 throw new IllegalArgumentException("File descriptors passed in Intent");
1066 }
1067 SafeActivityOptions options = SafeActivityOptions.fromBundle(bOptions);
1068
1069 synchronized (mGlobalLock) {
1070 final ActivityRecord r = ActivityRecord.isInStackLocked(callingActivity);
1071 if (r == null) {
1072 SafeActivityOptions.abort(options);
1073 return false;
1074 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001075 if (!r.attachedToProcess()) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001076 // The caller is not running... d'oh!
1077 SafeActivityOptions.abort(options);
1078 return false;
1079 }
1080 intent = new Intent(intent);
1081 // The caller is not allowed to change the data.
1082 intent.setDataAndType(r.intent.getData(), r.intent.getType());
1083 // And we are resetting to find the next component...
1084 intent.setComponent(null);
1085
1086 final boolean debug = ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
1087
1088 ActivityInfo aInfo = null;
1089 try {
1090 List<ResolveInfo> resolves =
1091 AppGlobals.getPackageManager().queryIntentActivities(
1092 intent, r.resolvedType,
1093 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS,
1094 UserHandle.getCallingUserId()).getList();
1095
1096 // Look for the original activity in the list...
1097 final int N = resolves != null ? resolves.size() : 0;
1098 for (int i=0; i<N; i++) {
1099 ResolveInfo rInfo = resolves.get(i);
1100 if (rInfo.activityInfo.packageName.equals(r.packageName)
1101 && rInfo.activityInfo.name.equals(r.info.name)) {
1102 // We found the current one... the next matching is
1103 // after it.
1104 i++;
1105 if (i<N) {
1106 aInfo = resolves.get(i).activityInfo;
1107 }
1108 if (debug) {
1109 Slog.v(TAG, "Next matching activity: found current " + r.packageName
1110 + "/" + r.info.name);
1111 Slog.v(TAG, "Next matching activity: next is " + ((aInfo == null)
1112 ? "null" : aInfo.packageName + "/" + aInfo.name));
1113 }
1114 break;
1115 }
1116 }
1117 } catch (RemoteException e) {
1118 }
1119
1120 if (aInfo == null) {
1121 // Nobody who is next!
1122 SafeActivityOptions.abort(options);
1123 if (debug) Slog.d(TAG, "Next matching activity: nothing found");
1124 return false;
1125 }
1126
1127 intent.setComponent(new ComponentName(
1128 aInfo.applicationInfo.packageName, aInfo.name));
1129 intent.setFlags(intent.getFlags()&~(
1130 Intent.FLAG_ACTIVITY_FORWARD_RESULT|
1131 Intent.FLAG_ACTIVITY_CLEAR_TOP|
1132 Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
1133 FLAG_ACTIVITY_NEW_TASK));
1134
1135 // Okay now we need to start the new activity, replacing the currently running activity.
1136 // This is a little tricky because we want to start the new one as if the current one is
1137 // finished, but not finish the current one first so that there is no flicker.
1138 // And thus...
1139 final boolean wasFinishing = r.finishing;
1140 r.finishing = true;
1141
1142 // Propagate reply information over to the new activity.
1143 final ActivityRecord resultTo = r.resultTo;
1144 final String resultWho = r.resultWho;
1145 final int requestCode = r.requestCode;
1146 r.resultTo = null;
1147 if (resultTo != null) {
1148 resultTo.removeResultsLocked(r, resultWho, requestCode);
1149 }
1150
1151 final long origId = Binder.clearCallingIdentity();
1152 // TODO(b/64750076): Check if calling pid should really be -1.
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001153 final int res = getActivityStartController()
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001154 .obtainStarter(intent, "startNextMatchingActivity")
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001155 .setCaller(r.app.getThread())
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001156 .setResolvedType(r.resolvedType)
1157 .setActivityInfo(aInfo)
1158 .setResultTo(resultTo != null ? resultTo.appToken : null)
1159 .setResultWho(resultWho)
1160 .setRequestCode(requestCode)
1161 .setCallingPid(-1)
1162 .setCallingUid(r.launchedFromUid)
1163 .setCallingPackage(r.launchedFromPackage)
1164 .setRealCallingPid(-1)
1165 .setRealCallingUid(r.launchedFromUid)
1166 .setActivityOptions(options)
1167 .execute();
1168 Binder.restoreCallingIdentity(origId);
1169
1170 r.finishing = wasFinishing;
1171 if (res != ActivityManager.START_SUCCESS) {
1172 return false;
1173 }
1174 return true;
1175 }
1176 }
1177
1178 @Override
1179 public final WaitResult startActivityAndWait(IApplicationThread caller, String callingPackage,
1180 Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
1181 int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
1182 final WaitResult res = new WaitResult();
1183 synchronized (mGlobalLock) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001184 enforceNotIsolatedCaller("startActivityAndWait");
1185 userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
1186 userId, "startActivityAndWait");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001187 // TODO: Switch to user app stacks here.
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001188 getActivityStartController().obtainStarter(intent, "startActivityAndWait")
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001189 .setCaller(caller)
1190 .setCallingPackage(callingPackage)
1191 .setResolvedType(resolvedType)
1192 .setResultTo(resultTo)
1193 .setResultWho(resultWho)
1194 .setRequestCode(requestCode)
1195 .setStartFlags(startFlags)
1196 .setActivityOptions(bOptions)
1197 .setMayWait(userId)
1198 .setProfilerInfo(profilerInfo)
1199 .setWaitResult(res)
1200 .execute();
1201 }
1202 return res;
1203 }
1204
1205 @Override
1206 public final int startActivityWithConfig(IApplicationThread caller, String callingPackage,
1207 Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
1208 int startFlags, Configuration config, Bundle bOptions, int userId) {
1209 synchronized (mGlobalLock) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001210 enforceNotIsolatedCaller("startActivityWithConfig");
1211 userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
1212 "startActivityWithConfig");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001213 // TODO: Switch to user app stacks here.
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001214 return getActivityStartController().obtainStarter(intent, "startActivityWithConfig")
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001215 .setCaller(caller)
1216 .setCallingPackage(callingPackage)
1217 .setResolvedType(resolvedType)
1218 .setResultTo(resultTo)
1219 .setResultWho(resultWho)
1220 .setRequestCode(requestCode)
1221 .setStartFlags(startFlags)
1222 .setGlobalConfiguration(config)
1223 .setActivityOptions(bOptions)
1224 .setMayWait(userId)
1225 .execute();
1226 }
1227 }
1228
Alison Cichowlas3e340502018-08-07 17:15:01 -04001229
1230 @Override
1231 public IBinder requestStartActivityPermissionToken(IBinder delegatorToken) {
1232 int callingUid = Binder.getCallingUid();
1233 if (UserHandle.getAppId(callingUid) != SYSTEM_UID) {
1234 throw new SecurityException("Only the system process can request a permission token, "
1235 + "received request from uid: " + callingUid);
1236 }
1237 IBinder permissionToken = new Binder();
1238 synchronized (mGlobalLock) {
1239 mStartActivitySources.put(permissionToken, delegatorToken);
1240 }
1241
1242 Message expireMsg = PooledLambda.obtainMessage(
1243 ActivityTaskManagerService::expireStartAsCallerTokenMsg, this, permissionToken);
1244 mUiHandler.sendMessageDelayed(expireMsg, START_AS_CALLER_TOKEN_TIMEOUT_IMPL);
1245
1246 Message forgetMsg = PooledLambda.obtainMessage(
1247 ActivityTaskManagerService::forgetStartAsCallerTokenMsg, this, permissionToken);
1248 mUiHandler.sendMessageDelayed(forgetMsg, START_AS_CALLER_TOKEN_EXPIRED_TIMEOUT);
1249
1250 return permissionToken;
1251 }
1252
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001253 @Override
1254 public final int startActivityAsCaller(IApplicationThread caller, String callingPackage,
1255 Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
Alison Cichowlas3e340502018-08-07 17:15:01 -04001256 int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, IBinder permissionToken,
1257 boolean ignoreTargetSecurity, int userId) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001258 // This is very dangerous -- it allows you to perform a start activity (including
Alison Cichowlas3e340502018-08-07 17:15:01 -04001259 // permission grants) as any app that may launch one of your own activities. So we only
1260 // allow this in two cases:
1261 // 1) The caller is an activity that is part of the core framework, and then only when it
1262 // is running as the system.
1263 // 2) The caller provides a valid permissionToken. Permission tokens are one-time use and
1264 // can only be requested by a system activity, which may then delegate this call to
1265 // another app.
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001266 final ActivityRecord sourceRecord;
1267 final int targetUid;
1268 final String targetPackage;
1269 final boolean isResolver;
1270 synchronized (mGlobalLock) {
1271 if (resultTo == null) {
1272 throw new SecurityException("Must be called from an activity");
1273 }
Alison Cichowlas3e340502018-08-07 17:15:01 -04001274 final IBinder sourceToken;
1275 if (permissionToken != null) {
1276 // To even attempt to use a permissionToken, an app must also have this signature
1277 // permission.
1278 mAmInternal.enforceCallingPermission(
1279 android.Manifest.permission.START_ACTIVITY_AS_CALLER,
1280 "startActivityAsCaller");
1281 // If called with a permissionToken, we want the sourceRecord from the delegator
1282 // activity that requested this token.
1283 sourceToken = mStartActivitySources.remove(permissionToken);
1284 if (sourceToken == null) {
1285 // Invalid permissionToken, check if it recently expired.
1286 if (mExpiredStartAsCallerTokens.contains(permissionToken)) {
1287 throw new SecurityException("Called with expired permission token: "
1288 + permissionToken);
1289 } else {
1290 throw new SecurityException("Called with invalid permission token: "
1291 + permissionToken);
1292 }
1293 }
1294 } else {
1295 // This method was called directly by the source.
1296 sourceToken = resultTo;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001297 }
Alison Cichowlas3e340502018-08-07 17:15:01 -04001298
Wale Ogunwaled32da472018-11-16 07:19:28 -08001299 sourceRecord = mRootActivityContainer.isInAnyStack(sourceToken);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001300 if (sourceRecord == null) {
1301 throw new SecurityException("Called with bad activity token: " + sourceToken);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001302 }
1303 if (sourceRecord.app == null) {
1304 throw new SecurityException("Called without a process attached to activity");
1305 }
Alison Cichowlas3e340502018-08-07 17:15:01 -04001306
1307 // Whether called directly or from a delegate, the source activity must be from the
1308 // android package.
1309 if (!sourceRecord.info.packageName.equals("android")) {
1310 throw new SecurityException("Must be called from an activity that is "
1311 + "declared in the android package");
1312 }
1313
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001314 if (UserHandle.getAppId(sourceRecord.app.mUid) != SYSTEM_UID) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001315 // This is still okay, as long as this activity is running under the
1316 // uid of the original calling activity.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001317 if (sourceRecord.app.mUid != sourceRecord.launchedFromUid) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001318 throw new SecurityException(
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001319 "Calling activity in uid " + sourceRecord.app.mUid
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001320 + " must be system uid or original calling uid "
1321 + sourceRecord.launchedFromUid);
1322 }
1323 }
1324 if (ignoreTargetSecurity) {
1325 if (intent.getComponent() == null) {
1326 throw new SecurityException(
1327 "Component must be specified with ignoreTargetSecurity");
1328 }
1329 if (intent.getSelector() != null) {
1330 throw new SecurityException(
1331 "Selector not allowed with ignoreTargetSecurity");
1332 }
1333 }
1334 targetUid = sourceRecord.launchedFromUid;
1335 targetPackage = sourceRecord.launchedFromPackage;
1336 isResolver = sourceRecord.isResolverOrChildActivity();
1337 }
1338
1339 if (userId == UserHandle.USER_NULL) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001340 userId = UserHandle.getUserId(sourceRecord.app.mUid);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001341 }
1342
1343 // TODO: Switch to user app stacks here.
1344 try {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001345 return getActivityStartController().obtainStarter(intent, "startActivityAsCaller")
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001346 .setCallingUid(targetUid)
1347 .setCallingPackage(targetPackage)
1348 .setResolvedType(resolvedType)
1349 .setResultTo(resultTo)
1350 .setResultWho(resultWho)
1351 .setRequestCode(requestCode)
1352 .setStartFlags(startFlags)
1353 .setActivityOptions(bOptions)
1354 .setMayWait(userId)
1355 .setIgnoreTargetSecurity(ignoreTargetSecurity)
1356 .setFilterCallingUid(isResolver ? 0 /* system */ : targetUid)
1357 .execute();
1358 } catch (SecurityException e) {
1359 // XXX need to figure out how to propagate to original app.
1360 // A SecurityException here is generally actually a fault of the original
1361 // calling activity (such as a fairly granting permissions), so propagate it
1362 // back to them.
1363 /*
1364 StringBuilder msg = new StringBuilder();
1365 msg.append("While launching");
1366 msg.append(intent.toString());
1367 msg.append(": ");
1368 msg.append(e.getMessage());
1369 */
1370 throw e;
1371 }
1372 }
1373
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001374 int handleIncomingUser(int callingPid, int callingUid, int userId, String name) {
1375 return mAmInternal.handleIncomingUser(callingPid, callingUid, userId, false /* allowAll */,
1376 ALLOW_FULL_ONLY, name, null /* callerPackage */);
1377 }
1378
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001379 @Override
1380 public int startVoiceActivity(String callingPackage, int callingPid, int callingUid,
1381 Intent intent, String resolvedType, IVoiceInteractionSession session,
1382 IVoiceInteractor interactor, int startFlags, ProfilerInfo profilerInfo,
1383 Bundle bOptions, int userId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001384 mAmInternal.enforceCallingPermission(BIND_VOICE_INTERACTION, "startVoiceActivity()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001385 if (session == null || interactor == null) {
1386 throw new NullPointerException("null session or interactor");
1387 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001388 userId = handleIncomingUser(callingPid, callingUid, userId, "startVoiceActivity");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001389 // TODO: Switch to user app stacks here.
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001390 return getActivityStartController().obtainStarter(intent, "startVoiceActivity")
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001391 .setCallingUid(callingUid)
1392 .setCallingPackage(callingPackage)
1393 .setResolvedType(resolvedType)
1394 .setVoiceSession(session)
1395 .setVoiceInteractor(interactor)
1396 .setStartFlags(startFlags)
1397 .setProfilerInfo(profilerInfo)
1398 .setActivityOptions(bOptions)
1399 .setMayWait(userId)
Michal Karpinski85fa2022019-02-08 12:05:09 +00001400 .setAllowBackgroundActivityStart(true)
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001401 .execute();
1402 }
1403
1404 @Override
1405 public int startAssistantActivity(String callingPackage, int callingPid, int callingUid,
1406 Intent intent, String resolvedType, Bundle bOptions, int userId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001407 mAmInternal.enforceCallingPermission(BIND_VOICE_INTERACTION, "startAssistantActivity()");
1408 userId = handleIncomingUser(callingPid, callingUid, userId, "startAssistantActivity");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001409
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001410 return getActivityStartController().obtainStarter(intent, "startAssistantActivity")
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001411 .setCallingUid(callingUid)
1412 .setCallingPackage(callingPackage)
1413 .setResolvedType(resolvedType)
1414 .setActivityOptions(bOptions)
1415 .setMayWait(userId)
Michal Karpinski85fa2022019-02-08 12:05:09 +00001416 .setAllowBackgroundActivityStart(true)
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001417 .execute();
1418 }
1419
1420 @Override
1421 public void startRecentsActivity(Intent intent, IAssistDataReceiver assistDataReceiver,
1422 IRecentsAnimationRunner recentsAnimationRunner) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001423 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "startRecentsActivity()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001424 final int callingPid = Binder.getCallingPid();
1425 final long origId = Binder.clearCallingIdentity();
1426 try {
1427 synchronized (mGlobalLock) {
Wale Ogunwale16e505a2018-05-07 15:00:49 -07001428 final ComponentName recentsComponent = mRecentTasks.getRecentsComponent();
1429 final int recentsUid = mRecentTasks.getRecentsComponentUid();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001430
1431 // Start a new recents animation
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001432 final RecentsAnimation anim = new RecentsAnimation(this, mStackSupervisor,
1433 getActivityStartController(), mWindowManager, callingPid);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001434 anim.startRecentsActivity(intent, recentsAnimationRunner, recentsComponent,
1435 recentsUid, assistDataReceiver);
1436 }
1437 } finally {
1438 Binder.restoreCallingIdentity(origId);
1439 }
1440 }
1441
1442 @Override
1443 public final int startActivityFromRecents(int taskId, Bundle bOptions) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001444 enforceCallerIsRecentsOrHasPermission(START_TASKS_FROM_RECENTS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001445 "startActivityFromRecents()");
1446
1447 final int callingPid = Binder.getCallingPid();
1448 final int callingUid = Binder.getCallingUid();
1449 final SafeActivityOptions safeOptions = SafeActivityOptions.fromBundle(bOptions);
1450 final long origId = Binder.clearCallingIdentity();
1451 try {
1452 synchronized (mGlobalLock) {
1453 return mStackSupervisor.startActivityFromRecents(callingPid, callingUid, taskId,
1454 safeOptions);
1455 }
1456 } finally {
1457 Binder.restoreCallingIdentity(origId);
1458 }
1459 }
1460
1461 /**
Andrii Kulian2eb84b22018-12-13 18:18:54 -08001462 * Public API to check if the client is allowed to start an activity on specified display.
1463 *
1464 * If the target display is private or virtual, some restrictions will apply.
1465 *
1466 * @param displayId Target display id.
1467 * @param intent Intent used to launch the activity.
1468 * @param resolvedType The MIME type of the intent.
1469 * @param userId The id of the user for whom the call is made.
1470 * @return {@code true} if a call to start an activity on the target display should succeed and
1471 * no {@link SecurityException} will be thrown, {@code false} otherwise.
1472 */
1473 @Override
1474 public final boolean isActivityStartAllowedOnDisplay(int displayId, Intent intent,
1475 String resolvedType, int userId) {
1476 final int callingUid = Binder.getCallingUid();
1477 final int callingPid = Binder.getCallingPid();
1478 final long origId = Binder.clearCallingIdentity();
1479
1480 try {
1481 // Collect information about the target of the Intent.
1482 ActivityInfo aInfo = mStackSupervisor.resolveActivity(intent, resolvedType,
1483 0 /* startFlags */, null /* profilerInfo */, userId,
1484 ActivityStarter.computeResolveFilterUid(callingUid, callingUid,
1485 UserHandle.USER_NULL));
1486 aInfo = mAmInternal.getActivityInfoForUser(aInfo, userId);
1487
1488 synchronized (mGlobalLock) {
1489 return mStackSupervisor.canPlaceEntityOnDisplay(displayId, callingPid, callingUid,
1490 aInfo);
1491 }
1492 } finally {
1493 Binder.restoreCallingIdentity(origId);
1494 }
1495 }
1496
1497 /**
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001498 * This is the internal entry point for handling Activity.finish().
1499 *
1500 * @param token The Binder token referencing the Activity we want to finish.
1501 * @param resultCode Result code, if any, from this Activity.
1502 * @param resultData Result data (Intent), if any, from this Activity.
1503 * @param finishTask Whether to finish the task associated with this Activity.
1504 *
1505 * @return Returns true if the activity successfully finished, or false if it is still running.
1506 */
1507 @Override
1508 public final boolean finishActivity(IBinder token, int resultCode, Intent resultData,
1509 int finishTask) {
1510 // Refuse possible leaked file descriptors
1511 if (resultData != null && resultData.hasFileDescriptors()) {
1512 throw new IllegalArgumentException("File descriptors passed in Intent");
1513 }
1514
1515 synchronized (mGlobalLock) {
1516 ActivityRecord r = ActivityRecord.isInStackLocked(token);
1517 if (r == null) {
1518 return true;
1519 }
1520 // Keep track of the root activity of the task before we finish it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001521 final TaskRecord tr = r.getTaskRecord();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001522 ActivityRecord rootR = tr.getRootActivity();
1523 if (rootR == null) {
1524 Slog.w(TAG, "Finishing task with all activities already finished");
1525 }
1526 // Do not allow task to finish if last task in lockTask mode. Launchable priv-apps can
1527 // finish.
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07001528 if (getLockTaskController().activityBlockedFromFinish(r)) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001529 return false;
1530 }
1531
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001532 // TODO: There is a dup. of this block of code in ActivityStack.navigateUpToLocked
1533 // We should consolidate.
1534 if (mController != null) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001535 // Find the first activity that is not finishing.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001536 final ActivityRecord next = r.getActivityStack().topRunningActivityLocked(token, 0);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001537 if (next != null) {
1538 // ask watcher if this is allowed
1539 boolean resumeOK = true;
1540 try {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001541 resumeOK = mController.activityResuming(next.packageName);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001542 } catch (RemoteException e) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001543 mController = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001544 Watchdog.getInstance().setActivityController(null);
1545 }
1546
1547 if (!resumeOK) {
1548 Slog.i(TAG, "Not finishing activity because controller resumed");
1549 return false;
1550 }
1551 }
1552 }
1553 final long origId = Binder.clearCallingIdentity();
1554 try {
1555 boolean res;
1556 final boolean finishWithRootActivity =
1557 finishTask == Activity.FINISH_TASK_WITH_ROOT_ACTIVITY;
1558 if (finishTask == Activity.FINISH_TASK_WITH_ACTIVITY
1559 || (finishWithRootActivity && r == rootR)) {
1560 // If requested, remove the task that is associated to this activity only if it
1561 // was the root activity in the task. The result code and data is ignored
1562 // because we don't support returning them across task boundaries. Also, to
1563 // keep backwards compatibility we remove the task from recents when finishing
1564 // task with root activity.
1565 res = mStackSupervisor.removeTaskByIdLocked(tr.taskId, false,
1566 finishWithRootActivity, "finish-activity");
1567 if (!res) {
1568 Slog.i(TAG, "Removing task failed to finish activity");
1569 }
Garfield Tan2746ab52018-07-25 12:33:01 -07001570 // Explicitly dismissing the activity so reset its relaunch flag.
Wale Ogunwale64258362018-10-16 15:13:37 -07001571 r.mRelaunchReason = RELAUNCH_REASON_NONE;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001572 } else {
1573 res = tr.getStack().requestFinishActivityLocked(token, resultCode,
1574 resultData, "app-request", true);
1575 if (!res) {
1576 Slog.i(TAG, "Failed to finish by app-request");
1577 }
1578 }
1579 return res;
1580 } finally {
1581 Binder.restoreCallingIdentity(origId);
1582 }
1583 }
1584 }
1585
1586 @Override
1587 public boolean finishActivityAffinity(IBinder token) {
1588 synchronized (mGlobalLock) {
1589 final long origId = Binder.clearCallingIdentity();
1590 try {
1591 ActivityRecord r = ActivityRecord.isInStackLocked(token);
1592 if (r == null) {
1593 return false;
1594 }
1595
1596 // Do not allow task to finish if last task in lockTask mode. Launchable priv-apps
1597 // can finish.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001598 final TaskRecord task = r.getTaskRecord();
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07001599 if (getLockTaskController().activityBlockedFromFinish(r)) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001600 return false;
1601 }
1602 return task.getStack().finishActivityAffinityLocked(r);
1603 } finally {
1604 Binder.restoreCallingIdentity(origId);
1605 }
1606 }
1607 }
1608
1609 @Override
1610 public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) {
1611 final long origId = Binder.clearCallingIdentity();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001612 try {
1613 WindowProcessController proc = null;
1614 synchronized (mGlobalLock) {
1615 ActivityStack stack = ActivityRecord.getStackLocked(token);
1616 if (stack == null) {
1617 return;
1618 }
1619 final ActivityRecord r = mStackSupervisor.activityIdleInternalLocked(token,
1620 false /* fromTimeout */, false /* processPausingActivities */, config);
1621 if (r != null) {
1622 proc = r.app;
1623 }
1624 if (stopProfiling && proc != null) {
1625 proc.clearProfilerIfNeeded();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001626 }
1627 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001628 } finally {
1629 Binder.restoreCallingIdentity(origId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001630 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001631 }
1632
1633 @Override
1634 public final void activityResumed(IBinder token) {
1635 final long origId = Binder.clearCallingIdentity();
1636 synchronized (mGlobalLock) {
1637 ActivityRecord.activityResumedLocked(token);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001638 mWindowManager.notifyAppResumedFinished(token);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001639 }
1640 Binder.restoreCallingIdentity(origId);
1641 }
1642
1643 @Override
Andrii Kulian86e70fc2019-02-12 11:04:10 +00001644 public final void activityTopResumedStateLost() {
1645 final long origId = Binder.clearCallingIdentity();
1646 synchronized (mGlobalLock) {
1647 mStackSupervisor.handleTopResumedStateReleased(false /* timeout */);
1648 }
1649 Binder.restoreCallingIdentity(origId);
1650 }
1651
1652 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001653 public final void activityPaused(IBinder token) {
1654 final long origId = Binder.clearCallingIdentity();
1655 synchronized (mGlobalLock) {
1656 ActivityStack stack = ActivityRecord.getStackLocked(token);
1657 if (stack != null) {
1658 stack.activityPausedLocked(token, false);
1659 }
1660 }
1661 Binder.restoreCallingIdentity(origId);
1662 }
1663
1664 @Override
1665 public final void activityStopped(IBinder token, Bundle icicle,
1666 PersistableBundle persistentState, CharSequence description) {
1667 if (DEBUG_ALL) Slog.v(TAG, "Activity stopped: token=" + token);
1668
1669 // Refuse possible leaked file descriptors
1670 if (icicle != null && icicle.hasFileDescriptors()) {
1671 throw new IllegalArgumentException("File descriptors passed in Bundle");
1672 }
1673
1674 final long origId = Binder.clearCallingIdentity();
1675
Riddle Hsu7b766fd2019-01-28 21:14:59 +08001676 String restartingName = null;
1677 int restartingUid = 0;
1678 final ActivityRecord r;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001679 synchronized (mGlobalLock) {
Riddle Hsu7b766fd2019-01-28 21:14:59 +08001680 r = ActivityRecord.isInStackLocked(token);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001681 if (r != null) {
Riddle Hsu7b766fd2019-01-28 21:14:59 +08001682 if (r.attachedToProcess()
1683 && r.isState(ActivityStack.ActivityState.RESTARTING_PROCESS)) {
1684 // The activity was requested to restart from
1685 // {@link #restartActivityProcessIfVisible}.
1686 restartingName = r.app.mName;
1687 restartingUid = r.app.mUid;
1688 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001689 r.activityStoppedLocked(icicle, persistentState, description);
1690 }
1691 }
1692
Riddle Hsu7b766fd2019-01-28 21:14:59 +08001693 if (restartingName != null) {
1694 // In order to let the foreground activity can be restarted with its saved state from
1695 // {@link android.app.Activity#onSaveInstanceState}, the kill operation is postponed
1696 // until the activity reports stopped with the state. And the activity record will be
1697 // kept because the record state is restarting, then the activity will be restarted
1698 // immediately if it is still the top one.
1699 mStackSupervisor.removeRestartTimeouts(r);
1700 mAmInternal.killProcess(restartingName, restartingUid, "restartActivityProcess");
1701 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001702 mAmInternal.trimApplications();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001703
1704 Binder.restoreCallingIdentity(origId);
1705 }
1706
1707 @Override
1708 public final void activityDestroyed(IBinder token) {
1709 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "ACTIVITY DESTROYED: " + token);
1710 synchronized (mGlobalLock) {
1711 ActivityStack stack = ActivityRecord.getStackLocked(token);
1712 if (stack != null) {
1713 stack.activityDestroyedLocked(token, "activityDestroyed");
1714 }
1715 }
1716 }
1717
1718 @Override
1719 public final void activityRelaunched(IBinder token) {
1720 final long origId = Binder.clearCallingIdentity();
1721 synchronized (mGlobalLock) {
1722 mStackSupervisor.activityRelaunchedLocked(token);
1723 }
1724 Binder.restoreCallingIdentity(origId);
1725 }
1726
1727 public final void activitySlept(IBinder token) {
1728 if (DEBUG_ALL) Slog.v(TAG, "Activity slept: token=" + token);
1729
1730 final long origId = Binder.clearCallingIdentity();
1731
1732 synchronized (mGlobalLock) {
1733 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1734 if (r != null) {
1735 mStackSupervisor.activitySleptLocked(r);
1736 }
1737 }
1738
1739 Binder.restoreCallingIdentity(origId);
1740 }
1741
1742 @Override
1743 public void setRequestedOrientation(IBinder token, int requestedOrientation) {
1744 synchronized (mGlobalLock) {
1745 ActivityRecord r = ActivityRecord.isInStackLocked(token);
1746 if (r == null) {
1747 return;
1748 }
1749 final long origId = Binder.clearCallingIdentity();
1750 try {
1751 r.setRequestedOrientation(requestedOrientation);
1752 } finally {
1753 Binder.restoreCallingIdentity(origId);
1754 }
1755 }
1756 }
1757
1758 @Override
1759 public int getRequestedOrientation(IBinder token) {
1760 synchronized (mGlobalLock) {
1761 ActivityRecord r = ActivityRecord.isInStackLocked(token);
1762 if (r == null) {
1763 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
1764 }
Riddle Hsu0a343c32018-12-21 00:40:48 +08001765 return r.getOrientation();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001766 }
1767 }
1768
1769 @Override
1770 public void setImmersive(IBinder token, boolean immersive) {
1771 synchronized (mGlobalLock) {
1772 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1773 if (r == null) {
1774 throw new IllegalArgumentException();
1775 }
1776 r.immersive = immersive;
1777
1778 // update associated state if we're frontmost
Andrii Kulian52d255c2018-07-13 11:32:19 -07001779 if (r.isResumedActivityOnDisplay()) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001780 if (DEBUG_IMMERSIVE) Slog.d(TAG_IMMERSIVE, "Frontmost changed immersion: "+ r);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001781 applyUpdateLockStateLocked(r);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001782 }
1783 }
1784 }
1785
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001786 void applyUpdateLockStateLocked(ActivityRecord r) {
1787 // Modifications to the UpdateLock state are done on our handler, outside
1788 // the activity manager's locks. The new state is determined based on the
1789 // state *now* of the relevant activity record. The object is passed to
1790 // the handler solely for logging detail, not to be consulted/modified.
1791 final boolean nextState = r != null && r.immersive;
1792 mH.post(() -> {
1793 if (mUpdateLock.isHeld() != nextState) {
1794 if (DEBUG_IMMERSIVE) Slog.d(TAG_IMMERSIVE,
1795 "Applying new update lock state '" + nextState + "' for " + r);
1796 if (nextState) {
1797 mUpdateLock.acquire();
1798 } else {
1799 mUpdateLock.release();
1800 }
1801 }
1802 });
1803 }
1804
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001805 @Override
1806 public boolean isImmersive(IBinder token) {
1807 synchronized (mGlobalLock) {
1808 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1809 if (r == null) {
1810 throw new IllegalArgumentException();
1811 }
1812 return r.immersive;
1813 }
1814 }
1815
1816 @Override
1817 public boolean isTopActivityImmersive() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001818 enforceNotIsolatedCaller("isTopActivityImmersive");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001819 synchronized (mGlobalLock) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001820 final ActivityRecord r = getTopDisplayFocusedStack().topRunningActivityLocked();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001821 return (r != null) ? r.immersive : false;
1822 }
1823 }
1824
1825 @Override
1826 public void overridePendingTransition(IBinder token, String packageName,
1827 int enterAnim, int exitAnim) {
1828 synchronized (mGlobalLock) {
1829 ActivityRecord self = ActivityRecord.isInStackLocked(token);
1830 if (self == null) {
1831 return;
1832 }
1833
1834 final long origId = Binder.clearCallingIdentity();
1835
1836 if (self.isState(
1837 ActivityStack.ActivityState.RESUMED, ActivityStack.ActivityState.PAUSING)) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001838 self.getDisplay().mDisplayContent.mAppTransition.overridePendingAppTransition(
lumark588a3e82018-07-20 18:53:54 +08001839 packageName, enterAnim, exitAnim, null);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001840 }
1841
1842 Binder.restoreCallingIdentity(origId);
1843 }
1844 }
1845
1846 @Override
1847 public int getFrontActivityScreenCompatMode() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001848 enforceNotIsolatedCaller("getFrontActivityScreenCompatMode");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001849 synchronized (mGlobalLock) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001850 final ActivityRecord r = getTopDisplayFocusedStack().topRunningActivityLocked();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001851 if (r == null) {
1852 return ActivityManager.COMPAT_MODE_UNKNOWN;
1853 }
Wale Ogunwale53783742018-09-16 10:21:51 -07001854 return mCompatModePackages.computeCompatModeLocked(r.info.applicationInfo);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001855 }
1856 }
1857
1858 @Override
1859 public void setFrontActivityScreenCompatMode(int mode) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001860 mAmInternal.enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001861 "setFrontActivityScreenCompatMode");
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001862 ApplicationInfo ai;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001863 synchronized (mGlobalLock) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001864 final ActivityRecord r = getTopDisplayFocusedStack().topRunningActivityLocked();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001865 if (r == null) {
1866 Slog.w(TAG, "setFrontActivityScreenCompatMode failed: no top activity");
1867 return;
1868 }
1869 ai = r.info.applicationInfo;
Wale Ogunwale53783742018-09-16 10:21:51 -07001870 mCompatModePackages.setPackageScreenCompatModeLocked(ai, mode);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001871 }
1872 }
1873
1874 @Override
1875 public int getLaunchedFromUid(IBinder activityToken) {
1876 ActivityRecord srec;
1877 synchronized (mGlobalLock) {
1878 srec = ActivityRecord.forTokenLocked(activityToken);
1879 }
1880 if (srec == null) {
1881 return -1;
1882 }
1883 return srec.launchedFromUid;
1884 }
1885
1886 @Override
1887 public String getLaunchedFromPackage(IBinder activityToken) {
1888 ActivityRecord srec;
1889 synchronized (mGlobalLock) {
1890 srec = ActivityRecord.forTokenLocked(activityToken);
1891 }
1892 if (srec == null) {
1893 return null;
1894 }
1895 return srec.launchedFromPackage;
1896 }
1897
1898 @Override
1899 public boolean convertFromTranslucent(IBinder token) {
1900 final long origId = Binder.clearCallingIdentity();
1901 try {
1902 synchronized (mGlobalLock) {
1903 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1904 if (r == null) {
1905 return false;
1906 }
1907 final boolean translucentChanged = r.changeWindowTranslucency(true);
1908 if (translucentChanged) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001909 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001910 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001911 mWindowManager.setAppFullscreen(token, true);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001912 return translucentChanged;
1913 }
1914 } finally {
1915 Binder.restoreCallingIdentity(origId);
1916 }
1917 }
1918
1919 @Override
1920 public boolean convertToTranslucent(IBinder token, Bundle options) {
1921 SafeActivityOptions safeOptions = SafeActivityOptions.fromBundle(options);
1922 final long origId = Binder.clearCallingIdentity();
1923 try {
1924 synchronized (mGlobalLock) {
1925 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1926 if (r == null) {
1927 return false;
1928 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001929 final TaskRecord task = r.getTaskRecord();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001930 int index = task.mActivities.lastIndexOf(r);
1931 if (index > 0) {
1932 ActivityRecord under = task.mActivities.get(index - 1);
1933 under.returningOptions = safeOptions != null ? safeOptions.getOptions(r) : null;
1934 }
1935 final boolean translucentChanged = r.changeWindowTranslucency(false);
1936 if (translucentChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001937 r.getActivityStack().convertActivityToTranslucent(r);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001938 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08001939 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001940 mWindowManager.setAppFullscreen(token, false);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001941 return translucentChanged;
1942 }
1943 } finally {
1944 Binder.restoreCallingIdentity(origId);
1945 }
1946 }
1947
1948 @Override
1949 public void notifyActivityDrawn(IBinder token) {
1950 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY, "notifyActivityDrawn: token=" + token);
1951 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001952 ActivityRecord r = mRootActivityContainer.isInAnyStack(token);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001953 if (r != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001954 r.getActivityStack().notifyActivityDrawnLocked(r);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001955 }
1956 }
1957 }
1958
1959 @Override
1960 public void reportActivityFullyDrawn(IBinder token, boolean restoredFromBundle) {
1961 synchronized (mGlobalLock) {
1962 ActivityRecord r = ActivityRecord.isInStackLocked(token);
1963 if (r == null) {
1964 return;
1965 }
1966 r.reportFullyDrawnLocked(restoredFromBundle);
1967 }
1968 }
1969
1970 @Override
1971 public int getActivityDisplayId(IBinder activityToken) throws RemoteException {
1972 synchronized (mGlobalLock) {
1973 final ActivityStack stack = ActivityRecord.getStackLocked(activityToken);
1974 if (stack != null && stack.mDisplayId != INVALID_DISPLAY) {
1975 return stack.mDisplayId;
1976 }
1977 return DEFAULT_DISPLAY;
1978 }
1979 }
1980
1981 @Override
1982 public ActivityManager.StackInfo getFocusedStackInfo() throws RemoteException {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001983 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "getStackInfo()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001984 long ident = Binder.clearCallingIdentity();
1985 try {
1986 synchronized (mGlobalLock) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07001987 ActivityStack focusedStack = getTopDisplayFocusedStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001988 if (focusedStack != null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001989 return mRootActivityContainer.getStackInfo(focusedStack.mStackId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001990 }
1991 return null;
1992 }
1993 } finally {
1994 Binder.restoreCallingIdentity(ident);
1995 }
1996 }
1997
1998 @Override
1999 public void setFocusedStack(int stackId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002000 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "setFocusedStack()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002001 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS, "setFocusedStack: stackId=" + stackId);
2002 final long callingId = Binder.clearCallingIdentity();
2003 try {
2004 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002005 final ActivityStack stack = mRootActivityContainer.getStack(stackId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002006 if (stack == null) {
2007 Slog.w(TAG, "setFocusedStack: No stack with id=" + stackId);
2008 return;
2009 }
2010 final ActivityRecord r = stack.topRunningActivityLocked();
Louis Chang19443452018-10-09 12:10:21 +08002011 if (r != null && r.moveFocusableActivityToTop("setFocusedStack")) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002012 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002013 }
2014 }
2015 } finally {
2016 Binder.restoreCallingIdentity(callingId);
2017 }
2018 }
2019
2020 @Override
2021 public void setFocusedTask(int taskId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002022 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "setFocusedTask()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002023 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS, "setFocusedTask: taskId=" + taskId);
2024 final long callingId = Binder.clearCallingIdentity();
2025 try {
2026 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002027 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId,
Riddle Hsu090ac6f2018-10-31 12:55:29 +08002028 MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002029 if (task == null) {
2030 return;
2031 }
2032 final ActivityRecord r = task.topRunningActivityLocked();
Louis Chang19443452018-10-09 12:10:21 +08002033 if (r != null && r.moveFocusableActivityToTop("setFocusedTask")) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002034 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002035 }
2036 }
2037 } finally {
2038 Binder.restoreCallingIdentity(callingId);
2039 }
2040 }
2041
2042 @Override
Riddle Hsu7b766fd2019-01-28 21:14:59 +08002043 public void restartActivityProcessIfVisible(IBinder activityToken) {
2044 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "restartActivityProcess()");
2045 final long callingId = Binder.clearCallingIdentity();
2046 try {
2047 synchronized (mGlobalLock) {
2048 final ActivityRecord r = ActivityRecord.isInStackLocked(activityToken);
2049 if (r == null) {
2050 return;
2051 }
2052 r.restartProcessIfVisible();
2053 }
2054 } finally {
2055 Binder.restoreCallingIdentity(callingId);
2056 }
2057 }
2058
2059 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002060 public boolean removeTask(int taskId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002061 enforceCallerIsRecentsOrHasPermission(REMOVE_TASKS, "removeTask()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002062 synchronized (mGlobalLock) {
2063 final long ident = Binder.clearCallingIdentity();
2064 try {
2065 return mStackSupervisor.removeTaskByIdLocked(taskId, true, REMOVE_FROM_RECENTS,
2066 "remove-task");
2067 } finally {
2068 Binder.restoreCallingIdentity(ident);
2069 }
2070 }
2071 }
2072
2073 @Override
Winson Chunge6439102018-07-30 15:48:01 -07002074 public void removeAllVisibleRecentTasks() {
2075 enforceCallerIsRecentsOrHasPermission(REMOVE_TASKS, "removeAllVisibleRecentTasks()");
2076 synchronized (mGlobalLock) {
2077 final long ident = Binder.clearCallingIdentity();
2078 try {
2079 getRecentTasks().removeAllVisibleTasks();
2080 } finally {
2081 Binder.restoreCallingIdentity(ident);
2082 }
2083 }
2084 }
2085
2086 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002087 public boolean shouldUpRecreateTask(IBinder token, String destAffinity) {
2088 synchronized (mGlobalLock) {
2089 final ActivityRecord srec = ActivityRecord.forTokenLocked(token);
2090 if (srec != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002091 return srec.getActivityStack().shouldUpRecreateTaskLocked(srec, destAffinity);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002092 }
2093 }
2094 return false;
2095 }
2096
2097 @Override
2098 public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode,
2099 Intent resultData) {
2100
2101 synchronized (mGlobalLock) {
2102 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
2103 if (r != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002104 return r.getActivityStack().navigateUpToLocked(
2105 r, destIntent, resultCode, resultData);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002106 }
2107 return false;
2108 }
2109 }
2110
2111 /**
2112 * Attempts to move a task backwards in z-order (the order of activities within the task is
2113 * unchanged).
2114 *
2115 * There are several possible results of this call:
2116 * - if the task is locked, then we will show the lock toast
2117 * - if there is a task behind the provided task, then that task is made visible and resumed as
2118 * this task is moved to the back
2119 * - otherwise, if there are no other tasks in the stack:
2120 * - if this task is in the pinned stack, then we remove the stack completely, which will
2121 * have the effect of moving the task to the top or bottom of the fullscreen stack
2122 * (depending on whether it is visible)
2123 * - otherwise, we simply return home and hide this task
2124 *
2125 * @param token A reference to the activity we wish to move
2126 * @param nonRoot If false then this only works if the activity is the root
2127 * of a task; if true it will work for any activity in a task.
2128 * @return Returns true if the move completed, false if not.
2129 */
2130 @Override
2131 public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002132 enforceNotIsolatedCaller("moveActivityTaskToBack");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002133 synchronized (mGlobalLock) {
2134 final long origId = Binder.clearCallingIdentity();
2135 try {
2136 int taskId = ActivityRecord.getTaskForActivityLocked(token, !nonRoot);
Wale Ogunwaled32da472018-11-16 07:19:28 -08002137 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002138 if (task != null) {
2139 return ActivityRecord.getStackLocked(token).moveTaskToBackLocked(taskId);
2140 }
2141 } finally {
2142 Binder.restoreCallingIdentity(origId);
2143 }
2144 }
2145 return false;
2146 }
2147
2148 @Override
2149 public Rect getTaskBounds(int taskId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002150 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "getTaskBounds()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002151 long ident = Binder.clearCallingIdentity();
2152 Rect rect = new Rect();
2153 try {
2154 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002155 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002156 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
2157 if (task == null) {
2158 Slog.w(TAG, "getTaskBounds: taskId=" + taskId + " not found");
2159 return rect;
2160 }
2161 if (task.getStack() != null) {
2162 // Return the bounds from window manager since it will be adjusted for various
2163 // things like the presense of a docked stack for tasks that aren't resizeable.
2164 task.getWindowContainerBounds(rect);
2165 } else {
2166 // Task isn't in window manager yet since it isn't associated with a stack.
2167 // Return the persist value from activity manager
2168 if (!task.matchParentBounds()) {
2169 rect.set(task.getBounds());
2170 } else if (task.mLastNonFullscreenBounds != null) {
2171 rect.set(task.mLastNonFullscreenBounds);
2172 }
2173 }
2174 }
2175 } finally {
2176 Binder.restoreCallingIdentity(ident);
2177 }
2178 return rect;
2179 }
2180
2181 @Override
2182 public ActivityManager.TaskDescription getTaskDescription(int id) {
2183 synchronized (mGlobalLock) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002184 enforceCallerIsRecentsOrHasPermission(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002185 MANAGE_ACTIVITY_STACKS, "getTaskDescription()");
Wale Ogunwaled32da472018-11-16 07:19:28 -08002186 final TaskRecord tr = mRootActivityContainer.anyTaskForId(id,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002187 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
2188 if (tr != null) {
2189 return tr.lastTaskDescription;
2190 }
2191 }
2192 return null;
2193 }
2194
2195 @Override
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002196 public void setTaskWindowingMode(int taskId, int windowingMode, boolean toTop) {
2197 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
2198 setTaskWindowingModeSplitScreenPrimary(taskId, SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT,
2199 toTop, ANIMATE, null /* initialBounds */, true /* showRecents */);
2200 return;
2201 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002202 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "setTaskWindowingMode()");
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002203 synchronized (mGlobalLock) {
2204 final long ident = Binder.clearCallingIdentity();
2205 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002206 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId,
Riddle Hsu090ac6f2018-10-31 12:55:29 +08002207 MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002208 if (task == null) {
2209 Slog.w(TAG, "setTaskWindowingMode: No task for id=" + taskId);
2210 return;
2211 }
2212
2213 if (DEBUG_STACK) Slog.d(TAG_STACK, "setTaskWindowingMode: moving task=" + taskId
2214 + " to windowingMode=" + windowingMode + " toTop=" + toTop);
2215
2216 if (!task.isActivityTypeStandardOrUndefined()) {
2217 throw new IllegalArgumentException("setTaskWindowingMode: Attempt to move"
2218 + " non-standard task " + taskId + " to windowing mode="
2219 + windowingMode);
2220 }
2221
2222 final ActivityStack stack = task.getStack();
2223 if (toTop) {
2224 stack.moveToFront("setTaskWindowingMode", task);
2225 }
2226 stack.setWindowingMode(windowingMode);
2227 } finally {
2228 Binder.restoreCallingIdentity(ident);
2229 }
2230 }
2231 }
2232
2233 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002234 public String getCallingPackage(IBinder token) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002235 synchronized (mGlobalLock) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002236 ActivityRecord r = getCallingRecordLocked(token);
2237 return r != null ? r.info.packageName : null;
2238 }
2239 }
2240
2241 @Override
2242 public ComponentName getCallingActivity(IBinder token) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002243 synchronized (mGlobalLock) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002244 ActivityRecord r = getCallingRecordLocked(token);
2245 return r != null ? r.intent.getComponent() : null;
2246 }
2247 }
2248
2249 private ActivityRecord getCallingRecordLocked(IBinder token) {
2250 ActivityRecord r = ActivityRecord.isInStackLocked(token);
2251 if (r == null) {
2252 return null;
2253 }
2254 return r.resultTo;
2255 }
2256
2257 @Override
2258 public void unhandledBack() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002259 mAmInternal.enforceCallingPermission(android.Manifest.permission.FORCE_BACK, "unhandledBack()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002260
2261 synchronized (mGlobalLock) {
2262 final long origId = Binder.clearCallingIdentity();
2263 try {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002264 getTopDisplayFocusedStack().unhandledBackLocked();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002265 } finally {
2266 Binder.restoreCallingIdentity(origId);
2267 }
2268 }
2269 }
2270
2271 /**
2272 * TODO: Add mController hook
2273 */
2274 @Override
2275 public void moveTaskToFront(int taskId, int flags, Bundle bOptions) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002276 mAmInternal.enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, "moveTaskToFront()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002277
2278 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveTaskToFront: moving taskId=" + taskId);
2279 synchronized (mGlobalLock) {
2280 moveTaskToFrontLocked(taskId, flags, SafeActivityOptions.fromBundle(bOptions),
2281 false /* fromRecents */);
2282 }
2283 }
2284
2285 void moveTaskToFrontLocked(int taskId, int flags, SafeActivityOptions options,
2286 boolean fromRecents) {
2287
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002288 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002289 Binder.getCallingUid(), -1, -1, "Task to front")) {
2290 SafeActivityOptions.abort(options);
2291 return;
2292 }
2293 final long origId = Binder.clearCallingIdentity();
2294 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002295 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002296 if (task == null) {
2297 Slog.d(TAG, "Could not find task for id: "+ taskId);
Winson Chungd0243682018-09-25 18:11:54 -07002298 SafeActivityOptions.abort(options);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002299 return;
2300 }
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002301 if (getLockTaskController().isLockTaskModeViolation(task)) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002302 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
Winson Chungd0243682018-09-25 18:11:54 -07002303 SafeActivityOptions.abort(options);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002304 return;
2305 }
2306 ActivityOptions realOptions = options != null
2307 ? options.getOptions(mStackSupervisor)
2308 : null;
2309 mStackSupervisor.findTaskToMoveToFront(task, flags, realOptions, "moveTaskToFront",
2310 false /* forceNonResizable */);
2311
2312 final ActivityRecord topActivity = task.getTopActivity();
2313 if (topActivity != null) {
2314
2315 // We are reshowing a task, use a starting window to hide the initial draw delay
2316 // so the transition can start earlier.
2317 topActivity.showStartingWindow(null /* prev */, false /* newTask */,
2318 true /* taskSwitch */, fromRecents);
2319 }
2320 } finally {
2321 Binder.restoreCallingIdentity(origId);
2322 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002323 }
2324
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002325 boolean checkAppSwitchAllowedLocked(int sourcePid, int sourceUid,
2326 int callingPid, int callingUid, String name) {
2327 if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
2328 return true;
2329 }
2330
2331 if (getRecentTasks().isCallerRecents(sourceUid)) {
2332 return true;
2333 }
2334
2335 int perm = checkComponentPermission(STOP_APP_SWITCHES, sourcePid, sourceUid, -1, true);
2336 if (perm == PackageManager.PERMISSION_GRANTED) {
2337 return true;
2338 }
2339 if (checkAllowAppSwitchUid(sourceUid)) {
2340 return true;
2341 }
2342
2343 // If the actual IPC caller is different from the logical source, then
2344 // also see if they are allowed to control app switches.
2345 if (callingUid != -1 && callingUid != sourceUid) {
2346 perm = checkComponentPermission(STOP_APP_SWITCHES, callingPid, callingUid, -1, true);
2347 if (perm == PackageManager.PERMISSION_GRANTED) {
2348 return true;
2349 }
2350 if (checkAllowAppSwitchUid(callingUid)) {
2351 return true;
2352 }
2353 }
2354
2355 Slog.w(TAG, name + " request from " + sourceUid + " stopped");
2356 return false;
2357 }
2358
2359 private boolean checkAllowAppSwitchUid(int uid) {
2360 ArrayMap<String, Integer> types = mAllowAppSwitchUids.get(UserHandle.getUserId(uid));
2361 if (types != null) {
2362 for (int i = types.size() - 1; i >= 0; i--) {
2363 if (types.valueAt(i).intValue() == uid) {
2364 return true;
2365 }
2366 }
2367 }
2368 return false;
2369 }
2370
2371 @Override
2372 public void setActivityController(IActivityController controller, boolean imAMonkey) {
2373 mAmInternal.enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
2374 "setActivityController()");
2375 synchronized (mGlobalLock) {
2376 mController = controller;
2377 mControllerIsAMonkey = imAMonkey;
2378 Watchdog.getInstance().setActivityController(controller);
2379 }
2380 }
2381
Wale Ogunwale387b34c2018-10-25 19:59:40 -07002382 public boolean isControllerAMonkey() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002383 synchronized (mGlobalLock) {
2384 return mController != null && mControllerIsAMonkey;
2385 }
2386 }
2387
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002388 @Override
2389 public int getTaskForActivity(IBinder token, boolean onlyRoot) {
2390 synchronized (mGlobalLock) {
2391 return ActivityRecord.getTaskForActivityLocked(token, onlyRoot);
2392 }
2393 }
2394
2395 @Override
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002396 public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) {
2397 return getFilteredTasks(maxNum, ACTIVITY_TYPE_UNDEFINED, WINDOWING_MODE_UNDEFINED);
2398 }
2399
2400 @Override
2401 public List<ActivityManager.RunningTaskInfo> getFilteredTasks(int maxNum,
2402 @WindowConfiguration.ActivityType int ignoreActivityType,
2403 @WindowConfiguration.WindowingMode int ignoreWindowingMode) {
2404 final int callingUid = Binder.getCallingUid();
2405 ArrayList<ActivityManager.RunningTaskInfo> list = new ArrayList<>();
2406
2407 synchronized (mGlobalLock) {
2408 if (DEBUG_ALL) Slog.v(TAG, "getTasks: max=" + maxNum);
2409
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002410 final boolean allowed = isGetTasksAllowed("getTasks", Binder.getCallingPid(),
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002411 callingUid);
Wale Ogunwaled32da472018-11-16 07:19:28 -08002412 mRootActivityContainer.getRunningTasks(maxNum, list, ignoreActivityType,
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002413 ignoreWindowingMode, callingUid, allowed);
2414 }
2415
2416 return list;
2417 }
2418
2419 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002420 public final void finishSubActivity(IBinder token, String resultWho, int requestCode) {
2421 synchronized (mGlobalLock) {
2422 final long origId = Binder.clearCallingIdentity();
2423 ActivityRecord r = ActivityRecord.isInStackLocked(token);
2424 if (r != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002425 r.getActivityStack().finishSubActivityLocked(r, resultWho, requestCode);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002426 }
2427 Binder.restoreCallingIdentity(origId);
2428 }
2429 }
2430
2431 @Override
2432 public boolean willActivityBeVisible(IBinder token) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002433 synchronized (mGlobalLock) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002434 ActivityStack stack = ActivityRecord.getStackLocked(token);
2435 if (stack != null) {
2436 return stack.willActivityBeVisibleLocked(token);
2437 }
2438 return false;
2439 }
2440 }
2441
2442 @Override
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002443 public void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002444 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "moveTaskToStack()");
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002445 synchronized (mGlobalLock) {
2446 final long ident = Binder.clearCallingIdentity();
2447 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002448 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002449 if (task == null) {
2450 Slog.w(TAG, "moveTaskToStack: No task for id=" + taskId);
2451 return;
2452 }
2453
2454 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveTaskToStack: moving task=" + taskId
2455 + " to stackId=" + stackId + " toTop=" + toTop);
2456
Wale Ogunwaled32da472018-11-16 07:19:28 -08002457 final ActivityStack stack = mRootActivityContainer.getStack(stackId);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002458 if (stack == null) {
2459 throw new IllegalStateException(
2460 "moveTaskToStack: No stack for stackId=" + stackId);
2461 }
2462 if (!stack.isActivityTypeStandardOrUndefined()) {
2463 throw new IllegalArgumentException("moveTaskToStack: Attempt to move task "
2464 + taskId + " to stack " + stackId);
2465 }
2466 if (stack.inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002467 mWindowManager.setDockedStackCreateState(
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002468 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
2469 }
2470 task.reparent(stack, toTop, REPARENT_KEEP_STACK_AT_FRONT, ANIMATE, !DEFER_RESUME,
2471 "moveTaskToStack");
2472 } finally {
2473 Binder.restoreCallingIdentity(ident);
2474 }
2475 }
2476 }
2477
2478 @Override
2479 public void resizeStack(int stackId, Rect destBounds, boolean allowResizeInDockedMode,
2480 boolean preserveWindows, boolean animate, int animationDuration) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002481 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "resizeStack()");
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002482
2483 final long ident = Binder.clearCallingIdentity();
2484 try {
2485 synchronized (mGlobalLock) {
2486 if (animate) {
Yunfan Chen279f5582018-12-12 15:24:50 -08002487 final ActivityStack stack = mRootActivityContainer.getStack(stackId);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002488 if (stack == null) {
2489 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2490 return;
2491 }
2492 if (stack.getWindowingMode() != WINDOWING_MODE_PINNED) {
2493 throw new IllegalArgumentException("Stack: " + stackId
2494 + " doesn't support animated resize.");
2495 }
2496 stack.animateResizePinnedStack(null /* sourceHintBounds */, destBounds,
2497 animationDuration, false /* fromFullscreen */);
2498 } else {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002499 final ActivityStack stack = mRootActivityContainer.getStack(stackId);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002500 if (stack == null) {
2501 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2502 return;
2503 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08002504 mRootActivityContainer.resizeStack(stack, destBounds,
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002505 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
2506 preserveWindows, allowResizeInDockedMode, !DEFER_RESUME);
2507 }
2508 }
2509 } finally {
2510 Binder.restoreCallingIdentity(ident);
2511 }
2512 }
2513
wilsonshih5c4cf522019-01-25 09:03:47 +08002514 @Override
2515 public void offsetPinnedStackBounds(int stackId, Rect compareBounds, int xOffset, int yOffset,
2516 int animationDuration) {
2517 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "offsetPinnedStackBounds()");
2518
2519 final long ident = Binder.clearCallingIdentity();
2520 try {
2521 synchronized (mGlobalLock) {
2522 if (xOffset == 0 && yOffset == 0) {
2523 return;
2524 }
2525 final ActivityStack stack = mRootActivityContainer.getStack(stackId);
2526 if (stack == null) {
2527 Slog.w(TAG, "offsetPinnedStackBounds: stackId " + stackId + " not found.");
2528 return;
2529 }
2530 if (stack.getWindowingMode() != WINDOWING_MODE_PINNED) {
2531 throw new IllegalArgumentException("Stack: " + stackId
2532 + " doesn't support animated resize.");
2533 }
2534 final Rect destBounds = new Rect();
2535 stack.getAnimationOrCurrentBounds(destBounds);
wilsonshiha6e408c2019-02-19 17:30:03 +08002536 if (destBounds.isEmpty() || !destBounds.equals(compareBounds)) {
wilsonshih5c4cf522019-01-25 09:03:47 +08002537 Slog.w(TAG, "The current stack bounds does not matched! It may be obsolete.");
2538 return;
2539 }
2540 destBounds.offset(xOffset, yOffset);
2541 stack.animateResizePinnedStack(null /* sourceHintBounds */, destBounds,
2542 animationDuration, false /* fromFullscreen */);
2543 }
2544 } finally {
2545 Binder.restoreCallingIdentity(ident);
2546 }
2547 }
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002548 /**
2549 * Moves the specified task to the primary-split-screen stack.
2550 *
2551 * @param taskId Id of task to move.
2552 * @param createMode The mode the primary split screen stack should be created in if it doesn't
2553 * exist already. See
2554 * {@link android.app.ActivityTaskManager#SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT}
2555 * and
2556 * {@link android.app.ActivityTaskManager#SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT}
2557 * @param toTop If the task and stack should be moved to the top.
2558 * @param animate Whether we should play an animation for the moving the task.
2559 * @param initialBounds If the primary stack gets created, it will use these bounds for the
2560 * stack. Pass {@code null} to use default bounds.
2561 * @param showRecents If the recents activity should be shown on the other side of the task
2562 * going into split-screen mode.
2563 */
2564 @Override
2565 public boolean setTaskWindowingModeSplitScreenPrimary(int taskId, int createMode,
2566 boolean toTop, boolean animate, Rect initialBounds, boolean showRecents) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002567 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002568 "setTaskWindowingModeSplitScreenPrimary()");
2569 synchronized (mGlobalLock) {
2570 final long ident = Binder.clearCallingIdentity();
2571 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002572 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId,
Riddle Hsu090ac6f2018-10-31 12:55:29 +08002573 MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002574 if (task == null) {
2575 Slog.w(TAG, "setTaskWindowingModeSplitScreenPrimary: No task for id=" + taskId);
2576 return false;
2577 }
2578 if (DEBUG_STACK) Slog.d(TAG_STACK,
2579 "setTaskWindowingModeSplitScreenPrimary: moving task=" + taskId
2580 + " to createMode=" + createMode + " toTop=" + toTop);
2581 if (!task.isActivityTypeStandardOrUndefined()) {
2582 throw new IllegalArgumentException("setTaskWindowingMode: Attempt to move"
2583 + " non-standard task " + taskId + " to split-screen windowing mode");
2584 }
2585
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002586 mWindowManager.setDockedStackCreateState(createMode, initialBounds);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002587 final int windowingMode = task.getWindowingMode();
2588 final ActivityStack stack = task.getStack();
2589 if (toTop) {
2590 stack.moveToFront("setTaskWindowingModeSplitScreenPrimary", task);
2591 }
2592 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, animate, showRecents,
Evan Roskyc5abbd82018-10-05 16:02:19 -07002593 false /* enteringSplitScreenMode */, false /* deferEnsuringVisibility */,
2594 false /* creating */);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002595 return windowingMode != task.getWindowingMode();
2596 } finally {
2597 Binder.restoreCallingIdentity(ident);
2598 }
2599 }
2600 }
2601
2602 /**
2603 * Removes stacks in the input windowing modes from the system if they are of activity type
2604 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2605 */
2606 @Override
2607 public void removeStacksInWindowingModes(int[] windowingModes) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002608 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002609 "removeStacksInWindowingModes()");
2610
2611 synchronized (mGlobalLock) {
2612 final long ident = Binder.clearCallingIdentity();
2613 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002614 mRootActivityContainer.removeStacksInWindowingModes(windowingModes);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002615 } finally {
2616 Binder.restoreCallingIdentity(ident);
2617 }
2618 }
2619 }
2620
2621 @Override
2622 public void removeStacksWithActivityTypes(int[] activityTypes) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002623 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002624 "removeStacksWithActivityTypes()");
2625
2626 synchronized (mGlobalLock) {
2627 final long ident = Binder.clearCallingIdentity();
2628 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002629 mRootActivityContainer.removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale65ebd952018-04-25 15:41:44 -07002630 } finally {
2631 Binder.restoreCallingIdentity(ident);
2632 }
2633 }
2634 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002635
2636 @Override
2637 public ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags,
2638 int userId) {
2639 final int callingUid = Binder.getCallingUid();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002640 userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId, "getRecentTasks");
2641 final boolean allowed = isGetTasksAllowed("getRecentTasks", Binder.getCallingPid(),
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002642 callingUid);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002643 final boolean detailed = checkGetTasksPermission(
2644 android.Manifest.permission.GET_DETAILED_TASKS, Binder.getCallingPid(),
2645 UserHandle.getAppId(callingUid))
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002646 == PackageManager.PERMISSION_GRANTED;
2647
2648 synchronized (mGlobalLock) {
Wale Ogunwale16e505a2018-05-07 15:00:49 -07002649 return mRecentTasks.getRecentTasks(maxNum, flags, allowed, detailed, userId,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002650 callingUid);
2651 }
2652 }
2653
2654 @Override
2655 public List<ActivityManager.StackInfo> getAllStackInfos() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002656 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "getAllStackInfos()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002657 long ident = Binder.clearCallingIdentity();
2658 try {
2659 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002660 return mRootActivityContainer.getAllStackInfos();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002661 }
2662 } finally {
2663 Binder.restoreCallingIdentity(ident);
2664 }
2665 }
2666
2667 @Override
2668 public ActivityManager.StackInfo getStackInfo(int windowingMode, int activityType) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002669 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "getStackInfo()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002670 long ident = Binder.clearCallingIdentity();
2671 try {
2672 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002673 return mRootActivityContainer.getStackInfo(windowingMode, activityType);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002674 }
2675 } finally {
2676 Binder.restoreCallingIdentity(ident);
2677 }
2678 }
2679
2680 @Override
2681 public void cancelRecentsAnimation(boolean restoreHomeStackPosition) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002682 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "cancelRecentsAnimation()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002683 final long callingUid = Binder.getCallingUid();
2684 final long origId = Binder.clearCallingIdentity();
2685 try {
2686 synchronized (mGlobalLock) {
2687 // Cancel the recents animation synchronously (do not hold the WM lock)
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002688 mWindowManager.cancelRecentsAnimationSynchronously(restoreHomeStackPosition
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002689 ? REORDER_MOVE_TO_ORIGINAL_POSITION
2690 : REORDER_KEEP_IN_PLACE, "cancelRecentsAnimation/uid=" + callingUid);
2691 }
2692 } finally {
2693 Binder.restoreCallingIdentity(origId);
2694 }
2695 }
2696
2697 @Override
2698 public void startLockTaskModeByToken(IBinder token) {
2699 synchronized (mGlobalLock) {
2700 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
2701 if (r == null) {
2702 return;
2703 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002704 startLockTaskModeLocked(r.getTaskRecord(), false /* isSystemCaller */);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002705 }
2706 }
2707
2708 @Override
Riddle Hsu090ac6f2018-10-31 12:55:29 +08002709 public void startSystemLockTaskMode(int taskId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002710 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "startSystemLockTaskMode");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002711 // This makes inner call to look as if it was initiated by system.
2712 long ident = Binder.clearCallingIdentity();
2713 try {
2714 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08002715 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId,
Riddle Hsu090ac6f2018-10-31 12:55:29 +08002716 MATCH_TASK_IN_STACKS_ONLY);
2717 if (task == null) {
2718 return;
2719 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002720
2721 // When starting lock task mode the stack must be in front and focused
2722 task.getStack().moveToFront("startSystemLockTaskMode");
2723 startLockTaskModeLocked(task, true /* isSystemCaller */);
2724 }
2725 } finally {
2726 Binder.restoreCallingIdentity(ident);
2727 }
2728 }
2729
2730 @Override
2731 public void stopLockTaskModeByToken(IBinder token) {
2732 synchronized (mGlobalLock) {
2733 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
2734 if (r == null) {
2735 return;
2736 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002737 stopLockTaskModeInternal(r.getTaskRecord(), false /* isSystemCaller */);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002738 }
2739 }
2740
2741 /**
2742 * This API should be called by SystemUI only when user perform certain action to dismiss
2743 * lock task mode. We should only dismiss pinned lock task mode in this case.
2744 */
2745 @Override
2746 public void stopSystemLockTaskMode() throws RemoteException {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002747 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "stopSystemLockTaskMode");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002748 stopLockTaskModeInternal(null, true /* isSystemCaller */);
2749 }
2750
2751 private void startLockTaskModeLocked(@Nullable TaskRecord task, boolean isSystemCaller) {
2752 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "startLockTaskModeLocked: " + task);
2753 if (task == null || task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
2754 return;
2755 }
2756
Wale Ogunwaled32da472018-11-16 07:19:28 -08002757 final ActivityStack stack = mRootActivityContainer.getTopDisplayFocusedStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002758 if (stack == null || task != stack.topTask()) {
2759 throw new IllegalArgumentException("Invalid task, not in foreground");
2760 }
2761
2762 // {@code isSystemCaller} is used to distinguish whether this request is initiated by the
2763 // system or a specific app.
2764 // * System-initiated requests will only start the pinned mode (screen pinning)
2765 // * App-initiated requests
2766 // - will put the device in fully locked mode (LockTask), if the app is whitelisted
2767 // - will start the pinned mode, otherwise
2768 final int callingUid = Binder.getCallingUid();
2769 long ident = Binder.clearCallingIdentity();
2770 try {
2771 // When a task is locked, dismiss the pinned stack if it exists
Wale Ogunwaled32da472018-11-16 07:19:28 -08002772 mRootActivityContainer.removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002773
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002774 getLockTaskController().startLockTaskMode(task, isSystemCaller, callingUid);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002775 } finally {
2776 Binder.restoreCallingIdentity(ident);
2777 }
2778 }
2779
2780 private void stopLockTaskModeInternal(@Nullable TaskRecord task, boolean isSystemCaller) {
2781 final int callingUid = Binder.getCallingUid();
2782 long ident = Binder.clearCallingIdentity();
2783 try {
2784 synchronized (mGlobalLock) {
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002785 getLockTaskController().stopLockTaskMode(task, isSystemCaller, callingUid);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002786 }
2787 // Launch in-call UI if a call is ongoing. This is necessary to allow stopping the lock
2788 // task and jumping straight into a call in the case of emergency call back.
2789 TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
2790 if (tm != null) {
2791 tm.showInCallScreen(false);
2792 }
2793 } finally {
2794 Binder.restoreCallingIdentity(ident);
2795 }
2796 }
2797
2798 @Override
Wale Ogunwale27c48ae2018-10-25 19:01:01 -07002799 public void updateLockTaskPackages(int userId, String[] packages) {
2800 final int callingUid = Binder.getCallingUid();
2801 if (callingUid != 0 && callingUid != SYSTEM_UID) {
2802 mAmInternal.enforceCallingPermission(Manifest.permission.UPDATE_LOCK_TASK_PACKAGES,
2803 "updateLockTaskPackages()");
2804 }
2805 synchronized (this) {
2806 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "Whitelisting " + userId + ":"
2807 + Arrays.toString(packages));
2808 getLockTaskController().updateLockTaskPackages(userId, packages);
2809 }
2810 }
2811
2812 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002813 public boolean isInLockTaskMode() {
2814 return getLockTaskModeState() != LOCK_TASK_MODE_NONE;
2815 }
2816
2817 @Override
2818 public int getLockTaskModeState() {
2819 synchronized (mGlobalLock) {
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002820 return getLockTaskController().getLockTaskModeState();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002821 }
2822 }
2823
2824 @Override
2825 public void setTaskDescription(IBinder token, ActivityManager.TaskDescription td) {
2826 synchronized (mGlobalLock) {
2827 ActivityRecord r = ActivityRecord.isInStackLocked(token);
2828 if (r != null) {
2829 r.setTaskDescription(td);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002830 final TaskRecord task = r.getTaskRecord();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002831 task.updateTaskDescription();
Mark Renoufc808f062019-02-07 15:20:37 -05002832 mTaskChangeNotificationController.notifyTaskDescriptionChanged(task.getTaskInfo());
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002833 }
2834 }
2835 }
2836
2837 @Override
2838 public Bundle getActivityOptions(IBinder token) {
2839 final long origId = Binder.clearCallingIdentity();
2840 try {
2841 synchronized (mGlobalLock) {
2842 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2843 if (r != null) {
2844 final ActivityOptions activityOptions = r.takeOptionsLocked();
2845 return activityOptions == null ? null : activityOptions.toBundle();
2846 }
2847 return null;
2848 }
2849 } finally {
2850 Binder.restoreCallingIdentity(origId);
2851 }
2852 }
2853
2854 @Override
2855 public List<IBinder> getAppTasks(String callingPackage) {
2856 int callingUid = Binder.getCallingUid();
2857 long ident = Binder.clearCallingIdentity();
2858 try {
2859 synchronized (mGlobalLock) {
Wale Ogunwale16e505a2018-05-07 15:00:49 -07002860 return mRecentTasks.getAppTasksList(callingUid, callingPackage);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002861 }
2862 } finally {
2863 Binder.restoreCallingIdentity(ident);
2864 }
2865 }
2866
2867 @Override
2868 public void finishVoiceTask(IVoiceInteractionSession session) {
2869 synchronized (mGlobalLock) {
2870 final long origId = Binder.clearCallingIdentity();
2871 try {
2872 // TODO: VI Consider treating local voice interactions and voice tasks
2873 // differently here
Wale Ogunwaled32da472018-11-16 07:19:28 -08002874 mRootActivityContainer.finishVoiceTask(session);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002875 } finally {
2876 Binder.restoreCallingIdentity(origId);
2877 }
2878 }
2879
2880 }
2881
2882 @Override
2883 public boolean isTopOfTask(IBinder token) {
2884 synchronized (mGlobalLock) {
2885 ActivityRecord r = ActivityRecord.isInStackLocked(token);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002886 return r != null && r.getTaskRecord().getTopActivity() == r;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002887 }
2888 }
2889
2890 @Override
2891 public void notifyLaunchTaskBehindComplete(IBinder token) {
2892 mStackSupervisor.scheduleLaunchTaskBehindComplete(token);
2893 }
2894
2895 @Override
2896 public void notifyEnterAnimationComplete(IBinder token) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002897 mH.post(() -> {
2898 synchronized (mGlobalLock) {
2899 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002900 if (r != null && r.attachedToProcess()) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002901 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002902 r.app.getThread().scheduleEnterAnimationComplete(r.appToken);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002903 } catch (RemoteException e) {
2904 }
2905 }
2906 }
2907
2908 });
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002909 }
2910
2911 /** Called from an app when assist data is ready. */
2912 @Override
2913 public void reportAssistContextExtras(IBinder token, Bundle extras, AssistStructure structure,
2914 AssistContent content, Uri referrer) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002915 PendingAssistExtras pae = (PendingAssistExtras) token;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002916 synchronized (pae) {
2917 pae.result = extras;
2918 pae.structure = structure;
2919 pae.content = content;
2920 if (referrer != null) {
2921 pae.extras.putParcelable(Intent.EXTRA_REFERRER, referrer);
2922 }
2923 if (structure != null) {
Winson Chung48b25652018-10-22 14:04:30 -07002924 // Pre-fill the task/activity component for all assist data receivers
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002925 structure.setTaskId(pae.activity.getTaskRecord().taskId);
2926 structure.setActivityComponent(pae.activity.mActivityComponent);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002927 structure.setHomeActivity(pae.isHome);
2928 }
2929 pae.haveResult = true;
2930 pae.notifyAll();
2931 if (pae.intent == null && pae.receiver == null) {
2932 // Caller is just waiting for the result.
2933 return;
2934 }
2935 }
2936 // We are now ready to launch the assist activity.
2937 IAssistDataReceiver sendReceiver = null;
2938 Bundle sendBundle = null;
2939 synchronized (mGlobalLock) {
2940 buildAssistBundleLocked(pae, extras);
2941 boolean exists = mPendingAssistExtras.remove(pae);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002942 mUiHandler.removeCallbacks(pae);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002943 if (!exists) {
2944 // Timed out.
2945 return;
2946 }
2947
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002948 if ((sendReceiver = pae.receiver) != null) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002949 // Caller wants result sent back to them.
2950 sendBundle = new Bundle();
2951 sendBundle.putBundle(ASSIST_KEY_DATA, pae.extras);
2952 sendBundle.putParcelable(ASSIST_KEY_STRUCTURE, pae.structure);
2953 sendBundle.putParcelable(ASSIST_KEY_CONTENT, pae.content);
2954 sendBundle.putBundle(ASSIST_KEY_RECEIVER_EXTRAS, pae.receiverExtras);
2955 }
2956 }
2957 if (sendReceiver != null) {
2958 try {
2959 sendReceiver.onHandleAssistData(sendBundle);
2960 } catch (RemoteException e) {
2961 }
2962 return;
2963 }
2964
2965 final long ident = Binder.clearCallingIdentity();
2966 try {
2967 if (TextUtils.equals(pae.intent.getAction(),
2968 android.service.voice.VoiceInteractionService.SERVICE_INTERFACE)) {
2969 pae.intent.putExtras(pae.extras);
2970 mContext.startServiceAsUser(pae.intent, new UserHandle(pae.userHandle));
2971 } else {
2972 pae.intent.replaceExtras(pae.extras);
2973 pae.intent.setFlags(FLAG_ACTIVITY_NEW_TASK
2974 | Intent.FLAG_ACTIVITY_SINGLE_TOP
2975 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Wale Ogunwale31913b52018-10-13 08:29:31 -07002976 mInternal.closeSystemDialogs("assist");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002977
2978 try {
2979 mContext.startActivityAsUser(pae.intent, new UserHandle(pae.userHandle));
2980 } catch (ActivityNotFoundException e) {
2981 Slog.w(TAG, "No activity to handle assist action.", e);
2982 }
2983 }
2984 } finally {
2985 Binder.restoreCallingIdentity(ident);
2986 }
2987 }
2988
2989 @Override
2990 public int addAppTask(IBinder activityToken, Intent intent,
2991 ActivityManager.TaskDescription description, Bitmap thumbnail) throws RemoteException {
2992 final int callingUid = Binder.getCallingUid();
2993 final long callingIdent = Binder.clearCallingIdentity();
2994
2995 try {
2996 synchronized (mGlobalLock) {
2997 ActivityRecord r = ActivityRecord.isInStackLocked(activityToken);
2998 if (r == null) {
2999 throw new IllegalArgumentException("Activity does not exist; token="
3000 + activityToken);
3001 }
3002 ComponentName comp = intent.getComponent();
3003 if (comp == null) {
3004 throw new IllegalArgumentException("Intent " + intent
3005 + " must specify explicit component");
3006 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003007 if (thumbnail.getWidth() != mThumbnailWidth
3008 || thumbnail.getHeight() != mThumbnailHeight) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003009 throw new IllegalArgumentException("Bad thumbnail size: got "
3010 + thumbnail.getWidth() + "x" + thumbnail.getHeight() + ", require "
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003011 + mThumbnailWidth + "x" + mThumbnailHeight);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003012 }
3013 if (intent.getSelector() != null) {
3014 intent.setSelector(null);
3015 }
3016 if (intent.getSourceBounds() != null) {
3017 intent.setSourceBounds(null);
3018 }
3019 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0) {
3020 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS) == 0) {
3021 // The caller has added this as an auto-remove task... that makes no
3022 // sense, so turn off auto-remove.
3023 intent.addFlags(Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
3024 }
3025 }
3026 final ActivityInfo ainfo = AppGlobals.getPackageManager().getActivityInfo(comp,
3027 STOCK_PM_FLAGS, UserHandle.getUserId(callingUid));
3028 if (ainfo.applicationInfo.uid != callingUid) {
3029 throw new SecurityException(
3030 "Can't add task for another application: target uid="
3031 + ainfo.applicationInfo.uid + ", calling uid=" + callingUid);
3032 }
3033
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003034 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003035 final TaskRecord task = stack.createTaskRecord(
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003036 mStackSupervisor.getNextTaskIdForUserLocked(r.mUserId), ainfo, intent,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003037 null /* voiceSession */, null /* voiceInteractor */, !ON_TOP);
Wale Ogunwale16e505a2018-05-07 15:00:49 -07003038 if (!mRecentTasks.addToBottom(task)) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003039 // The app has too many tasks already and we can't add any more
3040 stack.removeTask(task, "addAppTask", REMOVE_TASK_MODE_DESTROYING);
3041 return INVALID_TASK_ID;
3042 }
3043 task.lastTaskDescription.copyFrom(description);
3044
3045 // TODO: Send the thumbnail to WM to store it.
3046
3047 return task.taskId;
3048 }
3049 } finally {
3050 Binder.restoreCallingIdentity(callingIdent);
3051 }
3052 }
3053
3054 @Override
3055 public Point getAppTaskThumbnailSize() {
3056 synchronized (mGlobalLock) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003057 return new Point(mThumbnailWidth, mThumbnailHeight);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003058 }
3059 }
3060
3061 @Override
3062 public void setTaskResizeable(int taskId, int resizeableMode) {
3063 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08003064 final TaskRecord task = mRootActivityContainer.anyTaskForId(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003065 taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
3066 if (task == null) {
3067 Slog.w(TAG, "setTaskResizeable: taskId=" + taskId + " not found");
3068 return;
3069 }
3070 task.setResizeMode(resizeableMode);
3071 }
3072 }
3073
3074 @Override
3075 public void resizeTask(int taskId, Rect bounds, int resizeMode) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003076 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "resizeTask()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003077 long ident = Binder.clearCallingIdentity();
3078 try {
3079 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08003080 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId,
Riddle Hsu090ac6f2018-10-31 12:55:29 +08003081 MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003082 if (task == null) {
3083 Slog.w(TAG, "resizeTask: taskId=" + taskId + " not found");
3084 return;
3085 }
3086 // Place the task in the right stack if it isn't there already based on
3087 // the requested bounds.
3088 // The stack transition logic is:
3089 // - a null bounds on a freeform task moves that task to fullscreen
3090 // - a non-null bounds on a non-freeform (fullscreen OR docked) task moves
3091 // that task to freeform
3092 // - otherwise the task is not moved
3093 ActivityStack stack = task.getStack();
3094 if (!task.getWindowConfiguration().canResizeTask()) {
3095 throw new IllegalArgumentException("resizeTask not allowed on task=" + task);
3096 }
3097 if (bounds == null && stack.getWindowingMode() == WINDOWING_MODE_FREEFORM) {
3098 stack = stack.getDisplay().getOrCreateStack(
3099 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), ON_TOP);
3100 } else if (bounds != null && stack.getWindowingMode() != WINDOWING_MODE_FREEFORM) {
3101 stack = stack.getDisplay().getOrCreateStack(
3102 WINDOWING_MODE_FREEFORM, stack.getActivityType(), ON_TOP);
3103 }
3104
3105 // Reparent the task to the right stack if necessary
3106 boolean preserveWindow = (resizeMode & RESIZE_MODE_PRESERVE_WINDOW) != 0;
3107 if (stack != task.getStack()) {
3108 // Defer resume until the task is resized below
3109 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, ANIMATE,
3110 DEFER_RESUME, "resizeTask");
3111 preserveWindow = false;
3112 }
3113
3114 // After reparenting (which only resizes the task to the stack bounds), resize the
3115 // task to the actual bounds provided
3116 task.resize(bounds, resizeMode, preserveWindow, !DEFER_RESUME);
3117 }
3118 } finally {
3119 Binder.restoreCallingIdentity(ident);
3120 }
3121 }
3122
3123 @Override
3124 public boolean releaseActivityInstance(IBinder token) {
3125 synchronized (mGlobalLock) {
3126 final long origId = Binder.clearCallingIdentity();
3127 try {
3128 ActivityRecord r = ActivityRecord.isInStackLocked(token);
3129 if (r == null) {
3130 return false;
3131 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003132 return r.getActivityStack().safelyDestroyActivityLocked(r, "app-req");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003133 } finally {
3134 Binder.restoreCallingIdentity(origId);
3135 }
3136 }
3137 }
3138
3139 @Override
3140 public void releaseSomeActivities(IApplicationThread appInt) {
3141 synchronized (mGlobalLock) {
3142 final long origId = Binder.clearCallingIdentity();
3143 try {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07003144 final WindowProcessController app = getProcessController(appInt);
Wale Ogunwaled32da472018-11-16 07:19:28 -08003145 mRootActivityContainer.releaseSomeActivitiesLocked(app, "low-mem");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003146 } finally {
3147 Binder.restoreCallingIdentity(origId);
3148 }
3149 }
3150 }
3151
3152 @Override
wilsonshih177261f2019-02-22 12:02:18 +08003153 public void setLockScreenShown(boolean keyguardShowing, boolean aodShowing) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003154 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003155 != PackageManager.PERMISSION_GRANTED) {
3156 throw new SecurityException("Requires permission "
3157 + android.Manifest.permission.DEVICE_POWER);
3158 }
3159
3160 synchronized (mGlobalLock) {
3161 long ident = Binder.clearCallingIdentity();
3162 if (mKeyguardShown != keyguardShowing) {
3163 mKeyguardShown = keyguardShowing;
Wale Ogunwale342fbe92018-10-09 08:44:10 -07003164 final Message msg = PooledLambda.obtainMessage(
3165 ActivityManagerInternal::reportCurKeyguardUsageEvent, mAmInternal,
3166 keyguardShowing);
3167 mH.sendMessage(msg);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003168 }
3169 try {
wilsonshih177261f2019-02-22 12:02:18 +08003170 mKeyguardController.setKeyguardShown(keyguardShowing, aodShowing);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003171 } finally {
3172 Binder.restoreCallingIdentity(ident);
3173 }
3174 }
3175
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003176 mH.post(() -> {
3177 for (int i = mScreenObservers.size() - 1; i >= 0; i--) {
3178 mScreenObservers.get(i).onKeyguardStateChanged(keyguardShowing);
3179 }
3180 });
3181 }
3182
Wale Ogunwale387b34c2018-10-25 19:59:40 -07003183 public void onScreenAwakeChanged(boolean isAwake) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003184 mH.post(() -> {
3185 for (int i = mScreenObservers.size() - 1; i >= 0; i--) {
3186 mScreenObservers.get(i).onAwakeStateChanged(isAwake);
3187 }
3188 });
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003189 }
3190
3191 @Override
3192 public Bitmap getTaskDescriptionIcon(String filePath, int userId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003193 userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
3194 userId, "getTaskDescriptionIcon");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003195
3196 final File passedIconFile = new File(filePath);
3197 final File legitIconFile = new File(TaskPersister.getUserImagesDir(userId),
3198 passedIconFile.getName());
3199 if (!legitIconFile.getPath().equals(filePath)
3200 || !filePath.contains(ActivityRecord.ACTIVITY_ICON_SUFFIX)) {
3201 throw new IllegalArgumentException("Bad file path: " + filePath
3202 + " passed for userId " + userId);
3203 }
Wale Ogunwale16e505a2018-05-07 15:00:49 -07003204 return mRecentTasks.getTaskDescriptionIcon(filePath);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003205 }
3206
3207 @Override
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003208 public void startInPlaceAnimationOnFrontMostApplication(Bundle opts) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003209 final SafeActivityOptions safeOptions = SafeActivityOptions.fromBundle(opts);
3210 final ActivityOptions activityOptions = safeOptions != null
3211 ? safeOptions.getOptions(mStackSupervisor)
3212 : null;
3213 if (activityOptions == null
3214 || activityOptions.getAnimationType() != ActivityOptions.ANIM_CUSTOM_IN_PLACE
3215 || activityOptions.getCustomInPlaceResId() == 0) {
3216 throw new IllegalArgumentException("Expected in-place ActivityOption " +
3217 "with valid animation");
3218 }
lumark588a3e82018-07-20 18:53:54 +08003219 // Get top display of front most application.
3220 final ActivityStack focusedStack = getTopDisplayFocusedStack();
3221 if (focusedStack != null) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08003222 final DisplayContent dc = focusedStack.getDisplay().mDisplayContent;
3223 dc.prepareAppTransition(TRANSIT_TASK_IN_PLACE, false);
3224 dc.mAppTransition.overrideInPlaceAppTransition(activityOptions.getPackageName(),
lumark588a3e82018-07-20 18:53:54 +08003225 activityOptions.getCustomInPlaceResId());
Wale Ogunwale3a256e62018-12-06 14:41:18 -08003226 dc.executeAppTransition();
lumark588a3e82018-07-20 18:53:54 +08003227 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003228 }
3229
3230 @Override
3231 public void removeStack(int stackId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003232 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "removeStack()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003233 synchronized (mGlobalLock) {
3234 final long ident = Binder.clearCallingIdentity();
3235 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08003236 final ActivityStack stack = mRootActivityContainer.getStack(stackId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003237 if (stack == null) {
3238 Slog.w(TAG, "removeStack: No stack with id=" + stackId);
3239 return;
3240 }
3241 if (!stack.isActivityTypeStandardOrUndefined()) {
3242 throw new IllegalArgumentException(
3243 "Removing non-standard stack is not allowed.");
3244 }
3245 mStackSupervisor.removeStack(stack);
3246 } finally {
3247 Binder.restoreCallingIdentity(ident);
3248 }
3249 }
3250 }
3251
3252 @Override
3253 public void moveStackToDisplay(int stackId, int displayId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003254 mAmInternal.enforceCallingPermission(INTERNAL_SYSTEM_WINDOW, "moveStackToDisplay()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003255
3256 synchronized (mGlobalLock) {
3257 final long ident = Binder.clearCallingIdentity();
3258 try {
3259 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveStackToDisplay: moving stackId=" + stackId
3260 + " to displayId=" + displayId);
Wale Ogunwaled32da472018-11-16 07:19:28 -08003261 mRootActivityContainer.moveStackToDisplay(stackId, displayId, ON_TOP);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003262 } finally {
3263 Binder.restoreCallingIdentity(ident);
3264 }
3265 }
3266 }
3267
3268 @Override
Yunfan Chend967af82019-01-17 18:30:18 +09003269 public void toggleFreeformWindowingMode(IBinder token) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003270 synchronized (mGlobalLock) {
3271 long ident = Binder.clearCallingIdentity();
3272 try {
3273 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
3274 if (r == null) {
3275 throw new IllegalArgumentException(
Yunfan Chend967af82019-01-17 18:30:18 +09003276 "toggleFreeformWindowingMode: No activity record matching token="
3277 + token);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003278 }
3279
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003280 final ActivityStack stack = r.getActivityStack();
Yunfan Chend967af82019-01-17 18:30:18 +09003281 if (stack == null) {
3282 throw new IllegalStateException("toggleFreeformWindowingMode: the activity "
3283 + "doesn't have a stack");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003284 }
3285
Yunfan Chend967af82019-01-17 18:30:18 +09003286 if (!stack.inFreeformWindowingMode()
3287 && stack.getWindowingMode() != WINDOWING_MODE_FULLSCREEN) {
3288 throw new IllegalStateException("toggleFreeformWindowingMode: You can only "
3289 + "toggle between fullscreen and freeform.");
3290 }
3291
3292 if (stack.inFreeformWindowingMode()) {
3293 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
3294 } else {
3295 stack.setWindowingMode(WINDOWING_MODE_FREEFORM);
3296 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003297 } finally {
3298 Binder.restoreCallingIdentity(ident);
3299 }
3300 }
3301 }
3302
3303 /** Sets the task stack listener that gets callbacks when a task stack changes. */
3304 @Override
3305 public void registerTaskStackListener(ITaskStackListener listener) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003306 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003307 "registerTaskStackListener()");
Wale Ogunwaled0412b32018-05-08 09:25:50 -07003308 mTaskChangeNotificationController.registerTaskStackListener(listener);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003309 }
3310
3311 /** Unregister a task stack listener so that it stops receiving callbacks. */
3312 @Override
3313 public void unregisterTaskStackListener(ITaskStackListener listener) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003314 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003315 "unregisterTaskStackListener()");
Wale Ogunwaled0412b32018-05-08 09:25:50 -07003316 mTaskChangeNotificationController.unregisterTaskStackListener(listener);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003317 }
3318
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003319 @Override
3320 public boolean requestAssistContextExtras(int requestType, IAssistDataReceiver receiver,
3321 Bundle receiverExtras, IBinder activityToken, boolean focused, boolean newSessionId) {
3322 return enqueueAssistContext(requestType, null, null, receiver, receiverExtras,
3323 activityToken, focused, newSessionId, UserHandle.getCallingUserId(), null,
3324 PENDING_ASSIST_EXTRAS_LONG_TIMEOUT, 0) != null;
3325 }
3326
3327 @Override
3328 public boolean requestAutofillData(IAssistDataReceiver receiver, Bundle receiverExtras,
3329 IBinder activityToken, int flags) {
3330 return enqueueAssistContext(ActivityManager.ASSIST_CONTEXT_AUTOFILL, null, null,
3331 receiver, receiverExtras, activityToken, true, true, UserHandle.getCallingUserId(),
3332 null, PENDING_AUTOFILL_ASSIST_STRUCTURE_TIMEOUT, flags) != null;
3333 }
3334
3335 @Override
3336 public boolean launchAssistIntent(Intent intent, int requestType, String hint, int userHandle,
3337 Bundle args) {
3338 return enqueueAssistContext(requestType, intent, hint, null, null, null,
3339 true /* focused */, true /* newSessionId */, userHandle, args,
3340 PENDING_ASSIST_EXTRAS_TIMEOUT, 0) != null;
3341 }
3342
3343 @Override
3344 public Bundle getAssistContextExtras(int requestType) {
3345 PendingAssistExtras pae = enqueueAssistContext(requestType, null, null, null,
3346 null, null, true /* focused */, true /* newSessionId */,
3347 UserHandle.getCallingUserId(), null, PENDING_ASSIST_EXTRAS_TIMEOUT, 0);
3348 if (pae == null) {
3349 return null;
3350 }
3351 synchronized (pae) {
3352 while (!pae.haveResult) {
3353 try {
3354 pae.wait();
3355 } catch (InterruptedException e) {
3356 }
3357 }
3358 }
3359 synchronized (mGlobalLock) {
3360 buildAssistBundleLocked(pae, pae.result);
3361 mPendingAssistExtras.remove(pae);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003362 mUiHandler.removeCallbacks(pae);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003363 }
3364 return pae.extras;
3365 }
3366
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003367 /**
3368 * Binder IPC calls go through the public entry point.
3369 * This can be called with or without the global lock held.
3370 */
3371 private static int checkCallingPermission(String permission) {
3372 return checkPermission(
3373 permission, Binder.getCallingPid(), UserHandle.getAppId(Binder.getCallingUid()));
3374 }
3375
3376 /** This can be called with or without the global lock held. */
Wale Ogunwale214f3482018-10-04 11:00:47 -07003377 private void enforceCallerIsRecentsOrHasPermission(String permission, String func) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003378 if (!getRecentTasks().isCallerRecents(Binder.getCallingUid())) {
3379 mAmInternal.enforceCallingPermission(permission, func);
3380 }
3381 }
3382
3383 @VisibleForTesting
3384 int checkGetTasksPermission(String permission, int pid, int uid) {
3385 return checkPermission(permission, pid, uid);
3386 }
3387
3388 static int checkPermission(String permission, int pid, int uid) {
3389 if (permission == null) {
3390 return PackageManager.PERMISSION_DENIED;
3391 }
3392 return checkComponentPermission(permission, pid, uid, -1, true);
3393 }
3394
Wale Ogunwale214f3482018-10-04 11:00:47 -07003395 public static int checkComponentPermission(String permission, int pid, int uid,
3396 int owningUid, boolean exported) {
3397 return ActivityManagerService.checkComponentPermission(
3398 permission, pid, uid, owningUid, exported);
3399 }
3400
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003401 boolean isGetTasksAllowed(String caller, int callingPid, int callingUid) {
3402 if (getRecentTasks().isCallerRecents(callingUid)) {
3403 // Always allow the recents component to get tasks
3404 return true;
3405 }
3406
3407 boolean allowed = checkGetTasksPermission(android.Manifest.permission.REAL_GET_TASKS,
3408 callingPid, callingUid) == PackageManager.PERMISSION_GRANTED;
3409 if (!allowed) {
3410 if (checkGetTasksPermission(android.Manifest.permission.GET_TASKS,
3411 callingPid, callingUid) == PackageManager.PERMISSION_GRANTED) {
3412 // Temporary compatibility: some existing apps on the system image may
3413 // still be requesting the old permission and not switched to the new
3414 // one; if so, we'll still allow them full access. This means we need
3415 // to see if they are holding the old permission and are a system app.
3416 try {
3417 if (AppGlobals.getPackageManager().isUidPrivileged(callingUid)) {
3418 allowed = true;
3419 if (DEBUG_TASKS) Slog.w(TAG, caller + ": caller " + callingUid
3420 + " is using old GET_TASKS but privileged; allowing");
3421 }
3422 } catch (RemoteException e) {
3423 }
3424 }
3425 if (DEBUG_TASKS) Slog.w(TAG, caller + ": caller " + callingUid
3426 + " does not hold REAL_GET_TASKS; limiting output");
3427 }
3428 return allowed;
3429 }
3430
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003431 private PendingAssistExtras enqueueAssistContext(int requestType, Intent intent, String hint,
3432 IAssistDataReceiver receiver, Bundle receiverExtras, IBinder activityToken,
3433 boolean focused, boolean newSessionId, int userHandle, Bundle args, long timeout,
3434 int flags) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003435 mAmInternal.enforceCallingPermission(android.Manifest.permission.GET_TOP_ACTIVITY_INFO,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003436 "enqueueAssistContext()");
3437
3438 synchronized (mGlobalLock) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003439 ActivityRecord activity = getTopDisplayFocusedStack().getTopActivity();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003440 if (activity == null) {
3441 Slog.w(TAG, "getAssistContextExtras failed: no top activity");
3442 return null;
3443 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003444 if (!activity.attachedToProcess()) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003445 Slog.w(TAG, "getAssistContextExtras failed: no process for " + activity);
3446 return null;
3447 }
3448 if (focused) {
3449 if (activityToken != null) {
3450 ActivityRecord caller = ActivityRecord.forTokenLocked(activityToken);
3451 if (activity != caller) {
3452 Slog.w(TAG, "enqueueAssistContext failed: caller " + caller
3453 + " is not current top " + activity);
3454 return null;
3455 }
3456 }
3457 } else {
3458 activity = ActivityRecord.forTokenLocked(activityToken);
3459 if (activity == null) {
3460 Slog.w(TAG, "enqueueAssistContext failed: activity for token=" + activityToken
3461 + " couldn't be found");
3462 return null;
3463 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003464 if (!activity.attachedToProcess()) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003465 Slog.w(TAG, "enqueueAssistContext failed: no process for " + activity);
3466 return null;
3467 }
3468 }
3469
3470 PendingAssistExtras pae;
3471 Bundle extras = new Bundle();
3472 if (args != null) {
3473 extras.putAll(args);
3474 }
3475 extras.putString(Intent.EXTRA_ASSIST_PACKAGE, activity.packageName);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003476 extras.putInt(Intent.EXTRA_ASSIST_UID, activity.app.mUid);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003477
3478 pae = new PendingAssistExtras(activity, extras, intent, hint, receiver, receiverExtras,
3479 userHandle);
3480 pae.isHome = activity.isActivityTypeHome();
3481
3482 // Increment the sessionId if necessary
3483 if (newSessionId) {
3484 mViSessionId++;
3485 }
3486 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003487 activity.app.getThread().requestAssistContextExtras(activity.appToken, pae,
3488 requestType, mViSessionId, flags);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003489 mPendingAssistExtras.add(pae);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003490 mUiHandler.postDelayed(pae, timeout);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003491 } catch (RemoteException e) {
3492 Slog.w(TAG, "getAssistContextExtras failed: crash calling " + activity);
3493 return null;
3494 }
3495 return pae;
3496 }
3497 }
3498
3499 private void buildAssistBundleLocked(PendingAssistExtras pae, Bundle result) {
3500 if (result != null) {
3501 pae.extras.putBundle(Intent.EXTRA_ASSIST_CONTEXT, result);
3502 }
3503 if (pae.hint != null) {
3504 pae.extras.putBoolean(pae.hint, true);
3505 }
3506 }
3507
3508 private void pendingAssistExtrasTimedOut(PendingAssistExtras pae) {
3509 IAssistDataReceiver receiver;
3510 synchronized (mGlobalLock) {
3511 mPendingAssistExtras.remove(pae);
3512 receiver = pae.receiver;
3513 }
3514 if (receiver != null) {
3515 // Caller wants result sent back to them.
3516 Bundle sendBundle = new Bundle();
3517 // At least return the receiver extras
3518 sendBundle.putBundle(ASSIST_KEY_RECEIVER_EXTRAS, pae.receiverExtras);
3519 try {
3520 pae.receiver.onHandleAssistData(sendBundle);
3521 } catch (RemoteException e) {
3522 }
3523 }
3524 }
3525
3526 public class PendingAssistExtras extends Binder implements Runnable {
3527 public final ActivityRecord activity;
3528 public boolean isHome;
3529 public final Bundle extras;
3530 public final Intent intent;
3531 public final String hint;
3532 public final IAssistDataReceiver receiver;
3533 public final int userHandle;
3534 public boolean haveResult = false;
3535 public Bundle result = null;
3536 public AssistStructure structure = null;
3537 public AssistContent content = null;
3538 public Bundle receiverExtras;
3539
3540 public PendingAssistExtras(ActivityRecord _activity, Bundle _extras, Intent _intent,
3541 String _hint, IAssistDataReceiver _receiver, Bundle _receiverExtras,
3542 int _userHandle) {
3543 activity = _activity;
3544 extras = _extras;
3545 intent = _intent;
3546 hint = _hint;
3547 receiver = _receiver;
3548 receiverExtras = _receiverExtras;
3549 userHandle = _userHandle;
3550 }
3551
3552 @Override
3553 public void run() {
3554 Slog.w(TAG, "getAssistContextExtras failed: timeout retrieving from " + activity);
3555 synchronized (this) {
3556 haveResult = true;
3557 notifyAll();
3558 }
3559 pendingAssistExtrasTimedOut(this);
3560 }
3561 }
3562
3563 @Override
3564 public boolean isAssistDataAllowedOnCurrentActivity() {
3565 int userId;
3566 synchronized (mGlobalLock) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003567 final ActivityStack focusedStack = getTopDisplayFocusedStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003568 if (focusedStack == null || focusedStack.isActivityTypeAssistant()) {
3569 return false;
3570 }
3571
3572 final ActivityRecord activity = focusedStack.getTopActivity();
3573 if (activity == null) {
3574 return false;
3575 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003576 userId = activity.mUserId;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003577 }
3578 return !DevicePolicyCache.getInstance().getScreenCaptureDisabled(userId);
3579 }
3580
3581 @Override
3582 public boolean showAssistFromActivity(IBinder token, Bundle args) {
3583 long ident = Binder.clearCallingIdentity();
3584 try {
3585 synchronized (mGlobalLock) {
3586 ActivityRecord caller = ActivityRecord.forTokenLocked(token);
Andrii Kulian5f750bc2018-07-17 08:57:23 -07003587 ActivityRecord top = getTopDisplayFocusedStack().getTopActivity();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003588 if (top != caller) {
3589 Slog.w(TAG, "showAssistFromActivity failed: caller " + caller
3590 + " is not current top " + top);
3591 return false;
3592 }
3593 if (!top.nowVisible) {
3594 Slog.w(TAG, "showAssistFromActivity failed: caller " + caller
3595 + " is not visible");
3596 return false;
3597 }
3598 }
3599 return mAssistUtils.showSessionForActiveService(args, SHOW_SOURCE_APPLICATION, null,
3600 token);
3601 } finally {
3602 Binder.restoreCallingIdentity(ident);
3603 }
3604 }
3605
3606 @Override
3607 public boolean isRootVoiceInteraction(IBinder token) {
3608 synchronized (mGlobalLock) {
3609 ActivityRecord r = ActivityRecord.isInStackLocked(token);
3610 if (r == null) {
3611 return false;
3612 }
3613 return r.rootVoiceInteraction;
3614 }
3615 }
3616
Wale Ogunwalef6733932018-06-27 05:14:34 -07003617 private void onLocalVoiceInteractionStartedLocked(IBinder activity,
3618 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor) {
3619 ActivityRecord activityToCallback = ActivityRecord.forTokenLocked(activity);
3620 if (activityToCallback == null) return;
3621 activityToCallback.setVoiceSessionLocked(voiceSession);
3622
3623 // Inform the activity
3624 try {
3625 activityToCallback.app.getThread().scheduleLocalVoiceInteractionStarted(activity,
3626 voiceInteractor);
3627 long token = Binder.clearCallingIdentity();
3628 try {
3629 startRunningVoiceLocked(voiceSession, activityToCallback.appInfo.uid);
3630 } finally {
3631 Binder.restoreCallingIdentity(token);
3632 }
3633 // TODO: VI Should we cache the activity so that it's easier to find later
3634 // rather than scan through all the stacks and activities?
3635 } catch (RemoteException re) {
3636 activityToCallback.clearVoiceSessionLocked();
3637 // TODO: VI Should this terminate the voice session?
3638 }
3639 }
3640
3641 private void startRunningVoiceLocked(IVoiceInteractionSession session, int targetUid) {
3642 Slog.d(TAG, "<<< startRunningVoiceLocked()");
3643 mVoiceWakeLock.setWorkSource(new WorkSource(targetUid));
3644 if (mRunningVoice == null || mRunningVoice.asBinder() != session.asBinder()) {
3645 boolean wasRunningVoice = mRunningVoice != null;
3646 mRunningVoice = session;
3647 if (!wasRunningVoice) {
3648 mVoiceWakeLock.acquire();
3649 updateSleepIfNeededLocked();
3650 }
3651 }
3652 }
3653
3654 void finishRunningVoiceLocked() {
3655 if (mRunningVoice != null) {
3656 mRunningVoice = null;
3657 mVoiceWakeLock.release();
3658 updateSleepIfNeededLocked();
3659 }
3660 }
3661
3662 @Override
3663 public void setVoiceKeepAwake(IVoiceInteractionSession session, boolean keepAwake) {
3664 synchronized (mGlobalLock) {
3665 if (mRunningVoice != null && mRunningVoice.asBinder() == session.asBinder()) {
3666 if (keepAwake) {
3667 mVoiceWakeLock.acquire();
3668 } else {
3669 mVoiceWakeLock.release();
3670 }
3671 }
3672 }
3673 }
3674
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003675 @Override
3676 public ComponentName getActivityClassForToken(IBinder token) {
3677 synchronized (mGlobalLock) {
3678 ActivityRecord r = ActivityRecord.isInStackLocked(token);
3679 if (r == null) {
3680 return null;
3681 }
3682 return r.intent.getComponent();
3683 }
3684 }
3685
3686 @Override
3687 public String getPackageForToken(IBinder token) {
3688 synchronized (mGlobalLock) {
3689 ActivityRecord r = ActivityRecord.isInStackLocked(token);
3690 if (r == null) {
3691 return null;
3692 }
3693 return r.packageName;
3694 }
3695 }
3696
3697 @Override
3698 public void showLockTaskEscapeMessage(IBinder token) {
3699 synchronized (mGlobalLock) {
3700 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
3701 if (r == null) {
3702 return;
3703 }
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07003704 getLockTaskController().showLockTaskToast();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003705 }
3706 }
3707
3708 @Override
3709 public void keyguardGoingAway(int flags) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003710 enforceNotIsolatedCaller("keyguardGoingAway");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003711 final long token = Binder.clearCallingIdentity();
3712 try {
3713 synchronized (mGlobalLock) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07003714 mKeyguardController.keyguardGoingAway(flags);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003715 }
3716 } finally {
3717 Binder.restoreCallingIdentity(token);
3718 }
3719 }
3720
3721 /**
3722 * Try to place task to provided position. The final position might be different depending on
3723 * current user and stacks state. The task will be moved to target stack if it's currently in
3724 * different stack.
3725 */
3726 @Override
3727 public void positionTaskInStack(int taskId, int stackId, int position) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003728 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "positionTaskInStack()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003729 synchronized (mGlobalLock) {
3730 long ident = Binder.clearCallingIdentity();
3731 try {
3732 if (DEBUG_STACK) Slog.d(TAG_STACK, "positionTaskInStack: positioning task="
3733 + taskId + " in stackId=" + stackId + " at position=" + position);
Wale Ogunwaled32da472018-11-16 07:19:28 -08003734 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003735 if (task == null) {
3736 throw new IllegalArgumentException("positionTaskInStack: no task for id="
3737 + taskId);
3738 }
3739
Wale Ogunwaled32da472018-11-16 07:19:28 -08003740 final ActivityStack stack = mRootActivityContainer.getStack(stackId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003741
3742 if (stack == null) {
3743 throw new IllegalArgumentException("positionTaskInStack: no stack for id="
3744 + stackId);
3745 }
3746 if (!stack.isActivityTypeStandardOrUndefined()) {
3747 throw new IllegalArgumentException("positionTaskInStack: Attempt to change"
3748 + " the position of task " + taskId + " in/to non-standard stack");
3749 }
3750
3751 // TODO: Have the callers of this API call a separate reparent method if that is
3752 // what they intended to do vs. having this method also do reparenting.
3753 if (task.getStack() == stack) {
3754 // Change position in current stack.
3755 stack.positionChildAt(task, position);
3756 } else {
3757 // Reparent to new stack.
3758 task.reparent(stack, position, REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE,
3759 !DEFER_RESUME, "positionTaskInStack");
3760 }
3761 } finally {
3762 Binder.restoreCallingIdentity(ident);
3763 }
3764 }
3765 }
3766
3767 @Override
3768 public void reportSizeConfigurations(IBinder token, int[] horizontalSizeConfiguration,
3769 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
3770 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Report configuration: " + token + " "
3771 + horizontalSizeConfiguration + " " + verticalSizeConfigurations);
3772 synchronized (mGlobalLock) {
3773 ActivityRecord record = ActivityRecord.isInStackLocked(token);
3774 if (record == null) {
3775 throw new IllegalArgumentException("reportSizeConfigurations: ActivityRecord not "
3776 + "found for: " + token);
3777 }
3778 record.setSizeConfigurations(horizontalSizeConfiguration,
3779 verticalSizeConfigurations, smallestSizeConfigurations);
3780 }
3781 }
3782
3783 /**
3784 * Dismisses split-screen multi-window mode.
3785 * @param toTop If true the current primary split-screen stack will be placed or left on top.
3786 */
3787 @Override
3788 public void dismissSplitScreenMode(boolean toTop) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003789 enforceCallerIsRecentsOrHasPermission(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003790 MANAGE_ACTIVITY_STACKS, "dismissSplitScreenMode()");
3791 final long ident = Binder.clearCallingIdentity();
3792 try {
3793 synchronized (mGlobalLock) {
3794 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08003795 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003796 if (stack == null) {
3797 Slog.w(TAG, "dismissSplitScreenMode: primary split-screen stack not found.");
3798 return;
3799 }
3800
3801 if (toTop) {
3802 // Caller wants the current split-screen primary stack to be the top stack after
3803 // it goes fullscreen, so move it to the front.
3804 stack.moveToFront("dismissSplitScreenMode");
Wale Ogunwaled32da472018-11-16 07:19:28 -08003805 } else if (mRootActivityContainer.isTopDisplayFocusedStack(stack)) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003806 // In this case the current split-screen primary stack shouldn't be the top
3807 // stack after it goes fullscreen, but it current has focus, so we move the
3808 // focus to the top-most split-screen secondary stack next to it.
3809 final ActivityStack otherStack = stack.getDisplay().getTopStackInWindowingMode(
3810 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
3811 if (otherStack != null) {
3812 otherStack.moveToFront("dismissSplitScreenMode_other");
3813 }
3814 }
3815
Evan Rosky10475742018-09-05 19:02:48 -07003816 stack.setWindowingMode(WINDOWING_MODE_UNDEFINED);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003817 }
3818 } finally {
3819 Binder.restoreCallingIdentity(ident);
3820 }
3821 }
3822
3823 /**
3824 * Dismisses Pip
3825 * @param animate True if the dismissal should be animated.
3826 * @param animationDuration The duration of the resize animation in milliseconds or -1 if the
3827 * default animation duration should be used.
3828 */
3829 @Override
3830 public void dismissPip(boolean animate, int animationDuration) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003831 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "dismissPip()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003832 final long ident = Binder.clearCallingIdentity();
3833 try {
3834 synchronized (mGlobalLock) {
Yunfan Chen279f5582018-12-12 15:24:50 -08003835 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08003836 mRootActivityContainer.getDefaultDisplay().getPinnedStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003837 if (stack == null) {
3838 Slog.w(TAG, "dismissPip: pinned stack not found.");
3839 return;
3840 }
3841 if (stack.getWindowingMode() != WINDOWING_MODE_PINNED) {
3842 throw new IllegalArgumentException("Stack: " + stack
3843 + " doesn't support animated resize.");
3844 }
3845 if (animate) {
3846 stack.animateResizePinnedStack(null /* sourceHintBounds */,
3847 null /* destBounds */, animationDuration, false /* fromFullscreen */);
3848 } else {
3849 mStackSupervisor.moveTasksToFullscreenStackLocked(stack, true /* onTop */);
3850 }
3851 }
3852 } finally {
3853 Binder.restoreCallingIdentity(ident);
3854 }
3855 }
3856
3857 @Override
3858 public void suppressResizeConfigChanges(boolean suppress) throws RemoteException {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003859 mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "suppressResizeConfigChanges()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003860 synchronized (mGlobalLock) {
3861 mSuppressResizeConfigChanges = suppress;
3862 }
3863 }
3864
3865 /**
3866 * NOTE: For the pinned stack, this method is usually called after the bounds animation has
3867 * animated the stack to the fullscreen, but can also be called if we are relaunching an
3868 * activity and clearing the task at the same time.
3869 */
3870 @Override
3871 // TODO: API should just be about changing windowing modes...
3872 public void moveTasksToFullscreenStack(int fromStackId, boolean onTop) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003873 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003874 "moveTasksToFullscreenStack()");
3875 synchronized (mGlobalLock) {
3876 final long origId = Binder.clearCallingIdentity();
3877 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08003878 final ActivityStack stack = mRootActivityContainer.getStack(fromStackId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003879 if (stack != null){
3880 if (!stack.isActivityTypeStandardOrUndefined()) {
3881 throw new IllegalArgumentException(
3882 "You can't move tasks from non-standard stacks.");
3883 }
3884 mStackSupervisor.moveTasksToFullscreenStackLocked(stack, onTop);
3885 }
3886 } finally {
3887 Binder.restoreCallingIdentity(origId);
3888 }
3889 }
3890 }
3891
3892 /**
3893 * Moves the top activity in the input stackId to the pinned stack.
3894 *
3895 * @param stackId Id of stack to move the top activity to pinned stack.
3896 * @param bounds Bounds to use for pinned stack.
3897 *
3898 * @return True if the top activity of the input stack was successfully moved to the pinned
3899 * stack.
3900 */
3901 @Override
3902 public boolean moveTopActivityToPinnedStack(int stackId, Rect bounds) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003903 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003904 "moveTopActivityToPinnedStack()");
3905 synchronized (mGlobalLock) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07003906 if (!mSupportsPictureInPicture) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003907 throw new IllegalStateException("moveTopActivityToPinnedStack:"
3908 + "Device doesn't support picture-in-picture mode");
3909 }
3910
3911 long ident = Binder.clearCallingIdentity();
3912 try {
Wale Ogunwaled32da472018-11-16 07:19:28 -08003913 return mRootActivityContainer.moveTopStackActivityToPinnedStack(stackId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003914 } finally {
3915 Binder.restoreCallingIdentity(ident);
3916 }
3917 }
3918 }
3919
3920 @Override
3921 public boolean isInMultiWindowMode(IBinder token) {
3922 final long origId = Binder.clearCallingIdentity();
3923 try {
3924 synchronized (mGlobalLock) {
3925 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3926 if (r == null) {
3927 return false;
3928 }
3929 // An activity is consider to be in multi-window mode if its task isn't fullscreen.
3930 return r.inMultiWindowMode();
3931 }
3932 } finally {
3933 Binder.restoreCallingIdentity(origId);
3934 }
3935 }
3936
3937 @Override
3938 public boolean isInPictureInPictureMode(IBinder token) {
3939 final long origId = Binder.clearCallingIdentity();
3940 try {
3941 synchronized (mGlobalLock) {
3942 return isInPictureInPictureMode(ActivityRecord.forTokenLocked(token));
3943 }
3944 } finally {
3945 Binder.restoreCallingIdentity(origId);
3946 }
3947 }
3948
3949 private boolean isInPictureInPictureMode(ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003950 if (r == null || r.getActivityStack() == null || !r.inPinnedWindowingMode()
3951 || r.getActivityStack().isInStackLocked(r) == null) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003952 return false;
3953 }
3954
3955 // If we are animating to fullscreen then we have already dispatched the PIP mode
3956 // changed, so we should reflect that check here as well.
Yunfan Chen279f5582018-12-12 15:24:50 -08003957 final TaskStack taskStack = r.getActivityStack().getTaskStack();
3958 return !taskStack.isAnimatingBoundsToFullscreen();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003959 }
3960
3961 @Override
3962 public boolean enterPictureInPictureMode(IBinder token, final PictureInPictureParams params) {
3963 final long origId = Binder.clearCallingIdentity();
3964 try {
3965 synchronized (mGlobalLock) {
3966 final ActivityRecord r = ensureValidPictureInPictureActivityParamsLocked(
3967 "enterPictureInPictureMode", token, params);
3968
3969 // If the activity is already in picture in picture mode, then just return early
3970 if (isInPictureInPictureMode(r)) {
3971 return true;
3972 }
3973
3974 // Activity supports picture-in-picture, now check that we can enter PiP at this
3975 // point, if it is
3976 if (!r.checkEnterPictureInPictureState("enterPictureInPictureMode",
3977 false /* beforeStopping */)) {
3978 return false;
3979 }
3980
3981 final Runnable enterPipRunnable = () -> {
Wale Ogunwalef276a6f2018-06-15 08:26:07 -07003982 synchronized (mGlobalLock) {
3983 // Only update the saved args from the args that are set
3984 r.pictureInPictureArgs.copyOnlySet(params);
3985 final float aspectRatio = r.pictureInPictureArgs.getAspectRatio();
3986 final List<RemoteAction> actions = r.pictureInPictureArgs.getActions();
3987 // Adjust the source bounds by the insets for the transition down
3988 final Rect sourceBounds = new Rect(
3989 r.pictureInPictureArgs.getSourceRectHint());
Wale Ogunwaled32da472018-11-16 07:19:28 -08003990 mRootActivityContainer.moveActivityToPinnedStack(
Wale Ogunwalef276a6f2018-06-15 08:26:07 -07003991 r, sourceBounds, aspectRatio, "enterPictureInPictureMode");
Yunfan Chen279f5582018-12-12 15:24:50 -08003992 final ActivityStack stack = r.getActivityStack();
Wale Ogunwalef276a6f2018-06-15 08:26:07 -07003993 stack.setPictureInPictureAspectRatio(aspectRatio);
3994 stack.setPictureInPictureActions(actions);
3995 MetricsLoggerWrapper.logPictureInPictureEnter(mContext, r.appInfo.uid,
3996 r.shortComponentName, r.supportsEnterPipOnTaskSwitch);
3997 logPictureInPictureArgs(params);
3998 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003999 };
4000
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004001 if (isKeyguardLocked()) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004002 // If the keyguard is showing or occluded, then try and dismiss it before
4003 // entering picture-in-picture (this will prompt the user to authenticate if the
4004 // device is currently locked).
4005 dismissKeyguard(token, new KeyguardDismissCallback() {
4006 @Override
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004007 public void onDismissSucceeded() {
4008 mH.post(enterPipRunnable);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004009 }
4010 }, null /* message */);
4011 } else {
4012 // Enter picture in picture immediately otherwise
4013 enterPipRunnable.run();
4014 }
4015 return true;
4016 }
4017 } finally {
4018 Binder.restoreCallingIdentity(origId);
4019 }
4020 }
4021
4022 @Override
4023 public void setPictureInPictureParams(IBinder token, final PictureInPictureParams params) {
4024 final long origId = Binder.clearCallingIdentity();
4025 try {
4026 synchronized (mGlobalLock) {
4027 final ActivityRecord r = ensureValidPictureInPictureActivityParamsLocked(
4028 "setPictureInPictureParams", token, params);
4029
4030 // Only update the saved args from the args that are set
4031 r.pictureInPictureArgs.copyOnlySet(params);
4032 if (r.inPinnedWindowingMode()) {
4033 // If the activity is already in picture-in-picture, update the pinned stack now
4034 // if it is not already expanding to fullscreen. Otherwise, the arguments will
4035 // be used the next time the activity enters PiP
Yunfan Chen279f5582018-12-12 15:24:50 -08004036 final ActivityStack stack = r.getActivityStack();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004037 if (!stack.isAnimatingBoundsToFullscreen()) {
4038 stack.setPictureInPictureAspectRatio(
4039 r.pictureInPictureArgs.getAspectRatio());
4040 stack.setPictureInPictureActions(r.pictureInPictureArgs.getActions());
4041 }
4042 }
4043 logPictureInPictureArgs(params);
4044 }
4045 } finally {
4046 Binder.restoreCallingIdentity(origId);
4047 }
4048 }
4049
4050 @Override
4051 public int getMaxNumPictureInPictureActions(IBinder token) {
4052 // Currently, this is a static constant, but later, we may change this to be dependent on
4053 // the context of the activity
4054 return 3;
4055 }
4056
4057 private void logPictureInPictureArgs(PictureInPictureParams params) {
4058 if (params.hasSetActions()) {
4059 MetricsLogger.histogram(mContext, "tron_varz_picture_in_picture_actions_count",
4060 params.getActions().size());
4061 }
4062 if (params.hasSetAspectRatio()) {
4063 LogMaker lm = new LogMaker(MetricsEvent.ACTION_PICTURE_IN_PICTURE_ASPECT_RATIO_CHANGED);
4064 lm.addTaggedData(MetricsEvent.PICTURE_IN_PICTURE_ASPECT_RATIO, params.getAspectRatio());
4065 MetricsLogger.action(lm);
4066 }
4067 }
4068
4069 /**
4070 * Checks the state of the system and the activity associated with the given {@param token} to
4071 * verify that picture-in-picture is supported for that activity.
4072 *
4073 * @return the activity record for the given {@param token} if all the checks pass.
4074 */
4075 private ActivityRecord ensureValidPictureInPictureActivityParamsLocked(String caller,
4076 IBinder token, PictureInPictureParams params) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004077 if (!mSupportsPictureInPicture) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004078 throw new IllegalStateException(caller
4079 + ": Device doesn't support picture-in-picture mode.");
4080 }
4081
4082 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
4083 if (r == null) {
4084 throw new IllegalStateException(caller
4085 + ": Can't find activity for token=" + token);
4086 }
4087
4088 if (!r.supportsPictureInPicture()) {
4089 throw new IllegalStateException(caller
4090 + ": Current activity does not support picture-in-picture.");
4091 }
4092
4093 if (params.hasSetAspectRatio()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004094 && !mWindowManager.isValidPictureInPictureAspectRatio(
4095 r.getActivityStack().mDisplayId, params.getAspectRatio())) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004096 final float minAspectRatio = mContext.getResources().getFloat(
4097 com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio);
4098 final float maxAspectRatio = mContext.getResources().getFloat(
4099 com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio);
4100 throw new IllegalArgumentException(String.format(caller
4101 + ": Aspect ratio is too extreme (must be between %f and %f).",
4102 minAspectRatio, maxAspectRatio));
4103 }
4104
4105 // Truncate the number of actions if necessary
4106 params.truncateActions(getMaxNumPictureInPictureActions(token));
4107
4108 return r;
4109 }
4110
4111 @Override
4112 public IBinder getUriPermissionOwnerForActivity(IBinder activityToken) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004113 enforceNotIsolatedCaller("getUriPermissionOwnerForActivity");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004114 synchronized (mGlobalLock) {
4115 ActivityRecord r = ActivityRecord.isInStackLocked(activityToken);
4116 if (r == null) {
4117 throw new IllegalArgumentException("Activity does not exist; token="
4118 + activityToken);
4119 }
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07004120 return r.getUriPermissionsLocked().getExternalToken();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004121 }
4122 }
4123
4124 @Override
4125 public void resizeDockedStack(Rect dockedBounds, Rect tempDockedTaskBounds,
4126 Rect tempDockedTaskInsetBounds,
4127 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004128 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "resizeDockedStack()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004129 long ident = Binder.clearCallingIdentity();
4130 try {
4131 synchronized (mGlobalLock) {
4132 mStackSupervisor.resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds,
4133 tempDockedTaskInsetBounds, tempOtherTaskBounds, tempOtherTaskInsetBounds,
4134 PRESERVE_WINDOWS);
4135 }
4136 } finally {
4137 Binder.restoreCallingIdentity(ident);
4138 }
4139 }
4140
4141 @Override
4142 public void setSplitScreenResizing(boolean resizing) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004143 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "setSplitScreenResizing()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004144 final long ident = Binder.clearCallingIdentity();
4145 try {
4146 synchronized (mGlobalLock) {
4147 mStackSupervisor.setSplitScreenResizing(resizing);
4148 }
4149 } finally {
4150 Binder.restoreCallingIdentity(ident);
4151 }
4152 }
4153
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004154 /**
4155 * Check that we have the features required for VR-related API calls, and throw an exception if
4156 * not.
4157 */
Wale Ogunwale59507092018-10-29 09:00:30 -07004158 public void enforceSystemHasVrFeature() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004159 if (!mContext.getPackageManager().hasSystemFeature(
4160 PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE)) {
4161 throw new UnsupportedOperationException("VR mode not supported on this device!");
4162 }
4163 }
4164
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004165 @Override
4166 public int setVrMode(IBinder token, boolean enabled, ComponentName packageName) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004167 enforceSystemHasVrFeature();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004168
4169 final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
4170
4171 ActivityRecord r;
4172 synchronized (mGlobalLock) {
4173 r = ActivityRecord.isInStackLocked(token);
4174 }
4175
4176 if (r == null) {
4177 throw new IllegalArgumentException();
4178 }
4179
4180 int err;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004181 if ((err = vrService.hasVrPackage(packageName, r.mUserId)) !=
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004182 VrManagerInternal.NO_ERROR) {
4183 return err;
4184 }
4185
4186 // Clear the binder calling uid since this path may call moveToTask().
4187 final long callingId = Binder.clearCallingIdentity();
4188 try {
4189 synchronized (mGlobalLock) {
4190 r.requestedVrComponent = (enabled) ? packageName : null;
4191
4192 // Update associated state if this activity is currently focused
Andrii Kulian52d255c2018-07-13 11:32:19 -07004193 if (r.isResumedActivityOnDisplay()) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004194 applyUpdateVrModeLocked(r);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004195 }
4196 return 0;
4197 }
4198 } finally {
4199 Binder.restoreCallingIdentity(callingId);
4200 }
4201 }
4202
4203 @Override
4204 public void startLocalVoiceInteraction(IBinder callingActivity, Bundle options) {
4205 Slog.i(TAG, "Activity tried to startLocalVoiceInteraction");
4206 synchronized (mGlobalLock) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07004207 ActivityRecord activity = getTopDisplayFocusedStack().getTopActivity();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004208 if (ActivityRecord.forTokenLocked(callingActivity) != activity) {
4209 throw new SecurityException("Only focused activity can call startVoiceInteraction");
4210 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004211 if (mRunningVoice != null || activity.getTaskRecord().voiceSession != null
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004212 || activity.voiceSession != null) {
4213 Slog.w(TAG, "Already in a voice interaction, cannot start new voice interaction");
4214 return;
4215 }
4216 if (activity.pendingVoiceInteractionStart) {
4217 Slog.w(TAG, "Pending start of voice interaction already.");
4218 return;
4219 }
4220 activity.pendingVoiceInteractionStart = true;
4221 }
4222 LocalServices.getService(VoiceInteractionManagerInternal.class)
4223 .startLocalVoiceInteraction(callingActivity, options);
4224 }
4225
4226 @Override
4227 public void stopLocalVoiceInteraction(IBinder callingActivity) {
4228 LocalServices.getService(VoiceInteractionManagerInternal.class)
4229 .stopLocalVoiceInteraction(callingActivity);
4230 }
4231
4232 @Override
4233 public boolean supportsLocalVoiceInteraction() {
4234 return LocalServices.getService(VoiceInteractionManagerInternal.class)
4235 .supportsLocalVoiceInteraction();
4236 }
4237
4238 /** Notifies all listeners when the pinned stack animation starts. */
4239 @Override
4240 public void notifyPinnedStackAnimationStarted() {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004241 mTaskChangeNotificationController.notifyPinnedStackAnimationStarted();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004242 }
4243
4244 /** Notifies all listeners when the pinned stack animation ends. */
4245 @Override
4246 public void notifyPinnedStackAnimationEnded() {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004247 mTaskChangeNotificationController.notifyPinnedStackAnimationEnded();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004248 }
4249
4250 @Override
4251 public void resizePinnedStack(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004252 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "resizePinnedStack()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004253 final long ident = Binder.clearCallingIdentity();
4254 try {
4255 synchronized (mGlobalLock) {
4256 mStackSupervisor.resizePinnedStackLocked(pinnedBounds, tempPinnedTaskBounds);
4257 }
4258 } finally {
4259 Binder.restoreCallingIdentity(ident);
4260 }
4261 }
4262
4263 @Override
4264 public boolean updateDisplayOverrideConfiguration(Configuration values, int displayId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004265 mAmInternal.enforceCallingPermission(CHANGE_CONFIGURATION, "updateDisplayOverrideConfiguration()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004266
4267 synchronized (mGlobalLock) {
4268 // Check if display is initialized in AM.
Wale Ogunwaled32da472018-11-16 07:19:28 -08004269 if (!mRootActivityContainer.isDisplayAdded(displayId)) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004270 // Call might come when display is not yet added or has already been removed.
4271 if (DEBUG_CONFIGURATION) {
4272 Slog.w(TAG, "Trying to update display configuration for non-existing displayId="
4273 + displayId);
4274 }
4275 return false;
4276 }
4277
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004278 if (values == null && mWindowManager != null) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004279 // sentinel: fetch the current configuration from the window manager
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004280 values = mWindowManager.computeNewConfiguration(displayId);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004281 }
4282
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004283 if (mWindowManager != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07004284 final Message msg = PooledLambda.obtainMessage(
4285 ActivityManagerInternal::updateOomLevelsForDisplay, mAmInternal, displayId);
4286 mH.sendMessage(msg);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004287 }
4288
4289 final long origId = Binder.clearCallingIdentity();
4290 try {
4291 if (values != null) {
4292 Settings.System.clearConfiguration(values);
4293 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004294 updateDisplayOverrideConfigurationLocked(values, null /* starting */,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004295 false /* deferResume */, displayId, mTmpUpdateConfigurationResult);
4296 return mTmpUpdateConfigurationResult.changes != 0;
4297 } finally {
4298 Binder.restoreCallingIdentity(origId);
4299 }
4300 }
4301 }
4302
4303 @Override
4304 public boolean updateConfiguration(Configuration values) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004305 mAmInternal.enforceCallingPermission(CHANGE_CONFIGURATION, "updateConfiguration()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004306
4307 synchronized (mGlobalLock) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004308 if (values == null && mWindowManager != null) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004309 // sentinel: fetch the current configuration from the window manager
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004310 values = mWindowManager.computeNewConfiguration(DEFAULT_DISPLAY);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004311 }
4312
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004313 if (mWindowManager != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07004314 final Message msg = PooledLambda.obtainMessage(
4315 ActivityManagerInternal::updateOomLevelsForDisplay, mAmInternal,
4316 DEFAULT_DISPLAY);
4317 mH.sendMessage(msg);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004318 }
4319
4320 final long origId = Binder.clearCallingIdentity();
4321 try {
4322 if (values != null) {
4323 Settings.System.clearConfiguration(values);
4324 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004325 updateConfigurationLocked(values, null, false, false /* persistent */,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004326 UserHandle.USER_NULL, false /* deferResume */,
4327 mTmpUpdateConfigurationResult);
4328 return mTmpUpdateConfigurationResult.changes != 0;
4329 } finally {
4330 Binder.restoreCallingIdentity(origId);
4331 }
4332 }
4333 }
4334
4335 @Override
4336 public void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback,
4337 CharSequence message) {
4338 if (message != null) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004339 mAmInternal.enforceCallingPermission(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004340 Manifest.permission.SHOW_KEYGUARD_MESSAGE, "dismissKeyguard()");
4341 }
4342 final long callingId = Binder.clearCallingIdentity();
4343 try {
4344 synchronized (mGlobalLock) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004345 mKeyguardController.dismissKeyguard(token, callback, message);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004346 }
4347 } finally {
4348 Binder.restoreCallingIdentity(callingId);
4349 }
4350 }
4351
4352 @Override
4353 public void cancelTaskWindowTransition(int taskId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004354 enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004355 "cancelTaskWindowTransition()");
4356 final long ident = Binder.clearCallingIdentity();
4357 try {
4358 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08004359 final TaskRecord task = mRootActivityContainer.anyTaskForId(taskId,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004360 MATCH_TASK_IN_STACKS_ONLY);
4361 if (task == null) {
4362 Slog.w(TAG, "cancelTaskWindowTransition: taskId=" + taskId + " not found");
4363 return;
4364 }
4365 task.cancelWindowTransition();
4366 }
4367 } finally {
4368 Binder.restoreCallingIdentity(ident);
4369 }
4370 }
4371
4372 @Override
4373 public ActivityManager.TaskSnapshot getTaskSnapshot(int taskId, boolean reducedResolution) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004374 enforceCallerIsRecentsOrHasPermission(READ_FRAME_BUFFER, "getTaskSnapshot()");
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004375 final long ident = Binder.clearCallingIdentity();
4376 try {
4377 final TaskRecord task;
4378 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08004379 task = mRootActivityContainer.anyTaskForId(taskId,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004380 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
4381 if (task == null) {
4382 Slog.w(TAG, "getTaskSnapshot: taskId=" + taskId + " not found");
4383 return null;
4384 }
4385 }
4386 // Don't call this while holding the lock as this operation might hit the disk.
4387 return task.getSnapshot(reducedResolution);
4388 } finally {
4389 Binder.restoreCallingIdentity(ident);
4390 }
4391 }
4392
4393 @Override
4394 public void setDisablePreviewScreenshots(IBinder token, boolean disable) {
4395 synchronized (mGlobalLock) {
4396 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4397 if (r == null) {
4398 Slog.w(TAG, "setDisablePreviewScreenshots: Unable to find activity for token="
4399 + token);
4400 return;
4401 }
4402 final long origId = Binder.clearCallingIdentity();
4403 try {
4404 r.setDisablePreviewScreenshots(disable);
4405 } finally {
4406 Binder.restoreCallingIdentity(origId);
4407 }
4408 }
4409 }
4410
4411 /** Return the user id of the last resumed activity. */
4412 @Override
4413 public @UserIdInt
4414 int getLastResumedActivityUserId() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004415 mAmInternal.enforceCallingPermission(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004416 Manifest.permission.INTERACT_ACROSS_USERS_FULL, "getLastResumedActivityUserId()");
4417 synchronized (mGlobalLock) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07004418 if (mLastResumedActivity == null) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004419 return getCurrentUserId();
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004420 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004421 return mLastResumedActivity.mUserId;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004422 }
4423 }
4424
4425 @Override
4426 public void updateLockTaskFeatures(int userId, int flags) {
4427 final int callingUid = Binder.getCallingUid();
4428 if (callingUid != 0 && callingUid != SYSTEM_UID) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004429 mAmInternal.enforceCallingPermission(android.Manifest.permission.UPDATE_LOCK_TASK_PACKAGES,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004430 "updateLockTaskFeatures()");
4431 }
4432 synchronized (mGlobalLock) {
4433 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "Allowing features " + userId + ":0x" +
4434 Integer.toHexString(flags));
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07004435 getLockTaskController().updateLockTaskFeatures(userId, flags);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004436 }
4437 }
4438
4439 @Override
4440 public void setShowWhenLocked(IBinder token, boolean showWhenLocked) {
4441 synchronized (mGlobalLock) {
4442 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4443 if (r == null) {
4444 return;
4445 }
4446 final long origId = Binder.clearCallingIdentity();
4447 try {
4448 r.setShowWhenLocked(showWhenLocked);
4449 } finally {
4450 Binder.restoreCallingIdentity(origId);
4451 }
4452 }
4453 }
4454
4455 @Override
Issei Suzuki74e1eb22018-12-20 17:42:52 +01004456 public void setInheritShowWhenLocked(IBinder token, boolean inheritShowWhenLocked) {
4457 synchronized (mGlobalLock) {
4458 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4459 if (r == null) {
4460 return;
4461 }
4462 final long origId = Binder.clearCallingIdentity();
4463 try {
4464 r.setInheritShowWhenLocked(inheritShowWhenLocked);
4465 } finally {
4466 Binder.restoreCallingIdentity(origId);
4467 }
4468 }
4469 }
4470
4471 @Override
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004472 public void setTurnScreenOn(IBinder token, boolean turnScreenOn) {
4473 synchronized (mGlobalLock) {
4474 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4475 if (r == null) {
4476 return;
4477 }
4478 final long origId = Binder.clearCallingIdentity();
4479 try {
4480 r.setTurnScreenOn(turnScreenOn);
4481 } finally {
4482 Binder.restoreCallingIdentity(origId);
4483 }
4484 }
4485 }
4486
4487 @Override
4488 public void registerRemoteAnimations(IBinder token, RemoteAnimationDefinition definition) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004489 mAmInternal.enforceCallingPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004490 "registerRemoteAnimations");
4491 definition.setCallingPid(Binder.getCallingPid());
4492 synchronized (mGlobalLock) {
4493 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4494 if (r == null) {
4495 return;
4496 }
4497 final long origId = Binder.clearCallingIdentity();
4498 try {
4499 r.registerRemoteAnimations(definition);
4500 } finally {
4501 Binder.restoreCallingIdentity(origId);
4502 }
4503 }
4504 }
4505
4506 @Override
4507 public void registerRemoteAnimationForNextActivityStart(String packageName,
4508 RemoteAnimationAdapter adapter) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004509 mAmInternal.enforceCallingPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS,
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004510 "registerRemoteAnimationForNextActivityStart");
4511 adapter.setCallingPid(Binder.getCallingPid());
4512 synchronized (mGlobalLock) {
4513 final long origId = Binder.clearCallingIdentity();
4514 try {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07004515 getActivityStartController().registerRemoteAnimationForNextActivityStart(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004516 packageName, adapter);
4517 } finally {
4518 Binder.restoreCallingIdentity(origId);
4519 }
4520 }
4521 }
4522
Evan Rosky966759f2019-01-15 10:33:58 -08004523 @Override
4524 public void registerRemoteAnimationsForDisplay(int displayId,
4525 RemoteAnimationDefinition definition) {
4526 mAmInternal.enforceCallingPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS,
4527 "registerRemoteAnimations");
4528 definition.setCallingPid(Binder.getCallingPid());
4529 synchronized (mGlobalLock) {
4530 final ActivityDisplay display = mRootActivityContainer.getActivityDisplay(displayId);
4531 if (display == null) {
4532 Slog.e(TAG, "Couldn't find display with id: " + displayId);
4533 return;
4534 }
4535 final long origId = Binder.clearCallingIdentity();
4536 try {
4537 display.mDisplayContent.registerRemoteAnimations(definition);
4538 } finally {
4539 Binder.restoreCallingIdentity(origId);
4540 }
4541 }
4542 }
4543
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004544 /** @see android.app.ActivityManager#alwaysShowUnsupportedCompileSdkWarning */
4545 @Override
4546 public void alwaysShowUnsupportedCompileSdkWarning(ComponentName activity) {
4547 synchronized (mGlobalLock) {
4548 final long origId = Binder.clearCallingIdentity();
4549 try {
Wale Ogunwale008163e2018-07-23 23:11:08 -07004550 mAppWarnings.alwaysShowUnsupportedCompileSdkWarning(activity);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004551 } finally {
4552 Binder.restoreCallingIdentity(origId);
4553 }
4554 }
4555 }
Wale Ogunwale6767eae2018-05-03 15:52:51 -07004556
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004557 @Override
4558 public void setVrThread(int tid) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004559 enforceSystemHasVrFeature();
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004560 synchronized (mGlobalLock) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07004561 final int pid = Binder.getCallingPid();
4562 final WindowProcessController wpc = mPidMap.get(pid);
4563 mVrController.setVrThreadLocked(tid, pid, wpc);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004564 }
4565 }
4566
4567 @Override
4568 public void setPersistentVrThread(int tid) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004569 if (checkCallingPermission(Manifest.permission.RESTRICTED_VR_ACCESS)
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004570 != PERMISSION_GRANTED) {
4571 final String msg = "Permission Denial: setPersistentVrThread() from pid="
4572 + Binder.getCallingPid()
4573 + ", uid=" + Binder.getCallingUid()
4574 + " requires " + Manifest.permission.RESTRICTED_VR_ACCESS;
4575 Slog.w(TAG, msg);
4576 throw new SecurityException(msg);
4577 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004578 enforceSystemHasVrFeature();
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004579 synchronized (mGlobalLock) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07004580 final int pid = Binder.getCallingPid();
4581 final WindowProcessController proc = mPidMap.get(pid);
4582 mVrController.setPersistentVrThreadLocked(tid, pid, proc);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004583 }
4584 }
4585
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004586 @Override
4587 public void stopAppSwitches() {
4588 enforceCallerIsRecentsOrHasPermission(STOP_APP_SWITCHES, "stopAppSwitches");
4589 synchronized (mGlobalLock) {
4590 mAppSwitchesAllowedTime = SystemClock.uptimeMillis() + APP_SWITCH_DELAY_TIME;
4591 mDidAppSwitch = false;
4592 getActivityStartController().schedulePendingActivityLaunches(APP_SWITCH_DELAY_TIME);
4593 }
4594 }
4595
4596 @Override
4597 public void resumeAppSwitches() {
4598 enforceCallerIsRecentsOrHasPermission(STOP_APP_SWITCHES, "resumeAppSwitches");
4599 synchronized (mGlobalLock) {
4600 // Note that we don't execute any pending app switches... we will
4601 // let those wait until either the timeout, or the next start
4602 // activity request.
4603 mAppSwitchesAllowedTime = 0;
4604 }
4605 }
4606
4607 void onStartActivitySetDidAppSwitch() {
4608 if (mDidAppSwitch) {
4609 // This is the second allowed switch since we stopped switches, so now just generally
4610 // allow switches. Use case:
4611 // - user presses home (switches disabled, switch to home, mDidAppSwitch now true);
4612 // - user taps a home icon (coming from home so allowed, we hit here and now allow
4613 // anyone to switch again).
4614 mAppSwitchesAllowedTime = 0;
4615 } else {
4616 mDidAppSwitch = true;
4617 }
4618 }
4619
4620 /** @return whether the system should disable UI modes incompatible with VR mode. */
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004621 boolean shouldDisableNonVrUiLocked() {
4622 return mVrController.shouldDisableNonVrUiLocked();
4623 }
4624
Wale Ogunwale53783742018-09-16 10:21:51 -07004625 private void applyUpdateVrModeLocked(ActivityRecord r) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004626 // VR apps are expected to run in a main display. If an app is turning on VR for
4627 // itself, but lives in a dynamic stack, then make sure that it is moved to the main
4628 // fullscreen stack before enabling VR Mode.
4629 // TODO: The goal of this code is to keep the VR app on the main display. When the
4630 // stack implementation changes in the future, keep in mind that the use of the fullscreen
4631 // stack is a means to move the activity to the main display and a moveActivityToDisplay()
4632 // option would be a better choice here.
4633 if (r.requestedVrComponent != null && r.getDisplayId() != DEFAULT_DISPLAY) {
4634 Slog.i(TAG, "Moving " + r.shortComponentName + " from stack " + r.getStackId()
4635 + " to main stack for VR");
Wale Ogunwaled32da472018-11-16 07:19:28 -08004636 final ActivityStack stack = mRootActivityContainer.getDefaultDisplay().getOrCreateStack(
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004637 WINDOWING_MODE_FULLSCREEN, r.getActivityType(), true /* toTop */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004638 moveTaskToStack(r.getTaskRecord().taskId, stack.mStackId, true /* toTop */);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004639 }
4640 mH.post(() -> {
4641 if (!mVrController.onVrModeChanged(r)) {
4642 return;
4643 }
4644 synchronized (mGlobalLock) {
4645 final boolean disableNonVrUi = mVrController.shouldDisableNonVrUiLocked();
4646 mWindowManager.disableNonVrUi(disableNonVrUi);
4647 if (disableNonVrUi) {
4648 // If we are in a VR mode where Picture-in-Picture mode is unsupported,
4649 // then remove the pinned stack.
Wale Ogunwaled32da472018-11-16 07:19:28 -08004650 mRootActivityContainer.removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004651 }
4652 }
4653 });
4654 }
4655
Wale Ogunwale53783742018-09-16 10:21:51 -07004656 @Override
4657 public int getPackageScreenCompatMode(String packageName) {
4658 enforceNotIsolatedCaller("getPackageScreenCompatMode");
4659 synchronized (mGlobalLock) {
4660 return mCompatModePackages.getPackageScreenCompatModeLocked(packageName);
4661 }
4662 }
4663
4664 @Override
4665 public void setPackageScreenCompatMode(String packageName, int mode) {
4666 mAmInternal.enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
4667 "setPackageScreenCompatMode");
4668 synchronized (mGlobalLock) {
4669 mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode);
4670 }
4671 }
4672
4673 @Override
4674 public boolean getPackageAskScreenCompat(String packageName) {
4675 enforceNotIsolatedCaller("getPackageAskScreenCompat");
4676 synchronized (mGlobalLock) {
4677 return mCompatModePackages.getPackageAskCompatModeLocked(packageName);
4678 }
4679 }
4680
4681 @Override
4682 public void setPackageAskScreenCompat(String packageName, boolean ask) {
4683 mAmInternal.enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
4684 "setPackageAskScreenCompat");
4685 synchronized (mGlobalLock) {
4686 mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask);
4687 }
4688 }
4689
Wale Ogunwale64258362018-10-16 15:13:37 -07004690 public static String relaunchReasonToString(int relaunchReason) {
4691 switch (relaunchReason) {
4692 case RELAUNCH_REASON_WINDOWING_MODE_RESIZE:
4693 return "window_resize";
4694 case RELAUNCH_REASON_FREE_RESIZE:
4695 return "free_resize";
4696 default:
4697 return null;
4698 }
4699 }
4700
Andrii Kulian5f750bc2018-07-17 08:57:23 -07004701 ActivityStack getTopDisplayFocusedStack() {
Wale Ogunwaled32da472018-11-16 07:19:28 -08004702 return mRootActivityContainer.getTopDisplayFocusedStack();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004703 }
4704
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004705 /** Pokes the task persister. */
4706 void notifyTaskPersisterLocked(TaskRecord task, boolean flush) {
4707 mRecentTasks.notifyTaskPersisterLocked(task, flush);
4708 }
4709
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004710 boolean isKeyguardLocked() {
4711 return mKeyguardController.isKeyguardLocked();
4712 }
4713
Garfield Tan01548632018-11-27 10:15:48 -08004714 /**
4715 * Clears launch params for the given package.
4716 * @param packageNames the names of the packages of which the launch params are to be cleared
4717 */
4718 @Override
4719 public void clearLaunchParamsForPackages(List<String> packageNames) {
4720 mAmInternal.enforceCallingPermission(Manifest.permission.MANAGE_ACTIVITY_STACKS,
4721 "clearLaunchParamsForPackages");
4722 synchronized (mGlobalLock) {
4723 for (int i = 0; i < packageNames.size(); ++i) {
4724 mStackSupervisor.mLaunchParamsPersister.removeRecordForPackage(packageNames.get(i));
4725 }
4726 }
4727 }
4728
Wale Ogunwale9e737db2018-12-17 15:42:37 -08004729 /**
4730 * Makes the display with the given id a single task instance display. I.e the display can only
4731 * contain one task.
4732 */
4733 @Override
4734 public void setDisplayToSingleTaskInstance(int displayId) {
4735 mAmInternal.enforceCallingPermission(Manifest.permission.MANAGE_ACTIVITY_STACKS,
4736 "setDisplayToSingleTaskInstance");
4737 final long origId = Binder.clearCallingIdentity();
4738 try {
4739 final ActivityDisplay display =
4740 mRootActivityContainer.getActivityDisplayOrCreate(displayId);
4741 if (display != null) {
4742 display.setDisplayToSingleTaskInstance();
4743 }
4744 } finally {
4745 Binder.restoreCallingIdentity(origId);
4746 }
4747 }
4748
Wale Ogunwale31913b52018-10-13 08:29:31 -07004749 void dumpLastANRLocked(PrintWriter pw) {
4750 pw.println("ACTIVITY MANAGER LAST ANR (dumpsys activity lastanr)");
4751 if (mLastANRState == null) {
4752 pw.println(" <no ANR has occurred since boot>");
4753 } else {
4754 pw.println(mLastANRState);
4755 }
4756 }
4757
4758 void dumpLastANRTracesLocked(PrintWriter pw) {
4759 pw.println("ACTIVITY MANAGER LAST ANR TRACES (dumpsys activity lastanr-traces)");
4760
4761 final File[] files = new File(ANR_TRACE_DIR).listFiles();
4762 if (ArrayUtils.isEmpty(files)) {
4763 pw.println(" <no ANR has occurred since boot>");
4764 return;
4765 }
4766 // Find the latest file.
4767 File latest = null;
4768 for (File f : files) {
4769 if ((latest == null) || (latest.lastModified() < f.lastModified())) {
4770 latest = f;
Wale Ogunwalef6733932018-06-27 05:14:34 -07004771 }
Wale Ogunwale31913b52018-10-13 08:29:31 -07004772 }
4773 pw.print("File: ");
4774 pw.print(latest.getName());
4775 pw.println();
4776 try (BufferedReader in = new BufferedReader(new FileReader(latest))) {
4777 String line;
4778 while ((line = in.readLine()) != null) {
4779 pw.println(line);
4780 }
4781 } catch (IOException e) {
4782 pw.print("Unable to read: ");
4783 pw.print(e);
4784 pw.println();
4785 }
4786 }
4787
4788 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
4789 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) {
4790 dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage,
4791 "ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)");
4792 }
4793
4794 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
4795 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage, String header) {
4796 pw.println(header);
4797
Wale Ogunwaled32da472018-11-16 07:19:28 -08004798 boolean printedAnything = mRootActivityContainer.dumpActivities(fd, pw, dumpAll, dumpClient,
Wale Ogunwale31913b52018-10-13 08:29:31 -07004799 dumpPackage);
4800 boolean needSep = printedAnything;
4801
4802 boolean printed = ActivityStackSupervisor.printThisActivity(pw,
Wale Ogunwaled32da472018-11-16 07:19:28 -08004803 mRootActivityContainer.getTopResumedActivity(), dumpPackage, needSep,
Wale Ogunwale31913b52018-10-13 08:29:31 -07004804 " ResumedActivity: ");
4805 if (printed) {
4806 printedAnything = true;
4807 needSep = false;
4808 }
4809
4810 if (dumpPackage == null) {
4811 if (needSep) {
4812 pw.println();
4813 }
4814 printedAnything = true;
4815 mStackSupervisor.dump(pw, " ");
4816 }
4817
4818 if (!printedAnything) {
4819 pw.println(" (nothing)");
4820 }
4821 }
4822
4823 void dumpActivityContainersLocked(PrintWriter pw) {
4824 pw.println("ACTIVITY MANAGER STARTER (dumpsys activity containers)");
Wale Ogunwaled32da472018-11-16 07:19:28 -08004825 mRootActivityContainer.dumpChildrenNames(pw, " ");
Wale Ogunwale31913b52018-10-13 08:29:31 -07004826 pw.println(" ");
4827 }
4828
4829 void dumpActivityStarterLocked(PrintWriter pw, String dumpPackage) {
4830 pw.println("ACTIVITY MANAGER STARTER (dumpsys activity starter)");
4831 getActivityStartController().dump(pw, "", dumpPackage);
4832 }
4833
4834 /**
4835 * There are three things that cmd can be:
4836 * - a flattened component name that matches an existing activity
4837 * - the cmd arg isn't the flattened component name of an existing activity:
4838 * dump all activity whose component contains the cmd as a substring
4839 * - A hex number of the ActivityRecord object instance.
4840 *
4841 * @param dumpVisibleStacksOnly dump activity with {@param name} only if in a visible stack
4842 * @param dumpFocusedStackOnly dump activity with {@param name} only if in the focused stack
4843 */
4844 protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args,
4845 int opti, boolean dumpAll, boolean dumpVisibleStacksOnly, boolean dumpFocusedStackOnly) {
4846 ArrayList<ActivityRecord> activities;
4847
4848 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08004849 activities = mRootActivityContainer.getDumpActivities(name, dumpVisibleStacksOnly,
Wale Ogunwale31913b52018-10-13 08:29:31 -07004850 dumpFocusedStackOnly);
4851 }
4852
4853 if (activities.size() <= 0) {
4854 return false;
4855 }
4856
4857 String[] newArgs = new String[args.length - opti];
4858 System.arraycopy(args, opti, newArgs, 0, args.length - opti);
4859
4860 TaskRecord lastTask = null;
4861 boolean needSep = false;
4862 for (int i = activities.size() - 1; i >= 0; i--) {
4863 ActivityRecord r = activities.get(i);
4864 if (needSep) {
4865 pw.println();
4866 }
4867 needSep = true;
4868 synchronized (mGlobalLock) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004869 final TaskRecord task = r.getTaskRecord();
Wale Ogunwale31913b52018-10-13 08:29:31 -07004870 if (lastTask != task) {
4871 lastTask = task;
4872 pw.print("TASK "); pw.print(lastTask.affinity);
4873 pw.print(" id="); pw.print(lastTask.taskId);
4874 pw.print(" userId="); pw.println(lastTask.userId);
4875 if (dumpAll) {
4876 lastTask.dump(pw, " ");
4877 }
4878 }
4879 }
4880 dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll);
4881 }
4882 return true;
4883 }
4884
4885 /**
4886 * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if
4887 * there is a thread associated with the activity.
4888 */
4889 private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw,
4890 final ActivityRecord r, String[] args, boolean dumpAll) {
4891 String innerPrefix = prefix + " ";
4892 synchronized (mGlobalLock) {
4893 pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName);
4894 pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r)));
4895 pw.print(" pid=");
4896 if (r.hasProcess()) pw.println(r.app.getPid());
4897 else pw.println("(not running)");
4898 if (dumpAll) {
4899 r.dump(pw, innerPrefix);
4900 }
4901 }
4902 if (r.attachedToProcess()) {
4903 // flush anything that is already in the PrintWriter since the thread is going
4904 // to write to the file descriptor directly
4905 pw.flush();
4906 try {
4907 TransferPipe tp = new TransferPipe();
4908 try {
4909 r.app.getThread().dumpActivity(tp.getWriteFd(),
4910 r.appToken, innerPrefix, args);
4911 tp.go(fd);
4912 } finally {
4913 tp.kill();
4914 }
4915 } catch (IOException e) {
4916 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4917 } catch (RemoteException e) {
4918 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4919 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07004920 }
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004921 }
4922
sanryhuang498e77e2018-12-06 14:57:01 +08004923 private void writeSleepStateToProto(ProtoOutputStream proto, int wakeFullness,
4924 boolean testPssMode) {
4925 final long sleepToken = proto.start(ActivityManagerServiceDumpProcessesProto.SLEEP_STATUS);
4926 proto.write(ActivityManagerServiceDumpProcessesProto.SleepStatus.WAKEFULNESS,
4927 PowerManagerInternal.wakefulnessToProtoEnum(wakeFullness));
Wale Ogunwaled32da472018-11-16 07:19:28 -08004928 for (ActivityTaskManagerInternal.SleepToken st : mRootActivityContainer.mSleepTokens) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07004929 proto.write(ActivityManagerServiceDumpProcessesProto.SleepStatus.SLEEP_TOKENS,
4930 st.toString());
4931 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07004932 proto.write(ActivityManagerServiceDumpProcessesProto.SleepStatus.SLEEPING, mSleeping);
4933 proto.write(ActivityManagerServiceDumpProcessesProto.SleepStatus.SHUTTING_DOWN,
4934 mShuttingDown);
sanryhuang498e77e2018-12-06 14:57:01 +08004935 proto.write(ActivityManagerServiceDumpProcessesProto.SleepStatus.TEST_PSS_MODE,
4936 testPssMode);
4937 proto.end(sleepToken);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004938 }
4939
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004940 int getCurrentUserId() {
4941 return mAmInternal.getCurrentUserId();
4942 }
4943
4944 private void enforceNotIsolatedCaller(String caller) {
4945 if (UserHandle.isIsolated(Binder.getCallingUid())) {
4946 throw new SecurityException("Isolated process not allowed to call " + caller);
4947 }
4948 }
4949
Wale Ogunwalef6733932018-06-27 05:14:34 -07004950 public Configuration getConfiguration() {
4951 Configuration ci;
4952 synchronized(mGlobalLock) {
Yunfan Chen75157d72018-07-27 14:47:21 +09004953 ci = new Configuration(getGlobalConfigurationForCallingPid());
Wale Ogunwalef6733932018-06-27 05:14:34 -07004954 ci.userSetLocale = false;
4955 }
4956 return ci;
4957 }
4958
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004959 /**
4960 * Current global configuration information. Contains general settings for the entire system,
4961 * also corresponds to the merged configuration of the default display.
4962 */
4963 Configuration getGlobalConfiguration() {
Wale Ogunwaled32da472018-11-16 07:19:28 -08004964 return mRootActivityContainer.getConfiguration();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004965 }
4966
4967 boolean updateConfigurationLocked(Configuration values, ActivityRecord starting,
4968 boolean initLocale) {
4969 return updateConfigurationLocked(values, starting, initLocale, false /* deferResume */);
4970 }
4971
4972 boolean updateConfigurationLocked(Configuration values, ActivityRecord starting,
4973 boolean initLocale, boolean deferResume) {
4974 // pass UserHandle.USER_NULL as userId because we don't persist configuration for any user
4975 return updateConfigurationLocked(values, starting, initLocale, false /* persistent */,
4976 UserHandle.USER_NULL, deferResume);
4977 }
4978
Wale Ogunwale59507092018-10-29 09:00:30 -07004979 public void updatePersistentConfiguration(Configuration values, @UserIdInt int userId) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004980 final long origId = Binder.clearCallingIdentity();
4981 try {
4982 synchronized (mGlobalLock) {
4983 updateConfigurationLocked(values, null, false, true, userId,
4984 false /* deferResume */);
4985 }
4986 } finally {
4987 Binder.restoreCallingIdentity(origId);
4988 }
4989 }
4990
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004991 private boolean updateConfigurationLocked(Configuration values, ActivityRecord starting,
4992 boolean initLocale, boolean persistent, int userId, boolean deferResume) {
4993 return updateConfigurationLocked(values, starting, initLocale, persistent, userId,
4994 deferResume, null /* result */);
4995 }
4996
4997 /**
4998 * Do either or both things: (1) change the current configuration, and (2)
4999 * make sure the given activity is running with the (now) current
5000 * configuration. Returns true if the activity has been left running, or
5001 * false if <var>starting</var> is being destroyed to match the new
5002 * configuration.
5003 *
5004 * @param userId is only used when persistent parameter is set to true to persist configuration
5005 * for that particular user
5006 */
5007 boolean updateConfigurationLocked(Configuration values, ActivityRecord starting,
5008 boolean initLocale, boolean persistent, int userId, boolean deferResume,
5009 ActivityTaskManagerService.UpdateConfigurationResult result) {
5010 int changes = 0;
5011 boolean kept = true;
5012
5013 if (mWindowManager != null) {
5014 mWindowManager.deferSurfaceLayout();
5015 }
5016 try {
5017 if (values != null) {
5018 changes = updateGlobalConfigurationLocked(values, initLocale, persistent, userId,
5019 deferResume);
5020 }
5021
5022 kept = ensureConfigAndVisibilityAfterUpdate(starting, changes);
5023 } finally {
5024 if (mWindowManager != null) {
5025 mWindowManager.continueSurfaceLayout();
5026 }
5027 }
5028
5029 if (result != null) {
5030 result.changes = changes;
5031 result.activityRelaunched = !kept;
5032 }
5033 return kept;
5034 }
5035
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005036 /** Update default (global) configuration and notify listeners about changes. */
5037 private int updateGlobalConfigurationLocked(@NonNull Configuration values, boolean initLocale,
5038 boolean persistent, int userId, boolean deferResume) {
5039 mTempConfig.setTo(getGlobalConfiguration());
5040 final int changes = mTempConfig.updateFrom(values);
5041 if (changes == 0) {
5042 // Since calling to Activity.setRequestedOrientation leads to freezing the window with
5043 // setting WindowManagerService.mWaitingForConfig to true, it is important that we call
5044 // performDisplayOverrideConfigUpdate in order to send the new display configuration
5045 // (even if there are no actual changes) to unfreeze the window.
5046 performDisplayOverrideConfigUpdate(values, deferResume, DEFAULT_DISPLAY);
5047 return 0;
5048 }
5049
5050 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.i(TAG_CONFIGURATION,
5051 "Updating global configuration to: " + values);
5052
5053 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
5054 StatsLog.write(StatsLog.RESOURCE_CONFIGURATION_CHANGED,
5055 values.colorMode,
5056 values.densityDpi,
5057 values.fontScale,
5058 values.hardKeyboardHidden,
5059 values.keyboard,
5060 values.keyboardHidden,
5061 values.mcc,
5062 values.mnc,
5063 values.navigation,
5064 values.navigationHidden,
5065 values.orientation,
5066 values.screenHeightDp,
5067 values.screenLayout,
5068 values.screenWidthDp,
5069 values.smallestScreenWidthDp,
5070 values.touchscreen,
5071 values.uiMode);
5072
5073
5074 if (!initLocale && !values.getLocales().isEmpty() && values.userSetLocale) {
5075 final LocaleList locales = values.getLocales();
5076 int bestLocaleIndex = 0;
5077 if (locales.size() > 1) {
5078 if (mSupportedSystemLocales == null) {
5079 mSupportedSystemLocales = Resources.getSystem().getAssets().getLocales();
5080 }
5081 bestLocaleIndex = Math.max(0, locales.getFirstMatchIndex(mSupportedSystemLocales));
5082 }
5083 SystemProperties.set("persist.sys.locale",
5084 locales.get(bestLocaleIndex).toLanguageTag());
5085 LocaleList.setDefault(locales, bestLocaleIndex);
Wale Ogunwale342fbe92018-10-09 08:44:10 -07005086
5087 final Message m = PooledLambda.obtainMessage(
5088 ActivityTaskManagerService::sendLocaleToMountDaemonMsg, this,
5089 locales.get(bestLocaleIndex));
5090 mH.sendMessage(m);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005091 }
5092
Yunfan Chen75157d72018-07-27 14:47:21 +09005093 mTempConfig.seq = increaseConfigurationSeqLocked();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005094
5095 // Update stored global config and notify everyone about the change.
Wale Ogunwaled32da472018-11-16 07:19:28 -08005096 mRootActivityContainer.onConfigurationChanged(mTempConfig);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005097
5098 Slog.i(TAG, "Config changes=" + Integer.toHexString(changes) + " " + mTempConfig);
5099 // TODO(multi-display): Update UsageEvents#Event to include displayId.
Wale Ogunwale342fbe92018-10-09 08:44:10 -07005100 mUsageStatsInternal.reportConfigurationChange(mTempConfig, mAmInternal.getCurrentUserId());
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005101
5102 // TODO: If our config changes, should we auto dismiss any currently showing dialogs?
Wale Ogunwalef6733932018-06-27 05:14:34 -07005103 updateShouldShowDialogsLocked(mTempConfig);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005104
5105 AttributeCache ac = AttributeCache.instance();
5106 if (ac != null) {
5107 ac.updateConfiguration(mTempConfig);
5108 }
5109
5110 // Make sure all resources in our process are updated right now, so that anyone who is going
5111 // to retrieve resource values after we return will be sure to get the new ones. This is
5112 // especially important during boot, where the first config change needs to guarantee all
5113 // resources have that config before following boot code is executed.
Wale Ogunwale342fbe92018-10-09 08:44:10 -07005114 mSystemThread.applyConfigurationToResources(mTempConfig);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005115
5116 // We need another copy of global config because we're scheduling some calls instead of
5117 // running them in place. We need to be sure that object we send will be handled unchanged.
5118 final Configuration configCopy = new Configuration(mTempConfig);
5119 if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07005120 final Message msg = PooledLambda.obtainMessage(
5121 ActivityTaskManagerService::sendPutConfigurationForUserMsg,
5122 this, userId, configCopy);
5123 mH.sendMessage(msg);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005124 }
5125
Yunfan Chen34fcc7a2018-10-11 16:26:09 -07005126 for (int i = mPidMap.size() - 1; i >= 0; i--) {
Yunfan Chen79b96062018-10-17 12:45:23 -07005127 final int pid = mPidMap.keyAt(i);
5128 final WindowProcessController app = mPidMap.get(pid);
Yunfan Chen34fcc7a2018-10-11 16:26:09 -07005129 if (DEBUG_CONFIGURATION) {
5130 Slog.v(TAG_CONFIGURATION, "Update process config of "
5131 + app.mName + " to new config " + configCopy);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005132 }
Yunfan Chen34fcc7a2018-10-11 16:26:09 -07005133 app.onConfigurationChanged(configCopy);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005134 }
5135
Wale Ogunwale2ea36d42018-10-18 10:27:31 -07005136 final Message msg = PooledLambda.obtainMessage(
5137 ActivityManagerInternal::broadcastGlobalConfigurationChanged,
5138 mAmInternal, changes, initLocale);
5139 mH.sendMessage(msg);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005140
5141 // Override configuration of the default display duplicates global config, so we need to
5142 // update it also. This will also notify WindowManager about changes.
Wale Ogunwaled32da472018-11-16 07:19:28 -08005143 performDisplayOverrideConfigUpdate(mRootActivityContainer.getConfiguration(), deferResume,
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005144 DEFAULT_DISPLAY);
5145
5146 return changes;
5147 }
5148
5149 boolean updateDisplayOverrideConfigurationLocked(Configuration values, ActivityRecord starting,
5150 boolean deferResume, int displayId) {
5151 return updateDisplayOverrideConfigurationLocked(values, starting, deferResume /* deferResume */,
5152 displayId, null /* result */);
5153 }
5154
5155 /**
5156 * Updates override configuration specific for the selected display. If no config is provided,
5157 * new one will be computed in WM based on current display info.
5158 */
5159 boolean updateDisplayOverrideConfigurationLocked(Configuration values,
5160 ActivityRecord starting, boolean deferResume, int displayId,
5161 ActivityTaskManagerService.UpdateConfigurationResult result) {
5162 int changes = 0;
5163 boolean kept = true;
5164
5165 if (mWindowManager != null) {
5166 mWindowManager.deferSurfaceLayout();
5167 }
5168 try {
5169 if (values != null) {
5170 if (displayId == DEFAULT_DISPLAY) {
5171 // Override configuration of the default display duplicates global config, so
5172 // we're calling global config update instead for default display. It will also
5173 // apply the correct override config.
5174 changes = updateGlobalConfigurationLocked(values, false /* initLocale */,
5175 false /* persistent */, UserHandle.USER_NULL /* userId */, deferResume);
5176 } else {
5177 changes = performDisplayOverrideConfigUpdate(values, deferResume, displayId);
5178 }
5179 }
5180
5181 kept = ensureConfigAndVisibilityAfterUpdate(starting, changes);
5182 } finally {
5183 if (mWindowManager != null) {
5184 mWindowManager.continueSurfaceLayout();
5185 }
5186 }
5187
5188 if (result != null) {
5189 result.changes = changes;
5190 result.activityRelaunched = !kept;
5191 }
5192 return kept;
5193 }
5194
5195 private int performDisplayOverrideConfigUpdate(Configuration values, boolean deferResume,
5196 int displayId) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005197 mTempConfig.setTo(mRootActivityContainer.getDisplayOverrideConfiguration(displayId));
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005198 final int changes = mTempConfig.updateFrom(values);
5199 if (changes != 0) {
5200 Slog.i(TAG, "Override config changes=" + Integer.toHexString(changes) + " "
5201 + mTempConfig + " for displayId=" + displayId);
Wale Ogunwaled32da472018-11-16 07:19:28 -08005202 mRootActivityContainer.setDisplayOverrideConfiguration(mTempConfig, displayId);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005203
5204 final boolean isDensityChange = (changes & ActivityInfo.CONFIG_DENSITY) != 0;
5205 if (isDensityChange && displayId == DEFAULT_DISPLAY) {
Wale Ogunwale008163e2018-07-23 23:11:08 -07005206 mAppWarnings.onDensityChanged();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005207
Wale Ogunwale5c918702018-10-18 11:06:33 -07005208 // Post message to start process to avoid possible deadlock of calling into AMS with
5209 // the ATMS lock held.
5210 final Message msg = PooledLambda.obtainMessage(
5211 ActivityManagerInternal::killAllBackgroundProcessesExcept, mAmInternal,
5212 N, ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE);
5213 mH.sendMessage(msg);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005214 }
5215 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005216 return changes;
5217 }
5218
Wale Ogunwalef6733932018-06-27 05:14:34 -07005219 private void updateEventDispatchingLocked(boolean booted) {
5220 mWindowManager.setEventDispatching(booted && !mShuttingDown);
5221 }
5222
Wale Ogunwale342fbe92018-10-09 08:44:10 -07005223 private void sendPutConfigurationForUserMsg(int userId, Configuration config) {
5224 final ContentResolver resolver = mContext.getContentResolver();
5225 Settings.System.putConfigurationForUser(resolver, config, userId);
5226 }
5227
5228 private void sendLocaleToMountDaemonMsg(Locale l) {
5229 try {
5230 IBinder service = ServiceManager.getService("mount");
5231 IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
5232 Log.d(TAG, "Storing locale " + l.toLanguageTag() + " for decryption UI");
5233 storageManager.setField(StorageManager.SYSTEM_LOCALE_KEY, l.toLanguageTag());
5234 } catch (RemoteException e) {
5235 Log.e(TAG, "Error storing locale for decryption UI", e);
5236 }
5237 }
5238
Alison Cichowlas3e340502018-08-07 17:15:01 -04005239 private void expireStartAsCallerTokenMsg(IBinder permissionToken) {
5240 mStartActivitySources.remove(permissionToken);
5241 mExpiredStartAsCallerTokens.add(permissionToken);
5242 }
5243
5244 private void forgetStartAsCallerTokenMsg(IBinder permissionToken) {
5245 mExpiredStartAsCallerTokens.remove(permissionToken);
5246 }
5247
Wale Ogunwale342fbe92018-10-09 08:44:10 -07005248 boolean isActivityStartsLoggingEnabled() {
5249 return mAmInternal.isActivityStartsLoggingEnabled();
5250 }
5251
Michal Karpinski8596ded2018-11-14 14:43:48 +00005252 boolean isBackgroundActivityStartsEnabled() {
5253 return mAmInternal.isBackgroundActivityStartsEnabled();
5254 }
5255
Michal Karpinski666631b2019-02-26 16:59:11 +00005256 boolean isPackageNameWhitelistedForBgActivityStarts(String packageName) {
5257 return mAmInternal.isPackageNameWhitelistedForBgActivityStarts(packageName);
5258 }
5259
Wale Ogunwalef6733932018-06-27 05:14:34 -07005260 void enableScreenAfterBoot(boolean booted) {
5261 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
5262 SystemClock.uptimeMillis());
5263 mWindowManager.enableScreenAfterBoot();
5264
5265 synchronized (mGlobalLock) {
5266 updateEventDispatchingLocked(booted);
5267 }
5268 }
5269
Wale Ogunwale906f9c62018-07-23 11:23:44 -07005270 static long getInputDispatchingTimeoutLocked(ActivityRecord r) {
5271 if (r == null || !r.hasProcess()) {
5272 return KEY_DISPATCHING_TIMEOUT_MS;
5273 }
5274 return getInputDispatchingTimeoutLocked(r.app);
5275 }
5276
5277 private static long getInputDispatchingTimeoutLocked(WindowProcessController r) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07005278 return r != null ? r.getInputDispatchingTimeout() : KEY_DISPATCHING_TIMEOUT_MS;
Wale Ogunwale906f9c62018-07-23 11:23:44 -07005279 }
5280
Wale Ogunwalef6733932018-06-27 05:14:34 -07005281 /**
5282 * Decide based on the configuration whether we should show the ANR,
5283 * crash, etc dialogs. The idea is that if there is no affordance to
5284 * press the on-screen buttons, or the user experience would be more
5285 * greatly impacted than the crash itself, we shouldn't show the dialog.
5286 *
5287 * A thought: SystemUI might also want to get told about this, the Power
5288 * dialog / global actions also might want different behaviors.
5289 */
5290 private void updateShouldShowDialogsLocked(Configuration config) {
5291 final boolean inputMethodExists = !(config.keyboard == Configuration.KEYBOARD_NOKEYS
5292 && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH
5293 && config.navigation == Configuration.NAVIGATION_NONAV);
5294 int modeType = config.uiMode & Configuration.UI_MODE_TYPE_MASK;
5295 final boolean uiModeSupportsDialogs = (modeType != Configuration.UI_MODE_TYPE_CAR
5296 && !(modeType == Configuration.UI_MODE_TYPE_WATCH && Build.IS_USER)
5297 && modeType != Configuration.UI_MODE_TYPE_TELEVISION
5298 && modeType != Configuration.UI_MODE_TYPE_VR_HEADSET);
5299 final boolean hideDialogsSet = Settings.Global.getInt(mContext.getContentResolver(),
5300 HIDE_ERROR_DIALOGS, 0) != 0;
5301 mShowDialogs = inputMethodExists && uiModeSupportsDialogs && !hideDialogsSet;
5302 }
5303
5304 private void updateFontScaleIfNeeded(@UserIdInt int userId) {
5305 final float scaleFactor = Settings.System.getFloatForUser(mContext.getContentResolver(),
5306 FONT_SCALE, 1.0f, userId);
5307
5308 synchronized (this) {
5309 if (getGlobalConfiguration().fontScale == scaleFactor) {
5310 return;
5311 }
5312
5313 final Configuration configuration
5314 = mWindowManager.computeNewConfiguration(DEFAULT_DISPLAY);
5315 configuration.fontScale = scaleFactor;
5316 updatePersistentConfiguration(configuration, userId);
5317 }
5318 }
5319
5320 // Actually is sleeping or shutting down or whatever else in the future
5321 // is an inactive state.
5322 boolean isSleepingOrShuttingDownLocked() {
5323 return isSleepingLocked() || mShuttingDown;
5324 }
5325
5326 boolean isSleepingLocked() {
5327 return mSleeping;
5328 }
5329
Riddle Hsu16567132018-08-16 21:37:47 +08005330 /** Update AMS states when an activity is resumed. */
Wale Ogunwalef6733932018-06-27 05:14:34 -07005331 void setResumedActivityUncheckLocked(ActivityRecord r, String reason) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005332 final TaskRecord task = r.getTaskRecord();
Wale Ogunwalef6733932018-06-27 05:14:34 -07005333 if (task.isActivityTypeStandard()) {
5334 if (mCurAppTimeTracker != r.appTimeTracker) {
5335 // We are switching app tracking. Complete the current one.
5336 if (mCurAppTimeTracker != null) {
5337 mCurAppTimeTracker.stop();
5338 mH.obtainMessage(
5339 REPORT_TIME_TRACKER_MSG, mCurAppTimeTracker).sendToTarget();
Wale Ogunwaled32da472018-11-16 07:19:28 -08005340 mRootActivityContainer.clearOtherAppTimeTrackers(r.appTimeTracker);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005341 mCurAppTimeTracker = null;
5342 }
5343 if (r.appTimeTracker != null) {
5344 mCurAppTimeTracker = r.appTimeTracker;
5345 startTimeTrackingFocusedActivityLocked();
5346 }
5347 } else {
5348 startTimeTrackingFocusedActivityLocked();
5349 }
5350 } else {
5351 r.appTimeTracker = null;
5352 }
5353 // TODO: VI Maybe r.task.voiceInteractor || r.voiceInteractor != null
5354 // TODO: Probably not, because we don't want to resume voice on switching
5355 // back to this activity
5356 if (task.voiceInteractor != null) {
5357 startRunningVoiceLocked(task.voiceSession, r.info.applicationInfo.uid);
5358 } else {
5359 finishRunningVoiceLocked();
5360
5361 if (mLastResumedActivity != null) {
5362 final IVoiceInteractionSession session;
5363
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005364 final TaskRecord lastResumedActivityTask = mLastResumedActivity.getTaskRecord();
Wale Ogunwalef6733932018-06-27 05:14:34 -07005365 if (lastResumedActivityTask != null
5366 && lastResumedActivityTask.voiceSession != null) {
5367 session = lastResumedActivityTask.voiceSession;
5368 } else {
5369 session = mLastResumedActivity.voiceSession;
5370 }
5371
5372 if (session != null) {
5373 // We had been in a voice interaction session, but now focused has
5374 // move to something different. Just finish the session, we can't
5375 // return to it and retain the proper state and synchronization with
5376 // the voice interaction service.
5377 finishVoiceTask(session);
5378 }
5379 }
5380 }
5381
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005382 if (mLastResumedActivity != null && r.mUserId != mLastResumedActivity.mUserId) {
5383 mAmInternal.sendForegroundProfileChanged(r.mUserId);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005384 }
5385 updateResumedAppTrace(r);
5386 mLastResumedActivity = r;
5387
Tiger Huang1e5b10a2018-07-30 20:19:51 +08005388 r.getDisplay().setFocusedApp(r, true);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005389
5390 applyUpdateLockStateLocked(r);
5391 applyUpdateVrModeLocked(r);
5392
5393 EventLogTags.writeAmSetResumedActivity(
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005394 r == null ? -1 : r.mUserId,
Wale Ogunwalef6733932018-06-27 05:14:34 -07005395 r == null ? "NULL" : r.shortComponentName,
5396 reason);
5397 }
5398
5399 ActivityTaskManagerInternal.SleepToken acquireSleepToken(String tag, int displayId) {
5400 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005401 final ActivityTaskManagerInternal.SleepToken token =
5402 mRootActivityContainer.createSleepToken(tag, displayId);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005403 updateSleepIfNeededLocked();
5404 return token;
5405 }
5406 }
5407
5408 void updateSleepIfNeededLocked() {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005409 final boolean shouldSleep = !mRootActivityContainer.hasAwakeDisplay();
Wale Ogunwalef6733932018-06-27 05:14:34 -07005410 final boolean wasSleeping = mSleeping;
5411 boolean updateOomAdj = false;
5412
5413 if (!shouldSleep) {
5414 // If wasSleeping is true, we need to wake up activity manager state from when
5415 // we started sleeping. In either case, we need to apply the sleep tokens, which
5416 // will wake up stacks or put them to sleep as appropriate.
5417 if (wasSleeping) {
5418 mSleeping = false;
Chenjie Yubd1a28f2018-07-17 14:55:19 -07005419 StatsLog.write(StatsLog.ACTIVITY_MANAGER_SLEEP_STATE_CHANGED,
5420 StatsLog.ACTIVITY_MANAGER_SLEEP_STATE_CHANGED__STATE__AWAKE);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005421 startTimeTrackingFocusedActivityLocked();
5422 mTopProcessState = ActivityManager.PROCESS_STATE_TOP;
5423 mStackSupervisor.comeOutOfSleepIfNeededLocked();
5424 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08005425 mRootActivityContainer.applySleepTokens(true /* applyToStacks */);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005426 if (wasSleeping) {
5427 updateOomAdj = true;
5428 }
5429 } else if (!mSleeping && shouldSleep) {
5430 mSleeping = true;
Chenjie Yubd1a28f2018-07-17 14:55:19 -07005431 StatsLog.write(StatsLog.ACTIVITY_MANAGER_SLEEP_STATE_CHANGED,
5432 StatsLog.ACTIVITY_MANAGER_SLEEP_STATE_CHANGED__STATE__ASLEEP);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005433 if (mCurAppTimeTracker != null) {
5434 mCurAppTimeTracker.stop();
5435 }
5436 mTopProcessState = ActivityManager.PROCESS_STATE_TOP_SLEEPING;
5437 mStackSupervisor.goingToSleepLocked();
5438 updateResumedAppTrace(null /* resumed */);
5439 updateOomAdj = true;
5440 }
5441 if (updateOomAdj) {
5442 mH.post(mAmInternal::updateOomAdj);
5443 }
5444 }
5445
5446 void updateOomAdj() {
5447 mH.post(mAmInternal::updateOomAdj);
5448 }
5449
Wale Ogunwale53783742018-09-16 10:21:51 -07005450 void updateCpuStats() {
5451 mH.post(mAmInternal::updateCpuStats);
5452 }
5453
Hui Yu03d12402018-12-06 18:00:37 -08005454 void updateBatteryStats(ActivityRecord component, boolean resumed) {
5455 final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::updateBatteryStats,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005456 mAmInternal, component.mActivityComponent, component.app.mUid, component.mUserId,
5457 resumed);
Wale Ogunwale53783742018-09-16 10:21:51 -07005458 mH.sendMessage(m);
5459 }
5460
Hui Yu03d12402018-12-06 18:00:37 -08005461 void updateActivityUsageStats(ActivityRecord activity, int event) {
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -08005462 ComponentName taskRoot = null;
5463 final TaskRecord task = activity.getTaskRecord();
5464 if (task != null) {
5465 final ActivityRecord rootActivity = task.getRootActivity();
5466 if (rootActivity != null) {
5467 taskRoot = rootActivity.mActivityComponent;
5468 }
5469 }
5470
Hui Yu03d12402018-12-06 18:00:37 -08005471 final Message m = PooledLambda.obtainMessage(
5472 ActivityManagerInternal::updateActivityUsageStats, mAmInternal,
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -08005473 activity.mActivityComponent, activity.mUserId, event, activity.appToken, taskRoot);
Hui Yu03d12402018-12-06 18:00:37 -08005474 mH.sendMessage(m);
5475 }
5476
Wale Ogunwale53783742018-09-16 10:21:51 -07005477 void setBooting(boolean booting) {
5478 mAmInternal.setBooting(booting);
5479 }
5480
5481 boolean isBooting() {
5482 return mAmInternal.isBooting();
5483 }
5484
5485 void setBooted(boolean booted) {
5486 mAmInternal.setBooted(booted);
5487 }
5488
5489 boolean isBooted() {
5490 return mAmInternal.isBooted();
5491 }
5492
5493 void postFinishBooting(boolean finishBooting, boolean enableScreen) {
5494 mH.post(() -> {
5495 if (finishBooting) {
5496 mAmInternal.finishBooting();
5497 }
5498 if (enableScreen) {
5499 mInternal.enableScreenAfterBoot(isBooted());
5500 }
5501 });
5502 }
5503
5504 void setHeavyWeightProcess(ActivityRecord root) {
5505 mHeavyWeightProcess = root.app;
5506 final Message m = PooledLambda.obtainMessage(
5507 ActivityTaskManagerService::postHeavyWeightProcessNotification, this,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005508 root.app, root.intent, root.mUserId);
Wale Ogunwale53783742018-09-16 10:21:51 -07005509 mH.sendMessage(m);
5510 }
5511
5512 void clearHeavyWeightProcessIfEquals(WindowProcessController proc) {
5513 if (mHeavyWeightProcess == null || mHeavyWeightProcess != proc) {
5514 return;
5515 }
5516
5517 mHeavyWeightProcess = null;
5518 final Message m = PooledLambda.obtainMessage(
5519 ActivityTaskManagerService::cancelHeavyWeightProcessNotification, this,
5520 proc.mUserId);
5521 mH.sendMessage(m);
5522 }
5523
5524 private void cancelHeavyWeightProcessNotification(int userId) {
5525 final INotificationManager inm = NotificationManager.getService();
5526 if (inm == null) {
5527 return;
5528 }
5529 try {
5530 inm.cancelNotificationWithTag("android", null,
5531 SystemMessage.NOTE_HEAVY_WEIGHT_NOTIFICATION, userId);
5532 } catch (RuntimeException e) {
5533 Slog.w(TAG, "Error canceling notification for service", e);
5534 } catch (RemoteException e) {
5535 }
5536
5537 }
5538
5539 private void postHeavyWeightProcessNotification(
5540 WindowProcessController proc, Intent intent, int userId) {
5541 if (proc == null) {
5542 return;
5543 }
5544
5545 final INotificationManager inm = NotificationManager.getService();
5546 if (inm == null) {
5547 return;
5548 }
5549
5550 try {
5551 Context context = mContext.createPackageContext(proc.mInfo.packageName, 0);
5552 String text = mContext.getString(R.string.heavy_weight_notification,
5553 context.getApplicationInfo().loadLabel(context.getPackageManager()));
5554 Notification notification =
5555 new Notification.Builder(context,
5556 SystemNotificationChannels.HEAVY_WEIGHT_APP)
5557 .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
5558 .setWhen(0)
5559 .setOngoing(true)
5560 .setTicker(text)
5561 .setColor(mContext.getColor(
5562 com.android.internal.R.color.system_notification_accent_color))
5563 .setContentTitle(text)
5564 .setContentText(
5565 mContext.getText(R.string.heavy_weight_notification_detail))
5566 .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0,
5567 intent, PendingIntent.FLAG_CANCEL_CURRENT, null,
5568 new UserHandle(userId)))
5569 .build();
5570 try {
5571 inm.enqueueNotificationWithTag("android", "android", null,
5572 SystemMessage.NOTE_HEAVY_WEIGHT_NOTIFICATION, notification, userId);
5573 } catch (RuntimeException e) {
5574 Slog.w(TAG, "Error showing notification for heavy-weight app", e);
5575 } catch (RemoteException e) {
5576 }
5577 } catch (PackageManager.NameNotFoundException e) {
5578 Slog.w(TAG, "Unable to create context for heavy notification", e);
5579 }
5580
5581 }
5582
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07005583 IIntentSender getIntentSenderLocked(int type, String packageName, int callingUid, int userId,
5584 IBinder token, String resultWho, int requestCode, Intent[] intents,
5585 String[] resolvedTypes, int flags, Bundle bOptions) {
5586
5587 ActivityRecord activity = null;
5588 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
5589 activity = ActivityRecord.isInStackLocked(token);
5590 if (activity == null) {
5591 Slog.w(TAG, "Failed createPendingResult: activity " + token + " not in any stack");
5592 return null;
5593 }
5594 if (activity.finishing) {
5595 Slog.w(TAG, "Failed createPendingResult: activity " + activity + " is finishing");
5596 return null;
5597 }
5598 }
5599
5600 final PendingIntentRecord rec = mPendingIntentController.getIntentSender(type, packageName,
5601 callingUid, userId, token, resultWho, requestCode, intents, resolvedTypes, flags,
5602 bOptions);
5603 final boolean noCreate = (flags & PendingIntent.FLAG_NO_CREATE) != 0;
5604 if (noCreate) {
5605 return rec;
5606 }
5607 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
5608 if (activity.pendingResults == null) {
5609 activity.pendingResults = new HashSet<>();
5610 }
5611 activity.pendingResults.add(rec.ref);
5612 }
5613 return rec;
5614 }
5615
Andrii Kulian52d255c2018-07-13 11:32:19 -07005616 // TODO(b/111541062): Update app time tracking to make it aware of multiple resumed activities
Wale Ogunwalef6733932018-06-27 05:14:34 -07005617 private void startTimeTrackingFocusedActivityLocked() {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005618 final ActivityRecord resumedActivity = mRootActivityContainer.getTopResumedActivity();
Wale Ogunwalef6733932018-06-27 05:14:34 -07005619 if (!mSleeping && mCurAppTimeTracker != null && resumedActivity != null) {
5620 mCurAppTimeTracker.start(resumedActivity.packageName);
5621 }
5622 }
5623
5624 private void updateResumedAppTrace(@Nullable ActivityRecord resumed) {
5625 if (mTracedResumedActivity != null) {
5626 Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER,
5627 constructResumedTraceName(mTracedResumedActivity.packageName), 0);
5628 }
5629 if (resumed != null) {
5630 Trace.asyncTraceBegin(TRACE_TAG_ACTIVITY_MANAGER,
5631 constructResumedTraceName(resumed.packageName), 0);
5632 }
5633 mTracedResumedActivity = resumed;
5634 }
5635
5636 private String constructResumedTraceName(String packageName) {
5637 return "focused app: " + packageName;
5638 }
5639
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005640 /** Applies latest configuration and/or visibility updates if needed. */
5641 private boolean ensureConfigAndVisibilityAfterUpdate(ActivityRecord starting, int changes) {
5642 boolean kept = true;
Wale Ogunwaled32da472018-11-16 07:19:28 -08005643 final ActivityStack mainStack = mRootActivityContainer.getTopDisplayFocusedStack();
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005644 // mainStack is null during startup.
5645 if (mainStack != null) {
5646 if (changes != 0 && starting == null) {
5647 // If the configuration changed, and the caller is not already
5648 // in the process of starting an activity, then find the top
5649 // activity to check if its configuration needs to change.
5650 starting = mainStack.topRunningActivityLocked();
5651 }
5652
5653 if (starting != null) {
5654 kept = starting.ensureActivityConfiguration(changes,
5655 false /* preserveWindow */);
5656 // And we need to make sure at this point that all other activities
5657 // are made visible with the correct configuration.
Wale Ogunwaled32da472018-11-16 07:19:28 -08005658 mRootActivityContainer.ensureActivitiesVisible(starting, changes,
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005659 !PRESERVE_WINDOWS);
5660 }
5661 }
5662
5663 return kept;
5664 }
5665
Wale Ogunwale906f9c62018-07-23 11:23:44 -07005666 void scheduleAppGcsLocked() {
5667 mH.post(() -> mAmInternal.scheduleAppGcs());
5668 }
5669
Wale Ogunwale53783742018-09-16 10:21:51 -07005670 CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
5671 return mCompatModePackages.compatibilityInfoForPackageLocked(ai);
5672 }
5673
Wale Ogunwale906f9c62018-07-23 11:23:44 -07005674 /**
5675 * Returns the PackageManager. Used by classes hosted by {@link ActivityTaskManagerService}. The
5676 * PackageManager could be unavailable at construction time and therefore needs to be accessed
5677 * on demand.
5678 */
5679 IPackageManager getPackageManager() {
5680 return AppGlobals.getPackageManager();
5681 }
5682
5683 PackageManagerInternal getPackageManagerInternalLocked() {
5684 if (mPmInternal == null) {
5685 mPmInternal = LocalServices.getService(PackageManagerInternal.class);
5686 }
5687 return mPmInternal;
5688 }
5689
Wale Ogunwale008163e2018-07-23 23:11:08 -07005690 AppWarnings getAppWarningsLocked() {
5691 return mAppWarnings;
5692 }
5693
Wale Ogunwale214f3482018-10-04 11:00:47 -07005694 Intent getHomeIntent() {
5695 Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
5696 intent.setComponent(mTopComponent);
5697 intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
5698 if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
5699 intent.addCategory(Intent.CATEGORY_HOME);
5700 }
5701 return intent;
5702 }
5703
Chilun2ef71f72018-11-16 17:57:15 +08005704 /**
5705 * Return the intent set with {@link Intent#CATEGORY_SECONDARY_HOME} to resolve secondary home
5706 * activities.
5707 *
5708 * @param preferredPackage Specify a preferred package name, otherwise use secondary home
5709 * component defined in config_secondaryHomeComponent.
5710 * @return the intent set with {@link Intent#CATEGORY_SECONDARY_HOME}
5711 */
5712 Intent getSecondaryHomeIntent(String preferredPackage) {
5713 final Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
5714 if (preferredPackage == null) {
5715 // Using the component stored in config if no package name.
5716 final String secondaryHomeComponent = mContext.getResources().getString(
5717 com.android.internal.R.string.config_secondaryHomeComponent);
5718 intent.setComponent(ComponentName.unflattenFromString(secondaryHomeComponent));
5719 } else {
5720 intent.setPackage(preferredPackage);
5721 }
5722 intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
5723 if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
5724 intent.addCategory(Intent.CATEGORY_SECONDARY_HOME);
5725 }
5726 return intent;
5727 }
5728
Wale Ogunwale214f3482018-10-04 11:00:47 -07005729 ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) {
5730 if (info == null) return null;
5731 ApplicationInfo newInfo = new ApplicationInfo(info);
5732 newInfo.initForUser(userId);
5733 return newInfo;
5734 }
5735
Wale Ogunwale9c103022018-10-18 07:44:54 -07005736 WindowProcessController getProcessController(String processName, int uid) {
Wale Ogunwale214f3482018-10-04 11:00:47 -07005737 if (uid == SYSTEM_UID) {
5738 // The system gets to run in any process. If there are multiple processes with the same
5739 // uid, just pick the first (this should never happen).
5740 final SparseArray<WindowProcessController> procs =
5741 mProcessNames.getMap().get(processName);
5742 if (procs == null) return null;
5743 final int procCount = procs.size();
5744 for (int i = 0; i < procCount; i++) {
5745 final int procUid = procs.keyAt(i);
5746 if (UserHandle.isApp(procUid) || !UserHandle.isSameUser(procUid, uid)) {
5747 // Don't use an app process or different user process for system component.
5748 continue;
5749 }
5750 return procs.valueAt(i);
5751 }
5752 }
5753
5754 return mProcessNames.get(processName, uid);
5755 }
5756
Wale Ogunwale342fbe92018-10-09 08:44:10 -07005757 WindowProcessController getProcessController(IApplicationThread thread) {
5758 if (thread == null) {
5759 return null;
5760 }
5761
5762 final IBinder threadBinder = thread.asBinder();
5763 final ArrayMap<String, SparseArray<WindowProcessController>> pmap = mProcessNames.getMap();
5764 for (int i = pmap.size()-1; i >= 0; i--) {
5765 final SparseArray<WindowProcessController> procs = pmap.valueAt(i);
5766 for (int j = procs.size() - 1; j >= 0; j--) {
5767 final WindowProcessController proc = procs.valueAt(j);
5768 if (proc.hasThread() && proc.getThread().asBinder() == threadBinder) {
5769 return proc;
5770 }
5771 }
5772 }
5773
5774 return null;
5775 }
5776
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00005777 WindowProcessController getProcessController(int pid, int uid) {
5778 final ArrayMap<String, SparseArray<WindowProcessController>> pmap = mProcessNames.getMap();
5779 for (int i = pmap.size()-1; i >= 0; i--) {
5780 final SparseArray<WindowProcessController> procs = pmap.valueAt(i);
5781 for (int j = procs.size() - 1; j >= 0; j--) {
5782 final WindowProcessController proc = procs.valueAt(j);
5783 if (UserHandle.isApp(uid) && proc.getPid() == pid && proc.mUid == uid) {
5784 return proc;
5785 }
5786 }
5787 }
5788 return null;
5789 }
5790
Riddle Hsua0536432019-02-16 00:38:59 +08005791 int getUidState(int uid) {
5792 return mActiveUids.getUidState(uid);
Wale Ogunwalebff2df42018-10-18 17:09:19 -07005793 }
5794
Michal Karpinskicc88d7e2019-01-24 15:32:12 +00005795 boolean isUidForeground(int uid) {
Riddle Hsua0536432019-02-16 00:38:59 +08005796 return (getUidState(uid) == ActivityManager.PROCESS_STATE_TOP)
Michal Karpinskicc88d7e2019-01-24 15:32:12 +00005797 || mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(uid);
5798 }
5799
Michal Karpinski4026cae2019-02-12 11:51:47 +00005800 boolean isDeviceOwner(String packageName) {
5801 if (packageName == null) {
5802 return false;
5803 }
5804 return packageName.equals(mDeviceOwnerPackageName);
5805 }
5806
5807 void setDeviceOwnerPackageName(String deviceOwnerPkg) {
5808 mDeviceOwnerPackageName = deviceOwnerPkg;
5809 }
5810
Wale Ogunwale9de19442018-10-18 19:05:03 -07005811 /**
5812 * @return whitelist tag for a uid from mPendingTempWhitelist, null if not currently on
5813 * the whitelist
5814 */
5815 String getPendingTempWhitelistTagForUidLocked(int uid) {
5816 return mPendingTempWhitelist.get(uid);
5817 }
5818
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07005819 void logAppTooSlow(WindowProcessController app, long startTime, String msg) {
5820 if (true || Build.IS_USER) {
5821 return;
5822 }
5823
5824 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
5825 StrictMode.allowThreadDiskWrites();
5826 try {
5827 File tracesDir = new File("/data/anr");
5828 File tracesFile = null;
5829 try {
5830 tracesFile = File.createTempFile("app_slow", null, tracesDir);
5831
5832 StringBuilder sb = new StringBuilder();
5833 Time tobj = new Time();
5834 tobj.set(System.currentTimeMillis());
5835 sb.append(tobj.format("%Y-%m-%d %H:%M:%S"));
5836 sb.append(": ");
5837 TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb);
5838 sb.append(" since ");
5839 sb.append(msg);
5840 FileOutputStream fos = new FileOutputStream(tracesFile);
5841 fos.write(sb.toString().getBytes());
5842 if (app == null) {
5843 fos.write("\n*** No application process!".getBytes());
5844 }
5845 fos.close();
5846 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
5847 } catch (IOException e) {
5848 Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesFile, e);
5849 return;
5850 }
5851
5852 if (app != null && app.getPid() > 0) {
5853 ArrayList<Integer> firstPids = new ArrayList<Integer>();
5854 firstPids.add(app.getPid());
5855 dumpStackTraces(tracesFile.getAbsolutePath(), firstPids, null, null);
5856 }
5857
5858 File lastTracesFile = null;
5859 File curTracesFile = null;
5860 for (int i=9; i>=0; i--) {
5861 String name = String.format(Locale.US, "slow%02d.txt", i);
5862 curTracesFile = new File(tracesDir, name);
5863 if (curTracesFile.exists()) {
5864 if (lastTracesFile != null) {
5865 curTracesFile.renameTo(lastTracesFile);
5866 } else {
5867 curTracesFile.delete();
5868 }
5869 }
5870 lastTracesFile = curTracesFile;
5871 }
5872 tracesFile.renameTo(curTracesFile);
5873 } finally {
5874 StrictMode.setThreadPolicy(oldPolicy);
5875 }
5876 }
5877
Wale Ogunwaled0412b32018-05-08 09:25:50 -07005878 final class H extends Handler {
Wale Ogunwalef6733932018-06-27 05:14:34 -07005879 static final int REPORT_TIME_TRACKER_MSG = 1;
Alison Cichowlas3e340502018-08-07 17:15:01 -04005880
5881
Wale Ogunwale98875612018-10-12 07:53:02 -07005882 static final int FIRST_ACTIVITY_STACK_MSG = 100;
5883 static final int FIRST_SUPERVISOR_STACK_MSG = 200;
Wale Ogunwalef6733932018-06-27 05:14:34 -07005884
Riddle Hsud93a6c42018-11-29 21:50:06 +08005885 H(Looper looper) {
5886 super(looper);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07005887 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07005888
5889 @Override
5890 public void handleMessage(Message msg) {
5891 switch (msg.what) {
5892 case REPORT_TIME_TRACKER_MSG: {
5893 AppTimeTracker tracker = (AppTimeTracker) msg.obj;
5894 tracker.deliverResult(mContext);
5895 } break;
5896 }
5897 }
Wale Ogunwaled0412b32018-05-08 09:25:50 -07005898 }
5899
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005900 final class UiHandler extends Handler {
Wale Ogunwalef6733932018-06-27 05:14:34 -07005901 static final int DISMISS_DIALOG_UI_MSG = 1;
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005902
5903 public UiHandler() {
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08005904 super(UiThread.get().getLooper(), null, true);
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005905 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07005906
5907 @Override
5908 public void handleMessage(Message msg) {
5909 switch (msg.what) {
5910 case DISMISS_DIALOG_UI_MSG: {
5911 final Dialog d = (Dialog) msg.obj;
5912 d.dismiss();
5913 break;
5914 }
5915 }
5916 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07005917 }
5918
Wale Ogunwale6767eae2018-05-03 15:52:51 -07005919 final class LocalService extends ActivityTaskManagerInternal {
5920 @Override
5921 public SleepToken acquireSleepToken(String tag, int displayId) {
5922 Preconditions.checkNotNull(tag);
Wale Ogunwalef6733932018-06-27 05:14:34 -07005923 return ActivityTaskManagerService.this.acquireSleepToken(tag, displayId);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07005924 }
5925
5926 @Override
5927 public ComponentName getHomeActivityForUser(int userId) {
5928 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005929 final ActivityRecord homeActivity =
5930 mRootActivityContainer.getDefaultDisplayHomeActivityForUser(userId);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005931 return homeActivity == null ? null : homeActivity.mActivityComponent;
Wale Ogunwale6767eae2018-05-03 15:52:51 -07005932 }
5933 }
5934
5935 @Override
5936 public void onLocalVoiceInteractionStarted(IBinder activity,
5937 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor) {
5938 synchronized (mGlobalLock) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07005939 onLocalVoiceInteractionStartedLocked(activity, voiceSession, voiceInteractor);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07005940 }
5941 }
5942
5943 @Override
5944 public void notifyAppTransitionStarting(SparseIntArray reasons, long timestamp) {
5945 synchronized (mGlobalLock) {
5946 mStackSupervisor.getActivityMetricsLogger().notifyTransitionStarting(
5947 reasons, timestamp);
5948 }
5949 }
5950
5951 @Override
5952 public void notifyAppTransitionFinished() {
5953 synchronized (mGlobalLock) {
5954 mStackSupervisor.notifyAppTransitionDone();
5955 }
5956 }
5957
5958 @Override
5959 public void notifyAppTransitionCancelled() {
5960 synchronized (mGlobalLock) {
5961 mStackSupervisor.notifyAppTransitionDone();
5962 }
5963 }
5964
5965 @Override
5966 public List<IBinder> getTopVisibleActivities() {
5967 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005968 return mRootActivityContainer.getTopVisibleActivities();
Wale Ogunwale6767eae2018-05-03 15:52:51 -07005969 }
5970 }
5971
5972 @Override
5973 public void notifyDockedStackMinimizedChanged(boolean minimized) {
5974 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005975 mRootActivityContainer.setDockedStackMinimized(minimized);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07005976 }
5977 }
5978
5979 @Override
5980 public int startActivitiesAsPackage(String packageName, int userId, Intent[] intents,
5981 Bundle bOptions) {
5982 Preconditions.checkNotNull(intents, "intents");
5983 final String[] resolvedTypes = new String[intents.length];
5984
5985 // UID of the package on user userId.
5986 // "= 0" is needed because otherwise catch(RemoteException) would make it look like
5987 // packageUid may not be initialized.
5988 int packageUid = 0;
5989 final long ident = Binder.clearCallingIdentity();
5990
5991 try {
5992 for (int i = 0; i < intents.length; i++) {
5993 resolvedTypes[i] =
5994 intents[i].resolveTypeIfNeeded(mContext.getContentResolver());
5995 }
5996
5997 packageUid = AppGlobals.getPackageManager().getPackageUid(
5998 packageName, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, userId);
5999 } catch (RemoteException e) {
6000 // Shouldn't happen.
6001 } finally {
6002 Binder.restoreCallingIdentity(ident);
6003 }
6004
Riddle Hsu591bf612019-02-14 17:55:31 +08006005 return getActivityStartController().startActivitiesInPackage(
6006 packageUid, packageName,
6007 intents, resolvedTypes, null /* resultTo */,
6008 SafeActivityOptions.fromBundle(bOptions), userId,
6009 false /* validateIncomingUser */, null /* originatingPendingIntent */,
6010 false /* allowBackgroundActivityStart */);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006011 }
6012
6013 @Override
Michal Karpinski84d9ebd2019-01-17 18:28:59 +00006014 public int startActivitiesInPackage(int uid, int realCallingPid, int realCallingUid,
6015 String callingPackage, Intent[] intents, String[] resolvedTypes, IBinder resultTo,
6016 SafeActivityOptions options, int userId, boolean validateIncomingUser,
6017 PendingIntentRecord originatingPendingIntent,
Michal Karpinskiac116df2018-12-10 17:51:42 +00006018 boolean allowBackgroundActivityStart) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07006019 synchronized (mGlobalLock) {
Michal Karpinski84d9ebd2019-01-17 18:28:59 +00006020 return getActivityStartController().startActivitiesInPackage(uid, realCallingPid,
6021 realCallingUid, callingPackage, intents, resolvedTypes, resultTo, options,
6022 userId, validateIncomingUser, originatingPendingIntent,
6023 allowBackgroundActivityStart);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07006024 }
6025 }
6026
6027 @Override
6028 public int startActivityInPackage(int uid, int realCallingPid, int realCallingUid,
6029 String callingPackage, Intent intent, String resolvedType, IBinder resultTo,
6030 String resultWho, int requestCode, int startFlags, SafeActivityOptions options,
6031 int userId, TaskRecord inTask, String reason, boolean validateIncomingUser,
Michal Karpinskiac116df2018-12-10 17:51:42 +00006032 PendingIntentRecord originatingPendingIntent,
6033 boolean allowBackgroundActivityStart) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07006034 synchronized (mGlobalLock) {
6035 return getActivityStartController().startActivityInPackage(uid, realCallingPid,
6036 realCallingUid, callingPackage, intent, resolvedType, resultTo, resultWho,
6037 requestCode, startFlags, options, userId, inTask, reason,
Michal Karpinskiac116df2018-12-10 17:51:42 +00006038 validateIncomingUser, originatingPendingIntent,
6039 allowBackgroundActivityStart);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07006040 }
6041 }
6042
6043 @Override
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006044 public int startActivityAsUser(IApplicationThread caller, String callerPacakge,
6045 Intent intent, Bundle options, int userId) {
6046 return ActivityTaskManagerService.this.startActivityAsUser(
6047 caller, callerPacakge, intent,
6048 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
6049 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options, userId,
6050 false /*validateIncomingUser*/);
6051 }
6052
6053 @Override
lumark588a3e82018-07-20 18:53:54 +08006054 public void notifyKeyguardFlagsChanged(@Nullable Runnable callback, int displayId) {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006055 synchronized (mGlobalLock) {
6056
6057 // We might change the visibilities here, so prepare an empty app transition which
6058 // might be overridden later if we actually change visibilities.
lumark52ea28e2018-11-09 17:30:47 +08006059 final ActivityDisplay activityDisplay =
Wale Ogunwaled32da472018-11-16 07:19:28 -08006060 mRootActivityContainer.getActivityDisplay(displayId);
lumark52ea28e2018-11-09 17:30:47 +08006061 if (activityDisplay == null) {
6062 return;
6063 }
Wale Ogunwale3a256e62018-12-06 14:41:18 -08006064 final DisplayContent dc = activityDisplay.mDisplayContent;
6065 final boolean wasTransitionSet =
6066 dc.mAppTransition.getAppTransition() != TRANSIT_NONE;
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006067 if (!wasTransitionSet) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08006068 dc.prepareAppTransition(TRANSIT_NONE, false /* alwaysKeepCurrent */);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006069 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08006070 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006071
6072 // If there was a transition set already we don't want to interfere with it as we
6073 // might be starting it too early.
6074 if (!wasTransitionSet) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08006075 dc.executeAppTransition();
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006076 }
6077 }
6078 if (callback != null) {
6079 callback.run();
6080 }
6081 }
6082
6083 @Override
6084 public void notifyKeyguardTrustedChanged() {
6085 synchronized (mGlobalLock) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07006086 if (mKeyguardController.isKeyguardShowing(DEFAULT_DISPLAY)) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006087 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006088 }
6089 }
6090 }
6091
6092 /**
6093 * Called after virtual display Id is updated by
6094 * {@link com.android.server.vr.Vr2dDisplay} with a specific
6095 * {@param vrVr2dDisplayId}.
6096 */
6097 @Override
6098 public void setVr2dDisplayId(int vr2dDisplayId) {
6099 if (DEBUG_STACK) Slog.d(TAG, "setVr2dDisplayId called for: " + vr2dDisplayId);
6100 synchronized (mGlobalLock) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07006101 mVr2dDisplayId = vr2dDisplayId;
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006102 }
6103 }
6104
6105 @Override
6106 public void setFocusedActivity(IBinder token) {
6107 synchronized (mGlobalLock) {
6108 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
6109 if (r == null) {
6110 throw new IllegalArgumentException(
6111 "setFocusedActivity: No activity record matching token=" + token);
6112 }
Louis Chang19443452018-10-09 12:10:21 +08006113 if (r.moveFocusableActivityToTop("setFocusedActivity")) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006114 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006115 }
6116 }
6117 }
6118
6119 @Override
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006120 public void registerScreenObserver(ScreenObserver observer) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07006121 mScreenObservers.add(observer);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006122 }
6123
6124 @Override
6125 public boolean isCallerRecents(int callingUid) {
Wale Ogunwale16e505a2018-05-07 15:00:49 -07006126 return getRecentTasks().isCallerRecents(callingUid);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006127 }
6128
6129 @Override
6130 public boolean isRecentsComponentHomeActivity(int userId) {
Wale Ogunwale16e505a2018-05-07 15:00:49 -07006131 return getRecentTasks().isRecentsComponentHomeActivity(userId);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006132 }
6133
6134 @Override
6135 public void cancelRecentsAnimation(boolean restoreHomeStackPosition) {
6136 ActivityTaskManagerService.this.cancelRecentsAnimation(restoreHomeStackPosition);
6137 }
6138
6139 @Override
6140 public void enforceCallerIsRecentsOrHasPermission(String permission, String func) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07006141 ActivityTaskManagerService.this.enforceCallerIsRecentsOrHasPermission(permission, func);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07006142 }
Wale Ogunwaled0412b32018-05-08 09:25:50 -07006143
6144 @Override
6145 public void notifyActiveVoiceInteractionServiceChanged(ComponentName component) {
6146 synchronized (mGlobalLock) {
6147 mActiveVoiceInteractionServiceComponent = component;
6148 }
6149 }
Wale Ogunwalea6191b42018-05-09 07:41:32 -07006150
6151 @Override
6152 public void setAllowAppSwitches(@NonNull String type, int uid, int userId) {
6153 if (!mAmInternal.isUserRunning(userId, ActivityManager.FLAG_OR_STOPPED)) {
6154 return;
6155 }
6156 synchronized (mGlobalLock) {
6157 ArrayMap<String, Integer> types = mAllowAppSwitchUids.get(userId);
6158 if (types == null) {
6159 if (uid < 0) {
6160 return;
6161 }
6162 types = new ArrayMap<>();
6163 mAllowAppSwitchUids.put(userId, types);
6164 }
6165 if (uid < 0) {
6166 types.remove(type);
6167 } else {
6168 types.put(type, uid);
6169 }
6170 }
6171 }
6172
6173 @Override
6174 public void onUserStopped(int userId) {
6175 synchronized (mGlobalLock) {
6176 getRecentTasks().unloadUserDataFromMemoryLocked(userId);
6177 mAllowAppSwitchUids.remove(userId);
6178 }
6179 }
6180
6181 @Override
6182 public boolean isGetTasksAllowed(String caller, int callingPid, int callingUid) {
6183 synchronized (mGlobalLock) {
6184 return ActivityTaskManagerService.this.isGetTasksAllowed(
6185 caller, callingPid, callingUid);
6186 }
6187 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07006188
Riddle Hsua0536432019-02-16 00:38:59 +08006189 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07006190 @Override
6191 public void onProcessAdded(WindowProcessController proc) {
Riddle Hsua0536432019-02-16 00:38:59 +08006192 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07006193 mProcessNames.put(proc.mName, proc.mUid, proc);
6194 }
6195 }
6196
Riddle Hsua0536432019-02-16 00:38:59 +08006197 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07006198 @Override
6199 public void onProcessRemoved(String name, int uid) {
Riddle Hsua0536432019-02-16 00:38:59 +08006200 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07006201 mProcessNames.remove(name, uid);
6202 }
6203 }
6204
Riddle Hsua0536432019-02-16 00:38:59 +08006205 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07006206 @Override
6207 public void onCleanUpApplicationRecord(WindowProcessController proc) {
Riddle Hsua0536432019-02-16 00:38:59 +08006208 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07006209 if (proc == mHomeProcess) {
6210 mHomeProcess = null;
6211 }
6212 if (proc == mPreviousProcess) {
6213 mPreviousProcess = null;
6214 }
6215 }
6216 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07006217
Riddle Hsua0536432019-02-16 00:38:59 +08006218 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwalef6733932018-06-27 05:14:34 -07006219 @Override
6220 public int getTopProcessState() {
Riddle Hsua0536432019-02-16 00:38:59 +08006221 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07006222 return mTopProcessState;
6223 }
6224 }
6225
Riddle Hsua0536432019-02-16 00:38:59 +08006226 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwalef6733932018-06-27 05:14:34 -07006227 @Override
Wale Ogunwale53783742018-09-16 10:21:51 -07006228 public boolean isHeavyWeightProcess(WindowProcessController proc) {
Riddle Hsua0536432019-02-16 00:38:59 +08006229 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale53783742018-09-16 10:21:51 -07006230 return proc == mHeavyWeightProcess;
6231 }
6232 }
6233
Riddle Hsua0536432019-02-16 00:38:59 +08006234 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale53783742018-09-16 10:21:51 -07006235 @Override
6236 public void clearHeavyWeightProcessIfEquals(WindowProcessController proc) {
Riddle Hsua0536432019-02-16 00:38:59 +08006237 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale53783742018-09-16 10:21:51 -07006238 ActivityTaskManagerService.this.clearHeavyWeightProcessIfEquals(proc);
6239 }
6240 }
6241
6242 @Override
6243 public void finishHeavyWeightApp() {
6244 synchronized (mGlobalLock) {
Sudheer Shankaee1da272018-10-20 20:11:44 -07006245 if (mHeavyWeightProcess != null) {
6246 mHeavyWeightProcess.finishActivities();
6247 }
Wale Ogunwale53783742018-09-16 10:21:51 -07006248 ActivityTaskManagerService.this.clearHeavyWeightProcessIfEquals(
6249 mHeavyWeightProcess);
6250 }
6251 }
6252
Riddle Hsua0536432019-02-16 00:38:59 +08006253 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwale53783742018-09-16 10:21:51 -07006254 @Override
Wale Ogunwalef6733932018-06-27 05:14:34 -07006255 public boolean isSleeping() {
Riddle Hsua0536432019-02-16 00:38:59 +08006256 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07006257 return isSleepingLocked();
6258 }
6259 }
6260
6261 @Override
6262 public boolean isShuttingDown() {
6263 synchronized (mGlobalLock) {
6264 return mShuttingDown;
6265 }
6266 }
6267
6268 @Override
6269 public boolean shuttingDown(boolean booted, int timeout) {
6270 synchronized (mGlobalLock) {
6271 mShuttingDown = true;
Wale Ogunwaled32da472018-11-16 07:19:28 -08006272 mRootActivityContainer.prepareForShutdown();
Wale Ogunwalef6733932018-06-27 05:14:34 -07006273 updateEventDispatchingLocked(booted);
Wale Ogunwaled4d67d02018-10-25 18:09:39 -07006274 notifyTaskPersisterLocked(null, true);
Wale Ogunwalef6733932018-06-27 05:14:34 -07006275 return mStackSupervisor.shutdownLocked(timeout);
6276 }
6277 }
6278
6279 @Override
6280 public void enableScreenAfterBoot(boolean booted) {
6281 synchronized (mGlobalLock) {
6282 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
6283 SystemClock.uptimeMillis());
6284 mWindowManager.enableScreenAfterBoot();
6285 updateEventDispatchingLocked(booted);
6286 }
6287 }
6288
6289 @Override
6290 public boolean showStrictModeViolationDialog() {
6291 synchronized (mGlobalLock) {
6292 return mShowDialogs && !mSleeping && !mShuttingDown;
6293 }
6294 }
6295
6296 @Override
6297 public void showSystemReadyErrorDialogsIfNeeded() {
6298 synchronized (mGlobalLock) {
6299 try {
6300 if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
6301 Slog.e(TAG, "UIDs on the system are inconsistent, you need to wipe your"
6302 + " data partition or your device will be unstable.");
6303 mUiHandler.post(() -> {
6304 if (mShowDialogs) {
6305 AlertDialog d = new BaseErrorDialog(mUiContext);
6306 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
6307 d.setCancelable(false);
6308 d.setTitle(mUiContext.getText(R.string.android_system_label));
6309 d.setMessage(mUiContext.getText(R.string.system_error_wipe_data));
6310 d.setButton(DialogInterface.BUTTON_POSITIVE,
6311 mUiContext.getText(R.string.ok),
6312 mUiHandler.obtainMessage(DISMISS_DIALOG_UI_MSG, d));
6313 d.show();
6314 }
6315 });
6316 }
6317 } catch (RemoteException e) {
6318 }
6319
6320 if (!Build.isBuildConsistent()) {
6321 Slog.e(TAG, "Build fingerprint is not consistent, warning user");
6322 mUiHandler.post(() -> {
6323 if (mShowDialogs) {
6324 AlertDialog d = new BaseErrorDialog(mUiContext);
6325 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
6326 d.setCancelable(false);
6327 d.setTitle(mUiContext.getText(R.string.android_system_label));
6328 d.setMessage(mUiContext.getText(R.string.system_error_manufacturer));
6329 d.setButton(DialogInterface.BUTTON_POSITIVE,
6330 mUiContext.getText(R.string.ok),
6331 mUiHandler.obtainMessage(DISMISS_DIALOG_UI_MSG, d));
6332 d.show();
6333 }
6334 });
6335 }
6336 }
6337 }
Wale Ogunwale906f9c62018-07-23 11:23:44 -07006338
6339 @Override
Wale Ogunwale906f9c62018-07-23 11:23:44 -07006340 public void onProcessMapped(int pid, WindowProcessController proc) {
6341 synchronized (mGlobalLock) {
6342 mPidMap.put(pid, proc);
6343 }
6344 }
6345
6346 @Override
6347 public void onProcessUnMapped(int pid) {
6348 synchronized (mGlobalLock) {
6349 mPidMap.remove(pid);
6350 }
6351 }
Wale Ogunwale008163e2018-07-23 23:11:08 -07006352
6353 @Override
6354 public void onPackageDataCleared(String name) {
6355 synchronized (mGlobalLock) {
Wale Ogunwale53783742018-09-16 10:21:51 -07006356 mCompatModePackages.handlePackageDataClearedLocked(name);
Wale Ogunwale008163e2018-07-23 23:11:08 -07006357 mAppWarnings.onPackageDataCleared(name);
6358 }
6359 }
6360
6361 @Override
6362 public void onPackageUninstalled(String name) {
6363 synchronized (mGlobalLock) {
6364 mAppWarnings.onPackageUninstalled(name);
Wale Ogunwale53783742018-09-16 10:21:51 -07006365 mCompatModePackages.handlePackageUninstalledLocked(name);
Wale Ogunwale008163e2018-07-23 23:11:08 -07006366 }
6367 }
Wale Ogunwale53783742018-09-16 10:21:51 -07006368
6369 @Override
6370 public void onPackageAdded(String name, boolean replacing) {
6371 synchronized (mGlobalLock) {
6372 mCompatModePackages.handlePackageAddedLocked(name, replacing);
6373 }
6374 }
6375
6376 @Override
Wale Ogunwale31913b52018-10-13 08:29:31 -07006377 public void onPackageReplaced(ApplicationInfo aInfo) {
6378 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006379 mRootActivityContainer.updateActivityApplicationInfo(aInfo);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006380 }
6381 }
6382
6383 @Override
Wale Ogunwale53783742018-09-16 10:21:51 -07006384 public CompatibilityInfo compatibilityInfoForPackage(ApplicationInfo ai) {
6385 synchronized (mGlobalLock) {
6386 return compatibilityInfoForPackageLocked(ai);
6387 }
6388 }
6389
Yunfan Chen75157d72018-07-27 14:47:21 +09006390 /**
6391 * Set the corresponding display information for the process global configuration. To be
6392 * called when we need to show IME on a different display.
6393 *
6394 * @param pid The process id associated with the IME window.
6395 * @param displayId The ID of the display showing the IME.
6396 */
6397 @Override
Yunfan Chen79b96062018-10-17 12:45:23 -07006398 public void onImeWindowSetOnDisplay(final int pid, final int displayId) {
Yunfan Chen75157d72018-07-27 14:47:21 +09006399 if (pid == MY_PID || pid < 0) {
6400 if (DEBUG_CONFIGURATION) {
6401 Slog.w(TAG,
6402 "Trying to update display configuration for system/invalid process.");
6403 }
6404 return;
6405 }
Yunfan Chencafc7062019-01-22 17:21:32 +09006406 synchronized (mGlobalLock) {
6407 final ActivityDisplay activityDisplay =
6408 mRootActivityContainer.getActivityDisplay(displayId);
6409 if (activityDisplay == null) {
6410 // Call might come when display is not yet added or has been removed.
6411 if (DEBUG_CONFIGURATION) {
6412 Slog.w(TAG, "Trying to update display configuration for non-existing "
6413 + "displayId=" + displayId);
Yunfan Chen75157d72018-07-27 14:47:21 +09006414 }
Yunfan Chencafc7062019-01-22 17:21:32 +09006415 return;
Yunfan Chen75157d72018-07-27 14:47:21 +09006416 }
Yunfan Chencafc7062019-01-22 17:21:32 +09006417 final WindowProcessController process = mPidMap.get(pid);
6418 if (process == null) {
6419 if (DEBUG_CONFIGURATION) {
6420 Slog.w(TAG, "Trying to update display configuration for invalid "
6421 + "process, pid=" + pid);
6422 }
6423 return;
6424 }
6425 process.registerDisplayConfigurationListenerLocked(activityDisplay);
6426 }
Yunfan Chen75157d72018-07-27 14:47:21 +09006427 }
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07006428
6429 @Override
6430 public void sendActivityResult(int callingUid, IBinder activityToken, String resultWho,
6431 int requestCode, int resultCode, Intent data) {
6432 synchronized (mGlobalLock) {
6433 final ActivityRecord r = ActivityRecord.isInStackLocked(activityToken);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08006434 if (r != null && r.getActivityStack() != null) {
6435 r.getActivityStack().sendActivityResultLocked(callingUid, r, resultWho,
6436 requestCode, resultCode, data);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07006437 }
6438 }
6439 }
6440
6441 @Override
6442 public void clearPendingResultForActivity(IBinder activityToken,
6443 WeakReference<PendingIntentRecord> pir) {
6444 synchronized (mGlobalLock) {
6445 final ActivityRecord r = ActivityRecord.isInStackLocked(activityToken);
6446 if (r != null && r.pendingResults != null) {
6447 r.pendingResults.remove(pir);
6448 }
6449 }
6450 }
6451
6452 @Override
6453 public IIntentSender getIntentSender(int type, String packageName,
6454 int callingUid, int userId, IBinder token, String resultWho,
6455 int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
6456 Bundle bOptions) {
6457 synchronized (mGlobalLock) {
6458 return getIntentSenderLocked(type, packageName, callingUid, userId, token,
6459 resultWho, requestCode, intents, resolvedTypes, flags, bOptions);
6460 }
6461 }
Wale Ogunwalec4e63a42018-10-02 13:19:54 -07006462
6463 @Override
6464 public ActivityServiceConnectionsHolder getServiceConnectionsHolder(IBinder token) {
6465 synchronized (mGlobalLock) {
6466 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
6467 if (r == null) {
6468 return null;
6469 }
6470 if (r.mServiceConnectionsHolder == null) {
6471 r.mServiceConnectionsHolder = new ActivityServiceConnectionsHolder(
6472 ActivityTaskManagerService.this, r);
6473 }
6474
6475 return r.mServiceConnectionsHolder;
6476 }
6477 }
Wale Ogunwale214f3482018-10-04 11:00:47 -07006478
6479 @Override
6480 public Intent getHomeIntent() {
6481 synchronized (mGlobalLock) {
6482 return ActivityTaskManagerService.this.getHomeIntent();
6483 }
6484 }
6485
6486 @Override
6487 public boolean startHomeActivity(int userId, String reason) {
6488 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006489 return mRootActivityContainer.startHomeOnDisplay(userId, reason, DEFAULT_DISPLAY);
Louis Chang89f43fc2018-10-05 10:59:14 +08006490 }
6491 }
6492
6493 @Override
Chilun8b1f1be2019-03-13 17:14:36 +08006494 public boolean startHomeOnDisplay(int userId, String reason, int displayId,
6495 boolean fromHomeKey) {
Chilun5fd56542019-03-21 19:51:37 +08006496 synchronized (mGlobalLock) {
6497 return mRootActivityContainer.startHomeOnDisplay(userId, reason, displayId,
6498 fromHomeKey);
6499 }
Chilun8b1f1be2019-03-13 17:14:36 +08006500 }
6501
6502 @Override
Louis Chang89f43fc2018-10-05 10:59:14 +08006503 public boolean startHomeOnAllDisplays(int userId, String reason) {
6504 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006505 return mRootActivityContainer.startHomeOnAllDisplays(userId, reason);
Wale Ogunwale214f3482018-10-04 11:00:47 -07006506 }
6507 }
6508
Riddle Hsua0536432019-02-16 00:38:59 +08006509 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale214f3482018-10-04 11:00:47 -07006510 @Override
6511 public boolean isFactoryTestProcess(WindowProcessController wpc) {
Riddle Hsua0536432019-02-16 00:38:59 +08006512 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale214f3482018-10-04 11:00:47 -07006513 if (mFactoryTest == FACTORY_TEST_OFF) {
6514 return false;
6515 }
6516 if (mFactoryTest == FACTORY_TEST_LOW_LEVEL && mTopComponent != null
6517 && wpc.mName.equals(mTopComponent.getPackageName())) {
6518 return true;
6519 }
6520 return mFactoryTest == FACTORY_TEST_HIGH_LEVEL
6521 && (wpc.mInfo.flags & FLAG_FACTORY_TEST) != 0;
6522 }
6523 }
6524
6525 @Override
6526 public void updateTopComponentForFactoryTest() {
6527 synchronized (mGlobalLock) {
6528 if (mFactoryTest != FACTORY_TEST_LOW_LEVEL) {
6529 return;
6530 }
6531 final ResolveInfo ri = mContext.getPackageManager()
6532 .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), STOCK_PM_FLAGS);
6533 final CharSequence errorMsg;
6534 if (ri != null) {
6535 final ActivityInfo ai = ri.activityInfo;
6536 final ApplicationInfo app = ai.applicationInfo;
6537 if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
6538 mTopAction = Intent.ACTION_FACTORY_TEST;
6539 mTopData = null;
6540 mTopComponent = new ComponentName(app.packageName, ai.name);
6541 errorMsg = null;
6542 } else {
6543 errorMsg = mContext.getResources().getText(
6544 com.android.internal.R.string.factorytest_not_system);
6545 }
6546 } else {
6547 errorMsg = mContext.getResources().getText(
6548 com.android.internal.R.string.factorytest_no_action);
6549 }
6550 if (errorMsg == null) {
6551 return;
6552 }
6553
6554 mTopAction = null;
6555 mTopData = null;
6556 mTopComponent = null;
6557 mUiHandler.post(() -> {
6558 Dialog d = new FactoryErrorDialog(mUiContext, errorMsg);
6559 d.show();
Wale Ogunwale342fbe92018-10-09 08:44:10 -07006560 mAmInternal.ensureBootCompleted();
Wale Ogunwale214f3482018-10-04 11:00:47 -07006561 });
6562 }
6563 }
Wale Ogunwale31913b52018-10-13 08:29:31 -07006564
Riddle Hsua0536432019-02-16 00:38:59 +08006565 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale31913b52018-10-13 08:29:31 -07006566 @Override
6567 public void handleAppDied(WindowProcessController wpc, boolean restarting,
6568 Runnable finishInstrumentationCallback) {
Riddle Hsua0536432019-02-16 00:38:59 +08006569 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale31913b52018-10-13 08:29:31 -07006570 // Remove this application's activities from active lists.
Wale Ogunwaled32da472018-11-16 07:19:28 -08006571 boolean hasVisibleActivities = mRootActivityContainer.handleAppDied(wpc);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006572
6573 wpc.clearRecentTasks();
6574 wpc.clearActivities();
6575
6576 if (wpc.isInstrumenting()) {
6577 finishInstrumentationCallback.run();
6578 }
6579
Jorim Jaggid0752812018-10-16 16:07:20 +02006580 if (!restarting && hasVisibleActivities) {
6581 mWindowManager.deferSurfaceLayout();
6582 try {
6583 if (!mRootActivityContainer.resumeFocusedStacksTopActivities()) {
6584 // If there was nothing to resume, and we are not already restarting
6585 // this process, but there is a visible activity that is hosted by the
6586 // process...then make sure all visible activities are running, taking
6587 // care of restarting this process.
6588 mRootActivityContainer.ensureActivitiesVisible(null, 0,
6589 !PRESERVE_WINDOWS);
6590 }
6591 } finally {
6592 mWindowManager.continueSurfaceLayout();
Wale Ogunwale31913b52018-10-13 08:29:31 -07006593 }
Wale Ogunwale31913b52018-10-13 08:29:31 -07006594 }
6595 }
6596 }
6597
6598 @Override
6599 public void closeSystemDialogs(String reason) {
6600 enforceNotIsolatedCaller("closeSystemDialogs");
6601
6602 final int pid = Binder.getCallingPid();
6603 final int uid = Binder.getCallingUid();
6604 final long origId = Binder.clearCallingIdentity();
6605 try {
6606 synchronized (mGlobalLock) {
6607 // Only allow this from foreground processes, so that background
6608 // applications can't abuse it to prevent system UI from being shown.
6609 if (uid >= FIRST_APPLICATION_UID) {
6610 final WindowProcessController proc = mPidMap.get(pid);
6611 if (!proc.isPerceptible()) {
6612 Slog.w(TAG, "Ignoring closeSystemDialogs " + reason
6613 + " from background process " + proc);
6614 return;
6615 }
6616 }
Wale Ogunwale31913b52018-10-13 08:29:31 -07006617 mWindowManager.closeSystemDialogs(reason);
6618
Wale Ogunwaled32da472018-11-16 07:19:28 -08006619 mRootActivityContainer.closeSystemDialogs();
Wale Ogunwale31913b52018-10-13 08:29:31 -07006620 }
Wale Ogunwale2ea36d42018-10-18 10:27:31 -07006621 // Call into AM outside the synchronized block.
6622 mAmInternal.broadcastCloseSystemDialogs(reason);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006623 } finally {
6624 Binder.restoreCallingIdentity(origId);
6625 }
6626 }
6627
6628 @Override
6629 public void cleanupDisabledPackageComponents(
6630 String packageName, Set<String> disabledClasses, int userId, boolean booted) {
6631 synchronized (mGlobalLock) {
6632 // Clean-up disabled activities.
Wale Ogunwaled32da472018-11-16 07:19:28 -08006633 if (mRootActivityContainer.finishDisabledPackageActivities(
Wale Ogunwale31913b52018-10-13 08:29:31 -07006634 packageName, disabledClasses, true, false, userId) && booted) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006635 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwale31913b52018-10-13 08:29:31 -07006636 mStackSupervisor.scheduleIdleLocked();
6637 }
6638
6639 // Clean-up disabled tasks
6640 getRecentTasks().cleanupDisabledPackageTasksLocked(
6641 packageName, disabledClasses, userId);
6642 }
6643 }
6644
6645 @Override
6646 public boolean onForceStopPackage(String packageName, boolean doit, boolean evenPersistent,
6647 int userId) {
6648 synchronized (mGlobalLock) {
6649
6650 boolean didSomething =
6651 getActivityStartController().clearPendingActivityLaunches(packageName);
Wale Ogunwaled32da472018-11-16 07:19:28 -08006652 didSomething |= mRootActivityContainer.finishDisabledPackageActivities(packageName,
Wale Ogunwale31913b52018-10-13 08:29:31 -07006653 null, doit, evenPersistent, userId);
6654 return didSomething;
6655 }
6656 }
6657
6658 @Override
6659 public void resumeTopActivities(boolean scheduleIdle) {
6660 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006661 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwale31913b52018-10-13 08:29:31 -07006662 if (scheduleIdle) {
6663 mStackSupervisor.scheduleIdleLocked();
6664 }
6665 }
6666 }
6667
Riddle Hsua0536432019-02-16 00:38:59 +08006668 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale31913b52018-10-13 08:29:31 -07006669 @Override
6670 public void preBindApplication(WindowProcessController wpc) {
Riddle Hsua0536432019-02-16 00:38:59 +08006671 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale31913b52018-10-13 08:29:31 -07006672 mStackSupervisor.getActivityMetricsLogger().notifyBindApplication(wpc.mInfo);
6673 }
6674 }
6675
Riddle Hsua0536432019-02-16 00:38:59 +08006676 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale31913b52018-10-13 08:29:31 -07006677 @Override
6678 public boolean attachApplication(WindowProcessController wpc) throws RemoteException {
Riddle Hsua0536432019-02-16 00:38:59 +08006679 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08006680 return mRootActivityContainer.attachApplication(wpc);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006681 }
6682 }
6683
6684 @Override
6685 public void notifyLockedProfile(@UserIdInt int userId, int currentUserId) {
6686 try {
6687 if (!AppGlobals.getPackageManager().isUidPrivileged(Binder.getCallingUid())) {
6688 throw new SecurityException("Only privileged app can call notifyLockedProfile");
6689 }
6690 } catch (RemoteException ex) {
6691 throw new SecurityException("Fail to check is caller a privileged app", ex);
6692 }
6693
6694 synchronized (mGlobalLock) {
6695 final long ident = Binder.clearCallingIdentity();
6696 try {
6697 if (mAmInternal.shouldConfirmCredentials(userId)) {
6698 if (mKeyguardController.isKeyguardLocked()) {
6699 // Showing launcher to avoid user entering credential twice.
6700 startHomeActivity(currentUserId, "notifyLockedProfile");
6701 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08006702 mRootActivityContainer.lockAllProfileTasks(userId);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006703 }
6704 } finally {
6705 Binder.restoreCallingIdentity(ident);
6706 }
6707 }
6708 }
6709
6710 @Override
6711 public void startConfirmDeviceCredentialIntent(Intent intent, Bundle options) {
6712 mAmInternal.enforceCallingPermission(
6713 MANAGE_ACTIVITY_STACKS, "startConfirmDeviceCredentialIntent");
6714
6715 synchronized (mGlobalLock) {
6716 final long ident = Binder.clearCallingIdentity();
6717 try {
6718 intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |
6719 FLAG_ACTIVITY_TASK_ON_HOME);
6720 ActivityOptions activityOptions = options != null
6721 ? new ActivityOptions(options) : ActivityOptions.makeBasic();
Martijn Coenen2a5c8392018-10-01 10:17:06 +02006722 final ActivityRecord homeActivity =
Wale Ogunwaled32da472018-11-16 07:19:28 -08006723 mRootActivityContainer.getDefaultDisplayHomeActivity();
Martijn Coenen2a5c8392018-10-01 10:17:06 +02006724 if (homeActivity != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08006725 activityOptions.setLaunchTaskId(homeActivity.getTaskRecord().taskId);
Martijn Coenen2a5c8392018-10-01 10:17:06 +02006726 }
Wale Ogunwale31913b52018-10-13 08:29:31 -07006727 mContext.startActivityAsUser(intent, activityOptions.toBundle(),
6728 UserHandle.CURRENT);
6729 } finally {
6730 Binder.restoreCallingIdentity(ident);
6731 }
6732 }
6733 }
6734
6735 @Override
6736 public void writeActivitiesToProto(ProtoOutputStream proto) {
6737 synchronized (mGlobalLock) {
6738 // The output proto of "activity --proto activities"
6739 // is ActivityManagerServiceDumpActivitiesProto
Wale Ogunwaled32da472018-11-16 07:19:28 -08006740 mRootActivityContainer.writeToProto(proto,
Nataniel Borges023ecb52019-01-16 14:15:43 -08006741 ActivityManagerServiceDumpActivitiesProto.ACTIVITY_STACK_SUPERVISOR,
6742 WindowTraceLogLevel.ALL);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006743 }
6744 }
6745
6746 @Override
6747 public void saveANRState(String reason) {
6748 synchronized (mGlobalLock) {
6749 final StringWriter sw = new StringWriter();
6750 final PrintWriter pw = new FastPrintWriter(sw, false, 1024);
6751 pw.println(" ANR time: " + DateFormat.getDateTimeInstance().format(new Date()));
6752 if (reason != null) {
6753 pw.println(" Reason: " + reason);
6754 }
6755 pw.println();
6756 getActivityStartController().dump(pw, " ", null);
6757 pw.println();
6758 pw.println("-------------------------------------------------------------------------------");
6759 dumpActivitiesLocked(null /* fd */, pw, null /* args */, 0 /* opti */,
6760 true /* dumpAll */, false /* dumpClient */, null /* dumpPackage */,
6761 "" /* header */);
6762 pw.println();
6763 pw.close();
6764
6765 mLastANRState = sw.toString();
6766 }
6767 }
6768
6769 @Override
6770 public void clearSavedANRState() {
6771 synchronized (mGlobalLock) {
6772 mLastANRState = null;
6773 }
6774 }
6775
6776 @Override
6777 public void dump(String cmd, FileDescriptor fd, PrintWriter pw, String[] args, int opti,
6778 boolean dumpAll, boolean dumpClient, String dumpPackage) {
6779 synchronized (mGlobalLock) {
6780 if (DUMP_ACTIVITIES_CMD.equals(cmd) || DUMP_ACTIVITIES_SHORT_CMD.equals(cmd)) {
6781 dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
6782 } else if (DUMP_LASTANR_CMD.equals(cmd)) {
6783 dumpLastANRLocked(pw);
6784 } else if (DUMP_LASTANR_TRACES_CMD.equals(cmd)) {
6785 dumpLastANRTracesLocked(pw);
6786 } else if (DUMP_STARTER_CMD.equals(cmd)) {
6787 dumpActivityStarterLocked(pw, dumpPackage);
6788 } else if (DUMP_CONTAINERS_CMD.equals(cmd)) {
6789 dumpActivityContainersLocked(pw);
6790 } else if (DUMP_RECENTS_CMD.equals(cmd) || DUMP_RECENTS_SHORT_CMD.equals(cmd)) {
6791 if (getRecentTasks() != null) {
6792 getRecentTasks().dump(pw, dumpAll, dumpPackage);
6793 }
6794 }
6795 }
6796 }
6797
6798 @Override
6799 public boolean dumpForProcesses(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
6800 String dumpPackage, int dumpAppId, boolean needSep, boolean testPssMode,
6801 int wakefulness) {
6802 synchronized (mGlobalLock) {
6803 if (mHomeProcess != null && (dumpPackage == null
6804 || mHomeProcess.mPkgList.contains(dumpPackage))) {
6805 if (needSep) {
6806 pw.println();
6807 needSep = false;
6808 }
6809 pw.println(" mHomeProcess: " + mHomeProcess);
6810 }
6811 if (mPreviousProcess != null && (dumpPackage == null
6812 || mPreviousProcess.mPkgList.contains(dumpPackage))) {
6813 if (needSep) {
6814 pw.println();
6815 needSep = false;
6816 }
6817 pw.println(" mPreviousProcess: " + mPreviousProcess);
6818 }
6819 if (dumpAll && (mPreviousProcess == null || dumpPackage == null
6820 || mPreviousProcess.mPkgList.contains(dumpPackage))) {
6821 StringBuilder sb = new StringBuilder(128);
6822 sb.append(" mPreviousProcessVisibleTime: ");
6823 TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb);
6824 pw.println(sb);
6825 }
6826 if (mHeavyWeightProcess != null && (dumpPackage == null
6827 || mHeavyWeightProcess.mPkgList.contains(dumpPackage))) {
6828 if (needSep) {
6829 pw.println();
6830 needSep = false;
6831 }
6832 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
6833 }
6834 if (dumpPackage == null) {
6835 pw.println(" mGlobalConfiguration: " + getGlobalConfiguration());
Wale Ogunwaled32da472018-11-16 07:19:28 -08006836 mRootActivityContainer.dumpDisplayConfigs(pw, " ");
Wale Ogunwale31913b52018-10-13 08:29:31 -07006837 }
6838 if (dumpAll) {
6839 if (dumpPackage == null) {
6840 pw.println(" mConfigWillChange: "
6841 + getTopDisplayFocusedStack().mConfigWillChange);
6842 }
6843 if (mCompatModePackages.getPackages().size() > 0) {
6844 boolean printed = false;
6845 for (Map.Entry<String, Integer> entry
6846 : mCompatModePackages.getPackages().entrySet()) {
6847 String pkg = entry.getKey();
6848 int mode = entry.getValue();
6849 if (dumpPackage != null && !dumpPackage.equals(pkg)) {
6850 continue;
6851 }
6852 if (!printed) {
6853 pw.println(" mScreenCompatPackages:");
6854 printed = true;
6855 }
6856 pw.println(" " + pkg + ": " + mode);
6857 }
6858 }
6859 }
6860
6861 if (dumpPackage == null) {
6862 pw.println(" mWakefulness="
6863 + PowerManagerInternal.wakefulnessToString(wakefulness));
Wale Ogunwaled32da472018-11-16 07:19:28 -08006864 pw.println(" mSleepTokens=" + mRootActivityContainer.mSleepTokens);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006865 if (mRunningVoice != null) {
6866 pw.println(" mRunningVoice=" + mRunningVoice);
6867 pw.println(" mVoiceWakeLock" + mVoiceWakeLock);
6868 }
6869 pw.println(" mSleeping=" + mSleeping);
6870 pw.println(" mShuttingDown=" + mShuttingDown + " mTestPssMode=" + testPssMode);
6871 pw.println(" mVrController=" + mVrController);
6872 }
6873 if (mCurAppTimeTracker != null) {
6874 mCurAppTimeTracker.dumpWithHeader(pw, " ", true);
6875 }
6876 if (mAllowAppSwitchUids.size() > 0) {
6877 boolean printed = false;
6878 for (int i = 0; i < mAllowAppSwitchUids.size(); i++) {
6879 ArrayMap<String, Integer> types = mAllowAppSwitchUids.valueAt(i);
6880 for (int j = 0; j < types.size(); j++) {
6881 if (dumpPackage == null ||
6882 UserHandle.getAppId(types.valueAt(j).intValue()) == dumpAppId) {
6883 if (needSep) {
6884 pw.println();
6885 needSep = false;
6886 }
6887 if (!printed) {
6888 pw.println(" mAllowAppSwitchUids:");
6889 printed = true;
6890 }
6891 pw.print(" User ");
6892 pw.print(mAllowAppSwitchUids.keyAt(i));
6893 pw.print(": Type ");
6894 pw.print(types.keyAt(j));
6895 pw.print(" = ");
6896 UserHandle.formatUid(pw, types.valueAt(j).intValue());
6897 pw.println();
6898 }
6899 }
6900 }
6901 }
6902 if (dumpPackage == null) {
6903 if (mController != null) {
6904 pw.println(" mController=" + mController
6905 + " mControllerIsAMonkey=" + mControllerIsAMonkey);
6906 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08006907 pw.println(" mGoingToSleepWakeLock=" + mStackSupervisor.mGoingToSleepWakeLock);
6908 pw.println(" mLaunchingActivityWakeLock="
6909 + mStackSupervisor.mLaunchingActivityWakeLock);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006910 }
6911
6912 return needSep;
6913 }
6914 }
6915
6916 @Override
sanryhuang498e77e2018-12-06 14:57:01 +08006917 public void writeProcessesToProto(ProtoOutputStream proto, String dumpPackage,
6918 int wakeFullness, boolean testPssMode) {
Wale Ogunwale31913b52018-10-13 08:29:31 -07006919 synchronized (mGlobalLock) {
6920 if (dumpPackage == null) {
6921 getGlobalConfiguration().writeToProto(proto, GLOBAL_CONFIGURATION);
6922 proto.write(CONFIG_WILL_CHANGE, getTopDisplayFocusedStack().mConfigWillChange);
sanryhuang498e77e2018-12-06 14:57:01 +08006923 writeSleepStateToProto(proto, wakeFullness, testPssMode);
6924 if (mRunningVoice != null) {
6925 final long vrToken = proto.start(
6926 ActivityManagerServiceDumpProcessesProto.RUNNING_VOICE);
6927 proto.write(ActivityManagerServiceDumpProcessesProto.Voice.SESSION,
6928 mRunningVoice.toString());
6929 mVoiceWakeLock.writeToProto(
6930 proto, ActivityManagerServiceDumpProcessesProto.Voice.WAKELOCK);
6931 proto.end(vrToken);
6932 }
6933 mVrController.writeToProto(proto,
6934 ActivityManagerServiceDumpProcessesProto.VR_CONTROLLER);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006935 if (mController != null) {
6936 final long token = proto.start(CONTROLLER);
6937 proto.write(CONTROLLER, mController.toString());
6938 proto.write(IS_A_MONKEY, mControllerIsAMonkey);
6939 proto.end(token);
6940 }
Andrii Kulianc598b2d2019-02-07 17:16:38 -08006941 mStackSupervisor.mGoingToSleepWakeLock.writeToProto(proto, GOING_TO_SLEEP);
6942 mStackSupervisor.mLaunchingActivityWakeLock.writeToProto(proto,
6943 LAUNCHING_ACTIVITY);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006944 }
6945
6946 if (mHomeProcess != null && (dumpPackage == null
6947 || mHomeProcess.mPkgList.contains(dumpPackage))) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07006948 mHomeProcess.writeToProto(proto, HOME_PROC);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006949 }
6950
6951 if (mPreviousProcess != null && (dumpPackage == null
6952 || mPreviousProcess.mPkgList.contains(dumpPackage))) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07006953 mPreviousProcess.writeToProto(proto, PREVIOUS_PROC);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006954 proto.write(PREVIOUS_PROC_VISIBLE_TIME_MS, mPreviousProcessVisibleTime);
6955 }
6956
6957 if (mHeavyWeightProcess != null && (dumpPackage == null
6958 || mHeavyWeightProcess.mPkgList.contains(dumpPackage))) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07006959 mHeavyWeightProcess.writeToProto(proto, HEAVY_WEIGHT_PROC);
Wale Ogunwale31913b52018-10-13 08:29:31 -07006960 }
6961
6962 for (Map.Entry<String, Integer> entry
6963 : mCompatModePackages.getPackages().entrySet()) {
6964 String pkg = entry.getKey();
6965 int mode = entry.getValue();
6966 if (dumpPackage == null || dumpPackage.equals(pkg)) {
6967 long compatToken = proto.start(SCREEN_COMPAT_PACKAGES);
6968 proto.write(PACKAGE, pkg);
6969 proto.write(MODE, mode);
6970 proto.end(compatToken);
6971 }
6972 }
6973
6974 if (mCurAppTimeTracker != null) {
6975 mCurAppTimeTracker.writeToProto(proto, CURRENT_TRACKER, true);
6976 }
6977
6978 }
6979 }
6980
6981 @Override
6982 public boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name,
6983 String[] args, int opti, boolean dumpAll, boolean dumpVisibleStacksOnly,
6984 boolean dumpFocusedStackOnly) {
6985 synchronized (mGlobalLock) {
6986 return ActivityTaskManagerService.this.dumpActivity(fd, pw, name, args, opti,
6987 dumpAll, dumpVisibleStacksOnly, dumpFocusedStackOnly);
6988 }
6989 }
6990
6991 @Override
Wale Ogunwaled4d67d02018-10-25 18:09:39 -07006992 public void dumpForOom(PrintWriter pw) {
6993 synchronized (mGlobalLock) {
6994 pw.println(" mHomeProcess: " + mHomeProcess);
6995 pw.println(" mPreviousProcess: " + mPreviousProcess);
6996 if (mHeavyWeightProcess != null) {
6997 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
6998 }
6999 }
7000 }
7001
7002 @Override
Wale Ogunwale31913b52018-10-13 08:29:31 -07007003 public boolean canGcNow() {
7004 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007005 return isSleeping() || mRootActivityContainer.allResumedActivitiesIdle();
Wale Ogunwale31913b52018-10-13 08:29:31 -07007006 }
7007 }
7008
Riddle Hsua0536432019-02-16 00:38:59 +08007009 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwale31913b52018-10-13 08:29:31 -07007010 @Override
7011 public WindowProcessController getTopApp() {
Riddle Hsud7088f82019-01-30 13:04:50 +08007012 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007013 final ActivityRecord top = mRootActivityContainer.getTopResumedActivity();
Wale Ogunwale31913b52018-10-13 08:29:31 -07007014 return top != null ? top.app : null;
7015 }
7016 }
7017
Riddle Hsua0536432019-02-16 00:38:59 +08007018 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwale31913b52018-10-13 08:29:31 -07007019 @Override
7020 public void rankTaskLayersIfNeeded() {
Riddle Hsud7088f82019-01-30 13:04:50 +08007021 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007022 if (mRootActivityContainer != null) {
7023 mRootActivityContainer.rankTaskLayersIfNeeded();
Wale Ogunwale31913b52018-10-13 08:29:31 -07007024 }
7025 }
7026 }
7027
7028 @Override
7029 public void scheduleDestroyAllActivities(String reason) {
7030 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007031 mRootActivityContainer.scheduleDestroyAllActivities(null, reason);
Wale Ogunwale31913b52018-10-13 08:29:31 -07007032 }
7033 }
7034
7035 @Override
7036 public void removeUser(int userId) {
7037 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007038 mRootActivityContainer.removeUser(userId);
Wale Ogunwale31913b52018-10-13 08:29:31 -07007039 }
7040 }
7041
7042 @Override
7043 public boolean switchUser(int userId, UserState userState) {
7044 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007045 return mRootActivityContainer.switchUser(userId, userState);
Wale Ogunwale31913b52018-10-13 08:29:31 -07007046 }
7047 }
7048
7049 @Override
7050 public void onHandleAppCrash(WindowProcessController wpc) {
7051 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007052 mRootActivityContainer.handleAppCrash(wpc);
Wale Ogunwale31913b52018-10-13 08:29:31 -07007053 }
7054 }
Wale Ogunwale64258362018-10-16 15:13:37 -07007055
7056 @Override
7057 public int finishTopCrashedActivities(WindowProcessController crashedApp, String reason) {
7058 synchronized (mGlobalLock) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08007059 return mRootActivityContainer.finishTopCrashedActivities(crashedApp, reason);
Wale Ogunwale64258362018-10-16 15:13:37 -07007060 }
7061 }
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007062
Riddle Hsua0536432019-02-16 00:38:59 +08007063 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007064 @Override
7065 public void onUidActive(int uid, int procState) {
Riddle Hsua0536432019-02-16 00:38:59 +08007066 mActiveUids.onUidActive(uid, procState);
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007067 }
7068
Riddle Hsua0536432019-02-16 00:38:59 +08007069 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007070 @Override
7071 public void onUidInactive(int uid) {
Riddle Hsua0536432019-02-16 00:38:59 +08007072 mActiveUids.onUidInactive(uid);
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007073 }
7074
Riddle Hsua0536432019-02-16 00:38:59 +08007075 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007076 @Override
7077 public void onActiveUidsCleared() {
Riddle Hsua0536432019-02-16 00:38:59 +08007078 mActiveUids.onActiveUidsCleared();
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007079 }
7080
Riddle Hsua0536432019-02-16 00:38:59 +08007081 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007082 @Override
7083 public void onUidProcStateChanged(int uid, int procState) {
Riddle Hsua0536432019-02-16 00:38:59 +08007084 mActiveUids.onUidProcStateChanged(uid, procState);
Wale Ogunwalebff2df42018-10-18 17:09:19 -07007085 }
Wale Ogunwale9de19442018-10-18 19:05:03 -07007086
7087 @Override
7088 public void onUidAddedToPendingTempWhitelist(int uid, String tag) {
Riddle Hsud7088f82019-01-30 13:04:50 +08007089 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale9de19442018-10-18 19:05:03 -07007090 mPendingTempWhitelist.put(uid, tag);
7091 }
7092 }
7093
7094 @Override
7095 public void onUidRemovedFromPendingTempWhitelist(int uid) {
Riddle Hsud7088f82019-01-30 13:04:50 +08007096 synchronized (mGlobalLockWithoutBoost) {
Wale Ogunwale9de19442018-10-18 19:05:03 -07007097 mPendingTempWhitelist.remove(uid);
7098 }
7099 }
Wale Ogunwalee2172292018-10-25 10:11:10 -07007100
7101 @Override
7102 public boolean handleAppCrashInActivityController(String processName, int pid,
7103 String shortMsg, String longMsg, long timeMillis, String stackTrace,
7104 Runnable killCrashingAppCallback) {
7105 synchronized (mGlobalLock) {
7106 if (mController == null) {
7107 return false;
7108 }
7109
7110 try {
7111 if (!mController.appCrashed(processName, pid, shortMsg, longMsg, timeMillis,
7112 stackTrace)) {
7113 killCrashingAppCallback.run();
7114 return true;
7115 }
7116 } catch (RemoteException e) {
7117 mController = null;
7118 Watchdog.getInstance().setActivityController(null);
7119 }
7120 return false;
7121 }
7122 }
Wale Ogunwaled7889f52018-10-25 11:03:20 -07007123
7124 @Override
7125 public void removeRecentTasksByPackageName(String packageName, int userId) {
7126 synchronized (mGlobalLock) {
7127 mRecentTasks.removeTasksByPackageName(packageName, userId);
7128 }
7129 }
7130
7131 @Override
7132 public void cleanupRecentTasksForUser(int userId) {
7133 synchronized (mGlobalLock) {
7134 mRecentTasks.cleanupLocked(userId);
7135 }
7136 }
7137
7138 @Override
7139 public void loadRecentTasksForUser(int userId) {
7140 synchronized (mGlobalLock) {
7141 mRecentTasks.loadUserRecentsLocked(userId);
7142 }
7143 }
7144
7145 @Override
7146 public void onPackagesSuspendedChanged(String[] packages, boolean suspended, int userId) {
7147 synchronized (mGlobalLock) {
7148 mRecentTasks.onPackagesSuspendedChanged(packages, suspended, userId);
7149 }
7150 }
7151
7152 @Override
7153 public void flushRecentTasks() {
7154 mRecentTasks.flush();
7155 }
Wale Ogunwaled4d67d02018-10-25 18:09:39 -07007156
7157 @Override
7158 public WindowProcessController getHomeProcess() {
7159 synchronized (mGlobalLock) {
7160 return mHomeProcess;
7161 }
7162 }
7163
7164 @Override
7165 public WindowProcessController getPreviousProcess() {
7166 synchronized (mGlobalLock) {
7167 return mPreviousProcess;
7168 }
7169 }
Wale Ogunwale27c48ae2018-10-25 19:01:01 -07007170
7171 @Override
7172 public void clearLockedTasks(String reason) {
7173 synchronized (mGlobalLock) {
7174 getLockTaskController().clearLockedTasks(reason);
7175 }
7176 }
7177
7178 @Override
7179 public void updateUserConfiguration() {
7180 synchronized (mGlobalLock) {
7181 final Configuration configuration = new Configuration(getGlobalConfiguration());
7182 final int currentUserId = mAmInternal.getCurrentUserId();
7183 Settings.System.adjustConfigurationForUser(mContext.getContentResolver(),
7184 configuration, currentUserId, Settings.System.canWrite(mContext));
7185 updateConfigurationLocked(configuration, null /* starting */,
7186 false /* initLocale */, false /* persistent */, currentUserId,
7187 false /* deferResume */);
7188 }
7189 }
Wale Ogunwale387b34c2018-10-25 19:59:40 -07007190
7191 @Override
7192 public boolean canShowErrorDialogs() {
7193 synchronized (mGlobalLock) {
7194 return mShowDialogs && !mSleeping && !mShuttingDown
7195 && !mKeyguardController.isKeyguardOrAodShowing(DEFAULT_DISPLAY)
7196 && !hasUserRestriction(UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
7197 mAmInternal.getCurrentUserId())
7198 && !(UserManager.isDeviceInDemoMode(mContext)
7199 && mAmInternal.getCurrentUser().isDemo());
7200 }
7201 }
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08007202
7203 @Override
7204 public void setProfileApp(String profileApp) {
7205 synchronized (mGlobalLock) {
7206 mProfileApp = profileApp;
7207 }
7208 }
7209
7210 @Override
7211 public void setProfileProc(WindowProcessController wpc) {
7212 synchronized (mGlobalLock) {
7213 mProfileProc = wpc;
7214 }
7215 }
7216
7217 @Override
7218 public void setProfilerInfo(ProfilerInfo profilerInfo) {
7219 synchronized (mGlobalLock) {
7220 mProfilerInfo = profilerInfo;
7221 }
7222 }
Igor Murashkinc0b47e42018-11-07 15:54:18 -08007223
7224 @Override
7225 public ActivityMetricsLaunchObserverRegistry getLaunchObserverRegistry() {
7226 synchronized (mGlobalLock) {
7227 return mStackSupervisor.getActivityMetricsLogger().getLaunchObserverRegistry();
7228 }
7229 }
Winson Chung3fb0f252019-01-08 17:41:55 -08007230
7231 @Override
7232 public ActivityManager.TaskSnapshot getTaskSnapshot(int taskId, boolean reducedResolution) {
7233 synchronized (mGlobalLock) {
7234 return ActivityTaskManagerService.this.getTaskSnapshot(taskId, reducedResolution);
7235 }
7236 }
Michal Karpinskicc88d7e2019-01-24 15:32:12 +00007237
7238 @Override
7239 public boolean isUidForeground(int uid) {
7240 synchronized (mGlobalLock) {
7241 return ActivityTaskManagerService.this.isUidForeground(uid);
7242 }
7243 }
Michal Karpinski4026cae2019-02-12 11:51:47 +00007244
7245 @Override
7246 public void setDeviceOwnerPackageName(String deviceOwnerPkg) {
7247 synchronized (mGlobalLock) {
7248 ActivityTaskManagerService.this.setDeviceOwnerPackageName(deviceOwnerPkg);
7249 }
7250 }
Wale Ogunwale6767eae2018-05-03 15:52:51 -07007251 }
Wale Ogunwaleb73f3962018-11-20 07:58:22 -08007252}