ActivityManagerService.java revision 9e18397112df0ebe49b9e4325d7040498b2afcfa
1/*
2 * Copyright (C) 2006-2008 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
17package com.android.server.am;
18
19import static android.Manifest.permission.INTERACT_ACROSS_USERS;
20import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
21import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
22import static android.content.pm.PackageManager.PERMISSION_GRANTED;
23import static com.android.internal.util.XmlUtils.readBooleanAttribute;
24import static com.android.internal.util.XmlUtils.readIntAttribute;
25import static com.android.internal.util.XmlUtils.readLongAttribute;
26import static com.android.internal.util.XmlUtils.writeBooleanAttribute;
27import static com.android.internal.util.XmlUtils.writeIntAttribute;
28import static com.android.internal.util.XmlUtils.writeLongAttribute;
29import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
30import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
31import static org.xmlpull.v1.XmlPullParser.START_TAG;
32import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
33
34import android.Manifest;
35import android.app.AppOpsManager;
36import android.app.ApplicationThreadNative;
37import android.app.IActivityContainer;
38import android.app.IActivityContainerCallback;
39import android.app.IAppTask;
40import android.app.ProfilerInfo;
41import android.app.admin.DevicePolicyManager;
42import android.app.usage.UsageEvents;
43import android.app.usage.UsageStatsManagerInternal;
44import android.appwidget.AppWidgetManager;
45import android.content.res.Resources;
46import android.graphics.Bitmap;
47import android.graphics.Point;
48import android.graphics.Rect;
49import android.os.BatteryStats;
50import android.os.PersistableBundle;
51import android.service.voice.IVoiceInteractionSession;
52import android.util.ArrayMap;
53import android.util.ArraySet;
54import android.util.SparseIntArray;
55
56import com.android.internal.R;
57import com.android.internal.annotations.GuardedBy;
58import com.android.internal.app.IAppOpsService;
59import com.android.internal.app.IVoiceInteractor;
60import com.android.internal.app.ProcessMap;
61import com.android.internal.app.ProcessStats;
62import com.android.internal.content.PackageMonitor;
63import com.android.internal.os.BackgroundThread;
64import com.android.internal.os.BatteryStatsImpl;
65import com.android.internal.os.ProcessCpuTracker;
66import com.android.internal.os.TransferPipe;
67import com.android.internal.os.Zygote;
68import com.android.internal.util.FastPrintWriter;
69import com.android.internal.util.FastXmlSerializer;
70import com.android.internal.util.MemInfoReader;
71import com.android.internal.util.Preconditions;
72import com.android.server.AppOpsService;
73import com.android.server.AttributeCache;
74import com.android.server.IntentResolver;
75import com.android.server.LocalServices;
76import com.android.server.ServiceThread;
77import com.android.server.SystemService;
78import com.android.server.SystemServiceManager;
79import com.android.server.Watchdog;
80import com.android.server.am.ActivityStack.ActivityState;
81import com.android.server.firewall.IntentFirewall;
82import com.android.server.pm.UserManagerService;
83import com.android.server.wm.AppTransition;
84import com.android.server.wm.WindowManagerService;
85import com.google.android.collect.Lists;
86import com.google.android.collect.Maps;
87
88import libcore.io.IoUtils;
89
90import org.xmlpull.v1.XmlPullParser;
91import org.xmlpull.v1.XmlPullParserException;
92import org.xmlpull.v1.XmlSerializer;
93
94import android.app.Activity;
95import android.app.ActivityManager;
96import android.app.ActivityManager.RunningTaskInfo;
97import android.app.ActivityManager.StackInfo;
98import android.app.ActivityManagerInternal;
99import android.app.ActivityManagerNative;
100import android.app.ActivityOptions;
101import android.app.ActivityThread;
102import android.app.AlertDialog;
103import android.app.AppGlobals;
104import android.app.ApplicationErrorReport;
105import android.app.Dialog;
106import android.app.IActivityController;
107import android.app.IApplicationThread;
108import android.app.IInstrumentationWatcher;
109import android.app.INotificationManager;
110import android.app.IProcessObserver;
111import android.app.IServiceConnection;
112import android.app.IStopUserCallback;
113import android.app.IUiAutomationConnection;
114import android.app.IUserSwitchObserver;
115import android.app.Instrumentation;
116import android.app.Notification;
117import android.app.NotificationManager;
118import android.app.PendingIntent;
119import android.app.backup.IBackupManager;
120import android.content.ActivityNotFoundException;
121import android.content.BroadcastReceiver;
122import android.content.ClipData;
123import android.content.ComponentCallbacks2;
124import android.content.ComponentName;
125import android.content.ContentProvider;
126import android.content.ContentResolver;
127import android.content.Context;
128import android.content.DialogInterface;
129import android.content.IContentProvider;
130import android.content.IIntentReceiver;
131import android.content.IIntentSender;
132import android.content.Intent;
133import android.content.IntentFilter;
134import android.content.IntentSender;
135import android.content.pm.ActivityInfo;
136import android.content.pm.ApplicationInfo;
137import android.content.pm.ConfigurationInfo;
138import android.content.pm.IPackageDataObserver;
139import android.content.pm.IPackageManager;
140import android.content.pm.InstrumentationInfo;
141import android.content.pm.PackageInfo;
142import android.content.pm.PackageManager;
143import android.content.pm.ParceledListSlice;
144import android.content.pm.UserInfo;
145import android.content.pm.PackageManager.NameNotFoundException;
146import android.content.pm.PathPermission;
147import android.content.pm.ProviderInfo;
148import android.content.pm.ResolveInfo;
149import android.content.pm.ServiceInfo;
150import android.content.res.CompatibilityInfo;
151import android.content.res.Configuration;
152import android.net.Proxy;
153import android.net.ProxyInfo;
154import android.net.Uri;
155import android.os.Binder;
156import android.os.Build;
157import android.os.Bundle;
158import android.os.Debug;
159import android.os.DropBoxManager;
160import android.os.Environment;
161import android.os.FactoryTest;
162import android.os.FileObserver;
163import android.os.FileUtils;
164import android.os.Handler;
165import android.os.IBinder;
166import android.os.IPermissionController;
167import android.os.IRemoteCallback;
168import android.os.IUserManager;
169import android.os.Looper;
170import android.os.Message;
171import android.os.Parcel;
172import android.os.ParcelFileDescriptor;
173import android.os.Process;
174import android.os.RemoteCallbackList;
175import android.os.RemoteException;
176import android.os.SELinux;
177import android.os.ServiceManager;
178import android.os.StrictMode;
179import android.os.SystemClock;
180import android.os.SystemProperties;
181import android.os.UpdateLock;
182import android.os.UserHandle;
183import android.provider.Settings;
184import android.text.format.DateUtils;
185import android.text.format.Time;
186import android.util.AtomicFile;
187import android.util.EventLog;
188import android.util.Log;
189import android.util.Pair;
190import android.util.PrintWriterPrinter;
191import android.util.Slog;
192import android.util.SparseArray;
193import android.util.TimeUtils;
194import android.util.Xml;
195import android.view.Gravity;
196import android.view.LayoutInflater;
197import android.view.View;
198import android.view.WindowManager;
199
200import java.io.BufferedInputStream;
201import java.io.BufferedOutputStream;
202import java.io.DataInputStream;
203import java.io.DataOutputStream;
204import java.io.File;
205import java.io.FileDescriptor;
206import java.io.FileInputStream;
207import java.io.FileNotFoundException;
208import java.io.FileOutputStream;
209import java.io.IOException;
210import java.io.InputStreamReader;
211import java.io.PrintWriter;
212import java.io.StringWriter;
213import java.lang.ref.WeakReference;
214import java.util.ArrayList;
215import java.util.Arrays;
216import java.util.Collections;
217import java.util.Comparator;
218import java.util.HashMap;
219import java.util.HashSet;
220import java.util.Iterator;
221import java.util.List;
222import java.util.Locale;
223import java.util.Map;
224import java.util.Set;
225import java.util.concurrent.atomic.AtomicBoolean;
226import java.util.concurrent.atomic.AtomicLong;
227
228public final class ActivityManagerService extends ActivityManagerNative
229        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
230
231    private static final String USER_DATA_DIR = "/data/user/";
232    // File that stores last updated system version and called preboot receivers
233    static final String CALLED_PRE_BOOTS_FILENAME = "called_pre_boots.dat";
234
235    static final String TAG = "ActivityManager";
236    static final String TAG_MU = "ActivityManagerServiceMU";
237    static final boolean DEBUG = false;
238    static final boolean localLOGV = DEBUG;
239    static final boolean DEBUG_BACKUP = localLOGV || false;
240    static final boolean DEBUG_BROADCAST = localLOGV || false;
241    static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
242    static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false;
243    static final boolean DEBUG_CLEANUP = localLOGV || false;
244    static final boolean DEBUG_CONFIGURATION = localLOGV || false;
245    static final boolean DEBUG_FOCUS = false;
246    static final boolean DEBUG_IMMERSIVE = localLOGV || false;
247    static final boolean DEBUG_MU = localLOGV || false;
248    static final boolean DEBUG_OOM_ADJ = localLOGV || false;
249    static final boolean DEBUG_LRU = localLOGV || false;
250    static final boolean DEBUG_PAUSE = localLOGV || false;
251    static final boolean DEBUG_POWER = localLOGV || false;
252    static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false;
253    static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false;
254    static final boolean DEBUG_PROCESSES = localLOGV || false;
255    static final boolean DEBUG_PROVIDER = localLOGV || false;
256    static final boolean DEBUG_RESULTS = localLOGV || false;
257    static final boolean DEBUG_SERVICE = localLOGV || false;
258    static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false;
259    static final boolean DEBUG_STACK = localLOGV || false;
260    static final boolean DEBUG_SWITCH = localLOGV || false;
261    static final boolean DEBUG_TASKS = localLOGV || false;
262    static final boolean DEBUG_THUMBNAILS = localLOGV || false;
263    static final boolean DEBUG_TRANSITION = localLOGV || false;
264    static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
265    static final boolean DEBUG_USER_LEAVING = localLOGV || false;
266    static final boolean DEBUG_VISBILITY = localLOGV || false;
267    static final boolean DEBUG_PSS = localLOGV || false;
268    static final boolean DEBUG_LOCKSCREEN = localLOGV || false;
269    static final boolean DEBUG_RECENTS = localLOGV || false;
270    static final boolean VALIDATE_TOKENS = false;
271    static final boolean SHOW_ACTIVITY_START_TIME = true;
272
273    // Control over CPU and battery monitoring.
274    static final long BATTERY_STATS_TIME = 30*60*1000;      // write battery stats every 30 minutes.
275    static final boolean MONITOR_CPU_USAGE = true;
276    static final long MONITOR_CPU_MIN_TIME = 5*1000;        // don't sample cpu less than every 5 seconds.
277    static final long MONITOR_CPU_MAX_TIME = 0x0fffffff;    // wait possibly forever for next cpu sample.
278    static final boolean MONITOR_THREAD_CPU_USAGE = false;
279
280    // The flags that are set for all calls we make to the package manager.
281    static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
282
283    private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
284
285    static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
286
287    // Maximum number recent bitmaps to keep in memory.
288    static final int MAX_RECENT_BITMAPS = 5;
289
290    // Amount of time after a call to stopAppSwitches() during which we will
291    // prevent further untrusted switches from happening.
292    static final long APP_SWITCH_DELAY_TIME = 5*1000;
293
294    // How long we wait for a launched process to attach to the activity manager
295    // before we decide it's never going to come up for real.
296    static final int PROC_START_TIMEOUT = 10*1000;
297
298    // How long we wait for a launched process to attach to the activity manager
299    // before we decide it's never going to come up for real, when the process was
300    // started with a wrapper for instrumentation (such as Valgrind) because it
301    // could take much longer than usual.
302    static final int PROC_START_TIMEOUT_WITH_WRAPPER = 1200*1000;
303
304    // How long to wait after going idle before forcing apps to GC.
305    static final int GC_TIMEOUT = 5*1000;
306
307    // The minimum amount of time between successive GC requests for a process.
308    static final int GC_MIN_INTERVAL = 60*1000;
309
310    // The minimum amount of time between successive PSS requests for a process.
311    static final int FULL_PSS_MIN_INTERVAL = 10*60*1000;
312
313    // The minimum amount of time between successive PSS requests for a process
314    // when the request is due to the memory state being lowered.
315    static final int FULL_PSS_LOWERED_INTERVAL = 2*60*1000;
316
317    // The rate at which we check for apps using excessive power -- 15 mins.
318    static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;
319
320    // The minimum sample duration we will allow before deciding we have
321    // enough data on wake locks to start killing things.
322    static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
323
324    // The minimum sample duration we will allow before deciding we have
325    // enough data on CPU usage to start killing things.
326    static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
327
328    // How long we allow a receiver to run before giving up on it.
329    static final int BROADCAST_FG_TIMEOUT = 10*1000;
330    static final int BROADCAST_BG_TIMEOUT = 60*1000;
331
332    // How long we wait until we timeout on key dispatching.
333    static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
334
335    // How long we wait until we timeout on key dispatching during instrumentation.
336    static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
337
338    // Amount of time we wait for observers to handle a user switch before
339    // giving up on them and unfreezing the screen.
340    static final int USER_SWITCH_TIMEOUT = 2*1000;
341
342    // Maximum number of users we allow to be running at a time.
343    static final int MAX_RUNNING_USERS = 3;
344
345    // How long to wait in getAssistContextExtras for the activity and foreground services
346    // to respond with the result.
347    static final int PENDING_ASSIST_EXTRAS_TIMEOUT = 500;
348
349    // Maximum number of persisted Uri grants a package is allowed
350    static final int MAX_PERSISTED_URI_GRANTS = 128;
351
352    static final int MY_PID = Process.myPid();
353
354    static final String[] EMPTY_STRING_ARRAY = new String[0];
355
356    // How many bytes to write into the dropbox log before truncating
357    static final int DROPBOX_MAX_SIZE = 256 * 1024;
358
359    // Access modes for handleIncomingUser.
360    static final int ALLOW_NON_FULL = 0;
361    static final int ALLOW_NON_FULL_IN_PROFILE = 1;
362    static final int ALLOW_FULL_ONLY = 2;
363
364    static final int LAST_PREBOOT_DELIVERED_FILE_VERSION = 10000;
365
366    /** All system services */
367    SystemServiceManager mSystemServiceManager;
368
369    /** Run all ActivityStacks through this */
370    ActivityStackSupervisor mStackSupervisor;
371
372    public IntentFirewall mIntentFirewall;
373
374    // Whether we should show our dialogs (ANR, crash, etc) or just perform their
375    // default actuion automatically.  Important for devices without direct input
376    // devices.
377    private boolean mShowDialogs = true;
378
379    BroadcastQueue mFgBroadcastQueue;
380    BroadcastQueue mBgBroadcastQueue;
381    // Convenient for easy iteration over the queues. Foreground is first
382    // so that dispatch of foreground broadcasts gets precedence.
383    final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2];
384
385    BroadcastQueue broadcastQueueForIntent(Intent intent) {
386        final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0;
387        if (DEBUG_BACKGROUND_BROADCAST) {
388            Slog.i(TAG, "Broadcast intent " + intent + " on "
389                    + (isFg ? "foreground" : "background")
390                    + " queue");
391        }
392        return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue;
393    }
394
395    BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) {
396        for (BroadcastQueue queue : mBroadcastQueues) {
397            BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver);
398            if (r != null) {
399                return r;
400            }
401        }
402        return null;
403    }
404
405    /**
406     * Activity we have told the window manager to have key focus.
407     */
408    ActivityRecord mFocusedActivity = null;
409
410    /**
411     * List of intents that were used to start the most recent tasks.
412     */
413    ArrayList<TaskRecord> mRecentTasks;
414    ArraySet<TaskRecord> mTmpRecents = new ArraySet<TaskRecord>();
415
416    /**
417     * For addAppTask: cached of the last activity component that was added.
418     */
419    ComponentName mLastAddedTaskComponent;
420
421    /**
422     * For addAppTask: cached of the last activity uid that was added.
423     */
424    int mLastAddedTaskUid;
425
426    /**
427     * For addAppTask: cached of the last ActivityInfo that was added.
428     */
429    ActivityInfo mLastAddedTaskActivity;
430
431    public class PendingAssistExtras extends Binder implements Runnable {
432        public final ActivityRecord activity;
433        public boolean haveResult = false;
434        public Bundle result = null;
435        public PendingAssistExtras(ActivityRecord _activity) {
436            activity = _activity;
437        }
438        @Override
439        public void run() {
440            Slog.w(TAG, "getAssistContextExtras failed: timeout retrieving from " + activity);
441            synchronized (this) {
442                haveResult = true;
443                notifyAll();
444            }
445        }
446    }
447
448    final ArrayList<PendingAssistExtras> mPendingAssistExtras
449            = new ArrayList<PendingAssistExtras>();
450
451    /**
452     * Process management.
453     */
454    final ProcessList mProcessList = new ProcessList();
455
456    /**
457     * All of the applications we currently have running organized by name.
458     * The keys are strings of the application package name (as
459     * returned by the package manager), and the keys are ApplicationRecord
460     * objects.
461     */
462    final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>();
463
464    /**
465     * Tracking long-term execution of processes to look for abuse and other
466     * bad app behavior.
467     */
468    final ProcessStatsService mProcessStats;
469
470    /**
471     * The currently running isolated processes.
472     */
473    final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>();
474
475    /**
476     * Counter for assigning isolated process uids, to avoid frequently reusing the
477     * same ones.
478     */
479    int mNextIsolatedProcessUid = 0;
480
481    /**
482     * The currently running heavy-weight process, if any.
483     */
484    ProcessRecord mHeavyWeightProcess = null;
485
486    /**
487     * The last time that various processes have crashed.
488     */
489    final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
490
491    /**
492     * Information about a process that is currently marked as bad.
493     */
494    static final class BadProcessInfo {
495        BadProcessInfo(long time, String shortMsg, String longMsg, String stack) {
496            this.time = time;
497            this.shortMsg = shortMsg;
498            this.longMsg = longMsg;
499            this.stack = stack;
500        }
501
502        final long time;
503        final String shortMsg;
504        final String longMsg;
505        final String stack;
506    }
507
508    /**
509     * Set of applications that we consider to be bad, and will reject
510     * incoming broadcasts from (which the user has no control over).
511     * Processes are added to this set when they have crashed twice within
512     * a minimum amount of time; they are removed from it when they are
513     * later restarted (hopefully due to some user action).  The value is the
514     * time it was added to the list.
515     */
516    final ProcessMap<BadProcessInfo> mBadProcesses = new ProcessMap<BadProcessInfo>();
517
518    /**
519     * All of the processes we currently have running organized by pid.
520     * The keys are the pid running the application.
521     *
522     * <p>NOTE: This object is protected by its own lock, NOT the global
523     * activity manager lock!
524     */
525    final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>();
526
527    /**
528     * All of the processes that have been forced to be foreground.  The key
529     * is the pid of the caller who requested it (we hold a death
530     * link on it).
531     */
532    abstract class ForegroundToken implements IBinder.DeathRecipient {
533        int pid;
534        IBinder token;
535    }
536    final SparseArray<ForegroundToken> mForegroundProcesses = new SparseArray<ForegroundToken>();
537
538    /**
539     * List of records for processes that someone had tried to start before the
540     * system was ready.  We don't start them at that point, but ensure they
541     * are started by the time booting is complete.
542     */
543    final ArrayList<ProcessRecord> mProcessesOnHold = new ArrayList<ProcessRecord>();
544
545    /**
546     * List of persistent applications that are in the process
547     * of being started.
548     */
549    final ArrayList<ProcessRecord> mPersistentStartingProcesses = new ArrayList<ProcessRecord>();
550
551    /**
552     * Processes that are being forcibly torn down.
553     */
554    final ArrayList<ProcessRecord> mRemovedProcesses = new ArrayList<ProcessRecord>();
555
556    /**
557     * List of running applications, sorted by recent usage.
558     * The first entry in the list is the least recently used.
559     */
560    final ArrayList<ProcessRecord> mLruProcesses = new ArrayList<ProcessRecord>();
561
562    /**
563     * Where in mLruProcesses that the processes hosting activities start.
564     */
565    int mLruProcessActivityStart = 0;
566
567    /**
568     * Where in mLruProcesses that the processes hosting services start.
569     * This is after (lower index) than mLruProcessesActivityStart.
570     */
571    int mLruProcessServiceStart = 0;
572
573    /**
574     * List of processes that should gc as soon as things are idle.
575     */
576    final ArrayList<ProcessRecord> mProcessesToGc = new ArrayList<ProcessRecord>();
577
578    /**
579     * Processes we want to collect PSS data from.
580     */
581    final ArrayList<ProcessRecord> mPendingPssProcesses = new ArrayList<ProcessRecord>();
582
583    /**
584     * Last time we requested PSS data of all processes.
585     */
586    long mLastFullPssTime = SystemClock.uptimeMillis();
587
588    /**
589     * If set, the next time we collect PSS data we should do a full collection
590     * with data from native processes and the kernel.
591     */
592    boolean mFullPssPending = false;
593
594    /**
595     * This is the process holding what we currently consider to be
596     * the "home" activity.
597     */
598    ProcessRecord mHomeProcess;
599
600    /**
601     * This is the process holding the activity the user last visited that
602     * is in a different process from the one they are currently in.
603     */
604    ProcessRecord mPreviousProcess;
605
606    /**
607     * The time at which the previous process was last visible.
608     */
609    long mPreviousProcessVisibleTime;
610
611    /**
612     * Which uses have been started, so are allowed to run code.
613     */
614    final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>();
615
616    /**
617     * LRU list of history of current users.  Most recently current is at the end.
618     */
619    final ArrayList<Integer> mUserLru = new ArrayList<Integer>();
620
621    /**
622     * Constant array of the users that are currently started.
623     */
624    int[] mStartedUserArray = new int[] { 0 };
625
626    /**
627     * Registered observers of the user switching mechanics.
628     */
629    final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers
630            = new RemoteCallbackList<IUserSwitchObserver>();
631
632    /**
633     * Currently active user switch.
634     */
635    Object mCurUserSwitchCallback;
636
637    /**
638     * Packages that the user has asked to have run in screen size
639     * compatibility mode instead of filling the screen.
640     */
641    final CompatModePackages mCompatModePackages;
642
643    /**
644     * Set of IntentSenderRecord objects that are currently active.
645     */
646    final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
647            = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
648
649    /**
650     * Fingerprints (hashCode()) of stack traces that we've
651     * already logged DropBox entries for.  Guarded by itself.  If
652     * something (rogue user app) forces this over
653     * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared.
654     */
655    private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>();
656    private static final int MAX_DUP_SUPPRESSED_STACKS = 5000;
657
658    /**
659     * Strict Mode background batched logging state.
660     *
661     * The string buffer is guarded by itself, and its lock is also
662     * used to determine if another batched write is already
663     * in-flight.
664     */
665    private final StringBuilder mStrictModeBuffer = new StringBuilder();
666
667    /**
668     * Keeps track of all IIntentReceivers that have been registered for
669     * broadcasts.  Hash keys are the receiver IBinder, hash value is
670     * a ReceiverList.
671     */
672    final HashMap<IBinder, ReceiverList> mRegisteredReceivers =
673            new HashMap<IBinder, ReceiverList>();
674
675    /**
676     * Resolver for broadcast intents to registered receivers.
677     * Holds BroadcastFilter (subclass of IntentFilter).
678     */
679    final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
680            = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
681        @Override
682        protected boolean allowFilterResult(
683                BroadcastFilter filter, List<BroadcastFilter> dest) {
684            IBinder target = filter.receiverList.receiver.asBinder();
685            for (int i=dest.size()-1; i>=0; i--) {
686                if (dest.get(i).receiverList.receiver.asBinder() == target) {
687                    return false;
688                }
689            }
690            return true;
691        }
692
693        @Override
694        protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) {
695            if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL
696                    || userId == filter.owningUserId) {
697                return super.newResult(filter, match, userId);
698            }
699            return null;
700        }
701
702        @Override
703        protected BroadcastFilter[] newArray(int size) {
704            return new BroadcastFilter[size];
705        }
706
707        @Override
708        protected boolean isPackageForFilter(String packageName, BroadcastFilter filter) {
709            return packageName.equals(filter.packageName);
710        }
711    };
712
713    /**
714     * State of all active sticky broadcasts per user.  Keys are the action of the
715     * sticky Intent, values are an ArrayList of all broadcasted intents with
716     * that action (which should usually be one).  The SparseArray is keyed
717     * by the user ID the sticky is for, and can include UserHandle.USER_ALL
718     * for stickies that are sent to all users.
719     */
720    final SparseArray<ArrayMap<String, ArrayList<Intent>>> mStickyBroadcasts =
721            new SparseArray<ArrayMap<String, ArrayList<Intent>>>();
722
723    final ActiveServices mServices;
724
725    /**
726     * Backup/restore process management
727     */
728    String mBackupAppName = null;
729    BackupRecord mBackupTarget = null;
730
731    final ProviderMap mProviderMap;
732
733    /**
734     * List of content providers who have clients waiting for them.  The
735     * application is currently being launched and the provider will be
736     * removed from this list once it is published.
737     */
738    final ArrayList<ContentProviderRecord> mLaunchingProviders
739            = new ArrayList<ContentProviderRecord>();
740
741    /**
742     * File storing persisted {@link #mGrantedUriPermissions}.
743     */
744    private final AtomicFile mGrantFile;
745
746    /** XML constants used in {@link #mGrantFile} */
747    private static final String TAG_URI_GRANTS = "uri-grants";
748    private static final String TAG_URI_GRANT = "uri-grant";
749    private static final String ATTR_USER_HANDLE = "userHandle";
750    private static final String ATTR_SOURCE_USER_ID = "sourceUserId";
751    private static final String ATTR_TARGET_USER_ID = "targetUserId";
752    private static final String ATTR_SOURCE_PKG = "sourcePkg";
753    private static final String ATTR_TARGET_PKG = "targetPkg";
754    private static final String ATTR_URI = "uri";
755    private static final String ATTR_MODE_FLAGS = "modeFlags";
756    private static final String ATTR_CREATED_TIME = "createdTime";
757    private static final String ATTR_PREFIX = "prefix";
758
759    /**
760     * Global set of specific {@link Uri} permissions that have been granted.
761     * This optimized lookup structure maps from {@link UriPermission#targetUid}
762     * to {@link UriPermission#uri} to {@link UriPermission}.
763     */
764    @GuardedBy("this")
765    private final SparseArray<ArrayMap<GrantUri, UriPermission>>
766            mGrantedUriPermissions = new SparseArray<ArrayMap<GrantUri, UriPermission>>();
767
768    public static class GrantUri {
769        public final int sourceUserId;
770        public final Uri uri;
771        public boolean prefix;
772
773        public GrantUri(int sourceUserId, Uri uri, boolean prefix) {
774            this.sourceUserId = sourceUserId;
775            this.uri = uri;
776            this.prefix = prefix;
777        }
778
779        @Override
780        public int hashCode() {
781            return toString().hashCode();
782        }
783
784        @Override
785        public boolean equals(Object o) {
786            if (o instanceof GrantUri) {
787                GrantUri other = (GrantUri) o;
788                return uri.equals(other.uri) && (sourceUserId == other.sourceUserId)
789                        && prefix == other.prefix;
790            }
791            return false;
792        }
793
794        @Override
795        public String toString() {
796            String result = Integer.toString(sourceUserId) + " @ " + uri.toString();
797            if (prefix) result += " [prefix]";
798            return result;
799        }
800
801        public String toSafeString() {
802            String result = Integer.toString(sourceUserId) + " @ " + uri.toSafeString();
803            if (prefix) result += " [prefix]";
804            return result;
805        }
806
807        public static GrantUri resolve(int defaultSourceUserHandle, Uri uri) {
808            return new GrantUri(ContentProvider.getUserIdFromUri(uri, defaultSourceUserHandle),
809                    ContentProvider.getUriWithoutUserId(uri), false);
810        }
811    }
812
813    CoreSettingsObserver mCoreSettingsObserver;
814
815    /**
816     * Thread-local storage used to carry caller permissions over through
817     * indirect content-provider access.
818     */
819    private class Identity {
820        public int pid;
821        public int uid;
822
823        Identity(int _pid, int _uid) {
824            pid = _pid;
825            uid = _uid;
826        }
827    }
828
829    private static final ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
830
831    /**
832     * All information we have collected about the runtime performance of
833     * any user id that can impact battery performance.
834     */
835    final BatteryStatsService mBatteryStatsService;
836
837    /**
838     * Information about component usage
839     */
840    UsageStatsManagerInternal mUsageStatsService;
841
842    /**
843     * Information about and control over application operations
844     */
845    final AppOpsService mAppOpsService;
846
847    /**
848     * Save recent tasks information across reboots.
849     */
850    final TaskPersister mTaskPersister;
851
852    /**
853     * Current configuration information.  HistoryRecord objects are given
854     * a reference to this object to indicate which configuration they are
855     * currently running in, so this object must be kept immutable.
856     */
857    Configuration mConfiguration = new Configuration();
858
859    /**
860     * Current sequencing integer of the configuration, for skipping old
861     * configurations.
862     */
863    int mConfigurationSeq = 0;
864
865    /**
866     * Hardware-reported OpenGLES version.
867     */
868    final int GL_ES_VERSION;
869
870    /**
871     * List of initialization arguments to pass to all processes when binding applications to them.
872     * For example, references to the commonly used services.
873     */
874    HashMap<String, IBinder> mAppBindArgs;
875
876    /**
877     * Temporary to avoid allocations.  Protected by main lock.
878     */
879    final StringBuilder mStringBuilder = new StringBuilder(256);
880
881    /**
882     * Used to control how we initialize the service.
883     */
884    ComponentName mTopComponent;
885    String mTopAction = Intent.ACTION_MAIN;
886    String mTopData;
887    boolean mProcessesReady = false;
888    boolean mSystemReady = false;
889    boolean mBooting = false;
890    boolean mWaitingUpdate = false;
891    boolean mDidUpdate = false;
892    boolean mOnBattery = false;
893    boolean mLaunchWarningShown = false;
894
895    Context mContext;
896
897    int mFactoryTest;
898
899    boolean mCheckedForSetup;
900
901    /**
902     * The time at which we will allow normal application switches again,
903     * after a call to {@link #stopAppSwitches()}.
904     */
905    long mAppSwitchesAllowedTime;
906
907    /**
908     * This is set to true after the first switch after mAppSwitchesAllowedTime
909     * is set; any switches after that will clear the time.
910     */
911    boolean mDidAppSwitch;
912
913    /**
914     * Last time (in realtime) at which we checked for power usage.
915     */
916    long mLastPowerCheckRealtime;
917
918    /**
919     * Last time (in uptime) at which we checked for power usage.
920     */
921    long mLastPowerCheckUptime;
922
923    /**
924     * Set while we are wanting to sleep, to prevent any
925     * activities from being started/resumed.
926     */
927    private boolean mSleeping = false;
928
929    /**
930     * Set while we are running a voice interaction.  This overrides
931     * sleeping while it is active.
932     */
933    private boolean mRunningVoice = false;
934
935    /**
936     * State of external calls telling us if the device is asleep.
937     */
938    private boolean mWentToSleep = false;
939
940    /**
941     * State of external call telling us if the lock screen is shown.
942     */
943    private boolean mLockScreenShown = false;
944
945    /**
946     * Set if we are shutting down the system, similar to sleeping.
947     */
948    boolean mShuttingDown = false;
949
950    /**
951     * Current sequence id for oom_adj computation traversal.
952     */
953    int mAdjSeq = 0;
954
955    /**
956     * Current sequence id for process LRU updating.
957     */
958    int mLruSeq = 0;
959
960    /**
961     * Keep track of the non-cached/empty process we last found, to help
962     * determine how to distribute cached/empty processes next time.
963     */
964    int mNumNonCachedProcs = 0;
965
966    /**
967     * Keep track of the number of cached hidden procs, to balance oom adj
968     * distribution between those and empty procs.
969     */
970    int mNumCachedHiddenProcs = 0;
971
972    /**
973     * Keep track of the number of service processes we last found, to
974     * determine on the next iteration which should be B services.
975     */
976    int mNumServiceProcs = 0;
977    int mNewNumAServiceProcs = 0;
978    int mNewNumServiceProcs = 0;
979
980    /**
981     * Allow the current computed overall memory level of the system to go down?
982     * This is set to false when we are killing processes for reasons other than
983     * memory management, so that the now smaller process list will not be taken as
984     * an indication that memory is tighter.
985     */
986    boolean mAllowLowerMemLevel = false;
987
988    /**
989     * The last computed memory level, for holding when we are in a state that
990     * processes are going away for other reasons.
991     */
992    int mLastMemoryLevel = ProcessStats.ADJ_MEM_FACTOR_NORMAL;
993
994    /**
995     * The last total number of process we have, to determine if changes actually look
996     * like a shrinking number of process due to lower RAM.
997     */
998    int mLastNumProcesses;
999
1000    /**
1001     * The uptime of the last time we performed idle maintenance.
1002     */
1003    long mLastIdleTime = SystemClock.uptimeMillis();
1004
1005    /**
1006     * Total time spent with RAM that has been added in the past since the last idle time.
1007     */
1008    long mLowRamTimeSinceLastIdle = 0;
1009
1010    /**
1011     * If RAM is currently low, when that horrible situation started.
1012     */
1013    long mLowRamStartTime = 0;
1014
1015    /**
1016     * For reporting to battery stats the current top application.
1017     */
1018    private String mCurResumedPackage = null;
1019    private int mCurResumedUid = -1;
1020
1021    /**
1022     * For reporting to battery stats the apps currently running foreground
1023     * service.  The ProcessMap is package/uid tuples; each of these contain
1024     * an array of the currently foreground processes.
1025     */
1026    final ProcessMap<ArrayList<ProcessRecord>> mForegroundPackages
1027            = new ProcessMap<ArrayList<ProcessRecord>>();
1028
1029    /**
1030     * This is set if we had to do a delayed dexopt of an app before launching
1031     * it, to increase the ANR timeouts in that case.
1032     */
1033    boolean mDidDexOpt;
1034
1035    /**
1036     * Set if the systemServer made a call to enterSafeMode.
1037     */
1038    boolean mSafeMode;
1039
1040    String mDebugApp = null;
1041    boolean mWaitForDebugger = false;
1042    boolean mDebugTransient = false;
1043    String mOrigDebugApp = null;
1044    boolean mOrigWaitForDebugger = false;
1045    boolean mAlwaysFinishActivities = false;
1046    IActivityController mController = null;
1047    String mProfileApp = null;
1048    ProcessRecord mProfileProc = null;
1049    String mProfileFile;
1050    ParcelFileDescriptor mProfileFd;
1051    int mSamplingInterval = 0;
1052    boolean mAutoStopProfiler = false;
1053    int mProfileType = 0;
1054    String mOpenGlTraceApp = null;
1055
1056    static class ProcessChangeItem {
1057        static final int CHANGE_ACTIVITIES = 1<<0;
1058        static final int CHANGE_PROCESS_STATE = 1<<1;
1059        int changes;
1060        int uid;
1061        int pid;
1062        int processState;
1063        boolean foregroundActivities;
1064    }
1065
1066    final RemoteCallbackList<IProcessObserver> mProcessObservers
1067            = new RemoteCallbackList<IProcessObserver>();
1068    ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5];
1069
1070    final ArrayList<ProcessChangeItem> mPendingProcessChanges
1071            = new ArrayList<ProcessChangeItem>();
1072    final ArrayList<ProcessChangeItem> mAvailProcessChanges
1073            = new ArrayList<ProcessChangeItem>();
1074
1075    /**
1076     * Runtime CPU use collection thread.  This object's lock is used to
1077     * protect all related state.
1078     */
1079    final Thread mProcessCpuThread;
1080
1081    /**
1082     * Used to collect process stats when showing not responding dialog.
1083     * Protected by mProcessCpuThread.
1084     */
1085    final ProcessCpuTracker mProcessCpuTracker = new ProcessCpuTracker(
1086            MONITOR_THREAD_CPU_USAGE);
1087    final AtomicLong mLastCpuTime = new AtomicLong(0);
1088    final AtomicBoolean mProcessCpuMutexFree = new AtomicBoolean(true);
1089
1090    long mLastWriteTime = 0;
1091
1092    /**
1093     * Used to retain an update lock when the foreground activity is in
1094     * immersive mode.
1095     */
1096    final UpdateLock mUpdateLock = new UpdateLock("immersive");
1097
1098    /**
1099     * Set to true after the system has finished booting.
1100     */
1101    boolean mBooted = false;
1102
1103    int mProcessLimit = ProcessList.MAX_CACHED_APPS;
1104    int mProcessLimitOverride = -1;
1105
1106    WindowManagerService mWindowManager;
1107
1108    final ActivityThread mSystemThread;
1109
1110    // Holds the current foreground user's id
1111    int mCurrentUserId = 0;
1112    // Holds the target user's id during a user switch
1113    int mTargetUserId = UserHandle.USER_NULL;
1114    // If there are multiple profiles for the current user, their ids are here
1115    // Currently only the primary user can have managed profiles
1116    int[] mCurrentProfileIds = new int[] {UserHandle.USER_OWNER}; // Accessed by ActivityStack
1117
1118    /**
1119     * Mapping from each known user ID to the profile group ID it is associated with.
1120     */
1121    SparseIntArray mUserProfileGroupIdsSelfLocked = new SparseIntArray();
1122
1123    private UserManagerService mUserManager;
1124
1125    private final class AppDeathRecipient implements IBinder.DeathRecipient {
1126        final ProcessRecord mApp;
1127        final int mPid;
1128        final IApplicationThread mAppThread;
1129
1130        AppDeathRecipient(ProcessRecord app, int pid,
1131                IApplicationThread thread) {
1132            if (localLOGV) Slog.v(
1133                TAG, "New death recipient " + this
1134                + " for thread " + thread.asBinder());
1135            mApp = app;
1136            mPid = pid;
1137            mAppThread = thread;
1138        }
1139
1140        @Override
1141        public void binderDied() {
1142            if (localLOGV) Slog.v(
1143                TAG, "Death received in " + this
1144                + " for thread " + mAppThread.asBinder());
1145            synchronized(ActivityManagerService.this) {
1146                appDiedLocked(mApp, mPid, mAppThread);
1147            }
1148        }
1149    }
1150
1151    static final int SHOW_ERROR_MSG = 1;
1152    static final int SHOW_NOT_RESPONDING_MSG = 2;
1153    static final int SHOW_FACTORY_ERROR_MSG = 3;
1154    static final int UPDATE_CONFIGURATION_MSG = 4;
1155    static final int GC_BACKGROUND_PROCESSES_MSG = 5;
1156    static final int WAIT_FOR_DEBUGGER_MSG = 6;
1157    static final int SERVICE_TIMEOUT_MSG = 12;
1158    static final int UPDATE_TIME_ZONE = 13;
1159    static final int SHOW_UID_ERROR_MSG = 14;
1160    static final int IM_FEELING_LUCKY_MSG = 15;
1161    static final int PROC_START_TIMEOUT_MSG = 20;
1162    static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
1163    static final int KILL_APPLICATION_MSG = 22;
1164    static final int FINALIZE_PENDING_INTENT_MSG = 23;
1165    static final int POST_HEAVY_NOTIFICATION_MSG = 24;
1166    static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25;
1167    static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26;
1168    static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27;
1169    static final int CLEAR_DNS_CACHE_MSG = 28;
1170    static final int UPDATE_HTTP_PROXY_MSG = 29;
1171    static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30;
1172    static final int DISPATCH_PROCESSES_CHANGED = 31;
1173    static final int DISPATCH_PROCESS_DIED = 32;
1174    static final int REPORT_MEM_USAGE_MSG = 33;
1175    static final int REPORT_USER_SWITCH_MSG = 34;
1176    static final int CONTINUE_USER_SWITCH_MSG = 35;
1177    static final int USER_SWITCH_TIMEOUT_MSG = 36;
1178    static final int IMMERSIVE_MODE_LOCK_MSG = 37;
1179    static final int PERSIST_URI_GRANTS_MSG = 38;
1180    static final int REQUEST_ALL_PSS_MSG = 39;
1181    static final int START_PROFILES_MSG = 40;
1182    static final int UPDATE_TIME = 41;
1183    static final int SYSTEM_USER_START_MSG = 42;
1184    static final int SYSTEM_USER_CURRENT_MSG = 43;
1185    static final int ENTER_ANIMATION_COMPLETE_MSG = 44;
1186    static final int ENABLE_SCREEN_AFTER_BOOT_MSG = 45;
1187    static final int START_USER_SWITCH_MSG = 46;
1188
1189    static final int FIRST_ACTIVITY_STACK_MSG = 100;
1190    static final int FIRST_BROADCAST_QUEUE_MSG = 200;
1191    static final int FIRST_COMPAT_MODE_MSG = 300;
1192    static final int FIRST_SUPERVISOR_STACK_MSG = 100;
1193
1194    AlertDialog mUidAlert;
1195    CompatModeDialog mCompatModeDialog;
1196    long mLastMemUsageReportTime = 0;
1197
1198    private LockToAppRequestDialog mLockToAppRequest;
1199
1200    /**
1201     * Flag whether the current user is a "monkey", i.e. whether
1202     * the UI is driven by a UI automation tool.
1203     */
1204    private boolean mUserIsMonkey;
1205
1206    /** Flag whether the device has a Recents UI */
1207    boolean mHasRecents;
1208
1209    /** The dimensions of the thumbnails in the Recents UI. */
1210    int mThumbnailWidth;
1211    int mThumbnailHeight;
1212
1213    final ServiceThread mHandlerThread;
1214    final MainHandler mHandler;
1215
1216    final class MainHandler extends Handler {
1217        public MainHandler(Looper looper) {
1218            super(looper, null, true);
1219        }
1220
1221        @Override
1222        public void handleMessage(Message msg) {
1223            switch (msg.what) {
1224            case SHOW_ERROR_MSG: {
1225                HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
1226                boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
1227                        Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
1228                synchronized (ActivityManagerService.this) {
1229                    ProcessRecord proc = (ProcessRecord)data.get("app");
1230                    AppErrorResult res = (AppErrorResult) data.get("result");
1231                    if (proc != null && proc.crashDialog != null) {
1232                        Slog.e(TAG, "App already has crash dialog: " + proc);
1233                        if (res != null) {
1234                            res.set(0);
1235                        }
1236                        return;
1237                    }
1238                    boolean isBackground = (UserHandle.getAppId(proc.uid)
1239                            >= Process.FIRST_APPLICATION_UID
1240                            && proc.pid != MY_PID);
1241                    for (int userId : mCurrentProfileIds) {
1242                        isBackground &= (proc.userId != userId);
1243                    }
1244                    if (isBackground && !showBackground) {
1245                        Slog.w(TAG, "Skipping crash dialog of " + proc + ": background");
1246                        if (res != null) {
1247                            res.set(0);
1248                        }
1249                        return;
1250                    }
1251                    if (mShowDialogs && !mSleeping && !mShuttingDown) {
1252                        Dialog d = new AppErrorDialog(mContext,
1253                                ActivityManagerService.this, res, proc);
1254                        d.show();
1255                        proc.crashDialog = d;
1256                    } else {
1257                        // The device is asleep, so just pretend that the user
1258                        // saw a crash dialog and hit "force quit".
1259                        if (res != null) {
1260                            res.set(0);
1261                        }
1262                    }
1263                }
1264
1265                ensureBootCompleted();
1266            } break;
1267            case SHOW_NOT_RESPONDING_MSG: {
1268                synchronized (ActivityManagerService.this) {
1269                    HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
1270                    ProcessRecord proc = (ProcessRecord)data.get("app");
1271                    if (proc != null && proc.anrDialog != null) {
1272                        Slog.e(TAG, "App already has anr dialog: " + proc);
1273                        return;
1274                    }
1275
1276                    Intent intent = new Intent("android.intent.action.ANR");
1277                    if (!mProcessesReady) {
1278                        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
1279                                | Intent.FLAG_RECEIVER_FOREGROUND);
1280                    }
1281                    broadcastIntentLocked(null, null, intent,
1282                            null, null, 0, null, null, null, AppOpsManager.OP_NONE,
1283                            false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
1284
1285                    if (mShowDialogs) {
1286                        Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
1287                                mContext, proc, (ActivityRecord)data.get("activity"),
1288                                msg.arg1 != 0);
1289                        d.show();
1290                        proc.anrDialog = d;
1291                    } else {
1292                        // Just kill the app if there is no dialog to be shown.
1293                        killAppAtUsersRequest(proc, null);
1294                    }
1295                }
1296
1297                ensureBootCompleted();
1298            } break;
1299            case SHOW_STRICT_MODE_VIOLATION_MSG: {
1300                HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
1301                synchronized (ActivityManagerService.this) {
1302                    ProcessRecord proc = (ProcessRecord) data.get("app");
1303                    if (proc == null) {
1304                        Slog.e(TAG, "App not found when showing strict mode dialog.");
1305                        break;
1306                    }
1307                    if (proc.crashDialog != null) {
1308                        Slog.e(TAG, "App already has strict mode dialog: " + proc);
1309                        return;
1310                    }
1311                    AppErrorResult res = (AppErrorResult) data.get("result");
1312                    if (mShowDialogs && !mSleeping && !mShuttingDown) {
1313                        Dialog d = new StrictModeViolationDialog(mContext,
1314                                ActivityManagerService.this, res, proc);
1315                        d.show();
1316                        proc.crashDialog = d;
1317                    } else {
1318                        // The device is asleep, so just pretend that the user
1319                        // saw a crash dialog and hit "force quit".
1320                        res.set(0);
1321                    }
1322                }
1323                ensureBootCompleted();
1324            } break;
1325            case SHOW_FACTORY_ERROR_MSG: {
1326                Dialog d = new FactoryErrorDialog(
1327                    mContext, msg.getData().getCharSequence("msg"));
1328                d.show();
1329                ensureBootCompleted();
1330            } break;
1331            case UPDATE_CONFIGURATION_MSG: {
1332                final ContentResolver resolver = mContext.getContentResolver();
1333                Settings.System.putConfiguration(resolver, (Configuration)msg.obj);
1334            } break;
1335            case GC_BACKGROUND_PROCESSES_MSG: {
1336                synchronized (ActivityManagerService.this) {
1337                    performAppGcsIfAppropriateLocked();
1338                }
1339            } break;
1340            case WAIT_FOR_DEBUGGER_MSG: {
1341                synchronized (ActivityManagerService.this) {
1342                    ProcessRecord app = (ProcessRecord)msg.obj;
1343                    if (msg.arg1 != 0) {
1344                        if (!app.waitedForDebugger) {
1345                            Dialog d = new AppWaitingForDebuggerDialog(
1346                                    ActivityManagerService.this,
1347                                    mContext, app);
1348                            app.waitDialog = d;
1349                            app.waitedForDebugger = true;
1350                            d.show();
1351                        }
1352                    } else {
1353                        if (app.waitDialog != null) {
1354                            app.waitDialog.dismiss();
1355                            app.waitDialog = null;
1356                        }
1357                    }
1358                }
1359            } break;
1360            case SERVICE_TIMEOUT_MSG: {
1361                if (mDidDexOpt) {
1362                    mDidDexOpt = false;
1363                    Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
1364                    nmsg.obj = msg.obj;
1365                    mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT);
1366                    return;
1367                }
1368                mServices.serviceTimeout((ProcessRecord)msg.obj);
1369            } break;
1370            case UPDATE_TIME_ZONE: {
1371                synchronized (ActivityManagerService.this) {
1372                    for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1373                        ProcessRecord r = mLruProcesses.get(i);
1374                        if (r.thread != null) {
1375                            try {
1376                                r.thread.updateTimeZone();
1377                            } catch (RemoteException ex) {
1378                                Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
1379                            }
1380                        }
1381                    }
1382                }
1383            } break;
1384            case CLEAR_DNS_CACHE_MSG: {
1385                synchronized (ActivityManagerService.this) {
1386                    for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1387                        ProcessRecord r = mLruProcesses.get(i);
1388                        if (r.thread != null) {
1389                            try {
1390                                r.thread.clearDnsCache();
1391                            } catch (RemoteException ex) {
1392                                Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName);
1393                            }
1394                        }
1395                    }
1396                }
1397            } break;
1398            case UPDATE_HTTP_PROXY_MSG: {
1399                ProxyInfo proxy = (ProxyInfo)msg.obj;
1400                String host = "";
1401                String port = "";
1402                String exclList = "";
1403                Uri pacFileUrl = Uri.EMPTY;
1404                if (proxy != null) {
1405                    host = proxy.getHost();
1406                    port = Integer.toString(proxy.getPort());
1407                    exclList = proxy.getExclusionListAsString();
1408                    pacFileUrl = proxy.getPacFileUrl();
1409                }
1410                synchronized (ActivityManagerService.this) {
1411                    for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1412                        ProcessRecord r = mLruProcesses.get(i);
1413                        if (r.thread != null) {
1414                            try {
1415                                r.thread.setHttpProxy(host, port, exclList, pacFileUrl);
1416                            } catch (RemoteException ex) {
1417                                Slog.w(TAG, "Failed to update http proxy for: " +
1418                                        r.info.processName);
1419                            }
1420                        }
1421                    }
1422                }
1423            } break;
1424            case SHOW_UID_ERROR_MSG: {
1425                String title = "System UIDs Inconsistent";
1426                String text = "UIDs on the system are inconsistent, you need to wipe your"
1427                        + " data partition or your device will be unstable.";
1428                Log.e(TAG, title + ": " + text);
1429                if (mShowDialogs) {
1430                    // XXX This is a temporary dialog, no need to localize.
1431                    AlertDialog d = new BaseErrorDialog(mContext);
1432                    d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
1433                    d.setCancelable(false);
1434                    d.setTitle(title);
1435                    d.setMessage(text);
1436                    d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky",
1437                            mHandler.obtainMessage(IM_FEELING_LUCKY_MSG));
1438                    mUidAlert = d;
1439                    d.show();
1440                }
1441            } break;
1442            case IM_FEELING_LUCKY_MSG: {
1443                if (mUidAlert != null) {
1444                    mUidAlert.dismiss();
1445                    mUidAlert = null;
1446                }
1447            } break;
1448            case PROC_START_TIMEOUT_MSG: {
1449                if (mDidDexOpt) {
1450                    mDidDexOpt = false;
1451                    Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1452                    nmsg.obj = msg.obj;
1453                    mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
1454                    return;
1455                }
1456                ProcessRecord app = (ProcessRecord)msg.obj;
1457                synchronized (ActivityManagerService.this) {
1458                    processStartTimedOutLocked(app);
1459                }
1460            } break;
1461            case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
1462                synchronized (ActivityManagerService.this) {
1463                    mStackSupervisor.doPendingActivityLaunchesLocked(true);
1464                }
1465            } break;
1466            case KILL_APPLICATION_MSG: {
1467                synchronized (ActivityManagerService.this) {
1468                    int appid = msg.arg1;
1469                    boolean restart = (msg.arg2 == 1);
1470                    Bundle bundle = (Bundle)msg.obj;
1471                    String pkg = bundle.getString("pkg");
1472                    String reason = bundle.getString("reason");
1473                    forceStopPackageLocked(pkg, appid, restart, false, true, false,
1474                            false, UserHandle.USER_ALL, reason);
1475                }
1476            } break;
1477            case FINALIZE_PENDING_INTENT_MSG: {
1478                ((PendingIntentRecord)msg.obj).completeFinalize();
1479            } break;
1480            case POST_HEAVY_NOTIFICATION_MSG: {
1481                INotificationManager inm = NotificationManager.getService();
1482                if (inm == null) {
1483                    return;
1484                }
1485
1486                ActivityRecord root = (ActivityRecord)msg.obj;
1487                ProcessRecord process = root.app;
1488                if (process == null) {
1489                    return;
1490                }
1491
1492                try {
1493                    Context context = mContext.createPackageContext(process.info.packageName, 0);
1494                    String text = mContext.getString(R.string.heavy_weight_notification,
1495                            context.getApplicationInfo().loadLabel(context.getPackageManager()));
1496                    Notification notification = new Notification();
1497                    notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
1498                    notification.when = 0;
1499                    notification.flags = Notification.FLAG_ONGOING_EVENT;
1500                    notification.tickerText = text;
1501                    notification.defaults = 0; // please be quiet
1502                    notification.sound = null;
1503                    notification.vibrate = null;
1504                    notification.color = mContext.getResources().getColor(
1505                            com.android.internal.R.color.system_notification_accent_color);
1506                    notification.setLatestEventInfo(context, text,
1507                            mContext.getText(R.string.heavy_weight_notification_detail),
1508                            PendingIntent.getActivityAsUser(mContext, 0, root.intent,
1509                                    PendingIntent.FLAG_CANCEL_CURRENT, null,
1510                                    new UserHandle(root.userId)));
1511
1512                    try {
1513                        int[] outId = new int[1];
1514                        inm.enqueueNotificationWithTag("android", "android", null,
1515                                R.string.heavy_weight_notification,
1516                                notification, outId, root.userId);
1517                    } catch (RuntimeException e) {
1518                        Slog.w(ActivityManagerService.TAG,
1519                                "Error showing notification for heavy-weight app", e);
1520                    } catch (RemoteException e) {
1521                    }
1522                } catch (NameNotFoundException e) {
1523                    Slog.w(TAG, "Unable to create context for heavy notification", e);
1524                }
1525            } break;
1526            case CANCEL_HEAVY_NOTIFICATION_MSG: {
1527                INotificationManager inm = NotificationManager.getService();
1528                if (inm == null) {
1529                    return;
1530                }
1531                try {
1532                    inm.cancelNotificationWithTag("android", null,
1533                            R.string.heavy_weight_notification,  msg.arg1);
1534                } catch (RuntimeException e) {
1535                    Slog.w(ActivityManagerService.TAG,
1536                            "Error canceling notification for service", e);
1537                } catch (RemoteException e) {
1538                }
1539            } break;
1540            case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: {
1541                synchronized (ActivityManagerService.this) {
1542                    checkExcessivePowerUsageLocked(true);
1543                    removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
1544                    Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
1545                    sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
1546                }
1547            } break;
1548            case SHOW_COMPAT_MODE_DIALOG_MSG: {
1549                synchronized (ActivityManagerService.this) {
1550                    ActivityRecord ar = (ActivityRecord)msg.obj;
1551                    if (mCompatModeDialog != null) {
1552                        if (mCompatModeDialog.mAppInfo.packageName.equals(
1553                                ar.info.applicationInfo.packageName)) {
1554                            return;
1555                        }
1556                        mCompatModeDialog.dismiss();
1557                        mCompatModeDialog = null;
1558                    }
1559                    if (ar != null && false) {
1560                        if (mCompatModePackages.getPackageAskCompatModeLocked(
1561                                ar.packageName)) {
1562                            int mode = mCompatModePackages.computeCompatModeLocked(
1563                                    ar.info.applicationInfo);
1564                            if (mode == ActivityManager.COMPAT_MODE_DISABLED
1565                                    || mode == ActivityManager.COMPAT_MODE_ENABLED) {
1566                                mCompatModeDialog = new CompatModeDialog(
1567                                        ActivityManagerService.this, mContext,
1568                                        ar.info.applicationInfo);
1569                                mCompatModeDialog.show();
1570                            }
1571                        }
1572                    }
1573                }
1574                break;
1575            }
1576            case DISPATCH_PROCESSES_CHANGED: {
1577                dispatchProcessesChanged();
1578                break;
1579            }
1580            case DISPATCH_PROCESS_DIED: {
1581                final int pid = msg.arg1;
1582                final int uid = msg.arg2;
1583                dispatchProcessDied(pid, uid);
1584                break;
1585            }
1586            case REPORT_MEM_USAGE_MSG: {
1587                final ArrayList<ProcessMemInfo> memInfos = (ArrayList<ProcessMemInfo>)msg.obj;
1588                Thread thread = new Thread() {
1589                    @Override public void run() {
1590                        final SparseArray<ProcessMemInfo> infoMap
1591                                = new SparseArray<ProcessMemInfo>(memInfos.size());
1592                        for (int i=0, N=memInfos.size(); i<N; i++) {
1593                            ProcessMemInfo mi = memInfos.get(i);
1594                            infoMap.put(mi.pid, mi);
1595                        }
1596                        updateCpuStatsNow();
1597                        synchronized (mProcessCpuThread) {
1598                            final int N = mProcessCpuTracker.countStats();
1599                            for (int i=0; i<N; i++) {
1600                                ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
1601                                if (st.vsize > 0) {
1602                                    long pss = Debug.getPss(st.pid, null);
1603                                    if (pss > 0) {
1604                                        if (infoMap.indexOfKey(st.pid) < 0) {
1605                                            ProcessMemInfo mi = new ProcessMemInfo(st.name, st.pid,
1606                                                    ProcessList.NATIVE_ADJ, -1, "native", null);
1607                                            mi.pss = pss;
1608                                            memInfos.add(mi);
1609                                        }
1610                                    }
1611                                }
1612                            }
1613                        }
1614
1615                        long totalPss = 0;
1616                        for (int i=0, N=memInfos.size(); i<N; i++) {
1617                            ProcessMemInfo mi = memInfos.get(i);
1618                            if (mi.pss == 0) {
1619                                mi.pss = Debug.getPss(mi.pid, null);
1620                            }
1621                            totalPss += mi.pss;
1622                        }
1623                        Collections.sort(memInfos, new Comparator<ProcessMemInfo>() {
1624                            @Override public int compare(ProcessMemInfo lhs, ProcessMemInfo rhs) {
1625                                if (lhs.oomAdj != rhs.oomAdj) {
1626                                    return lhs.oomAdj < rhs.oomAdj ? -1 : 1;
1627                                }
1628                                if (lhs.pss != rhs.pss) {
1629                                    return lhs.pss < rhs.pss ? 1 : -1;
1630                                }
1631                                return 0;
1632                            }
1633                        });
1634
1635                        StringBuilder tag = new StringBuilder(128);
1636                        StringBuilder stack = new StringBuilder(128);
1637                        tag.append("Low on memory -- ");
1638                        appendMemBucket(tag, totalPss, "total", false);
1639                        appendMemBucket(stack, totalPss, "total", true);
1640
1641                        StringBuilder logBuilder = new StringBuilder(1024);
1642                        logBuilder.append("Low on memory:\n");
1643
1644                        boolean firstLine = true;
1645                        int lastOomAdj = Integer.MIN_VALUE;
1646                        for (int i=0, N=memInfos.size(); i<N; i++) {
1647                            ProcessMemInfo mi = memInfos.get(i);
1648
1649                            if (mi.oomAdj != ProcessList.NATIVE_ADJ
1650                                    && (mi.oomAdj < ProcessList.SERVICE_ADJ
1651                                            || mi.oomAdj == ProcessList.HOME_APP_ADJ
1652                                            || mi.oomAdj == ProcessList.PREVIOUS_APP_ADJ)) {
1653                                if (lastOomAdj != mi.oomAdj) {
1654                                    lastOomAdj = mi.oomAdj;
1655                                    if (mi.oomAdj <= ProcessList.FOREGROUND_APP_ADJ) {
1656                                        tag.append(" / ");
1657                                    }
1658                                    if (mi.oomAdj >= ProcessList.FOREGROUND_APP_ADJ) {
1659                                        if (firstLine) {
1660                                            stack.append(":");
1661                                            firstLine = false;
1662                                        }
1663                                        stack.append("\n\t at ");
1664                                    } else {
1665                                        stack.append("$");
1666                                    }
1667                                } else {
1668                                    tag.append(" ");
1669                                    stack.append("$");
1670                                }
1671                                if (mi.oomAdj <= ProcessList.FOREGROUND_APP_ADJ) {
1672                                    appendMemBucket(tag, mi.pss, mi.name, false);
1673                                }
1674                                appendMemBucket(stack, mi.pss, mi.name, true);
1675                                if (mi.oomAdj >= ProcessList.FOREGROUND_APP_ADJ
1676                                        && ((i+1) >= N || memInfos.get(i+1).oomAdj != lastOomAdj)) {
1677                                    stack.append("(");
1678                                    for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) {
1679                                        if (DUMP_MEM_OOM_ADJ[k] == mi.oomAdj) {
1680                                            stack.append(DUMP_MEM_OOM_LABEL[k]);
1681                                            stack.append(":");
1682                                            stack.append(DUMP_MEM_OOM_ADJ[k]);
1683                                        }
1684                                    }
1685                                    stack.append(")");
1686                                }
1687                            }
1688
1689                            logBuilder.append("  ");
1690                            logBuilder.append(ProcessList.makeOomAdjString(mi.oomAdj));
1691                            logBuilder.append(' ');
1692                            logBuilder.append(ProcessList.makeProcStateString(mi.procState));
1693                            logBuilder.append(' ');
1694                            ProcessList.appendRamKb(logBuilder, mi.pss);
1695                            logBuilder.append(" kB: ");
1696                            logBuilder.append(mi.name);
1697                            logBuilder.append(" (");
1698                            logBuilder.append(mi.pid);
1699                            logBuilder.append(") ");
1700                            logBuilder.append(mi.adjType);
1701                            logBuilder.append('\n');
1702                            if (mi.adjReason != null) {
1703                                logBuilder.append("                      ");
1704                                logBuilder.append(mi.adjReason);
1705                                logBuilder.append('\n');
1706                            }
1707                        }
1708
1709                        logBuilder.append("           ");
1710                        ProcessList.appendRamKb(logBuilder, totalPss);
1711                        logBuilder.append(" kB: TOTAL\n");
1712
1713                        long[] infos = new long[Debug.MEMINFO_COUNT];
1714                        Debug.getMemInfo(infos);
1715                        logBuilder.append("  MemInfo: ");
1716                        logBuilder.append(infos[Debug.MEMINFO_SLAB]).append(" kB slab, ");
1717                        logBuilder.append(infos[Debug.MEMINFO_SHMEM]).append(" kB shmem, ");
1718                        logBuilder.append(infos[Debug.MEMINFO_BUFFERS]).append(" kB buffers, ");
1719                        logBuilder.append(infos[Debug.MEMINFO_CACHED]).append(" kB cached, ");
1720                        logBuilder.append(infos[Debug.MEMINFO_FREE]).append(" kB free\n");
1721                        if (infos[Debug.MEMINFO_ZRAM_TOTAL] != 0) {
1722                            logBuilder.append("  ZRAM: ");
1723                            logBuilder.append(infos[Debug.MEMINFO_ZRAM_TOTAL]);
1724                            logBuilder.append(" kB RAM, ");
1725                            logBuilder.append(infos[Debug.MEMINFO_SWAP_TOTAL]);
1726                            logBuilder.append(" kB swap total, ");
1727                            logBuilder.append(infos[Debug.MEMINFO_SWAP_FREE]);
1728                            logBuilder.append(" kB swap free\n");
1729                        }
1730                        Slog.i(TAG, logBuilder.toString());
1731
1732                        StringBuilder dropBuilder = new StringBuilder(1024);
1733                        /*
1734                        StringWriter oomSw = new StringWriter();
1735                        PrintWriter oomPw = new FastPrintWriter(oomSw, false, 256);
1736                        StringWriter catSw = new StringWriter();
1737                        PrintWriter catPw = new FastPrintWriter(catSw, false, 256);
1738                        String[] emptyArgs = new String[] { };
1739                        dumpApplicationMemoryUsage(null, oomPw, "  ", emptyArgs, true, catPw);
1740                        oomPw.flush();
1741                        String oomString = oomSw.toString();
1742                        */
1743                        dropBuilder.append(stack);
1744                        dropBuilder.append('\n');
1745                        dropBuilder.append('\n');
1746                        dropBuilder.append(logBuilder);
1747                        dropBuilder.append('\n');
1748                        /*
1749                        dropBuilder.append(oomString);
1750                        dropBuilder.append('\n');
1751                        */
1752                        StringWriter catSw = new StringWriter();
1753                        synchronized (ActivityManagerService.this) {
1754                            PrintWriter catPw = new FastPrintWriter(catSw, false, 256);
1755                            String[] emptyArgs = new String[] { };
1756                            catPw.println();
1757                            dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null);
1758                            catPw.println();
1759                            mServices.dumpServicesLocked(null, catPw, emptyArgs, 0,
1760                                    false, false, null);
1761                            catPw.println();
1762                            dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null);
1763                            catPw.flush();
1764                        }
1765                        dropBuilder.append(catSw.toString());
1766                        addErrorToDropBox("lowmem", null, "system_server", null,
1767                                null, tag.toString(), dropBuilder.toString(), null, null);
1768                        //Slog.i(TAG, "Sent to dropbox:");
1769                        //Slog.i(TAG, dropBuilder.toString());
1770                        synchronized (ActivityManagerService.this) {
1771                            long now = SystemClock.uptimeMillis();
1772                            if (mLastMemUsageReportTime < now) {
1773                                mLastMemUsageReportTime = now;
1774                            }
1775                        }
1776                    }
1777                };
1778                thread.start();
1779                break;
1780            }
1781            case START_USER_SWITCH_MSG: {
1782                showUserSwitchDialog(msg.arg1, (String) msg.obj);
1783                break;
1784            }
1785            case REPORT_USER_SWITCH_MSG: {
1786                dispatchUserSwitch((UserStartedState) msg.obj, msg.arg1, msg.arg2);
1787                break;
1788            }
1789            case CONTINUE_USER_SWITCH_MSG: {
1790                continueUserSwitch((UserStartedState) msg.obj, msg.arg1, msg.arg2);
1791                break;
1792            }
1793            case USER_SWITCH_TIMEOUT_MSG: {
1794                timeoutUserSwitch((UserStartedState) msg.obj, msg.arg1, msg.arg2);
1795                break;
1796            }
1797            case IMMERSIVE_MODE_LOCK_MSG: {
1798                final boolean nextState = (msg.arg1 != 0);
1799                if (mUpdateLock.isHeld() != nextState) {
1800                    if (DEBUG_IMMERSIVE) {
1801                        final ActivityRecord r = (ActivityRecord) msg.obj;
1802                        Slog.d(TAG, "Applying new update lock state '" + nextState + "' for " + r);
1803                    }
1804                    if (nextState) {
1805                        mUpdateLock.acquire();
1806                    } else {
1807                        mUpdateLock.release();
1808                    }
1809                }
1810                break;
1811            }
1812            case PERSIST_URI_GRANTS_MSG: {
1813                writeGrantedUriPermissions();
1814                break;
1815            }
1816            case REQUEST_ALL_PSS_MSG: {
1817                requestPssAllProcsLocked(SystemClock.uptimeMillis(), true, false);
1818                break;
1819            }
1820            case START_PROFILES_MSG: {
1821                synchronized (ActivityManagerService.this) {
1822                    startProfilesLocked();
1823                }
1824                break;
1825            }
1826            case UPDATE_TIME: {
1827                synchronized (ActivityManagerService.this) {
1828                    for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1829                        ProcessRecord r = mLruProcesses.get(i);
1830                        if (r.thread != null) {
1831                            try {
1832                                r.thread.updateTimePrefs(msg.arg1 == 0 ? false : true);
1833                            } catch (RemoteException ex) {
1834                                Slog.w(TAG, "Failed to update preferences for: " + r.info.processName);
1835                            }
1836                        }
1837                    }
1838                }
1839                break;
1840            }
1841            case SYSTEM_USER_START_MSG: {
1842                mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_USER_RUNNING_START,
1843                        Integer.toString(msg.arg1), msg.arg1);
1844                mSystemServiceManager.startUser(msg.arg1);
1845                break;
1846            }
1847            case SYSTEM_USER_CURRENT_MSG: {
1848                mBatteryStatsService.noteEvent(
1849                        BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_FINISH,
1850                        Integer.toString(msg.arg2), msg.arg2);
1851                mBatteryStatsService.noteEvent(
1852                        BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_START,
1853                        Integer.toString(msg.arg1), msg.arg1);
1854                mSystemServiceManager.switchUser(msg.arg1);
1855                mLockToAppRequest.clearPrompt();
1856                break;
1857            }
1858            case ENTER_ANIMATION_COMPLETE_MSG: {
1859                synchronized (ActivityManagerService.this) {
1860                    ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
1861                    if (r != null && r.app != null && r.app.thread != null) {
1862                        try {
1863                            r.app.thread.scheduleEnterAnimationComplete(r.appToken);
1864                        } catch (RemoteException e) {
1865                        }
1866                    }
1867                }
1868                break;
1869            }
1870            case ENABLE_SCREEN_AFTER_BOOT_MSG: {
1871                enableScreenAfterBoot();
1872                break;
1873            }
1874            }
1875        }
1876    };
1877
1878    static final int COLLECT_PSS_BG_MSG = 1;
1879
1880    final Handler mBgHandler = new Handler(BackgroundThread.getHandler().getLooper()) {
1881        @Override
1882        public void handleMessage(Message msg) {
1883            switch (msg.what) {
1884            case COLLECT_PSS_BG_MSG: {
1885                long start = SystemClock.uptimeMillis();
1886                MemInfoReader memInfo = null;
1887                synchronized (ActivityManagerService.this) {
1888                    if (mFullPssPending) {
1889                        mFullPssPending = false;
1890                        memInfo = new MemInfoReader();
1891                    }
1892                }
1893                if (memInfo != null) {
1894                    updateCpuStatsNow();
1895                    long nativeTotalPss = 0;
1896                    synchronized (mProcessCpuThread) {
1897                        final int N = mProcessCpuTracker.countStats();
1898                        for (int j=0; j<N; j++) {
1899                            ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(j);
1900                            if (st.vsize <= 0 || st.uid >= Process.FIRST_APPLICATION_UID) {
1901                                // This is definitely an application process; skip it.
1902                                continue;
1903                            }
1904                            synchronized (mPidsSelfLocked) {
1905                                if (mPidsSelfLocked.indexOfKey(st.pid) >= 0) {
1906                                    // This is one of our own processes; skip it.
1907                                    continue;
1908                                }
1909                            }
1910                            nativeTotalPss += Debug.getPss(st.pid, null);
1911                        }
1912                    }
1913                    memInfo.readMemInfo();
1914                    synchronized (this) {
1915                        if (DEBUG_PSS) Slog.d(TAG, "Collected native and kernel memory in "
1916                                + (SystemClock.uptimeMillis()-start) + "ms");
1917                        mProcessStats.addSysMemUsageLocked(memInfo.getCachedSizeKb(),
1918                                memInfo.getFreeSizeKb(), memInfo.getZramTotalSizeKb(),
1919                                memInfo.getBuffersSizeKb()+memInfo.getShmemSizeKb()
1920                                        +memInfo.getSlabSizeKb(),
1921                                nativeTotalPss);
1922                    }
1923                }
1924
1925                int i=0, num=0;
1926                long[] tmp = new long[1];
1927                do {
1928                    ProcessRecord proc;
1929                    int procState;
1930                    int pid;
1931                    synchronized (ActivityManagerService.this) {
1932                        if (i >= mPendingPssProcesses.size()) {
1933                            if (DEBUG_PSS) Slog.d(TAG, "Collected PSS of " + num + " of " + i
1934                                    + " processes in " + (SystemClock.uptimeMillis()-start) + "ms");
1935                            mPendingPssProcesses.clear();
1936                            return;
1937                        }
1938                        proc = mPendingPssProcesses.get(i);
1939                        procState = proc.pssProcState;
1940                        if (proc.thread != null && procState == proc.setProcState) {
1941                            pid = proc.pid;
1942                        } else {
1943                            proc = null;
1944                            pid = 0;
1945                        }
1946                        i++;
1947                    }
1948                    if (proc != null) {
1949                        long pss = Debug.getPss(pid, tmp);
1950                        synchronized (ActivityManagerService.this) {
1951                            if (proc.thread != null && proc.setProcState == procState
1952                                    && proc.pid == pid) {
1953                                num++;
1954                                proc.lastPssTime = SystemClock.uptimeMillis();
1955                                proc.baseProcessTracker.addPss(pss, tmp[0], true, proc.pkgList);
1956                                if (DEBUG_PSS) Slog.d(TAG, "PSS of " + proc.toShortString()
1957                                        + ": " + pss + " lastPss=" + proc.lastPss
1958                                        + " state=" + ProcessList.makeProcStateString(procState));
1959                                if (proc.initialIdlePss == 0) {
1960                                    proc.initialIdlePss = pss;
1961                                }
1962                                proc.lastPss = pss;
1963                                if (procState >= ActivityManager.PROCESS_STATE_HOME) {
1964                                    proc.lastCachedPss = pss;
1965                                }
1966                            }
1967                        }
1968                    }
1969                } while (true);
1970            }
1971            }
1972        }
1973    };
1974
1975    /**
1976     * Monitor for package changes and update our internal state.
1977     */
1978    private final PackageMonitor mPackageMonitor = new PackageMonitor() {
1979        @Override
1980        public void onPackageRemoved(String packageName, int uid) {
1981            // Remove all tasks with activities in the specified package from the list of recent tasks
1982            synchronized (ActivityManagerService.this) {
1983                for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
1984                    TaskRecord tr = mRecentTasks.get(i);
1985                    ComponentName cn = tr.intent.getComponent();
1986                    if (cn != null && cn.getPackageName().equals(packageName)) {
1987                        // If the package name matches, remove the task and kill the process
1988                        removeTaskByIdLocked(tr.taskId, ActivityManager.REMOVE_TASK_KILL_PROCESS);
1989                    }
1990                }
1991            }
1992        }
1993
1994        @Override
1995        public boolean onPackageChanged(String packageName, int uid, String[] components) {
1996            onPackageModified(packageName);
1997            return true;
1998        }
1999
2000        @Override
2001        public void onPackageModified(String packageName) {
2002            final PackageManager pm = mContext.getPackageManager();
2003            final ArrayList<Pair<Intent, Integer>> recentTaskIntents =
2004                    new ArrayList<Pair<Intent, Integer>>();
2005            final ArrayList<Integer> tasksToRemove = new ArrayList<Integer>();
2006            // Copy the list of recent tasks so that we don't hold onto the lock on
2007            // ActivityManagerService for long periods while checking if components exist.
2008            synchronized (ActivityManagerService.this) {
2009                for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
2010                    TaskRecord tr = mRecentTasks.get(i);
2011                    recentTaskIntents.add(new Pair<Intent, Integer>(tr.intent, tr.taskId));
2012                }
2013            }
2014            // Check the recent tasks and filter out all tasks with components that no longer exist.
2015            Intent tmpI = new Intent();
2016            for (int i = recentTaskIntents.size() - 1; i >= 0; i--) {
2017                Pair<Intent, Integer> p = recentTaskIntents.get(i);
2018                ComponentName cn = p.first.getComponent();
2019                if (cn != null && cn.getPackageName().equals(packageName)) {
2020                    try {
2021                        // Add the task to the list to remove if the component no longer exists
2022                        tmpI.setComponent(cn);
2023                        if (pm.queryIntentActivities(tmpI, PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
2024                            tasksToRemove.add(p.second);
2025                        }
2026                    } catch (Exception e) {}
2027                }
2028            }
2029            // Prune all the tasks with removed components from the list of recent tasks
2030            synchronized (ActivityManagerService.this) {
2031                for (int i = tasksToRemove.size() - 1; i >= 0; i--) {
2032                    // Remove the task but don't kill the process (since other components in that
2033                    // package may still be running and in the background)
2034                    removeTaskByIdLocked(tasksToRemove.get(i), 0);
2035                }
2036            }
2037        }
2038
2039        @Override
2040        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
2041            // Force stop the specified packages
2042            if (packages != null) {
2043                for (String pkg : packages) {
2044                    synchronized (ActivityManagerService.this) {
2045                        if (forceStopPackageLocked(pkg, -1, false, false, false, false, false, 0,
2046                                "finished booting")) {
2047                            return true;
2048                        }
2049                    }
2050                }
2051            }
2052            return false;
2053        }
2054    };
2055
2056    public void setSystemProcess() {
2057        try {
2058            ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
2059            ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
2060            ServiceManager.addService("meminfo", new MemBinder(this));
2061            ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
2062            ServiceManager.addService("dbinfo", new DbBinder(this));
2063            if (MONITOR_CPU_USAGE) {
2064                ServiceManager.addService("cpuinfo", new CpuBinder(this));
2065            }
2066            ServiceManager.addService("permission", new PermissionController(this));
2067
2068            ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
2069                    "android", STOCK_PM_FLAGS);
2070            mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
2071
2072            synchronized (this) {
2073                ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0);
2074                app.persistent = true;
2075                app.pid = MY_PID;
2076                app.maxAdj = ProcessList.SYSTEM_ADJ;
2077                app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
2078                mProcessNames.put(app.processName, app.uid, app);
2079                synchronized (mPidsSelfLocked) {
2080                    mPidsSelfLocked.put(app.pid, app);
2081                }
2082                updateLruProcessLocked(app, false, null);
2083                updateOomAdjLocked();
2084            }
2085        } catch (PackageManager.NameNotFoundException e) {
2086            throw new RuntimeException(
2087                    "Unable to find android system package", e);
2088        }
2089    }
2090
2091    public void setWindowManager(WindowManagerService wm) {
2092        mWindowManager = wm;
2093        mStackSupervisor.setWindowManager(wm);
2094    }
2095
2096    public void setUsageStatsManager(UsageStatsManagerInternal usageStatsManager) {
2097        mUsageStatsService = usageStatsManager;
2098    }
2099
2100    public void startObservingNativeCrashes() {
2101        final NativeCrashListener ncl = new NativeCrashListener(this);
2102        ncl.start();
2103    }
2104
2105    public IAppOpsService getAppOpsService() {
2106        return mAppOpsService;
2107    }
2108
2109    static class MemBinder extends Binder {
2110        ActivityManagerService mActivityManagerService;
2111        MemBinder(ActivityManagerService activityManagerService) {
2112            mActivityManagerService = activityManagerService;
2113        }
2114
2115        @Override
2116        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2117            if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
2118                    != PackageManager.PERMISSION_GRANTED) {
2119                pw.println("Permission Denial: can't dump meminfo from from pid="
2120                        + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2121                        + " without permission " + android.Manifest.permission.DUMP);
2122                return;
2123            }
2124
2125            mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, "  ", args, false, null);
2126        }
2127    }
2128
2129    static class GraphicsBinder extends Binder {
2130        ActivityManagerService mActivityManagerService;
2131        GraphicsBinder(ActivityManagerService activityManagerService) {
2132            mActivityManagerService = activityManagerService;
2133        }
2134
2135        @Override
2136        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2137            if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
2138                    != PackageManager.PERMISSION_GRANTED) {
2139                pw.println("Permission Denial: can't dump gfxinfo from from pid="
2140                        + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2141                        + " without permission " + android.Manifest.permission.DUMP);
2142                return;
2143            }
2144
2145            mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args);
2146        }
2147    }
2148
2149    static class DbBinder extends Binder {
2150        ActivityManagerService mActivityManagerService;
2151        DbBinder(ActivityManagerService activityManagerService) {
2152            mActivityManagerService = activityManagerService;
2153        }
2154
2155        @Override
2156        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2157            if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
2158                    != PackageManager.PERMISSION_GRANTED) {
2159                pw.println("Permission Denial: can't dump dbinfo from from pid="
2160                        + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2161                        + " without permission " + android.Manifest.permission.DUMP);
2162                return;
2163            }
2164
2165            mActivityManagerService.dumpDbInfo(fd, pw, args);
2166        }
2167    }
2168
2169    static class CpuBinder extends Binder {
2170        ActivityManagerService mActivityManagerService;
2171        CpuBinder(ActivityManagerService activityManagerService) {
2172            mActivityManagerService = activityManagerService;
2173        }
2174
2175        @Override
2176        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2177            if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
2178                    != PackageManager.PERMISSION_GRANTED) {
2179                pw.println("Permission Denial: can't dump cpuinfo from from pid="
2180                        + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2181                        + " without permission " + android.Manifest.permission.DUMP);
2182                return;
2183            }
2184
2185            synchronized (mActivityManagerService.mProcessCpuThread) {
2186                pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentLoad());
2187                pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentState(
2188                        SystemClock.uptimeMillis()));
2189            }
2190        }
2191    }
2192
2193    public static final class Lifecycle extends SystemService {
2194        private final ActivityManagerService mService;
2195
2196        public Lifecycle(Context context) {
2197            super(context);
2198            mService = new ActivityManagerService(context);
2199        }
2200
2201        @Override
2202        public void onStart() {
2203            mService.start();
2204        }
2205
2206        public ActivityManagerService getService() {
2207            return mService;
2208        }
2209    }
2210
2211    // Note: This method is invoked on the main thread but may need to attach various
2212    // handlers to other threads.  So take care to be explicit about the looper.
2213    public ActivityManagerService(Context systemContext) {
2214        mContext = systemContext;
2215        mFactoryTest = FactoryTest.getMode();
2216        mSystemThread = ActivityThread.currentActivityThread();
2217
2218        Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
2219
2220        mHandlerThread = new ServiceThread(TAG,
2221                android.os.Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
2222        mHandlerThread.start();
2223        mHandler = new MainHandler(mHandlerThread.getLooper());
2224
2225        mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
2226                "foreground", BROADCAST_FG_TIMEOUT, false);
2227        mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
2228                "background", BROADCAST_BG_TIMEOUT, true);
2229        mBroadcastQueues[0] = mFgBroadcastQueue;
2230        mBroadcastQueues[1] = mBgBroadcastQueue;
2231
2232        mServices = new ActiveServices(this);
2233        mProviderMap = new ProviderMap(this);
2234
2235        // TODO: Move creation of battery stats service outside of activity manager service.
2236        File dataDir = Environment.getDataDirectory();
2237        File systemDir = new File(dataDir, "system");
2238        systemDir.mkdirs();
2239        mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
2240        mBatteryStatsService.getActiveStatistics().readLocked();
2241        mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
2242        mOnBattery = DEBUG_POWER ? true
2243                : mBatteryStatsService.getActiveStatistics().getIsOnBattery();
2244        mBatteryStatsService.getActiveStatistics().setCallback(this);
2245
2246        mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
2247
2248        mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml"), mHandler);
2249
2250        mGrantFile = new AtomicFile(new File(systemDir, "urigrants.xml"));
2251
2252        // User 0 is the first and only user that runs at boot.
2253        mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true));
2254        mUserLru.add(Integer.valueOf(0));
2255        updateStartedUserArrayLocked();
2256
2257        GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
2258            ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
2259
2260        mConfiguration.setToDefaults();
2261        mConfiguration.setLocale(Locale.getDefault());
2262
2263        mConfigurationSeq = mConfiguration.seq = 1;
2264        mProcessCpuTracker.init();
2265
2266        mCompatModePackages = new CompatModePackages(this, systemDir, mHandler);
2267        mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler);
2268        mStackSupervisor = new ActivityStackSupervisor(this);
2269        mTaskPersister = new TaskPersister(systemDir, mStackSupervisor);
2270
2271        mProcessCpuThread = new Thread("CpuTracker") {
2272            @Override
2273            public void run() {
2274                while (true) {
2275                    try {
2276                        try {
2277                            synchronized(this) {
2278                                final long now = SystemClock.uptimeMillis();
2279                                long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
2280                                long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
2281                                //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
2282                                //        + ", write delay=" + nextWriteDelay);
2283                                if (nextWriteDelay < nextCpuDelay) {
2284                                    nextCpuDelay = nextWriteDelay;
2285                                }
2286                                if (nextCpuDelay > 0) {
2287                                    mProcessCpuMutexFree.set(true);
2288                                    this.wait(nextCpuDelay);
2289                                }
2290                            }
2291                        } catch (InterruptedException e) {
2292                        }
2293                        updateCpuStatsNow();
2294                    } catch (Exception e) {
2295                        Slog.e(TAG, "Unexpected exception collecting process stats", e);
2296                    }
2297                }
2298            }
2299        };
2300
2301        mLockToAppRequest = new LockToAppRequestDialog(mContext, this);
2302
2303        Watchdog.getInstance().addMonitor(this);
2304        Watchdog.getInstance().addThread(mHandler);
2305    }
2306
2307    public void setSystemServiceManager(SystemServiceManager mgr) {
2308        mSystemServiceManager = mgr;
2309    }
2310
2311    private void start() {
2312        Process.removeAllProcessGroups();
2313        mProcessCpuThread.start();
2314
2315        mBatteryStatsService.publish(mContext);
2316        mAppOpsService.publish(mContext);
2317        Slog.d("AppOps", "AppOpsService published");
2318        LocalServices.addService(ActivityManagerInternal.class, new LocalService());
2319    }
2320
2321    public void initPowerManagement() {
2322        mStackSupervisor.initPowerManagement();
2323        mBatteryStatsService.initPowerManagement();
2324    }
2325
2326    @Override
2327    public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
2328            throws RemoteException {
2329        if (code == SYSPROPS_TRANSACTION) {
2330            // We need to tell all apps about the system property change.
2331            ArrayList<IBinder> procs = new ArrayList<IBinder>();
2332            synchronized(this) {
2333                final int NP = mProcessNames.getMap().size();
2334                for (int ip=0; ip<NP; ip++) {
2335                    SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
2336                    final int NA = apps.size();
2337                    for (int ia=0; ia<NA; ia++) {
2338                        ProcessRecord app = apps.valueAt(ia);
2339                        if (app.thread != null) {
2340                            procs.add(app.thread.asBinder());
2341                        }
2342                    }
2343                }
2344            }
2345
2346            int N = procs.size();
2347            for (int i=0; i<N; i++) {
2348                Parcel data2 = Parcel.obtain();
2349                try {
2350                    procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0);
2351                } catch (RemoteException e) {
2352                }
2353                data2.recycle();
2354            }
2355        }
2356        try {
2357            return super.onTransact(code, data, reply, flags);
2358        } catch (RuntimeException e) {
2359            // The activity manager only throws security exceptions, so let's
2360            // log all others.
2361            if (!(e instanceof SecurityException)) {
2362                Slog.wtf(TAG, "Activity Manager Crash", e);
2363            }
2364            throw e;
2365        }
2366    }
2367
2368    void updateCpuStats() {
2369        final long now = SystemClock.uptimeMillis();
2370        if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
2371            return;
2372        }
2373        if (mProcessCpuMutexFree.compareAndSet(true, false)) {
2374            synchronized (mProcessCpuThread) {
2375                mProcessCpuThread.notify();
2376            }
2377        }
2378    }
2379
2380    void updateCpuStatsNow() {
2381        synchronized (mProcessCpuThread) {
2382            mProcessCpuMutexFree.set(false);
2383            final long now = SystemClock.uptimeMillis();
2384            boolean haveNewCpuStats = false;
2385
2386            if (MONITOR_CPU_USAGE &&
2387                    mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
2388                mLastCpuTime.set(now);
2389                haveNewCpuStats = true;
2390                mProcessCpuTracker.update();
2391                //Slog.i(TAG, mProcessCpu.printCurrentState());
2392                //Slog.i(TAG, "Total CPU usage: "
2393                //        + mProcessCpu.getTotalCpuPercent() + "%");
2394
2395                // Slog the cpu usage if the property is set.
2396                if ("true".equals(SystemProperties.get("events.cpu"))) {
2397                    int user = mProcessCpuTracker.getLastUserTime();
2398                    int system = mProcessCpuTracker.getLastSystemTime();
2399                    int iowait = mProcessCpuTracker.getLastIoWaitTime();
2400                    int irq = mProcessCpuTracker.getLastIrqTime();
2401                    int softIrq = mProcessCpuTracker.getLastSoftIrqTime();
2402                    int idle = mProcessCpuTracker.getLastIdleTime();
2403
2404                    int total = user + system + iowait + irq + softIrq + idle;
2405                    if (total == 0) total = 1;
2406
2407                    EventLog.writeEvent(EventLogTags.CPU,
2408                            ((user+system+iowait+irq+softIrq) * 100) / total,
2409                            (user * 100) / total,
2410                            (system * 100) / total,
2411                            (iowait * 100) / total,
2412                            (irq * 100) / total,
2413                            (softIrq * 100) / total);
2414                }
2415            }
2416
2417            long[] cpuSpeedTimes = mProcessCpuTracker.getLastCpuSpeedTimes();
2418            final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
2419            synchronized(bstats) {
2420                synchronized(mPidsSelfLocked) {
2421                    if (haveNewCpuStats) {
2422                        if (mOnBattery) {
2423                            int perc = bstats.startAddingCpuLocked();
2424                            int totalUTime = 0;
2425                            int totalSTime = 0;
2426                            final int N = mProcessCpuTracker.countStats();
2427                            for (int i=0; i<N; i++) {
2428                                ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
2429                                if (!st.working) {
2430                                    continue;
2431                                }
2432                                ProcessRecord pr = mPidsSelfLocked.get(st.pid);
2433                                int otherUTime = (st.rel_utime*perc)/100;
2434                                int otherSTime = (st.rel_stime*perc)/100;
2435                                totalUTime += otherUTime;
2436                                totalSTime += otherSTime;
2437                                if (pr != null) {
2438                                    BatteryStatsImpl.Uid.Proc ps = pr.curProcBatteryStats;
2439                                    if (ps == null || !ps.isActive()) {
2440                                        pr.curProcBatteryStats = ps = bstats.getProcessStatsLocked(
2441                                                pr.info.uid, pr.processName);
2442                                    }
2443                                    ps.addCpuTimeLocked(st.rel_utime-otherUTime,
2444                                            st.rel_stime-otherSTime);
2445                                    ps.addSpeedStepTimes(cpuSpeedTimes);
2446                                    pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10;
2447                                } else {
2448                                    BatteryStatsImpl.Uid.Proc ps = st.batteryStats;
2449                                    if (ps == null || !ps.isActive()) {
2450                                        st.batteryStats = ps = bstats.getProcessStatsLocked(
2451                                                bstats.mapUid(st.uid), st.name);
2452                                    }
2453                                    ps.addCpuTimeLocked(st.rel_utime-otherUTime,
2454                                            st.rel_stime-otherSTime);
2455                                    ps.addSpeedStepTimes(cpuSpeedTimes);
2456                                }
2457                            }
2458                            bstats.finishAddingCpuLocked(perc, totalUTime,
2459                                    totalSTime, cpuSpeedTimes);
2460                        }
2461                    }
2462                }
2463
2464                if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
2465                    mLastWriteTime = now;
2466                    mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
2467                }
2468            }
2469        }
2470    }
2471
2472    @Override
2473    public void batteryNeedsCpuUpdate() {
2474        updateCpuStatsNow();
2475    }
2476
2477    @Override
2478    public void batteryPowerChanged(boolean onBattery) {
2479        // When plugging in, update the CPU stats first before changing
2480        // the plug state.
2481        updateCpuStatsNow();
2482        synchronized (this) {
2483            synchronized(mPidsSelfLocked) {
2484                mOnBattery = DEBUG_POWER ? true : onBattery;
2485            }
2486        }
2487    }
2488
2489    /**
2490     * Initialize the application bind args. These are passed to each
2491     * process when the bindApplication() IPC is sent to the process. They're
2492     * lazily setup to make sure the services are running when they're asked for.
2493     */
2494    private HashMap<String, IBinder> getCommonServicesLocked() {
2495        if (mAppBindArgs == null) {
2496            mAppBindArgs = new HashMap<String, IBinder>();
2497
2498            // Setup the application init args
2499            mAppBindArgs.put("package", ServiceManager.getService("package"));
2500            mAppBindArgs.put("window", ServiceManager.getService("window"));
2501            mAppBindArgs.put(Context.ALARM_SERVICE,
2502                    ServiceManager.getService(Context.ALARM_SERVICE));
2503        }
2504        return mAppBindArgs;
2505    }
2506
2507    final void setFocusedActivityLocked(ActivityRecord r) {
2508        if (mFocusedActivity != r) {
2509            if (DEBUG_FOCUS) Slog.d(TAG, "setFocusedActivityLocked: r=" + r);
2510            mFocusedActivity = r;
2511            if (r.task != null && r.task.voiceInteractor != null) {
2512                startRunningVoiceLocked();
2513            } else {
2514                finishRunningVoiceLocked();
2515            }
2516            mStackSupervisor.setFocusedStack(r);
2517            if (r != null) {
2518                mWindowManager.setFocusedApp(r.appToken, true);
2519            }
2520            applyUpdateLockStateLocked(r);
2521        }
2522    }
2523
2524    final void clearFocusedActivity(ActivityRecord r) {
2525        if (mFocusedActivity == r) {
2526            mFocusedActivity = null;
2527        }
2528    }
2529
2530    @Override
2531    public void setFocusedStack(int stackId) {
2532        if (DEBUG_FOCUS) Slog.d(TAG, "setFocusedStack: stackId=" + stackId);
2533        synchronized (ActivityManagerService.this) {
2534            ActivityStack stack = mStackSupervisor.getStack(stackId);
2535            if (stack != null) {
2536                ActivityRecord r = stack.topRunningActivityLocked(null);
2537                if (r != null) {
2538                    setFocusedActivityLocked(r);
2539                }
2540            }
2541        }
2542    }
2543
2544    @Override
2545    public void notifyActivityDrawn(IBinder token) {
2546        if (DEBUG_VISBILITY) Slog.d(TAG, "notifyActivityDrawn: token=" + token);
2547        synchronized (this) {
2548            ActivityRecord r= mStackSupervisor.isInAnyStackLocked(token);
2549            if (r != null) {
2550                r.task.stack.notifyActivityDrawnLocked(r);
2551            }
2552        }
2553    }
2554
2555    final void applyUpdateLockStateLocked(ActivityRecord r) {
2556        // Modifications to the UpdateLock state are done on our handler, outside
2557        // the activity manager's locks.  The new state is determined based on the
2558        // state *now* of the relevant activity record.  The object is passed to
2559        // the handler solely for logging detail, not to be consulted/modified.
2560        final boolean nextState = r != null && r.immersive;
2561        mHandler.sendMessage(
2562                mHandler.obtainMessage(IMMERSIVE_MODE_LOCK_MSG, (nextState) ? 1 : 0, 0, r));
2563    }
2564
2565    final void showAskCompatModeDialogLocked(ActivityRecord r) {
2566        Message msg = Message.obtain();
2567        msg.what = SHOW_COMPAT_MODE_DIALOG_MSG;
2568        msg.obj = r.task.askedCompatMode ? null : r;
2569        mHandler.sendMessage(msg);
2570    }
2571
2572    private final int updateLruProcessInternalLocked(ProcessRecord app, long now, int index,
2573            String what, Object obj, ProcessRecord srcApp) {
2574        app.lastActivityTime = now;
2575
2576        if (app.activities.size() > 0) {
2577            // Don't want to touch dependent processes that are hosting activities.
2578            return index;
2579        }
2580
2581        int lrui = mLruProcesses.lastIndexOf(app);
2582        if (lrui < 0) {
2583            Slog.wtf(TAG, "Adding dependent process " + app + " not on LRU list: "
2584                    + what + " " + obj + " from " + srcApp);
2585            return index;
2586        }
2587
2588        if (lrui >= index) {
2589            // Don't want to cause this to move dependent processes *back* in the
2590            // list as if they were less frequently used.
2591            return index;
2592        }
2593
2594        if (lrui >= mLruProcessActivityStart) {
2595            // Don't want to touch dependent processes that are hosting activities.
2596            return index;
2597        }
2598
2599        mLruProcesses.remove(lrui);
2600        if (index > 0) {
2601            index--;
2602        }
2603        if (DEBUG_LRU) Slog.d(TAG, "Moving dep from " + lrui + " to " + index
2604                + " in LRU list: " + app);
2605        mLruProcesses.add(index, app);
2606        return index;
2607    }
2608
2609    final void removeLruProcessLocked(ProcessRecord app) {
2610        int lrui = mLruProcesses.lastIndexOf(app);
2611        if (lrui >= 0) {
2612            if (lrui <= mLruProcessActivityStart) {
2613                mLruProcessActivityStart--;
2614            }
2615            if (lrui <= mLruProcessServiceStart) {
2616                mLruProcessServiceStart--;
2617            }
2618            mLruProcesses.remove(lrui);
2619        }
2620    }
2621
2622    final void updateLruProcessLocked(ProcessRecord app, boolean activityChange,
2623            ProcessRecord client) {
2624        final boolean hasActivity = app.activities.size() > 0 || app.hasClientActivities
2625                || app.treatLikeActivity;
2626        final boolean hasService = false; // not impl yet. app.services.size() > 0;
2627        if (!activityChange && hasActivity) {
2628            // The process has activities, so we are only allowing activity-based adjustments
2629            // to move it.  It should be kept in the front of the list with other
2630            // processes that have activities, and we don't want those to change their
2631            // order except due to activity operations.
2632            return;
2633        }
2634
2635        mLruSeq++;
2636        final long now = SystemClock.uptimeMillis();
2637        app.lastActivityTime = now;
2638
2639        // First a quick reject: if the app is already at the position we will
2640        // put it, then there is nothing to do.
2641        if (hasActivity) {
2642            final int N = mLruProcesses.size();
2643            if (N > 0 && mLruProcesses.get(N-1) == app) {
2644                if (DEBUG_LRU) Slog.d(TAG, "Not moving, already top activity: " + app);
2645                return;
2646            }
2647        } else {
2648            if (mLruProcessServiceStart > 0
2649                    && mLruProcesses.get(mLruProcessServiceStart-1) == app) {
2650                if (DEBUG_LRU) Slog.d(TAG, "Not moving, already top other: " + app);
2651                return;
2652            }
2653        }
2654
2655        int lrui = mLruProcesses.lastIndexOf(app);
2656
2657        if (app.persistent && lrui >= 0) {
2658            // We don't care about the position of persistent processes, as long as
2659            // they are in the list.
2660            if (DEBUG_LRU) Slog.d(TAG, "Not moving, persistent: " + app);
2661            return;
2662        }
2663
2664        /* In progress: compute new position first, so we can avoid doing work
2665           if the process is not actually going to move.  Not yet working.
2666        int addIndex;
2667        int nextIndex;
2668        boolean inActivity = false, inService = false;
2669        if (hasActivity) {
2670            // Process has activities, put it at the very tipsy-top.
2671            addIndex = mLruProcesses.size();
2672            nextIndex = mLruProcessServiceStart;
2673            inActivity = true;
2674        } else if (hasService) {
2675            // Process has services, put it at the top of the service list.
2676            addIndex = mLruProcessActivityStart;
2677            nextIndex = mLruProcessServiceStart;
2678            inActivity = true;
2679            inService = true;
2680        } else  {
2681            // Process not otherwise of interest, it goes to the top of the non-service area.
2682            addIndex = mLruProcessServiceStart;
2683            if (client != null) {
2684                int clientIndex = mLruProcesses.lastIndexOf(client);
2685                if (clientIndex < 0) Slog.d(TAG, "Unknown client " + client + " when updating "
2686                        + app);
2687                if (clientIndex >= 0 && addIndex > clientIndex) {
2688                    addIndex = clientIndex;
2689                }
2690            }
2691            nextIndex = addIndex > 0 ? addIndex-1 : addIndex;
2692        }
2693
2694        Slog.d(TAG, "Update LRU at " + lrui + " to " + addIndex + " (act="
2695                + mLruProcessActivityStart + "): " + app);
2696        */
2697
2698        if (lrui >= 0) {
2699            if (lrui < mLruProcessActivityStart) {
2700                mLruProcessActivityStart--;
2701            }
2702            if (lrui < mLruProcessServiceStart) {
2703                mLruProcessServiceStart--;
2704            }
2705            /*
2706            if (addIndex > lrui) {
2707                addIndex--;
2708            }
2709            if (nextIndex > lrui) {
2710                nextIndex--;
2711            }
2712            */
2713            mLruProcesses.remove(lrui);
2714        }
2715
2716        /*
2717        mLruProcesses.add(addIndex, app);
2718        if (inActivity) {
2719            mLruProcessActivityStart++;
2720        }
2721        if (inService) {
2722            mLruProcessActivityStart++;
2723        }
2724        */
2725
2726        int nextIndex;
2727        if (hasActivity) {
2728            final int N = mLruProcesses.size();
2729            if (app.activities.size() == 0 && mLruProcessActivityStart < (N-1)) {
2730                // Process doesn't have activities, but has clients with
2731                // activities...  move it up, but one below the top (the top
2732                // should always have a real activity).
2733                if (DEBUG_LRU) Slog.d(TAG, "Adding to second-top of LRU activity list: " + app);
2734                mLruProcesses.add(N-1, app);
2735                // To keep it from spamming the LRU list (by making a bunch of clients),
2736                // we will push down any other entries owned by the app.
2737                final int uid = app.info.uid;
2738                for (int i=N-2; i>mLruProcessActivityStart; i--) {
2739                    ProcessRecord subProc = mLruProcesses.get(i);
2740                    if (subProc.info.uid == uid) {
2741                        // We want to push this one down the list.  If the process after
2742                        // it is for the same uid, however, don't do so, because we don't
2743                        // want them internally to be re-ordered.
2744                        if (mLruProcesses.get(i-1).info.uid != uid) {
2745                            if (DEBUG_LRU) Slog.d(TAG, "Pushing uid " + uid + " swapping at " + i
2746                                    + ": " + mLruProcesses.get(i) + " : " + mLruProcesses.get(i-1));
2747                            ProcessRecord tmp = mLruProcesses.get(i);
2748                            mLruProcesses.set(i, mLruProcesses.get(i-1));
2749                            mLruProcesses.set(i-1, tmp);
2750                            i--;
2751                        }
2752                    } else {
2753                        // A gap, we can stop here.
2754                        break;
2755                    }
2756                }
2757            } else {
2758                // Process has activities, put it at the very tipsy-top.
2759                if (DEBUG_LRU) Slog.d(TAG, "Adding to top of LRU activity list: " + app);
2760                mLruProcesses.add(app);
2761            }
2762            nextIndex = mLruProcessServiceStart;
2763        } else if (hasService) {
2764            // Process has services, put it at the top of the service list.
2765            if (DEBUG_LRU) Slog.d(TAG, "Adding to top of LRU service list: " + app);
2766            mLruProcesses.add(mLruProcessActivityStart, app);
2767            nextIndex = mLruProcessServiceStart;
2768            mLruProcessActivityStart++;
2769        } else  {
2770            // Process not otherwise of interest, it goes to the top of the non-service area.
2771            int index = mLruProcessServiceStart;
2772            if (client != null) {
2773                // If there is a client, don't allow the process to be moved up higher
2774                // in the list than that client.
2775                int clientIndex = mLruProcesses.lastIndexOf(client);
2776                if (DEBUG_LRU && clientIndex < 0) Slog.d(TAG, "Unknown client " + client
2777                        + " when updating " + app);
2778                if (clientIndex <= lrui) {
2779                    // Don't allow the client index restriction to push it down farther in the
2780                    // list than it already is.
2781                    clientIndex = lrui;
2782                }
2783                if (clientIndex >= 0 && index > clientIndex) {
2784                    index = clientIndex;
2785                }
2786            }
2787            if (DEBUG_LRU) Slog.d(TAG, "Adding at " + index + " of LRU list: " + app);
2788            mLruProcesses.add(index, app);
2789            nextIndex = index-1;
2790            mLruProcessActivityStart++;
2791            mLruProcessServiceStart++;
2792        }
2793
2794        // If the app is currently using a content provider or service,
2795        // bump those processes as well.
2796        for (int j=app.connections.size()-1; j>=0; j--) {
2797            ConnectionRecord cr = app.connections.valueAt(j);
2798            if (cr.binding != null && !cr.serviceDead && cr.binding.service != null
2799                    && cr.binding.service.app != null
2800                    && cr.binding.service.app.lruSeq != mLruSeq
2801                    && !cr.binding.service.app.persistent) {
2802                nextIndex = updateLruProcessInternalLocked(cr.binding.service.app, now, nextIndex,
2803                        "service connection", cr, app);
2804            }
2805        }
2806        for (int j=app.conProviders.size()-1; j>=0; j--) {
2807            ContentProviderRecord cpr = app.conProviders.get(j).provider;
2808            if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq && !cpr.proc.persistent) {
2809                nextIndex = updateLruProcessInternalLocked(cpr.proc, now, nextIndex,
2810                        "provider reference", cpr, app);
2811            }
2812        }
2813    }
2814
2815    final ProcessRecord getProcessRecordLocked(String processName, int uid, boolean keepIfLarge) {
2816        if (uid == Process.SYSTEM_UID) {
2817            // The system gets to run in any process.  If there are multiple
2818            // processes with the same uid, just pick the first (this
2819            // should never happen).
2820            SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(processName);
2821            if (procs == null) return null;
2822            final int N = procs.size();
2823            for (int i = 0; i < N; i++) {
2824                if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i);
2825            }
2826        }
2827        ProcessRecord proc = mProcessNames.get(processName, uid);
2828        if (false && proc != null && !keepIfLarge
2829                && proc.setProcState >= ActivityManager.PROCESS_STATE_CACHED_EMPTY
2830                && proc.lastCachedPss >= 4000) {
2831            // Turn this condition on to cause killing to happen regularly, for testing.
2832            if (proc.baseProcessTracker != null) {
2833                proc.baseProcessTracker.reportCachedKill(proc.pkgList, proc.lastCachedPss);
2834            }
2835            proc.kill(Long.toString(proc.lastCachedPss) + "k from cached", true);
2836        } else if (proc != null && !keepIfLarge
2837                && mLastMemoryLevel > ProcessStats.ADJ_MEM_FACTOR_NORMAL
2838                && proc.setProcState >= ActivityManager.PROCESS_STATE_CACHED_EMPTY) {
2839            if (DEBUG_PSS) Slog.d(TAG, "May not keep " + proc + ": pss=" + proc.lastCachedPss);
2840            if (proc.lastCachedPss >= mProcessList.getCachedRestoreThresholdKb()) {
2841                if (proc.baseProcessTracker != null) {
2842                    proc.baseProcessTracker.reportCachedKill(proc.pkgList, proc.lastCachedPss);
2843                }
2844                proc.kill(Long.toString(proc.lastCachedPss) + "k from cached", true);
2845            }
2846        }
2847        return proc;
2848    }
2849
2850    void ensurePackageDexOpt(String packageName) {
2851        IPackageManager pm = AppGlobals.getPackageManager();
2852        try {
2853            if (pm.performDexOptIfNeeded(packageName, null /* instruction set */)) {
2854                mDidDexOpt = true;
2855            }
2856        } catch (RemoteException e) {
2857        }
2858    }
2859
2860    boolean isNextTransitionForward() {
2861        int transit = mWindowManager.getPendingAppTransition();
2862        return transit == AppTransition.TRANSIT_ACTIVITY_OPEN
2863                || transit == AppTransition.TRANSIT_TASK_OPEN
2864                || transit == AppTransition.TRANSIT_TASK_TO_FRONT;
2865    }
2866
2867    int startIsolatedProcess(String entryPoint, String[] entryPointArgs,
2868            String processName, String abiOverride, int uid, Runnable crashHandler) {
2869        synchronized(this) {
2870            ApplicationInfo info = new ApplicationInfo();
2871            // In general the ApplicationInfo.uid isn't neccesarily equal to ProcessRecord.uid.
2872            // For isolated processes, the former contains the parent's uid and the latter the
2873            // actual uid of the isolated process.
2874            // In the special case introduced by this method (which is, starting an isolated
2875            // process directly from the SystemServer without an actual parent app process) the
2876            // closest thing to a parent's uid is SYSTEM_UID.
2877            // The only important thing here is to keep AI.uid != PR.uid, in order to trigger
2878            // the |isolated| logic in the ProcessRecord constructor.
2879            info.uid = Process.SYSTEM_UID;
2880            info.processName = processName;
2881            info.className = entryPoint;
2882            info.packageName = "android";
2883            ProcessRecord proc = startProcessLocked(processName, info /* info */,
2884                    false /* knownToBeDead */, 0 /* intentFlags */, ""  /* hostingType */,
2885                    null /* hostingName */, true /* allowWhileBooting */, true /* isolated */,
2886                    uid, true /* keepIfLarge */, abiOverride, entryPoint, entryPointArgs,
2887                    crashHandler);
2888            return proc != null ? proc.pid : 0;
2889        }
2890    }
2891
2892    final ProcessRecord startProcessLocked(String processName,
2893            ApplicationInfo info, boolean knownToBeDead, int intentFlags,
2894            String hostingType, ComponentName hostingName, boolean allowWhileBooting,
2895            boolean isolated, boolean keepIfLarge) {
2896        return startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingType,
2897                hostingName, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,
2898                null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,
2899                null /* crashHandler */);
2900    }
2901
2902    final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
2903            boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,
2904            boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,
2905            String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
2906        ProcessRecord app;
2907        if (!isolated) {
2908            app = getProcessRecordLocked(processName, info.uid, keepIfLarge);
2909        } else {
2910            // If this is an isolated process, it can't re-use an existing process.
2911            app = null;
2912        }
2913        // We don't have to do anything more if:
2914        // (1) There is an existing application record; and
2915        // (2) The caller doesn't think it is dead, OR there is no thread
2916        //     object attached to it so we know it couldn't have crashed; and
2917        // (3) There is a pid assigned to it, so it is either starting or
2918        //     already running.
2919        if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
2920                + " app=" + app + " knownToBeDead=" + knownToBeDead
2921                + " thread=" + (app != null ? app.thread : null)
2922                + " pid=" + (app != null ? app.pid : -1));
2923        if (app != null && app.pid > 0) {
2924            if (!knownToBeDead || app.thread == null) {
2925                // We already have the app running, or are waiting for it to
2926                // come up (we have a pid but not yet its thread), so keep it.
2927                if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app);
2928                // If this is a new package in the process, add the package to the list
2929                app.addPackage(info.packageName, info.versionCode, mProcessStats);
2930                return app;
2931            }
2932
2933            // An application record is attached to a previous process,
2934            // clean it up now.
2935            if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, "App died: " + app);
2936            Process.killProcessGroup(app.info.uid, app.pid);
2937            handleAppDiedLocked(app, true, true);
2938        }
2939
2940        String hostingNameStr = hostingName != null
2941                ? hostingName.flattenToShortString() : null;
2942
2943        if (!isolated) {
2944            if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
2945                // If we are in the background, then check to see if this process
2946                // is bad.  If so, we will just silently fail.
2947                if (mBadProcesses.get(info.processName, info.uid) != null) {
2948                    if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid
2949                            + "/" + info.processName);
2950                    return null;
2951                }
2952            } else {
2953                // When the user is explicitly starting a process, then clear its
2954                // crash count so that we won't make it bad until they see at
2955                // least one crash dialog again, and make the process good again
2956                // if it had been bad.
2957                if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid
2958                        + "/" + info.processName);
2959                mProcessCrashTimes.remove(info.processName, info.uid);
2960                if (mBadProcesses.get(info.processName, info.uid) != null) {
2961                    EventLog.writeEvent(EventLogTags.AM_PROC_GOOD,
2962                            UserHandle.getUserId(info.uid), info.uid,
2963                            info.processName);
2964                    mBadProcesses.remove(info.processName, info.uid);
2965                    if (app != null) {
2966                        app.bad = false;
2967                    }
2968                }
2969            }
2970        }
2971
2972        if (app == null) {
2973            app = newProcessRecordLocked(info, processName, isolated, isolatedUid);
2974            app.crashHandler = crashHandler;
2975            if (app == null) {
2976                Slog.w(TAG, "Failed making new process record for "
2977                        + processName + "/" + info.uid + " isolated=" + isolated);
2978                return null;
2979            }
2980            mProcessNames.put(processName, app.uid, app);
2981            if (isolated) {
2982                mIsolatedProcesses.put(app.uid, app);
2983            }
2984        } else {
2985            // If this is a new package in the process, add the package to the list
2986            app.addPackage(info.packageName, info.versionCode, mProcessStats);
2987        }
2988
2989        // If the system is not ready yet, then hold off on starting this
2990        // process until it is.
2991        if (!mProcessesReady
2992                && !isAllowedWhileBooting(info)
2993                && !allowWhileBooting) {
2994            if (!mProcessesOnHold.contains(app)) {
2995                mProcessesOnHold.add(app);
2996            }
2997            if (DEBUG_PROCESSES) Slog.v(TAG, "System not ready, putting on hold: " + app);
2998            return app;
2999        }
3000
3001        startProcessLocked(
3002                app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs);
3003        return (app.pid != 0) ? app : null;
3004    }
3005
3006    boolean isAllowedWhileBooting(ApplicationInfo ai) {
3007        return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
3008    }
3009
3010    private final void startProcessLocked(ProcessRecord app,
3011            String hostingType, String hostingNameStr) {
3012        startProcessLocked(app, hostingType, hostingNameStr, null /* abiOverride */,
3013                null /* entryPoint */, null /* entryPointArgs */);
3014    }
3015
3016    private final void startProcessLocked(ProcessRecord app, String hostingType,
3017            String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
3018        if (app.pid > 0 && app.pid != MY_PID) {
3019            synchronized (mPidsSelfLocked) {
3020                mPidsSelfLocked.remove(app.pid);
3021                mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3022            }
3023            app.setPid(0);
3024        }
3025
3026        if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
3027                "startProcessLocked removing on hold: " + app);
3028        mProcessesOnHold.remove(app);
3029
3030        updateCpuStats();
3031
3032        try {
3033            int uid = app.uid;
3034
3035            int[] gids = null;
3036            int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;
3037            if (!app.isolated) {
3038                int[] permGids = null;
3039                try {
3040                    final PackageManager pm = mContext.getPackageManager();
3041                    permGids = pm.getPackageGids(app.info.packageName);
3042
3043                    if (Environment.isExternalStorageEmulated()) {
3044                        if (pm.checkPermission(
3045                                android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE,
3046                                app.info.packageName) == PERMISSION_GRANTED) {
3047                            mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL;
3048                        } else {
3049                            mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER;
3050                        }
3051                    }
3052                } catch (PackageManager.NameNotFoundException e) {
3053                    Slog.w(TAG, "Unable to retrieve gids", e);
3054                }
3055
3056                /*
3057                 * Add shared application and profile GIDs so applications can share some
3058                 * resources like shared libraries and access user-wide resources
3059                 */
3060                if (permGids == null) {
3061                    gids = new int[2];
3062                } else {
3063                    gids = new int[permGids.length + 2];
3064                    System.arraycopy(permGids, 0, gids, 2, permGids.length);
3065                }
3066                gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
3067                gids[1] = UserHandle.getUserGid(UserHandle.getUserId(uid));
3068            }
3069            if (mFactoryTest != FactoryTest.FACTORY_TEST_OFF) {
3070                if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
3071                        && mTopComponent != null
3072                        && app.processName.equals(mTopComponent.getPackageName())) {
3073                    uid = 0;
3074                }
3075                if (mFactoryTest == FactoryTest.FACTORY_TEST_HIGH_LEVEL
3076                        && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
3077                    uid = 0;
3078                }
3079            }
3080            int debugFlags = 0;
3081            if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
3082                debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
3083                // Also turn on CheckJNI for debuggable apps. It's quite
3084                // awkward to turn on otherwise.
3085                debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
3086            }
3087            // Run the app in safe mode if its manifest requests so or the
3088            // system is booted in safe mode.
3089            if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
3090                mSafeMode == true) {
3091                debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
3092            }
3093            if ("1".equals(SystemProperties.get("debug.checkjni"))) {
3094                debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
3095            }
3096            if ("1".equals(SystemProperties.get("debug.jni.logging"))) {
3097                debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING;
3098            }
3099            if ("1".equals(SystemProperties.get("debug.assert"))) {
3100                debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
3101            }
3102
3103            String requiredAbi = (abiOverride != null) ? abiOverride : app.info.primaryCpuAbi;
3104            if (requiredAbi == null) {
3105                requiredAbi = Build.SUPPORTED_ABIS[0];
3106            }
3107
3108            // Start the process.  It will either succeed and return a result containing
3109            // the PID of the new process, or else throw a RuntimeException.
3110            boolean isActivityProcess = (entryPoint == null);
3111            if (entryPoint == null) entryPoint = "android.app.ActivityThread";
3112            Process.ProcessStartResult startResult = Process.start(entryPoint,
3113                    app.processName, uid, uid, gids, debugFlags, mountExternal,
3114                    app.info.targetSdkVersion, app.info.seinfo, requiredAbi, entryPointArgs);
3115
3116            if (app.isolated) {
3117                mBatteryStatsService.addIsolatedUid(app.uid, app.info.uid);
3118            }
3119            mBatteryStatsService.noteProcessStart(app.processName, app.info.uid);
3120
3121            EventLog.writeEvent(EventLogTags.AM_PROC_START,
3122                    UserHandle.getUserId(uid), startResult.pid, uid,
3123                    app.processName, hostingType,
3124                    hostingNameStr != null ? hostingNameStr : "");
3125
3126            if (app.persistent) {
3127                Watchdog.getInstance().processStarted(app.processName, startResult.pid);
3128            }
3129
3130            StringBuilder buf = mStringBuilder;
3131            buf.setLength(0);
3132            buf.append("Start proc ");
3133            buf.append(app.processName);
3134            if (!isActivityProcess) {
3135                buf.append(" [");
3136                buf.append(entryPoint);
3137                buf.append("]");
3138            }
3139            buf.append(" for ");
3140            buf.append(hostingType);
3141            if (hostingNameStr != null) {
3142                buf.append(" ");
3143                buf.append(hostingNameStr);
3144            }
3145            buf.append(": pid=");
3146            buf.append(startResult.pid);
3147            buf.append(" uid=");
3148            buf.append(uid);
3149            buf.append(" gids={");
3150            if (gids != null) {
3151                for (int gi=0; gi<gids.length; gi++) {
3152                    if (gi != 0) buf.append(", ");
3153                    buf.append(gids[gi]);
3154
3155                }
3156            }
3157            buf.append("}");
3158            if (requiredAbi != null) {
3159                buf.append(" abi=");
3160                buf.append(requiredAbi);
3161            }
3162            Slog.i(TAG, buf.toString());
3163            app.setPid(startResult.pid);
3164            app.usingWrapper = startResult.usingWrapper;
3165            app.removed = false;
3166            app.killedByAm = false;
3167            synchronized (mPidsSelfLocked) {
3168                this.mPidsSelfLocked.put(startResult.pid, app);
3169                if (isActivityProcess) {
3170                    Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
3171                    msg.obj = app;
3172                    mHandler.sendMessageDelayed(msg, startResult.usingWrapper
3173                            ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT);
3174                }
3175            }
3176        } catch (RuntimeException e) {
3177            // XXX do better error recovery.
3178            app.setPid(0);
3179            mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
3180            if (app.isolated) {
3181                mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
3182            }
3183            Slog.e(TAG, "Failure starting process " + app.processName, e);
3184        }
3185    }
3186
3187    void updateUsageStats(ActivityRecord component, boolean resumed) {
3188        if (DEBUG_SWITCH) Slog.d(TAG, "updateUsageStats: comp=" + component + "res=" + resumed);
3189        final BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
3190        if (resumed) {
3191            if (mUsageStatsService != null) {
3192                mUsageStatsService.reportEvent(component.realActivity, component.userId,
3193                        System.currentTimeMillis(),
3194                        UsageEvents.Event.MOVE_TO_FOREGROUND);
3195            }
3196            synchronized (stats) {
3197                stats.noteActivityResumedLocked(component.app.uid);
3198            }
3199        } else {
3200            if (mUsageStatsService != null) {
3201                mUsageStatsService.reportEvent(component.realActivity, component.userId,
3202                        System.currentTimeMillis(),
3203                        UsageEvents.Event.MOVE_TO_BACKGROUND);
3204            }
3205            synchronized (stats) {
3206                stats.noteActivityPausedLocked(component.app.uid);
3207            }
3208        }
3209    }
3210
3211    Intent getHomeIntent() {
3212        Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
3213        intent.setComponent(mTopComponent);
3214        if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
3215            intent.addCategory(Intent.CATEGORY_HOME);
3216        }
3217        return intent;
3218    }
3219
3220    boolean startHomeActivityLocked(int userId) {
3221        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
3222                && mTopAction == null) {
3223            // We are running in factory test mode, but unable to find
3224            // the factory test app, so just sit around displaying the
3225            // error message and don't try to start anything.
3226            return false;
3227        }
3228        Intent intent = getHomeIntent();
3229        ActivityInfo aInfo =
3230            resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
3231        if (aInfo != null) {
3232            intent.setComponent(new ComponentName(
3233                    aInfo.applicationInfo.packageName, aInfo.name));
3234            // Don't do this if the home app is currently being
3235            // instrumented.
3236            aInfo = new ActivityInfo(aInfo);
3237            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
3238            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
3239                    aInfo.applicationInfo.uid, true);
3240            if (app == null || app.instrumentationClass == null) {
3241                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
3242                mStackSupervisor.startHomeActivity(intent, aInfo);
3243            }
3244        }
3245
3246        return true;
3247    }
3248
3249    private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) {
3250        ActivityInfo ai = null;
3251        ComponentName comp = intent.getComponent();
3252        try {
3253            if (comp != null) {
3254                ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId);
3255            } else {
3256                ResolveInfo info = AppGlobals.getPackageManager().resolveIntent(
3257                        intent,
3258                        intent.resolveTypeIfNeeded(mContext.getContentResolver()),
3259                            flags, userId);
3260
3261                if (info != null) {
3262                    ai = info.activityInfo;
3263                }
3264            }
3265        } catch (RemoteException e) {
3266            // ignore
3267        }
3268
3269        return ai;
3270    }
3271
3272    /**
3273     * Starts the "new version setup screen" if appropriate.
3274     */
3275    void startSetupActivityLocked() {
3276        // Only do this once per boot.
3277        if (mCheckedForSetup) {
3278            return;
3279        }
3280
3281        // We will show this screen if the current one is a different
3282        // version than the last one shown, and we are not running in
3283        // low-level factory test mode.
3284        final ContentResolver resolver = mContext.getContentResolver();
3285        if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL &&
3286                Settings.Global.getInt(resolver,
3287                        Settings.Global.DEVICE_PROVISIONED, 0) != 0) {
3288            mCheckedForSetup = true;
3289
3290            // See if we should be showing the platform update setup UI.
3291            Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
3292            List<ResolveInfo> ris = mContext.getPackageManager()
3293                    .queryIntentActivities(intent, PackageManager.GET_META_DATA);
3294
3295            // We don't allow third party apps to replace this.
3296            ResolveInfo ri = null;
3297            for (int i=0; ris != null && i<ris.size(); i++) {
3298                if ((ris.get(i).activityInfo.applicationInfo.flags
3299                        & ApplicationInfo.FLAG_SYSTEM) != 0) {
3300                    ri = ris.get(i);
3301                    break;
3302                }
3303            }
3304
3305            if (ri != null) {
3306                String vers = ri.activityInfo.metaData != null
3307                        ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
3308                        : null;
3309                if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
3310                    vers = ri.activityInfo.applicationInfo.metaData.getString(
3311                            Intent.METADATA_SETUP_VERSION);
3312                }
3313                String lastVers = Settings.Secure.getString(
3314                        resolver, Settings.Secure.LAST_SETUP_SHOWN);
3315                if (vers != null && !vers.equals(lastVers)) {
3316                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3317                    intent.setComponent(new ComponentName(
3318                            ri.activityInfo.packageName, ri.activityInfo.name));
3319                    mStackSupervisor.startActivityLocked(null, intent, null, ri.activityInfo,
3320                            null, null, null, null, 0, 0, 0, null, 0, null, false, null, null,
3321                            null);
3322                }
3323            }
3324        }
3325    }
3326
3327    CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
3328        return mCompatModePackages.compatibilityInfoForPackageLocked(ai);
3329    }
3330
3331    void enforceNotIsolatedCaller(String caller) {
3332        if (UserHandle.isIsolated(Binder.getCallingUid())) {
3333            throw new SecurityException("Isolated process not allowed to call " + caller);
3334        }
3335    }
3336
3337    @Override
3338    public int getFrontActivityScreenCompatMode() {
3339        enforceNotIsolatedCaller("getFrontActivityScreenCompatMode");
3340        synchronized (this) {
3341            return mCompatModePackages.getFrontActivityScreenCompatModeLocked();
3342        }
3343    }
3344
3345    @Override
3346    public void setFrontActivityScreenCompatMode(int mode) {
3347        enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
3348                "setFrontActivityScreenCompatMode");
3349        synchronized (this) {
3350            mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode);
3351        }
3352    }
3353
3354    @Override
3355    public int getPackageScreenCompatMode(String packageName) {
3356        enforceNotIsolatedCaller("getPackageScreenCompatMode");
3357        synchronized (this) {
3358            return mCompatModePackages.getPackageScreenCompatModeLocked(packageName);
3359        }
3360    }
3361
3362    @Override
3363    public void setPackageScreenCompatMode(String packageName, int mode) {
3364        enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
3365                "setPackageScreenCompatMode");
3366        synchronized (this) {
3367            mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode);
3368        }
3369    }
3370
3371    @Override
3372    public boolean getPackageAskScreenCompat(String packageName) {
3373        enforceNotIsolatedCaller("getPackageAskScreenCompat");
3374        synchronized (this) {
3375            return mCompatModePackages.getPackageAskCompatModeLocked(packageName);
3376        }
3377    }
3378
3379    @Override
3380    public void setPackageAskScreenCompat(String packageName, boolean ask) {
3381        enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
3382                "setPackageAskScreenCompat");
3383        synchronized (this) {
3384            mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask);
3385        }
3386    }
3387
3388    private void dispatchProcessesChanged() {
3389        int N;
3390        synchronized (this) {
3391            N = mPendingProcessChanges.size();
3392            if (mActiveProcessChanges.length < N) {
3393                mActiveProcessChanges = new ProcessChangeItem[N];
3394            }
3395            mPendingProcessChanges.toArray(mActiveProcessChanges);
3396            mAvailProcessChanges.addAll(mPendingProcessChanges);
3397            mPendingProcessChanges.clear();
3398            if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes");
3399        }
3400
3401        int i = mProcessObservers.beginBroadcast();
3402        while (i > 0) {
3403            i--;
3404            final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
3405            if (observer != null) {
3406                try {
3407                    for (int j=0; j<N; j++) {
3408                        ProcessChangeItem item = mActiveProcessChanges[j];
3409                        if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) {
3410                            if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid="
3411                                    + item.pid + " uid=" + item.uid + ": "
3412                                    + item.foregroundActivities);
3413                            observer.onForegroundActivitiesChanged(item.pid, item.uid,
3414                                    item.foregroundActivities);
3415                        }
3416                        if ((item.changes&ProcessChangeItem.CHANGE_PROCESS_STATE) != 0) {
3417                            if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "PROCSTATE CHANGED pid="
3418                                    + item.pid + " uid=" + item.uid + ": " + item.processState);
3419                            observer.onProcessStateChanged(item.pid, item.uid, item.processState);
3420                        }
3421                    }
3422                } catch (RemoteException e) {
3423                }
3424            }
3425        }
3426        mProcessObservers.finishBroadcast();
3427    }
3428
3429    private void dispatchProcessDied(int pid, int uid) {
3430        int i = mProcessObservers.beginBroadcast();
3431        while (i > 0) {
3432            i--;
3433            final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
3434            if (observer != null) {
3435                try {
3436                    observer.onProcessDied(pid, uid);
3437                } catch (RemoteException e) {
3438                }
3439            }
3440        }
3441        mProcessObservers.finishBroadcast();
3442    }
3443
3444    @Override
3445    public final int startActivity(IApplicationThread caller, String callingPackage,
3446            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
3447            int startFlags, ProfilerInfo profilerInfo, Bundle options) {
3448        return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
3449            resultWho, requestCode, startFlags, profilerInfo, options,
3450            UserHandle.getCallingUserId());
3451    }
3452
3453    @Override
3454    public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
3455            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
3456            int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) {
3457        enforceNotIsolatedCaller("startActivity");
3458        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
3459                false, ALLOW_FULL_ONLY, "startActivity", null);
3460        // TODO: Switch to user app stacks here.
3461        return mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent,
3462                resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
3463                profilerInfo, null, null, options, userId, null, null);
3464    }
3465
3466    @Override
3467    public final int startActivityAsCaller(IApplicationThread caller, String callingPackage,
3468            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
3469            int startFlags, ProfilerInfo profilerInfo, Bundle options) {
3470
3471        // This is very dangerous -- it allows you to perform a start activity (including
3472        // permission grants) as any app that may launch one of your own activities.  So
3473        // we will only allow this to be done from activities that are part of the core framework,
3474        // and then only when they are running as the system.
3475        final ActivityRecord sourceRecord;
3476        final int targetUid;
3477        final String targetPackage;
3478        synchronized (this) {
3479            if (resultTo == null) {
3480                throw new SecurityException("Must be called from an activity");
3481            }
3482            sourceRecord = mStackSupervisor.isInAnyStackLocked(resultTo);
3483            if (sourceRecord == null) {
3484                throw new SecurityException("Called with bad activity token: " + resultTo);
3485            }
3486            if (!sourceRecord.info.packageName.equals("android")) {
3487                throw new SecurityException(
3488                        "Must be called from an activity that is declared in the android package");
3489            }
3490            if (sourceRecord.app == null) {
3491                throw new SecurityException("Called without a process attached to activity");
3492            }
3493            if (UserHandle.getAppId(sourceRecord.app.uid) != Process.SYSTEM_UID) {
3494                // This is still okay, as long as this activity is running under the
3495                // uid of the original calling activity.
3496                if (sourceRecord.app.uid != sourceRecord.launchedFromUid) {
3497                    throw new SecurityException(
3498                            "Calling activity in uid " + sourceRecord.app.uid
3499                                    + " must be system uid or original calling uid "
3500                                    + sourceRecord.launchedFromUid);
3501                }
3502            }
3503            targetUid = sourceRecord.launchedFromUid;
3504            targetPackage = sourceRecord.launchedFromPackage;
3505        }
3506
3507        // TODO: Switch to user app stacks here.
3508        try {
3509            int ret = mStackSupervisor.startActivityMayWait(null, targetUid, targetPackage, intent,
3510                    resolvedType, null, null, resultTo, resultWho, requestCode, startFlags, null,
3511                    null, null, options, UserHandle.getUserId(sourceRecord.app.uid), null, null);
3512            return ret;
3513        } catch (SecurityException e) {
3514            // XXX need to figure out how to propagate to original app.
3515            // A SecurityException here is generally actually a fault of the original
3516            // calling activity (such as a fairly granting permissions), so propagate it
3517            // back to them.
3518            /*
3519            StringBuilder msg = new StringBuilder();
3520            msg.append("While launching");
3521            msg.append(intent.toString());
3522            msg.append(": ");
3523            msg.append(e.getMessage());
3524            */
3525            throw e;
3526        }
3527    }
3528
3529    @Override
3530    public final WaitResult startActivityAndWait(IApplicationThread caller, String callingPackage,
3531            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
3532            int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) {
3533        enforceNotIsolatedCaller("startActivityAndWait");
3534        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
3535                false, ALLOW_FULL_ONLY, "startActivityAndWait", null);
3536        WaitResult res = new WaitResult();
3537        // TODO: Switch to user app stacks here.
3538        mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType,
3539                null, null, resultTo, resultWho, requestCode, startFlags, profilerInfo, res, null,
3540                options, userId, null, null);
3541        return res;
3542    }
3543
3544    @Override
3545    public final int startActivityWithConfig(IApplicationThread caller, String callingPackage,
3546            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
3547            int startFlags, Configuration config, Bundle options, int userId) {
3548        enforceNotIsolatedCaller("startActivityWithConfig");
3549        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
3550                false, ALLOW_FULL_ONLY, "startActivityWithConfig", null);
3551        // TODO: Switch to user app stacks here.
3552        int ret = mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent,
3553                resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
3554                null, null, config, options, userId, null, null);
3555        return ret;
3556    }
3557
3558    @Override
3559    public int startActivityIntentSender(IApplicationThread caller,
3560            IntentSender intent, Intent fillInIntent, String resolvedType,
3561            IBinder resultTo, String resultWho, int requestCode,
3562            int flagsMask, int flagsValues, Bundle options) {
3563        enforceNotIsolatedCaller("startActivityIntentSender");
3564        // Refuse possible leaked file descriptors
3565        if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
3566            throw new IllegalArgumentException("File descriptors passed in Intent");
3567        }
3568
3569        IIntentSender sender = intent.getTarget();
3570        if (!(sender instanceof PendingIntentRecord)) {
3571            throw new IllegalArgumentException("Bad PendingIntent object");
3572        }
3573
3574        PendingIntentRecord pir = (PendingIntentRecord)sender;
3575
3576        synchronized (this) {
3577            // If this is coming from the currently resumed activity, it is
3578            // effectively saying that app switches are allowed at this point.
3579            final ActivityStack stack = getFocusedStack();
3580            if (stack.mResumedActivity != null &&
3581                    stack.mResumedActivity.info.applicationInfo.uid == Binder.getCallingUid()) {
3582                mAppSwitchesAllowedTime = 0;
3583            }
3584        }
3585        int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null,
3586                resultTo, resultWho, requestCode, flagsMask, flagsValues, options, null);
3587        return ret;
3588    }
3589
3590    @Override
3591    public int startVoiceActivity(String callingPackage, int callingPid, int callingUid,
3592            Intent intent, String resolvedType, IVoiceInteractionSession session,
3593            IVoiceInteractor interactor, int startFlags, ProfilerInfo profilerInfo,
3594            Bundle options, int userId) {
3595        if (checkCallingPermission(Manifest.permission.BIND_VOICE_INTERACTION)
3596                != PackageManager.PERMISSION_GRANTED) {
3597            String msg = "Permission Denial: startVoiceActivity() from pid="
3598                    + Binder.getCallingPid()
3599                    + ", uid=" + Binder.getCallingUid()
3600                    + " requires " + android.Manifest.permission.BIND_VOICE_INTERACTION;
3601            Slog.w(TAG, msg);
3602            throw new SecurityException(msg);
3603        }
3604        if (session == null || interactor == null) {
3605            throw new NullPointerException("null session or interactor");
3606        }
3607        userId = handleIncomingUser(callingPid, callingUid, userId,
3608                false, ALLOW_FULL_ONLY, "startVoiceActivity", null);
3609        // TODO: Switch to user app stacks here.
3610        return mStackSupervisor.startActivityMayWait(null, callingUid, callingPackage, intent,
3611                resolvedType, session, interactor, null, null, 0, startFlags, profilerInfo, null,
3612                null, options, userId, null, null);
3613    }
3614
3615    @Override
3616    public boolean startNextMatchingActivity(IBinder callingActivity,
3617            Intent intent, Bundle options) {
3618        // Refuse possible leaked file descriptors
3619        if (intent != null && intent.hasFileDescriptors() == true) {
3620            throw new IllegalArgumentException("File descriptors passed in Intent");
3621        }
3622
3623        synchronized (this) {
3624            final ActivityRecord r = ActivityRecord.isInStackLocked(callingActivity);
3625            if (r == null) {
3626                ActivityOptions.abort(options);
3627                return false;
3628            }
3629            if (r.app == null || r.app.thread == null) {
3630                // The caller is not running...  d'oh!
3631                ActivityOptions.abort(options);
3632                return false;
3633            }
3634            intent = new Intent(intent);
3635            // The caller is not allowed to change the data.
3636            intent.setDataAndType(r.intent.getData(), r.intent.getType());
3637            // And we are resetting to find the next component...
3638            intent.setComponent(null);
3639
3640            final boolean debug = ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
3641
3642            ActivityInfo aInfo = null;
3643            try {
3644                List<ResolveInfo> resolves =
3645                    AppGlobals.getPackageManager().queryIntentActivities(
3646                            intent, r.resolvedType,
3647                            PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS,
3648                            UserHandle.getCallingUserId());
3649
3650                // Look for the original activity in the list...
3651                final int N = resolves != null ? resolves.size() : 0;
3652                for (int i=0; i<N; i++) {
3653                    ResolveInfo rInfo = resolves.get(i);
3654                    if (rInfo.activityInfo.packageName.equals(r.packageName)
3655                            && rInfo.activityInfo.name.equals(r.info.name)) {
3656                        // We found the current one...  the next matching is
3657                        // after it.
3658                        i++;
3659                        if (i<N) {
3660                            aInfo = resolves.get(i).activityInfo;
3661                        }
3662                        if (debug) {
3663                            Slog.v(TAG, "Next matching activity: found current " + r.packageName
3664                                    + "/" + r.info.name);
3665                            Slog.v(TAG, "Next matching activity: next is " + aInfo.packageName
3666                                    + "/" + aInfo.name);
3667                        }
3668                        break;
3669                    }
3670                }
3671            } catch (RemoteException e) {
3672            }
3673
3674            if (aInfo == null) {
3675                // Nobody who is next!
3676                ActivityOptions.abort(options);
3677                if (debug) Slog.d(TAG, "Next matching activity: nothing found");
3678                return false;
3679            }
3680
3681            intent.setComponent(new ComponentName(
3682                    aInfo.applicationInfo.packageName, aInfo.name));
3683            intent.setFlags(intent.getFlags()&~(
3684                    Intent.FLAG_ACTIVITY_FORWARD_RESULT|
3685                    Intent.FLAG_ACTIVITY_CLEAR_TOP|
3686                    Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
3687                    Intent.FLAG_ACTIVITY_NEW_TASK));
3688
3689            // Okay now we need to start the new activity, replacing the
3690            // currently running activity.  This is a little tricky because
3691            // we want to start the new one as if the current one is finished,
3692            // but not finish the current one first so that there is no flicker.
3693            // And thus...
3694            final boolean wasFinishing = r.finishing;
3695            r.finishing = true;
3696
3697            // Propagate reply information over to the new activity.
3698            final ActivityRecord resultTo = r.resultTo;
3699            final String resultWho = r.resultWho;
3700            final int requestCode = r.requestCode;
3701            r.resultTo = null;
3702            if (resultTo != null) {
3703                resultTo.removeResultsLocked(r, resultWho, requestCode);
3704            }
3705
3706            final long origId = Binder.clearCallingIdentity();
3707            int res = mStackSupervisor.startActivityLocked(r.app.thread, intent,
3708                    r.resolvedType, aInfo, null, null, resultTo != null ? resultTo.appToken : null,
3709                    resultWho, requestCode, -1, r.launchedFromUid, r.launchedFromPackage, 0,
3710                    options, false, null, null, null);
3711            Binder.restoreCallingIdentity(origId);
3712
3713            r.finishing = wasFinishing;
3714            if (res != ActivityManager.START_SUCCESS) {
3715                return false;
3716            }
3717            return true;
3718        }
3719    }
3720
3721    @Override
3722    public final int startActivityFromRecents(int taskId, Bundle options) {
3723        if (checkCallingPermission(START_TASKS_FROM_RECENTS) != PackageManager.PERMISSION_GRANTED) {
3724            String msg = "Permission Denial: startActivityFromRecents called without " +
3725                    START_TASKS_FROM_RECENTS;
3726            Slog.w(TAG, msg);
3727            throw new SecurityException(msg);
3728        }
3729        return startActivityFromRecentsInner(taskId, options);
3730    }
3731
3732    final int startActivityFromRecentsInner(int taskId, Bundle options) {
3733        final TaskRecord task;
3734        final int callingUid;
3735        final String callingPackage;
3736        final Intent intent;
3737        final int userId;
3738        synchronized (this) {
3739            task = recentTaskForIdLocked(taskId);
3740            if (task == null) {
3741                throw new IllegalArgumentException("Task " + taskId + " not found.");
3742            }
3743            callingUid = task.mCallingUid;
3744            callingPackage = task.mCallingPackage;
3745            intent = task.intent;
3746            intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
3747            userId = task.userId;
3748        }
3749        return startActivityInPackage(callingUid, callingPackage, intent, null, null, null, 0, 0,
3750                options, userId, null, task);
3751    }
3752
3753    final int startActivityInPackage(int uid, String callingPackage,
3754            Intent intent, String resolvedType, IBinder resultTo,
3755            String resultWho, int requestCode, int startFlags, Bundle options, int userId,
3756            IActivityContainer container, TaskRecord inTask) {
3757
3758        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
3759                false, ALLOW_FULL_ONLY, "startActivityInPackage", null);
3760
3761        // TODO: Switch to user app stacks here.
3762        int ret = mStackSupervisor.startActivityMayWait(null, uid, callingPackage, intent,
3763                resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
3764                null, null, null, options, userId, container, inTask);
3765        return ret;
3766    }
3767
3768    @Override
3769    public final int startActivities(IApplicationThread caller, String callingPackage,
3770            Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options,
3771            int userId) {
3772        enforceNotIsolatedCaller("startActivities");
3773        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
3774                false, ALLOW_FULL_ONLY, "startActivity", null);
3775        // TODO: Switch to user app stacks here.
3776        int ret = mStackSupervisor.startActivities(caller, -1, callingPackage, intents,
3777                resolvedTypes, resultTo, options, userId);
3778        return ret;
3779    }
3780
3781    final int startActivitiesInPackage(int uid, String callingPackage,
3782            Intent[] intents, String[] resolvedTypes, IBinder resultTo,
3783            Bundle options, int userId) {
3784
3785        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
3786                false, ALLOW_FULL_ONLY, "startActivityInPackage", null);
3787        // TODO: Switch to user app stacks here.
3788        int ret = mStackSupervisor.startActivities(null, uid, callingPackage, intents, resolvedTypes,
3789                resultTo, options, userId);
3790        return ret;
3791    }
3792
3793    //explicitly remove thd old information in mRecentTasks when removing existing user.
3794    private void removeRecentTasksForUserLocked(int userId) {
3795        if(userId <= 0) {
3796            Slog.i(TAG, "Can't remove recent task on user " + userId);
3797            return;
3798        }
3799
3800        for (int i = mRecentTasks.size() - 1; i >= 0; --i) {
3801            TaskRecord tr = mRecentTasks.get(i);
3802            if (tr.userId == userId) {
3803                if(DEBUG_TASKS) Slog.i(TAG, "remove RecentTask " + tr
3804                        + " when finishing user" + userId);
3805                mRecentTasks.remove(i);
3806                tr.removedFromRecents(mTaskPersister);
3807            }
3808        }
3809
3810        // Remove tasks from persistent storage.
3811        mTaskPersister.wakeup(null, true);
3812    }
3813
3814    /**
3815     * Update the recent tasks lists: make sure tasks should still be here (their
3816     * applications / activities still exist), update their availability, fixup ordering
3817     * of affiliations.
3818     */
3819    void cleanupRecentTasksLocked(int userId) {
3820        if (mRecentTasks == null) {
3821            // Happens when called from the packagemanager broadcast before boot.
3822            return;
3823        }
3824
3825        final HashMap<ComponentName, ActivityInfo> availActCache = new HashMap<>();
3826        final HashMap<String, ApplicationInfo> availAppCache = new HashMap<>();
3827        final IPackageManager pm = AppGlobals.getPackageManager();
3828        final ActivityInfo dummyAct = new ActivityInfo();
3829        final ApplicationInfo dummyApp = new ApplicationInfo();
3830
3831        int N = mRecentTasks.size();
3832
3833        int[] users = userId == UserHandle.USER_ALL
3834                ? getUsersLocked() : new int[] { userId };
3835        for (int user : users) {
3836            for (int i = 0; i < N; i++) {
3837                TaskRecord task = mRecentTasks.get(i);
3838                if (task.userId != user) {
3839                    // Only look at tasks for the user ID of interest.
3840                    continue;
3841                }
3842                if (task.autoRemoveRecents && task.getTopActivity() == null) {
3843                    // This situation is broken, and we should just get rid of it now.
3844                    mRecentTasks.remove(i);
3845                    task.removedFromRecents(mTaskPersister);
3846                    i--;
3847                    N--;
3848                    Slog.w(TAG, "Removing auto-remove without activity: " + task);
3849                    continue;
3850                }
3851                // Check whether this activity is currently available.
3852                if (task.realActivity != null) {
3853                    ActivityInfo ai = availActCache.get(task.realActivity);
3854                    if (ai == null) {
3855                        try {
3856                            ai = pm.getActivityInfo(task.realActivity,
3857                                    PackageManager.GET_UNINSTALLED_PACKAGES
3858                                    | PackageManager.GET_DISABLED_COMPONENTS, user);
3859                        } catch (RemoteException e) {
3860                            // Will never happen.
3861                            continue;
3862                        }
3863                        if (ai == null) {
3864                            ai = dummyAct;
3865                        }
3866                        availActCache.put(task.realActivity, ai);
3867                    }
3868                    if (ai == dummyAct) {
3869                        // This could be either because the activity no longer exists, or the
3870                        // app is temporarily gone.  For the former we want to remove the recents
3871                        // entry; for the latter we want to mark it as unavailable.
3872                        ApplicationInfo app = availAppCache.get(task.realActivity.getPackageName());
3873                        if (app == null) {
3874                            try {
3875                                app = pm.getApplicationInfo(task.realActivity.getPackageName(),
3876                                        PackageManager.GET_UNINSTALLED_PACKAGES
3877                                        | PackageManager.GET_DISABLED_COMPONENTS, user);
3878                            } catch (RemoteException e) {
3879                                // Will never happen.
3880                                continue;
3881                            }
3882                            if (app == null) {
3883                                app = dummyApp;
3884                            }
3885                            availAppCache.put(task.realActivity.getPackageName(), app);
3886                        }
3887                        if (app == dummyApp || (app.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
3888                            // Doesn't exist any more!  Good-bye.
3889                            mRecentTasks.remove(i);
3890                            task.removedFromRecents(mTaskPersister);
3891                            i--;
3892                            N--;
3893                            Slog.w(TAG, "Removing no longer valid recent: " + task);
3894                            continue;
3895                        } else {
3896                            // Otherwise just not available for now.
3897                            if (task.isAvailable) {
3898                                if (DEBUG_RECENTS) Slog.d(TAG, "Making recent unavailable: "
3899                                        + task);
3900                            }
3901                            task.isAvailable = false;
3902                        }
3903                    } else {
3904                        if (!ai.enabled || !ai.applicationInfo.enabled
3905                                || (ai.applicationInfo.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
3906                            if (task.isAvailable) {
3907                                if (DEBUG_RECENTS) Slog.d(TAG, "Making recent unavailable: "
3908                                        + task + " (enabled=" + ai.enabled + "/"
3909                                        + ai.applicationInfo.enabled +  " flags="
3910                                        + Integer.toHexString(ai.applicationInfo.flags) + ")");
3911                            }
3912                            task.isAvailable = false;
3913                        } else {
3914                            if (!task.isAvailable) {
3915                                if (DEBUG_RECENTS) Slog.d(TAG, "Making recent available: "
3916                                        + task);
3917                            }
3918                            task.isAvailable = true;
3919                        }
3920                    }
3921                }
3922            }
3923        }
3924
3925        // Verify the affiliate chain for each task.
3926        for (int i = 0; i < N; ) {
3927            TaskRecord task = mRecentTasks.remove(i);
3928            if (mTmpRecents.contains(task)) {
3929                continue;
3930            }
3931            int affiliatedTaskId = task.mAffiliatedTaskId;
3932            while (true) {
3933                TaskRecord next = task.mNextAffiliate;
3934                if (next == null) {
3935                    break;
3936                }
3937                if (next.mAffiliatedTaskId != affiliatedTaskId) {
3938                    Slog.e(TAG, "Error in Recents: next.affiliatedTaskId=" +
3939                            next.mAffiliatedTaskId + " affiliatedTaskId=" + affiliatedTaskId);
3940                    task.setNextAffiliate(null);
3941                    if (next.mPrevAffiliate == task) {
3942                        next.setPrevAffiliate(null);
3943                    }
3944                    break;
3945                }
3946                if (next.mPrevAffiliate != task) {
3947                    Slog.e(TAG, "Error in Recents chain prev.mNextAffiliate=" +
3948                            next.mPrevAffiliate + " task=" + task);
3949                    next.setPrevAffiliate(null);
3950                    task.setNextAffiliate(null);
3951                    break;
3952                }
3953                if (!mRecentTasks.contains(next)) {
3954                    Slog.e(TAG, "Error in Recents: next=" + next + " not in mRecentTasks");
3955                    task.setNextAffiliate(null);
3956                    // We know that next.mPrevAffiliate is always task, from above, so clear
3957                    // its previous affiliate.
3958                    next.setPrevAffiliate(null);
3959                    break;
3960                }
3961                task = next;
3962            }
3963            // task is now the end of the list
3964            do {
3965                mRecentTasks.remove(task);
3966                mRecentTasks.add(i++, task);
3967                mTmpRecents.add(task);
3968                task.inRecents = true;
3969            } while ((task = task.mPrevAffiliate) != null);
3970        }
3971        mTmpRecents.clear();
3972        // mRecentTasks is now in sorted, affiliated order.
3973    }
3974
3975    private final boolean moveAffiliatedTasksToFront(TaskRecord task, int taskIndex) {
3976        int N = mRecentTasks.size();
3977        TaskRecord top = task;
3978        int topIndex = taskIndex;
3979        while (top.mNextAffiliate != null && topIndex > 0) {
3980            top = top.mNextAffiliate;
3981            topIndex--;
3982        }
3983        if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: adding affilliates starting at "
3984                + topIndex + " from intial " + taskIndex);
3985        // Find the end of the chain, doing a sanity check along the way.
3986        boolean sane = top.mAffiliatedTaskId == task.mAffiliatedTaskId;
3987        int endIndex = topIndex;
3988        TaskRecord prev = top;
3989        while (endIndex < N) {
3990            TaskRecord cur = mRecentTasks.get(endIndex);
3991            if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: looking at next chain @"
3992                    + endIndex + " " + cur);
3993            if (cur == top) {
3994                // Verify start of the chain.
3995                if (cur.mNextAffiliate != null || cur.mNextAffiliateTaskId != -1) {
3996                    Slog.wtf(TAG, "Bad chain @" + endIndex
3997                            + ": first task has next affiliate: " + prev);
3998                    sane = false;
3999                    break;
4000                }
4001            } else {
4002                // Verify middle of the chain's next points back to the one before.
4003                if (cur.mNextAffiliate != prev
4004                        || cur.mNextAffiliateTaskId != prev.taskId) {
4005                    Slog.wtf(TAG, "Bad chain @" + endIndex
4006                            + ": middle task " + cur + " @" + endIndex
4007                            + " has bad next affiliate "
4008                            + cur.mNextAffiliate + " id " + cur.mNextAffiliateTaskId
4009                            + ", expected " + prev);
4010                    sane = false;
4011                    break;
4012                }
4013            }
4014            if (cur.mPrevAffiliateTaskId == -1) {
4015                // Chain ends here.
4016                if (cur.mPrevAffiliate != null) {
4017                    Slog.wtf(TAG, "Bad chain @" + endIndex
4018                            + ": last task " + cur + " has previous affiliate "
4019                            + cur.mPrevAffiliate);
4020                    sane = false;
4021                }
4022                if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: end of chain @" + endIndex);
4023                break;
4024            } else {
4025                // Verify middle of the chain's prev points to a valid item.
4026                if (cur.mPrevAffiliate == null) {
4027                    Slog.wtf(TAG, "Bad chain @" + endIndex
4028                            + ": task " + cur + " has previous affiliate "
4029                            + cur.mPrevAffiliate + " but should be id "
4030                            + cur.mPrevAffiliate);
4031                    sane = false;
4032                    break;
4033                }
4034            }
4035            if (cur.mAffiliatedTaskId != task.mAffiliatedTaskId) {
4036                Slog.wtf(TAG, "Bad chain @" + endIndex
4037                        + ": task " + cur + " has affiliated id "
4038                        + cur.mAffiliatedTaskId + " but should be "
4039                        + task.mAffiliatedTaskId);
4040                sane = false;
4041                break;
4042            }
4043            prev = cur;
4044            endIndex++;
4045            if (endIndex >= N) {
4046                Slog.wtf(TAG, "Bad chain ran off index " + endIndex
4047                        + ": last task " + prev);
4048                sane = false;
4049                break;
4050            }
4051        }
4052        if (sane) {
4053            if (endIndex < taskIndex) {
4054                Slog.wtf(TAG, "Bad chain @" + endIndex
4055                        + ": did not extend to task " + task + " @" + taskIndex);
4056                sane = false;
4057            }
4058        }
4059        if (sane) {
4060            // All looks good, we can just move all of the affiliated tasks
4061            // to the top.
4062            for (int i=topIndex; i<=endIndex; i++) {
4063                if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: moving affiliated " + task
4064                        + " from " + i + " to " + (i-topIndex));
4065                TaskRecord cur = mRecentTasks.remove(i);
4066                mRecentTasks.add(i-topIndex, cur);
4067            }
4068            if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: done moving tasks  " +  topIndex
4069                    + " to " + endIndex);
4070            return true;
4071        }
4072
4073        // Whoops, couldn't do it.
4074        return false;
4075    }
4076
4077    final void addRecentTaskLocked(TaskRecord task) {
4078        final boolean isAffiliated = task.mAffiliatedTaskId != task.taskId
4079                || task.mNextAffiliateTaskId != -1 || task.mPrevAffiliateTaskId != -1;
4080
4081        int N = mRecentTasks.size();
4082        // Quick case: check if the top-most recent task is the same.
4083        if (!isAffiliated && N > 0 && mRecentTasks.get(0) == task) {
4084            if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: already at top: " + task);
4085            return;
4086        }
4087        // Another quick case: check if this is part of a set of affiliated
4088        // tasks that are at the top.
4089        if (isAffiliated && N > 0 && task.inRecents
4090                && task.mAffiliatedTaskId == mRecentTasks.get(0).mAffiliatedTaskId) {
4091            if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: affiliated " + mRecentTasks.get(0)
4092                    + " at top when adding " + task);
4093            return;
4094        }
4095        // Another quick case: never add voice sessions.
4096        if (task.voiceSession != null) {
4097            if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: not adding voice interaction " + task);
4098            return;
4099        }
4100
4101        boolean needAffiliationFix = false;
4102
4103        // Slightly less quick case: the task is already in recents, so all we need
4104        // to do is move it.
4105        if (task.inRecents) {
4106            int taskIndex = mRecentTasks.indexOf(task);
4107            if (taskIndex >= 0) {
4108                if (!isAffiliated) {
4109                    // Simple case: this is not an affiliated task, so we just move it to the front.
4110                    mRecentTasks.remove(taskIndex);
4111                    mRecentTasks.add(0, task);
4112                    notifyTaskPersisterLocked(task, false);
4113                    if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: moving to top " + task
4114                            + " from " + taskIndex);
4115                    return;
4116                } else {
4117                    // More complicated: need to keep all affiliated tasks together.
4118                    if (moveAffiliatedTasksToFront(task, taskIndex)) {
4119                        // All went well.
4120                        return;
4121                    }
4122
4123                    // Uh oh...  something bad in the affiliation chain, try to rebuild
4124                    // everything and then go through our general path of adding a new task.
4125                    needAffiliationFix = true;
4126                }
4127            } else {
4128                Slog.wtf(TAG, "Task with inRecent not in recents: " + task);
4129                needAffiliationFix = true;
4130            }
4131        }
4132
4133        if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: trimming tasks for " + task);
4134        trimRecentsForTask(task, true);
4135
4136        N = mRecentTasks.size();
4137        while (N >= ActivityManager.getMaxRecentTasksStatic()) {
4138            final TaskRecord tr = mRecentTasks.remove(N - 1);
4139            tr.removedFromRecents(mTaskPersister);
4140            N--;
4141        }
4142        task.inRecents = true;
4143        if (!isAffiliated || needAffiliationFix) {
4144            // If this is a simple non-affiliated task, or we had some failure trying to
4145            // handle it as part of an affilated task, then just place it at the top.
4146            mRecentTasks.add(0, task);
4147        } else if (isAffiliated) {
4148            // If this is a new affiliated task, then move all of the affiliated tasks
4149            // to the front and insert this new one.
4150            TaskRecord other = task.mNextAffiliate;
4151            if (other == null) {
4152                other = task.mPrevAffiliate;
4153            }
4154            if (other != null) {
4155                int otherIndex = mRecentTasks.indexOf(other);
4156                if (otherIndex >= 0) {
4157                    // Insert new task at appropriate location.
4158                    int taskIndex;
4159                    if (other == task.mNextAffiliate) {
4160                        // We found the index of our next affiliation, which is who is
4161                        // before us in the list, so add after that point.
4162                        taskIndex = otherIndex+1;
4163                    } else {
4164                        // We found the index of our previous affiliation, which is who is
4165                        // after us in the list, so add at their position.
4166                        taskIndex = otherIndex;
4167                    }
4168                    if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: new affiliated task added at "
4169                            + taskIndex + ": " + task);
4170                    mRecentTasks.add(taskIndex, task);
4171
4172                    // Now move everything to the front.
4173                    if (moveAffiliatedTasksToFront(task, taskIndex)) {
4174                        // All went well.
4175                        return;
4176                    }
4177
4178                    // Uh oh...  something bad in the affiliation chain, try to rebuild
4179                    // everything and then go through our general path of adding a new task.
4180                    needAffiliationFix = true;
4181                } else {
4182                    if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: couldn't find other affiliation "
4183                            + other);
4184                    needAffiliationFix = true;
4185                }
4186            } else {
4187                if (DEBUG_RECENTS) Slog.d(TAG,
4188                        "addRecent: adding affiliated task without next/prev:" + task);
4189                needAffiliationFix = true;
4190            }
4191        }
4192        if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: adding " + task);
4193
4194        if (needAffiliationFix) {
4195            if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: regrouping affiliations");
4196            cleanupRecentTasksLocked(task.userId);
4197        }
4198    }
4199
4200    /**
4201     * If needed, remove oldest existing entries in recents that are for the same kind
4202     * of task as the given one.
4203     */
4204    int trimRecentsForTask(TaskRecord task, boolean doTrim) {
4205        int N = mRecentTasks.size();
4206        final Intent intent = task.intent;
4207        final boolean document = intent != null && intent.isDocument();
4208
4209        int maxRecents = task.maxRecents - 1;
4210        for (int i=0; i<N; i++) {
4211            final TaskRecord tr = mRecentTasks.get(i);
4212            if (task != tr) {
4213                if (task.userId != tr.userId) {
4214                    continue;
4215                }
4216                if (i > MAX_RECENT_BITMAPS) {
4217                    tr.freeLastThumbnail();
4218                }
4219                final Intent trIntent = tr.intent;
4220                if ((task.affinity == null || !task.affinity.equals(tr.affinity)) &&
4221                    (intent == null || !intent.filterEquals(trIntent))) {
4222                    continue;
4223                }
4224                final boolean trIsDocument = trIntent != null && trIntent.isDocument();
4225                if (document && trIsDocument) {
4226                    // These are the same document activity (not necessarily the same doc).
4227                    if (maxRecents > 0) {
4228                        --maxRecents;
4229                        continue;
4230                    }
4231                    // Hit the maximum number of documents for this task. Fall through
4232                    // and remove this document from recents.
4233                } else if (document || trIsDocument) {
4234                    // Only one of these is a document. Not the droid we're looking for.
4235                    continue;
4236                }
4237            }
4238
4239            if (!doTrim) {
4240                // If the caller is not actually asking for a trim, just tell them we reached
4241                // a point where the trim would happen.
4242                return i;
4243            }
4244
4245            // Either task and tr are the same or, their affinities match or their intents match
4246            // and neither of them is a document, or they are documents using the same activity
4247            // and their maxRecents has been reached.
4248            tr.disposeThumbnail();
4249            mRecentTasks.remove(i);
4250            if (task != tr) {
4251                tr.removedFromRecents(mTaskPersister);
4252            }
4253            i--;
4254            N--;
4255            if (task.intent == null) {
4256                // If the new recent task we are adding is not fully
4257                // specified, then replace it with the existing recent task.
4258                task = tr;
4259            }
4260            notifyTaskPersisterLocked(tr, false);
4261        }
4262
4263        return -1;
4264    }
4265
4266    @Override
4267    public void reportActivityFullyDrawn(IBinder token) {
4268        synchronized (this) {
4269            ActivityRecord r = ActivityRecord.isInStackLocked(token);
4270            if (r == null) {
4271                return;
4272            }
4273            r.reportFullyDrawnLocked();
4274        }
4275    }
4276
4277    @Override
4278    public void setRequestedOrientation(IBinder token, int requestedOrientation) {
4279        synchronized (this) {
4280            ActivityRecord r = ActivityRecord.isInStackLocked(token);
4281            if (r == null) {
4282                return;
4283            }
4284            final long origId = Binder.clearCallingIdentity();
4285            mWindowManager.setAppOrientation(r.appToken, requestedOrientation);
4286            Configuration config = mWindowManager.updateOrientationFromAppTokens(
4287                    mConfiguration, r.mayFreezeScreenLocked(r.app) ? r.appToken : null);
4288            if (config != null) {
4289                r.frozenBeforeDestroy = true;
4290                if (!updateConfigurationLocked(config, r, false, false)) {
4291                    mStackSupervisor.resumeTopActivitiesLocked();
4292                }
4293            }
4294            Binder.restoreCallingIdentity(origId);
4295        }
4296    }
4297
4298    @Override
4299    public int getRequestedOrientation(IBinder token) {
4300        synchronized (this) {
4301            ActivityRecord r = ActivityRecord.isInStackLocked(token);
4302            if (r == null) {
4303                return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
4304            }
4305            return mWindowManager.getAppOrientation(r.appToken);
4306        }
4307    }
4308
4309    /**
4310     * This is the internal entry point for handling Activity.finish().
4311     *
4312     * @param token The Binder token referencing the Activity we want to finish.
4313     * @param resultCode Result code, if any, from this Activity.
4314     * @param resultData Result data (Intent), if any, from this Activity.
4315     * @param finishTask Whether to finish the task associated with this Activity.  Only applies to
4316     *            the root Activity in the task.
4317     *
4318     * @return Returns true if the activity successfully finished, or false if it is still running.
4319     */
4320    @Override
4321    public final boolean finishActivity(IBinder token, int resultCode, Intent resultData,
4322            boolean finishTask) {
4323        // Refuse possible leaked file descriptors
4324        if (resultData != null && resultData.hasFileDescriptors() == true) {
4325            throw new IllegalArgumentException("File descriptors passed in Intent");
4326        }
4327
4328        synchronized(this) {
4329            ActivityRecord r = ActivityRecord.isInStackLocked(token);
4330            if (r == null) {
4331                return true;
4332            }
4333            // Keep track of the root activity of the task before we finish it
4334            TaskRecord tr = r.task;
4335            ActivityRecord rootR = tr.getRootActivity();
4336            // Do not allow task to finish in Lock Task mode.
4337            if (tr == mStackSupervisor.mLockTaskModeTask) {
4338                if (rootR == r) {
4339                    mStackSupervisor.showLockTaskToast();
4340                    return false;
4341                }
4342            }
4343            if (mController != null) {
4344                // Find the first activity that is not finishing.
4345                ActivityRecord next = r.task.stack.topRunningActivityLocked(token, 0);
4346                if (next != null) {
4347                    // ask watcher if this is allowed
4348                    boolean resumeOK = true;
4349                    try {
4350                        resumeOK = mController.activityResuming(next.packageName);
4351                    } catch (RemoteException e) {
4352                        mController = null;
4353                        Watchdog.getInstance().setActivityController(null);
4354                    }
4355
4356                    if (!resumeOK) {
4357                        return false;
4358                    }
4359                }
4360            }
4361            final long origId = Binder.clearCallingIdentity();
4362            try {
4363                boolean res;
4364                if (finishTask && r == rootR) {
4365                    // If requested, remove the task that is associated to this activity only if it
4366                    // was the root activity in the task.  The result code and data is ignored because
4367                    // we don't support returning them across task boundaries.
4368                    res = removeTaskByIdLocked(tr.taskId, 0);
4369                } else {
4370                    res = tr.stack.requestFinishActivityLocked(token, resultCode,
4371                            resultData, "app-request", true);
4372                }
4373                return res;
4374            } finally {
4375                Binder.restoreCallingIdentity(origId);
4376            }
4377        }
4378    }
4379
4380    @Override
4381    public final void finishHeavyWeightApp() {
4382        if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
4383                != PackageManager.PERMISSION_GRANTED) {
4384            String msg = "Permission Denial: finishHeavyWeightApp() from pid="
4385                    + Binder.getCallingPid()
4386                    + ", uid=" + Binder.getCallingUid()
4387                    + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
4388            Slog.w(TAG, msg);
4389            throw new SecurityException(msg);
4390        }
4391
4392        synchronized(this) {
4393            if (mHeavyWeightProcess == null) {
4394                return;
4395            }
4396
4397            ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(
4398                    mHeavyWeightProcess.activities);
4399            for (int i=0; i<activities.size(); i++) {
4400                ActivityRecord r = activities.get(i);
4401                if (!r.finishing) {
4402                    r.task.stack.finishActivityLocked(r, Activity.RESULT_CANCELED,
4403                            null, "finish-heavy", true);
4404                }
4405            }
4406
4407            mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
4408                    mHeavyWeightProcess.userId, 0));
4409            mHeavyWeightProcess = null;
4410        }
4411    }
4412
4413    @Override
4414    public void crashApplication(int uid, int initialPid, String packageName,
4415            String message) {
4416        if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
4417                != PackageManager.PERMISSION_GRANTED) {
4418            String msg = "Permission Denial: crashApplication() from pid="
4419                    + Binder.getCallingPid()
4420                    + ", uid=" + Binder.getCallingUid()
4421                    + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
4422            Slog.w(TAG, msg);
4423            throw new SecurityException(msg);
4424        }
4425
4426        synchronized(this) {
4427            ProcessRecord proc = null;
4428
4429            // Figure out which process to kill.  We don't trust that initialPid
4430            // still has any relation to current pids, so must scan through the
4431            // list.
4432            synchronized (mPidsSelfLocked) {
4433                for (int i=0; i<mPidsSelfLocked.size(); i++) {
4434                    ProcessRecord p = mPidsSelfLocked.valueAt(i);
4435                    if (p.uid != uid) {
4436                        continue;
4437                    }
4438                    if (p.pid == initialPid) {
4439                        proc = p;
4440                        break;
4441                    }
4442                    if (p.pkgList.containsKey(packageName)) {
4443                        proc = p;
4444                    }
4445                }
4446            }
4447
4448            if (proc == null) {
4449                Slog.w(TAG, "crashApplication: nothing for uid=" + uid
4450                        + " initialPid=" + initialPid
4451                        + " packageName=" + packageName);
4452                return;
4453            }
4454
4455            if (proc.thread != null) {
4456                if (proc.pid == Process.myPid()) {
4457                    Log.w(TAG, "crashApplication: trying to crash self!");
4458                    return;
4459                }
4460                long ident = Binder.clearCallingIdentity();
4461                try {
4462                    proc.thread.scheduleCrash(message);
4463                } catch (RemoteException e) {
4464                }
4465                Binder.restoreCallingIdentity(ident);
4466            }
4467        }
4468    }
4469
4470    @Override
4471    public final void finishSubActivity(IBinder token, String resultWho,
4472            int requestCode) {
4473        synchronized(this) {
4474            final long origId = Binder.clearCallingIdentity();
4475            ActivityRecord r = ActivityRecord.isInStackLocked(token);
4476            if (r != null) {
4477                r.task.stack.finishSubActivityLocked(r, resultWho, requestCode);
4478            }
4479            Binder.restoreCallingIdentity(origId);
4480        }
4481    }
4482
4483    @Override
4484    public boolean finishActivityAffinity(IBinder token) {
4485        synchronized(this) {
4486            final long origId = Binder.clearCallingIdentity();
4487            try {
4488                ActivityRecord r = ActivityRecord.isInStackLocked(token);
4489
4490                ActivityRecord rootR = r.task.getRootActivity();
4491                // Do not allow task to finish in Lock Task mode.
4492                if (r.task == mStackSupervisor.mLockTaskModeTask) {
4493                    if (rootR == r) {
4494                        mStackSupervisor.showLockTaskToast();
4495                        return false;
4496                    }
4497                }
4498                boolean res = false;
4499                if (r != null) {
4500                    res = r.task.stack.finishActivityAffinityLocked(r);
4501                }
4502                return res;
4503            } finally {
4504                Binder.restoreCallingIdentity(origId);
4505            }
4506        }
4507    }
4508
4509    @Override
4510    public void finishVoiceTask(IVoiceInteractionSession session) {
4511        synchronized(this) {
4512            final long origId = Binder.clearCallingIdentity();
4513            try {
4514                mStackSupervisor.finishVoiceTask(session);
4515            } finally {
4516                Binder.restoreCallingIdentity(origId);
4517            }
4518        }
4519
4520    }
4521
4522    @Override
4523    public boolean releaseActivityInstance(IBinder token) {
4524        synchronized(this) {
4525            final long origId = Binder.clearCallingIdentity();
4526            try {
4527                ActivityRecord r = ActivityRecord.isInStackLocked(token);
4528                if (r.task == null || r.task.stack == null) {
4529                    return false;
4530                }
4531                return r.task.stack.safelyDestroyActivityLocked(r, "app-req");
4532            } finally {
4533                Binder.restoreCallingIdentity(origId);
4534            }
4535        }
4536    }
4537
4538    @Override
4539    public void releaseSomeActivities(IApplicationThread appInt) {
4540        synchronized(this) {
4541            final long origId = Binder.clearCallingIdentity();
4542            try {
4543                ProcessRecord app = getRecordForAppLocked(appInt);
4544                mStackSupervisor.releaseSomeActivitiesLocked(app, "low-mem");
4545            } finally {
4546                Binder.restoreCallingIdentity(origId);
4547            }
4548        }
4549    }
4550
4551    @Override
4552    public boolean willActivityBeVisible(IBinder token) {
4553        synchronized(this) {
4554            ActivityStack stack = ActivityRecord.getStackLocked(token);
4555            if (stack != null) {
4556                return stack.willActivityBeVisibleLocked(token);
4557            }
4558            return false;
4559        }
4560    }
4561
4562    @Override
4563    public void overridePendingTransition(IBinder token, String packageName,
4564            int enterAnim, int exitAnim) {
4565        synchronized(this) {
4566            ActivityRecord self = ActivityRecord.isInStackLocked(token);
4567            if (self == null) {
4568                return;
4569            }
4570
4571            final long origId = Binder.clearCallingIdentity();
4572
4573            if (self.state == ActivityState.RESUMED
4574                    || self.state == ActivityState.PAUSING) {
4575                mWindowManager.overridePendingAppTransition(packageName,
4576                        enterAnim, exitAnim, null);
4577            }
4578
4579            Binder.restoreCallingIdentity(origId);
4580        }
4581    }
4582
4583    /**
4584     * Main function for removing an existing process from the activity manager
4585     * as a result of that process going away.  Clears out all connections
4586     * to the process.
4587     */
4588    private final void handleAppDiedLocked(ProcessRecord app,
4589            boolean restarting, boolean allowRestart) {
4590        int pid = app.pid;
4591        cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1);
4592        if (!restarting) {
4593            removeLruProcessLocked(app);
4594            if (pid > 0) {
4595                ProcessList.remove(pid);
4596            }
4597        }
4598
4599        if (mProfileProc == app) {
4600            clearProfilerLocked();
4601        }
4602
4603        // Remove this application's activities from active lists.
4604        boolean hasVisibleActivities = mStackSupervisor.handleAppDiedLocked(app);
4605
4606        app.activities.clear();
4607
4608        if (app.instrumentationClass != null) {
4609            Slog.w(TAG, "Crash of app " + app.processName
4610                  + " running instrumentation " + app.instrumentationClass);
4611            Bundle info = new Bundle();
4612            info.putString("shortMsg", "Process crashed.");
4613            finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
4614        }
4615
4616        if (!restarting) {
4617            if (!mStackSupervisor.resumeTopActivitiesLocked()) {
4618                // If there was nothing to resume, and we are not already
4619                // restarting this process, but there is a visible activity that
4620                // is hosted by the process...  then make sure all visible
4621                // activities are running, taking care of restarting this
4622                // process.
4623                if (hasVisibleActivities) {
4624                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
4625                }
4626            }
4627        }
4628    }
4629
4630    private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
4631        IBinder threadBinder = thread.asBinder();
4632        // Find the application record.
4633        for (int i=mLruProcesses.size()-1; i>=0; i--) {
4634            ProcessRecord rec = mLruProcesses.get(i);
4635            if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
4636                return i;
4637            }
4638        }
4639        return -1;
4640    }
4641
4642    final ProcessRecord getRecordForAppLocked(
4643            IApplicationThread thread) {
4644        if (thread == null) {
4645            return null;
4646        }
4647
4648        int appIndex = getLRURecordIndexForAppLocked(thread);
4649        return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
4650    }
4651
4652    final void doLowMemReportIfNeededLocked(ProcessRecord dyingProc) {
4653        // If there are no longer any background processes running,
4654        // and the app that died was not running instrumentation,
4655        // then tell everyone we are now low on memory.
4656        boolean haveBg = false;
4657        for (int i=mLruProcesses.size()-1; i>=0; i--) {
4658            ProcessRecord rec = mLruProcesses.get(i);
4659            if (rec.thread != null
4660                    && rec.setProcState >= ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) {
4661                haveBg = true;
4662                break;
4663            }
4664        }
4665
4666        if (!haveBg) {
4667            boolean doReport = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4668            if (doReport) {
4669                long now = SystemClock.uptimeMillis();
4670                if (now < (mLastMemUsageReportTime+5*60*1000)) {
4671                    doReport = false;
4672                } else {
4673                    mLastMemUsageReportTime = now;
4674                }
4675            }
4676            final ArrayList<ProcessMemInfo> memInfos
4677                    = doReport ? new ArrayList<ProcessMemInfo>(mLruProcesses.size()) : null;
4678            EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
4679            long now = SystemClock.uptimeMillis();
4680            for (int i=mLruProcesses.size()-1; i>=0; i--) {
4681                ProcessRecord rec = mLruProcesses.get(i);
4682                if (rec == dyingProc || rec.thread == null) {
4683                    continue;
4684                }
4685                if (doReport) {
4686                    memInfos.add(new ProcessMemInfo(rec.processName, rec.pid, rec.setAdj,
4687                            rec.setProcState, rec.adjType, rec.makeAdjReason()));
4688                }
4689                if ((rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
4690                    // The low memory report is overriding any current
4691                    // state for a GC request.  Make sure to do
4692                    // heavy/important/visible/foreground processes first.
4693                    if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
4694                        rec.lastRequestedGc = 0;
4695                    } else {
4696                        rec.lastRequestedGc = rec.lastLowMemory;
4697                    }
4698                    rec.reportLowMemory = true;
4699                    rec.lastLowMemory = now;
4700                    mProcessesToGc.remove(rec);
4701                    addProcessToGcListLocked(rec);
4702                }
4703            }
4704            if (doReport) {
4705                Message msg = mHandler.obtainMessage(REPORT_MEM_USAGE_MSG, memInfos);
4706                mHandler.sendMessage(msg);
4707            }
4708            scheduleAppGcsLocked();
4709        }
4710    }
4711
4712    final void appDiedLocked(ProcessRecord app) {
4713       appDiedLocked(app, app.pid, app.thread);
4714    }
4715
4716    final void appDiedLocked(ProcessRecord app, int pid,
4717            IApplicationThread thread) {
4718
4719        BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
4720        synchronized (stats) {
4721            stats.noteProcessDiedLocked(app.info.uid, pid);
4722        }
4723
4724        Process.killProcessGroup(app.info.uid, pid);
4725
4726        // Clean up already done if the process has been re-started.
4727        if (app.pid == pid && app.thread != null &&
4728                app.thread.asBinder() == thread.asBinder()) {
4729            boolean doLowMem = app.instrumentationClass == null;
4730            boolean doOomAdj = doLowMem;
4731            if (!app.killedByAm) {
4732                Slog.i(TAG, "Process " + app.processName + " (pid " + pid
4733                        + ") has died.");
4734                mAllowLowerMemLevel = true;
4735            } else {
4736                // Note that we always want to do oom adj to update our state with the
4737                // new number of procs.
4738                mAllowLowerMemLevel = false;
4739                doLowMem = false;
4740            }
4741            EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName);
4742            if (DEBUG_CLEANUP) Slog.v(
4743                TAG, "Dying app: " + app + ", pid: " + pid
4744                + ", thread: " + thread.asBinder());
4745            handleAppDiedLocked(app, false, true);
4746
4747            if (doOomAdj) {
4748                updateOomAdjLocked();
4749            }
4750            if (doLowMem) {
4751                doLowMemReportIfNeededLocked(app);
4752            }
4753        } else if (app.pid != pid) {
4754            // A new process has already been started.
4755            Slog.i(TAG, "Process " + app.processName + " (pid " + pid
4756                    + ") has died and restarted (pid " + app.pid + ").");
4757            EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName);
4758        } else if (DEBUG_PROCESSES) {
4759            Slog.d(TAG, "Received spurious death notification for thread "
4760                    + thread.asBinder());
4761        }
4762    }
4763
4764    /**
4765     * If a stack trace dump file is configured, dump process stack traces.
4766     * @param clearTraces causes the dump file to be erased prior to the new
4767     *    traces being written, if true; when false, the new traces will be
4768     *    appended to any existing file content.
4769     * @param firstPids of dalvik VM processes to dump stack traces for first
4770     * @param lastPids of dalvik VM processes to dump stack traces for last
4771     * @param nativeProcs optional list of native process names to dump stack crawls
4772     * @return file containing stack traces, or null if no dump file is configured
4773     */
4774    public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids,
4775            ProcessCpuTracker processCpuTracker, SparseArray<Boolean> lastPids, String[] nativeProcs) {
4776        String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
4777        if (tracesPath == null || tracesPath.length() == 0) {
4778            return null;
4779        }
4780
4781        File tracesFile = new File(tracesPath);
4782        try {
4783            File tracesDir = tracesFile.getParentFile();
4784            if (!tracesDir.exists()) {
4785                tracesFile.mkdirs();
4786                if (!SELinux.restorecon(tracesDir)) {
4787                    return null;
4788                }
4789            }
4790            FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1);  // drwxrwxr-x
4791
4792            if (clearTraces && tracesFile.exists()) tracesFile.delete();
4793            tracesFile.createNewFile();
4794            FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
4795        } catch (IOException e) {
4796            Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
4797            return null;
4798        }
4799
4800        dumpStackTraces(tracesPath, firstPids, processCpuTracker, lastPids, nativeProcs);
4801        return tracesFile;
4802    }
4803
4804    private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids,
4805            ProcessCpuTracker processCpuTracker, SparseArray<Boolean> lastPids, String[] nativeProcs) {
4806        // Use a FileObserver to detect when traces finish writing.
4807        // The order of traces is considered important to maintain for legibility.
4808        FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
4809            @Override
4810            public synchronized void onEvent(int event, String path) { notify(); }
4811        };
4812
4813        try {
4814            observer.startWatching();
4815
4816            // First collect all of the stacks of the most important pids.
4817            if (firstPids != null) {
4818                try {
4819                    int num = firstPids.size();
4820                    for (int i = 0; i < num; i++) {
4821                        synchronized (observer) {
4822                            Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
4823                            observer.wait(200);  // Wait for write-close, give up after 200msec
4824                        }
4825                    }
4826                } catch (InterruptedException e) {
4827                    Log.wtf(TAG, e);
4828                }
4829            }
4830
4831            // Next collect the stacks of the native pids
4832            if (nativeProcs != null) {
4833                int[] pids = Process.getPidsForCommands(nativeProcs);
4834                if (pids != null) {
4835                    for (int pid : pids) {
4836                        Debug.dumpNativeBacktraceToFile(pid, tracesPath);
4837                    }
4838                }
4839            }
4840
4841            // Lastly, measure CPU usage.
4842            if (processCpuTracker != null) {
4843                processCpuTracker.init();
4844                System.gc();
4845                processCpuTracker.update();
4846                try {
4847                    synchronized (processCpuTracker) {
4848                        processCpuTracker.wait(500); // measure over 1/2 second.
4849                    }
4850                } catch (InterruptedException e) {
4851                }
4852                processCpuTracker.update();
4853
4854                // We'll take the stack crawls of just the top apps using CPU.
4855                final int N = processCpuTracker.countWorkingStats();
4856                int numProcs = 0;
4857                for (int i=0; i<N && numProcs<5; i++) {
4858                    ProcessCpuTracker.Stats stats = processCpuTracker.getWorkingStats(i);
4859                    if (lastPids.indexOfKey(stats.pid) >= 0) {
4860                        numProcs++;
4861                        try {
4862                            synchronized (observer) {
4863                                Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
4864                                observer.wait(200);  // Wait for write-close, give up after 200msec
4865                            }
4866                        } catch (InterruptedException e) {
4867                            Log.wtf(TAG, e);
4868                        }
4869
4870                    }
4871                }
4872            }
4873        } finally {
4874            observer.stopWatching();
4875        }
4876    }
4877
4878    final void logAppTooSlow(ProcessRecord app, long startTime, String msg) {
4879        if (true || IS_USER_BUILD) {
4880            return;
4881        }
4882        String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
4883        if (tracesPath == null || tracesPath.length() == 0) {
4884            return;
4885        }
4886
4887        StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
4888        StrictMode.allowThreadDiskWrites();
4889        try {
4890            final File tracesFile = new File(tracesPath);
4891            final File tracesDir = tracesFile.getParentFile();
4892            final File tracesTmp = new File(tracesDir, "__tmp__");
4893            try {
4894                if (!tracesDir.exists()) {
4895                    tracesFile.mkdirs();
4896                    if (!SELinux.restorecon(tracesDir.getPath())) {
4897                        return;
4898                    }
4899                }
4900                FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1);  // drwxrwxr-x
4901
4902                if (tracesFile.exists()) {
4903                    tracesTmp.delete();
4904                    tracesFile.renameTo(tracesTmp);
4905                }
4906                StringBuilder sb = new StringBuilder();
4907                Time tobj = new Time();
4908                tobj.set(System.currentTimeMillis());
4909                sb.append(tobj.format("%Y-%m-%d %H:%M:%S"));
4910                sb.append(": ");
4911                TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb);
4912                sb.append(" since ");
4913                sb.append(msg);
4914                FileOutputStream fos = new FileOutputStream(tracesFile);
4915                fos.write(sb.toString().getBytes());
4916                if (app == null) {
4917                    fos.write("\n*** No application process!".getBytes());
4918                }
4919                fos.close();
4920                FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
4921            } catch (IOException e) {
4922                Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e);
4923                return;
4924            }
4925
4926            if (app != null) {
4927                ArrayList<Integer> firstPids = new ArrayList<Integer>();
4928                firstPids.add(app.pid);
4929                dumpStackTraces(tracesPath, firstPids, null, null, null);
4930            }
4931
4932            File lastTracesFile = null;
4933            File curTracesFile = null;
4934            for (int i=9; i>=0; i--) {
4935                String name = String.format(Locale.US, "slow%02d.txt", i);
4936                curTracesFile = new File(tracesDir, name);
4937                if (curTracesFile.exists()) {
4938                    if (lastTracesFile != null) {
4939                        curTracesFile.renameTo(lastTracesFile);
4940                    } else {
4941                        curTracesFile.delete();
4942                    }
4943                }
4944                lastTracesFile = curTracesFile;
4945            }
4946            tracesFile.renameTo(curTracesFile);
4947            if (tracesTmp.exists()) {
4948                tracesTmp.renameTo(tracesFile);
4949            }
4950        } finally {
4951            StrictMode.setThreadPolicy(oldPolicy);
4952        }
4953    }
4954
4955    final void appNotResponding(ProcessRecord app, ActivityRecord activity,
4956            ActivityRecord parent, boolean aboveSystem, final String annotation) {
4957        ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
4958        SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
4959
4960        if (mController != null) {
4961            try {
4962                // 0 == continue, -1 = kill process immediately
4963                int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation);
4964                if (res < 0 && app.pid != MY_PID) {
4965                    app.kill("anr", true);
4966                }
4967            } catch (RemoteException e) {
4968                mController = null;
4969                Watchdog.getInstance().setActivityController(null);
4970            }
4971        }
4972
4973        long anrTime = SystemClock.uptimeMillis();
4974        if (MONITOR_CPU_USAGE) {
4975            updateCpuStatsNow();
4976        }
4977
4978        synchronized (this) {
4979            // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
4980            if (mShuttingDown) {
4981                Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
4982                return;
4983            } else if (app.notResponding) {
4984                Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
4985                return;
4986            } else if (app.crashing) {
4987                Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
4988                return;
4989            }
4990
4991            // In case we come through here for the same app before completing
4992            // this one, mark as anring now so we will bail out.
4993            app.notResponding = true;
4994
4995            // Log the ANR to the event log.
4996            EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid,
4997                    app.processName, app.info.flags, annotation);
4998
4999            // Dump thread traces as quickly as we can, starting with "interesting" processes.
5000            firstPids.add(app.pid);
5001
5002            int parentPid = app.pid;
5003            if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
5004            if (parentPid != app.pid) firstPids.add(parentPid);
5005
5006            if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
5007
5008            for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
5009                ProcessRecord r = mLruProcesses.get(i);
5010                if (r != null && r.thread != null) {
5011                    int pid = r.pid;
5012                    if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
5013                        if (r.persistent) {
5014                            firstPids.add(pid);
5015                        } else {
5016                            lastPids.put(pid, Boolean.TRUE);
5017                        }
5018                    }
5019                }
5020            }
5021        }
5022
5023        // Log the ANR to the main log.
5024        StringBuilder info = new StringBuilder();
5025        info.setLength(0);
5026        info.append("ANR in ").append(app.processName);
5027        if (activity != null && activity.shortComponentName != null) {
5028            info.append(" (").append(activity.shortComponentName).append(")");
5029        }
5030        info.append("\n");
5031        info.append("PID: ").append(app.pid).append("\n");
5032        if (annotation != null) {
5033            info.append("Reason: ").append(annotation).append("\n");
5034        }
5035        if (parent != null && parent != activity) {
5036            info.append("Parent: ").append(parent.shortComponentName).append("\n");
5037        }
5038
5039        final ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true);
5040
5041        File tracesFile = dumpStackTraces(true, firstPids, processCpuTracker, lastPids,
5042                NATIVE_STACKS_OF_INTEREST);
5043
5044        String cpuInfo = null;
5045        if (MONITOR_CPU_USAGE) {
5046            updateCpuStatsNow();
5047            synchronized (mProcessCpuThread) {
5048                cpuInfo = mProcessCpuTracker.printCurrentState(anrTime);
5049            }
5050            info.append(processCpuTracker.printCurrentLoad());
5051            info.append(cpuInfo);
5052        }
5053
5054        info.append(processCpuTracker.printCurrentState(anrTime));
5055
5056        Slog.e(TAG, info.toString());
5057        if (tracesFile == null) {
5058            // There is no trace file, so dump (only) the alleged culprit's threads to the log
5059            Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
5060        }
5061
5062        addErrorToDropBox("anr", app, app.processName, activity, parent, annotation,
5063                cpuInfo, tracesFile, null);
5064
5065        if (mController != null) {
5066            try {
5067                // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
5068                int res = mController.appNotResponding(app.processName, app.pid, info.toString());
5069                if (res != 0) {
5070                    if (res < 0 && app.pid != MY_PID) {
5071                        app.kill("anr", true);
5072                    } else {
5073                        synchronized (this) {
5074                            mServices.scheduleServiceTimeoutLocked(app);
5075                        }
5076                    }
5077                    return;
5078                }
5079            } catch (RemoteException e) {
5080                mController = null;
5081                Watchdog.getInstance().setActivityController(null);
5082            }
5083        }
5084
5085        // Unless configured otherwise, swallow ANRs in background processes & kill the process.
5086        boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
5087                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
5088
5089        synchronized (this) {
5090            if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
5091                app.kill("bg anr", true);
5092                return;
5093            }
5094
5095            // Set the app's notResponding state, and look up the errorReportReceiver
5096            makeAppNotRespondingLocked(app,
5097                    activity != null ? activity.shortComponentName : null,
5098                    annotation != null ? "ANR " + annotation : "ANR",
5099                    info.toString());
5100
5101            // Bring up the infamous App Not Responding dialog
5102            Message msg = Message.obtain();
5103            HashMap<String, Object> map = new HashMap<String, Object>();
5104            msg.what = SHOW_NOT_RESPONDING_MSG;
5105            msg.obj = map;
5106            msg.arg1 = aboveSystem ? 1 : 0;
5107            map.put("app", app);
5108            if (activity != null) {
5109                map.put("activity", activity);
5110            }
5111
5112            mHandler.sendMessage(msg);
5113        }
5114    }
5115
5116    final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) {
5117        if (!mLaunchWarningShown) {
5118            mLaunchWarningShown = true;
5119            mHandler.post(new Runnable() {
5120                @Override
5121                public void run() {
5122                    synchronized (ActivityManagerService.this) {
5123                        final Dialog d = new LaunchWarningWindow(mContext, cur, next);
5124                        d.show();
5125                        mHandler.postDelayed(new Runnable() {
5126                            @Override
5127                            public void run() {
5128                                synchronized (ActivityManagerService.this) {
5129                                    d.dismiss();
5130                                    mLaunchWarningShown = false;
5131                                }
5132                            }
5133                        }, 4000);
5134                    }
5135                }
5136            });
5137        }
5138    }
5139
5140    @Override
5141    public boolean clearApplicationUserData(final String packageName,
5142            final IPackageDataObserver observer, int userId) {
5143        enforceNotIsolatedCaller("clearApplicationUserData");
5144        int uid = Binder.getCallingUid();
5145        int pid = Binder.getCallingPid();
5146        userId = handleIncomingUser(pid, uid,
5147                userId, false, ALLOW_FULL_ONLY, "clearApplicationUserData", null);
5148        long callingId = Binder.clearCallingIdentity();
5149        try {
5150            IPackageManager pm = AppGlobals.getPackageManager();
5151            int pkgUid = -1;
5152            synchronized(this) {
5153                try {
5154                    pkgUid = pm.getPackageUid(packageName, userId);
5155                } catch (RemoteException e) {
5156                }
5157                if (pkgUid == -1) {
5158                    Slog.w(TAG, "Invalid packageName: " + packageName);
5159                    if (observer != null) {
5160                        try {
5161                            observer.onRemoveCompleted(packageName, false);
5162                        } catch (RemoteException e) {
5163                            Slog.i(TAG, "Observer no longer exists.");
5164                        }
5165                    }
5166                    return false;
5167                }
5168                if (uid == pkgUid || checkComponentPermission(
5169                        android.Manifest.permission.CLEAR_APP_USER_DATA,
5170                        pid, uid, -1, true)
5171                        == PackageManager.PERMISSION_GRANTED) {
5172                    forceStopPackageLocked(packageName, pkgUid, "clear data");
5173                } else {
5174                    throw new SecurityException("PID " + pid + " does not have permission "
5175                            + android.Manifest.permission.CLEAR_APP_USER_DATA + " to clear data"
5176                                    + " of package " + packageName);
5177                }
5178
5179                // Remove all tasks match the cleared application package and user
5180                for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
5181                    final TaskRecord tr = mRecentTasks.get(i);
5182                    final String taskPackageName =
5183                            tr.getBaseIntent().getComponent().getPackageName();
5184                    if (tr.userId != userId) continue;
5185                    if (!taskPackageName.equals(packageName)) continue;
5186                    removeTaskByIdLocked(tr.taskId, 0);
5187                }
5188            }
5189
5190            try {
5191                // Clear application user data
5192                pm.clearApplicationUserData(packageName, observer, userId);
5193
5194                synchronized(this) {
5195                    // Remove all permissions granted from/to this package
5196                    removeUriPermissionsForPackageLocked(packageName, userId, true);
5197                }
5198
5199                Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
5200                        Uri.fromParts("package", packageName, null));
5201                intent.putExtra(Intent.EXTRA_UID, pkgUid);
5202                broadcastIntentInPackage("android", Process.SYSTEM_UID, intent,
5203                        null, null, 0, null, null, null, false, false, userId);
5204            } catch (RemoteException e) {
5205            }
5206        } finally {
5207            Binder.restoreCallingIdentity(callingId);
5208        }
5209        return true;
5210    }
5211
5212    @Override
5213    public void killBackgroundProcesses(final String packageName, int userId) {
5214        if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
5215                != PackageManager.PERMISSION_GRANTED &&
5216                checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
5217                        != PackageManager.PERMISSION_GRANTED) {
5218            String msg = "Permission Denial: killBackgroundProcesses() from pid="
5219                    + Binder.getCallingPid()
5220                    + ", uid=" + Binder.getCallingUid()
5221                    + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
5222            Slog.w(TAG, msg);
5223            throw new SecurityException(msg);
5224        }
5225
5226        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
5227                userId, true, ALLOW_FULL_ONLY, "killBackgroundProcesses", null);
5228        long callingId = Binder.clearCallingIdentity();
5229        try {
5230            IPackageManager pm = AppGlobals.getPackageManager();
5231            synchronized(this) {
5232                int appId = -1;
5233                try {
5234                    appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0));
5235                } catch (RemoteException e) {
5236                }
5237                if (appId == -1) {
5238                    Slog.w(TAG, "Invalid packageName: " + packageName);
5239                    return;
5240                }
5241                killPackageProcessesLocked(packageName, appId, userId,
5242                        ProcessList.SERVICE_ADJ, false, true, true, false, "kill background");
5243            }
5244        } finally {
5245            Binder.restoreCallingIdentity(callingId);
5246        }
5247    }
5248
5249    @Override
5250    public void killAllBackgroundProcesses() {
5251        if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
5252                != PackageManager.PERMISSION_GRANTED) {
5253            String msg = "Permission Denial: killAllBackgroundProcesses() from pid="
5254                    + Binder.getCallingPid()
5255                    + ", uid=" + Binder.getCallingUid()
5256                    + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
5257            Slog.w(TAG, msg);
5258            throw new SecurityException(msg);
5259        }
5260
5261        long callingId = Binder.clearCallingIdentity();
5262        try {
5263            synchronized(this) {
5264                ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
5265                final int NP = mProcessNames.getMap().size();
5266                for (int ip=0; ip<NP; ip++) {
5267                    SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
5268                    final int NA = apps.size();
5269                    for (int ia=0; ia<NA; ia++) {
5270                        ProcessRecord app = apps.valueAt(ia);
5271                        if (app.persistent) {
5272                            // we don't kill persistent processes
5273                            continue;
5274                        }
5275                        if (app.removed) {
5276                            procs.add(app);
5277                        } else if (app.setAdj >= ProcessList.CACHED_APP_MIN_ADJ) {
5278                            app.removed = true;
5279                            procs.add(app);
5280                        }
5281                    }
5282                }
5283
5284                int N = procs.size();
5285                for (int i=0; i<N; i++) {
5286                    removeProcessLocked(procs.get(i), false, true, "kill all background");
5287                }
5288                mAllowLowerMemLevel = true;
5289                updateOomAdjLocked();
5290                doLowMemReportIfNeededLocked(null);
5291            }
5292        } finally {
5293            Binder.restoreCallingIdentity(callingId);
5294        }
5295    }
5296
5297    @Override
5298    public void forceStopPackage(final String packageName, int userId) {
5299        if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
5300                != PackageManager.PERMISSION_GRANTED) {
5301            String msg = "Permission Denial: forceStopPackage() from pid="
5302                    + Binder.getCallingPid()
5303                    + ", uid=" + Binder.getCallingUid()
5304                    + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
5305            Slog.w(TAG, msg);
5306            throw new SecurityException(msg);
5307        }
5308        final int callingPid = Binder.getCallingPid();
5309        userId = handleIncomingUser(callingPid, Binder.getCallingUid(),
5310                userId, true, ALLOW_FULL_ONLY, "forceStopPackage", null);
5311        long callingId = Binder.clearCallingIdentity();
5312        try {
5313            IPackageManager pm = AppGlobals.getPackageManager();
5314            synchronized(this) {
5315                int[] users = userId == UserHandle.USER_ALL
5316                        ? getUsersLocked() : new int[] { userId };
5317                for (int user : users) {
5318                    int pkgUid = -1;
5319                    try {
5320                        pkgUid = pm.getPackageUid(packageName, user);
5321                    } catch (RemoteException e) {
5322                    }
5323                    if (pkgUid == -1) {
5324                        Slog.w(TAG, "Invalid packageName: " + packageName);
5325                        continue;
5326                    }
5327                    try {
5328                        pm.setPackageStoppedState(packageName, true, user);
5329                    } catch (RemoteException e) {
5330                    } catch (IllegalArgumentException e) {
5331                        Slog.w(TAG, "Failed trying to unstop package "
5332                                + packageName + ": " + e);
5333                    }
5334                    if (isUserRunningLocked(user, false)) {
5335                        forceStopPackageLocked(packageName, pkgUid, "from pid " + callingPid);
5336                    }
5337                }
5338            }
5339        } finally {
5340            Binder.restoreCallingIdentity(callingId);
5341        }
5342    }
5343
5344    @Override
5345    public void addPackageDependency(String packageName) {
5346        synchronized (this) {
5347            int callingPid = Binder.getCallingPid();
5348            if (callingPid == Process.myPid()) {
5349                //  Yeah, um, no.
5350                Slog.w(TAG, "Can't addPackageDependency on system process");
5351                return;
5352            }
5353            ProcessRecord proc;
5354            synchronized (mPidsSelfLocked) {
5355                proc = mPidsSelfLocked.get(Binder.getCallingPid());
5356            }
5357            if (proc != null) {
5358                if (proc.pkgDeps == null) {
5359                    proc.pkgDeps = new ArraySet<String>(1);
5360                }
5361                proc.pkgDeps.add(packageName);
5362            }
5363        }
5364    }
5365
5366    /*
5367     * The pkg name and app id have to be specified.
5368     */
5369    @Override
5370    public void killApplicationWithAppId(String pkg, int appid, String reason) {
5371        if (pkg == null) {
5372            return;
5373        }
5374        // Make sure the uid is valid.
5375        if (appid < 0) {
5376            Slog.w(TAG, "Invalid appid specified for pkg : " + pkg);
5377            return;
5378        }
5379        int callerUid = Binder.getCallingUid();
5380        // Only the system server can kill an application
5381        if (callerUid == Process.SYSTEM_UID) {
5382            // Post an aysnc message to kill the application
5383            Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
5384            msg.arg1 = appid;
5385            msg.arg2 = 0;
5386            Bundle bundle = new Bundle();
5387            bundle.putString("pkg", pkg);
5388            bundle.putString("reason", reason);
5389            msg.obj = bundle;
5390            mHandler.sendMessage(msg);
5391        } else {
5392            throw new SecurityException(callerUid + " cannot kill pkg: " +
5393                    pkg);
5394        }
5395    }
5396
5397    @Override
5398    public void closeSystemDialogs(String reason) {
5399        enforceNotIsolatedCaller("closeSystemDialogs");
5400
5401        final int pid = Binder.getCallingPid();
5402        final int uid = Binder.getCallingUid();
5403        final long origId = Binder.clearCallingIdentity();
5404        try {
5405            synchronized (this) {
5406                // Only allow this from foreground processes, so that background
5407                // applications can't abuse it to prevent system UI from being shown.
5408                if (uid >= Process.FIRST_APPLICATION_UID) {
5409                    ProcessRecord proc;
5410                    synchronized (mPidsSelfLocked) {
5411                        proc = mPidsSelfLocked.get(pid);
5412                    }
5413                    if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
5414                        Slog.w(TAG, "Ignoring closeSystemDialogs " + reason
5415                                + " from background process " + proc);
5416                        return;
5417                    }
5418                }
5419                closeSystemDialogsLocked(reason);
5420            }
5421        } finally {
5422            Binder.restoreCallingIdentity(origId);
5423        }
5424    }
5425
5426    void closeSystemDialogsLocked(String reason) {
5427        Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
5428        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
5429                | Intent.FLAG_RECEIVER_FOREGROUND);
5430        if (reason != null) {
5431            intent.putExtra("reason", reason);
5432        }
5433        mWindowManager.closeSystemDialogs(reason);
5434
5435        mStackSupervisor.closeSystemDialogsLocked();
5436
5437        broadcastIntentLocked(null, null, intent, null,
5438                null, 0, null, null, null, AppOpsManager.OP_NONE, false, false, -1,
5439                Process.SYSTEM_UID, UserHandle.USER_ALL);
5440    }
5441
5442    @Override
5443    public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
5444        enforceNotIsolatedCaller("getProcessMemoryInfo");
5445        Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
5446        for (int i=pids.length-1; i>=0; i--) {
5447            ProcessRecord proc;
5448            int oomAdj;
5449            synchronized (this) {
5450                synchronized (mPidsSelfLocked) {
5451                    proc = mPidsSelfLocked.get(pids[i]);
5452                    oomAdj = proc != null ? proc.setAdj : 0;
5453                }
5454            }
5455            infos[i] = new Debug.MemoryInfo();
5456            Debug.getMemoryInfo(pids[i], infos[i]);
5457            if (proc != null) {
5458                synchronized (this) {
5459                    if (proc.thread != null && proc.setAdj == oomAdj) {
5460                        // Record this for posterity if the process has been stable.
5461                        proc.baseProcessTracker.addPss(infos[i].getTotalPss(),
5462                                infos[i].getTotalUss(), false, proc.pkgList);
5463                    }
5464                }
5465            }
5466        }
5467        return infos;
5468    }
5469
5470    @Override
5471    public long[] getProcessPss(int[] pids) {
5472        enforceNotIsolatedCaller("getProcessPss");
5473        long[] pss = new long[pids.length];
5474        for (int i=pids.length-1; i>=0; i--) {
5475            ProcessRecord proc;
5476            int oomAdj;
5477            synchronized (this) {
5478                synchronized (mPidsSelfLocked) {
5479                    proc = mPidsSelfLocked.get(pids[i]);
5480                    oomAdj = proc != null ? proc.setAdj : 0;
5481                }
5482            }
5483            long[] tmpUss = new long[1];
5484            pss[i] = Debug.getPss(pids[i], tmpUss);
5485            if (proc != null) {
5486                synchronized (this) {
5487                    if (proc.thread != null && proc.setAdj == oomAdj) {
5488                        // Record this for posterity if the process has been stable.
5489                        proc.baseProcessTracker.addPss(pss[i], tmpUss[0], false, proc.pkgList);
5490                    }
5491                }
5492            }
5493        }
5494        return pss;
5495    }
5496
5497    @Override
5498    public void killApplicationProcess(String processName, int uid) {
5499        if (processName == null) {
5500            return;
5501        }
5502
5503        int callerUid = Binder.getCallingUid();
5504        // Only the system server can kill an application
5505        if (callerUid == Process.SYSTEM_UID) {
5506            synchronized (this) {
5507                ProcessRecord app = getProcessRecordLocked(processName, uid, true);
5508                if (app != null && app.thread != null) {
5509                    try {
5510                        app.thread.scheduleSuicide();
5511                    } catch (RemoteException e) {
5512                        // If the other end already died, then our work here is done.
5513                    }
5514                } else {
5515                    Slog.w(TAG, "Process/uid not found attempting kill of "
5516                            + processName + " / " + uid);
5517                }
5518            }
5519        } else {
5520            throw new SecurityException(callerUid + " cannot kill app process: " +
5521                    processName);
5522        }
5523    }
5524
5525    private void forceStopPackageLocked(final String packageName, int uid, String reason) {
5526        forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false,
5527                false, true, false, false, UserHandle.getUserId(uid), reason);
5528        Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
5529                Uri.fromParts("package", packageName, null));
5530        if (!mProcessesReady) {
5531            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
5532                    | Intent.FLAG_RECEIVER_FOREGROUND);
5533        }
5534        intent.putExtra(Intent.EXTRA_UID, uid);
5535        intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid));
5536        broadcastIntentLocked(null, null, intent,
5537                null, null, 0, null, null, null, AppOpsManager.OP_NONE,
5538                false, false,
5539                MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid));
5540    }
5541
5542    private void forceStopUserLocked(int userId, String reason) {
5543        forceStopPackageLocked(null, -1, false, false, true, false, false, userId, reason);
5544        Intent intent = new Intent(Intent.ACTION_USER_STOPPED);
5545        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
5546                | Intent.FLAG_RECEIVER_FOREGROUND);
5547        intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
5548        broadcastIntentLocked(null, null, intent,
5549                null, null, 0, null, null, null, AppOpsManager.OP_NONE,
5550                false, false,
5551                MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
5552    }
5553
5554    private final boolean killPackageProcessesLocked(String packageName, int appId,
5555            int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart,
5556            boolean doit, boolean evenPersistent, String reason) {
5557        ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
5558
5559        // Remove all processes this package may have touched: all with the
5560        // same UID (except for the system or root user), and all whose name
5561        // matches the package name.
5562        final int NP = mProcessNames.getMap().size();
5563        for (int ip=0; ip<NP; ip++) {
5564            SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
5565            final int NA = apps.size();
5566            for (int ia=0; ia<NA; ia++) {
5567                ProcessRecord app = apps.valueAt(ia);
5568                if (app.persistent && !evenPersistent) {
5569                    // we don't kill persistent processes
5570                    continue;
5571                }
5572                if (app.removed) {
5573                    if (doit) {
5574                        procs.add(app);
5575                    }
5576                    continue;
5577                }
5578
5579                // Skip process if it doesn't meet our oom adj requirement.
5580                if (app.setAdj < minOomAdj) {
5581                    continue;
5582                }
5583
5584                // If no package is specified, we call all processes under the
5585                // give user id.
5586                if (packageName == null) {
5587                    if (app.userId != userId) {
5588                        continue;
5589                    }
5590                    if (appId >= 0 && UserHandle.getAppId(app.uid) != appId) {
5591                        continue;
5592                    }
5593                // Package has been specified, we want to hit all processes
5594                // that match it.  We need to qualify this by the processes
5595                // that are running under the specified app and user ID.
5596                } else {
5597                    final boolean isDep = app.pkgDeps != null
5598                            && app.pkgDeps.contains(packageName);
5599                    if (!isDep && UserHandle.getAppId(app.uid) != appId) {
5600                        continue;
5601                    }
5602                    if (userId != UserHandle.USER_ALL && app.userId != userId) {
5603                        continue;
5604                    }
5605                    if (!app.pkgList.containsKey(packageName) && !isDep) {
5606                        continue;
5607                    }
5608                }
5609
5610                // Process has passed all conditions, kill it!
5611                if (!doit) {
5612                    return true;
5613                }
5614                app.removed = true;
5615                procs.add(app);
5616            }
5617        }
5618
5619        int N = procs.size();
5620        for (int i=0; i<N; i++) {
5621            removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
5622        }
5623        updateOomAdjLocked();
5624        return N > 0;
5625    }
5626
5627    private final boolean forceStopPackageLocked(String name, int appId,
5628            boolean callerWillRestart, boolean purgeCache, boolean doit,
5629            boolean evenPersistent, boolean uninstalling, int userId, String reason) {
5630        int i;
5631        int N;
5632
5633        if (userId == UserHandle.USER_ALL && name == null) {
5634            Slog.w(TAG, "Can't force stop all processes of all users, that is insane!");
5635        }
5636
5637        if (appId < 0 && name != null) {
5638            try {
5639                appId = UserHandle.getAppId(
5640                        AppGlobals.getPackageManager().getPackageUid(name, 0));
5641            } catch (RemoteException e) {
5642            }
5643        }
5644
5645        if (doit) {
5646            if (name != null) {
5647                Slog.i(TAG, "Force stopping " + name + " appid=" + appId
5648                        + " user=" + userId + ": " + reason);
5649            } else {
5650                Slog.i(TAG, "Force stopping u" + userId + ": " + reason);
5651            }
5652
5653            final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap();
5654            for (int ip=pmap.size()-1; ip>=0; ip--) {
5655                SparseArray<Long> ba = pmap.valueAt(ip);
5656                for (i=ba.size()-1; i>=0; i--) {
5657                    boolean remove = false;
5658                    final int entUid = ba.keyAt(i);
5659                    if (name != null) {
5660                        if (userId == UserHandle.USER_ALL) {
5661                            if (UserHandle.getAppId(entUid) == appId) {
5662                                remove = true;
5663                            }
5664                        } else {
5665                            if (entUid == UserHandle.getUid(userId, appId)) {
5666                                remove = true;
5667                            }
5668                        }
5669                    } else if (UserHandle.getUserId(entUid) == userId) {
5670                        remove = true;
5671                    }
5672                    if (remove) {
5673                        ba.removeAt(i);
5674                    }
5675                }
5676                if (ba.size() == 0) {
5677                    pmap.removeAt(ip);
5678                }
5679            }
5680        }
5681
5682        boolean didSomething = killPackageProcessesLocked(name, appId, userId,
5683                -100, callerWillRestart, true, doit, evenPersistent,
5684                name == null ? ("stop user " + userId) : ("stop " + name));
5685
5686        if (mStackSupervisor.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
5687            if (!doit) {
5688                return true;
5689            }
5690            didSomething = true;
5691        }
5692
5693        if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) {
5694            if (!doit) {
5695                return true;
5696            }
5697            didSomething = true;
5698        }
5699
5700        if (name == null) {
5701            // Remove all sticky broadcasts from this user.
5702            mStickyBroadcasts.remove(userId);
5703        }
5704
5705        ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>();
5706        if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent,
5707                userId, providers)) {
5708            if (!doit) {
5709                return true;
5710            }
5711            didSomething = true;
5712        }
5713        N = providers.size();
5714        for (i=0; i<N; i++) {
5715            removeDyingProviderLocked(null, providers.get(i), true);
5716        }
5717
5718        // Remove transient permissions granted from/to this package/user
5719        removeUriPermissionsForPackageLocked(name, userId, false);
5720
5721        if (name == null || uninstalling) {
5722            // Remove pending intents.  For now we only do this when force
5723            // stopping users, because we have some problems when doing this
5724            // for packages -- app widgets are not currently cleaned up for
5725            // such packages, so they can be left with bad pending intents.
5726            if (mIntentSenderRecords.size() > 0) {
5727                Iterator<WeakReference<PendingIntentRecord>> it
5728                        = mIntentSenderRecords.values().iterator();
5729                while (it.hasNext()) {
5730                    WeakReference<PendingIntentRecord> wpir = it.next();
5731                    if (wpir == null) {
5732                        it.remove();
5733                        continue;
5734                    }
5735                    PendingIntentRecord pir = wpir.get();
5736                    if (pir == null) {
5737                        it.remove();
5738                        continue;
5739                    }
5740                    if (name == null) {
5741                        // Stopping user, remove all objects for the user.
5742                        if (pir.key.userId != userId) {
5743                            // Not the same user, skip it.
5744                            continue;
5745                        }
5746                    } else {
5747                        if (UserHandle.getAppId(pir.uid) != appId) {
5748                            // Different app id, skip it.
5749                            continue;
5750                        }
5751                        if (userId != UserHandle.USER_ALL && pir.key.userId != userId) {
5752                            // Different user, skip it.
5753                            continue;
5754                        }
5755                        if (!pir.key.packageName.equals(name)) {
5756                            // Different package, skip it.
5757                            continue;
5758                        }
5759                    }
5760                    if (!doit) {
5761                        return true;
5762                    }
5763                    didSomething = true;
5764                    it.remove();
5765                    pir.canceled = true;
5766                    if (pir.key.activity != null) {
5767                        pir.key.activity.pendingResults.remove(pir.ref);
5768                    }
5769                }
5770            }
5771        }
5772
5773        if (doit) {
5774            if (purgeCache && name != null) {
5775                AttributeCache ac = AttributeCache.instance();
5776                if (ac != null) {
5777                    ac.removePackage(name);
5778                }
5779            }
5780            if (mBooted) {
5781                mStackSupervisor.resumeTopActivitiesLocked();
5782                mStackSupervisor.scheduleIdleLocked();
5783            }
5784        }
5785
5786        return didSomething;
5787    }
5788
5789    private final boolean removeProcessLocked(ProcessRecord app,
5790            boolean callerWillRestart, boolean allowRestart, String reason) {
5791        final String name = app.processName;
5792        final int uid = app.uid;
5793        if (DEBUG_PROCESSES) Slog.d(
5794            TAG, "Force removing proc " + app.toShortString() + " (" + name
5795            + "/" + uid + ")");
5796
5797        mProcessNames.remove(name, uid);
5798        mIsolatedProcesses.remove(app.uid);
5799        if (mHeavyWeightProcess == app) {
5800            mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
5801                    mHeavyWeightProcess.userId, 0));
5802            mHeavyWeightProcess = null;
5803        }
5804        boolean needRestart = false;
5805        if (app.pid > 0 && app.pid != MY_PID) {
5806            int pid = app.pid;
5807            synchronized (mPidsSelfLocked) {
5808                mPidsSelfLocked.remove(pid);
5809                mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
5810            }
5811            mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
5812            if (app.isolated) {
5813                mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
5814            }
5815            app.kill(reason, true);
5816            handleAppDiedLocked(app, true, allowRestart);
5817            removeLruProcessLocked(app);
5818
5819            if (app.persistent && !app.isolated) {
5820                if (!callerWillRestart) {
5821                    addAppLocked(app.info, false, null /* ABI override */);
5822                } else {
5823                    needRestart = true;
5824                }
5825            }
5826        } else {
5827            mRemovedProcesses.add(app);
5828        }
5829
5830        return needRestart;
5831    }
5832
5833    private final void processStartTimedOutLocked(ProcessRecord app) {
5834        final int pid = app.pid;
5835        boolean gone = false;
5836        synchronized (mPidsSelfLocked) {
5837            ProcessRecord knownApp = mPidsSelfLocked.get(pid);
5838            if (knownApp != null && knownApp.thread == null) {
5839                mPidsSelfLocked.remove(pid);
5840                gone = true;
5841            }
5842        }
5843
5844        if (gone) {
5845            Slog.w(TAG, "Process " + app + " failed to attach");
5846            EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, app.userId,
5847                    pid, app.uid, app.processName);
5848            mProcessNames.remove(app.processName, app.uid);
5849            mIsolatedProcesses.remove(app.uid);
5850            if (mHeavyWeightProcess == app) {
5851                mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
5852                        mHeavyWeightProcess.userId, 0));
5853                mHeavyWeightProcess = null;
5854            }
5855            mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
5856            if (app.isolated) {
5857                mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
5858            }
5859            // Take care of any launching providers waiting for this process.
5860            checkAppInLaunchingProvidersLocked(app, true);
5861            // Take care of any services that are waiting for the process.
5862            mServices.processStartTimedOutLocked(app);
5863            app.kill("start timeout", true);
5864            if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
5865                Slog.w(TAG, "Unattached app died before backup, skipping");
5866                try {
5867                    IBackupManager bm = IBackupManager.Stub.asInterface(
5868                            ServiceManager.getService(Context.BACKUP_SERVICE));
5869                    bm.agentDisconnected(app.info.packageName);
5870                } catch (RemoteException e) {
5871                    // Can't happen; the backup manager is local
5872                }
5873            }
5874            if (isPendingBroadcastProcessLocked(pid)) {
5875                Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
5876                skipPendingBroadcastLocked(pid);
5877            }
5878        } else {
5879            Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
5880        }
5881    }
5882
5883    private final boolean attachApplicationLocked(IApplicationThread thread,
5884            int pid) {
5885
5886        // Find the application record that is being attached...  either via
5887        // the pid if we are running in multiple processes, or just pull the
5888        // next app record if we are emulating process with anonymous threads.
5889        ProcessRecord app;
5890        if (pid != MY_PID && pid >= 0) {
5891            synchronized (mPidsSelfLocked) {
5892                app = mPidsSelfLocked.get(pid);
5893            }
5894        } else {
5895            app = null;
5896        }
5897
5898        if (app == null) {
5899            Slog.w(TAG, "No pending application record for pid " + pid
5900                    + " (IApplicationThread " + thread + "); dropping process");
5901            EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
5902            if (pid > 0 && pid != MY_PID) {
5903                Process.killProcessQuiet(pid);
5904                //TODO: Process.killProcessGroup(app.info.uid, pid);
5905            } else {
5906                try {
5907                    thread.scheduleExit();
5908                } catch (Exception e) {
5909                    // Ignore exceptions.
5910                }
5911            }
5912            return false;
5913        }
5914
5915        // If this application record is still attached to a previous
5916        // process, clean it up now.
5917        if (app.thread != null) {
5918            handleAppDiedLocked(app, true, true);
5919        }
5920
5921        // Tell the process all about itself.
5922
5923        if (localLOGV) Slog.v(
5924                TAG, "Binding process pid " + pid + " to record " + app);
5925
5926        final String processName = app.processName;
5927        try {
5928            AppDeathRecipient adr = new AppDeathRecipient(
5929                    app, pid, thread);
5930            thread.asBinder().linkToDeath(adr, 0);
5931            app.deathRecipient = adr;
5932        } catch (RemoteException e) {
5933            app.resetPackageList(mProcessStats);
5934            startProcessLocked(app, "link fail", processName);
5935            return false;
5936        }
5937
5938        EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.userId, app.pid, app.processName);
5939
5940        app.makeActive(thread, mProcessStats);
5941        app.curAdj = app.setAdj = -100;
5942        app.curSchedGroup = app.setSchedGroup = Process.THREAD_GROUP_DEFAULT;
5943        app.forcingToForeground = null;
5944        updateProcessForegroundLocked(app, false, false);
5945        app.hasShownUi = false;
5946        app.debugging = false;
5947        app.cached = false;
5948
5949        mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
5950
5951        boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
5952        List<ProviderInfo> providers = normalMode ? generateApplicationProvidersLocked(app) : null;
5953
5954        if (!normalMode) {
5955            Slog.i(TAG, "Launching preboot mode app: " + app);
5956        }
5957
5958        if (localLOGV) Slog.v(
5959            TAG, "New app record " + app
5960            + " thread=" + thread.asBinder() + " pid=" + pid);
5961        try {
5962            int testMode = IApplicationThread.DEBUG_OFF;
5963            if (mDebugApp != null && mDebugApp.equals(processName)) {
5964                testMode = mWaitForDebugger
5965                    ? IApplicationThread.DEBUG_WAIT
5966                    : IApplicationThread.DEBUG_ON;
5967                app.debugging = true;
5968                if (mDebugTransient) {
5969                    mDebugApp = mOrigDebugApp;
5970                    mWaitForDebugger = mOrigWaitForDebugger;
5971                }
5972            }
5973            String profileFile = app.instrumentationProfileFile;
5974            ParcelFileDescriptor profileFd = null;
5975            int samplingInterval = 0;
5976            boolean profileAutoStop = false;
5977            if (mProfileApp != null && mProfileApp.equals(processName)) {
5978                mProfileProc = app;
5979                profileFile = mProfileFile;
5980                profileFd = mProfileFd;
5981                samplingInterval = mSamplingInterval;
5982                profileAutoStop = mAutoStopProfiler;
5983            }
5984            boolean enableOpenGlTrace = false;
5985            if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) {
5986                enableOpenGlTrace = true;
5987                mOpenGlTraceApp = null;
5988            }
5989
5990            // If the app is being launched for restore or full backup, set it up specially
5991            boolean isRestrictedBackupMode = false;
5992            if (mBackupTarget != null && mBackupAppName.equals(processName)) {
5993                isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
5994                        || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL)
5995                        || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
5996            }
5997
5998            ensurePackageDexOpt(app.instrumentationInfo != null
5999                    ? app.instrumentationInfo.packageName
6000                    : app.info.packageName);
6001            if (app.instrumentationClass != null) {
6002                ensurePackageDexOpt(app.instrumentationClass.getPackageName());
6003            }
6004            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
6005                    + processName + " with config " + mConfiguration);
6006            ApplicationInfo appInfo = app.instrumentationInfo != null
6007                    ? app.instrumentationInfo : app.info;
6008            app.compat = compatibilityInfoForPackageLocked(appInfo);
6009            if (profileFd != null) {
6010                profileFd = profileFd.dup();
6011            }
6012            ProfilerInfo profilerInfo = profileFile == null ? null
6013                    : new ProfilerInfo(profileFile, profileFd, samplingInterval, profileAutoStop);
6014            thread.bindApplication(processName, appInfo, providers, app.instrumentationClass,
6015                    profilerInfo, app.instrumentationArguments, app.instrumentationWatcher,
6016                    app.instrumentationUiAutomationConnection, testMode, enableOpenGlTrace,
6017                    isRestrictedBackupMode || !normalMode, app.persistent,
6018                    new Configuration(mConfiguration), app.compat, getCommonServicesLocked(),
6019                    mCoreSettingsObserver.getCoreSettingsLocked());
6020            updateLruProcessLocked(app, false, null);
6021            app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
6022        } catch (Exception e) {
6023            // todo: Yikes!  What should we do?  For now we will try to
6024            // start another process, but that could easily get us in
6025            // an infinite loop of restarting processes...
6026            Slog.w(TAG, "Exception thrown during bind!", e);
6027
6028            app.resetPackageList(mProcessStats);
6029            app.unlinkDeathRecipient();
6030            startProcessLocked(app, "bind fail", processName);
6031            return false;
6032        }
6033
6034        // Remove this record from the list of starting applications.
6035        mPersistentStartingProcesses.remove(app);
6036        if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
6037                "Attach application locked removing on hold: " + app);
6038        mProcessesOnHold.remove(app);
6039
6040        boolean badApp = false;
6041        boolean didSomething = false;
6042
6043        // See if the top visible activity is waiting to run in this process...
6044        if (normalMode) {
6045            try {
6046                if (mStackSupervisor.attachApplicationLocked(app)) {
6047                    didSomething = true;
6048                }
6049            } catch (Exception e) {
6050                badApp = true;
6051            }
6052        }
6053
6054        // Find any services that should be running in this process...
6055        if (!badApp) {
6056            try {
6057                didSomething |= mServices.attachApplicationLocked(app, processName);
6058            } catch (Exception e) {
6059                badApp = true;
6060            }
6061        }
6062
6063        // Check if a next-broadcast receiver is in this process...
6064        if (!badApp && isPendingBroadcastProcessLocked(pid)) {
6065            try {
6066                didSomething |= sendPendingBroadcastsLocked(app);
6067            } catch (Exception e) {
6068                // If the app died trying to launch the receiver we declare it 'bad'
6069                badApp = true;
6070            }
6071        }
6072
6073        // Check whether the next backup agent is in this process...
6074        if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) {
6075            if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
6076            ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
6077            try {
6078                thread.scheduleCreateBackupAgent(mBackupTarget.appInfo,
6079                        compatibilityInfoForPackageLocked(mBackupTarget.appInfo),
6080                        mBackupTarget.backupMode);
6081            } catch (Exception e) {
6082                Slog.w(TAG, "Exception scheduling backup agent creation: ");
6083                e.printStackTrace();
6084            }
6085        }
6086
6087        if (badApp) {
6088            // todo: Also need to kill application to deal with all
6089            // kinds of exceptions.
6090            handleAppDiedLocked(app, false, true);
6091            return false;
6092        }
6093
6094        if (!didSomething) {
6095            updateOomAdjLocked();
6096        }
6097
6098        return true;
6099    }
6100
6101    @Override
6102    public final void attachApplication(IApplicationThread thread) {
6103        synchronized (this) {
6104            int callingPid = Binder.getCallingPid();
6105            final long origId = Binder.clearCallingIdentity();
6106            attachApplicationLocked(thread, callingPid);
6107            Binder.restoreCallingIdentity(origId);
6108        }
6109    }
6110
6111    @Override
6112    public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) {
6113        final long origId = Binder.clearCallingIdentity();
6114        synchronized (this) {
6115            ActivityStack stack = ActivityRecord.getStackLocked(token);
6116            if (stack != null) {
6117                ActivityRecord r =
6118                        mStackSupervisor.activityIdleInternalLocked(token, false, config);
6119                if (stopProfiling) {
6120                    if ((mProfileProc == r.app) && (mProfileFd != null)) {
6121                        try {
6122                            mProfileFd.close();
6123                        } catch (IOException e) {
6124                        }
6125                        clearProfilerLocked();
6126                    }
6127                }
6128            }
6129        }
6130        Binder.restoreCallingIdentity(origId);
6131    }
6132
6133    void postEnableScreenAfterBootLocked() {
6134        mHandler.sendEmptyMessage(ENABLE_SCREEN_AFTER_BOOT_MSG);
6135    }
6136
6137    void enableScreenAfterBoot() {
6138        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
6139                SystemClock.uptimeMillis());
6140        mWindowManager.enableScreenAfterBoot();
6141
6142        synchronized (this) {
6143            updateEventDispatchingLocked();
6144        }
6145    }
6146
6147    @Override
6148    public void showBootMessage(final CharSequence msg, final boolean always) {
6149        enforceNotIsolatedCaller("showBootMessage");
6150        mWindowManager.showBootMessage(msg, always);
6151    }
6152
6153    @Override
6154    public void keyguardWaitingForActivityDrawn() {
6155        enforceNotIsolatedCaller("keyguardWaitingForActivityDrawn");
6156        final long token = Binder.clearCallingIdentity();
6157        try {
6158            synchronized (this) {
6159                if (DEBUG_LOCKSCREEN) logLockScreen("");
6160                mWindowManager.keyguardWaitingForActivityDrawn();
6161            }
6162        } finally {
6163            Binder.restoreCallingIdentity(token);
6164        }
6165    }
6166
6167    final void finishBooting() {
6168        // Register receivers to handle package update events
6169        mPackageMonitor.register(mContext, Looper.getMainLooper(), false);
6170
6171        // Let system services know.
6172        mSystemServiceManager.startBootPhase(SystemService.PHASE_BOOT_COMPLETED);
6173
6174        synchronized (this) {
6175            // Ensure that any processes we had put on hold are now started
6176            // up.
6177            final int NP = mProcessesOnHold.size();
6178            if (NP > 0) {
6179                ArrayList<ProcessRecord> procs =
6180                    new ArrayList<ProcessRecord>(mProcessesOnHold);
6181                for (int ip=0; ip<NP; ip++) {
6182                    if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: "
6183                            + procs.get(ip));
6184                    startProcessLocked(procs.get(ip), "on-hold", null);
6185                }
6186            }
6187
6188            if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
6189                // Start looking for apps that are abusing wake locks.
6190                Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
6191                mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
6192                // Tell anyone interested that we are done booting!
6193                SystemProperties.set("sys.boot_completed", "1");
6194                SystemProperties.set("dev.bootcomplete", "1");
6195                for (int i=0; i<mStartedUsers.size(); i++) {
6196                    UserStartedState uss = mStartedUsers.valueAt(i);
6197                    if (uss.mState == UserStartedState.STATE_BOOTING) {
6198                        uss.mState = UserStartedState.STATE_RUNNING;
6199                        final int userId = mStartedUsers.keyAt(i);
6200                        Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null);
6201                        intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
6202                        intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT);
6203                        broadcastIntentLocked(null, null, intent, null,
6204                                new IIntentReceiver.Stub() {
6205                                    @Override
6206                                    public void performReceive(Intent intent, int resultCode,
6207                                            String data, Bundle extras, boolean ordered,
6208                                            boolean sticky, int sendingUser) {
6209                                        synchronized (ActivityManagerService.this) {
6210                                            requestPssAllProcsLocked(SystemClock.uptimeMillis(),
6211                                                    true, false);
6212                                        }
6213                                    }
6214                                },
6215                                0, null, null,
6216                                android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
6217                                AppOpsManager.OP_NONE, true, false, MY_PID, Process.SYSTEM_UID,
6218                                userId);
6219                    }
6220                }
6221                scheduleStartProfilesLocked();
6222            }
6223        }
6224    }
6225
6226    final void ensureBootCompleted() {
6227        boolean booting;
6228        boolean enableScreen;
6229        synchronized (this) {
6230            booting = mBooting;
6231            mBooting = false;
6232            enableScreen = !mBooted;
6233            mBooted = true;
6234        }
6235
6236        if (booting) {
6237            finishBooting();
6238        }
6239
6240        if (enableScreen) {
6241            enableScreenAfterBoot();
6242        }
6243    }
6244
6245    @Override
6246    public final void activityResumed(IBinder token) {
6247        final long origId = Binder.clearCallingIdentity();
6248        synchronized(this) {
6249            ActivityStack stack = ActivityRecord.getStackLocked(token);
6250            if (stack != null) {
6251                ActivityRecord.activityResumedLocked(token);
6252            }
6253        }
6254        Binder.restoreCallingIdentity(origId);
6255    }
6256
6257    @Override
6258    public final void activityPaused(IBinder token, PersistableBundle persistentState) {
6259        final long origId = Binder.clearCallingIdentity();
6260        synchronized(this) {
6261            ActivityStack stack = ActivityRecord.getStackLocked(token);
6262            if (stack != null) {
6263                stack.activityPausedLocked(token, false, persistentState);
6264            }
6265        }
6266        Binder.restoreCallingIdentity(origId);
6267    }
6268
6269    @Override
6270    public final void activityStopped(IBinder token, Bundle icicle,
6271            PersistableBundle persistentState, CharSequence description) {
6272        if (localLOGV) Slog.v(TAG, "Activity stopped: token=" + token);
6273
6274        // Refuse possible leaked file descriptors
6275        if (icicle != null && icicle.hasFileDescriptors()) {
6276            throw new IllegalArgumentException("File descriptors passed in Bundle");
6277        }
6278
6279        final long origId = Binder.clearCallingIdentity();
6280
6281        synchronized (this) {
6282            ActivityRecord r = ActivityRecord.isInStackLocked(token);
6283            if (r != null) {
6284                r.task.stack.activityStoppedLocked(r, icicle, persistentState, description);
6285            }
6286        }
6287
6288        trimApplications();
6289
6290        Binder.restoreCallingIdentity(origId);
6291    }
6292
6293    @Override
6294    public final void activityDestroyed(IBinder token) {
6295        if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
6296        synchronized (this) {
6297            ActivityStack stack = ActivityRecord.getStackLocked(token);
6298            if (stack != null) {
6299                stack.activityDestroyedLocked(token);
6300            }
6301        }
6302    }
6303
6304    @Override
6305    public final void backgroundResourcesReleased(IBinder token) {
6306        final long origId = Binder.clearCallingIdentity();
6307        try {
6308            synchronized (this) {
6309                ActivityStack stack = ActivityRecord.getStackLocked(token);
6310                if (stack != null) {
6311                    stack.backgroundResourcesReleased(token);
6312                }
6313            }
6314        } finally {
6315            Binder.restoreCallingIdentity(origId);
6316        }
6317    }
6318
6319    @Override
6320    public final void notifyLaunchTaskBehindComplete(IBinder token) {
6321        mStackSupervisor.scheduleLaunchTaskBehindComplete(token);
6322    }
6323
6324    @Override
6325    public final void notifyEnterAnimationComplete(IBinder token) {
6326        mHandler.sendMessage(mHandler.obtainMessage(ENTER_ANIMATION_COMPLETE_MSG, token));
6327    }
6328
6329    @Override
6330    public String getCallingPackage(IBinder token) {
6331        synchronized (this) {
6332            ActivityRecord r = getCallingRecordLocked(token);
6333            return r != null ? r.info.packageName : null;
6334        }
6335    }
6336
6337    @Override
6338    public ComponentName getCallingActivity(IBinder token) {
6339        synchronized (this) {
6340            ActivityRecord r = getCallingRecordLocked(token);
6341            return r != null ? r.intent.getComponent() : null;
6342        }
6343    }
6344
6345    private ActivityRecord getCallingRecordLocked(IBinder token) {
6346        ActivityRecord r = ActivityRecord.isInStackLocked(token);
6347        if (r == null) {
6348            return null;
6349        }
6350        return r.resultTo;
6351    }
6352
6353    @Override
6354    public ComponentName getActivityClassForToken(IBinder token) {
6355        synchronized(this) {
6356            ActivityRecord r = ActivityRecord.isInStackLocked(token);
6357            if (r == null) {
6358                return null;
6359            }
6360            return r.intent.getComponent();
6361        }
6362    }
6363
6364    @Override
6365    public String getPackageForToken(IBinder token) {
6366        synchronized(this) {
6367            ActivityRecord r = ActivityRecord.isInStackLocked(token);
6368            if (r == null) {
6369                return null;
6370            }
6371            return r.packageName;
6372        }
6373    }
6374
6375    @Override
6376    public IIntentSender getIntentSender(int type,
6377            String packageName, IBinder token, String resultWho,
6378            int requestCode, Intent[] intents, String[] resolvedTypes,
6379            int flags, Bundle options, int userId) {
6380        enforceNotIsolatedCaller("getIntentSender");
6381        // Refuse possible leaked file descriptors
6382        if (intents != null) {
6383            if (intents.length < 1) {
6384                throw new IllegalArgumentException("Intents array length must be >= 1");
6385            }
6386            for (int i=0; i<intents.length; i++) {
6387                Intent intent = intents[i];
6388                if (intent != null) {
6389                    if (intent.hasFileDescriptors()) {
6390                        throw new IllegalArgumentException("File descriptors passed in Intent");
6391                    }
6392                    if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
6393                            (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
6394                        throw new IllegalArgumentException(
6395                                "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
6396                    }
6397                    intents[i] = new Intent(intent);
6398                }
6399            }
6400            if (resolvedTypes != null && resolvedTypes.length != intents.length) {
6401                throw new IllegalArgumentException(
6402                        "Intent array length does not match resolvedTypes length");
6403            }
6404        }
6405        if (options != null) {
6406            if (options.hasFileDescriptors()) {
6407                throw new IllegalArgumentException("File descriptors passed in options");
6408            }
6409        }
6410
6411        synchronized(this) {
6412            int callingUid = Binder.getCallingUid();
6413            int origUserId = userId;
6414            userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId,
6415                    type == ActivityManager.INTENT_SENDER_BROADCAST,
6416                    ALLOW_NON_FULL, "getIntentSender", null);
6417            if (origUserId == UserHandle.USER_CURRENT) {
6418                // We don't want to evaluate this until the pending intent is
6419                // actually executed.  However, we do want to always do the
6420                // security checking for it above.
6421                userId = UserHandle.USER_CURRENT;
6422            }
6423            try {
6424                if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
6425                    int uid = AppGlobals.getPackageManager()
6426                            .getPackageUid(packageName, UserHandle.getUserId(callingUid));
6427                    if (!UserHandle.isSameApp(callingUid, uid)) {
6428                        String msg = "Permission Denial: getIntentSender() from pid="
6429                            + Binder.getCallingPid()
6430                            + ", uid=" + Binder.getCallingUid()
6431                            + ", (need uid=" + uid + ")"
6432                            + " is not allowed to send as package " + packageName;
6433                        Slog.w(TAG, msg);
6434                        throw new SecurityException(msg);
6435                    }
6436                }
6437
6438                return getIntentSenderLocked(type, packageName, callingUid, userId,
6439                        token, resultWho, requestCode, intents, resolvedTypes, flags, options);
6440
6441            } catch (RemoteException e) {
6442                throw new SecurityException(e);
6443            }
6444        }
6445    }
6446
6447    IIntentSender getIntentSenderLocked(int type, String packageName,
6448            int callingUid, int userId, IBinder token, String resultWho,
6449            int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
6450            Bundle options) {
6451        if (DEBUG_MU)
6452            Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid);
6453        ActivityRecord activity = null;
6454        if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
6455            activity = ActivityRecord.isInStackLocked(token);
6456            if (activity == null) {
6457                return null;
6458            }
6459            if (activity.finishing) {
6460                return null;
6461            }
6462        }
6463
6464        final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
6465        final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
6466        final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
6467        flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
6468                |PendingIntent.FLAG_UPDATE_CURRENT);
6469
6470        PendingIntentRecord.Key key = new PendingIntentRecord.Key(
6471                type, packageName, activity, resultWho,
6472                requestCode, intents, resolvedTypes, flags, options, userId);
6473        WeakReference<PendingIntentRecord> ref;
6474        ref = mIntentSenderRecords.get(key);
6475        PendingIntentRecord rec = ref != null ? ref.get() : null;
6476        if (rec != null) {
6477            if (!cancelCurrent) {
6478                if (updateCurrent) {
6479                    if (rec.key.requestIntent != null) {
6480                        rec.key.requestIntent.replaceExtras(intents != null ?
6481                                intents[intents.length - 1] : null);
6482                    }
6483                    if (intents != null) {
6484                        intents[intents.length-1] = rec.key.requestIntent;
6485                        rec.key.allIntents = intents;
6486                        rec.key.allResolvedTypes = resolvedTypes;
6487                    } else {
6488                        rec.key.allIntents = null;
6489                        rec.key.allResolvedTypes = null;
6490                    }
6491                }
6492                return rec;
6493            }
6494            rec.canceled = true;
6495            mIntentSenderRecords.remove(key);
6496        }
6497        if (noCreate) {
6498            return rec;
6499        }
6500        rec = new PendingIntentRecord(this, key, callingUid);
6501        mIntentSenderRecords.put(key, rec.ref);
6502        if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
6503            if (activity.pendingResults == null) {
6504                activity.pendingResults
6505                        = new HashSet<WeakReference<PendingIntentRecord>>();
6506            }
6507            activity.pendingResults.add(rec.ref);
6508        }
6509        return rec;
6510    }
6511
6512    @Override
6513    public void cancelIntentSender(IIntentSender sender) {
6514        if (!(sender instanceof PendingIntentRecord)) {
6515            return;
6516        }
6517        synchronized(this) {
6518            PendingIntentRecord rec = (PendingIntentRecord)sender;
6519            try {
6520                int uid = AppGlobals.getPackageManager()
6521                        .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId());
6522                if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) {
6523                    String msg = "Permission Denial: cancelIntentSender() from pid="
6524                        + Binder.getCallingPid()
6525                        + ", uid=" + Binder.getCallingUid()
6526                        + " is not allowed to cancel packges "
6527                        + rec.key.packageName;
6528                    Slog.w(TAG, msg);
6529                    throw new SecurityException(msg);
6530                }
6531            } catch (RemoteException e) {
6532                throw new SecurityException(e);
6533            }
6534            cancelIntentSenderLocked(rec, true);
6535        }
6536    }
6537
6538    void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
6539        rec.canceled = true;
6540        mIntentSenderRecords.remove(rec.key);
6541        if (cleanActivity && rec.key.activity != null) {
6542            rec.key.activity.pendingResults.remove(rec.ref);
6543        }
6544    }
6545
6546    @Override
6547    public String getPackageForIntentSender(IIntentSender pendingResult) {
6548        if (!(pendingResult instanceof PendingIntentRecord)) {
6549            return null;
6550        }
6551        try {
6552            PendingIntentRecord res = (PendingIntentRecord)pendingResult;
6553            return res.key.packageName;
6554        } catch (ClassCastException e) {
6555        }
6556        return null;
6557    }
6558
6559    @Override
6560    public int getUidForIntentSender(IIntentSender sender) {
6561        if (sender instanceof PendingIntentRecord) {
6562            try {
6563                PendingIntentRecord res = (PendingIntentRecord)sender;
6564                return res.uid;
6565            } catch (ClassCastException e) {
6566            }
6567        }
6568        return -1;
6569    }
6570
6571    @Override
6572    public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) {
6573        if (!(pendingResult instanceof PendingIntentRecord)) {
6574            return false;
6575        }
6576        try {
6577            PendingIntentRecord res = (PendingIntentRecord)pendingResult;
6578            if (res.key.allIntents == null) {
6579                return false;
6580            }
6581            for (int i=0; i<res.key.allIntents.length; i++) {
6582                Intent intent = res.key.allIntents[i];
6583                if (intent.getPackage() != null && intent.getComponent() != null) {
6584                    return false;
6585                }
6586            }
6587            return true;
6588        } catch (ClassCastException e) {
6589        }
6590        return false;
6591    }
6592
6593    @Override
6594    public boolean isIntentSenderAnActivity(IIntentSender pendingResult) {
6595        if (!(pendingResult instanceof PendingIntentRecord)) {
6596            return false;
6597        }
6598        try {
6599            PendingIntentRecord res = (PendingIntentRecord)pendingResult;
6600            if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) {
6601                return true;
6602            }
6603            return false;
6604        } catch (ClassCastException e) {
6605        }
6606        return false;
6607    }
6608
6609    @Override
6610    public Intent getIntentForIntentSender(IIntentSender pendingResult) {
6611        if (!(pendingResult instanceof PendingIntentRecord)) {
6612            return null;
6613        }
6614        try {
6615            PendingIntentRecord res = (PendingIntentRecord)pendingResult;
6616            return res.key.requestIntent != null ? new Intent(res.key.requestIntent) : null;
6617        } catch (ClassCastException e) {
6618        }
6619        return null;
6620    }
6621
6622    @Override
6623    public String getTagForIntentSender(IIntentSender pendingResult, String prefix) {
6624        if (!(pendingResult instanceof PendingIntentRecord)) {
6625            return null;
6626        }
6627        try {
6628            PendingIntentRecord res = (PendingIntentRecord)pendingResult;
6629            Intent intent = res.key.requestIntent;
6630            if (intent != null) {
6631                if (res.lastTag != null && res.lastTagPrefix == prefix && (res.lastTagPrefix == null
6632                        || res.lastTagPrefix.equals(prefix))) {
6633                    return res.lastTag;
6634                }
6635                res.lastTagPrefix = prefix;
6636                StringBuilder sb = new StringBuilder(128);
6637                if (prefix != null) {
6638                    sb.append(prefix);
6639                }
6640                if (intent.getAction() != null) {
6641                    sb.append(intent.getAction());
6642                } else if (intent.getComponent() != null) {
6643                    intent.getComponent().appendShortString(sb);
6644                } else {
6645                    sb.append("?");
6646                }
6647                return res.lastTag = sb.toString();
6648            }
6649        } catch (ClassCastException e) {
6650        }
6651        return null;
6652    }
6653
6654    @Override
6655    public void setProcessLimit(int max) {
6656        enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
6657                "setProcessLimit()");
6658        synchronized (this) {
6659            mProcessLimit = max < 0 ? ProcessList.MAX_CACHED_APPS : max;
6660            mProcessLimitOverride = max;
6661        }
6662        trimApplications();
6663    }
6664
6665    @Override
6666    public int getProcessLimit() {
6667        synchronized (this) {
6668            return mProcessLimitOverride;
6669        }
6670    }
6671
6672    void foregroundTokenDied(ForegroundToken token) {
6673        synchronized (ActivityManagerService.this) {
6674            synchronized (mPidsSelfLocked) {
6675                ForegroundToken cur
6676                    = mForegroundProcesses.get(token.pid);
6677                if (cur != token) {
6678                    return;
6679                }
6680                mForegroundProcesses.remove(token.pid);
6681                ProcessRecord pr = mPidsSelfLocked.get(token.pid);
6682                if (pr == null) {
6683                    return;
6684                }
6685                pr.forcingToForeground = null;
6686                updateProcessForegroundLocked(pr, false, false);
6687            }
6688            updateOomAdjLocked();
6689        }
6690    }
6691
6692    @Override
6693    public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
6694        enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
6695                "setProcessForeground()");
6696        synchronized(this) {
6697            boolean changed = false;
6698
6699            synchronized (mPidsSelfLocked) {
6700                ProcessRecord pr = mPidsSelfLocked.get(pid);
6701                if (pr == null && isForeground) {
6702                    Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
6703                    return;
6704                }
6705                ForegroundToken oldToken = mForegroundProcesses.get(pid);
6706                if (oldToken != null) {
6707                    oldToken.token.unlinkToDeath(oldToken, 0);
6708                    mForegroundProcesses.remove(pid);
6709                    if (pr != null) {
6710                        pr.forcingToForeground = null;
6711                    }
6712                    changed = true;
6713                }
6714                if (isForeground && token != null) {
6715                    ForegroundToken newToken = new ForegroundToken() {
6716                        @Override
6717                        public void binderDied() {
6718                            foregroundTokenDied(this);
6719                        }
6720                    };
6721                    newToken.pid = pid;
6722                    newToken.token = token;
6723                    try {
6724                        token.linkToDeath(newToken, 0);
6725                        mForegroundProcesses.put(pid, newToken);
6726                        pr.forcingToForeground = token;
6727                        changed = true;
6728                    } catch (RemoteException e) {
6729                        // If the process died while doing this, we will later
6730                        // do the cleanup with the process death link.
6731                    }
6732                }
6733            }
6734
6735            if (changed) {
6736                updateOomAdjLocked();
6737            }
6738        }
6739    }
6740
6741    // =========================================================
6742    // PERMISSIONS
6743    // =========================================================
6744
6745    static class PermissionController extends IPermissionController.Stub {
6746        ActivityManagerService mActivityManagerService;
6747        PermissionController(ActivityManagerService activityManagerService) {
6748            mActivityManagerService = activityManagerService;
6749        }
6750
6751        @Override
6752        public boolean checkPermission(String permission, int pid, int uid) {
6753            return mActivityManagerService.checkPermission(permission, pid,
6754                    uid) == PackageManager.PERMISSION_GRANTED;
6755        }
6756    }
6757
6758    class IntentFirewallInterface implements IntentFirewall.AMSInterface {
6759        @Override
6760        public int checkComponentPermission(String permission, int pid, int uid,
6761                int owningUid, boolean exported) {
6762            return ActivityManagerService.this.checkComponentPermission(permission, pid, uid,
6763                    owningUid, exported);
6764        }
6765
6766        @Override
6767        public Object getAMSLock() {
6768            return ActivityManagerService.this;
6769        }
6770    }
6771
6772    /**
6773     * This can be called with or without the global lock held.
6774     */
6775    int checkComponentPermission(String permission, int pid, int uid,
6776            int owningUid, boolean exported) {
6777        // We might be performing an operation on behalf of an indirect binder
6778        // invocation, e.g. via {@link #openContentUri}.  Check and adjust the
6779        // client identity accordingly before proceeding.
6780        Identity tlsIdentity = sCallerIdentity.get();
6781        if (tlsIdentity != null) {
6782            Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
6783                    + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
6784            uid = tlsIdentity.uid;
6785            pid = tlsIdentity.pid;
6786        }
6787
6788        if (pid == MY_PID) {
6789            return PackageManager.PERMISSION_GRANTED;
6790        }
6791
6792        return ActivityManager.checkComponentPermission(permission, uid,
6793                owningUid, exported);
6794    }
6795
6796    /**
6797     * As the only public entry point for permissions checking, this method
6798     * can enforce the semantic that requesting a check on a null global
6799     * permission is automatically denied.  (Internally a null permission
6800     * string is used when calling {@link #checkComponentPermission} in cases
6801     * when only uid-based security is needed.)
6802     *
6803     * This can be called with or without the global lock held.
6804     */
6805    @Override
6806    public int checkPermission(String permission, int pid, int uid) {
6807        if (permission == null) {
6808            return PackageManager.PERMISSION_DENIED;
6809        }
6810        return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true);
6811    }
6812
6813    /**
6814     * Binder IPC calls go through the public entry point.
6815     * This can be called with or without the global lock held.
6816     */
6817    int checkCallingPermission(String permission) {
6818        return checkPermission(permission,
6819                Binder.getCallingPid(),
6820                UserHandle.getAppId(Binder.getCallingUid()));
6821    }
6822
6823    /**
6824     * This can be called with or without the global lock held.
6825     */
6826    void enforceCallingPermission(String permission, String func) {
6827        if (checkCallingPermission(permission)
6828                == PackageManager.PERMISSION_GRANTED) {
6829            return;
6830        }
6831
6832        String msg = "Permission Denial: " + func + " from pid="
6833                + Binder.getCallingPid()
6834                + ", uid=" + Binder.getCallingUid()
6835                + " requires " + permission;
6836        Slog.w(TAG, msg);
6837        throw new SecurityException(msg);
6838    }
6839
6840    /**
6841     * Determine if UID is holding permissions required to access {@link Uri} in
6842     * the given {@link ProviderInfo}. Final permission checking is always done
6843     * in {@link ContentProvider}.
6844     */
6845    private final boolean checkHoldingPermissionsLocked(
6846            IPackageManager pm, ProviderInfo pi, GrantUri grantUri, int uid, final int modeFlags) {
6847        if (DEBUG_URI_PERMISSION) Slog.v(TAG,
6848                "checkHoldingPermissionsLocked: uri=" + grantUri + " uid=" + uid);
6849        if (UserHandle.getUserId(uid) != grantUri.sourceUserId) {
6850            if (ActivityManager.checkComponentPermission(INTERACT_ACROSS_USERS, uid, -1, true)
6851                    != PERMISSION_GRANTED) {
6852                return false;
6853            }
6854        }
6855        return checkHoldingPermissionsInternalLocked(pm, pi, grantUri, uid, modeFlags, true);
6856    }
6857
6858    private final boolean checkHoldingPermissionsInternalLocked(IPackageManager pm, ProviderInfo pi,
6859            GrantUri grantUri, int uid, final int modeFlags, boolean considerUidPermissions) {
6860        if (pi.applicationInfo.uid == uid) {
6861            return true;
6862        } else if (!pi.exported) {
6863            return false;
6864        }
6865
6866        boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0;
6867        boolean writeMet = (modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
6868        try {
6869            // check if target holds top-level <provider> permissions
6870            if (!readMet && pi.readPermission != null && considerUidPermissions
6871                    && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) {
6872                readMet = true;
6873            }
6874            if (!writeMet && pi.writePermission != null && considerUidPermissions
6875                    && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) {
6876                writeMet = true;
6877            }
6878
6879            // track if unprotected read/write is allowed; any denied
6880            // <path-permission> below removes this ability
6881            boolean allowDefaultRead = pi.readPermission == null;
6882            boolean allowDefaultWrite = pi.writePermission == null;
6883
6884            // check if target holds any <path-permission> that match uri
6885            final PathPermission[] pps = pi.pathPermissions;
6886            if (pps != null) {
6887                final String path = grantUri.uri.getPath();
6888                int i = pps.length;
6889                while (i > 0 && (!readMet || !writeMet)) {
6890                    i--;
6891                    PathPermission pp = pps[i];
6892                    if (pp.match(path)) {
6893                        if (!readMet) {
6894                            final String pprperm = pp.getReadPermission();
6895                            if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for "
6896                                    + pprperm + " for " + pp.getPath()
6897                                    + ": match=" + pp.match(path)
6898                                    + " check=" + pm.checkUidPermission(pprperm, uid));
6899                            if (pprperm != null) {
6900                                if (considerUidPermissions && pm.checkUidPermission(pprperm, uid)
6901                                        == PERMISSION_GRANTED) {
6902                                    readMet = true;
6903                                } else {
6904                                    allowDefaultRead = false;
6905                                }
6906                            }
6907                        }
6908                        if (!writeMet) {
6909                            final String ppwperm = pp.getWritePermission();
6910                            if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm "
6911                                    + ppwperm + " for " + pp.getPath()
6912                                    + ": match=" + pp.match(path)
6913                                    + " check=" + pm.checkUidPermission(ppwperm, uid));
6914                            if (ppwperm != null) {
6915                                if (considerUidPermissions && pm.checkUidPermission(ppwperm, uid)
6916                                        == PERMISSION_GRANTED) {
6917                                    writeMet = true;
6918                                } else {
6919                                    allowDefaultWrite = false;
6920                                }
6921                            }
6922                        }
6923                    }
6924                }
6925            }
6926
6927            // grant unprotected <provider> read/write, if not blocked by
6928            // <path-permission> above
6929            if (allowDefaultRead) readMet = true;
6930            if (allowDefaultWrite) writeMet = true;
6931
6932        } catch (RemoteException e) {
6933            return false;
6934        }
6935
6936        return readMet && writeMet;
6937    }
6938
6939    private ProviderInfo getProviderInfoLocked(String authority, int userHandle) {
6940        ProviderInfo pi = null;
6941        ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userHandle);
6942        if (cpr != null) {
6943            pi = cpr.info;
6944        } else {
6945            try {
6946                pi = AppGlobals.getPackageManager().resolveContentProvider(
6947                        authority, PackageManager.GET_URI_PERMISSION_PATTERNS, userHandle);
6948            } catch (RemoteException ex) {
6949            }
6950        }
6951        return pi;
6952    }
6953
6954    private UriPermission findUriPermissionLocked(int targetUid, GrantUri grantUri) {
6955        final ArrayMap<GrantUri, UriPermission> targetUris = mGrantedUriPermissions.get(targetUid);
6956        if (targetUris != null) {
6957            return targetUris.get(grantUri);
6958        }
6959        return null;
6960    }
6961
6962    private UriPermission findOrCreateUriPermissionLocked(String sourcePkg,
6963            String targetPkg, int targetUid, GrantUri grantUri) {
6964        ArrayMap<GrantUri, UriPermission> targetUris = mGrantedUriPermissions.get(targetUid);
6965        if (targetUris == null) {
6966            targetUris = Maps.newArrayMap();
6967            mGrantedUriPermissions.put(targetUid, targetUris);
6968        }
6969
6970        UriPermission perm = targetUris.get(grantUri);
6971        if (perm == null) {
6972            perm = new UriPermission(sourcePkg, targetPkg, targetUid, grantUri);
6973            targetUris.put(grantUri, perm);
6974        }
6975
6976        return perm;
6977    }
6978
6979    private final boolean checkUriPermissionLocked(GrantUri grantUri, int uid,
6980            final int modeFlags) {
6981        final boolean persistable = (modeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0;
6982        final int minStrength = persistable ? UriPermission.STRENGTH_PERSISTABLE
6983                : UriPermission.STRENGTH_OWNED;
6984
6985        // Root gets to do everything.
6986        if (uid == 0) {
6987            return true;
6988        }
6989
6990        final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(uid);
6991        if (perms == null) return false;
6992
6993        // First look for exact match
6994        final UriPermission exactPerm = perms.get(grantUri);
6995        if (exactPerm != null && exactPerm.getStrength(modeFlags) >= minStrength) {
6996            return true;
6997        }
6998
6999        // No exact match, look for prefixes
7000        final int N = perms.size();
7001        for (int i = 0; i < N; i++) {
7002            final UriPermission perm = perms.valueAt(i);
7003            if (perm.uri.prefix && grantUri.uri.isPathPrefixMatch(perm.uri.uri)
7004                    && perm.getStrength(modeFlags) >= minStrength) {
7005                return true;
7006            }
7007        }
7008
7009        return false;
7010    }
7011
7012    /**
7013     * @param uri This uri must NOT contain an embedded userId.
7014     * @param userId The userId in which the uri is to be resolved.
7015     */
7016    @Override
7017    public int checkUriPermission(Uri uri, int pid, int uid,
7018            final int modeFlags, int userId) {
7019        enforceNotIsolatedCaller("checkUriPermission");
7020
7021        // Another redirected-binder-call permissions check as in
7022        // {@link checkComponentPermission}.
7023        Identity tlsIdentity = sCallerIdentity.get();
7024        if (tlsIdentity != null) {
7025            uid = tlsIdentity.uid;
7026            pid = tlsIdentity.pid;
7027        }
7028
7029        // Our own process gets to do everything.
7030        if (pid == MY_PID) {
7031            return PackageManager.PERMISSION_GRANTED;
7032        }
7033        synchronized (this) {
7034            return checkUriPermissionLocked(new GrantUri(userId, uri, false), uid, modeFlags)
7035                    ? PackageManager.PERMISSION_GRANTED
7036                    : PackageManager.PERMISSION_DENIED;
7037        }
7038    }
7039
7040    /**
7041     * Check if the targetPkg can be granted permission to access uri by
7042     * the callingUid using the given modeFlags.  Throws a security exception
7043     * if callingUid is not allowed to do this.  Returns the uid of the target
7044     * if the URI permission grant should be performed; returns -1 if it is not
7045     * needed (for example targetPkg already has permission to access the URI).
7046     * If you already know the uid of the target, you can supply it in
7047     * lastTargetUid else set that to -1.
7048     */
7049    int checkGrantUriPermissionLocked(int callingUid, String targetPkg, GrantUri grantUri,
7050            final int modeFlags, int lastTargetUid) {
7051        if (!Intent.isAccessUriMode(modeFlags)) {
7052            return -1;
7053        }
7054
7055        if (targetPkg != null) {
7056            if (DEBUG_URI_PERMISSION) Slog.v(TAG,
7057                    "Checking grant " + targetPkg + " permission to " + grantUri);
7058        }
7059
7060        final IPackageManager pm = AppGlobals.getPackageManager();
7061
7062        // If this is not a content: uri, we can't do anything with it.
7063        if (!ContentResolver.SCHEME_CONTENT.equals(grantUri.uri.getScheme())) {
7064            if (DEBUG_URI_PERMISSION) Slog.v(TAG,
7065                    "Can't grant URI permission for non-content URI: " + grantUri);
7066            return -1;
7067        }
7068
7069        final String authority = grantUri.uri.getAuthority();
7070        final ProviderInfo pi = getProviderInfoLocked(authority, grantUri.sourceUserId);
7071        if (pi == null) {
7072            Slog.w(TAG, "No content provider found for permission check: " +
7073                    grantUri.uri.toSafeString());
7074            return -1;
7075        }
7076
7077        int targetUid = lastTargetUid;
7078        if (targetUid < 0 && targetPkg != null) {
7079            try {
7080                targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid));
7081                if (targetUid < 0) {
7082                    if (DEBUG_URI_PERMISSION) Slog.v(TAG,
7083                            "Can't grant URI permission no uid for: " + targetPkg);
7084                    return -1;
7085                }
7086            } catch (RemoteException ex) {
7087                return -1;
7088            }
7089        }
7090
7091        if (targetUid >= 0) {
7092            // First...  does the target actually need this permission?
7093            if (checkHoldingPermissionsLocked(pm, pi, grantUri, targetUid, modeFlags)) {
7094                // No need to grant the target this permission.
7095                if (DEBUG_URI_PERMISSION) Slog.v(TAG,
7096                        "Target " + targetPkg + " already has full permission to " + grantUri);
7097                return -1;
7098            }
7099        } else {
7100            // First...  there is no target package, so can anyone access it?
7101            boolean allowed = pi.exported;
7102            if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
7103                if (pi.readPermission != null) {
7104                    allowed = false;
7105                }
7106            }
7107            if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
7108                if (pi.writePermission != null) {
7109                    allowed = false;
7110                }
7111            }
7112            if (allowed) {
7113                return -1;
7114            }
7115        }
7116
7117        /* There is a special cross user grant if:
7118         * - The target is on another user.
7119         * - Apps on the current user can access the uri without any uid permissions.
7120         * In this case, we grant a uri permission, even if the ContentProvider does not normally
7121         * grant uri permissions.
7122         */
7123        boolean specialCrossUserGrant = UserHandle.getUserId(targetUid) != grantUri.sourceUserId
7124                && checkHoldingPermissionsInternalLocked(pm, pi, grantUri, callingUid,
7125                modeFlags, false /*without considering the uid permissions*/);
7126
7127        // Second...  is the provider allowing granting of URI permissions?
7128        if (!specialCrossUserGrant) {
7129            if (!pi.grantUriPermissions) {
7130                throw new SecurityException("Provider " + pi.packageName
7131                        + "/" + pi.name
7132                        + " does not allow granting of Uri permissions (uri "
7133                        + grantUri + ")");
7134            }
7135            if (pi.uriPermissionPatterns != null) {
7136                final int N = pi.uriPermissionPatterns.length;
7137                boolean allowed = false;
7138                for (int i=0; i<N; i++) {
7139                    if (pi.uriPermissionPatterns[i] != null
7140                            && pi.uriPermissionPatterns[i].match(grantUri.uri.getPath())) {
7141                        allowed = true;
7142                        break;
7143                    }
7144                }
7145                if (!allowed) {
7146                    throw new SecurityException("Provider " + pi.packageName
7147                            + "/" + pi.name
7148                            + " does not allow granting of permission to path of Uri "
7149                            + grantUri);
7150                }
7151            }
7152        }
7153
7154        // Third...  does the caller itself have permission to access
7155        // this uri?
7156        if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) {
7157            if (!checkHoldingPermissionsLocked(pm, pi, grantUri, callingUid, modeFlags)) {
7158                // Require they hold a strong enough Uri permission
7159                if (!checkUriPermissionLocked(grantUri, callingUid, modeFlags)) {
7160                    throw new SecurityException("Uid " + callingUid
7161                            + " does not have permission to uri " + grantUri);
7162                }
7163            }
7164        }
7165        return targetUid;
7166    }
7167
7168    /**
7169     * @param uri This uri must NOT contain an embedded userId.
7170     * @param userId The userId in which the uri is to be resolved.
7171     */
7172    @Override
7173    public int checkGrantUriPermission(int callingUid, String targetPkg, Uri uri,
7174            final int modeFlags, int userId) {
7175        enforceNotIsolatedCaller("checkGrantUriPermission");
7176        synchronized(this) {
7177            return checkGrantUriPermissionLocked(callingUid, targetPkg,
7178                    new GrantUri(userId, uri, false), modeFlags, -1);
7179        }
7180    }
7181
7182    void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, GrantUri grantUri,
7183            final int modeFlags, UriPermissionOwner owner) {
7184        if (!Intent.isAccessUriMode(modeFlags)) {
7185            return;
7186        }
7187
7188        // So here we are: the caller has the assumed permission
7189        // to the uri, and the target doesn't.  Let's now give this to
7190        // the target.
7191
7192        if (DEBUG_URI_PERMISSION) Slog.v(TAG,
7193                "Granting " + targetPkg + "/" + targetUid + " permission to " + grantUri);
7194
7195        final String authority = grantUri.uri.getAuthority();
7196        final ProviderInfo pi = getProviderInfoLocked(authority, grantUri.sourceUserId);
7197        if (pi == null) {
7198            Slog.w(TAG, "No content provider found for grant: " + grantUri.toSafeString());
7199            return;
7200        }
7201
7202        if ((modeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
7203            grantUri.prefix = true;
7204        }
7205        final UriPermission perm = findOrCreateUriPermissionLocked(
7206                pi.packageName, targetPkg, targetUid, grantUri);
7207        perm.grantModes(modeFlags, owner);
7208    }
7209
7210    void grantUriPermissionLocked(int callingUid, String targetPkg, GrantUri grantUri,
7211            final int modeFlags, UriPermissionOwner owner, int targetUserId) {
7212        if (targetPkg == null) {
7213            throw new NullPointerException("targetPkg");
7214        }
7215        int targetUid;
7216        final IPackageManager pm = AppGlobals.getPackageManager();
7217        try {
7218            targetUid = pm.getPackageUid(targetPkg, targetUserId);
7219        } catch (RemoteException ex) {
7220            return;
7221        }
7222
7223        targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, grantUri, modeFlags,
7224                targetUid);
7225        if (targetUid < 0) {
7226            return;
7227        }
7228
7229        grantUriPermissionUncheckedLocked(targetUid, targetPkg, grantUri, modeFlags,
7230                owner);
7231    }
7232
7233    static class NeededUriGrants extends ArrayList<GrantUri> {
7234        final String targetPkg;
7235        final int targetUid;
7236        final int flags;
7237
7238        NeededUriGrants(String targetPkg, int targetUid, int flags) {
7239            this.targetPkg = targetPkg;
7240            this.targetUid = targetUid;
7241            this.flags = flags;
7242        }
7243    }
7244
7245    /**
7246     * Like checkGrantUriPermissionLocked, but takes an Intent.
7247     */
7248    NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid,
7249            String targetPkg, Intent intent, int mode, NeededUriGrants needed, int targetUserId) {
7250        if (DEBUG_URI_PERMISSION) Slog.v(TAG,
7251                "Checking URI perm to data=" + (intent != null ? intent.getData() : null)
7252                + " clip=" + (intent != null ? intent.getClipData() : null)
7253                + " from " + intent + "; flags=0x"
7254                + Integer.toHexString(intent != null ? intent.getFlags() : 0));
7255
7256        if (targetPkg == null) {
7257            throw new NullPointerException("targetPkg");
7258        }
7259
7260        if (intent == null) {
7261            return null;
7262        }
7263        Uri data = intent.getData();
7264        ClipData clip = intent.getClipData();
7265        if (data == null && clip == null) {
7266            return null;
7267        }
7268        // Default userId for uris in the intent (if they don't specify it themselves)
7269        int contentUserHint = intent.getContentUserHint();
7270        if (contentUserHint == UserHandle.USER_CURRENT) {
7271            contentUserHint = UserHandle.getUserId(callingUid);
7272        }
7273        final IPackageManager pm = AppGlobals.getPackageManager();
7274        int targetUid;
7275        if (needed != null) {
7276            targetUid = needed.targetUid;
7277        } else {
7278            try {
7279                targetUid = pm.getPackageUid(targetPkg, targetUserId);
7280            } catch (RemoteException ex) {
7281                return null;
7282            }
7283            if (targetUid < 0) {
7284                if (DEBUG_URI_PERMISSION) {
7285                    Slog.v(TAG, "Can't grant URI permission no uid for: " + targetPkg
7286                            + " on user " + targetUserId);
7287                }
7288                return null;
7289            }
7290        }
7291        if (data != null) {
7292            GrantUri grantUri = GrantUri.resolve(contentUserHint, data);
7293            targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, grantUri, mode,
7294                    targetUid);
7295            if (targetUid > 0) {
7296                if (needed == null) {
7297                    needed = new NeededUriGrants(targetPkg, targetUid, mode);
7298                }
7299                needed.add(grantUri);
7300            }
7301        }
7302        if (clip != null) {
7303            for (int i=0; i<clip.getItemCount(); i++) {
7304                Uri uri = clip.getItemAt(i).getUri();
7305                if (uri != null) {
7306                    GrantUri grantUri = GrantUri.resolve(contentUserHint, uri);
7307                    targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, grantUri, mode,
7308                            targetUid);
7309                    if (targetUid > 0) {
7310                        if (needed == null) {
7311                            needed = new NeededUriGrants(targetPkg, targetUid, mode);
7312                        }
7313                        needed.add(grantUri);
7314                    }
7315                } else {
7316                    Intent clipIntent = clip.getItemAt(i).getIntent();
7317                    if (clipIntent != null) {
7318                        NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked(
7319                                callingUid, targetPkg, clipIntent, mode, needed, targetUserId);
7320                        if (newNeeded != null) {
7321                            needed = newNeeded;
7322                        }
7323                    }
7324                }
7325            }
7326        }
7327
7328        return needed;
7329    }
7330
7331    /**
7332     * Like grantUriPermissionUncheckedLocked, but takes an Intent.
7333     */
7334    void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed,
7335            UriPermissionOwner owner) {
7336        if (needed != null) {
7337            for (int i=0; i<needed.size(); i++) {
7338                GrantUri grantUri = needed.get(i);
7339                grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg,
7340                        grantUri, needed.flags, owner);
7341            }
7342        }
7343    }
7344
7345    void grantUriPermissionFromIntentLocked(int callingUid,
7346            String targetPkg, Intent intent, UriPermissionOwner owner, int targetUserId) {
7347        NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg,
7348                intent, intent != null ? intent.getFlags() : 0, null, targetUserId);
7349        if (needed == null) {
7350            return;
7351        }
7352
7353        grantUriPermissionUncheckedFromIntentLocked(needed, owner);
7354    }
7355
7356    /**
7357     * @param uri This uri must NOT contain an embedded userId.
7358     * @param userId The userId in which the uri is to be resolved.
7359     */
7360    @Override
7361    public void grantUriPermission(IApplicationThread caller, String targetPkg, Uri uri,
7362            final int modeFlags, int userId) {
7363        enforceNotIsolatedCaller("grantUriPermission");
7364        GrantUri grantUri = new GrantUri(userId, uri, false);
7365        synchronized(this) {
7366            final ProcessRecord r = getRecordForAppLocked(caller);
7367            if (r == null) {
7368                throw new SecurityException("Unable to find app for caller "
7369                        + caller
7370                        + " when granting permission to uri " + grantUri);
7371            }
7372            if (targetPkg == null) {
7373                throw new IllegalArgumentException("null target");
7374            }
7375            if (grantUri == null) {
7376                throw new IllegalArgumentException("null uri");
7377            }
7378
7379            Preconditions.checkFlagsArgument(modeFlags, Intent.FLAG_GRANT_READ_URI_PERMISSION
7380                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
7381                    | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
7382                    | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
7383
7384            grantUriPermissionLocked(r.uid, targetPkg, grantUri, modeFlags, null,
7385                    UserHandle.getUserId(r.uid));
7386        }
7387    }
7388
7389    void removeUriPermissionIfNeededLocked(UriPermission perm) {
7390        if (perm.modeFlags == 0) {
7391            final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(
7392                    perm.targetUid);
7393            if (perms != null) {
7394                if (DEBUG_URI_PERMISSION) Slog.v(TAG,
7395                        "Removing " + perm.targetUid + " permission to " + perm.uri);
7396
7397                perms.remove(perm.uri);
7398                if (perms.isEmpty()) {
7399                    mGrantedUriPermissions.remove(perm.targetUid);
7400                }
7401            }
7402        }
7403    }
7404
7405    private void revokeUriPermissionLocked(int callingUid, GrantUri grantUri, final int modeFlags) {
7406        if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Revoking all granted permissions to " + grantUri);
7407
7408        final IPackageManager pm = AppGlobals.getPackageManager();
7409        final String authority = grantUri.uri.getAuthority();
7410        final ProviderInfo pi = getProviderInfoLocked(authority, grantUri.sourceUserId);
7411        if (pi == null) {
7412            Slog.w(TAG, "No content provider found for permission revoke: "
7413                    + grantUri.toSafeString());
7414            return;
7415        }
7416
7417        // Does the caller have this permission on the URI?
7418        if (!checkHoldingPermissionsLocked(pm, pi, grantUri, callingUid, modeFlags)) {
7419            // Right now, if you are not the original owner of the permission,
7420            // you are not allowed to revoke it.
7421            //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
7422                throw new SecurityException("Uid " + callingUid
7423                        + " does not have permission to uri " + grantUri);
7424            //}
7425        }
7426
7427        boolean persistChanged = false;
7428
7429        // Go through all of the permissions and remove any that match.
7430        int N = mGrantedUriPermissions.size();
7431        for (int i = 0; i < N; i++) {
7432            final int targetUid = mGrantedUriPermissions.keyAt(i);
7433            final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
7434
7435            for (Iterator<UriPermission> it = perms.values().iterator(); it.hasNext();) {
7436                final UriPermission perm = it.next();
7437                if (perm.uri.sourceUserId == grantUri.sourceUserId
7438                        && perm.uri.uri.isPathPrefixMatch(grantUri.uri)) {
7439                    if (DEBUG_URI_PERMISSION)
7440                        Slog.v(TAG,
7441                                "Revoking " + perm.targetUid + " permission to " + perm.uri);
7442                    persistChanged |= perm.revokeModes(
7443                            modeFlags | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
7444                    if (perm.modeFlags == 0) {
7445                        it.remove();
7446                    }
7447                }
7448            }
7449
7450            if (perms.isEmpty()) {
7451                mGrantedUriPermissions.remove(targetUid);
7452                N--;
7453                i--;
7454            }
7455        }
7456
7457        if (persistChanged) {
7458            schedulePersistUriGrants();
7459        }
7460    }
7461
7462    /**
7463     * @param uri This uri must NOT contain an embedded userId.
7464     * @param userId The userId in which the uri is to be resolved.
7465     */
7466    @Override
7467    public void revokeUriPermission(IApplicationThread caller, Uri uri, final int modeFlags,
7468            int userId) {
7469        enforceNotIsolatedCaller("revokeUriPermission");
7470        synchronized(this) {
7471            final ProcessRecord r = getRecordForAppLocked(caller);
7472            if (r == null) {
7473                throw new SecurityException("Unable to find app for caller "
7474                        + caller
7475                        + " when revoking permission to uri " + uri);
7476            }
7477            if (uri == null) {
7478                Slog.w(TAG, "revokeUriPermission: null uri");
7479                return;
7480            }
7481
7482            if (!Intent.isAccessUriMode(modeFlags)) {
7483                return;
7484            }
7485
7486            final IPackageManager pm = AppGlobals.getPackageManager();
7487            final String authority = uri.getAuthority();
7488            final ProviderInfo pi = getProviderInfoLocked(authority, userId);
7489            if (pi == null) {
7490                Slog.w(TAG, "No content provider found for permission revoke: "
7491                        + uri.toSafeString());
7492                return;
7493            }
7494
7495            revokeUriPermissionLocked(r.uid, new GrantUri(userId, uri, false), modeFlags);
7496        }
7497    }
7498
7499    /**
7500     * Remove any {@link UriPermission} granted <em>from</em> or <em>to</em> the
7501     * given package.
7502     *
7503     * @param packageName Package name to match, or {@code null} to apply to all
7504     *            packages.
7505     * @param userHandle User to match, or {@link UserHandle#USER_ALL} to apply
7506     *            to all users.
7507     * @param persistable If persistable grants should be removed.
7508     */
7509    private void removeUriPermissionsForPackageLocked(
7510            String packageName, int userHandle, boolean persistable) {
7511        if (userHandle == UserHandle.USER_ALL && packageName == null) {
7512            throw new IllegalArgumentException("Must narrow by either package or user");
7513        }
7514
7515        boolean persistChanged = false;
7516
7517        int N = mGrantedUriPermissions.size();
7518        for (int i = 0; i < N; i++) {
7519            final int targetUid = mGrantedUriPermissions.keyAt(i);
7520            final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
7521
7522            // Only inspect grants matching user
7523            if (userHandle == UserHandle.USER_ALL
7524                    || userHandle == UserHandle.getUserId(targetUid)) {
7525                for (Iterator<UriPermission> it = perms.values().iterator(); it.hasNext();) {
7526                    final UriPermission perm = it.next();
7527
7528                    // Only inspect grants matching package
7529                    if (packageName == null || perm.sourcePkg.equals(packageName)
7530                            || perm.targetPkg.equals(packageName)) {
7531                        persistChanged |= perm.revokeModes(
7532                                persistable ? ~0 : ~Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
7533
7534                        // Only remove when no modes remain; any persisted grants
7535                        // will keep this alive.
7536                        if (perm.modeFlags == 0) {
7537                            it.remove();
7538                        }
7539                    }
7540                }
7541
7542                if (perms.isEmpty()) {
7543                    mGrantedUriPermissions.remove(targetUid);
7544                    N--;
7545                    i--;
7546                }
7547            }
7548        }
7549
7550        if (persistChanged) {
7551            schedulePersistUriGrants();
7552        }
7553    }
7554
7555    @Override
7556    public IBinder newUriPermissionOwner(String name) {
7557        enforceNotIsolatedCaller("newUriPermissionOwner");
7558        synchronized(this) {
7559            UriPermissionOwner owner = new UriPermissionOwner(this, name);
7560            return owner.getExternalTokenLocked();
7561        }
7562    }
7563
7564    /**
7565     * @param uri This uri must NOT contain an embedded userId.
7566     * @param sourceUserId The userId in which the uri is to be resolved.
7567     * @param targetUserId The userId of the app that receives the grant.
7568     */
7569    @Override
7570    public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, Uri uri,
7571            final int modeFlags, int sourceUserId, int targetUserId) {
7572        targetUserId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
7573                targetUserId, false, ALLOW_FULL_ONLY, "grantUriPermissionFromOwner", null);
7574        synchronized(this) {
7575            UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
7576            if (owner == null) {
7577                throw new IllegalArgumentException("Unknown owner: " + token);
7578            }
7579            if (fromUid != Binder.getCallingUid()) {
7580                if (Binder.getCallingUid() != Process.myUid()) {
7581                    // Only system code can grant URI permissions on behalf
7582                    // of other users.
7583                    throw new SecurityException("nice try");
7584                }
7585            }
7586            if (targetPkg == null) {
7587                throw new IllegalArgumentException("null target");
7588            }
7589            if (uri == null) {
7590                throw new IllegalArgumentException("null uri");
7591            }
7592
7593            grantUriPermissionLocked(fromUid, targetPkg, new GrantUri(sourceUserId, uri, false),
7594                    modeFlags, owner, targetUserId);
7595        }
7596    }
7597
7598    /**
7599     * @param uri This uri must NOT contain an embedded userId.
7600     * @param userId The userId in which the uri is to be resolved.
7601     */
7602    @Override
7603    public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode, int userId) {
7604        synchronized(this) {
7605            UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
7606            if (owner == null) {
7607                throw new IllegalArgumentException("Unknown owner: " + token);
7608            }
7609
7610            if (uri == null) {
7611                owner.removeUriPermissionsLocked(mode);
7612            } else {
7613                owner.removeUriPermissionLocked(new GrantUri(userId, uri, false), mode);
7614            }
7615        }
7616    }
7617
7618    private void schedulePersistUriGrants() {
7619        if (!mHandler.hasMessages(PERSIST_URI_GRANTS_MSG)) {
7620            mHandler.sendMessageDelayed(mHandler.obtainMessage(PERSIST_URI_GRANTS_MSG),
7621                    10 * DateUtils.SECOND_IN_MILLIS);
7622        }
7623    }
7624
7625    private void writeGrantedUriPermissions() {
7626        if (DEBUG_URI_PERMISSION) Slog.v(TAG, "writeGrantedUriPermissions()");
7627
7628        // Snapshot permissions so we can persist without lock
7629        ArrayList<UriPermission.Snapshot> persist = Lists.newArrayList();
7630        synchronized (this) {
7631            final int size = mGrantedUriPermissions.size();
7632            for (int i = 0; i < size; i++) {
7633                final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
7634                for (UriPermission perm : perms.values()) {
7635                    if (perm.persistedModeFlags != 0) {
7636                        persist.add(perm.snapshot());
7637                    }
7638                }
7639            }
7640        }
7641
7642        FileOutputStream fos = null;
7643        try {
7644            fos = mGrantFile.startWrite();
7645
7646            XmlSerializer out = new FastXmlSerializer();
7647            out.setOutput(fos, "utf-8");
7648            out.startDocument(null, true);
7649            out.startTag(null, TAG_URI_GRANTS);
7650            for (UriPermission.Snapshot perm : persist) {
7651                out.startTag(null, TAG_URI_GRANT);
7652                writeIntAttribute(out, ATTR_SOURCE_USER_ID, perm.uri.sourceUserId);
7653                writeIntAttribute(out, ATTR_TARGET_USER_ID, perm.targetUserId);
7654                out.attribute(null, ATTR_SOURCE_PKG, perm.sourcePkg);
7655                out.attribute(null, ATTR_TARGET_PKG, perm.targetPkg);
7656                out.attribute(null, ATTR_URI, String.valueOf(perm.uri.uri));
7657                writeBooleanAttribute(out, ATTR_PREFIX, perm.uri.prefix);
7658                writeIntAttribute(out, ATTR_MODE_FLAGS, perm.persistedModeFlags);
7659                writeLongAttribute(out, ATTR_CREATED_TIME, perm.persistedCreateTime);
7660                out.endTag(null, TAG_URI_GRANT);
7661            }
7662            out.endTag(null, TAG_URI_GRANTS);
7663            out.endDocument();
7664
7665            mGrantFile.finishWrite(fos);
7666        } catch (IOException e) {
7667            if (fos != null) {
7668                mGrantFile.failWrite(fos);
7669            }
7670        }
7671    }
7672
7673    private void readGrantedUriPermissionsLocked() {
7674        if (DEBUG_URI_PERMISSION) Slog.v(TAG, "readGrantedUriPermissions()");
7675
7676        final long now = System.currentTimeMillis();
7677
7678        FileInputStream fis = null;
7679        try {
7680            fis = mGrantFile.openRead();
7681            final XmlPullParser in = Xml.newPullParser();
7682            in.setInput(fis, null);
7683
7684            int type;
7685            while ((type = in.next()) != END_DOCUMENT) {
7686                final String tag = in.getName();
7687                if (type == START_TAG) {
7688                    if (TAG_URI_GRANT.equals(tag)) {
7689                        final int sourceUserId;
7690                        final int targetUserId;
7691                        final int userHandle = readIntAttribute(in,
7692                                ATTR_USER_HANDLE, UserHandle.USER_NULL);
7693                        if (userHandle != UserHandle.USER_NULL) {
7694                            // For backwards compatibility.
7695                            sourceUserId = userHandle;
7696                            targetUserId = userHandle;
7697                        } else {
7698                            sourceUserId = readIntAttribute(in, ATTR_SOURCE_USER_ID);
7699                            targetUserId = readIntAttribute(in, ATTR_TARGET_USER_ID);
7700                        }
7701                        final String sourcePkg = in.getAttributeValue(null, ATTR_SOURCE_PKG);
7702                        final String targetPkg = in.getAttributeValue(null, ATTR_TARGET_PKG);
7703                        final Uri uri = Uri.parse(in.getAttributeValue(null, ATTR_URI));
7704                        final boolean prefix = readBooleanAttribute(in, ATTR_PREFIX);
7705                        final int modeFlags = readIntAttribute(in, ATTR_MODE_FLAGS);
7706                        final long createdTime = readLongAttribute(in, ATTR_CREATED_TIME, now);
7707
7708                        // Sanity check that provider still belongs to source package
7709                        final ProviderInfo pi = getProviderInfoLocked(
7710                                uri.getAuthority(), sourceUserId);
7711                        if (pi != null && sourcePkg.equals(pi.packageName)) {
7712                            int targetUid = -1;
7713                            try {
7714                                targetUid = AppGlobals.getPackageManager()
7715                                        .getPackageUid(targetPkg, targetUserId);
7716                            } catch (RemoteException e) {
7717                            }
7718                            if (targetUid != -1) {
7719                                final UriPermission perm = findOrCreateUriPermissionLocked(
7720                                        sourcePkg, targetPkg, targetUid,
7721                                        new GrantUri(sourceUserId, uri, prefix));
7722                                perm.initPersistedModes(modeFlags, createdTime);
7723                            }
7724                        } else {
7725                            Slog.w(TAG, "Persisted grant for " + uri + " had source " + sourcePkg
7726                                    + " but instead found " + pi);
7727                        }
7728                    }
7729                }
7730            }
7731        } catch (FileNotFoundException e) {
7732            // Missing grants is okay
7733        } catch (IOException e) {
7734            Log.wtf(TAG, "Failed reading Uri grants", e);
7735        } catch (XmlPullParserException e) {
7736            Log.wtf(TAG, "Failed reading Uri grants", e);
7737        } finally {
7738            IoUtils.closeQuietly(fis);
7739        }
7740    }
7741
7742    /**
7743     * @param uri This uri must NOT contain an embedded userId.
7744     * @param userId The userId in which the uri is to be resolved.
7745     */
7746    @Override
7747    public void takePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
7748        enforceNotIsolatedCaller("takePersistableUriPermission");
7749
7750        Preconditions.checkFlagsArgument(modeFlags,
7751                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
7752
7753        synchronized (this) {
7754            final int callingUid = Binder.getCallingUid();
7755            boolean persistChanged = false;
7756            GrantUri grantUri = new GrantUri(userId, uri, false);
7757
7758            UriPermission exactPerm = findUriPermissionLocked(callingUid,
7759                    new GrantUri(userId, uri, false));
7760            UriPermission prefixPerm = findUriPermissionLocked(callingUid,
7761                    new GrantUri(userId, uri, true));
7762
7763            final boolean exactValid = (exactPerm != null)
7764                    && ((modeFlags & exactPerm.persistableModeFlags) == modeFlags);
7765            final boolean prefixValid = (prefixPerm != null)
7766                    && ((modeFlags & prefixPerm.persistableModeFlags) == modeFlags);
7767
7768            if (!(exactValid || prefixValid)) {
7769                throw new SecurityException("No persistable permission grants found for UID "
7770                        + callingUid + " and Uri " + grantUri.toSafeString());
7771            }
7772
7773            if (exactValid) {
7774                persistChanged |= exactPerm.takePersistableModes(modeFlags);
7775            }
7776            if (prefixValid) {
7777                persistChanged |= prefixPerm.takePersistableModes(modeFlags);
7778            }
7779
7780            persistChanged |= maybePrunePersistedUriGrantsLocked(callingUid);
7781
7782            if (persistChanged) {
7783                schedulePersistUriGrants();
7784            }
7785        }
7786    }
7787
7788    /**
7789     * @param uri This uri must NOT contain an embedded userId.
7790     * @param userId The userId in which the uri is to be resolved.
7791     */
7792    @Override
7793    public void releasePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
7794        enforceNotIsolatedCaller("releasePersistableUriPermission");
7795
7796        Preconditions.checkFlagsArgument(modeFlags,
7797                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
7798
7799        synchronized (this) {
7800            final int callingUid = Binder.getCallingUid();
7801            boolean persistChanged = false;
7802
7803            UriPermission exactPerm = findUriPermissionLocked(callingUid,
7804                    new GrantUri(userId, uri, false));
7805            UriPermission prefixPerm = findUriPermissionLocked(callingUid,
7806                    new GrantUri(userId, uri, true));
7807            if (exactPerm == null && prefixPerm == null) {
7808                throw new SecurityException("No permission grants found for UID " + callingUid
7809                        + " and Uri " + uri.toSafeString());
7810            }
7811
7812            if (exactPerm != null) {
7813                persistChanged |= exactPerm.releasePersistableModes(modeFlags);
7814                removeUriPermissionIfNeededLocked(exactPerm);
7815            }
7816            if (prefixPerm != null) {
7817                persistChanged |= prefixPerm.releasePersistableModes(modeFlags);
7818                removeUriPermissionIfNeededLocked(prefixPerm);
7819            }
7820
7821            if (persistChanged) {
7822                schedulePersistUriGrants();
7823            }
7824        }
7825    }
7826
7827    /**
7828     * Prune any older {@link UriPermission} for the given UID until outstanding
7829     * persisted grants are below {@link #MAX_PERSISTED_URI_GRANTS}.
7830     *
7831     * @return if any mutations occured that require persisting.
7832     */
7833    private boolean maybePrunePersistedUriGrantsLocked(int uid) {
7834        final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(uid);
7835        if (perms == null) return false;
7836        if (perms.size() < MAX_PERSISTED_URI_GRANTS) return false;
7837
7838        final ArrayList<UriPermission> persisted = Lists.newArrayList();
7839        for (UriPermission perm : perms.values()) {
7840            if (perm.persistedModeFlags != 0) {
7841                persisted.add(perm);
7842            }
7843        }
7844
7845        final int trimCount = persisted.size() - MAX_PERSISTED_URI_GRANTS;
7846        if (trimCount <= 0) return false;
7847
7848        Collections.sort(persisted, new UriPermission.PersistedTimeComparator());
7849        for (int i = 0; i < trimCount; i++) {
7850            final UriPermission perm = persisted.get(i);
7851
7852            if (DEBUG_URI_PERMISSION) {
7853                Slog.v(TAG, "Trimming grant created at " + perm.persistedCreateTime);
7854            }
7855
7856            perm.releasePersistableModes(~0);
7857            removeUriPermissionIfNeededLocked(perm);
7858        }
7859
7860        return true;
7861    }
7862
7863    @Override
7864    public ParceledListSlice<android.content.UriPermission> getPersistedUriPermissions(
7865            String packageName, boolean incoming) {
7866        enforceNotIsolatedCaller("getPersistedUriPermissions");
7867        Preconditions.checkNotNull(packageName, "packageName");
7868
7869        final int callingUid = Binder.getCallingUid();
7870        final IPackageManager pm = AppGlobals.getPackageManager();
7871        try {
7872            final int packageUid = pm.getPackageUid(packageName, UserHandle.getUserId(callingUid));
7873            if (packageUid != callingUid) {
7874                throw new SecurityException(
7875                        "Package " + packageName + " does not belong to calling UID " + callingUid);
7876            }
7877        } catch (RemoteException e) {
7878            throw new SecurityException("Failed to verify package name ownership");
7879        }
7880
7881        final ArrayList<android.content.UriPermission> result = Lists.newArrayList();
7882        synchronized (this) {
7883            if (incoming) {
7884                final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(
7885                        callingUid);
7886                if (perms == null) {
7887                    Slog.w(TAG, "No permission grants found for " + packageName);
7888                } else {
7889                    for (UriPermission perm : perms.values()) {
7890                        if (packageName.equals(perm.targetPkg) && perm.persistedModeFlags != 0) {
7891                            result.add(perm.buildPersistedPublicApiObject());
7892                        }
7893                    }
7894                }
7895            } else {
7896                final int size = mGrantedUriPermissions.size();
7897                for (int i = 0; i < size; i++) {
7898                    final ArrayMap<GrantUri, UriPermission> perms =
7899                            mGrantedUriPermissions.valueAt(i);
7900                    for (UriPermission perm : perms.values()) {
7901                        if (packageName.equals(perm.sourcePkg) && perm.persistedModeFlags != 0) {
7902                            result.add(perm.buildPersistedPublicApiObject());
7903                        }
7904                    }
7905                }
7906            }
7907        }
7908        return new ParceledListSlice<android.content.UriPermission>(result);
7909    }
7910
7911    @Override
7912    public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
7913        synchronized (this) {
7914            ProcessRecord app =
7915                who != null ? getRecordForAppLocked(who) : null;
7916            if (app == null) return;
7917
7918            Message msg = Message.obtain();
7919            msg.what = WAIT_FOR_DEBUGGER_MSG;
7920            msg.obj = app;
7921            msg.arg1 = waiting ? 1 : 0;
7922            mHandler.sendMessage(msg);
7923        }
7924    }
7925
7926    @Override
7927    public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
7928        final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ);
7929        final long cachedAppMem = mProcessList.getMemLevel(ProcessList.CACHED_APP_MIN_ADJ);
7930        outInfo.availMem = Process.getFreeMemory();
7931        outInfo.totalMem = Process.getTotalMemory();
7932        outInfo.threshold = homeAppMem;
7933        outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((cachedAppMem-homeAppMem)/2));
7934        outInfo.hiddenAppThreshold = cachedAppMem;
7935        outInfo.secondaryServerThreshold = mProcessList.getMemLevel(
7936                ProcessList.SERVICE_ADJ);
7937        outInfo.visibleAppThreshold = mProcessList.getMemLevel(
7938                ProcessList.VISIBLE_APP_ADJ);
7939        outInfo.foregroundAppThreshold = mProcessList.getMemLevel(
7940                ProcessList.FOREGROUND_APP_ADJ);
7941    }
7942
7943    // =========================================================
7944    // TASK MANAGEMENT
7945    // =========================================================
7946
7947    @Override
7948    public List<IAppTask> getAppTasks(String callingPackage) {
7949        int callingUid = Binder.getCallingUid();
7950        long ident = Binder.clearCallingIdentity();
7951
7952        synchronized(this) {
7953            ArrayList<IAppTask> list = new ArrayList<IAppTask>();
7954            try {
7955                if (localLOGV) Slog.v(TAG, "getAppTasks");
7956
7957                final int N = mRecentTasks.size();
7958                for (int i = 0; i < N; i++) {
7959                    TaskRecord tr = mRecentTasks.get(i);
7960                    // Skip tasks that do not match the caller.  We don't need to verify
7961                    // callingPackage, because we are also limiting to callingUid and know
7962                    // that will limit to the correct security sandbox.
7963                    if (tr.effectiveUid != callingUid) {
7964                        continue;
7965                    }
7966                    Intent intent = tr.getBaseIntent();
7967                    if (intent == null ||
7968                            !callingPackage.equals(intent.getComponent().getPackageName())) {
7969                        continue;
7970                    }
7971                    ActivityManager.RecentTaskInfo taskInfo =
7972                            createRecentTaskInfoFromTaskRecord(tr);
7973                    AppTaskImpl taskImpl = new AppTaskImpl(taskInfo.persistentId, callingUid);
7974                    list.add(taskImpl);
7975                }
7976            } finally {
7977                Binder.restoreCallingIdentity(ident);
7978            }
7979            return list;
7980        }
7981    }
7982
7983    @Override
7984    public List<RunningTaskInfo> getTasks(int maxNum, int flags) {
7985        final int callingUid = Binder.getCallingUid();
7986        ArrayList<RunningTaskInfo> list = new ArrayList<RunningTaskInfo>();
7987
7988        synchronized(this) {
7989            if (localLOGV) Slog.v(
7990                TAG, "getTasks: max=" + maxNum + ", flags=" + flags);
7991
7992            final boolean allowed = checkCallingPermission(
7993                    android.Manifest.permission.GET_TASKS)
7994                    == PackageManager.PERMISSION_GRANTED;
7995            if (!allowed) {
7996                Slog.w(TAG, "getTasks: caller " + callingUid
7997                        + " does not hold GET_TASKS; limiting output");
7998            }
7999
8000            // TODO: Improve with MRU list from all ActivityStacks.
8001            mStackSupervisor.getTasksLocked(maxNum, list, callingUid, allowed);
8002        }
8003
8004        return list;
8005    }
8006
8007    TaskRecord getMostRecentTask() {
8008        return mRecentTasks.get(0);
8009    }
8010
8011    /**
8012     * Creates a new RecentTaskInfo from a TaskRecord.
8013     */
8014    private ActivityManager.RecentTaskInfo createRecentTaskInfoFromTaskRecord(TaskRecord tr) {
8015        // Update the task description to reflect any changes in the task stack
8016        tr.updateTaskDescription();
8017
8018        // Compose the recent task info
8019        ActivityManager.RecentTaskInfo rti = new ActivityManager.RecentTaskInfo();
8020        rti.id = tr.getTopActivity() == null ? -1 : tr.taskId;
8021        rti.persistentId = tr.taskId;
8022        rti.baseIntent = new Intent(tr.getBaseIntent());
8023        rti.origActivity = tr.origActivity;
8024        rti.description = tr.lastDescription;
8025        rti.stackId = tr.stack != null ? tr.stack.mStackId : -1;
8026        rti.userId = tr.userId;
8027        rti.taskDescription = new ActivityManager.TaskDescription(tr.lastTaskDescription);
8028        rti.firstActiveTime = tr.firstActiveTime;
8029        rti.lastActiveTime = tr.lastActiveTime;
8030        rti.affiliatedTaskId = tr.mAffiliatedTaskId;
8031        rti.affiliatedTaskColor = tr.mAffiliatedTaskColor;
8032        return rti;
8033    }
8034
8035    @Override
8036    public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags, int userId) {
8037        final int callingUid = Binder.getCallingUid();
8038        userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId,
8039                false, ALLOW_FULL_ONLY, "getRecentTasks", null);
8040
8041        final boolean includeProfiles = (flags & ActivityManager.RECENT_INCLUDE_PROFILES) != 0;
8042        final boolean withExcluded = (flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0;
8043        synchronized (this) {
8044            final boolean allowed = checkCallingPermission(android.Manifest.permission.GET_TASKS)
8045                    == PackageManager.PERMISSION_GRANTED;
8046            if (!allowed) {
8047                Slog.w(TAG, "getRecentTasks: caller " + callingUid
8048                        + " does not hold GET_TASKS; limiting output");
8049            }
8050            final boolean detailed = checkCallingPermission(
8051                    android.Manifest.permission.GET_DETAILED_TASKS)
8052                    == PackageManager.PERMISSION_GRANTED;
8053
8054            final int N = mRecentTasks.size();
8055            ArrayList<ActivityManager.RecentTaskInfo> res
8056                    = new ArrayList<ActivityManager.RecentTaskInfo>(
8057                            maxNum < N ? maxNum : N);
8058
8059            final Set<Integer> includedUsers;
8060            if (includeProfiles) {
8061                includedUsers = getProfileIdsLocked(userId);
8062            } else {
8063                includedUsers = new HashSet<Integer>();
8064            }
8065            includedUsers.add(Integer.valueOf(userId));
8066
8067            for (int i=0; i<N && maxNum > 0; i++) {
8068                TaskRecord tr = mRecentTasks.get(i);
8069                // Only add calling user or related users recent tasks
8070                if (!includedUsers.contains(Integer.valueOf(tr.userId))) {
8071                    if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, not user: " + tr);
8072                    continue;
8073                }
8074
8075                // Return the entry if desired by the caller.  We always return
8076                // the first entry, because callers always expect this to be the
8077                // foreground app.  We may filter others if the caller has
8078                // not supplied RECENT_WITH_EXCLUDED and there is some reason
8079                // we should exclude the entry.
8080
8081                if (i == 0
8082                        || withExcluded
8083                        || (tr.intent == null)
8084                        || ((tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
8085                                == 0)) {
8086                    if (!allowed) {
8087                        // If the caller doesn't have the GET_TASKS permission, then only
8088                        // allow them to see a small subset of tasks -- their own and home.
8089                        if (!tr.isHomeTask() && tr.effectiveUid != callingUid) {
8090                            if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, not allowed: " + tr);
8091                            continue;
8092                        }
8093                    }
8094                    if ((flags & ActivityManager.RECENT_IGNORE_HOME_STACK_TASKS) != 0) {
8095                        if (tr.stack != null && tr.stack.isHomeStack()) {
8096                            if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, home stack task: " + tr);
8097                            continue;
8098                        }
8099                    }
8100                    if (tr.autoRemoveRecents && tr.getTopActivity() == null) {
8101                        // Don't include auto remove tasks that are finished or finishing.
8102                        if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, auto-remove without activity: "
8103                                + tr);
8104                        continue;
8105                    }
8106                    if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0
8107                            && !tr.isAvailable) {
8108                        if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, unavail real act: " + tr);
8109                        continue;
8110                    }
8111
8112                    ActivityManager.RecentTaskInfo rti = createRecentTaskInfoFromTaskRecord(tr);
8113                    if (!detailed) {
8114                        rti.baseIntent.replaceExtras((Bundle)null);
8115                    }
8116
8117                    res.add(rti);
8118                    maxNum--;
8119                }
8120            }
8121            return res;
8122        }
8123    }
8124
8125    private TaskRecord recentTaskForIdLocked(int id) {
8126        final int N = mRecentTasks.size();
8127            for (int i=0; i<N; i++) {
8128                TaskRecord tr = mRecentTasks.get(i);
8129                if (tr.taskId == id) {
8130                    return tr;
8131                }
8132            }
8133            return null;
8134    }
8135
8136    @Override
8137    public ActivityManager.TaskThumbnail getTaskThumbnail(int id) {
8138        synchronized (this) {
8139            enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER,
8140                    "getTaskThumbnail()");
8141            TaskRecord tr = recentTaskForIdLocked(id);
8142            if (tr != null) {
8143                return tr.getTaskThumbnailLocked();
8144            }
8145        }
8146        return null;
8147    }
8148
8149    @Override
8150    public int addAppTask(IBinder activityToken, Intent intent,
8151            ActivityManager.TaskDescription description, Bitmap thumbnail) throws RemoteException {
8152        final int callingUid = Binder.getCallingUid();
8153        final long callingIdent = Binder.clearCallingIdentity();
8154
8155        try {
8156            synchronized (this) {
8157                ActivityRecord r = ActivityRecord.isInStackLocked(activityToken);
8158                if (r == null) {
8159                    throw new IllegalArgumentException("Activity does not exist; token="
8160                            + activityToken);
8161                }
8162                ComponentName comp = intent.getComponent();
8163                if (comp == null) {
8164                    throw new IllegalArgumentException("Intent " + intent
8165                            + " must specify explicit component");
8166                }
8167                if (thumbnail.getWidth() != mThumbnailWidth
8168                        || thumbnail.getHeight() != mThumbnailHeight) {
8169                    throw new IllegalArgumentException("Bad thumbnail size: got "
8170                            + thumbnail.getWidth() + "x" + thumbnail.getHeight() + ", require "
8171                            + mThumbnailWidth + "x" + mThumbnailHeight);
8172                }
8173                if (intent.getSelector() != null) {
8174                    intent.setSelector(null);
8175                }
8176                if (intent.getSourceBounds() != null) {
8177                    intent.setSourceBounds(null);
8178                }
8179                if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0) {
8180                    if ((intent.getFlags()&Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS) == 0) {
8181                        // The caller has added this as an auto-remove task...  that makes no
8182                        // sense, so turn off auto-remove.
8183                        intent.addFlags(Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
8184                    }
8185                } else if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
8186                    // Must be a new task.
8187                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
8188                }
8189                if (!comp.equals(mLastAddedTaskComponent) || callingUid != mLastAddedTaskUid) {
8190                    mLastAddedTaskActivity = null;
8191                }
8192                ActivityInfo ainfo = mLastAddedTaskActivity;
8193                if (ainfo == null) {
8194                    ainfo = mLastAddedTaskActivity = AppGlobals.getPackageManager().getActivityInfo(
8195                            comp, 0, UserHandle.getUserId(callingUid));
8196                    if (ainfo.applicationInfo.uid != callingUid) {
8197                        throw new SecurityException(
8198                                "Can't add task for another application: target uid="
8199                                + ainfo.applicationInfo.uid + ", calling uid=" + callingUid);
8200                    }
8201                }
8202
8203                TaskRecord task = new TaskRecord(this, mStackSupervisor.getNextTaskId(), ainfo,
8204                        intent, description);
8205
8206                int trimIdx = trimRecentsForTask(task, false);
8207                if (trimIdx >= 0) {
8208                    // If this would have caused a trim, then we'll abort because that
8209                    // means it would be added at the end of the list but then just removed.
8210                    return -1;
8211                }
8212
8213                final int N = mRecentTasks.size();
8214                if (N >= (ActivityManager.getMaxRecentTasksStatic()-1)) {
8215                    final TaskRecord tr = mRecentTasks.remove(N - 1);
8216                    tr.removedFromRecents(mTaskPersister);
8217                }
8218
8219                task.inRecents = true;
8220                mRecentTasks.add(task);
8221                r.task.stack.addTask(task, false, false);
8222
8223                task.setLastThumbnail(thumbnail);
8224                task.freeLastThumbnail();
8225
8226                return task.taskId;
8227            }
8228        } finally {
8229            Binder.restoreCallingIdentity(callingIdent);
8230        }
8231    }
8232
8233    @Override
8234    public Point getAppTaskThumbnailSize() {
8235        synchronized (this) {
8236            return new Point(mThumbnailWidth,  mThumbnailHeight);
8237        }
8238    }
8239
8240    @Override
8241    public void setTaskDescription(IBinder token, ActivityManager.TaskDescription td) {
8242        synchronized (this) {
8243            ActivityRecord r = ActivityRecord.isInStackLocked(token);
8244            if (r != null) {
8245                r.taskDescription = td;
8246                r.task.updateTaskDescription();
8247            }
8248        }
8249    }
8250
8251    private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) {
8252        mRecentTasks.remove(tr);
8253        tr.removedFromRecents(mTaskPersister);
8254        final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0;
8255        Intent baseIntent = new Intent(
8256                tr.intent != null ? tr.intent : tr.affinityIntent);
8257        ComponentName component = baseIntent.getComponent();
8258        if (component == null) {
8259            Slog.w(TAG, "Now component for base intent of task: " + tr);
8260            return;
8261        }
8262
8263        // Find any running services associated with this app.
8264        mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent);
8265
8266        if (killProcesses) {
8267            // Find any running processes associated with this app.
8268            final String pkg = component.getPackageName();
8269            ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
8270            ArrayMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap();
8271            for (int i=0; i<pmap.size(); i++) {
8272                SparseArray<ProcessRecord> uids = pmap.valueAt(i);
8273                for (int j=0; j<uids.size(); j++) {
8274                    ProcessRecord proc = uids.valueAt(j);
8275                    if (proc.userId != tr.userId) {
8276                        continue;
8277                    }
8278                    if (!proc.pkgList.containsKey(pkg)) {
8279                        continue;
8280                    }
8281                    procs.add(proc);
8282                }
8283            }
8284
8285            // Kill the running processes.
8286            for (int i=0; i<procs.size(); i++) {
8287                ProcessRecord pr = procs.get(i);
8288                if (pr == mHomeProcess) {
8289                    // Don't kill the home process along with tasks from the same package.
8290                    continue;
8291                }
8292                if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
8293                    pr.kill("remove task", true);
8294                } else {
8295                    pr.waitingToKill = "remove task";
8296                }
8297            }
8298        }
8299    }
8300
8301    /**
8302     * Removes the task with the specified task id.
8303     *
8304     * @param taskId Identifier of the task to be removed.
8305     * @param flags Additional operational flags.  May be 0 or
8306     * {@link ActivityManager#REMOVE_TASK_KILL_PROCESS}.
8307     * @return Returns true if the given task was found and removed.
8308     */
8309    private boolean removeTaskByIdLocked(int taskId, int flags) {
8310        TaskRecord tr = recentTaskForIdLocked(taskId);
8311        if (tr != null) {
8312            tr.removeTaskActivitiesLocked();
8313            cleanUpRemovedTaskLocked(tr, flags);
8314            if (tr.isPersistable) {
8315                notifyTaskPersisterLocked(null, true);
8316            }
8317            return true;
8318        }
8319        return false;
8320    }
8321
8322    @Override
8323    public boolean removeTask(int taskId, int flags) {
8324        synchronized (this) {
8325            enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
8326                    "removeTask()");
8327            long ident = Binder.clearCallingIdentity();
8328            try {
8329                return removeTaskByIdLocked(taskId, flags);
8330            } finally {
8331                Binder.restoreCallingIdentity(ident);
8332            }
8333        }
8334    }
8335
8336    /**
8337     * TODO: Add mController hook
8338     */
8339    @Override
8340    public void moveTaskToFront(int taskId, int flags, Bundle options) {
8341        enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
8342                "moveTaskToFront()");
8343
8344        if (DEBUG_STACK) Slog.d(TAG, "moveTaskToFront: moving taskId=" + taskId);
8345        synchronized(this) {
8346            moveTaskToFrontLocked(taskId, flags, options);
8347        }
8348    }
8349
8350    void moveTaskToFrontLocked(int taskId, int flags, Bundle options) {
8351        if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
8352                Binder.getCallingUid(), "Task to front")) {
8353            ActivityOptions.abort(options);
8354            return;
8355        }
8356        final long origId = Binder.clearCallingIdentity();
8357        try {
8358            final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
8359            if (task == null) {
8360                return;
8361            }
8362            if (mStackSupervisor.isLockTaskModeViolation(task)) {
8363                mStackSupervisor.showLockTaskToast();
8364                Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
8365                return;
8366            }
8367            final ActivityRecord prev = mStackSupervisor.topRunningActivityLocked();
8368            if (prev != null && prev.isRecentsActivity()) {
8369                task.setTaskToReturnTo(ActivityRecord.RECENTS_ACTIVITY_TYPE);
8370            }
8371            mStackSupervisor.findTaskToMoveToFrontLocked(task, flags, options);
8372        } finally {
8373            Binder.restoreCallingIdentity(origId);
8374        }
8375        ActivityOptions.abort(options);
8376    }
8377
8378    @Override
8379    public void moveTaskToBack(int taskId) {
8380        enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
8381                "moveTaskToBack()");
8382
8383        synchronized(this) {
8384            TaskRecord tr = recentTaskForIdLocked(taskId);
8385            if (tr != null) {
8386                if (DEBUG_STACK) Slog.d(TAG, "moveTaskToBack: moving task=" + tr);
8387                ActivityStack stack = tr.stack;
8388                if (stack.mResumedActivity != null && stack.mResumedActivity.task == tr) {
8389                    if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
8390                            Binder.getCallingUid(), "Task to back")) {
8391                        return;
8392                    }
8393                }
8394                final long origId = Binder.clearCallingIdentity();
8395                try {
8396                    stack.moveTaskToBackLocked(taskId, null);
8397                } finally {
8398                    Binder.restoreCallingIdentity(origId);
8399                }
8400            }
8401        }
8402    }
8403
8404    /**
8405     * Moves an activity, and all of the other activities within the same task, to the bottom
8406     * of the history stack.  The activity's order within the task is unchanged.
8407     *
8408     * @param token A reference to the activity we wish to move
8409     * @param nonRoot If false then this only works if the activity is the root
8410     *                of a task; if true it will work for any activity in a task.
8411     * @return Returns true if the move completed, false if not.
8412     */
8413    @Override
8414    public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
8415        enforceNotIsolatedCaller("moveActivityTaskToBack");
8416        synchronized(this) {
8417            final long origId = Binder.clearCallingIdentity();
8418            int taskId = ActivityRecord.getTaskForActivityLocked(token, !nonRoot);
8419            if (taskId >= 0) {
8420                return ActivityRecord.getStackLocked(token).moveTaskToBackLocked(taskId, null);
8421            }
8422            Binder.restoreCallingIdentity(origId);
8423        }
8424        return false;
8425    }
8426
8427    @Override
8428    public void moveTaskBackwards(int task) {
8429        enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
8430                "moveTaskBackwards()");
8431
8432        synchronized(this) {
8433            if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
8434                    Binder.getCallingUid(), "Task backwards")) {
8435                return;
8436            }
8437            final long origId = Binder.clearCallingIdentity();
8438            moveTaskBackwardsLocked(task);
8439            Binder.restoreCallingIdentity(origId);
8440        }
8441    }
8442
8443    private final void moveTaskBackwardsLocked(int task) {
8444        Slog.e(TAG, "moveTaskBackwards not yet implemented!");
8445    }
8446
8447    @Override
8448    public IBinder getHomeActivityToken() throws RemoteException {
8449        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8450                "getHomeActivityToken()");
8451        synchronized (this) {
8452            return mStackSupervisor.getHomeActivityToken();
8453        }
8454    }
8455
8456    @Override
8457    public IActivityContainer createActivityContainer(IBinder parentActivityToken,
8458            IActivityContainerCallback callback) throws RemoteException {
8459        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8460                "createActivityContainer()");
8461        synchronized (this) {
8462            if (parentActivityToken == null) {
8463                throw new IllegalArgumentException("parent token must not be null");
8464            }
8465            ActivityRecord r = ActivityRecord.forToken(parentActivityToken);
8466            if (r == null) {
8467                return null;
8468            }
8469            if (callback == null) {
8470                throw new IllegalArgumentException("callback must not be null");
8471            }
8472            return mStackSupervisor.createActivityContainer(r, callback);
8473        }
8474    }
8475
8476    @Override
8477    public void deleteActivityContainer(IActivityContainer container) throws RemoteException {
8478        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8479                "deleteActivityContainer()");
8480        synchronized (this) {
8481            mStackSupervisor.deleteActivityContainer(container);
8482        }
8483    }
8484
8485    @Override
8486    public IActivityContainer getEnclosingActivityContainer(IBinder activityToken)
8487            throws RemoteException {
8488        synchronized (this) {
8489            ActivityStack stack = ActivityRecord.getStackLocked(activityToken);
8490            if (stack != null) {
8491                return stack.mActivityContainer;
8492            }
8493            return null;
8494        }
8495    }
8496
8497    @Override
8498    public void moveTaskToStack(int taskId, int stackId, boolean toTop) {
8499        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8500                "moveTaskToStack()");
8501        if (stackId == HOME_STACK_ID) {
8502            Slog.e(TAG, "moveTaskToStack: Attempt to move task " + taskId + " to home stack",
8503                    new RuntimeException("here").fillInStackTrace());
8504        }
8505        synchronized (this) {
8506            long ident = Binder.clearCallingIdentity();
8507            try {
8508                if (DEBUG_STACK) Slog.d(TAG, "moveTaskToStack: moving task=" + taskId + " to stackId="
8509                        + stackId + " toTop=" + toTop);
8510                mStackSupervisor.moveTaskToStack(taskId, stackId, toTop);
8511            } finally {
8512                Binder.restoreCallingIdentity(ident);
8513            }
8514        }
8515    }
8516
8517    @Override
8518    public void resizeStack(int stackBoxId, Rect bounds) {
8519        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8520                "resizeStackBox()");
8521        long ident = Binder.clearCallingIdentity();
8522        try {
8523            mWindowManager.resizeStack(stackBoxId, bounds);
8524        } finally {
8525            Binder.restoreCallingIdentity(ident);
8526        }
8527    }
8528
8529    @Override
8530    public List<StackInfo> getAllStackInfos() {
8531        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8532                "getAllStackInfos()");
8533        long ident = Binder.clearCallingIdentity();
8534        try {
8535            synchronized (this) {
8536                return mStackSupervisor.getAllStackInfosLocked();
8537            }
8538        } finally {
8539            Binder.restoreCallingIdentity(ident);
8540        }
8541    }
8542
8543    @Override
8544    public StackInfo getStackInfo(int stackId) {
8545        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8546                "getStackInfo()");
8547        long ident = Binder.clearCallingIdentity();
8548        try {
8549            synchronized (this) {
8550                return mStackSupervisor.getStackInfoLocked(stackId);
8551            }
8552        } finally {
8553            Binder.restoreCallingIdentity(ident);
8554        }
8555    }
8556
8557    @Override
8558    public boolean isInHomeStack(int taskId) {
8559        enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
8560                "getStackInfo()");
8561        long ident = Binder.clearCallingIdentity();
8562        try {
8563            synchronized (this) {
8564                TaskRecord tr = recentTaskForIdLocked(taskId);
8565                return tr != null && tr.stack != null && tr.stack.isHomeStack();
8566            }
8567        } finally {
8568            Binder.restoreCallingIdentity(ident);
8569        }
8570    }
8571
8572    @Override
8573    public int getTaskForActivity(IBinder token, boolean onlyRoot) {
8574        synchronized(this) {
8575            return ActivityRecord.getTaskForActivityLocked(token, onlyRoot);
8576        }
8577    }
8578
8579    private boolean isLockTaskAuthorized(String pkg) {
8580        final DevicePolicyManager dpm = (DevicePolicyManager)
8581                mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
8582        try {
8583            int uid = mContext.getPackageManager().getPackageUid(pkg,
8584                    Binder.getCallingUserHandle().getIdentifier());
8585            return (uid == Binder.getCallingUid()) && dpm != null && dpm.isLockTaskPermitted(pkg);
8586        } catch (NameNotFoundException e) {
8587            return false;
8588        }
8589    }
8590
8591    void startLockTaskMode(TaskRecord task) {
8592        final String pkg;
8593        synchronized (this) {
8594            pkg = task.intent.getComponent().getPackageName();
8595        }
8596        boolean isSystemInitiated = Binder.getCallingUid() == Process.SYSTEM_UID;
8597        if (!isSystemInitiated && !isLockTaskAuthorized(pkg)) {
8598            final TaskRecord taskRecord = task;
8599            mHandler.post(new Runnable() {
8600                @Override
8601                public void run() {
8602                    mLockToAppRequest.showLockTaskPrompt(taskRecord);
8603                }
8604            });
8605            return;
8606        }
8607        long ident = Binder.clearCallingIdentity();
8608        try {
8609            synchronized (this) {
8610                // Since we lost lock on task, make sure it is still there.
8611                task = mStackSupervisor.anyTaskForIdLocked(task.taskId);
8612                if (task != null) {
8613                    if (!isSystemInitiated
8614                            && ((mFocusedActivity == null) || (task != mFocusedActivity.task))) {
8615                        throw new IllegalArgumentException("Invalid task, not in foreground");
8616                    }
8617                    mStackSupervisor.setLockTaskModeLocked(task, !isSystemInitiated);
8618                }
8619            }
8620        } finally {
8621            Binder.restoreCallingIdentity(ident);
8622        }
8623    }
8624
8625    @Override
8626    public void startLockTaskMode(int taskId) {
8627        final TaskRecord task;
8628        long ident = Binder.clearCallingIdentity();
8629        try {
8630            synchronized (this) {
8631                task = mStackSupervisor.anyTaskForIdLocked(taskId);
8632            }
8633        } finally {
8634            Binder.restoreCallingIdentity(ident);
8635        }
8636        if (task != null) {
8637            startLockTaskMode(task);
8638        }
8639    }
8640
8641    @Override
8642    public void startLockTaskMode(IBinder token) {
8643        final TaskRecord task;
8644        long ident = Binder.clearCallingIdentity();
8645        try {
8646            synchronized (this) {
8647                final ActivityRecord r = ActivityRecord.forToken(token);
8648                if (r == null) {
8649                    return;
8650                }
8651                task = r.task;
8652            }
8653        } finally {
8654            Binder.restoreCallingIdentity(ident);
8655        }
8656        if (task != null) {
8657            startLockTaskMode(task);
8658        }
8659    }
8660
8661    @Override
8662    public void startLockTaskModeOnCurrent() throws RemoteException {
8663        checkCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS);
8664        ActivityRecord r = null;
8665        synchronized (this) {
8666            r = mStackSupervisor.topRunningActivityLocked();
8667        }
8668        startLockTaskMode(r.task);
8669    }
8670
8671    @Override
8672    public void stopLockTaskMode() {
8673        // Verify that the user matches the package of the intent for the TaskRecord
8674        // we are locked to or systtem.  This will ensure the same caller for startLockTaskMode
8675        // and stopLockTaskMode.
8676        final int callingUid = Binder.getCallingUid();
8677        if (callingUid != Process.SYSTEM_UID) {
8678            try {
8679                String pkg =
8680                        mStackSupervisor.mLockTaskModeTask.intent.getComponent().getPackageName();
8681                int uid = mContext.getPackageManager().getPackageUid(pkg,
8682                        Binder.getCallingUserHandle().getIdentifier());
8683                if (uid != callingUid) {
8684                    throw new SecurityException("Invalid uid, expected " + uid);
8685                }
8686            } catch (NameNotFoundException e) {
8687                Log.d(TAG, "stopLockTaskMode " + e);
8688                return;
8689            }
8690        }
8691        long ident = Binder.clearCallingIdentity();
8692        try {
8693            Log.d(TAG, "stopLockTaskMode");
8694            // Stop lock task
8695            synchronized (this) {
8696                mStackSupervisor.setLockTaskModeLocked(null, false);
8697            }
8698        } finally {
8699            Binder.restoreCallingIdentity(ident);
8700        }
8701    }
8702
8703    @Override
8704    public void stopLockTaskModeOnCurrent() throws RemoteException {
8705        checkCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS);
8706        long ident = Binder.clearCallingIdentity();
8707        try {
8708            stopLockTaskMode();
8709        } finally {
8710            Binder.restoreCallingIdentity(ident);
8711        }
8712    }
8713
8714    @Override
8715    public boolean isInLockTaskMode() {
8716        synchronized (this) {
8717            return mStackSupervisor.isInLockTaskMode();
8718        }
8719    }
8720
8721    // =========================================================
8722    // CONTENT PROVIDERS
8723    // =========================================================
8724
8725    private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) {
8726        List<ProviderInfo> providers = null;
8727        try {
8728            providers = AppGlobals.getPackageManager().
8729                queryContentProviders(app.processName, app.uid,
8730                        STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
8731        } catch (RemoteException ex) {
8732        }
8733        if (DEBUG_MU)
8734            Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid);
8735        int userId = app.userId;
8736        if (providers != null) {
8737            int N = providers.size();
8738            app.pubProviders.ensureCapacity(N + app.pubProviders.size());
8739            for (int i=0; i<N; i++) {
8740                ProviderInfo cpi =
8741                    (ProviderInfo)providers.get(i);
8742                boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo,
8743                        cpi.name, cpi.flags);
8744                if (singleton && UserHandle.getUserId(app.uid) != 0) {
8745                    // This is a singleton provider, but a user besides the
8746                    // default user is asking to initialize a process it runs
8747                    // in...  well, no, it doesn't actually run in this process,
8748                    // it runs in the process of the default user.  Get rid of it.
8749                    providers.remove(i);
8750                    N--;
8751                    i--;
8752                    continue;
8753                }
8754
8755                ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
8756                ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId);
8757                if (cpr == null) {
8758                    cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton);
8759                    mProviderMap.putProviderByClass(comp, cpr);
8760                }
8761                if (DEBUG_MU)
8762                    Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid);
8763                app.pubProviders.put(cpi.name, cpr);
8764                if (!cpi.multiprocess || !"android".equals(cpi.packageName)) {
8765                    // Don't add this if it is a platform component that is marked
8766                    // to run in multiple processes, because this is actually
8767                    // part of the framework so doesn't make sense to track as a
8768                    // separate apk in the process.
8769                    app.addPackage(cpi.applicationInfo.packageName, cpi.applicationInfo.versionCode,
8770                            mProcessStats);
8771                }
8772                ensurePackageDexOpt(cpi.applicationInfo.packageName);
8773            }
8774        }
8775        return providers;
8776    }
8777
8778    /**
8779     * Check if {@link ProcessRecord} has a possible chance at accessing the
8780     * given {@link ProviderInfo}. Final permission checking is always done
8781     * in {@link ContentProvider}.
8782     */
8783    private final String checkContentProviderPermissionLocked(
8784            ProviderInfo cpi, ProcessRecord r, int userId, boolean checkUser) {
8785        final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
8786        final int callingUid = (r != null) ? r.uid : Binder.getCallingUid();
8787        boolean checkedGrants = false;
8788        if (checkUser) {
8789            // Looking for cross-user grants before enforcing the typical cross-users permissions
8790            int tmpTargetUserId = unsafeConvertIncomingUser(userId);
8791            if (tmpTargetUserId != UserHandle.getUserId(callingUid)) {
8792                if (checkAuthorityGrants(callingUid, cpi, tmpTargetUserId, checkUser)) {
8793                    return null;
8794                }
8795                checkedGrants = true;
8796            }
8797            userId = handleIncomingUser(callingPid, callingUid, userId,
8798                    false, ALLOW_NON_FULL,
8799                    "checkContentProviderPermissionLocked " + cpi.authority, null);
8800            if (userId != tmpTargetUserId) {
8801                // When we actually went to determine the final targer user ID, this ended
8802                // up different than our initial check for the authority.  This is because
8803                // they had asked for USER_CURRENT_OR_SELF and we ended up switching to
8804                // SELF.  So we need to re-check the grants again.
8805                checkedGrants = false;
8806            }
8807        }
8808        if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
8809                cpi.applicationInfo.uid, cpi.exported)
8810                == PackageManager.PERMISSION_GRANTED) {
8811            return null;
8812        }
8813        if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
8814                cpi.applicationInfo.uid, cpi.exported)
8815                == PackageManager.PERMISSION_GRANTED) {
8816            return null;
8817        }
8818
8819        PathPermission[] pps = cpi.pathPermissions;
8820        if (pps != null) {
8821            int i = pps.length;
8822            while (i > 0) {
8823                i--;
8824                PathPermission pp = pps[i];
8825                String pprperm = pp.getReadPermission();
8826                if (pprperm != null && checkComponentPermission(pprperm, callingPid, callingUid,
8827                        cpi.applicationInfo.uid, cpi.exported)
8828                        == PackageManager.PERMISSION_GRANTED) {
8829                    return null;
8830                }
8831                String ppwperm = pp.getWritePermission();
8832                if (ppwperm != null && checkComponentPermission(ppwperm, callingPid, callingUid,
8833                        cpi.applicationInfo.uid, cpi.exported)
8834                        == PackageManager.PERMISSION_GRANTED) {
8835                    return null;
8836                }
8837            }
8838        }
8839        if (!checkedGrants && checkAuthorityGrants(callingUid, cpi, userId, checkUser)) {
8840            return null;
8841        }
8842
8843        String msg;
8844        if (!cpi.exported) {
8845            msg = "Permission Denial: opening provider " + cpi.name
8846                    + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
8847                    + ", uid=" + callingUid + ") that is not exported from uid "
8848                    + cpi.applicationInfo.uid;
8849        } else {
8850            msg = "Permission Denial: opening provider " + cpi.name
8851                    + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
8852                    + ", uid=" + callingUid + ") requires "
8853                    + cpi.readPermission + " or " + cpi.writePermission;
8854        }
8855        Slog.w(TAG, msg);
8856        return msg;
8857    }
8858
8859    /**
8860     * Returns if the ContentProvider has granted a uri to callingUid
8861     */
8862    boolean checkAuthorityGrants(int callingUid, ProviderInfo cpi, int userId, boolean checkUser) {
8863        final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
8864        if (perms != null) {
8865            for (int i=perms.size()-1; i>=0; i--) {
8866                GrantUri grantUri = perms.keyAt(i);
8867                if (grantUri.sourceUserId == userId || !checkUser) {
8868                    if (matchesProvider(grantUri.uri, cpi)) {
8869                        return true;
8870                    }
8871                }
8872            }
8873        }
8874        return false;
8875    }
8876
8877    /**
8878     * Returns true if the uri authority is one of the authorities specified in the provider.
8879     */
8880    boolean matchesProvider(Uri uri, ProviderInfo cpi) {
8881        String uriAuth = uri.getAuthority();
8882        String cpiAuth = cpi.authority;
8883        if (cpiAuth.indexOf(';') == -1) {
8884            return cpiAuth.equals(uriAuth);
8885        }
8886        String[] cpiAuths = cpiAuth.split(";");
8887        int length = cpiAuths.length;
8888        for (int i = 0; i < length; i++) {
8889            if (cpiAuths[i].equals(uriAuth)) return true;
8890        }
8891        return false;
8892    }
8893
8894    ContentProviderConnection incProviderCountLocked(ProcessRecord r,
8895            final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
8896        if (r != null) {
8897            for (int i=0; i<r.conProviders.size(); i++) {
8898                ContentProviderConnection conn = r.conProviders.get(i);
8899                if (conn.provider == cpr) {
8900                    if (DEBUG_PROVIDER) Slog.v(TAG,
8901                            "Adding provider requested by "
8902                            + r.processName + " from process "
8903                            + cpr.info.processName + ": " + cpr.name.flattenToShortString()
8904                            + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
8905                    if (stable) {
8906                        conn.stableCount++;
8907                        conn.numStableIncs++;
8908                    } else {
8909                        conn.unstableCount++;
8910                        conn.numUnstableIncs++;
8911                    }
8912                    return conn;
8913                }
8914            }
8915            ContentProviderConnection conn = new ContentProviderConnection(cpr, r);
8916            if (stable) {
8917                conn.stableCount = 1;
8918                conn.numStableIncs = 1;
8919            } else {
8920                conn.unstableCount = 1;
8921                conn.numUnstableIncs = 1;
8922            }
8923            cpr.connections.add(conn);
8924            r.conProviders.add(conn);
8925            return conn;
8926        }
8927        cpr.addExternalProcessHandleLocked(externalProcessToken);
8928        return null;
8929    }
8930
8931    boolean decProviderCountLocked(ContentProviderConnection conn,
8932            ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
8933        if (conn != null) {
8934            cpr = conn.provider;
8935            if (DEBUG_PROVIDER) Slog.v(TAG,
8936                    "Removing provider requested by "
8937                    + conn.client.processName + " from process "
8938                    + cpr.info.processName + ": " + cpr.name.flattenToShortString()
8939                    + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
8940            if (stable) {
8941                conn.stableCount--;
8942            } else {
8943                conn.unstableCount--;
8944            }
8945            if (conn.stableCount == 0 && conn.unstableCount == 0) {
8946                cpr.connections.remove(conn);
8947                conn.client.conProviders.remove(conn);
8948                return true;
8949            }
8950            return false;
8951        }
8952        cpr.removeExternalProcessHandleLocked(externalProcessToken);
8953        return false;
8954    }
8955
8956    private void checkTime(long startTime, String where) {
8957        long now = SystemClock.elapsedRealtime();
8958        if ((now-startTime) > 1000) {
8959            // If we are taking more than a second, log about it.
8960            Slog.w(TAG, "Slow operation: " + (now-startTime) + "ms so far, now at " + where);
8961        }
8962    }
8963
8964    private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller,
8965            String name, IBinder token, boolean stable, int userId) {
8966        ContentProviderRecord cpr;
8967        ContentProviderConnection conn = null;
8968        ProviderInfo cpi = null;
8969
8970        synchronized(this) {
8971            long startTime = SystemClock.elapsedRealtime();
8972
8973            ProcessRecord r = null;
8974            if (caller != null) {
8975                r = getRecordForAppLocked(caller);
8976                if (r == null) {
8977                    throw new SecurityException(
8978                            "Unable to find app for caller " + caller
8979                          + " (pid=" + Binder.getCallingPid()
8980                          + ") when getting content provider " + name);
8981                }
8982            }
8983
8984            boolean checkCrossUser = true;
8985
8986            checkTime(startTime, "getContentProviderImpl: getProviderByName");
8987
8988            // First check if this content provider has been published...
8989            cpr = mProviderMap.getProviderByName(name, userId);
8990            // If that didn't work, check if it exists for user 0 and then
8991            // verify that it's a singleton provider before using it.
8992            if (cpr == null && userId != UserHandle.USER_OWNER) {
8993                cpr = mProviderMap.getProviderByName(name, UserHandle.USER_OWNER);
8994                if (cpr != null) {
8995                    cpi = cpr.info;
8996                    if (isSingleton(cpi.processName, cpi.applicationInfo,
8997                            cpi.name, cpi.flags)
8998                            && isValidSingletonCall(r.uid, cpi.applicationInfo.uid)) {
8999                        userId = UserHandle.USER_OWNER;
9000                        checkCrossUser = false;
9001                    } else {
9002                        cpr = null;
9003                        cpi = null;
9004                    }
9005                }
9006            }
9007
9008            boolean providerRunning = cpr != null;
9009            if (providerRunning) {
9010                cpi = cpr.info;
9011                String msg;
9012                checkTime(startTime, "getContentProviderImpl: before checkContentProviderPermission");
9013                if ((msg = checkContentProviderPermissionLocked(cpi, r, userId, checkCrossUser))
9014                        != null) {
9015                    throw new SecurityException(msg);
9016                }
9017                checkTime(startTime, "getContentProviderImpl: after checkContentProviderPermission");
9018
9019                if (r != null && cpr.canRunHere(r)) {
9020                    // This provider has been published or is in the process
9021                    // of being published...  but it is also allowed to run
9022                    // in the caller's process, so don't make a connection
9023                    // and just let the caller instantiate its own instance.
9024                    ContentProviderHolder holder = cpr.newHolder(null);
9025                    // don't give caller the provider object, it needs
9026                    // to make its own.
9027                    holder.provider = null;
9028                    return holder;
9029                }
9030
9031                final long origId = Binder.clearCallingIdentity();
9032
9033                checkTime(startTime, "getContentProviderImpl: incProviderCountLocked");
9034
9035                // In this case the provider instance already exists, so we can
9036                // return it right away.
9037                conn = incProviderCountLocked(r, cpr, token, stable);
9038                if (conn != null && (conn.stableCount+conn.unstableCount) == 1) {
9039                    if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
9040                        // If this is a perceptible app accessing the provider,
9041                        // make sure to count it as being accessed and thus
9042                        // back up on the LRU list.  This is good because
9043                        // content providers are often expensive to start.
9044                        checkTime(startTime, "getContentProviderImpl: before updateLruProcess");
9045                        updateLruProcessLocked(cpr.proc, false, null);
9046                        checkTime(startTime, "getContentProviderImpl: after updateLruProcess");
9047                    }
9048                }
9049
9050                if (cpr.proc != null) {
9051                    if (false) {
9052                        if (cpr.name.flattenToShortString().equals(
9053                                "com.android.providers.calendar/.CalendarProvider2")) {
9054                            Slog.v(TAG, "****************** KILLING "
9055                                + cpr.name.flattenToShortString());
9056                            Process.killProcess(cpr.proc.pid);
9057                        }
9058                    }
9059                    checkTime(startTime, "getContentProviderImpl: before updateOomAdj");
9060                    boolean success = updateOomAdjLocked(cpr.proc);
9061                    checkTime(startTime, "getContentProviderImpl: after updateOomAdj");
9062                    if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success);
9063                    // NOTE: there is still a race here where a signal could be
9064                    // pending on the process even though we managed to update its
9065                    // adj level.  Not sure what to do about this, but at least
9066                    // the race is now smaller.
9067                    if (!success) {
9068                        // Uh oh...  it looks like the provider's process
9069                        // has been killed on us.  We need to wait for a new
9070                        // process to be started, and make sure its death
9071                        // doesn't kill our process.
9072                        Slog.i(TAG,
9073                                "Existing provider " + cpr.name.flattenToShortString()
9074                                + " is crashing; detaching " + r);
9075                        boolean lastRef = decProviderCountLocked(conn, cpr, token, stable);
9076                        checkTime(startTime, "getContentProviderImpl: before appDied");
9077                        appDiedLocked(cpr.proc);
9078                        checkTime(startTime, "getContentProviderImpl: after appDied");
9079                        if (!lastRef) {
9080                            // This wasn't the last ref our process had on
9081                            // the provider...  we have now been killed, bail.
9082                            return null;
9083                        }
9084                        providerRunning = false;
9085                        conn = null;
9086                    }
9087                }
9088
9089                Binder.restoreCallingIdentity(origId);
9090            }
9091
9092            boolean singleton;
9093            if (!providerRunning) {
9094                try {
9095                    checkTime(startTime, "getContentProviderImpl: before resolveContentProvider");
9096                    cpi = AppGlobals.getPackageManager().
9097                        resolveContentProvider(name,
9098                            STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
9099                    checkTime(startTime, "getContentProviderImpl: after resolveContentProvider");
9100                } catch (RemoteException ex) {
9101                }
9102                if (cpi == null) {
9103                    return null;
9104                }
9105                // If the provider is a singleton AND
9106                // (it's a call within the same user || the provider is a
9107                // privileged app)
9108                // Then allow connecting to the singleton provider
9109                singleton = isSingleton(cpi.processName, cpi.applicationInfo,
9110                        cpi.name, cpi.flags)
9111                        && isValidSingletonCall(r.uid, cpi.applicationInfo.uid);
9112                if (singleton) {
9113                    userId = UserHandle.USER_OWNER;
9114                }
9115                cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId);
9116                checkTime(startTime, "getContentProviderImpl: got app info for user");
9117
9118                String msg;
9119                checkTime(startTime, "getContentProviderImpl: before checkContentProviderPermission");
9120                if ((msg = checkContentProviderPermissionLocked(cpi, r, userId, !singleton))
9121                        != null) {
9122                    throw new SecurityException(msg);
9123                }
9124                checkTime(startTime, "getContentProviderImpl: after checkContentProviderPermission");
9125
9126                if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate
9127                        && !cpi.processName.equals("system")) {
9128                    // If this content provider does not run in the system
9129                    // process, and the system is not yet ready to run other
9130                    // processes, then fail fast instead of hanging.
9131                    throw new IllegalArgumentException(
9132                            "Attempt to launch content provider before system ready");
9133                }
9134
9135                // Make sure that the user who owns this provider is started.  If not,
9136                // we don't want to allow it to run.
9137                if (mStartedUsers.get(userId) == null) {
9138                    Slog.w(TAG, "Unable to launch app "
9139                            + cpi.applicationInfo.packageName + "/"
9140                            + cpi.applicationInfo.uid + " for provider "
9141                            + name + ": user " + userId + " is stopped");
9142                    return null;
9143                }
9144
9145                ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
9146                checkTime(startTime, "getContentProviderImpl: before getProviderByClass");
9147                cpr = mProviderMap.getProviderByClass(comp, userId);
9148                checkTime(startTime, "getContentProviderImpl: after getProviderByClass");
9149                final boolean firstClass = cpr == null;
9150                if (firstClass) {
9151                    try {
9152                        checkTime(startTime, "getContentProviderImpl: before getApplicationInfo");
9153                        ApplicationInfo ai =
9154                            AppGlobals.getPackageManager().
9155                                getApplicationInfo(
9156                                        cpi.applicationInfo.packageName,
9157                                        STOCK_PM_FLAGS, userId);
9158                        checkTime(startTime, "getContentProviderImpl: after getApplicationInfo");
9159                        if (ai == null) {
9160                            Slog.w(TAG, "No package info for content provider "
9161                                    + cpi.name);
9162                            return null;
9163                        }
9164                        ai = getAppInfoForUser(ai, userId);
9165                        cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton);
9166                    } catch (RemoteException ex) {
9167                        // pm is in same process, this will never happen.
9168                    }
9169                }
9170
9171                checkTime(startTime, "getContentProviderImpl: now have ContentProviderRecord");
9172
9173                if (r != null && cpr.canRunHere(r)) {
9174                    // If this is a multiprocess provider, then just return its
9175                    // info and allow the caller to instantiate it.  Only do
9176                    // this if the provider is the same user as the caller's
9177                    // process, or can run as root (so can be in any process).
9178                    return cpr.newHolder(null);
9179                }
9180
9181                if (DEBUG_PROVIDER) {
9182                    RuntimeException e = new RuntimeException("here");
9183                    Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + (r != null ? r.uid : null)
9184                          + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
9185                }
9186
9187                // This is single process, and our app is now connecting to it.
9188                // See if we are already in the process of launching this
9189                // provider.
9190                final int N = mLaunchingProviders.size();
9191                int i;
9192                for (i=0; i<N; i++) {
9193                    if (mLaunchingProviders.get(i) == cpr) {
9194                        break;
9195                    }
9196                }
9197
9198                // If the provider is not already being launched, then get it
9199                // started.
9200                if (i >= N) {
9201                    final long origId = Binder.clearCallingIdentity();
9202
9203                    try {
9204                        // Content provider is now in use, its package can't be stopped.
9205                        try {
9206                            checkTime(startTime, "getContentProviderImpl: before set stopped state");
9207                            AppGlobals.getPackageManager().setPackageStoppedState(
9208                                    cpr.appInfo.packageName, false, userId);
9209                            checkTime(startTime, "getContentProviderImpl: after set stopped state");
9210                        } catch (RemoteException e) {
9211                        } catch (IllegalArgumentException e) {
9212                            Slog.w(TAG, "Failed trying to unstop package "
9213                                    + cpr.appInfo.packageName + ": " + e);
9214                        }
9215
9216                        // Use existing process if already started
9217                        checkTime(startTime, "getContentProviderImpl: looking for process record");
9218                        ProcessRecord proc = getProcessRecordLocked(
9219                                cpi.processName, cpr.appInfo.uid, false);
9220                        if (proc != null && proc.thread != null) {
9221                            if (DEBUG_PROVIDER) {
9222                                Slog.d(TAG, "Installing in existing process " + proc);
9223                            }
9224                            checkTime(startTime, "getContentProviderImpl: scheduling install");
9225                            proc.pubProviders.put(cpi.name, cpr);
9226                            try {
9227                                proc.thread.scheduleInstallProvider(cpi);
9228                            } catch (RemoteException e) {
9229                            }
9230                        } else {
9231                            checkTime(startTime, "getContentProviderImpl: before start process");
9232                            proc = startProcessLocked(cpi.processName,
9233                                    cpr.appInfo, false, 0, "content provider",
9234                                    new ComponentName(cpi.applicationInfo.packageName,
9235                                            cpi.name), false, false, false);
9236                            checkTime(startTime, "getContentProviderImpl: after start process");
9237                            if (proc == null) {
9238                                Slog.w(TAG, "Unable to launch app "
9239                                        + cpi.applicationInfo.packageName + "/"
9240                                        + cpi.applicationInfo.uid + " for provider "
9241                                        + name + ": process is bad");
9242                                return null;
9243                            }
9244                        }
9245                        cpr.launchingApp = proc;
9246                        mLaunchingProviders.add(cpr);
9247                    } finally {
9248                        Binder.restoreCallingIdentity(origId);
9249                    }
9250                }
9251
9252                checkTime(startTime, "getContentProviderImpl: updating data structures");
9253
9254                // Make sure the provider is published (the same provider class
9255                // may be published under multiple names).
9256                if (firstClass) {
9257                    mProviderMap.putProviderByClass(comp, cpr);
9258                }
9259
9260                mProviderMap.putProviderByName(name, cpr);
9261                conn = incProviderCountLocked(r, cpr, token, stable);
9262                if (conn != null) {
9263                    conn.waiting = true;
9264                }
9265            }
9266            checkTime(startTime, "getContentProviderImpl: done!");
9267        }
9268
9269        // Wait for the provider to be published...
9270        synchronized (cpr) {
9271            while (cpr.provider == null) {
9272                if (cpr.launchingApp == null) {
9273                    Slog.w(TAG, "Unable to launch app "
9274                            + cpi.applicationInfo.packageName + "/"
9275                            + cpi.applicationInfo.uid + " for provider "
9276                            + name + ": launching app became null");
9277                    EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
9278                            UserHandle.getUserId(cpi.applicationInfo.uid),
9279                            cpi.applicationInfo.packageName,
9280                            cpi.applicationInfo.uid, name);
9281                    return null;
9282                }
9283                try {
9284                    if (DEBUG_MU) {
9285                        Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp="
9286                                + cpr.launchingApp);
9287                    }
9288                    if (conn != null) {
9289                        conn.waiting = true;
9290                    }
9291                    cpr.wait();
9292                } catch (InterruptedException ex) {
9293                } finally {
9294                    if (conn != null) {
9295                        conn.waiting = false;
9296                    }
9297                }
9298            }
9299        }
9300        return cpr != null ? cpr.newHolder(conn) : null;
9301    }
9302
9303    @Override
9304    public final ContentProviderHolder getContentProvider(
9305            IApplicationThread caller, String name, int userId, boolean stable) {
9306        enforceNotIsolatedCaller("getContentProvider");
9307        if (caller == null) {
9308            String msg = "null IApplicationThread when getting content provider "
9309                    + name;
9310            Slog.w(TAG, msg);
9311            throw new SecurityException(msg);
9312        }
9313        // The incoming user check is now handled in checkContentProviderPermissionLocked() to deal
9314        // with cross-user grant.
9315        return getContentProviderImpl(caller, name, null, stable, userId);
9316    }
9317
9318    public ContentProviderHolder getContentProviderExternal(
9319            String name, int userId, IBinder token) {
9320        enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
9321            "Do not have permission in call getContentProviderExternal()");
9322        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
9323                false, ALLOW_FULL_ONLY, "getContentProvider", null);
9324        return getContentProviderExternalUnchecked(name, token, userId);
9325    }
9326
9327    private ContentProviderHolder getContentProviderExternalUnchecked(String name,
9328            IBinder token, int userId) {
9329        return getContentProviderImpl(null, name, token, true, userId);
9330    }
9331
9332    /**
9333     * Drop a content provider from a ProcessRecord's bookkeeping
9334     */
9335    public void removeContentProvider(IBinder connection, boolean stable) {
9336        enforceNotIsolatedCaller("removeContentProvider");
9337        long ident = Binder.clearCallingIdentity();
9338        try {
9339            synchronized (this) {
9340                ContentProviderConnection conn;
9341                try {
9342                    conn = (ContentProviderConnection)connection;
9343                } catch (ClassCastException e) {
9344                    String msg ="removeContentProvider: " + connection
9345                            + " not a ContentProviderConnection";
9346                    Slog.w(TAG, msg);
9347                    throw new IllegalArgumentException(msg);
9348                }
9349                if (conn == null) {
9350                    throw new NullPointerException("connection is null");
9351                }
9352                if (decProviderCountLocked(conn, null, null, stable)) {
9353                    updateOomAdjLocked();
9354                }
9355            }
9356        } finally {
9357            Binder.restoreCallingIdentity(ident);
9358        }
9359    }
9360
9361    public void removeContentProviderExternal(String name, IBinder token) {
9362        enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
9363            "Do not have permission in call removeContentProviderExternal()");
9364        removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId());
9365    }
9366
9367    private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) {
9368        synchronized (this) {
9369            ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId);
9370            if(cpr == null) {
9371                //remove from mProvidersByClass
9372                if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
9373                return;
9374            }
9375
9376            //update content provider record entry info
9377            ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name);
9378            ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId);
9379            if (localCpr.hasExternalProcessHandles()) {
9380                if (localCpr.removeExternalProcessHandleLocked(token)) {
9381                    updateOomAdjLocked();
9382                } else {
9383                    Slog.e(TAG, "Attmpt to remove content provider " + localCpr
9384                            + " with no external reference for token: "
9385                            + token + ".");
9386                }
9387            } else {
9388                Slog.e(TAG, "Attmpt to remove content provider: " + localCpr
9389                        + " with no external references.");
9390            }
9391        }
9392    }
9393
9394    public final void publishContentProviders(IApplicationThread caller,
9395            List<ContentProviderHolder> providers) {
9396        if (providers == null) {
9397            return;
9398        }
9399
9400        enforceNotIsolatedCaller("publishContentProviders");
9401        synchronized (this) {
9402            final ProcessRecord r = getRecordForAppLocked(caller);
9403            if (DEBUG_MU)
9404                Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid);
9405            if (r == null) {
9406                throw new SecurityException(
9407                        "Unable to find app for caller " + caller
9408                      + " (pid=" + Binder.getCallingPid()
9409                      + ") when publishing content providers");
9410            }
9411
9412            final long origId = Binder.clearCallingIdentity();
9413
9414            final int N = providers.size();
9415            for (int i=0; i<N; i++) {
9416                ContentProviderHolder src = providers.get(i);
9417                if (src == null || src.info == null || src.provider == null) {
9418                    continue;
9419                }
9420                ContentProviderRecord dst = r.pubProviders.get(src.info.name);
9421                if (DEBUG_MU)
9422                    Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid);
9423                if (dst != null) {
9424                    ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name);
9425                    mProviderMap.putProviderByClass(comp, dst);
9426                    String names[] = dst.info.authority.split(";");
9427                    for (int j = 0; j < names.length; j++) {
9428                        mProviderMap.putProviderByName(names[j], dst);
9429                    }
9430
9431                    int NL = mLaunchingProviders.size();
9432                    int j;
9433                    for (j=0; j<NL; j++) {
9434                        if (mLaunchingProviders.get(j) == dst) {
9435                            mLaunchingProviders.remove(j);
9436                            j--;
9437                            NL--;
9438                        }
9439                    }
9440                    synchronized (dst) {
9441                        dst.provider = src.provider;
9442                        dst.proc = r;
9443                        dst.notifyAll();
9444                    }
9445                    updateOomAdjLocked(r);
9446                }
9447            }
9448
9449            Binder.restoreCallingIdentity(origId);
9450        }
9451    }
9452
9453    public boolean refContentProvider(IBinder connection, int stable, int unstable) {
9454        ContentProviderConnection conn;
9455        try {
9456            conn = (ContentProviderConnection)connection;
9457        } catch (ClassCastException e) {
9458            String msg ="refContentProvider: " + connection
9459                    + " not a ContentProviderConnection";
9460            Slog.w(TAG, msg);
9461            throw new IllegalArgumentException(msg);
9462        }
9463        if (conn == null) {
9464            throw new NullPointerException("connection is null");
9465        }
9466
9467        synchronized (this) {
9468            if (stable > 0) {
9469                conn.numStableIncs += stable;
9470            }
9471            stable = conn.stableCount + stable;
9472            if (stable < 0) {
9473                throw new IllegalStateException("stableCount < 0: " + stable);
9474            }
9475
9476            if (unstable > 0) {
9477                conn.numUnstableIncs += unstable;
9478            }
9479            unstable = conn.unstableCount + unstable;
9480            if (unstable < 0) {
9481                throw new IllegalStateException("unstableCount < 0: " + unstable);
9482            }
9483
9484            if ((stable+unstable) <= 0) {
9485                throw new IllegalStateException("ref counts can't go to zero here: stable="
9486                        + stable + " unstable=" + unstable);
9487            }
9488            conn.stableCount = stable;
9489            conn.unstableCount = unstable;
9490            return !conn.dead;
9491        }
9492    }
9493
9494    public void unstableProviderDied(IBinder connection) {
9495        ContentProviderConnection conn;
9496        try {
9497            conn = (ContentProviderConnection)connection;
9498        } catch (ClassCastException e) {
9499            String msg ="refContentProvider: " + connection
9500                    + " not a ContentProviderConnection";
9501            Slog.w(TAG, msg);
9502            throw new IllegalArgumentException(msg);
9503        }
9504        if (conn == null) {
9505            throw new NullPointerException("connection is null");
9506        }
9507
9508        // Safely retrieve the content provider associated with the connection.
9509        IContentProvider provider;
9510        synchronized (this) {
9511            provider = conn.provider.provider;
9512        }
9513
9514        if (provider == null) {
9515            // Um, yeah, we're way ahead of you.
9516            return;
9517        }
9518
9519        // Make sure the caller is being honest with us.
9520        if (provider.asBinder().pingBinder()) {
9521            // Er, no, still looks good to us.
9522            synchronized (this) {
9523                Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid()
9524                        + " says " + conn + " died, but we don't agree");
9525                return;
9526            }
9527        }
9528
9529        // Well look at that!  It's dead!
9530        synchronized (this) {
9531            if (conn.provider.provider != provider) {
9532                // But something changed...  good enough.
9533                return;
9534            }
9535
9536            ProcessRecord proc = conn.provider.proc;
9537            if (proc == null || proc.thread == null) {
9538                // Seems like the process is already cleaned up.
9539                return;
9540            }
9541
9542            // As far as we're concerned, this is just like receiving a
9543            // death notification...  just a bit prematurely.
9544            Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid
9545                    + ") early provider death");
9546            final long ident = Binder.clearCallingIdentity();
9547            try {
9548                appDiedLocked(proc);
9549            } finally {
9550                Binder.restoreCallingIdentity(ident);
9551            }
9552        }
9553    }
9554
9555    @Override
9556    public void appNotRespondingViaProvider(IBinder connection) {
9557        enforceCallingPermission(
9558                android.Manifest.permission.REMOVE_TASKS, "appNotRespondingViaProvider()");
9559
9560        final ContentProviderConnection conn = (ContentProviderConnection) connection;
9561        if (conn == null) {
9562            Slog.w(TAG, "ContentProviderConnection is null");
9563            return;
9564        }
9565
9566        final ProcessRecord host = conn.provider.proc;
9567        if (host == null) {
9568            Slog.w(TAG, "Failed to find hosting ProcessRecord");
9569            return;
9570        }
9571
9572        final long token = Binder.clearCallingIdentity();
9573        try {
9574            appNotResponding(host, null, null, false, "ContentProvider not responding");
9575        } finally {
9576            Binder.restoreCallingIdentity(token);
9577        }
9578    }
9579
9580    public final void installSystemProviders() {
9581        List<ProviderInfo> providers;
9582        synchronized (this) {
9583            ProcessRecord app = mProcessNames.get("system", Process.SYSTEM_UID);
9584            providers = generateApplicationProvidersLocked(app);
9585            if (providers != null) {
9586                for (int i=providers.size()-1; i>=0; i--) {
9587                    ProviderInfo pi = (ProviderInfo)providers.get(i);
9588                    if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
9589                        Slog.w(TAG, "Not installing system proc provider " + pi.name
9590                                + ": not system .apk");
9591                        providers.remove(i);
9592                    }
9593                }
9594            }
9595        }
9596        if (providers != null) {
9597            mSystemThread.installSystemProviders(providers);
9598        }
9599
9600        mCoreSettingsObserver = new CoreSettingsObserver(this);
9601
9602        //mUsageStatsService.monitorPackages();
9603    }
9604
9605    /**
9606     * Allows apps to retrieve the MIME type of a URI.
9607     * If an app is in the same user as the ContentProvider, or if it is allowed to interact across
9608     * users, then it does not need permission to access the ContentProvider.
9609     * Either, it needs cross-user uri grants.
9610     *
9611     * CTS tests for this functionality can be run with "runtest cts-appsecurity".
9612     *
9613     * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
9614     *     src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
9615     */
9616    public String getProviderMimeType(Uri uri, int userId) {
9617        enforceNotIsolatedCaller("getProviderMimeType");
9618        final String name = uri.getAuthority();
9619        int callingUid = Binder.getCallingUid();
9620        int callingPid = Binder.getCallingPid();
9621        long ident = 0;
9622        boolean clearedIdentity = false;
9623        userId = unsafeConvertIncomingUser(userId);
9624        if (UserHandle.getUserId(callingUid) != userId) {
9625            if (checkComponentPermission(INTERACT_ACROSS_USERS, callingPid,
9626                    callingUid, -1, true) == PackageManager.PERMISSION_GRANTED
9627                    || checkComponentPermission(INTERACT_ACROSS_USERS_FULL, callingPid,
9628                    callingUid, -1, true) == PackageManager.PERMISSION_GRANTED) {
9629                clearedIdentity = true;
9630                ident = Binder.clearCallingIdentity();
9631            }
9632        }
9633        ContentProviderHolder holder = null;
9634        try {
9635            holder = getContentProviderExternalUnchecked(name, null, userId);
9636            if (holder != null) {
9637                return holder.provider.getType(uri);
9638            }
9639        } catch (RemoteException e) {
9640            Log.w(TAG, "Content provider dead retrieving " + uri, e);
9641            return null;
9642        } finally {
9643            // We need to clear the identity to call removeContentProviderExternalUnchecked
9644            if (!clearedIdentity) {
9645                ident = Binder.clearCallingIdentity();
9646            }
9647            try {
9648                if (holder != null) {
9649                    removeContentProviderExternalUnchecked(name, null, userId);
9650                }
9651            } finally {
9652                Binder.restoreCallingIdentity(ident);
9653            }
9654        }
9655
9656        return null;
9657    }
9658
9659    // =========================================================
9660    // GLOBAL MANAGEMENT
9661    // =========================================================
9662
9663    final ProcessRecord newProcessRecordLocked(ApplicationInfo info, String customProcess,
9664            boolean isolated, int isolatedUid) {
9665        String proc = customProcess != null ? customProcess : info.processName;
9666        BatteryStatsImpl.Uid.Proc ps = null;
9667        BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
9668        int uid = info.uid;
9669        if (isolated) {
9670            if (isolatedUid == 0) {
9671                int userId = UserHandle.getUserId(uid);
9672                int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1;
9673                while (true) {
9674                    if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID
9675                            || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) {
9676                        mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID;
9677                    }
9678                    uid = UserHandle.getUid(userId, mNextIsolatedProcessUid);
9679                    mNextIsolatedProcessUid++;
9680                    if (mIsolatedProcesses.indexOfKey(uid) < 0) {
9681                        // No process for this uid, use it.
9682                        break;
9683                    }
9684                    stepsLeft--;
9685                    if (stepsLeft <= 0) {
9686                        return null;
9687                    }
9688                }
9689            } else {
9690                // Special case for startIsolatedProcess (internal only), where
9691                // the uid of the isolated process is specified by the caller.
9692                uid = isolatedUid;
9693            }
9694        }
9695        return new ProcessRecord(stats, info, proc, uid);
9696    }
9697
9698    final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated,
9699            String abiOverride) {
9700        ProcessRecord app;
9701        if (!isolated) {
9702            app = getProcessRecordLocked(info.processName, info.uid, true);
9703        } else {
9704            app = null;
9705        }
9706
9707        if (app == null) {
9708            app = newProcessRecordLocked(info, null, isolated, 0);
9709            mProcessNames.put(info.processName, app.uid, app);
9710            if (isolated) {
9711                mIsolatedProcesses.put(app.uid, app);
9712            }
9713            updateLruProcessLocked(app, false, null);
9714            updateOomAdjLocked();
9715        }
9716
9717        // This package really, really can not be stopped.
9718        try {
9719            AppGlobals.getPackageManager().setPackageStoppedState(
9720                    info.packageName, false, UserHandle.getUserId(app.uid));
9721        } catch (RemoteException e) {
9722        } catch (IllegalArgumentException e) {
9723            Slog.w(TAG, "Failed trying to unstop package "
9724                    + info.packageName + ": " + e);
9725        }
9726
9727        if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
9728                == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
9729            app.persistent = true;
9730            app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ;
9731        }
9732        if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
9733            mPersistentStartingProcesses.add(app);
9734            startProcessLocked(app, "added application", app.processName, abiOverride,
9735                    null /* entryPoint */, null /* entryPointArgs */);
9736        }
9737
9738        return app;
9739    }
9740
9741    public void unhandledBack() {
9742        enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
9743                "unhandledBack()");
9744
9745        synchronized(this) {
9746            final long origId = Binder.clearCallingIdentity();
9747            try {
9748                getFocusedStack().unhandledBackLocked();
9749            } finally {
9750                Binder.restoreCallingIdentity(origId);
9751            }
9752        }
9753    }
9754
9755    public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
9756        enforceNotIsolatedCaller("openContentUri");
9757        final int userId = UserHandle.getCallingUserId();
9758        String name = uri.getAuthority();
9759        ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId);
9760        ParcelFileDescriptor pfd = null;
9761        if (cph != null) {
9762            // We record the binder invoker's uid in thread-local storage before
9763            // going to the content provider to open the file.  Later, in the code
9764            // that handles all permissions checks, we look for this uid and use
9765            // that rather than the Activity Manager's own uid.  The effect is that
9766            // we do the check against the caller's permissions even though it looks
9767            // to the content provider like the Activity Manager itself is making
9768            // the request.
9769            sCallerIdentity.set(new Identity(
9770                    Binder.getCallingPid(), Binder.getCallingUid()));
9771            try {
9772                pfd = cph.provider.openFile(null, uri, "r", null);
9773            } catch (FileNotFoundException e) {
9774                // do nothing; pfd will be returned null
9775            } finally {
9776                // Ensure that whatever happens, we clean up the identity state
9777                sCallerIdentity.remove();
9778            }
9779
9780            // We've got the fd now, so we're done with the provider.
9781            removeContentProviderExternalUnchecked(name, null, userId);
9782        } else {
9783            Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
9784        }
9785        return pfd;
9786    }
9787
9788    // Actually is sleeping or shutting down or whatever else in the future
9789    // is an inactive state.
9790    public boolean isSleepingOrShuttingDown() {
9791        return mSleeping || mShuttingDown;
9792    }
9793
9794    public boolean isSleeping() {
9795        return mSleeping;
9796    }
9797
9798    void goingToSleep() {
9799        synchronized(this) {
9800            mWentToSleep = true;
9801            updateEventDispatchingLocked();
9802            goToSleepIfNeededLocked();
9803        }
9804    }
9805
9806    void finishRunningVoiceLocked() {
9807        if (mRunningVoice) {
9808            mRunningVoice = false;
9809            goToSleepIfNeededLocked();
9810        }
9811    }
9812
9813    void goToSleepIfNeededLocked() {
9814        if (mWentToSleep && !mRunningVoice) {
9815            if (!mSleeping) {
9816                mSleeping = true;
9817                mStackSupervisor.goingToSleepLocked();
9818
9819                // Initialize the wake times of all processes.
9820                checkExcessivePowerUsageLocked(false);
9821                mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
9822                Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
9823                mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
9824            }
9825        }
9826    }
9827
9828    void notifyTaskPersisterLocked(TaskRecord task, boolean flush) {
9829        if (task != null && task.stack != null && task.stack.isHomeStack()) {
9830            // Never persist the home stack.
9831            return;
9832        }
9833        mTaskPersister.wakeup(task, flush);
9834    }
9835
9836    @Override
9837    public boolean shutdown(int timeout) {
9838        if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
9839                != PackageManager.PERMISSION_GRANTED) {
9840            throw new SecurityException("Requires permission "
9841                    + android.Manifest.permission.SHUTDOWN);
9842        }
9843
9844        boolean timedout = false;
9845
9846        synchronized(this) {
9847            mShuttingDown = true;
9848            updateEventDispatchingLocked();
9849            timedout = mStackSupervisor.shutdownLocked(timeout);
9850        }
9851
9852        mAppOpsService.shutdown();
9853        if (mUsageStatsService != null) {
9854            mUsageStatsService.prepareShutdown();
9855        }
9856        mBatteryStatsService.shutdown();
9857        synchronized (this) {
9858            mProcessStats.shutdownLocked();
9859        }
9860        notifyTaskPersisterLocked(null, true);
9861
9862        return timedout;
9863    }
9864
9865    public final void activitySlept(IBinder token) {
9866        if (localLOGV) Slog.v(TAG, "Activity slept: token=" + token);
9867
9868        final long origId = Binder.clearCallingIdentity();
9869
9870        synchronized (this) {
9871            final ActivityRecord r = ActivityRecord.isInStackLocked(token);
9872            if (r != null) {
9873                mStackSupervisor.activitySleptLocked(r);
9874            }
9875        }
9876
9877        Binder.restoreCallingIdentity(origId);
9878    }
9879
9880    void logLockScreen(String msg) {
9881        if (DEBUG_LOCKSCREEN) Slog.d(TAG, Debug.getCallers(2) + ":" + msg +
9882                " mLockScreenShown=" + mLockScreenShown + " mWentToSleep=" +
9883                mWentToSleep + " mSleeping=" + mSleeping);
9884    }
9885
9886    private void comeOutOfSleepIfNeededLocked() {
9887        if ((!mWentToSleep && !mLockScreenShown) || mRunningVoice) {
9888            if (mSleeping) {
9889                mSleeping = false;
9890                mStackSupervisor.comeOutOfSleepIfNeededLocked();
9891            }
9892        }
9893    }
9894
9895    void wakingUp() {
9896        synchronized(this) {
9897            mWentToSleep = false;
9898            updateEventDispatchingLocked();
9899            comeOutOfSleepIfNeededLocked();
9900        }
9901    }
9902
9903    void startRunningVoiceLocked() {
9904        if (!mRunningVoice) {
9905            mRunningVoice = true;
9906            comeOutOfSleepIfNeededLocked();
9907        }
9908    }
9909
9910    private void updateEventDispatchingLocked() {
9911        mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown);
9912    }
9913
9914    public void setLockScreenShown(boolean shown) {
9915        if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
9916                != PackageManager.PERMISSION_GRANTED) {
9917            throw new SecurityException("Requires permission "
9918                    + android.Manifest.permission.DEVICE_POWER);
9919        }
9920
9921        synchronized(this) {
9922            long ident = Binder.clearCallingIdentity();
9923            try {
9924                if (DEBUG_LOCKSCREEN) logLockScreen(" shown=" + shown);
9925                mLockScreenShown = shown;
9926                comeOutOfSleepIfNeededLocked();
9927            } finally {
9928                Binder.restoreCallingIdentity(ident);
9929            }
9930        }
9931    }
9932
9933    @Override
9934    public void stopAppSwitches() {
9935        if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
9936                != PackageManager.PERMISSION_GRANTED) {
9937            throw new SecurityException("Requires permission "
9938                    + android.Manifest.permission.STOP_APP_SWITCHES);
9939        }
9940
9941        synchronized(this) {
9942            mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
9943                    + APP_SWITCH_DELAY_TIME;
9944            mDidAppSwitch = false;
9945            mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
9946            Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
9947            mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
9948        }
9949    }
9950
9951    public void resumeAppSwitches() {
9952        if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
9953                != PackageManager.PERMISSION_GRANTED) {
9954            throw new SecurityException("Requires permission "
9955                    + android.Manifest.permission.STOP_APP_SWITCHES);
9956        }
9957
9958        synchronized(this) {
9959            // Note that we don't execute any pending app switches... we will
9960            // let those wait until either the timeout, or the next start
9961            // activity request.
9962            mAppSwitchesAllowedTime = 0;
9963        }
9964    }
9965
9966    boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid,
9967            String name) {
9968        if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
9969            return true;
9970        }
9971
9972        final int perm = checkComponentPermission(
9973                android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
9974                callingUid, -1, true);
9975        if (perm == PackageManager.PERMISSION_GRANTED) {
9976            return true;
9977        }
9978
9979        Slog.w(TAG, name + " request from " + callingUid + " stopped");
9980        return false;
9981    }
9982
9983    public void setDebugApp(String packageName, boolean waitForDebugger,
9984            boolean persistent) {
9985        enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
9986                "setDebugApp()");
9987
9988        long ident = Binder.clearCallingIdentity();
9989        try {
9990            // Note that this is not really thread safe if there are multiple
9991            // callers into it at the same time, but that's not a situation we
9992            // care about.
9993            if (persistent) {
9994                final ContentResolver resolver = mContext.getContentResolver();
9995                Settings.Global.putString(
9996                    resolver, Settings.Global.DEBUG_APP,
9997                    packageName);
9998                Settings.Global.putInt(
9999                    resolver, Settings.Global.WAIT_FOR_DEBUGGER,
10000                    waitForDebugger ? 1 : 0);
10001            }
10002
10003            synchronized (this) {
10004                if (!persistent) {
10005                    mOrigDebugApp = mDebugApp;
10006                    mOrigWaitForDebugger = mWaitForDebugger;
10007                }
10008                mDebugApp = packageName;
10009                mWaitForDebugger = waitForDebugger;
10010                mDebugTransient = !persistent;
10011                if (packageName != null) {
10012                    forceStopPackageLocked(packageName, -1, false, false, true, true,
10013                            false, UserHandle.USER_ALL, "set debug app");
10014                }
10015            }
10016        } finally {
10017            Binder.restoreCallingIdentity(ident);
10018        }
10019    }
10020
10021    void setOpenGlTraceApp(ApplicationInfo app, String processName) {
10022        synchronized (this) {
10023            boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
10024            if (!isDebuggable) {
10025                if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
10026                    throw new SecurityException("Process not debuggable: " + app.packageName);
10027                }
10028            }
10029
10030            mOpenGlTraceApp = processName;
10031        }
10032    }
10033
10034    void setProfileApp(ApplicationInfo app, String processName, ProfilerInfo profilerInfo) {
10035        synchronized (this) {
10036            boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
10037            if (!isDebuggable) {
10038                if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
10039                    throw new SecurityException("Process not debuggable: " + app.packageName);
10040                }
10041            }
10042            mProfileApp = processName;
10043            mProfileFile = profilerInfo.profileFile;
10044            if (mProfileFd != null) {
10045                try {
10046                    mProfileFd.close();
10047                } catch (IOException e) {
10048                }
10049                mProfileFd = null;
10050            }
10051            mProfileFd = profilerInfo.profileFd;
10052            mSamplingInterval = profilerInfo.samplingInterval;
10053            mAutoStopProfiler = profilerInfo.autoStopProfiler;
10054            mProfileType = 0;
10055        }
10056    }
10057
10058    @Override
10059    public void setAlwaysFinish(boolean enabled) {
10060        enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
10061                "setAlwaysFinish()");
10062
10063        Settings.Global.putInt(
10064                mContext.getContentResolver(),
10065                Settings.Global.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
10066
10067        synchronized (this) {
10068            mAlwaysFinishActivities = enabled;
10069        }
10070    }
10071
10072    @Override
10073    public void setActivityController(IActivityController controller) {
10074        enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
10075                "setActivityController()");
10076        synchronized (this) {
10077            mController = controller;
10078            Watchdog.getInstance().setActivityController(controller);
10079        }
10080    }
10081
10082    @Override
10083    public void setUserIsMonkey(boolean userIsMonkey) {
10084        synchronized (this) {
10085            synchronized (mPidsSelfLocked) {
10086                final int callingPid = Binder.getCallingPid();
10087                ProcessRecord precessRecord = mPidsSelfLocked.get(callingPid);
10088                if (precessRecord == null) {
10089                    throw new SecurityException("Unknown process: " + callingPid);
10090                }
10091                if (precessRecord.instrumentationUiAutomationConnection  == null) {
10092                    throw new SecurityException("Only an instrumentation process "
10093                            + "with a UiAutomation can call setUserIsMonkey");
10094                }
10095            }
10096            mUserIsMonkey = userIsMonkey;
10097        }
10098    }
10099
10100    @Override
10101    public boolean isUserAMonkey() {
10102        synchronized (this) {
10103            // If there is a controller also implies the user is a monkey.
10104            return (mUserIsMonkey || mController != null);
10105        }
10106    }
10107
10108    public void requestBugReport() {
10109        enforceCallingPermission(android.Manifest.permission.DUMP, "requestBugReport");
10110        SystemProperties.set("ctl.start", "bugreport");
10111    }
10112
10113    public static long getInputDispatchingTimeoutLocked(ActivityRecord r) {
10114        return r != null ? getInputDispatchingTimeoutLocked(r.app) : KEY_DISPATCHING_TIMEOUT;
10115    }
10116
10117    public static long getInputDispatchingTimeoutLocked(ProcessRecord r) {
10118        if (r != null && (r.instrumentationClass != null || r.usingWrapper)) {
10119            return INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT;
10120        }
10121        return KEY_DISPATCHING_TIMEOUT;
10122    }
10123
10124    @Override
10125    public long inputDispatchingTimedOut(int pid, final boolean aboveSystem, String reason) {
10126        if (checkCallingPermission(android.Manifest.permission.FILTER_EVENTS)
10127                != PackageManager.PERMISSION_GRANTED) {
10128            throw new SecurityException("Requires permission "
10129                    + android.Manifest.permission.FILTER_EVENTS);
10130        }
10131        ProcessRecord proc;
10132        long timeout;
10133        synchronized (this) {
10134            synchronized (mPidsSelfLocked) {
10135                proc = mPidsSelfLocked.get(pid);
10136            }
10137            timeout = getInputDispatchingTimeoutLocked(proc);
10138        }
10139
10140        if (!inputDispatchingTimedOut(proc, null, null, aboveSystem, reason)) {
10141            return -1;
10142        }
10143
10144        return timeout;
10145    }
10146
10147    /**
10148     * Handle input dispatching timeouts.
10149     * Returns whether input dispatching should be aborted or not.
10150     */
10151    public boolean inputDispatchingTimedOut(final ProcessRecord proc,
10152            final ActivityRecord activity, final ActivityRecord parent,
10153            final boolean aboveSystem, String reason) {
10154        if (checkCallingPermission(android.Manifest.permission.FILTER_EVENTS)
10155                != PackageManager.PERMISSION_GRANTED) {
10156            throw new SecurityException("Requires permission "
10157                    + android.Manifest.permission.FILTER_EVENTS);
10158        }
10159
10160        final String annotation;
10161        if (reason == null) {
10162            annotation = "Input dispatching timed out";
10163        } else {
10164            annotation = "Input dispatching timed out (" + reason + ")";
10165        }
10166
10167        if (proc != null) {
10168            synchronized (this) {
10169                if (proc.debugging) {
10170                    return false;
10171                }
10172
10173                if (mDidDexOpt) {
10174                    // Give more time since we were dexopting.
10175                    mDidDexOpt = false;
10176                    return false;
10177                }
10178
10179                if (proc.instrumentationClass != null) {
10180                    Bundle info = new Bundle();
10181                    info.putString("shortMsg", "keyDispatchingTimedOut");
10182                    info.putString("longMsg", annotation);
10183                    finishInstrumentationLocked(proc, Activity.RESULT_CANCELED, info);
10184                    return true;
10185                }
10186            }
10187            mHandler.post(new Runnable() {
10188                @Override
10189                public void run() {
10190                    appNotResponding(proc, activity, parent, aboveSystem, annotation);
10191                }
10192            });
10193        }
10194
10195        return true;
10196    }
10197
10198    public Bundle getAssistContextExtras(int requestType) {
10199        enforceCallingPermission(android.Manifest.permission.GET_TOP_ACTIVITY_INFO,
10200                "getAssistContextExtras()");
10201        PendingAssistExtras pae;
10202        Bundle extras = new Bundle();
10203        synchronized (this) {
10204            ActivityRecord activity = getFocusedStack().mResumedActivity;
10205            if (activity == null) {
10206                Slog.w(TAG, "getAssistContextExtras failed: no resumed activity");
10207                return null;
10208            }
10209            extras.putString(Intent.EXTRA_ASSIST_PACKAGE, activity.packageName);
10210            if (activity.app == null || activity.app.thread == null) {
10211                Slog.w(TAG, "getAssistContextExtras failed: no process for " + activity);
10212                return extras;
10213            }
10214            if (activity.app.pid == Binder.getCallingPid()) {
10215                Slog.w(TAG, "getAssistContextExtras failed: request process same as " + activity);
10216                return extras;
10217            }
10218            pae = new PendingAssistExtras(activity);
10219            try {
10220                activity.app.thread.requestAssistContextExtras(activity.appToken, pae,
10221                        requestType);
10222                mPendingAssistExtras.add(pae);
10223                mHandler.postDelayed(pae, PENDING_ASSIST_EXTRAS_TIMEOUT);
10224            } catch (RemoteException e) {
10225                Slog.w(TAG, "getAssistContextExtras failed: crash calling " + activity);
10226                return extras;
10227            }
10228        }
10229        synchronized (pae) {
10230            while (!pae.haveResult) {
10231                try {
10232                    pae.wait();
10233                } catch (InterruptedException e) {
10234                }
10235            }
10236            if (pae.result != null) {
10237                extras.putBundle(Intent.EXTRA_ASSIST_CONTEXT, pae.result);
10238            }
10239        }
10240        synchronized (this) {
10241            mPendingAssistExtras.remove(pae);
10242            mHandler.removeCallbacks(pae);
10243        }
10244        return extras;
10245    }
10246
10247    public void reportAssistContextExtras(IBinder token, Bundle extras) {
10248        PendingAssistExtras pae = (PendingAssistExtras)token;
10249        synchronized (pae) {
10250            pae.result = extras;
10251            pae.haveResult = true;
10252            pae.notifyAll();
10253        }
10254    }
10255
10256    public void registerProcessObserver(IProcessObserver observer) {
10257        enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
10258                "registerProcessObserver()");
10259        synchronized (this) {
10260            mProcessObservers.register(observer);
10261        }
10262    }
10263
10264    @Override
10265    public void unregisterProcessObserver(IProcessObserver observer) {
10266        synchronized (this) {
10267            mProcessObservers.unregister(observer);
10268        }
10269    }
10270
10271    @Override
10272    public boolean convertFromTranslucent(IBinder token) {
10273        final long origId = Binder.clearCallingIdentity();
10274        try {
10275            synchronized (this) {
10276                final ActivityRecord r = ActivityRecord.isInStackLocked(token);
10277                if (r == null) {
10278                    return false;
10279                }
10280                final boolean translucentChanged = r.changeWindowTranslucency(true);
10281                if (translucentChanged) {
10282                    r.task.stack.releaseBackgroundResources();
10283                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
10284                }
10285                mWindowManager.setAppFullscreen(token, true);
10286                return translucentChanged;
10287            }
10288        } finally {
10289            Binder.restoreCallingIdentity(origId);
10290        }
10291    }
10292
10293    @Override
10294    public boolean convertToTranslucent(IBinder token, ActivityOptions options) {
10295        final long origId = Binder.clearCallingIdentity();
10296        try {
10297            synchronized (this) {
10298                final ActivityRecord r = ActivityRecord.isInStackLocked(token);
10299                if (r == null) {
10300                    return false;
10301                }
10302                int index = r.task.mActivities.lastIndexOf(r);
10303                if (index > 0) {
10304                    ActivityRecord under = r.task.mActivities.get(index - 1);
10305                    under.returningOptions = options;
10306                }
10307                final boolean translucentChanged = r.changeWindowTranslucency(false);
10308                if (translucentChanged) {
10309                    r.task.stack.convertToTranslucent(r);
10310                }
10311                mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
10312                mWindowManager.setAppFullscreen(token, false);
10313                return translucentChanged;
10314            }
10315        } finally {
10316            Binder.restoreCallingIdentity(origId);
10317        }
10318    }
10319
10320    @Override
10321    public boolean requestVisibleBehind(IBinder token, boolean visible) {
10322        final long origId = Binder.clearCallingIdentity();
10323        try {
10324            synchronized (this) {
10325                final ActivityRecord r = ActivityRecord.isInStackLocked(token);
10326                if (r != null) {
10327                    return mStackSupervisor.requestVisibleBehindLocked(r, visible);
10328                }
10329            }
10330            return false;
10331        } finally {
10332            Binder.restoreCallingIdentity(origId);
10333        }
10334    }
10335
10336    @Override
10337    public boolean isBackgroundVisibleBehind(IBinder token) {
10338        final long origId = Binder.clearCallingIdentity();
10339        try {
10340            synchronized (this) {
10341                final ActivityStack stack = ActivityRecord.getStackLocked(token);
10342                final boolean visible = stack == null ? false : stack.hasVisibleBehindActivity();
10343                if (ActivityStackSupervisor.DEBUG_VISIBLE_BEHIND) Slog.d(TAG,
10344                        "isBackgroundVisibleBehind: stack=" + stack + " visible=" + visible);
10345                return visible;
10346            }
10347        } finally {
10348            Binder.restoreCallingIdentity(origId);
10349        }
10350    }
10351
10352    @Override
10353    public ActivityOptions getActivityOptions(IBinder token) {
10354        final long origId = Binder.clearCallingIdentity();
10355        try {
10356            synchronized (this) {
10357                final ActivityRecord r = ActivityRecord.isInStackLocked(token);
10358                if (r != null) {
10359                    final ActivityOptions activityOptions = r.pendingOptions;
10360                    r.pendingOptions = null;
10361                    return activityOptions;
10362                }
10363                return null;
10364            }
10365        } finally {
10366            Binder.restoreCallingIdentity(origId);
10367        }
10368    }
10369
10370    @Override
10371    public void setImmersive(IBinder token, boolean immersive) {
10372        synchronized(this) {
10373            final ActivityRecord r = ActivityRecord.isInStackLocked(token);
10374            if (r == null) {
10375                throw new IllegalArgumentException();
10376            }
10377            r.immersive = immersive;
10378
10379            // update associated state if we're frontmost
10380            if (r == mFocusedActivity) {
10381                if (DEBUG_IMMERSIVE) {
10382                    Slog.d(TAG, "Frontmost changed immersion: "+ r);
10383                }
10384                applyUpdateLockStateLocked(r);
10385            }
10386        }
10387    }
10388
10389    @Override
10390    public boolean isImmersive(IBinder token) {
10391        synchronized (this) {
10392            ActivityRecord r = ActivityRecord.isInStackLocked(token);
10393            if (r == null) {
10394                throw new IllegalArgumentException();
10395            }
10396            return r.immersive;
10397        }
10398    }
10399
10400    public boolean isTopActivityImmersive() {
10401        enforceNotIsolatedCaller("startActivity");
10402        synchronized (this) {
10403            ActivityRecord r = getFocusedStack().topRunningActivityLocked(null);
10404            return (r != null) ? r.immersive : false;
10405        }
10406    }
10407
10408    @Override
10409    public boolean isTopOfTask(IBinder token) {
10410        synchronized (this) {
10411            ActivityRecord r = ActivityRecord.isInStackLocked(token);
10412            if (r == null) {
10413                throw new IllegalArgumentException();
10414            }
10415            return r.task.getTopActivity() == r;
10416        }
10417    }
10418
10419    public final void enterSafeMode() {
10420        synchronized(this) {
10421            // It only makes sense to do this before the system is ready
10422            // and started launching other packages.
10423            if (!mSystemReady) {
10424                try {
10425                    AppGlobals.getPackageManager().enterSafeMode();
10426                } catch (RemoteException e) {
10427                }
10428            }
10429
10430            mSafeMode = true;
10431        }
10432    }
10433
10434    public final void showSafeModeOverlay() {
10435        View v = LayoutInflater.from(mContext).inflate(
10436                com.android.internal.R.layout.safe_mode, null);
10437        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
10438        lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
10439        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
10440        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
10441        lp.gravity = Gravity.BOTTOM | Gravity.START;
10442        lp.format = v.getBackground().getOpacity();
10443        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
10444                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
10445        lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
10446        ((WindowManager)mContext.getSystemService(
10447                Context.WINDOW_SERVICE)).addView(v, lp);
10448    }
10449
10450    public void noteWakeupAlarm(IIntentSender sender, int sourceUid, String sourcePkg) {
10451        if (!(sender instanceof PendingIntentRecord)) {
10452            return;
10453        }
10454        BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
10455        synchronized (stats) {
10456            if (mBatteryStatsService.isOnBattery()) {
10457                mBatteryStatsService.enforceCallingPermission();
10458                PendingIntentRecord rec = (PendingIntentRecord)sender;
10459                int MY_UID = Binder.getCallingUid();
10460                int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
10461                BatteryStatsImpl.Uid.Pkg pkg =
10462                    stats.getPackageStatsLocked(sourceUid >= 0 ? sourceUid : uid,
10463                            sourcePkg != null ? sourcePkg : rec.key.packageName);
10464                pkg.incWakeupsLocked();
10465            }
10466        }
10467    }
10468
10469    public boolean killPids(int[] pids, String pReason, boolean secure) {
10470        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10471            throw new SecurityException("killPids only available to the system");
10472        }
10473        String reason = (pReason == null) ? "Unknown" : pReason;
10474        // XXX Note: don't acquire main activity lock here, because the window
10475        // manager calls in with its locks held.
10476
10477        boolean killed = false;
10478        synchronized (mPidsSelfLocked) {
10479            int[] types = new int[pids.length];
10480            int worstType = 0;
10481            for (int i=0; i<pids.length; i++) {
10482                ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
10483                if (proc != null) {
10484                    int type = proc.setAdj;
10485                    types[i] = type;
10486                    if (type > worstType) {
10487                        worstType = type;
10488                    }
10489                }
10490            }
10491
10492            // If the worst oom_adj is somewhere in the cached proc LRU range,
10493            // then constrain it so we will kill all cached procs.
10494            if (worstType < ProcessList.CACHED_APP_MAX_ADJ
10495                    && worstType > ProcessList.CACHED_APP_MIN_ADJ) {
10496                worstType = ProcessList.CACHED_APP_MIN_ADJ;
10497            }
10498
10499            // If this is not a secure call, don't let it kill processes that
10500            // are important.
10501            if (!secure && worstType < ProcessList.SERVICE_ADJ) {
10502                worstType = ProcessList.SERVICE_ADJ;
10503            }
10504
10505            Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
10506            for (int i=0; i<pids.length; i++) {
10507                ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
10508                if (proc == null) {
10509                    continue;
10510                }
10511                int adj = proc.setAdj;
10512                if (adj >= worstType && !proc.killedByAm) {
10513                    proc.kill(reason, true);
10514                    killed = true;
10515                }
10516            }
10517        }
10518        return killed;
10519    }
10520
10521    @Override
10522    public void killUid(int uid, String reason) {
10523        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10524            throw new SecurityException("killUid only available to the system");
10525        }
10526        synchronized (this) {
10527            killPackageProcessesLocked(null, UserHandle.getAppId(uid), UserHandle.getUserId(uid),
10528                    ProcessList.FOREGROUND_APP_ADJ-1, false, true, true, false,
10529                    reason != null ? reason : "kill uid");
10530        }
10531    }
10532
10533    @Override
10534    public boolean killProcessesBelowForeground(String reason) {
10535        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10536            throw new SecurityException("killProcessesBelowForeground() only available to system");
10537        }
10538
10539        return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason);
10540    }
10541
10542    private boolean killProcessesBelowAdj(int belowAdj, String reason) {
10543        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10544            throw new SecurityException("killProcessesBelowAdj() only available to system");
10545        }
10546
10547        boolean killed = false;
10548        synchronized (mPidsSelfLocked) {
10549            final int size = mPidsSelfLocked.size();
10550            for (int i = 0; i < size; i++) {
10551                final int pid = mPidsSelfLocked.keyAt(i);
10552                final ProcessRecord proc = mPidsSelfLocked.valueAt(i);
10553                if (proc == null) continue;
10554
10555                final int adj = proc.setAdj;
10556                if (adj > belowAdj && !proc.killedByAm) {
10557                    proc.kill(reason, true);
10558                    killed = true;
10559                }
10560            }
10561        }
10562        return killed;
10563    }
10564
10565    @Override
10566    public void hang(final IBinder who, boolean allowRestart) {
10567        if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
10568                != PackageManager.PERMISSION_GRANTED) {
10569            throw new SecurityException("Requires permission "
10570                    + android.Manifest.permission.SET_ACTIVITY_WATCHER);
10571        }
10572
10573        final IBinder.DeathRecipient death = new DeathRecipient() {
10574            @Override
10575            public void binderDied() {
10576                synchronized (this) {
10577                    notifyAll();
10578                }
10579            }
10580        };
10581
10582        try {
10583            who.linkToDeath(death, 0);
10584        } catch (RemoteException e) {
10585            Slog.w(TAG, "hang: given caller IBinder is already dead.");
10586            return;
10587        }
10588
10589        synchronized (this) {
10590            Watchdog.getInstance().setAllowRestart(allowRestart);
10591            Slog.i(TAG, "Hanging system process at request of pid " + Binder.getCallingPid());
10592            synchronized (death) {
10593                while (who.isBinderAlive()) {
10594                    try {
10595                        death.wait();
10596                    } catch (InterruptedException e) {
10597                    }
10598                }
10599            }
10600            Watchdog.getInstance().setAllowRestart(true);
10601        }
10602    }
10603
10604    @Override
10605    public void restart() {
10606        if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
10607                != PackageManager.PERMISSION_GRANTED) {
10608            throw new SecurityException("Requires permission "
10609                    + android.Manifest.permission.SET_ACTIVITY_WATCHER);
10610        }
10611
10612        Log.i(TAG, "Sending shutdown broadcast...");
10613
10614        BroadcastReceiver br = new BroadcastReceiver() {
10615            @Override public void onReceive(Context context, Intent intent) {
10616                // Now the broadcast is done, finish up the low-level shutdown.
10617                Log.i(TAG, "Shutting down activity manager...");
10618                shutdown(10000);
10619                Log.i(TAG, "Shutdown complete, restarting!");
10620                Process.killProcess(Process.myPid());
10621                System.exit(10);
10622            }
10623        };
10624
10625        // First send the high-level shut down broadcast.
10626        Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
10627        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
10628        intent.putExtra(Intent.EXTRA_SHUTDOWN_USERSPACE_ONLY, true);
10629        /* For now we are not doing a clean shutdown, because things seem to get unhappy.
10630        mContext.sendOrderedBroadcastAsUser(intent,
10631                UserHandle.ALL, null, br, mHandler, 0, null, null);
10632        */
10633        br.onReceive(mContext, intent);
10634    }
10635
10636    private long getLowRamTimeSinceIdle(long now) {
10637        return mLowRamTimeSinceLastIdle + (mLowRamStartTime > 0 ? (now-mLowRamStartTime) : 0);
10638    }
10639
10640    @Override
10641    public void performIdleMaintenance() {
10642        if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
10643                != PackageManager.PERMISSION_GRANTED) {
10644            throw new SecurityException("Requires permission "
10645                    + android.Manifest.permission.SET_ACTIVITY_WATCHER);
10646        }
10647
10648        synchronized (this) {
10649            final long now = SystemClock.uptimeMillis();
10650            final long timeSinceLastIdle = now - mLastIdleTime;
10651            final long lowRamSinceLastIdle = getLowRamTimeSinceIdle(now);
10652            mLastIdleTime = now;
10653            mLowRamTimeSinceLastIdle = 0;
10654            if (mLowRamStartTime != 0) {
10655                mLowRamStartTime = now;
10656            }
10657
10658            StringBuilder sb = new StringBuilder(128);
10659            sb.append("Idle maintenance over ");
10660            TimeUtils.formatDuration(timeSinceLastIdle, sb);
10661            sb.append(" low RAM for ");
10662            TimeUtils.formatDuration(lowRamSinceLastIdle, sb);
10663            Slog.i(TAG, sb.toString());
10664
10665            // If at least 1/3 of our time since the last idle period has been spent
10666            // with RAM low, then we want to kill processes.
10667            boolean doKilling = lowRamSinceLastIdle > (timeSinceLastIdle/3);
10668
10669            for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
10670                ProcessRecord proc = mLruProcesses.get(i);
10671                if (proc.notCachedSinceIdle) {
10672                    if (proc.setProcState > ActivityManager.PROCESS_STATE_TOP
10673                            && proc.setProcState <= ActivityManager.PROCESS_STATE_SERVICE) {
10674                        if (doKilling && proc.initialIdlePss != 0
10675                                && proc.lastPss > ((proc.initialIdlePss*3)/2)) {
10676                            proc.kill("idle maint (pss " + proc.lastPss
10677                                    + " from " + proc.initialIdlePss + ")", true);
10678                        }
10679                    }
10680                } else if (proc.setProcState < ActivityManager.PROCESS_STATE_HOME) {
10681                    proc.notCachedSinceIdle = true;
10682                    proc.initialIdlePss = 0;
10683                    proc.nextPssTime = ProcessList.computeNextPssTime(proc.curProcState, true,
10684                            isSleeping(), now);
10685                }
10686            }
10687
10688            mHandler.removeMessages(REQUEST_ALL_PSS_MSG);
10689            mHandler.sendEmptyMessageDelayed(REQUEST_ALL_PSS_MSG, 2*60*1000);
10690        }
10691    }
10692
10693    private void retrieveSettings() {
10694        final ContentResolver resolver = mContext.getContentResolver();
10695        String debugApp = Settings.Global.getString(
10696            resolver, Settings.Global.DEBUG_APP);
10697        boolean waitForDebugger = Settings.Global.getInt(
10698            resolver, Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0;
10699        boolean alwaysFinishActivities = Settings.Global.getInt(
10700            resolver, Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
10701        boolean forceRtl = Settings.Global.getInt(
10702                resolver, Settings.Global.DEVELOPMENT_FORCE_RTL, 0) != 0;
10703        // Transfer any global setting for forcing RTL layout, into a System Property
10704        SystemProperties.set(Settings.Global.DEVELOPMENT_FORCE_RTL, forceRtl ? "1":"0");
10705
10706        Configuration configuration = new Configuration();
10707        Settings.System.getConfiguration(resolver, configuration);
10708        if (forceRtl) {
10709            // This will take care of setting the correct layout direction flags
10710            configuration.setLayoutDirection(configuration.locale);
10711        }
10712
10713        synchronized (this) {
10714            mDebugApp = mOrigDebugApp = debugApp;
10715            mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
10716            mAlwaysFinishActivities = alwaysFinishActivities;
10717            // This happens before any activities are started, so we can
10718            // change mConfiguration in-place.
10719            updateConfigurationLocked(configuration, null, false, true);
10720            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
10721        }
10722    }
10723
10724    /** Loads resources after the current configuration has been set. */
10725    private void loadResourcesOnSystemReady() {
10726        final Resources res = mContext.getResources();
10727        mHasRecents = res.getBoolean(com.android.internal.R.bool.config_hasRecents);
10728        mThumbnailWidth = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
10729        mThumbnailHeight = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
10730    }
10731
10732    public boolean testIsSystemReady() {
10733        // no need to synchronize(this) just to read & return the value
10734        return mSystemReady;
10735    }
10736
10737    private static File getCalledPreBootReceiversFile() {
10738        File dataDir = Environment.getDataDirectory();
10739        File systemDir = new File(dataDir, "system");
10740        File fname = new File(systemDir, CALLED_PRE_BOOTS_FILENAME);
10741        return fname;
10742    }
10743
10744    private static ArrayList<ComponentName> readLastDonePreBootReceivers() {
10745        ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>();
10746        File file = getCalledPreBootReceiversFile();
10747        FileInputStream fis = null;
10748        try {
10749            fis = new FileInputStream(file);
10750            DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048));
10751            int fvers = dis.readInt();
10752            if (fvers == LAST_PREBOOT_DELIVERED_FILE_VERSION) {
10753                String vers = dis.readUTF();
10754                String codename = dis.readUTF();
10755                String build = dis.readUTF();
10756                if (android.os.Build.VERSION.RELEASE.equals(vers)
10757                        && android.os.Build.VERSION.CODENAME.equals(codename)
10758                        && android.os.Build.VERSION.INCREMENTAL.equals(build)) {
10759                    int num = dis.readInt();
10760                    while (num > 0) {
10761                        num--;
10762                        String pkg = dis.readUTF();
10763                        String cls = dis.readUTF();
10764                        lastDoneReceivers.add(new ComponentName(pkg, cls));
10765                    }
10766                }
10767            }
10768        } catch (FileNotFoundException e) {
10769        } catch (IOException e) {
10770            Slog.w(TAG, "Failure reading last done pre-boot receivers", e);
10771        } finally {
10772            if (fis != null) {
10773                try {
10774                    fis.close();
10775                } catch (IOException e) {
10776                }
10777            }
10778        }
10779        return lastDoneReceivers;
10780    }
10781
10782    private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) {
10783        File file = getCalledPreBootReceiversFile();
10784        FileOutputStream fos = null;
10785        DataOutputStream dos = null;
10786        try {
10787            fos = new FileOutputStream(file);
10788            dos = new DataOutputStream(new BufferedOutputStream(fos, 2048));
10789            dos.writeInt(LAST_PREBOOT_DELIVERED_FILE_VERSION);
10790            dos.writeUTF(android.os.Build.VERSION.RELEASE);
10791            dos.writeUTF(android.os.Build.VERSION.CODENAME);
10792            dos.writeUTF(android.os.Build.VERSION.INCREMENTAL);
10793            dos.writeInt(list.size());
10794            for (int i=0; i<list.size(); i++) {
10795                dos.writeUTF(list.get(i).getPackageName());
10796                dos.writeUTF(list.get(i).getClassName());
10797            }
10798        } catch (IOException e) {
10799            Slog.w(TAG, "Failure writing last done pre-boot receivers", e);
10800            file.delete();
10801        } finally {
10802            FileUtils.sync(fos);
10803            if (dos != null) {
10804                try {
10805                    dos.close();
10806                } catch (IOException e) {
10807                    // TODO Auto-generated catch block
10808                    e.printStackTrace();
10809                }
10810            }
10811        }
10812    }
10813
10814    private boolean deliverPreBootCompleted(final Runnable onFinishCallback,
10815            ArrayList<ComponentName> doneReceivers, int userId) {
10816        boolean waitingUpdate = false;
10817        Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
10818        List<ResolveInfo> ris = null;
10819        try {
10820            ris = AppGlobals.getPackageManager().queryIntentReceivers(
10821                    intent, null, 0, userId);
10822        } catch (RemoteException e) {
10823        }
10824        if (ris != null) {
10825            for (int i=ris.size()-1; i>=0; i--) {
10826                if ((ris.get(i).activityInfo.applicationInfo.flags
10827                        &ApplicationInfo.FLAG_SYSTEM) == 0) {
10828                    ris.remove(i);
10829                }
10830            }
10831            intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
10832
10833            // For User 0, load the version number. When delivering to a new user, deliver
10834            // to all receivers.
10835            if (userId == UserHandle.USER_OWNER) {
10836                ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers();
10837                for (int i=0; i<ris.size(); i++) {
10838                    ActivityInfo ai = ris.get(i).activityInfo;
10839                    ComponentName comp = new ComponentName(ai.packageName, ai.name);
10840                    if (lastDoneReceivers.contains(comp)) {
10841                        // We already did the pre boot receiver for this app with the current
10842                        // platform version, so don't do it again...
10843                        ris.remove(i);
10844                        i--;
10845                        // ...however, do keep it as one that has been done, so we don't
10846                        // forget about it when rewriting the file of last done receivers.
10847                        doneReceivers.add(comp);
10848                    }
10849                }
10850            }
10851
10852            // If primary user, send broadcast to all available users, else just to userId
10853            final int[] users = userId == UserHandle.USER_OWNER ? getUsersLocked()
10854                    : new int[] { userId };
10855            for (int i = 0; i < ris.size(); i++) {
10856                ActivityInfo ai = ris.get(i).activityInfo;
10857                ComponentName comp = new ComponentName(ai.packageName, ai.name);
10858                doneReceivers.add(comp);
10859                intent.setComponent(comp);
10860                for (int j=0; j<users.length; j++) {
10861                    IIntentReceiver finisher = null;
10862                    // On last receiver and user, set up a completion callback
10863                    if (i == ris.size() - 1 && j == users.length - 1 && onFinishCallback != null) {
10864                        finisher = new IIntentReceiver.Stub() {
10865                            public void performReceive(Intent intent, int resultCode,
10866                                    String data, Bundle extras, boolean ordered,
10867                                    boolean sticky, int sendingUser) {
10868                                // The raw IIntentReceiver interface is called
10869                                // with the AM lock held, so redispatch to
10870                                // execute our code without the lock.
10871                                mHandler.post(onFinishCallback);
10872                            }
10873                        };
10874                    }
10875                    Slog.i(TAG, "Sending system update to " + intent.getComponent()
10876                            + " for user " + users[j]);
10877                    broadcastIntentLocked(null, null, intent, null, finisher,
10878                            0, null, null, null, AppOpsManager.OP_NONE,
10879                            true, false, MY_PID, Process.SYSTEM_UID,
10880                            users[j]);
10881                    if (finisher != null) {
10882                        waitingUpdate = true;
10883                    }
10884                }
10885            }
10886        }
10887
10888        return waitingUpdate;
10889    }
10890
10891    public void systemReady(final Runnable goingCallback) {
10892        synchronized(this) {
10893            if (mSystemReady) {
10894                // If we're done calling all the receivers, run the next "boot phase" passed in
10895                // by the SystemServer
10896                if (goingCallback != null) {
10897                    goingCallback.run();
10898                }
10899                return;
10900            }
10901
10902            // Make sure we have the current profile info, since it is needed for
10903            // security checks.
10904            updateCurrentProfileIdsLocked();
10905
10906            if (mRecentTasks == null) {
10907                mRecentTasks = mTaskPersister.restoreTasksLocked();
10908                if (!mRecentTasks.isEmpty()) {
10909                    mStackSupervisor.createStackForRestoredTaskHistory(mRecentTasks);
10910                }
10911                cleanupRecentTasksLocked(UserHandle.USER_ALL);
10912                mTaskPersister.startPersisting();
10913            }
10914
10915            // Check to see if there are any update receivers to run.
10916            if (!mDidUpdate) {
10917                if (mWaitingUpdate) {
10918                    return;
10919                }
10920                final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>();
10921                mWaitingUpdate = deliverPreBootCompleted(new Runnable() {
10922                    public void run() {
10923                        synchronized (ActivityManagerService.this) {
10924                            mDidUpdate = true;
10925                        }
10926                        writeLastDonePreBootReceivers(doneReceivers);
10927                        showBootMessage(mContext.getText(
10928                                R.string.android_upgrading_complete),
10929                                false);
10930                        systemReady(goingCallback);
10931                    }
10932                }, doneReceivers, UserHandle.USER_OWNER);
10933
10934                if (mWaitingUpdate) {
10935                    return;
10936                }
10937                mDidUpdate = true;
10938            }
10939
10940            mAppOpsService.systemReady();
10941            mSystemReady = true;
10942        }
10943
10944        ArrayList<ProcessRecord> procsToKill = null;
10945        synchronized(mPidsSelfLocked) {
10946            for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
10947                ProcessRecord proc = mPidsSelfLocked.valueAt(i);
10948                if (!isAllowedWhileBooting(proc.info)){
10949                    if (procsToKill == null) {
10950                        procsToKill = new ArrayList<ProcessRecord>();
10951                    }
10952                    procsToKill.add(proc);
10953                }
10954            }
10955        }
10956
10957        synchronized(this) {
10958            if (procsToKill != null) {
10959                for (int i=procsToKill.size()-1; i>=0; i--) {
10960                    ProcessRecord proc = procsToKill.get(i);
10961                    Slog.i(TAG, "Removing system update proc: " + proc);
10962                    removeProcessLocked(proc, true, false, "system update done");
10963                }
10964            }
10965
10966            // Now that we have cleaned up any update processes, we
10967            // are ready to start launching real processes and know that
10968            // we won't trample on them any more.
10969            mProcessesReady = true;
10970        }
10971
10972        Slog.i(TAG, "System now ready");
10973        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
10974            SystemClock.uptimeMillis());
10975
10976        synchronized(this) {
10977            // Make sure we have no pre-ready processes sitting around.
10978
10979            if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
10980                ResolveInfo ri = mContext.getPackageManager()
10981                        .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
10982                                STOCK_PM_FLAGS);
10983                CharSequence errorMsg = null;
10984                if (ri != null) {
10985                    ActivityInfo ai = ri.activityInfo;
10986                    ApplicationInfo app = ai.applicationInfo;
10987                    if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
10988                        mTopAction = Intent.ACTION_FACTORY_TEST;
10989                        mTopData = null;
10990                        mTopComponent = new ComponentName(app.packageName,
10991                                ai.name);
10992                    } else {
10993                        errorMsg = mContext.getResources().getText(
10994                                com.android.internal.R.string.factorytest_not_system);
10995                    }
10996                } else {
10997                    errorMsg = mContext.getResources().getText(
10998                            com.android.internal.R.string.factorytest_no_action);
10999                }
11000                if (errorMsg != null) {
11001                    mTopAction = null;
11002                    mTopData = null;
11003                    mTopComponent = null;
11004                    Message msg = Message.obtain();
11005                    msg.what = SHOW_FACTORY_ERROR_MSG;
11006                    msg.getData().putCharSequence("msg", errorMsg);
11007                    mHandler.sendMessage(msg);
11008                }
11009            }
11010        }
11011
11012        retrieveSettings();
11013        loadResourcesOnSystemReady();
11014
11015        synchronized (this) {
11016            readGrantedUriPermissionsLocked();
11017        }
11018
11019        if (goingCallback != null) goingCallback.run();
11020
11021        mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_USER_RUNNING_START,
11022                Integer.toString(mCurrentUserId), mCurrentUserId);
11023        mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_START,
11024                Integer.toString(mCurrentUserId), mCurrentUserId);
11025        mSystemServiceManager.startUser(mCurrentUserId);
11026
11027        synchronized (this) {
11028            if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
11029                try {
11030                    List apps = AppGlobals.getPackageManager().
11031                        getPersistentApplications(STOCK_PM_FLAGS);
11032                    if (apps != null) {
11033                        int N = apps.size();
11034                        int i;
11035                        for (i=0; i<N; i++) {
11036                            ApplicationInfo info
11037                                = (ApplicationInfo)apps.get(i);
11038                            if (info != null &&
11039                                    !info.packageName.equals("android")) {
11040                                addAppLocked(info, false, null /* ABI override */);
11041                            }
11042                        }
11043                    }
11044                } catch (RemoteException ex) {
11045                    // pm is in same process, this will never happen.
11046                }
11047            }
11048
11049            // Start up initial activity.
11050            mBooting = true;
11051
11052            try {
11053                if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
11054                    Message msg = Message.obtain();
11055                    msg.what = SHOW_UID_ERROR_MSG;
11056                    mHandler.sendMessage(msg);
11057                }
11058            } catch (RemoteException e) {
11059            }
11060
11061            long ident = Binder.clearCallingIdentity();
11062            try {
11063                Intent intent = new Intent(Intent.ACTION_USER_STARTED);
11064                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
11065                        | Intent.FLAG_RECEIVER_FOREGROUND);
11066                intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId);
11067                broadcastIntentLocked(null, null, intent,
11068                        null, null, 0, null, null, null, AppOpsManager.OP_NONE,
11069                        false, false, MY_PID, Process.SYSTEM_UID, mCurrentUserId);
11070                intent = new Intent(Intent.ACTION_USER_STARTING);
11071                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
11072                intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId);
11073                broadcastIntentLocked(null, null, intent,
11074                        null, new IIntentReceiver.Stub() {
11075                            @Override
11076                            public void performReceive(Intent intent, int resultCode, String data,
11077                                    Bundle extras, boolean ordered, boolean sticky, int sendingUser)
11078                                    throws RemoteException {
11079                            }
11080                        }, 0, null, null,
11081                        INTERACT_ACROSS_USERS, AppOpsManager.OP_NONE,
11082                        true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
11083            } catch (Throwable t) {
11084                Slog.wtf(TAG, "Failed sending first user broadcasts", t);
11085            } finally {
11086                Binder.restoreCallingIdentity(ident);
11087            }
11088            mStackSupervisor.resumeTopActivitiesLocked();
11089            sendUserSwitchBroadcastsLocked(-1, mCurrentUserId);
11090        }
11091    }
11092
11093    private boolean makeAppCrashingLocked(ProcessRecord app,
11094            String shortMsg, String longMsg, String stackTrace) {
11095        app.crashing = true;
11096        app.crashingReport = generateProcessError(app,
11097                ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
11098        startAppProblemLocked(app);
11099        app.stopFreezingAllLocked();
11100        return handleAppCrashLocked(app, shortMsg, longMsg, stackTrace);
11101    }
11102
11103    private void makeAppNotRespondingLocked(ProcessRecord app,
11104            String activity, String shortMsg, String longMsg) {
11105        app.notResponding = true;
11106        app.notRespondingReport = generateProcessError(app,
11107                ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
11108                activity, shortMsg, longMsg, null);
11109        startAppProblemLocked(app);
11110        app.stopFreezingAllLocked();
11111    }
11112
11113    /**
11114     * Generate a process error record, suitable for attachment to a ProcessRecord.
11115     *
11116     * @param app The ProcessRecord in which the error occurred.
11117     * @param condition Crashing, Application Not Responding, etc.  Values are defined in
11118     *                      ActivityManager.AppErrorStateInfo
11119     * @param activity The activity associated with the crash, if known.
11120     * @param shortMsg Short message describing the crash.
11121     * @param longMsg Long message describing the crash.
11122     * @param stackTrace Full crash stack trace, may be null.
11123     *
11124     * @return Returns a fully-formed AppErrorStateInfo record.
11125     */
11126    private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
11127            int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
11128        ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
11129
11130        report.condition = condition;
11131        report.processName = app.processName;
11132        report.pid = app.pid;
11133        report.uid = app.info.uid;
11134        report.tag = activity;
11135        report.shortMsg = shortMsg;
11136        report.longMsg = longMsg;
11137        report.stackTrace = stackTrace;
11138
11139        return report;
11140    }
11141
11142    void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) {
11143        synchronized (this) {
11144            app.crashing = false;
11145            app.crashingReport = null;
11146            app.notResponding = false;
11147            app.notRespondingReport = null;
11148            if (app.anrDialog == fromDialog) {
11149                app.anrDialog = null;
11150            }
11151            if (app.waitDialog == fromDialog) {
11152                app.waitDialog = null;
11153            }
11154            if (app.pid > 0 && app.pid != MY_PID) {
11155                handleAppCrashLocked(app, null, null, null);
11156                app.kill("user request after error", true);
11157            }
11158        }
11159    }
11160
11161    private boolean handleAppCrashLocked(ProcessRecord app, String shortMsg, String longMsg,
11162            String stackTrace) {
11163        long now = SystemClock.uptimeMillis();
11164
11165        Long crashTime;
11166        if (!app.isolated) {
11167            crashTime = mProcessCrashTimes.get(app.info.processName, app.uid);
11168        } else {
11169            crashTime = null;
11170        }
11171        if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) {
11172            // This process loses!
11173            Slog.w(TAG, "Process " + app.info.processName
11174                    + " has crashed too many times: killing!");
11175            EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
11176                    app.userId, app.info.processName, app.uid);
11177            mStackSupervisor.handleAppCrashLocked(app);
11178            if (!app.persistent) {
11179                // We don't want to start this process again until the user
11180                // explicitly does so...  but for persistent process, we really
11181                // need to keep it running.  If a persistent process is actually
11182                // repeatedly crashing, then badness for everyone.
11183                EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.userId, app.uid,
11184                        app.info.processName);
11185                if (!app.isolated) {
11186                    // XXX We don't have a way to mark isolated processes
11187                    // as bad, since they don't have a peristent identity.
11188                    mBadProcesses.put(app.info.processName, app.uid,
11189                            new BadProcessInfo(now, shortMsg, longMsg, stackTrace));
11190                    mProcessCrashTimes.remove(app.info.processName, app.uid);
11191                }
11192                app.bad = true;
11193                app.removed = true;
11194                // Don't let services in this process be restarted and potentially
11195                // annoy the user repeatedly.  Unless it is persistent, since those
11196                // processes run critical code.
11197                removeProcessLocked(app, false, false, "crash");
11198                mStackSupervisor.resumeTopActivitiesLocked();
11199                return false;
11200            }
11201            mStackSupervisor.resumeTopActivitiesLocked();
11202        } else {
11203            mStackSupervisor.finishTopRunningActivityLocked(app);
11204        }
11205
11206        // Bump up the crash count of any services currently running in the proc.
11207        for (int i=app.services.size()-1; i>=0; i--) {
11208            // Any services running in the application need to be placed
11209            // back in the pending list.
11210            ServiceRecord sr = app.services.valueAt(i);
11211            sr.crashCount++;
11212        }
11213
11214        // If the crashing process is what we consider to be the "home process" and it has been
11215        // replaced by a third-party app, clear the package preferred activities from packages
11216        // with a home activity running in the process to prevent a repeatedly crashing app
11217        // from blocking the user to manually clear the list.
11218        final ArrayList<ActivityRecord> activities = app.activities;
11219        if (app == mHomeProcess && activities.size() > 0
11220                    && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
11221            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
11222                final ActivityRecord r = activities.get(activityNdx);
11223                if (r.isHomeActivity()) {
11224                    Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
11225                    try {
11226                        ActivityThread.getPackageManager()
11227                                .clearPackagePreferredActivities(r.packageName);
11228                    } catch (RemoteException c) {
11229                        // pm is in same process, this will never happen.
11230                    }
11231                }
11232            }
11233        }
11234
11235        if (!app.isolated) {
11236            // XXX Can't keep track of crash times for isolated processes,
11237            // because they don't have a perisistent identity.
11238            mProcessCrashTimes.put(app.info.processName, app.uid, now);
11239        }
11240
11241        if (app.crashHandler != null) mHandler.post(app.crashHandler);
11242        return true;
11243    }
11244
11245    void startAppProblemLocked(ProcessRecord app) {
11246        // If this app is not running under the current user, then we
11247        // can't give it a report button because that would require
11248        // launching the report UI under a different user.
11249        app.errorReportReceiver = null;
11250
11251        for (int userId : mCurrentProfileIds) {
11252            if (app.userId == userId) {
11253                app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
11254                        mContext, app.info.packageName, app.info.flags);
11255            }
11256        }
11257        skipCurrentReceiverLocked(app);
11258    }
11259
11260    void skipCurrentReceiverLocked(ProcessRecord app) {
11261        for (BroadcastQueue queue : mBroadcastQueues) {
11262            queue.skipCurrentReceiverLocked(app);
11263        }
11264    }
11265
11266    /**
11267     * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes.
11268     * The application process will exit immediately after this call returns.
11269     * @param app object of the crashing app, null for the system server
11270     * @param crashInfo describing the exception
11271     */
11272    public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) {
11273        ProcessRecord r = findAppProcess(app, "Crash");
11274        final String processName = app == null ? "system_server"
11275                : (r == null ? "unknown" : r.processName);
11276
11277        handleApplicationCrashInner("crash", r, processName, crashInfo);
11278    }
11279
11280    /* Native crash reporting uses this inner version because it needs to be somewhat
11281     * decoupled from the AM-managed cleanup lifecycle
11282     */
11283    void handleApplicationCrashInner(String eventType, ProcessRecord r, String processName,
11284            ApplicationErrorReport.CrashInfo crashInfo) {
11285        EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(),
11286                UserHandle.getUserId(Binder.getCallingUid()), processName,
11287                r == null ? -1 : r.info.flags,
11288                crashInfo.exceptionClassName,
11289                crashInfo.exceptionMessage,
11290                crashInfo.throwFileName,
11291                crashInfo.throwLineNumber);
11292
11293        addErrorToDropBox(eventType, r, processName, null, null, null, null, null, crashInfo);
11294
11295        crashApplication(r, crashInfo);
11296    }
11297
11298    public void handleApplicationStrictModeViolation(
11299            IBinder app,
11300            int violationMask,
11301            StrictMode.ViolationInfo info) {
11302        ProcessRecord r = findAppProcess(app, "StrictMode");
11303        if (r == null) {
11304            return;
11305        }
11306
11307        if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) {
11308            Integer stackFingerprint = info.hashCode();
11309            boolean logIt = true;
11310            synchronized (mAlreadyLoggedViolatedStacks) {
11311                if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) {
11312                    logIt = false;
11313                    // TODO: sub-sample into EventLog for these, with
11314                    // the info.durationMillis?  Then we'd get
11315                    // the relative pain numbers, without logging all
11316                    // the stack traces repeatedly.  We'd want to do
11317                    // likewise in the client code, which also does
11318                    // dup suppression, before the Binder call.
11319                } else {
11320                    if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) {
11321                        mAlreadyLoggedViolatedStacks.clear();
11322                    }
11323                    mAlreadyLoggedViolatedStacks.add(stackFingerprint);
11324                }
11325            }
11326            if (logIt) {
11327                logStrictModeViolationToDropBox(r, info);
11328            }
11329        }
11330
11331        if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) {
11332            AppErrorResult result = new AppErrorResult();
11333            synchronized (this) {
11334                final long origId = Binder.clearCallingIdentity();
11335
11336                Message msg = Message.obtain();
11337                msg.what = SHOW_STRICT_MODE_VIOLATION_MSG;
11338                HashMap<String, Object> data = new HashMap<String, Object>();
11339                data.put("result", result);
11340                data.put("app", r);
11341                data.put("violationMask", violationMask);
11342                data.put("info", info);
11343                msg.obj = data;
11344                mHandler.sendMessage(msg);
11345
11346                Binder.restoreCallingIdentity(origId);
11347            }
11348            int res = result.get();
11349            Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res);
11350        }
11351    }
11352
11353    // Depending on the policy in effect, there could be a bunch of
11354    // these in quick succession so we try to batch these together to
11355    // minimize disk writes, number of dropbox entries, and maximize
11356    // compression, by having more fewer, larger records.
11357    private void logStrictModeViolationToDropBox(
11358            ProcessRecord process,
11359            StrictMode.ViolationInfo info) {
11360        if (info == null) {
11361            return;
11362        }
11363        final boolean isSystemApp = process == null ||
11364                (process.info.flags & (ApplicationInfo.FLAG_SYSTEM |
11365                                       ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
11366        final String processName = process == null ? "unknown" : process.processName;
11367        final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode";
11368        final DropBoxManager dbox = (DropBoxManager)
11369                mContext.getSystemService(Context.DROPBOX_SERVICE);
11370
11371        // Exit early if the dropbox isn't configured to accept this report type.
11372        if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
11373
11374        boolean bufferWasEmpty;
11375        boolean needsFlush;
11376        final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024);
11377        synchronized (sb) {
11378            bufferWasEmpty = sb.length() == 0;
11379            appendDropBoxProcessHeaders(process, processName, sb);
11380            sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
11381            sb.append("System-App: ").append(isSystemApp).append("\n");
11382            sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n");
11383            if (info.violationNumThisLoop != 0) {
11384                sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n");
11385            }
11386            if (info.numAnimationsRunning != 0) {
11387                sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n");
11388            }
11389            if (info.broadcastIntentAction != null) {
11390                sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n");
11391            }
11392            if (info.durationMillis != -1) {
11393                sb.append("Duration-Millis: ").append(info.durationMillis).append("\n");
11394            }
11395            if (info.numInstances != -1) {
11396                sb.append("Instance-Count: ").append(info.numInstances).append("\n");
11397            }
11398            if (info.tags != null) {
11399                for (String tag : info.tags) {
11400                    sb.append("Span-Tag: ").append(tag).append("\n");
11401                }
11402            }
11403            sb.append("\n");
11404            if (info.crashInfo != null && info.crashInfo.stackTrace != null) {
11405                sb.append(info.crashInfo.stackTrace);
11406            }
11407            sb.append("\n");
11408
11409            // Only buffer up to ~64k.  Various logging bits truncate
11410            // things at 128k.
11411            needsFlush = (sb.length() > 64 * 1024);
11412        }
11413
11414        // Flush immediately if the buffer's grown too large, or this
11415        // is a non-system app.  Non-system apps are isolated with a
11416        // different tag & policy and not batched.
11417        //
11418        // Batching is useful during internal testing with
11419        // StrictMode settings turned up high.  Without batching,
11420        // thousands of separate files could be created on boot.
11421        if (!isSystemApp || needsFlush) {
11422            new Thread("Error dump: " + dropboxTag) {
11423                @Override
11424                public void run() {
11425                    String report;
11426                    synchronized (sb) {
11427                        report = sb.toString();
11428                        sb.delete(0, sb.length());
11429                        sb.trimToSize();
11430                    }
11431                    if (report.length() != 0) {
11432                        dbox.addText(dropboxTag, report);
11433                    }
11434                }
11435            }.start();
11436            return;
11437        }
11438
11439        // System app batching:
11440        if (!bufferWasEmpty) {
11441            // An existing dropbox-writing thread is outstanding, so
11442            // we don't need to start it up.  The existing thread will
11443            // catch the buffer appends we just did.
11444            return;
11445        }
11446
11447        // Worker thread to both batch writes and to avoid blocking the caller on I/O.
11448        // (After this point, we shouldn't access AMS internal data structures.)
11449        new Thread("Error dump: " + dropboxTag) {
11450            @Override
11451            public void run() {
11452                // 5 second sleep to let stacks arrive and be batched together
11453                try {
11454                    Thread.sleep(5000);  // 5 seconds
11455                } catch (InterruptedException e) {}
11456
11457                String errorReport;
11458                synchronized (mStrictModeBuffer) {
11459                    errorReport = mStrictModeBuffer.toString();
11460                    if (errorReport.length() == 0) {
11461                        return;
11462                    }
11463                    mStrictModeBuffer.delete(0, mStrictModeBuffer.length());
11464                    mStrictModeBuffer.trimToSize();
11465                }
11466                dbox.addText(dropboxTag, errorReport);
11467            }
11468        }.start();
11469    }
11470
11471    /**
11472     * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors.
11473     * @param app object of the crashing app, null for the system server
11474     * @param tag reported by the caller
11475     * @param system whether this wtf is coming from the system
11476     * @param crashInfo describing the context of the error
11477     * @return true if the process should exit immediately (WTF is fatal)
11478     */
11479    public boolean handleApplicationWtf(IBinder app, final String tag, boolean system,
11480            final ApplicationErrorReport.CrashInfo crashInfo) {
11481        final ProcessRecord r = findAppProcess(app, "WTF");
11482        final String processName = app == null ? "system_server"
11483                : (r == null ? "unknown" : r.processName);
11484
11485        EventLog.writeEvent(EventLogTags.AM_WTF,
11486                UserHandle.getUserId(Binder.getCallingUid()), Binder.getCallingPid(),
11487                processName,
11488                r == null ? -1 : r.info.flags,
11489                tag, crashInfo.exceptionMessage);
11490
11491        if (system) {
11492            // If this is coming from the system, we could very well have low-level
11493            // system locks held, so we want to do this all asynchronously.  And we
11494            // never want this to become fatal, so there is that too.
11495            mHandler.post(new Runnable() {
11496                @Override public void run() {
11497                    addErrorToDropBox("wtf", r, processName, null, null, tag, null, null,
11498                            crashInfo);
11499                }
11500            });
11501            return false;
11502        }
11503
11504        addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo);
11505
11506        if (r != null && r.pid != Process.myPid() &&
11507                Settings.Global.getInt(mContext.getContentResolver(),
11508                        Settings.Global.WTF_IS_FATAL, 0) != 0) {
11509            crashApplication(r, crashInfo);
11510            return true;
11511        } else {
11512            return false;
11513        }
11514    }
11515
11516    /**
11517     * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit})
11518     * @return the corresponding {@link ProcessRecord} object, or null if none could be found
11519     */
11520    private ProcessRecord findAppProcess(IBinder app, String reason) {
11521        if (app == null) {
11522            return null;
11523        }
11524
11525        synchronized (this) {
11526            final int NP = mProcessNames.getMap().size();
11527            for (int ip=0; ip<NP; ip++) {
11528                SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
11529                final int NA = apps.size();
11530                for (int ia=0; ia<NA; ia++) {
11531                    ProcessRecord p = apps.valueAt(ia);
11532                    if (p.thread != null && p.thread.asBinder() == app) {
11533                        return p;
11534                    }
11535                }
11536            }
11537
11538            Slog.w(TAG, "Can't find mystery application for " + reason
11539                    + " from pid=" + Binder.getCallingPid()
11540                    + " uid=" + Binder.getCallingUid() + ": " + app);
11541            return null;
11542        }
11543    }
11544
11545    /**
11546     * Utility function for addErrorToDropBox and handleStrictModeViolation's logging
11547     * to append various headers to the dropbox log text.
11548     */
11549    private void appendDropBoxProcessHeaders(ProcessRecord process, String processName,
11550            StringBuilder sb) {
11551        // Watchdog thread ends up invoking this function (with
11552        // a null ProcessRecord) to add the stack file to dropbox.
11553        // Do not acquire a lock on this (am) in such cases, as it
11554        // could cause a potential deadlock, if and when watchdog
11555        // is invoked due to unavailability of lock on am and it
11556        // would prevent watchdog from killing system_server.
11557        if (process == null) {
11558            sb.append("Process: ").append(processName).append("\n");
11559            return;
11560        }
11561        // Note: ProcessRecord 'process' is guarded by the service
11562        // instance.  (notably process.pkgList, which could otherwise change
11563        // concurrently during execution of this method)
11564        synchronized (this) {
11565            sb.append("Process: ").append(processName).append("\n");
11566            int flags = process.info.flags;
11567            IPackageManager pm = AppGlobals.getPackageManager();
11568            sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
11569            for (int ip=0; ip<process.pkgList.size(); ip++) {
11570                String pkg = process.pkgList.keyAt(ip);
11571                sb.append("Package: ").append(pkg);
11572                try {
11573                    PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId());
11574                    if (pi != null) {
11575                        sb.append(" v").append(pi.versionCode);
11576                        if (pi.versionName != null) {
11577                            sb.append(" (").append(pi.versionName).append(")");
11578                        }
11579                    }
11580                } catch (RemoteException e) {
11581                    Slog.e(TAG, "Error getting package info: " + pkg, e);
11582                }
11583                sb.append("\n");
11584            }
11585        }
11586    }
11587
11588    private static String processClass(ProcessRecord process) {
11589        if (process == null || process.pid == MY_PID) {
11590            return "system_server";
11591        } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
11592            return "system_app";
11593        } else {
11594            return "data_app";
11595        }
11596    }
11597
11598    /**
11599     * Write a description of an error (crash, WTF, ANR) to the drop box.
11600     * @param eventType to include in the drop box tag ("crash", "wtf", etc.)
11601     * @param process which caused the error, null means the system server
11602     * @param activity which triggered the error, null if unknown
11603     * @param parent activity related to the error, null if unknown
11604     * @param subject line related to the error, null if absent
11605     * @param report in long form describing the error, null if absent
11606     * @param logFile to include in the report, null if none
11607     * @param crashInfo giving an application stack trace, null if absent
11608     */
11609    public void addErrorToDropBox(String eventType,
11610            ProcessRecord process, String processName, ActivityRecord activity,
11611            ActivityRecord parent, String subject,
11612            final String report, final File logFile,
11613            final ApplicationErrorReport.CrashInfo crashInfo) {
11614        // NOTE -- this must never acquire the ActivityManagerService lock,
11615        // otherwise the watchdog may be prevented from resetting the system.
11616
11617        final String dropboxTag = processClass(process) + "_" + eventType;
11618        final DropBoxManager dbox = (DropBoxManager)
11619                mContext.getSystemService(Context.DROPBOX_SERVICE);
11620
11621        // Exit early if the dropbox isn't configured to accept this report type.
11622        if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
11623
11624        final StringBuilder sb = new StringBuilder(1024);
11625        appendDropBoxProcessHeaders(process, processName, sb);
11626        if (activity != null) {
11627            sb.append("Activity: ").append(activity.shortComponentName).append("\n");
11628        }
11629        if (parent != null && parent.app != null && parent.app.pid != process.pid) {
11630            sb.append("Parent-Process: ").append(parent.app.processName).append("\n");
11631        }
11632        if (parent != null && parent != activity) {
11633            sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n");
11634        }
11635        if (subject != null) {
11636            sb.append("Subject: ").append(subject).append("\n");
11637        }
11638        sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
11639        if (Debug.isDebuggerConnected()) {
11640            sb.append("Debugger: Connected\n");
11641        }
11642        sb.append("\n");
11643
11644        // Do the rest in a worker thread to avoid blocking the caller on I/O
11645        // (After this point, we shouldn't access AMS internal data structures.)
11646        Thread worker = new Thread("Error dump: " + dropboxTag) {
11647            @Override
11648            public void run() {
11649                if (report != null) {
11650                    sb.append(report);
11651                }
11652                if (logFile != null) {
11653                    try {
11654                        sb.append(FileUtils.readTextFile(logFile, DROPBOX_MAX_SIZE,
11655                                    "\n\n[[TRUNCATED]]"));
11656                    } catch (IOException e) {
11657                        Slog.e(TAG, "Error reading " + logFile, e);
11658                    }
11659                }
11660                if (crashInfo != null && crashInfo.stackTrace != null) {
11661                    sb.append(crashInfo.stackTrace);
11662                }
11663
11664                String setting = Settings.Global.ERROR_LOGCAT_PREFIX + dropboxTag;
11665                int lines = Settings.Global.getInt(mContext.getContentResolver(), setting, 0);
11666                if (lines > 0) {
11667                    sb.append("\n");
11668
11669                    // Merge several logcat streams, and take the last N lines
11670                    InputStreamReader input = null;
11671                    try {
11672                        java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
11673                                "-v", "time", "-b", "events", "-b", "system", "-b", "main",
11674                                "-b", "crash",
11675                                "-t", String.valueOf(lines)).redirectErrorStream(true).start();
11676
11677                        try { logcat.getOutputStream().close(); } catch (IOException e) {}
11678                        try { logcat.getErrorStream().close(); } catch (IOException e) {}
11679                        input = new InputStreamReader(logcat.getInputStream());
11680
11681                        int num;
11682                        char[] buf = new char[8192];
11683                        while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
11684                    } catch (IOException e) {
11685                        Slog.e(TAG, "Error running logcat", e);
11686                    } finally {
11687                        if (input != null) try { input.close(); } catch (IOException e) {}
11688                    }
11689                }
11690
11691                dbox.addText(dropboxTag, sb.toString());
11692            }
11693        };
11694
11695        if (process == null) {
11696            // If process is null, we are being called from some internal code
11697            // and may be about to die -- run this synchronously.
11698            worker.run();
11699        } else {
11700            worker.start();
11701        }
11702    }
11703
11704    /**
11705     * Bring up the "unexpected error" dialog box for a crashing app.
11706     * Deal with edge cases (intercepts from instrumented applications,
11707     * ActivityController, error intent receivers, that sort of thing).
11708     * @param r the application crashing
11709     * @param crashInfo describing the failure
11710     */
11711    private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
11712        long timeMillis = System.currentTimeMillis();
11713        String shortMsg = crashInfo.exceptionClassName;
11714        String longMsg = crashInfo.exceptionMessage;
11715        String stackTrace = crashInfo.stackTrace;
11716        if (shortMsg != null && longMsg != null) {
11717            longMsg = shortMsg + ": " + longMsg;
11718        } else if (shortMsg != null) {
11719            longMsg = shortMsg;
11720        }
11721
11722        AppErrorResult result = new AppErrorResult();
11723        synchronized (this) {
11724            if (mController != null) {
11725                try {
11726                    String name = r != null ? r.processName : null;
11727                    int pid = r != null ? r.pid : Binder.getCallingPid();
11728                    int uid = r != null ? r.info.uid : Binder.getCallingUid();
11729                    if (!mController.appCrashed(name, pid,
11730                            shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
11731                        if ("1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"))
11732                                && "Native crash".equals(crashInfo.exceptionClassName)) {
11733                            Slog.w(TAG, "Skip killing native crashed app " + name
11734                                    + "(" + pid + ") during testing");
11735                        } else {
11736                            Slog.w(TAG, "Force-killing crashed app " + name
11737                                    + " at watcher's request");
11738                            if (r != null) {
11739                                r.kill("crash", true);
11740                            } else {
11741                                // Huh.
11742                                Process.killProcess(pid);
11743                                Process.killProcessGroup(uid, pid);
11744                            }
11745                        }
11746                        return;
11747                    }
11748                } catch (RemoteException e) {
11749                    mController = null;
11750                    Watchdog.getInstance().setActivityController(null);
11751                }
11752            }
11753
11754            final long origId = Binder.clearCallingIdentity();
11755
11756            // If this process is running instrumentation, finish it.
11757            if (r != null && r.instrumentationClass != null) {
11758                Slog.w(TAG, "Error in app " + r.processName
11759                      + " running instrumentation " + r.instrumentationClass + ":");
11760                if (shortMsg != null) Slog.w(TAG, "  " + shortMsg);
11761                if (longMsg != null) Slog.w(TAG, "  " + longMsg);
11762                Bundle info = new Bundle();
11763                info.putString("shortMsg", shortMsg);
11764                info.putString("longMsg", longMsg);
11765                finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info);
11766                Binder.restoreCallingIdentity(origId);
11767                return;
11768            }
11769
11770            // If we can't identify the process or it's already exceeded its crash quota,
11771            // quit right away without showing a crash dialog.
11772            if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) {
11773                Binder.restoreCallingIdentity(origId);
11774                return;
11775            }
11776
11777            Message msg = Message.obtain();
11778            msg.what = SHOW_ERROR_MSG;
11779            HashMap data = new HashMap();
11780            data.put("result", result);
11781            data.put("app", r);
11782            msg.obj = data;
11783            mHandler.sendMessage(msg);
11784
11785            Binder.restoreCallingIdentity(origId);
11786        }
11787
11788        int res = result.get();
11789
11790        Intent appErrorIntent = null;
11791        synchronized (this) {
11792            if (r != null && !r.isolated) {
11793                // XXX Can't keep track of crash time for isolated processes,
11794                // since they don't have a persistent identity.
11795                mProcessCrashTimes.put(r.info.processName, r.uid,
11796                        SystemClock.uptimeMillis());
11797            }
11798            if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
11799                appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
11800            }
11801        }
11802
11803        if (appErrorIntent != null) {
11804            try {
11805                mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId));
11806            } catch (ActivityNotFoundException e) {
11807                Slog.w(TAG, "bug report receiver dissappeared", e);
11808            }
11809        }
11810    }
11811
11812    Intent createAppErrorIntentLocked(ProcessRecord r,
11813            long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
11814        ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
11815        if (report == null) {
11816            return null;
11817        }
11818        Intent result = new Intent(Intent.ACTION_APP_ERROR);
11819        result.setComponent(r.errorReportReceiver);
11820        result.putExtra(Intent.EXTRA_BUG_REPORT, report);
11821        result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
11822        return result;
11823    }
11824
11825    private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
11826            long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
11827        if (r.errorReportReceiver == null) {
11828            return null;
11829        }
11830
11831        if (!r.crashing && !r.notResponding && !r.forceCrashReport) {
11832            return null;
11833        }
11834
11835        ApplicationErrorReport report = new ApplicationErrorReport();
11836        report.packageName = r.info.packageName;
11837        report.installerPackageName = r.errorReportReceiver.getPackageName();
11838        report.processName = r.processName;
11839        report.time = timeMillis;
11840        report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
11841
11842        if (r.crashing || r.forceCrashReport) {
11843            report.type = ApplicationErrorReport.TYPE_CRASH;
11844            report.crashInfo = crashInfo;
11845        } else if (r.notResponding) {
11846            report.type = ApplicationErrorReport.TYPE_ANR;
11847            report.anrInfo = new ApplicationErrorReport.AnrInfo();
11848
11849            report.anrInfo.activity = r.notRespondingReport.tag;
11850            report.anrInfo.cause = r.notRespondingReport.shortMsg;
11851            report.anrInfo.info = r.notRespondingReport.longMsg;
11852        }
11853
11854        return report;
11855    }
11856
11857    public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
11858        enforceNotIsolatedCaller("getProcessesInErrorState");
11859        // assume our apps are happy - lazy create the list
11860        List<ActivityManager.ProcessErrorStateInfo> errList = null;
11861
11862        final boolean allUsers = ActivityManager.checkUidPermission(INTERACT_ACROSS_USERS_FULL,
11863                Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
11864        int userId = UserHandle.getUserId(Binder.getCallingUid());
11865
11866        synchronized (this) {
11867
11868            // iterate across all processes
11869            for (int i=mLruProcesses.size()-1; i>=0; i--) {
11870                ProcessRecord app = mLruProcesses.get(i);
11871                if (!allUsers && app.userId != userId) {
11872                    continue;
11873                }
11874                if ((app.thread != null) && (app.crashing || app.notResponding)) {
11875                    // This one's in trouble, so we'll generate a report for it
11876                    // crashes are higher priority (in case there's a crash *and* an anr)
11877                    ActivityManager.ProcessErrorStateInfo report = null;
11878                    if (app.crashing) {
11879                        report = app.crashingReport;
11880                    } else if (app.notResponding) {
11881                        report = app.notRespondingReport;
11882                    }
11883
11884                    if (report != null) {
11885                        if (errList == null) {
11886                            errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1);
11887                        }
11888                        errList.add(report);
11889                    } else {
11890                        Slog.w(TAG, "Missing app error report, app = " + app.processName +
11891                                " crashing = " + app.crashing +
11892                                " notResponding = " + app.notResponding);
11893                    }
11894                }
11895            }
11896        }
11897
11898        return errList;
11899    }
11900
11901    static int procStateToImportance(int procState, int memAdj,
11902            ActivityManager.RunningAppProcessInfo currApp) {
11903        int imp = ActivityManager.RunningAppProcessInfo.procStateToImportance(procState);
11904        if (imp == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
11905            currApp.lru = memAdj;
11906        } else {
11907            currApp.lru = 0;
11908        }
11909        return imp;
11910    }
11911
11912    private void fillInProcMemInfo(ProcessRecord app,
11913            ActivityManager.RunningAppProcessInfo outInfo) {
11914        outInfo.pid = app.pid;
11915        outInfo.uid = app.info.uid;
11916        if (mHeavyWeightProcess == app) {
11917            outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE;
11918        }
11919        if (app.persistent) {
11920            outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT;
11921        }
11922        if (app.activities.size() > 0) {
11923            outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES;
11924        }
11925        outInfo.lastTrimLevel = app.trimMemoryLevel;
11926        int adj = app.curAdj;
11927        int procState = app.curProcState;
11928        outInfo.importance = procStateToImportance(procState, adj, outInfo);
11929        outInfo.importanceReasonCode = app.adjTypeCode;
11930        outInfo.processState = app.curProcState;
11931    }
11932
11933    public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
11934        enforceNotIsolatedCaller("getRunningAppProcesses");
11935        // Lazy instantiation of list
11936        List<ActivityManager.RunningAppProcessInfo> runList = null;
11937        final boolean allUsers = ActivityManager.checkUidPermission(INTERACT_ACROSS_USERS_FULL,
11938                Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
11939        int userId = UserHandle.getUserId(Binder.getCallingUid());
11940        synchronized (this) {
11941            // Iterate across all processes
11942            for (int i=mLruProcesses.size()-1; i>=0; i--) {
11943                ProcessRecord app = mLruProcesses.get(i);
11944                if (!allUsers && app.userId != userId) {
11945                    continue;
11946                }
11947                if ((app.thread != null) && (!app.crashing && !app.notResponding)) {
11948                    // Generate process state info for running application
11949                    ActivityManager.RunningAppProcessInfo currApp =
11950                        new ActivityManager.RunningAppProcessInfo(app.processName,
11951                                app.pid, app.getPackageList());
11952                    fillInProcMemInfo(app, currApp);
11953                    if (app.adjSource instanceof ProcessRecord) {
11954                        currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
11955                        currApp.importanceReasonImportance =
11956                                ActivityManager.RunningAppProcessInfo.procStateToImportance(
11957                                        app.adjSourceProcState);
11958                    } else if (app.adjSource instanceof ActivityRecord) {
11959                        ActivityRecord r = (ActivityRecord)app.adjSource;
11960                        if (r.app != null) currApp.importanceReasonPid = r.app.pid;
11961                    }
11962                    if (app.adjTarget instanceof ComponentName) {
11963                        currApp.importanceReasonComponent = (ComponentName)app.adjTarget;
11964                    }
11965                    //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance
11966                    //        + " lru=" + currApp.lru);
11967                    if (runList == null) {
11968                        runList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
11969                    }
11970                    runList.add(currApp);
11971                }
11972            }
11973        }
11974        return runList;
11975    }
11976
11977    public List<ApplicationInfo> getRunningExternalApplications() {
11978        enforceNotIsolatedCaller("getRunningExternalApplications");
11979        List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses();
11980        List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
11981        if (runningApps != null && runningApps.size() > 0) {
11982            Set<String> extList = new HashSet<String>();
11983            for (ActivityManager.RunningAppProcessInfo app : runningApps) {
11984                if (app.pkgList != null) {
11985                    for (String pkg : app.pkgList) {
11986                        extList.add(pkg);
11987                    }
11988                }
11989            }
11990            IPackageManager pm = AppGlobals.getPackageManager();
11991            for (String pkg : extList) {
11992                try {
11993                    ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId());
11994                    if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
11995                        retList.add(info);
11996                    }
11997                } catch (RemoteException e) {
11998                }
11999            }
12000        }
12001        return retList;
12002    }
12003
12004    @Override
12005    public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) {
12006        enforceNotIsolatedCaller("getMyMemoryState");
12007        synchronized (this) {
12008            ProcessRecord proc;
12009            synchronized (mPidsSelfLocked) {
12010                proc = mPidsSelfLocked.get(Binder.getCallingPid());
12011            }
12012            fillInProcMemInfo(proc, outInfo);
12013        }
12014    }
12015
12016    @Override
12017    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
12018        if (checkCallingPermission(android.Manifest.permission.DUMP)
12019                != PackageManager.PERMISSION_GRANTED) {
12020            pw.println("Permission Denial: can't dump ActivityManager from from pid="
12021                    + Binder.getCallingPid()
12022                    + ", uid=" + Binder.getCallingUid()
12023                    + " without permission "
12024                    + android.Manifest.permission.DUMP);
12025            return;
12026        }
12027
12028        boolean dumpAll = false;
12029        boolean dumpClient = false;
12030        String dumpPackage = null;
12031
12032        int opti = 0;
12033        while (opti < args.length) {
12034            String opt = args[opti];
12035            if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
12036                break;
12037            }
12038            opti++;
12039            if ("-a".equals(opt)) {
12040                dumpAll = true;
12041            } else if ("-c".equals(opt)) {
12042                dumpClient = true;
12043            } else if ("-h".equals(opt)) {
12044                pw.println("Activity manager dump options:");
12045                pw.println("  [-a] [-c] [-h] [cmd] ...");
12046                pw.println("  cmd may be one of:");
12047                pw.println("    a[ctivities]: activity stack state");
12048                pw.println("    r[recents]: recent activities state");
12049                pw.println("    b[roadcasts] [PACKAGE_NAME] [history [-s]]: broadcast state");
12050                pw.println("    i[ntents] [PACKAGE_NAME]: pending intent state");
12051                pw.println("    p[rocesses] [PACKAGE_NAME]: process state");
12052                pw.println("    o[om]: out of memory management");
12053                pw.println("    prov[iders] [COMP_SPEC ...]: content provider state");
12054                pw.println("    provider [COMP_SPEC]: provider client-side state");
12055                pw.println("    s[ervices] [COMP_SPEC ...]: service state");
12056                pw.println("    service [COMP_SPEC]: service client-side state");
12057                pw.println("    package [PACKAGE_NAME]: all state related to given package");
12058                pw.println("    all: dump all activities");
12059                pw.println("    top: dump the top activity");
12060                pw.println("  cmd may also be a COMP_SPEC to dump activities.");
12061                pw.println("  COMP_SPEC may be a component name (com.foo/.myApp),");
12062                pw.println("    a partial substring in a component name, a");
12063                pw.println("    hex object identifier.");
12064                pw.println("  -a: include all available server state.");
12065                pw.println("  -c: include client state.");
12066                return;
12067            } else {
12068                pw.println("Unknown argument: " + opt + "; use -h for help");
12069            }
12070        }
12071
12072        long origId = Binder.clearCallingIdentity();
12073        boolean more = false;
12074        // Is the caller requesting to dump a particular piece of data?
12075        if (opti < args.length) {
12076            String cmd = args[opti];
12077            opti++;
12078            if ("activities".equals(cmd) || "a".equals(cmd)) {
12079                synchronized (this) {
12080                    dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null);
12081                }
12082            } else if ("recents".equals(cmd) || "r".equals(cmd)) {
12083                synchronized (this) {
12084                    dumpRecentsLocked(fd, pw, args, opti, true, null);
12085                }
12086            } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) {
12087                String[] newArgs;
12088                String name;
12089                if (opti >= args.length) {
12090                    name = null;
12091                    newArgs = EMPTY_STRING_ARRAY;
12092                } else {
12093                    name = args[opti];
12094                    opti++;
12095                    newArgs = new String[args.length - opti];
12096                    if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
12097                            args.length - opti);
12098                }
12099                synchronized (this) {
12100                    dumpBroadcastsLocked(fd, pw, args, opti, true, name);
12101                }
12102            } else if ("intents".equals(cmd) || "i".equals(cmd)) {
12103                String[] newArgs;
12104                String name;
12105                if (opti >= args.length) {
12106                    name = null;
12107                    newArgs = EMPTY_STRING_ARRAY;
12108                } else {
12109                    name = args[opti];
12110                    opti++;
12111                    newArgs = new String[args.length - opti];
12112                    if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
12113                            args.length - opti);
12114                }
12115                synchronized (this) {
12116                    dumpPendingIntentsLocked(fd, pw, args, opti, true, name);
12117                }
12118            } else if ("processes".equals(cmd) || "p".equals(cmd)) {
12119                String[] newArgs;
12120                String name;
12121                if (opti >= args.length) {
12122                    name = null;
12123                    newArgs = EMPTY_STRING_ARRAY;
12124                } else {
12125                    name = args[opti];
12126                    opti++;
12127                    newArgs = new String[args.length - opti];
12128                    if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
12129                            args.length - opti);
12130                }
12131                synchronized (this) {
12132                    dumpProcessesLocked(fd, pw, args, opti, true, name);
12133                }
12134            } else if ("oom".equals(cmd) || "o".equals(cmd)) {
12135                synchronized (this) {
12136                    dumpOomLocked(fd, pw, args, opti, true);
12137                }
12138            } else if ("provider".equals(cmd)) {
12139                String[] newArgs;
12140                String name;
12141                if (opti >= args.length) {
12142                    name = null;
12143                    newArgs = EMPTY_STRING_ARRAY;
12144                } else {
12145                    name = args[opti];
12146                    opti++;
12147                    newArgs = new String[args.length - opti];
12148                    if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
12149                }
12150                if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) {
12151                    pw.println("No providers match: " + name);
12152                    pw.println("Use -h for help.");
12153                }
12154            } else if ("providers".equals(cmd) || "prov".equals(cmd)) {
12155                synchronized (this) {
12156                    dumpProvidersLocked(fd, pw, args, opti, true, null);
12157                }
12158            } else if ("service".equals(cmd)) {
12159                String[] newArgs;
12160                String name;
12161                if (opti >= args.length) {
12162                    name = null;
12163                    newArgs = EMPTY_STRING_ARRAY;
12164                } else {
12165                    name = args[opti];
12166                    opti++;
12167                    newArgs = new String[args.length - opti];
12168                    if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
12169                            args.length - opti);
12170                }
12171                if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) {
12172                    pw.println("No services match: " + name);
12173                    pw.println("Use -h for help.");
12174                }
12175            } else if ("package".equals(cmd)) {
12176                String[] newArgs;
12177                if (opti >= args.length) {
12178                    pw.println("package: no package name specified");
12179                    pw.println("Use -h for help.");
12180                } else {
12181                    dumpPackage = args[opti];
12182                    opti++;
12183                    newArgs = new String[args.length - opti];
12184                    if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
12185                            args.length - opti);
12186                    args = newArgs;
12187                    opti = 0;
12188                    more = true;
12189                }
12190            } else if ("services".equals(cmd) || "s".equals(cmd)) {
12191                synchronized (this) {
12192                    mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null);
12193                }
12194            } else {
12195                // Dumping a single activity?
12196                if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) {
12197                    pw.println("Bad activity command, or no activities match: " + cmd);
12198                    pw.println("Use -h for help.");
12199                }
12200            }
12201            if (!more) {
12202                Binder.restoreCallingIdentity(origId);
12203                return;
12204            }
12205        }
12206
12207        // No piece of data specified, dump everything.
12208        synchronized (this) {
12209            dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
12210            pw.println();
12211            if (dumpAll) {
12212                pw.println("-------------------------------------------------------------------------------");
12213            }
12214            dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
12215            pw.println();
12216            if (dumpAll) {
12217                pw.println("-------------------------------------------------------------------------------");
12218            }
12219            dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage);
12220            pw.println();
12221            if (dumpAll) {
12222                pw.println("-------------------------------------------------------------------------------");
12223            }
12224            mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
12225            pw.println();
12226            if (dumpAll) {
12227                pw.println("-------------------------------------------------------------------------------");
12228            }
12229            dumpRecentsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
12230            pw.println();
12231            if (dumpAll) {
12232                pw.println("-------------------------------------------------------------------------------");
12233            }
12234            dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
12235            pw.println();
12236            if (dumpAll) {
12237                pw.println("-------------------------------------------------------------------------------");
12238            }
12239            dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage);
12240        }
12241        Binder.restoreCallingIdentity(origId);
12242    }
12243
12244    void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
12245            int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) {
12246        pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)");
12247
12248        boolean printedAnything = mStackSupervisor.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient,
12249                dumpPackage);
12250        boolean needSep = printedAnything;
12251
12252        boolean printed = ActivityStackSupervisor.printThisActivity(pw, mFocusedActivity,
12253                dumpPackage, needSep, "  mFocusedActivity: ");
12254        if (printed) {
12255            printedAnything = true;
12256            needSep = false;
12257        }
12258
12259        if (dumpPackage == null) {
12260            if (needSep) {
12261                pw.println();
12262            }
12263            needSep = true;
12264            printedAnything = true;
12265            mStackSupervisor.dump(pw, "  ");
12266        }
12267
12268        if (!printedAnything) {
12269            pw.println("  (nothing)");
12270        }
12271    }
12272
12273    void dumpRecentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
12274            int opti, boolean dumpAll, String dumpPackage) {
12275        pw.println("ACTIVITY MANAGER RECENT ACTIVITIES (dumpsys activity recents)");
12276
12277        boolean printedAnything = false;
12278
12279        if (mRecentTasks.size() > 0) {
12280            boolean printedHeader = false;
12281
12282            final int N = mRecentTasks.size();
12283            for (int i=0; i<N; i++) {
12284                TaskRecord tr = mRecentTasks.get(i);
12285                if (dumpPackage != null) {
12286                    if (tr.realActivity == null ||
12287                            !dumpPackage.equals(tr.realActivity)) {
12288                        continue;
12289                    }
12290                }
12291                if (!printedHeader) {
12292                    pw.println("  Recent tasks:");
12293                    printedHeader = true;
12294                    printedAnything = true;
12295                }
12296                pw.print("  * Recent #"); pw.print(i); pw.print(": ");
12297                        pw.println(tr);
12298                if (dumpAll) {
12299                    mRecentTasks.get(i).dump(pw, "    ");
12300                }
12301            }
12302        }
12303
12304        if (!printedAnything) {
12305            pw.println("  (nothing)");
12306        }
12307    }
12308
12309    void dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
12310            int opti, boolean dumpAll, String dumpPackage) {
12311        boolean needSep = false;
12312        boolean printedAnything = false;
12313        int numPers = 0;
12314
12315        pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)");
12316
12317        if (dumpAll) {
12318            final int NP = mProcessNames.getMap().size();
12319            for (int ip=0; ip<NP; ip++) {
12320                SparseArray<ProcessRecord> procs = mProcessNames.getMap().valueAt(ip);
12321                final int NA = procs.size();
12322                for (int ia=0; ia<NA; ia++) {
12323                    ProcessRecord r = procs.valueAt(ia);
12324                    if (dumpPackage != null && !r.pkgList.containsKey(dumpPackage)) {
12325                        continue;
12326                    }
12327                    if (!needSep) {
12328                        pw.println("  All known processes:");
12329                        needSep = true;
12330                        printedAnything = true;
12331                    }
12332                    pw.print(r.persistent ? "  *PERS*" : "  *APP*");
12333                        pw.print(" UID "); pw.print(procs.keyAt(ia));
12334                        pw.print(" "); pw.println(r);
12335                    r.dump(pw, "    ");
12336                    if (r.persistent) {
12337                        numPers++;
12338                    }
12339                }
12340            }
12341        }
12342
12343        if (mIsolatedProcesses.size() > 0) {
12344            boolean printed = false;
12345            for (int i=0; i<mIsolatedProcesses.size(); i++) {
12346                ProcessRecord r = mIsolatedProcesses.valueAt(i);
12347                if (dumpPackage != null && !r.pkgList.containsKey(dumpPackage)) {
12348                    continue;
12349                }
12350                if (!printed) {
12351                    if (needSep) {
12352                        pw.println();
12353                    }
12354                    pw.println("  Isolated process list (sorted by uid):");
12355                    printedAnything = true;
12356                    printed = true;
12357                    needSep = true;
12358                }
12359                pw.println(String.format("%sIsolated #%2d: %s",
12360                        "    ", i, r.toString()));
12361            }
12362        }
12363
12364        if (mLruProcesses.size() > 0) {
12365            if (needSep) {
12366                pw.println();
12367            }
12368            pw.print("  Process LRU list (sorted by oom_adj, "); pw.print(mLruProcesses.size());
12369                    pw.print(" total, non-act at ");
12370                    pw.print(mLruProcesses.size()-mLruProcessActivityStart);
12371                    pw.print(", non-svc at ");
12372                    pw.print(mLruProcesses.size()-mLruProcessServiceStart);
12373                    pw.println("):");
12374            dumpProcessOomList(pw, this, mLruProcesses, "    ", "Proc", "PERS", false, dumpPackage);
12375            needSep = true;
12376            printedAnything = true;
12377        }
12378
12379        if (dumpAll || dumpPackage != null) {
12380            synchronized (mPidsSelfLocked) {
12381                boolean printed = false;
12382                for (int i=0; i<mPidsSelfLocked.size(); i++) {
12383                    ProcessRecord r = mPidsSelfLocked.valueAt(i);
12384                    if (dumpPackage != null && !r.pkgList.containsKey(dumpPackage)) {
12385                        continue;
12386                    }
12387                    if (!printed) {
12388                        if (needSep) pw.println();
12389                        needSep = true;
12390                        pw.println("  PID mappings:");
12391                        printed = true;
12392                        printedAnything = true;
12393                    }
12394                    pw.print("    PID #"); pw.print(mPidsSelfLocked.keyAt(i));
12395                        pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i));
12396                }
12397            }
12398        }
12399
12400        if (mForegroundProcesses.size() > 0) {
12401            synchronized (mPidsSelfLocked) {
12402                boolean printed = false;
12403                for (int i=0; i<mForegroundProcesses.size(); i++) {
12404                    ProcessRecord r = mPidsSelfLocked.get(
12405                            mForegroundProcesses.valueAt(i).pid);
12406                    if (dumpPackage != null && (r == null
12407                            || !r.pkgList.containsKey(dumpPackage))) {
12408                        continue;
12409                    }
12410                    if (!printed) {
12411                        if (needSep) pw.println();
12412                        needSep = true;
12413                        pw.println("  Foreground Processes:");
12414                        printed = true;
12415                        printedAnything = true;
12416                    }
12417                    pw.print("    PID #"); pw.print(mForegroundProcesses.keyAt(i));
12418                            pw.print(": "); pw.println(mForegroundProcesses.valueAt(i));
12419                }
12420            }
12421        }
12422
12423        if (mPersistentStartingProcesses.size() > 0) {
12424            if (needSep) pw.println();
12425            needSep = true;
12426            printedAnything = true;
12427            pw.println("  Persisent processes that are starting:");
12428            dumpProcessList(pw, this, mPersistentStartingProcesses, "    ",
12429                    "Starting Norm", "Restarting PERS", dumpPackage);
12430        }
12431
12432        if (mRemovedProcesses.size() > 0) {
12433            if (needSep) pw.println();
12434            needSep = true;
12435            printedAnything = true;
12436            pw.println("  Processes that are being removed:");
12437            dumpProcessList(pw, this, mRemovedProcesses, "    ",
12438                    "Removed Norm", "Removed PERS", dumpPackage);
12439        }
12440
12441        if (mProcessesOnHold.size() > 0) {
12442            if (needSep) pw.println();
12443            needSep = true;
12444            printedAnything = true;
12445            pw.println("  Processes that are on old until the system is ready:");
12446            dumpProcessList(pw, this, mProcessesOnHold, "    ",
12447                    "OnHold Norm", "OnHold PERS", dumpPackage);
12448        }
12449
12450        needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage);
12451
12452        if (mProcessCrashTimes.getMap().size() > 0) {
12453            boolean printed = false;
12454            long now = SystemClock.uptimeMillis();
12455            final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap();
12456            final int NP = pmap.size();
12457            for (int ip=0; ip<NP; ip++) {
12458                String pname = pmap.keyAt(ip);
12459                SparseArray<Long> uids = pmap.valueAt(ip);
12460                final int N = uids.size();
12461                for (int i=0; i<N; i++) {
12462                    int puid = uids.keyAt(i);
12463                    ProcessRecord r = mProcessNames.get(pname, puid);
12464                    if (dumpPackage != null && (r == null
12465                            || !r.pkgList.containsKey(dumpPackage))) {
12466                        continue;
12467                    }
12468                    if (!printed) {
12469                        if (needSep) pw.println();
12470                        needSep = true;
12471                        pw.println("  Time since processes crashed:");
12472                        printed = true;
12473                        printedAnything = true;
12474                    }
12475                    pw.print("    Process "); pw.print(pname);
12476                            pw.print(" uid "); pw.print(puid);
12477                            pw.print(": last crashed ");
12478                            TimeUtils.formatDuration(now-uids.valueAt(i), pw);
12479                            pw.println(" ago");
12480                }
12481            }
12482        }
12483
12484        if (mBadProcesses.getMap().size() > 0) {
12485            boolean printed = false;
12486            final ArrayMap<String, SparseArray<BadProcessInfo>> pmap = mBadProcesses.getMap();
12487            final int NP = pmap.size();
12488            for (int ip=0; ip<NP; ip++) {
12489                String pname = pmap.keyAt(ip);
12490                SparseArray<BadProcessInfo> uids = pmap.valueAt(ip);
12491                final int N = uids.size();
12492                for (int i=0; i<N; i++) {
12493                    int puid = uids.keyAt(i);
12494                    ProcessRecord r = mProcessNames.get(pname, puid);
12495                    if (dumpPackage != null && (r == null
12496                            || !r.pkgList.containsKey(dumpPackage))) {
12497                        continue;
12498                    }
12499                    if (!printed) {
12500                        if (needSep) pw.println();
12501                        needSep = true;
12502                        pw.println("  Bad processes:");
12503                        printedAnything = true;
12504                    }
12505                    BadProcessInfo info = uids.valueAt(i);
12506                    pw.print("    Bad process "); pw.print(pname);
12507                            pw.print(" uid "); pw.print(puid);
12508                            pw.print(": crashed at time "); pw.println(info.time);
12509                    if (info.shortMsg != null) {
12510                        pw.print("      Short msg: "); pw.println(info.shortMsg);
12511                    }
12512                    if (info.longMsg != null) {
12513                        pw.print("      Long msg: "); pw.println(info.longMsg);
12514                    }
12515                    if (info.stack != null) {
12516                        pw.println("      Stack:");
12517                        int lastPos = 0;
12518                        for (int pos=0; pos<info.stack.length(); pos++) {
12519                            if (info.stack.charAt(pos) == '\n') {
12520                                pw.print("        ");
12521                                pw.write(info.stack, lastPos, pos-lastPos);
12522                                pw.println();
12523                                lastPos = pos+1;
12524                            }
12525                        }
12526                        if (lastPos < info.stack.length()) {
12527                            pw.print("        ");
12528                            pw.write(info.stack, lastPos, info.stack.length()-lastPos);
12529                            pw.println();
12530                        }
12531                    }
12532                }
12533            }
12534        }
12535
12536        if (dumpPackage == null) {
12537            pw.println();
12538            needSep = false;
12539            pw.println("  mStartedUsers:");
12540            for (int i=0; i<mStartedUsers.size(); i++) {
12541                UserStartedState uss = mStartedUsers.valueAt(i);
12542                pw.print("    User #"); pw.print(uss.mHandle.getIdentifier());
12543                        pw.print(": "); uss.dump("", pw);
12544            }
12545            pw.print("  mStartedUserArray: [");
12546            for (int i=0; i<mStartedUserArray.length; i++) {
12547                if (i > 0) pw.print(", ");
12548                pw.print(mStartedUserArray[i]);
12549            }
12550            pw.println("]");
12551            pw.print("  mUserLru: [");
12552            for (int i=0; i<mUserLru.size(); i++) {
12553                if (i > 0) pw.print(", ");
12554                pw.print(mUserLru.get(i));
12555            }
12556            pw.println("]");
12557            if (dumpAll) {
12558                pw.print("  mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray));
12559            }
12560            synchronized (mUserProfileGroupIdsSelfLocked) {
12561                if (mUserProfileGroupIdsSelfLocked.size() > 0) {
12562                    pw.println("  mUserProfileGroupIds:");
12563                    for (int i=0; i<mUserProfileGroupIdsSelfLocked.size(); i++) {
12564                        pw.print("    User #");
12565                        pw.print(mUserProfileGroupIdsSelfLocked.keyAt(i));
12566                        pw.print(" -> profile #");
12567                        pw.println(mUserProfileGroupIdsSelfLocked.valueAt(i));
12568                    }
12569                }
12570            }
12571        }
12572        if (mHomeProcess != null && (dumpPackage == null
12573                || mHomeProcess.pkgList.containsKey(dumpPackage))) {
12574            if (needSep) {
12575                pw.println();
12576                needSep = false;
12577            }
12578            pw.println("  mHomeProcess: " + mHomeProcess);
12579        }
12580        if (mPreviousProcess != null && (dumpPackage == null
12581                || mPreviousProcess.pkgList.containsKey(dumpPackage))) {
12582            if (needSep) {
12583                pw.println();
12584                needSep = false;
12585            }
12586            pw.println("  mPreviousProcess: " + mPreviousProcess);
12587        }
12588        if (dumpAll) {
12589            StringBuilder sb = new StringBuilder(128);
12590            sb.append("  mPreviousProcessVisibleTime: ");
12591            TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb);
12592            pw.println(sb);
12593        }
12594        if (mHeavyWeightProcess != null && (dumpPackage == null
12595                || mHeavyWeightProcess.pkgList.containsKey(dumpPackage))) {
12596            if (needSep) {
12597                pw.println();
12598                needSep = false;
12599            }
12600            pw.println("  mHeavyWeightProcess: " + mHeavyWeightProcess);
12601        }
12602        if (dumpPackage == null) {
12603            pw.println("  mConfiguration: " + mConfiguration);
12604        }
12605        if (dumpAll) {
12606            pw.println("  mConfigWillChange: " + getFocusedStack().mConfigWillChange);
12607            if (mCompatModePackages.getPackages().size() > 0) {
12608                boolean printed = false;
12609                for (Map.Entry<String, Integer> entry
12610                        : mCompatModePackages.getPackages().entrySet()) {
12611                    String pkg = entry.getKey();
12612                    int mode = entry.getValue();
12613                    if (dumpPackage != null && !dumpPackage.equals(pkg)) {
12614                        continue;
12615                    }
12616                    if (!printed) {
12617                        pw.println("  mScreenCompatPackages:");
12618                        printed = true;
12619                    }
12620                    pw.print("    "); pw.print(pkg); pw.print(": ");
12621                            pw.print(mode); pw.println();
12622                }
12623            }
12624        }
12625        if (dumpPackage == null) {
12626            if (mSleeping || mWentToSleep || mLockScreenShown) {
12627                pw.println("  mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep
12628                        + " mLockScreenShown " + mLockScreenShown);
12629            }
12630            if (mShuttingDown || mRunningVoice) {
12631                pw.print("  mShuttingDown=" + mShuttingDown + " mRunningVoice=" + mRunningVoice);
12632            }
12633        }
12634        if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient
12635                || mOrigWaitForDebugger) {
12636            if (dumpPackage == null || dumpPackage.equals(mDebugApp)
12637                    || dumpPackage.equals(mOrigDebugApp)) {
12638                if (needSep) {
12639                    pw.println();
12640                    needSep = false;
12641                }
12642                pw.println("  mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp
12643                        + " mDebugTransient=" + mDebugTransient
12644                        + " mOrigWaitForDebugger=" + mOrigWaitForDebugger);
12645            }
12646        }
12647        if (mOpenGlTraceApp != null) {
12648            if (dumpPackage == null || dumpPackage.equals(mOpenGlTraceApp)) {
12649                if (needSep) {
12650                    pw.println();
12651                    needSep = false;
12652                }
12653                pw.println("  mOpenGlTraceApp=" + mOpenGlTraceApp);
12654            }
12655        }
12656        if (mProfileApp != null || mProfileProc != null || mProfileFile != null
12657                || mProfileFd != null) {
12658            if (dumpPackage == null || dumpPackage.equals(mProfileApp)) {
12659                if (needSep) {
12660                    pw.println();
12661                    needSep = false;
12662                }
12663                pw.println("  mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc);
12664                pw.println("  mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd);
12665                pw.println("  mSamplingInterval=" + mSamplingInterval + " mAutoStopProfiler="
12666                        + mAutoStopProfiler);
12667                pw.println("  mProfileType=" + mProfileType);
12668            }
12669        }
12670        if (dumpPackage == null) {
12671            if (mAlwaysFinishActivities || mController != null) {
12672                pw.println("  mAlwaysFinishActivities=" + mAlwaysFinishActivities
12673                        + " mController=" + mController);
12674            }
12675            if (dumpAll) {
12676                pw.println("  Total persistent processes: " + numPers);
12677                pw.println("  mProcessesReady=" + mProcessesReady
12678                        + " mSystemReady=" + mSystemReady);
12679                pw.println("  mBooting=" + mBooting
12680                        + " mBooted=" + mBooted
12681                        + " mFactoryTest=" + mFactoryTest);
12682                pw.print("  mLastPowerCheckRealtime=");
12683                        TimeUtils.formatDuration(mLastPowerCheckRealtime, pw);
12684                        pw.println("");
12685                pw.print("  mLastPowerCheckUptime=");
12686                        TimeUtils.formatDuration(mLastPowerCheckUptime, pw);
12687                        pw.println("");
12688                pw.println("  mGoingToSleep=" + mStackSupervisor.mGoingToSleep);
12689                pw.println("  mLaunchingActivity=" + mStackSupervisor.mLaunchingActivity);
12690                pw.println("  mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq);
12691                pw.println("  mNumNonCachedProcs=" + mNumNonCachedProcs
12692                        + " (" + mLruProcesses.size() + " total)"
12693                        + " mNumCachedHiddenProcs=" + mNumCachedHiddenProcs
12694                        + " mNumServiceProcs=" + mNumServiceProcs
12695                        + " mNewNumServiceProcs=" + mNewNumServiceProcs);
12696                pw.println("  mAllowLowerMemLevel=" + mAllowLowerMemLevel
12697                        + " mLastMemoryLevel" + mLastMemoryLevel
12698                        + " mLastNumProcesses" + mLastNumProcesses);
12699                long now = SystemClock.uptimeMillis();
12700                pw.print("  mLastIdleTime=");
12701                        TimeUtils.formatDuration(now, mLastIdleTime, pw);
12702                        pw.print(" mLowRamSinceLastIdle=");
12703                        TimeUtils.formatDuration(getLowRamTimeSinceIdle(now), pw);
12704                        pw.println();
12705            }
12706        }
12707
12708        if (!printedAnything) {
12709            pw.println("  (nothing)");
12710        }
12711    }
12712
12713    boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args,
12714            int opti, boolean needSep, boolean dumpAll, String dumpPackage) {
12715        if (mProcessesToGc.size() > 0) {
12716            boolean printed = false;
12717            long now = SystemClock.uptimeMillis();
12718            for (int i=0; i<mProcessesToGc.size(); i++) {
12719                ProcessRecord proc = mProcessesToGc.get(i);
12720                if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) {
12721                    continue;
12722                }
12723                if (!printed) {
12724                    if (needSep) pw.println();
12725                    needSep = true;
12726                    pw.println("  Processes that are waiting to GC:");
12727                    printed = true;
12728                }
12729                pw.print("    Process "); pw.println(proc);
12730                pw.print("      lowMem="); pw.print(proc.reportLowMemory);
12731                        pw.print(", last gced=");
12732                        pw.print(now-proc.lastRequestedGc);
12733                        pw.print(" ms ago, last lowMem=");
12734                        pw.print(now-proc.lastLowMemory);
12735                        pw.println(" ms ago");
12736
12737            }
12738        }
12739        return needSep;
12740    }
12741
12742    void printOomLevel(PrintWriter pw, String name, int adj) {
12743        pw.print("    ");
12744        if (adj >= 0) {
12745            pw.print(' ');
12746            if (adj < 10) pw.print(' ');
12747        } else {
12748            if (adj > -10) pw.print(' ');
12749        }
12750        pw.print(adj);
12751        pw.print(": ");
12752        pw.print(name);
12753        pw.print(" (");
12754        pw.print(mProcessList.getMemLevel(adj)/1024);
12755        pw.println(" kB)");
12756    }
12757
12758    boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args,
12759            int opti, boolean dumpAll) {
12760        boolean needSep = false;
12761
12762        if (mLruProcesses.size() > 0) {
12763            if (needSep) pw.println();
12764            needSep = true;
12765            pw.println("  OOM levels:");
12766            printOomLevel(pw, "SYSTEM_ADJ", ProcessList.SYSTEM_ADJ);
12767            printOomLevel(pw, "PERSISTENT_PROC_ADJ", ProcessList.PERSISTENT_PROC_ADJ);
12768            printOomLevel(pw, "FOREGROUND_APP_ADJ", ProcessList.FOREGROUND_APP_ADJ);
12769            printOomLevel(pw, "VISIBLE_APP_ADJ", ProcessList.VISIBLE_APP_ADJ);
12770            printOomLevel(pw, "PERCEPTIBLE_APP_ADJ", ProcessList.PERCEPTIBLE_APP_ADJ);
12771            printOomLevel(pw, "BACKUP_APP_ADJ", ProcessList.BACKUP_APP_ADJ);
12772            printOomLevel(pw, "HEAVY_WEIGHT_APP_ADJ", ProcessList.HEAVY_WEIGHT_APP_ADJ);
12773            printOomLevel(pw, "SERVICE_ADJ", ProcessList.SERVICE_ADJ);
12774            printOomLevel(pw, "HOME_APP_ADJ", ProcessList.HOME_APP_ADJ);
12775            printOomLevel(pw, "PREVIOUS_APP_ADJ", ProcessList.PREVIOUS_APP_ADJ);
12776            printOomLevel(pw, "SERVICE_B_ADJ", ProcessList.SERVICE_B_ADJ);
12777            printOomLevel(pw, "CACHED_APP_MIN_ADJ", ProcessList.CACHED_APP_MIN_ADJ);
12778            printOomLevel(pw, "CACHED_APP_MAX_ADJ", ProcessList.CACHED_APP_MAX_ADJ);
12779
12780            if (needSep) pw.println();
12781            pw.print("  Process OOM control ("); pw.print(mLruProcesses.size());
12782                    pw.print(" total, non-act at ");
12783                    pw.print(mLruProcesses.size()-mLruProcessActivityStart);
12784                    pw.print(", non-svc at ");
12785                    pw.print(mLruProcesses.size()-mLruProcessServiceStart);
12786                    pw.println("):");
12787            dumpProcessOomList(pw, this, mLruProcesses, "    ", "Proc", "PERS", true, null);
12788            needSep = true;
12789        }
12790
12791        dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null);
12792
12793        pw.println();
12794        pw.println("  mHomeProcess: " + mHomeProcess);
12795        pw.println("  mPreviousProcess: " + mPreviousProcess);
12796        if (mHeavyWeightProcess != null) {
12797            pw.println("  mHeavyWeightProcess: " + mHeavyWeightProcess);
12798        }
12799
12800        return true;
12801    }
12802
12803    /**
12804     * There are three ways to call this:
12805     *  - no provider specified: dump all the providers
12806     *  - a flattened component name that matched an existing provider was specified as the
12807     *    first arg: dump that one provider
12808     *  - the first arg isn't the flattened component name of an existing provider:
12809     *    dump all providers whose component contains the first arg as a substring
12810     */
12811    protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args,
12812            int opti, boolean dumpAll) {
12813        return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll);
12814    }
12815
12816    static class ItemMatcher {
12817        ArrayList<ComponentName> components;
12818        ArrayList<String> strings;
12819        ArrayList<Integer> objects;
12820        boolean all;
12821
12822        ItemMatcher() {
12823            all = true;
12824        }
12825
12826        void build(String name) {
12827            ComponentName componentName = ComponentName.unflattenFromString(name);
12828            if (componentName != null) {
12829                if (components == null) {
12830                    components = new ArrayList<ComponentName>();
12831                }
12832                components.add(componentName);
12833                all = false;
12834            } else {
12835                int objectId = 0;
12836                // Not a '/' separated full component name; maybe an object ID?
12837                try {
12838                    objectId = Integer.parseInt(name, 16);
12839                    if (objects == null) {
12840                        objects = new ArrayList<Integer>();
12841                    }
12842                    objects.add(objectId);
12843                    all = false;
12844                } catch (RuntimeException e) {
12845                    // Not an integer; just do string match.
12846                    if (strings == null) {
12847                        strings = new ArrayList<String>();
12848                    }
12849                    strings.add(name);
12850                    all = false;
12851                }
12852            }
12853        }
12854
12855        int build(String[] args, int opti) {
12856            for (; opti<args.length; opti++) {
12857                String name = args[opti];
12858                if ("--".equals(name)) {
12859                    return opti+1;
12860                }
12861                build(name);
12862            }
12863            return opti;
12864        }
12865
12866        boolean match(Object object, ComponentName comp) {
12867            if (all) {
12868                return true;
12869            }
12870            if (components != null) {
12871                for (int i=0; i<components.size(); i++) {
12872                    if (components.get(i).equals(comp)) {
12873                        return true;
12874                    }
12875                }
12876            }
12877            if (objects != null) {
12878                for (int i=0; i<objects.size(); i++) {
12879                    if (System.identityHashCode(object) == objects.get(i)) {
12880                        return true;
12881                    }
12882                }
12883            }
12884            if (strings != null) {
12885                String flat = comp.flattenToString();
12886                for (int i=0; i<strings.size(); i++) {
12887                    if (flat.contains(strings.get(i))) {
12888                        return true;
12889                    }
12890                }
12891            }
12892            return false;
12893        }
12894    }
12895
12896    /**
12897     * There are three things that cmd can be:
12898     *  - a flattened component name that matches an existing activity
12899     *  - the cmd arg isn't the flattened component name of an existing activity:
12900     *    dump all activity whose component contains the cmd as a substring
12901     *  - A hex number of the ActivityRecord object instance.
12902     */
12903    protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args,
12904            int opti, boolean dumpAll) {
12905        ArrayList<ActivityRecord> activities;
12906
12907        synchronized (this) {
12908            activities = mStackSupervisor.getDumpActivitiesLocked(name);
12909        }
12910
12911        if (activities.size() <= 0) {
12912            return false;
12913        }
12914
12915        String[] newArgs = new String[args.length - opti];
12916        System.arraycopy(args, opti, newArgs, 0, args.length - opti);
12917
12918        TaskRecord lastTask = null;
12919        boolean needSep = false;
12920        for (int i=activities.size()-1; i>=0; i--) {
12921            ActivityRecord r = activities.get(i);
12922            if (needSep) {
12923                pw.println();
12924            }
12925            needSep = true;
12926            synchronized (this) {
12927                if (lastTask != r.task) {
12928                    lastTask = r.task;
12929                    pw.print("TASK "); pw.print(lastTask.affinity);
12930                            pw.print(" id="); pw.println(lastTask.taskId);
12931                    if (dumpAll) {
12932                        lastTask.dump(pw, "  ");
12933                    }
12934                }
12935            }
12936            dumpActivity("  ", fd, pw, activities.get(i), newArgs, dumpAll);
12937        }
12938        return true;
12939    }
12940
12941    /**
12942     * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if
12943     * there is a thread associated with the activity.
12944     */
12945    private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw,
12946            final ActivityRecord r, String[] args, boolean dumpAll) {
12947        String innerPrefix = prefix + "  ";
12948        synchronized (this) {
12949            pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName);
12950                    pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r)));
12951                    pw.print(" pid=");
12952                    if (r.app != null) pw.println(r.app.pid);
12953                    else pw.println("(not running)");
12954            if (dumpAll) {
12955                r.dump(pw, innerPrefix);
12956            }
12957        }
12958        if (r.app != null && r.app.thread != null) {
12959            // flush anything that is already in the PrintWriter since the thread is going
12960            // to write to the file descriptor directly
12961            pw.flush();
12962            try {
12963                TransferPipe tp = new TransferPipe();
12964                try {
12965                    r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
12966                            r.appToken, innerPrefix, args);
12967                    tp.go(fd);
12968                } finally {
12969                    tp.kill();
12970                }
12971            } catch (IOException e) {
12972                pw.println(innerPrefix + "Failure while dumping the activity: " + e);
12973            } catch (RemoteException e) {
12974                pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
12975            }
12976        }
12977    }
12978
12979    void dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
12980            int opti, boolean dumpAll, String dumpPackage) {
12981        boolean needSep = false;
12982        boolean onlyHistory = false;
12983        boolean printedAnything = false;
12984
12985        if ("history".equals(dumpPackage)) {
12986            if (opti < args.length && "-s".equals(args[opti])) {
12987                dumpAll = false;
12988            }
12989            onlyHistory = true;
12990            dumpPackage = null;
12991        }
12992
12993        pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)");
12994        if (!onlyHistory && dumpAll) {
12995            if (mRegisteredReceivers.size() > 0) {
12996                boolean printed = false;
12997                Iterator it = mRegisteredReceivers.values().iterator();
12998                while (it.hasNext()) {
12999                    ReceiverList r = (ReceiverList)it.next();
13000                    if (dumpPackage != null && (r.app == null ||
13001                            !dumpPackage.equals(r.app.info.packageName))) {
13002                        continue;
13003                    }
13004                    if (!printed) {
13005                        pw.println("  Registered Receivers:");
13006                        needSep = true;
13007                        printed = true;
13008                        printedAnything = true;
13009                    }
13010                    pw.print("  * "); pw.println(r);
13011                    r.dump(pw, "    ");
13012                }
13013            }
13014
13015            if (mReceiverResolver.dump(pw, needSep ?
13016                    "\n  Receiver Resolver Table:" : "  Receiver Resolver Table:",
13017                    "    ", dumpPackage, false)) {
13018                needSep = true;
13019                printedAnything = true;
13020            }
13021        }
13022
13023        for (BroadcastQueue q : mBroadcastQueues) {
13024            needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep);
13025            printedAnything |= needSep;
13026        }
13027
13028        needSep = true;
13029
13030        if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) {
13031            for (int user=0; user<mStickyBroadcasts.size(); user++) {
13032                if (needSep) {
13033                    pw.println();
13034                }
13035                needSep = true;
13036                printedAnything = true;
13037                pw.print("  Sticky broadcasts for user ");
13038                        pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":");
13039                StringBuilder sb = new StringBuilder(128);
13040                for (Map.Entry<String, ArrayList<Intent>> ent
13041                        : mStickyBroadcasts.valueAt(user).entrySet()) {
13042                    pw.print("  * Sticky action "); pw.print(ent.getKey());
13043                    if (dumpAll) {
13044                        pw.println(":");
13045                        ArrayList<Intent> intents = ent.getValue();
13046                        final int N = intents.size();
13047                        for (int i=0; i<N; i++) {
13048                            sb.setLength(0);
13049                            sb.append("    Intent: ");
13050                            intents.get(i).toShortString(sb, false, true, false, false);
13051                            pw.println(sb.toString());
13052                            Bundle bundle = intents.get(i).getExtras();
13053                            if (bundle != null) {
13054                                pw.print("      ");
13055                                pw.println(bundle.toString());
13056                            }
13057                        }
13058                    } else {
13059                        pw.println("");
13060                    }
13061                }
13062            }
13063        }
13064
13065        if (!onlyHistory && dumpAll) {
13066            pw.println();
13067            for (BroadcastQueue queue : mBroadcastQueues) {
13068                pw.println("  mBroadcastsScheduled [" + queue.mQueueName + "]="
13069                        + queue.mBroadcastsScheduled);
13070            }
13071            pw.println("  mHandler:");
13072            mHandler.dump(new PrintWriterPrinter(pw), "    ");
13073            needSep = true;
13074            printedAnything = true;
13075        }
13076
13077        if (!printedAnything) {
13078            pw.println("  (nothing)");
13079        }
13080    }
13081
13082    void dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args,
13083            int opti, boolean dumpAll, String dumpPackage) {
13084        boolean needSep;
13085        boolean printedAnything = false;
13086
13087        ItemMatcher matcher = new ItemMatcher();
13088        matcher.build(args, opti);
13089
13090        pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)");
13091
13092        needSep = mProviderMap.dumpProvidersLocked(pw, dumpAll, dumpPackage);
13093        printedAnything |= needSep;
13094
13095        if (mLaunchingProviders.size() > 0) {
13096            boolean printed = false;
13097            for (int i=mLaunchingProviders.size()-1; i>=0; i--) {
13098                ContentProviderRecord r = mLaunchingProviders.get(i);
13099                if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) {
13100                    continue;
13101                }
13102                if (!printed) {
13103                    if (needSep) pw.println();
13104                    needSep = true;
13105                    pw.println("  Launching content providers:");
13106                    printed = true;
13107                    printedAnything = true;
13108                }
13109                pw.print("  Launching #"); pw.print(i); pw.print(": ");
13110                        pw.println(r);
13111            }
13112        }
13113
13114        if (mGrantedUriPermissions.size() > 0) {
13115            boolean printed = false;
13116            int dumpUid = -2;
13117            if (dumpPackage != null) {
13118                try {
13119                    dumpUid = mContext.getPackageManager().getPackageUid(dumpPackage, 0);
13120                } catch (NameNotFoundException e) {
13121                    dumpUid = -1;
13122                }
13123            }
13124            for (int i=0; i<mGrantedUriPermissions.size(); i++) {
13125                int uid = mGrantedUriPermissions.keyAt(i);
13126                if (dumpUid >= -1 && UserHandle.getAppId(uid) != dumpUid) {
13127                    continue;
13128                }
13129                final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
13130                if (!printed) {
13131                    if (needSep) pw.println();
13132                    needSep = true;
13133                    pw.println("  Granted Uri Permissions:");
13134                    printed = true;
13135                    printedAnything = true;
13136                }
13137                pw.print("  * UID "); pw.print(uid); pw.println(" holds:");
13138                for (UriPermission perm : perms.values()) {
13139                    pw.print("    "); pw.println(perm);
13140                    if (dumpAll) {
13141                        perm.dump(pw, "      ");
13142                    }
13143                }
13144            }
13145        }
13146
13147        if (!printedAnything) {
13148            pw.println("  (nothing)");
13149        }
13150    }
13151
13152    void dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
13153            int opti, boolean dumpAll, String dumpPackage) {
13154        boolean printed = false;
13155
13156        pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)");
13157
13158        if (mIntentSenderRecords.size() > 0) {
13159            Iterator<WeakReference<PendingIntentRecord>> it
13160                    = mIntentSenderRecords.values().iterator();
13161            while (it.hasNext()) {
13162                WeakReference<PendingIntentRecord> ref = it.next();
13163                PendingIntentRecord rec = ref != null ? ref.get(): null;
13164                if (dumpPackage != null && (rec == null
13165                        || !dumpPackage.equals(rec.key.packageName))) {
13166                    continue;
13167                }
13168                printed = true;
13169                if (rec != null) {
13170                    pw.print("  * "); pw.println(rec);
13171                    if (dumpAll) {
13172                        rec.dump(pw, "    ");
13173                    }
13174                } else {
13175                    pw.print("  * "); pw.println(ref);
13176                }
13177            }
13178        }
13179
13180        if (!printed) {
13181            pw.println("  (nothing)");
13182        }
13183    }
13184
13185    private static final int dumpProcessList(PrintWriter pw,
13186            ActivityManagerService service, List list,
13187            String prefix, String normalLabel, String persistentLabel,
13188            String dumpPackage) {
13189        int numPers = 0;
13190        final int N = list.size()-1;
13191        for (int i=N; i>=0; i--) {
13192            ProcessRecord r = (ProcessRecord)list.get(i);
13193            if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
13194                continue;
13195            }
13196            pw.println(String.format("%s%s #%2d: %s",
13197                    prefix, (r.persistent ? persistentLabel : normalLabel),
13198                    i, r.toString()));
13199            if (r.persistent) {
13200                numPers++;
13201            }
13202        }
13203        return numPers;
13204    }
13205
13206    private static final boolean dumpProcessOomList(PrintWriter pw,
13207            ActivityManagerService service, List<ProcessRecord> origList,
13208            String prefix, String normalLabel, String persistentLabel,
13209            boolean inclDetails, String dumpPackage) {
13210
13211        ArrayList<Pair<ProcessRecord, Integer>> list
13212                = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size());
13213        for (int i=0; i<origList.size(); i++) {
13214            ProcessRecord r = origList.get(i);
13215            if (dumpPackage != null && !r.pkgList.containsKey(dumpPackage)) {
13216                continue;
13217            }
13218            list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i));
13219        }
13220
13221        if (list.size() <= 0) {
13222            return false;
13223        }
13224
13225        Comparator<Pair<ProcessRecord, Integer>> comparator
13226                = new Comparator<Pair<ProcessRecord, Integer>>() {
13227            @Override
13228            public int compare(Pair<ProcessRecord, Integer> object1,
13229                    Pair<ProcessRecord, Integer> object2) {
13230                if (object1.first.setAdj != object2.first.setAdj) {
13231                    return object1.first.setAdj > object2.first.setAdj ? -1 : 1;
13232                }
13233                if (object1.second.intValue() != object2.second.intValue()) {
13234                    return object1.second.intValue() > object2.second.intValue() ? -1 : 1;
13235                }
13236                return 0;
13237            }
13238        };
13239
13240        Collections.sort(list, comparator);
13241
13242        final long curRealtime = SystemClock.elapsedRealtime();
13243        final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
13244        final long curUptime = SystemClock.uptimeMillis();
13245        final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
13246
13247        for (int i=list.size()-1; i>=0; i--) {
13248            ProcessRecord r = list.get(i).first;
13249            String oomAdj = ProcessList.makeOomAdjString(r.setAdj);
13250            char schedGroup;
13251            switch (r.setSchedGroup) {
13252                case Process.THREAD_GROUP_BG_NONINTERACTIVE:
13253                    schedGroup = 'B';
13254                    break;
13255                case Process.THREAD_GROUP_DEFAULT:
13256                    schedGroup = 'F';
13257                    break;
13258                default:
13259                    schedGroup = '?';
13260                    break;
13261            }
13262            char foreground;
13263            if (r.foregroundActivities) {
13264                foreground = 'A';
13265            } else if (r.foregroundServices) {
13266                foreground = 'S';
13267            } else {
13268                foreground = ' ';
13269            }
13270            String procState = ProcessList.makeProcStateString(r.curProcState);
13271            pw.print(prefix);
13272            pw.print(r.persistent ? persistentLabel : normalLabel);
13273            pw.print(" #");
13274            int num = (origList.size()-1)-list.get(i).second;
13275            if (num < 10) pw.print(' ');
13276            pw.print(num);
13277            pw.print(": ");
13278            pw.print(oomAdj);
13279            pw.print(' ');
13280            pw.print(schedGroup);
13281            pw.print('/');
13282            pw.print(foreground);
13283            pw.print('/');
13284            pw.print(procState);
13285            pw.print(" trm:");
13286            if (r.trimMemoryLevel < 10) pw.print(' ');
13287            pw.print(r.trimMemoryLevel);
13288            pw.print(' ');
13289            pw.print(r.toShortString());
13290            pw.print(" (");
13291            pw.print(r.adjType);
13292            pw.println(')');
13293            if (r.adjSource != null || r.adjTarget != null) {
13294                pw.print(prefix);
13295                pw.print("    ");
13296                if (r.adjTarget instanceof ComponentName) {
13297                    pw.print(((ComponentName)r.adjTarget).flattenToShortString());
13298                } else if (r.adjTarget != null) {
13299                    pw.print(r.adjTarget.toString());
13300                } else {
13301                    pw.print("{null}");
13302                }
13303                pw.print("<=");
13304                if (r.adjSource instanceof ProcessRecord) {
13305                    pw.print("Proc{");
13306                    pw.print(((ProcessRecord)r.adjSource).toShortString());
13307                    pw.println("}");
13308                } else if (r.adjSource != null) {
13309                    pw.println(r.adjSource.toString());
13310                } else {
13311                    pw.println("{null}");
13312                }
13313            }
13314            if (inclDetails) {
13315                pw.print(prefix);
13316                pw.print("    ");
13317                pw.print("oom: max="); pw.print(r.maxAdj);
13318                pw.print(" curRaw="); pw.print(r.curRawAdj);
13319                pw.print(" setRaw="); pw.print(r.setRawAdj);
13320                pw.print(" cur="); pw.print(r.curAdj);
13321                pw.print(" set="); pw.println(r.setAdj);
13322                pw.print(prefix);
13323                pw.print("    ");
13324                pw.print("state: cur="); pw.print(ProcessList.makeProcStateString(r.curProcState));
13325                pw.print(" set="); pw.print(ProcessList.makeProcStateString(r.setProcState));
13326                pw.print(" lastPss="); pw.print(r.lastPss);
13327                pw.print(" lastCachedPss="); pw.println(r.lastCachedPss);
13328                pw.print(prefix);
13329                pw.print("    ");
13330                pw.print("cached="); pw.print(r.cached);
13331                pw.print(" empty="); pw.print(r.empty);
13332                pw.print(" hasAboveClient="); pw.println(r.hasAboveClient);
13333
13334                if (r.setProcState >= ActivityManager.PROCESS_STATE_SERVICE) {
13335                    if (r.lastWakeTime != 0) {
13336                        long wtime;
13337                        BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics();
13338                        synchronized (stats) {
13339                            wtime = stats.getProcessWakeTime(r.info.uid,
13340                                    r.pid, curRealtime);
13341                        }
13342                        long timeUsed = wtime - r.lastWakeTime;
13343                        pw.print(prefix);
13344                        pw.print("    ");
13345                        pw.print("keep awake over ");
13346                        TimeUtils.formatDuration(realtimeSince, pw);
13347                        pw.print(" used ");
13348                        TimeUtils.formatDuration(timeUsed, pw);
13349                        pw.print(" (");
13350                        pw.print((timeUsed*100)/realtimeSince);
13351                        pw.println("%)");
13352                    }
13353                    if (r.lastCpuTime != 0) {
13354                        long timeUsed = r.curCpuTime - r.lastCpuTime;
13355                        pw.print(prefix);
13356                        pw.print("    ");
13357                        pw.print("run cpu over ");
13358                        TimeUtils.formatDuration(uptimeSince, pw);
13359                        pw.print(" used ");
13360                        TimeUtils.formatDuration(timeUsed, pw);
13361                        pw.print(" (");
13362                        pw.print((timeUsed*100)/uptimeSince);
13363                        pw.println("%)");
13364                    }
13365                }
13366            }
13367        }
13368        return true;
13369    }
13370
13371    ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) {
13372        ArrayList<ProcessRecord> procs;
13373        synchronized (this) {
13374            if (args != null && args.length > start
13375                    && args[start].charAt(0) != '-') {
13376                procs = new ArrayList<ProcessRecord>();
13377                int pid = -1;
13378                try {
13379                    pid = Integer.parseInt(args[start]);
13380                } catch (NumberFormatException e) {
13381                }
13382                for (int i=mLruProcesses.size()-1; i>=0; i--) {
13383                    ProcessRecord proc = mLruProcesses.get(i);
13384                    if (proc.pid == pid) {
13385                        procs.add(proc);
13386                    } else if (proc.processName.equals(args[start])) {
13387                        procs.add(proc);
13388                    }
13389                }
13390                if (procs.size() <= 0) {
13391                    return null;
13392                }
13393            } else {
13394                procs = new ArrayList<ProcessRecord>(mLruProcesses);
13395            }
13396        }
13397        return procs;
13398    }
13399
13400    final void dumpGraphicsHardwareUsage(FileDescriptor fd,
13401            PrintWriter pw, String[] args) {
13402        ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
13403        if (procs == null) {
13404            pw.println("No process found for: " + args[0]);
13405            return;
13406        }
13407
13408        long uptime = SystemClock.uptimeMillis();
13409        long realtime = SystemClock.elapsedRealtime();
13410        pw.println("Applications Graphics Acceleration Info:");
13411        pw.println("Uptime: " + uptime + " Realtime: " + realtime);
13412
13413        for (int i = procs.size() - 1 ; i >= 0 ; i--) {
13414            ProcessRecord r = procs.get(i);
13415            if (r.thread != null) {
13416                pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **");
13417                pw.flush();
13418                try {
13419                    TransferPipe tp = new TransferPipe();
13420                    try {
13421                        r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args);
13422                        tp.go(fd);
13423                    } finally {
13424                        tp.kill();
13425                    }
13426                } catch (IOException e) {
13427                    pw.println("Failure while dumping the app: " + r);
13428                    pw.flush();
13429                } catch (RemoteException e) {
13430                    pw.println("Got a RemoteException while dumping the app " + r);
13431                    pw.flush();
13432                }
13433            }
13434        }
13435    }
13436
13437    final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) {
13438        ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
13439        if (procs == null) {
13440            pw.println("No process found for: " + args[0]);
13441            return;
13442        }
13443
13444        pw.println("Applications Database Info:");
13445
13446        for (int i = procs.size() - 1 ; i >= 0 ; i--) {
13447            ProcessRecord r = procs.get(i);
13448            if (r.thread != null) {
13449                pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **");
13450                pw.flush();
13451                try {
13452                    TransferPipe tp = new TransferPipe();
13453                    try {
13454                        r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args);
13455                        tp.go(fd);
13456                    } finally {
13457                        tp.kill();
13458                    }
13459                } catch (IOException e) {
13460                    pw.println("Failure while dumping the app: " + r);
13461                    pw.flush();
13462                } catch (RemoteException e) {
13463                    pw.println("Got a RemoteException while dumping the app " + r);
13464                    pw.flush();
13465                }
13466            }
13467        }
13468    }
13469
13470    final static class MemItem {
13471        final boolean isProc;
13472        final String label;
13473        final String shortLabel;
13474        final long pss;
13475        final int id;
13476        final boolean hasActivities;
13477        ArrayList<MemItem> subitems;
13478
13479        public MemItem(String _label, String _shortLabel, long _pss, int _id,
13480                boolean _hasActivities) {
13481            isProc = true;
13482            label = _label;
13483            shortLabel = _shortLabel;
13484            pss = _pss;
13485            id = _id;
13486            hasActivities = _hasActivities;
13487        }
13488
13489        public MemItem(String _label, String _shortLabel, long _pss, int _id) {
13490            isProc = false;
13491            label = _label;
13492            shortLabel = _shortLabel;
13493            pss = _pss;
13494            id = _id;
13495            hasActivities = false;
13496        }
13497    }
13498
13499    static final void dumpMemItems(PrintWriter pw, String prefix, String tag,
13500            ArrayList<MemItem> items, boolean sort, boolean isCompact) {
13501        if (sort && !isCompact) {
13502            Collections.sort(items, new Comparator<MemItem>() {
13503                @Override
13504                public int compare(MemItem lhs, MemItem rhs) {
13505                    if (lhs.pss < rhs.pss) {
13506                        return 1;
13507                    } else if (lhs.pss > rhs.pss) {
13508                        return -1;
13509                    }
13510                    return 0;
13511                }
13512            });
13513        }
13514
13515        for (int i=0; i<items.size(); i++) {
13516            MemItem mi = items.get(i);
13517            if (!isCompact) {
13518                pw.print(prefix); pw.printf("%7d kB: ", mi.pss); pw.println(mi.label);
13519            } else if (mi.isProc) {
13520                pw.print("proc,"); pw.print(tag); pw.print(","); pw.print(mi.shortLabel);
13521                pw.print(","); pw.print(mi.id); pw.print(","); pw.print(mi.pss);
13522                pw.println(mi.hasActivities ? ",a" : ",e");
13523            } else {
13524                pw.print(tag); pw.print(","); pw.print(mi.shortLabel); pw.print(",");
13525                pw.println(mi.pss);
13526            }
13527            if (mi.subitems != null) {
13528                dumpMemItems(pw, prefix + "           ", mi.shortLabel, mi.subitems,
13529                        true, isCompact);
13530            }
13531        }
13532    }
13533
13534    // These are in KB.
13535    static final long[] DUMP_MEM_BUCKETS = new long[] {
13536        5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024,
13537        120*1024, 160*1024, 200*1024,
13538        250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024,
13539        1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024
13540    };
13541
13542    static final void appendMemBucket(StringBuilder out, long memKB, String label,
13543            boolean stackLike) {
13544        int start = label.lastIndexOf('.');
13545        if (start >= 0) start++;
13546        else start = 0;
13547        int end = label.length();
13548        for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) {
13549            if (DUMP_MEM_BUCKETS[i] >= memKB) {
13550                long bucket = DUMP_MEM_BUCKETS[i]/1024;
13551                out.append(bucket);
13552                out.append(stackLike ? "MB." : "MB ");
13553                out.append(label, start, end);
13554                return;
13555            }
13556        }
13557        out.append(memKB/1024);
13558        out.append(stackLike ? "MB." : "MB ");
13559        out.append(label, start, end);
13560    }
13561
13562    static final int[] DUMP_MEM_OOM_ADJ = new int[] {
13563            ProcessList.NATIVE_ADJ,
13564            ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ,
13565            ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ,
13566            ProcessList.BACKUP_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ,
13567            ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ,
13568            ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.CACHED_APP_MAX_ADJ
13569    };
13570    static final String[] DUMP_MEM_OOM_LABEL = new String[] {
13571            "Native",
13572            "System", "Persistent", "Foreground",
13573            "Visible", "Perceptible",
13574            "Heavy Weight", "Backup",
13575            "A Services", "Home",
13576            "Previous", "B Services", "Cached"
13577    };
13578    static final String[] DUMP_MEM_OOM_COMPACT_LABEL = new String[] {
13579            "native",
13580            "sys", "pers", "fore",
13581            "vis", "percept",
13582            "heavy", "backup",
13583            "servicea", "home",
13584            "prev", "serviceb", "cached"
13585    };
13586
13587    private final void dumpApplicationMemoryUsageHeader(PrintWriter pw, long uptime,
13588            long realtime, boolean isCheckinRequest, boolean isCompact) {
13589        if (isCheckinRequest || isCompact) {
13590            // short checkin version
13591            pw.print("time,"); pw.print(uptime); pw.print(","); pw.println(realtime);
13592        } else {
13593            pw.println("Applications Memory Usage (kB):");
13594            pw.println("Uptime: " + uptime + " Realtime: " + realtime);
13595        }
13596    }
13597
13598    final void dumpApplicationMemoryUsage(FileDescriptor fd,
13599            PrintWriter pw, String prefix, String[] args, boolean brief, PrintWriter categoryPw) {
13600        boolean dumpDetails = false;
13601        boolean dumpFullDetails = false;
13602        boolean dumpDalvik = false;
13603        boolean oomOnly = false;
13604        boolean isCompact = false;
13605        boolean localOnly = false;
13606
13607        int opti = 0;
13608        while (opti < args.length) {
13609            String opt = args[opti];
13610            if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
13611                break;
13612            }
13613            opti++;
13614            if ("-a".equals(opt)) {
13615                dumpDetails = true;
13616                dumpFullDetails = true;
13617                dumpDalvik = true;
13618            } else if ("-d".equals(opt)) {
13619                dumpDalvik = true;
13620            } else if ("-c".equals(opt)) {
13621                isCompact = true;
13622            } else if ("--oom".equals(opt)) {
13623                oomOnly = true;
13624            } else if ("--local".equals(opt)) {
13625                localOnly = true;
13626            } else if ("-h".equals(opt)) {
13627                pw.println("meminfo dump options: [-a] [-d] [-c] [--oom] [process]");
13628                pw.println("  -a: include all available information for each process.");
13629                pw.println("  -d: include dalvik details when dumping process details.");
13630                pw.println("  -c: dump in a compact machine-parseable representation.");
13631                pw.println("  --oom: only show processes organized by oom adj.");
13632                pw.println("  --local: only collect details locally, don't call process.");
13633                pw.println("If [process] is specified it can be the name or ");
13634                pw.println("pid of a specific process to dump.");
13635                return;
13636            } else {
13637                pw.println("Unknown argument: " + opt + "; use -h for help");
13638            }
13639        }
13640
13641        final boolean isCheckinRequest = scanArgs(args, "--checkin");
13642        long uptime = SystemClock.uptimeMillis();
13643        long realtime = SystemClock.elapsedRealtime();
13644        final long[] tmpLong = new long[1];
13645
13646        ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args);
13647        if (procs == null) {
13648            // No Java processes.  Maybe they want to print a native process.
13649            if (args != null && args.length > opti
13650                    && args[opti].charAt(0) != '-') {
13651                ArrayList<ProcessCpuTracker.Stats> nativeProcs
13652                        = new ArrayList<ProcessCpuTracker.Stats>();
13653                updateCpuStatsNow();
13654                int findPid = -1;
13655                try {
13656                    findPid = Integer.parseInt(args[opti]);
13657                } catch (NumberFormatException e) {
13658                }
13659                synchronized (mProcessCpuThread) {
13660                    final int N = mProcessCpuTracker.countStats();
13661                    for (int i=0; i<N; i++) {
13662                        ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
13663                        if (st.pid == findPid || (st.baseName != null
13664                                && st.baseName.equals(args[opti]))) {
13665                            nativeProcs.add(st);
13666                        }
13667                    }
13668                }
13669                if (nativeProcs.size() > 0) {
13670                    dumpApplicationMemoryUsageHeader(pw, uptime, realtime, isCheckinRequest,
13671                            isCompact);
13672                    Debug.MemoryInfo mi = null;
13673                    for (int i = nativeProcs.size() - 1 ; i >= 0 ; i--) {
13674                        final ProcessCpuTracker.Stats r = nativeProcs.get(i);
13675                        final int pid = r.pid;
13676                        if (!isCheckinRequest && dumpDetails) {
13677                            pw.println("\n** MEMINFO in pid " + pid + " [" + r.baseName + "] **");
13678                        }
13679                        if (mi == null) {
13680                            mi = new Debug.MemoryInfo();
13681                        }
13682                        if (dumpDetails || (!brief && !oomOnly)) {
13683                            Debug.getMemoryInfo(pid, mi);
13684                        } else {
13685                            mi.dalvikPss = (int)Debug.getPss(pid, tmpLong);
13686                            mi.dalvikPrivateDirty = (int)tmpLong[0];
13687                        }
13688                        ActivityThread.dumpMemInfoTable(pw, mi, isCheckinRequest, dumpFullDetails,
13689                                dumpDalvik, pid, r.baseName, 0, 0, 0, 0, 0, 0);
13690                        if (isCheckinRequest) {
13691                            pw.println();
13692                        }
13693                    }
13694                    return;
13695                }
13696            }
13697            pw.println("No process found for: " + args[opti]);
13698            return;
13699        }
13700
13701        if (!brief && !oomOnly && (procs.size() == 1 || isCheckinRequest)) {
13702            dumpDetails = true;
13703        }
13704
13705        dumpApplicationMemoryUsageHeader(pw, uptime, realtime, isCheckinRequest, isCompact);
13706
13707        String[] innerArgs = new String[args.length-opti];
13708        System.arraycopy(args, opti, innerArgs, 0, args.length-opti);
13709
13710        ArrayList<MemItem> procMems = new ArrayList<MemItem>();
13711        final SparseArray<MemItem> procMemsMap = new SparseArray<MemItem>();
13712        long nativePss=0, dalvikPss=0, otherPss=0;
13713        long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS];
13714
13715        long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length];
13716        ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[])
13717                new ArrayList[DUMP_MEM_OOM_LABEL.length];
13718
13719        long totalPss = 0;
13720        long cachedPss = 0;
13721
13722        Debug.MemoryInfo mi = null;
13723        for (int i = procs.size() - 1 ; i >= 0 ; i--) {
13724            final ProcessRecord r = procs.get(i);
13725            final IApplicationThread thread;
13726            final int pid;
13727            final int oomAdj;
13728            final boolean hasActivities;
13729            synchronized (this) {
13730                thread = r.thread;
13731                pid = r.pid;
13732                oomAdj = r.getSetAdjWithServices();
13733                hasActivities = r.activities.size() > 0;
13734            }
13735            if (thread != null) {
13736                if (!isCheckinRequest && dumpDetails) {
13737                    pw.println("\n** MEMINFO in pid " + pid + " [" + r.processName + "] **");
13738                }
13739                if (mi == null) {
13740                    mi = new Debug.MemoryInfo();
13741                }
13742                if (dumpDetails || (!brief && !oomOnly)) {
13743                    Debug.getMemoryInfo(pid, mi);
13744                } else {
13745                    mi.dalvikPss = (int)Debug.getPss(pid, tmpLong);
13746                    mi.dalvikPrivateDirty = (int)tmpLong[0];
13747                }
13748                if (dumpDetails) {
13749                    if (localOnly) {
13750                        ActivityThread.dumpMemInfoTable(pw, mi, isCheckinRequest, dumpFullDetails,
13751                                dumpDalvik, pid, r.processName, 0, 0, 0, 0, 0, 0);
13752                        if (isCheckinRequest) {
13753                            pw.println();
13754                        }
13755                    } else {
13756                        try {
13757                            pw.flush();
13758                            thread.dumpMemInfo(fd, mi, isCheckinRequest, dumpFullDetails,
13759                                    dumpDalvik, innerArgs);
13760                        } catch (RemoteException e) {
13761                            if (!isCheckinRequest) {
13762                                pw.println("Got RemoteException!");
13763                                pw.flush();
13764                            }
13765                        }
13766                    }
13767                }
13768
13769                final long myTotalPss = mi.getTotalPss();
13770                final long myTotalUss = mi.getTotalUss();
13771
13772                synchronized (this) {
13773                    if (r.thread != null && oomAdj == r.getSetAdjWithServices()) {
13774                        // Record this for posterity if the process has been stable.
13775                        r.baseProcessTracker.addPss(myTotalPss, myTotalUss, true, r.pkgList);
13776                    }
13777                }
13778
13779                if (!isCheckinRequest && mi != null) {
13780                    totalPss += myTotalPss;
13781                    MemItem pssItem = new MemItem(r.processName + " (pid " + pid +
13782                            (hasActivities ? " / activities)" : ")"),
13783                            r.processName, myTotalPss, pid, hasActivities);
13784                    procMems.add(pssItem);
13785                    procMemsMap.put(pid, pssItem);
13786
13787                    nativePss += mi.nativePss;
13788                    dalvikPss += mi.dalvikPss;
13789                    otherPss += mi.otherPss;
13790                    for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
13791                        long mem = mi.getOtherPss(j);
13792                        miscPss[j] += mem;
13793                        otherPss -= mem;
13794                    }
13795
13796                    if (oomAdj >= ProcessList.CACHED_APP_MIN_ADJ) {
13797                        cachedPss += myTotalPss;
13798                    }
13799
13800                    for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) {
13801                        if (oomAdj <= DUMP_MEM_OOM_ADJ[oomIndex]
13802                                || oomIndex == (oomPss.length-1)) {
13803                            oomPss[oomIndex] += myTotalPss;
13804                            if (oomProcs[oomIndex] == null) {
13805                                oomProcs[oomIndex] = new ArrayList<MemItem>();
13806                            }
13807                            oomProcs[oomIndex].add(pssItem);
13808                            break;
13809                        }
13810                    }
13811                }
13812            }
13813        }
13814
13815        long nativeProcTotalPss = 0;
13816
13817        if (!isCheckinRequest && procs.size() > 1) {
13818            // If we are showing aggregations, also look for native processes to
13819            // include so that our aggregations are more accurate.
13820            updateCpuStatsNow();
13821            synchronized (mProcessCpuThread) {
13822                final int N = mProcessCpuTracker.countStats();
13823                for (int i=0; i<N; i++) {
13824                    ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
13825                    if (st.vsize > 0 && procMemsMap.indexOfKey(st.pid) < 0) {
13826                        if (mi == null) {
13827                            mi = new Debug.MemoryInfo();
13828                        }
13829                        if (!brief && !oomOnly) {
13830                            Debug.getMemoryInfo(st.pid, mi);
13831                        } else {
13832                            mi.nativePss = (int)Debug.getPss(st.pid, tmpLong);
13833                            mi.nativePrivateDirty = (int)tmpLong[0];
13834                        }
13835
13836                        final long myTotalPss = mi.getTotalPss();
13837                        totalPss += myTotalPss;
13838                        nativeProcTotalPss += myTotalPss;
13839
13840                        MemItem pssItem = new MemItem(st.name + " (pid " + st.pid + ")",
13841                                st.name, myTotalPss, st.pid, false);
13842                        procMems.add(pssItem);
13843
13844                        nativePss += mi.nativePss;
13845                        dalvikPss += mi.dalvikPss;
13846                        otherPss += mi.otherPss;
13847                        for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
13848                            long mem = mi.getOtherPss(j);
13849                            miscPss[j] += mem;
13850                            otherPss -= mem;
13851                        }
13852                        oomPss[0] += myTotalPss;
13853                        if (oomProcs[0] == null) {
13854                            oomProcs[0] = new ArrayList<MemItem>();
13855                        }
13856                        oomProcs[0].add(pssItem);
13857                    }
13858                }
13859            }
13860
13861            ArrayList<MemItem> catMems = new ArrayList<MemItem>();
13862
13863            catMems.add(new MemItem("Native", "Native", nativePss, -1));
13864            catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2));
13865            catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3));
13866            for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
13867                String label = Debug.MemoryInfo.getOtherLabel(j);
13868                catMems.add(new MemItem(label, label, miscPss[j], j));
13869            }
13870
13871            ArrayList<MemItem> oomMems = new ArrayList<MemItem>();
13872            for (int j=0; j<oomPss.length; j++) {
13873                if (oomPss[j] != 0) {
13874                    String label = isCompact ? DUMP_MEM_OOM_COMPACT_LABEL[j]
13875                            : DUMP_MEM_OOM_LABEL[j];
13876                    MemItem item = new MemItem(label, label, oomPss[j],
13877                            DUMP_MEM_OOM_ADJ[j]);
13878                    item.subitems = oomProcs[j];
13879                    oomMems.add(item);
13880                }
13881            }
13882
13883            if (!brief && !oomOnly && !isCompact) {
13884                pw.println();
13885                pw.println("Total PSS by process:");
13886                dumpMemItems(pw, "  ", "proc", procMems, true, isCompact);
13887                pw.println();
13888            }
13889            if (!isCompact) {
13890                pw.println("Total PSS by OOM adjustment:");
13891            }
13892            dumpMemItems(pw, "  ", "oom", oomMems, false, isCompact);
13893            if (!brief && !oomOnly) {
13894                PrintWriter out = categoryPw != null ? categoryPw : pw;
13895                if (!isCompact) {
13896                    out.println();
13897                    out.println("Total PSS by category:");
13898                }
13899                dumpMemItems(out, "  ", "cat", catMems, true, isCompact);
13900            }
13901            if (!isCompact) {
13902                pw.println();
13903            }
13904            MemInfoReader memInfo = new MemInfoReader();
13905            memInfo.readMemInfo();
13906            if (nativeProcTotalPss > 0) {
13907                synchronized (this) {
13908                    mProcessStats.addSysMemUsageLocked(memInfo.getCachedSizeKb(),
13909                            memInfo.getFreeSizeKb(), memInfo.getZramTotalSizeKb(),
13910                            memInfo.getBuffersSizeKb()+memInfo.getShmemSizeKb()+memInfo.getSlabSizeKb(),
13911                            nativeProcTotalPss);
13912                }
13913            }
13914            if (!brief) {
13915                if (!isCompact) {
13916                    pw.print("Total RAM: "); pw.print(memInfo.getTotalSizeKb());
13917                    pw.print(" kB (status ");
13918                    switch (mLastMemoryLevel) {
13919                        case ProcessStats.ADJ_MEM_FACTOR_NORMAL:
13920                            pw.println("normal)");
13921                            break;
13922                        case ProcessStats.ADJ_MEM_FACTOR_MODERATE:
13923                            pw.println("moderate)");
13924                            break;
13925                        case ProcessStats.ADJ_MEM_FACTOR_LOW:
13926                            pw.println("low)");
13927                            break;
13928                        case ProcessStats.ADJ_MEM_FACTOR_CRITICAL:
13929                            pw.println("critical)");
13930                            break;
13931                        default:
13932                            pw.print(mLastMemoryLevel);
13933                            pw.println(")");
13934                            break;
13935                    }
13936                    pw.print(" Free RAM: "); pw.print(cachedPss + memInfo.getCachedSizeKb()
13937                            + memInfo.getFreeSizeKb()); pw.print(" kB (");
13938                            pw.print(cachedPss); pw.print(" cached pss + ");
13939                            pw.print(memInfo.getCachedSizeKb()); pw.print(" cached + ");
13940                            pw.print(memInfo.getFreeSizeKb()); pw.println(" free)");
13941                } else {
13942                    pw.print("ram,"); pw.print(memInfo.getTotalSizeKb()); pw.print(",");
13943                    pw.print(cachedPss + memInfo.getCachedSizeKb()
13944                            + memInfo.getFreeSizeKb()); pw.print(",");
13945                    pw.println(totalPss - cachedPss);
13946                }
13947            }
13948            if (!isCompact) {
13949                pw.print(" Used RAM: "); pw.print(totalPss - cachedPss
13950                        + memInfo.getBuffersSizeKb() + memInfo.getShmemSizeKb()
13951                        + memInfo.getSlabSizeKb()); pw.print(" kB (");
13952                        pw.print(totalPss - cachedPss); pw.print(" used pss + ");
13953                        pw.print(memInfo.getBuffersSizeKb()); pw.print(" buffers + ");
13954                        pw.print(memInfo.getShmemSizeKb()); pw.print(" shmem + ");
13955                        pw.print(memInfo.getSlabSizeKb()); pw.println(" slab)");
13956                pw.print(" Lost RAM: "); pw.print(memInfo.getTotalSizeKb()
13957                        - totalPss - memInfo.getFreeSizeKb() - memInfo.getCachedSizeKb()
13958                        - memInfo.getBuffersSizeKb() - memInfo.getShmemSizeKb()
13959                        - memInfo.getSlabSizeKb()); pw.println(" kB");
13960            }
13961            if (!brief) {
13962                if (memInfo.getZramTotalSizeKb() != 0) {
13963                    if (!isCompact) {
13964                        pw.print("     ZRAM: "); pw.print(memInfo.getZramTotalSizeKb());
13965                                pw.print(" kB physical used for ");
13966                                pw.print(memInfo.getSwapTotalSizeKb()
13967                                        - memInfo.getSwapFreeSizeKb());
13968                                pw.print(" kB in swap (");
13969                                pw.print(memInfo.getSwapTotalSizeKb());
13970                                pw.println(" kB total swap)");
13971                    } else {
13972                        pw.print("zram,"); pw.print(memInfo.getZramTotalSizeKb()); pw.print(",");
13973                                pw.print(memInfo.getSwapTotalSizeKb()); pw.print(",");
13974                                pw.println(memInfo.getSwapFreeSizeKb());
13975                    }
13976                }
13977                final int[] SINGLE_LONG_FORMAT = new int[] {
13978                    Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG
13979                };
13980                long[] longOut = new long[1];
13981                Process.readProcFile("/sys/kernel/mm/ksm/pages_shared",
13982                        SINGLE_LONG_FORMAT, null, longOut, null);
13983                long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
13984                longOut[0] = 0;
13985                Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing",
13986                        SINGLE_LONG_FORMAT, null, longOut, null);
13987                long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024;
13988                longOut[0] = 0;
13989                Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared",
13990                        SINGLE_LONG_FORMAT, null, longOut, null);
13991                long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
13992                longOut[0] = 0;
13993                Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile",
13994                        SINGLE_LONG_FORMAT, null, longOut, null);
13995                long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024;
13996                if (!isCompact) {
13997                    if (sharing != 0 || shared != 0 || unshared != 0 || voltile != 0) {
13998                        pw.print("      KSM: "); pw.print(sharing);
13999                                pw.print(" kB saved from shared ");
14000                                pw.print(shared); pw.println(" kB");
14001                        pw.print("           "); pw.print(unshared); pw.print(" kB unshared; ");
14002                                pw.print(voltile); pw.println(" kB volatile");
14003                    }
14004                    pw.print("   Tuning: ");
14005                    pw.print(ActivityManager.staticGetMemoryClass());
14006                    pw.print(" (large ");
14007                    pw.print(ActivityManager.staticGetLargeMemoryClass());
14008                    pw.print("), oom ");
14009                    pw.print(mProcessList.getMemLevel(ProcessList.CACHED_APP_MAX_ADJ)/1024);
14010                    pw.print(" kB");
14011                    pw.print(", restore limit ");
14012                    pw.print(mProcessList.getCachedRestoreThresholdKb());
14013                    pw.print(" kB");
14014                    if (ActivityManager.isLowRamDeviceStatic()) {
14015                        pw.print(" (low-ram)");
14016                    }
14017                    if (ActivityManager.isHighEndGfx()) {
14018                        pw.print(" (high-end-gfx)");
14019                    }
14020                    pw.println();
14021                } else {
14022                    pw.print("ksm,"); pw.print(sharing); pw.print(",");
14023                    pw.print(shared); pw.print(","); pw.print(unshared); pw.print(",");
14024                    pw.println(voltile);
14025                    pw.print("tuning,");
14026                    pw.print(ActivityManager.staticGetMemoryClass());
14027                    pw.print(',');
14028                    pw.print(ActivityManager.staticGetLargeMemoryClass());
14029                    pw.print(',');
14030                    pw.print(mProcessList.getMemLevel(ProcessList.CACHED_APP_MAX_ADJ)/1024);
14031                    if (ActivityManager.isLowRamDeviceStatic()) {
14032                        pw.print(",low-ram");
14033                    }
14034                    if (ActivityManager.isHighEndGfx()) {
14035                        pw.print(",high-end-gfx");
14036                    }
14037                    pw.println();
14038                }
14039            }
14040        }
14041    }
14042
14043    /**
14044     * Searches array of arguments for the specified string
14045     * @param args array of argument strings
14046     * @param value value to search for
14047     * @return true if the value is contained in the array
14048     */
14049    private static boolean scanArgs(String[] args, String value) {
14050        if (args != null) {
14051            for (String arg : args) {
14052                if (value.equals(arg)) {
14053                    return true;
14054                }
14055            }
14056        }
14057        return false;
14058    }
14059
14060    private final boolean removeDyingProviderLocked(ProcessRecord proc,
14061            ContentProviderRecord cpr, boolean always) {
14062        final boolean inLaunching = mLaunchingProviders.contains(cpr);
14063
14064        if (!inLaunching || always) {
14065            synchronized (cpr) {
14066                cpr.launchingApp = null;
14067                cpr.notifyAll();
14068            }
14069            mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid));
14070            String names[] = cpr.info.authority.split(";");
14071            for (int j = 0; j < names.length; j++) {
14072                mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid));
14073            }
14074        }
14075
14076        for (int i=0; i<cpr.connections.size(); i++) {
14077            ContentProviderConnection conn = cpr.connections.get(i);
14078            if (conn.waiting) {
14079                // If this connection is waiting for the provider, then we don't
14080                // need to mess with its process unless we are always removing
14081                // or for some reason the provider is not currently launching.
14082                if (inLaunching && !always) {
14083                    continue;
14084                }
14085            }
14086            ProcessRecord capp = conn.client;
14087            conn.dead = true;
14088            if (conn.stableCount > 0) {
14089                if (!capp.persistent && capp.thread != null
14090                        && capp.pid != 0
14091                        && capp.pid != MY_PID) {
14092                    capp.kill("depends on provider "
14093                            + cpr.name.flattenToShortString()
14094                            + " in dying proc " + (proc != null ? proc.processName : "??"), true);
14095                }
14096            } else if (capp.thread != null && conn.provider.provider != null) {
14097                try {
14098                    capp.thread.unstableProviderDied(conn.provider.provider.asBinder());
14099                } catch (RemoteException e) {
14100                }
14101                // In the protocol here, we don't expect the client to correctly
14102                // clean up this connection, we'll just remove it.
14103                cpr.connections.remove(i);
14104                conn.client.conProviders.remove(conn);
14105            }
14106        }
14107
14108        if (inLaunching && always) {
14109            mLaunchingProviders.remove(cpr);
14110        }
14111        return inLaunching;
14112    }
14113
14114    /**
14115     * Main code for cleaning up a process when it has gone away.  This is
14116     * called both as a result of the process dying, or directly when stopping
14117     * a process when running in single process mode.
14118     */
14119    private final void cleanUpApplicationRecordLocked(ProcessRecord app,
14120            boolean restarting, boolean allowRestart, int index) {
14121        if (index >= 0) {
14122            removeLruProcessLocked(app);
14123            ProcessList.remove(app.pid);
14124        }
14125
14126        mProcessesToGc.remove(app);
14127        mPendingPssProcesses.remove(app);
14128
14129        // Dismiss any open dialogs.
14130        if (app.crashDialog != null && !app.forceCrashReport) {
14131            app.crashDialog.dismiss();
14132            app.crashDialog = null;
14133        }
14134        if (app.anrDialog != null) {
14135            app.anrDialog.dismiss();
14136            app.anrDialog = null;
14137        }
14138        if (app.waitDialog != null) {
14139            app.waitDialog.dismiss();
14140            app.waitDialog = null;
14141        }
14142
14143        app.crashing = false;
14144        app.notResponding = false;
14145
14146        app.resetPackageList(mProcessStats);
14147        app.unlinkDeathRecipient();
14148        app.makeInactive(mProcessStats);
14149        app.waitingToKill = null;
14150        app.forcingToForeground = null;
14151        updateProcessForegroundLocked(app, false, false);
14152        app.foregroundActivities = false;
14153        app.hasShownUi = false;
14154        app.treatLikeActivity = false;
14155        app.hasAboveClient = false;
14156        app.hasClientActivities = false;
14157
14158        mServices.killServicesLocked(app, allowRestart);
14159
14160        boolean restart = false;
14161
14162        // Remove published content providers.
14163        for (int i=app.pubProviders.size()-1; i>=0; i--) {
14164            ContentProviderRecord cpr = app.pubProviders.valueAt(i);
14165            final boolean always = app.bad || !allowRestart;
14166            if (removeDyingProviderLocked(app, cpr, always) || always) {
14167                // We left the provider in the launching list, need to
14168                // restart it.
14169                restart = true;
14170            }
14171
14172            cpr.provider = null;
14173            cpr.proc = null;
14174        }
14175        app.pubProviders.clear();
14176
14177        // Take care of any launching providers waiting for this process.
14178        if (checkAppInLaunchingProvidersLocked(app, false)) {
14179            restart = true;
14180        }
14181
14182        // Unregister from connected content providers.
14183        if (!app.conProviders.isEmpty()) {
14184            for (int i=0; i<app.conProviders.size(); i++) {
14185                ContentProviderConnection conn = app.conProviders.get(i);
14186                conn.provider.connections.remove(conn);
14187            }
14188            app.conProviders.clear();
14189        }
14190
14191        // At this point there may be remaining entries in mLaunchingProviders
14192        // where we were the only one waiting, so they are no longer of use.
14193        // Look for these and clean up if found.
14194        // XXX Commented out for now.  Trying to figure out a way to reproduce
14195        // the actual situation to identify what is actually going on.
14196        if (false) {
14197            for (int i=0; i<mLaunchingProviders.size(); i++) {
14198                ContentProviderRecord cpr = (ContentProviderRecord)
14199                        mLaunchingProviders.get(i);
14200                if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) {
14201                    synchronized (cpr) {
14202                        cpr.launchingApp = null;
14203                        cpr.notifyAll();
14204                    }
14205                }
14206            }
14207        }
14208
14209        skipCurrentReceiverLocked(app);
14210
14211        // Unregister any receivers.
14212        for (int i=app.receivers.size()-1; i>=0; i--) {
14213            removeReceiverLocked(app.receivers.valueAt(i));
14214        }
14215        app.receivers.clear();
14216
14217        // If the app is undergoing backup, tell the backup manager about it
14218        if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) {
14219            if (DEBUG_BACKUP || DEBUG_CLEANUP) Slog.d(TAG, "App "
14220                    + mBackupTarget.appInfo + " died during backup");
14221            try {
14222                IBackupManager bm = IBackupManager.Stub.asInterface(
14223                        ServiceManager.getService(Context.BACKUP_SERVICE));
14224                bm.agentDisconnected(app.info.packageName);
14225            } catch (RemoteException e) {
14226                // can't happen; backup manager is local
14227            }
14228        }
14229
14230        for (int i = mPendingProcessChanges.size()-1; i>=0; i--) {
14231            ProcessChangeItem item = mPendingProcessChanges.get(i);
14232            if (item.pid == app.pid) {
14233                mPendingProcessChanges.remove(i);
14234                mAvailProcessChanges.add(item);
14235            }
14236        }
14237        mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget();
14238
14239        // If the caller is restarting this app, then leave it in its
14240        // current lists and let the caller take care of it.
14241        if (restarting) {
14242            return;
14243        }
14244
14245        if (!app.persistent || app.isolated) {
14246            if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG,
14247                    "Removing non-persistent process during cleanup: " + app);
14248            mProcessNames.remove(app.processName, app.uid);
14249            mIsolatedProcesses.remove(app.uid);
14250            if (mHeavyWeightProcess == app) {
14251                mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
14252                        mHeavyWeightProcess.userId, 0));
14253                mHeavyWeightProcess = null;
14254            }
14255        } else if (!app.removed) {
14256            // This app is persistent, so we need to keep its record around.
14257            // If it is not already on the pending app list, add it there
14258            // and start a new process for it.
14259            if (mPersistentStartingProcesses.indexOf(app) < 0) {
14260                mPersistentStartingProcesses.add(app);
14261                restart = true;
14262            }
14263        }
14264        if ((DEBUG_PROCESSES || DEBUG_CLEANUP) && mProcessesOnHold.contains(app)) Slog.v(TAG,
14265                "Clean-up removing on hold: " + app);
14266        mProcessesOnHold.remove(app);
14267
14268        if (app == mHomeProcess) {
14269            mHomeProcess = null;
14270        }
14271        if (app == mPreviousProcess) {
14272            mPreviousProcess = null;
14273        }
14274
14275        if (restart && !app.isolated) {
14276            // We have components that still need to be running in the
14277            // process, so re-launch it.
14278            mProcessNames.put(app.processName, app.uid, app);
14279            startProcessLocked(app, "restart", app.processName);
14280        } else if (app.pid > 0 && app.pid != MY_PID) {
14281            // Goodbye!
14282            boolean removed;
14283            synchronized (mPidsSelfLocked) {
14284                mPidsSelfLocked.remove(app.pid);
14285                mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
14286            }
14287            mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
14288            if (app.isolated) {
14289                mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
14290            }
14291            app.setPid(0);
14292        }
14293    }
14294
14295    boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) {
14296        // Look through the content providers we are waiting to have launched,
14297        // and if any run in this process then either schedule a restart of
14298        // the process or kill the client waiting for it if this process has
14299        // gone bad.
14300        int NL = mLaunchingProviders.size();
14301        boolean restart = false;
14302        for (int i=0; i<NL; i++) {
14303            ContentProviderRecord cpr = mLaunchingProviders.get(i);
14304            if (cpr.launchingApp == app) {
14305                if (!alwaysBad && !app.bad) {
14306                    restart = true;
14307                } else {
14308                    removeDyingProviderLocked(app, cpr, true);
14309                    // cpr should have been removed from mLaunchingProviders
14310                    NL = mLaunchingProviders.size();
14311                    i--;
14312                }
14313            }
14314        }
14315        return restart;
14316    }
14317
14318    // =========================================================
14319    // SERVICES
14320    // =========================================================
14321
14322    @Override
14323    public List<ActivityManager.RunningServiceInfo> getServices(int maxNum,
14324            int flags) {
14325        enforceNotIsolatedCaller("getServices");
14326        synchronized (this) {
14327            return mServices.getRunningServiceInfoLocked(maxNum, flags);
14328        }
14329    }
14330
14331    @Override
14332    public PendingIntent getRunningServiceControlPanel(ComponentName name) {
14333        enforceNotIsolatedCaller("getRunningServiceControlPanel");
14334        synchronized (this) {
14335            return mServices.getRunningServiceControlPanelLocked(name);
14336        }
14337    }
14338
14339    @Override
14340    public ComponentName startService(IApplicationThread caller, Intent service,
14341            String resolvedType, int userId) {
14342        enforceNotIsolatedCaller("startService");
14343        // Refuse possible leaked file descriptors
14344        if (service != null && service.hasFileDescriptors() == true) {
14345            throw new IllegalArgumentException("File descriptors passed in Intent");
14346        }
14347
14348        if (DEBUG_SERVICE)
14349            Slog.v(TAG, "startService: " + service + " type=" + resolvedType);
14350        synchronized(this) {
14351            final int callingPid = Binder.getCallingPid();
14352            final int callingUid = Binder.getCallingUid();
14353            final long origId = Binder.clearCallingIdentity();
14354            ComponentName res = mServices.startServiceLocked(caller, service,
14355                    resolvedType, callingPid, callingUid, userId);
14356            Binder.restoreCallingIdentity(origId);
14357            return res;
14358        }
14359    }
14360
14361    ComponentName startServiceInPackage(int uid,
14362            Intent service, String resolvedType, int userId) {
14363        synchronized(this) {
14364            if (DEBUG_SERVICE)
14365                Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType);
14366            final long origId = Binder.clearCallingIdentity();
14367            ComponentName res = mServices.startServiceLocked(null, service,
14368                    resolvedType, -1, uid, userId);
14369            Binder.restoreCallingIdentity(origId);
14370            return res;
14371        }
14372    }
14373
14374    @Override
14375    public int stopService(IApplicationThread caller, Intent service,
14376            String resolvedType, int userId) {
14377        enforceNotIsolatedCaller("stopService");
14378        // Refuse possible leaked file descriptors
14379        if (service != null && service.hasFileDescriptors() == true) {
14380            throw new IllegalArgumentException("File descriptors passed in Intent");
14381        }
14382
14383        synchronized(this) {
14384            return mServices.stopServiceLocked(caller, service, resolvedType, userId);
14385        }
14386    }
14387
14388    @Override
14389    public IBinder peekService(Intent service, String resolvedType) {
14390        enforceNotIsolatedCaller("peekService");
14391        // Refuse possible leaked file descriptors
14392        if (service != null && service.hasFileDescriptors() == true) {
14393            throw new IllegalArgumentException("File descriptors passed in Intent");
14394        }
14395        synchronized(this) {
14396            return mServices.peekServiceLocked(service, resolvedType);
14397        }
14398    }
14399
14400    @Override
14401    public boolean stopServiceToken(ComponentName className, IBinder token,
14402            int startId) {
14403        synchronized(this) {
14404            return mServices.stopServiceTokenLocked(className, token, startId);
14405        }
14406    }
14407
14408    @Override
14409    public void setServiceForeground(ComponentName className, IBinder token,
14410            int id, Notification notification, boolean removeNotification) {
14411        synchronized(this) {
14412            mServices.setServiceForegroundLocked(className, token, id, notification,
14413                    removeNotification);
14414        }
14415    }
14416
14417    @Override
14418    public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll,
14419            boolean requireFull, String name, String callerPackage) {
14420        return handleIncomingUser(callingPid, callingUid, userId, allowAll,
14421                requireFull ? ALLOW_FULL_ONLY : ALLOW_NON_FULL, name, callerPackage);
14422    }
14423
14424    int unsafeConvertIncomingUser(int userId) {
14425        return (userId == UserHandle.USER_CURRENT || userId == UserHandle.USER_CURRENT_OR_SELF)
14426                ? mCurrentUserId : userId;
14427    }
14428
14429    int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll,
14430            int allowMode, String name, String callerPackage) {
14431        final int callingUserId = UserHandle.getUserId(callingUid);
14432        if (callingUserId == userId) {
14433            return userId;
14434        }
14435
14436        // Note that we may be accessing mCurrentUserId outside of a lock...
14437        // shouldn't be a big deal, if this is being called outside
14438        // of a locked context there is intrinsically a race with
14439        // the value the caller will receive and someone else changing it.
14440        // We assume that USER_CURRENT_OR_SELF will use the current user; later
14441        // we will switch to the calling user if access to the current user fails.
14442        int targetUserId = unsafeConvertIncomingUser(userId);
14443
14444        if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
14445            final boolean allow;
14446            if (checkComponentPermission(INTERACT_ACROSS_USERS_FULL, callingPid,
14447                    callingUid, -1, true) == PackageManager.PERMISSION_GRANTED) {
14448                // If the caller has this permission, they always pass go.  And collect $200.
14449                allow = true;
14450            } else if (allowMode == ALLOW_FULL_ONLY) {
14451                // We require full access, sucks to be you.
14452                allow = false;
14453            } else if (checkComponentPermission(INTERACT_ACROSS_USERS, callingPid,
14454                    callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) {
14455                // If the caller does not have either permission, they are always doomed.
14456                allow = false;
14457            } else if (allowMode == ALLOW_NON_FULL) {
14458                // We are blanket allowing non-full access, you lucky caller!
14459                allow = true;
14460            } else if (allowMode == ALLOW_NON_FULL_IN_PROFILE) {
14461                // We may or may not allow this depending on whether the two users are
14462                // in the same profile.
14463                synchronized (mUserProfileGroupIdsSelfLocked) {
14464                    int callingProfile = mUserProfileGroupIdsSelfLocked.get(callingUserId,
14465                            UserInfo.NO_PROFILE_GROUP_ID);
14466                    int targetProfile = mUserProfileGroupIdsSelfLocked.get(targetUserId,
14467                            UserInfo.NO_PROFILE_GROUP_ID);
14468                    allow = callingProfile != UserInfo.NO_PROFILE_GROUP_ID
14469                            && callingProfile == targetProfile;
14470                }
14471            } else {
14472                throw new IllegalArgumentException("Unknown mode: " + allowMode);
14473            }
14474            if (!allow) {
14475                if (userId == UserHandle.USER_CURRENT_OR_SELF) {
14476                    // In this case, they would like to just execute as their
14477                    // owner user instead of failing.
14478                    targetUserId = callingUserId;
14479                } else {
14480                    StringBuilder builder = new StringBuilder(128);
14481                    builder.append("Permission Denial: ");
14482                    builder.append(name);
14483                    if (callerPackage != null) {
14484                        builder.append(" from ");
14485                        builder.append(callerPackage);
14486                    }
14487                    builder.append(" asks to run as user ");
14488                    builder.append(userId);
14489                    builder.append(" but is calling from user ");
14490                    builder.append(UserHandle.getUserId(callingUid));
14491                    builder.append("; this requires ");
14492                    builder.append(INTERACT_ACROSS_USERS_FULL);
14493                    if (allowMode != ALLOW_FULL_ONLY) {
14494                        builder.append(" or ");
14495                        builder.append(INTERACT_ACROSS_USERS);
14496                    }
14497                    String msg = builder.toString();
14498                    Slog.w(TAG, msg);
14499                    throw new SecurityException(msg);
14500                }
14501            }
14502        }
14503        if (!allowAll && targetUserId < 0) {
14504            throw new IllegalArgumentException(
14505                    "Call does not support special user #" + targetUserId);
14506        }
14507        return targetUserId;
14508    }
14509
14510    boolean isSingleton(String componentProcessName, ApplicationInfo aInfo,
14511            String className, int flags) {
14512        boolean result = false;
14513        // For apps that don't have pre-defined UIDs, check for permission
14514        if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) {
14515            if ((flags & ServiceInfo.FLAG_SINGLE_USER) != 0) {
14516                if (ActivityManager.checkUidPermission(
14517                        INTERACT_ACROSS_USERS,
14518                        aInfo.uid) != PackageManager.PERMISSION_GRANTED) {
14519                    ComponentName comp = new ComponentName(aInfo.packageName, className);
14520                    String msg = "Permission Denial: Component " + comp.flattenToShortString()
14521                            + " requests FLAG_SINGLE_USER, but app does not hold "
14522                            + INTERACT_ACROSS_USERS;
14523                    Slog.w(TAG, msg);
14524                    throw new SecurityException(msg);
14525                }
14526                // Permission passed
14527                result = true;
14528            }
14529        } else if ("system".equals(componentProcessName)) {
14530            result = true;
14531        } else if (UserHandle.isSameApp(aInfo.uid, Process.PHONE_UID)
14532                && (flags & ServiceInfo.FLAG_SINGLE_USER) != 0) {
14533            // Phone app is allowed to export singleuser providers.
14534            result = true;
14535        } else {
14536            // App with pre-defined UID, check if it's a persistent app
14537            result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0;
14538        }
14539        if (DEBUG_MU) {
14540            Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo
14541                    + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result);
14542        }
14543        return result;
14544    }
14545
14546    /**
14547     * Checks to see if the caller is in the same app as the singleton
14548     * component, or the component is in a special app. It allows special apps
14549     * to export singleton components but prevents exporting singleton
14550     * components for regular apps.
14551     */
14552    boolean isValidSingletonCall(int callingUid, int componentUid) {
14553        int componentAppId = UserHandle.getAppId(componentUid);
14554        return UserHandle.isSameApp(callingUid, componentUid)
14555                || componentAppId == Process.SYSTEM_UID
14556                || componentAppId == Process.PHONE_UID
14557                || ActivityManager.checkUidPermission(INTERACT_ACROSS_USERS_FULL, componentUid)
14558                        == PackageManager.PERMISSION_GRANTED;
14559    }
14560
14561    public int bindService(IApplicationThread caller, IBinder token,
14562            Intent service, String resolvedType,
14563            IServiceConnection connection, int flags, int userId) {
14564        enforceNotIsolatedCaller("bindService");
14565        // Refuse possible leaked file descriptors
14566        if (service != null && service.hasFileDescriptors() == true) {
14567            throw new IllegalArgumentException("File descriptors passed in Intent");
14568        }
14569
14570        synchronized(this) {
14571            return mServices.bindServiceLocked(caller, token, service, resolvedType,
14572                    connection, flags, userId);
14573        }
14574    }
14575
14576    public boolean unbindService(IServiceConnection connection) {
14577        synchronized (this) {
14578            return mServices.unbindServiceLocked(connection);
14579        }
14580    }
14581
14582    public void publishService(IBinder token, Intent intent, IBinder service) {
14583        // Refuse possible leaked file descriptors
14584        if (intent != null && intent.hasFileDescriptors() == true) {
14585            throw new IllegalArgumentException("File descriptors passed in Intent");
14586        }
14587
14588        synchronized(this) {
14589            if (!(token instanceof ServiceRecord)) {
14590                throw new IllegalArgumentException("Invalid service token");
14591            }
14592            mServices.publishServiceLocked((ServiceRecord)token, intent, service);
14593        }
14594    }
14595
14596    public void unbindFinished(IBinder token, Intent intent, boolean doRebind) {
14597        // Refuse possible leaked file descriptors
14598        if (intent != null && intent.hasFileDescriptors() == true) {
14599            throw new IllegalArgumentException("File descriptors passed in Intent");
14600        }
14601
14602        synchronized(this) {
14603            mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind);
14604        }
14605    }
14606
14607    public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {
14608        synchronized(this) {
14609            if (!(token instanceof ServiceRecord)) {
14610                throw new IllegalArgumentException("Invalid service token");
14611            }
14612            mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res);
14613        }
14614    }
14615
14616    // =========================================================
14617    // BACKUP AND RESTORE
14618    // =========================================================
14619
14620    // Cause the target app to be launched if necessary and its backup agent
14621    // instantiated.  The backup agent will invoke backupAgentCreated() on the
14622    // activity manager to announce its creation.
14623    public boolean bindBackupAgent(ApplicationInfo app, int backupMode) {
14624        if (DEBUG_BACKUP) Slog.v(TAG, "bindBackupAgent: app=" + app + " mode=" + backupMode);
14625        enforceCallingPermission("android.permission.CONFIRM_FULL_BACKUP", "bindBackupAgent");
14626
14627        synchronized(this) {
14628            // !!! TODO: currently no check here that we're already bound
14629            BatteryStatsImpl.Uid.Pkg.Serv ss = null;
14630            BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
14631            synchronized (stats) {
14632                ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name);
14633            }
14634
14635            // Backup agent is now in use, its package can't be stopped.
14636            try {
14637                AppGlobals.getPackageManager().setPackageStoppedState(
14638                        app.packageName, false, UserHandle.getUserId(app.uid));
14639            } catch (RemoteException e) {
14640            } catch (IllegalArgumentException e) {
14641                Slog.w(TAG, "Failed trying to unstop package "
14642                        + app.packageName + ": " + e);
14643            }
14644
14645            BackupRecord r = new BackupRecord(ss, app, backupMode);
14646            ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL)
14647                    ? new ComponentName(app.packageName, app.backupAgentName)
14648                    : new ComponentName("android", "FullBackupAgent");
14649            // startProcessLocked() returns existing proc's record if it's already running
14650            ProcessRecord proc = startProcessLocked(app.processName, app,
14651                    false, 0, "backup", hostingName, false, false, false);
14652            if (proc == null) {
14653                Slog.e(TAG, "Unable to start backup agent process " + r);
14654                return false;
14655            }
14656
14657            r.app = proc;
14658            mBackupTarget = r;
14659            mBackupAppName = app.packageName;
14660
14661            // Try not to kill the process during backup
14662            updateOomAdjLocked(proc);
14663
14664            // If the process is already attached, schedule the creation of the backup agent now.
14665            // If it is not yet live, this will be done when it attaches to the framework.
14666            if (proc.thread != null) {
14667                if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc);
14668                try {
14669                    proc.thread.scheduleCreateBackupAgent(app,
14670                            compatibilityInfoForPackageLocked(app), backupMode);
14671                } catch (RemoteException e) {
14672                    // Will time out on the backup manager side
14673                }
14674            } else {
14675                if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach");
14676            }
14677            // Invariants: at this point, the target app process exists and the application
14678            // is either already running or in the process of coming up.  mBackupTarget and
14679            // mBackupAppName describe the app, so that when it binds back to the AM we
14680            // know that it's scheduled for a backup-agent operation.
14681        }
14682
14683        return true;
14684    }
14685
14686    @Override
14687    public void clearPendingBackup() {
14688        if (DEBUG_BACKUP) Slog.v(TAG, "clearPendingBackup");
14689        enforceCallingPermission("android.permission.BACKUP", "clearPendingBackup");
14690
14691        synchronized (this) {
14692            mBackupTarget = null;
14693            mBackupAppName = null;
14694        }
14695    }
14696
14697    // A backup agent has just come up
14698    public void backupAgentCreated(String agentPackageName, IBinder agent) {
14699        if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName
14700                + " = " + agent);
14701
14702        synchronized(this) {
14703            if (!agentPackageName.equals(mBackupAppName)) {
14704                Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!");
14705                return;
14706            }
14707        }
14708
14709        long oldIdent = Binder.clearCallingIdentity();
14710        try {
14711            IBackupManager bm = IBackupManager.Stub.asInterface(
14712                    ServiceManager.getService(Context.BACKUP_SERVICE));
14713            bm.agentConnected(agentPackageName, agent);
14714        } catch (RemoteException e) {
14715            // can't happen; the backup manager service is local
14716        } catch (Exception e) {
14717            Slog.w(TAG, "Exception trying to deliver BackupAgent binding: ");
14718            e.printStackTrace();
14719        } finally {
14720            Binder.restoreCallingIdentity(oldIdent);
14721        }
14722    }
14723
14724    // done with this agent
14725    public void unbindBackupAgent(ApplicationInfo appInfo) {
14726        if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo);
14727        if (appInfo == null) {
14728            Slog.w(TAG, "unbind backup agent for null app");
14729            return;
14730        }
14731
14732        synchronized(this) {
14733            try {
14734                if (mBackupAppName == null) {
14735                    Slog.w(TAG, "Unbinding backup agent with no active backup");
14736                    return;
14737                }
14738
14739                if (!mBackupAppName.equals(appInfo.packageName)) {
14740                    Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target");
14741                    return;
14742                }
14743
14744                // Not backing this app up any more; reset its OOM adjustment
14745                final ProcessRecord proc = mBackupTarget.app;
14746                updateOomAdjLocked(proc);
14747
14748                // If the app crashed during backup, 'thread' will be null here
14749                if (proc.thread != null) {
14750                    try {
14751                        proc.thread.scheduleDestroyBackupAgent(appInfo,
14752                                compatibilityInfoForPackageLocked(appInfo));
14753                    } catch (Exception e) {
14754                        Slog.e(TAG, "Exception when unbinding backup agent:");
14755                        e.printStackTrace();
14756                    }
14757                }
14758            } finally {
14759                mBackupTarget = null;
14760                mBackupAppName = null;
14761            }
14762        }
14763    }
14764    // =========================================================
14765    // BROADCASTS
14766    // =========================================================
14767
14768    private final List getStickiesLocked(String action, IntentFilter filter,
14769            List cur, int userId) {
14770        final ContentResolver resolver = mContext.getContentResolver();
14771        ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId);
14772        if (stickies == null) {
14773            return cur;
14774        }
14775        final ArrayList<Intent> list = stickies.get(action);
14776        if (list == null) {
14777            return cur;
14778        }
14779        int N = list.size();
14780        for (int i=0; i<N; i++) {
14781            Intent intent = list.get(i);
14782            if (filter.match(resolver, intent, true, TAG) >= 0) {
14783                if (cur == null) {
14784                    cur = new ArrayList<Intent>();
14785                }
14786                cur.add(intent);
14787            }
14788        }
14789        return cur;
14790    }
14791
14792    boolean isPendingBroadcastProcessLocked(int pid) {
14793        return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid)
14794                || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid);
14795    }
14796
14797    void skipPendingBroadcastLocked(int pid) {
14798            Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
14799            for (BroadcastQueue queue : mBroadcastQueues) {
14800                queue.skipPendingBroadcastLocked(pid);
14801            }
14802    }
14803
14804    // The app just attached; send any pending broadcasts that it should receive
14805    boolean sendPendingBroadcastsLocked(ProcessRecord app) {
14806        boolean didSomething = false;
14807        for (BroadcastQueue queue : mBroadcastQueues) {
14808            didSomething |= queue.sendPendingBroadcastsLocked(app);
14809        }
14810        return didSomething;
14811    }
14812
14813    public Intent registerReceiver(IApplicationThread caller, String callerPackage,
14814            IIntentReceiver receiver, IntentFilter filter, String permission, int userId) {
14815        enforceNotIsolatedCaller("registerReceiver");
14816        int callingUid;
14817        int callingPid;
14818        synchronized(this) {
14819            ProcessRecord callerApp = null;
14820            if (caller != null) {
14821                callerApp = getRecordForAppLocked(caller);
14822                if (callerApp == null) {
14823                    throw new SecurityException(
14824                            "Unable to find app for caller " + caller
14825                            + " (pid=" + Binder.getCallingPid()
14826                            + ") when registering receiver " + receiver);
14827                }
14828                if (callerApp.info.uid != Process.SYSTEM_UID &&
14829                        !callerApp.pkgList.containsKey(callerPackage) &&
14830                        !"android".equals(callerPackage)) {
14831                    throw new SecurityException("Given caller package " + callerPackage
14832                            + " is not running in process " + callerApp);
14833                }
14834                callingUid = callerApp.info.uid;
14835                callingPid = callerApp.pid;
14836            } else {
14837                callerPackage = null;
14838                callingUid = Binder.getCallingUid();
14839                callingPid = Binder.getCallingPid();
14840            }
14841
14842            userId = this.handleIncomingUser(callingPid, callingUid, userId,
14843                    true, ALLOW_FULL_ONLY, "registerReceiver", callerPackage);
14844
14845            List allSticky = null;
14846
14847            // Look for any matching sticky broadcasts...
14848            Iterator actions = filter.actionsIterator();
14849            if (actions != null) {
14850                while (actions.hasNext()) {
14851                    String action = (String)actions.next();
14852                    allSticky = getStickiesLocked(action, filter, allSticky,
14853                            UserHandle.USER_ALL);
14854                    allSticky = getStickiesLocked(action, filter, allSticky,
14855                            UserHandle.getUserId(callingUid));
14856                }
14857            } else {
14858                allSticky = getStickiesLocked(null, filter, allSticky,
14859                        UserHandle.USER_ALL);
14860                allSticky = getStickiesLocked(null, filter, allSticky,
14861                        UserHandle.getUserId(callingUid));
14862            }
14863
14864            // The first sticky in the list is returned directly back to
14865            // the client.
14866            Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null;
14867
14868            if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter
14869                    + ": " + sticky);
14870
14871            if (receiver == null) {
14872                return sticky;
14873            }
14874
14875            ReceiverList rl
14876                = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
14877            if (rl == null) {
14878                rl = new ReceiverList(this, callerApp, callingPid, callingUid,
14879                        userId, receiver);
14880                if (rl.app != null) {
14881                    rl.app.receivers.add(rl);
14882                } else {
14883                    try {
14884                        receiver.asBinder().linkToDeath(rl, 0);
14885                    } catch (RemoteException e) {
14886                        return sticky;
14887                    }
14888                    rl.linkedToDeath = true;
14889                }
14890                mRegisteredReceivers.put(receiver.asBinder(), rl);
14891            } else if (rl.uid != callingUid) {
14892                throw new IllegalArgumentException(
14893                        "Receiver requested to register for uid " + callingUid
14894                        + " was previously registered for uid " + rl.uid);
14895            } else if (rl.pid != callingPid) {
14896                throw new IllegalArgumentException(
14897                        "Receiver requested to register for pid " + callingPid
14898                        + " was previously registered for pid " + rl.pid);
14899            } else if (rl.userId != userId) {
14900                throw new IllegalArgumentException(
14901                        "Receiver requested to register for user " + userId
14902                        + " was previously registered for user " + rl.userId);
14903            }
14904            BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
14905                    permission, callingUid, userId);
14906            rl.add(bf);
14907            if (!bf.debugCheck()) {
14908                Slog.w(TAG, "==> For Dynamic broadast");
14909            }
14910            mReceiverResolver.addFilter(bf);
14911
14912            // Enqueue broadcasts for all existing stickies that match
14913            // this filter.
14914            if (allSticky != null) {
14915                ArrayList receivers = new ArrayList();
14916                receivers.add(bf);
14917
14918                int N = allSticky.size();
14919                for (int i=0; i<N; i++) {
14920                    Intent intent = (Intent)allSticky.get(i);
14921                    BroadcastQueue queue = broadcastQueueForIntent(intent);
14922                    BroadcastRecord r = new BroadcastRecord(queue, intent, null,
14923                            null, -1, -1, null, null, AppOpsManager.OP_NONE, receivers, null, 0,
14924                            null, null, false, true, true, -1);
14925                    queue.enqueueParallelBroadcastLocked(r);
14926                    queue.scheduleBroadcastsLocked();
14927                }
14928            }
14929
14930            return sticky;
14931        }
14932    }
14933
14934    public void unregisterReceiver(IIntentReceiver receiver) {
14935        if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver);
14936
14937        final long origId = Binder.clearCallingIdentity();
14938        try {
14939            boolean doTrim = false;
14940
14941            synchronized(this) {
14942                ReceiverList rl = mRegisteredReceivers.get(receiver.asBinder());
14943                if (rl != null) {
14944                    if (rl.curBroadcast != null) {
14945                        BroadcastRecord r = rl.curBroadcast;
14946                        final boolean doNext = finishReceiverLocked(
14947                                receiver.asBinder(), r.resultCode, r.resultData,
14948                                r.resultExtras, r.resultAbort);
14949                        if (doNext) {
14950                            doTrim = true;
14951                            r.queue.processNextBroadcast(false);
14952                        }
14953                    }
14954
14955                    if (rl.app != null) {
14956                        rl.app.receivers.remove(rl);
14957                    }
14958                    removeReceiverLocked(rl);
14959                    if (rl.linkedToDeath) {
14960                        rl.linkedToDeath = false;
14961                        rl.receiver.asBinder().unlinkToDeath(rl, 0);
14962                    }
14963                }
14964            }
14965
14966            // If we actually concluded any broadcasts, we might now be able
14967            // to trim the recipients' apps from our working set
14968            if (doTrim) {
14969                trimApplications();
14970                return;
14971            }
14972
14973        } finally {
14974            Binder.restoreCallingIdentity(origId);
14975        }
14976    }
14977
14978    void removeReceiverLocked(ReceiverList rl) {
14979        mRegisteredReceivers.remove(rl.receiver.asBinder());
14980        int N = rl.size();
14981        for (int i=0; i<N; i++) {
14982            mReceiverResolver.removeFilter(rl.get(i));
14983        }
14984    }
14985
14986    private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) {
14987        for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
14988            ProcessRecord r = mLruProcesses.get(i);
14989            if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) {
14990                try {
14991                    r.thread.dispatchPackageBroadcast(cmd, packages);
14992                } catch (RemoteException ex) {
14993                }
14994            }
14995        }
14996    }
14997
14998    private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType,
14999            int[] users) {
15000        List<ResolveInfo> receivers = null;
15001        try {
15002            HashSet<ComponentName> singleUserReceivers = null;
15003            boolean scannedFirstReceivers = false;
15004            for (int user : users) {
15005                List<ResolveInfo> newReceivers = AppGlobals.getPackageManager()
15006                        .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user);
15007                if (user != 0 && newReceivers != null) {
15008                    // If this is not the primary user, we need to check for
15009                    // any receivers that should be filtered out.
15010                    for (int i=0; i<newReceivers.size(); i++) {
15011                        ResolveInfo ri = newReceivers.get(i);
15012                        if ((ri.activityInfo.flags&ActivityInfo.FLAG_PRIMARY_USER_ONLY) != 0) {
15013                            newReceivers.remove(i);
15014                            i--;
15015                        }
15016                    }
15017                }
15018                if (newReceivers != null && newReceivers.size() == 0) {
15019                    newReceivers = null;
15020                }
15021                if (receivers == null) {
15022                    receivers = newReceivers;
15023                } else if (newReceivers != null) {
15024                    // We need to concatenate the additional receivers
15025                    // found with what we have do far.  This would be easy,
15026                    // but we also need to de-dup any receivers that are
15027                    // singleUser.
15028                    if (!scannedFirstReceivers) {
15029                        // Collect any single user receivers we had already retrieved.
15030                        scannedFirstReceivers = true;
15031                        for (int i=0; i<receivers.size(); i++) {
15032                            ResolveInfo ri = receivers.get(i);
15033                            if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
15034                                ComponentName cn = new ComponentName(
15035                                        ri.activityInfo.packageName, ri.activityInfo.name);
15036                                if (singleUserReceivers == null) {
15037                                    singleUserReceivers = new HashSet<ComponentName>();
15038                                }
15039                                singleUserReceivers.add(cn);
15040                            }
15041                        }
15042                    }
15043                    // Add the new results to the existing results, tracking
15044                    // and de-dupping single user receivers.
15045                    for (int i=0; i<newReceivers.size(); i++) {
15046                        ResolveInfo ri = newReceivers.get(i);
15047                        if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
15048                            ComponentName cn = new ComponentName(
15049                                    ri.activityInfo.packageName, ri.activityInfo.name);
15050                            if (singleUserReceivers == null) {
15051                                singleUserReceivers = new HashSet<ComponentName>();
15052                            }
15053                            if (!singleUserReceivers.contains(cn)) {
15054                                singleUserReceivers.add(cn);
15055                                receivers.add(ri);
15056                            }
15057                        } else {
15058                            receivers.add(ri);
15059                        }
15060                    }
15061                }
15062            }
15063        } catch (RemoteException ex) {
15064            // pm is in same process, this will never happen.
15065        }
15066        return receivers;
15067    }
15068
15069    private final int broadcastIntentLocked(ProcessRecord callerApp,
15070            String callerPackage, Intent intent, String resolvedType,
15071            IIntentReceiver resultTo, int resultCode, String resultData,
15072            Bundle map, String requiredPermission, int appOp,
15073            boolean ordered, boolean sticky, int callingPid, int callingUid,
15074            int userId) {
15075        intent = new Intent(intent);
15076
15077        // By default broadcasts do not go to stopped apps.
15078        intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
15079
15080        if (DEBUG_BROADCAST_LIGHT) Slog.v(
15081            TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
15082            + " ordered=" + ordered + " userid=" + userId);
15083        if ((resultTo != null) && !ordered) {
15084            Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
15085        }
15086
15087        userId = handleIncomingUser(callingPid, callingUid, userId,
15088                true, ALLOW_NON_FULL, "broadcast", callerPackage);
15089
15090        // Make sure that the user who is receiving this broadcast is started.
15091        // If not, we will just skip it.
15092
15093
15094        if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) {
15095            if (callingUid != Process.SYSTEM_UID || (intent.getFlags()
15096                    & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0) {
15097                Slog.w(TAG, "Skipping broadcast of " + intent
15098                        + ": user " + userId + " is stopped");
15099                return ActivityManager.BROADCAST_SUCCESS;
15100            }
15101        }
15102
15103        /*
15104         * Prevent non-system code (defined here to be non-persistent
15105         * processes) from sending protected broadcasts.
15106         */
15107        int callingAppId = UserHandle.getAppId(callingUid);
15108        if (callingAppId == Process.SYSTEM_UID || callingAppId == Process.PHONE_UID
15109            || callingAppId == Process.SHELL_UID || callingAppId == Process.BLUETOOTH_UID
15110            || callingAppId == Process.NFC_UID || callingUid == 0) {
15111            // Always okay.
15112        } else if (callerApp == null || !callerApp.persistent) {
15113            try {
15114                if (AppGlobals.getPackageManager().isProtectedBroadcast(
15115                        intent.getAction())) {
15116                    String msg = "Permission Denial: not allowed to send broadcast "
15117                            + intent.getAction() + " from pid="
15118                            + callingPid + ", uid=" + callingUid;
15119                    Slog.w(TAG, msg);
15120                    throw new SecurityException(msg);
15121                } else if (AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(intent.getAction())) {
15122                    // Special case for compatibility: we don't want apps to send this,
15123                    // but historically it has not been protected and apps may be using it
15124                    // to poke their own app widget.  So, instead of making it protected,
15125                    // just limit it to the caller.
15126                    if (callerApp == null) {
15127                        String msg = "Permission Denial: not allowed to send broadcast "
15128                                + intent.getAction() + " from unknown caller.";
15129                        Slog.w(TAG, msg);
15130                        throw new SecurityException(msg);
15131                    } else if (intent.getComponent() != null) {
15132                        // They are good enough to send to an explicit component...  verify
15133                        // it is being sent to the calling app.
15134                        if (!intent.getComponent().getPackageName().equals(
15135                                callerApp.info.packageName)) {
15136                            String msg = "Permission Denial: not allowed to send broadcast "
15137                                    + intent.getAction() + " to "
15138                                    + intent.getComponent().getPackageName() + " from "
15139                                    + callerApp.info.packageName;
15140                            Slog.w(TAG, msg);
15141                            throw new SecurityException(msg);
15142                        }
15143                    } else {
15144                        // Limit broadcast to their own package.
15145                        intent.setPackage(callerApp.info.packageName);
15146                    }
15147                }
15148            } catch (RemoteException e) {
15149                Slog.w(TAG, "Remote exception", e);
15150                return ActivityManager.BROADCAST_SUCCESS;
15151            }
15152        }
15153
15154        // Handle special intents: if this broadcast is from the package
15155        // manager about a package being removed, we need to remove all of
15156        // its activities from the history stack.
15157        final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals(
15158                intent.getAction());
15159        if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
15160                || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())
15161                || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())
15162                || Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())
15163                || uidRemoved) {
15164            if (checkComponentPermission(
15165                    android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,
15166                    callingPid, callingUid, -1, true)
15167                    == PackageManager.PERMISSION_GRANTED) {
15168                if (uidRemoved) {
15169                    final Bundle intentExtras = intent.getExtras();
15170                    final int uid = intentExtras != null
15171                            ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
15172                    if (uid >= 0) {
15173                        BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
15174                        synchronized (bs) {
15175                            bs.removeUidStatsLocked(uid);
15176                        }
15177                        mAppOpsService.uidRemoved(uid);
15178                    }
15179                } else {
15180                    // If resources are unavailable just force stop all
15181                    // those packages and flush the attribute cache as well.
15182                    if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {
15183                        String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
15184                        if (list != null && (list.length > 0)) {
15185                            for (String pkg : list) {
15186                                forceStopPackageLocked(pkg, -1, false, true, true, false, false, userId,
15187                                        "storage unmount");
15188                            }
15189                            cleanupRecentTasksLocked(UserHandle.USER_ALL);
15190                            sendPackageBroadcastLocked(
15191                                    IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId);
15192                        }
15193                    } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(
15194                            intent.getAction())) {
15195                        cleanupRecentTasksLocked(UserHandle.USER_ALL);
15196                    } else {
15197                        Uri data = intent.getData();
15198                        String ssp;
15199                        if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
15200                            boolean removed = Intent.ACTION_PACKAGE_REMOVED.equals(
15201                                    intent.getAction());
15202                            boolean fullUninstall = removed &&
15203                                    !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
15204                            if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
15205                                forceStopPackageLocked(ssp, UserHandle.getAppId(
15206                                        intent.getIntExtra(Intent.EXTRA_UID, -1)), false, true, true,
15207                                        false, fullUninstall, userId,
15208                                        removed ? "pkg removed" : "pkg changed");
15209                            }
15210                            if (removed) {
15211                                sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,
15212                                        new String[] {ssp}, userId);
15213                                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
15214                                    mAppOpsService.packageRemoved(
15215                                            intent.getIntExtra(Intent.EXTRA_UID, -1), ssp);
15216
15217                                    // Remove all permissions granted from/to this package
15218                                    removeUriPermissionsForPackageLocked(ssp, userId, true);
15219                                }
15220                            }
15221                        }
15222                    }
15223                }
15224            } else {
15225                String msg = "Permission Denial: " + intent.getAction()
15226                        + " broadcast from " + callerPackage + " (pid=" + callingPid
15227                        + ", uid=" + callingUid + ")"
15228                        + " requires "
15229                        + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;
15230                Slog.w(TAG, msg);
15231                throw new SecurityException(msg);
15232            }
15233
15234        // Special case for adding a package: by default turn on compatibility
15235        // mode.
15236        } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
15237            Uri data = intent.getData();
15238            String ssp;
15239            if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
15240                mCompatModePackages.handlePackageAddedLocked(ssp,
15241                        intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));
15242            }
15243        }
15244
15245        /*
15246         * If this is the time zone changed action, queue up a message that will reset the timezone
15247         * of all currently running processes. This message will get queued up before the broadcast
15248         * happens.
15249         */
15250        if (Intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
15251            mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);
15252        }
15253
15254        /*
15255         * If the user set the time, let all running processes know.
15256         */
15257        if (Intent.ACTION_TIME_CHANGED.equals(intent.getAction())) {
15258            final int is24Hour = intent.getBooleanExtra(
15259                    Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, false) ? 1 : 0;
15260            mHandler.sendMessage(mHandler.obtainMessage(UPDATE_TIME, is24Hour, 0));
15261            BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
15262            synchronized (stats) {
15263                stats.noteCurrentTimeChangedLocked();
15264            }
15265        }
15266
15267        if (Intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) {
15268            mHandler.sendEmptyMessage(CLEAR_DNS_CACHE_MSG);
15269        }
15270
15271        if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) {
15272            ProxyInfo proxy = intent.getParcelableExtra(Proxy.EXTRA_PROXY_INFO);
15273            mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY_MSG, proxy));
15274        }
15275
15276        // Add to the sticky list if requested.
15277        if (sticky) {
15278            if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
15279                    callingPid, callingUid)
15280                    != PackageManager.PERMISSION_GRANTED) {
15281                String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="
15282                        + callingPid + ", uid=" + callingUid
15283                        + " requires " + android.Manifest.permission.BROADCAST_STICKY;
15284                Slog.w(TAG, msg);
15285                throw new SecurityException(msg);
15286            }
15287            if (requiredPermission != null) {
15288                Slog.w(TAG, "Can't broadcast sticky intent " + intent
15289                        + " and enforce permission " + requiredPermission);
15290                return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION;
15291            }
15292            if (intent.getComponent() != null) {
15293                throw new SecurityException(
15294                        "Sticky broadcasts can't target a specific component");
15295            }
15296            // We use userId directly here, since the "all" target is maintained
15297            // as a separate set of sticky broadcasts.
15298            if (userId != UserHandle.USER_ALL) {
15299                // But first, if this is not a broadcast to all users, then
15300                // make sure it doesn't conflict with an existing broadcast to
15301                // all users.
15302                ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(
15303                        UserHandle.USER_ALL);
15304                if (stickies != null) {
15305                    ArrayList<Intent> list = stickies.get(intent.getAction());
15306                    if (list != null) {
15307                        int N = list.size();
15308                        int i;
15309                        for (i=0; i<N; i++) {
15310                            if (intent.filterEquals(list.get(i))) {
15311                                throw new IllegalArgumentException(
15312                                        "Sticky broadcast " + intent + " for user "
15313                                        + userId + " conflicts with existing global broadcast");
15314                            }
15315                        }
15316                    }
15317                }
15318            }
15319            ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId);
15320            if (stickies == null) {
15321                stickies = new ArrayMap<String, ArrayList<Intent>>();
15322                mStickyBroadcasts.put(userId, stickies);
15323            }
15324            ArrayList<Intent> list = stickies.get(intent.getAction());
15325            if (list == null) {
15326                list = new ArrayList<Intent>();
15327                stickies.put(intent.getAction(), list);
15328            }
15329            int N = list.size();
15330            int i;
15331            for (i=0; i<N; i++) {
15332                if (intent.filterEquals(list.get(i))) {
15333                    // This sticky already exists, replace it.
15334                    list.set(i, new Intent(intent));
15335                    break;
15336                }
15337            }
15338            if (i >= N) {
15339                list.add(new Intent(intent));
15340            }
15341        }
15342
15343        int[] users;
15344        if (userId == UserHandle.USER_ALL) {
15345            // Caller wants broadcast to go to all started users.
15346            users = mStartedUserArray;
15347        } else {
15348            // Caller wants broadcast to go to one specific user.
15349            users = new int[] {userId};
15350        }
15351
15352        // Figure out who all will receive this broadcast.
15353        List receivers = null;
15354        List<BroadcastFilter> registeredReceivers = null;
15355        // Need to resolve the intent to interested receivers...
15356        if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
15357                 == 0) {
15358            receivers = collectReceiverComponents(intent, resolvedType, users);
15359        }
15360        if (intent.getComponent() == null) {
15361            registeredReceivers = mReceiverResolver.queryIntent(intent,
15362                    resolvedType, false, userId);
15363        }
15364
15365        final boolean replacePending =
15366                (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;
15367
15368        if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()
15369                + " replacePending=" + replacePending);
15370
15371        int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
15372        if (!ordered && NR > 0) {
15373            // If we are not serializing this broadcast, then send the
15374            // registered receivers separately so they don't wait for the
15375            // components to be launched.
15376            final BroadcastQueue queue = broadcastQueueForIntent(intent);
15377            BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
15378                    callerPackage, callingPid, callingUid, resolvedType, requiredPermission,
15379                    appOp, registeredReceivers, resultTo, resultCode, resultData, map,
15380                    ordered, sticky, false, userId);
15381            if (DEBUG_BROADCAST) Slog.v(
15382                    TAG, "Enqueueing parallel broadcast " + r);
15383            final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r);
15384            if (!replaced) {
15385                queue.enqueueParallelBroadcastLocked(r);
15386                queue.scheduleBroadcastsLocked();
15387            }
15388            registeredReceivers = null;
15389            NR = 0;
15390        }
15391
15392        // Merge into one list.
15393        int ir = 0;
15394        if (receivers != null) {
15395            // A special case for PACKAGE_ADDED: do not allow the package
15396            // being added to see this broadcast.  This prevents them from
15397            // using this as a back door to get run as soon as they are
15398            // installed.  Maybe in the future we want to have a special install
15399            // broadcast or such for apps, but we'd like to deliberately make
15400            // this decision.
15401            String skipPackages[] = null;
15402            if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())
15403                    || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())
15404                    || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
15405                Uri data = intent.getData();
15406                if (data != null) {
15407                    String pkgName = data.getSchemeSpecificPart();
15408                    if (pkgName != null) {
15409                        skipPackages = new String[] { pkgName };
15410                    }
15411                }
15412            } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {
15413                skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
15414            }
15415            if (skipPackages != null && (skipPackages.length > 0)) {
15416                for (String skipPackage : skipPackages) {
15417                    if (skipPackage != null) {
15418                        int NT = receivers.size();
15419                        for (int it=0; it<NT; it++) {
15420                            ResolveInfo curt = (ResolveInfo)receivers.get(it);
15421                            if (curt.activityInfo.packageName.equals(skipPackage)) {
15422                                receivers.remove(it);
15423                                it--;
15424                                NT--;
15425                            }
15426                        }
15427                    }
15428                }
15429            }
15430
15431            int NT = receivers != null ? receivers.size() : 0;
15432            int it = 0;
15433            ResolveInfo curt = null;
15434            BroadcastFilter curr = null;
15435            while (it < NT && ir < NR) {
15436                if (curt == null) {
15437                    curt = (ResolveInfo)receivers.get(it);
15438                }
15439                if (curr == null) {
15440                    curr = registeredReceivers.get(ir);
15441                }
15442                if (curr.getPriority() >= curt.priority) {
15443                    // Insert this broadcast record into the final list.
15444                    receivers.add(it, curr);
15445                    ir++;
15446                    curr = null;
15447                    it++;
15448                    NT++;
15449                } else {
15450                    // Skip to the next ResolveInfo in the final list.
15451                    it++;
15452                    curt = null;
15453                }
15454            }
15455        }
15456        while (ir < NR) {
15457            if (receivers == null) {
15458                receivers = new ArrayList();
15459            }
15460            receivers.add(registeredReceivers.get(ir));
15461            ir++;
15462        }
15463
15464        if ((receivers != null && receivers.size() > 0)
15465                || resultTo != null) {
15466            BroadcastQueue queue = broadcastQueueForIntent(intent);
15467            BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
15468                    callerPackage, callingPid, callingUid, resolvedType,
15469                    requiredPermission, appOp, receivers, resultTo, resultCode,
15470                    resultData, map, ordered, sticky, false, userId);
15471            if (DEBUG_BROADCAST) Slog.v(
15472                    TAG, "Enqueueing ordered broadcast " + r
15473                    + ": prev had " + queue.mOrderedBroadcasts.size());
15474            if (DEBUG_BROADCAST) {
15475                int seq = r.intent.getIntExtra("seq", -1);
15476                Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);
15477            }
15478            boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r);
15479            if (!replaced) {
15480                queue.enqueueOrderedBroadcastLocked(r);
15481                queue.scheduleBroadcastsLocked();
15482            }
15483        }
15484
15485        return ActivityManager.BROADCAST_SUCCESS;
15486    }
15487
15488    final Intent verifyBroadcastLocked(Intent intent) {
15489        // Refuse possible leaked file descriptors
15490        if (intent != null && intent.hasFileDescriptors() == true) {
15491            throw new IllegalArgumentException("File descriptors passed in Intent");
15492        }
15493
15494        int flags = intent.getFlags();
15495
15496        if (!mProcessesReady) {
15497            // if the caller really truly claims to know what they're doing, go
15498            // ahead and allow the broadcast without launching any receivers
15499            if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) {
15500                intent = new Intent(intent);
15501                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
15502            } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
15503                Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent
15504                        + " before boot completion");
15505                throw new IllegalStateException("Cannot broadcast before boot completed");
15506            }
15507        }
15508
15509        if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
15510            throw new IllegalArgumentException(
15511                    "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
15512        }
15513
15514        return intent;
15515    }
15516
15517    public final int broadcastIntent(IApplicationThread caller,
15518            Intent intent, String resolvedType, IIntentReceiver resultTo,
15519            int resultCode, String resultData, Bundle map,
15520            String requiredPermission, int appOp, boolean serialized, boolean sticky, int userId) {
15521        enforceNotIsolatedCaller("broadcastIntent");
15522        synchronized(this) {
15523            intent = verifyBroadcastLocked(intent);
15524
15525            final ProcessRecord callerApp = getRecordForAppLocked(caller);
15526            final int callingPid = Binder.getCallingPid();
15527            final int callingUid = Binder.getCallingUid();
15528            final long origId = Binder.clearCallingIdentity();
15529            int res = broadcastIntentLocked(callerApp,
15530                    callerApp != null ? callerApp.info.packageName : null,
15531                    intent, resolvedType, resultTo,
15532                    resultCode, resultData, map, requiredPermission, appOp, serialized, sticky,
15533                    callingPid, callingUid, userId);
15534            Binder.restoreCallingIdentity(origId);
15535            return res;
15536        }
15537    }
15538
15539    int broadcastIntentInPackage(String packageName, int uid,
15540            Intent intent, String resolvedType, IIntentReceiver resultTo,
15541            int resultCode, String resultData, Bundle map,
15542            String requiredPermission, boolean serialized, boolean sticky, int userId) {
15543        synchronized(this) {
15544            intent = verifyBroadcastLocked(intent);
15545
15546            final long origId = Binder.clearCallingIdentity();
15547            int res = broadcastIntentLocked(null, packageName, intent, resolvedType,
15548                    resultTo, resultCode, resultData, map, requiredPermission,
15549                    AppOpsManager.OP_NONE, serialized, sticky, -1, uid, userId);
15550            Binder.restoreCallingIdentity(origId);
15551            return res;
15552        }
15553    }
15554
15555    public final void unbroadcastIntent(IApplicationThread caller, Intent intent, int userId) {
15556        // Refuse possible leaked file descriptors
15557        if (intent != null && intent.hasFileDescriptors() == true) {
15558            throw new IllegalArgumentException("File descriptors passed in Intent");
15559        }
15560
15561        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
15562                userId, true, ALLOW_NON_FULL, "removeStickyBroadcast", null);
15563
15564        synchronized(this) {
15565            if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY)
15566                    != PackageManager.PERMISSION_GRANTED) {
15567                String msg = "Permission Denial: unbroadcastIntent() from pid="
15568                        + Binder.getCallingPid()
15569                        + ", uid=" + Binder.getCallingUid()
15570                        + " requires " + android.Manifest.permission.BROADCAST_STICKY;
15571                Slog.w(TAG, msg);
15572                throw new SecurityException(msg);
15573            }
15574            ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId);
15575            if (stickies != null) {
15576                ArrayList<Intent> list = stickies.get(intent.getAction());
15577                if (list != null) {
15578                    int N = list.size();
15579                    int i;
15580                    for (i=0; i<N; i++) {
15581                        if (intent.filterEquals(list.get(i))) {
15582                            list.remove(i);
15583                            break;
15584                        }
15585                    }
15586                    if (list.size() <= 0) {
15587                        stickies.remove(intent.getAction());
15588                    }
15589                }
15590                if (stickies.size() <= 0) {
15591                    mStickyBroadcasts.remove(userId);
15592                }
15593            }
15594        }
15595    }
15596
15597    private final boolean finishReceiverLocked(IBinder receiver, int resultCode,
15598            String resultData, Bundle resultExtras, boolean resultAbort) {
15599        final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver);
15600        if (r == null) {
15601            Slog.w(TAG, "finishReceiver called but not found on queue");
15602            return false;
15603        }
15604
15605        return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, false);
15606    }
15607
15608    void backgroundServicesFinishedLocked(int userId) {
15609        for (BroadcastQueue queue : mBroadcastQueues) {
15610            queue.backgroundServicesFinishedLocked(userId);
15611        }
15612    }
15613
15614    public void finishReceiver(IBinder who, int resultCode, String resultData,
15615            Bundle resultExtras, boolean resultAbort) {
15616        if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who);
15617
15618        // Refuse possible leaked file descriptors
15619        if (resultExtras != null && resultExtras.hasFileDescriptors()) {
15620            throw new IllegalArgumentException("File descriptors passed in Bundle");
15621        }
15622
15623        final long origId = Binder.clearCallingIdentity();
15624        try {
15625            boolean doNext = false;
15626            BroadcastRecord r;
15627
15628            synchronized(this) {
15629                r = broadcastRecordForReceiverLocked(who);
15630                if (r != null) {
15631                    doNext = r.queue.finishReceiverLocked(r, resultCode,
15632                        resultData, resultExtras, resultAbort, true);
15633                }
15634            }
15635
15636            if (doNext) {
15637                r.queue.processNextBroadcast(false);
15638            }
15639            trimApplications();
15640        } finally {
15641            Binder.restoreCallingIdentity(origId);
15642        }
15643    }
15644
15645    // =========================================================
15646    // INSTRUMENTATION
15647    // =========================================================
15648
15649    public boolean startInstrumentation(ComponentName className,
15650            String profileFile, int flags, Bundle arguments,
15651            IInstrumentationWatcher watcher, IUiAutomationConnection uiAutomationConnection,
15652            int userId, String abiOverride) {
15653        enforceNotIsolatedCaller("startInstrumentation");
15654        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
15655                userId, false, ALLOW_FULL_ONLY, "startInstrumentation", null);
15656        // Refuse possible leaked file descriptors
15657        if (arguments != null && arguments.hasFileDescriptors()) {
15658            throw new IllegalArgumentException("File descriptors passed in Bundle");
15659        }
15660
15661        synchronized(this) {
15662            InstrumentationInfo ii = null;
15663            ApplicationInfo ai = null;
15664            try {
15665                ii = mContext.getPackageManager().getInstrumentationInfo(
15666                    className, STOCK_PM_FLAGS);
15667                ai = AppGlobals.getPackageManager().getApplicationInfo(
15668                        ii.targetPackage, STOCK_PM_FLAGS, userId);
15669            } catch (PackageManager.NameNotFoundException e) {
15670            } catch (RemoteException e) {
15671            }
15672            if (ii == null) {
15673                reportStartInstrumentationFailure(watcher, className,
15674                        "Unable to find instrumentation info for: " + className);
15675                return false;
15676            }
15677            if (ai == null) {
15678                reportStartInstrumentationFailure(watcher, className,
15679                        "Unable to find instrumentation target package: " + ii.targetPackage);
15680                return false;
15681            }
15682
15683            int match = mContext.getPackageManager().checkSignatures(
15684                    ii.targetPackage, ii.packageName);
15685            if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) {
15686                String msg = "Permission Denial: starting instrumentation "
15687                        + className + " from pid="
15688                        + Binder.getCallingPid()
15689                        + ", uid=" + Binder.getCallingPid()
15690                        + " not allowed because package " + ii.packageName
15691                        + " does not have a signature matching the target "
15692                        + ii.targetPackage;
15693                reportStartInstrumentationFailure(watcher, className, msg);
15694                throw new SecurityException(msg);
15695            }
15696
15697            final long origId = Binder.clearCallingIdentity();
15698            // Instrumentation can kill and relaunch even persistent processes
15699            forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, false, userId,
15700                    "start instr");
15701            ProcessRecord app = addAppLocked(ai, false, abiOverride);
15702            app.instrumentationClass = className;
15703            app.instrumentationInfo = ai;
15704            app.instrumentationProfileFile = profileFile;
15705            app.instrumentationArguments = arguments;
15706            app.instrumentationWatcher = watcher;
15707            app.instrumentationUiAutomationConnection = uiAutomationConnection;
15708            app.instrumentationResultClass = className;
15709            Binder.restoreCallingIdentity(origId);
15710        }
15711
15712        return true;
15713    }
15714
15715    /**
15716     * Report errors that occur while attempting to start Instrumentation.  Always writes the
15717     * error to the logs, but if somebody is watching, send the report there too.  This enables
15718     * the "am" command to report errors with more information.
15719     *
15720     * @param watcher The IInstrumentationWatcher.  Null if there isn't one.
15721     * @param cn The component name of the instrumentation.
15722     * @param report The error report.
15723     */
15724    private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher,
15725            ComponentName cn, String report) {
15726        Slog.w(TAG, report);
15727        try {
15728            if (watcher != null) {
15729                Bundle results = new Bundle();
15730                results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService");
15731                results.putString("Error", report);
15732                watcher.instrumentationStatus(cn, -1, results);
15733            }
15734        } catch (RemoteException e) {
15735            Slog.w(TAG, e);
15736        }
15737    }
15738
15739    void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) {
15740        if (app.instrumentationWatcher != null) {
15741            try {
15742                // NOTE:  IInstrumentationWatcher *must* be oneway here
15743                app.instrumentationWatcher.instrumentationFinished(
15744                    app.instrumentationClass,
15745                    resultCode,
15746                    results);
15747            } catch (RemoteException e) {
15748            }
15749        }
15750        if (app.instrumentationUiAutomationConnection != null) {
15751            try {
15752                app.instrumentationUiAutomationConnection.shutdown();
15753            } catch (RemoteException re) {
15754                /* ignore */
15755            }
15756            // Only a UiAutomation can set this flag and now that
15757            // it is finished we make sure it is reset to its default.
15758            mUserIsMonkey = false;
15759        }
15760        app.instrumentationWatcher = null;
15761        app.instrumentationUiAutomationConnection = null;
15762        app.instrumentationClass = null;
15763        app.instrumentationInfo = null;
15764        app.instrumentationProfileFile = null;
15765        app.instrumentationArguments = null;
15766
15767        forceStopPackageLocked(app.info.packageName, -1, false, false, true, true, false, app.userId,
15768                "finished inst");
15769    }
15770
15771    public void finishInstrumentation(IApplicationThread target,
15772            int resultCode, Bundle results) {
15773        int userId = UserHandle.getCallingUserId();
15774        // Refuse possible leaked file descriptors
15775        if (results != null && results.hasFileDescriptors()) {
15776            throw new IllegalArgumentException("File descriptors passed in Intent");
15777        }
15778
15779        synchronized(this) {
15780            ProcessRecord app = getRecordForAppLocked(target);
15781            if (app == null) {
15782                Slog.w(TAG, "finishInstrumentation: no app for " + target);
15783                return;
15784            }
15785            final long origId = Binder.clearCallingIdentity();
15786            finishInstrumentationLocked(app, resultCode, results);
15787            Binder.restoreCallingIdentity(origId);
15788        }
15789    }
15790
15791    // =========================================================
15792    // CONFIGURATION
15793    // =========================================================
15794
15795    public ConfigurationInfo getDeviceConfigurationInfo() {
15796        ConfigurationInfo config = new ConfigurationInfo();
15797        synchronized (this) {
15798            config.reqTouchScreen = mConfiguration.touchscreen;
15799            config.reqKeyboardType = mConfiguration.keyboard;
15800            config.reqNavigation = mConfiguration.navigation;
15801            if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD
15802                    || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) {
15803                config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
15804            }
15805            if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED
15806                    && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) {
15807                config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
15808            }
15809            config.reqGlEsVersion = GL_ES_VERSION;
15810        }
15811        return config;
15812    }
15813
15814    ActivityStack getFocusedStack() {
15815        return mStackSupervisor.getFocusedStack();
15816    }
15817
15818    public Configuration getConfiguration() {
15819        Configuration ci;
15820        synchronized(this) {
15821            ci = new Configuration(mConfiguration);
15822        }
15823        return ci;
15824    }
15825
15826    public void updatePersistentConfiguration(Configuration values) {
15827        enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
15828                "updateConfiguration()");
15829        enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS,
15830                "updateConfiguration()");
15831        if (values == null) {
15832            throw new NullPointerException("Configuration must not be null");
15833        }
15834
15835        synchronized(this) {
15836            final long origId = Binder.clearCallingIdentity();
15837            updateConfigurationLocked(values, null, true, false);
15838            Binder.restoreCallingIdentity(origId);
15839        }
15840    }
15841
15842    public void updateConfiguration(Configuration values) {
15843        enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
15844                "updateConfiguration()");
15845
15846        synchronized(this) {
15847            if (values == null && mWindowManager != null) {
15848                // sentinel: fetch the current configuration from the window manager
15849                values = mWindowManager.computeNewConfiguration();
15850            }
15851
15852            if (mWindowManager != null) {
15853                mProcessList.applyDisplaySize(mWindowManager);
15854            }
15855
15856            final long origId = Binder.clearCallingIdentity();
15857            if (values != null) {
15858                Settings.System.clearConfiguration(values);
15859            }
15860            updateConfigurationLocked(values, null, false, false);
15861            Binder.restoreCallingIdentity(origId);
15862        }
15863    }
15864
15865    /**
15866     * Do either or both things: (1) change the current configuration, and (2)
15867     * make sure the given activity is running with the (now) current
15868     * configuration.  Returns true if the activity has been left running, or
15869     * false if <var>starting</var> is being destroyed to match the new
15870     * configuration.
15871     * @param persistent TODO
15872     */
15873    boolean updateConfigurationLocked(Configuration values,
15874            ActivityRecord starting, boolean persistent, boolean initLocale) {
15875        int changes = 0;
15876
15877        if (values != null) {
15878            Configuration newConfig = new Configuration(mConfiguration);
15879            changes = newConfig.updateFrom(values);
15880            if (changes != 0) {
15881                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
15882                    Slog.i(TAG, "Updating configuration to: " + values);
15883                }
15884
15885                EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
15886
15887                if (values.locale != null && !initLocale) {
15888                    saveLocaleLocked(values.locale,
15889                                     !values.locale.equals(mConfiguration.locale),
15890                                     values.userSetLocale);
15891                }
15892
15893                mConfigurationSeq++;
15894                if (mConfigurationSeq <= 0) {
15895                    mConfigurationSeq = 1;
15896                }
15897                newConfig.seq = mConfigurationSeq;
15898                mConfiguration = newConfig;
15899                Slog.i(TAG, "Config changes=" + Integer.toHexString(changes) + " " + newConfig);
15900                //mUsageStatsService.noteStartConfig(newConfig);
15901
15902                final Configuration configCopy = new Configuration(mConfiguration);
15903
15904                // TODO: If our config changes, should we auto dismiss any currently
15905                // showing dialogs?
15906                mShowDialogs = shouldShowDialogs(newConfig);
15907
15908                AttributeCache ac = AttributeCache.instance();
15909                if (ac != null) {
15910                    ac.updateConfiguration(configCopy);
15911                }
15912
15913                // Make sure all resources in our process are updated
15914                // right now, so that anyone who is going to retrieve
15915                // resource values after we return will be sure to get
15916                // the new ones.  This is especially important during
15917                // boot, where the first config change needs to guarantee
15918                // all resources have that config before following boot
15919                // code is executed.
15920                mSystemThread.applyConfigurationToResources(configCopy);
15921
15922                if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) {
15923                    Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
15924                    msg.obj = new Configuration(configCopy);
15925                    mHandler.sendMessage(msg);
15926                }
15927
15928                for (int i=mLruProcesses.size()-1; i>=0; i--) {
15929                    ProcessRecord app = mLruProcesses.get(i);
15930                    try {
15931                        if (app.thread != null) {
15932                            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
15933                                    + app.processName + " new config " + mConfiguration);
15934                            app.thread.scheduleConfigurationChanged(configCopy);
15935                        }
15936                    } catch (Exception e) {
15937                    }
15938                }
15939                Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
15940                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
15941                        | Intent.FLAG_RECEIVER_REPLACE_PENDING
15942                        | Intent.FLAG_RECEIVER_FOREGROUND);
15943                broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
15944                        null, AppOpsManager.OP_NONE, false, false, MY_PID,
15945                        Process.SYSTEM_UID, UserHandle.USER_ALL);
15946                if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
15947                    intent = new Intent(Intent.ACTION_LOCALE_CHANGED);
15948                    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
15949                    broadcastIntentLocked(null, null, intent,
15950                            null, null, 0, null, null, null, AppOpsManager.OP_NONE,
15951                            false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
15952                }
15953            }
15954        }
15955
15956        boolean kept = true;
15957        final ActivityStack mainStack = mStackSupervisor.getFocusedStack();
15958        // mainStack is null during startup.
15959        if (mainStack != null) {
15960            if (changes != 0 && starting == null) {
15961                // If the configuration changed, and the caller is not already
15962                // in the process of starting an activity, then find the top
15963                // activity to check if its configuration needs to change.
15964                starting = mainStack.topRunningActivityLocked(null);
15965            }
15966
15967            if (starting != null) {
15968                kept = mainStack.ensureActivityConfigurationLocked(starting, changes);
15969                // And we need to make sure at this point that all other activities
15970                // are made visible with the correct configuration.
15971                mStackSupervisor.ensureActivitiesVisibleLocked(starting, changes);
15972            }
15973        }
15974
15975        if (values != null && mWindowManager != null) {
15976            mWindowManager.setNewConfiguration(mConfiguration);
15977        }
15978
15979        return kept;
15980    }
15981
15982    /**
15983     * Decide based on the configuration whether we should shouw the ANR,
15984     * crash, etc dialogs.  The idea is that if there is no affordnace to
15985     * press the on-screen buttons, we shouldn't show the dialog.
15986     *
15987     * A thought: SystemUI might also want to get told about this, the Power
15988     * dialog / global actions also might want different behaviors.
15989     */
15990    private static final boolean shouldShowDialogs(Configuration config) {
15991        return !(config.keyboard == Configuration.KEYBOARD_NOKEYS
15992                && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH);
15993    }
15994
15995    /**
15996     * Save the locale.  You must be inside a synchronized (this) block.
15997     */
15998    private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) {
15999        if(isDiff) {
16000            SystemProperties.set("user.language", l.getLanguage());
16001            SystemProperties.set("user.region", l.getCountry());
16002        }
16003
16004        if(isPersist) {
16005            SystemProperties.set("persist.sys.language", l.getLanguage());
16006            SystemProperties.set("persist.sys.country", l.getCountry());
16007            SystemProperties.set("persist.sys.localevar", l.getVariant());
16008        }
16009    }
16010
16011    @Override
16012    public boolean shouldUpRecreateTask(IBinder token, String destAffinity) {
16013        synchronized (this) {
16014            ActivityRecord srec = ActivityRecord.forToken(token);
16015            if (srec.task != null && srec.task.stack != null) {
16016                return srec.task.stack.shouldUpRecreateTaskLocked(srec, destAffinity);
16017            }
16018        }
16019        return false;
16020    }
16021
16022    public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode,
16023            Intent resultData) {
16024
16025        synchronized (this) {
16026            final ActivityStack stack = ActivityRecord.getStackLocked(token);
16027            if (stack != null) {
16028                return stack.navigateUpToLocked(token, destIntent, resultCode, resultData);
16029            }
16030            return false;
16031        }
16032    }
16033
16034    public int getLaunchedFromUid(IBinder activityToken) {
16035        ActivityRecord srec = ActivityRecord.forToken(activityToken);
16036        if (srec == null) {
16037            return -1;
16038        }
16039        return srec.launchedFromUid;
16040    }
16041
16042    public String getLaunchedFromPackage(IBinder activityToken) {
16043        ActivityRecord srec = ActivityRecord.forToken(activityToken);
16044        if (srec == null) {
16045            return null;
16046        }
16047        return srec.launchedFromPackage;
16048    }
16049
16050    // =========================================================
16051    // LIFETIME MANAGEMENT
16052    // =========================================================
16053
16054    // Returns which broadcast queue the app is the current [or imminent] receiver
16055    // on, or 'null' if the app is not an active broadcast recipient.
16056    private BroadcastQueue isReceivingBroadcast(ProcessRecord app) {
16057        BroadcastRecord r = app.curReceiver;
16058        if (r != null) {
16059            return r.queue;
16060        }
16061
16062        // It's not the current receiver, but it might be starting up to become one
16063        synchronized (this) {
16064            for (BroadcastQueue queue : mBroadcastQueues) {
16065                r = queue.mPendingBroadcast;
16066                if (r != null && r.curApp == app) {
16067                    // found it; report which queue it's in
16068                    return queue;
16069                }
16070            }
16071        }
16072
16073        return null;
16074    }
16075
16076    private final int computeOomAdjLocked(ProcessRecord app, int cachedAdj, ProcessRecord TOP_APP,
16077            boolean doingAll, long now) {
16078        if (mAdjSeq == app.adjSeq) {
16079            // This adjustment has already been computed.
16080            return app.curRawAdj;
16081        }
16082
16083        if (app.thread == null) {
16084            app.adjSeq = mAdjSeq;
16085            app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
16086            app.curProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
16087            return (app.curAdj=app.curRawAdj=ProcessList.CACHED_APP_MAX_ADJ);
16088        }
16089
16090        app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN;
16091        app.adjSource = null;
16092        app.adjTarget = null;
16093        app.empty = false;
16094        app.cached = false;
16095
16096        final int activitiesSize = app.activities.size();
16097
16098        if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) {
16099            // The max adjustment doesn't allow this app to be anything
16100            // below foreground, so it is not worth doing work for it.
16101            app.adjType = "fixed";
16102            app.adjSeq = mAdjSeq;
16103            app.curRawAdj = app.maxAdj;
16104            app.foregroundActivities = false;
16105            app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
16106            app.curProcState = ActivityManager.PROCESS_STATE_PERSISTENT;
16107            // System processes can do UI, and when they do we want to have
16108            // them trim their memory after the user leaves the UI.  To
16109            // facilitate this, here we need to determine whether or not it
16110            // is currently showing UI.
16111            app.systemNoUi = true;
16112            if (app == TOP_APP) {
16113                app.systemNoUi = false;
16114            } else if (activitiesSize > 0) {
16115                for (int j = 0; j < activitiesSize; j++) {
16116                    final ActivityRecord r = app.activities.get(j);
16117                    if (r.visible) {
16118                        app.systemNoUi = false;
16119                    }
16120                }
16121            }
16122            if (!app.systemNoUi) {
16123                app.curProcState = ActivityManager.PROCESS_STATE_PERSISTENT_UI;
16124            }
16125            return (app.curAdj=app.maxAdj);
16126        }
16127
16128        app.systemNoUi = false;
16129
16130        // Determine the importance of the process, starting with most
16131        // important to least, and assign an appropriate OOM adjustment.
16132        int adj;
16133        int schedGroup;
16134        int procState;
16135        boolean foregroundActivities = false;
16136        BroadcastQueue queue;
16137        if (app == TOP_APP) {
16138            // The last app on the list is the foreground app.
16139            adj = ProcessList.FOREGROUND_APP_ADJ;
16140            schedGroup = Process.THREAD_GROUP_DEFAULT;
16141            app.adjType = "top-activity";
16142            foregroundActivities = true;
16143            procState = ActivityManager.PROCESS_STATE_TOP;
16144        } else if (app.instrumentationClass != null) {
16145            // Don't want to kill running instrumentation.
16146            adj = ProcessList.FOREGROUND_APP_ADJ;
16147            schedGroup = Process.THREAD_GROUP_DEFAULT;
16148            app.adjType = "instrumentation";
16149            procState = ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
16150        } else if ((queue = isReceivingBroadcast(app)) != null) {
16151            // An app that is currently receiving a broadcast also
16152            // counts as being in the foreground for OOM killer purposes.
16153            // It's placed in a sched group based on the nature of the
16154            // broadcast as reflected by which queue it's active in.
16155            adj = ProcessList.FOREGROUND_APP_ADJ;
16156            schedGroup = (queue == mFgBroadcastQueue)
16157                    ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE;
16158            app.adjType = "broadcast";
16159            procState = ActivityManager.PROCESS_STATE_RECEIVER;
16160        } else if (app.executingServices.size() > 0) {
16161            // An app that is currently executing a service callback also
16162            // counts as being in the foreground.
16163            adj = ProcessList.FOREGROUND_APP_ADJ;
16164            schedGroup = app.execServicesFg ?
16165                    Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE;
16166            app.adjType = "exec-service";
16167            procState = ActivityManager.PROCESS_STATE_SERVICE;
16168            //Slog.i(TAG, "EXEC " + (app.execServicesFg ? "FG" : "BG") + ": " + app);
16169        } else {
16170            // As far as we know the process is empty.  We may change our mind later.
16171            schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
16172            // At this point we don't actually know the adjustment.  Use the cached adj
16173            // value that the caller wants us to.
16174            adj = cachedAdj;
16175            procState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
16176            app.cached = true;
16177            app.empty = true;
16178            app.adjType = "cch-empty";
16179        }
16180
16181        // Examine all activities if not already foreground.
16182        if (!foregroundActivities && activitiesSize > 0) {
16183            for (int j = 0; j < activitiesSize; j++) {
16184                final ActivityRecord r = app.activities.get(j);
16185                if (r.app != app) {
16186                    Slog.w(TAG, "Wtf, activity " + r + " in proc activity list not using proc "
16187                            + app + "?!?");
16188                    continue;
16189                }
16190                if (r.visible) {
16191                    // App has a visible activity; only upgrade adjustment.
16192                    if (adj > ProcessList.VISIBLE_APP_ADJ) {
16193                        adj = ProcessList.VISIBLE_APP_ADJ;
16194                        app.adjType = "visible";
16195                    }
16196                    if (procState > ActivityManager.PROCESS_STATE_TOP) {
16197                        procState = ActivityManager.PROCESS_STATE_TOP;
16198                    }
16199                    schedGroup = Process.THREAD_GROUP_DEFAULT;
16200                    app.cached = false;
16201                    app.empty = false;
16202                    foregroundActivities = true;
16203                    break;
16204                } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) {
16205                    if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
16206                        adj = ProcessList.PERCEPTIBLE_APP_ADJ;
16207                        app.adjType = "pausing";
16208                    }
16209                    if (procState > ActivityManager.PROCESS_STATE_TOP) {
16210                        procState = ActivityManager.PROCESS_STATE_TOP;
16211                    }
16212                    schedGroup = Process.THREAD_GROUP_DEFAULT;
16213                    app.cached = false;
16214                    app.empty = false;
16215                    foregroundActivities = true;
16216                } else if (r.state == ActivityState.STOPPING) {
16217                    if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
16218                        adj = ProcessList.PERCEPTIBLE_APP_ADJ;
16219                        app.adjType = "stopping";
16220                    }
16221                    // For the process state, we will at this point consider the
16222                    // process to be cached.  It will be cached either as an activity
16223                    // or empty depending on whether the activity is finishing.  We do
16224                    // this so that we can treat the process as cached for purposes of
16225                    // memory trimming (determing current memory level, trim command to
16226                    // send to process) since there can be an arbitrary number of stopping
16227                    // processes and they should soon all go into the cached state.
16228                    if (!r.finishing) {
16229                        if (procState > ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) {
16230                            procState = ActivityManager.PROCESS_STATE_CACHED_ACTIVITY;
16231                        }
16232                    }
16233                    app.cached = false;
16234                    app.empty = false;
16235                    foregroundActivities = true;
16236                } else {
16237                    if (procState > ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) {
16238                        procState = ActivityManager.PROCESS_STATE_CACHED_ACTIVITY;
16239                        app.adjType = "cch-act";
16240                    }
16241                }
16242            }
16243        }
16244
16245        if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
16246            if (app.foregroundServices) {
16247                // The user is aware of this app, so make it visible.
16248                adj = ProcessList.PERCEPTIBLE_APP_ADJ;
16249                procState = ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
16250                app.cached = false;
16251                app.adjType = "fg-service";
16252                schedGroup = Process.THREAD_GROUP_DEFAULT;
16253            } else if (app.forcingToForeground != null) {
16254                // The user is aware of this app, so make it visible.
16255                adj = ProcessList.PERCEPTIBLE_APP_ADJ;
16256                procState = ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
16257                app.cached = false;
16258                app.adjType = "force-fg";
16259                app.adjSource = app.forcingToForeground;
16260                schedGroup = Process.THREAD_GROUP_DEFAULT;
16261            }
16262        }
16263
16264        if (app == mHeavyWeightProcess) {
16265            if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ) {
16266                // We don't want to kill the current heavy-weight process.
16267                adj = ProcessList.HEAVY_WEIGHT_APP_ADJ;
16268                schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
16269                app.cached = false;
16270                app.adjType = "heavy";
16271            }
16272            if (procState > ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
16273                procState = ActivityManager.PROCESS_STATE_HEAVY_WEIGHT;
16274            }
16275        }
16276
16277        if (app == mHomeProcess) {
16278            if (adj > ProcessList.HOME_APP_ADJ) {
16279                // This process is hosting what we currently consider to be the
16280                // home app, so we don't want to let it go into the background.
16281                adj = ProcessList.HOME_APP_ADJ;
16282                schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
16283                app.cached = false;
16284                app.adjType = "home";
16285            }
16286            if (procState > ActivityManager.PROCESS_STATE_HOME) {
16287                procState = ActivityManager.PROCESS_STATE_HOME;
16288            }
16289        }
16290
16291        if (app == mPreviousProcess && app.activities.size() > 0) {
16292            if (adj > ProcessList.PREVIOUS_APP_ADJ) {
16293                // This was the previous process that showed UI to the user.
16294                // We want to try to keep it around more aggressively, to give
16295                // a good experience around switching between two apps.
16296                adj = ProcessList.PREVIOUS_APP_ADJ;
16297                schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
16298                app.cached = false;
16299                app.adjType = "previous";
16300            }
16301            if (procState > ActivityManager.PROCESS_STATE_LAST_ACTIVITY) {
16302                procState = ActivityManager.PROCESS_STATE_LAST_ACTIVITY;
16303            }
16304        }
16305
16306        if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj
16307                + " reason=" + app.adjType);
16308
16309        // By default, we use the computed adjustment.  It may be changed if
16310        // there are applications dependent on our services or providers, but
16311        // this gives us a baseline and makes sure we don't get into an
16312        // infinite recursion.
16313        app.adjSeq = mAdjSeq;
16314        app.curRawAdj = adj;
16315        app.hasStartedServices = false;
16316
16317        if (mBackupTarget != null && app == mBackupTarget.app) {
16318            // If possible we want to avoid killing apps while they're being backed up
16319            if (adj > ProcessList.BACKUP_APP_ADJ) {
16320                if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app);
16321                adj = ProcessList.BACKUP_APP_ADJ;
16322                if (procState > ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND) {
16323                    procState = ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND;
16324                }
16325                app.adjType = "backup";
16326                app.cached = false;
16327            }
16328            if (procState > ActivityManager.PROCESS_STATE_BACKUP) {
16329                procState = ActivityManager.PROCESS_STATE_BACKUP;
16330            }
16331        }
16332
16333        boolean mayBeTop = false;
16334
16335        for (int is = app.services.size()-1;
16336                is >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
16337                        || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE
16338                        || procState > ActivityManager.PROCESS_STATE_TOP);
16339                is--) {
16340            ServiceRecord s = app.services.valueAt(is);
16341            if (s.startRequested) {
16342                app.hasStartedServices = true;
16343                if (procState > ActivityManager.PROCESS_STATE_SERVICE) {
16344                    procState = ActivityManager.PROCESS_STATE_SERVICE;
16345                }
16346                if (app.hasShownUi && app != mHomeProcess) {
16347                    // If this process has shown some UI, let it immediately
16348                    // go to the LRU list because it may be pretty heavy with
16349                    // UI stuff.  We'll tag it with a label just to help
16350                    // debug and understand what is going on.
16351                    if (adj > ProcessList.SERVICE_ADJ) {
16352                        app.adjType = "cch-started-ui-services";
16353                    }
16354                } else {
16355                    if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) {
16356                        // This service has seen some activity within
16357                        // recent memory, so we will keep its process ahead
16358                        // of the background processes.
16359                        if (adj > ProcessList.SERVICE_ADJ) {
16360                            adj = ProcessList.SERVICE_ADJ;
16361                            app.adjType = "started-services";
16362                            app.cached = false;
16363                        }
16364                    }
16365                    // If we have let the service slide into the background
16366                    // state, still have some text describing what it is doing
16367                    // even though the service no longer has an impact.
16368                    if (adj > ProcessList.SERVICE_ADJ) {
16369                        app.adjType = "cch-started-services";
16370                    }
16371                }
16372            }
16373            for (int conni = s.connections.size()-1;
16374                    conni >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
16375                            || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE
16376                            || procState > ActivityManager.PROCESS_STATE_TOP);
16377                    conni--) {
16378                ArrayList<ConnectionRecord> clist = s.connections.valueAt(conni);
16379                for (int i = 0;
16380                        i < clist.size() && (adj > ProcessList.FOREGROUND_APP_ADJ
16381                                || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE
16382                                || procState > ActivityManager.PROCESS_STATE_TOP);
16383                        i++) {
16384                    // XXX should compute this based on the max of
16385                    // all connected clients.
16386                    ConnectionRecord cr = clist.get(i);
16387                    if (cr.binding.client == app) {
16388                        // Binding to ourself is not interesting.
16389                        continue;
16390                    }
16391                    if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) {
16392                        ProcessRecord client = cr.binding.client;
16393                        int clientAdj = computeOomAdjLocked(client, cachedAdj,
16394                                TOP_APP, doingAll, now);
16395                        int clientProcState = client.curProcState;
16396                        if (clientProcState >= ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) {
16397                            // If the other app is cached for any reason, for purposes here
16398                            // we are going to consider it empty.  The specific cached state
16399                            // doesn't propagate except under certain conditions.
16400                            clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
16401                        }
16402                        String adjType = null;
16403                        if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) {
16404                            // Not doing bind OOM management, so treat
16405                            // this guy more like a started service.
16406                            if (app.hasShownUi && app != mHomeProcess) {
16407                                // If this process has shown some UI, let it immediately
16408                                // go to the LRU list because it may be pretty heavy with
16409                                // UI stuff.  We'll tag it with a label just to help
16410                                // debug and understand what is going on.
16411                                if (adj > clientAdj) {
16412                                    adjType = "cch-bound-ui-services";
16413                                }
16414                                app.cached = false;
16415                                clientAdj = adj;
16416                                clientProcState = procState;
16417                            } else {
16418                                if (now >= (s.lastActivity
16419                                        + ActiveServices.MAX_SERVICE_INACTIVITY)) {
16420                                    // This service has not seen activity within
16421                                    // recent memory, so allow it to drop to the
16422                                    // LRU list if there is no other reason to keep
16423                                    // it around.  We'll also tag it with a label just
16424                                    // to help debug and undertand what is going on.
16425                                    if (adj > clientAdj) {
16426                                        adjType = "cch-bound-services";
16427                                    }
16428                                    clientAdj = adj;
16429                                }
16430                            }
16431                        }
16432                        if (adj > clientAdj) {
16433                            // If this process has recently shown UI, and
16434                            // the process that is binding to it is less
16435                            // important than being visible, then we don't
16436                            // care about the binding as much as we care
16437                            // about letting this process get into the LRU
16438                            // list to be killed and restarted if needed for
16439                            // memory.
16440                            if (app.hasShownUi && app != mHomeProcess
16441                                    && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
16442                                adjType = "cch-bound-ui-services";
16443                            } else {
16444                                if ((cr.flags&(Context.BIND_ABOVE_CLIENT
16445                                        |Context.BIND_IMPORTANT)) != 0) {
16446                                    adj = clientAdj;
16447                                } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0
16448                                        && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ
16449                                        && adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
16450                                    adj = ProcessList.PERCEPTIBLE_APP_ADJ;
16451                                } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) {
16452                                    adj = clientAdj;
16453                                } else {
16454                                    if (adj > ProcessList.VISIBLE_APP_ADJ) {
16455                                        adj = ProcessList.VISIBLE_APP_ADJ;
16456                                    }
16457                                }
16458                                if (!client.cached) {
16459                                    app.cached = false;
16460                                }
16461                                adjType = "service";
16462                            }
16463                        }
16464                        if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
16465                            if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
16466                                schedGroup = Process.THREAD_GROUP_DEFAULT;
16467                            }
16468                            if (clientProcState <= ActivityManager.PROCESS_STATE_TOP) {
16469                                if (clientProcState == ActivityManager.PROCESS_STATE_TOP) {
16470                                    // Special handling of clients who are in the top state.
16471                                    // We *may* want to consider this process to be in the
16472                                    // top state as well, but only if there is not another
16473                                    // reason for it to be running.  Being on the top is a
16474                                    // special state, meaning you are specifically running
16475                                    // for the current top app.  If the process is already
16476                                    // running in the background for some other reason, it
16477                                    // is more important to continue considering it to be
16478                                    // in the background state.
16479                                    mayBeTop = true;
16480                                    clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
16481                                } else {
16482                                    // Special handling for above-top states (persistent
16483                                    // processes).  These should not bring the current process
16484                                    // into the top state, since they are not on top.  Instead
16485                                    // give them the best state after that.
16486                                    clientProcState =
16487                                            ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
16488                                }
16489                            }
16490                        } else {
16491                            if (clientProcState <
16492                                    ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND) {
16493                                clientProcState =
16494                                        ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND;
16495                            }
16496                        }
16497                        if (procState > clientProcState) {
16498                            procState = clientProcState;
16499                        }
16500                        if (procState < ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
16501                                && (cr.flags&Context.BIND_SHOWING_UI) != 0) {
16502                            app.pendingUiClean = true;
16503                        }
16504                        if (adjType != null) {
16505                            app.adjType = adjType;
16506                            app.adjTypeCode = ActivityManager.RunningAppProcessInfo
16507                                    .REASON_SERVICE_IN_USE;
16508                            app.adjSource = cr.binding.client;
16509                            app.adjSourceProcState = clientProcState;
16510                            app.adjTarget = s.name;
16511                        }
16512                    }
16513                    if ((cr.flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
16514                        app.treatLikeActivity = true;
16515                    }
16516                    final ActivityRecord a = cr.activity;
16517                    if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) {
16518                        if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ &&
16519                                (a.visible || a.state == ActivityState.RESUMED
16520                                 || a.state == ActivityState.PAUSING)) {
16521                            adj = ProcessList.FOREGROUND_APP_ADJ;
16522                            if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
16523                                schedGroup = Process.THREAD_GROUP_DEFAULT;
16524                            }
16525                            app.cached = false;
16526                            app.adjType = "service";
16527                            app.adjTypeCode = ActivityManager.RunningAppProcessInfo
16528                                    .REASON_SERVICE_IN_USE;
16529                            app.adjSource = a;
16530                            app.adjSourceProcState = procState;
16531                            app.adjTarget = s.name;
16532                        }
16533                    }
16534                }
16535            }
16536        }
16537
16538        for (int provi = app.pubProviders.size()-1;
16539                provi >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
16540                        || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE
16541                        || procState > ActivityManager.PROCESS_STATE_TOP);
16542                provi--) {
16543            ContentProviderRecord cpr = app.pubProviders.valueAt(provi);
16544            for (int i = cpr.connections.size()-1;
16545                    i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
16546                            || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE
16547                            || procState > ActivityManager.PROCESS_STATE_TOP);
16548                    i--) {
16549                ContentProviderConnection conn = cpr.connections.get(i);
16550                ProcessRecord client = conn.client;
16551                if (client == app) {
16552                    // Being our own client is not interesting.
16553                    continue;
16554                }
16555                int clientAdj = computeOomAdjLocked(client, cachedAdj, TOP_APP, doingAll, now);
16556                int clientProcState = client.curProcState;
16557                if (clientProcState >= ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) {
16558                    // If the other app is cached for any reason, for purposes here
16559                    // we are going to consider it empty.
16560                    clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
16561                }
16562                if (adj > clientAdj) {
16563                    if (app.hasShownUi && app != mHomeProcess
16564                            && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
16565                        app.adjType = "cch-ui-provider";
16566                    } else {
16567                        adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ
16568                                ? clientAdj : ProcessList.FOREGROUND_APP_ADJ;
16569                        app.adjType = "provider";
16570                    }
16571                    app.cached &= client.cached;
16572                    app.adjTypeCode = ActivityManager.RunningAppProcessInfo
16573                            .REASON_PROVIDER_IN_USE;
16574                    app.adjSource = client;
16575                    app.adjSourceProcState = clientProcState;
16576                    app.adjTarget = cpr.name;
16577                }
16578                if (clientProcState <= ActivityManager.PROCESS_STATE_TOP) {
16579                    if (clientProcState == ActivityManager.PROCESS_STATE_TOP) {
16580                        // Special handling of clients who are in the top state.
16581                        // We *may* want to consider this process to be in the
16582                        // top state as well, but only if there is not another
16583                        // reason for it to be running.  Being on the top is a
16584                        // special state, meaning you are specifically running
16585                        // for the current top app.  If the process is already
16586                        // running in the background for some other reason, it
16587                        // is more important to continue considering it to be
16588                        // in the background state.
16589                        mayBeTop = true;
16590                        clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
16591                    } else {
16592                        // Special handling for above-top states (persistent
16593                        // processes).  These should not bring the current process
16594                        // into the top state, since they are not on top.  Instead
16595                        // give them the best state after that.
16596                        clientProcState =
16597                                ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
16598                    }
16599                }
16600                if (procState > clientProcState) {
16601                    procState = clientProcState;
16602                }
16603                if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
16604                    schedGroup = Process.THREAD_GROUP_DEFAULT;
16605                }
16606            }
16607            // If the provider has external (non-framework) process
16608            // dependencies, ensure that its adjustment is at least
16609            // FOREGROUND_APP_ADJ.
16610            if (cpr.hasExternalProcessHandles()) {
16611                if (adj > ProcessList.FOREGROUND_APP_ADJ) {
16612                    adj = ProcessList.FOREGROUND_APP_ADJ;
16613                    schedGroup = Process.THREAD_GROUP_DEFAULT;
16614                    app.cached = false;
16615                    app.adjType = "provider";
16616                    app.adjTarget = cpr.name;
16617                }
16618                if (procState > ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
16619                    procState = ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
16620                }
16621            }
16622        }
16623
16624        if (mayBeTop && procState > ActivityManager.PROCESS_STATE_TOP) {
16625            // A client of one of our services or providers is in the top state.  We
16626            // *may* want to be in the top state, but not if we are already running in
16627            // the background for some other reason.  For the decision here, we are going
16628            // to pick out a few specific states that we want to remain in when a client
16629            // is top (states that tend to be longer-term) and otherwise allow it to go
16630            // to the top state.
16631            switch (procState) {
16632                case ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND:
16633                case ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND:
16634                case ActivityManager.PROCESS_STATE_SERVICE:
16635                    // These all are longer-term states, so pull them up to the top
16636                    // of the background states, but not all the way to the top state.
16637                    procState = ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
16638                    break;
16639                default:
16640                    // Otherwise, top is a better choice, so take it.
16641                    procState = ActivityManager.PROCESS_STATE_TOP;
16642                    break;
16643            }
16644        }
16645
16646        if (procState >= ActivityManager.PROCESS_STATE_CACHED_EMPTY) {
16647            if (app.hasClientActivities) {
16648                // This is a cached process, but with client activities.  Mark it so.
16649                procState = ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT;
16650                app.adjType = "cch-client-act";
16651            } else if (app.treatLikeActivity) {
16652                // This is a cached process, but somebody wants us to treat it like it has
16653                // an activity, okay!
16654                procState = ActivityManager.PROCESS_STATE_CACHED_ACTIVITY;
16655                app.adjType = "cch-as-act";
16656            }
16657        }
16658
16659        if (adj == ProcessList.SERVICE_ADJ) {
16660            if (doingAll) {
16661                app.serviceb = mNewNumAServiceProcs > (mNumServiceProcs/3);
16662                mNewNumServiceProcs++;
16663                //Slog.i(TAG, "ADJ " + app + " serviceb=" + app.serviceb);
16664                if (!app.serviceb) {
16665                    // This service isn't far enough down on the LRU list to
16666                    // normally be a B service, but if we are low on RAM and it
16667                    // is large we want to force it down since we would prefer to
16668                    // keep launcher over it.
16669                    if (mLastMemoryLevel > ProcessStats.ADJ_MEM_FACTOR_NORMAL
16670                            && app.lastPss >= mProcessList.getCachedRestoreThresholdKb()) {
16671                        app.serviceHighRam = true;
16672                        app.serviceb = true;
16673                        //Slog.i(TAG, "ADJ " + app + " high ram!");
16674                    } else {
16675                        mNewNumAServiceProcs++;
16676                        //Slog.i(TAG, "ADJ " + app + " not high ram!");
16677                    }
16678                } else {
16679                    app.serviceHighRam = false;
16680                }
16681            }
16682            if (app.serviceb) {
16683                adj = ProcessList.SERVICE_B_ADJ;
16684            }
16685        }
16686
16687        app.curRawAdj = adj;
16688
16689        //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid +
16690        //      " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj);
16691        if (adj > app.maxAdj) {
16692            adj = app.maxAdj;
16693            if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
16694                schedGroup = Process.THREAD_GROUP_DEFAULT;
16695            }
16696        }
16697
16698        // Do final modification to adj.  Everything we do between here and applying
16699        // the final setAdj must be done in this function, because we will also use
16700        // it when computing the final cached adj later.  Note that we don't need to
16701        // worry about this for max adj above, since max adj will always be used to
16702        // keep it out of the cached vaues.
16703        app.curAdj = app.modifyRawOomAdj(adj);
16704        app.curSchedGroup = schedGroup;
16705        app.curProcState = procState;
16706        app.foregroundActivities = foregroundActivities;
16707
16708        return app.curRawAdj;
16709    }
16710
16711    /**
16712     * Schedule PSS collection of a process.
16713     */
16714    void requestPssLocked(ProcessRecord proc, int procState) {
16715        if (mPendingPssProcesses.contains(proc)) {
16716            return;
16717        }
16718        if (mPendingPssProcesses.size() == 0) {
16719            mBgHandler.sendEmptyMessage(COLLECT_PSS_BG_MSG);
16720        }
16721        if (DEBUG_PSS) Slog.d(TAG, "Requesting PSS of: " + proc);
16722        proc.pssProcState = procState;
16723        mPendingPssProcesses.add(proc);
16724    }
16725
16726    /**
16727     * Schedule PSS collection of all processes.
16728     */
16729    void requestPssAllProcsLocked(long now, boolean always, boolean memLowered) {
16730        if (!always) {
16731            if (now < (mLastFullPssTime +
16732                    (memLowered ? FULL_PSS_LOWERED_INTERVAL : FULL_PSS_MIN_INTERVAL))) {
16733                return;
16734            }
16735        }
16736        if (DEBUG_PSS) Slog.d(TAG, "Requesting PSS of all procs!  memLowered=" + memLowered);
16737        mLastFullPssTime = now;
16738        mFullPssPending = true;
16739        mPendingPssProcesses.ensureCapacity(mLruProcesses.size());
16740        mPendingPssProcesses.clear();
16741        for (int i=mLruProcesses.size()-1; i>=0; i--) {
16742            ProcessRecord app = mLruProcesses.get(i);
16743            if (memLowered || now > (app.lastStateTime+ProcessList.PSS_ALL_INTERVAL)) {
16744                app.pssProcState = app.setProcState;
16745                app.nextPssTime = ProcessList.computeNextPssTime(app.curProcState, true,
16746                        isSleeping(), now);
16747                mPendingPssProcesses.add(app);
16748            }
16749        }
16750        mBgHandler.sendEmptyMessage(COLLECT_PSS_BG_MSG);
16751    }
16752
16753    /**
16754     * Ask a given process to GC right now.
16755     */
16756    final void performAppGcLocked(ProcessRecord app) {
16757        try {
16758            app.lastRequestedGc = SystemClock.uptimeMillis();
16759            if (app.thread != null) {
16760                if (app.reportLowMemory) {
16761                    app.reportLowMemory = false;
16762                    app.thread.scheduleLowMemory();
16763                } else {
16764                    app.thread.processInBackground();
16765                }
16766            }
16767        } catch (Exception e) {
16768            // whatever.
16769        }
16770    }
16771
16772    /**
16773     * Returns true if things are idle enough to perform GCs.
16774     */
16775    private final boolean canGcNowLocked() {
16776        boolean processingBroadcasts = false;
16777        for (BroadcastQueue q : mBroadcastQueues) {
16778            if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) {
16779                processingBroadcasts = true;
16780            }
16781        }
16782        return !processingBroadcasts
16783                && (isSleeping() || mStackSupervisor.allResumedActivitiesIdle());
16784    }
16785
16786    /**
16787     * Perform GCs on all processes that are waiting for it, but only
16788     * if things are idle.
16789     */
16790    final void performAppGcsLocked() {
16791        final int N = mProcessesToGc.size();
16792        if (N <= 0) {
16793            return;
16794        }
16795        if (canGcNowLocked()) {
16796            while (mProcessesToGc.size() > 0) {
16797                ProcessRecord proc = mProcessesToGc.remove(0);
16798                if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) {
16799                    if ((proc.lastRequestedGc+GC_MIN_INTERVAL)
16800                            <= SystemClock.uptimeMillis()) {
16801                        // To avoid spamming the system, we will GC processes one
16802                        // at a time, waiting a few seconds between each.
16803                        performAppGcLocked(proc);
16804                        scheduleAppGcsLocked();
16805                        return;
16806                    } else {
16807                        // It hasn't been long enough since we last GCed this
16808                        // process...  put it in the list to wait for its time.
16809                        addProcessToGcListLocked(proc);
16810                        break;
16811                    }
16812                }
16813            }
16814
16815            scheduleAppGcsLocked();
16816        }
16817    }
16818
16819    /**
16820     * If all looks good, perform GCs on all processes waiting for them.
16821     */
16822    final void performAppGcsIfAppropriateLocked() {
16823        if (canGcNowLocked()) {
16824            performAppGcsLocked();
16825            return;
16826        }
16827        // Still not idle, wait some more.
16828        scheduleAppGcsLocked();
16829    }
16830
16831    /**
16832     * Schedule the execution of all pending app GCs.
16833     */
16834    final void scheduleAppGcsLocked() {
16835        mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG);
16836
16837        if (mProcessesToGc.size() > 0) {
16838            // Schedule a GC for the time to the next process.
16839            ProcessRecord proc = mProcessesToGc.get(0);
16840            Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG);
16841
16842            long when = proc.lastRequestedGc + GC_MIN_INTERVAL;
16843            long now = SystemClock.uptimeMillis();
16844            if (when < (now+GC_TIMEOUT)) {
16845                when = now + GC_TIMEOUT;
16846            }
16847            mHandler.sendMessageAtTime(msg, when);
16848        }
16849    }
16850
16851    /**
16852     * Add a process to the array of processes waiting to be GCed.  Keeps the
16853     * list in sorted order by the last GC time.  The process can't already be
16854     * on the list.
16855     */
16856    final void addProcessToGcListLocked(ProcessRecord proc) {
16857        boolean added = false;
16858        for (int i=mProcessesToGc.size()-1; i>=0; i--) {
16859            if (mProcessesToGc.get(i).lastRequestedGc <
16860                    proc.lastRequestedGc) {
16861                added = true;
16862                mProcessesToGc.add(i+1, proc);
16863                break;
16864            }
16865        }
16866        if (!added) {
16867            mProcessesToGc.add(0, proc);
16868        }
16869    }
16870
16871    /**
16872     * Set up to ask a process to GC itself.  This will either do it
16873     * immediately, or put it on the list of processes to gc the next
16874     * time things are idle.
16875     */
16876    final void scheduleAppGcLocked(ProcessRecord app) {
16877        long now = SystemClock.uptimeMillis();
16878        if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) {
16879            return;
16880        }
16881        if (!mProcessesToGc.contains(app)) {
16882            addProcessToGcListLocked(app);
16883            scheduleAppGcsLocked();
16884        }
16885    }
16886
16887    final void checkExcessivePowerUsageLocked(boolean doKills) {
16888        updateCpuStatsNow();
16889
16890        BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
16891        boolean doWakeKills = doKills;
16892        boolean doCpuKills = doKills;
16893        if (mLastPowerCheckRealtime == 0) {
16894            doWakeKills = false;
16895        }
16896        if (mLastPowerCheckUptime == 0) {
16897            doCpuKills = false;
16898        }
16899        if (stats.isScreenOn()) {
16900            doWakeKills = false;
16901        }
16902        final long curRealtime = SystemClock.elapsedRealtime();
16903        final long realtimeSince = curRealtime - mLastPowerCheckRealtime;
16904        final long curUptime = SystemClock.uptimeMillis();
16905        final long uptimeSince = curUptime - mLastPowerCheckUptime;
16906        mLastPowerCheckRealtime = curRealtime;
16907        mLastPowerCheckUptime = curUptime;
16908        if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) {
16909            doWakeKills = false;
16910        }
16911        if (uptimeSince < CPU_MIN_CHECK_DURATION) {
16912            doCpuKills = false;
16913        }
16914        int i = mLruProcesses.size();
16915        while (i > 0) {
16916            i--;
16917            ProcessRecord app = mLruProcesses.get(i);
16918            if (app.setProcState >= ActivityManager.PROCESS_STATE_HOME) {
16919                long wtime;
16920                synchronized (stats) {
16921                    wtime = stats.getProcessWakeTime(app.info.uid,
16922                            app.pid, curRealtime);
16923                }
16924                long wtimeUsed = wtime - app.lastWakeTime;
16925                long cputimeUsed = app.curCpuTime - app.lastCpuTime;
16926                if (DEBUG_POWER) {
16927                    StringBuilder sb = new StringBuilder(128);
16928                    sb.append("Wake for ");
16929                    app.toShortString(sb);
16930                    sb.append(": over ");
16931                    TimeUtils.formatDuration(realtimeSince, sb);
16932                    sb.append(" used ");
16933                    TimeUtils.formatDuration(wtimeUsed, sb);
16934                    sb.append(" (");
16935                    sb.append((wtimeUsed*100)/realtimeSince);
16936                    sb.append("%)");
16937                    Slog.i(TAG, sb.toString());
16938                    sb.setLength(0);
16939                    sb.append("CPU for ");
16940                    app.toShortString(sb);
16941                    sb.append(": over ");
16942                    TimeUtils.formatDuration(uptimeSince, sb);
16943                    sb.append(" used ");
16944                    TimeUtils.formatDuration(cputimeUsed, sb);
16945                    sb.append(" (");
16946                    sb.append((cputimeUsed*100)/uptimeSince);
16947                    sb.append("%)");
16948                    Slog.i(TAG, sb.toString());
16949                }
16950                // If a process has held a wake lock for more
16951                // than 50% of the time during this period,
16952                // that sounds bad.  Kill!
16953                if (doWakeKills && realtimeSince > 0
16954                        && ((wtimeUsed*100)/realtimeSince) >= 50) {
16955                    synchronized (stats) {
16956                        stats.reportExcessiveWakeLocked(app.info.uid, app.processName,
16957                                realtimeSince, wtimeUsed);
16958                    }
16959                    app.kill("excessive wake held " + wtimeUsed + " during " + realtimeSince, true);
16960                    app.baseProcessTracker.reportExcessiveWake(app.pkgList);
16961                } else if (doCpuKills && uptimeSince > 0
16962                        && ((cputimeUsed*100)/uptimeSince) >= 25) {
16963                    synchronized (stats) {
16964                        stats.reportExcessiveCpuLocked(app.info.uid, app.processName,
16965                                uptimeSince, cputimeUsed);
16966                    }
16967                    app.kill("excessive cpu " + cputimeUsed + " during " + uptimeSince, true);
16968                    app.baseProcessTracker.reportExcessiveCpu(app.pkgList);
16969                } else {
16970                    app.lastWakeTime = wtime;
16971                    app.lastCpuTime = app.curCpuTime;
16972                }
16973            }
16974        }
16975    }
16976
16977    private final boolean applyOomAdjLocked(ProcessRecord app,
16978            ProcessRecord TOP_APP, boolean doingAll, long now) {
16979        boolean success = true;
16980
16981        if (app.curRawAdj != app.setRawAdj) {
16982            app.setRawAdj = app.curRawAdj;
16983        }
16984
16985        int changes = 0;
16986
16987        if (app.curAdj != app.setAdj) {
16988            ProcessList.setOomAdj(app.pid, app.info.uid, app.curAdj);
16989            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
16990                TAG, "Set " + app.pid + " " + app.processName +
16991                " adj " + app.curAdj + ": " + app.adjType);
16992            app.setAdj = app.curAdj;
16993        }
16994
16995        if (app.setSchedGroup != app.curSchedGroup) {
16996            app.setSchedGroup = app.curSchedGroup;
16997            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
16998                    "Setting process group of " + app.processName
16999                    + " to " + app.curSchedGroup);
17000            if (app.waitingToKill != null &&
17001                    app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
17002                app.kill(app.waitingToKill, true);
17003                success = false;
17004            } else {
17005                if (true) {
17006                    long oldId = Binder.clearCallingIdentity();
17007                    try {
17008                        Process.setProcessGroup(app.pid, app.curSchedGroup);
17009                    } catch (Exception e) {
17010                        Slog.w(TAG, "Failed setting process group of " + app.pid
17011                                + " to " + app.curSchedGroup);
17012                        e.printStackTrace();
17013                    } finally {
17014                        Binder.restoreCallingIdentity(oldId);
17015                    }
17016                } else {
17017                    if (app.thread != null) {
17018                        try {
17019                            app.thread.setSchedulingGroup(app.curSchedGroup);
17020                        } catch (RemoteException e) {
17021                        }
17022                    }
17023                }
17024                Process.setSwappiness(app.pid,
17025                        app.curSchedGroup <= Process.THREAD_GROUP_BG_NONINTERACTIVE);
17026            }
17027        }
17028        if (app.repForegroundActivities != app.foregroundActivities) {
17029            app.repForegroundActivities = app.foregroundActivities;
17030            changes |= ProcessChangeItem.CHANGE_ACTIVITIES;
17031        }
17032        if (app.repProcState != app.curProcState) {
17033            app.repProcState = app.curProcState;
17034            changes |= ProcessChangeItem.CHANGE_PROCESS_STATE;
17035            if (app.thread != null) {
17036                try {
17037                    if (false) {
17038                        //RuntimeException h = new RuntimeException("here");
17039                        Slog.i(TAG, "Sending new process state " + app.repProcState
17040                                + " to " + app /*, h*/);
17041                    }
17042                    app.thread.setProcessState(app.repProcState);
17043                } catch (RemoteException e) {
17044                }
17045            }
17046        }
17047        if (app.setProcState < 0 || ProcessList.procStatesDifferForMem(app.curProcState,
17048                app.setProcState)) {
17049            app.lastStateTime = now;
17050            app.nextPssTime = ProcessList.computeNextPssTime(app.curProcState, true,
17051                    isSleeping(), now);
17052            if (DEBUG_PSS) Slog.d(TAG, "Process state change from "
17053                    + ProcessList.makeProcStateString(app.setProcState) + " to "
17054                    + ProcessList.makeProcStateString(app.curProcState) + " next pss in "
17055                    + (app.nextPssTime-now) + ": " + app);
17056        } else {
17057            if (now > app.nextPssTime || (now > (app.lastPssTime+ProcessList.PSS_MAX_INTERVAL)
17058                    && now > (app.lastStateTime+ProcessList.PSS_MIN_TIME_FROM_STATE_CHANGE))) {
17059                requestPssLocked(app, app.setProcState);
17060                app.nextPssTime = ProcessList.computeNextPssTime(app.curProcState, false,
17061                        isSleeping(), now);
17062            } else if (false && DEBUG_PSS) {
17063                Slog.d(TAG, "Not requesting PSS of " + app + ": next=" + (app.nextPssTime-now));
17064            }
17065        }
17066        if (app.setProcState != app.curProcState) {
17067            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
17068                    "Proc state change of " + app.processName
17069                    + " to " + app.curProcState);
17070            boolean setImportant = app.setProcState < ActivityManager.PROCESS_STATE_SERVICE;
17071            boolean curImportant = app.curProcState < ActivityManager.PROCESS_STATE_SERVICE;
17072            if (setImportant && !curImportant) {
17073                // This app is no longer something we consider important enough to allow to
17074                // use arbitrary amounts of battery power.  Note
17075                // its current wake lock time to later know to kill it if
17076                // it is not behaving well.
17077                BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
17078                synchronized (stats) {
17079                    app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
17080                            app.pid, SystemClock.elapsedRealtime());
17081                }
17082                app.lastCpuTime = app.curCpuTime;
17083
17084            }
17085            app.setProcState = app.curProcState;
17086            if (app.setProcState >= ActivityManager.PROCESS_STATE_HOME) {
17087                app.notCachedSinceIdle = false;
17088            }
17089            if (!doingAll) {
17090                setProcessTrackerStateLocked(app, mProcessStats.getMemFactorLocked(), now);
17091            } else {
17092                app.procStateChanged = true;
17093            }
17094        }
17095
17096        if (changes != 0) {
17097            if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes);
17098            int i = mPendingProcessChanges.size()-1;
17099            ProcessChangeItem item = null;
17100            while (i >= 0) {
17101                item = mPendingProcessChanges.get(i);
17102                if (item.pid == app.pid) {
17103                    if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item);
17104                    break;
17105                }
17106                i--;
17107            }
17108            if (i < 0) {
17109                // No existing item in pending changes; need a new one.
17110                final int NA = mAvailProcessChanges.size();
17111                if (NA > 0) {
17112                    item = mAvailProcessChanges.remove(NA-1);
17113                    if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item);
17114                } else {
17115                    item = new ProcessChangeItem();
17116                    if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item);
17117                }
17118                item.changes = 0;
17119                item.pid = app.pid;
17120                item.uid = app.info.uid;
17121                if (mPendingProcessChanges.size() == 0) {
17122                    if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG,
17123                            "*** Enqueueing dispatch processes changed!");
17124                    mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget();
17125                }
17126                mPendingProcessChanges.add(item);
17127            }
17128            item.changes |= changes;
17129            item.processState = app.repProcState;
17130            item.foregroundActivities = app.repForegroundActivities;
17131            if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item "
17132                    + Integer.toHexString(System.identityHashCode(item))
17133                    + " " + app.toShortString() + ": changes=" + item.changes
17134                    + " procState=" + item.processState
17135                    + " foreground=" + item.foregroundActivities
17136                    + " type=" + app.adjType + " source=" + app.adjSource
17137                    + " target=" + app.adjTarget);
17138        }
17139
17140        return success;
17141    }
17142
17143    private final void setProcessTrackerStateLocked(ProcessRecord proc, int memFactor, long now) {
17144        if (proc.thread != null) {
17145            if (proc.baseProcessTracker != null) {
17146                proc.baseProcessTracker.setState(proc.repProcState, memFactor, now, proc.pkgList);
17147            }
17148            if (proc.repProcState >= 0) {
17149                mBatteryStatsService.noteProcessState(proc.processName, proc.info.uid,
17150                        proc.repProcState);
17151            }
17152        }
17153    }
17154
17155    private final boolean updateOomAdjLocked(ProcessRecord app, int cachedAdj,
17156            ProcessRecord TOP_APP, boolean doingAll, long now) {
17157        if (app.thread == null) {
17158            return false;
17159        }
17160
17161        computeOomAdjLocked(app, cachedAdj, TOP_APP, doingAll, now);
17162
17163        return applyOomAdjLocked(app, TOP_APP, doingAll, now);
17164    }
17165
17166    final void updateProcessForegroundLocked(ProcessRecord proc, boolean isForeground,
17167            boolean oomAdj) {
17168        if (isForeground != proc.foregroundServices) {
17169            proc.foregroundServices = isForeground;
17170            ArrayList<ProcessRecord> curProcs = mForegroundPackages.get(proc.info.packageName,
17171                    proc.info.uid);
17172            if (isForeground) {
17173                if (curProcs == null) {
17174                    curProcs = new ArrayList<ProcessRecord>();
17175                    mForegroundPackages.put(proc.info.packageName, proc.info.uid, curProcs);
17176                }
17177                if (!curProcs.contains(proc)) {
17178                    curProcs.add(proc);
17179                    mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_FOREGROUND_START,
17180                            proc.info.packageName, proc.info.uid);
17181                }
17182            } else {
17183                if (curProcs != null) {
17184                    if (curProcs.remove(proc)) {
17185                        mBatteryStatsService.noteEvent(
17186                                BatteryStats.HistoryItem.EVENT_FOREGROUND_FINISH,
17187                                proc.info.packageName, proc.info.uid);
17188                        if (curProcs.size() <= 0) {
17189                            mForegroundPackages.remove(proc.info.packageName, proc.info.uid);
17190                        }
17191                    }
17192                }
17193            }
17194            if (oomAdj) {
17195                updateOomAdjLocked();
17196            }
17197        }
17198    }
17199
17200    private final ActivityRecord resumedAppLocked() {
17201        ActivityRecord act = mStackSupervisor.resumedAppLocked();
17202        String pkg;
17203        int uid;
17204        if (act != null) {
17205            pkg = act.packageName;
17206            uid = act.info.applicationInfo.uid;
17207        } else {
17208            pkg = null;
17209            uid = -1;
17210        }
17211        // Has the UID or resumed package name changed?
17212        if (uid != mCurResumedUid || (pkg != mCurResumedPackage
17213                && (pkg == null || !pkg.equals(mCurResumedPackage)))) {
17214            if (mCurResumedPackage != null) {
17215                mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_TOP_FINISH,
17216                        mCurResumedPackage, mCurResumedUid);
17217            }
17218            mCurResumedPackage = pkg;
17219            mCurResumedUid = uid;
17220            if (mCurResumedPackage != null) {
17221                mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_TOP_START,
17222                        mCurResumedPackage, mCurResumedUid);
17223            }
17224        }
17225        return act;
17226    }
17227
17228    final boolean updateOomAdjLocked(ProcessRecord app) {
17229        final ActivityRecord TOP_ACT = resumedAppLocked();
17230        final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
17231        final boolean wasCached = app.cached;
17232
17233        mAdjSeq++;
17234
17235        // This is the desired cached adjusment we want to tell it to use.
17236        // If our app is currently cached, we know it, and that is it.  Otherwise,
17237        // we don't know it yet, and it needs to now be cached we will then
17238        // need to do a complete oom adj.
17239        final int cachedAdj = app.curRawAdj >= ProcessList.CACHED_APP_MIN_ADJ
17240                ? app.curRawAdj : ProcessList.UNKNOWN_ADJ;
17241        boolean success = updateOomAdjLocked(app, cachedAdj, TOP_APP, false,
17242                SystemClock.uptimeMillis());
17243        if (wasCached != app.cached || app.curRawAdj == ProcessList.UNKNOWN_ADJ) {
17244            // Changed to/from cached state, so apps after it in the LRU
17245            // list may also be changed.
17246            updateOomAdjLocked();
17247        }
17248        return success;
17249    }
17250
17251    final void updateOomAdjLocked() {
17252        final ActivityRecord TOP_ACT = resumedAppLocked();
17253        final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
17254        final long now = SystemClock.uptimeMillis();
17255        final long oldTime = now - ProcessList.MAX_EMPTY_TIME;
17256        final int N = mLruProcesses.size();
17257
17258        if (false) {
17259            RuntimeException e = new RuntimeException();
17260            e.fillInStackTrace();
17261            Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e);
17262        }
17263
17264        mAdjSeq++;
17265        mNewNumServiceProcs = 0;
17266        mNewNumAServiceProcs = 0;
17267
17268        final int emptyProcessLimit;
17269        final int cachedProcessLimit;
17270        if (mProcessLimit <= 0) {
17271            emptyProcessLimit = cachedProcessLimit = 0;
17272        } else if (mProcessLimit == 1) {
17273            emptyProcessLimit = 1;
17274            cachedProcessLimit = 0;
17275        } else {
17276            emptyProcessLimit = ProcessList.computeEmptyProcessLimit(mProcessLimit);
17277            cachedProcessLimit = mProcessLimit - emptyProcessLimit;
17278        }
17279
17280        // Let's determine how many processes we have running vs.
17281        // how many slots we have for background processes; we may want
17282        // to put multiple processes in a slot of there are enough of
17283        // them.
17284        int numSlots = (ProcessList.CACHED_APP_MAX_ADJ
17285                - ProcessList.CACHED_APP_MIN_ADJ + 1) / 2;
17286        int numEmptyProcs = N - mNumNonCachedProcs - mNumCachedHiddenProcs;
17287        if (numEmptyProcs > cachedProcessLimit) {
17288            // If there are more empty processes than our limit on cached
17289            // processes, then use the cached process limit for the factor.
17290            // This ensures that the really old empty processes get pushed
17291            // down to the bottom, so if we are running low on memory we will
17292            // have a better chance at keeping around more cached processes
17293            // instead of a gazillion empty processes.
17294            numEmptyProcs = cachedProcessLimit;
17295        }
17296        int emptyFactor = numEmptyProcs/numSlots;
17297        if (emptyFactor < 1) emptyFactor = 1;
17298        int cachedFactor = (mNumCachedHiddenProcs > 0 ? mNumCachedHiddenProcs : 1)/numSlots;
17299        if (cachedFactor < 1) cachedFactor = 1;
17300        int stepCached = 0;
17301        int stepEmpty = 0;
17302        int numCached = 0;
17303        int numEmpty = 0;
17304        int numTrimming = 0;
17305
17306        mNumNonCachedProcs = 0;
17307        mNumCachedHiddenProcs = 0;
17308
17309        // First update the OOM adjustment for each of the
17310        // application processes based on their current state.
17311        int curCachedAdj = ProcessList.CACHED_APP_MIN_ADJ;
17312        int nextCachedAdj = curCachedAdj+1;
17313        int curEmptyAdj = ProcessList.CACHED_APP_MIN_ADJ;
17314        int nextEmptyAdj = curEmptyAdj+2;
17315        for (int i=N-1; i>=0; i--) {
17316            ProcessRecord app = mLruProcesses.get(i);
17317            if (!app.killedByAm && app.thread != null) {
17318                app.procStateChanged = false;
17319                computeOomAdjLocked(app, ProcessList.UNKNOWN_ADJ, TOP_APP, true, now);
17320
17321                // If we haven't yet assigned the final cached adj
17322                // to the process, do that now.
17323                if (app.curAdj >= ProcessList.UNKNOWN_ADJ) {
17324                    switch (app.curProcState) {
17325                        case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY:
17326                        case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT:
17327                            // This process is a cached process holding activities...
17328                            // assign it the next cached value for that type, and then
17329                            // step that cached level.
17330                            app.curRawAdj = curCachedAdj;
17331                            app.curAdj = app.modifyRawOomAdj(curCachedAdj);
17332                            if (DEBUG_LRU && false) Slog.d(TAG, "Assigning activity LRU #" + i
17333                                    + " adj: " + app.curAdj + " (curCachedAdj=" + curCachedAdj
17334                                    + ")");
17335                            if (curCachedAdj != nextCachedAdj) {
17336                                stepCached++;
17337                                if (stepCached >= cachedFactor) {
17338                                    stepCached = 0;
17339                                    curCachedAdj = nextCachedAdj;
17340                                    nextCachedAdj += 2;
17341                                    if (nextCachedAdj > ProcessList.CACHED_APP_MAX_ADJ) {
17342                                        nextCachedAdj = ProcessList.CACHED_APP_MAX_ADJ;
17343                                    }
17344                                }
17345                            }
17346                            break;
17347                        default:
17348                            // For everything else, assign next empty cached process
17349                            // level and bump that up.  Note that this means that
17350                            // long-running services that have dropped down to the
17351                            // cached level will be treated as empty (since their process
17352                            // state is still as a service), which is what we want.
17353                            app.curRawAdj = curEmptyAdj;
17354                            app.curAdj = app.modifyRawOomAdj(curEmptyAdj);
17355                            if (DEBUG_LRU && false) Slog.d(TAG, "Assigning empty LRU #" + i
17356                                    + " adj: " + app.curAdj + " (curEmptyAdj=" + curEmptyAdj
17357                                    + ")");
17358                            if (curEmptyAdj != nextEmptyAdj) {
17359                                stepEmpty++;
17360                                if (stepEmpty >= emptyFactor) {
17361                                    stepEmpty = 0;
17362                                    curEmptyAdj = nextEmptyAdj;
17363                                    nextEmptyAdj += 2;
17364                                    if (nextEmptyAdj > ProcessList.CACHED_APP_MAX_ADJ) {
17365                                        nextEmptyAdj = ProcessList.CACHED_APP_MAX_ADJ;
17366                                    }
17367                                }
17368                            }
17369                            break;
17370                    }
17371                }
17372
17373                applyOomAdjLocked(app, TOP_APP, true, now);
17374
17375                // Count the number of process types.
17376                switch (app.curProcState) {
17377                    case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY:
17378                    case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT:
17379                        mNumCachedHiddenProcs++;
17380                        numCached++;
17381                        if (numCached > cachedProcessLimit) {
17382                            app.kill("cached #" + numCached, true);
17383                        }
17384                        break;
17385                    case ActivityManager.PROCESS_STATE_CACHED_EMPTY:
17386                        if (numEmpty > ProcessList.TRIM_EMPTY_APPS
17387                                && app.lastActivityTime < oldTime) {
17388                            app.kill("empty for "
17389                                    + ((oldTime + ProcessList.MAX_EMPTY_TIME - app.lastActivityTime)
17390                                    / 1000) + "s", true);
17391                        } else {
17392                            numEmpty++;
17393                            if (numEmpty > emptyProcessLimit) {
17394                                app.kill("empty #" + numEmpty, true);
17395                            }
17396                        }
17397                        break;
17398                    default:
17399                        mNumNonCachedProcs++;
17400                        break;
17401                }
17402
17403                if (app.isolated && app.services.size() <= 0) {
17404                    // If this is an isolated process, and there are no
17405                    // services running in it, then the process is no longer
17406                    // needed.  We agressively kill these because we can by
17407                    // definition not re-use the same process again, and it is
17408                    // good to avoid having whatever code was running in them
17409                    // left sitting around after no longer needed.
17410                    app.kill("isolated not needed", true);
17411                }
17412
17413                if (app.curProcState >= ActivityManager.PROCESS_STATE_HOME
17414                        && !app.killedByAm) {
17415                    numTrimming++;
17416                }
17417            }
17418        }
17419
17420        mNumServiceProcs = mNewNumServiceProcs;
17421
17422        // Now determine the memory trimming level of background processes.
17423        // Unfortunately we need to start at the back of the list to do this
17424        // properly.  We only do this if the number of background apps we
17425        // are managing to keep around is less than half the maximum we desire;
17426        // if we are keeping a good number around, we'll let them use whatever
17427        // memory they want.
17428        final int numCachedAndEmpty = numCached + numEmpty;
17429        int memFactor;
17430        if (numCached <= ProcessList.TRIM_CACHED_APPS
17431                && numEmpty <= ProcessList.TRIM_EMPTY_APPS) {
17432            if (numCachedAndEmpty <= ProcessList.TRIM_CRITICAL_THRESHOLD) {
17433                memFactor = ProcessStats.ADJ_MEM_FACTOR_CRITICAL;
17434            } else if (numCachedAndEmpty <= ProcessList.TRIM_LOW_THRESHOLD) {
17435                memFactor = ProcessStats.ADJ_MEM_FACTOR_LOW;
17436            } else {
17437                memFactor = ProcessStats.ADJ_MEM_FACTOR_MODERATE;
17438            }
17439        } else {
17440            memFactor = ProcessStats.ADJ_MEM_FACTOR_NORMAL;
17441        }
17442        // We always allow the memory level to go up (better).  We only allow it to go
17443        // down if we are in a state where that is allowed, *and* the total number of processes
17444        // has gone down since last time.
17445        if (DEBUG_OOM_ADJ) Slog.d(TAG, "oom: memFactor=" + memFactor + " last=" + mLastMemoryLevel
17446                + " allowLow=" + mAllowLowerMemLevel + " numProcs=" + mLruProcesses.size()
17447                + " last=" + mLastNumProcesses);
17448        if (memFactor > mLastMemoryLevel) {
17449            if (!mAllowLowerMemLevel || mLruProcesses.size() >= mLastNumProcesses) {
17450                memFactor = mLastMemoryLevel;
17451                if (DEBUG_OOM_ADJ) Slog.d(TAG, "Keeping last mem factor!");
17452            }
17453        }
17454        mLastMemoryLevel = memFactor;
17455        mLastNumProcesses = mLruProcesses.size();
17456        boolean allChanged = mProcessStats.setMemFactorLocked(memFactor, !isSleeping(), now);
17457        final int trackerMemFactor = mProcessStats.getMemFactorLocked();
17458        if (memFactor != ProcessStats.ADJ_MEM_FACTOR_NORMAL) {
17459            if (mLowRamStartTime == 0) {
17460                mLowRamStartTime = now;
17461            }
17462            int step = 0;
17463            int fgTrimLevel;
17464            switch (memFactor) {
17465                case ProcessStats.ADJ_MEM_FACTOR_CRITICAL:
17466                    fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
17467                    break;
17468                case ProcessStats.ADJ_MEM_FACTOR_LOW:
17469                    fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
17470                    break;
17471                default:
17472                    fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
17473                    break;
17474            }
17475            int factor = numTrimming/3;
17476            int minFactor = 2;
17477            if (mHomeProcess != null) minFactor++;
17478            if (mPreviousProcess != null) minFactor++;
17479            if (factor < minFactor) factor = minFactor;
17480            int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
17481            for (int i=N-1; i>=0; i--) {
17482                ProcessRecord app = mLruProcesses.get(i);
17483                if (allChanged || app.procStateChanged) {
17484                    setProcessTrackerStateLocked(app, trackerMemFactor, now);
17485                    app.procStateChanged = false;
17486                }
17487                if (app.curProcState >= ActivityManager.PROCESS_STATE_HOME
17488                        && !app.killedByAm) {
17489                    if (app.trimMemoryLevel < curLevel && app.thread != null) {
17490                        try {
17491                            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
17492                                    "Trimming memory of " + app.processName
17493                                    + " to " + curLevel);
17494                            app.thread.scheduleTrimMemory(curLevel);
17495                        } catch (RemoteException e) {
17496                        }
17497                        if (false) {
17498                            // For now we won't do this; our memory trimming seems
17499                            // to be good enough at this point that destroying
17500                            // activities causes more harm than good.
17501                            if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE
17502                                    && app != mHomeProcess && app != mPreviousProcess) {
17503                                // Need to do this on its own message because the stack may not
17504                                // be in a consistent state at this point.
17505                                // For these apps we will also finish their activities
17506                                // to help them free memory.
17507                                mStackSupervisor.scheduleDestroyAllActivities(app, "trim");
17508                            }
17509                        }
17510                    }
17511                    app.trimMemoryLevel = curLevel;
17512                    step++;
17513                    if (step >= factor) {
17514                        step = 0;
17515                        switch (curLevel) {
17516                            case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
17517                                curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE;
17518                                break;
17519                            case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
17520                                curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
17521                                break;
17522                        }
17523                    }
17524                } else if (app.curProcState == ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
17525                    if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
17526                            && app.thread != null) {
17527                        try {
17528                            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
17529                                    "Trimming memory of heavy-weight " + app.processName
17530                                    + " to " + ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
17531                            app.thread.scheduleTrimMemory(
17532                                    ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
17533                        } catch (RemoteException e) {
17534                        }
17535                    }
17536                    app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
17537                } else {
17538                    if ((app.curProcState >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
17539                            || app.systemNoUi) && app.pendingUiClean) {
17540                        // If this application is now in the background and it
17541                        // had done UI, then give it the special trim level to
17542                        // have it free UI resources.
17543                        final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
17544                        if (app.trimMemoryLevel < level && app.thread != null) {
17545                            try {
17546                                if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
17547                                        "Trimming memory of bg-ui " + app.processName
17548                                        + " to " + level);
17549                                app.thread.scheduleTrimMemory(level);
17550                            } catch (RemoteException e) {
17551                            }
17552                        }
17553                        app.pendingUiClean = false;
17554                    }
17555                    if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) {
17556                        try {
17557                            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
17558                                    "Trimming memory of fg " + app.processName
17559                                    + " to " + fgTrimLevel);
17560                            app.thread.scheduleTrimMemory(fgTrimLevel);
17561                        } catch (RemoteException e) {
17562                        }
17563                    }
17564                    app.trimMemoryLevel = fgTrimLevel;
17565                }
17566            }
17567        } else {
17568            if (mLowRamStartTime != 0) {
17569                mLowRamTimeSinceLastIdle += now - mLowRamStartTime;
17570                mLowRamStartTime = 0;
17571            }
17572            for (int i=N-1; i>=0; i--) {
17573                ProcessRecord app = mLruProcesses.get(i);
17574                if (allChanged || app.procStateChanged) {
17575                    setProcessTrackerStateLocked(app, trackerMemFactor, now);
17576                    app.procStateChanged = false;
17577                }
17578                if ((app.curProcState >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
17579                        || app.systemNoUi) && app.pendingUiClean) {
17580                    if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
17581                            && app.thread != null) {
17582                        try {
17583                            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
17584                                    "Trimming memory of ui hidden " + app.processName
17585                                    + " to " + ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
17586                            app.thread.scheduleTrimMemory(
17587                                    ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
17588                        } catch (RemoteException e) {
17589                        }
17590                    }
17591                    app.pendingUiClean = false;
17592                }
17593                app.trimMemoryLevel = 0;
17594            }
17595        }
17596
17597        if (mAlwaysFinishActivities) {
17598            // Need to do this on its own message because the stack may not
17599            // be in a consistent state at this point.
17600            mStackSupervisor.scheduleDestroyAllActivities(null, "always-finish");
17601        }
17602
17603        if (allChanged) {
17604            requestPssAllProcsLocked(now, false, mProcessStats.isMemFactorLowered());
17605        }
17606
17607        if (mProcessStats.shouldWriteNowLocked(now)) {
17608            mHandler.post(new Runnable() {
17609                @Override public void run() {
17610                    synchronized (ActivityManagerService.this) {
17611                        mProcessStats.writeStateAsyncLocked();
17612                    }
17613                }
17614            });
17615        }
17616
17617        if (DEBUG_OOM_ADJ) {
17618            Slog.d(TAG, "Did OOM ADJ in " + (SystemClock.uptimeMillis()-now) + "ms");
17619        }
17620    }
17621
17622    final void trimApplications() {
17623        synchronized (this) {
17624            int i;
17625
17626            // First remove any unused application processes whose package
17627            // has been removed.
17628            for (i=mRemovedProcesses.size()-1; i>=0; i--) {
17629                final ProcessRecord app = mRemovedProcesses.get(i);
17630                if (app.activities.size() == 0
17631                        && app.curReceiver == null && app.services.size() == 0) {
17632                    Slog.i(
17633                        TAG, "Exiting empty application process "
17634                        + app.processName + " ("
17635                        + (app.thread != null ? app.thread.asBinder() : null)
17636                        + ")\n");
17637                    if (app.pid > 0 && app.pid != MY_PID) {
17638                        app.kill("empty", false);
17639                    } else {
17640                        try {
17641                            app.thread.scheduleExit();
17642                        } catch (Exception e) {
17643                            // Ignore exceptions.
17644                        }
17645                    }
17646                    cleanUpApplicationRecordLocked(app, false, true, -1);
17647                    mRemovedProcesses.remove(i);
17648
17649                    if (app.persistent) {
17650                        addAppLocked(app.info, false, null /* ABI override */);
17651                    }
17652                }
17653            }
17654
17655            // Now update the oom adj for all processes.
17656            updateOomAdjLocked();
17657        }
17658    }
17659
17660    /** This method sends the specified signal to each of the persistent apps */
17661    public void signalPersistentProcesses(int sig) throws RemoteException {
17662        if (sig != Process.SIGNAL_USR1) {
17663            throw new SecurityException("Only SIGNAL_USR1 is allowed");
17664        }
17665
17666        synchronized (this) {
17667            if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES)
17668                    != PackageManager.PERMISSION_GRANTED) {
17669                throw new SecurityException("Requires permission "
17670                        + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES);
17671            }
17672
17673            for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
17674                ProcessRecord r = mLruProcesses.get(i);
17675                if (r.thread != null && r.persistent) {
17676                    Process.sendSignal(r.pid, sig);
17677                }
17678            }
17679        }
17680    }
17681
17682    private void stopProfilerLocked(ProcessRecord proc, int profileType) {
17683        if (proc == null || proc == mProfileProc) {
17684            proc = mProfileProc;
17685            profileType = mProfileType;
17686            clearProfilerLocked();
17687        }
17688        if (proc == null) {
17689            return;
17690        }
17691        try {
17692            proc.thread.profilerControl(false, null, profileType);
17693        } catch (RemoteException e) {
17694            throw new IllegalStateException("Process disappeared");
17695        }
17696    }
17697
17698    private void clearProfilerLocked() {
17699        if (mProfileFd != null) {
17700            try {
17701                mProfileFd.close();
17702            } catch (IOException e) {
17703            }
17704        }
17705        mProfileApp = null;
17706        mProfileProc = null;
17707        mProfileFile = null;
17708        mProfileType = 0;
17709        mAutoStopProfiler = false;
17710        mSamplingInterval = 0;
17711    }
17712
17713    public boolean profileControl(String process, int userId, boolean start,
17714            ProfilerInfo profilerInfo, int profileType) throws RemoteException {
17715
17716        try {
17717            synchronized (this) {
17718                // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
17719                // its own permission.
17720                if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
17721                        != PackageManager.PERMISSION_GRANTED) {
17722                    throw new SecurityException("Requires permission "
17723                            + android.Manifest.permission.SET_ACTIVITY_WATCHER);
17724                }
17725
17726                if (start && (profilerInfo == null || profilerInfo.profileFd == null)) {
17727                    throw new IllegalArgumentException("null profile info or fd");
17728                }
17729
17730                ProcessRecord proc = null;
17731                if (process != null) {
17732                    proc = findProcessLocked(process, userId, "profileControl");
17733                }
17734
17735                if (start && (proc == null || proc.thread == null)) {
17736                    throw new IllegalArgumentException("Unknown process: " + process);
17737                }
17738
17739                if (start) {
17740                    stopProfilerLocked(null, 0);
17741                    setProfileApp(proc.info, proc.processName, profilerInfo);
17742                    mProfileProc = proc;
17743                    mProfileType = profileType;
17744                    ParcelFileDescriptor fd = profilerInfo.profileFd;
17745                    try {
17746                        fd = fd.dup();
17747                    } catch (IOException e) {
17748                        fd = null;
17749                    }
17750                    profilerInfo.profileFd = fd;
17751                    proc.thread.profilerControl(start, profilerInfo, profileType);
17752                    fd = null;
17753                    mProfileFd = null;
17754                } else {
17755                    stopProfilerLocked(proc, profileType);
17756                    if (profilerInfo != null && profilerInfo.profileFd != null) {
17757                        try {
17758                            profilerInfo.profileFd.close();
17759                        } catch (IOException e) {
17760                        }
17761                    }
17762                }
17763
17764                return true;
17765            }
17766        } catch (RemoteException e) {
17767            throw new IllegalStateException("Process disappeared");
17768        } finally {
17769            if (profilerInfo != null && profilerInfo.profileFd != null) {
17770                try {
17771                    profilerInfo.profileFd.close();
17772                } catch (IOException e) {
17773                }
17774            }
17775        }
17776    }
17777
17778    private ProcessRecord findProcessLocked(String process, int userId, String callName) {
17779        userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
17780                userId, true, ALLOW_FULL_ONLY, callName, null);
17781        ProcessRecord proc = null;
17782        try {
17783            int pid = Integer.parseInt(process);
17784            synchronized (mPidsSelfLocked) {
17785                proc = mPidsSelfLocked.get(pid);
17786            }
17787        } catch (NumberFormatException e) {
17788        }
17789
17790        if (proc == null) {
17791            ArrayMap<String, SparseArray<ProcessRecord>> all
17792                    = mProcessNames.getMap();
17793            SparseArray<ProcessRecord> procs = all.get(process);
17794            if (procs != null && procs.size() > 0) {
17795                proc = procs.valueAt(0);
17796                if (userId != UserHandle.USER_ALL && proc.userId != userId) {
17797                    for (int i=1; i<procs.size(); i++) {
17798                        ProcessRecord thisProc = procs.valueAt(i);
17799                        if (thisProc.userId == userId) {
17800                            proc = thisProc;
17801                            break;
17802                        }
17803                    }
17804                }
17805            }
17806        }
17807
17808        return proc;
17809    }
17810
17811    public boolean dumpHeap(String process, int userId, boolean managed,
17812            String path, ParcelFileDescriptor fd) throws RemoteException {
17813
17814        try {
17815            synchronized (this) {
17816                // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
17817                // its own permission (same as profileControl).
17818                if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
17819                        != PackageManager.PERMISSION_GRANTED) {
17820                    throw new SecurityException("Requires permission "
17821                            + android.Manifest.permission.SET_ACTIVITY_WATCHER);
17822                }
17823
17824                if (fd == null) {
17825                    throw new IllegalArgumentException("null fd");
17826                }
17827
17828                ProcessRecord proc = findProcessLocked(process, userId, "dumpHeap");
17829                if (proc == null || proc.thread == null) {
17830                    throw new IllegalArgumentException("Unknown process: " + process);
17831                }
17832
17833                boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
17834                if (!isDebuggable) {
17835                    if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
17836                        throw new SecurityException("Process not debuggable: " + proc);
17837                    }
17838                }
17839
17840                proc.thread.dumpHeap(managed, path, fd);
17841                fd = null;
17842                return true;
17843            }
17844        } catch (RemoteException e) {
17845            throw new IllegalStateException("Process disappeared");
17846        } finally {
17847            if (fd != null) {
17848                try {
17849                    fd.close();
17850                } catch (IOException e) {
17851                }
17852            }
17853        }
17854    }
17855
17856    /** In this method we try to acquire our lock to make sure that we have not deadlocked */
17857    public void monitor() {
17858        synchronized (this) { }
17859    }
17860
17861    void onCoreSettingsChange(Bundle settings) {
17862        for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
17863            ProcessRecord processRecord = mLruProcesses.get(i);
17864            try {
17865                if (processRecord.thread != null) {
17866                    processRecord.thread.setCoreSettings(settings);
17867                }
17868            } catch (RemoteException re) {
17869                /* ignore */
17870            }
17871        }
17872    }
17873
17874    // Multi-user methods
17875
17876    /**
17877     * Start user, if its not already running, but don't bring it to foreground.
17878     */
17879    @Override
17880    public boolean startUserInBackground(final int userId) {
17881        return startUser(userId, /* foreground */ false);
17882    }
17883
17884    /**
17885     * Start user, if its not already running, and bring it to foreground.
17886     */
17887    boolean startUserInForeground(final int userId, Dialog dlg) {
17888        boolean result = startUser(userId, /* foreground */ true);
17889        dlg.dismiss();
17890        return result;
17891    }
17892
17893    /**
17894     * Refreshes the list of users related to the current user when either a
17895     * user switch happens or when a new related user is started in the
17896     * background.
17897     */
17898    private void updateCurrentProfileIdsLocked() {
17899        final List<UserInfo> profiles = getUserManagerLocked().getProfiles(
17900                mCurrentUserId, false /* enabledOnly */);
17901        int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
17902        for (int i = 0; i < currentProfileIds.length; i++) {
17903            currentProfileIds[i] = profiles.get(i).id;
17904        }
17905        mCurrentProfileIds = currentProfileIds;
17906
17907        synchronized (mUserProfileGroupIdsSelfLocked) {
17908            mUserProfileGroupIdsSelfLocked.clear();
17909            final List<UserInfo> users = getUserManagerLocked().getUsers(false);
17910            for (int i = 0; i < users.size(); i++) {
17911                UserInfo user = users.get(i);
17912                if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
17913                    mUserProfileGroupIdsSelfLocked.put(user.id, user.profileGroupId);
17914                }
17915            }
17916        }
17917    }
17918
17919    private Set getProfileIdsLocked(int userId) {
17920        Set userIds = new HashSet<Integer>();
17921        final List<UserInfo> profiles = getUserManagerLocked().getProfiles(
17922                userId, false /* enabledOnly */);
17923        for (UserInfo user : profiles) {
17924            userIds.add(Integer.valueOf(user.id));
17925        }
17926        return userIds;
17927    }
17928
17929    @Override
17930    public boolean switchUser(final int userId) {
17931        String userName;
17932        synchronized (this) {
17933            UserInfo userInfo = getUserManagerLocked().getUserInfo(userId);
17934            if (userInfo == null) {
17935                Slog.w(TAG, "No user info for user #" + userId);
17936                return false;
17937            }
17938            if (userInfo.isManagedProfile()) {
17939                Slog.w(TAG, "Cannot switch to User #" + userId + ": not a full user");
17940                return false;
17941            }
17942            userName = userInfo.name;
17943            mTargetUserId = userId;
17944        }
17945        mHandler.removeMessages(START_USER_SWITCH_MSG);
17946        mHandler.sendMessage(mHandler.obtainMessage(START_USER_SWITCH_MSG, userId, 0, userName));
17947        return true;
17948    }
17949
17950    private void showUserSwitchDialog(int userId, String userName) {
17951        // The dialog will show and then initiate the user switch by calling startUserInForeground
17952        Dialog d = new UserSwitchingDialog(this, mContext, userId, userName,
17953                true /* above system */);
17954        d.show();
17955    }
17956
17957    private boolean startUser(final int userId, final boolean foreground) {
17958        if (checkCallingPermission(INTERACT_ACROSS_USERS_FULL)
17959                != PackageManager.PERMISSION_GRANTED) {
17960            String msg = "Permission Denial: switchUser() from pid="
17961                    + Binder.getCallingPid()
17962                    + ", uid=" + Binder.getCallingUid()
17963                    + " requires " + INTERACT_ACROSS_USERS_FULL;
17964            Slog.w(TAG, msg);
17965            throw new SecurityException(msg);
17966        }
17967
17968        if (DEBUG_MU) Slog.i(TAG_MU, "starting userid:" + userId + " fore:" + foreground);
17969
17970        final long ident = Binder.clearCallingIdentity();
17971        try {
17972            synchronized (this) {
17973                final int oldUserId = mCurrentUserId;
17974                if (oldUserId == userId) {
17975                    return true;
17976                }
17977
17978                mStackSupervisor.setLockTaskModeLocked(null, false);
17979
17980                final UserInfo userInfo = getUserManagerLocked().getUserInfo(userId);
17981                if (userInfo == null) {
17982                    Slog.w(TAG, "No user info for user #" + userId);
17983                    return false;
17984                }
17985                if (foreground && userInfo.isManagedProfile()) {
17986                    Slog.w(TAG, "Cannot switch to User #" + userId + ": not a full user");
17987                    return false;
17988                }
17989
17990                if (foreground) {
17991                    mWindowManager.startFreezingScreen(R.anim.screen_user_exit,
17992                            R.anim.screen_user_enter);
17993                }
17994
17995                boolean needStart = false;
17996
17997                // If the user we are switching to is not currently started, then
17998                // we need to start it now.
17999                if (mStartedUsers.get(userId) == null) {
18000                    mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false));
18001                    updateStartedUserArrayLocked();
18002                    needStart = true;
18003                }
18004
18005                final Integer userIdInt = Integer.valueOf(userId);
18006                mUserLru.remove(userIdInt);
18007                mUserLru.add(userIdInt);
18008
18009                if (foreground) {
18010                    mCurrentUserId = userId;
18011                    mTargetUserId = UserHandle.USER_NULL; // reset, mCurrentUserId has caught up
18012                    updateCurrentProfileIdsLocked();
18013                    mWindowManager.setCurrentUser(userId, mCurrentProfileIds);
18014                    // Once the internal notion of the active user has switched, we lock the device
18015                    // with the option to show the user switcher on the keyguard.
18016                    mWindowManager.lockNow(null);
18017                } else {
18018                    final Integer currentUserIdInt = Integer.valueOf(mCurrentUserId);
18019                    updateCurrentProfileIdsLocked();
18020                    mWindowManager.setCurrentProfileIds(mCurrentProfileIds);
18021                    mUserLru.remove(currentUserIdInt);
18022                    mUserLru.add(currentUserIdInt);
18023                }
18024
18025                final UserStartedState uss = mStartedUsers.get(userId);
18026
18027                // Make sure user is in the started state.  If it is currently
18028                // stopping, we need to knock that off.
18029                if (uss.mState == UserStartedState.STATE_STOPPING) {
18030                    // If we are stopping, we haven't sent ACTION_SHUTDOWN,
18031                    // so we can just fairly silently bring the user back from
18032                    // the almost-dead.
18033                    uss.mState = UserStartedState.STATE_RUNNING;
18034                    updateStartedUserArrayLocked();
18035                    needStart = true;
18036                } else if (uss.mState == UserStartedState.STATE_SHUTDOWN) {
18037                    // This means ACTION_SHUTDOWN has been sent, so we will
18038                    // need to treat this as a new boot of the user.
18039                    uss.mState = UserStartedState.STATE_BOOTING;
18040                    updateStartedUserArrayLocked();
18041                    needStart = true;
18042                }
18043
18044                if (uss.mState == UserStartedState.STATE_BOOTING) {
18045                    // Booting up a new user, need to tell system services about it.
18046                    // Note that this is on the same handler as scheduling of broadcasts,
18047                    // which is important because it needs to go first.
18048                    mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_START_MSG, userId, 0));
18049                }
18050
18051                if (foreground) {
18052                    mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_CURRENT_MSG, userId,
18053                            oldUserId));
18054                    mHandler.removeMessages(REPORT_USER_SWITCH_MSG);
18055                    mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG);
18056                    mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG,
18057                            oldUserId, userId, uss));
18058                    mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG,
18059                            oldUserId, userId, uss), USER_SWITCH_TIMEOUT);
18060                }
18061
18062                if (needStart) {
18063                    // Send USER_STARTED broadcast
18064                    Intent intent = new Intent(Intent.ACTION_USER_STARTED);
18065                    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
18066                            | Intent.FLAG_RECEIVER_FOREGROUND);
18067                    intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
18068                    broadcastIntentLocked(null, null, intent,
18069                            null, null, 0, null, null, null, AppOpsManager.OP_NONE,
18070                            false, false, MY_PID, Process.SYSTEM_UID, userId);
18071                }
18072
18073                if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) {
18074                    if (userId != UserHandle.USER_OWNER) {
18075                        Intent intent = new Intent(Intent.ACTION_USER_INITIALIZE);
18076                        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
18077                        broadcastIntentLocked(null, null, intent, null,
18078                                new IIntentReceiver.Stub() {
18079                                    public void performReceive(Intent intent, int resultCode,
18080                                            String data, Bundle extras, boolean ordered,
18081                                            boolean sticky, int sendingUser) {
18082                                        onUserInitialized(uss, foreground, oldUserId, userId);
18083                                    }
18084                                }, 0, null, null, null, AppOpsManager.OP_NONE,
18085                                true, false, MY_PID, Process.SYSTEM_UID,
18086                                userId);
18087                        uss.initializing = true;
18088                    } else {
18089                        getUserManagerLocked().makeInitialized(userInfo.id);
18090                    }
18091                }
18092
18093                if (foreground) {
18094                    if (!uss.initializing) {
18095                        moveUserToForeground(uss, oldUserId, userId);
18096                    }
18097                } else {
18098                    mStackSupervisor.startBackgroundUserLocked(userId, uss);
18099                }
18100
18101                if (needStart) {
18102                    Intent intent = new Intent(Intent.ACTION_USER_STARTING);
18103                    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
18104                    intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
18105                    broadcastIntentLocked(null, null, intent,
18106                            null, new IIntentReceiver.Stub() {
18107                                @Override
18108                                public void performReceive(Intent intent, int resultCode, String data,
18109                                        Bundle extras, boolean ordered, boolean sticky, int sendingUser)
18110                                        throws RemoteException {
18111                                }
18112                            }, 0, null, null,
18113                            INTERACT_ACROSS_USERS, AppOpsManager.OP_NONE,
18114                            true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
18115                }
18116            }
18117        } finally {
18118            Binder.restoreCallingIdentity(ident);
18119        }
18120
18121        return true;
18122    }
18123
18124    void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) {
18125        long ident = Binder.clearCallingIdentity();
18126        try {
18127            Intent intent;
18128            if (oldUserId >= 0) {
18129                // Send USER_BACKGROUND broadcast to all profiles of the outgoing user
18130                List<UserInfo> profiles = mUserManager.getProfiles(oldUserId, false);
18131                int count = profiles.size();
18132                for (int i = 0; i < count; i++) {
18133                    int profileUserId = profiles.get(i).id;
18134                    intent = new Intent(Intent.ACTION_USER_BACKGROUND);
18135                    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
18136                            | Intent.FLAG_RECEIVER_FOREGROUND);
18137                    intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId);
18138                    broadcastIntentLocked(null, null, intent,
18139                            null, null, 0, null, null, null, AppOpsManager.OP_NONE,
18140                            false, false, MY_PID, Process.SYSTEM_UID, profileUserId);
18141                }
18142            }
18143            if (newUserId >= 0) {
18144                // Send USER_FOREGROUND broadcast to all profiles of the incoming user
18145                List<UserInfo> profiles = mUserManager.getProfiles(newUserId, false);
18146                int count = profiles.size();
18147                for (int i = 0; i < count; i++) {
18148                    int profileUserId = profiles.get(i).id;
18149                    intent = new Intent(Intent.ACTION_USER_FOREGROUND);
18150                    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
18151                            | Intent.FLAG_RECEIVER_FOREGROUND);
18152                    intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId);
18153                    broadcastIntentLocked(null, null, intent,
18154                            null, null, 0, null, null, null, AppOpsManager.OP_NONE,
18155                            false, false, MY_PID, Process.SYSTEM_UID, profileUserId);
18156                }
18157                intent = new Intent(Intent.ACTION_USER_SWITCHED);
18158                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
18159                        | Intent.FLAG_RECEIVER_FOREGROUND);
18160                intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId);
18161                broadcastIntentLocked(null, null, intent,
18162                        null, null, 0, null, null,
18163                        android.Manifest.permission.MANAGE_USERS, AppOpsManager.OP_NONE,
18164                        false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
18165            }
18166        } finally {
18167            Binder.restoreCallingIdentity(ident);
18168        }
18169    }
18170
18171    void dispatchUserSwitch(final UserStartedState uss, final int oldUserId,
18172            final int newUserId) {
18173        final int N = mUserSwitchObservers.beginBroadcast();
18174        if (N > 0) {
18175            final IRemoteCallback callback = new IRemoteCallback.Stub() {
18176                int mCount = 0;
18177                @Override
18178                public void sendResult(Bundle data) throws RemoteException {
18179                    synchronized (ActivityManagerService.this) {
18180                        if (mCurUserSwitchCallback == this) {
18181                            mCount++;
18182                            if (mCount == N) {
18183                                sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
18184                            }
18185                        }
18186                    }
18187                }
18188            };
18189            synchronized (this) {
18190                uss.switching = true;
18191                mCurUserSwitchCallback = callback;
18192            }
18193            for (int i=0; i<N; i++) {
18194                try {
18195                    mUserSwitchObservers.getBroadcastItem(i).onUserSwitching(
18196                            newUserId, callback);
18197                } catch (RemoteException e) {
18198                }
18199            }
18200        } else {
18201            synchronized (this) {
18202                sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
18203            }
18204        }
18205        mUserSwitchObservers.finishBroadcast();
18206    }
18207
18208    void timeoutUserSwitch(UserStartedState uss, int oldUserId, int newUserId) {
18209        synchronized (this) {
18210            Slog.w(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId);
18211            sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
18212        }
18213    }
18214
18215    void sendContinueUserSwitchLocked(UserStartedState uss, int oldUserId, int newUserId) {
18216        mCurUserSwitchCallback = null;
18217        mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG);
18218        mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG,
18219                oldUserId, newUserId, uss));
18220    }
18221
18222    void onUserInitialized(UserStartedState uss, boolean foreground, int oldUserId, int newUserId) {
18223        synchronized (this) {
18224            if (foreground) {
18225                moveUserToForeground(uss, oldUserId, newUserId);
18226            }
18227        }
18228
18229        completeSwitchAndInitalize(uss, newUserId, true, false);
18230    }
18231
18232    void moveUserToForeground(UserStartedState uss, int oldUserId, int newUserId) {
18233        boolean homeInFront = mStackSupervisor.switchUserLocked(newUserId, uss);
18234        if (homeInFront) {
18235            startHomeActivityLocked(newUserId);
18236        } else {
18237            mStackSupervisor.resumeTopActivitiesLocked();
18238        }
18239        EventLogTags.writeAmSwitchUser(newUserId);
18240        getUserManagerLocked().userForeground(newUserId);
18241        sendUserSwitchBroadcastsLocked(oldUserId, newUserId);
18242    }
18243
18244    void continueUserSwitch(UserStartedState uss, int oldUserId, int newUserId) {
18245        completeSwitchAndInitalize(uss, newUserId, false, true);
18246    }
18247
18248    void completeSwitchAndInitalize(UserStartedState uss, int newUserId,
18249            boolean clearInitializing, boolean clearSwitching) {
18250        boolean unfrozen = false;
18251        synchronized (this) {
18252            if (clearInitializing) {
18253                uss.initializing = false;
18254                getUserManagerLocked().makeInitialized(uss.mHandle.getIdentifier());
18255            }
18256            if (clearSwitching) {
18257                uss.switching = false;
18258            }
18259            if (!uss.switching && !uss.initializing) {
18260                mWindowManager.stopFreezingScreen();
18261                unfrozen = true;
18262            }
18263        }
18264        if (unfrozen) {
18265            final int N = mUserSwitchObservers.beginBroadcast();
18266            for (int i=0; i<N; i++) {
18267                try {
18268                    mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(newUserId);
18269                } catch (RemoteException e) {
18270                }
18271            }
18272            mUserSwitchObservers.finishBroadcast();
18273        }
18274    }
18275
18276    void scheduleStartProfilesLocked() {
18277        if (!mHandler.hasMessages(START_PROFILES_MSG)) {
18278            mHandler.sendMessageDelayed(mHandler.obtainMessage(START_PROFILES_MSG),
18279                    DateUtils.SECOND_IN_MILLIS);
18280        }
18281    }
18282
18283    void startProfilesLocked() {
18284        if (DEBUG_MU) Slog.i(TAG_MU, "startProfilesLocked");
18285        List<UserInfo> profiles = getUserManagerLocked().getProfiles(
18286                mCurrentUserId, false /* enabledOnly */);
18287        List<UserInfo> toStart = new ArrayList<UserInfo>(profiles.size());
18288        for (UserInfo user : profiles) {
18289            if ((user.flags & UserInfo.FLAG_INITIALIZED) == UserInfo.FLAG_INITIALIZED
18290                    && user.id != mCurrentUserId) {
18291                toStart.add(user);
18292            }
18293        }
18294        final int n = toStart.size();
18295        int i = 0;
18296        for (; i < n && i < (MAX_RUNNING_USERS - 1); ++i) {
18297            startUserInBackground(toStart.get(i).id);
18298        }
18299        if (i < n) {
18300            Slog.w(TAG_MU, "More profiles than MAX_RUNNING_USERS");
18301        }
18302    }
18303
18304    void finishUserBoot(UserStartedState uss) {
18305        synchronized (this) {
18306            if (uss.mState == UserStartedState.STATE_BOOTING
18307                    && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) {
18308                uss.mState = UserStartedState.STATE_RUNNING;
18309                final int userId = uss.mHandle.getIdentifier();
18310                Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null);
18311                intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
18312                intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT);
18313                broadcastIntentLocked(null, null, intent,
18314                        null, null, 0, null, null,
18315                        android.Manifest.permission.RECEIVE_BOOT_COMPLETED, AppOpsManager.OP_NONE,
18316                        true, false, MY_PID, Process.SYSTEM_UID, userId);
18317            }
18318        }
18319    }
18320
18321    void finishUserSwitch(UserStartedState uss) {
18322        synchronized (this) {
18323            finishUserBoot(uss);
18324
18325            startProfilesLocked();
18326
18327            int num = mUserLru.size();
18328            int i = 0;
18329            while (num > MAX_RUNNING_USERS && i < mUserLru.size()) {
18330                Integer oldUserId = mUserLru.get(i);
18331                UserStartedState oldUss = mStartedUsers.get(oldUserId);
18332                if (oldUss == null) {
18333                    // Shouldn't happen, but be sane if it does.
18334                    mUserLru.remove(i);
18335                    num--;
18336                    continue;
18337                }
18338                if (oldUss.mState == UserStartedState.STATE_STOPPING
18339                        || oldUss.mState == UserStartedState.STATE_SHUTDOWN) {
18340                    // This user is already stopping, doesn't count.
18341                    num--;
18342                    i++;
18343                    continue;
18344                }
18345                if (oldUserId == UserHandle.USER_OWNER || oldUserId == mCurrentUserId) {
18346                    // Owner and current can't be stopped, but count as running.
18347                    i++;
18348                    continue;
18349                }
18350                // This is a user to be stopped.
18351                stopUserLocked(oldUserId, null);
18352                num--;
18353                i++;
18354            }
18355        }
18356    }
18357
18358    @Override
18359    public int stopUser(final int userId, final IStopUserCallback callback) {
18360        if (checkCallingPermission(INTERACT_ACROSS_USERS_FULL)
18361                != PackageManager.PERMISSION_GRANTED) {
18362            String msg = "Permission Denial: switchUser() from pid="
18363                    + Binder.getCallingPid()
18364                    + ", uid=" + Binder.getCallingUid()
18365                    + " requires " + INTERACT_ACROSS_USERS_FULL;
18366            Slog.w(TAG, msg);
18367            throw new SecurityException(msg);
18368        }
18369        if (userId <= 0) {
18370            throw new IllegalArgumentException("Can't stop primary user " + userId);
18371        }
18372        synchronized (this) {
18373            return stopUserLocked(userId, callback);
18374        }
18375    }
18376
18377    private int stopUserLocked(final int userId, final IStopUserCallback callback) {
18378        if (DEBUG_MU) Slog.i(TAG_MU, "stopUserLocked userId=" + userId);
18379        if (mCurrentUserId == userId && mTargetUserId == UserHandle.USER_NULL) {
18380            return ActivityManager.USER_OP_IS_CURRENT;
18381        }
18382
18383        final UserStartedState uss = mStartedUsers.get(userId);
18384        if (uss == null) {
18385            // User is not started, nothing to do...  but we do need to
18386            // callback if requested.
18387            if (callback != null) {
18388                mHandler.post(new Runnable() {
18389                    @Override
18390                    public void run() {
18391                        try {
18392                            callback.userStopped(userId);
18393                        } catch (RemoteException e) {
18394                        }
18395                    }
18396                });
18397            }
18398            return ActivityManager.USER_OP_SUCCESS;
18399        }
18400
18401        if (callback != null) {
18402            uss.mStopCallbacks.add(callback);
18403        }
18404
18405        if (uss.mState != UserStartedState.STATE_STOPPING
18406                && uss.mState != UserStartedState.STATE_SHUTDOWN) {
18407            uss.mState = UserStartedState.STATE_STOPPING;
18408            updateStartedUserArrayLocked();
18409
18410            long ident = Binder.clearCallingIdentity();
18411            try {
18412                // We are going to broadcast ACTION_USER_STOPPING and then
18413                // once that is done send a final ACTION_SHUTDOWN and then
18414                // stop the user.
18415                final Intent stoppingIntent = new Intent(Intent.ACTION_USER_STOPPING);
18416                stoppingIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
18417                stoppingIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
18418                stoppingIntent.putExtra(Intent.EXTRA_SHUTDOWN_USERSPACE_ONLY, true);
18419                final Intent shutdownIntent = new Intent(Intent.ACTION_SHUTDOWN);
18420                // This is the result receiver for the final shutdown broadcast.
18421                final IIntentReceiver shutdownReceiver = new IIntentReceiver.Stub() {
18422                    @Override
18423                    public void performReceive(Intent intent, int resultCode, String data,
18424                            Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
18425                        finishUserStop(uss);
18426                    }
18427                };
18428                // This is the result receiver for the initial stopping broadcast.
18429                final IIntentReceiver stoppingReceiver = new IIntentReceiver.Stub() {
18430                    @Override
18431                    public void performReceive(Intent intent, int resultCode, String data,
18432                            Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
18433                        // On to the next.
18434                        synchronized (ActivityManagerService.this) {
18435                            if (uss.mState != UserStartedState.STATE_STOPPING) {
18436                                // Whoops, we are being started back up.  Abort, abort!
18437                                return;
18438                            }
18439                            uss.mState = UserStartedState.STATE_SHUTDOWN;
18440                        }
18441                        mBatteryStatsService.noteEvent(
18442                                BatteryStats.HistoryItem.EVENT_USER_RUNNING_FINISH,
18443                                Integer.toString(userId), userId);
18444                        mSystemServiceManager.stopUser(userId);
18445                        broadcastIntentLocked(null, null, shutdownIntent,
18446                                null, shutdownReceiver, 0, null, null, null, AppOpsManager.OP_NONE,
18447                                true, false, MY_PID, Process.SYSTEM_UID, userId);
18448                    }
18449                };
18450                // Kick things off.
18451                broadcastIntentLocked(null, null, stoppingIntent,
18452                        null, stoppingReceiver, 0, null, null,
18453                        INTERACT_ACROSS_USERS, AppOpsManager.OP_NONE,
18454                        true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
18455            } finally {
18456                Binder.restoreCallingIdentity(ident);
18457            }
18458        }
18459
18460        return ActivityManager.USER_OP_SUCCESS;
18461    }
18462
18463    void finishUserStop(UserStartedState uss) {
18464        final int userId = uss.mHandle.getIdentifier();
18465        boolean stopped;
18466        ArrayList<IStopUserCallback> callbacks;
18467        synchronized (this) {
18468            callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks);
18469            if (mStartedUsers.get(userId) != uss) {
18470                stopped = false;
18471            } else if (uss.mState != UserStartedState.STATE_SHUTDOWN) {
18472                stopped = false;
18473            } else {
18474                stopped = true;
18475                // User can no longer run.
18476                mStartedUsers.remove(userId);
18477                mUserLru.remove(Integer.valueOf(userId));
18478                updateStartedUserArrayLocked();
18479
18480                // Clean up all state and processes associated with the user.
18481                // Kill all the processes for the user.
18482                forceStopUserLocked(userId, "finish user");
18483            }
18484
18485            // Explicitly remove the old information in mRecentTasks.
18486            removeRecentTasksForUserLocked(userId);
18487        }
18488
18489        for (int i=0; i<callbacks.size(); i++) {
18490            try {
18491                if (stopped) callbacks.get(i).userStopped(userId);
18492                else callbacks.get(i).userStopAborted(userId);
18493            } catch (RemoteException e) {
18494            }
18495        }
18496
18497        if (stopped) {
18498            mSystemServiceManager.cleanupUser(userId);
18499            synchronized (this) {
18500                mStackSupervisor.removeUserLocked(userId);
18501            }
18502        }
18503    }
18504
18505    @Override
18506    public UserInfo getCurrentUser() {
18507        if ((checkCallingPermission(INTERACT_ACROSS_USERS)
18508                != PackageManager.PERMISSION_GRANTED) && (
18509                checkCallingPermission(INTERACT_ACROSS_USERS_FULL)
18510                != PackageManager.PERMISSION_GRANTED)) {
18511            String msg = "Permission Denial: getCurrentUser() from pid="
18512                    + Binder.getCallingPid()
18513                    + ", uid=" + Binder.getCallingUid()
18514                    + " requires " + INTERACT_ACROSS_USERS;
18515            Slog.w(TAG, msg);
18516            throw new SecurityException(msg);
18517        }
18518        synchronized (this) {
18519            int userId = mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId;
18520            return getUserManagerLocked().getUserInfo(userId);
18521        }
18522    }
18523
18524    int getCurrentUserIdLocked() {
18525        return mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId;
18526    }
18527
18528    @Override
18529    public boolean isUserRunning(int userId, boolean orStopped) {
18530        if (checkCallingPermission(INTERACT_ACROSS_USERS)
18531                != PackageManager.PERMISSION_GRANTED) {
18532            String msg = "Permission Denial: isUserRunning() from pid="
18533                    + Binder.getCallingPid()
18534                    + ", uid=" + Binder.getCallingUid()
18535                    + " requires " + INTERACT_ACROSS_USERS;
18536            Slog.w(TAG, msg);
18537            throw new SecurityException(msg);
18538        }
18539        synchronized (this) {
18540            return isUserRunningLocked(userId, orStopped);
18541        }
18542    }
18543
18544    boolean isUserRunningLocked(int userId, boolean orStopped) {
18545        UserStartedState state = mStartedUsers.get(userId);
18546        if (state == null) {
18547            return false;
18548        }
18549        if (orStopped) {
18550            return true;
18551        }
18552        return state.mState != UserStartedState.STATE_STOPPING
18553                && state.mState != UserStartedState.STATE_SHUTDOWN;
18554    }
18555
18556    @Override
18557    public int[] getRunningUserIds() {
18558        if (checkCallingPermission(INTERACT_ACROSS_USERS)
18559                != PackageManager.PERMISSION_GRANTED) {
18560            String msg = "Permission Denial: isUserRunning() from pid="
18561                    + Binder.getCallingPid()
18562                    + ", uid=" + Binder.getCallingUid()
18563                    + " requires " + INTERACT_ACROSS_USERS;
18564            Slog.w(TAG, msg);
18565            throw new SecurityException(msg);
18566        }
18567        synchronized (this) {
18568            return mStartedUserArray;
18569        }
18570    }
18571
18572    private void updateStartedUserArrayLocked() {
18573        int num = 0;
18574        for (int i=0; i<mStartedUsers.size();  i++) {
18575            UserStartedState uss = mStartedUsers.valueAt(i);
18576            // This list does not include stopping users.
18577            if (uss.mState != UserStartedState.STATE_STOPPING
18578                    && uss.mState != UserStartedState.STATE_SHUTDOWN) {
18579                num++;
18580            }
18581        }
18582        mStartedUserArray = new int[num];
18583        num = 0;
18584        for (int i=0; i<mStartedUsers.size();  i++) {
18585            UserStartedState uss = mStartedUsers.valueAt(i);
18586            if (uss.mState != UserStartedState.STATE_STOPPING
18587                    && uss.mState != UserStartedState.STATE_SHUTDOWN) {
18588                mStartedUserArray[num] = mStartedUsers.keyAt(i);
18589                num++;
18590            }
18591        }
18592    }
18593
18594    @Override
18595    public void registerUserSwitchObserver(IUserSwitchObserver observer) {
18596        if (checkCallingPermission(INTERACT_ACROSS_USERS_FULL)
18597                != PackageManager.PERMISSION_GRANTED) {
18598            String msg = "Permission Denial: registerUserSwitchObserver() from pid="
18599                    + Binder.getCallingPid()
18600                    + ", uid=" + Binder.getCallingUid()
18601                    + " requires " + INTERACT_ACROSS_USERS_FULL;
18602            Slog.w(TAG, msg);
18603            throw new SecurityException(msg);
18604        }
18605
18606        mUserSwitchObservers.register(observer);
18607    }
18608
18609    @Override
18610    public void unregisterUserSwitchObserver(IUserSwitchObserver observer) {
18611        mUserSwitchObservers.unregister(observer);
18612    }
18613
18614    private boolean userExists(int userId) {
18615        if (userId == 0) {
18616            return true;
18617        }
18618        UserManagerService ums = getUserManagerLocked();
18619        return ums != null ? (ums.getUserInfo(userId) != null) : false;
18620    }
18621
18622    int[] getUsersLocked() {
18623        UserManagerService ums = getUserManagerLocked();
18624        return ums != null ? ums.getUserIds() : new int[] { 0 };
18625    }
18626
18627    UserManagerService getUserManagerLocked() {
18628        if (mUserManager == null) {
18629            IBinder b = ServiceManager.getService(Context.USER_SERVICE);
18630            mUserManager = (UserManagerService)IUserManager.Stub.asInterface(b);
18631        }
18632        return mUserManager;
18633    }
18634
18635    private int applyUserId(int uid, int userId) {
18636        return UserHandle.getUid(userId, uid);
18637    }
18638
18639    ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) {
18640        if (info == null) return null;
18641        ApplicationInfo newInfo = new ApplicationInfo(info);
18642        newInfo.uid = applyUserId(info.uid, userId);
18643        newInfo.dataDir = USER_DATA_DIR + userId + "/"
18644                + info.packageName;
18645        return newInfo;
18646    }
18647
18648    ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) {
18649        if (aInfo == null
18650                || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) {
18651            return aInfo;
18652        }
18653
18654        ActivityInfo info = new ActivityInfo(aInfo);
18655        info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId);
18656        return info;
18657    }
18658
18659    private final class LocalService extends ActivityManagerInternal {
18660        @Override
18661        public void goingToSleep() {
18662            ActivityManagerService.this.goingToSleep();
18663        }
18664
18665        @Override
18666        public void wakingUp() {
18667            ActivityManagerService.this.wakingUp();
18668        }
18669
18670        @Override
18671        public int startIsolatedProcess(String entryPoint, String[] entryPointArgs,
18672                String processName, String abiOverride, int uid, Runnable crashHandler) {
18673            return ActivityManagerService.this.startIsolatedProcess(entryPoint, entryPointArgs,
18674                    processName, abiOverride, uid, crashHandler);
18675        }
18676    }
18677
18678    /**
18679     * An implementation of IAppTask, that allows an app to manage its own tasks via
18680     * {@link android.app.ActivityManager.AppTask}.  We keep track of the callingUid to ensure that
18681     * only the process that calls getAppTasks() can call the AppTask methods.
18682     */
18683    class AppTaskImpl extends IAppTask.Stub {
18684        private int mTaskId;
18685        private int mCallingUid;
18686
18687        public AppTaskImpl(int taskId, int callingUid) {
18688            mTaskId = taskId;
18689            mCallingUid = callingUid;
18690        }
18691
18692        private void checkCaller() {
18693            if (mCallingUid != Binder.getCallingUid()) {
18694                throw new SecurityException("Caller " + mCallingUid
18695                        + " does not match caller of getAppTasks(): " + Binder.getCallingUid());
18696            }
18697        }
18698
18699        @Override
18700        public void finishAndRemoveTask() {
18701            checkCaller();
18702
18703            synchronized (ActivityManagerService.this) {
18704                long origId = Binder.clearCallingIdentity();
18705                try {
18706                    TaskRecord tr = recentTaskForIdLocked(mTaskId);
18707                    if (tr == null) {
18708                        throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
18709                    }
18710                    // Only kill the process if we are not a new document
18711                    int flags = tr.getBaseIntent().getFlags();
18712                    boolean isDocument = (flags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) ==
18713                            Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
18714                    removeTaskByIdLocked(mTaskId,
18715                            !isDocument ? ActivityManager.REMOVE_TASK_KILL_PROCESS : 0);
18716                } finally {
18717                    Binder.restoreCallingIdentity(origId);
18718                }
18719            }
18720        }
18721
18722        @Override
18723        public ActivityManager.RecentTaskInfo getTaskInfo() {
18724            checkCaller();
18725
18726            synchronized (ActivityManagerService.this) {
18727                long origId = Binder.clearCallingIdentity();
18728                try {
18729                    TaskRecord tr = recentTaskForIdLocked(mTaskId);
18730                    if (tr == null) {
18731                        throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
18732                    }
18733                    return createRecentTaskInfoFromTaskRecord(tr);
18734                } finally {
18735                    Binder.restoreCallingIdentity(origId);
18736                }
18737            }
18738        }
18739
18740        @Override
18741        public void moveToFront() {
18742            checkCaller();
18743
18744            final TaskRecord tr;
18745            synchronized (ActivityManagerService.this) {
18746                tr = recentTaskForIdLocked(mTaskId);
18747                if (tr == null) {
18748                    throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
18749                }
18750                if (tr.getRootActivity() != null) {
18751                    long origId = Binder.clearCallingIdentity();
18752                    try {
18753                        moveTaskToFrontLocked(tr.taskId, 0, null);
18754                        return;
18755                    } finally {
18756                        Binder.restoreCallingIdentity(origId);
18757                    }
18758                }
18759            }
18760
18761            startActivityFromRecentsInner(tr.taskId, null);
18762        }
18763
18764        @Override
18765        public int startActivity(IBinder whoThread, String callingPackage,
18766                Intent intent, String resolvedType, Bundle options) {
18767            checkCaller();
18768
18769            int callingUser = UserHandle.getCallingUserId();
18770            TaskRecord tr;
18771            IApplicationThread appThread;
18772            synchronized (ActivityManagerService.this) {
18773                tr = recentTaskForIdLocked(mTaskId);
18774                if (tr == null) {
18775                    throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
18776                }
18777                appThread = ApplicationThreadNative.asInterface(whoThread);
18778                if (appThread == null) {
18779                    throw new IllegalArgumentException("Bad app thread " + appThread);
18780                }
18781            }
18782            return mStackSupervisor.startActivityMayWait(appThread, -1, callingPackage, intent,
18783                    resolvedType, null, null, null, null, 0, 0, null, null,
18784                    null, options, callingUser, null, tr);
18785        }
18786
18787        @Override
18788        public void setExcludeFromRecents(boolean exclude) {
18789            checkCaller();
18790
18791            synchronized (ActivityManagerService.this) {
18792                long origId = Binder.clearCallingIdentity();
18793                try {
18794                    TaskRecord tr = recentTaskForIdLocked(mTaskId);
18795                    if (tr == null) {
18796                        throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
18797                    }
18798                    Intent intent = tr.getBaseIntent();
18799                    if (exclude) {
18800                        intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
18801                    } else {
18802                        intent.setFlags(intent.getFlags()
18803                                & ~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
18804                    }
18805                } finally {
18806                    Binder.restoreCallingIdentity(origId);
18807                }
18808            }
18809        }
18810    }
18811}
18812