blob: e0a9af543f99a6e2a3a727c32c095d8ccee64347 [file] [log] [blame]
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -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 Ogunwale9e4f3e02018-05-17 09:35:39 -070018
19import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
Louis Chang77ce34d2019-01-03 15:45:12 +080020import static android.os.Build.VERSION_CODES.Q;
Yunfan Chen79b96062018-10-17 12:45:23 -070021import static android.view.Display.INVALID_DISPLAY;
Yunfan Chen75157d72018-07-27 14:47:21 +090022
Wale Ogunwalee2172292018-10-25 10:11:10 -070023import static com.android.server.am.ActivityManagerService.MY_PID;
Wale Ogunwale59507092018-10-29 09:00:30 -070024import static com.android.server.wm.ActivityStack.ActivityState.DESTROYED;
25import static com.android.server.wm.ActivityStack.ActivityState.DESTROYING;
26import static com.android.server.wm.ActivityStack.ActivityState.PAUSED;
27import static com.android.server.wm.ActivityStack.ActivityState.PAUSING;
28import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
29import static com.android.server.wm.ActivityStack.ActivityState.STOPPING;
30import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
31import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RELEASE;
32import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFIGURATION;
33import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RELEASE;
34import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
35import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Michal Karpinskifc6872e2019-05-21 15:18:44 +010036import static com.android.server.wm.ActivityTaskManagerService.ACTIVITY_BG_START_GRACE_PERIOD_MS;
Louis Chang77ce34d2019-01-03 15:45:12 +080037import static com.android.server.wm.ActivityTaskManagerService.INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT_MS;
Wale Ogunwale59507092018-10-29 09:00:30 -070038import static com.android.server.wm.ActivityTaskManagerService.KEY_DISPATCHING_TIMEOUT_MS;
39import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070040
Louis Chang77ce34d2019-01-03 15:45:12 +080041import android.annotation.NonNull;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070042import android.app.Activity;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070043import android.app.ActivityThread;
44import android.app.IApplicationThread;
Wale Ogunwale9c103022018-10-18 07:44:54 -070045import android.app.ProfilerInfo;
Yunfan Chen75157d72018-07-27 14:47:21 +090046import android.app.servertransaction.ConfigurationChangeItem;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070047import android.content.Intent;
Riddle Hsu17e38422019-04-12 16:55:11 +080048import android.content.pm.ActivityInfo;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070049import android.content.pm.ApplicationInfo;
Yunfan Chen75157d72018-07-27 14:47:21 +090050import android.content.res.Configuration;
Yohei Yukawae2fa39e2018-09-22 13:13:10 -070051import android.os.Message;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070052import android.os.RemoteException;
Michal Karpinskifc6872e2019-05-21 15:18:44 +010053import android.os.SystemClock;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070054import android.util.ArraySet;
55import android.util.Log;
56import android.util.Slog;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070057import android.util.proto.ProtoOutputStream;
Jorim Jaggi589c5ba2019-07-30 16:50:13 +020058import android.view.IRemoteAnimationRunner;
Yunfan Chen79b96062018-10-17 12:45:23 -070059
Yunfan Chenb29cbfd2019-01-24 17:30:33 +090060import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070061import com.android.internal.app.HeavyWeightSwitcherActivity;
62import com.android.internal.util.function.pooled.PooledLambda;
Wale Ogunwalee2172292018-10-25 10:11:10 -070063import com.android.server.Watchdog;
Riddle Hsua0536432019-02-16 00:38:59 +080064import com.android.server.wm.ActivityTaskManagerService.HotPath;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070065
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -080066import java.io.IOException;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070067import java.io.PrintWriter;
68import java.util.ArrayList;
69
70/**
71 * The Activity Manager (AM) package manages the lifecycle of processes in the system through
Wale Ogunwale59507092018-10-29 09:00:30 -070072 * ProcessRecord. However, it is important for the Window Manager (WM) package to be aware
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070073 * of the processes and their state since it affects how WM manages windows and activities. This
Wale Ogunwale59507092018-10-29 09:00:30 -070074 * class that allows the ProcessRecord object in the AM package to communicate important
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070075 * changes to its state to the WM package in a structured way. WM package also uses
76 * {@link WindowProcessListener} to request changes to the process state on the AM side.
77 * Note that public calls into this class are assumed to be originating from outside the
78 * window manager so the window manager lock is held and appropriate permissions are checked before
79 * calls are allowed to proceed.
80 */
Yunfan Chen79b96062018-10-17 12:45:23 -070081public class WindowProcessController extends ConfigurationContainer<ConfigurationContainer>
82 implements ConfigurationContainerListener {
Wale Ogunwale98875612018-10-12 07:53:02 -070083 private static final String TAG = TAG_WITH_CLASS_NAME ? "WindowProcessController" : TAG_ATM;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070084 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Yunfan Chen75157d72018-07-27 14:47:21 +090085 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070086
87 // all about the first app in the process
88 final ApplicationInfo mInfo;
89 final String mName;
90 final int mUid;
91 // The process of this application; 0 if none
92 private volatile int mPid;
93 // user of process.
94 final int mUserId;
95 // The owner of this window process controller object. Mainly for identification when we
96 // communicate back to the activity manager side.
97 public final Object mOwner;
98 // List of packages running in the process
99 final ArraySet<String> mPkgList = new ArraySet<>();
100 private final WindowProcessListener mListener;
101 private final ActivityTaskManagerService mAtm;
102 // The actual proc... may be null only if 'persistent' is true (in which case we are in the
103 // process of launching the app)
Louis Chang4221d792019-04-17 17:15:01 +0800104 private IApplicationThread mThread;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700105 // Currently desired scheduling class
106 private volatile int mCurSchedGroup;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700107 // Currently computed process state
108 private volatile int mCurProcState = PROCESS_STATE_NONEXISTENT;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700109 // Last reported process state;
110 private volatile int mRepProcState = PROCESS_STATE_NONEXISTENT;
111 // are we in the process of crashing?
112 private volatile boolean mCrashing;
113 // does the app have a not responding dialog?
114 private volatile boolean mNotResponding;
115 // always keep this application running?
116 private volatile boolean mPersistent;
117 // The ABI this process was launched with
118 private volatile String mRequiredAbi;
119 // Running any services that are foreground?
120 private volatile boolean mHasForegroundServices;
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700121 // Running any activities that are foreground?
122 private volatile boolean mHasForegroundActivities;
123 // Are there any client services with activities?
124 private volatile boolean mHasClientActivities;
125 // Is this process currently showing a non-activity UI that the user is interacting with?
126 // E.g. The status bar when it is expanded, but not when it is minimized. When true the process
127 // will be set to use the ProcessList#SCHED_GROUP_TOP_APP scheduling group to boost performance.
128 private volatile boolean mHasTopUi;
129 // Is the process currently showing a non-activity UI that overlays on-top of activity UIs on
130 // screen. E.g. display a window of type
131 // android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY When true the process will
132 // oom adj score will be set to ProcessList#PERCEPTIBLE_APP_ADJ at minimum to reduce the chance
133 // of the process getting killed.
134 private volatile boolean mHasOverlayUi;
135 // Want to clean up resources from showing UI?
136 private volatile boolean mPendingUiClean;
137 // The time we sent the last interaction event
138 private volatile long mInteractionEventTime;
139 // When we became foreground for interaction purposes
140 private volatile long mFgInteractionTime;
141 // When (uptime) the process last became unimportant
142 private volatile long mWhenUnimportant;
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700143 // was app launched for debugging?
144 private volatile boolean mDebugging;
145 // Active instrumentation running in process?
146 private volatile boolean mInstrumenting;
Michal Karpinskidaef80f2019-01-29 16:50:51 +0000147 // Active instrumentation with background activity starts privilege running in process?
148 private volatile boolean mInstrumentingWithBackgroundActivityStartPrivileges;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700149 // This process it perceptible by the user.
150 private volatile boolean mPerceptible;
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700151 // Set to true when process was launched with a wrapper attached
152 private volatile boolean mUsingWrapper;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000153 // Set to true if this process is currently temporarily whitelisted to start activities even if
154 // it's not in the foreground
155 private volatile boolean mAllowBackgroundActivityStarts;
Michal Karpinskib7daac22019-03-25 10:12:41 +0000156 // Set of UIDs of clients currently bound to this process
157 private volatile ArraySet<Integer> mBoundClientUids = new ArraySet<Integer>();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700158
159 // Thread currently set for VR scheduling
160 int mVrThreadTid;
161
162 // all activities running in the process
163 private final ArrayList<ActivityRecord> mActivities = new ArrayList<>();
164 // any tasks this process had run root activities in
165 private final ArrayList<TaskRecord> mRecentTasks = new ArrayList<>();
Louis Chang77ce34d2019-01-03 15:45:12 +0800166 // The most recent top-most activity that was resumed in the process for pre-Q app.
167 private ActivityRecord mPreQTopResumedActivity = null;
Michal Karpinskifc6872e2019-05-21 15:18:44 +0100168 // The last time an activity was launched in the process
169 private long mLastActivityLaunchTime;
170 // The last time an activity was finished in the process while the process participated
171 // in a visible task
172 private long mLastActivityFinishTime;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700173
Yunfan Chen75157d72018-07-27 14:47:21 +0900174 // Last configuration that was reported to the process.
175 private final Configuration mLastReportedConfiguration;
Yunfan Chen79b96062018-10-17 12:45:23 -0700176 // Registered display id as a listener to override config change
177 private int mDisplayId;
Yunfan Chen75157d72018-07-27 14:47:21 +0900178
Jorim Jaggi589c5ba2019-07-30 16:50:13 +0200179 /** Whether our process is currently running a {@link RecentsAnimation} */
180 private boolean mRunningRecentsAnimation;
181
182 /** Whether our process is currently running a {@link IRemoteAnimationRunner} */
183 private boolean mRunningRemoteAnimation;
184
Wale Ogunwale59507092018-10-29 09:00:30 -0700185 public WindowProcessController(ActivityTaskManagerService atm, ApplicationInfo info,
186 String name, int uid, int userId, Object owner, WindowProcessListener listener) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700187 mInfo = info;
188 mName = name;
189 mUid = uid;
190 mUserId = userId;
191 mOwner = owner;
192 mListener = listener;
193 mAtm = atm;
Yunfan Chen75157d72018-07-27 14:47:21 +0900194 mLastReportedConfiguration = new Configuration();
Yunfan Chen79b96062018-10-17 12:45:23 -0700195 mDisplayId = INVALID_DISPLAY;
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700196 if (atm != null) {
197 onConfigurationChanged(atm.getGlobalConfiguration());
Yunfan Chen75157d72018-07-27 14:47:21 +0900198 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700199 }
200
201 public void setPid(int pid) {
202 mPid = pid;
203 }
204
Wale Ogunwale59507092018-10-29 09:00:30 -0700205 public int getPid() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700206 return mPid;
207 }
208
Louis Chang4221d792019-04-17 17:15:01 +0800209 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700210 public void setThread(IApplicationThread thread) {
Louis Chang4221d792019-04-17 17:15:01 +0800211 synchronized (mAtm.mGlobalLockWithoutBoost) {
212 mThread = thread;
213 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700214 }
215
216 IApplicationThread getThread() {
217 return mThread;
218 }
219
220 boolean hasThread() {
221 return mThread != null;
222 }
223
224 public void setCurrentSchedulingGroup(int curSchedGroup) {
225 mCurSchedGroup = curSchedGroup;
226 }
227
228 int getCurrentSchedulingGroup() {
229 return mCurSchedGroup;
230 }
231
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700232 public void setCurrentProcState(int curProcState) {
233 mCurProcState = curProcState;
234 }
235
236 int getCurrentProcState() {
237 return mCurProcState;
238 }
239
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700240 public void setReportedProcState(int repProcState) {
241 mRepProcState = repProcState;
242 }
243
244 int getReportedProcState() {
245 return mRepProcState;
246 }
247
248 public void setCrashing(boolean crashing) {
249 mCrashing = crashing;
250 }
251
252 boolean isCrashing() {
253 return mCrashing;
254 }
255
256 public void setNotResponding(boolean notResponding) {
257 mNotResponding = notResponding;
258 }
259
260 boolean isNotResponding() {
261 return mNotResponding;
262 }
263
264 public void setPersistent(boolean persistent) {
265 mPersistent = persistent;
266 }
267
268 boolean isPersistent() {
269 return mPersistent;
270 }
271
272 public void setHasForegroundServices(boolean hasForegroundServices) {
273 mHasForegroundServices = hasForegroundServices;
274 }
275
276 boolean hasForegroundServices() {
277 return mHasForegroundServices;
278 }
279
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700280 public void setHasForegroundActivities(boolean hasForegroundActivities) {
281 mHasForegroundActivities = hasForegroundActivities;
282 }
283
284 boolean hasForegroundActivities() {
285 return mHasForegroundActivities;
286 }
287
288 public void setHasClientActivities(boolean hasClientActivities) {
289 mHasClientActivities = hasClientActivities;
290 }
291
292 boolean hasClientActivities() {
293 return mHasClientActivities;
294 }
295
296 public void setHasTopUi(boolean hasTopUi) {
297 mHasTopUi = hasTopUi;
298 }
299
300 boolean hasTopUi() {
301 return mHasTopUi;
302 }
303
304 public void setHasOverlayUi(boolean hasOverlayUi) {
305 mHasOverlayUi = hasOverlayUi;
306 }
307
308 boolean hasOverlayUi() {
309 return mHasOverlayUi;
310 }
311
312 public void setPendingUiClean(boolean hasPendingUiClean) {
313 mPendingUiClean = hasPendingUiClean;
314 }
315
316 boolean hasPendingUiClean() {
317 return mPendingUiClean;
318 }
319
Yunfan Chenb4fe58c2019-03-27 18:35:06 +0900320 /** @return {@code true} if the process registered to a display as a config listener. */
321 boolean registeredForDisplayConfigChanges() {
322 return mDisplayId != INVALID_DISPLAY;
323 }
324
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700325 void postPendingUiCleanMsg(boolean pendingUiClean) {
326 if (mListener == null) return;
327 // Posting on handler so WM lock isn't held when we call into AM.
328 final Message m = PooledLambda.obtainMessage(
329 WindowProcessListener::setPendingUiClean, mListener, pendingUiClean);
330 mAtm.mH.sendMessage(m);
331 }
332
333 public void setInteractionEventTime(long interactionEventTime) {
334 mInteractionEventTime = interactionEventTime;
335 }
336
337 long getInteractionEventTime() {
338 return mInteractionEventTime;
339 }
340
341 public void setFgInteractionTime(long fgInteractionTime) {
342 mFgInteractionTime = fgInteractionTime;
343 }
344
345 long getFgInteractionTime() {
346 return mFgInteractionTime;
347 }
348
349 public void setWhenUnimportant(long whenUnimportant) {
350 mWhenUnimportant = whenUnimportant;
351 }
352
353 long getWhenUnimportant() {
354 return mWhenUnimportant;
355 }
356
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700357 public void setRequiredAbi(String requiredAbi) {
358 mRequiredAbi = requiredAbi;
359 }
360
361 String getRequiredAbi() {
362 return mRequiredAbi;
363 }
364
Yunfan Chenb29cbfd2019-01-24 17:30:33 +0900365 /** Returns ID of display overriding the configuration for this process, or
366 * INVALID_DISPLAY if no display is overriding. */
367 @VisibleForTesting
368 int getDisplayId() {
369 return mDisplayId;
370 }
371
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700372 public void setDebugging(boolean debugging) {
373 mDebugging = debugging;
374 }
375
376 boolean isDebugging() {
377 return mDebugging;
378 }
379
380 public void setUsingWrapper(boolean usingWrapper) {
381 mUsingWrapper = usingWrapper;
382 }
383
384 boolean isUsingWrapper() {
385 return mUsingWrapper;
386 }
387
Michal Karpinskifc6872e2019-05-21 15:18:44 +0100388 void setLastActivityLaunchTime(long launchTime) {
389 if (launchTime <= mLastActivityLaunchTime) {
390 return;
391 }
392 mLastActivityLaunchTime = launchTime;
393 }
394
395 void setLastActivityFinishTimeIfNeeded(long finishTime) {
396 if (finishTime <= mLastActivityFinishTime || !hasActivityInVisibleTask()) {
397 return;
398 }
399 mLastActivityFinishTime = finishTime;
400 }
401
Michal Karpinskiac116df2018-12-10 17:51:42 +0000402 public void setAllowBackgroundActivityStarts(boolean allowBackgroundActivityStarts) {
403 mAllowBackgroundActivityStarts = allowBackgroundActivityStarts;
404 }
405
Michal Karpinski2e0aad22019-04-12 16:22:55 +0100406 boolean areBackgroundActivityStartsAllowed() {
407 // allow if the whitelisting flag was explicitly set
408 if (mAllowBackgroundActivityStarts) {
409 return true;
410 }
Ricky Wai906af482019-06-03 17:25:28 +0100411 // allow if any activity in the caller has either started or finished very recently, and
412 // it must be started or finished after last stop app switches time.
Michal Karpinskifc6872e2019-05-21 15:18:44 +0100413 final long now = SystemClock.uptimeMillis();
414 if (now - mLastActivityLaunchTime < ACTIVITY_BG_START_GRACE_PERIOD_MS
415 || now - mLastActivityFinishTime < ACTIVITY_BG_START_GRACE_PERIOD_MS) {
Ricky Wai906af482019-06-03 17:25:28 +0100416 // if activity is started and finished before stop app switch time, we should not
417 // let app to be able to start background activity even it's in grace period.
418 if (mLastActivityLaunchTime > mAtm.getLastStopAppSwitchesTime()
419 || mLastActivityFinishTime > mAtm.getLastStopAppSwitchesTime()) {
420 return true;
421 }
Michal Karpinskifc6872e2019-05-21 15:18:44 +0100422 }
Michal Karpinski2e0aad22019-04-12 16:22:55 +0100423 // allow if the proc is instrumenting with background activity starts privs
424 if (mInstrumentingWithBackgroundActivityStartPrivileges) {
425 return true;
426 }
427 // allow if the caller has an activity in any foreground task
428 if (hasActivityInVisibleTask()) {
429 return true;
430 }
431 // allow if the caller is bound by a UID that's currently foreground
432 if (isBoundByForegroundUid()) {
433 return true;
434 }
435 return false;
436 }
437
438 private boolean isBoundByForegroundUid() {
439 for (int i = mBoundClientUids.size() - 1; i >= 0; --i) {
440 if (mAtm.isUidForeground(mBoundClientUids.valueAt(i))) {
441 return true;
442 }
443 }
444 return false;
Michal Karpinskiac116df2018-12-10 17:51:42 +0000445 }
446
Michal Karpinskib7daac22019-03-25 10:12:41 +0000447 public void setBoundClientUids(ArraySet<Integer> boundClientUids) {
448 mBoundClientUids = boundClientUids;
449 }
450
Michal Karpinski4bc56492019-01-31 12:07:33 +0000451 public void setInstrumenting(boolean instrumenting,
452 boolean hasBackgroundActivityStartPrivileges) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700453 mInstrumenting = instrumenting;
Michal Karpinski4bc56492019-01-31 12:07:33 +0000454 mInstrumentingWithBackgroundActivityStartPrivileges = hasBackgroundActivityStartPrivileges;
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700455 }
456
457 boolean isInstrumenting() {
458 return mInstrumenting;
459 }
460
Wale Ogunwale31913b52018-10-13 08:29:31 -0700461 public void setPerceptible(boolean perceptible) {
462 mPerceptible = perceptible;
463 }
464
465 boolean isPerceptible() {
466 return mPerceptible;
467 }
468
Yunfan Chen75157d72018-07-27 14:47:21 +0900469 @Override
470 protected int getChildCount() {
471 return 0;
472 }
473
474 @Override
475 protected ConfigurationContainer getChildAt(int index) {
476 return null;
477 }
478
479 @Override
480 protected ConfigurationContainer getParent() {
Andrii Kulian859f0a52019-05-20 12:18:09 +0000481 return null;
Yunfan Chen75157d72018-07-27 14:47:21 +0900482 }
483
Riddle Hsua0536432019-02-16 00:38:59 +0800484 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700485 public void addPackage(String packageName) {
Riddle Hsud7088f82019-01-30 13:04:50 +0800486 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700487 mPkgList.add(packageName);
488 }
489 }
490
Riddle Hsua0536432019-02-16 00:38:59 +0800491 @HotPath(caller = HotPath.PROCESS_CHANGE)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700492 public void clearPackageList() {
Riddle Hsud7088f82019-01-30 13:04:50 +0800493 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700494 mPkgList.clear();
495 }
496 }
497
498 void addActivityIfNeeded(ActivityRecord r) {
Michal Karpinskifc6872e2019-05-21 15:18:44 +0100499 // even if we already track this activity, note down that it has been launched
500 setLastActivityLaunchTime(r.lastLaunchTime);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700501 if (mActivities.contains(r)) {
502 return;
503 }
504 mActivities.add(r);
505 }
506
507 void removeActivity(ActivityRecord r) {
508 mActivities.remove(r);
509 }
510
Riddle Hsuaaef7312019-01-24 19:00:58 +0800511 void makeFinishingForProcessRemoved() {
512 for (int i = mActivities.size() - 1; i >= 0; --i) {
513 mActivities.get(i).makeFinishingLocked();
514 }
515 }
516
Riddle Hsud7088f82019-01-30 13:04:50 +0800517 void clearActivities() {
518 mActivities.clear();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700519 }
520
Riddle Hsua0536432019-02-16 00:38:59 +0800521 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700522 public boolean hasActivities() {
Riddle Hsud7088f82019-01-30 13:04:50 +0800523 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700524 return !mActivities.isEmpty();
525 }
526 }
527
Riddle Hsua0536432019-02-16 00:38:59 +0800528 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700529 public boolean hasVisibleActivities() {
Riddle Hsud7088f82019-01-30 13:04:50 +0800530 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700531 for (int i = mActivities.size() - 1; i >= 0; --i) {
532 final ActivityRecord r = mActivities.get(i);
533 if (r.visible) {
534 return true;
535 }
536 }
537 }
538 return false;
539 }
540
Riddle Hsua0536432019-02-16 00:38:59 +0800541 @HotPath(caller = HotPath.LRU_UPDATE)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700542 public boolean hasActivitiesOrRecentTasks() {
Riddle Hsud7088f82019-01-30 13:04:50 +0800543 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700544 return !mActivities.isEmpty() || !mRecentTasks.isEmpty();
545 }
546 }
547
Michal Karpinski2e0aad22019-04-12 16:22:55 +0100548 private boolean hasActivityInVisibleTask() {
Michal Karpinskicb6873c2019-03-26 17:12:25 +0000549 for (int i = mActivities.size() - 1; i >= 0; --i) {
550 TaskRecord task = mActivities.get(i).getTaskRecord();
551 if (task == null) {
552 continue;
553 }
554 ActivityRecord topActivity = task.getTopActivity();
Bernardo Rufino7a294132019-08-29 19:12:28 +0100555 if (topActivity != null && topActivity.visible) {
Michal Karpinskicb6873c2019-03-26 17:12:25 +0000556 return true;
557 }
558 }
559 return false;
560 }
561
Louis Chang77ce34d2019-01-03 15:45:12 +0800562 /**
563 * Update the top resuming activity in process for pre-Q apps, only the top-most visible
564 * activities are allowed to be resumed per process.
565 * @return {@code true} if the activity is allowed to be resumed by compatibility
566 * restrictions, which the activity was the topmost visible activity in process or the app is
Hongwei Wangacad2442019-07-24 11:23:57 -0700567 * targeting after Q. Note that non-focusable activity, in picture-in-picture mode for instance,
568 * does not count as a topmost activity.
Louis Chang77ce34d2019-01-03 15:45:12 +0800569 */
570 boolean updateTopResumingActivityInProcessIfNeeded(@NonNull ActivityRecord activity) {
571 if (mInfo.targetSdkVersion >= Q || mPreQTopResumedActivity == activity) {
572 return true;
573 }
574
575 final ActivityDisplay display = activity.getDisplay();
576 if (display == null) {
577 // No need to update if the activity hasn't attach to any display.
578 return false;
579 }
580
581 boolean canUpdate = false;
582 final ActivityDisplay topDisplay =
583 mPreQTopResumedActivity != null ? mPreQTopResumedActivity.getDisplay() : null;
Hongwei Wangacad2442019-07-24 11:23:57 -0700584 // Update the topmost activity if current top activity is
585 // - not on any display OR
586 // - no longer visible OR
587 // - not focusable (in PiP mode for instance)
588 if (topDisplay == null
589 || !mPreQTopResumedActivity.visible
590 || !mPreQTopResumedActivity.isFocusable()) {
Louis Chang77ce34d2019-01-03 15:45:12 +0800591 canUpdate = true;
592 }
593
594 // Update the topmost activity if the current top activity wasn't on top of the other one.
595 if (!canUpdate && topDisplay.mDisplayContent.compareTo(display.mDisplayContent) < 0) {
596 canUpdate = true;
597 }
598
599 // Compare the z-order of ActivityStacks if both activities landed on same display.
600 if (display == topDisplay
601 && mPreQTopResumedActivity.getActivityStack().mTaskStack.compareTo(
602 activity.getActivityStack().mTaskStack) <= 0) {
603 canUpdate = true;
604 }
605
606 if (canUpdate) {
607 // Make sure the previous top activity in the process no longer be resumed.
608 if (mPreQTopResumedActivity != null && mPreQTopResumedActivity.isState(RESUMED)) {
609 final ActivityStack stack = mPreQTopResumedActivity.getActivityStack();
610 if (stack != null) {
611 stack.startPausingLocked(false /* userLeaving */, false /* uiSleeping */,
612 null /* resuming */, false /* pauseImmediately */);
613 }
614 }
615 mPreQTopResumedActivity = activity;
616 }
617 return canUpdate;
618 }
619
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700620 public void stopFreezingActivities() {
621 synchronized (mAtm.mGlobalLock) {
622 int i = mActivities.size();
623 while (i > 0) {
624 i--;
625 mActivities.get(i).stopFreezingScreenLocked(true);
626 }
627 }
628 }
629
Riddle Hsud7088f82019-01-30 13:04:50 +0800630 void finishActivities() {
631 ArrayList<ActivityRecord> activities = new ArrayList<>(mActivities);
632 for (int i = 0; i < activities.size(); i++) {
633 final ActivityRecord r = activities.get(i);
634 if (!r.finishing && r.isInStackLocked()) {
635 r.getActivityStack().finishActivityLocked(r, Activity.RESULT_CANCELED,
Wale Ogunwale586a8ee2019-06-04 13:44:14 +0000636 null, "finish-heavy", true);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700637 }
638 }
639 }
640
641 public boolean isInterestingToUser() {
642 synchronized (mAtm.mGlobalLock) {
643 final int size = mActivities.size();
644 for (int i = 0; i < size; i++) {
645 ActivityRecord r = mActivities.get(i);
646 if (r.isInterestingToUserLocked()) {
647 return true;
648 }
649 }
650 }
651 return false;
652 }
653
654 public boolean hasRunningActivity(String packageName) {
655 synchronized (mAtm.mGlobalLock) {
656 for (int i = mActivities.size() - 1; i >= 0; --i) {
657 final ActivityRecord r = mActivities.get(i);
658 if (packageName.equals(r.packageName)) {
659 return true;
660 }
661 }
662 }
663 return false;
664 }
665
666 public void clearPackagePreferredForHomeActivities() {
667 synchronized (mAtm.mGlobalLock) {
668 for (int i = mActivities.size() - 1; i >= 0; --i) {
669 final ActivityRecord r = mActivities.get(i);
670 if (r.isActivityTypeHome()) {
671 Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
672 try {
673 ActivityThread.getPackageManager()
674 .clearPackagePreferredActivities(r.packageName);
675 } catch (RemoteException c) {
676 // pm is in same process, this will never happen.
677 }
678 }
679 }
680 }
681 }
682
683 boolean hasStartedActivity(ActivityRecord launchedActivity) {
684 for (int i = mActivities.size() - 1; i >= 0; i--) {
685 final ActivityRecord activity = mActivities.get(i);
686 if (launchedActivity == activity) {
687 continue;
688 }
689 if (!activity.stopped) {
690 return true;
691 }
692 }
693 return false;
694 }
695
696
Riddle Hsud7088f82019-01-30 13:04:50 +0800697 void updateIntentForHeavyWeightActivity(Intent intent) {
698 if (mActivities.isEmpty()) {
699 return;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700700 }
Riddle Hsud7088f82019-01-30 13:04:50 +0800701 ActivityRecord hist = mActivities.get(0);
702 intent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP, hist.packageName);
703 intent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK, hist.getTaskRecord().taskId);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700704 }
705
706 boolean shouldKillProcessForRemovedTask(TaskRecord tr) {
707 for (int k = 0; k < mActivities.size(); k++) {
Yunfan Chenafc15832018-07-26 16:34:28 +0900708 final ActivityRecord activity = mActivities.get(k);
709 if (!activity.stopped) {
710 // Don't kill process(es) that has an activity not stopped.
711 return false;
712 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800713 final TaskRecord otherTask = activity.getTaskRecord();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700714 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
715 // Don't kill process(es) that has an activity in a different task that is
716 // also in recents.
717 return false;
718 }
719 }
720 return true;
721 }
722
723 ArraySet<TaskRecord> getReleaseSomeActivitiesTasks() {
724 // Examine all activities currently running in the process.
725 TaskRecord firstTask = null;
726 // Tasks is non-null only if two or more tasks are found.
727 ArraySet<TaskRecord> tasks = null;
728 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + this);
729 for (int i = 0; i < mActivities.size(); i++) {
730 final ActivityRecord r = mActivities.get(i);
731 // First, if we find an activity that is in the process of being destroyed,
732 // then we just aren't going to do anything for now; we want things to settle
733 // down before we try to prune more activities.
734 if (r.finishing || r.isState(DESTROYING, DESTROYED)) {
735 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
736 return null;
737 }
738 // Don't consider any activies that are currently not in a state where they
739 // can be destroyed.
740 if (r.visible || !r.stopped || !r.haveState
741 || r.isState(RESUMED, PAUSING, PAUSED, STOPPING)) {
742 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
743 continue;
744 }
745
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800746 final TaskRecord task = r.getTaskRecord();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700747 if (task != null) {
748 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
749 + " from " + r);
750 if (firstTask == null) {
751 firstTask = task;
752 } else if (firstTask != task) {
753 if (tasks == null) {
754 tasks = new ArraySet<>();
755 tasks.add(firstTask);
756 }
757 tasks.add(task);
758 }
759 }
760 }
761
762 return tasks;
763 }
764
765 public interface ComputeOomAdjCallback {
766 void onVisibleActivity();
767 void onPausedActivity();
768 void onStoppingActivity(boolean finishing);
769 void onOtherActivity();
770 }
771
Riddle Hsua0536432019-02-16 00:38:59 +0800772 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700773 public int computeOomAdjFromActivities(int minTaskLayer, ComputeOomAdjCallback callback) {
Riddle Hsud7088f82019-01-30 13:04:50 +0800774 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700775 final int activitiesSize = mActivities.size();
776 for (int j = 0; j < activitiesSize; j++) {
777 final ActivityRecord r = mActivities.get(j);
778 if (r.app != this) {
779 Log.e(TAG, "Found activity " + r + " in proc activity list using " + r.app
780 + " instead of expected " + this);
781 if (r.app == null || (r.app.mUid == mUid)) {
782 // Only fix things up when they look sane
783 r.setProcess(this);
784 } else {
785 continue;
786 }
787 }
788 if (r.visible) {
789 callback.onVisibleActivity();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800790 final TaskRecord task = r.getTaskRecord();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700791 if (task != null && minTaskLayer > 0) {
792 final int layer = task.mLayerRank;
793 if (layer >= 0 && minTaskLayer > layer) {
794 minTaskLayer = layer;
795 }
796 }
797 break;
798 } else if (r.isState(PAUSING, PAUSED)) {
799 callback.onPausedActivity();
800 } else if (r.isState(STOPPING)) {
801 callback.onStoppingActivity(r.finishing);
802 } else {
803 callback.onOtherActivity();
804 }
805 }
806 }
807
808 return minTaskLayer;
809 }
810
Wale Ogunwale59507092018-10-29 09:00:30 -0700811 public int computeRelaunchReason() {
Garfield Tan2746ab52018-07-25 12:33:01 -0700812 synchronized (mAtm.mGlobalLock) {
813 final int activitiesSize = mActivities.size();
814 for (int i = activitiesSize - 1; i >= 0; i--) {
815 final ActivityRecord r = mActivities.get(i);
Wale Ogunwale64258362018-10-16 15:13:37 -0700816 if (r.mRelaunchReason != RELAUNCH_REASON_NONE) {
Garfield Tan2746ab52018-07-25 12:33:01 -0700817 return r.mRelaunchReason;
818 }
819 }
820 }
Wale Ogunwale64258362018-10-16 15:13:37 -0700821 return RELAUNCH_REASON_NONE;
Garfield Tan2746ab52018-07-25 12:33:01 -0700822 }
823
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700824 public long getInputDispatchingTimeout() {
825 synchronized (mAtm.mGlobalLock) {
826 return isInstrumenting() || isUsingWrapper()
827 ? INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT_MS : KEY_DISPATCHING_TIMEOUT_MS;
828 }
829 }
830
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700831 void clearProfilerIfNeeded() {
832 if (mListener == null) return;
833 // Posting on handler so WM lock isn't held when we call into AM.
Yohei Yukawae2fa39e2018-09-22 13:13:10 -0700834 mAtm.mH.sendMessage(PooledLambda.obtainMessage(
835 WindowProcessListener::clearProfilerIfNeeded, mListener));
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700836 }
837
Riddle Hsu17e38422019-04-12 16:55:11 +0800838 void updateProcessInfo(boolean updateServiceConnectionActivities, boolean activityChange,
839 boolean updateOomAdj) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700840 if (mListener == null) return;
841 // Posting on handler so WM lock isn't held when we call into AM.
Yohei Yukawae2fa39e2018-09-22 13:13:10 -0700842 final Message m = PooledLambda.obtainMessage(WindowProcessListener::updateProcessInfo,
Riddle Hsu17e38422019-04-12 16:55:11 +0800843 mListener, updateServiceConnectionActivities, activityChange, updateOomAdj);
Yohei Yukawae2fa39e2018-09-22 13:13:10 -0700844 mAtm.mH.sendMessage(m);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700845 }
846
847 void updateServiceConnectionActivities() {
848 if (mListener == null) return;
849 // Posting on handler so WM lock isn't held when we call into AM.
Yohei Yukawae2fa39e2018-09-22 13:13:10 -0700850 mAtm.mH.sendMessage(PooledLambda.obtainMessage(
851 WindowProcessListener::updateServiceConnectionActivities, mListener));
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700852 }
853
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700854 void setPendingUiCleanAndForceProcessStateUpTo(int newState) {
855 if (mListener == null) return;
856 // Posting on handler so WM lock isn't held when we call into AM.
Yohei Yukawae2fa39e2018-09-22 13:13:10 -0700857 final Message m = PooledLambda.obtainMessage(
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700858 WindowProcessListener::setPendingUiCleanAndForceProcessStateUpTo,
859 mListener, newState);
Yohei Yukawae2fa39e2018-09-22 13:13:10 -0700860 mAtm.mH.sendMessage(m);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700861 }
862
Riddle Hsuaaef7312019-01-24 19:00:58 +0800863 boolean isRemoved() {
864 return mListener == null ? false : mListener.isRemoved();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700865 }
866
Riddle Hsu17e38422019-04-12 16:55:11 +0800867 private boolean shouldSetProfileProc() {
868 return mAtm.mProfileApp != null && mAtm.mProfileApp.equals(mName)
869 && (mAtm.mProfileProc == null || mAtm.mProfileProc == this);
Wale Ogunwale9c103022018-10-18 07:44:54 -0700870 }
871
Riddle Hsu17e38422019-04-12 16:55:11 +0800872 ProfilerInfo createProfilerInfoIfNeeded() {
873 final ProfilerInfo currentProfilerInfo = mAtm.mProfilerInfo;
874 if (currentProfilerInfo == null || currentProfilerInfo.profileFile == null
875 || !shouldSetProfileProc()) {
876 return null;
877 }
878 if (currentProfilerInfo.profileFd != null) {
879 try {
880 currentProfilerInfo.profileFd = currentProfilerInfo.profileFd.dup();
881 } catch (IOException e) {
882 currentProfilerInfo.closeFd();
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800883 }
884 }
Riddle Hsu17e38422019-04-12 16:55:11 +0800885 return new ProfilerInfo(currentProfilerInfo);
886 }
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800887
Riddle Hsu17e38422019-04-12 16:55:11 +0800888 void onStartActivity(int topProcessState, ActivityInfo info) {
889 if (mListener == null) return;
890 String packageName = null;
891 if ((info.flags & ActivityInfo.FLAG_MULTIPROCESS) == 0
892 || !"android".equals(info.packageName)) {
893 // Don't add this if it is a platform component that is marked to run in multiple
894 // processes, because this is actually part of the framework so doesn't make sense
895 // to track as a separate apk in the process.
896 packageName = info.packageName;
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800897 }
Riddle Hsu17e38422019-04-12 16:55:11 +0800898 // Posting the message at the front of queue so WM lock isn't held when we call into AM,
899 // and the process state of starting activity can be updated quicker which will give it a
900 // higher scheduling group.
901 final Message m = PooledLambda.obtainMessage(WindowProcessListener::onStartActivity,
902 mListener, topProcessState, shouldSetProfileProc(), packageName,
903 info.applicationInfo.longVersionCode);
904 mAtm.mH.sendMessageAtFrontOfQueue(m);
Wale Ogunwale9c103022018-10-18 07:44:54 -0700905 }
906
907 public void appDied() {
Wale Ogunwale9c103022018-10-18 07:44:54 -0700908 if (mListener == null) return;
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800909 // Posting on handler so WM lock isn't held when we call into AM.
910 final Message m = PooledLambda.obtainMessage(
911 WindowProcessListener::appDied, mListener);
912 mAtm.mH.sendMessage(m);
Wale Ogunwale9c103022018-10-18 07:44:54 -0700913 }
914
Yunfan Chen79b96062018-10-17 12:45:23 -0700915 void registerDisplayConfigurationListenerLocked(ActivityDisplay activityDisplay) {
916 if (activityDisplay == null) {
917 return;
918 }
919 // A process can only register to one display to listener to the override configuration
920 // change. Unregister existing listener if it has one before register the new one.
921 unregisterDisplayConfigurationListenerLocked();
922 mDisplayId = activityDisplay.mDisplayId;
923 activityDisplay.registerConfigurationChangeListener(this);
924 }
925
Yunfan Chenb29cbfd2019-01-24 17:30:33 +0900926 @VisibleForTesting
927 void unregisterDisplayConfigurationListenerLocked() {
Yunfan Chen79b96062018-10-17 12:45:23 -0700928 if (mDisplayId == INVALID_DISPLAY) {
929 return;
930 }
931 final ActivityDisplay activityDisplay =
Wale Ogunwaled32da472018-11-16 07:19:28 -0800932 mAtm.mRootActivityContainer.getActivityDisplay(mDisplayId);
Yunfan Chen79b96062018-10-17 12:45:23 -0700933 if (activityDisplay != null) {
Yunfan Chenb29cbfd2019-01-24 17:30:33 +0900934 activityDisplay.unregisterConfigurationChangeListener(this);
Yunfan Chen79b96062018-10-17 12:45:23 -0700935 }
936 mDisplayId = INVALID_DISPLAY;
937 }
938
Yunfan Chen75157d72018-07-27 14:47:21 +0900939 @Override
940 public void onConfigurationChanged(Configuration newGlobalConfig) {
941 super.onConfigurationChanged(newGlobalConfig);
942 updateConfiguration();
943 }
944
945 @Override
Evan Roskydfe3da72018-10-26 17:21:06 -0700946 public void onRequestedOverrideConfigurationChanged(Configuration newOverrideConfig) {
947 super.onRequestedOverrideConfigurationChanged(newOverrideConfig);
Yunfan Chen75157d72018-07-27 14:47:21 +0900948 updateConfiguration();
949 }
950
951 private void updateConfiguration() {
952 final Configuration config = getConfiguration();
953 if (mLastReportedConfiguration.diff(config) == 0) {
954 // Nothing changed.
955 return;
956 }
957
958 try {
959 if (mThread == null) {
960 return;
961 }
962 if (DEBUG_CONFIGURATION) {
963 Slog.v(TAG_CONFIGURATION, "Sending to proc " + mName
964 + " new config " + config);
965 }
966 config.seq = mAtm.increaseConfigurationSeqLocked();
967 mAtm.getLifecycleManager().scheduleTransaction(mThread,
968 ConfigurationChangeItem.obtain(config));
969 setLastReportedConfiguration(config);
970 } catch (Exception e) {
971 Slog.e(TAG_CONFIGURATION, "Failed to schedule configuration change", e);
972 }
973 }
974
975 private void setLastReportedConfiguration(Configuration config) {
976 mLastReportedConfiguration.setTo(config);
977 }
978
979 Configuration getLastReportedConfiguration() {
980 return mLastReportedConfiguration;
981 }
982
Wale Ogunwale86b74462018-07-02 08:42:43 -0700983 /** Returns the total time (in milliseconds) spent executing in both user and system code. */
984 public long getCpuTime() {
985 return (mListener != null) ? mListener.getCpuTime() : 0;
986 }
987
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700988 void addRecentTask(TaskRecord task) {
989 mRecentTasks.add(task);
990 }
991
992 void removeRecentTask(TaskRecord task) {
993 mRecentTasks.remove(task);
994 }
995
Riddle Hsua0536432019-02-16 00:38:59 +0800996 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700997 public boolean hasRecentTasks() {
Riddle Hsud7088f82019-01-30 13:04:50 +0800998 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700999 return !mRecentTasks.isEmpty();
1000 }
1001 }
1002
Riddle Hsud7088f82019-01-30 13:04:50 +08001003 void clearRecentTasks() {
1004 for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
1005 mRecentTasks.get(i).clearRootProcess();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001006 }
Riddle Hsud7088f82019-01-30 13:04:50 +08001007 mRecentTasks.clear();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001008 }
1009
Wale Ogunwalee2172292018-10-25 10:11:10 -07001010 public void appEarlyNotResponding(String annotation, Runnable killAppCallback) {
1011 synchronized (mAtm.mGlobalLock) {
1012 if (mAtm.mController == null) {
1013 return;
1014 }
1015
1016 try {
1017 // 0 == continue, -1 = kill process immediately
1018 int res = mAtm.mController.appEarlyNotResponding(mName, mPid, annotation);
1019 if (res < 0 && mPid != MY_PID) {
1020 killAppCallback.run();
1021 }
1022 } catch (RemoteException e) {
1023 mAtm.mController = null;
1024 Watchdog.getInstance().setActivityController(null);
1025 }
1026 }
1027 }
1028
1029 public boolean appNotResponding(String info, Runnable killAppCallback,
1030 Runnable serviceTimeoutCallback) {
Jeff Changed2f5bc2019-01-24 19:33:28 +08001031 Runnable targetRunnable = null;
Wale Ogunwalee2172292018-10-25 10:11:10 -07001032 synchronized (mAtm.mGlobalLock) {
1033 if (mAtm.mController == null) {
1034 return false;
1035 }
1036
1037 try {
1038 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
1039 int res = mAtm.mController.appNotResponding(mName, mPid, info);
1040 if (res != 0) {
1041 if (res < 0 && mPid != MY_PID) {
Jeff Changed2f5bc2019-01-24 19:33:28 +08001042 targetRunnable = killAppCallback;
Wale Ogunwalee2172292018-10-25 10:11:10 -07001043 } else {
Jeff Changed2f5bc2019-01-24 19:33:28 +08001044 targetRunnable = serviceTimeoutCallback;
Wale Ogunwalee2172292018-10-25 10:11:10 -07001045 }
Wale Ogunwalee2172292018-10-25 10:11:10 -07001046 }
1047 } catch (RemoteException e) {
1048 mAtm.mController = null;
1049 Watchdog.getInstance().setActivityController(null);
Jeff Changed2f5bc2019-01-24 19:33:28 +08001050 return false;
Wale Ogunwalee2172292018-10-25 10:11:10 -07001051 }
Wale Ogunwalee2172292018-10-25 10:11:10 -07001052 }
Jeff Changed2f5bc2019-01-24 19:33:28 +08001053 if (targetRunnable != null) {
1054 targetRunnable.run();
1055 return true;
1056 }
1057 return false;
Wale Ogunwalee2172292018-10-25 10:11:10 -07001058 }
1059
Riddle Hsua0536432019-02-16 00:38:59 +08001060 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwalee2172292018-10-25 10:11:10 -07001061 public void onTopProcChanged() {
Riddle Hsud7088f82019-01-30 13:04:50 +08001062 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwalee2172292018-10-25 10:11:10 -07001063 mAtm.mVrController.onTopProcChangedLocked(this);
1064 }
1065 }
1066
Riddle Hsua0536432019-02-16 00:38:59 +08001067 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwaled4d67d02018-10-25 18:09:39 -07001068 public boolean isHomeProcess() {
Riddle Hsud7088f82019-01-30 13:04:50 +08001069 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwaled4d67d02018-10-25 18:09:39 -07001070 return this == mAtm.mHomeProcess;
1071 }
1072 }
1073
Riddle Hsua0536432019-02-16 00:38:59 +08001074 @HotPath(caller = HotPath.OOM_ADJUSTMENT)
Wale Ogunwaled4d67d02018-10-25 18:09:39 -07001075 public boolean isPreviousProcess() {
Riddle Hsud7088f82019-01-30 13:04:50 +08001076 synchronized (mAtm.mGlobalLockWithoutBoost) {
Wale Ogunwaled4d67d02018-10-25 18:09:39 -07001077 return this == mAtm.mPreviousProcess;
1078 }
1079 }
1080
Jorim Jaggi589c5ba2019-07-30 16:50:13 +02001081 void setRunningRecentsAnimation(boolean running) {
1082 if (mRunningRecentsAnimation == running) {
1083 return;
1084 }
1085 mRunningRecentsAnimation = running;
1086 updateRunningRemoteOrRecentsAnimation();
1087 }
1088
1089 void setRunningRemoteAnimation(boolean running) {
1090 if (mRunningRemoteAnimation == running) {
1091 return;
1092 }
1093 mRunningRemoteAnimation = running;
1094 updateRunningRemoteOrRecentsAnimation();
1095 }
1096
1097 private void updateRunningRemoteOrRecentsAnimation() {
1098
1099 // Posting on handler so WM lock isn't held when we call into AM.
1100 mAtm.mH.sendMessage(PooledLambda.obtainMessage(
1101 WindowProcessListener::setRunningRemoteAnimation, mListener,
1102 mRunningRecentsAnimation || mRunningRemoteAnimation));
1103 }
1104
Wale Ogunwale2a47d122018-11-19 16:49:40 -08001105 @Override
1106 public String toString() {
Michal Karpinski9cbb20b2019-02-05 17:31:50 +00001107 return mOwner != null ? mOwner.toString() : null;
Wale Ogunwale2a47d122018-11-19 16:49:40 -08001108 }
1109
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001110 public void dump(PrintWriter pw, String prefix) {
1111 synchronized (mAtm.mGlobalLock) {
1112 if (mActivities.size() > 0) {
1113 pw.print(prefix); pw.println("Activities:");
1114 for (int i = 0; i < mActivities.size(); i++) {
1115 pw.print(prefix); pw.print(" - "); pw.println(mActivities.get(i));
1116 }
1117 }
1118
1119 if (mRecentTasks.size() > 0) {
1120 pw.println(prefix + "Recent Tasks:");
1121 for (int i = 0; i < mRecentTasks.size(); i++) {
1122 pw.println(prefix + " - " + mRecentTasks.get(i));
1123 }
1124 }
1125
1126 if (mVrThreadTid != 0) {
1127 pw.print(prefix); pw.print("mVrThreadTid="); pw.println(mVrThreadTid);
1128 }
1129 }
Yunfan Chen75157d72018-07-27 14:47:21 +09001130 pw.println(prefix + " Configuration=" + getConfiguration());
Evan Roskydfe3da72018-10-26 17:21:06 -07001131 pw.println(prefix + " OverrideConfiguration=" + getRequestedOverrideConfiguration());
Yunfan Chen75157d72018-07-27 14:47:21 +09001132 pw.println(prefix + " mLastReportedConfiguration=" + mLastReportedConfiguration);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001133 }
1134
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001135 void writeToProto(ProtoOutputStream proto, long fieldId) {
1136 if (mListener != null) {
1137 mListener.writeToProto(proto, fieldId);
1138 }
1139 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001140}