Home | History | Annotate | Download | only in am
      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 
     17 package com.android.server.am;
     18 
     19 import static android.Manifest.permission.INTERACT_ACROSS_USERS;
     20 import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
     21 import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
     22 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
     23 import static com.android.internal.util.XmlUtils.readBooleanAttribute;
     24 import static com.android.internal.util.XmlUtils.readIntAttribute;
     25 import static com.android.internal.util.XmlUtils.readLongAttribute;
     26 import static com.android.internal.util.XmlUtils.writeBooleanAttribute;
     27 import static com.android.internal.util.XmlUtils.writeIntAttribute;
     28 import static com.android.internal.util.XmlUtils.writeLongAttribute;
     29 import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
     30 import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
     31 import static org.xmlpull.v1.XmlPullParser.START_TAG;
     32 import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
     33 import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
     34 
     35 import android.Manifest;
     36 import android.app.AppOpsManager;
     37 import android.app.ApplicationThreadNative;
     38 import android.app.IActivityContainer;
     39 import android.app.IActivityContainerCallback;
     40 import android.app.IAppTask;
     41 import android.app.ITaskStackListener;
     42 import android.app.ProfilerInfo;
     43 import android.app.admin.DevicePolicyManager;
     44 import android.app.usage.UsageEvents;
     45 import android.app.usage.UsageStatsManagerInternal;
     46 import android.appwidget.AppWidgetManager;
     47 import android.content.res.Resources;
     48 import android.graphics.Bitmap;
     49 import android.graphics.Point;
     50 import android.graphics.Rect;
     51 import android.os.BatteryStats;
     52 import android.os.PersistableBundle;
     53 import android.os.storage.IMountService;
     54 import android.os.storage.StorageManager;
     55 import android.service.voice.IVoiceInteractionSession;
     56 import android.util.ArrayMap;
     57 import android.util.ArraySet;
     58 import android.util.SparseIntArray;
     59 
     60 import android.view.Display;
     61 import com.android.internal.R;
     62 import com.android.internal.annotations.GuardedBy;
     63 import com.android.internal.app.IAppOpsService;
     64 import com.android.internal.app.IVoiceInteractor;
     65 import com.android.internal.app.ProcessMap;
     66 import com.android.internal.app.ProcessStats;
     67 import com.android.internal.os.BackgroundThread;
     68 import com.android.internal.os.BatteryStatsImpl;
     69 import com.android.internal.os.ProcessCpuTracker;
     70 import com.android.internal.os.TransferPipe;
     71 import com.android.internal.os.Zygote;
     72 import com.android.internal.util.FastPrintWriter;
     73 import com.android.internal.util.FastXmlSerializer;
     74 import com.android.internal.util.MemInfoReader;
     75 import com.android.internal.util.Preconditions;
     76 import com.android.server.AppOpsService;
     77 import com.android.server.AttributeCache;
     78 import com.android.server.IntentResolver;
     79 import com.android.server.LocalServices;
     80 import com.android.server.ServiceThread;
     81 import com.android.server.SystemService;
     82 import com.android.server.SystemServiceManager;
     83 import com.android.server.Watchdog;
     84 import com.android.server.am.ActivityStack.ActivityState;
     85 import com.android.server.firewall.IntentFirewall;
     86 import com.android.server.pm.Installer;
     87 import com.android.server.pm.UserManagerService;
     88 import com.android.server.statusbar.StatusBarManagerInternal;
     89 import com.android.server.wm.AppTransition;
     90 import com.android.server.wm.WindowManagerService;
     91 import com.google.android.collect.Lists;
     92 import com.google.android.collect.Maps;
     93 
     94 import libcore.io.IoUtils;
     95 
     96 import org.xmlpull.v1.XmlPullParser;
     97 import org.xmlpull.v1.XmlPullParserException;
     98 import org.xmlpull.v1.XmlSerializer;
     99 
    100 import android.app.Activity;
    101 import android.app.ActivityManager;
    102 import android.app.ActivityManager.RunningTaskInfo;
    103 import android.app.ActivityManager.StackInfo;
    104 import android.app.ActivityManagerInternal;
    105 import android.app.ActivityManagerNative;
    106 import android.app.ActivityOptions;
    107 import android.app.ActivityThread;
    108 import android.app.AlertDialog;
    109 import android.app.AppGlobals;
    110 import android.app.ApplicationErrorReport;
    111 import android.app.Dialog;
    112 import android.app.IActivityController;
    113 import android.app.IApplicationThread;
    114 import android.app.IInstrumentationWatcher;
    115 import android.app.INotificationManager;
    116 import android.app.IProcessObserver;
    117 import android.app.IServiceConnection;
    118 import android.app.IStopUserCallback;
    119 import android.app.IUiAutomationConnection;
    120 import android.app.IUserSwitchObserver;
    121 import android.app.Instrumentation;
    122 import android.app.Notification;
    123 import android.app.NotificationManager;
    124 import android.app.PendingIntent;
    125 import android.app.backup.IBackupManager;
    126 import android.content.ActivityNotFoundException;
    127 import android.content.BroadcastReceiver;
    128 import android.content.ClipData;
    129 import android.content.ComponentCallbacks2;
    130 import android.content.ComponentName;
    131 import android.content.ContentProvider;
    132 import android.content.ContentResolver;
    133 import android.content.Context;
    134 import android.content.DialogInterface;
    135 import android.content.IContentProvider;
    136 import android.content.IIntentReceiver;
    137 import android.content.IIntentSender;
    138 import android.content.Intent;
    139 import android.content.IntentFilter;
    140 import android.content.IntentSender;
    141 import android.content.pm.ActivityInfo;
    142 import android.content.pm.ApplicationInfo;
    143 import android.content.pm.ConfigurationInfo;
    144 import android.content.pm.IPackageDataObserver;
    145 import android.content.pm.IPackageManager;
    146 import android.content.pm.InstrumentationInfo;
    147 import android.content.pm.PackageInfo;
    148 import android.content.pm.PackageManager;
    149 import android.content.pm.ParceledListSlice;
    150 import android.content.pm.UserInfo;
    151 import android.content.pm.PackageManager.NameNotFoundException;
    152 import android.content.pm.PathPermission;
    153 import android.content.pm.ProviderInfo;
    154 import android.content.pm.ResolveInfo;
    155 import android.content.pm.ServiceInfo;
    156 import android.content.res.CompatibilityInfo;
    157 import android.content.res.Configuration;
    158 import android.net.Proxy;
    159 import android.net.ProxyInfo;
    160 import android.net.Uri;
    161 import android.os.Binder;
    162 import android.os.Build;
    163 import android.os.Bundle;
    164 import android.os.Debug;
    165 import android.os.DropBoxManager;
    166 import android.os.Environment;
    167 import android.os.FactoryTest;
    168 import android.os.FileObserver;
    169 import android.os.FileUtils;
    170 import android.os.Handler;
    171 import android.os.IBinder;
    172 import android.os.IPermissionController;
    173 import android.os.IRemoteCallback;
    174 import android.os.IUserManager;
    175 import android.os.Looper;
    176 import android.os.Message;
    177 import android.os.Parcel;
    178 import android.os.ParcelFileDescriptor;
    179 import android.os.PowerManagerInternal;
    180 import android.os.Process;
    181 import android.os.RemoteCallbackList;
    182 import android.os.RemoteException;
    183 import android.os.SELinux;
    184 import android.os.ServiceManager;
    185 import android.os.StrictMode;
    186 import android.os.SystemClock;
    187 import android.os.SystemProperties;
    188 import android.os.UpdateLock;
    189 import android.os.UserHandle;
    190 import android.os.UserManager;
    191 import android.provider.Settings;
    192 import android.text.format.DateUtils;
    193 import android.text.format.Time;
    194 import android.util.AtomicFile;
    195 import android.util.EventLog;
    196 import android.util.Log;
    197 import android.util.Pair;
    198 import android.util.PrintWriterPrinter;
    199 import android.util.Slog;
    200 import android.util.SparseArray;
    201 import android.util.TimeUtils;
    202 import android.util.Xml;
    203 import android.view.Gravity;
    204 import android.view.LayoutInflater;
    205 import android.view.View;
    206 import android.view.WindowManager;
    207 
    208 import dalvik.system.VMRuntime;
    209 
    210 import java.io.BufferedInputStream;
    211 import java.io.BufferedOutputStream;
    212 import java.io.DataInputStream;
    213 import java.io.DataOutputStream;
    214 import java.io.File;
    215 import java.io.FileDescriptor;
    216 import java.io.FileInputStream;
    217 import java.io.FileNotFoundException;
    218 import java.io.FileOutputStream;
    219 import java.io.IOException;
    220 import java.io.InputStreamReader;
    221 import java.io.PrintWriter;
    222 import java.io.StringWriter;
    223 import java.lang.ref.WeakReference;
    224 import java.util.ArrayList;
    225 import java.util.Arrays;
    226 import java.util.Collections;
    227 import java.util.Comparator;
    228 import java.util.HashMap;
    229 import java.util.HashSet;
    230 import java.util.Iterator;
    231 import java.util.List;
    232 import java.util.Locale;
    233 import java.util.Map;
    234 import java.util.Set;
    235 import java.util.concurrent.atomic.AtomicBoolean;
    236 import java.util.concurrent.atomic.AtomicLong;
    237 
    238 public final class ActivityManagerService extends ActivityManagerNative
    239         implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
    240 
    241     private static final String USER_DATA_DIR = "/data/user/";
    242     // File that stores last updated system version and called preboot receivers
    243     static final String CALLED_PRE_BOOTS_FILENAME = "called_pre_boots.dat";
    244 
    245     static final String TAG = "ActivityManager";
    246     static final String TAG_MU = "ActivityManagerServiceMU";
    247     static final boolean DEBUG = false;
    248     static final boolean localLOGV = DEBUG;
    249     static final boolean DEBUG_BACKUP = localLOGV || false;
    250     static final boolean DEBUG_BROADCAST = localLOGV || false;
    251     static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
    252     static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false;
    253     static final boolean DEBUG_CLEANUP = localLOGV || false;
    254     static final boolean DEBUG_CONFIGURATION = localLOGV || false;
    255     static final boolean DEBUG_FOCUS = false;
    256     static final boolean DEBUG_IMMERSIVE = localLOGV || false;
    257     static final boolean DEBUG_MU = localLOGV || false;
    258     static final boolean DEBUG_OOM_ADJ = localLOGV || false;
    259     static final boolean DEBUG_LRU = localLOGV || false;
    260     static final boolean DEBUG_PAUSE = localLOGV || false;
    261     static final boolean DEBUG_POWER = localLOGV || false;
    262     static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false;
    263     static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false;
    264     static final boolean DEBUG_PROCESSES = localLOGV || false;
    265     static final boolean DEBUG_PROVIDER = localLOGV || false;
    266     static final boolean DEBUG_RESULTS = localLOGV || false;
    267     static final boolean DEBUG_SERVICE = localLOGV || false;
    268     static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false;
    269     static final boolean DEBUG_STACK = localLOGV || false;
    270     static final boolean DEBUG_SWITCH = localLOGV || false;
    271     static final boolean DEBUG_TASKS = localLOGV || false;
    272     static final boolean DEBUG_THUMBNAILS = localLOGV || false;
    273     static final boolean DEBUG_TRANSITION = localLOGV || false;
    274     static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
    275     static final boolean DEBUG_USER_LEAVING = localLOGV || false;
    276     static final boolean DEBUG_VISBILITY = localLOGV || false;
    277     static final boolean DEBUG_PSS = localLOGV || false;
    278     static final boolean DEBUG_LOCKSCREEN = localLOGV || false;
    279     static final boolean DEBUG_RECENTS = localLOGV || false;
    280     static final boolean VALIDATE_TOKENS = false;
    281     static final boolean SHOW_ACTIVITY_START_TIME = true;
    282 
    283     // Control over CPU and battery monitoring.
    284     static final long BATTERY_STATS_TIME = 30*60*1000;      // write battery stats every 30 minutes.
    285     static final boolean MONITOR_CPU_USAGE = true;
    286     static final long MONITOR_CPU_MIN_TIME = 5*1000;        // don't sample cpu less than every 5 seconds.
    287     static final long MONITOR_CPU_MAX_TIME = 0x0fffffff;    // wait possibly forever for next cpu sample.
    288     static final boolean MONITOR_THREAD_CPU_USAGE = false;
    289 
    290     // The flags that are set for all calls we make to the package manager.
    291     static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
    292 
    293     private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
    294 
    295     static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
    296 
    297     // Maximum number recent bitmaps to keep in memory.
    298     static final int MAX_RECENT_BITMAPS = 3;
    299 
    300     // Amount of time after a call to stopAppSwitches() during which we will
    301     // prevent further untrusted switches from happening.
    302     static final long APP_SWITCH_DELAY_TIME = 5*1000;
    303 
    304     // How long we wait for a launched process to attach to the activity manager
    305     // before we decide it's never going to come up for real.
    306     static final int PROC_START_TIMEOUT = 10*1000;
    307 
    308     // How long we wait for a launched process to attach to the activity manager
    309     // before we decide it's never going to come up for real, when the process was
    310     // started with a wrapper for instrumentation (such as Valgrind) because it
    311     // could take much longer than usual.
    312     static final int PROC_START_TIMEOUT_WITH_WRAPPER = 1200*1000;
    313 
    314     // How long to wait after going idle before forcing apps to GC.
    315     static final int GC_TIMEOUT = 5*1000;
    316 
    317     // The minimum amount of time between successive GC requests for a process.
    318     static final int GC_MIN_INTERVAL = 60*1000;
    319 
    320     // The minimum amount of time between successive PSS requests for a process.
    321     static final int FULL_PSS_MIN_INTERVAL = 10*60*1000;
    322 
    323     // The minimum amount of time between successive PSS requests for a process
    324     // when the request is due to the memory state being lowered.
    325     static final int FULL_PSS_LOWERED_INTERVAL = 2*60*1000;
    326 
    327     // The rate at which we check for apps using excessive power -- 15 mins.
    328     static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;
    329 
    330     // The minimum sample duration we will allow before deciding we have
    331     // enough data on wake locks to start killing things.
    332     static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
    333 
    334     // The minimum sample duration we will allow before deciding we have
    335     // enough data on CPU usage to start killing things.
    336     static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
    337 
    338     // How long we allow a receiver to run before giving up on it.
    339     static final int BROADCAST_FG_TIMEOUT = 10*1000;
    340     static final int BROADCAST_BG_TIMEOUT = 60*1000;
    341 
    342     // How long we wait until we timeout on key dispatching.
    343     static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
    344 
    345     // How long we wait until we timeout on key dispatching during instrumentation.
    346     static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
    347 
    348     // Amount of time we wait for observers to handle a user switch before
    349     // giving up on them and unfreezing the screen.
    350     static final int USER_SWITCH_TIMEOUT = 2*1000;
    351 
    352     // Maximum number of users we allow to be running at a time.
    353     static final int MAX_RUNNING_USERS = 3;
    354 
    355     // How long to wait in getAssistContextExtras for the activity and foreground services
    356     // to respond with the result.
    357     static final int PENDING_ASSIST_EXTRAS_TIMEOUT = 500;
    358 
    359     // Maximum number of persisted Uri grants a package is allowed
    360     static final int MAX_PERSISTED_URI_GRANTS = 128;
    361 
    362     static final int MY_PID = Process.myPid();
    363 
    364     static final String[] EMPTY_STRING_ARRAY = new String[0];
    365 
    366     // How many bytes to write into the dropbox log before truncating
    367     static final int DROPBOX_MAX_SIZE = 256 * 1024;
    368 
    369     // Access modes for handleIncomingUser.
    370     static final int ALLOW_NON_FULL = 0;
    371     static final int ALLOW_NON_FULL_IN_PROFILE = 1;
    372     static final int ALLOW_FULL_ONLY = 2;
    373 
    374     static final int LAST_PREBOOT_DELIVERED_FILE_VERSION = 10000;
    375 
    376     // Delay in notifying task stack change listeners (in millis)
    377     static final int NOTIFY_TASK_STACK_CHANGE_LISTENERS_DELAY = 1000;
    378 
    379     /** All system services */
    380     SystemServiceManager mSystemServiceManager;
    381 
    382     private Installer mInstaller;
    383 
    384     /** Run all ActivityStacks through this */
    385     ActivityStackSupervisor mStackSupervisor;
    386 
    387     /** Task stack change listeners. */
    388     private RemoteCallbackList<ITaskStackListener> mTaskStackListeners =
    389             new RemoteCallbackList<ITaskStackListener>();
    390 
    391     public IntentFirewall mIntentFirewall;
    392 
    393     // Whether we should show our dialogs (ANR, crash, etc) or just perform their
    394     // default actuion automatically.  Important for devices without direct input
    395     // devices.
    396     private boolean mShowDialogs = true;
    397 
    398     BroadcastQueue mFgBroadcastQueue;
    399     BroadcastQueue mBgBroadcastQueue;
    400     // Convenient for easy iteration over the queues. Foreground is first
    401     // so that dispatch of foreground broadcasts gets precedence.
    402     final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2];
    403 
    404     BroadcastQueue broadcastQueueForIntent(Intent intent) {
    405         final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0;
    406         if (DEBUG_BACKGROUND_BROADCAST) {
    407             Slog.i(TAG, "Broadcast intent " + intent + " on "
    408                     + (isFg ? "foreground" : "background")
    409                     + " queue");
    410         }
    411         return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue;
    412     }
    413 
    414     BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) {
    415         for (BroadcastQueue queue : mBroadcastQueues) {
    416             BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver);
    417             if (r != null) {
    418                 return r;
    419             }
    420         }
    421         return null;
    422     }
    423 
    424     /**
    425      * Activity we have told the window manager to have key focus.
    426      */
    427     ActivityRecord mFocusedActivity = null;
    428 
    429     /**
    430      * List of intents that were used to start the most recent tasks.
    431      */
    432     ArrayList<TaskRecord> mRecentTasks;
    433     ArrayList<TaskRecord> mTmpRecents = new ArrayList<TaskRecord>();
    434 
    435     /**
    436      * For addAppTask: cached of the last activity component that was added.
    437      */
    438     ComponentName mLastAddedTaskComponent;
    439 
    440     /**
    441      * For addAppTask: cached of the last activity uid that was added.
    442      */
    443     int mLastAddedTaskUid;
    444 
    445     /**
    446      * For addAppTask: cached of the last ActivityInfo that was added.
    447      */
    448     ActivityInfo mLastAddedTaskActivity;
    449 
    450     public class PendingAssistExtras extends Binder implements Runnable {
    451         public final ActivityRecord activity;
    452         public final Bundle extras;
    453         public final Intent intent;
    454         public final String hint;
    455         public final int userHandle;
    456         public boolean haveResult = false;
    457         public Bundle result = null;
    458         public PendingAssistExtras(ActivityRecord _activity, Bundle _extras, Intent _intent,
    459                 String _hint, int _userHandle) {
    460             activity = _activity;
    461             extras = _extras;
    462             intent = _intent;
    463             hint = _hint;
    464             userHandle = _userHandle;
    465         }
    466         @Override
    467         public void run() {
    468             Slog.w(TAG, "getAssistContextExtras failed: timeout retrieving from " + activity);
    469             synchronized (this) {
    470                 haveResult = true;
    471                 notifyAll();
    472             }
    473         }
    474     }
    475 
    476     final ArrayList<PendingAssistExtras> mPendingAssistExtras
    477             = new ArrayList<PendingAssistExtras>();
    478 
    479     /**
    480      * Process management.
    481      */
    482     final ProcessList mProcessList = new ProcessList();
    483 
    484     /**
    485      * All of the applications we currently have running organized by name.
    486      * The keys are strings of the application package name (as
    487      * returned by the package manager), and the keys are ApplicationRecord
    488      * objects.
    489      */
    490     final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>();
    491 
    492     /**
    493      * Tracking long-term execution of processes to look for abuse and other
    494      * bad app behavior.
    495      */
    496     final ProcessStatsService mProcessStats;
    497 
    498     /**
    499      * The currently running isolated processes.
    500      */
    501     final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>();
    502 
    503     /**
    504      * Counter for assigning isolated process uids, to avoid frequently reusing the
    505      * same ones.
    506      */
    507     int mNextIsolatedProcessUid = 0;
    508 
    509     /**
    510      * The currently running heavy-weight process, if any.
    511      */
    512     ProcessRecord mHeavyWeightProcess = null;
    513 
    514     /**
    515      * The last time that various processes have crashed.
    516      */
    517     final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
    518 
    519     /**
    520      * Information about a process that is currently marked as bad.
    521      */
    522     static final class BadProcessInfo {
    523         BadProcessInfo(long time, String shortMsg, String longMsg, String stack) {
    524             this.time = time;
    525             this.shortMsg = shortMsg;
    526             this.longMsg = longMsg;
    527             this.stack = stack;
    528         }
    529 
    530         final long time;
    531         final String shortMsg;
    532         final String longMsg;
    533         final String stack;
    534     }
    535 
    536     /**
    537      * Set of applications that we consider to be bad, and will reject
    538      * incoming broadcasts from (which the user has no control over).
    539      * Processes are added to this set when they have crashed twice within
    540      * a minimum amount of time; they are removed from it when they are
    541      * later restarted (hopefully due to some user action).  The value is the
    542      * time it was added to the list.
    543      */
    544     final ProcessMap<BadProcessInfo> mBadProcesses = new ProcessMap<BadProcessInfo>();
    545 
    546     /**
    547      * All of the processes we currently have running organized by pid.
    548      * The keys are the pid running the application.
    549      *
    550      * <p>NOTE: This object is protected by its own lock, NOT the global
    551      * activity manager lock!
    552      */
    553     final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>();
    554 
    555     /**
    556      * All of the processes that have been forced to be foreground.  The key
    557      * is the pid of the caller who requested it (we hold a death
    558      * link on it).
    559      */
    560     abstract class ForegroundToken implements IBinder.DeathRecipient {
    561         int pid;
    562         IBinder token;
    563     }
    564     final SparseArray<ForegroundToken> mForegroundProcesses = new SparseArray<ForegroundToken>();
    565 
    566     /**
    567      * List of records for processes that someone had tried to start before the
    568      * system was ready.  We don't start them at that point, but ensure they
    569      * are started by the time booting is complete.
    570      */
    571     final ArrayList<ProcessRecord> mProcessesOnHold = new ArrayList<ProcessRecord>();
    572 
    573     /**
    574      * List of persistent applications that are in the process
    575      * of being started.
    576      */
    577     final ArrayList<ProcessRecord> mPersistentStartingProcesses = new ArrayList<ProcessRecord>();
    578 
    579     /**
    580      * Processes that are being forcibly torn down.
    581      */
    582     final ArrayList<ProcessRecord> mRemovedProcesses = new ArrayList<ProcessRecord>();
    583 
    584     /**
    585      * List of running applications, sorted by recent usage.
    586      * The first entry in the list is the least recently used.
    587      */
    588     final ArrayList<ProcessRecord> mLruProcesses = new ArrayList<ProcessRecord>();
    589 
    590     /**
    591      * Where in mLruProcesses that the processes hosting activities start.
    592      */
    593     int mLruProcessActivityStart = 0;
    594 
    595     /**
    596      * Where in mLruProcesses that the processes hosting services start.
    597      * This is after (lower index) than mLruProcessesActivityStart.
    598      */
    599     int mLruProcessServiceStart = 0;
    600 
    601     /**
    602      * List of processes that should gc as soon as things are idle.
    603      */
    604     final ArrayList<ProcessRecord> mProcessesToGc = new ArrayList<ProcessRecord>();
    605 
    606     /**
    607      * Processes we want to collect PSS data from.
    608      */
    609     final ArrayList<ProcessRecord> mPendingPssProcesses = new ArrayList<ProcessRecord>();
    610 
    611     /**
    612      * Last time we requested PSS data of all processes.
    613      */
    614     long mLastFullPssTime = SystemClock.uptimeMillis();
    615 
    616     /**
    617      * If set, the next time we collect PSS data we should do a full collection
    618      * with data from native processes and the kernel.
    619      */
    620     boolean mFullPssPending = false;
    621 
    622     /**
    623      * This is the process holding what we currently consider to be
    624      * the "home" activity.
    625      */
    626     ProcessRecord mHomeProcess;
    627 
    628     /**
    629      * This is the process holding the activity the user last visited that
    630      * is in a different process from the one they are currently in.
    631      */
    632     ProcessRecord mPreviousProcess;
    633 
    634     /**
    635      * The time at which the previous process was last visible.
    636      */
    637     long mPreviousProcessVisibleTime;
    638 
    639     /**
    640      * Which uses have been started, so are allowed to run code.
    641      */
    642     final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>();
    643 
    644     /**
    645      * LRU list of history of current users.  Most recently current is at the end.
    646      */
    647     final ArrayList<Integer> mUserLru = new ArrayList<Integer>();
    648 
    649     /**
    650      * Constant array of the users that are currently started.
    651      */
    652     int[] mStartedUserArray = new int[] { 0 };
    653 
    654     /**
    655      * Registered observers of the user switching mechanics.
    656      */
    657     final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers
    658             = new RemoteCallbackList<IUserSwitchObserver>();
    659 
    660     /**
    661      * Currently active user switch.
    662      */
    663     Object mCurUserSwitchCallback;
    664 
    665     /**
    666      * Packages that the user has asked to have run in screen size
    667      * compatibility mode instead of filling the screen.
    668      */
    669     final CompatModePackages mCompatModePackages;
    670 
    671     /**
    672      * Set of IntentSenderRecord objects that are currently active.
    673      */
    674     final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
    675             = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
    676 
    677     /**
    678      * Fingerprints (hashCode()) of stack traces that we've
    679      * already logged DropBox entries for.  Guarded by itself.  If
    680      * something (rogue user app) forces this over
    681      * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared.
    682      */
    683     private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>();
    684     private static final int MAX_DUP_SUPPRESSED_STACKS = 5000;
    685 
    686     /**
    687      * Strict Mode background batched logging state.
    688      *
    689      * The string buffer is guarded by itself, and its lock is also
    690      * used to determine if another batched write is already
    691      * in-flight.
    692      */
    693     private final StringBuilder mStrictModeBuffer = new StringBuilder();
    694 
    695     /**
    696      * Keeps track of all IIntentReceivers that have been registered for
    697      * broadcasts.  Hash keys are the receiver IBinder, hash value is
    698      * a ReceiverList.
    699      */
    700     final HashMap<IBinder, ReceiverList> mRegisteredReceivers =
    701             new HashMap<IBinder, ReceiverList>();
    702 
    703     /**
    704      * Resolver for broadcast intents to registered receivers.
    705      * Holds BroadcastFilter (subclass of IntentFilter).
    706      */
    707     final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
    708             = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
    709         @Override
    710         protected boolean allowFilterResult(
    711                 BroadcastFilter filter, List<BroadcastFilter> dest) {
    712             IBinder target = filter.receiverList.receiver.asBinder();
    713             for (int i=dest.size()-1; i>=0; i--) {
    714                 if (dest.get(i).receiverList.receiver.asBinder() == target) {
    715                     return false;
    716                 }
    717             }
    718             return true;
    719         }
    720 
    721         @Override
    722         protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) {
    723             if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL
    724                     || userId == filter.owningUserId) {
    725                 return super.newResult(filter, match, userId);
    726             }
    727             return null;
    728         }
    729 
    730         @Override
    731         protected BroadcastFilter[] newArray(int size) {
    732             return new BroadcastFilter[size];
    733         }
    734 
    735         @Override
    736         protected boolean isPackageForFilter(String packageName, BroadcastFilter filter) {
    737             return packageName.equals(filter.packageName);
    738         }
    739     };
    740 
    741     /**
    742      * State of all active sticky broadcasts per user.  Keys are the action of the
    743      * sticky Intent, values are an ArrayList of all broadcasted intents with
    744      * that action (which should usually be one).  The SparseArray is keyed
    745      * by the user ID the sticky is for, and can include UserHandle.USER_ALL
    746      * for stickies that are sent to all users.
    747      */
    748     final SparseArray<ArrayMap<String, ArrayList<Intent>>> mStickyBroadcasts =
    749             new SparseArray<ArrayMap<String, ArrayList<Intent>>>();
    750 
    751     final ActiveServices mServices;
    752 
    753     final static class Association {
    754         final int mSourceUid;
    755         final String mSourceProcess;
    756         final int mTargetUid;
    757         final ComponentName mTargetComponent;
    758         final String mTargetProcess;
    759 
    760         int mCount;
    761         long mTime;
    762 
    763         int mNesting;
    764         long mStartTime;
    765 
    766         Association(int sourceUid, String sourceProcess, int targetUid,
    767                 ComponentName targetComponent, String targetProcess) {
    768             mSourceUid = sourceUid;
    769             mSourceProcess = sourceProcess;
    770             mTargetUid = targetUid;
    771             mTargetComponent = targetComponent;
    772             mTargetProcess = targetProcess;
    773         }
    774     }
    775 
    776     /**
    777      * When service association tracking is enabled, this is all of the associations we
    778      * have seen.  Mapping is target uid -> target component -> source uid -> source process name
    779      * -> association data.
    780      */
    781     final SparseArray<ArrayMap<ComponentName, SparseArray<ArrayMap<String, Association>>>>
    782             mAssociations = new SparseArray<>();
    783     boolean mTrackingAssociations;
    784 
    785     /**
    786      * Backup/restore process management
    787      */
    788     String mBackupAppName = null;
    789     BackupRecord mBackupTarget = null;
    790 
    791     final ProviderMap mProviderMap;
    792 
    793     /**
    794      * List of content providers who have clients waiting for them.  The
    795      * application is currently being launched and the provider will be
    796      * removed from this list once it is published.
    797      */
    798     final ArrayList<ContentProviderRecord> mLaunchingProviders
    799             = new ArrayList<ContentProviderRecord>();
    800 
    801     /**
    802      * File storing persisted {@link #mGrantedUriPermissions}.
    803      */
    804     private final AtomicFile mGrantFile;
    805 
    806     /** XML constants used in {@link #mGrantFile} */
    807     private static final String TAG_URI_GRANTS = "uri-grants";
    808     private static final String TAG_URI_GRANT = "uri-grant";
    809     private static final String ATTR_USER_HANDLE = "userHandle";
    810     private static final String ATTR_SOURCE_USER_ID = "sourceUserId";
    811     private static final String ATTR_TARGET_USER_ID = "targetUserId";
    812     private static final String ATTR_SOURCE_PKG = "sourcePkg";
    813     private static final String ATTR_TARGET_PKG = "targetPkg";
    814     private static final String ATTR_URI = "uri";
    815     private static final String ATTR_MODE_FLAGS = "modeFlags";
    816     private static final String ATTR_CREATED_TIME = "createdTime";
    817     private static final String ATTR_PREFIX = "prefix";
    818 
    819     /**
    820      * Global set of specific {@link Uri} permissions that have been granted.
    821      * This optimized lookup structure maps from {@link UriPermission#targetUid}
    822      * to {@link UriPermission#uri} to {@link UriPermission}.
    823      */
    824     @GuardedBy("this")
    825     private final SparseArray<ArrayMap<GrantUri, UriPermission>>
    826             mGrantedUriPermissions = new SparseArray<ArrayMap<GrantUri, UriPermission>>();
    827 
    828     public static class GrantUri {
    829         public final int sourceUserId;
    830         public final Uri uri;
    831         public boolean prefix;
    832 
    833         public GrantUri(int sourceUserId, Uri uri, boolean prefix) {
    834             this.sourceUserId = sourceUserId;
    835             this.uri = uri;
    836             this.prefix = prefix;
    837         }
    838 
    839         @Override
    840         public int hashCode() {
    841             int hashCode = 1;
    842             hashCode = 31 * hashCode + sourceUserId;
    843             hashCode = 31 * hashCode + uri.hashCode();
    844             hashCode = 31 * hashCode + (prefix ? 1231 : 1237);
    845             return hashCode;
    846         }
    847 
    848         @Override
    849         public boolean equals(Object o) {
    850             if (o instanceof GrantUri) {
    851                 GrantUri other = (GrantUri) o;
    852                 return uri.equals(other.uri) && (sourceUserId == other.sourceUserId)
    853                         && prefix == other.prefix;
    854             }
    855             return false;
    856         }
    857 
    858         @Override
    859         public String toString() {
    860             String result = Integer.toString(sourceUserId) + " @ " + uri.toString();
    861             if (prefix) result += " [prefix]";
    862             return result;
    863         }
    864 
    865         public String toSafeString() {
    866             String result = Integer.toString(sourceUserId) + " @ " + uri.toSafeString();
    867             if (prefix) result += " [prefix]";
    868             return result;
    869         }
    870 
    871         public static GrantUri resolve(int defaultSourceUserHandle, Uri uri) {
    872             return new GrantUri(ContentProvider.getUserIdFromUri(uri, defaultSourceUserHandle),
    873                     ContentProvider.getUriWithoutUserId(uri), false);
    874         }
    875     }
    876 
    877     CoreSettingsObserver mCoreSettingsObserver;
    878 
    879     /**
    880      * Thread-local storage used to carry caller permissions over through
    881      * indirect content-provider access.
    882      */
    883     private class Identity {
    884         public final IBinder token;
    885         public final int pid;
    886         public final int uid;
    887 
    888         Identity(IBinder _token, int _pid, int _uid) {
    889             token = _token;
    890             pid = _pid;
    891             uid = _uid;
    892         }
    893     }
    894 
    895     private static final ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
    896 
    897     /**
    898      * All information we have collected about the runtime performance of
    899      * any user id that can impact battery performance.
    900      */
    901     final BatteryStatsService mBatteryStatsService;
    902 
    903     /**
    904      * Information about component usage
    905      */
    906     UsageStatsManagerInternal mUsageStatsService;
    907 
    908     /**
    909      * Information about and control over application operations
    910      */
    911     final AppOpsService mAppOpsService;
    912 
    913     /**
    914      * Save recent tasks information across reboots.
    915      */
    916     final TaskPersister mTaskPersister;
    917 
    918     /**
    919      * Current configuration information.  HistoryRecord objects are given
    920      * a reference to this object to indicate which configuration they are
    921      * currently running in, so this object must be kept immutable.
    922      */
    923     Configuration mConfiguration = new Configuration();
    924 
    925     /**
    926      * Current sequencing integer of the configuration, for skipping old
    927      * configurations.
    928      */
    929     int mConfigurationSeq = 0;
    930 
    931     /**
    932      * Hardware-reported OpenGLES version.
    933      */
    934     final int GL_ES_VERSION;
    935 
    936     /**
    937      * List of initialization arguments to pass to all processes when binding applications to them.
    938      * For example, references to the commonly used services.
    939      */
    940     HashMap<String, IBinder> mAppBindArgs;
    941 
    942     /**
    943      * Temporary to avoid allocations.  Protected by main lock.
    944      */
    945     final StringBuilder mStringBuilder = new StringBuilder(256);
    946 
    947     /**
    948      * Used to control how we initialize the service.
    949      */
    950     ComponentName mTopComponent;
    951     String mTopAction = Intent.ACTION_MAIN;
    952     String mTopData;
    953     boolean mProcessesReady = false;
    954     boolean mSystemReady = false;
    955     boolean mBooting = false;
    956     boolean mCallFinishBooting = false;
    957     boolean mBootAnimationComplete = false;
    958     boolean mWaitingUpdate = false;
    959     boolean mDidUpdate = false;
    960     boolean mOnBattery = false;
    961     boolean mLaunchWarningShown = false;
    962 
    963     Context mContext;
    964 
    965     int mFactoryTest;
    966 
    967     boolean mCheckedForSetup;
    968 
    969     /**
    970      * The time at which we will allow normal application switches again,
    971      * after a call to {@link #stopAppSwitches()}.
    972      */
    973     long mAppSwitchesAllowedTime;
    974 
    975     /**
    976      * This is set to true after the first switch after mAppSwitchesAllowedTime
    977      * is set; any switches after that will clear the time.
    978      */
    979     boolean mDidAppSwitch;
    980 
    981     /**
    982      * Last time (in realtime) at which we checked for power usage.
    983      */
    984     long mLastPowerCheckRealtime;
    985 
    986     /**
    987      * Last time (in uptime) at which we checked for power usage.
    988      */
    989     long mLastPowerCheckUptime;
    990 
    991     /**
    992      * Set while we are wanting to sleep, to prevent any
    993      * activities from being started/resumed.
    994      */
    995     private boolean mSleeping = false;
    996 
    997     /**
    998      * Set while we are running a voice interaction.  This overrides
    999      * sleeping while it is active.
   1000      */
   1001     private boolean mRunningVoice = false;
   1002 
   1003     /**
   1004      * State of external calls telling us if the device is awake or asleep.
   1005      */
   1006     private int mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE;
   1007 
   1008     static final int LOCK_SCREEN_HIDDEN = 0;
   1009     static final int LOCK_SCREEN_LEAVING = 1;
   1010     static final int LOCK_SCREEN_SHOWN = 2;
   1011     /**
   1012      * State of external call telling us if the lock screen is shown.
   1013      */
   1014     int mLockScreenShown = LOCK_SCREEN_HIDDEN;
   1015 
   1016     /**
   1017      * Set if we are shutting down the system, similar to sleeping.
   1018      */
   1019     boolean mShuttingDown = false;
   1020 
   1021     /**
   1022      * Current sequence id for oom_adj computation traversal.
   1023      */
   1024     int mAdjSeq = 0;
   1025 
   1026     /**
   1027      * Current sequence id for process LRU updating.
   1028      */
   1029     int mLruSeq = 0;
   1030 
   1031     /**
   1032      * Keep track of the non-cached/empty process we last found, to help
   1033      * determine how to distribute cached/empty processes next time.
   1034      */
   1035     int mNumNonCachedProcs = 0;
   1036 
   1037     /**
   1038      * Keep track of the number of cached hidden procs, to balance oom adj
   1039      * distribution between those and empty procs.
   1040      */
   1041     int mNumCachedHiddenProcs = 0;
   1042 
   1043     /**
   1044      * Keep track of the number of service processes we last found, to
   1045      * determine on the next iteration which should be B services.
   1046      */
   1047     int mNumServiceProcs = 0;
   1048     int mNewNumAServiceProcs = 0;
   1049     int mNewNumServiceProcs = 0;
   1050 
   1051     /**
   1052      * Allow the current computed overall memory level of the system to go down?
   1053      * This is set to false when we are killing processes for reasons other than
   1054      * memory management, so that the now smaller process list will not be taken as
   1055      * an indication that memory is tighter.
   1056      */
   1057     boolean mAllowLowerMemLevel = false;
   1058 
   1059     /**
   1060      * The last computed memory level, for holding when we are in a state that
   1061      * processes are going away for other reasons.
   1062      */
   1063     int mLastMemoryLevel = ProcessStats.ADJ_MEM_FACTOR_NORMAL;
   1064 
   1065     /**
   1066      * The last total number of process we have, to determine if changes actually look
   1067      * like a shrinking number of process due to lower RAM.
   1068      */
   1069     int mLastNumProcesses;
   1070 
   1071     /**
   1072      * The uptime of the last time we performed idle maintenance.
   1073      */
   1074     long mLastIdleTime = SystemClock.uptimeMillis();
   1075 
   1076     /**
   1077      * Total time spent with RAM that has been added in the past since the last idle time.
   1078      */
   1079     long mLowRamTimeSinceLastIdle = 0;
   1080 
   1081     /**
   1082      * If RAM is currently low, when that horrible situation started.
   1083      */
   1084     long mLowRamStartTime = 0;
   1085 
   1086     /**
   1087      * For reporting to battery stats the current top application.
   1088      */
   1089     private String mCurResumedPackage = null;
   1090     private int mCurResumedUid = -1;
   1091 
   1092     /**
   1093      * For reporting to battery stats the apps currently running foreground
   1094      * service.  The ProcessMap is package/uid tuples; each of these contain
   1095      * an array of the currently foreground processes.
   1096      */
   1097     final ProcessMap<ArrayList<ProcessRecord>> mForegroundPackages
   1098             = new ProcessMap<ArrayList<ProcessRecord>>();
   1099 
   1100     /**
   1101      * This is set if we had to do a delayed dexopt of an app before launching
   1102      * it, to increase the ANR timeouts in that case.
   1103      */
   1104     boolean mDidDexOpt;
   1105 
   1106     /**
   1107      * Set if the systemServer made a call to enterSafeMode.
   1108      */
   1109     boolean mSafeMode;
   1110 
   1111     /**
   1112      * If true, we are running under a test environment so will sample PSS from processes
   1113      * much more rapidly to try to collect better data when the tests are rapidly
   1114      * running through apps.
   1115      */
   1116     boolean mTestPssMode = false;
   1117 
   1118     String mDebugApp = null;
   1119     boolean mWaitForDebugger = false;
   1120     boolean mDebugTransient = false;
   1121     String mOrigDebugApp = null;
   1122     boolean mOrigWaitForDebugger = false;
   1123     boolean mAlwaysFinishActivities = false;
   1124     IActivityController mController = null;
   1125     String mProfileApp = null;
   1126     ProcessRecord mProfileProc = null;
   1127     String mProfileFile;
   1128     ParcelFileDescriptor mProfileFd;
   1129     int mSamplingInterval = 0;
   1130     boolean mAutoStopProfiler = false;
   1131     int mProfileType = 0;
   1132     String mOpenGlTraceApp = null;
   1133 
   1134     final long[] mTmpLong = new long[1];
   1135 
   1136     static class ProcessChangeItem {
   1137         static final int CHANGE_ACTIVITIES = 1<<0;
   1138         static final int CHANGE_PROCESS_STATE = 1<<1;
   1139         int changes;
   1140         int uid;
   1141         int pid;
   1142         int processState;
   1143         boolean foregroundActivities;
   1144     }
   1145 
   1146     final RemoteCallbackList<IProcessObserver> mProcessObservers
   1147             = new RemoteCallbackList<IProcessObserver>();
   1148     ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5];
   1149 
   1150     final ArrayList<ProcessChangeItem> mPendingProcessChanges
   1151             = new ArrayList<ProcessChangeItem>();
   1152     final ArrayList<ProcessChangeItem> mAvailProcessChanges
   1153             = new ArrayList<ProcessChangeItem>();
   1154 
   1155     /**
   1156      * Runtime CPU use collection thread.  This object's lock is used to
   1157      * perform synchronization with the thread (notifying it to run).
   1158      */
   1159     final Thread mProcessCpuThread;
   1160 
   1161     /**
   1162      * Used to collect per-process CPU use for ANRs, battery stats, etc.
   1163      * Must acquire this object's lock when accessing it.
   1164      * NOTE: this lock will be held while doing long operations (trawling
   1165      * through all processes in /proc), so it should never be acquired by
   1166      * any critical paths such as when holding the main activity manager lock.
   1167      */
   1168     final ProcessCpuTracker mProcessCpuTracker = new ProcessCpuTracker(
   1169             MONITOR_THREAD_CPU_USAGE);
   1170     final AtomicLong mLastCpuTime = new AtomicLong(0);
   1171     final AtomicBoolean mProcessCpuMutexFree = new AtomicBoolean(true);
   1172 
   1173     long mLastWriteTime = 0;
   1174 
   1175     /**
   1176      * Used to retain an update lock when the foreground activity is in
   1177      * immersive mode.
   1178      */
   1179     final UpdateLock mUpdateLock = new UpdateLock("immersive");
   1180 
   1181     /**
   1182      * Set to true after the system has finished booting.
   1183      */
   1184     boolean mBooted = false;
   1185 
   1186     int mProcessLimit = ProcessList.MAX_CACHED_APPS;
   1187     int mProcessLimitOverride = -1;
   1188 
   1189     WindowManagerService mWindowManager;
   1190 
   1191     final ActivityThread mSystemThread;
   1192 
   1193     // Holds the current foreground user's id
   1194     int mCurrentUserId = 0;
   1195     // Holds the target user's id during a user switch
   1196     int mTargetUserId = UserHandle.USER_NULL;
   1197     // If there are multiple profiles for the current user, their ids are here
   1198     // Currently only the primary user can have managed profiles
   1199     int[] mCurrentProfileIds = new int[] {UserHandle.USER_OWNER}; // Accessed by ActivityStack
   1200 
   1201     /**
   1202      * Mapping from each known user ID to the profile group ID it is associated with.
   1203      */
   1204     SparseIntArray mUserProfileGroupIdsSelfLocked = new SparseIntArray();
   1205 
   1206     private UserManagerService mUserManager;
   1207 
   1208     private final class AppDeathRecipient implements IBinder.DeathRecipient {
   1209         final ProcessRecord mApp;
   1210         final int mPid;
   1211         final IApplicationThread mAppThread;
   1212 
   1213         AppDeathRecipient(ProcessRecord app, int pid,
   1214                 IApplicationThread thread) {
   1215             if (localLOGV) Slog.v(
   1216                 TAG, "New death recipient " + this
   1217                 + " for thread " + thread.asBinder());
   1218             mApp = app;
   1219             mPid = pid;
   1220             mAppThread = thread;
   1221         }
   1222 
   1223         @Override
   1224         public void binderDied() {
   1225             if (localLOGV) Slog.v(
   1226                 TAG, "Death received in " + this
   1227                 + " for thread " + mAppThread.asBinder());
   1228             synchronized(ActivityManagerService.this) {
   1229                 appDiedLocked(mApp, mPid, mAppThread);
   1230             }
   1231         }
   1232     }
   1233 
   1234     static final int SHOW_ERROR_MSG = 1;
   1235     static final int SHOW_NOT_RESPONDING_MSG = 2;
   1236     static final int SHOW_FACTORY_ERROR_MSG = 3;
   1237     static final int UPDATE_CONFIGURATION_MSG = 4;
   1238     static final int GC_BACKGROUND_PROCESSES_MSG = 5;
   1239     static final int WAIT_FOR_DEBUGGER_MSG = 6;
   1240     static final int SERVICE_TIMEOUT_MSG = 12;
   1241     static final int UPDATE_TIME_ZONE = 13;
   1242     static final int SHOW_UID_ERROR_MSG = 14;
   1243     static final int SHOW_FINGERPRINT_ERROR_MSG = 15;
   1244     static final int PROC_START_TIMEOUT_MSG = 20;
   1245     static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
   1246     static final int KILL_APPLICATION_MSG = 22;
   1247     static final int FINALIZE_PENDING_INTENT_MSG = 23;
   1248     static final int POST_HEAVY_NOTIFICATION_MSG = 24;
   1249     static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25;
   1250     static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26;
   1251     static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27;
   1252     static final int CLEAR_DNS_CACHE_MSG = 28;
   1253     static final int UPDATE_HTTP_PROXY_MSG = 29;
   1254     static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30;
   1255     static final int DISPATCH_PROCESSES_CHANGED = 31;
   1256     static final int DISPATCH_PROCESS_DIED = 32;
   1257     static final int REPORT_MEM_USAGE_MSG = 33;
   1258     static final int REPORT_USER_SWITCH_MSG = 34;
   1259     static final int CONTINUE_USER_SWITCH_MSG = 35;
   1260     static final int USER_SWITCH_TIMEOUT_MSG = 36;
   1261     static final int IMMERSIVE_MODE_LOCK_MSG = 37;
   1262     static final int PERSIST_URI_GRANTS_MSG = 38;
   1263     static final int REQUEST_ALL_PSS_MSG = 39;
   1264     static final int START_PROFILES_MSG = 40;
   1265     static final int UPDATE_TIME = 41;
   1266     static final int SYSTEM_USER_START_MSG = 42;
   1267     static final int SYSTEM_USER_CURRENT_MSG = 43;
   1268     static final int ENTER_ANIMATION_COMPLETE_MSG = 44;
   1269     static final int FINISH_BOOTING_MSG = 45;
   1270     static final int START_USER_SWITCH_MSG = 46;
   1271     static final int SEND_LOCALE_TO_MOUNT_DAEMON_MSG = 47;
   1272     static final int DISMISS_DIALOG_MSG = 48;
   1273     static final int NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG = 49;
   1274 
   1275     static final int FIRST_ACTIVITY_STACK_MSG = 100;
   1276     static final int FIRST_BROADCAST_QUEUE_MSG = 200;
   1277     static final int FIRST_COMPAT_MODE_MSG = 300;
   1278     static final int FIRST_SUPERVISOR_STACK_MSG = 100;
   1279 
   1280     CompatModeDialog mCompatModeDialog;
   1281     long mLastMemUsageReportTime = 0;
   1282 
   1283     /**
   1284      * Flag whether the current user is a "monkey", i.e. whether
   1285      * the UI is driven by a UI automation tool.
   1286      */
   1287     private boolean mUserIsMonkey;
   1288 
   1289     /** Flag whether the device has a Recents UI */
   1290     boolean mHasRecents;
   1291 
   1292     /** The dimensions of the thumbnails in the Recents UI. */
   1293     int mThumbnailWidth;
   1294     int mThumbnailHeight;
   1295 
   1296     final ServiceThread mHandlerThread;
   1297     final MainHandler mHandler;
   1298 
   1299     final class MainHandler extends Handler {
   1300         public MainHandler(Looper looper) {
   1301             super(looper, null, true);
   1302         }
   1303 
   1304         @Override
   1305         public void handleMessage(Message msg) {
   1306             switch (msg.what) {
   1307             case SHOW_ERROR_MSG: {
   1308                 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
   1309                 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
   1310                         Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
   1311                 synchronized (ActivityManagerService.this) {
   1312                     ProcessRecord proc = (ProcessRecord)data.get("app");
   1313                     AppErrorResult res = (AppErrorResult) data.get("result");
   1314                     if (proc != null && proc.crashDialog != null) {
   1315                         Slog.e(TAG, "App already has crash dialog: " + proc);
   1316                         if (res != null) {
   1317                             res.set(0);
   1318                         }
   1319                         return;
   1320                     }
   1321                     boolean isBackground = (UserHandle.getAppId(proc.uid)
   1322                             >= Process.FIRST_APPLICATION_UID
   1323                             && proc.pid != MY_PID);
   1324                     for (int userId : mCurrentProfileIds) {
   1325                         isBackground &= (proc.userId != userId);
   1326                     }
   1327                     if (isBackground && !showBackground) {
   1328                         Slog.w(TAG, "Skipping crash dialog of " + proc + ": background");
   1329                         if (res != null) {
   1330                             res.set(0);
   1331                         }
   1332                         return;
   1333                     }
   1334                     if (mShowDialogs && !mSleeping && !mShuttingDown) {
   1335                         Dialog d = new AppErrorDialog(mContext,
   1336                                 ActivityManagerService.this, res, proc);
   1337                         d.show();
   1338                         proc.crashDialog = d;
   1339                     } else {
   1340                         // The device is asleep, so just pretend that the user
   1341                         // saw a crash dialog and hit "force quit".
   1342                         if (res != null) {
   1343                             res.set(0);
   1344                         }
   1345                     }
   1346                 }
   1347 
   1348                 ensureBootCompleted();
   1349             } break;
   1350             case SHOW_NOT_RESPONDING_MSG: {
   1351                 synchronized (ActivityManagerService.this) {
   1352                     HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
   1353                     ProcessRecord proc = (ProcessRecord)data.get("app");
   1354                     if (proc != null && proc.anrDialog != null) {
   1355                         Slog.e(TAG, "App already has anr dialog: " + proc);
   1356                         return;
   1357                     }
   1358 
   1359                     Intent intent = new Intent("android.intent.action.ANR");
   1360                     if (!mProcessesReady) {
   1361                         intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
   1362                                 | Intent.FLAG_RECEIVER_FOREGROUND);
   1363                     }
   1364                     broadcastIntentLocked(null, null, intent,
   1365                             null, null, 0, null, null, null, AppOpsManager.OP_NONE,
   1366                             false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
   1367 
   1368                     if (mShowDialogs) {
   1369                         Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
   1370                                 mContext, proc, (ActivityRecord)data.get("activity"),
   1371                                 msg.arg1 != 0);
   1372                         d.show();
   1373                         proc.anrDialog = d;
   1374                     } else {
   1375                         // Just kill the app if there is no dialog to be shown.
   1376                         killAppAtUsersRequest(proc, null);
   1377                     }
   1378                 }
   1379 
   1380                 ensureBootCompleted();
   1381             } break;
   1382             case SHOW_STRICT_MODE_VIOLATION_MSG: {
   1383                 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
   1384                 synchronized (ActivityManagerService.this) {
   1385                     ProcessRecord proc = (ProcessRecord) data.get("app");
   1386                     if (proc == null) {
   1387                         Slog.e(TAG, "App not found when showing strict mode dialog.");
   1388                         break;
   1389                     }
   1390                     if (proc.crashDialog != null) {
   1391                         Slog.e(TAG, "App already has strict mode dialog: " + proc);
   1392                         return;
   1393                     }
   1394                     AppErrorResult res = (AppErrorResult) data.get("result");
   1395                     if (mShowDialogs && !mSleeping && !mShuttingDown) {
   1396                         Dialog d = new StrictModeViolationDialog(mContext,
   1397                                 ActivityManagerService.this, res, proc);
   1398                         d.show();
   1399                         proc.crashDialog = d;
   1400                     } else {
   1401                         // The device is asleep, so just pretend that the user
   1402                         // saw a crash dialog and hit "force quit".
   1403                         res.set(0);
   1404                     }
   1405                 }
   1406                 ensureBootCompleted();
   1407             } break;
   1408             case SHOW_FACTORY_ERROR_MSG: {
   1409                 Dialog d = new FactoryErrorDialog(
   1410                     mContext, msg.getData().getCharSequence("msg"));
   1411                 d.show();
   1412                 ensureBootCompleted();
   1413             } break;
   1414             case UPDATE_CONFIGURATION_MSG: {
   1415                 final ContentResolver resolver = mContext.getContentResolver();
   1416                 Settings.System.putConfiguration(resolver, (Configuration)msg.obj);
   1417             } break;
   1418             case GC_BACKGROUND_PROCESSES_MSG: {
   1419                 synchronized (ActivityManagerService.this) {
   1420                     performAppGcsIfAppropriateLocked();
   1421                 }
   1422             } break;
   1423             case WAIT_FOR_DEBUGGER_MSG: {
   1424                 synchronized (ActivityManagerService.this) {
   1425                     ProcessRecord app = (ProcessRecord)msg.obj;
   1426                     if (msg.arg1 != 0) {
   1427                         if (!app.waitedForDebugger) {
   1428                             Dialog d = new AppWaitingForDebuggerDialog(
   1429                                     ActivityManagerService.this,
   1430                                     mContext, app);
   1431                             app.waitDialog = d;
   1432                             app.waitedForDebugger = true;
   1433                             d.show();
   1434                         }
   1435                     } else {
   1436                         if (app.waitDialog != null) {
   1437                             app.waitDialog.dismiss();
   1438                             app.waitDialog = null;
   1439                         }
   1440                     }
   1441                 }
   1442             } break;
   1443             case SERVICE_TIMEOUT_MSG: {
   1444                 if (mDidDexOpt) {
   1445                     mDidDexOpt = false;
   1446                     Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
   1447                     nmsg.obj = msg.obj;
   1448                     mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT);
   1449                     return;
   1450                 }
   1451                 mServices.serviceTimeout((ProcessRecord)msg.obj);
   1452             } break;
   1453             case UPDATE_TIME_ZONE: {
   1454                 synchronized (ActivityManagerService.this) {
   1455                     for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
   1456                         ProcessRecord r = mLruProcesses.get(i);
   1457                         if (r.thread != null) {
   1458                             try {
   1459                                 r.thread.updateTimeZone();
   1460                             } catch (RemoteException ex) {
   1461                                 Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
   1462                             }
   1463                         }
   1464                     }
   1465                 }
   1466             } break;
   1467             case CLEAR_DNS_CACHE_MSG: {
   1468                 synchronized (ActivityManagerService.this) {
   1469                     for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
   1470                         ProcessRecord r = mLruProcesses.get(i);
   1471                         if (r.thread != null) {
   1472                             try {
   1473                                 r.thread.clearDnsCache();
   1474                             } catch (RemoteException ex) {
   1475                                 Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName);
   1476                             }
   1477                         }
   1478                     }
   1479                 }
   1480             } break;
   1481             case UPDATE_HTTP_PROXY_MSG: {
   1482                 ProxyInfo proxy = (ProxyInfo)msg.obj;
   1483                 String host = "";
   1484                 String port = "";
   1485                 String exclList = "";
   1486                 Uri pacFileUrl = Uri.EMPTY;
   1487                 if (proxy != null) {
   1488                     host = proxy.getHost();
   1489                     port = Integer.toString(proxy.getPort());
   1490                     exclList = proxy.getExclusionListAsString();
   1491                     pacFileUrl = proxy.getPacFileUrl();
   1492                 }
   1493                 synchronized (ActivityManagerService.this) {
   1494                     for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
   1495                         ProcessRecord r = mLruProcesses.get(i);
   1496                         if (r.thread != null) {
   1497                             try {
   1498                                 r.thread.setHttpProxy(host, port, exclList, pacFileUrl);
   1499                             } catch (RemoteException ex) {
   1500                                 Slog.w(TAG, "Failed to update http proxy for: " +
   1501                                         r.info.processName);
   1502                             }
   1503                         }
   1504                     }
   1505                 }
   1506             } break;
   1507             case SHOW_UID_ERROR_MSG: {
   1508                 if (mShowDialogs) {
   1509                     AlertDialog d = new BaseErrorDialog(mContext);
   1510                     d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
   1511                     d.setCancelable(false);
   1512                     d.setTitle(mContext.getText(R.string.android_system_label));
   1513                     d.setMessage(mContext.getText(R.string.system_error_wipe_data));
   1514                     d.setButton(DialogInterface.BUTTON_POSITIVE, mContext.getText(R.string.ok),
   1515                             mHandler.obtainMessage(DISMISS_DIALOG_MSG, d));
   1516                     d.show();
   1517                 }
   1518             } break;
   1519             case SHOW_FINGERPRINT_ERROR_MSG: {
   1520                 if (mShowDialogs) {
   1521                     AlertDialog d = new BaseErrorDialog(mContext);
   1522                     d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
   1523                     d.setCancelable(false);
   1524                     d.setTitle(mContext.getText(R.string.android_system_label));
   1525                     d.setMessage(mContext.getText(R.string.system_error_manufacturer));
   1526                     d.setButton(DialogInterface.BUTTON_POSITIVE, mContext.getText(R.string.ok),
   1527                             mHandler.obtainMessage(DISMISS_DIALOG_MSG, d));
   1528                     d.show();
   1529                 }
   1530             } break;
   1531             case PROC_START_TIMEOUT_MSG: {
   1532                 if (mDidDexOpt) {
   1533                     mDidDexOpt = false;
   1534                     Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
   1535                     nmsg.obj = msg.obj;
   1536                     mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
   1537                     return;
   1538                 }
   1539                 ProcessRecord app = (ProcessRecord)msg.obj;
   1540                 synchronized (ActivityManagerService.this) {
   1541                     processStartTimedOutLocked(app);
   1542                 }
   1543             } break;
   1544             case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
   1545                 synchronized (ActivityManagerService.this) {
   1546                     mStackSupervisor.doPendingActivityLaunchesLocked(true);
   1547                 }
   1548             } break;
   1549             case KILL_APPLICATION_MSG: {
   1550                 synchronized (ActivityManagerService.this) {
   1551                     int appid = msg.arg1;
   1552                     boolean restart = (msg.arg2 == 1);
   1553                     Bundle bundle = (Bundle)msg.obj;
   1554                     String pkg = bundle.getString("pkg");
   1555                     String reason = bundle.getString("reason");
   1556                     forceStopPackageLocked(pkg, appid, restart, false, true, false,
   1557                             false, UserHandle.USER_ALL, reason);
   1558                 }
   1559             } break;
   1560             case FINALIZE_PENDING_INTENT_MSG: {
   1561                 ((PendingIntentRecord)msg.obj).completeFinalize();
   1562             } break;
   1563             case POST_HEAVY_NOTIFICATION_MSG: {
   1564                 INotificationManager inm = NotificationManager.getService();
   1565                 if (inm == null) {
   1566                     return;
   1567                 }
   1568 
   1569                 ActivityRecord root = (ActivityRecord)msg.obj;
   1570                 ProcessRecord process = root.app;
   1571                 if (process == null) {
   1572                     return;
   1573                 }
   1574 
   1575                 try {
   1576                     Context context = mContext.createPackageContext(process.info.packageName, 0);
   1577                     String text = mContext.getString(R.string.heavy_weight_notification,
   1578                             context.getApplicationInfo().loadLabel(context.getPackageManager()));
   1579                     Notification notification = new Notification();
   1580                     notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
   1581                     notification.when = 0;
   1582                     notification.flags = Notification.FLAG_ONGOING_EVENT;
   1583                     notification.tickerText = text;
   1584                     notification.defaults = 0; // please be quiet
   1585                     notification.sound = null;
   1586                     notification.vibrate = null;
   1587                     notification.color = mContext.getResources().getColor(
   1588                             com.android.internal.R.color.system_notification_accent_color);
   1589                     notification.setLatestEventInfo(context, text,
   1590                             mContext.getText(R.string.heavy_weight_notification_detail),
   1591                             PendingIntent.getActivityAsUser(mContext, 0, root.intent,
   1592                                     PendingIntent.FLAG_CANCEL_CURRENT, null,
   1593                                     new UserHandle(root.userId)));
   1594 
   1595                     try {
   1596                         int[] outId = new int[1];
   1597                         inm.enqueueNotificationWithTag("android", "android", null,
   1598                                 R.string.heavy_weight_notification,
   1599                                 notification, outId, root.userId);
   1600                     } catch (RuntimeException e) {
   1601                         Slog.w(ActivityManagerService.TAG,
   1602                                 "Error showing notification for heavy-weight app", e);
   1603                     } catch (RemoteException e) {
   1604                     }
   1605                 } catch (NameNotFoundException e) {
   1606                     Slog.w(TAG, "Unable to create context for heavy notification", e);
   1607                 }
   1608             } break;
   1609             case CANCEL_HEAVY_NOTIFICATION_MSG: {
   1610                 INotificationManager inm = NotificationManager.getService();
   1611                 if (inm == null) {
   1612                     return;
   1613                 }
   1614                 try {
   1615                     inm.cancelNotificationWithTag("android", null,
   1616                             R.string.heavy_weight_notification,  msg.arg1);
   1617                 } catch (RuntimeException e) {
   1618                     Slog.w(ActivityManagerService.TAG,
   1619                             "Error canceling notification for service", e);
   1620                 } catch (RemoteException e) {
   1621                 }
   1622             } break;
   1623             case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: {
   1624                 synchronized (ActivityManagerService.this) {
   1625                     checkExcessivePowerUsageLocked(true);
   1626                     removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
   1627                     Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
   1628                     sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
   1629                 }
   1630             } break;
   1631             case SHOW_COMPAT_MODE_DIALOG_MSG: {
   1632                 synchronized (ActivityManagerService.this) {
   1633                     ActivityRecord ar = (ActivityRecord)msg.obj;
   1634                     if (mCompatModeDialog != null) {
   1635                         if (mCompatModeDialog.mAppInfo.packageName.equals(
   1636                                 ar.info.applicationInfo.packageName)) {
   1637                             return;
   1638                         }
   1639                         mCompatModeDialog.dismiss();
   1640                         mCompatModeDialog = null;
   1641                     }
   1642                     if (ar != null && false) {
   1643                         if (mCompatModePackages.getPackageAskCompatModeLocked(
   1644                                 ar.packageName)) {
   1645                             int mode = mCompatModePackages.computeCompatModeLocked(
   1646                                     ar.info.applicationInfo);
   1647                             if (mode == ActivityManager.COMPAT_MODE_DISABLED
   1648                                     || mode == ActivityManager.COMPAT_MODE_ENABLED) {
   1649                                 mCompatModeDialog = new CompatModeDialog(
   1650                                         ActivityManagerService.this, mContext,
   1651                                         ar.info.applicationInfo);
   1652                                 mCompatModeDialog.show();
   1653                             }
   1654                         }
   1655                     }
   1656                 }
   1657                 break;
   1658             }
   1659             case DISPATCH_PROCESSES_CHANGED: {
   1660                 dispatchProcessesChanged();
   1661                 break;
   1662             }
   1663             case DISPATCH_PROCESS_DIED: {
   1664                 final int pid = msg.arg1;
   1665                 final int uid = msg.arg2;
   1666                 dispatchProcessDied(pid, uid);
   1667                 break;
   1668             }
   1669             case REPORT_MEM_USAGE_MSG: {
   1670                 final ArrayList<ProcessMemInfo> memInfos = (ArrayList<ProcessMemInfo>)msg.obj;
   1671                 Thread thread = new Thread() {
   1672                     @Override public void run() {
   1673                         reportMemUsage(memInfos);
   1674                     }
   1675                 };
   1676                 thread.start();
   1677                 break;
   1678             }
   1679             case START_USER_SWITCH_MSG: {
   1680                 showUserSwitchDialog(msg.arg1, (String) msg.obj);
   1681                 break;
   1682             }
   1683             case REPORT_USER_SWITCH_MSG: {
   1684                 dispatchUserSwitch((UserStartedState) msg.obj, msg.arg1, msg.arg2);
   1685                 break;
   1686             }
   1687             case CONTINUE_USER_SWITCH_MSG: {
   1688                 continueUserSwitch((UserStartedState) msg.obj, msg.arg1, msg.arg2);
   1689                 break;
   1690             }
   1691             case USER_SWITCH_TIMEOUT_MSG: {
   1692                 timeoutUserSwitch((UserStartedState) msg.obj, msg.arg1, msg.arg2);
   1693                 break;
   1694             }
   1695             case IMMERSIVE_MODE_LOCK_MSG: {
   1696                 final boolean nextState = (msg.arg1 != 0);
   1697                 if (mUpdateLock.isHeld() != nextState) {
   1698                     if (DEBUG_IMMERSIVE) {
   1699                         final ActivityRecord r = (ActivityRecord) msg.obj;
   1700                         Slog.d(TAG, "Applying new update lock state '" + nextState + "' for " + r);
   1701                     }
   1702                     if (nextState) {
   1703                         mUpdateLock.acquire();
   1704                     } else {
   1705                         mUpdateLock.release();
   1706                     }
   1707                 }
   1708                 break;
   1709             }
   1710             case PERSIST_URI_GRANTS_MSG: {
   1711                 writeGrantedUriPermissions();
   1712                 break;
   1713             }
   1714             case REQUEST_ALL_PSS_MSG: {
   1715                 synchronized (ActivityManagerService.this) {
   1716                     requestPssAllProcsLocked(SystemClock.uptimeMillis(), true, false);
   1717                 }
   1718                 break;
   1719             }
   1720             case START_PROFILES_MSG: {
   1721                 synchronized (ActivityManagerService.this) {
   1722                     startProfilesLocked();
   1723                 }
   1724                 break;
   1725             }
   1726             case UPDATE_TIME: {
   1727                 synchronized (ActivityManagerService.this) {
   1728                     for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
   1729                         ProcessRecord r = mLruProcesses.get(i);
   1730                         if (r.thread != null) {
   1731                             try {
   1732                                 r.thread.updateTimePrefs(msg.arg1 == 0 ? false : true);
   1733                             } catch (RemoteException ex) {
   1734                                 Slog.w(TAG, "Failed to update preferences for: " + r.info.processName);
   1735                             }
   1736                         }
   1737                     }
   1738                 }
   1739                 break;
   1740             }
   1741             case SYSTEM_USER_START_MSG: {
   1742                 mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_USER_RUNNING_START,
   1743                         Integer.toString(msg.arg1), msg.arg1);
   1744                 mSystemServiceManager.startUser(msg.arg1);
   1745                 break;
   1746             }
   1747             case SYSTEM_USER_CURRENT_MSG: {
   1748                 mBatteryStatsService.noteEvent(
   1749                         BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_FINISH,
   1750                         Integer.toString(msg.arg2), msg.arg2);
   1751                 mBatteryStatsService.noteEvent(
   1752                         BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_START,
   1753                         Integer.toString(msg.arg1), msg.arg1);
   1754                 mSystemServiceManager.switchUser(msg.arg1);
   1755                 break;
   1756             }
   1757             case ENTER_ANIMATION_COMPLETE_MSG: {
   1758                 synchronized (ActivityManagerService.this) {
   1759                     ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
   1760                     if (r != null && r.app != null && r.app.thread != null) {
   1761                         try {
   1762                             r.app.thread.scheduleEnterAnimationComplete(r.appToken);
   1763                         } catch (RemoteException e) {
   1764                         }
   1765                     }
   1766                 }
   1767                 break;
   1768             }
   1769             case FINISH_BOOTING_MSG: {
   1770                 if (msg.arg1 != 0) {
   1771                     finishBooting();
   1772                 }
   1773                 if (msg.arg2 != 0) {
   1774                     enableScreenAfterBoot();
   1775                 }
   1776                 break;
   1777             }
   1778             case SEND_LOCALE_TO_MOUNT_DAEMON_MSG: {
   1779                 try {
   1780                     Locale l = (Locale) msg.obj;
   1781                     IBinder service = ServiceManager.getService("mount");
   1782                     IMountService mountService = IMountService.Stub.asInterface(service);
   1783                     Log.d(TAG, "Storing locale " + l.toLanguageTag() + " for decryption UI");
   1784                     mountService.setField(StorageManager.SYSTEM_LOCALE_KEY, l.toLanguageTag());
   1785                 } catch (RemoteException e) {
   1786                     Log.e(TAG, "Error storing locale for decryption UI", e);
   1787                 }
   1788                 break;
   1789             }
   1790             case DISMISS_DIALOG_MSG: {
   1791                 final Dialog d = (Dialog) msg.obj;
   1792                 d.dismiss();
   1793                 break;
   1794             }
   1795             case NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG: {
   1796                 synchronized (ActivityManagerService.this) {
   1797                     int i = mTaskStackListeners.beginBroadcast();
   1798                     while (i > 0) {
   1799                         i--;
   1800                         try {
   1801                             // Make a one-way callback to the listener
   1802                             mTaskStackListeners.getBroadcastItem(i).onTaskStackChanged();
   1803                         } catch (RemoteException e){
   1804                             // Handled by the RemoteCallbackList
   1805                         }
   1806                     }
   1807                     mTaskStackListeners.finishBroadcast();
   1808                 }
   1809                 break;
   1810             }
   1811             }
   1812         }
   1813     };
   1814 
   1815     static final int COLLECT_PSS_BG_MSG = 1;
   1816 
   1817     final Handler mBgHandler = new Handler(BackgroundThread.getHandler().getLooper()) {
   1818         @Override
   1819         public void handleMessage(Message msg) {
   1820             switch (msg.what) {
   1821             case COLLECT_PSS_BG_MSG: {
   1822                 long start = SystemClock.uptimeMillis();
   1823                 MemInfoReader memInfo = null;
   1824                 synchronized (ActivityManagerService.this) {
   1825                     if (mFullPssPending) {
   1826                         mFullPssPending = false;
   1827                         memInfo = new MemInfoReader();
   1828                     }
   1829                 }
   1830                 if (memInfo != null) {
   1831                     updateCpuStatsNow();
   1832                     long nativeTotalPss = 0;
   1833                     synchronized (mProcessCpuTracker) {
   1834                         final int N = mProcessCpuTracker.countStats();
   1835                         for (int j=0; j<N; j++) {
   1836                             ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(j);
   1837                             if (st.vsize <= 0 || st.uid >= Process.FIRST_APPLICATION_UID) {
   1838                                 // This is definitely an application process; skip it.
   1839                                 continue;
   1840                             }
   1841                             synchronized (mPidsSelfLocked) {
   1842                                 if (mPidsSelfLocked.indexOfKey(st.pid) >= 0) {
   1843                                     // This is one of our own processes; skip it.
   1844                                     continue;
   1845                                 }
   1846                             }
   1847                             nativeTotalPss += Debug.getPss(st.pid, null, null);
   1848                         }
   1849                     }
   1850                     memInfo.readMemInfo();
   1851                     synchronized (ActivityManagerService.this) {
   1852                         if (DEBUG_PSS) Slog.d(TAG, "Collected native and kernel memory in "
   1853                                 + (SystemClock.uptimeMillis()-start) + "ms");
   1854                         mProcessStats.addSysMemUsageLocked(memInfo.getCachedSizeKb(),
   1855                                 memInfo.getFreeSizeKb(), memInfo.getZramTotalSizeKb(),
   1856                                 memInfo.getKernelUsedSizeKb(), nativeTotalPss);
   1857                     }
   1858                 }
   1859 
   1860                 int num = 0;
   1861                 long[] tmp = new long[1];
   1862                 do {
   1863                     ProcessRecord proc;
   1864                     int procState;
   1865                     int pid;
   1866                     long lastPssTime;
   1867                     synchronized (ActivityManagerService.this) {
   1868                         if (mPendingPssProcesses.size() <= 0) {
   1869                             if (mTestPssMode || DEBUG_PSS) Slog.d(TAG, "Collected PSS of " + num
   1870                                     + " processes in " + (SystemClock.uptimeMillis()-start) + "ms");
   1871                             mPendingPssProcesses.clear();
   1872                             return;
   1873                         }
   1874                         proc = mPendingPssProcesses.remove(0);
   1875                         procState = proc.pssProcState;
   1876                         lastPssTime = proc.lastPssTime;
   1877                         if (proc.thread != null && procState == proc.setProcState
   1878                                 && (lastPssTime+ProcessList.PSS_SAFE_TIME_FROM_STATE_CHANGE)
   1879                                         < SystemClock.uptimeMillis()) {
   1880                             pid = proc.pid;
   1881                         } else {
   1882                             proc = null;
   1883                             pid = 0;
   1884                         }
   1885                     }
   1886                     if (proc != null) {
   1887                         long pss = Debug.getPss(pid, tmp, null);
   1888                         synchronized (ActivityManagerService.this) {
   1889                             if (pss != 0 && proc.thread != null && proc.setProcState == procState
   1890                                     && proc.pid == pid && proc.lastPssTime == lastPssTime) {
   1891                                 num++;
   1892                                 recordPssSample(proc, procState, pss, tmp[0],
   1893                                         SystemClock.uptimeMillis());
   1894                             }
   1895                         }
   1896                     }
   1897                 } while (true);
   1898             }
   1899             }
   1900         }
   1901     };
   1902 
   1903     public void setSystemProcess() {
   1904         try {
   1905             ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
   1906             ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
   1907             ServiceManager.addService("meminfo", new MemBinder(this));
   1908             ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
   1909             ServiceManager.addService("dbinfo", new DbBinder(this));
   1910             if (MONITOR_CPU_USAGE) {
   1911                 ServiceManager.addService("cpuinfo", new CpuBinder(this));
   1912             }
   1913             ServiceManager.addService("permission", new PermissionController(this));
   1914 
   1915             ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
   1916                     "android", STOCK_PM_FLAGS);
   1917             mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
   1918 
   1919             synchronized (this) {
   1920                 ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0);
   1921                 app.persistent = true;
   1922                 app.pid = MY_PID;
   1923                 app.maxAdj = ProcessList.SYSTEM_ADJ;
   1924                 app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
   1925                 mProcessNames.put(app.processName, app.uid, app);
   1926                 synchronized (mPidsSelfLocked) {
   1927                     mPidsSelfLocked.put(app.pid, app);
   1928                 }
   1929                 updateLruProcessLocked(app, false, null);
   1930                 updateOomAdjLocked();
   1931             }
   1932         } catch (PackageManager.NameNotFoundException e) {
   1933             throw new RuntimeException(
   1934                     "Unable to find android system package", e);
   1935         }
   1936     }
   1937 
   1938     public void setWindowManager(WindowManagerService wm) {
   1939         mWindowManager = wm;
   1940         mStackSupervisor.setWindowManager(wm);
   1941     }
   1942 
   1943     public void setUsageStatsManager(UsageStatsManagerInternal usageStatsManager) {
   1944         mUsageStatsService = usageStatsManager;
   1945     }
   1946 
   1947     public void startObservingNativeCrashes() {
   1948         final NativeCrashListener ncl = new NativeCrashListener(this);
   1949         ncl.start();
   1950     }
   1951 
   1952     public IAppOpsService getAppOpsService() {
   1953         return mAppOpsService;
   1954     }
   1955 
   1956     static class MemBinder extends Binder {
   1957         ActivityManagerService mActivityManagerService;
   1958         MemBinder(ActivityManagerService activityManagerService) {
   1959             mActivityManagerService = activityManagerService;
   1960         }
   1961 
   1962         @Override
   1963         protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
   1964             if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
   1965                     != PackageManager.PERMISSION_GRANTED) {
   1966                 pw.println("Permission Denial: can't dump meminfo from from pid="
   1967                         + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
   1968                         + " without permission " + android.Manifest.permission.DUMP);
   1969                 return;
   1970             }
   1971 
   1972             mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, "  ", args, false, null);
   1973         }
   1974     }
   1975 
   1976     static class GraphicsBinder extends Binder {
   1977         ActivityManagerService mActivityManagerService;
   1978         GraphicsBinder(ActivityManagerService activityManagerService) {
   1979             mActivityManagerService = activityManagerService;
   1980         }
   1981 
   1982         @Override
   1983         protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
   1984             if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
   1985                     != PackageManager.PERMISSION_GRANTED) {
   1986                 pw.println("Permission Denial: can't dump gfxinfo from from pid="
   1987                         + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
   1988                         + " without permission " + android.Manifest.permission.DUMP);
   1989                 return;
   1990             }
   1991 
   1992             mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args);
   1993         }
   1994     }
   1995 
   1996     static class DbBinder extends Binder {
   1997         ActivityManagerService mActivityManagerService;
   1998         DbBinder(ActivityManagerService activityManagerService) {
   1999             mActivityManagerService = activityManagerService;
   2000         }
   2001 
   2002         @Override
   2003         protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
   2004             if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
   2005                     != PackageManager.PERMISSION_GRANTED) {
   2006                 pw.println("Permission Denial: can't dump dbinfo from from pid="
   2007                         + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
   2008                         + " without permission " + android.Manifest.permission.DUMP);
   2009                 return;
   2010             }
   2011 
   2012             mActivityManagerService.dumpDbInfo(fd, pw, args);
   2013         }
   2014     }
   2015 
   2016     static class CpuBinder extends Binder {
   2017         ActivityManagerService mActivityManagerService;
   2018         CpuBinder(ActivityManagerService activityManagerService) {
   2019             mActivityManagerService = activityManagerService;
   2020         }
   2021 
   2022         @Override
   2023         protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
   2024             if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
   2025                     != PackageManager.PERMISSION_GRANTED) {
   2026                 pw.println("Permission Denial: can't dump cpuinfo from from pid="
   2027                         + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
   2028                         + " without permission " + android.Manifest.permission.DUMP);
   2029                 return;
   2030             }
   2031 
   2032             synchronized (mActivityManagerService.mProcessCpuTracker) {
   2033                 pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentLoad());
   2034                 pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentState(
   2035                         SystemClock.uptimeMillis()));
   2036             }
   2037         }
   2038     }
   2039 
   2040     public static final class Lifecycle extends SystemService {
   2041         private final ActivityManagerService mService;
   2042 
   2043         public Lifecycle(Context context) {
   2044             super(context);
   2045             mService = new ActivityManagerService(context);
   2046         }
   2047 
   2048         @Override
   2049         public void onStart() {
   2050             mService.start();
   2051         }
   2052 
   2053         public ActivityManagerService getService() {
   2054             return mService;
   2055         }
   2056     }
   2057 
   2058     // Note: This method is invoked on the main thread but may need to attach various
   2059     // handlers to other threads.  So take care to be explicit about the looper.
   2060     public ActivityManagerService(Context systemContext) {
   2061         mContext = systemContext;
   2062         mFactoryTest = FactoryTest.getMode();
   2063         mSystemThread = ActivityThread.currentActivityThread();
   2064 
   2065         Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
   2066 
   2067         mHandlerThread = new ServiceThread(TAG,
   2068                 android.os.Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
   2069         mHandlerThread.start();
   2070         mHandler = new MainHandler(mHandlerThread.getLooper());
   2071 
   2072         mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
   2073                 "foreground", BROADCAST_FG_TIMEOUT, false);
   2074         mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
   2075                 "background", BROADCAST_BG_TIMEOUT, true);
   2076         mBroadcastQueues[0] = mFgBroadcastQueue;
   2077         mBroadcastQueues[1] = mBgBroadcastQueue;
   2078 
   2079         mServices = new ActiveServices(this);
   2080         mProviderMap = new ProviderMap(this);
   2081 
   2082         // TODO: Move creation of battery stats service outside of activity manager service.
   2083         File dataDir = Environment.getDataDirectory();
   2084         File systemDir = new File(dataDir, "system");
   2085         systemDir.mkdirs();
   2086         mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
   2087         mBatteryStatsService.getActiveStatistics().readLocked();
   2088         mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
   2089         mOnBattery = DEBUG_POWER ? true
   2090                 : mBatteryStatsService.getActiveStatistics().getIsOnBattery();
   2091         mBatteryStatsService.getActiveStatistics().setCallback(this);
   2092 
   2093         mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
   2094 
   2095         mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml"), mHandler);
   2096 
   2097         mGrantFile = new AtomicFile(new File(systemDir, "urigrants.xml"));
   2098 
   2099         // User 0 is the first and only user that runs at boot.
   2100         mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true));
   2101         mUserLru.add(Integer.valueOf(0));
   2102         updateStartedUserArrayLocked();
   2103 
   2104         GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
   2105             ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
   2106 
   2107         mTrackingAssociations = "1".equals(SystemProperties.get("debug.track-associations"));
   2108 
   2109         mConfiguration.setToDefaults();
   2110         mConfiguration.locale = Locale.getDefault();
   2111 
   2112         mConfigurationSeq = mConfiguration.seq = 1;
   2113         mProcessCpuTracker.init();
   2114 
   2115         mCompatModePackages = new CompatModePackages(this, systemDir, mHandler);
   2116         mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler);
   2117         mStackSupervisor = new ActivityStackSupervisor(this);
   2118         mTaskPersister = new TaskPersister(systemDir, mStackSupervisor);
   2119 
   2120         mProcessCpuThread = new Thread("CpuTracker") {
   2121             @Override
   2122             public void run() {
   2123                 while (true) {
   2124                     try {
   2125                         try {
   2126                             synchronized(this) {
   2127                                 final long now = SystemClock.uptimeMillis();
   2128                                 long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
   2129                                 long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
   2130                                 //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
   2131                                 //        + ", write delay=" + nextWriteDelay);
   2132                                 if (nextWriteDelay < nextCpuDelay) {
   2133                                     nextCpuDelay = nextWriteDelay;
   2134                                 }
   2135                                 if (nextCpuDelay > 0) {
   2136                                     mProcessCpuMutexFree.set(true);
   2137                                     this.wait(nextCpuDelay);
   2138                                 }
   2139                             }
   2140                         } catch (InterruptedException e) {
   2141                         }
   2142                         updateCpuStatsNow();
   2143                     } catch (Exception e) {
   2144                         Slog.e(TAG, "Unexpected exception collecting process stats", e);
   2145                     }
   2146                 }
   2147             }
   2148         };
   2149 
   2150         Watchdog.getInstance().addMonitor(this);
   2151         Watchdog.getInstance().addThread(mHandler);
   2152     }
   2153 
   2154     public void setSystemServiceManager(SystemServiceManager mgr) {
   2155         mSystemServiceManager = mgr;
   2156     }
   2157 
   2158     public void setInstaller(Installer installer) {
   2159         mInstaller = installer;
   2160     }
   2161 
   2162     private void start() {
   2163         Process.removeAllProcessGroups();
   2164         mProcessCpuThread.start();
   2165 
   2166         mBatteryStatsService.publish(mContext);
   2167         mAppOpsService.publish(mContext);
   2168         Slog.d("AppOps", "AppOpsService published");
   2169         LocalServices.addService(ActivityManagerInternal.class, new LocalService());
   2170     }
   2171 
   2172     public void initPowerManagement() {
   2173         mStackSupervisor.initPowerManagement();
   2174         mBatteryStatsService.initPowerManagement();
   2175     }
   2176 
   2177     @Override
   2178     public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
   2179             throws RemoteException {
   2180         if (code == SYSPROPS_TRANSACTION) {
   2181             // We need to tell all apps about the system property change.
   2182             ArrayList<IBinder> procs = new ArrayList<IBinder>();
   2183             synchronized(this) {
   2184                 final int NP = mProcessNames.getMap().size();
   2185                 for (int ip=0; ip<NP; ip++) {
   2186                     SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
   2187                     final int NA = apps.size();
   2188                     for (int ia=0; ia<NA; ia++) {
   2189                         ProcessRecord app = apps.valueAt(ia);
   2190                         if (app.thread != null) {
   2191                             procs.add(app.thread.asBinder());
   2192                         }
   2193                     }
   2194                 }
   2195             }
   2196 
   2197             int N = procs.size();
   2198             for (int i=0; i<N; i++) {
   2199                 Parcel data2 = Parcel.obtain();
   2200                 try {
   2201                     procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0);
   2202                 } catch (RemoteException e) {
   2203                 }
   2204                 data2.recycle();
   2205             }
   2206         }
   2207         try {
   2208             return super.onTransact(code, data, reply, flags);
   2209         } catch (RuntimeException e) {
   2210             // The activity manager only throws security exceptions, so let's
   2211             // log all others.
   2212             if (!(e instanceof SecurityException)) {
   2213                 Slog.wtf(TAG, "Activity Manager Crash", e);
   2214             }
   2215             throw e;
   2216         }
   2217     }
   2218 
   2219     void updateCpuStats() {
   2220         final long now = SystemClock.uptimeMillis();
   2221         if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
   2222             return;
   2223         }
   2224         if (mProcessCpuMutexFree.compareAndSet(true, false)) {
   2225             synchronized (mProcessCpuThread) {
   2226                 mProcessCpuThread.notify();
   2227             }
   2228         }
   2229     }
   2230 
   2231     void updateCpuStatsNow() {
   2232         synchronized (mProcessCpuTracker) {
   2233             mProcessCpuMutexFree.set(false);
   2234             final long now = SystemClock.uptimeMillis();
   2235             boolean haveNewCpuStats = false;
   2236 
   2237             if (MONITOR_CPU_USAGE &&
   2238                     mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
   2239                 mLastCpuTime.set(now);
   2240                 haveNewCpuStats = true;
   2241                 mProcessCpuTracker.update();
   2242                 //Slog.i(TAG, mProcessCpu.printCurrentState());
   2243                 //Slog.i(TAG, "Total CPU usage: "
   2244                 //        + mProcessCpu.getTotalCpuPercent() + "%");
   2245 
   2246                 // Slog the cpu usage if the property is set.
   2247                 if ("true".equals(SystemProperties.get("events.cpu"))) {
   2248                     int user = mProcessCpuTracker.getLastUserTime();
   2249                     int system = mProcessCpuTracker.getLastSystemTime();
   2250                     int iowait = mProcessCpuTracker.getLastIoWaitTime();
   2251                     int irq = mProcessCpuTracker.getLastIrqTime();
   2252                     int softIrq = mProcessCpuTracker.getLastSoftIrqTime();
   2253                     int idle = mProcessCpuTracker.getLastIdleTime();
   2254 
   2255                     int total = user + system + iowait + irq + softIrq + idle;
   2256                     if (total == 0) total = 1;
   2257 
   2258                     EventLog.writeEvent(EventLogTags.CPU,
   2259                             ((user+system+iowait+irq+softIrq) * 100) / total,
   2260                             (user * 100) / total,
   2261                             (system * 100) / total,
   2262                             (iowait * 100) / total,
   2263                             (irq * 100) / total,
   2264                             (softIrq * 100) / total);
   2265                 }
   2266             }
   2267 
   2268             long[] cpuSpeedTimes = mProcessCpuTracker.getLastCpuSpeedTimes();
   2269             final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
   2270             synchronized(bstats) {
   2271                 synchronized(mPidsSelfLocked) {
   2272                     if (haveNewCpuStats) {
   2273                         if (mOnBattery) {
   2274                             int perc = bstats.startAddingCpuLocked();
   2275                             int totalUTime = 0;
   2276                             int totalSTime = 0;
   2277                             final int N = mProcessCpuTracker.countStats();
   2278                             for (int i=0; i<N; i++) {
   2279                                 ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
   2280                                 if (!st.working) {
   2281                                     continue;
   2282                                 }
   2283                                 ProcessRecord pr = mPidsSelfLocked.get(st.pid);
   2284                                 int otherUTime = (st.rel_utime*perc)/100;
   2285                                 int otherSTime = (st.rel_stime*perc)/100;
   2286                                 totalUTime += otherUTime;
   2287                                 totalSTime += otherSTime;
   2288                                 if (pr != null) {
   2289                                     BatteryStatsImpl.Uid.Proc ps = pr.curProcBatteryStats;
   2290                                     if (ps == null || !ps.isActive()) {
   2291                                         pr.curProcBatteryStats = ps = bstats.getProcessStatsLocked(
   2292                                                 pr.info.uid, pr.processName);
   2293                                     }
   2294                                     ps.addCpuTimeLocked(st.rel_utime-otherUTime,
   2295                                             st.rel_stime-otherSTime);
   2296                                     ps.addSpeedStepTimes(cpuSpeedTimes);
   2297                                     pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10;
   2298                                 } else {
   2299                                     BatteryStatsImpl.Uid.Proc ps = st.batteryStats;
   2300                                     if (ps == null || !ps.isActive()) {
   2301                                         st.batteryStats = ps = bstats.getProcessStatsLocked(
   2302                                                 bstats.mapUid(st.uid), st.name);
   2303                                     }
   2304                                     ps.addCpuTimeLocked(st.rel_utime-otherUTime,
   2305                                             st.rel_stime-otherSTime);
   2306                                     ps.addSpeedStepTimes(cpuSpeedTimes);
   2307                                 }
   2308                             }
   2309                             bstats.finishAddingCpuLocked(perc, totalUTime,
   2310                                     totalSTime, cpuSpeedTimes);
   2311                         }
   2312                     }
   2313                 }
   2314 
   2315                 if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
   2316                     mLastWriteTime = now;
   2317                     mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
   2318                 }
   2319             }
   2320         }
   2321     }
   2322 
   2323     @Override
   2324     public void batteryNeedsCpuUpdate() {
   2325         updateCpuStatsNow();
   2326     }
   2327 
   2328     @Override
   2329     public void batteryPowerChanged(boolean onBattery) {
   2330         // When plugging in, update the CPU stats first before changing
   2331         // the plug state.
   2332         updateCpuStatsNow();
   2333         synchronized (this) {
   2334             synchronized(mPidsSelfLocked) {
   2335                 mOnBattery = DEBUG_POWER ? true : onBattery;
   2336             }
   2337         }
   2338     }
   2339 
   2340     /**
   2341      * Initialize the application bind args. These are passed to each
   2342      * process when the bindApplication() IPC is sent to the process. They're
   2343      * lazily setup to make sure the services are running when they're asked for.
   2344      */
   2345     private HashMap<String, IBinder> getCommonServicesLocked(boolean isolated) {
   2346         if (mAppBindArgs == null) {
   2347             mAppBindArgs = new HashMap<>();
   2348 
   2349             // Isolated processes won't get this optimization, so that we don't
   2350             // violate the rules about which services they have access to.
   2351             if (!isolated) {
   2352                 // Setup the application init args
   2353                 mAppBindArgs.put("package", ServiceManager.getService("package"));
   2354                 mAppBindArgs.put("window", ServiceManager.getService("window"));
   2355                 mAppBindArgs.put(Context.ALARM_SERVICE,
   2356                         ServiceManager.getService(Context.ALARM_SERVICE));
   2357             }
   2358         }
   2359         return mAppBindArgs;
   2360     }
   2361 
   2362     final void setFocusedActivityLocked(ActivityRecord r, String reason) {
   2363         if (mFocusedActivity != r) {
   2364             if (DEBUG_FOCUS) Slog.d(TAG, "setFocusedActivityLocked: r=" + r);
   2365             mFocusedActivity = r;
   2366             if (r.task != null && r.task.voiceInteractor != null) {
   2367                 startRunningVoiceLocked();
   2368             } else {
   2369                 finishRunningVoiceLocked();
   2370             }
   2371             mStackSupervisor.setFocusedStack(r, reason + " setFocusedActivity");
   2372             if (r != null) {
   2373                 mWindowManager.setFocusedApp(r.appToken, true);
   2374             }
   2375             applyUpdateLockStateLocked(r);
   2376         }
   2377         EventLog.writeEvent(EventLogTags.AM_FOCUSED_ACTIVITY, mCurrentUserId,
   2378                 mFocusedActivity == null ? "NULL" : mFocusedActivity.shortComponentName);
   2379     }
   2380 
   2381     final void clearFocusedActivity(ActivityRecord r) {
   2382         if (mFocusedActivity == r) {
   2383             mFocusedActivity = null;
   2384         }
   2385     }
   2386 
   2387     @Override
   2388     public void setFocusedStack(int stackId) {
   2389         if (DEBUG_FOCUS) Slog.d(TAG, "setFocusedStack: stackId=" + stackId);
   2390         synchronized (ActivityManagerService.this) {
   2391             ActivityStack stack = mStackSupervisor.getStack(stackId);
   2392             if (stack != null) {
   2393                 ActivityRecord r = stack.topRunningActivityLocked(null);
   2394                 if (r != null) {
   2395                     setFocusedActivityLocked(r, "setFocusedStack");
   2396                 }
   2397             }
   2398         }
   2399     }
   2400 
   2401     /** Sets the task stack listener that gets callbacks when a task stack changes. */
   2402     @Override
   2403     public void registerTaskStackListener(ITaskStackListener listener) throws RemoteException {
   2404         synchronized (ActivityManagerService.this) {
   2405             if (listener != null) {
   2406                 mTaskStackListeners.register(listener);
   2407             }
   2408         }
   2409     }
   2410 
   2411     @Override
   2412     public void notifyActivityDrawn(IBinder token) {
   2413         if (DEBUG_VISBILITY) Slog.d(TAG, "notifyActivityDrawn: token=" + token);
   2414         synchronized (this) {
   2415             ActivityRecord r= mStackSupervisor.isInAnyStackLocked(token);
   2416             if (r != null) {
   2417                 r.task.stack.notifyActivityDrawnLocked(r);
   2418             }
   2419         }
   2420     }
   2421 
   2422     final void applyUpdateLockStateLocked(ActivityRecord r) {
   2423         // Modifications to the UpdateLock state are done on our handler, outside
   2424         // the activity manager's locks.  The new state is determined based on the
   2425         // state *now* of the relevant activity record.  The object is passed to
   2426         // the handler solely for logging detail, not to be consulted/modified.
   2427         final boolean nextState = r != null && r.immersive;
   2428         mHandler.sendMessage(
   2429                 mHandler.obtainMessage(IMMERSIVE_MODE_LOCK_MSG, (nextState) ? 1 : 0, 0, r));
   2430     }
   2431 
   2432     final void showAskCompatModeDialogLocked(ActivityRecord r) {
   2433         Message msg = Message.obtain();
   2434         msg.what = SHOW_COMPAT_MODE_DIALOG_MSG;
   2435         msg.obj = r.task.askedCompatMode ? null : r;
   2436         mHandler.sendMessage(msg);
   2437     }
   2438 
   2439     private int updateLruProcessInternalLocked(ProcessRecord app, long now, int index,
   2440             String what, Object obj, ProcessRecord srcApp) {
   2441         app.lastActivityTime = now;
   2442 
   2443         if (app.activities.size() > 0) {
   2444             // Don't want to touch dependent processes that are hosting activities.
   2445             return index;
   2446         }
   2447 
   2448         int lrui = mLruProcesses.lastIndexOf(app);
   2449         if (lrui < 0) {
   2450             Slog.wtf(TAG, "Adding dependent process " + app + " not on LRU list: "
   2451                     + what + " " + obj + " from " + srcApp);
   2452             return index;
   2453         }
   2454 
   2455         if (lrui >= index) {
   2456             // Don't want to cause this to move dependent processes *back* in the
   2457             // list as if they were less frequently used.
   2458             return index;
   2459         }
   2460 
   2461         if (lrui >= mLruProcessActivityStart) {
   2462             // Don't want to touch dependent processes that are hosting activities.
   2463             return index;
   2464         }
   2465 
   2466         mLruProcesses.remove(lrui);
   2467         if (index > 0) {
   2468             index--;
   2469         }
   2470         if (DEBUG_LRU) Slog.d(TAG, "Moving dep from " + lrui + " to " + index
   2471                 + " in LRU list: " + app);
   2472         mLruProcesses.add(index, app);
   2473         return index;
   2474     }
   2475 
   2476     final void removeLruProcessLocked(ProcessRecord app) {
   2477         int lrui = mLruProcesses.lastIndexOf(app);
   2478         if (lrui >= 0) {
   2479             if (!app.killed) {
   2480                 Slog.wtfStack(TAG, "Removing process that hasn't been killed: " + app);
   2481                 Process.killProcessQuiet(app.pid);
   2482                 Process.killProcessGroup(app.info.uid, app.pid);
   2483             }
   2484             if (lrui <= mLruProcessActivityStart) {
   2485                 mLruProcessActivityStart--;
   2486             }
   2487             if (lrui <= mLruProcessServiceStart) {
   2488                 mLruProcessServiceStart--;
   2489             }
   2490             mLruProcesses.remove(lrui);
   2491         }
   2492     }
   2493 
   2494     final void updateLruProcessLocked(ProcessRecord app, boolean activityChange,
   2495             ProcessRecord client) {
   2496         final boolean hasActivity = app.activities.size() > 0 || app.hasClientActivities
   2497                 || app.treatLikeActivity;
   2498         final boolean hasService = false; // not impl yet. app.services.size() > 0;
   2499         if (!activityChange && hasActivity) {
   2500             // The process has activities, so we are only allowing activity-based adjustments
   2501             // to move it.  It should be kept in the front of the list with other
   2502             // processes that have activities, and we don't want those to change their
   2503             // order except due to activity operations.
   2504             return;
   2505         }
   2506 
   2507         mLruSeq++;
   2508         final long now = SystemClock.uptimeMillis();
   2509         app.lastActivityTime = now;
   2510 
   2511         // First a quick reject: if the app is already at the position we will
   2512         // put it, then there is nothing to do.
   2513         if (hasActivity) {
   2514             final int N = mLruProcesses.size();
   2515             if (N > 0 && mLruProcesses.get(N-1) == app) {
   2516                 if (DEBUG_LRU) Slog.d(TAG, "Not moving, already top activity: " + app);
   2517                 return;
   2518             }
   2519         } else {
   2520             if (mLruProcessServiceStart > 0
   2521                     && mLruProcesses.get(mLruProcessServiceStart-1) == app) {
   2522                 if (DEBUG_LRU) Slog.d(TAG, "Not moving, already top other: " + app);
   2523                 return;
   2524             }
   2525         }
   2526 
   2527         int lrui = mLruProcesses.lastIndexOf(app);
   2528 
   2529         if (app.persistent && lrui >= 0) {
   2530             // We don't care about the position of persistent processes, as long as
   2531             // they are in the list.
   2532             if (DEBUG_LRU) Slog.d(TAG, "Not moving, persistent: " + app);
   2533             return;
   2534         }
   2535 
   2536         /* In progress: compute new position first, so we can avoid doing work
   2537            if the process is not actually going to move.  Not yet working.
   2538         int addIndex;
   2539         int nextIndex;
   2540         boolean inActivity = false, inService = false;
   2541         if (hasActivity) {
   2542             // Process has activities, put it at the very tipsy-top.
   2543             addIndex = mLruProcesses.size();
   2544             nextIndex = mLruProcessServiceStart;
   2545             inActivity = true;
   2546         } else if (hasService) {
   2547             // Process has services, put it at the top of the service list.
   2548             addIndex = mLruProcessActivityStart;
   2549             nextIndex = mLruProcessServiceStart;
   2550             inActivity = true;
   2551             inService = true;
   2552         } else  {
   2553             // Process not otherwise of interest, it goes to the top of the non-service area.
   2554             addIndex = mLruProcessServiceStart;
   2555             if (client != null) {
   2556                 int clientIndex = mLruProcesses.lastIndexOf(client);
   2557                 if (clientIndex < 0) Slog.d(TAG, "Unknown client " + client + " when updating "
   2558                         + app);
   2559                 if (clientIndex >= 0 && addIndex > clientIndex) {
   2560                     addIndex = clientIndex;
   2561                 }
   2562             }
   2563             nextIndex = addIndex > 0 ? addIndex-1 : addIndex;
   2564         }
   2565 
   2566         Slog.d(TAG, "Update LRU at " + lrui + " to " + addIndex + " (act="
   2567                 + mLruProcessActivityStart + "): " + app);
   2568         */
   2569 
   2570         if (lrui >= 0) {
   2571             if (lrui < mLruProcessActivityStart) {
   2572                 mLruProcessActivityStart--;
   2573             }
   2574             if (lrui < mLruProcessServiceStart) {
   2575                 mLruProcessServiceStart--;
   2576             }
   2577             /*
   2578             if (addIndex > lrui) {
   2579                 addIndex--;
   2580             }
   2581             if (nextIndex > lrui) {
   2582                 nextIndex--;
   2583             }
   2584             */
   2585             mLruProcesses.remove(lrui);
   2586         }
   2587 
   2588         /*
   2589         mLruProcesses.add(addIndex, app);
   2590         if (inActivity) {
   2591             mLruProcessActivityStart++;
   2592         }
   2593         if (inService) {
   2594             mLruProcessActivityStart++;
   2595         }
   2596         */
   2597 
   2598         int nextIndex;
   2599         if (hasActivity) {
   2600             final int N = mLruProcesses.size();
   2601             if (app.activities.size() == 0 && mLruProcessActivityStart < (N-1)) {
   2602                 // Process doesn't have activities, but has clients with
   2603                 // activities...  move it up, but one below the top (the top
   2604                 // should always have a real activity).
   2605                 if (DEBUG_LRU) Slog.d(TAG, "Adding to second-top of LRU activity list: " + app);
   2606                 mLruProcesses.add(N-1, app);
   2607                 // To keep it from spamming the LRU list (by making a bunch of clients),
   2608                 // we will push down any other entries owned by the app.
   2609                 final int uid = app.info.uid;
   2610                 for (int i=N-2; i>mLruProcessActivityStart; i--) {
   2611                     ProcessRecord subProc = mLruProcesses.get(i);
   2612                     if (subProc.info.uid == uid) {
   2613                         // We want to push this one down the list.  If the process after
   2614                         // it is for the same uid, however, don't do so, because we don't
   2615                         // want them internally to be re-ordered.
   2616                         if (mLruProcesses.get(i-1).info.uid != uid) {
   2617                             if (DEBUG_LRU) Slog.d(TAG, "Pushing uid " + uid + " swapping at " + i
   2618                                     + ": " + mLruProcesses.get(i) + " : " + mLruProcesses.get(i-1));
   2619                             ProcessRecord tmp = mLruProcesses.get(i);
   2620                             mLruProcesses.set(i, mLruProcesses.get(i-1));
   2621                             mLruProcesses.set(i-1, tmp);
   2622                             i--;
   2623                         }
   2624                     } else {
   2625                         // A gap, we can stop here.
   2626                         break;
   2627                     }
   2628                 }
   2629             } else {
   2630                 // Process has activities, put it at the very tipsy-top.
   2631                 if (DEBUG_LRU) Slog.d(TAG, "Adding to top of LRU activity list: " + app);
   2632                 mLruProcesses.add(app);
   2633             }
   2634             nextIndex = mLruProcessServiceStart;
   2635         } else if (hasService) {
   2636             // Process has services, put it at the top of the service list.
   2637             if (DEBUG_LRU) Slog.d(TAG, "Adding to top of LRU service list: " + app);
   2638             mLruProcesses.add(mLruProcessActivityStart, app);
   2639             nextIndex = mLruProcessServiceStart;
   2640             mLruProcessActivityStart++;
   2641         } else  {
   2642             // Process not otherwise of interest, it goes to the top of the non-service area.
   2643             int index = mLruProcessServiceStart;
   2644             if (client != null) {
   2645                 // If there is a client, don't allow the process to be moved up higher
   2646                 // in the list than that client.
   2647                 int clientIndex = mLruProcesses.lastIndexOf(client);
   2648                 if (DEBUG_LRU && clientIndex < 0) Slog.d(TAG, "Unknown client " + client
   2649                         + " when updating " + app);
   2650                 if (clientIndex <= lrui) {
   2651                     // Don't allow the client index restriction to push it down farther in the
   2652                     // list than it already is.
   2653                     clientIndex = lrui;
   2654                 }
   2655                 if (clientIndex >= 0 && index > clientIndex) {
   2656                     index = clientIndex;
   2657                 }
   2658             }
   2659             if (DEBUG_LRU) Slog.d(TAG, "Adding at " + index + " of LRU list: " + app);
   2660             mLruProcesses.add(index, app);
   2661             nextIndex = index-1;
   2662             mLruProcessActivityStart++;
   2663             mLruProcessServiceStart++;
   2664         }
   2665 
   2666         // If the app is currently using a content provider or service,
   2667         // bump those processes as well.
   2668         for (int j=app.connections.size()-1; j>=0; j--) {
   2669             ConnectionRecord cr = app.connections.valueAt(j);
   2670             if (cr.binding != null && !cr.serviceDead && cr.binding.service != null
   2671                     && cr.binding.service.app != null
   2672                     && cr.binding.service.app.lruSeq != mLruSeq
   2673                     && !cr.binding.service.app.persistent) {
   2674                 nextIndex = updateLruProcessInternalLocked(cr.binding.service.app, now, nextIndex,
   2675                         "service connection", cr, app);
   2676             }
   2677         }
   2678         for (int j=app.conProviders.size()-1; j>=0; j--) {
   2679             ContentProviderRecord cpr = app.conProviders.get(j).provider;
   2680             if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq && !cpr.proc.persistent) {
   2681                 nextIndex = updateLruProcessInternalLocked(cpr.proc, now, nextIndex,
   2682                         "provider reference", cpr, app);
   2683             }
   2684         }
   2685     }
   2686 
   2687     final ProcessRecord getProcessRecordLocked(String processName, int uid, boolean keepIfLarge) {
   2688         if (uid == Process.SYSTEM_UID) {
   2689             // The system gets to run in any process.  If there are multiple
   2690             // processes with the same uid, just pick the first (this
   2691             // should never happen).
   2692             SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(processName);
   2693             if (procs == null) return null;
   2694             final int N = procs.size();
   2695             for (int i = 0; i < N; i++) {
   2696                 if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i);
   2697             }
   2698         }
   2699         ProcessRecord proc = mProcessNames.get(processName, uid);
   2700         if (false && proc != null && !keepIfLarge
   2701                 && proc.setProcState >= ActivityManager.PROCESS_STATE_CACHED_EMPTY
   2702                 && proc.lastCachedPss >= 4000) {
   2703             // Turn this condition on to cause killing to happen regularly, for testing.
   2704             if (proc.baseProcessTracker != null) {
   2705                 proc.baseProcessTracker.reportCachedKill(proc.pkgList, proc.lastCachedPss);
   2706             }
   2707             proc.kill(Long.toString(proc.lastCachedPss) + "k from cached", true);
   2708         } else if (proc != null && !keepIfLarge
   2709                 && mLastMemoryLevel > ProcessStats.ADJ_MEM_FACTOR_NORMAL
   2710                 && proc.setProcState >= ActivityManager.PROCESS_STATE_CACHED_EMPTY) {
   2711             if (DEBUG_PSS) Slog.d(TAG, "May not keep " + proc + ": pss=" + proc.lastCachedPss);
   2712             if (proc.lastCachedPss >= mProcessList.getCachedRestoreThresholdKb()) {
   2713                 if (proc.baseProcessTracker != null) {
   2714                     proc.baseProcessTracker.reportCachedKill(proc.pkgList, proc.lastCachedPss);
   2715                 }
   2716                 proc.kill(Long.toString(proc.lastCachedPss) + "k from cached", true);
   2717             }
   2718         }
   2719         return proc;
   2720     }
   2721 
   2722     void ensurePackageDexOpt(String packageName) {
   2723         IPackageManager pm = AppGlobals.getPackageManager();
   2724         try {
   2725             if (pm.performDexOptIfNeeded(packageName, null /* instruction set */)) {
   2726                 mDidDexOpt = true;
   2727             }
   2728         } catch (RemoteException e) {
   2729         }
   2730     }
   2731 
   2732     boolean isNextTransitionForward() {
   2733         int transit = mWindowManager.getPendingAppTransition();
   2734         return transit == AppTransition.TRANSIT_ACTIVITY_OPEN
   2735                 || transit == AppTransition.TRANSIT_TASK_OPEN
   2736                 || transit == AppTransition.TRANSIT_TASK_TO_FRONT;
   2737     }
   2738 
   2739     int startIsolatedProcess(String entryPoint, String[] entryPointArgs,
   2740             String processName, String abiOverride, int uid, Runnable crashHandler) {
   2741         synchronized(this) {
   2742             ApplicationInfo info = new ApplicationInfo();
   2743             // In general the ApplicationInfo.uid isn't neccesarily equal to ProcessRecord.uid.
   2744             // For isolated processes, the former contains the parent's uid and the latter the
   2745             // actual uid of the isolated process.
   2746             // In the special case introduced by this method (which is, starting an isolated
   2747             // process directly from the SystemServer without an actual parent app process) the
   2748             // closest thing to a parent's uid is SYSTEM_UID.
   2749             // The only important thing here is to keep AI.uid != PR.uid, in order to trigger
   2750             // the |isolated| logic in the ProcessRecord constructor.
   2751             info.uid = Process.SYSTEM_UID;
   2752             info.processName = processName;
   2753             info.className = entryPoint;
   2754             info.packageName = "android";
   2755             ProcessRecord proc = startProcessLocked(processName, info /* info */,
   2756                     false /* knownToBeDead */, 0 /* intentFlags */, ""  /* hostingType */,
   2757                     null /* hostingName */, true /* allowWhileBooting */, true /* isolated */,
   2758                     uid, true /* keepIfLarge */, abiOverride, entryPoint, entryPointArgs,
   2759                     crashHandler);
   2760             return proc != null ? proc.pid : 0;
   2761         }
   2762     }
   2763 
   2764     final ProcessRecord startProcessLocked(String processName,
   2765             ApplicationInfo info, boolean knownToBeDead, int intentFlags,
   2766             String hostingType, ComponentName hostingName, boolean allowWhileBooting,
   2767             boolean isolated, boolean keepIfLarge) {
   2768         return startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingType,
   2769                 hostingName, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,
   2770                 null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,
   2771                 null /* crashHandler */);
   2772     }
   2773 
   2774     final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
   2775             boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,
   2776             boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,
   2777             String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
   2778         long startTime = SystemClock.elapsedRealtime();
   2779         ProcessRecord app;
   2780         if (!isolated) {
   2781             app = getProcessRecordLocked(processName, info.uid, keepIfLarge);
   2782             checkTime(startTime, "startProcess: after getProcessRecord");
   2783         } else {
   2784             // If this is an isolated process, it can't re-use an existing process.
   2785             app = null;
   2786         }
   2787         // We don't have to do anything more if:
   2788         // (1) There is an existing application record; and
   2789         // (2) The caller doesn't think it is dead, OR there is no thread
   2790         //     object attached to it so we know it couldn't have crashed; and
   2791         // (3) There is a pid assigned to it, so it is either starting or
   2792         //     already running.
   2793         if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
   2794                 + " app=" + app + " knownToBeDead=" + knownToBeDead
   2795                 + " thread=" + (app != null ? app.thread : null)
   2796                 + " pid=" + (app != null ? app.pid : -1));
   2797         if (app != null && app.pid > 0) {
   2798             if (!knownToBeDead || app.thread == null) {
   2799                 // We already have the app running, or are waiting for it to
   2800                 // come up (we have a pid but not yet its thread), so keep it.
   2801                 if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app);
   2802                 // If this is a new package in the process, add the package to the list
   2803                 app.addPackage(info.packageName, info.versionCode, mProcessStats);
   2804                 checkTime(startTime, "startProcess: done, added package to proc");
   2805                 return app;
   2806             }
   2807 
   2808             // An application record is attached to a previous process,
   2809             // clean it up now.
   2810             if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, "App died: " + app);
   2811             checkTime(startTime, "startProcess: bad proc running, killing");
   2812             Process.killProcessGroup(app.info.uid, app.pid);
   2813             handleAppDiedLocked(app, true, true);
   2814             checkTime(startTime, "startProcess: done killing old proc");
   2815         }
   2816 
   2817         String hostingNameStr = hostingName != null
   2818                 ? hostingName.flattenToShortString() : null;
   2819 
   2820         if (!isolated) {
   2821             if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
   2822                 // If we are in the background, then check to see if this process
   2823                 // is bad.  If so, we will just silently fail.
   2824                 if (mBadProcesses.get(info.processName, info.uid) != null) {
   2825                     if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid
   2826                             + "/" + info.processName);
   2827                     return null;
   2828                 }
   2829             } else {
   2830                 // When the user is explicitly starting a process, then clear its
   2831                 // crash count so that we won't make it bad until they see at
   2832                 // least one crash dialog again, and make the process good again
   2833                 // if it had been bad.
   2834                 if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid
   2835                         + "/" + info.processName);
   2836                 mProcessCrashTimes.remove(info.processName, info.uid);
   2837                 if (mBadProcesses.get(info.processName, info.uid) != null) {
   2838                     EventLog.writeEvent(EventLogTags.AM_PROC_GOOD,
   2839                             UserHandle.getUserId(info.uid), info.uid,
   2840                             info.processName);
   2841                     mBadProcesses.remove(info.processName, info.uid);
   2842                     if (app != null) {
   2843                         app.bad = false;
   2844                     }
   2845                 }
   2846             }
   2847         }
   2848 
   2849         if (app == null) {
   2850             checkTime(startTime, "startProcess: creating new process record");
   2851             app = newProcessRecordLocked(info, processName, isolated, isolatedUid);
   2852             if (app == null) {
   2853                 Slog.w(TAG, "Failed making new process record for "
   2854                         + processName + "/" + info.uid + " isolated=" + isolated);
   2855                 return null;
   2856             }
   2857             app.crashHandler = crashHandler;
   2858             mProcessNames.put(processName, app.uid, app);
   2859             if (isolated) {
   2860                 mIsolatedProcesses.put(app.uid, app);
   2861             }
   2862             checkTime(startTime, "startProcess: done creating new process record");
   2863         } else {
   2864             // If this is a new package in the process, add the package to the list
   2865             app.addPackage(info.packageName, info.versionCode, mProcessStats);
   2866             checkTime(startTime, "startProcess: added package to existing proc");
   2867         }
   2868 
   2869         // If the system is not ready yet, then hold off on starting this
   2870         // process until it is.
   2871         if (!mProcessesReady
   2872                 && !isAllowedWhileBooting(info)
   2873                 && !allowWhileBooting) {
   2874             if (!mProcessesOnHold.contains(app)) {
   2875                 mProcessesOnHold.add(app);
   2876             }
   2877             if (DEBUG_PROCESSES) Slog.v(TAG, "System not ready, putting on hold: " + app);
   2878             checkTime(startTime, "startProcess: returning with proc on hold");
   2879             return app;
   2880         }
   2881 
   2882         checkTime(startTime, "startProcess: stepping in to startProcess");
   2883         startProcessLocked(
   2884                 app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs);
   2885         checkTime(startTime, "startProcess: done starting proc!");
   2886         return (app.pid != 0) ? app : null;
   2887     }
   2888 
   2889     boolean isAllowedWhileBooting(ApplicationInfo ai) {
   2890         return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
   2891     }
   2892 
   2893     private final void startProcessLocked(ProcessRecord app,
   2894             String hostingType, String hostingNameStr) {
   2895         startProcessLocked(app, hostingType, hostingNameStr, null /* abiOverride */,
   2896                 null /* entryPoint */, null /* entryPointArgs */);
   2897     }
   2898 
   2899     private final void startProcessLocked(ProcessRecord app, String hostingType,
   2900             String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
   2901         long startTime = SystemClock.elapsedRealtime();
   2902         if (app.pid > 0 && app.pid != MY_PID) {
   2903             checkTime(startTime, "startProcess: removing from pids map");
   2904             synchronized (mPidsSelfLocked) {
   2905                 mPidsSelfLocked.remove(app.pid);
   2906                 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
   2907             }
   2908             checkTime(startTime, "startProcess: done removing from pids map");
   2909             app.setPid(0);
   2910         }
   2911 
   2912         if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
   2913                 "startProcessLocked removing on hold: " + app);
   2914         mProcessesOnHold.remove(app);
   2915 
   2916         checkTime(startTime, "startProcess: starting to update cpu stats");
   2917         updateCpuStats();
   2918         checkTime(startTime, "startProcess: done updating cpu stats");
   2919 
   2920         try {
   2921             int uid = app.uid;
   2922 
   2923             int[] gids = null;
   2924             int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;
   2925             if (!app.isolated) {
   2926                 int[] permGids = null;
   2927                 try {
   2928                     checkTime(startTime, "startProcess: getting gids from package manager");
   2929                     final PackageManager pm = mContext.getPackageManager();
   2930                     permGids = pm.getPackageGids(app.info.packageName);
   2931 
   2932                     if (Environment.isExternalStorageEmulated()) {
   2933                         checkTime(startTime, "startProcess: checking external storage perm");
   2934                         if (pm.checkPermission(
   2935                                 android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE,
   2936                                 app.info.packageName) == PERMISSION_GRANTED) {
   2937                             mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL;
   2938                         } else {
   2939                             mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER;
   2940                         }
   2941                     }
   2942                 } catch (PackageManager.NameNotFoundException e) {
   2943                     Slog.w(TAG, "Unable to retrieve gids", e);
   2944                 }
   2945 
   2946                 /*
   2947                  * Add shared application and profile GIDs so applications can share some
   2948                  * resources like shared libraries and access user-wide resources
   2949                  */
   2950                 if (permGids == null) {
   2951                     gids = new int[2];
   2952                 } else {
   2953                     gids = new int[permGids.length + 2];
   2954                     System.arraycopy(permGids, 0, gids, 2, permGids.length);
   2955                 }
   2956                 gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
   2957                 gids[1] = UserHandle.getUserGid(UserHandle.getUserId(uid));
   2958             }
   2959             checkTime(startTime, "startProcess: building args");
   2960             if (mFactoryTest != FactoryTest.FACTORY_TEST_OFF) {
   2961                 if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
   2962                         && mTopComponent != null
   2963                         && app.processName.equals(mTopComponent.getPackageName())) {
   2964                     uid = 0;
   2965                 }
   2966                 if (mFactoryTest == FactoryTest.FACTORY_TEST_HIGH_LEVEL
   2967                         && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
   2968                     uid = 0;
   2969                 }
   2970             }
   2971             int debugFlags = 0;
   2972             if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
   2973                 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
   2974                 // Also turn on CheckJNI for debuggable apps. It's quite
   2975                 // awkward to turn on otherwise.
   2976                 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
   2977             }
   2978             // Run the app in safe mode if its manifest requests so or the
   2979             // system is booted in safe mode.
   2980             if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
   2981                 mSafeMode == true) {
   2982                 debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
   2983             }
   2984             if ("1".equals(SystemProperties.get("debug.checkjni"))) {
   2985                 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
   2986             }
   2987             if ("1".equals(SystemProperties.get("debug.jni.logging"))) {
   2988                 debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING;
   2989             }
   2990             if ("1".equals(SystemProperties.get("debug.assert"))) {
   2991                 debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
   2992             }
   2993 
   2994             String requiredAbi = (abiOverride != null) ? abiOverride : app.info.primaryCpuAbi;
   2995             if (requiredAbi == null) {
   2996                 requiredAbi = Build.SUPPORTED_ABIS[0];
   2997             }
   2998 
   2999             String instructionSet = null;
   3000             if (app.info.primaryCpuAbi != null) {
   3001                 instructionSet = VMRuntime.getInstructionSet(app.info.primaryCpuAbi);
   3002             }
   3003 
   3004             app.gids = gids;
   3005             app.requiredAbi = requiredAbi;
   3006             app.instructionSet = instructionSet;
   3007 
   3008             // Start the process.  It will either succeed and return a result containing
   3009             // the PID of the new process, or else throw a RuntimeException.
   3010             boolean isActivityProcess = (entryPoint == null);
   3011             if (entryPoint == null) entryPoint = "android.app.ActivityThread";
   3012             checkTime(startTime, "startProcess: asking zygote to start proc");
   3013             Process.ProcessStartResult startResult = Process.start(entryPoint,
   3014                     app.processName, uid, uid, gids, debugFlags, mountExternal,
   3015                     app.info.targetSdkVersion, app.info.seinfo, requiredAbi, instructionSet,
   3016                     app.info.dataDir, entryPointArgs);
   3017             checkTime(startTime, "startProcess: returned from zygote!");
   3018 
   3019             if (app.isolated) {
   3020                 mBatteryStatsService.addIsolatedUid(app.uid, app.info.uid);
   3021             }
   3022             mBatteryStatsService.noteProcessStart(app.processName, app.info.uid);
   3023             checkTime(startTime, "startProcess: done updating battery stats");
   3024 
   3025             EventLog.writeEvent(EventLogTags.AM_PROC_START,
   3026                     UserHandle.getUserId(uid), startResult.pid, uid,
   3027                     app.processName, hostingType,
   3028                     hostingNameStr != null ? hostingNameStr : "");
   3029 
   3030             if (app.persistent) {
   3031                 Watchdog.getInstance().processStarted(app.processName, startResult.pid);
   3032             }
   3033 
   3034             checkTime(startTime, "startProcess: building log message");
   3035             StringBuilder buf = mStringBuilder;
   3036             buf.setLength(0);
   3037             buf.append("Start proc ");
   3038             buf.append(startResult.pid);
   3039             buf.append(':');
   3040             buf.append(app.processName);
   3041             buf.append('/');
   3042             UserHandle.formatUid(buf, uid);
   3043             if (!isActivityProcess) {
   3044                 buf.append(" [");
   3045                 buf.append(entryPoint);
   3046                 buf.append("]");
   3047             }
   3048             buf.append(" for ");
   3049             buf.append(hostingType);
   3050             if (hostingNameStr != null) {
   3051                 buf.append(" ");
   3052                 buf.append(hostingNameStr);
   3053             }
   3054             Slog.i(TAG, buf.toString());
   3055             app.setPid(startResult.pid);
   3056             app.usingWrapper = startResult.usingWrapper;
   3057             app.removed = false;
   3058             app.killed = false;
   3059             app.killedByAm = false;
   3060             checkTime(startTime, "startProcess: starting to update pids map");
   3061             synchronized (mPidsSelfLocked) {
   3062                 this.mPidsSelfLocked.put(startResult.pid, app);
   3063                 if (isActivityProcess) {
   3064                     Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
   3065                     msg.obj = app;
   3066                     mHandler.sendMessageDelayed(msg, startResult.usingWrapper
   3067                             ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT);
   3068                 }
   3069             }
   3070             checkTime(startTime, "startProcess: done updating pids map");
   3071         } catch (RuntimeException e) {
   3072             // XXX do better error recovery.
   3073             app.setPid(0);
   3074             mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
   3075             if (app.isolated) {
   3076                 mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
   3077             }
   3078             Slog.e(TAG, "Failure starting process " + app.processName, e);
   3079         }
   3080     }
   3081 
   3082     void updateUsageStats(ActivityRecord component, boolean resumed) {
   3083         if (DEBUG_SWITCH) Slog.d(TAG, "updateUsageStats: comp=" + component + "res=" + resumed);
   3084         final BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
   3085         if (resumed) {
   3086             if (mUsageStatsService != null) {
   3087                 mUsageStatsService.reportEvent(component.realActivity, component.userId,
   3088                         UsageEvents.Event.MOVE_TO_FOREGROUND);
   3089             }
   3090             synchronized (stats) {
   3091                 stats.noteActivityResumedLocked(component.app.uid);
   3092             }
   3093         } else {
   3094             if (mUsageStatsService != null) {
   3095                 mUsageStatsService.reportEvent(component.realActivity, component.userId,
   3096                         UsageEvents.Event.MOVE_TO_BACKGROUND);
   3097             }
   3098             synchronized (stats) {
   3099                 stats.noteActivityPausedLocked(component.app.uid);
   3100             }
   3101         }
   3102     }
   3103 
   3104     Intent getHomeIntent() {
   3105         Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
   3106         intent.setComponent(mTopComponent);
   3107         if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
   3108             intent.addCategory(Intent.CATEGORY_HOME);
   3109         }
   3110         return intent;
   3111     }
   3112 
   3113     boolean startHomeActivityLocked(int userId, String reason) {
   3114         if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
   3115                 && mTopAction == null) {
   3116             // We are running in factory test mode, but unable to find
   3117             // the factory test app, so just sit around displaying the
   3118             // error message and don't try to start anything.
   3119             return false;
   3120         }
   3121         Intent intent = getHomeIntent();
   3122         ActivityInfo aInfo =
   3123             resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
   3124         if (aInfo != null) {
   3125             intent.setComponent(new ComponentName(
   3126                     aInfo.applicationInfo.packageName, aInfo.name));
   3127             // Don't do this if the home app is currently being
   3128             // instrumented.
   3129             aInfo = new ActivityInfo(aInfo);
   3130             aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
   3131             ProcessRecord app = getProcessRecordLocked(aInfo.processName,
   3132                     aInfo.applicationInfo.uid, true);
   3133             if (app == null || app.instrumentationClass == null) {
   3134                 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
   3135                 mStackSupervisor.startHomeActivity(intent, aInfo, reason);
   3136             }
   3137         }
   3138 
   3139         return true;
   3140     }
   3141 
   3142     private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) {
   3143         ActivityInfo ai = null;
   3144         ComponentName comp = intent.getComponent();
   3145         try {
   3146             if (comp != null) {
   3147                 ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId);
   3148             } else {
   3149                 ResolveInfo info = AppGlobals.getPackageManager().resolveIntent(
   3150                         intent,
   3151                         intent.resolveTypeIfNeeded(mContext.getContentResolver()),
   3152                             flags, userId);
   3153 
   3154                 if (info != null) {
   3155                     ai = info.activityInfo;
   3156                 }
   3157             }
   3158         } catch (RemoteException e) {
   3159             // ignore
   3160         }
   3161 
   3162         return ai;
   3163     }
   3164 
   3165     /**
   3166      * Starts the "new version setup screen" if appropriate.
   3167      */
   3168     void startSetupActivityLocked() {
   3169         // Only do this once per boot.
   3170         if (mCheckedForSetup) {
   3171             return;
   3172         }
   3173 
   3174         // We will show this screen if the current one is a different
   3175         // version than the last one shown, and we are not running in
   3176         // low-level factory test mode.
   3177         final ContentResolver resolver = mContext.getContentResolver();
   3178         if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL &&
   3179                 Settings.Global.getInt(resolver,
   3180                         Settings.Global.DEVICE_PROVISIONED, 0) != 0) {
   3181             mCheckedForSetup = true;
   3182 
   3183             // See if we should be showing the platform update setup UI.
   3184             Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
   3185             List<ResolveInfo> ris = mContext.getPackageManager()
   3186                     .queryIntentActivities(intent, PackageManager.GET_META_DATA);
   3187 
   3188             // We don't allow third party apps to replace this.
   3189             ResolveInfo ri = null;
   3190             for (int i=0; ris != null && i<ris.size(); i++) {
   3191                 if ((ris.get(i).activityInfo.applicationInfo.flags
   3192                         & ApplicationInfo.FLAG_SYSTEM) != 0) {
   3193                     ri = ris.get(i);
   3194                     break;
   3195                 }
   3196             }
   3197 
   3198             if (ri != null) {
   3199                 String vers = ri.activityInfo.metaData != null
   3200                         ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
   3201                         : null;
   3202                 if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
   3203                     vers = ri.activityInfo.applicationInfo.metaData.getString(
   3204                             Intent.METADATA_SETUP_VERSION);
   3205                 }
   3206                 String lastVers = Settings.Secure.getString(
   3207                         resolver, Settings.Secure.LAST_SETUP_SHOWN);
   3208                 if (vers != null && !vers.equals(lastVers)) {
   3209                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   3210                     intent.setComponent(new ComponentName(
   3211                             ri.activityInfo.packageName, ri.activityInfo.name));
   3212                     mStackSupervisor.startActivityLocked(null, intent, null, ri.activityInfo,
   3213                             null, null, null, null, 0, 0, 0, null, 0, 0, 0, null, false, null, null,
   3214                             null);
   3215                 }
   3216             }
   3217         }
   3218     }
   3219 
   3220     CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
   3221         return mCompatModePackages.compatibilityInfoForPackageLocked(ai);
   3222     }
   3223 
   3224     void enforceNotIsolatedCaller(String caller) {
   3225         if (UserHandle.isIsolated(Binder.getCallingUid())) {
   3226             throw new SecurityException("Isolated process not allowed to call " + caller);
   3227         }
   3228     }
   3229 
   3230     void enforceShellRestriction(String restriction, int userHandle) {
   3231         if (Binder.getCallingUid() == Process.SHELL_UID) {
   3232             if (userHandle < 0
   3233                     || mUserManager.hasUserRestriction(restriction, userHandle)) {
   3234                 throw new SecurityException("Shell does not have permission to access user "
   3235                         + userHandle);
   3236             }
   3237         }
   3238     }
   3239 
   3240     @Override
   3241     public int getFrontActivityScreenCompatMode() {
   3242         enforceNotIsolatedCaller("getFrontActivityScreenCompatMode");
   3243         synchronized (this) {
   3244             return mCompatModePackages.getFrontActivityScreenCompatModeLocked();
   3245         }
   3246     }
   3247 
   3248     @Override
   3249     public void setFrontActivityScreenCompatMode(int mode) {
   3250         enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
   3251                 "setFrontActivityScreenCompatMode");
   3252         synchronized (this) {
   3253             mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode);
   3254         }
   3255     }
   3256 
   3257     @Override
   3258     public int getPackageScreenCompatMode(String packageName) {
   3259         enforceNotIsolatedCaller("getPackageScreenCompatMode");
   3260         synchronized (this) {
   3261             return mCompatModePackages.getPackageScreenCompatModeLocked(packageName);
   3262         }
   3263     }
   3264 
   3265     @Override
   3266     public void setPackageScreenCompatMode(String packageName, int mode) {
   3267         enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
   3268                 "setPackageScreenCompatMode");
   3269         synchronized (this) {
   3270             mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode);
   3271         }
   3272     }
   3273 
   3274     @Override
   3275     public boolean getPackageAskScreenCompat(String packageName) {
   3276         enforceNotIsolatedCaller("getPackageAskScreenCompat");
   3277         synchronized (this) {
   3278             return mCompatModePackages.getPackageAskCompatModeLocked(packageName);
   3279         }
   3280     }
   3281 
   3282     @Override
   3283     public void setPackageAskScreenCompat(String packageName, boolean ask) {
   3284         enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
   3285                 "setPackageAskScreenCompat");
   3286         synchronized (this) {
   3287             mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask);
   3288         }
   3289     }
   3290 
   3291     private void dispatchProcessesChanged() {
   3292         int N;
   3293         synchronized (this) {
   3294             N = mPendingProcessChanges.size();
   3295             if (mActiveProcessChanges.length < N) {
   3296                 mActiveProcessChanges = new ProcessChangeItem[N];
   3297             }
   3298             mPendingProcessChanges.toArray(mActiveProcessChanges);
   3299             mAvailProcessChanges.addAll(mPendingProcessChanges);
   3300             mPendingProcessChanges.clear();
   3301             if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes");
   3302         }
   3303 
   3304         int i = mProcessObservers.beginBroadcast();
   3305         while (i > 0) {
   3306             i--;
   3307             final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
   3308             if (observer != null) {
   3309                 try {
   3310                     for (int j=0; j<N; j++) {
   3311                         ProcessChangeItem item = mActiveProcessChanges[j];
   3312                         if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) {
   3313                             if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid="
   3314                                     + item.pid + " uid=" + item.uid + ": "
   3315                                     + item.foregroundActivities);
   3316                             observer.onForegroundActivitiesChanged(item.pid, item.uid,
   3317                                     item.foregroundActivities);
   3318                         }
   3319                         if ((item.changes&ProcessChangeItem.CHANGE_PROCESS_STATE) != 0) {
   3320                             if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "PROCSTATE CHANGED pid="
   3321                                     + item.pid + " uid=" + item.uid + ": " + item.processState);
   3322                             observer.onProcessStateChanged(item.pid, item.uid, item.processState);
   3323                         }
   3324                     }
   3325                 } catch (RemoteException e) {
   3326                 }
   3327             }
   3328         }
   3329         mProcessObservers.finishBroadcast();
   3330     }
   3331 
   3332     private void dispatchProcessDied(int pid, int uid) {
   3333         int i = mProcessObservers.beginBroadcast();
   3334         while (i > 0) {
   3335             i--;
   3336             final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
   3337             if (observer != null) {
   3338                 try {
   3339                     observer.onProcessDied(pid, uid);
   3340                 } catch (RemoteException e) {
   3341                 }
   3342             }
   3343         }
   3344         mProcessObservers.finishBroadcast();
   3345     }
   3346 
   3347     @Override
   3348     public final int startActivity(IApplicationThread caller, String callingPackage,
   3349             Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
   3350             int startFlags, ProfilerInfo profilerInfo, Bundle options) {
   3351         return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
   3352             resultWho, requestCode, startFlags, profilerInfo, options,
   3353             UserHandle.getCallingUserId());
   3354     }
   3355 
   3356     @Override
   3357     public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
   3358             Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
   3359             int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) {
   3360         enforceNotIsolatedCaller("startActivity");
   3361         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
   3362                 false, ALLOW_FULL_ONLY, "startActivity", null);
   3363         // TODO: Switch to user app stacks here.
   3364         return mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent,
   3365                 resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
   3366                 profilerInfo, null, null, options, userId, null, null);
   3367     }
   3368 
   3369     @Override
   3370     public final int startActivityAsCaller(IApplicationThread caller, String callingPackage,
   3371             Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
   3372             int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) {
   3373 
   3374         // This is very dangerous -- it allows you to perform a start activity (including
   3375         // permission grants) as any app that may launch one of your own activities.  So
   3376         // we will only allow this to be done from activities that are part of the core framework,
   3377         // and then only when they are running as the system.
   3378         final ActivityRecord sourceRecord;
   3379         final int targetUid;
   3380         final String targetPackage;
   3381         synchronized (this) {
   3382             if (resultTo == null) {
   3383                 throw new SecurityException("Must be called from an activity");
   3384             }
   3385             sourceRecord = mStackSupervisor.isInAnyStackLocked(resultTo);
   3386             if (sourceRecord == null) {
   3387                 throw new SecurityException("Called with bad activity token: " + resultTo);
   3388             }
   3389             if (!sourceRecord.info.packageName.equals("android")) {
   3390                 throw new SecurityException(
   3391                         "Must be called from an activity that is declared in the android package");
   3392             }
   3393             if (sourceRecord.app == null) {
   3394                 throw new SecurityException("Called without a process attached to activity");
   3395             }
   3396             if (UserHandle.getAppId(sourceRecord.app.uid) != Process.SYSTEM_UID) {
   3397                 // This is still okay, as long as this activity is running under the
   3398                 // uid of the original calling activity.
   3399                 if (sourceRecord.app.uid != sourceRecord.launchedFromUid) {
   3400                     throw new SecurityException(
   3401                             "Calling activity in uid " + sourceRecord.app.uid
   3402                                     + " must be system uid or original calling uid "
   3403                                     + sourceRecord.launchedFromUid);
   3404                 }
   3405             }
   3406             targetUid = sourceRecord.launchedFromUid;
   3407             targetPackage = sourceRecord.launchedFromPackage;
   3408         }
   3409 
   3410         if (userId == UserHandle.USER_NULL) {
   3411             userId = UserHandle.getUserId(sourceRecord.app.uid);
   3412         }
   3413 
   3414         // TODO: Switch to user app stacks here.
   3415         try {
   3416             int ret = mStackSupervisor.startActivityMayWait(null, targetUid, targetPackage, intent,
   3417                     resolvedType, null, null, resultTo, resultWho, requestCode, startFlags, null,
   3418                     null, null, options, userId, null, null);
   3419             return ret;
   3420         } catch (SecurityException e) {
   3421             // XXX need to figure out how to propagate to original app.
   3422             // A SecurityException here is generally actually a fault of the original
   3423             // calling activity (such as a fairly granting permissions), so propagate it
   3424             // back to them.
   3425             /*
   3426             StringBuilder msg = new StringBuilder();
   3427             msg.append("While launching");
   3428             msg.append(intent.toString());
   3429             msg.append(": ");
   3430             msg.append(e.getMessage());
   3431             */
   3432             throw e;
   3433         }
   3434     }
   3435 
   3436     @Override
   3437     public final WaitResult startActivityAndWait(IApplicationThread caller, String callingPackage,
   3438             Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
   3439             int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) {
   3440         enforceNotIsolatedCaller("startActivityAndWait");
   3441         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
   3442                 false, ALLOW_FULL_ONLY, "startActivityAndWait", null);
   3443         WaitResult res = new WaitResult();
   3444         // TODO: Switch to user app stacks here.
   3445         mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType,
   3446                 null, null, resultTo, resultWho, requestCode, startFlags, profilerInfo, res, null,
   3447                 options, userId, null, null);
   3448         return res;
   3449     }
   3450 
   3451     @Override
   3452     public final int startActivityWithConfig(IApplicationThread caller, String callingPackage,
   3453             Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
   3454             int startFlags, Configuration config, Bundle options, int userId) {
   3455         enforceNotIsolatedCaller("startActivityWithConfig");
   3456         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
   3457                 false, ALLOW_FULL_ONLY, "startActivityWithConfig", null);
   3458         // TODO: Switch to user app stacks here.
   3459         int ret = mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent,
   3460                 resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
   3461                 null, null, config, options, userId, null, null);
   3462         return ret;
   3463     }
   3464 
   3465     @Override
   3466     public int startActivityIntentSender(IApplicationThread caller,
   3467             IntentSender intent, Intent fillInIntent, String resolvedType,
   3468             IBinder resultTo, String resultWho, int requestCode,
   3469             int flagsMask, int flagsValues, Bundle options) {
   3470         enforceNotIsolatedCaller("startActivityIntentSender");
   3471         // Refuse possible leaked file descriptors
   3472         if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
   3473             throw new IllegalArgumentException("File descriptors passed in Intent");
   3474         }
   3475 
   3476         IIntentSender sender = intent.getTarget();
   3477         if (!(sender instanceof PendingIntentRecord)) {
   3478             throw new IllegalArgumentException("Bad PendingIntent object");
   3479         }
   3480 
   3481         PendingIntentRecord pir = (PendingIntentRecord)sender;
   3482 
   3483         synchronized (this) {
   3484             // If this is coming from the currently resumed activity, it is
   3485             // effectively saying that app switches are allowed at this point.
   3486             final ActivityStack stack = getFocusedStack();
   3487             if (stack.mResumedActivity != null &&
   3488                     stack.mResumedActivity.info.applicationInfo.uid == Binder.getCallingUid()) {
   3489                 mAppSwitchesAllowedTime = 0;
   3490             }
   3491         }
   3492         int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null,
   3493                 resultTo, resultWho, requestCode, flagsMask, flagsValues, options, null);
   3494         return ret;
   3495     }
   3496 
   3497     @Override
   3498     public int startVoiceActivity(String callingPackage, int callingPid, int callingUid,
   3499             Intent intent, String resolvedType, IVoiceInteractionSession session,
   3500             IVoiceInteractor interactor, int startFlags, ProfilerInfo profilerInfo,
   3501             Bundle options, int userId) {
   3502         if (checkCallingPermission(Manifest.permission.BIND_VOICE_INTERACTION)
   3503                 != PackageManager.PERMISSION_GRANTED) {
   3504             String msg = "Permission Denial: startVoiceActivity() from pid="
   3505                     + Binder.getCallingPid()
   3506                     + ", uid=" + Binder.getCallingUid()
   3507                     + " requires " + android.Manifest.permission.BIND_VOICE_INTERACTION;
   3508             Slog.w(TAG, msg);
   3509             throw new SecurityException(msg);
   3510         }
   3511         if (session == null || interactor == null) {
   3512             throw new NullPointerException("null session or interactor");
   3513         }
   3514         userId = handleIncomingUser(callingPid, callingUid, userId,
   3515                 false, ALLOW_FULL_ONLY, "startVoiceActivity", null);
   3516         // TODO: Switch to user app stacks here.
   3517         return mStackSupervisor.startActivityMayWait(null, callingUid, callingPackage, intent,
   3518                 resolvedType, session, interactor, null, null, 0, startFlags, profilerInfo, null,
   3519                 null, options, userId, null, null);
   3520     }
   3521 
   3522     @Override
   3523     public boolean startNextMatchingActivity(IBinder callingActivity,
   3524             Intent intent, Bundle options) {
   3525         // Refuse possible leaked file descriptors
   3526         if (intent != null && intent.hasFileDescriptors() == true) {
   3527             throw new IllegalArgumentException("File descriptors passed in Intent");
   3528         }
   3529 
   3530         synchronized (this) {
   3531             final ActivityRecord r = ActivityRecord.isInStackLocked(callingActivity);
   3532             if (r == null) {
   3533                 ActivityOptions.abort(options);
   3534                 return false;
   3535             }
   3536             if (r.app == null || r.app.thread == null) {
   3537                 // The caller is not running...  d'oh!
   3538                 ActivityOptions.abort(options);
   3539                 return false;
   3540             }
   3541             intent = new Intent(intent);
   3542             // The caller is not allowed to change the data.
   3543             intent.setDataAndType(r.intent.getData(), r.intent.getType());
   3544             // And we are resetting to find the next component...
   3545             intent.setComponent(null);
   3546 
   3547             final boolean debug = ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
   3548 
   3549             ActivityInfo aInfo = null;
   3550             try {
   3551                 List<ResolveInfo> resolves =
   3552                     AppGlobals.getPackageManager().queryIntentActivities(
   3553                             intent, r.resolvedType,
   3554                             PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS,
   3555                             UserHandle.getCallingUserId());
   3556 
   3557                 // Look for the original activity in the list...
   3558                 final int N = resolves != null ? resolves.size() : 0;
   3559                 for (int i=0; i<N; i++) {
   3560                     ResolveInfo rInfo = resolves.get(i);
   3561                     if (rInfo.activityInfo.packageName.equals(r.packageName)
   3562                             && rInfo.activityInfo.name.equals(r.info.name)) {
   3563                         // We found the current one...  the next matching is
   3564                         // after it.
   3565                         i++;
   3566                         if (i<N) {
   3567                             aInfo = resolves.get(i).activityInfo;
   3568                         }
   3569                         if (debug) {
   3570                             Slog.v(TAG, "Next matching activity: found current " + r.packageName
   3571                                     + "/" + r.info.name);
   3572                             Slog.v(TAG, "Next matching activity: next is " + aInfo.packageName
   3573                                     + "/" + aInfo.name);
   3574                         }
   3575                         break;
   3576                     }
   3577                 }
   3578             } catch (RemoteException e) {
   3579             }
   3580 
   3581             if (aInfo == null) {
   3582                 // Nobody who is next!
   3583                 ActivityOptions.abort(options);
   3584                 if (debug) Slog.d(TAG, "Next matching activity: nothing found");
   3585                 return false;
   3586             }
   3587 
   3588             intent.setComponent(new ComponentName(
   3589                     aInfo.applicationInfo.packageName, aInfo.name));
   3590             intent.setFlags(intent.getFlags()&~(
   3591                     Intent.FLAG_ACTIVITY_FORWARD_RESULT|
   3592                     Intent.FLAG_ACTIVITY_CLEAR_TOP|
   3593                     Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
   3594                     Intent.FLAG_ACTIVITY_NEW_TASK));
   3595 
   3596             // Okay now we need to start the new activity, replacing the
   3597             // currently running activity.  This is a little tricky because
   3598             // we want to start the new one as if the current one is finished,
   3599             // but not finish the current one first so that there is no flicker.
   3600             // And thus...
   3601             final boolean wasFinishing = r.finishing;
   3602             r.finishing = true;
   3603 
   3604             // Propagate reply information over to the new activity.
   3605             final ActivityRecord resultTo = r.resultTo;
   3606             final String resultWho = r.resultWho;
   3607             final int requestCode = r.requestCode;
   3608             r.resultTo = null;
   3609             if (resultTo != null) {
   3610                 resultTo.removeResultsLocked(r, resultWho, requestCode);
   3611             }
   3612 
   3613             final long origId = Binder.clearCallingIdentity();
   3614             int res = mStackSupervisor.startActivityLocked(r.app.thread, intent,
   3615                     r.resolvedType, aInfo, null, null, resultTo != null ? resultTo.appToken : null,
   3616                     resultWho, requestCode, -1, r.launchedFromUid, r.launchedFromPackage,
   3617                     -1, r.launchedFromUid, 0, options, false, null, null, null);
   3618             Binder.restoreCallingIdentity(origId);
   3619 
   3620             r.finishing = wasFinishing;
   3621             if (res != ActivityManager.START_SUCCESS) {
   3622                 return false;
   3623             }
   3624             return true;
   3625         }
   3626     }
   3627 
   3628     @Override
   3629     public final int startActivityFromRecents(int taskId, Bundle options) {
   3630         if (checkCallingPermission(START_TASKS_FROM_RECENTS) != PackageManager.PERMISSION_GRANTED) {
   3631             String msg = "Permission Denial: startActivityFromRecents called without " +
   3632                     START_TASKS_FROM_RECENTS;
   3633             Slog.w(TAG, msg);
   3634             throw new SecurityException(msg);
   3635         }
   3636         return startActivityFromRecentsInner(taskId, options);
   3637     }
   3638 
   3639     final int startActivityFromRecentsInner(int taskId, Bundle options) {
   3640         final TaskRecord task;
   3641         final int callingUid;
   3642         final String callingPackage;
   3643         final Intent intent;
   3644         final int userId;
   3645         synchronized (this) {
   3646             task = recentTaskForIdLocked(taskId);
   3647             if (task == null) {
   3648                 throw new IllegalArgumentException("Task " + taskId + " not found.");
   3649             }
   3650             if (task.getRootActivity() != null) {
   3651                 moveTaskToFrontLocked(task.taskId, 0, null);
   3652                 return ActivityManager.START_TASK_TO_FRONT;
   3653             }
   3654             callingUid = task.mCallingUid;
   3655             callingPackage = task.mCallingPackage;
   3656             intent = task.intent;
   3657             intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
   3658             userId = task.userId;
   3659         }
   3660         return startActivityInPackage(callingUid, callingPackage, intent, null, null, null, 0, 0,
   3661                 options, userId, null, task);
   3662     }
   3663 
   3664     final int startActivityInPackage(int uid, String callingPackage,
   3665             Intent intent, String resolvedType, IBinder resultTo,
   3666             String resultWho, int requestCode, int startFlags, Bundle options, int userId,
   3667             IActivityContainer container, TaskRecord inTask) {
   3668 
   3669         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
   3670                 false, ALLOW_FULL_ONLY, "startActivityInPackage", null);
   3671 
   3672         // TODO: Switch to user app stacks here.
   3673         int ret = mStackSupervisor.startActivityMayWait(null, uid, callingPackage, intent,
   3674                 resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
   3675                 null, null, null, options, userId, container, inTask);
   3676         return ret;
   3677     }
   3678 
   3679     @Override
   3680     public final int startActivities(IApplicationThread caller, String callingPackage,
   3681             Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options,
   3682             int userId) {
   3683         enforceNotIsolatedCaller("startActivities");
   3684         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
   3685                 false, ALLOW_FULL_ONLY, "startActivity", null);
   3686         // TODO: Switch to user app stacks here.
   3687         int ret = mStackSupervisor.startActivities(caller, -1, callingPackage, intents,
   3688                 resolvedTypes, resultTo, options, userId);
   3689         return ret;
   3690     }
   3691 
   3692     final int startActivitiesInPackage(int uid, String callingPackage,
   3693             Intent[] intents, String[] resolvedTypes, IBinder resultTo,
   3694             Bundle options, int userId) {
   3695 
   3696         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
   3697                 false, ALLOW_FULL_ONLY, "startActivityInPackage", null);
   3698         // TODO: Switch to user app stacks here.
   3699         int ret = mStackSupervisor.startActivities(null, uid, callingPackage, intents, resolvedTypes,
   3700                 resultTo, options, userId);
   3701         return ret;
   3702     }
   3703 
   3704     //explicitly remove thd old information in mRecentTasks when removing existing user.
   3705     private void removeRecentTasksForUserLocked(int userId) {
   3706         if(userId <= 0) {
   3707             Slog.i(TAG, "Can't remove recent task on user " + userId);
   3708             return;
   3709         }
   3710 
   3711         for (int i = mRecentTasks.size() - 1; i >= 0; --i) {
   3712             TaskRecord tr = mRecentTasks.get(i);
   3713             if (tr.userId == userId) {
   3714                 if(DEBUG_TASKS) Slog.i(TAG, "remove RecentTask " + tr
   3715                         + " when finishing user" + userId);
   3716                 mRecentTasks.remove(i);
   3717                 tr.removedFromRecents();
   3718             }
   3719         }
   3720 
   3721         // Remove tasks from persistent storage.
   3722         notifyTaskPersisterLocked(null, true);
   3723     }
   3724 
   3725     // Sort by taskId
   3726     private Comparator<TaskRecord> mTaskRecordComparator = new Comparator<TaskRecord>() {
   3727         @Override
   3728         public int compare(TaskRecord lhs, TaskRecord rhs) {
   3729             return rhs.taskId - lhs.taskId;
   3730         }
   3731     };
   3732 
   3733     // Extract the affiliates of the chain containing mRecentTasks[start].
   3734     private int processNextAffiliateChainLocked(int start) {
   3735         final TaskRecord startTask = mRecentTasks.get(start);
   3736         final int affiliateId = startTask.mAffiliatedTaskId;
   3737 
   3738         // Quick identification of isolated tasks. I.e. those not launched behind.
   3739         if (startTask.taskId == affiliateId && startTask.mPrevAffiliate == null &&
   3740                 startTask.mNextAffiliate == null) {
   3741             // There is still a slim chance that there are other tasks that point to this task
   3742             // and that the chain is so messed up that this task no longer points to them but
   3743             // the gain of this optimization outweighs the risk.
   3744             startTask.inRecents = true;
   3745             return start + 1;
   3746         }
   3747 
   3748         // Remove all tasks that are affiliated to affiliateId and put them in mTmpRecents.
   3749         mTmpRecents.clear();
   3750         for (int i = mRecentTasks.size() - 1; i >= start; --i) {
   3751             final TaskRecord task = mRecentTasks.get(i);
   3752             if (task.mAffiliatedTaskId == affiliateId) {
   3753                 mRecentTasks.remove(i);
   3754                 mTmpRecents.add(task);
   3755             }
   3756         }
   3757 
   3758         // Sort them all by taskId. That is the order they were create in and that order will
   3759         // always be correct.
   3760         Collections.sort(mTmpRecents, mTaskRecordComparator);
   3761 
   3762         // Go through and fix up the linked list.
   3763         // The first one is the end of the chain and has no next.
   3764         final TaskRecord first = mTmpRecents.get(0);
   3765         first.inRecents = true;
   3766         if (first.mNextAffiliate != null) {
   3767             Slog.w(TAG, "Link error 1 first.next=" + first.mNextAffiliate);
   3768             first.setNextAffiliate(null);
   3769             notifyTaskPersisterLocked(first, false);
   3770         }
   3771         // Everything in the middle is doubly linked from next to prev.
   3772         final int tmpSize = mTmpRecents.size();
   3773         for (int i = 0; i < tmpSize - 1; ++i) {
   3774             final TaskRecord next = mTmpRecents.get(i);
   3775             final TaskRecord prev = mTmpRecents.get(i + 1);
   3776             if (next.mPrevAffiliate != prev) {
   3777                 Slog.w(TAG, "Link error 2 next=" + next + " prev=" + next.mPrevAffiliate +
   3778                         " setting prev=" + prev);
   3779                 next.setPrevAffiliate(prev);
   3780                 notifyTaskPersisterLocked(next, false);
   3781             }
   3782             if (prev.mNextAffiliate != next) {
   3783                 Slog.w(TAG, "Link error 3 prev=" + prev + " next=" + prev.mNextAffiliate +
   3784                         " setting next=" + next);
   3785                 prev.setNextAffiliate(next);
   3786                 notifyTaskPersisterLocked(prev, false);
   3787             }
   3788             prev.inRecents = true;
   3789         }
   3790         // The last one is the beginning of the list and has no prev.
   3791         final TaskRecord last = mTmpRecents.get(tmpSize - 1);
   3792         if (last.mPrevAffiliate != null) {
   3793             Slog.w(TAG, "Link error 4 last.prev=" + last.mPrevAffiliate);
   3794             last.setPrevAffiliate(null);
   3795             notifyTaskPersisterLocked(last, false);
   3796         }
   3797 
   3798         // Insert the group back into mRecentTasks at start.
   3799         mRecentTasks.addAll(start, mTmpRecents);
   3800 
   3801         // Let the caller know where we left off.
   3802         return start + tmpSize;
   3803     }
   3804 
   3805     /**
   3806      * Update the recent tasks lists: make sure tasks should still be here (their
   3807      * applications / activities still exist), update their availability, fixup ordering
   3808      * of affiliations.
   3809      */
   3810     void cleanupRecentTasksLocked(int userId) {
   3811         if (mRecentTasks == null) {
   3812             // Happens when called from the packagemanager broadcast before boot.
   3813             return;
   3814         }
   3815 
   3816         final HashMap<ComponentName, ActivityInfo> availActCache = new HashMap<>();
   3817         final HashMap<String, ApplicationInfo> availAppCache = new HashMap<>();
   3818         final IPackageManager pm = AppGlobals.getPackageManager();
   3819         final ActivityInfo dummyAct = new ActivityInfo();
   3820         final ApplicationInfo dummyApp = new ApplicationInfo();
   3821 
   3822         int N = mRecentTasks.size();
   3823 
   3824         int[] users = userId == UserHandle.USER_ALL
   3825                 ? getUsersLocked() : new int[] { userId };
   3826         for (int user : users) {
   3827             for (int i = 0; i < N; i++) {
   3828                 TaskRecord task = mRecentTasks.get(i);
   3829                 if (task.userId != user) {
   3830                     // Only look at tasks for the user ID of interest.
   3831                     continue;
   3832                 }
   3833                 if (task.autoRemoveRecents && task.getTopActivity() == null) {
   3834                     // This situation is broken, and we should just get rid of it now.
   3835                     mRecentTasks.remove(i);
   3836                     task.removedFromRecents();
   3837                     i--;
   3838                     N--;
   3839                     Slog.w(TAG, "Removing auto-remove without activity: " + task);
   3840                     continue;
   3841                 }
   3842                 // Check whether this activity is currently available.
   3843                 if (task.realActivity != null) {
   3844                     ActivityInfo ai = availActCache.get(task.realActivity);
   3845                     if (ai == null) {
   3846                         try {
   3847                             ai = pm.getActivityInfo(task.realActivity,
   3848                                     PackageManager.GET_UNINSTALLED_PACKAGES
   3849                                     | PackageManager.GET_DISABLED_COMPONENTS, user);
   3850                         } catch (RemoteException e) {
   3851                             // Will never happen.
   3852                             continue;
   3853                         }
   3854                         if (ai == null) {
   3855                             ai = dummyAct;
   3856                         }
   3857                         availActCache.put(task.realActivity, ai);
   3858                     }
   3859                     if (ai == dummyAct) {
   3860                         // This could be either because the activity no longer exists, or the
   3861                         // app is temporarily gone.  For the former we want to remove the recents
   3862                         // entry; for the latter we want to mark it as unavailable.
   3863                         ApplicationInfo app = availAppCache.get(task.realActivity.getPackageName());
   3864                         if (app == null) {
   3865                             try {
   3866                                 app = pm.getApplicationInfo(task.realActivity.getPackageName(),
   3867                                         PackageManager.GET_UNINSTALLED_PACKAGES
   3868                                         | PackageManager.GET_DISABLED_COMPONENTS, user);
   3869                             } catch (RemoteException e) {
   3870                                 // Will never happen.
   3871                                 continue;
   3872                             }
   3873                             if (app == null) {
   3874                                 app = dummyApp;
   3875                             }
   3876                             availAppCache.put(task.realActivity.getPackageName(), app);
   3877                         }
   3878                         if (app == dummyApp || (app.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
   3879                             // Doesn't exist any more!  Good-bye.
   3880                             mRecentTasks.remove(i);
   3881                             task.removedFromRecents();
   3882                             i--;
   3883                             N--;
   3884                             Slog.w(TAG, "Removing no longer valid recent: " + task);
   3885                             continue;
   3886                         } else {
   3887                             // Otherwise just not available for now.
   3888                             if (task.isAvailable) {
   3889                                 if (DEBUG_RECENTS) Slog.d(TAG, "Making recent unavailable: "
   3890                                         + task);
   3891                             }
   3892                             task.isAvailable = false;
   3893                         }
   3894                     } else {
   3895                         if (!ai.enabled || !ai.applicationInfo.enabled
   3896                                 || (ai.applicationInfo.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
   3897                             if (task.isAvailable) {
   3898                                 if (DEBUG_RECENTS) Slog.d(TAG, "Making recent unavailable: "
   3899                                         + task + " (enabled=" + ai.enabled + "/"
   3900                                         + ai.applicationInfo.enabled +  " flags="
   3901                                         + Integer.toHexString(ai.applicationInfo.flags) + ")");
   3902                             }
   3903                             task.isAvailable = false;
   3904                         } else {
   3905                             if (!task.isAvailable) {
   3906                                 if (DEBUG_RECENTS) Slog.d(TAG, "Making recent available: "
   3907                                         + task);
   3908                             }
   3909                             task.isAvailable = true;
   3910                         }
   3911                     }
   3912                 }
   3913             }
   3914         }
   3915 
   3916         // Verify the affiliate chain for each task.
   3917         for (int i = 0; i < N; i = processNextAffiliateChainLocked(i)) {
   3918         }
   3919 
   3920         mTmpRecents.clear();
   3921         // mRecentTasks is now in sorted, affiliated order.
   3922     }
   3923 
   3924     private final boolean moveAffiliatedTasksToFront(TaskRecord task, int taskIndex) {
   3925         int N = mRecentTasks.size();
   3926         TaskRecord top = task;
   3927         int topIndex = taskIndex;
   3928         while (top.mNextAffiliate != null && topIndex > 0) {
   3929             top = top.mNextAffiliate;
   3930             topIndex--;
   3931         }
   3932         if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: adding affilliates starting at "
   3933                 + topIndex + " from intial " + taskIndex);
   3934         // Find the end of the chain, doing a sanity check along the way.
   3935         boolean sane = top.mAffiliatedTaskId == task.mAffiliatedTaskId;
   3936         int endIndex = topIndex;
   3937         TaskRecord prev = top;
   3938         while (endIndex < N) {
   3939             TaskRecord cur = mRecentTasks.get(endIndex);
   3940             if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: looking at next chain @"
   3941                     + endIndex + " " + cur);
   3942             if (cur == top) {
   3943                 // Verify start of the chain.
   3944                 if (cur.mNextAffiliate != null || cur.mNextAffiliateTaskId != INVALID_TASK_ID) {
   3945                     Slog.wtf(TAG, "Bad chain @" + endIndex
   3946                             + ": first task has next affiliate: " + prev);
   3947                     sane = false;
   3948                     break;
   3949                 }
   3950             } else {
   3951                 // Verify middle of the chain's next points back to the one before.
   3952                 if (cur.mNextAffiliate != prev
   3953                         || cur.mNextAffiliateTaskId != prev.taskId) {
   3954                     Slog.wtf(TAG, "Bad chain @" + endIndex
   3955                             + ": middle task " + cur + " @" + endIndex
   3956                             + " has bad next affiliate "
   3957                             + cur.mNextAffiliate + " id " + cur.mNextAffiliateTaskId
   3958                             + ", expected " + prev);
   3959                     sane = false;
   3960                     break;
   3961                 }
   3962             }
   3963             if (cur.mPrevAffiliateTaskId == INVALID_TASK_ID) {
   3964                 // Chain ends here.
   3965                 if (cur.mPrevAffiliate != null) {
   3966                     Slog.wtf(TAG, "Bad chain @" + endIndex
   3967                             + ": last task " + cur + " has previous affiliate "
   3968                             + cur.mPrevAffiliate);
   3969                     sane = false;
   3970                 }
   3971                 if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: end of chain @" + endIndex);
   3972                 break;
   3973             } else {
   3974                 // Verify middle of the chain's prev points to a valid item.
   3975                 if (cur.mPrevAffiliate == null) {
   3976                     Slog.wtf(TAG, "Bad chain @" + endIndex
   3977                             + ": task " + cur + " has previous affiliate "
   3978                             + cur.mPrevAffiliate + " but should be id "
   3979                             + cur.mPrevAffiliate);
   3980                     sane = false;
   3981                     break;
   3982                 }
   3983             }
   3984             if (cur.mAffiliatedTaskId != task.mAffiliatedTaskId) {
   3985                 Slog.wtf(TAG, "Bad chain @" + endIndex
   3986                         + ": task " + cur + " has affiliated id "
   3987                         + cur.mAffiliatedTaskId + " but should be "
   3988                         + task.mAffiliatedTaskId);
   3989                 sane = false;
   3990                 break;
   3991             }
   3992             prev = cur;
   3993             endIndex++;
   3994             if (endIndex >= N) {
   3995                 Slog.wtf(TAG, "Bad chain ran off index " + endIndex
   3996                         + ": last task " + prev);
   3997                 sane = false;
   3998                 break;
   3999             }
   4000         }
   4001         if (sane) {
   4002             if (endIndex < taskIndex) {
   4003                 Slog.wtf(TAG, "Bad chain @" + endIndex
   4004                         + ": did not extend to task " + task + " @" + taskIndex);
   4005                 sane = false;
   4006             }
   4007         }
   4008         if (sane) {
   4009             // All looks good, we can just move all of the affiliated tasks
   4010             // to the top.
   4011             for (int i=topIndex; i<=endIndex; i++) {
   4012                 if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: moving affiliated " + task
   4013                         + " from " + i + " to " + (i-topIndex));
   4014                 TaskRecord cur = mRecentTasks.remove(i);
   4015                 mRecentTasks.add(i-topIndex, cur);
   4016             }
   4017             if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: done moving tasks  " +  topIndex
   4018                     + " to " + endIndex);
   4019             return true;
   4020         }
   4021 
   4022         // Whoops, couldn't do it.
   4023         return false;
   4024     }
   4025 
   4026     final void addRecentTaskLocked(TaskRecord task) {
   4027         final boolean isAffiliated = task.mAffiliatedTaskId != task.taskId
   4028                 || task.mNextAffiliateTaskId != INVALID_TASK_ID
   4029                 || task.mPrevAffiliateTaskId != INVALID_TASK_ID;
   4030 
   4031         int N = mRecentTasks.size();
   4032         // Quick case: check if the top-most recent task is the same.
   4033         if (!isAffiliated && N > 0 && mRecentTasks.get(0) == task) {
   4034             if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: already at top: " + task);
   4035             return;
   4036         }
   4037         // Another quick case: check if this is part of a set of affiliated
   4038         // tasks that are at the top.
   4039         if (isAffiliated && N > 0 && task.inRecents
   4040                 && task.mAffiliatedTaskId == mRecentTasks.get(0).mAffiliatedTaskId) {
   4041             if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: affiliated " + mRecentTasks.get(0)
   4042                     + " at top when adding " + task);
   4043             return;
   4044         }
   4045         // Another quick case: never add voice sessions.
   4046         if (task.voiceSession != null) {
   4047             if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: not adding voice interaction " + task);
   4048             return;
   4049         }
   4050 
   4051         boolean needAffiliationFix = false;
   4052 
   4053         // Slightly less quick case: the task is already in recents, so all we need
   4054         // to do is move it.
   4055         if (task.inRecents) {
   4056             int taskIndex = mRecentTasks.indexOf(task);
   4057             if (taskIndex >= 0) {
   4058                 if (!isAffiliated) {
   4059                     // Simple case: this is not an affiliated task, so we just move it to the front.
   4060                     mRecentTasks.remove(taskIndex);
   4061                     mRecentTasks.add(0, task);
   4062                     notifyTaskPersisterLocked(task, false);
   4063                     if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: moving to top " + task
   4064                             + " from " + taskIndex);
   4065                     return;
   4066                 } else {
   4067                     // More complicated: need to keep all affiliated tasks together.
   4068                     if (moveAffiliatedTasksToFront(task, taskIndex)) {
   4069                         // All went well.
   4070                         return;
   4071                     }
   4072 
   4073                     // Uh oh...  something bad in the affiliation chain, try to rebuild
   4074                     // everything and then go through our general path of adding a new task.
   4075                     needAffiliationFix = true;
   4076                 }
   4077             } else {
   4078                 Slog.wtf(TAG, "Task with inRecent not in recents: " + task);
   4079                 needAffiliationFix = true;
   4080             }
   4081         }
   4082 
   4083         if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: trimming tasks for " + task);
   4084         trimRecentsForTaskLocked(task, true);
   4085 
   4086         N = mRecentTasks.size();
   4087         while (N >= ActivityManager.getMaxRecentTasksStatic()) {
   4088             final TaskRecord tr = mRecentTasks.remove(N - 1);
   4089             tr.removedFromRecents();
   4090             N--;
   4091         }
   4092         task.inRecents = true;
   4093         if (!isAffiliated || needAffiliationFix) {
   4094             // If this is a simple non-affiliated task, or we had some failure trying to
   4095             // handle it as part of an affilated task, then just place it at the top.
   4096             mRecentTasks.add(0, task);
   4097         } else if (isAffiliated) {
   4098             // If this is a new affiliated task, then move all of the affiliated tasks
   4099             // to the front and insert this new one.
   4100             TaskRecord other = task.mNextAffiliate;
   4101             if (other == null) {
   4102                 other = task.mPrevAffiliate;
   4103             }
   4104             if (other != null) {
   4105                 int otherIndex = mRecentTasks.indexOf(other);
   4106                 if (otherIndex >= 0) {
   4107                     // Insert new task at appropriate location.
   4108                     int taskIndex;
   4109                     if (other == task.mNextAffiliate) {
   4110                         // We found the index of our next affiliation, which is who is
   4111                         // before us in the list, so add after that point.
   4112                         taskIndex = otherIndex+1;
   4113                     } else {
   4114                         // We found the index of our previous affiliation, which is who is
   4115                         // after us in the list, so add at their position.
   4116                         taskIndex = otherIndex;
   4117                     }
   4118                     if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: new affiliated task added at "
   4119                             + taskIndex + ": " + task);
   4120                     mRecentTasks.add(taskIndex, task);
   4121 
   4122                     // Now move everything to the front.
   4123                     if (moveAffiliatedTasksToFront(task, taskIndex)) {
   4124                         // All went well.
   4125                         return;
   4126                     }
   4127 
   4128                     // Uh oh...  something bad in the affiliation chain, try to rebuild
   4129                     // everything and then go through our general path of adding a new task.
   4130                     needAffiliationFix = true;
   4131                 } else {
   4132                     if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: couldn't find other affiliation "
   4133                             + other);
   4134                     needAffiliationFix = true;
   4135                 }
   4136             } else {
   4137                 if (DEBUG_RECENTS) Slog.d(TAG,
   4138                         "addRecent: adding affiliated task without next/prev:" + task);
   4139                 needAffiliationFix = true;
   4140             }
   4141         }
   4142         if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: adding " + task);
   4143 
   4144         if (needAffiliationFix) {
   4145             if (DEBUG_RECENTS) Slog.d(TAG, "addRecent: regrouping affiliations");
   4146             cleanupRecentTasksLocked(task.userId);
   4147         }
   4148     }
   4149 
   4150     /**
   4151      * If needed, remove oldest existing entries in recents that are for the same kind
   4152      * of task as the given one.
   4153      */
   4154     int trimRecentsForTaskLocked(TaskRecord task, boolean doTrim) {
   4155         int N = mRecentTasks.size();
   4156         final Intent intent = task.intent;
   4157         final boolean document = intent != null && intent.isDocument();
   4158 
   4159         int maxRecents = task.maxRecents - 1;
   4160         for (int i=0; i<N; i++) {
   4161             final TaskRecord tr = mRecentTasks.get(i);
   4162             if (task != tr) {
   4163                 if (task.userId != tr.userId) {
   4164                     continue;
   4165                 }
   4166                 if (i > MAX_RECENT_BITMAPS) {
   4167                     tr.freeLastThumbnail();
   4168                 }
   4169                 final Intent trIntent = tr.intent;
   4170                 if ((task.affinity == null || !task.affinity.equals(tr.affinity)) &&
   4171                     (intent == null || !intent.filterEquals(trIntent))) {
   4172                     continue;
   4173                 }
   4174                 final boolean trIsDocument = trIntent != null && trIntent.isDocument();
   4175                 if (document && trIsDocument) {
   4176                     // These are the same document activity (not necessarily the same doc).
   4177                     if (maxRecents > 0) {
   4178                         --maxRecents;
   4179                         continue;
   4180                     }
   4181                     // Hit the maximum number of documents for this task. Fall through
   4182                     // and remove this document from recents.
   4183                 } else if (document || trIsDocument) {
   4184                     // Only one of these is a document. Not the droid we're looking for.
   4185                     continue;
   4186                 }
   4187             }
   4188 
   4189             if (!doTrim) {
   4190                 // If the caller is not actually asking for a trim, just tell them we reached
   4191                 // a point where the trim would happen.
   4192                 return i;
   4193             }
   4194 
   4195             // Either task and tr are the same or, their affinities match or their intents match
   4196             // and neither of them is a document, or they are documents using the same activity
   4197             // and their maxRecents has been reached.
   4198             tr.disposeThumbnail();
   4199             mRecentTasks.remove(i);
   4200             if (task != tr) {
   4201                 tr.removedFromRecents();
   4202             }
   4203             i--;
   4204             N--;
   4205             if (task.intent == null) {
   4206                 // If the new recent task we are adding is not fully
   4207                 // specified, then replace it with the existing recent task.
   4208                 task = tr;
   4209             }
   4210             notifyTaskPersisterLocked(tr, false);
   4211         }
   4212 
   4213         return -1;
   4214     }
   4215 
   4216     @Override
   4217     public void reportActivityFullyDrawn(IBinder token) {
   4218         synchronized (this) {
   4219             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   4220             if (r == null) {
   4221                 return;
   4222             }
   4223             r.reportFullyDrawnLocked();
   4224         }
   4225     }
   4226 
   4227     @Override
   4228     public void setRequestedOrientation(IBinder token, int requestedOrientation) {
   4229         synchronized (this) {
   4230             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   4231             if (r == null) {
   4232                 return;
   4233             }
   4234             final long origId = Binder.clearCallingIdentity();
   4235             mWindowManager.setAppOrientation(r.appToken, requestedOrientation);
   4236             Configuration config = mWindowManager.updateOrientationFromAppTokens(
   4237                     mConfiguration, r.mayFreezeScreenLocked(r.app) ? r.appToken : null);
   4238             if (config != null) {
   4239                 r.frozenBeforeDestroy = true;
   4240                 if (!updateConfigurationLocked(config, r, false, false)) {
   4241                     mStackSupervisor.resumeTopActivitiesLocked();
   4242                 }
   4243             }
   4244             Binder.restoreCallingIdentity(origId);
   4245         }
   4246     }
   4247 
   4248     @Override
   4249     public int getRequestedOrientation(IBinder token) {
   4250         synchronized (this) {
   4251             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   4252             if (r == null) {
   4253                 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
   4254             }
   4255             return mWindowManager.getAppOrientation(r.appToken);
   4256         }
   4257     }
   4258 
   4259     /**
   4260      * This is the internal entry point for handling Activity.finish().
   4261      *
   4262      * @param token The Binder token referencing the Activity we want to finish.
   4263      * @param resultCode Result code, if any, from this Activity.
   4264      * @param resultData Result data (Intent), if any, from this Activity.
   4265      * @param finishTask Whether to finish the task associated with this Activity.  Only applies to
   4266      *            the root Activity in the task.
   4267      *
   4268      * @return Returns true if the activity successfully finished, or false if it is still running.
   4269      */
   4270     @Override
   4271     public final boolean finishActivity(IBinder token, int resultCode, Intent resultData,
   4272             boolean finishTask) {
   4273         // Refuse possible leaked file descriptors
   4274         if (resultData != null && resultData.hasFileDescriptors() == true) {
   4275             throw new IllegalArgumentException("File descriptors passed in Intent");
   4276         }
   4277 
   4278         synchronized(this) {
   4279             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   4280             if (r == null) {
   4281                 return true;
   4282             }
   4283             // Keep track of the root activity of the task before we finish it
   4284             TaskRecord tr = r.task;
   4285             ActivityRecord rootR = tr.getRootActivity();
   4286             if (rootR == null) {
   4287                 Slog.w(TAG, "Finishing task with all activities already finished");
   4288             }
   4289             // Do not allow task to finish in Lock Task mode.
   4290             if (tr == mStackSupervisor.mLockTaskModeTask) {
   4291                 if (rootR == r) {
   4292                     Slog.i(TAG, "Not finishing task in lock task mode");
   4293                     mStackSupervisor.showLockTaskToast();
   4294                     return false;
   4295                 }
   4296             }
   4297             if (mController != null) {
   4298                 // Find the first activity that is not finishing.
   4299                 ActivityRecord next = r.task.stack.topRunningActivityLocked(token, 0);
   4300                 if (next != null) {
   4301                     // ask watcher if this is allowed
   4302                     boolean resumeOK = true;
   4303                     try {
   4304                         resumeOK = mController.activityResuming(next.packageName);
   4305                     } catch (RemoteException e) {
   4306                         mController = null;
   4307                         Watchdog.getInstance().setActivityController(null);
   4308                     }
   4309 
   4310                     if (!resumeOK) {
   4311                         Slog.i(TAG, "Not finishing activity because controller resumed");
   4312                         return false;
   4313                     }
   4314                 }
   4315             }
   4316             final long origId = Binder.clearCallingIdentity();
   4317             try {
   4318                 boolean res;
   4319                 if (finishTask && r == rootR) {
   4320                     // If requested, remove the task that is associated to this activity only if it
   4321                     // was the root activity in the task. The result code and data is ignored
   4322                     // because we don't support returning them across task boundaries.
   4323                     res = removeTaskByIdLocked(tr.taskId, false);
   4324                     if (!res) {
   4325                         Slog.i(TAG, "Removing task failed to finish activity");
   4326                     }
   4327                 } else {
   4328                     res = tr.stack.requestFinishActivityLocked(token, resultCode,
   4329                             resultData, "app-request", true);
   4330                     if (!res) {
   4331                         Slog.i(TAG, "Failed to finish by app-request");
   4332                     }
   4333                 }
   4334                 return res;
   4335             } finally {
   4336                 Binder.restoreCallingIdentity(origId);
   4337             }
   4338         }
   4339     }
   4340 
   4341     @Override
   4342     public final void finishHeavyWeightApp() {
   4343         if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
   4344                 != PackageManager.PERMISSION_GRANTED) {
   4345             String msg = "Permission Denial: finishHeavyWeightApp() from pid="
   4346                     + Binder.getCallingPid()
   4347                     + ", uid=" + Binder.getCallingUid()
   4348                     + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
   4349             Slog.w(TAG, msg);
   4350             throw new SecurityException(msg);
   4351         }
   4352 
   4353         synchronized(this) {
   4354             if (mHeavyWeightProcess == null) {
   4355                 return;
   4356             }
   4357 
   4358             ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(
   4359                     mHeavyWeightProcess.activities);
   4360             for (int i=0; i<activities.size(); i++) {
   4361                 ActivityRecord r = activities.get(i);
   4362                 if (!r.finishing) {
   4363                     r.task.stack.finishActivityLocked(r, Activity.RESULT_CANCELED,
   4364                             null, "finish-heavy", true);
   4365                 }
   4366             }
   4367 
   4368             mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
   4369                     mHeavyWeightProcess.userId, 0));
   4370             mHeavyWeightProcess = null;
   4371         }
   4372     }
   4373 
   4374     @Override
   4375     public void crashApplication(int uid, int initialPid, String packageName,
   4376             String message) {
   4377         if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
   4378                 != PackageManager.PERMISSION_GRANTED) {
   4379             String msg = "Permission Denial: crashApplication() from pid="
   4380                     + Binder.getCallingPid()
   4381                     + ", uid=" + Binder.getCallingUid()
   4382                     + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
   4383             Slog.w(TAG, msg);
   4384             throw new SecurityException(msg);
   4385         }
   4386 
   4387         synchronized(this) {
   4388             ProcessRecord proc = null;
   4389 
   4390             // Figure out which process to kill.  We don't trust that initialPid
   4391             // still has any relation to current pids, so must scan through the
   4392             // list.
   4393             synchronized (mPidsSelfLocked) {
   4394                 for (int i=0; i<mPidsSelfLocked.size(); i++) {
   4395                     ProcessRecord p = mPidsSelfLocked.valueAt(i);
   4396                     if (p.uid != uid) {
   4397                         continue;
   4398                     }
   4399                     if (p.pid == initialPid) {
   4400                         proc = p;
   4401                         break;
   4402                     }
   4403                     if (p.pkgList.containsKey(packageName)) {
   4404                         proc = p;
   4405                     }
   4406                 }
   4407             }
   4408 
   4409             if (proc == null) {
   4410                 Slog.w(TAG, "crashApplication: nothing for uid=" + uid
   4411                         + " initialPid=" + initialPid
   4412                         + " packageName=" + packageName);
   4413                 return;
   4414             }
   4415 
   4416             if (proc.thread != null) {
   4417                 if (proc.pid == Process.myPid()) {
   4418                     Log.w(TAG, "crashApplication: trying to crash self!");
   4419                     return;
   4420                 }
   4421                 long ident = Binder.clearCallingIdentity();
   4422                 try {
   4423                     proc.thread.scheduleCrash(message);
   4424                 } catch (RemoteException e) {
   4425                 }
   4426                 Binder.restoreCallingIdentity(ident);
   4427             }
   4428         }
   4429     }
   4430 
   4431     @Override
   4432     public final void finishSubActivity(IBinder token, String resultWho,
   4433             int requestCode) {
   4434         synchronized(this) {
   4435             final long origId = Binder.clearCallingIdentity();
   4436             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   4437             if (r != null) {
   4438                 r.task.stack.finishSubActivityLocked(r, resultWho, requestCode);
   4439             }
   4440             Binder.restoreCallingIdentity(origId);
   4441         }
   4442     }
   4443 
   4444     @Override
   4445     public boolean finishActivityAffinity(IBinder token) {
   4446         synchronized(this) {
   4447             final long origId = Binder.clearCallingIdentity();
   4448             try {
   4449                 ActivityRecord r = ActivityRecord.isInStackLocked(token);
   4450 
   4451                 ActivityRecord rootR = r.task.getRootActivity();
   4452                 // Do not allow task to finish in Lock Task mode.
   4453                 if (r.task == mStackSupervisor.mLockTaskModeTask) {
   4454                     if (rootR == r) {
   4455                         mStackSupervisor.showLockTaskToast();
   4456                         return false;
   4457                     }
   4458                 }
   4459                 boolean res = false;
   4460                 if (r != null) {
   4461                     res = r.task.stack.finishActivityAffinityLocked(r);
   4462                 }
   4463                 return res;
   4464             } finally {
   4465                 Binder.restoreCallingIdentity(origId);
   4466             }
   4467         }
   4468     }
   4469 
   4470     @Override
   4471     public void finishVoiceTask(IVoiceInteractionSession session) {
   4472         synchronized(this) {
   4473             final long origId = Binder.clearCallingIdentity();
   4474             try {
   4475                 mStackSupervisor.finishVoiceTask(session);
   4476             } finally {
   4477                 Binder.restoreCallingIdentity(origId);
   4478             }
   4479         }
   4480 
   4481     }
   4482 
   4483     @Override
   4484     public boolean releaseActivityInstance(IBinder token) {
   4485         synchronized(this) {
   4486             final long origId = Binder.clearCallingIdentity();
   4487             try {
   4488                 ActivityRecord r = ActivityRecord.isInStackLocked(token);
   4489                 if (r.task == null || r.task.stack == null) {
   4490                     return false;
   4491                 }
   4492                 return r.task.stack.safelyDestroyActivityLocked(r, "app-req");
   4493             } finally {
   4494                 Binder.restoreCallingIdentity(origId);
   4495             }
   4496         }
   4497     }
   4498 
   4499     @Override
   4500     public void releaseSomeActivities(IApplicationThread appInt) {
   4501         synchronized(this) {
   4502             final long origId = Binder.clearCallingIdentity();
   4503             try {
   4504                 ProcessRecord app = getRecordForAppLocked(appInt);
   4505                 mStackSupervisor.releaseSomeActivitiesLocked(app, "low-mem");
   4506             } finally {
   4507                 Binder.restoreCallingIdentity(origId);
   4508             }
   4509         }
   4510     }
   4511 
   4512     @Override
   4513     public boolean willActivityBeVisible(IBinder token) {
   4514         synchronized(this) {
   4515             ActivityStack stack = ActivityRecord.getStackLocked(token);
   4516             if (stack != null) {
   4517                 return stack.willActivityBeVisibleLocked(token);
   4518             }
   4519             return false;
   4520         }
   4521     }
   4522 
   4523     @Override
   4524     public void overridePendingTransition(IBinder token, String packageName,
   4525             int enterAnim, int exitAnim) {
   4526         synchronized(this) {
   4527             ActivityRecord self = ActivityRecord.isInStackLocked(token);
   4528             if (self == null) {
   4529                 return;
   4530             }
   4531 
   4532             final long origId = Binder.clearCallingIdentity();
   4533 
   4534             if (self.state == ActivityState.RESUMED
   4535                     || self.state == ActivityState.PAUSING) {
   4536                 mWindowManager.overridePendingAppTransition(packageName,
   4537                         enterAnim, exitAnim, null);
   4538             }
   4539 
   4540             Binder.restoreCallingIdentity(origId);
   4541         }
   4542     }
   4543 
   4544     /**
   4545      * Main function for removing an existing process from the activity manager
   4546      * as a result of that process going away.  Clears out all connections
   4547      * to the process.
   4548      */
   4549     private final void handleAppDiedLocked(ProcessRecord app,
   4550             boolean restarting, boolean allowRestart) {
   4551         int pid = app.pid;
   4552         boolean kept = cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1);
   4553         if (!kept && !restarting) {
   4554             removeLruProcessLocked(app);
   4555             if (pid > 0) {
   4556                 ProcessList.remove(pid);
   4557             }
   4558         }
   4559 
   4560         if (mProfileProc == app) {
   4561             clearProfilerLocked();
   4562         }
   4563 
   4564         // Remove this application's activities from active lists.
   4565         boolean hasVisibleActivities = mStackSupervisor.handleAppDiedLocked(app);
   4566 
   4567         app.activities.clear();
   4568 
   4569         if (app.instrumentationClass != null) {
   4570             Slog.w(TAG, "Crash of app " + app.processName
   4571                   + " running instrumentation " + app.instrumentationClass);
   4572             Bundle info = new Bundle();
   4573             info.putString("shortMsg", "Process crashed.");
   4574             finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
   4575         }
   4576 
   4577         if (!restarting) {
   4578             if (!mStackSupervisor.resumeTopActivitiesLocked()) {
   4579                 // If there was nothing to resume, and we are not already
   4580                 // restarting this process, but there is a visible activity that
   4581                 // is hosted by the process...  then make sure all visible
   4582                 // activities are running, taking care of restarting this
   4583                 // process.
   4584                 if (hasVisibleActivities) {
   4585                     mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
   4586                 }
   4587             }
   4588         }
   4589     }
   4590 
   4591     private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
   4592         IBinder threadBinder = thread.asBinder();
   4593         // Find the application record.
   4594         for (int i=mLruProcesses.size()-1; i>=0; i--) {
   4595             ProcessRecord rec = mLruProcesses.get(i);
   4596             if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
   4597                 return i;
   4598             }
   4599         }
   4600         return -1;
   4601     }
   4602 
   4603     final ProcessRecord getRecordForAppLocked(
   4604             IApplicationThread thread) {
   4605         if (thread == null) {
   4606             return null;
   4607         }
   4608 
   4609         int appIndex = getLRURecordIndexForAppLocked(thread);
   4610         return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
   4611     }
   4612 
   4613     final void doLowMemReportIfNeededLocked(ProcessRecord dyingProc) {
   4614         // If there are no longer any background processes running,
   4615         // and the app that died was not running instrumentation,
   4616         // then tell everyone we are now low on memory.
   4617         boolean haveBg = false;
   4618         for (int i=mLruProcesses.size()-1; i>=0; i--) {
   4619             ProcessRecord rec = mLruProcesses.get(i);
   4620             if (rec.thread != null
   4621                     && rec.setProcState >= ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) {
   4622                 haveBg = true;
   4623                 break;
   4624             }
   4625         }
   4626 
   4627         if (!haveBg) {
   4628             boolean doReport = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
   4629             if (doReport) {
   4630                 long now = SystemClock.uptimeMillis();
   4631                 if (now < (mLastMemUsageReportTime+5*60*1000)) {
   4632                     doReport = false;
   4633                 } else {
   4634                     mLastMemUsageReportTime = now;
   4635                 }
   4636             }
   4637             final ArrayList<ProcessMemInfo> memInfos
   4638                     = doReport ? new ArrayList<ProcessMemInfo>(mLruProcesses.size()) : null;
   4639             EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
   4640             long now = SystemClock.uptimeMillis();
   4641             for (int i=mLruProcesses.size()-1; i>=0; i--) {
   4642                 ProcessRecord rec = mLruProcesses.get(i);
   4643                 if (rec == dyingProc || rec.thread == null) {
   4644                     continue;
   4645                 }
   4646                 if (doReport) {
   4647                     memInfos.add(new ProcessMemInfo(rec.processName, rec.pid, rec.setAdj,
   4648                             rec.setProcState, rec.adjType, rec.makeAdjReason()));
   4649                 }
   4650                 if ((rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
   4651                     // The low memory report is overriding any current
   4652                     // state for a GC request.  Make sure to do
   4653                     // heavy/important/visible/foreground processes first.
   4654                     if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
   4655                         rec.lastRequestedGc = 0;
   4656                     } else {
   4657                         rec.lastRequestedGc = rec.lastLowMemory;
   4658                     }
   4659                     rec.reportLowMemory = true;
   4660                     rec.lastLowMemory = now;
   4661                     mProcessesToGc.remove(rec);
   4662                     addProcessToGcListLocked(rec);
   4663                 }
   4664             }
   4665             if (doReport) {
   4666                 Message msg = mHandler.obtainMessage(REPORT_MEM_USAGE_MSG, memInfos);
   4667                 mHandler.sendMessage(msg);
   4668             }
   4669             scheduleAppGcsLocked();
   4670         }
   4671     }
   4672 
   4673     final void appDiedLocked(ProcessRecord app) {
   4674        appDiedLocked(app, app.pid, app.thread);
   4675     }
   4676 
   4677     final void appDiedLocked(ProcessRecord app, int pid, IApplicationThread thread) {
   4678         // First check if this ProcessRecord is actually active for the pid.
   4679         synchronized (mPidsSelfLocked) {
   4680             ProcessRecord curProc = mPidsSelfLocked.get(pid);
   4681             if (curProc != app) {
   4682                 Slog.w(TAG, "Spurious death for " + app + ", curProc for " + pid + ": " + curProc);
   4683                 return;
   4684             }
   4685         }
   4686 
   4687         BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
   4688         synchronized (stats) {
   4689             stats.noteProcessDiedLocked(app.info.uid, pid);
   4690         }
   4691 
   4692         if (!app.killed) {
   4693             Process.killProcessQuiet(pid);
   4694             Process.killProcessGroup(app.info.uid, pid);
   4695             app.killed = true;
   4696         }
   4697 
   4698         // Clean up already done if the process has been re-started.
   4699         if (app.pid == pid && app.thread != null &&
   4700                 app.thread.asBinder() == thread.asBinder()) {
   4701             boolean doLowMem = app.instrumentationClass == null;
   4702             boolean doOomAdj = doLowMem;
   4703             if (!app.killedByAm) {
   4704                 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
   4705                         + ") has died");
   4706                 mAllowLowerMemLevel = true;
   4707             } else {
   4708                 // Note that we always want to do oom adj to update our state with the
   4709                 // new number of procs.
   4710                 mAllowLowerMemLevel = false;
   4711                 doLowMem = false;
   4712             }
   4713             EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName);
   4714             if (DEBUG_CLEANUP) Slog.v(
   4715                 TAG, "Dying app: " + app + ", pid: " + pid
   4716                 + ", thread: " + thread.asBinder());
   4717             handleAppDiedLocked(app, false, true);
   4718 
   4719             if (doOomAdj) {
   4720                 updateOomAdjLocked();
   4721             }
   4722             if (doLowMem) {
   4723                 doLowMemReportIfNeededLocked(app);
   4724             }
   4725         } else if (app.pid != pid) {
   4726             // A new process has already been started.
   4727             Slog.i(TAG, "Process " + app.processName + " (pid " + pid
   4728                     + ") has died and restarted (pid " + app.pid + ").");
   4729             EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName);
   4730         } else if (DEBUG_PROCESSES) {
   4731             Slog.d(TAG, "Received spurious death notification for thread "
   4732                     + thread.asBinder());
   4733         }
   4734     }
   4735 
   4736     /**
   4737      * If a stack trace dump file is configured, dump process stack traces.
   4738      * @param clearTraces causes the dump file to be erased prior to the new
   4739      *    traces being written, if true; when false, the new traces will be
   4740      *    appended to any existing file content.
   4741      * @param firstPids of dalvik VM processes to dump stack traces for first
   4742      * @param lastPids of dalvik VM processes to dump stack traces for last
   4743      * @param nativeProcs optional list of native process names to dump stack crawls
   4744      * @return file containing stack traces, or null if no dump file is configured
   4745      */
   4746     public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids,
   4747             ProcessCpuTracker processCpuTracker, SparseArray<Boolean> lastPids, String[] nativeProcs) {
   4748         String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
   4749         if (tracesPath == null || tracesPath.length() == 0) {
   4750             return null;
   4751         }
   4752 
   4753         File tracesFile = new File(tracesPath);
   4754         try {
   4755             File tracesDir = tracesFile.getParentFile();
   4756             if (!tracesDir.exists()) {
   4757                 tracesDir.mkdirs();
   4758                 if (!SELinux.restorecon(tracesDir)) {
   4759                     return null;
   4760                 }
   4761             }
   4762             FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1);  // drwxrwxr-x
   4763 
   4764             if (clearTraces && tracesFile.exists()) tracesFile.delete();
   4765             tracesFile.createNewFile();
   4766             FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
   4767         } catch (IOException e) {
   4768             Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
   4769             return null;
   4770         }
   4771 
   4772         dumpStackTraces(tracesPath, firstPids, processCpuTracker, lastPids, nativeProcs);
   4773         return tracesFile;
   4774     }
   4775 
   4776     private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids,
   4777             ProcessCpuTracker processCpuTracker, SparseArray<Boolean> lastPids, String[] nativeProcs) {
   4778         // Use a FileObserver to detect when traces finish writing.
   4779         // The order of traces is considered important to maintain for legibility.
   4780         FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
   4781             @Override
   4782             public synchronized void onEvent(int event, String path) { notify(); }
   4783         };
   4784 
   4785         try {
   4786             observer.startWatching();
   4787 
   4788             // First collect all of the stacks of the most important pids.
   4789             if (firstPids != null) {
   4790                 try {
   4791                     int num = firstPids.size();
   4792                     for (int i = 0; i < num; i++) {
   4793                         synchronized (observer) {
   4794                             Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
   4795                             observer.wait(200);  // Wait for write-close, give up after 200msec
   4796                         }
   4797                     }
   4798                 } catch (InterruptedException e) {
   4799                     Slog.wtf(TAG, e);
   4800                 }
   4801             }
   4802 
   4803             // Next collect the stacks of the native pids
   4804             if (nativeProcs != null) {
   4805                 int[] pids = Process.getPidsForCommands(nativeProcs);
   4806                 if (pids != null) {
   4807                     for (int pid : pids) {
   4808                         Debug.dumpNativeBacktraceToFile(pid, tracesPath);
   4809                     }
   4810                 }
   4811             }
   4812 
   4813             // Lastly, measure CPU usage.
   4814             if (processCpuTracker != null) {
   4815                 processCpuTracker.init();
   4816                 System.gc();
   4817                 processCpuTracker.update();
   4818                 try {
   4819                     synchronized (processCpuTracker) {
   4820                         processCpuTracker.wait(500); // measure over 1/2 second.
   4821                     }
   4822                 } catch (InterruptedException e) {
   4823                 }
   4824                 processCpuTracker.update();
   4825 
   4826                 // We'll take the stack crawls of just the top apps using CPU.
   4827                 final int N = processCpuTracker.countWorkingStats();
   4828                 int numProcs = 0;
   4829                 for (int i=0; i<N && numProcs<5; i++) {
   4830                     ProcessCpuTracker.Stats stats = processCpuTracker.getWorkingStats(i);
   4831                     if (lastPids.indexOfKey(stats.pid) >= 0) {
   4832                         numProcs++;
   4833                         try {
   4834                             synchronized (observer) {
   4835                                 Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
   4836                                 observer.wait(200);  // Wait for write-close, give up after 200msec
   4837                             }
   4838                         } catch (InterruptedException e) {
   4839                             Slog.wtf(TAG, e);
   4840                         }
   4841 
   4842                     }
   4843                 }
   4844             }
   4845         } finally {
   4846             observer.stopWatching();
   4847         }
   4848     }
   4849 
   4850     final void logAppTooSlow(ProcessRecord app, long startTime, String msg) {
   4851         if (true || IS_USER_BUILD) {
   4852             return;
   4853         }
   4854         String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
   4855         if (tracesPath == null || tracesPath.length() == 0) {
   4856             return;
   4857         }
   4858 
   4859         StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
   4860         StrictMode.allowThreadDiskWrites();
   4861         try {
   4862             final File tracesFile = new File(tracesPath);
   4863             final File tracesDir = tracesFile.getParentFile();
   4864             final File tracesTmp = new File(tracesDir, "__tmp__");
   4865             try {
   4866                 if (!tracesDir.exists()) {
   4867                     tracesDir.mkdirs();
   4868                     if (!SELinux.restorecon(tracesDir.getPath())) {
   4869                         return;
   4870                     }
   4871                 }
   4872                 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1);  // drwxrwxr-x
   4873 
   4874                 if (tracesFile.exists()) {
   4875                     tracesTmp.delete();
   4876                     tracesFile.renameTo(tracesTmp);
   4877                 }
   4878                 StringBuilder sb = new StringBuilder();
   4879                 Time tobj = new Time();
   4880                 tobj.set(System.currentTimeMillis());
   4881                 sb.append(tobj.format("%Y-%m-%d %H:%M:%S"));
   4882                 sb.append(": ");
   4883                 TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb);
   4884                 sb.append(" since ");
   4885                 sb.append(msg);
   4886                 FileOutputStream fos = new FileOutputStream(tracesFile);
   4887                 fos.write(sb.toString().getBytes());
   4888                 if (app == null) {
   4889                     fos.write("\n*** No application process!".getBytes());
   4890                 }
   4891                 fos.close();
   4892                 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
   4893             } catch (IOException e) {
   4894                 Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e);
   4895                 return;
   4896             }
   4897 
   4898             if (app != null) {
   4899                 ArrayList<Integer> firstPids = new ArrayList<Integer>();
   4900                 firstPids.add(app.pid);
   4901                 dumpStackTraces(tracesPath, firstPids, null, null, null);
   4902             }
   4903 
   4904             File lastTracesFile = null;
   4905             File curTracesFile = null;
   4906             for (int i=9; i>=0; i--) {
   4907                 String name = String.format(Locale.US, "slow%02d.txt", i);
   4908                 curTracesFile = new File(tracesDir, name);
   4909                 if (curTracesFile.exists()) {
   4910                     if (lastTracesFile != null) {
   4911                         curTracesFile.renameTo(lastTracesFile);
   4912                     } else {
   4913                         curTracesFile.delete();
   4914                     }
   4915                 }
   4916                 lastTracesFile = curTracesFile;
   4917             }
   4918             tracesFile.renameTo(curTracesFile);
   4919             if (tracesTmp.exists()) {
   4920                 tracesTmp.renameTo(tracesFile);
   4921             }
   4922         } finally {
   4923             StrictMode.setThreadPolicy(oldPolicy);
   4924         }
   4925     }
   4926 
   4927     final void appNotResponding(ProcessRecord app, ActivityRecord activity,
   4928             ActivityRecord parent, boolean aboveSystem, final String annotation) {
   4929         ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
   4930         SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
   4931 
   4932         if (mController != null) {
   4933             try {
   4934                 // 0 == continue, -1 = kill process immediately
   4935                 int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation);
   4936                 if (res < 0 && app.pid != MY_PID) {
   4937                     app.kill("anr", true);
   4938                 }
   4939             } catch (RemoteException e) {
   4940                 mController = null;
   4941                 Watchdog.getInstance().setActivityController(null);
   4942             }
   4943         }
   4944 
   4945         long anrTime = SystemClock.uptimeMillis();
   4946         if (MONITOR_CPU_USAGE) {
   4947             updateCpuStatsNow();
   4948         }
   4949 
   4950         synchronized (this) {
   4951             // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
   4952             if (mShuttingDown) {
   4953                 Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
   4954                 return;
   4955             } else if (app.notResponding) {
   4956                 Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
   4957                 return;
   4958             } else if (app.crashing) {
   4959                 Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
   4960                 return;
   4961             }
   4962 
   4963             // In case we come through here for the same app before completing
   4964             // this one, mark as anring now so we will bail out.
   4965             app.notResponding = true;
   4966 
   4967             // Log the ANR to the event log.
   4968             EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid,
   4969                     app.processName, app.info.flags, annotation);
   4970 
   4971             // Dump thread traces as quickly as we can, starting with "interesting" processes.
   4972             firstPids.add(app.pid);
   4973 
   4974             int parentPid = app.pid;
   4975             if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
   4976             if (parentPid != app.pid) firstPids.add(parentPid);
   4977 
   4978             if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
   4979 
   4980             for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
   4981                 ProcessRecord r = mLruProcesses.get(i);
   4982                 if (r != null && r.thread != null) {
   4983                     int pid = r.pid;
   4984                     if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
   4985                         if (r.persistent) {
   4986                             firstPids.add(pid);
   4987                         } else {
   4988                             lastPids.put(pid, Boolean.TRUE);
   4989                         }
   4990                     }
   4991                 }
   4992             }
   4993         }
   4994 
   4995         // Log the ANR to the main log.
   4996         StringBuilder info = new StringBuilder();
   4997         info.setLength(0);
   4998         info.append("ANR in ").append(app.processName);
   4999         if (activity != null && activity.shortComponentName != null) {
   5000             info.append(" (").append(activity.shortComponentName).append(")");
   5001         }
   5002         info.append("\n");
   5003         info.append("PID: ").append(app.pid).append("\n");
   5004         if (annotation != null) {
   5005             info.append("Reason: ").append(annotation).append("\n");
   5006         }
   5007         if (parent != null && parent != activity) {
   5008             info.append("Parent: ").append(parent.shortComponentName).append("\n");
   5009         }
   5010 
   5011         final ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true);
   5012 
   5013         File tracesFile = dumpStackTraces(true, firstPids, processCpuTracker, lastPids,
   5014                 NATIVE_STACKS_OF_INTEREST);
   5015 
   5016         String cpuInfo = null;
   5017         if (MONITOR_CPU_USAGE) {
   5018             updateCpuStatsNow();
   5019             synchronized (mProcessCpuTracker) {
   5020                 cpuInfo = mProcessCpuTracker.printCurrentState(anrTime);
   5021             }
   5022             info.append(processCpuTracker.printCurrentLoad());
   5023             info.append(cpuInfo);
   5024         }
   5025 
   5026         info.append(processCpuTracker.printCurrentState(anrTime));
   5027 
   5028         Slog.e(TAG, info.toString());
   5029         if (tracesFile == null) {
   5030             // There is no trace file, so dump (only) the alleged culprit's threads to the log
   5031             Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
   5032         }
   5033 
   5034         addErrorToDropBox("anr", app, app.processName, activity, parent, annotation,
   5035                 cpuInfo, tracesFile, null);
   5036 
   5037         if (mController != null) {
   5038             try {
   5039                 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
   5040                 int res = mController.appNotResponding(app.processName, app.pid, info.toString());
   5041                 if (res != 0) {
   5042                     if (res < 0 && app.pid != MY_PID) {
   5043                         app.kill("anr", true);
   5044                     } else {
   5045                         synchronized (this) {
   5046                             mServices.scheduleServiceTimeoutLocked(app);
   5047                         }
   5048                     }
   5049                     return;
   5050                 }
   5051             } catch (RemoteException e) {
   5052                 mController = null;
   5053                 Watchdog.getInstance().setActivityController(null);
   5054             }
   5055         }
   5056 
   5057         // Unless configured otherwise, swallow ANRs in background processes & kill the process.
   5058         boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
   5059                 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
   5060 
   5061         synchronized (this) {
   5062             mBatteryStatsService.noteProcessAnr(app.processName, app.uid);
   5063 
   5064             if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
   5065                 app.kill("bg anr", true);
   5066                 return;
   5067             }
   5068 
   5069             // Set the app's notResponding state, and look up the errorReportReceiver
   5070             makeAppNotRespondingLocked(app,
   5071                     activity != null ? activity.shortComponentName : null,
   5072                     annotation != null ? "ANR " + annotation : "ANR",
   5073                     info.toString());
   5074 
   5075             // Bring up the infamous App Not Responding dialog
   5076             Message msg = Message.obtain();
   5077             HashMap<String, Object> map = new HashMap<String, Object>();
   5078             msg.what = SHOW_NOT_RESPONDING_MSG;
   5079             msg.obj = map;
   5080             msg.arg1 = aboveSystem ? 1 : 0;
   5081             map.put("app", app);
   5082             if (activity != null) {
   5083                 map.put("activity", activity);
   5084             }
   5085 
   5086             mHandler.sendMessage(msg);
   5087         }
   5088     }
   5089 
   5090     final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) {
   5091         if (!mLaunchWarningShown) {
   5092             mLaunchWarningShown = true;
   5093             mHandler.post(new Runnable() {
   5094                 @Override
   5095                 public void run() {
   5096                     synchronized (ActivityManagerService.this) {
   5097                         final Dialog d = new LaunchWarningWindow(mContext, cur, next);
   5098                         d.show();
   5099                         mHandler.postDelayed(new Runnable() {
   5100                             @Override
   5101                             public void run() {
   5102                                 synchronized (ActivityManagerService.this) {
   5103                                     d.dismiss();
   5104                                     mLaunchWarningShown = false;
   5105                                 }
   5106                             }
   5107                         }, 4000);
   5108                     }
   5109                 }
   5110             });
   5111         }
   5112     }
   5113 
   5114     @Override
   5115     public boolean clearApplicationUserData(final String packageName,
   5116             final IPackageDataObserver observer, int userId) {
   5117         enforceNotIsolatedCaller("clearApplicationUserData");
   5118         int uid = Binder.getCallingUid();
   5119         int pid = Binder.getCallingPid();
   5120         userId = handleIncomingUser(pid, uid,
   5121                 userId, false, ALLOW_FULL_ONLY, "clearApplicationUserData", null);
   5122         long callingId = Binder.clearCallingIdentity();
   5123         try {
   5124             IPackageManager pm = AppGlobals.getPackageManager();
   5125             int pkgUid = -1;
   5126             synchronized(this) {
   5127                 try {
   5128                     pkgUid = pm.getPackageUid(packageName, userId);
   5129                 } catch (RemoteException e) {
   5130                 }
   5131                 if (pkgUid == -1) {
   5132                     Slog.w(TAG, "Invalid packageName: " + packageName);
   5133                     if (observer != null) {
   5134                         try {
   5135                             observer.onRemoveCompleted(packageName, false);
   5136                         } catch (RemoteException e) {
   5137                             Slog.i(TAG, "Observer no longer exists.");
   5138                         }
   5139                     }
   5140                     return false;
   5141                 }
   5142                 if (uid == pkgUid || checkComponentPermission(
   5143                         android.Manifest.permission.CLEAR_APP_USER_DATA,
   5144                         pid, uid, -1, true)
   5145                         == PackageManager.PERMISSION_GRANTED) {
   5146                     forceStopPackageLocked(packageName, pkgUid, "clear data");
   5147                 } else {
   5148                     throw new SecurityException("PID " + pid + " does not have permission "
   5149                             + android.Manifest.permission.CLEAR_APP_USER_DATA + " to clear data"
   5150                                     + " of package " + packageName);
   5151                 }
   5152 
   5153                 // Remove all tasks match the cleared application package and user
   5154                 for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
   5155                     final TaskRecord tr = mRecentTasks.get(i);
   5156                     final String taskPackageName =
   5157                             tr.getBaseIntent().getComponent().getPackageName();
   5158                     if (tr.userId != userId) continue;
   5159                     if (!taskPackageName.equals(packageName)) continue;
   5160                     removeTaskByIdLocked(tr.taskId, false);
   5161                 }
   5162             }
   5163 
   5164             try {
   5165                 // Clear application user data
   5166                 pm.clearApplicationUserData(packageName, observer, userId);
   5167 
   5168                 synchronized(this) {
   5169                     // Remove all permissions granted from/to this package
   5170                     removeUriPermissionsForPackageLocked(packageName, userId, true);
   5171                 }
   5172 
   5173                 Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
   5174                         Uri.fromParts("package", packageName, null));
   5175                 intent.putExtra(Intent.EXTRA_UID, pkgUid);
   5176                 broadcastIntentInPackage("android", Process.SYSTEM_UID, intent,
   5177                         null, null, 0, null, null, null, false, false, userId);
   5178             } catch (RemoteException e) {
   5179             }
   5180         } finally {
   5181             Binder.restoreCallingIdentity(callingId);
   5182         }
   5183         return true;
   5184     }
   5185 
   5186     @Override
   5187     public void killBackgroundProcesses(final String packageName, int userId) {
   5188         if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
   5189                 != PackageManager.PERMISSION_GRANTED &&
   5190                 checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
   5191                         != PackageManager.PERMISSION_GRANTED) {
   5192             String msg = "Permission Denial: killBackgroundProcesses() from pid="
   5193                     + Binder.getCallingPid()
   5194                     + ", uid=" + Binder.getCallingUid()
   5195                     + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
   5196             Slog.w(TAG, msg);
   5197             throw new SecurityException(msg);
   5198         }
   5199 
   5200         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
   5201                 userId, true, ALLOW_FULL_ONLY, "killBackgroundProcesses", null);
   5202         long callingId = Binder.clearCallingIdentity();
   5203         try {
   5204             IPackageManager pm = AppGlobals.getPackageManager();
   5205             synchronized(this) {
   5206                 int appId = -1;
   5207                 try {
   5208                     appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0));
   5209                 } catch (RemoteException e) {
   5210                 }
   5211                 if (appId == -1) {
   5212                     Slog.w(TAG, "Invalid packageName: " + packageName);
   5213                     return;
   5214                 }
   5215                 killPackageProcessesLocked(packageName, appId, userId,
   5216                         ProcessList.SERVICE_ADJ, false, true, true, false, "kill background");
   5217             }
   5218         } finally {
   5219             Binder.restoreCallingIdentity(callingId);
   5220         }
   5221     }
   5222 
   5223     @Override
   5224     public void killAllBackgroundProcesses() {
   5225         if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
   5226                 != PackageManager.PERMISSION_GRANTED) {
   5227             String msg = "Permission Denial: killAllBackgroundProcesses() from pid="
   5228                     + Binder.getCallingPid()
   5229                     + ", uid=" + Binder.getCallingUid()
   5230                     + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
   5231             Slog.w(TAG, msg);
   5232             throw new SecurityException(msg);
   5233         }
   5234 
   5235         long callingId = Binder.clearCallingIdentity();
   5236         try {
   5237             synchronized(this) {
   5238                 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
   5239                 final int NP = mProcessNames.getMap().size();
   5240                 for (int ip=0; ip<NP; ip++) {
   5241                     SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
   5242                     final int NA = apps.size();
   5243                     for (int ia=0; ia<NA; ia++) {
   5244                         ProcessRecord app = apps.valueAt(ia);
   5245                         if (app.persistent) {
   5246                             // we don't kill persistent processes
   5247                             continue;
   5248                         }
   5249                         if (app.removed) {
   5250                             procs.add(app);
   5251                         } else if (app.setAdj >= ProcessList.CACHED_APP_MIN_ADJ) {
   5252                             app.removed = true;
   5253                             procs.add(app);
   5254                         }
   5255                     }
   5256                 }
   5257 
   5258                 int N = procs.size();
   5259                 for (int i=0; i<N; i++) {
   5260                     removeProcessLocked(procs.get(i), false, true, "kill all background");
   5261                 }
   5262                 mAllowLowerMemLevel = true;
   5263                 updateOomAdjLocked();
   5264                 doLowMemReportIfNeededLocked(null);
   5265             }
   5266         } finally {
   5267             Binder.restoreCallingIdentity(callingId);
   5268         }
   5269     }
   5270 
   5271     @Override
   5272     public void forceStopPackage(final String packageName, int userId) {
   5273         if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
   5274                 != PackageManager.PERMISSION_GRANTED) {
   5275             String msg = "Permission Denial: forceStopPackage() from pid="
   5276                     + Binder.getCallingPid()
   5277                     + ", uid=" + Binder.getCallingUid()
   5278                     + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
   5279             Slog.w(TAG, msg);
   5280             throw new SecurityException(msg);
   5281         }
   5282         final int callingPid = Binder.getCallingPid();
   5283         userId = handleIncomingUser(callingPid, Binder.getCallingUid(),
   5284                 userId, true, ALLOW_FULL_ONLY, "forceStopPackage", null);
   5285         long callingId = Binder.clearCallingIdentity();
   5286         try {
   5287             IPackageManager pm = AppGlobals.getPackageManager();
   5288             synchronized(this) {
   5289                 int[] users = userId == UserHandle.USER_ALL
   5290                         ? getUsersLocked() : new int[] { userId };
   5291                 for (int user : users) {
   5292                     int pkgUid = -1;
   5293                     try {
   5294                         pkgUid = pm.getPackageUid(packageName, user);
   5295                     } catch (RemoteException e) {
   5296                     }
   5297                     if (pkgUid == -1) {
   5298                         Slog.w(TAG, "Invalid packageName: " + packageName);
   5299                         continue;
   5300                     }
   5301                     try {
   5302                         pm.setPackageStoppedState(packageName, true, user);
   5303                     } catch (RemoteException e) {
   5304                     } catch (IllegalArgumentException e) {
   5305                         Slog.w(TAG, "Failed trying to unstop package "
   5306                                 + packageName + ": " + e);
   5307                     }
   5308                     if (isUserRunningLocked(user, false)) {
   5309                         forceStopPackageLocked(packageName, pkgUid, "from pid " + callingPid);
   5310                     }
   5311                 }
   5312             }
   5313         } finally {
   5314             Binder.restoreCallingIdentity(callingId);
   5315         }
   5316     }
   5317 
   5318     @Override
   5319     public void addPackageDependency(String packageName) {
   5320         synchronized (this) {
   5321             int callingPid = Binder.getCallingPid();
   5322             if (callingPid == Process.myPid()) {
   5323                 //  Yeah, um, no.
   5324                 return;
   5325             }
   5326             ProcessRecord proc;
   5327             synchronized (mPidsSelfLocked) {
   5328                 proc = mPidsSelfLocked.get(Binder.getCallingPid());
   5329             }
   5330             if (proc != null) {
   5331                 if (proc.pkgDeps == null) {
   5332                     proc.pkgDeps = new ArraySet<String>(1);
   5333                 }
   5334                 proc.pkgDeps.add(packageName);
   5335             }
   5336         }
   5337     }
   5338 
   5339     /*
   5340      * The pkg name and app id have to be specified.
   5341      */
   5342     @Override
   5343     public void killApplicationWithAppId(String pkg, int appid, String reason) {
   5344         if (pkg == null) {
   5345             return;
   5346         }
   5347         // Make sure the uid is valid.
   5348         if (appid < 0) {
   5349             Slog.w(TAG, "Invalid appid specified for pkg : " + pkg);
   5350             return;
   5351         }
   5352         int callerUid = Binder.getCallingUid();
   5353         // Only the system server can kill an application
   5354         if (callerUid == Process.SYSTEM_UID) {
   5355             // Post an aysnc message to kill the application
   5356             Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
   5357             msg.arg1 = appid;
   5358             msg.arg2 = 0;
   5359             Bundle bundle = new Bundle();
   5360             bundle.putString("pkg", pkg);
   5361             bundle.putString("reason", reason);
   5362             msg.obj = bundle;
   5363             mHandler.sendMessage(msg);
   5364         } else {
   5365             throw new SecurityException(callerUid + " cannot kill pkg: " +
   5366                     pkg);
   5367         }
   5368     }
   5369 
   5370     @Override
   5371     public void closeSystemDialogs(String reason) {
   5372         enforceNotIsolatedCaller("closeSystemDialogs");
   5373 
   5374         final int pid = Binder.getCallingPid();
   5375         final int uid = Binder.getCallingUid();
   5376         final long origId = Binder.clearCallingIdentity();
   5377         try {
   5378             synchronized (this) {
   5379                 // Only allow this from foreground processes, so that background
   5380                 // applications can't abuse it to prevent system UI from being shown.
   5381                 if (uid >= Process.FIRST_APPLICATION_UID) {
   5382                     ProcessRecord proc;
   5383                     synchronized (mPidsSelfLocked) {
   5384                         proc = mPidsSelfLocked.get(pid);
   5385                     }
   5386                     if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
   5387                         Slog.w(TAG, "Ignoring closeSystemDialogs " + reason
   5388                                 + " from background process " + proc);
   5389                         return;
   5390                     }
   5391                 }
   5392                 closeSystemDialogsLocked(reason);
   5393             }
   5394         } finally {
   5395             Binder.restoreCallingIdentity(origId);
   5396         }
   5397     }
   5398 
   5399     void closeSystemDialogsLocked(String reason) {
   5400         Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
   5401         intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
   5402                 | Intent.FLAG_RECEIVER_FOREGROUND);
   5403         if (reason != null) {
   5404             intent.putExtra("reason", reason);
   5405         }
   5406         mWindowManager.closeSystemDialogs(reason);
   5407 
   5408         mStackSupervisor.closeSystemDialogsLocked();
   5409 
   5410         broadcastIntentLocked(null, null, intent, null,
   5411                 null, 0, null, null, null, AppOpsManager.OP_NONE, false, false, -1,
   5412                 Process.SYSTEM_UID, UserHandle.USER_ALL);
   5413     }
   5414 
   5415     @Override
   5416     public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
   5417         enforceNotIsolatedCaller("getProcessMemoryInfo");
   5418         Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
   5419         for (int i=pids.length-1; i>=0; i--) {
   5420             ProcessRecord proc;
   5421             int oomAdj;
   5422             synchronized (this) {
   5423                 synchronized (mPidsSelfLocked) {
   5424                     proc = mPidsSelfLocked.get(pids[i]);
   5425                     oomAdj = proc != null ? proc.setAdj : 0;
   5426                 }
   5427             }
   5428             infos[i] = new Debug.MemoryInfo();
   5429             Debug.getMemoryInfo(pids[i], infos[i]);
   5430             if (proc != null) {
   5431                 synchronized (this) {
   5432                     if (proc.thread != null && proc.setAdj == oomAdj) {
   5433                         // Record this for posterity if the process has been stable.
   5434                         proc.baseProcessTracker.addPss(infos[i].getTotalPss(),
   5435                                 infos[i].getTotalUss(), false, proc.pkgList);
   5436                     }
   5437                 }
   5438             }
   5439         }
   5440         return infos;
   5441     }
   5442 
   5443     @Override
   5444     public long[] getProcessPss(int[] pids) {
   5445         enforceNotIsolatedCaller("getProcessPss");
   5446         long[] pss = new long[pids.length];
   5447         for (int i=pids.length-1; i>=0; i--) {
   5448             ProcessRecord proc;
   5449             int oomAdj;
   5450             synchronized (this) {
   5451                 synchronized (mPidsSelfLocked) {
   5452                     proc = mPidsSelfLocked.get(pids[i]);
   5453                     oomAdj = proc != null ? proc.setAdj : 0;
   5454                 }
   5455             }
   5456             long[] tmpUss = new long[1];
   5457             pss[i] = Debug.getPss(pids[i], tmpUss, null);
   5458             if (proc != null) {
   5459                 synchronized (this) {
   5460                     if (proc.thread != null && proc.setAdj == oomAdj) {
   5461                         // Record this for posterity if the process has been stable.
   5462                         proc.baseProcessTracker.addPss(pss[i], tmpUss[0], false, proc.pkgList);
   5463                     }
   5464                 }
   5465             }
   5466         }
   5467         return pss;
   5468     }
   5469 
   5470     @Override
   5471     public void killApplicationProcess(String processName, int uid) {
   5472         if (processName == null) {
   5473             return;
   5474         }
   5475 
   5476         int callerUid = Binder.getCallingUid();
   5477         // Only the system server can kill an application
   5478         if (callerUid == Process.SYSTEM_UID) {
   5479             synchronized (this) {
   5480                 ProcessRecord app = getProcessRecordLocked(processName, uid, true);
   5481                 if (app != null && app.thread != null) {
   5482                     try {
   5483                         app.thread.scheduleSuicide();
   5484                     } catch (RemoteException e) {
   5485                         // If the other end already died, then our work here is done.
   5486                     }
   5487                 } else {
   5488                     Slog.w(TAG, "Process/uid not found attempting kill of "
   5489                             + processName + " / " + uid);
   5490                 }
   5491             }
   5492         } else {
   5493             throw new SecurityException(callerUid + " cannot kill app process: " +
   5494                     processName);
   5495         }
   5496     }
   5497 
   5498     private void forceStopPackageLocked(final String packageName, int uid, String reason) {
   5499         forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false,
   5500                 false, true, false, false, UserHandle.getUserId(uid), reason);
   5501         Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
   5502                 Uri.fromParts("package", packageName, null));
   5503         if (!mProcessesReady) {
   5504             intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
   5505                     | Intent.FLAG_RECEIVER_FOREGROUND);
   5506         }
   5507         intent.putExtra(Intent.EXTRA_UID, uid);
   5508         intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid));
   5509         broadcastIntentLocked(null, null, intent,
   5510                 null, null, 0, null, null, null, AppOpsManager.OP_NONE,
   5511                 false, false,
   5512                 MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid));
   5513     }
   5514 
   5515     private void forceStopUserLocked(int userId, String reason) {
   5516         forceStopPackageLocked(null, -1, false, false, true, false, false, userId, reason);
   5517         Intent intent = new Intent(Intent.ACTION_USER_STOPPED);
   5518         intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
   5519                 | Intent.FLAG_RECEIVER_FOREGROUND);
   5520         intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
   5521         broadcastIntentLocked(null, null, intent,
   5522                 null, null, 0, null, null, null, AppOpsManager.OP_NONE,
   5523                 false, false,
   5524                 MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
   5525     }
   5526 
   5527     private final boolean killPackageProcessesLocked(String packageName, int appId,
   5528             int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart,
   5529             boolean doit, boolean evenPersistent, String reason) {
   5530         ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
   5531 
   5532         // Remove all processes this package may have touched: all with the
   5533         // same UID (except for the system or root user), and all whose name
   5534         // matches the package name.
   5535         final int NP = mProcessNames.getMap().size();
   5536         for (int ip=0; ip<NP; ip++) {
   5537             SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
   5538             final int NA = apps.size();
   5539             for (int ia=0; ia<NA; ia++) {
   5540                 ProcessRecord app = apps.valueAt(ia);
   5541                 if (app.persistent && !evenPersistent) {
   5542                     // we don't kill persistent processes
   5543                     continue;
   5544                 }
   5545                 if (app.removed) {
   5546                     if (doit) {
   5547                         procs.add(app);
   5548                     }
   5549                     continue;
   5550                 }
   5551 
   5552                 // Skip process if it doesn't meet our oom adj requirement.
   5553                 if (app.setAdj < minOomAdj) {
   5554                     continue;
   5555                 }
   5556 
   5557                 // If no package is specified, we call all processes under the
   5558                 // give user id.
   5559                 if (packageName == null) {
   5560                     if (app.userId != userId) {
   5561                         continue;
   5562                     }
   5563                     if (appId >= 0 && UserHandle.getAppId(app.uid) != appId) {
   5564                         continue;
   5565                     }
   5566                 // Package has been specified, we want to hit all processes
   5567                 // that match it.  We need to qualify this by the processes
   5568                 // that are running under the specified app and user ID.
   5569                 } else {
   5570                     final boolean isDep = app.pkgDeps != null
   5571                             && app.pkgDeps.contains(packageName);
   5572                     if (!isDep && UserHandle.getAppId(app.uid) != appId) {
   5573                         continue;
   5574                     }
   5575                     if (userId != UserHandle.USER_ALL && app.userId != userId) {
   5576                         continue;
   5577                     }
   5578                     if (!app.pkgList.containsKey(packageName) && !isDep) {
   5579                         continue;
   5580                     }
   5581                 }
   5582 
   5583                 // Process has passed all conditions, kill it!
   5584                 if (!doit) {
   5585                     return true;
   5586                 }
   5587                 app.removed = true;
   5588                 procs.add(app);
   5589             }
   5590         }
   5591 
   5592         int N = procs.size();
   5593         for (int i=0; i<N; i++) {
   5594             removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
   5595         }
   5596         updateOomAdjLocked();
   5597         return N > 0;
   5598     }
   5599 
   5600     private final boolean forceStopPackageLocked(String name, int appId,
   5601             boolean callerWillRestart, boolean purgeCache, boolean doit,
   5602             boolean evenPersistent, boolean uninstalling, int userId, String reason) {
   5603         int i;
   5604         int N;
   5605 
   5606         if (userId == UserHandle.USER_ALL && name == null) {
   5607             Slog.w(TAG, "Can't force stop all processes of all users, that is insane!");
   5608         }
   5609 
   5610         if (appId < 0 && name != null) {
   5611             try {
   5612                 appId = UserHandle.getAppId(
   5613                         AppGlobals.getPackageManager().getPackageUid(name, 0));
   5614             } catch (RemoteException e) {
   5615             }
   5616         }
   5617 
   5618         if (doit) {
   5619             if (name != null) {
   5620                 Slog.i(TAG, "Force stopping " + name + " appid=" + appId
   5621                         + " user=" + userId + ": " + reason);
   5622             } else {
   5623                 Slog.i(TAG, "Force stopping u" + userId + ": " + reason);
   5624             }
   5625 
   5626             final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap();
   5627             for (int ip=pmap.size()-1; ip>=0; ip--) {
   5628                 SparseArray<Long> ba = pmap.valueAt(ip);
   5629                 for (i=ba.size()-1; i>=0; i--) {
   5630                     boolean remove = false;
   5631                     final int entUid = ba.keyAt(i);
   5632                     if (name != null) {
   5633                         if (userId == UserHandle.USER_ALL) {
   5634                             if (UserHandle.getAppId(entUid) == appId) {
   5635                                 remove = true;
   5636                             }
   5637                         } else {
   5638                             if (entUid == UserHandle.getUid(userId, appId)) {
   5639                                 remove = true;
   5640                             }
   5641                         }
   5642                     } else if (UserHandle.getUserId(entUid) == userId) {
   5643                         remove = true;
   5644                     }
   5645                     if (remove) {
   5646                         ba.removeAt(i);
   5647                     }
   5648                 }
   5649                 if (ba.size() == 0) {
   5650                     pmap.removeAt(ip);
   5651                 }
   5652             }
   5653         }
   5654 
   5655         boolean didSomething = killPackageProcessesLocked(name, appId, userId,
   5656                 -100, callerWillRestart, true, doit, evenPersistent,
   5657                 name == null ? ("stop user " + userId) : ("stop " + name));
   5658 
   5659         if (mStackSupervisor.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
   5660             if (!doit) {
   5661                 return true;
   5662             }
   5663             didSomething = true;
   5664         }
   5665 
   5666         if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) {
   5667             if (!doit) {
   5668                 return true;
   5669             }
   5670             didSomething = true;
   5671         }
   5672 
   5673         if (name == null) {
   5674             // Remove all sticky broadcasts from this user.
   5675             mStickyBroadcasts.remove(userId);
   5676         }
   5677 
   5678         ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>();
   5679         if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent,
   5680                 userId, providers)) {
   5681             if (!doit) {
   5682                 return true;
   5683             }
   5684             didSomething = true;
   5685         }
   5686         N = providers.size();
   5687         for (i=0; i<N; i++) {
   5688             removeDyingProviderLocked(null, providers.get(i), true);
   5689         }
   5690 
   5691         // Remove transient permissions granted from/to this package/user
   5692         removeUriPermissionsForPackageLocked(name, userId, false);
   5693 
   5694         if (name == null || uninstalling) {
   5695             // Remove pending intents.  For now we only do this when force
   5696             // stopping users, because we have some problems when doing this
   5697             // for packages -- app widgets are not currently cleaned up for
   5698             // such packages, so they can be left with bad pending intents.
   5699             if (mIntentSenderRecords.size() > 0) {
   5700                 Iterator<WeakReference<PendingIntentRecord>> it
   5701                         = mIntentSenderRecords.values().iterator();
   5702                 while (it.hasNext()) {
   5703                     WeakReference<PendingIntentRecord> wpir = it.next();
   5704                     if (wpir == null) {
   5705                         it.remove();
   5706                         continue;
   5707                     }
   5708                     PendingIntentRecord pir = wpir.get();
   5709                     if (pir == null) {
   5710                         it.remove();
   5711                         continue;
   5712                     }
   5713                     if (name == null) {
   5714                         // Stopping user, remove all objects for the user.
   5715                         if (pir.key.userId != userId) {
   5716                             // Not the same user, skip it.
   5717                             continue;
   5718                         }
   5719                     } else {
   5720                         if (UserHandle.getAppId(pir.uid) != appId) {
   5721                             // Different app id, skip it.
   5722                             continue;
   5723                         }
   5724                         if (userId != UserHandle.USER_ALL && pir.key.userId != userId) {
   5725                             // Different user, skip it.
   5726                             continue;
   5727                         }
   5728                         if (!pir.key.packageName.equals(name)) {
   5729                             // Different package, skip it.
   5730                             continue;
   5731                         }
   5732                     }
   5733                     if (!doit) {
   5734                         return true;
   5735                     }
   5736                     didSomething = true;
   5737                     it.remove();
   5738                     pir.canceled = true;
   5739                     if (pir.key.activity != null && pir.key.activity.pendingResults != null) {
   5740                         pir.key.activity.pendingResults.remove(pir.ref);
   5741                     }
   5742                 }
   5743             }
   5744         }
   5745 
   5746         if (doit) {
   5747             if (purgeCache && name != null) {
   5748                 AttributeCache ac = AttributeCache.instance();
   5749                 if (ac != null) {
   5750                     ac.removePackage(name);
   5751                 }
   5752             }
   5753             if (mBooted) {
   5754                 mStackSupervisor.resumeTopActivitiesLocked();
   5755                 mStackSupervisor.scheduleIdleLocked();
   5756             }
   5757         }
   5758 
   5759         return didSomething;
   5760     }
   5761 
   5762     private final boolean removeProcessLocked(ProcessRecord app,
   5763             boolean callerWillRestart, boolean allowRestart, String reason) {
   5764         final String name = app.processName;
   5765         final int uid = app.uid;
   5766         if (DEBUG_PROCESSES) Slog.d(
   5767             TAG, "Force removing proc " + app.toShortString() + " (" + name
   5768             + "/" + uid + ")");
   5769 
   5770         mProcessNames.remove(name, uid);
   5771         mIsolatedProcesses.remove(app.uid);
   5772         if (mHeavyWeightProcess == app) {
   5773             mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
   5774                     mHeavyWeightProcess.userId, 0));
   5775             mHeavyWeightProcess = null;
   5776         }
   5777         boolean needRestart = false;
   5778         if (app.pid > 0 && app.pid != MY_PID) {
   5779             int pid = app.pid;
   5780             synchronized (mPidsSelfLocked) {
   5781                 mPidsSelfLocked.remove(pid);
   5782                 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
   5783             }
   5784             mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
   5785             if (app.isolated) {
   5786                 mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
   5787             }
   5788             app.kill(reason, true);
   5789             handleAppDiedLocked(app, true, allowRestart);
   5790             removeLruProcessLocked(app);
   5791 
   5792             if (app.persistent && !app.isolated) {
   5793                 if (!callerWillRestart) {
   5794                     addAppLocked(app.info, false, null /* ABI override */);
   5795                 } else {
   5796                     needRestart = true;
   5797                 }
   5798             }
   5799         } else {
   5800             mRemovedProcesses.add(app);
   5801         }
   5802 
   5803         return needRestart;
   5804     }
   5805 
   5806     private final void processStartTimedOutLocked(ProcessRecord app) {
   5807         final int pid = app.pid;
   5808         boolean gone = false;
   5809         synchronized (mPidsSelfLocked) {
   5810             ProcessRecord knownApp = mPidsSelfLocked.get(pid);
   5811             if (knownApp != null && knownApp.thread == null) {
   5812                 mPidsSelfLocked.remove(pid);
   5813                 gone = true;
   5814             }
   5815         }
   5816 
   5817         if (gone) {
   5818             Slog.w(TAG, "Process " + app + " failed to attach");
   5819             EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, app.userId,
   5820                     pid, app.uid, app.processName);
   5821             mProcessNames.remove(app.processName, app.uid);
   5822             mIsolatedProcesses.remove(app.uid);
   5823             if (mHeavyWeightProcess == app) {
   5824                 mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
   5825                         mHeavyWeightProcess.userId, 0));
   5826                 mHeavyWeightProcess = null;
   5827             }
   5828             mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
   5829             if (app.isolated) {
   5830                 mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
   5831             }
   5832             // Take care of any launching providers waiting for this process.
   5833             checkAppInLaunchingProvidersLocked(app, true);
   5834             // Take care of any services that are waiting for the process.
   5835             mServices.processStartTimedOutLocked(app);
   5836             app.kill("start timeout", true);
   5837             if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
   5838                 Slog.w(TAG, "Unattached app died before backup, skipping");
   5839                 try {
   5840                     IBackupManager bm = IBackupManager.Stub.asInterface(
   5841                             ServiceManager.getService(Context.BACKUP_SERVICE));
   5842                     bm.agentDisconnected(app.info.packageName);
   5843                 } catch (RemoteException e) {
   5844                     // Can't happen; the backup manager is local
   5845                 }
   5846             }
   5847             if (isPendingBroadcastProcessLocked(pid)) {
   5848                 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
   5849                 skipPendingBroadcastLocked(pid);
   5850             }
   5851         } else {
   5852             Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
   5853         }
   5854     }
   5855 
   5856     private final boolean attachApplicationLocked(IApplicationThread thread,
   5857             int pid) {
   5858 
   5859         // Find the application record that is being attached...  either via
   5860         // the pid if we are running in multiple processes, or just pull the
   5861         // next app record if we are emulating process with anonymous threads.
   5862         ProcessRecord app;
   5863         if (pid != MY_PID && pid >= 0) {
   5864             synchronized (mPidsSelfLocked) {
   5865                 app = mPidsSelfLocked.get(pid);
   5866             }
   5867         } else {
   5868             app = null;
   5869         }
   5870 
   5871         if (app == null) {
   5872             Slog.w(TAG, "No pending application record for pid " + pid
   5873                     + " (IApplicationThread " + thread + "); dropping process");
   5874             EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
   5875             if (pid > 0 && pid != MY_PID) {
   5876                 Process.killProcessQuiet(pid);
   5877                 //TODO: Process.killProcessGroup(app.info.uid, pid);
   5878             } else {
   5879                 try {
   5880                     thread.scheduleExit();
   5881                 } catch (Exception e) {
   5882                     // Ignore exceptions.
   5883                 }
   5884             }
   5885             return false;
   5886         }
   5887 
   5888         // If this application record is still attached to a previous
   5889         // process, clean it up now.
   5890         if (app.thread != null) {
   5891             handleAppDiedLocked(app, true, true);
   5892         }
   5893 
   5894         // Tell the process all about itself.
   5895 
   5896         if (localLOGV) Slog.v(
   5897                 TAG, "Binding process pid " + pid + " to record " + app);
   5898 
   5899         final String processName = app.processName;
   5900         try {
   5901             AppDeathRecipient adr = new AppDeathRecipient(
   5902                     app, pid, thread);
   5903             thread.asBinder().linkToDeath(adr, 0);
   5904             app.deathRecipient = adr;
   5905         } catch (RemoteException e) {
   5906             app.resetPackageList(mProcessStats);
   5907             startProcessLocked(app, "link fail", processName);
   5908             return false;
   5909         }
   5910 
   5911         EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.userId, app.pid, app.processName);
   5912 
   5913         app.makeActive(thread, mProcessStats);
   5914         app.curAdj = app.setAdj = -100;
   5915         app.curSchedGroup = app.setSchedGroup = Process.THREAD_GROUP_DEFAULT;
   5916         app.forcingToForeground = null;
   5917         updateProcessForegroundLocked(app, false, false);
   5918         app.hasShownUi = false;
   5919         app.debugging = false;
   5920         app.cached = false;
   5921         app.killedByAm = false;
   5922 
   5923         mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
   5924 
   5925         boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
   5926         List<ProviderInfo> providers = normalMode ? generateApplicationProvidersLocked(app) : null;
   5927 
   5928         if (!normalMode) {
   5929             Slog.i(TAG, "Launching preboot mode app: " + app);
   5930         }
   5931 
   5932         if (localLOGV) Slog.v(
   5933             TAG, "New app record " + app
   5934             + " thread=" + thread.asBinder() + " pid=" + pid);
   5935         try {
   5936             int testMode = IApplicationThread.DEBUG_OFF;
   5937             if (mDebugApp != null && mDebugApp.equals(processName)) {
   5938                 testMode = mWaitForDebugger
   5939                     ? IApplicationThread.DEBUG_WAIT
   5940                     : IApplicationThread.DEBUG_ON;
   5941                 app.debugging = true;
   5942                 if (mDebugTransient) {
   5943                     mDebugApp = mOrigDebugApp;
   5944                     mWaitForDebugger = mOrigWaitForDebugger;
   5945                 }
   5946             }
   5947             String profileFile = app.instrumentationProfileFile;
   5948             ParcelFileDescriptor profileFd = null;
   5949             int samplingInterval = 0;
   5950             boolean profileAutoStop = false;
   5951             if (mProfileApp != null && mProfileApp.equals(processName)) {
   5952                 mProfileProc = app;
   5953                 profileFile = mProfileFile;
   5954                 profileFd = mProfileFd;
   5955                 samplingInterval = mSamplingInterval;
   5956                 profileAutoStop = mAutoStopProfiler;
   5957             }
   5958             boolean enableOpenGlTrace = false;
   5959             if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) {
   5960                 enableOpenGlTrace = true;
   5961                 mOpenGlTraceApp = null;
   5962             }
   5963 
   5964             // If the app is being launched for restore or full backup, set it up specially
   5965             boolean isRestrictedBackupMode = false;
   5966             if (mBackupTarget != null && mBackupAppName.equals(processName)) {
   5967                 isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
   5968                         || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL)
   5969                         || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
   5970             }
   5971 
   5972             ensurePackageDexOpt(app.instrumentationInfo != null
   5973                     ? app.instrumentationInfo.packageName
   5974                     : app.info.packageName);
   5975             if (app.instrumentationClass != null) {
   5976                 ensurePackageDexOpt(app.instrumentationClass.getPackageName());
   5977             }
   5978             if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
   5979                     + processName + " with config " + mConfiguration);
   5980             ApplicationInfo appInfo = app.instrumentationInfo != null
   5981                     ? app.instrumentationInfo : app.info;
   5982             app.compat = compatibilityInfoForPackageLocked(appInfo);
   5983             if (profileFd != null) {
   5984                 profileFd = profileFd.dup();
   5985             }
   5986             ProfilerInfo profilerInfo = profileFile == null ? null
   5987                     : new ProfilerInfo(profileFile, profileFd, samplingInterval, profileAutoStop);
   5988             thread.bindApplication(processName, appInfo, providers, app.instrumentationClass,
   5989                     profilerInfo, app.instrumentationArguments, app.instrumentationWatcher,
   5990                     app.instrumentationUiAutomationConnection, testMode, enableOpenGlTrace,
   5991                     isRestrictedBackupMode || !normalMode, app.persistent,
   5992                     new Configuration(mConfiguration), app.compat,
   5993                     getCommonServicesLocked(app.isolated),
   5994                     mCoreSettingsObserver.getCoreSettingsLocked());
   5995             updateLruProcessLocked(app, false, null);
   5996             app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
   5997         } catch (Exception e) {
   5998             // todo: Yikes!  What should we do?  For now we will try to
   5999             // start another process, but that could easily get us in
   6000             // an infinite loop of restarting processes...
   6001             Slog.wtf(TAG, "Exception thrown during bind of " + app, e);
   6002 
   6003             app.resetPackageList(mProcessStats);
   6004             app.unlinkDeathRecipient();
   6005             startProcessLocked(app, "bind fail", processName);
   6006             return false;
   6007         }
   6008 
   6009         // Remove this record from the list of starting applications.
   6010         mPersistentStartingProcesses.remove(app);
   6011         if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
   6012                 "Attach application locked removing on hold: " + app);
   6013         mProcessesOnHold.remove(app);
   6014 
   6015         boolean badApp = false;
   6016         boolean didSomething = false;
   6017 
   6018         // See if the top visible activity is waiting to run in this process...
   6019         if (normalMode) {
   6020             try {
   6021                 if (mStackSupervisor.attachApplicationLocked(app)) {
   6022                     didSomething = true;
   6023                 }
   6024             } catch (Exception e) {
   6025                 Slog.wtf(TAG, "Exception thrown launching activities in " + app, e);
   6026                 badApp = true;
   6027             }
   6028         }
   6029 
   6030         // Find any services that should be running in this process...
   6031         if (!badApp) {
   6032             try {
   6033                 didSomething |= mServices.attachApplicationLocked(app, processName);
   6034             } catch (Exception e) {
   6035                 Slog.wtf(TAG, "Exception thrown starting services in " + app, e);
   6036                 badApp = true;
   6037             }
   6038         }
   6039 
   6040         // Check if a next-broadcast receiver is in this process...
   6041         if (!badApp && isPendingBroadcastProcessLocked(pid)) {
   6042             try {
   6043                 didSomething |= sendPendingBroadcastsLocked(app);
   6044             } catch (Exception e) {
   6045                 // If the app died trying to launch the receiver we declare it 'bad'
   6046                 Slog.wtf(TAG, "Exception thrown dispatching broadcasts in " + app, e);
   6047                 badApp = true;
   6048             }
   6049         }
   6050 
   6051         // Check whether the next backup agent is in this process...
   6052         if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) {
   6053             if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
   6054             ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
   6055             try {
   6056                 thread.scheduleCreateBackupAgent(mBackupTarget.appInfo,
   6057                         compatibilityInfoForPackageLocked(mBackupTarget.appInfo),
   6058                         mBackupTarget.backupMode);
   6059             } catch (Exception e) {
   6060                 Slog.wtf(TAG, "Exception thrown creating backup agent in " + app, e);
   6061                 badApp = true;
   6062             }
   6063         }
   6064 
   6065         if (badApp) {
   6066             app.kill("error during init", true);
   6067             handleAppDiedLocked(app, false, true);
   6068             return false;
   6069         }
   6070 
   6071         if (!didSomething) {
   6072             updateOomAdjLocked();
   6073         }
   6074 
   6075         return true;
   6076     }
   6077 
   6078     @Override
   6079     public final void attachApplication(IApplicationThread thread) {
   6080         synchronized (this) {
   6081             int callingPid = Binder.getCallingPid();
   6082             final long origId = Binder.clearCallingIdentity();
   6083             attachApplicationLocked(thread, callingPid);
   6084             Binder.restoreCallingIdentity(origId);
   6085         }
   6086     }
   6087 
   6088     @Override
   6089     public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) {
   6090         final long origId = Binder.clearCallingIdentity();
   6091         synchronized (this) {
   6092             ActivityStack stack = ActivityRecord.getStackLocked(token);
   6093             if (stack != null) {
   6094                 ActivityRecord r =
   6095                         mStackSupervisor.activityIdleInternalLocked(token, false, config);
   6096                 if (stopProfiling) {
   6097                     if ((mProfileProc == r.app) && (mProfileFd != null)) {
   6098                         try {
   6099                             mProfileFd.close();
   6100                         } catch (IOException e) {
   6101                         }
   6102                         clearProfilerLocked();
   6103                     }
   6104                 }
   6105             }
   6106         }
   6107         Binder.restoreCallingIdentity(origId);
   6108     }
   6109 
   6110     void postFinishBooting(boolean finishBooting, boolean enableScreen) {
   6111         mHandler.sendMessage(mHandler.obtainMessage(FINISH_BOOTING_MSG,
   6112                 finishBooting? 1 : 0, enableScreen ? 1 : 0));
   6113     }
   6114 
   6115     void enableScreenAfterBoot() {
   6116         EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
   6117                 SystemClock.uptimeMillis());
   6118         mWindowManager.enableScreenAfterBoot();
   6119 
   6120         synchronized (this) {
   6121             updateEventDispatchingLocked();
   6122         }
   6123     }
   6124 
   6125     @Override
   6126     public void showBootMessage(final CharSequence msg, final boolean always) {
   6127         enforceNotIsolatedCaller("showBootMessage");
   6128         mWindowManager.showBootMessage(msg, always);
   6129     }
   6130 
   6131     @Override
   6132     public void keyguardWaitingForActivityDrawn() {
   6133         enforceNotIsolatedCaller("keyguardWaitingForActivityDrawn");
   6134         final long token = Binder.clearCallingIdentity();
   6135         try {
   6136             synchronized (this) {
   6137                 if (DEBUG_LOCKSCREEN) logLockScreen("");
   6138                 mWindowManager.keyguardWaitingForActivityDrawn();
   6139                 if (mLockScreenShown == LOCK_SCREEN_SHOWN) {
   6140                     mLockScreenShown = LOCK_SCREEN_LEAVING;
   6141                     updateSleepIfNeededLocked();
   6142                 }
   6143             }
   6144         } finally {
   6145             Binder.restoreCallingIdentity(token);
   6146         }
   6147     }
   6148 
   6149     final void finishBooting() {
   6150         synchronized (this) {
   6151             if (!mBootAnimationComplete) {
   6152                 mCallFinishBooting = true;
   6153                 return;
   6154             }
   6155             mCallFinishBooting = false;
   6156         }
   6157 
   6158         ArraySet<String> completedIsas = new ArraySet<String>();
   6159         for (String abi : Build.SUPPORTED_ABIS) {
   6160             Process.establishZygoteConnectionForAbi(abi);
   6161             final String instructionSet = VMRuntime.getInstructionSet(abi);
   6162             if (!completedIsas.contains(instructionSet)) {
   6163                 if (mInstaller.markBootComplete(VMRuntime.getInstructionSet(abi)) != 0) {
   6164                     Slog.e(TAG, "Unable to mark boot complete for abi: " + abi);
   6165                 }
   6166                 completedIsas.add(instructionSet);
   6167             }
   6168         }
   6169 
   6170         IntentFilter pkgFilter = new IntentFilter();
   6171         pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
   6172         pkgFilter.addDataScheme("package");
   6173         mContext.registerReceiver(new BroadcastReceiver() {
   6174             @Override
   6175             public void onReceive(Context context, Intent intent) {
   6176                 String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
   6177                 if (pkgs != null) {
   6178                     for (String pkg : pkgs) {
   6179                         synchronized (ActivityManagerService.this) {
   6180                             if (forceStopPackageLocked(pkg, -1, false, false, false, false, false,
   6181                                     0, "finished booting")) {
   6182                                 setResultCode(Activity.RESULT_OK);
   6183                                 return;
   6184                             }
   6185                         }
   6186                     }
   6187                 }
   6188             }
   6189         }, pkgFilter);
   6190 
   6191         // Let system services know.
   6192         mSystemServiceManager.startBootPhase(SystemService.PHASE_BOOT_COMPLETED);
   6193 
   6194         synchronized (this) {
   6195             // Ensure that any processes we had put on hold are now started
   6196             // up.
   6197             final int NP = mProcessesOnHold.size();
   6198             if (NP > 0) {
   6199                 ArrayList<ProcessRecord> procs =
   6200                     new ArrayList<ProcessRecord>(mProcessesOnHold);
   6201                 for (int ip=0; ip<NP; ip++) {
   6202                     if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: "
   6203                             + procs.get(ip));
   6204                     startProcessLocked(procs.get(ip), "on-hold", null);
   6205                 }
   6206             }
   6207 
   6208             if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
   6209                 // Start looking for apps that are abusing wake locks.
   6210                 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
   6211                 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
   6212                 // Tell anyone interested that we are done booting!
   6213                 SystemProperties.set("sys.boot_completed", "1");
   6214 
   6215                 // And trigger dev.bootcomplete if we are not showing encryption progress
   6216                 if (!"trigger_restart_min_framework".equals(SystemProperties.get("vold.decrypt"))
   6217                     || "".equals(SystemProperties.get("vold.encrypt_progress"))) {
   6218                     SystemProperties.set("dev.bootcomplete", "1");
   6219                 }
   6220                 for (int i=0; i<mStartedUsers.size(); i++) {
   6221                     UserStartedState uss = mStartedUsers.valueAt(i);
   6222                     if (uss.mState == UserStartedState.STATE_BOOTING) {
   6223                         uss.mState = UserStartedState.STATE_RUNNING;
   6224                         final int userId = mStartedUsers.keyAt(i);
   6225                         Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null);
   6226                         intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
   6227                         intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT);
   6228                         broadcastIntentLocked(null, null, intent, null,
   6229                                 new IIntentReceiver.Stub() {
   6230                                     @Override
   6231                                     public void performReceive(Intent intent, int resultCode,
   6232                                             String data, Bundle extras, boolean ordered,
   6233                                             boolean sticky, int sendingUser) {
   6234                                         synchronized (ActivityManagerService.this) {
   6235                                             requestPssAllProcsLocked(SystemClock.uptimeMillis(),
   6236                                                     true, false);
   6237                                         }
   6238                                     }
   6239                                 },
   6240                                 0, null, null,
   6241                                 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
   6242                                 AppOpsManager.OP_NONE, true, false, MY_PID, Process.SYSTEM_UID,
   6243                                 userId);
   6244                     }
   6245                 }
   6246                 scheduleStartProfilesLocked();
   6247             }
   6248         }
   6249     }
   6250 
   6251     @Override
   6252     public void bootAnimationComplete() {
   6253         final boolean callFinishBooting;
   6254         synchronized (this) {
   6255             callFinishBooting = mCallFinishBooting;
   6256             mBootAnimationComplete = true;
   6257         }
   6258         if (callFinishBooting) {
   6259             finishBooting();
   6260         }
   6261     }
   6262 
   6263     @Override
   6264     public void systemBackupRestored() {
   6265         synchronized (this) {
   6266             if (mSystemReady) {
   6267                 mTaskPersister.restoreTasksFromOtherDeviceLocked();
   6268             } else {
   6269                 Slog.w(TAG, "System backup restored before system is ready");
   6270             }
   6271         }
   6272     }
   6273 
   6274     final void ensureBootCompleted() {
   6275         boolean booting;
   6276         boolean enableScreen;
   6277         synchronized (this) {
   6278             booting = mBooting;
   6279             mBooting = false;
   6280             enableScreen = !mBooted;
   6281             mBooted = true;
   6282         }
   6283 
   6284         if (booting) {
   6285             finishBooting();
   6286         }
   6287 
   6288         if (enableScreen) {
   6289             enableScreenAfterBoot();
   6290         }
   6291     }
   6292 
   6293     @Override
   6294     public final void activityResumed(IBinder token) {
   6295         final long origId = Binder.clearCallingIdentity();
   6296         synchronized(this) {
   6297             ActivityStack stack = ActivityRecord.getStackLocked(token);
   6298             if (stack != null) {
   6299                 ActivityRecord.activityResumedLocked(token);
   6300             }
   6301         }
   6302         Binder.restoreCallingIdentity(origId);
   6303     }
   6304 
   6305     @Override
   6306     public final void activityPaused(IBinder token) {
   6307         final long origId = Binder.clearCallingIdentity();
   6308         synchronized(this) {
   6309             ActivityStack stack = ActivityRecord.getStackLocked(token);
   6310             if (stack != null) {
   6311                 stack.activityPausedLocked(token, false);
   6312             }
   6313         }
   6314         Binder.restoreCallingIdentity(origId);
   6315     }
   6316 
   6317     @Override
   6318     public final void activityStopped(IBinder token, Bundle icicle,
   6319             PersistableBundle persistentState, CharSequence description) {
   6320         if (localLOGV) Slog.v(TAG, "Activity stopped: token=" + token);
   6321 
   6322         // Refuse possible leaked file descriptors
   6323         if (icicle != null && icicle.hasFileDescriptors()) {
   6324             throw new IllegalArgumentException("File descriptors passed in Bundle");
   6325         }
   6326 
   6327         final long origId = Binder.clearCallingIdentity();
   6328 
   6329         synchronized (this) {
   6330             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   6331             if (r != null) {
   6332                 r.task.stack.activityStoppedLocked(r, icicle, persistentState, description);
   6333             }
   6334         }
   6335 
   6336         trimApplications();
   6337 
   6338         Binder.restoreCallingIdentity(origId);
   6339     }
   6340 
   6341     @Override
   6342     public final void activityDestroyed(IBinder token) {
   6343         if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
   6344         synchronized (this) {
   6345             ActivityStack stack = ActivityRecord.getStackLocked(token);
   6346             if (stack != null) {
   6347                 stack.activityDestroyedLocked(token, "activityDestroyed");
   6348             }
   6349         }
   6350     }
   6351 
   6352     @Override
   6353     public final void backgroundResourcesReleased(IBinder token) {
   6354         final long origId = Binder.clearCallingIdentity();
   6355         try {
   6356             synchronized (this) {
   6357                 ActivityStack stack = ActivityRecord.getStackLocked(token);
   6358                 if (stack != null) {
   6359                     stack.backgroundResourcesReleased();
   6360                 }
   6361             }
   6362         } finally {
   6363             Binder.restoreCallingIdentity(origId);
   6364         }
   6365     }
   6366 
   6367     @Override
   6368     public final void notifyLaunchTaskBehindComplete(IBinder token) {
   6369         mStackSupervisor.scheduleLaunchTaskBehindComplete(token);
   6370     }
   6371 
   6372     @Override
   6373     public final void notifyEnterAnimationComplete(IBinder token) {
   6374         mHandler.sendMessage(mHandler.obtainMessage(ENTER_ANIMATION_COMPLETE_MSG, token));
   6375     }
   6376 
   6377     @Override
   6378     public String getCallingPackage(IBinder token) {
   6379         synchronized (this) {
   6380             ActivityRecord r = getCallingRecordLocked(token);
   6381             return r != null ? r.info.packageName : null;
   6382         }
   6383     }
   6384 
   6385     @Override
   6386     public ComponentName getCallingActivity(IBinder token) {
   6387         synchronized (this) {
   6388             ActivityRecord r = getCallingRecordLocked(token);
   6389             return r != null ? r.intent.getComponent() : null;
   6390         }
   6391     }
   6392 
   6393     private ActivityRecord getCallingRecordLocked(IBinder token) {
   6394         ActivityRecord r = ActivityRecord.isInStackLocked(token);
   6395         if (r == null) {
   6396             return null;
   6397         }
   6398         return r.resultTo;
   6399     }
   6400 
   6401     @Override
   6402     public ComponentName getActivityClassForToken(IBinder token) {
   6403         synchronized(this) {
   6404             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   6405             if (r == null) {
   6406                 return null;
   6407             }
   6408             return r.intent.getComponent();
   6409         }
   6410     }
   6411 
   6412     @Override
   6413     public String getPackageForToken(IBinder token) {
   6414         synchronized(this) {
   6415             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   6416             if (r == null) {
   6417                 return null;
   6418             }
   6419             return r.packageName;
   6420         }
   6421     }
   6422 
   6423     @Override
   6424     public IIntentSender getIntentSender(int type,
   6425             String packageName, IBinder token, String resultWho,
   6426             int requestCode, Intent[] intents, String[] resolvedTypes,
   6427             int flags, Bundle options, int userId) {
   6428         enforceNotIsolatedCaller("getIntentSender");
   6429         // Refuse possible leaked file descriptors
   6430         if (intents != null) {
   6431             if (intents.length < 1) {
   6432                 throw new IllegalArgumentException("Intents array length must be >= 1");
   6433             }
   6434             for (int i=0; i<intents.length; i++) {
   6435                 Intent intent = intents[i];
   6436                 if (intent != null) {
   6437                     if (intent.hasFileDescriptors()) {
   6438                         throw new IllegalArgumentException("File descriptors passed in Intent");
   6439                     }
   6440                     if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
   6441                             (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
   6442                         throw new IllegalArgumentException(
   6443                                 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
   6444                     }
   6445                     intents[i] = new Intent(intent);
   6446                 }
   6447             }
   6448             if (resolvedTypes != null && resolvedTypes.length != intents.length) {
   6449                 throw new IllegalArgumentException(
   6450                         "Intent array length does not match resolvedTypes length");
   6451             }
   6452         }
   6453         if (options != null) {
   6454             if (options.hasFileDescriptors()) {
   6455                 throw new IllegalArgumentException("File descriptors passed in options");
   6456             }
   6457         }
   6458 
   6459         synchronized(this) {
   6460             int callingUid = Binder.getCallingUid();
   6461             int origUserId = userId;
   6462             userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId,
   6463                     type == ActivityManager.INTENT_SENDER_BROADCAST,
   6464                     ALLOW_NON_FULL, "getIntentSender", null);
   6465             if (origUserId == UserHandle.USER_CURRENT) {
   6466                 // We don't want to evaluate this until the pending intent is
   6467                 // actually executed.  However, we do want to always do the
   6468                 // security checking for it above.
   6469                 userId = UserHandle.USER_CURRENT;
   6470             }
   6471             try {
   6472                 if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
   6473                     int uid = AppGlobals.getPackageManager()
   6474                             .getPackageUid(packageName, UserHandle.getUserId(callingUid));
   6475                     if (!UserHandle.isSameApp(callingUid, uid)) {
   6476                         String msg = "Permission Denial: getIntentSender() from pid="
   6477                             + Binder.getCallingPid()
   6478                             + ", uid=" + Binder.getCallingUid()
   6479                             + ", (need uid=" + uid + ")"
   6480                             + " is not allowed to send as package " + packageName;
   6481                         Slog.w(TAG, msg);
   6482                         throw new SecurityException(msg);
   6483                     }
   6484                 }
   6485 
   6486                 return getIntentSenderLocked(type, packageName, callingUid, userId,
   6487                         token, resultWho, requestCode, intents, resolvedTypes, flags, options);
   6488 
   6489             } catch (RemoteException e) {
   6490                 throw new SecurityException(e);
   6491             }
   6492         }
   6493     }
   6494 
   6495     IIntentSender getIntentSenderLocked(int type, String packageName,
   6496             int callingUid, int userId, IBinder token, String resultWho,
   6497             int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
   6498             Bundle options) {
   6499         if (DEBUG_MU)
   6500             Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid);
   6501         ActivityRecord activity = null;
   6502         if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
   6503             activity = ActivityRecord.isInStackLocked(token);
   6504             if (activity == null) {
   6505                 return null;
   6506             }
   6507             if (activity.finishing) {
   6508                 return null;
   6509             }
   6510         }
   6511 
   6512         final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
   6513         final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
   6514         final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
   6515         flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
   6516                 |PendingIntent.FLAG_UPDATE_CURRENT);
   6517 
   6518         PendingIntentRecord.Key key = new PendingIntentRecord.Key(
   6519                 type, packageName, activity, resultWho,
   6520                 requestCode, intents, resolvedTypes, flags, options, userId);
   6521         WeakReference<PendingIntentRecord> ref;
   6522         ref = mIntentSenderRecords.get(key);
   6523         PendingIntentRecord rec = ref != null ? ref.get() : null;
   6524         if (rec != null) {
   6525             if (!cancelCurrent) {
   6526                 if (updateCurrent) {
   6527                     if (rec.key.requestIntent != null) {
   6528                         rec.key.requestIntent.replaceExtras(intents != null ?
   6529                                 intents[intents.length - 1] : null);
   6530                     }
   6531                     if (intents != null) {
   6532                         intents[intents.length-1] = rec.key.requestIntent;
   6533                         rec.key.allIntents = intents;
   6534                         rec.key.allResolvedTypes = resolvedTypes;
   6535                     } else {
   6536                         rec.key.allIntents = null;
   6537                         rec.key.allResolvedTypes = null;
   6538                     }
   6539                 }
   6540                 return rec;
   6541             }
   6542             rec.canceled = true;
   6543             mIntentSenderRecords.remove(key);
   6544         }
   6545         if (noCreate) {
   6546             return rec;
   6547         }
   6548         rec = new PendingIntentRecord(this, key, callingUid);
   6549         mIntentSenderRecords.put(key, rec.ref);
   6550         if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
   6551             if (activity.pendingResults == null) {
   6552                 activity.pendingResults
   6553                         = new HashSet<WeakReference<PendingIntentRecord>>();
   6554             }
   6555             activity.pendingResults.add(rec.ref);
   6556         }
   6557         return rec;
   6558     }
   6559 
   6560     @Override
   6561     public void cancelIntentSender(IIntentSender sender) {
   6562         if (!(sender instanceof PendingIntentRecord)) {
   6563             return;
   6564         }
   6565         synchronized(this) {
   6566             PendingIntentRecord rec = (PendingIntentRecord)sender;
   6567             try {
   6568                 int uid = AppGlobals.getPackageManager()
   6569                         .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId());
   6570                 if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) {
   6571                     String msg = "Permission Denial: cancelIntentSender() from pid="
   6572                         + Binder.getCallingPid()
   6573                         + ", uid=" + Binder.getCallingUid()
   6574                         + " is not allowed to cancel packges "
   6575                         + rec.key.packageName;
   6576                     Slog.w(TAG, msg);
   6577                     throw new SecurityException(msg);
   6578                 }
   6579             } catch (RemoteException e) {
   6580                 throw new SecurityException(e);
   6581             }
   6582             cancelIntentSenderLocked(rec, true);
   6583         }
   6584     }
   6585 
   6586     void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
   6587         rec.canceled = true;
   6588         mIntentSenderRecords.remove(rec.key);
   6589         if (cleanActivity && rec.key.activity != null) {
   6590             rec.key.activity.pendingResults.remove(rec.ref);
   6591         }
   6592     }
   6593 
   6594     @Override
   6595     public String getPackageForIntentSender(IIntentSender pendingResult) {
   6596         if (!(pendingResult instanceof PendingIntentRecord)) {
   6597             return null;
   6598         }
   6599         try {
   6600             PendingIntentRecord res = (PendingIntentRecord)pendingResult;
   6601             return res.key.packageName;
   6602         } catch (ClassCastException e) {
   6603         }
   6604         return null;
   6605     }
   6606 
   6607     @Override
   6608     public int getUidForIntentSender(IIntentSender sender) {
   6609         if (sender instanceof PendingIntentRecord) {
   6610             try {
   6611                 PendingIntentRecord res = (PendingIntentRecord)sender;
   6612                 return res.uid;
   6613             } catch (ClassCastException e) {
   6614             }
   6615         }
   6616         return -1;
   6617     }
   6618 
   6619     @Override
   6620     public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) {
   6621         if (!(pendingResult instanceof PendingIntentRecord)) {
   6622             return false;
   6623         }
   6624         try {
   6625             PendingIntentRecord res = (PendingIntentRecord)pendingResult;
   6626             if (res.key.allIntents == null) {
   6627                 return false;
   6628             }
   6629             for (int i=0; i<res.key.allIntents.length; i++) {
   6630                 Intent intent = res.key.allIntents[i];
   6631                 if (intent.getPackage() != null && intent.getComponent() != null) {
   6632                     return false;
   6633                 }
   6634             }
   6635             return true;
   6636         } catch (ClassCastException e) {
   6637         }
   6638         return false;
   6639     }
   6640 
   6641     @Override
   6642     public boolean isIntentSenderAnActivity(IIntentSender pendingResult) {
   6643         if (!(pendingResult instanceof PendingIntentRecord)) {
   6644             return false;
   6645         }
   6646         try {
   6647             PendingIntentRecord res = (PendingIntentRecord)pendingResult;
   6648             if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) {
   6649                 return true;
   6650             }
   6651             return false;
   6652         } catch (ClassCastException e) {
   6653         }
   6654         return false;
   6655     }
   6656 
   6657     @Override
   6658     public Intent getIntentForIntentSender(IIntentSender pendingResult) {
   6659         if (!(pendingResult instanceof PendingIntentRecord)) {
   6660             return null;
   6661         }
   6662         try {
   6663             PendingIntentRecord res = (PendingIntentRecord)pendingResult;
   6664             return res.key.requestIntent != null ? new Intent(res.key.requestIntent) : null;
   6665         } catch (ClassCastException e) {
   6666         }
   6667         return null;
   6668     }
   6669 
   6670     @Override
   6671     public String getTagForIntentSender(IIntentSender pendingResult, String prefix) {
   6672         if (!(pendingResult instanceof PendingIntentRecord)) {
   6673             return null;
   6674         }
   6675         try {
   6676             PendingIntentRecord res = (PendingIntentRecord)pendingResult;
   6677             Intent intent = res.key.requestIntent;
   6678             if (intent != null) {
   6679                 if (res.lastTag != null && res.lastTagPrefix == prefix && (res.lastTagPrefix == null
   6680                         || res.lastTagPrefix.equals(prefix))) {
   6681                     return res.lastTag;
   6682                 }
   6683                 res.lastTagPrefix = prefix;
   6684                 StringBuilder sb = new StringBuilder(128);
   6685                 if (prefix != null) {
   6686                     sb.append(prefix);
   6687                 }
   6688                 if (intent.getAction() != null) {
   6689                     sb.append(intent.getAction());
   6690                 } else if (intent.getComponent() != null) {
   6691                     intent.getComponent().appendShortString(sb);
   6692                 } else {
   6693                     sb.append("?");
   6694                 }
   6695                 return res.lastTag = sb.toString();
   6696             }
   6697         } catch (ClassCastException e) {
   6698         }
   6699         return null;
   6700     }
   6701 
   6702     @Override
   6703     public void setProcessLimit(int max) {
   6704         enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
   6705                 "setProcessLimit()");
   6706         synchronized (this) {
   6707             mProcessLimit = max < 0 ? ProcessList.MAX_CACHED_APPS : max;
   6708             mProcessLimitOverride = max;
   6709         }
   6710         trimApplications();
   6711     }
   6712 
   6713     @Override
   6714     public int getProcessLimit() {
   6715         synchronized (this) {
   6716             return mProcessLimitOverride;
   6717         }
   6718     }
   6719 
   6720     void foregroundTokenDied(ForegroundToken token) {
   6721         synchronized (ActivityManagerService.this) {
   6722             synchronized (mPidsSelfLocked) {
   6723                 ForegroundToken cur
   6724                     = mForegroundProcesses.get(token.pid);
   6725                 if (cur != token) {
   6726                     return;
   6727                 }
   6728                 mForegroundProcesses.remove(token.pid);
   6729                 ProcessRecord pr = mPidsSelfLocked.get(token.pid);
   6730                 if (pr == null) {
   6731                     return;
   6732                 }
   6733                 pr.forcingToForeground = null;
   6734                 updateProcessForegroundLocked(pr, false, false);
   6735             }
   6736             updateOomAdjLocked();
   6737         }
   6738     }
   6739 
   6740     @Override
   6741     public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
   6742         enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
   6743                 "setProcessForeground()");
   6744         synchronized(this) {
   6745             boolean changed = false;
   6746 
   6747             synchronized (mPidsSelfLocked) {
   6748                 ProcessRecord pr = mPidsSelfLocked.get(pid);
   6749                 if (pr == null && isForeground) {
   6750                     Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
   6751                     return;
   6752                 }
   6753                 ForegroundToken oldToken = mForegroundProcesses.get(pid);
   6754                 if (oldToken != null) {
   6755                     oldToken.token.unlinkToDeath(oldToken, 0);
   6756                     mForegroundProcesses.remove(pid);
   6757                     if (pr != null) {
   6758                         pr.forcingToForeground = null;
   6759                     }
   6760                     changed = true;
   6761                 }
   6762                 if (isForeground && token != null) {
   6763                     ForegroundToken newToken = new ForegroundToken() {
   6764                         @Override
   6765                         public void binderDied() {
   6766                             foregroundTokenDied(this);
   6767                         }
   6768                     };
   6769                     newToken.pid = pid;
   6770                     newToken.token = token;
   6771                     try {
   6772                         token.linkToDeath(newToken, 0);
   6773                         mForegroundProcesses.put(pid, newToken);
   6774                         pr.forcingToForeground = token;
   6775                         changed = true;
   6776                     } catch (RemoteException e) {
   6777                         // If the process died while doing this, we will later
   6778                         // do the cleanup with the process death link.
   6779                     }
   6780                 }
   6781             }
   6782 
   6783             if (changed) {
   6784                 updateOomAdjLocked();
   6785             }
   6786         }
   6787     }
   6788 
   6789     // =========================================================
   6790     // PERMISSIONS
   6791     // =========================================================
   6792 
   6793     static class PermissionController extends IPermissionController.Stub {
   6794         ActivityManagerService mActivityManagerService;
   6795         PermissionController(ActivityManagerService activityManagerService) {
   6796             mActivityManagerService = activityManagerService;
   6797         }
   6798 
   6799         @Override
   6800         public boolean checkPermission(String permission, int pid, int uid) {
   6801             return mActivityManagerService.checkPermission(permission, pid,
   6802                     uid) == PackageManager.PERMISSION_GRANTED;
   6803         }
   6804     }
   6805 
   6806     class IntentFirewallInterface implements IntentFirewall.AMSInterface {
   6807         @Override
   6808         public int checkComponentPermission(String permission, int pid, int uid,
   6809                 int owningUid, boolean exported) {
   6810             return ActivityManagerService.this.checkComponentPermission(permission, pid, uid,
   6811                     owningUid, exported);
   6812         }
   6813 
   6814         @Override
   6815         public Object getAMSLock() {
   6816             return ActivityManagerService.this;
   6817         }
   6818     }
   6819 
   6820     /**
   6821      * This can be called with or without the global lock held.
   6822      */
   6823     int checkComponentPermission(String permission, int pid, int uid,
   6824             int owningUid, boolean exported) {
   6825         if (pid == MY_PID) {
   6826             return PackageManager.PERMISSION_GRANTED;
   6827         }
   6828         return ActivityManager.checkComponentPermission(permission, uid,
   6829                 owningUid, exported);
   6830     }
   6831 
   6832     /**
   6833      * As the only public entry point for permissions checking, this method
   6834      * can enforce the semantic that requesting a check on a null global
   6835      * permission is automatically denied.  (Internally a null permission
   6836      * string is used when calling {@link #checkComponentPermission} in cases
   6837      * when only uid-based security is needed.)
   6838      *
   6839      * This can be called with or without the global lock held.
   6840      */
   6841     @Override
   6842     public int checkPermission(String permission, int pid, int uid) {
   6843         if (permission == null) {
   6844             return PackageManager.PERMISSION_DENIED;
   6845         }
   6846         return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true);
   6847     }
   6848 
   6849     @Override
   6850     public int checkPermissionWithToken(String permission, int pid, int uid, IBinder callerToken) {
   6851         if (permission == null) {
   6852             return PackageManager.PERMISSION_DENIED;
   6853         }
   6854 
   6855         // We might be performing an operation on behalf of an indirect binder
   6856         // invocation, e.g. via {@link #openContentUri}.  Check and adjust the
   6857         // client identity accordingly before proceeding.
   6858         Identity tlsIdentity = sCallerIdentity.get();
   6859         if (tlsIdentity != null && tlsIdentity.token == callerToken) {
   6860             Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
   6861                     + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
   6862             uid = tlsIdentity.uid;
   6863             pid = tlsIdentity.pid;
   6864         }
   6865 
   6866         return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true);
   6867     }
   6868 
   6869     /**
   6870      * Binder IPC calls go through the public entry point.
   6871      * This can be called with or without the global lock held.
   6872      */
   6873     int checkCallingPermission(String permission) {
   6874         return checkPermission(permission,
   6875                 Binder.getCallingPid(),
   6876                 UserHandle.getAppId(Binder.getCallingUid()));
   6877     }
   6878 
   6879     /**
   6880      * This can be called with or without the global lock held.
   6881      */
   6882     void enforceCallingPermission(String permission, String func) {
   6883         if (checkCallingPermission(permission)
   6884                 == PackageManager.PERMISSION_GRANTED) {
   6885             return;
   6886         }
   6887 
   6888         String msg = "Permission Denial: " + func + " from pid="
   6889                 + Binder.getCallingPid()
   6890                 + ", uid=" + Binder.getCallingUid()
   6891                 + " requires " + permission;
   6892         Slog.w(TAG, msg);
   6893         throw new SecurityException(msg);
   6894     }
   6895 
   6896     /**
   6897      * Determine if UID is holding permissions required to access {@link Uri} in
   6898      * the given {@link ProviderInfo}. Final permission checking is always done
   6899      * in {@link ContentProvider}.
   6900      */
   6901     private final boolean checkHoldingPermissionsLocked(
   6902             IPackageManager pm, ProviderInfo pi, GrantUri grantUri, int uid, final int modeFlags) {
   6903         if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   6904                 "checkHoldingPermissionsLocked: uri=" + grantUri + " uid=" + uid);
   6905         if (UserHandle.getUserId(uid) != grantUri.sourceUserId) {
   6906             if (ActivityManager.checkComponentPermission(INTERACT_ACROSS_USERS, uid, -1, true)
   6907                     != PERMISSION_GRANTED) {
   6908                 return false;
   6909             }
   6910         }
   6911         return checkHoldingPermissionsInternalLocked(pm, pi, grantUri, uid, modeFlags, true);
   6912     }
   6913 
   6914     private final boolean checkHoldingPermissionsInternalLocked(IPackageManager pm, ProviderInfo pi,
   6915             GrantUri grantUri, int uid, final int modeFlags, boolean considerUidPermissions) {
   6916         if (pi.applicationInfo.uid == uid) {
   6917             return true;
   6918         } else if (!pi.exported) {
   6919             return false;
   6920         }
   6921 
   6922         boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0;
   6923         boolean writeMet = (modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
   6924         try {
   6925             // check if target holds top-level <provider> permissions
   6926             if (!readMet && pi.readPermission != null && considerUidPermissions
   6927                     && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) {
   6928                 readMet = true;
   6929             }
   6930             if (!writeMet && pi.writePermission != null && considerUidPermissions
   6931                     && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) {
   6932                 writeMet = true;
   6933             }
   6934 
   6935             // track if unprotected read/write is allowed; any denied
   6936             // <path-permission> below removes this ability
   6937             boolean allowDefaultRead = pi.readPermission == null;
   6938             boolean allowDefaultWrite = pi.writePermission == null;
   6939 
   6940             // check if target holds any <path-permission> that match uri
   6941             final PathPermission[] pps = pi.pathPermissions;
   6942             if (pps != null) {
   6943                 final String path = grantUri.uri.getPath();
   6944                 int i = pps.length;
   6945                 while (i > 0 && (!readMet || !writeMet)) {
   6946                     i--;
   6947                     PathPermission pp = pps[i];
   6948                     if (pp.match(path)) {
   6949                         if (!readMet) {
   6950                             final String pprperm = pp.getReadPermission();
   6951                             if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for "
   6952                                     + pprperm + " for " + pp.getPath()
   6953                                     + ": match=" + pp.match(path)
   6954                                     + " check=" + pm.checkUidPermission(pprperm, uid));
   6955                             if (pprperm != null) {
   6956                                 if (considerUidPermissions && pm.checkUidPermission(pprperm, uid)
   6957                                         == PERMISSION_GRANTED) {
   6958                                     readMet = true;
   6959                                 } else {
   6960                                     allowDefaultRead = false;
   6961                                 }
   6962                             }
   6963                         }
   6964                         if (!writeMet) {
   6965                             final String ppwperm = pp.getWritePermission();
   6966                             if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm "
   6967                                     + ppwperm + " for " + pp.getPath()
   6968                                     + ": match=" + pp.match(path)
   6969                                     + " check=" + pm.checkUidPermission(ppwperm, uid));
   6970                             if (ppwperm != null) {
   6971                                 if (considerUidPermissions && pm.checkUidPermission(ppwperm, uid)
   6972                                         == PERMISSION_GRANTED) {
   6973                                     writeMet = true;
   6974                                 } else {
   6975                                     allowDefaultWrite = false;
   6976                                 }
   6977                             }
   6978                         }
   6979                     }
   6980                 }
   6981             }
   6982 
   6983             // grant unprotected <provider> read/write, if not blocked by
   6984             // <path-permission> above
   6985             if (allowDefaultRead) readMet = true;
   6986             if (allowDefaultWrite) writeMet = true;
   6987 
   6988         } catch (RemoteException e) {
   6989             return false;
   6990         }
   6991 
   6992         return readMet && writeMet;
   6993     }
   6994 
   6995     private ProviderInfo getProviderInfoLocked(String authority, int userHandle) {
   6996         ProviderInfo pi = null;
   6997         ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userHandle);
   6998         if (cpr != null) {
   6999             pi = cpr.info;
   7000         } else {
   7001             try {
   7002                 pi = AppGlobals.getPackageManager().resolveContentProvider(
   7003                         authority, PackageManager.GET_URI_PERMISSION_PATTERNS, userHandle);
   7004             } catch (RemoteException ex) {
   7005             }
   7006         }
   7007         return pi;
   7008     }
   7009 
   7010     private UriPermission findUriPermissionLocked(int targetUid, GrantUri grantUri) {
   7011         final ArrayMap<GrantUri, UriPermission> targetUris = mGrantedUriPermissions.get(targetUid);
   7012         if (targetUris != null) {
   7013             return targetUris.get(grantUri);
   7014         }
   7015         return null;
   7016     }
   7017 
   7018     private UriPermission findOrCreateUriPermissionLocked(String sourcePkg,
   7019             String targetPkg, int targetUid, GrantUri grantUri) {
   7020         ArrayMap<GrantUri, UriPermission> targetUris = mGrantedUriPermissions.get(targetUid);
   7021         if (targetUris == null) {
   7022             targetUris = Maps.newArrayMap();
   7023             mGrantedUriPermissions.put(targetUid, targetUris);
   7024         }
   7025 
   7026         UriPermission perm = targetUris.get(grantUri);
   7027         if (perm == null) {
   7028             perm = new UriPermission(sourcePkg, targetPkg, targetUid, grantUri);
   7029             targetUris.put(grantUri, perm);
   7030         }
   7031 
   7032         return perm;
   7033     }
   7034 
   7035     private final boolean checkUriPermissionLocked(GrantUri grantUri, int uid,
   7036             final int modeFlags) {
   7037         final boolean persistable = (modeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0;
   7038         final int minStrength = persistable ? UriPermission.STRENGTH_PERSISTABLE
   7039                 : UriPermission.STRENGTH_OWNED;
   7040 
   7041         // Root gets to do everything.
   7042         if (uid == 0) {
   7043             return true;
   7044         }
   7045 
   7046         final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(uid);
   7047         if (perms == null) return false;
   7048 
   7049         // First look for exact match
   7050         final UriPermission exactPerm = perms.get(grantUri);
   7051         if (exactPerm != null && exactPerm.getStrength(modeFlags) >= minStrength) {
   7052             return true;
   7053         }
   7054 
   7055         // No exact match, look for prefixes
   7056         final int N = perms.size();
   7057         for (int i = 0; i < N; i++) {
   7058             final UriPermission perm = perms.valueAt(i);
   7059             if (perm.uri.prefix && grantUri.uri.isPathPrefixMatch(perm.uri.uri)
   7060                     && perm.getStrength(modeFlags) >= minStrength) {
   7061                 return true;
   7062             }
   7063         }
   7064 
   7065         return false;
   7066     }
   7067 
   7068     /**
   7069      * @param uri This uri must NOT contain an embedded userId.
   7070      * @param userId The userId in which the uri is to be resolved.
   7071      */
   7072     @Override
   7073     public int checkUriPermission(Uri uri, int pid, int uid,
   7074             final int modeFlags, int userId, IBinder callerToken) {
   7075         enforceNotIsolatedCaller("checkUriPermission");
   7076 
   7077         // Another redirected-binder-call permissions check as in
   7078         // {@link checkPermissionWithToken}.
   7079         Identity tlsIdentity = sCallerIdentity.get();
   7080         if (tlsIdentity != null && tlsIdentity.token == callerToken) {
   7081             uid = tlsIdentity.uid;
   7082             pid = tlsIdentity.pid;
   7083         }
   7084 
   7085         // Our own process gets to do everything.
   7086         if (pid == MY_PID) {
   7087             return PackageManager.PERMISSION_GRANTED;
   7088         }
   7089         synchronized (this) {
   7090             return checkUriPermissionLocked(new GrantUri(userId, uri, false), uid, modeFlags)
   7091                     ? PackageManager.PERMISSION_GRANTED
   7092                     : PackageManager.PERMISSION_DENIED;
   7093         }
   7094     }
   7095 
   7096     /**
   7097      * Check if the targetPkg can be granted permission to access uri by
   7098      * the callingUid using the given modeFlags.  Throws a security exception
   7099      * if callingUid is not allowed to do this.  Returns the uid of the target
   7100      * if the URI permission grant should be performed; returns -1 if it is not
   7101      * needed (for example targetPkg already has permission to access the URI).
   7102      * If you already know the uid of the target, you can supply it in
   7103      * lastTargetUid else set that to -1.
   7104      */
   7105     int checkGrantUriPermissionLocked(int callingUid, String targetPkg, GrantUri grantUri,
   7106             final int modeFlags, int lastTargetUid) {
   7107         if (!Intent.isAccessUriMode(modeFlags)) {
   7108             return -1;
   7109         }
   7110 
   7111         if (targetPkg != null) {
   7112             if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   7113                     "Checking grant " + targetPkg + " permission to " + grantUri);
   7114         }
   7115 
   7116         final IPackageManager pm = AppGlobals.getPackageManager();
   7117 
   7118         // If this is not a content: uri, we can't do anything with it.
   7119         if (!ContentResolver.SCHEME_CONTENT.equals(grantUri.uri.getScheme())) {
   7120             if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   7121                     "Can't grant URI permission for non-content URI: " + grantUri);
   7122             return -1;
   7123         }
   7124 
   7125         final String authority = grantUri.uri.getAuthority();
   7126         final ProviderInfo pi = getProviderInfoLocked(authority, grantUri.sourceUserId);
   7127         if (pi == null) {
   7128             Slog.w(TAG, "No content provider found for permission check: " +
   7129                     grantUri.uri.toSafeString());
   7130             return -1;
   7131         }
   7132 
   7133         int targetUid = lastTargetUid;
   7134         if (targetUid < 0 && targetPkg != null) {
   7135             try {
   7136                 targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid));
   7137                 if (targetUid < 0) {
   7138                     if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   7139                             "Can't grant URI permission no uid for: " + targetPkg);
   7140                     return -1;
   7141                 }
   7142             } catch (RemoteException ex) {
   7143                 return -1;
   7144             }
   7145         }
   7146 
   7147         if (targetUid >= 0) {
   7148             // First...  does the target actually need this permission?
   7149             if (checkHoldingPermissionsLocked(pm, pi, grantUri, targetUid, modeFlags)) {
   7150                 // No need to grant the target this permission.
   7151                 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   7152                         "Target " + targetPkg + " already has full permission to " + grantUri);
   7153                 return -1;
   7154             }
   7155         } else {
   7156             // First...  there is no target package, so can anyone access it?
   7157             boolean allowed = pi.exported;
   7158             if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
   7159                 if (pi.readPermission != null) {
   7160                     allowed = false;
   7161                 }
   7162             }
   7163             if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
   7164                 if (pi.writePermission != null) {
   7165                     allowed = false;
   7166                 }
   7167             }
   7168             if (allowed) {
   7169                 return -1;
   7170             }
   7171         }
   7172 
   7173         /* There is a special cross user grant if:
   7174          * - The target is on another user.
   7175          * - Apps on the current user can access the uri without any uid permissions.
   7176          * In this case, we grant a uri permission, even if the ContentProvider does not normally
   7177          * grant uri permissions.
   7178          */
   7179         boolean specialCrossUserGrant = UserHandle.getUserId(targetUid) != grantUri.sourceUserId
   7180                 && checkHoldingPermissionsInternalLocked(pm, pi, grantUri, callingUid,
   7181                 modeFlags, false /*without considering the uid permissions*/);
   7182 
   7183         // Second...  is the provider allowing granting of URI permissions?
   7184         if (!specialCrossUserGrant) {
   7185             if (!pi.grantUriPermissions) {
   7186                 throw new SecurityException("Provider " + pi.packageName
   7187                         + "/" + pi.name
   7188                         + " does not allow granting of Uri permissions (uri "
   7189                         + grantUri + ")");
   7190             }
   7191             if (pi.uriPermissionPatterns != null) {
   7192                 final int N = pi.uriPermissionPatterns.length;
   7193                 boolean allowed = false;
   7194                 for (int i=0; i<N; i++) {
   7195                     if (pi.uriPermissionPatterns[i] != null
   7196                             && pi.uriPermissionPatterns[i].match(grantUri.uri.getPath())) {
   7197                         allowed = true;
   7198                         break;
   7199                     }
   7200                 }
   7201                 if (!allowed) {
   7202                     throw new SecurityException("Provider " + pi.packageName
   7203                             + "/" + pi.name
   7204                             + " does not allow granting of permission to path of Uri "
   7205                             + grantUri);
   7206                 }
   7207             }
   7208         }
   7209 
   7210         // Third...  does the caller itself have permission to access
   7211         // this uri?
   7212         if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) {
   7213             if (!checkHoldingPermissionsLocked(pm, pi, grantUri, callingUid, modeFlags)) {
   7214                 // Require they hold a strong enough Uri permission
   7215                 if (!checkUriPermissionLocked(grantUri, callingUid, modeFlags)) {
   7216                     throw new SecurityException("Uid " + callingUid
   7217                             + " does not have permission to uri " + grantUri);
   7218                 }
   7219             }
   7220         }
   7221         return targetUid;
   7222     }
   7223 
   7224     /**
   7225      * @param uri This uri must NOT contain an embedded userId.
   7226      * @param userId The userId in which the uri is to be resolved.
   7227      */
   7228     @Override
   7229     public int checkGrantUriPermission(int callingUid, String targetPkg, Uri uri,
   7230             final int modeFlags, int userId) {
   7231         enforceNotIsolatedCaller("checkGrantUriPermission");
   7232         synchronized(this) {
   7233             return checkGrantUriPermissionLocked(callingUid, targetPkg,
   7234                     new GrantUri(userId, uri, false), modeFlags, -1);
   7235         }
   7236     }
   7237 
   7238     void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, GrantUri grantUri,
   7239             final int modeFlags, UriPermissionOwner owner) {
   7240         if (!Intent.isAccessUriMode(modeFlags)) {
   7241             return;
   7242         }
   7243 
   7244         // So here we are: the caller has the assumed permission
   7245         // to the uri, and the target doesn't.  Let's now give this to
   7246         // the target.
   7247 
   7248         if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   7249                 "Granting " + targetPkg + "/" + targetUid + " permission to " + grantUri);
   7250 
   7251         final String authority = grantUri.uri.getAuthority();
   7252         final ProviderInfo pi = getProviderInfoLocked(authority, grantUri.sourceUserId);
   7253         if (pi == null) {
   7254             Slog.w(TAG, "No content provider found for grant: " + grantUri.toSafeString());
   7255             return;
   7256         }
   7257 
   7258         if ((modeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
   7259             grantUri.prefix = true;
   7260         }
   7261         final UriPermission perm = findOrCreateUriPermissionLocked(
   7262                 pi.packageName, targetPkg, targetUid, grantUri);
   7263         perm.grantModes(modeFlags, owner);
   7264     }
   7265 
   7266     void grantUriPermissionLocked(int callingUid, String targetPkg, GrantUri grantUri,
   7267             final int modeFlags, UriPermissionOwner owner, int targetUserId) {
   7268         if (targetPkg == null) {
   7269             throw new NullPointerException("targetPkg");
   7270         }
   7271         int targetUid;
   7272         final IPackageManager pm = AppGlobals.getPackageManager();
   7273         try {
   7274             targetUid = pm.getPackageUid(targetPkg, targetUserId);
   7275         } catch (RemoteException ex) {
   7276             return;
   7277         }
   7278 
   7279         targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, grantUri, modeFlags,
   7280                 targetUid);
   7281         if (targetUid < 0) {
   7282             return;
   7283         }
   7284 
   7285         grantUriPermissionUncheckedLocked(targetUid, targetPkg, grantUri, modeFlags,
   7286                 owner);
   7287     }
   7288 
   7289     static class NeededUriGrants extends ArrayList<GrantUri> {
   7290         final String targetPkg;
   7291         final int targetUid;
   7292         final int flags;
   7293 
   7294         NeededUriGrants(String targetPkg, int targetUid, int flags) {
   7295             this.targetPkg = targetPkg;
   7296             this.targetUid = targetUid;
   7297             this.flags = flags;
   7298         }
   7299     }
   7300 
   7301     /**
   7302      * Like checkGrantUriPermissionLocked, but takes an Intent.
   7303      */
   7304     NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid,
   7305             String targetPkg, Intent intent, int mode, NeededUriGrants needed, int targetUserId) {
   7306         if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   7307                 "Checking URI perm to data=" + (intent != null ? intent.getData() : null)
   7308                 + " clip=" + (intent != null ? intent.getClipData() : null)
   7309                 + " from " + intent + "; flags=0x"
   7310                 + Integer.toHexString(intent != null ? intent.getFlags() : 0));
   7311 
   7312         if (targetPkg == null) {
   7313             throw new NullPointerException("targetPkg");
   7314         }
   7315 
   7316         if (intent == null) {
   7317             return null;
   7318         }
   7319         Uri data = intent.getData();
   7320         ClipData clip = intent.getClipData();
   7321         if (data == null && clip == null) {
   7322             return null;
   7323         }
   7324         // Default userId for uris in the intent (if they don't specify it themselves)
   7325         int contentUserHint = intent.getContentUserHint();
   7326         if (contentUserHint == UserHandle.USER_CURRENT) {
   7327             contentUserHint = UserHandle.getUserId(callingUid);
   7328         }
   7329         final IPackageManager pm = AppGlobals.getPackageManager();
   7330         int targetUid;
   7331         if (needed != null) {
   7332             targetUid = needed.targetUid;
   7333         } else {
   7334             try {
   7335                 targetUid = pm.getPackageUid(targetPkg, targetUserId);
   7336             } catch (RemoteException ex) {
   7337                 return null;
   7338             }
   7339             if (targetUid < 0) {
   7340                 if (DEBUG_URI_PERMISSION) {
   7341                     Slog.v(TAG, "Can't grant URI permission no uid for: " + targetPkg
   7342                             + " on user " + targetUserId);
   7343                 }
   7344                 return null;
   7345             }
   7346         }
   7347         if (data != null) {
   7348             GrantUri grantUri = GrantUri.resolve(contentUserHint, data);
   7349             targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, grantUri, mode,
   7350                     targetUid);
   7351             if (targetUid > 0) {
   7352                 if (needed == null) {
   7353                     needed = new NeededUriGrants(targetPkg, targetUid, mode);
   7354                 }
   7355                 needed.add(grantUri);
   7356             }
   7357         }
   7358         if (clip != null) {
   7359             for (int i=0; i<clip.getItemCount(); i++) {
   7360                 Uri uri = clip.getItemAt(i).getUri();
   7361                 if (uri != null) {
   7362                     GrantUri grantUri = GrantUri.resolve(contentUserHint, uri);
   7363                     targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, grantUri, mode,
   7364                             targetUid);
   7365                     if (targetUid > 0) {
   7366                         if (needed == null) {
   7367                             needed = new NeededUriGrants(targetPkg, targetUid, mode);
   7368                         }
   7369                         needed.add(grantUri);
   7370                     }
   7371                 } else {
   7372                     Intent clipIntent = clip.getItemAt(i).getIntent();
   7373                     if (clipIntent != null) {
   7374                         NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked(
   7375                                 callingUid, targetPkg, clipIntent, mode, needed, targetUserId);
   7376                         if (newNeeded != null) {
   7377                             needed = newNeeded;
   7378                         }
   7379                     }
   7380                 }
   7381             }
   7382         }
   7383 
   7384         return needed;
   7385     }
   7386 
   7387     /**
   7388      * Like grantUriPermissionUncheckedLocked, but takes an Intent.
   7389      */
   7390     void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed,
   7391             UriPermissionOwner owner) {
   7392         if (needed != null) {
   7393             for (int i=0; i<needed.size(); i++) {
   7394                 GrantUri grantUri = needed.get(i);
   7395                 grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg,
   7396                         grantUri, needed.flags, owner);
   7397             }
   7398         }
   7399     }
   7400 
   7401     void grantUriPermissionFromIntentLocked(int callingUid,
   7402             String targetPkg, Intent intent, UriPermissionOwner owner, int targetUserId) {
   7403         NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg,
   7404                 intent, intent != null ? intent.getFlags() : 0, null, targetUserId);
   7405         if (needed == null) {
   7406             return;
   7407         }
   7408 
   7409         grantUriPermissionUncheckedFromIntentLocked(needed, owner);
   7410     }
   7411 
   7412     /**
   7413      * @param uri This uri must NOT contain an embedded userId.
   7414      * @param userId The userId in which the uri is to be resolved.
   7415      */
   7416     @Override
   7417     public void grantUriPermission(IApplicationThread caller, String targetPkg, Uri uri,
   7418             final int modeFlags, int userId) {
   7419         enforceNotIsolatedCaller("grantUriPermission");
   7420         GrantUri grantUri = new GrantUri(userId, uri, false);
   7421         synchronized(this) {
   7422             final ProcessRecord r = getRecordForAppLocked(caller);
   7423             if (r == null) {
   7424                 throw new SecurityException("Unable to find app for caller "
   7425                         + caller
   7426                         + " when granting permission to uri " + grantUri);
   7427             }
   7428             if (targetPkg == null) {
   7429                 throw new IllegalArgumentException("null target");
   7430             }
   7431             if (grantUri == null) {
   7432                 throw new IllegalArgumentException("null uri");
   7433             }
   7434 
   7435             Preconditions.checkFlagsArgument(modeFlags, Intent.FLAG_GRANT_READ_URI_PERMISSION
   7436                     | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
   7437                     | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
   7438                     | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
   7439 
   7440             grantUriPermissionLocked(r.uid, targetPkg, grantUri, modeFlags, null,
   7441                     UserHandle.getUserId(r.uid));
   7442         }
   7443     }
   7444 
   7445     void removeUriPermissionIfNeededLocked(UriPermission perm) {
   7446         if (perm.modeFlags == 0) {
   7447             final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(
   7448                     perm.targetUid);
   7449             if (perms != null) {
   7450                 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
   7451                         "Removing " + perm.targetUid + " permission to " + perm.uri);
   7452 
   7453                 perms.remove(perm.uri);
   7454                 if (perms.isEmpty()) {
   7455                     mGrantedUriPermissions.remove(perm.targetUid);
   7456                 }
   7457             }
   7458         }
   7459     }
   7460 
   7461     private void revokeUriPermissionLocked(int callingUid, GrantUri grantUri, final int modeFlags) {
   7462         if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Revoking all granted permissions to " + grantUri);
   7463 
   7464         final IPackageManager pm = AppGlobals.getPackageManager();
   7465         final String authority = grantUri.uri.getAuthority();
   7466         final ProviderInfo pi = getProviderInfoLocked(authority, grantUri.sourceUserId);
   7467         if (pi == null) {
   7468             Slog.w(TAG, "No content provider found for permission revoke: "
   7469                     + grantUri.toSafeString());
   7470             return;
   7471         }
   7472 
   7473         // Does the caller have this permission on the URI?
   7474         if (!checkHoldingPermissionsLocked(pm, pi, grantUri, callingUid, modeFlags)) {
   7475             // If they don't have direct access to the URI, then revoke any
   7476             // ownerless URI permissions that have been granted to them.
   7477             final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
   7478             if (perms != null) {
   7479                 boolean persistChanged = false;
   7480                 for (Iterator<UriPermission> it = perms.values().iterator(); it.hasNext();) {
   7481                     final UriPermission perm = it.next();
   7482                     if (perm.uri.sourceUserId == grantUri.sourceUserId
   7483                             && perm.uri.uri.isPathPrefixMatch(grantUri.uri)) {
   7484                         if (DEBUG_URI_PERMISSION)
   7485                             Slog.v(TAG, "Revoking non-owned " + perm.targetUid +
   7486                                     " permission to " + perm.uri);
   7487                         persistChanged |= perm.revokeModes(
   7488                                 modeFlags | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION, false);
   7489                         if (perm.modeFlags == 0) {
   7490                             it.remove();
   7491                         }
   7492                     }
   7493                 }
   7494                 if (perms.isEmpty()) {
   7495                     mGrantedUriPermissions.remove(callingUid);
   7496                 }
   7497                 if (persistChanged) {
   7498                     schedulePersistUriGrants();
   7499                 }
   7500             }
   7501             return;
   7502         }
   7503 
   7504         boolean persistChanged = false;
   7505 
   7506         // Go through all of the permissions and remove any that match.
   7507         int N = mGrantedUriPermissions.size();
   7508         for (int i = 0; i < N; i++) {
   7509             final int targetUid = mGrantedUriPermissions.keyAt(i);
   7510             final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
   7511 
   7512             for (Iterator<UriPermission> it = perms.values().iterator(); it.hasNext();) {
   7513                 final UriPermission perm = it.next();
   7514                 if (perm.uri.sourceUserId == grantUri.sourceUserId
   7515                         && perm.uri.uri.isPathPrefixMatch(grantUri.uri)) {
   7516                     if (DEBUG_URI_PERMISSION)
   7517                         Slog.v(TAG,
   7518                                 "Revoking " + perm.targetUid + " permission to " + perm.uri);
   7519                     persistChanged |= perm.revokeModes(
   7520                             modeFlags | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION, true);
   7521                     if (perm.modeFlags == 0) {
   7522                         it.remove();
   7523                     }
   7524                 }
   7525             }
   7526 
   7527             if (perms.isEmpty()) {
   7528                 mGrantedUriPermissions.remove(targetUid);
   7529                 N--;
   7530                 i--;
   7531             }
   7532         }
   7533 
   7534         if (persistChanged) {
   7535             schedulePersistUriGrants();
   7536         }
   7537     }
   7538 
   7539     /**
   7540      * @param uri This uri must NOT contain an embedded userId.
   7541      * @param userId The userId in which the uri is to be resolved.
   7542      */
   7543     @Override
   7544     public void revokeUriPermission(IApplicationThread caller, Uri uri, final int modeFlags,
   7545             int userId) {
   7546         enforceNotIsolatedCaller("revokeUriPermission");
   7547         synchronized(this) {
   7548             final ProcessRecord r = getRecordForAppLocked(caller);
   7549             if (r == null) {
   7550                 throw new SecurityException("Unable to find app for caller "
   7551                         + caller
   7552                         + " when revoking permission to uri " + uri);
   7553             }
   7554             if (uri == null) {
   7555                 Slog.w(TAG, "revokeUriPermission: null uri");
   7556                 return;
   7557             }
   7558 
   7559             if (!Intent.isAccessUriMode(modeFlags)) {
   7560                 return;
   7561             }
   7562 
   7563             final IPackageManager pm = AppGlobals.getPackageManager();
   7564             final String authority = uri.getAuthority();
   7565             final ProviderInfo pi = getProviderInfoLocked(authority, userId);
   7566             if (pi == null) {
   7567                 Slog.w(TAG, "No content provider found for permission revoke: "
   7568                         + uri.toSafeString());
   7569                 return;
   7570             }
   7571 
   7572             revokeUriPermissionLocked(r.uid, new GrantUri(userId, uri, false), modeFlags);
   7573         }
   7574     }
   7575 
   7576     /**
   7577      * Remove any {@link UriPermission} granted <em>from</em> or <em>to</em> the
   7578      * given package.
   7579      *
   7580      * @param packageName Package name to match, or {@code null} to apply to all
   7581      *            packages.
   7582      * @param userHandle User to match, or {@link UserHandle#USER_ALL} to apply
   7583      *            to all users.
   7584      * @param persistable If persistable grants should be removed.
   7585      */
   7586     private void removeUriPermissionsForPackageLocked(
   7587             String packageName, int userHandle, boolean persistable) {
   7588         if (userHandle == UserHandle.USER_ALL && packageName == null) {
   7589             throw new IllegalArgumentException("Must narrow by either package or user");
   7590         }
   7591 
   7592         boolean persistChanged = false;
   7593 
   7594         int N = mGrantedUriPermissions.size();
   7595         for (int i = 0; i < N; i++) {
   7596             final int targetUid = mGrantedUriPermissions.keyAt(i);
   7597             final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
   7598 
   7599             // Only inspect grants matching user
   7600             if (userHandle == UserHandle.USER_ALL
   7601                     || userHandle == UserHandle.getUserId(targetUid)) {
   7602                 for (Iterator<UriPermission> it = perms.values().iterator(); it.hasNext();) {
   7603                     final UriPermission perm = it.next();
   7604 
   7605                     // Only inspect grants matching package
   7606                     if (packageName == null || perm.sourcePkg.equals(packageName)
   7607                             || perm.targetPkg.equals(packageName)) {
   7608                         persistChanged |= perm.revokeModes(persistable
   7609                                 ? ~0 : ~Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION, true);
   7610 
   7611                         // Only remove when no modes remain; any persisted grants
   7612                         // will keep this alive.
   7613                         if (perm.modeFlags == 0) {
   7614                             it.remove();
   7615                         }
   7616                     }
   7617                 }
   7618 
   7619                 if (perms.isEmpty()) {
   7620                     mGrantedUriPermissions.remove(targetUid);
   7621                     N--;
   7622                     i--;
   7623                 }
   7624             }
   7625         }
   7626 
   7627         if (persistChanged) {
   7628             schedulePersistUriGrants();
   7629         }
   7630     }
   7631 
   7632     @Override
   7633     public IBinder newUriPermissionOwner(String name) {
   7634         enforceNotIsolatedCaller("newUriPermissionOwner");
   7635         synchronized(this) {
   7636             UriPermissionOwner owner = new UriPermissionOwner(this, name);
   7637             return owner.getExternalTokenLocked();
   7638         }
   7639     }
   7640 
   7641     /**
   7642      * @param uri This uri must NOT contain an embedded userId.
   7643      * @param sourceUserId The userId in which the uri is to be resolved.
   7644      * @param targetUserId The userId of the app that receives the grant.
   7645      */
   7646     @Override
   7647     public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, Uri uri,
   7648             final int modeFlags, int sourceUserId, int targetUserId) {
   7649         targetUserId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
   7650                 targetUserId, false, ALLOW_FULL_ONLY, "grantUriPermissionFromOwner", null);
   7651         synchronized(this) {
   7652             UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
   7653             if (owner == null) {
   7654                 throw new IllegalArgumentException("Unknown owner: " + token);
   7655             }
   7656             if (fromUid != Binder.getCallingUid()) {
   7657                 if (Binder.getCallingUid() != Process.myUid()) {
   7658                     // Only system code can grant URI permissions on behalf
   7659                     // of other users.
   7660                     throw new SecurityException("nice try");
   7661                 }
   7662             }
   7663             if (targetPkg == null) {
   7664                 throw new IllegalArgumentException("null target");
   7665             }
   7666             if (uri == null) {
   7667                 throw new IllegalArgumentException("null uri");
   7668             }
   7669 
   7670             grantUriPermissionLocked(fromUid, targetPkg, new GrantUri(sourceUserId, uri, false),
   7671                     modeFlags, owner, targetUserId);
   7672         }
   7673     }
   7674 
   7675     /**
   7676      * @param uri This uri must NOT contain an embedded userId.
   7677      * @param userId The userId in which the uri is to be resolved.
   7678      */
   7679     @Override
   7680     public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode, int userId) {
   7681         synchronized(this) {
   7682             UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
   7683             if (owner == null) {
   7684                 throw new IllegalArgumentException("Unknown owner: " + token);
   7685             }
   7686 
   7687             if (uri == null) {
   7688                 owner.removeUriPermissionsLocked(mode);
   7689             } else {
   7690                 owner.removeUriPermissionLocked(new GrantUri(userId, uri, false), mode);
   7691             }
   7692         }
   7693     }
   7694 
   7695     private void schedulePersistUriGrants() {
   7696         if (!mHandler.hasMessages(PERSIST_URI_GRANTS_MSG)) {
   7697             mHandler.sendMessageDelayed(mHandler.obtainMessage(PERSIST_URI_GRANTS_MSG),
   7698                     10 * DateUtils.SECOND_IN_MILLIS);
   7699         }
   7700     }
   7701 
   7702     private void writeGrantedUriPermissions() {
   7703         if (DEBUG_URI_PERMISSION) Slog.v(TAG, "writeGrantedUriPermissions()");
   7704 
   7705         // Snapshot permissions so we can persist without lock
   7706         ArrayList<UriPermission.Snapshot> persist = Lists.newArrayList();
   7707         synchronized (this) {
   7708             final int size = mGrantedUriPermissions.size();
   7709             for (int i = 0; i < size; i++) {
   7710                 final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
   7711                 for (UriPermission perm : perms.values()) {
   7712                     if (perm.persistedModeFlags != 0) {
   7713                         persist.add(perm.snapshot());
   7714                     }
   7715                 }
   7716             }
   7717         }
   7718 
   7719         FileOutputStream fos = null;
   7720         try {
   7721             fos = mGrantFile.startWrite();
   7722 
   7723             XmlSerializer out = new FastXmlSerializer();
   7724             out.setOutput(fos, "utf-8");
   7725             out.startDocument(null, true);
   7726             out.startTag(null, TAG_URI_GRANTS);
   7727             for (UriPermission.Snapshot perm : persist) {
   7728                 out.startTag(null, TAG_URI_GRANT);
   7729                 writeIntAttribute(out, ATTR_SOURCE_USER_ID, perm.uri.sourceUserId);
   7730                 writeIntAttribute(out, ATTR_TARGET_USER_ID, perm.targetUserId);
   7731                 out.attribute(null, ATTR_SOURCE_PKG, perm.sourcePkg);
   7732                 out.attribute(null, ATTR_TARGET_PKG, perm.targetPkg);
   7733                 out.attribute(null, ATTR_URI, String.valueOf(perm.uri.uri));
   7734                 writeBooleanAttribute(out, ATTR_PREFIX, perm.uri.prefix);
   7735                 writeIntAttribute(out, ATTR_MODE_FLAGS, perm.persistedModeFlags);
   7736                 writeLongAttribute(out, ATTR_CREATED_TIME, perm.persistedCreateTime);
   7737                 out.endTag(null, TAG_URI_GRANT);
   7738             }
   7739             out.endTag(null, TAG_URI_GRANTS);
   7740             out.endDocument();
   7741 
   7742             mGrantFile.finishWrite(fos);
   7743         } catch (IOException e) {
   7744             if (fos != null) {
   7745                 mGrantFile.failWrite(fos);
   7746             }
   7747         }
   7748     }
   7749 
   7750     private void readGrantedUriPermissionsLocked() {
   7751         if (DEBUG_URI_PERMISSION) Slog.v(TAG, "readGrantedUriPermissions()");
   7752 
   7753         final long now = System.currentTimeMillis();
   7754 
   7755         FileInputStream fis = null;
   7756         try {
   7757             fis = mGrantFile.openRead();
   7758             final XmlPullParser in = Xml.newPullParser();
   7759             in.setInput(fis, null);
   7760 
   7761             int type;
   7762             while ((type = in.next()) != END_DOCUMENT) {
   7763                 final String tag = in.getName();
   7764                 if (type == START_TAG) {
   7765                     if (TAG_URI_GRANT.equals(tag)) {
   7766                         final int sourceUserId;
   7767                         final int targetUserId;
   7768                         final int userHandle = readIntAttribute(in,
   7769                                 ATTR_USER_HANDLE, UserHandle.USER_NULL);
   7770                         if (userHandle != UserHandle.USER_NULL) {
   7771                             // For backwards compatibility.
   7772                             sourceUserId = userHandle;
   7773                             targetUserId = userHandle;
   7774                         } else {
   7775                             sourceUserId = readIntAttribute(in, ATTR_SOURCE_USER_ID);
   7776                             targetUserId = readIntAttribute(in, ATTR_TARGET_USER_ID);
   7777                         }
   7778                         final String sourcePkg = in.getAttributeValue(null, ATTR_SOURCE_PKG);
   7779                         final String targetPkg = in.getAttributeValue(null, ATTR_TARGET_PKG);
   7780                         final Uri uri = Uri.parse(in.getAttributeValue(null, ATTR_URI));
   7781                         final boolean prefix = readBooleanAttribute(in, ATTR_PREFIX);
   7782                         final int modeFlags = readIntAttribute(in, ATTR_MODE_FLAGS);
   7783                         final long createdTime = readLongAttribute(in, ATTR_CREATED_TIME, now);
   7784 
   7785                         // Sanity check that provider still belongs to source package
   7786                         final ProviderInfo pi = getProviderInfoLocked(
   7787                                 uri.getAuthority(), sourceUserId);
   7788                         if (pi != null && sourcePkg.equals(pi.packageName)) {
   7789                             int targetUid = -1;
   7790                             try {
   7791                                 targetUid = AppGlobals.getPackageManager()
   7792                                         .getPackageUid(targetPkg, targetUserId);
   7793                             } catch (RemoteException e) {
   7794                             }
   7795                             if (targetUid != -1) {
   7796                                 final UriPermission perm = findOrCreateUriPermissionLocked(
   7797                                         sourcePkg, targetPkg, targetUid,
   7798                                         new GrantUri(sourceUserId, uri, prefix));
   7799                                 perm.initPersistedModes(modeFlags, createdTime);
   7800                             }
   7801                         } else {
   7802                             Slog.w(TAG, "Persisted grant for " + uri + " had source " + sourcePkg
   7803                                     + " but instead found " + pi);
   7804                         }
   7805                     }
   7806                 }
   7807             }
   7808         } catch (FileNotFoundException e) {
   7809             // Missing grants is okay
   7810         } catch (IOException e) {
   7811             Slog.wtf(TAG, "Failed reading Uri grants", e);
   7812         } catch (XmlPullParserException e) {
   7813             Slog.wtf(TAG, "Failed reading Uri grants", e);
   7814         } finally {
   7815             IoUtils.closeQuietly(fis);
   7816         }
   7817     }
   7818 
   7819     /**
   7820      * @param uri This uri must NOT contain an embedded userId.
   7821      * @param userId The userId in which the uri is to be resolved.
   7822      */
   7823     @Override
   7824     public void takePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
   7825         enforceNotIsolatedCaller("takePersistableUriPermission");
   7826 
   7827         Preconditions.checkFlagsArgument(modeFlags,
   7828                 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
   7829 
   7830         synchronized (this) {
   7831             final int callingUid = Binder.getCallingUid();
   7832             boolean persistChanged = false;
   7833             GrantUri grantUri = new GrantUri(userId, uri, false);
   7834 
   7835             UriPermission exactPerm = findUriPermissionLocked(callingUid,
   7836                     new GrantUri(userId, uri, false));
   7837             UriPermission prefixPerm = findUriPermissionLocked(callingUid,
   7838                     new GrantUri(userId, uri, true));
   7839 
   7840             final boolean exactValid = (exactPerm != null)
   7841                     && ((modeFlags & exactPerm.persistableModeFlags) == modeFlags);
   7842             final boolean prefixValid = (prefixPerm != null)
   7843                     && ((modeFlags & prefixPerm.persistableModeFlags) == modeFlags);
   7844 
   7845             if (!(exactValid || prefixValid)) {
   7846                 throw new SecurityException("No persistable permission grants found for UID "
   7847                         + callingUid + " and Uri " + grantUri.toSafeString());
   7848             }
   7849 
   7850             if (exactValid) {
   7851                 persistChanged |= exactPerm.takePersistableModes(modeFlags);
   7852             }
   7853             if (prefixValid) {
   7854                 persistChanged |= prefixPerm.takePersistableModes(modeFlags);
   7855             }
   7856 
   7857             persistChanged |= maybePrunePersistedUriGrantsLocked(callingUid);
   7858 
   7859             if (persistChanged) {
   7860                 schedulePersistUriGrants();
   7861             }
   7862         }
   7863     }
   7864 
   7865     /**
   7866      * @param uri This uri must NOT contain an embedded userId.
   7867      * @param userId The userId in which the uri is to be resolved.
   7868      */
   7869     @Override
   7870     public void releasePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
   7871         enforceNotIsolatedCaller("releasePersistableUriPermission");
   7872 
   7873         Preconditions.checkFlagsArgument(modeFlags,
   7874                 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
   7875 
   7876         synchronized (this) {
   7877             final int callingUid = Binder.getCallingUid();
   7878             boolean persistChanged = false;
   7879 
   7880             UriPermission exactPerm = findUriPermissionLocked(callingUid,
   7881                     new GrantUri(userId, uri, false));
   7882             UriPermission prefixPerm = findUriPermissionLocked(callingUid,
   7883                     new GrantUri(userId, uri, true));
   7884             if (exactPerm == null && prefixPerm == null) {
   7885                 throw new SecurityException("No permission grants found for UID " + callingUid
   7886                         + " and Uri " + uri.toSafeString());
   7887             }
   7888 
   7889             if (exactPerm != null) {
   7890                 persistChanged |= exactPerm.releasePersistableModes(modeFlags);
   7891                 removeUriPermissionIfNeededLocked(exactPerm);
   7892             }
   7893             if (prefixPerm != null) {
   7894                 persistChanged |= prefixPerm.releasePersistableModes(modeFlags);
   7895                 removeUriPermissionIfNeededLocked(prefixPerm);
   7896             }
   7897 
   7898             if (persistChanged) {
   7899                 schedulePersistUriGrants();
   7900             }
   7901         }
   7902     }
   7903 
   7904     /**
   7905      * Prune any older {@link UriPermission} for the given UID until outstanding
   7906      * persisted grants are below {@link #MAX_PERSISTED_URI_GRANTS}.
   7907      *
   7908      * @return if any mutations occured that require persisting.
   7909      */
   7910     private boolean maybePrunePersistedUriGrantsLocked(int uid) {
   7911         final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(uid);
   7912         if (perms == null) return false;
   7913         if (perms.size() < MAX_PERSISTED_URI_GRANTS) return false;
   7914 
   7915         final ArrayList<UriPermission> persisted = Lists.newArrayList();
   7916         for (UriPermission perm : perms.values()) {
   7917             if (perm.persistedModeFlags != 0) {
   7918                 persisted.add(perm);
   7919             }
   7920         }
   7921 
   7922         final int trimCount = persisted.size() - MAX_PERSISTED_URI_GRANTS;
   7923         if (trimCount <= 0) return false;
   7924 
   7925         Collections.sort(persisted, new UriPermission.PersistedTimeComparator());
   7926         for (int i = 0; i < trimCount; i++) {
   7927             final UriPermission perm = persisted.get(i);
   7928 
   7929             if (DEBUG_URI_PERMISSION) {
   7930                 Slog.v(TAG, "Trimming grant created at " + perm.persistedCreateTime);
   7931             }
   7932 
   7933             perm.releasePersistableModes(~0);
   7934             removeUriPermissionIfNeededLocked(perm);
   7935         }
   7936 
   7937         return true;
   7938     }
   7939 
   7940     @Override
   7941     public ParceledListSlice<android.content.UriPermission> getPersistedUriPermissions(
   7942             String packageName, boolean incoming) {
   7943         enforceNotIsolatedCaller("getPersistedUriPermissions");
   7944         Preconditions.checkNotNull(packageName, "packageName");
   7945 
   7946         final int callingUid = Binder.getCallingUid();
   7947         final IPackageManager pm = AppGlobals.getPackageManager();
   7948         try {
   7949             final int packageUid = pm.getPackageUid(packageName, UserHandle.getUserId(callingUid));
   7950             if (packageUid != callingUid) {
   7951                 throw new SecurityException(
   7952                         "Package " + packageName + " does not belong to calling UID " + callingUid);
   7953             }
   7954         } catch (RemoteException e) {
   7955             throw new SecurityException("Failed to verify package name ownership");
   7956         }
   7957 
   7958         final ArrayList<android.content.UriPermission> result = Lists.newArrayList();
   7959         synchronized (this) {
   7960             if (incoming) {
   7961                 final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(
   7962                         callingUid);
   7963                 if (perms == null) {
   7964                     Slog.w(TAG, "No permission grants found for " + packageName);
   7965                 } else {
   7966                     for (UriPermission perm : perms.values()) {
   7967                         if (packageName.equals(perm.targetPkg) && perm.persistedModeFlags != 0) {
   7968                             result.add(perm.buildPersistedPublicApiObject());
   7969                         }
   7970                     }
   7971                 }
   7972             } else {
   7973                 final int size = mGrantedUriPermissions.size();
   7974                 for (int i = 0; i < size; i++) {
   7975                     final ArrayMap<GrantUri, UriPermission> perms =
   7976                             mGrantedUriPermissions.valueAt(i);
   7977                     for (UriPermission perm : perms.values()) {
   7978                         if (packageName.equals(perm.sourcePkg) && perm.persistedModeFlags != 0) {
   7979                             result.add(perm.buildPersistedPublicApiObject());
   7980                         }
   7981                     }
   7982                 }
   7983             }
   7984         }
   7985         return new ParceledListSlice<android.content.UriPermission>(result);
   7986     }
   7987 
   7988     @Override
   7989     public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
   7990         synchronized (this) {
   7991             ProcessRecord app =
   7992                 who != null ? getRecordForAppLocked(who) : null;
   7993             if (app == null) return;
   7994 
   7995             Message msg = Message.obtain();
   7996             msg.what = WAIT_FOR_DEBUGGER_MSG;
   7997             msg.obj = app;
   7998             msg.arg1 = waiting ? 1 : 0;
   7999             mHandler.sendMessage(msg);
   8000         }
   8001     }
   8002 
   8003     @Override
   8004     public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
   8005         final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ);
   8006         final long cachedAppMem = mProcessList.getMemLevel(ProcessList.CACHED_APP_MIN_ADJ);
   8007         outInfo.availMem = Process.getFreeMemory();
   8008         outInfo.totalMem = Process.getTotalMemory();
   8009         outInfo.threshold = homeAppMem;
   8010         outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((cachedAppMem-homeAppMem)/2));
   8011         outInfo.hiddenAppThreshold = cachedAppMem;
   8012         outInfo.secondaryServerThreshold = mProcessList.getMemLevel(
   8013                 ProcessList.SERVICE_ADJ);
   8014         outInfo.visibleAppThreshold = mProcessList.getMemLevel(
   8015                 ProcessList.VISIBLE_APP_ADJ);
   8016         outInfo.foregroundAppThreshold = mProcessList.getMemLevel(
   8017                 ProcessList.FOREGROUND_APP_ADJ);
   8018     }
   8019 
   8020     // =========================================================
   8021     // TASK MANAGEMENT
   8022     // =========================================================
   8023 
   8024     @Override
   8025     public List<IAppTask> getAppTasks(String callingPackage) {
   8026         int callingUid = Binder.getCallingUid();
   8027         long ident = Binder.clearCallingIdentity();
   8028 
   8029         synchronized(this) {
   8030             ArrayList<IAppTask> list = new ArrayList<IAppTask>();
   8031             try {
   8032                 if (localLOGV) Slog.v(TAG, "getAppTasks");
   8033 
   8034                 final int N = mRecentTasks.size();
   8035                 for (int i = 0; i < N; i++) {
   8036                     TaskRecord tr = mRecentTasks.get(i);
   8037                     // Skip tasks that do not match the caller.  We don't need to verify
   8038                     // callingPackage, because we are also limiting to callingUid and know
   8039                     // that will limit to the correct security sandbox.
   8040                     if (tr.effectiveUid != callingUid) {
   8041                         continue;
   8042                     }
   8043                     Intent intent = tr.getBaseIntent();
   8044                     if (intent == null ||
   8045                             !callingPackage.equals(intent.getComponent().getPackageName())) {
   8046                         continue;
   8047                     }
   8048                     ActivityManager.RecentTaskInfo taskInfo =
   8049                             createRecentTaskInfoFromTaskRecord(tr);
   8050                     AppTaskImpl taskImpl = new AppTaskImpl(taskInfo.persistentId, callingUid);
   8051                     list.add(taskImpl);
   8052                 }
   8053             } finally {
   8054                 Binder.restoreCallingIdentity(ident);
   8055             }
   8056             return list;
   8057         }
   8058     }
   8059 
   8060     @Override
   8061     public List<RunningTaskInfo> getTasks(int maxNum, int flags) {
   8062         final int callingUid = Binder.getCallingUid();
   8063         ArrayList<RunningTaskInfo> list = new ArrayList<RunningTaskInfo>();
   8064 
   8065         synchronized(this) {
   8066             if (localLOGV) Slog.v(
   8067                 TAG, "getTasks: max=" + maxNum + ", flags=" + flags);
   8068 
   8069             final boolean allowed = isGetTasksAllowed("getTasks", Binder.getCallingPid(),
   8070                     callingUid);
   8071 
   8072             // TODO: Improve with MRU list from all ActivityStacks.
   8073             mStackSupervisor.getTasksLocked(maxNum, list, callingUid, allowed);
   8074         }
   8075 
   8076         return list;
   8077     }
   8078 
   8079     /**
   8080      * Creates a new RecentTaskInfo from a TaskRecord.
   8081      */
   8082     private ActivityManager.RecentTaskInfo createRecentTaskInfoFromTaskRecord(TaskRecord tr) {
   8083         // Update the task description to reflect any changes in the task stack
   8084         tr.updateTaskDescription();
   8085 
   8086         // Compose the recent task info
   8087         ActivityManager.RecentTaskInfo rti = new ActivityManager.RecentTaskInfo();
   8088         rti.id = tr.getTopActivity() == null ? INVALID_TASK_ID : tr.taskId;
   8089         rti.persistentId = tr.taskId;
   8090         rti.baseIntent = new Intent(tr.getBaseIntent());
   8091         rti.origActivity = tr.origActivity;
   8092         rti.description = tr.lastDescription;
   8093         rti.stackId = tr.stack != null ? tr.stack.mStackId : -1;
   8094         rti.userId = tr.userId;
   8095         rti.taskDescription = new ActivityManager.TaskDescription(tr.lastTaskDescription);
   8096         rti.firstActiveTime = tr.firstActiveTime;
   8097         rti.lastActiveTime = tr.lastActiveTime;
   8098         rti.affiliatedTaskId = tr.mAffiliatedTaskId;
   8099         rti.affiliatedTaskColor = tr.mAffiliatedTaskColor;
   8100         return rti;
   8101     }
   8102 
   8103     private boolean isGetTasksAllowed(String caller, int callingPid, int callingUid) {
   8104         boolean allowed = checkPermission(android.Manifest.permission.REAL_GET_TASKS,
   8105                 callingPid, callingUid) == PackageManager.PERMISSION_GRANTED;
   8106         if (!allowed) {
   8107             if (checkPermission(android.Manifest.permission.GET_TASKS,
   8108                     callingPid, callingUid) == PackageManager.PERMISSION_GRANTED) {
   8109                 // Temporary compatibility: some existing apps on the system image may
   8110                 // still be requesting the old permission and not switched to the new
   8111                 // one; if so, we'll still allow them full access.  This means we need
   8112                 // to see if they are holding the old permission and are a system app.
   8113                 try {
   8114                     if (AppGlobals.getPackageManager().isUidPrivileged(callingUid)) {
   8115                         allowed = true;
   8116                         Slog.w(TAG, caller + ": caller " + callingUid
   8117                                 + " is using old GET_TASKS but privileged; allowing");
   8118                     }
   8119                 } catch (RemoteException e) {
   8120                 }
   8121             }
   8122         }
   8123         if (!allowed) {
   8124             Slog.w(TAG, caller + ": caller " + callingUid
   8125                     + " does not hold GET_TASKS; limiting output");
   8126         }
   8127         return allowed;
   8128     }
   8129 
   8130     @Override
   8131     public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags, int userId) {
   8132         final int callingUid = Binder.getCallingUid();
   8133         userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId,
   8134                 false, ALLOW_FULL_ONLY, "getRecentTasks", null);
   8135 
   8136         final boolean includeProfiles = (flags & ActivityManager.RECENT_INCLUDE_PROFILES) != 0;
   8137         final boolean withExcluded = (flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0;
   8138         synchronized (this) {
   8139             final boolean allowed = isGetTasksAllowed("getRecentTasks", Binder.getCallingPid(),
   8140                     callingUid);
   8141             final boolean detailed = checkCallingPermission(
   8142                     android.Manifest.permission.GET_DETAILED_TASKS)
   8143                     == PackageManager.PERMISSION_GRANTED;
   8144 
   8145             final int N = mRecentTasks.size();
   8146             ArrayList<ActivityManager.RecentTaskInfo> res
   8147                     = new ArrayList<ActivityManager.RecentTaskInfo>(
   8148                             maxNum < N ? maxNum : N);
   8149 
   8150             final Set<Integer> includedUsers;
   8151             if (includeProfiles) {
   8152                 includedUsers = getProfileIdsLocked(userId);
   8153             } else {
   8154                 includedUsers = new HashSet<Integer>();
   8155             }
   8156             includedUsers.add(Integer.valueOf(userId));
   8157 
   8158             for (int i=0; i<N && maxNum > 0; i++) {
   8159                 TaskRecord tr = mRecentTasks.get(i);
   8160                 // Only add calling user or related users recent tasks
   8161                 if (!includedUsers.contains(Integer.valueOf(tr.userId))) {
   8162                     if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, not user: " + tr);
   8163                     continue;
   8164                 }
   8165 
   8166                 // Return the entry if desired by the caller.  We always return
   8167                 // the first entry, because callers always expect this to be the
   8168                 // foreground app.  We may filter others if the caller has
   8169                 // not supplied RECENT_WITH_EXCLUDED and there is some reason
   8170                 // we should exclude the entry.
   8171 
   8172                 if (i == 0
   8173                         || withExcluded
   8174                         || (tr.intent == null)
   8175                         || ((tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
   8176                                 == 0)) {
   8177                     if (!allowed) {
   8178                         // If the caller doesn't have the GET_TASKS permission, then only
   8179                         // allow them to see a small subset of tasks -- their own and home.
   8180                         if (!tr.isHomeTask() && tr.effectiveUid != callingUid) {
   8181                             if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, not allowed: " + tr);
   8182                             continue;
   8183                         }
   8184                     }
   8185                     if ((flags & ActivityManager.RECENT_IGNORE_HOME_STACK_TASKS) != 0) {
   8186                         if (tr.stack != null && tr.stack.isHomeStack()) {
   8187                             if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, home stack task: " + tr);
   8188                             continue;
   8189                         }
   8190                     }
   8191                     if (tr.autoRemoveRecents && tr.getTopActivity() == null) {
   8192                         // Don't include auto remove tasks that are finished or finishing.
   8193                         if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, auto-remove without activity: "
   8194                                 + tr);
   8195                         continue;
   8196                     }
   8197                     if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0
   8198                             && !tr.isAvailable) {
   8199                         if (DEBUG_RECENTS) Slog.d(TAG, "Skipping, unavail real act: " + tr);
   8200                         continue;
   8201                     }
   8202 
   8203                     ActivityManager.RecentTaskInfo rti = createRecentTaskInfoFromTaskRecord(tr);
   8204                     if (!detailed) {
   8205                         rti.baseIntent.replaceExtras((Bundle)null);
   8206                     }
   8207 
   8208                     res.add(rti);
   8209                     maxNum--;
   8210                 }
   8211             }
   8212             return res;
   8213         }
   8214     }
   8215 
   8216     TaskRecord recentTaskForIdLocked(int id) {
   8217         final int N = mRecentTasks.size();
   8218             for (int i=0; i<N; i++) {
   8219                 TaskRecord tr = mRecentTasks.get(i);
   8220                 if (tr.taskId == id) {
   8221                     return tr;
   8222                 }
   8223             }
   8224             return null;
   8225     }
   8226 
   8227     @Override
   8228     public ActivityManager.TaskThumbnail getTaskThumbnail(int id) {
   8229         synchronized (this) {
   8230             enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER,
   8231                     "getTaskThumbnail()");
   8232             TaskRecord tr = mStackSupervisor.anyTaskForIdLocked(id);
   8233             if (tr != null) {
   8234                 return tr.getTaskThumbnailLocked();
   8235             }
   8236         }
   8237         return null;
   8238     }
   8239 
   8240     @Override
   8241     public int addAppTask(IBinder activityToken, Intent intent,
   8242             ActivityManager.TaskDescription description, Bitmap thumbnail) throws RemoteException {
   8243         final int callingUid = Binder.getCallingUid();
   8244         final long callingIdent = Binder.clearCallingIdentity();
   8245 
   8246         try {
   8247             synchronized (this) {
   8248                 ActivityRecord r = ActivityRecord.isInStackLocked(activityToken);
   8249                 if (r == null) {
   8250                     throw new IllegalArgumentException("Activity does not exist; token="
   8251                             + activityToken);
   8252                 }
   8253                 ComponentName comp = intent.getComponent();
   8254                 if (comp == null) {
   8255                     throw new IllegalArgumentException("Intent " + intent
   8256                             + " must specify explicit component");
   8257                 }
   8258                 if (thumbnail.getWidth() != mThumbnailWidth
   8259                         || thumbnail.getHeight() != mThumbnailHeight) {
   8260                     throw new IllegalArgumentException("Bad thumbnail size: got "
   8261                             + thumbnail.getWidth() + "x" + thumbnail.getHeight() + ", require "
   8262                             + mThumbnailWidth + "x" + mThumbnailHeight);
   8263                 }
   8264                 if (intent.getSelector() != null) {
   8265                     intent.setSelector(null);
   8266                 }
   8267                 if (intent.getSourceBounds() != null) {
   8268                     intent.setSourceBounds(null);
   8269                 }
   8270                 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0) {
   8271                     if ((intent.getFlags()&Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS) == 0) {
   8272                         // The caller has added this as an auto-remove task...  that makes no
   8273                         // sense, so turn off auto-remove.
   8274                         intent.addFlags(Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
   8275                     }
   8276                 } else if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
   8277                     // Must be a new task.
   8278                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   8279                 }
   8280                 if (!comp.equals(mLastAddedTaskComponent) || callingUid != mLastAddedTaskUid) {
   8281                     mLastAddedTaskActivity = null;
   8282                 }
   8283                 ActivityInfo ainfo = mLastAddedTaskActivity;
   8284                 if (ainfo == null) {
   8285                     ainfo = mLastAddedTaskActivity = AppGlobals.getPackageManager().getActivityInfo(
   8286                             comp, 0, UserHandle.getUserId(callingUid));
   8287                     if (ainfo.applicationInfo.uid != callingUid) {
   8288                         throw new SecurityException(
   8289                                 "Can't add task for another application: target uid="
   8290                                 + ainfo.applicationInfo.uid + ", calling uid=" + callingUid);
   8291                     }
   8292                 }
   8293 
   8294                 TaskRecord task = new TaskRecord(this, mStackSupervisor.getNextTaskId(), ainfo,
   8295                         intent, description);
   8296 
   8297                 int trimIdx = trimRecentsForTaskLocked(task, false);
   8298                 if (trimIdx >= 0) {
   8299                     // If this would have caused a trim, then we'll abort because that
   8300                     // means it would be added at the end of the list but then just removed.
   8301                     return INVALID_TASK_ID;
   8302                 }
   8303 
   8304                 final int N = mRecentTasks.size();
   8305                 if (N >= (ActivityManager.getMaxRecentTasksStatic()-1)) {
   8306                     final TaskRecord tr = mRecentTasks.remove(N - 1);
   8307                     tr.removedFromRecents();
   8308                 }
   8309 
   8310                 task.inRecents = true;
   8311                 mRecentTasks.add(task);
   8312                 r.task.stack.addTask(task, false, false);
   8313 
   8314                 task.setLastThumbnail(thumbnail);
   8315                 task.freeLastThumbnail();
   8316 
   8317                 return task.taskId;
   8318             }
   8319         } finally {
   8320             Binder.restoreCallingIdentity(callingIdent);
   8321         }
   8322     }
   8323 
   8324     @Override
   8325     public Point getAppTaskThumbnailSize() {
   8326         synchronized (this) {
   8327             return new Point(mThumbnailWidth,  mThumbnailHeight);
   8328         }
   8329     }
   8330 
   8331     @Override
   8332     public void setTaskDescription(IBinder token, ActivityManager.TaskDescription td) {
   8333         synchronized (this) {
   8334             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   8335             if (r != null) {
   8336                 r.setTaskDescription(td);
   8337                 r.task.updateTaskDescription();
   8338             }
   8339         }
   8340     }
   8341 
   8342     @Override
   8343     public Bitmap getTaskDescriptionIcon(String filename) {
   8344         if (!FileUtils.isValidExtFilename(filename)
   8345                 || !filename.contains(ActivityRecord.ACTIVITY_ICON_SUFFIX)) {
   8346             throw new IllegalArgumentException("Bad filename: " + filename);
   8347         }
   8348         return mTaskPersister.getTaskDescriptionIcon(filename);
   8349     }
   8350 
   8351     @Override
   8352     public void startInPlaceAnimationOnFrontMostApplication(ActivityOptions opts)
   8353             throws RemoteException {
   8354         if (opts.getAnimationType() != ActivityOptions.ANIM_CUSTOM_IN_PLACE ||
   8355                 opts.getCustomInPlaceResId() == 0) {
   8356             throw new IllegalArgumentException("Expected in-place ActivityOption " +
   8357                     "with valid animation");
   8358         }
   8359         mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_IN_PLACE, false);
   8360         mWindowManager.overridePendingAppTransitionInPlace(opts.getPackageName(),
   8361                 opts.getCustomInPlaceResId());
   8362         mWindowManager.executeAppTransition();
   8363     }
   8364 
   8365     private void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess) {
   8366         mRecentTasks.remove(tr);
   8367         tr.removedFromRecents();
   8368         ComponentName component = tr.getBaseIntent().getComponent();
   8369         if (component == null) {
   8370             Slog.w(TAG, "No component for base intent of task: " + tr);
   8371             return;
   8372         }
   8373 
   8374         if (!killProcess) {
   8375             return;
   8376         }
   8377 
   8378         // Determine if the process(es) for this task should be killed.
   8379         final String pkg = component.getPackageName();
   8380         ArrayList<ProcessRecord> procsToKill = new ArrayList<ProcessRecord>();
   8381         ArrayMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap();
   8382         for (int i = 0; i < pmap.size(); i++) {
   8383 
   8384             SparseArray<ProcessRecord> uids = pmap.valueAt(i);
   8385             for (int j = 0; j < uids.size(); j++) {
   8386                 ProcessRecord proc = uids.valueAt(j);
   8387                 if (proc.userId != tr.userId) {
   8388                     // Don't kill process for a different user.
   8389                     continue;
   8390                 }
   8391                 if (proc == mHomeProcess) {
   8392                     // Don't kill the home process along with tasks from the same package.
   8393                     continue;
   8394                 }
   8395                 if (!proc.pkgList.containsKey(pkg)) {
   8396                     // Don't kill process that is not associated with this task.
   8397                     continue;
   8398                 }
   8399 
   8400                 for (int k = 0; k < proc.activities.size(); k++) {
   8401                     TaskRecord otherTask = proc.activities.get(k).task;
   8402                     if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
   8403                         // Don't kill process(es) that has an activity in a different task that is
   8404                         // also in recents.
   8405                         return;
   8406                     }
   8407                 }
   8408 
   8409                 // Add process to kill list.
   8410                 procsToKill.add(proc);
   8411             }
   8412         }
   8413 
   8414         // Find any running services associated with this app and stop if needed.
   8415         mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
   8416 
   8417         // Kill the running processes.
   8418         for (int i = 0; i < procsToKill.size(); i++) {
   8419             ProcessRecord pr = procsToKill.get(i);
   8420             if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
   8421                 pr.kill("remove task", true);
   8422             } else {
   8423                 pr.waitingToKill = "remove task";
   8424             }
   8425         }
   8426     }
   8427 
   8428     private void removeTasksByPackageNameLocked(String packageName, int userId) {
   8429         // Remove all tasks with activities in the specified package from the list of recent tasks
   8430         for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
   8431             TaskRecord tr = mRecentTasks.get(i);
   8432             if (tr.userId != userId) continue;
   8433 
   8434             ComponentName cn = tr.intent.getComponent();
   8435             if (cn != null && cn.getPackageName().equals(packageName)) {
   8436                 // If the package name matches, remove the task.
   8437                 removeTaskByIdLocked(tr.taskId, true);
   8438             }
   8439         }
   8440     }
   8441 
   8442     private void removeTasksByRemovedPackageComponentsLocked(String packageName, int userId) {
   8443         final IPackageManager pm = AppGlobals.getPackageManager();
   8444         final HashSet<ComponentName> componentsKnownToExist = new HashSet<ComponentName>();
   8445 
   8446         for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
   8447             TaskRecord tr = mRecentTasks.get(i);
   8448             if (tr.userId != userId) continue;
   8449 
   8450             ComponentName cn = tr.intent.getComponent();
   8451             if (cn != null && cn.getPackageName().equals(packageName)) {
   8452                 // Skip if component still exists in the package.
   8453                 if (componentsKnownToExist.contains(cn)) continue;
   8454 
   8455                 try {
   8456                     ActivityInfo info = pm.getActivityInfo(cn, 0, userId);
   8457                     if (info != null) {
   8458                         componentsKnownToExist.add(cn);
   8459                     } else {
   8460                         removeTaskByIdLocked(tr.taskId, false);
   8461                     }
   8462                 } catch (RemoteException e) {
   8463                     Log.e(TAG, "Activity info query failed. component=" + cn, e);
   8464                 }
   8465             }
   8466         }
   8467     }
   8468 
   8469     /**
   8470      * Removes the task with the specified task id.
   8471      *
   8472      * @param taskId Identifier of the task to be removed.
   8473      * @param killProcess Kill any process associated with the task if possible.
   8474      * @return Returns true if the given task was found and removed.
   8475      */
   8476     private boolean removeTaskByIdLocked(int taskId, boolean killProcess) {
   8477         TaskRecord tr = mStackSupervisor.anyTaskForIdLocked(taskId);
   8478         if (tr != null) {
   8479             tr.removeTaskActivitiesLocked();
   8480             cleanUpRemovedTaskLocked(tr, killProcess);
   8481             if (tr.isPersistable) {
   8482                 notifyTaskPersisterLocked(null, true);
   8483             }
   8484             return true;
   8485         }
   8486         Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
   8487         return false;
   8488     }
   8489 
   8490     @Override
   8491     public boolean removeTask(int taskId) {
   8492         synchronized (this) {
   8493             enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
   8494                     "removeTask()");
   8495             long ident = Binder.clearCallingIdentity();
   8496             try {
   8497                 return removeTaskByIdLocked(taskId, true);
   8498             } finally {
   8499                 Binder.restoreCallingIdentity(ident);
   8500             }
   8501         }
   8502     }
   8503 
   8504     /**
   8505      * TODO: Add mController hook
   8506      */
   8507     @Override
   8508     public void moveTaskToFront(int taskId, int flags, Bundle options) {
   8509         enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
   8510                 "moveTaskToFront()");
   8511 
   8512         if (DEBUG_STACK) Slog.d(TAG, "moveTaskToFront: moving taskId=" + taskId);
   8513         synchronized(this) {
   8514             moveTaskToFrontLocked(taskId, flags, options);
   8515         }
   8516     }
   8517 
   8518     void moveTaskToFrontLocked(int taskId, int flags, Bundle options) {
   8519         if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
   8520                 Binder.getCallingUid(), -1, -1, "Task to front")) {
   8521             ActivityOptions.abort(options);
   8522             return;
   8523         }
   8524         final long origId = Binder.clearCallingIdentity();
   8525         try {
   8526             final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
   8527             if (task == null) {
   8528                 Slog.d(TAG, "Could not find task for id: "+ taskId);
   8529                 return;
   8530             }
   8531             if (mStackSupervisor.isLockTaskModeViolation(task)) {
   8532                 mStackSupervisor.showLockTaskToast();
   8533                 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
   8534                 return;
   8535             }
   8536             final ActivityRecord prev = mStackSupervisor.topRunningActivityLocked();
   8537             if (prev != null && prev.isRecentsActivity()) {
   8538                 task.setTaskToReturnTo(ActivityRecord.RECENTS_ACTIVITY_TYPE);
   8539             }
   8540             mStackSupervisor.findTaskToMoveToFrontLocked(task, flags, options, "moveTaskToFront");
   8541         } finally {
   8542             Binder.restoreCallingIdentity(origId);
   8543         }
   8544         ActivityOptions.abort(options);
   8545     }
   8546 
   8547     @Override
   8548     public void moveTaskToBack(int taskId) {
   8549         enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
   8550                 "moveTaskToBack()");
   8551 
   8552         synchronized(this) {
   8553             TaskRecord tr = mStackSupervisor.anyTaskForIdLocked(taskId);
   8554             if (tr != null) {
   8555                 if (tr == mStackSupervisor.mLockTaskModeTask) {
   8556                     mStackSupervisor.showLockTaskToast();
   8557                     return;
   8558                 }
   8559                 if (DEBUG_STACK) Slog.d(TAG, "moveTaskToBack: moving task=" + tr);
   8560                 ActivityStack stack = tr.stack;
   8561                 if (stack.mResumedActivity != null && stack.mResumedActivity.task == tr) {
   8562                     if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
   8563                             Binder.getCallingUid(), -1, -1, "Task to back")) {
   8564                         return;
   8565                     }
   8566                 }
   8567                 final long origId = Binder.clearCallingIdentity();
   8568                 try {
   8569                     stack.moveTaskToBackLocked(taskId);
   8570                 } finally {
   8571                     Binder.restoreCallingIdentity(origId);
   8572                 }
   8573             }
   8574         }
   8575     }
   8576 
   8577     /**
   8578      * Moves an activity, and all of the other activities within the same task, to the bottom
   8579      * of the history stack.  The activity's order within the task is unchanged.
   8580      *
   8581      * @param token A reference to the activity we wish to move
   8582      * @param nonRoot If false then this only works if the activity is the root
   8583      *                of a task; if true it will work for any activity in a task.
   8584      * @return Returns true if the move completed, false if not.
   8585      */
   8586     @Override
   8587     public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
   8588         enforceNotIsolatedCaller("moveActivityTaskToBack");
   8589         synchronized(this) {
   8590             final long origId = Binder.clearCallingIdentity();
   8591             try {
   8592                 int taskId = ActivityRecord.getTaskForActivityLocked(token, !nonRoot);
   8593                 if (taskId >= 0) {
   8594                     if ((mStackSupervisor.mLockTaskModeTask != null)
   8595                             && (mStackSupervisor.mLockTaskModeTask.taskId == taskId)) {
   8596                         mStackSupervisor.showLockTaskToast();
   8597                         return false;
   8598                     }
   8599                     return ActivityRecord.getStackLocked(token).moveTaskToBackLocked(taskId);
   8600                 }
   8601             } finally {
   8602                 Binder.restoreCallingIdentity(origId);
   8603             }
   8604         }
   8605         return false;
   8606     }
   8607 
   8608     @Override
   8609     public void moveTaskBackwards(int task) {
   8610         enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
   8611                 "moveTaskBackwards()");
   8612 
   8613         synchronized(this) {
   8614             if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
   8615                     Binder.getCallingUid(), -1, -1, "Task backwards")) {
   8616                 return;
   8617             }
   8618             final long origId = Binder.clearCallingIdentity();
   8619             moveTaskBackwardsLocked(task);
   8620             Binder.restoreCallingIdentity(origId);
   8621         }
   8622     }
   8623 
   8624     private final void moveTaskBackwardsLocked(int task) {
   8625         Slog.e(TAG, "moveTaskBackwards not yet implemented!");
   8626     }
   8627 
   8628     @Override
   8629     public IBinder getHomeActivityToken() throws RemoteException {
   8630         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8631                 "getHomeActivityToken()");
   8632         synchronized (this) {
   8633             return mStackSupervisor.getHomeActivityToken();
   8634         }
   8635     }
   8636 
   8637     @Override
   8638     public IActivityContainer createActivityContainer(IBinder parentActivityToken,
   8639             IActivityContainerCallback callback) throws RemoteException {
   8640         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8641                 "createActivityContainer()");
   8642         synchronized (this) {
   8643             if (parentActivityToken == null) {
   8644                 throw new IllegalArgumentException("parent token must not be null");
   8645             }
   8646             ActivityRecord r = ActivityRecord.forToken(parentActivityToken);
   8647             if (r == null) {
   8648                 return null;
   8649             }
   8650             if (callback == null) {
   8651                 throw new IllegalArgumentException("callback must not be null");
   8652             }
   8653             return mStackSupervisor.createActivityContainer(r, callback);
   8654         }
   8655     }
   8656 
   8657     @Override
   8658     public void deleteActivityContainer(IActivityContainer container) throws RemoteException {
   8659         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8660                 "deleteActivityContainer()");
   8661         synchronized (this) {
   8662             mStackSupervisor.deleteActivityContainer(container);
   8663         }
   8664     }
   8665 
   8666     @Override
   8667     public int getActivityDisplayId(IBinder activityToken) throws RemoteException {
   8668         synchronized (this) {
   8669             ActivityStack stack = ActivityRecord.getStackLocked(activityToken);
   8670             if (stack != null && stack.mActivityContainer.isAttachedLocked()) {
   8671                 return stack.mActivityContainer.getDisplayId();
   8672             }
   8673             return Display.DEFAULT_DISPLAY;
   8674         }
   8675     }
   8676 
   8677     @Override
   8678     public void moveTaskToStack(int taskId, int stackId, boolean toTop) {
   8679         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8680                 "moveTaskToStack()");
   8681         if (stackId == HOME_STACK_ID) {
   8682             Slog.e(TAG, "moveTaskToStack: Attempt to move task " + taskId + " to home stack",
   8683                     new RuntimeException("here").fillInStackTrace());
   8684         }
   8685         synchronized (this) {
   8686             long ident = Binder.clearCallingIdentity();
   8687             try {
   8688                 if (DEBUG_STACK) Slog.d(TAG, "moveTaskToStack: moving task=" + taskId + " to stackId="
   8689                         + stackId + " toTop=" + toTop);
   8690                 mStackSupervisor.moveTaskToStackLocked(taskId, stackId, toTop);
   8691             } finally {
   8692                 Binder.restoreCallingIdentity(ident);
   8693             }
   8694         }
   8695     }
   8696 
   8697     @Override
   8698     public void resizeStack(int stackBoxId, Rect bounds) {
   8699         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8700                 "resizeStackBox()");
   8701         long ident = Binder.clearCallingIdentity();
   8702         try {
   8703             mWindowManager.resizeStack(stackBoxId, bounds);
   8704         } finally {
   8705             Binder.restoreCallingIdentity(ident);
   8706         }
   8707     }
   8708 
   8709     @Override
   8710     public List<StackInfo> getAllStackInfos() {
   8711         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8712                 "getAllStackInfos()");
   8713         long ident = Binder.clearCallingIdentity();
   8714         try {
   8715             synchronized (this) {
   8716                 return mStackSupervisor.getAllStackInfosLocked();
   8717             }
   8718         } finally {
   8719             Binder.restoreCallingIdentity(ident);
   8720         }
   8721     }
   8722 
   8723     @Override
   8724     public StackInfo getStackInfo(int stackId) {
   8725         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8726                 "getStackInfo()");
   8727         long ident = Binder.clearCallingIdentity();
   8728         try {
   8729             synchronized (this) {
   8730                 return mStackSupervisor.getStackInfoLocked(stackId);
   8731             }
   8732         } finally {
   8733             Binder.restoreCallingIdentity(ident);
   8734         }
   8735     }
   8736 
   8737     @Override
   8738     public boolean isInHomeStack(int taskId) {
   8739         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8740                 "getStackInfo()");
   8741         long ident = Binder.clearCallingIdentity();
   8742         try {
   8743             synchronized (this) {
   8744                 TaskRecord tr = mStackSupervisor.anyTaskForIdLocked(taskId);
   8745                 return tr != null && tr.stack != null && tr.stack.isHomeStack();
   8746             }
   8747         } finally {
   8748             Binder.restoreCallingIdentity(ident);
   8749         }
   8750     }
   8751 
   8752     @Override
   8753     public int getTaskForActivity(IBinder token, boolean onlyRoot) {
   8754         synchronized(this) {
   8755             return ActivityRecord.getTaskForActivityLocked(token, onlyRoot);
   8756         }
   8757     }
   8758 
   8759     private boolean isLockTaskAuthorized(String pkg) {
   8760         final DevicePolicyManager dpm = (DevicePolicyManager)
   8761                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
   8762         try {
   8763             int uid = mContext.getPackageManager().getPackageUid(pkg,
   8764                     Binder.getCallingUserHandle().getIdentifier());
   8765             return (uid == Binder.getCallingUid()) && dpm != null && dpm.isLockTaskPermitted(pkg);
   8766         } catch (NameNotFoundException e) {
   8767             return false;
   8768         }
   8769     }
   8770 
   8771     void startLockTaskMode(TaskRecord task) {
   8772         final String pkg;
   8773         synchronized (this) {
   8774             pkg = task.intent.getComponent().getPackageName();
   8775         }
   8776         boolean isSystemInitiated = Binder.getCallingUid() == Process.SYSTEM_UID;
   8777         if (!isSystemInitiated && !isLockTaskAuthorized(pkg)) {
   8778             StatusBarManagerInternal statusBarManager = LocalServices.getService(
   8779                     StatusBarManagerInternal.class);
   8780             if (statusBarManager != null) {
   8781                 statusBarManager.showScreenPinningRequest();
   8782             }
   8783             return;
   8784         }
   8785         long ident = Binder.clearCallingIdentity();
   8786         try {
   8787             synchronized (this) {
   8788                 // Since we lost lock on task, make sure it is still there.
   8789                 task = mStackSupervisor.anyTaskForIdLocked(task.taskId);
   8790                 if (task != null) {
   8791                     if (!isSystemInitiated
   8792                             && ((mStackSupervisor.getFocusedStack() == null)
   8793                                     || (task != mStackSupervisor.getFocusedStack().topTask()))) {
   8794                         throw new IllegalArgumentException("Invalid task, not in foreground");
   8795                     }
   8796                     mStackSupervisor.setLockTaskModeLocked(task, !isSystemInitiated,
   8797                             "startLockTask");
   8798                 }
   8799             }
   8800         } finally {
   8801             Binder.restoreCallingIdentity(ident);
   8802         }
   8803     }
   8804 
   8805     @Override
   8806     public void startLockTaskMode(int taskId) {
   8807         final TaskRecord task;
   8808         long ident = Binder.clearCallingIdentity();
   8809         try {
   8810             synchronized (this) {
   8811                 task = mStackSupervisor.anyTaskForIdLocked(taskId);
   8812             }
   8813         } finally {
   8814             Binder.restoreCallingIdentity(ident);
   8815         }
   8816         if (task != null) {
   8817             startLockTaskMode(task);
   8818         }
   8819     }
   8820 
   8821     @Override
   8822     public void startLockTaskMode(IBinder token) {
   8823         final TaskRecord task;
   8824         long ident = Binder.clearCallingIdentity();
   8825         try {
   8826             synchronized (this) {
   8827                 final ActivityRecord r = ActivityRecord.forToken(token);
   8828                 if (r == null) {
   8829                     return;
   8830                 }
   8831                 task = r.task;
   8832             }
   8833         } finally {
   8834             Binder.restoreCallingIdentity(ident);
   8835         }
   8836         if (task != null) {
   8837             startLockTaskMode(task);
   8838         }
   8839     }
   8840 
   8841     @Override
   8842     public void startLockTaskModeOnCurrent() throws RemoteException {
   8843         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8844                 "startLockTaskModeOnCurrent");
   8845         long ident = Binder.clearCallingIdentity();
   8846         try {
   8847             ActivityRecord r = null;
   8848             synchronized (this) {
   8849                 r = mStackSupervisor.topRunningActivityLocked();
   8850             }
   8851             startLockTaskMode(r.task);
   8852         } finally {
   8853             Binder.restoreCallingIdentity(ident);
   8854         }
   8855     }
   8856 
   8857     @Override
   8858     public void stopLockTaskMode() {
   8859         // Verify that the user matches the package of the intent for the TaskRecord
   8860         // we are locked to or systtem.  This will ensure the same caller for startLockTaskMode
   8861         // and stopLockTaskMode.
   8862         final int callingUid = Binder.getCallingUid();
   8863         if (callingUid != Process.SYSTEM_UID) {
   8864             try {
   8865                 String pkg =
   8866                         mStackSupervisor.mLockTaskModeTask.intent.getComponent().getPackageName();
   8867                 int uid = mContext.getPackageManager().getPackageUid(pkg,
   8868                         Binder.getCallingUserHandle().getIdentifier());
   8869                 if (uid != callingUid) {
   8870                     throw new SecurityException("Invalid uid, expected " + uid);
   8871                 }
   8872             } catch (NameNotFoundException e) {
   8873                 Log.d(TAG, "stopLockTaskMode " + e);
   8874                 return;
   8875             }
   8876         }
   8877         long ident = Binder.clearCallingIdentity();
   8878         try {
   8879             Log.d(TAG, "stopLockTaskMode");
   8880             // Stop lock task
   8881             synchronized (this) {
   8882                 mStackSupervisor.setLockTaskModeLocked(null, false, "stopLockTask");
   8883             }
   8884         } finally {
   8885             Binder.restoreCallingIdentity(ident);
   8886         }
   8887     }
   8888 
   8889     @Override
   8890     public void stopLockTaskModeOnCurrent() throws RemoteException {
   8891         enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
   8892                 "stopLockTaskModeOnCurrent");
   8893         long ident = Binder.clearCallingIdentity();
   8894         try {
   8895             stopLockTaskMode();
   8896         } finally {
   8897             Binder.restoreCallingIdentity(ident);
   8898         }
   8899     }
   8900 
   8901     @Override
   8902     public boolean isInLockTaskMode() {
   8903         synchronized (this) {
   8904             return mStackSupervisor.isInLockTaskMode();
   8905         }
   8906     }
   8907 
   8908     // =========================================================
   8909     // CONTENT PROVIDERS
   8910     // =========================================================
   8911 
   8912     private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) {
   8913         List<ProviderInfo> providers = null;
   8914         try {
   8915             providers = AppGlobals.getPackageManager().
   8916                 queryContentProviders(app.processName, app.uid,
   8917                         STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
   8918         } catch (RemoteException ex) {
   8919         }
   8920         if (DEBUG_MU)
   8921             Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid);
   8922         int userId = app.userId;
   8923         if (providers != null) {
   8924             int N = providers.size();
   8925             app.pubProviders.ensureCapacity(N + app.pubProviders.size());
   8926             for (int i=0; i<N; i++) {
   8927                 ProviderInfo cpi =
   8928                     (ProviderInfo)providers.get(i);
   8929                 boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo,
   8930                         cpi.name, cpi.flags);
   8931                 if (singleton && UserHandle.getUserId(app.uid) != 0) {
   8932                     // This is a singleton provider, but a user besides the
   8933                     // default user is asking to initialize a process it runs
   8934                     // in...  well, no, it doesn't actually run in this process,
   8935                     // it runs in the process of the default user.  Get rid of it.
   8936                     providers.remove(i);
   8937                     N--;
   8938                     i--;
   8939                     continue;
   8940                 }
   8941 
   8942                 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
   8943                 ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId);
   8944                 if (cpr == null) {
   8945                     cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton);
   8946                     mProviderMap.putProviderByClass(comp, cpr);
   8947                 }
   8948                 if (DEBUG_MU)
   8949                     Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid);
   8950                 app.pubProviders.put(cpi.name, cpr);
   8951                 if (!cpi.multiprocess || !"android".equals(cpi.packageName)) {
   8952                     // Don't add this if it is a platform component that is marked
   8953                     // to run in multiple processes, because this is actually
   8954                     // part of the framework so doesn't make sense to track as a
   8955                     // separate apk in the process.
   8956                     app.addPackage(cpi.applicationInfo.packageName, cpi.applicationInfo.versionCode,
   8957                             mProcessStats);
   8958                 }
   8959                 ensurePackageDexOpt(cpi.applicationInfo.packageName);
   8960             }
   8961         }
   8962         return providers;
   8963     }
   8964 
   8965     /**
   8966      * Check if {@link ProcessRecord} has a possible chance at accessing the
   8967      * given {@link ProviderInfo}. Final permission checking is always done
   8968      * in {@link ContentProvider}.
   8969      */
   8970     private final String checkContentProviderPermissionLocked(
   8971             ProviderInfo cpi, ProcessRecord r, int userId, boolean checkUser) {
   8972         final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
   8973         final int callingUid = (r != null) ? r.uid : Binder.getCallingUid();
   8974         boolean checkedGrants = false;
   8975         if (checkUser) {
   8976             // Looking for cross-user grants before enforcing the typical cross-users permissions
   8977             int tmpTargetUserId = unsafeConvertIncomingUser(userId);
   8978             if (tmpTargetUserId != UserHandle.getUserId(callingUid)) {
   8979                 if (checkAuthorityGrants(callingUid, cpi, tmpTargetUserId, checkUser)) {
   8980                     return null;
   8981                 }
   8982                 checkedGrants = true;
   8983             }
   8984             userId = handleIncomingUser(callingPid, callingUid, userId,
   8985                     false, ALLOW_NON_FULL,
   8986                     "checkContentProviderPermissionLocked " + cpi.authority, null);
   8987             if (userId != tmpTargetUserId) {
   8988                 // When we actually went to determine the final targer user ID, this ended
   8989                 // up different than our initial check for the authority.  This is because
   8990                 // they had asked for USER_CURRENT_OR_SELF and we ended up switching to
   8991                 // SELF.  So we need to re-check the grants again.
   8992                 checkedGrants = false;
   8993             }
   8994         }
   8995         if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
   8996                 cpi.applicationInfo.uid, cpi.exported)
   8997                 == PackageManager.PERMISSION_GRANTED) {
   8998             return null;
   8999         }
   9000         if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
   9001                 cpi.applicationInfo.uid, cpi.exported)
   9002                 == PackageManager.PERMISSION_GRANTED) {
   9003             return null;
   9004         }
   9005 
   9006         PathPermission[] pps = cpi.pathPermissions;
   9007         if (pps != null) {
   9008             int i = pps.length;
   9009             while (i > 0) {
   9010                 i--;
   9011                 PathPermission pp = pps[i];
   9012                 String pprperm = pp.getReadPermission();
   9013                 if (pprperm != null && checkComponentPermission(pprperm, callingPid, callingUid,
   9014                         cpi.applicationInfo.uid, cpi.exported)
   9015                         == PackageManager.PERMISSION_GRANTED) {
   9016                     return null;
   9017                 }
   9018                 String ppwperm = pp.getWritePermission();
   9019                 if (ppwperm != null && checkComponentPermission(ppwperm, callingPid, callingUid,
   9020                         cpi.applicationInfo.uid, cpi.exported)
   9021                         == PackageManager.PERMISSION_GRANTED) {
   9022                     return null;
   9023                 }
   9024             }
   9025         }
   9026         if (!checkedGrants && checkAuthorityGrants(callingUid, cpi, userId, checkUser)) {
   9027             return null;
   9028         }
   9029 
   9030         String msg;
   9031         if (!cpi.exported) {
   9032             msg = "Permission Denial: opening provider " + cpi.name
   9033                     + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
   9034                     + ", uid=" + callingUid + ") that is not exported from uid "
   9035                     + cpi.applicationInfo.uid;
   9036         } else {
   9037             msg = "Permission Denial: opening provider " + cpi.name
   9038                     + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
   9039                     + ", uid=" + callingUid + ") requires "
   9040                     + cpi.readPermission + " or " + cpi.writePermission;
   9041         }
   9042         Slog.w(TAG, msg);
   9043         return msg;
   9044     }
   9045 
   9046     /**
   9047      * Returns if the ContentProvider has granted a uri to callingUid
   9048      */
   9049     boolean checkAuthorityGrants(int callingUid, ProviderInfo cpi, int userId, boolean checkUser) {
   9050         final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
   9051         if (perms != null) {
   9052             for (int i=perms.size()-1; i>=0; i--) {
   9053                 GrantUri grantUri = perms.keyAt(i);
   9054                 if (grantUri.sourceUserId == userId || !checkUser) {
   9055                     if (matchesProvider(grantUri.uri, cpi)) {
   9056                         return true;
   9057                     }
   9058                 }
   9059             }
   9060         }
   9061         return false;
   9062     }
   9063 
   9064     /**
   9065      * Returns true if the uri authority is one of the authorities specified in the provider.
   9066      */
   9067     boolean matchesProvider(Uri uri, ProviderInfo cpi) {
   9068         String uriAuth = uri.getAuthority();
   9069         String cpiAuth = cpi.authority;
   9070         if (cpiAuth.indexOf(';') == -1) {
   9071             return cpiAuth.equals(uriAuth);
   9072         }
   9073         String[] cpiAuths = cpiAuth.split(";");
   9074         int length = cpiAuths.length;
   9075         for (int i = 0; i < length; i++) {
   9076             if (cpiAuths[i].equals(uriAuth)) return true;
   9077         }
   9078         return false;
   9079     }
   9080 
   9081     ContentProviderConnection incProviderCountLocked(ProcessRecord r,
   9082             final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
   9083         if (r != null) {
   9084             for (int i=0; i<r.conProviders.size(); i++) {
   9085                 ContentProviderConnection conn = r.conProviders.get(i);
   9086                 if (conn.provider == cpr) {
   9087                     if (DEBUG_PROVIDER) Slog.v(TAG,
   9088                             "Adding provider requested by "
   9089                             + r.processName + " from process "
   9090                             + cpr.info.processName + ": " + cpr.name.flattenToShortString()
   9091                             + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
   9092                     if (stable) {
   9093                         conn.stableCount++;
   9094                         conn.numStableIncs++;
   9095                     } else {
   9096                         conn.unstableCount++;
   9097                         conn.numUnstableIncs++;
   9098                     }
   9099                     return conn;
   9100                 }
   9101             }
   9102             ContentProviderConnection conn = new ContentProviderConnection(cpr, r);
   9103             if (stable) {
   9104                 conn.stableCount = 1;
   9105                 conn.numStableIncs = 1;
   9106             } else {
   9107                 conn.unstableCount = 1;
   9108                 conn.numUnstableIncs = 1;
   9109             }
   9110             cpr.connections.add(conn);
   9111             r.conProviders.add(conn);
   9112             startAssociationLocked(r.uid, r.processName, cpr.uid, cpr.name, cpr.info.processName);
   9113             return conn;
   9114         }
   9115         cpr.addExternalProcessHandleLocked(externalProcessToken);
   9116         return null;
   9117     }
   9118 
   9119     boolean decProviderCountLocked(ContentProviderConnection conn,
   9120             ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
   9121         if (conn != null) {
   9122             cpr = conn.provider;
   9123             if (DEBUG_PROVIDER) Slog.v(TAG,
   9124                     "Removing provider requested by "
   9125                     + conn.client.processName + " from process "
   9126                     + cpr.info.processName + ": " + cpr.name.flattenToShortString()
   9127                     + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
   9128             if (stable) {
   9129                 conn.stableCount--;
   9130             } else {
   9131                 conn.unstableCount--;
   9132             }
   9133             if (conn.stableCount == 0 && conn.unstableCount == 0) {
   9134                 cpr.connections.remove(conn);
   9135                 conn.client.conProviders.remove(conn);
   9136                 stopAssociationLocked(conn.client.uid, conn.client.processName, cpr.uid, cpr.name);
   9137                 return true;
   9138             }
   9139             return false;
   9140         }
   9141         cpr.removeExternalProcessHandleLocked(externalProcessToken);
   9142         return false;
   9143     }
   9144 
   9145     private void checkTime(long startTime, String where) {
   9146         long now = SystemClock.elapsedRealtime();
   9147         if ((now-startTime) > 1000) {
   9148             // If we are taking more than a second, log about it.
   9149             Slog.w(TAG, "Slow operation: " + (now-startTime) + "ms so far, now at " + where);
   9150         }
   9151     }
   9152 
   9153     private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller,
   9154             String name, IBinder token, boolean stable, int userId) {
   9155         ContentProviderRecord cpr;
   9156         ContentProviderConnection conn = null;
   9157         ProviderInfo cpi = null;
   9158 
   9159         synchronized(this) {
   9160             long startTime = SystemClock.elapsedRealtime();
   9161 
   9162             ProcessRecord r = null;
   9163             if (caller != null) {
   9164                 r = getRecordForAppLocked(caller);
   9165                 if (r == null) {
   9166                     throw new SecurityException(
   9167                             "Unable to find app for caller " + caller
   9168                           + " (pid=" + Binder.getCallingPid()
   9169                           + ") when getting content provider " + name);
   9170                 }
   9171             }
   9172 
   9173             boolean checkCrossUser = true;
   9174 
   9175             checkTime(startTime, "getContentProviderImpl: getProviderByName");
   9176 
   9177             // First check if this content provider has been published...
   9178             cpr = mProviderMap.getProviderByName(name, userId);
   9179             // If that didn't work, check if it exists for user 0 and then
   9180             // verify that it's a singleton provider before using it.
   9181             if (cpr == null && userId != UserHandle.USER_OWNER) {
   9182                 cpr = mProviderMap.getProviderByName(name, UserHandle.USER_OWNER);
   9183                 if (cpr != null) {
   9184                     cpi = cpr.info;
   9185                     if (isSingleton(cpi.processName, cpi.applicationInfo,
   9186                             cpi.name, cpi.flags)
   9187                             && isValidSingletonCall(r.uid, cpi.applicationInfo.uid)) {
   9188                         userId = UserHandle.USER_OWNER;
   9189                         checkCrossUser = false;
   9190                     } else {
   9191                         cpr = null;
   9192                         cpi = null;
   9193                     }
   9194                 }
   9195             }
   9196 
   9197             boolean providerRunning = cpr != null;
   9198             if (providerRunning) {
   9199                 cpi = cpr.info;
   9200                 String msg;
   9201                 checkTime(startTime, "getContentProviderImpl: before checkContentProviderPermission");
   9202                 if ((msg = checkContentProviderPermissionLocked(cpi, r, userId, checkCrossUser))
   9203                         != null) {
   9204                     throw new SecurityException(msg);
   9205                 }
   9206                 checkTime(startTime, "getContentProviderImpl: after checkContentProviderPermission");
   9207 
   9208                 if (r != null && cpr.canRunHere(r)) {
   9209                     // This provider has been published or is in the process
   9210                     // of being published...  but it is also allowed to run
   9211                     // in the caller's process, so don't make a connection
   9212                     // and just let the caller instantiate its own instance.
   9213                     ContentProviderHolder holder = cpr.newHolder(null);
   9214                     // don't give caller the provider object, it needs
   9215                     // to make its own.
   9216                     holder.provider = null;
   9217                     return holder;
   9218                 }
   9219 
   9220                 final long origId = Binder.clearCallingIdentity();
   9221 
   9222                 checkTime(startTime, "getContentProviderImpl: incProviderCountLocked");
   9223 
   9224                 // In this case the provider instance already exists, so we can
   9225                 // return it right away.
   9226                 conn = incProviderCountLocked(r, cpr, token, stable);
   9227                 if (conn != null && (conn.stableCount+conn.unstableCount) == 1) {
   9228                     if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
   9229                         // If this is a perceptible app accessing the provider,
   9230                         // make sure to count it as being accessed and thus
   9231                         // back up on the LRU list.  This is good because
   9232                         // content providers are often expensive to start.
   9233                         checkTime(startTime, "getContentProviderImpl: before updateLruProcess");
   9234                         updateLruProcessLocked(cpr.proc, false, null);
   9235                         checkTime(startTime, "getContentProviderImpl: after updateLruProcess");
   9236                     }
   9237                 }
   9238 
   9239                 if (cpr.proc != null) {
   9240                     if (false) {
   9241                         if (cpr.name.flattenToShortString().equals(
   9242                                 "com.android.providers.calendar/.CalendarProvider2")) {
   9243                             Slog.v(TAG, "****************** KILLING "
   9244                                 + cpr.name.flattenToShortString());
   9245                             Process.killProcess(cpr.proc.pid);
   9246                         }
   9247                     }
   9248                     checkTime(startTime, "getContentProviderImpl: before updateOomAdj");
   9249                     boolean success = updateOomAdjLocked(cpr.proc);
   9250                     checkTime(startTime, "getContentProviderImpl: after updateOomAdj");
   9251                     if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success);
   9252                     // NOTE: there is still a race here where a signal could be
   9253                     // pending on the process even though we managed to update its
   9254                     // adj level.  Not sure what to do about this, but at least
   9255                     // the race is now smaller.
   9256                     if (!success) {
   9257                         // Uh oh...  it looks like the provider's process
   9258                         // has been killed on us.  We need to wait for a new
   9259                         // process to be started, and make sure its death
   9260                         // doesn't kill our process.
   9261                         Slog.i(TAG,
   9262                                 "Existing provider " + cpr.name.flattenToShortString()
   9263                                 + " is crashing; detaching " + r);
   9264                         boolean lastRef = decProviderCountLocked(conn, cpr, token, stable);
   9265                         checkTime(startTime, "getContentProviderImpl: before appDied");
   9266                         appDiedLocked(cpr.proc);
   9267                         checkTime(startTime, "getContentProviderImpl: after appDied");
   9268                         if (!lastRef) {
   9269                             // This wasn't the last ref our process had on
   9270                             // the provider...  we have now been killed, bail.
   9271                             return null;
   9272                         }
   9273                         providerRunning = false;
   9274                         conn = null;
   9275                     }
   9276                 }
   9277 
   9278                 Binder.restoreCallingIdentity(origId);
   9279             }
   9280 
   9281             boolean singleton;
   9282             if (!providerRunning) {
   9283                 try {
   9284                     checkTime(startTime, "getContentProviderImpl: before resolveContentProvider");
   9285                     cpi = AppGlobals.getPackageManager().
   9286                         resolveContentProvider(name,
   9287                             STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
   9288                     checkTime(startTime, "getContentProviderImpl: after resolveContentProvider");
   9289                 } catch (RemoteException ex) {
   9290                 }
   9291                 if (cpi == null) {
   9292                     return null;
   9293                 }
   9294                 // If the provider is a singleton AND
   9295                 // (it's a call within the same user || the provider is a
   9296                 // privileged app)
   9297                 // Then allow connecting to the singleton provider
   9298                 singleton = isSingleton(cpi.processName, cpi.applicationInfo,
   9299                         cpi.name, cpi.flags)
   9300                         && isValidSingletonCall(r.uid, cpi.applicationInfo.uid);
   9301                 if (singleton) {
   9302                     userId = UserHandle.USER_OWNER;
   9303                 }
   9304                 cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId);
   9305                 checkTime(startTime, "getContentProviderImpl: got app info for user");
   9306 
   9307                 String msg;
   9308                 checkTime(startTime, "getContentProviderImpl: before checkContentProviderPermission");
   9309                 if ((msg = checkContentProviderPermissionLocked(cpi, r, userId, !singleton))
   9310                         != null) {
   9311                     throw new SecurityException(msg);
   9312                 }
   9313                 checkTime(startTime, "getContentProviderImpl: after checkContentProviderPermission");
   9314 
   9315                 if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate
   9316                         && !cpi.processName.equals("system")) {
   9317                     // If this content provider does not run in the system
   9318                     // process, and the system is not yet ready to run other
   9319                     // processes, then fail fast instead of hanging.
   9320                     throw new IllegalArgumentException(
   9321                             "Attempt to launch content provider before system ready");
   9322                 }
   9323 
   9324                 // Make sure that the user who owns this provider is running.  If not,
   9325                 // we don't want to allow it to run.
   9326                 if (!isUserRunningLocked(userId, false)) {
   9327                     Slog.w(TAG, "Unable to launch app "
   9328                             + cpi.applicationInfo.packageName + "/"
   9329                             + cpi.applicationInfo.uid + " for provider "
   9330                             + name + ": user " + userId + " is stopped");
   9331                     return null;
   9332                 }
   9333 
   9334                 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
   9335                 checkTime(startTime, "getContentProviderImpl: before getProviderByClass");
   9336                 cpr = mProviderMap.getProviderByClass(comp, userId);
   9337                 checkTime(startTime, "getContentProviderImpl: after getProviderByClass");
   9338                 final boolean firstClass = cpr == null;
   9339                 if (firstClass) {
   9340                     final long ident = Binder.clearCallingIdentity();
   9341                     try {
   9342                         checkTime(startTime, "getContentProviderImpl: before getApplicationInfo");
   9343                         ApplicationInfo ai =
   9344                             AppGlobals.getPackageManager().
   9345                                 getApplicationInfo(
   9346                                         cpi.applicationInfo.packageName,
   9347                                         STOCK_PM_FLAGS, userId);
   9348                         checkTime(startTime, "getContentProviderImpl: after getApplicationInfo");
   9349                         if (ai == null) {
   9350                             Slog.w(TAG, "No package info for content provider "
   9351                                     + cpi.name);
   9352                             return null;
   9353                         }
   9354                         ai = getAppInfoForUser(ai, userId);
   9355                         cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton);
   9356                     } catch (RemoteException ex) {
   9357                         // pm is in same process, this will never happen.
   9358                     } finally {
   9359                         Binder.restoreCallingIdentity(ident);
   9360                     }
   9361                 }
   9362 
   9363                 checkTime(startTime, "getContentProviderImpl: now have ContentProviderRecord");
   9364 
   9365                 if (r != null && cpr.canRunHere(r)) {
   9366                     // If this is a multiprocess provider, then just return its
   9367                     // info and allow the caller to instantiate it.  Only do
   9368                     // this if the provider is the same user as the caller's
   9369                     // process, or can run as root (so can be in any process).
   9370                     return cpr.newHolder(null);
   9371                 }
   9372 
   9373                 if (DEBUG_PROVIDER) {
   9374                     RuntimeException e = new RuntimeException("here");
   9375                     Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + (r != null ? r.uid : null)
   9376                           + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
   9377                 }
   9378 
   9379                 // This is single process, and our app is now connecting to it.
   9380                 // See if we are already in the process of launching this
   9381                 // provider.
   9382                 final int N = mLaunchingProviders.size();
   9383                 int i;
   9384                 for (i=0; i<N; i++) {
   9385                     if (mLaunchingProviders.get(i) == cpr) {
   9386                         break;
   9387                     }
   9388                 }
   9389 
   9390                 // If the provider is not already being launched, then get it
   9391                 // started.
   9392                 if (i >= N) {
   9393                     final long origId = Binder.clearCallingIdentity();
   9394 
   9395                     try {
   9396                         // Content provider is now in use, its package can't be stopped.
   9397                         try {
   9398                             checkTime(startTime, "getContentProviderImpl: before set stopped state");
   9399                             AppGlobals.getPackageManager().setPackageStoppedState(
   9400                                     cpr.appInfo.packageName, false, userId);
   9401                             checkTime(startTime, "getContentProviderImpl: after set stopped state");
   9402                         } catch (RemoteException e) {
   9403                         } catch (IllegalArgumentException e) {
   9404                             Slog.w(TAG, "Failed trying to unstop package "
   9405                                     + cpr.appInfo.packageName + ": " + e);
   9406                         }
   9407 
   9408                         // Use existing process if already started
   9409                         checkTime(startTime, "getContentProviderImpl: looking for process record");
   9410                         ProcessRecord proc = getProcessRecordLocked(
   9411                                 cpi.processName, cpr.appInfo.uid, false);
   9412                         if (proc != null && proc.thread != null) {
   9413                             if (DEBUG_PROVIDER) {
   9414                                 Slog.d(TAG, "Installing in existing process " + proc);
   9415                             }
   9416                             checkTime(startTime, "getContentProviderImpl: scheduling install");
   9417                             proc.pubProviders.put(cpi.name, cpr);
   9418                             try {
   9419                                 proc.thread.scheduleInstallProvider(cpi);
   9420                             } catch (RemoteException e) {
   9421                             }
   9422                         } else {
   9423                             checkTime(startTime, "getContentProviderImpl: before start process");
   9424                             proc = startProcessLocked(cpi.processName,
   9425                                     cpr.appInfo, false, 0, "content provider",
   9426                                     new ComponentName(cpi.applicationInfo.packageName,
   9427                                             cpi.name), false, false, false);
   9428                             checkTime(startTime, "getContentProviderImpl: after start process");
   9429                             if (proc == null) {
   9430                                 Slog.w(TAG, "Unable to launch app "
   9431                                         + cpi.applicationInfo.packageName + "/"
   9432                                         + cpi.applicationInfo.uid + " for provider "
   9433                                         + name + ": process is bad");
   9434                                 return null;
   9435                             }
   9436                         }
   9437                         cpr.launchingApp = proc;
   9438                         mLaunchingProviders.add(cpr);
   9439                     } finally {
   9440                         Binder.restoreCallingIdentity(origId);
   9441                     }
   9442                 }
   9443 
   9444                 checkTime(startTime, "getContentProviderImpl: updating data structures");
   9445 
   9446                 // Make sure the provider is published (the same provider class
   9447                 // may be published under multiple names).
   9448                 if (firstClass) {
   9449                     mProviderMap.putProviderByClass(comp, cpr);
   9450                 }
   9451 
   9452                 mProviderMap.putProviderByName(name, cpr);
   9453                 conn = incProviderCountLocked(r, cpr, token, stable);
   9454                 if (conn != null) {
   9455                     conn.waiting = true;
   9456                 }
   9457             }
   9458             checkTime(startTime, "getContentProviderImpl: done!");
   9459         }
   9460 
   9461         // Wait for the provider to be published...
   9462         synchronized (cpr) {
   9463             while (cpr.provider == null) {
   9464                 if (cpr.launchingApp == null) {
   9465                     Slog.w(TAG, "Unable to launch app "
   9466                             + cpi.applicationInfo.packageName + "/"
   9467                             + cpi.applicationInfo.uid + " for provider "
   9468                             + name + ": launching app became null");
   9469                     EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
   9470                             UserHandle.getUserId(cpi.applicationInfo.uid),
   9471                             cpi.applicationInfo.packageName,
   9472                             cpi.applicationInfo.uid, name);
   9473                     return null;
   9474                 }
   9475                 try {
   9476                     if (DEBUG_MU) {
   9477                         Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp="
   9478                                 + cpr.launchingApp);
   9479                     }
   9480                     if (conn != null) {
   9481                         conn.waiting = true;
   9482                     }
   9483                     cpr.wait();
   9484                 } catch (InterruptedException ex) {
   9485                 } finally {
   9486                     if (conn != null) {
   9487                         conn.waiting = false;
   9488                     }
   9489                 }
   9490             }
   9491         }
   9492         return cpr != null ? cpr.newHolder(conn) : null;
   9493     }
   9494 
   9495     @Override
   9496     public final ContentProviderHolder getContentProvider(
   9497             IApplicationThread caller, String name, int userId, boolean stable) {
   9498         enforceNotIsolatedCaller("getContentProvider");
   9499         if (caller == null) {
   9500             String msg = "null IApplicationThread when getting content provider "
   9501                     + name;
   9502             Slog.w(TAG, msg);
   9503             throw new SecurityException(msg);
   9504         }
   9505         // The incoming user check is now handled in checkContentProviderPermissionLocked() to deal
   9506         // with cross-user grant.
   9507         return getContentProviderImpl(caller, name, null, stable, userId);
   9508     }
   9509 
   9510     public ContentProviderHolder getContentProviderExternal(
   9511             String name, int userId, IBinder token) {
   9512         enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
   9513             "Do not have permission in call getContentProviderExternal()");
   9514         userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
   9515                 false, ALLOW_FULL_ONLY, "getContentProvider", null);
   9516         return getContentProviderExternalUnchecked(name, token, userId);
   9517     }
   9518 
   9519     private ContentProviderHolder getContentProviderExternalUnchecked(String name,
   9520             IBinder token, int userId) {
   9521         return getContentProviderImpl(null, name, token, true, userId);
   9522     }
   9523 
   9524     /**
   9525      * Drop a content provider from a ProcessRecord's bookkeeping
   9526      */
   9527     public void removeContentProvider(IBinder connection, boolean stable) {
   9528         enforceNotIsolatedCaller("removeContentProvider");
   9529         long ident = Binder.clearCallingIdentity();
   9530         try {
   9531             synchronized (this) {
   9532                 ContentProviderConnection conn;
   9533                 try {
   9534                     conn = (ContentProviderConnection)connection;
   9535                 } catch (ClassCastException e) {
   9536                     String msg ="removeContentProvider: " + connection
   9537                             + " not a ContentProviderConnection";
   9538                     Slog.w(TAG, msg);
   9539                     throw new IllegalArgumentException(msg);
   9540                 }
   9541                 if (conn == null) {
   9542                     throw new NullPointerException("connection is null");
   9543                 }
   9544                 if (decProviderCountLocked(conn, null, null, stable)) {
   9545                     updateOomAdjLocked();
   9546                 }
   9547             }
   9548         } finally {
   9549             Binder.restoreCallingIdentity(ident);
   9550         }
   9551     }
   9552 
   9553     public void removeContentProviderExternal(String name, IBinder token) {
   9554         enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
   9555             "Do not have permission in call removeContentProviderExternal()");
   9556         int userId = UserHandle.getCallingUserId();
   9557         long ident = Binder.clearCallingIdentity();
   9558         try {
   9559             removeContentProviderExternalUnchecked(name, token, userId);
   9560         } finally {
   9561             Binder.restoreCallingIdentity(ident);
   9562         }
   9563     }
   9564 
   9565     private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) {
   9566         synchronized (this) {
   9567             ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId);
   9568             if(cpr == null) {
   9569                 //remove from mProvidersByClass
   9570                 if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
   9571                 return;
   9572             }
   9573 
   9574             //update content provider record entry info
   9575             ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name);
   9576             ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId);
   9577             if (localCpr.hasExternalProcessHandles()) {
   9578                 if (localCpr.removeExternalProcessHandleLocked(token)) {
   9579                     updateOomAdjLocked();
   9580                 } else {
   9581                     Slog.e(TAG, "Attmpt to remove content provider " + localCpr
   9582                             + " with no external reference for token: "
   9583                             + token + ".");
   9584                 }
   9585             } else {
   9586                 Slog.e(TAG, "Attmpt to remove content provider: " + localCpr
   9587                         + " with no external references.");
   9588             }
   9589         }
   9590     }
   9591 
   9592     public final void publishContentProviders(IApplicationThread caller,
   9593             List<ContentProviderHolder> providers) {
   9594         if (providers == null) {
   9595             return;
   9596         }
   9597 
   9598         enforceNotIsolatedCaller("publishContentProviders");
   9599         synchronized (this) {
   9600             final ProcessRecord r = getRecordForAppLocked(caller);
   9601             if (DEBUG_MU)
   9602                 Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid);
   9603             if (r == null) {
   9604                 throw new SecurityException(
   9605                         "Unable to find app for caller " + caller
   9606                       + " (pid=" + Binder.getCallingPid()
   9607                       + ") when publishing content providers");
   9608             }
   9609 
   9610             final long origId = Binder.clearCallingIdentity();
   9611 
   9612             final int N = providers.size();
   9613             for (int i=0; i<N; i++) {
   9614                 ContentProviderHolder src = providers.get(i);
   9615                 if (src == null || src.info == null || src.provider == null) {
   9616                     continue;
   9617                 }
   9618                 ContentProviderRecord dst = r.pubProviders.get(src.info.name);
   9619                 if (DEBUG_MU)
   9620                     Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid);
   9621                 if (dst != null) {
   9622                     ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name);
   9623                     mProviderMap.putProviderByClass(comp, dst);
   9624                     String names[] = dst.info.authority.split(";");
   9625                     for (int j = 0; j < names.length; j++) {
   9626                         mProviderMap.putProviderByName(names[j], dst);
   9627                     }
   9628 
   9629                     int NL = mLaunchingProviders.size();
   9630                     int j;
   9631                     for (j=0; j<NL; j++) {
   9632                         if (mLaunchingProviders.get(j) == dst) {
   9633                             mLaunchingProviders.remove(j);
   9634                             j--;
   9635                             NL--;
   9636                         }
   9637                     }
   9638                     synchronized (dst) {
   9639                         dst.provider = src.provider;
   9640                         dst.proc = r;
   9641                         dst.notifyAll();
   9642                     }
   9643                     updateOomAdjLocked(r);
   9644                 }
   9645             }
   9646 
   9647             Binder.restoreCallingIdentity(origId);
   9648         }
   9649     }
   9650 
   9651     public boolean refContentProvider(IBinder connection, int stable, int unstable) {
   9652         ContentProviderConnection conn;
   9653         try {
   9654             conn = (ContentProviderConnection)connection;
   9655         } catch (ClassCastException e) {
   9656             String msg ="refContentProvider: " + connection
   9657                     + " not a ContentProviderConnection";
   9658             Slog.w(TAG, msg);
   9659             throw new IllegalArgumentException(msg);
   9660         }
   9661         if (conn == null) {
   9662             throw new NullPointerException("connection is null");
   9663         }
   9664 
   9665         synchronized (this) {
   9666             if (stable > 0) {
   9667                 conn.numStableIncs += stable;
   9668             }
   9669             stable = conn.stableCount + stable;
   9670             if (stable < 0) {
   9671                 throw new IllegalStateException("stableCount < 0: " + stable);
   9672             }
   9673 
   9674             if (unstable > 0) {
   9675                 conn.numUnstableIncs += unstable;
   9676             }
   9677             unstable = conn.unstableCount + unstable;
   9678             if (unstable < 0) {
   9679                 throw new IllegalStateException("unstableCount < 0: " + unstable);
   9680             }
   9681 
   9682             if ((stable+unstable) <= 0) {
   9683                 throw new IllegalStateException("ref counts can't go to zero here: stable="
   9684                         + stable + " unstable=" + unstable);
   9685             }
   9686             conn.stableCount = stable;
   9687             conn.unstableCount = unstable;
   9688             return !conn.dead;
   9689         }
   9690     }
   9691 
   9692     public void unstableProviderDied(IBinder connection) {
   9693         ContentProviderConnection conn;
   9694         try {
   9695             conn = (ContentProviderConnection)connection;
   9696         } catch (ClassCastException e) {
   9697             String msg ="refContentProvider: " + connection
   9698                     + " not a ContentProviderConnection";
   9699             Slog.w(TAG, msg);
   9700             throw new IllegalArgumentException(msg);
   9701         }
   9702         if (conn == null) {
   9703             throw new NullPointerException("connection is null");
   9704         }
   9705 
   9706         // Safely retrieve the content provider associated with the connection.
   9707         IContentProvider provider;
   9708         synchronized (this) {
   9709             provider = conn.provider.provider;
   9710         }
   9711 
   9712         if (provider == null) {
   9713             // Um, yeah, we're way ahead of you.
   9714             return;
   9715         }
   9716 
   9717         // Make sure the caller is being honest with us.
   9718         if (provider.asBinder().pingBinder()) {
   9719             // Er, no, still looks good to us.
   9720             synchronized (this) {
   9721                 Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid()
   9722                         + " says " + conn + " died, but we don't agree");
   9723                 return;
   9724             }
   9725         }
   9726 
   9727         // Well look at that!  It's dead!
   9728         synchronized (this) {
   9729             if (conn.provider.provider != provider) {
   9730                 // But something changed...  good enough.
   9731                 return;
   9732             }
   9733 
   9734             ProcessRecord proc = conn.provider.proc;
   9735             if (proc == null || proc.thread == null) {
   9736                 // Seems like the process is already cleaned up.
   9737                 return;
   9738             }
   9739 
   9740             // As far as we're concerned, this is just like receiving a
   9741             // death notification...  just a bit prematurely.
   9742             Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid
   9743                     + ") early provider death");
   9744             final long ident = Binder.clearCallingIdentity();
   9745             try {
   9746                 appDiedLocked(proc);
   9747             } finally {
   9748                 Binder.restoreCallingIdentity(ident);
   9749             }
   9750         }
   9751     }
   9752 
   9753     @Override
   9754     public void appNotRespondingViaProvider(IBinder connection) {
   9755         enforceCallingPermission(
   9756                 android.Manifest.permission.REMOVE_TASKS, "appNotRespondingViaProvider()");
   9757 
   9758         final ContentProviderConnection conn = (ContentProviderConnection) connection;
   9759         if (conn == null) {
   9760             Slog.w(TAG, "ContentProviderConnection is null");
   9761             return;
   9762         }
   9763 
   9764         final ProcessRecord host = conn.provider.proc;
   9765         if (host == null) {
   9766             Slog.w(TAG, "Failed to find hosting ProcessRecord");
   9767             return;
   9768         }
   9769 
   9770         final long token = Binder.clearCallingIdentity();
   9771         try {
   9772             appNotResponding(host, null, null, false, "ContentProvider not responding");
   9773         } finally {
   9774             Binder.restoreCallingIdentity(token);
   9775         }
   9776     }
   9777 
   9778     public final void installSystemProviders() {
   9779         List<ProviderInfo> providers;
   9780         synchronized (this) {
   9781             ProcessRecord app = mProcessNames.get("system", Process.SYSTEM_UID);
   9782             providers = generateApplicationProvidersLocked(app);
   9783             if (providers != null) {
   9784                 for (int i=providers.size()-1; i>=0; i--) {
   9785                     ProviderInfo pi = (ProviderInfo)providers.get(i);
   9786                     if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
   9787                         Slog.w(TAG, "Not installing system proc provider " + pi.name
   9788                                 + ": not system .apk");
   9789                         providers.remove(i);
   9790                     }
   9791                 }
   9792             }
   9793         }
   9794         if (providers != null) {
   9795             mSystemThread.installSystemProviders(providers);
   9796         }
   9797 
   9798         mCoreSettingsObserver = new CoreSettingsObserver(this);
   9799 
   9800         //mUsageStatsService.monitorPackages();
   9801     }
   9802 
   9803     /**
   9804      * Allows apps to retrieve the MIME type of a URI.
   9805      * If an app is in the same user as the ContentProvider, or if it is allowed to interact across
   9806      * users, then it does not need permission to access the ContentProvider.
   9807      * Either, it needs cross-user uri grants.
   9808      *
   9809      * CTS tests for this functionality can be run with "runtest cts-appsecurity".
   9810      *
   9811      * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
   9812      *     src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
   9813      */
   9814     public String getProviderMimeType(Uri uri, int userId) {
   9815         enforceNotIsolatedCaller("getProviderMimeType");
   9816         final String name = uri.getAuthority();
   9817         int callingUid = Binder.getCallingUid();
   9818         int callingPid = Binder.getCallingPid();
   9819         long ident = 0;
   9820         boolean clearedIdentity = false;
   9821         userId = unsafeConvertIncomingUser(userId);
   9822         if (canClearIdentity(callingPid, callingUid, userId)) {
   9823             clearedIdentity = true;
   9824             ident = Binder.clearCallingIdentity();
   9825         }
   9826         ContentProviderHolder holder = null;
   9827         try {
   9828             holder = getContentProviderExternalUnchecked(name, null, userId);
   9829             if (holder != null) {
   9830                 return holder.provider.getType(uri);
   9831             }
   9832         } catch (RemoteException e) {
   9833             Log.w(TAG, "Content provider dead retrieving " + uri, e);
   9834             return null;
   9835         } finally {
   9836             // We need to clear the identity to call removeContentProviderExternalUnchecked
   9837             if (!clearedIdentity) {
   9838                 ident = Binder.clearCallingIdentity();
   9839             }
   9840             try {
   9841                 if (holder != null) {
   9842                     removeContentProviderExternalUnchecked(name, null, userId);
   9843                 }
   9844             } finally {
   9845                 Binder.restoreCallingIdentity(ident);
   9846             }
   9847         }
   9848 
   9849         return null;
   9850     }
   9851 
   9852     private boolean canClearIdentity(int callingPid, int callingUid, int userId) {
   9853         if (UserHandle.getUserId(callingUid) == userId) {
   9854             return true;
   9855         }
   9856         if (checkComponentPermission(INTERACT_ACROSS_USERS, callingPid,
   9857                 callingUid, -1, true) == PackageManager.PERMISSION_GRANTED
   9858                 || checkComponentPermission(INTERACT_ACROSS_USERS_FULL, callingPid,
   9859                 callingUid, -1, true) == PackageManager.PERMISSION_GRANTED) {
   9860                 return true;
   9861         }
   9862         return false;
   9863     }
   9864 
   9865     // =========================================================
   9866     // GLOBAL MANAGEMENT
   9867     // =========================================================
   9868 
   9869     final ProcessRecord newProcessRecordLocked(ApplicationInfo info, String customProcess,
   9870             boolean isolated, int isolatedUid) {
   9871         String proc = customProcess != null ? customProcess : info.processName;
   9872         BatteryStatsImpl.Uid.Proc ps = null;
   9873         BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
   9874         int uid = info.uid;
   9875         if (isolated) {
   9876             if (isolatedUid == 0) {
   9877                 int userId = UserHandle.getUserId(uid);
   9878                 int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1;
   9879                 while (true) {
   9880                     if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID
   9881                             || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) {
   9882                         mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID;
   9883                     }
   9884                     uid = UserHandle.getUid(userId, mNextIsolatedProcessUid);
   9885                     mNextIsolatedProcessUid++;
   9886                     if (mIsolatedProcesses.indexOfKey(uid) < 0) {
   9887                         // No process for this uid, use it.
   9888                         break;
   9889                     }
   9890                     stepsLeft--;
   9891                     if (stepsLeft <= 0) {
   9892                         return null;
   9893                     }
   9894                 }
   9895             } else {
   9896                 // Special case for startIsolatedProcess (internal only), where
   9897                 // the uid of the isolated process is specified by the caller.
   9898                 uid = isolatedUid;
   9899             }
   9900         }
   9901         return new ProcessRecord(stats, info, proc, uid);
   9902     }
   9903 
   9904     final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated,
   9905             String abiOverride) {
   9906         ProcessRecord app;
   9907         if (!isolated) {
   9908             app = getProcessRecordLocked(info.processName, info.uid, true);
   9909         } else {
   9910             app = null;
   9911         }
   9912 
   9913         if (app == null) {
   9914             app = newProcessRecordLocked(info, null, isolated, 0);
   9915             mProcessNames.put(info.processName, app.uid, app);
   9916             if (isolated) {
   9917                 mIsolatedProcesses.put(app.uid, app);
   9918             }
   9919             updateLruProcessLocked(app, false, null);
   9920             updateOomAdjLocked();
   9921         }
   9922 
   9923         // This package really, really can not be stopped.
   9924         try {
   9925             AppGlobals.getPackageManager().setPackageStoppedState(
   9926                     info.packageName, false, UserHandle.getUserId(app.uid));
   9927         } catch (RemoteException e) {
   9928         } catch (IllegalArgumentException e) {
   9929             Slog.w(TAG, "Failed trying to unstop package "
   9930                     + info.packageName + ": " + e);
   9931         }
   9932 
   9933         if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
   9934                 == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
   9935             app.persistent = true;
   9936             app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ;
   9937         }
   9938         if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
   9939             mPersistentStartingProcesses.add(app);
   9940             startProcessLocked(app, "added application", app.processName, abiOverride,
   9941                     null /* entryPoint */, null /* entryPointArgs */);
   9942         }
   9943 
   9944         return app;
   9945     }
   9946 
   9947     public void unhandledBack() {
   9948         enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
   9949                 "unhandledBack()");
   9950 
   9951         synchronized(this) {
   9952             final long origId = Binder.clearCallingIdentity();
   9953             try {
   9954                 getFocusedStack().unhandledBackLocked();
   9955             } finally {
   9956                 Binder.restoreCallingIdentity(origId);
   9957             }
   9958         }
   9959     }
   9960 
   9961     public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
   9962         enforceNotIsolatedCaller("openContentUri");
   9963         final int userId = UserHandle.getCallingUserId();
   9964         String name = uri.getAuthority();
   9965         ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId);
   9966         ParcelFileDescriptor pfd = null;
   9967         if (cph != null) {
   9968             // We record the binder invoker's uid in thread-local storage before
   9969             // going to the content provider to open the file.  Later, in the code
   9970             // that handles all permissions checks, we look for this uid and use
   9971             // that rather than the Activity Manager's own uid.  The effect is that
   9972             // we do the check against the caller's permissions even though it looks
   9973             // to the content provider like the Activity Manager itself is making
   9974             // the request.
   9975             Binder token = new Binder();
   9976             sCallerIdentity.set(new Identity(
   9977                     token, Binder.getCallingPid(), Binder.getCallingUid()));
   9978             try {
   9979                 pfd = cph.provider.openFile(null, uri, "r", null, token);
   9980             } catch (FileNotFoundException e) {
   9981                 // do nothing; pfd will be returned null
   9982             } finally {
   9983                 // Ensure that whatever happens, we clean up the identity state
   9984                 sCallerIdentity.remove();
   9985             }
   9986 
   9987             // We've got the fd now, so we're done with the provider.
   9988             removeContentProviderExternalUnchecked(name, null, userId);
   9989         } else {
   9990             Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
   9991         }
   9992         return pfd;
   9993     }
   9994 
   9995     // Actually is sleeping or shutting down or whatever else in the future
   9996     // is an inactive state.
   9997     public boolean isSleepingOrShuttingDown() {
   9998         return isSleeping() || mShuttingDown;
   9999     }
   10000 
   10001     public boolean isSleeping() {
   10002         return mSleeping;
   10003     }
   10004 
   10005     void onWakefulnessChanged(int wakefulness) {
   10006         synchronized(this) {
   10007             mWakefulness = wakefulness;
   10008             updateSleepIfNeededLocked();
   10009         }
   10010     }
   10011 
   10012     void finishRunningVoiceLocked() {
   10013         if (mRunningVoice) {
   10014             mRunningVoice = false;
   10015             updateSleepIfNeededLocked();
   10016         }
   10017     }
   10018 
   10019     void updateSleepIfNeededLocked() {
   10020         if (mSleeping && !shouldSleepLocked()) {
   10021             mSleeping = false;
   10022             mStackSupervisor.comeOutOfSleepIfNeededLocked();
   10023         } else if (!mSleeping && shouldSleepLocked()) {
   10024             mSleeping = true;
   10025             mStackSupervisor.goingToSleepLocked();
   10026 
   10027             // Initialize the wake times of all processes.
   10028             checkExcessivePowerUsageLocked(false);
   10029             mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
   10030             Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
   10031             mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
   10032         }
   10033     }
   10034 
   10035     private boolean shouldSleepLocked() {
   10036         // Resume applications while running a voice interactor.
   10037         if (mRunningVoice) {
   10038             return false;
   10039         }
   10040 
   10041         switch (mWakefulness) {
   10042             case PowerManagerInternal.WAKEFULNESS_AWAKE:
   10043             case PowerManagerInternal.WAKEFULNESS_DREAMING:
   10044                 // If we're interactive but applications are already paused then defer
   10045                 // resuming them until the lock screen is hidden.
   10046                 return mSleeping && mLockScreenShown != LOCK_SCREEN_HIDDEN;
   10047             case PowerManagerInternal.WAKEFULNESS_DOZING:
   10048                 // If we're dozing then pause applications whenever the lock screen is shown.
   10049                 return mLockScreenShown != LOCK_SCREEN_HIDDEN;
   10050             case PowerManagerInternal.WAKEFULNESS_ASLEEP:
   10051             default:
   10052                 // If we're asleep then pause applications unconditionally.
   10053                 return true;
   10054         }
   10055     }
   10056 
   10057     /** Pokes the task persister. */
   10058     void notifyTaskPersisterLocked(TaskRecord task, boolean flush) {
   10059         if (task != null && task.stack != null && task.stack.isHomeStack()) {
   10060             // Never persist the home stack.
   10061             return;
   10062         }
   10063         mTaskPersister.wakeup(task, flush);
   10064     }
   10065 
   10066     /** Notifies all listeners when the task stack has changed. */
   10067     void notifyTaskStackChangedLocked() {
   10068         mHandler.removeMessages(NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG);
   10069         Message nmsg = mHandler.obtainMessage(NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG);
   10070         mHandler.sendMessageDelayed(nmsg, NOTIFY_TASK_STACK_CHANGE_LISTENERS_DELAY);
   10071     }
   10072 
   10073     @Override
   10074     public boolean shutdown(int timeout) {
   10075         if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
   10076                 != PackageManager.PERMISSION_GRANTED) {
   10077             throw new SecurityException("Requires permission "
   10078                     + android.Manifest.permission.SHUTDOWN);
   10079         }
   10080 
   10081         boolean timedout = false;
   10082 
   10083         synchronized(this) {
   10084             mShuttingDown = true;
   10085             updateEventDispatchingLocked();
   10086             timedout = mStackSupervisor.shutdownLocked(timeout);
   10087         }
   10088 
   10089         mAppOpsService.shutdown();
   10090         if (mUsageStatsService != null) {
   10091             mUsageStatsService.prepareShutdown();
   10092         }
   10093         mBatteryStatsService.shutdown();
   10094         synchronized (this) {
   10095             mProcessStats.shutdownLocked();
   10096             notifyTaskPersisterLocked(null, true);
   10097         }
   10098 
   10099         return timedout;
   10100     }
   10101 
   10102     public final void activitySlept(IBinder token) {
   10103         if (localLOGV) Slog.v(TAG, "Activity slept: token=" + token);
   10104 
   10105         final long origId = Binder.clearCallingIdentity();
   10106 
   10107         synchronized (this) {
   10108             final ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10109             if (r != null) {
   10110                 mStackSupervisor.activitySleptLocked(r);
   10111             }
   10112         }
   10113 
   10114         Binder.restoreCallingIdentity(origId);
   10115     }
   10116 
   10117     private String lockScreenShownToString() {
   10118         switch (mLockScreenShown) {
   10119             case LOCK_SCREEN_HIDDEN: return "LOCK_SCREEN_HIDDEN";
   10120             case LOCK_SCREEN_LEAVING: return "LOCK_SCREEN_LEAVING";
   10121             case LOCK_SCREEN_SHOWN: return "LOCK_SCREEN_SHOWN";
   10122             default: return "Unknown=" + mLockScreenShown;
   10123         }
   10124     }
   10125 
   10126     void logLockScreen(String msg) {
   10127         if (DEBUG_LOCKSCREEN) Slog.d(TAG, Debug.getCallers(2) + ":" + msg
   10128                 + " mLockScreenShown=" + lockScreenShownToString() + " mWakefulness="
   10129                 + PowerManagerInternal.wakefulnessToString(mWakefulness)
   10130                 + " mSleeping=" + mSleeping);
   10131     }
   10132 
   10133     void startRunningVoiceLocked() {
   10134         if (!mRunningVoice) {
   10135             mRunningVoice = true;
   10136             updateSleepIfNeededLocked();
   10137         }
   10138     }
   10139 
   10140     private void updateEventDispatchingLocked() {
   10141         mWindowManager.setEventDispatching(mBooted && !mShuttingDown);
   10142     }
   10143 
   10144     public void setLockScreenShown(boolean shown) {
   10145         if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
   10146                 != PackageManager.PERMISSION_GRANTED) {
   10147             throw new SecurityException("Requires permission "
   10148                     + android.Manifest.permission.DEVICE_POWER);
   10149         }
   10150 
   10151         synchronized(this) {
   10152             long ident = Binder.clearCallingIdentity();
   10153             try {
   10154                 if (DEBUG_LOCKSCREEN) logLockScreen(" shown=" + shown);
   10155                 mLockScreenShown = shown ? LOCK_SCREEN_SHOWN : LOCK_SCREEN_HIDDEN;
   10156                 updateSleepIfNeededLocked();
   10157             } finally {
   10158                 Binder.restoreCallingIdentity(ident);
   10159             }
   10160         }
   10161     }
   10162 
   10163     @Override
   10164     public void stopAppSwitches() {
   10165         if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
   10166                 != PackageManager.PERMISSION_GRANTED) {
   10167             throw new SecurityException("Requires permission "
   10168                     + android.Manifest.permission.STOP_APP_SWITCHES);
   10169         }
   10170 
   10171         synchronized(this) {
   10172             mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
   10173                     + APP_SWITCH_DELAY_TIME;
   10174             mDidAppSwitch = false;
   10175             mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
   10176             Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
   10177             mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
   10178         }
   10179     }
   10180 
   10181     public void resumeAppSwitches() {
   10182         if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
   10183                 != PackageManager.PERMISSION_GRANTED) {
   10184             throw new SecurityException("Requires permission "
   10185                     + android.Manifest.permission.STOP_APP_SWITCHES);
   10186         }
   10187 
   10188         synchronized(this) {
   10189             // Note that we don't execute any pending app switches... we will
   10190             // let those wait until either the timeout, or the next start
   10191             // activity request.
   10192             mAppSwitchesAllowedTime = 0;
   10193         }
   10194     }
   10195 
   10196     boolean checkAppSwitchAllowedLocked(int sourcePid, int sourceUid,
   10197             int callingPid, int callingUid, String name) {
   10198         if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
   10199             return true;
   10200         }
   10201 
   10202         int perm = checkComponentPermission(
   10203                 android.Manifest.permission.STOP_APP_SWITCHES, sourcePid,
   10204                 sourceUid, -1, true);
   10205         if (perm == PackageManager.PERMISSION_GRANTED) {
   10206             return true;
   10207         }
   10208 
   10209         // If the actual IPC caller is different from the logical source, then
   10210         // also see if they are allowed to control app switches.
   10211         if (callingUid != -1 && callingUid != sourceUid) {
   10212             perm = checkComponentPermission(
   10213                     android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
   10214                     callingUid, -1, true);
   10215             if (perm == PackageManager.PERMISSION_GRANTED) {
   10216                 return true;
   10217             }
   10218         }
   10219 
   10220         Slog.w(TAG, name + " request from " + sourceUid + " stopped");
   10221         return false;
   10222     }
   10223 
   10224     public void setDebugApp(String packageName, boolean waitForDebugger,
   10225             boolean persistent) {
   10226         enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
   10227                 "setDebugApp()");
   10228 
   10229         long ident = Binder.clearCallingIdentity();
   10230         try {
   10231             // Note that this is not really thread safe if there are multiple
   10232             // callers into it at the same time, but that's not a situation we
   10233             // care about.
   10234             if (persistent) {
   10235                 final ContentResolver resolver = mContext.getContentResolver();
   10236                 Settings.Global.putString(
   10237                     resolver, Settings.Global.DEBUG_APP,
   10238                     packageName);
   10239                 Settings.Global.putInt(
   10240                     resolver, Settings.Global.WAIT_FOR_DEBUGGER,
   10241                     waitForDebugger ? 1 : 0);
   10242             }
   10243 
   10244             synchronized (this) {
   10245                 if (!persistent) {
   10246                     mOrigDebugApp = mDebugApp;
   10247                     mOrigWaitForDebugger = mWaitForDebugger;
   10248                 }
   10249                 mDebugApp = packageName;
   10250                 mWaitForDebugger = waitForDebugger;
   10251                 mDebugTransient = !persistent;
   10252                 if (packageName != null) {
   10253                     forceStopPackageLocked(packageName, -1, false, false, true, true,
   10254                             false, UserHandle.USER_ALL, "set debug app");
   10255                 }
   10256             }
   10257         } finally {
   10258             Binder.restoreCallingIdentity(ident);
   10259         }
   10260     }
   10261 
   10262     void setOpenGlTraceApp(ApplicationInfo app, String processName) {
   10263         synchronized (this) {
   10264             boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
   10265             if (!isDebuggable) {
   10266                 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
   10267                     throw new SecurityException("Process not debuggable: " + app.packageName);
   10268                 }
   10269             }
   10270 
   10271             mOpenGlTraceApp = processName;
   10272         }
   10273     }
   10274 
   10275     void setProfileApp(ApplicationInfo app, String processName, ProfilerInfo profilerInfo) {
   10276         synchronized (this) {
   10277             boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
   10278             if (!isDebuggable) {
   10279                 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
   10280                     throw new SecurityException("Process not debuggable: " + app.packageName);
   10281                 }
   10282             }
   10283             mProfileApp = processName;
   10284             mProfileFile = profilerInfo.profileFile;
   10285             if (mProfileFd != null) {
   10286                 try {
   10287                     mProfileFd.close();
   10288                 } catch (IOException e) {
   10289                 }
   10290                 mProfileFd = null;
   10291             }
   10292             mProfileFd = profilerInfo.profileFd;
   10293             mSamplingInterval = profilerInfo.samplingInterval;
   10294             mAutoStopProfiler = profilerInfo.autoStopProfiler;
   10295             mProfileType = 0;
   10296         }
   10297     }
   10298 
   10299     @Override
   10300     public void setAlwaysFinish(boolean enabled) {
   10301         enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
   10302                 "setAlwaysFinish()");
   10303 
   10304         Settings.Global.putInt(
   10305                 mContext.getContentResolver(),
   10306                 Settings.Global.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
   10307 
   10308         synchronized (this) {
   10309             mAlwaysFinishActivities = enabled;
   10310         }
   10311     }
   10312 
   10313     @Override
   10314     public void setActivityController(IActivityController controller) {
   10315         enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
   10316                 "setActivityController()");
   10317         synchronized (this) {
   10318             mController = controller;
   10319             Watchdog.getInstance().setActivityController(controller);
   10320         }
   10321     }
   10322 
   10323     @Override
   10324     public void setUserIsMonkey(boolean userIsMonkey) {
   10325         synchronized (this) {
   10326             synchronized (mPidsSelfLocked) {
   10327                 final int callingPid = Binder.getCallingPid();
   10328                 ProcessRecord precessRecord = mPidsSelfLocked.get(callingPid);
   10329                 if (precessRecord == null) {
   10330                     throw new SecurityException("Unknown process: " + callingPid);
   10331                 }
   10332                 if (precessRecord.instrumentationUiAutomationConnection  == null) {
   10333                     throw new SecurityException("Only an instrumentation process "
   10334                             + "with a UiAutomation can call setUserIsMonkey");
   10335                 }
   10336             }
   10337             mUserIsMonkey = userIsMonkey;
   10338         }
   10339     }
   10340 
   10341     @Override
   10342     public boolean isUserAMonkey() {
   10343         synchronized (this) {
   10344             // If there is a controller also implies the user is a monkey.
   10345             return (mUserIsMonkey || mController != null);
   10346         }
   10347     }
   10348 
   10349     public void requestBugReport() {
   10350         enforceCallingPermission(android.Manifest.permission.DUMP, "requestBugReport");
   10351         SystemProperties.set("ctl.start", "bugreport");
   10352     }
   10353 
   10354     public static long getInputDispatchingTimeoutLocked(ActivityRecord r) {
   10355         return r != null ? getInputDispatchingTimeoutLocked(r.app) : KEY_DISPATCHING_TIMEOUT;
   10356     }
   10357 
   10358     public static long getInputDispatchingTimeoutLocked(ProcessRecord r) {
   10359         if (r != null && (r.instrumentationClass != null || r.usingWrapper)) {
   10360             return INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT;
   10361         }
   10362         return KEY_DISPATCHING_TIMEOUT;
   10363     }
   10364 
   10365     @Override
   10366     public long inputDispatchingTimedOut(int pid, final boolean aboveSystem, String reason) {
   10367         if (checkCallingPermission(android.Manifest.permission.FILTER_EVENTS)
   10368                 != PackageManager.PERMISSION_GRANTED) {
   10369             throw new SecurityException("Requires permission "
   10370                     + android.Manifest.permission.FILTER_EVENTS);
   10371         }
   10372         ProcessRecord proc;
   10373         long timeout;
   10374         synchronized (this) {
   10375             synchronized (mPidsSelfLocked) {
   10376                 proc = mPidsSelfLocked.get(pid);
   10377             }
   10378             timeout = getInputDispatchingTimeoutLocked(proc);
   10379         }
   10380 
   10381         if (!inputDispatchingTimedOut(proc, null, null, aboveSystem, reason)) {
   10382             return -1;
   10383         }
   10384 
   10385         return timeout;
   10386     }
   10387 
   10388     /**
   10389      * Handle input dispatching timeouts.
   10390      * Returns whether input dispatching should be aborted or not.
   10391      */
   10392     public boolean inputDispatchingTimedOut(final ProcessRecord proc,
   10393             final ActivityRecord activity, final ActivityRecord parent,
   10394             final boolean aboveSystem, String reason) {
   10395         if (checkCallingPermission(android.Manifest.permission.FILTER_EVENTS)
   10396                 != PackageManager.PERMISSION_GRANTED) {
   10397             throw new SecurityException("Requires permission "
   10398                     + android.Manifest.permission.FILTER_EVENTS);
   10399         }
   10400 
   10401         final String annotation;
   10402         if (reason == null) {
   10403             annotation = "Input dispatching timed out";
   10404         } else {
   10405             annotation = "Input dispatching timed out (" + reason + ")";
   10406         }
   10407 
   10408         if (proc != null) {
   10409             synchronized (this) {
   10410                 if (proc.debugging) {
   10411                     return false;
   10412                 }
   10413 
   10414                 if (mDidDexOpt) {
   10415                     // Give more time since we were dexopting.
   10416                     mDidDexOpt = false;
   10417                     return false;
   10418                 }
   10419 
   10420                 if (proc.instrumentationClass != null) {
   10421                     Bundle info = new Bundle();
   10422                     info.putString("shortMsg", "keyDispatchingTimedOut");
   10423                     info.putString("longMsg", annotation);
   10424                     finishInstrumentationLocked(proc, Activity.RESULT_CANCELED, info);
   10425                     return true;
   10426                 }
   10427             }
   10428             mHandler.post(new Runnable() {
   10429                 @Override
   10430                 public void run() {
   10431                     appNotResponding(proc, activity, parent, aboveSystem, annotation);
   10432                 }
   10433             });
   10434         }
   10435 
   10436         return true;
   10437     }
   10438 
   10439     public Bundle getAssistContextExtras(int requestType) {
   10440         PendingAssistExtras pae = enqueueAssistContext(requestType, null, null,
   10441                 UserHandle.getCallingUserId());
   10442         if (pae == null) {
   10443             return null;
   10444         }
   10445         synchronized (pae) {
   10446             while (!pae.haveResult) {
   10447                 try {
   10448                     pae.wait();
   10449                 } catch (InterruptedException e) {
   10450                 }
   10451             }
   10452             if (pae.result != null) {
   10453                 pae.extras.putBundle(Intent.EXTRA_ASSIST_CONTEXT, pae.result);
   10454             }
   10455         }
   10456         synchronized (this) {
   10457             mPendingAssistExtras.remove(pae);
   10458             mHandler.removeCallbacks(pae);
   10459         }
   10460         return pae.extras;
   10461     }
   10462 
   10463     private PendingAssistExtras enqueueAssistContext(int requestType, Intent intent, String hint,
   10464             int userHandle) {
   10465         enforceCallingPermission(android.Manifest.permission.GET_TOP_ACTIVITY_INFO,
   10466                 "getAssistContextExtras()");
   10467         PendingAssistExtras pae;
   10468         Bundle extras = new Bundle();
   10469         synchronized (this) {
   10470             ActivityRecord activity = getFocusedStack().mResumedActivity;
   10471             if (activity == null) {
   10472                 Slog.w(TAG, "getAssistContextExtras failed: no resumed activity");
   10473                 return null;
   10474             }
   10475             extras.putString(Intent.EXTRA_ASSIST_PACKAGE, activity.packageName);
   10476             if (activity.app == null || activity.app.thread == null) {
   10477                 Slog.w(TAG, "getAssistContextExtras failed: no process for " + activity);
   10478                 return null;
   10479             }
   10480             if (activity.app.pid == Binder.getCallingPid()) {
   10481                 Slog.w(TAG, "getAssistContextExtras failed: request process same as " + activity);
   10482                 return null;
   10483             }
   10484             pae = new PendingAssistExtras(activity, extras, intent, hint, userHandle);
   10485             try {
   10486                 activity.app.thread.requestAssistContextExtras(activity.appToken, pae,
   10487                         requestType);
   10488                 mPendingAssistExtras.add(pae);
   10489                 mHandler.postDelayed(pae, PENDING_ASSIST_EXTRAS_TIMEOUT);
   10490             } catch (RemoteException e) {
   10491                 Slog.w(TAG, "getAssistContextExtras failed: crash calling " + activity);
   10492                 return null;
   10493             }
   10494             return pae;
   10495         }
   10496     }
   10497 
   10498     public void reportAssistContextExtras(IBinder token, Bundle extras) {
   10499         PendingAssistExtras pae = (PendingAssistExtras)token;
   10500         synchronized (pae) {
   10501             pae.result = extras;
   10502             pae.haveResult = true;
   10503             pae.notifyAll();
   10504             if (pae.intent == null) {
   10505                 // Caller is just waiting for the result.
   10506                 return;
   10507             }
   10508         }
   10509 
   10510         // We are now ready to launch the assist activity.
   10511         synchronized (this) {
   10512             boolean exists = mPendingAssistExtras.remove(pae);
   10513             mHandler.removeCallbacks(pae);
   10514             if (!exists) {
   10515                 // Timed out.
   10516                 return;
   10517             }
   10518         }
   10519         pae.intent.replaceExtras(extras);
   10520         if (pae.hint != null) {
   10521             pae.intent.putExtra(pae.hint, true);
   10522         }
   10523         pae.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
   10524                 | Intent.FLAG_ACTIVITY_SINGLE_TOP
   10525                 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
   10526         closeSystemDialogs("assist");
   10527         try {
   10528             mContext.startActivityAsUser(pae.intent, new UserHandle(pae.userHandle));
   10529         } catch (ActivityNotFoundException e) {
   10530             Slog.w(TAG, "No activity to handle assist action.", e);
   10531         }
   10532     }
   10533 
   10534     public boolean launchAssistIntent(Intent intent, int requestType, String hint, int userHandle) {
   10535         return enqueueAssistContext(requestType, intent, hint, userHandle) != null;
   10536     }
   10537 
   10538     public void registerProcessObserver(IProcessObserver observer) {
   10539         enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
   10540                 "registerProcessObserver()");
   10541         synchronized (this) {
   10542             mProcessObservers.register(observer);
   10543         }
   10544     }
   10545 
   10546     @Override
   10547     public void unregisterProcessObserver(IProcessObserver observer) {
   10548         synchronized (this) {
   10549             mProcessObservers.unregister(observer);
   10550         }
   10551     }
   10552 
   10553     @Override
   10554     public boolean convertFromTranslucent(IBinder token) {
   10555         final long origId = Binder.clearCallingIdentity();
   10556         try {
   10557             synchronized (this) {
   10558                 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10559                 if (r == null) {
   10560                     return false;
   10561                 }
   10562                 final boolean translucentChanged = r.changeWindowTranslucency(true);
   10563                 if (translucentChanged) {
   10564                     r.task.stack.releaseBackgroundResources();
   10565                     mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
   10566                 }
   10567                 mWindowManager.setAppFullscreen(token, true);
   10568                 return translucentChanged;
   10569             }
   10570         } finally {
   10571             Binder.restoreCallingIdentity(origId);
   10572         }
   10573     }
   10574 
   10575     @Override
   10576     public boolean convertToTranslucent(IBinder token, ActivityOptions options) {
   10577         final long origId = Binder.clearCallingIdentity();
   10578         try {
   10579             synchronized (this) {
   10580                 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10581                 if (r == null) {
   10582                     return false;
   10583                 }
   10584                 int index = r.task.mActivities.lastIndexOf(r);
   10585                 if (index > 0) {
   10586                     ActivityRecord under = r.task.mActivities.get(index - 1);
   10587                     under.returningOptions = options;
   10588                 }
   10589                 final boolean translucentChanged = r.changeWindowTranslucency(false);
   10590                 if (translucentChanged) {
   10591                     r.task.stack.convertToTranslucent(r);
   10592                 }
   10593                 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
   10594                 mWindowManager.setAppFullscreen(token, false);
   10595                 return translucentChanged;
   10596             }
   10597         } finally {
   10598             Binder.restoreCallingIdentity(origId);
   10599         }
   10600     }
   10601 
   10602     @Override
   10603     public boolean requestVisibleBehind(IBinder token, boolean visible) {
   10604         final long origId = Binder.clearCallingIdentity();
   10605         try {
   10606             synchronized (this) {
   10607                 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10608                 if (r != null) {
   10609                     return mStackSupervisor.requestVisibleBehindLocked(r, visible);
   10610                 }
   10611             }
   10612             return false;
   10613         } finally {
   10614             Binder.restoreCallingIdentity(origId);
   10615         }
   10616     }
   10617 
   10618     @Override
   10619     public boolean isBackgroundVisibleBehind(IBinder token) {
   10620         final long origId = Binder.clearCallingIdentity();
   10621         try {
   10622             synchronized (this) {
   10623                 final ActivityStack stack = ActivityRecord.getStackLocked(token);
   10624                 final boolean visible = stack == null ? false : stack.hasVisibleBehindActivity();
   10625                 if (ActivityStackSupervisor.DEBUG_VISIBLE_BEHIND) Slog.d(TAG,
   10626                         "isBackgroundVisibleBehind: stack=" + stack + " visible=" + visible);
   10627                 return visible;
   10628             }
   10629         } finally {
   10630             Binder.restoreCallingIdentity(origId);
   10631         }
   10632     }
   10633 
   10634     @Override
   10635     public ActivityOptions getActivityOptions(IBinder token) {
   10636         final long origId = Binder.clearCallingIdentity();
   10637         try {
   10638             synchronized (this) {
   10639                 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10640                 if (r != null) {
   10641                     final ActivityOptions activityOptions = r.pendingOptions;
   10642                     r.pendingOptions = null;
   10643                     return activityOptions;
   10644                 }
   10645                 return null;
   10646             }
   10647         } finally {
   10648             Binder.restoreCallingIdentity(origId);
   10649         }
   10650     }
   10651 
   10652     @Override
   10653     public void setImmersive(IBinder token, boolean immersive) {
   10654         synchronized(this) {
   10655             final ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10656             if (r == null) {
   10657                 throw new IllegalArgumentException();
   10658             }
   10659             r.immersive = immersive;
   10660 
   10661             // update associated state if we're frontmost
   10662             if (r == mFocusedActivity) {
   10663                 if (DEBUG_IMMERSIVE) {
   10664                     Slog.d(TAG, "Frontmost changed immersion: "+ r);
   10665                 }
   10666                 applyUpdateLockStateLocked(r);
   10667             }
   10668         }
   10669     }
   10670 
   10671     @Override
   10672     public boolean isImmersive(IBinder token) {
   10673         synchronized (this) {
   10674             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10675             if (r == null) {
   10676                 throw new IllegalArgumentException();
   10677             }
   10678             return r.immersive;
   10679         }
   10680     }
   10681 
   10682     public boolean isTopActivityImmersive() {
   10683         enforceNotIsolatedCaller("startActivity");
   10684         synchronized (this) {
   10685             ActivityRecord r = getFocusedStack().topRunningActivityLocked(null);
   10686             return (r != null) ? r.immersive : false;
   10687         }
   10688     }
   10689 
   10690     @Override
   10691     public boolean isTopOfTask(IBinder token) {
   10692         synchronized (this) {
   10693             ActivityRecord r = ActivityRecord.isInStackLocked(token);
   10694             if (r == null) {
   10695                 throw new IllegalArgumentException();
   10696             }
   10697             return r.task.getTopActivity() == r;
   10698         }
   10699     }
   10700 
   10701     public final void enterSafeMode() {
   10702         synchronized(this) {
   10703             // It only makes sense to do this before the system is ready
   10704             // and started launching other packages.
   10705             if (!mSystemReady) {
   10706                 try {
   10707                     AppGlobals.getPackageManager().enterSafeMode();
   10708                 } catch (RemoteException e) {
   10709                 }
   10710             }
   10711 
   10712             mSafeMode = true;
   10713         }
   10714     }
   10715 
   10716     public final void showSafeModeOverlay() {
   10717         View v = LayoutInflater.from(mContext).inflate(
   10718                 com.android.internal.R.layout.safe_mode, null);
   10719         WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
   10720         lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
   10721         lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
   10722         lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
   10723         lp.gravity = Gravity.BOTTOM | Gravity.START;
   10724         lp.format = v.getBackground().getOpacity();
   10725         lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
   10726                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
   10727         lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
   10728         ((WindowManager)mContext.getSystemService(
   10729                 Context.WINDOW_SERVICE)).addView(v, lp);
   10730     }
   10731 
   10732     public void noteWakeupAlarm(IIntentSender sender, int sourceUid, String sourcePkg) {
   10733         if (!(sender instanceof PendingIntentRecord)) {
   10734             return;
   10735         }
   10736         BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
   10737         synchronized (stats) {
   10738             if (mBatteryStatsService.isOnBattery()) {
   10739                 mBatteryStatsService.enforceCallingPermission();
   10740                 PendingIntentRecord rec = (PendingIntentRecord)sender;
   10741                 int MY_UID = Binder.getCallingUid();
   10742                 int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
   10743                 BatteryStatsImpl.Uid.Pkg pkg =
   10744                     stats.getPackageStatsLocked(sourceUid >= 0 ? sourceUid : uid,
   10745                             sourcePkg != null ? sourcePkg : rec.key.packageName);
   10746                 pkg.incWakeupsLocked();
   10747             }
   10748         }
   10749     }
   10750 
   10751     public boolean killPids(int[] pids, String pReason, boolean secure) {
   10752         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
   10753             throw new SecurityException("killPids only available to the system");
   10754         }
   10755         String reason = (pReason == null) ? "Unknown" : pReason;
   10756         // XXX Note: don't acquire main activity lock here, because the window
   10757         // manager calls in with its locks held.
   10758 
   10759         boolean killed = false;
   10760         synchronized (mPidsSelfLocked) {
   10761             int[] types = new int[pids.length];
   10762             int worstType = 0;
   10763             for (int i=0; i<pids.length; i++) {
   10764                 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
   10765                 if (proc != null) {
   10766                     int type = proc.setAdj;
   10767                     types[i] = type;
   10768                     if (type > worstType) {
   10769                         worstType = type;
   10770                     }
   10771                 }
   10772             }
   10773 
   10774             // If the worst oom_adj is somewhere in the cached proc LRU range,
   10775             // then constrain it so we will kill all cached procs.
   10776             if (worstType < ProcessList.CACHED_APP_MAX_ADJ
   10777                     && worstType > ProcessList.CACHED_APP_MIN_ADJ) {
   10778                 worstType = ProcessList.CACHED_APP_MIN_ADJ;
   10779             }
   10780 
   10781             // If this is not a secure call, don't let it kill processes that
   10782             // are important.
   10783             if (!secure && worstType < ProcessList.SERVICE_ADJ) {
   10784                 worstType = ProcessList.SERVICE_ADJ;
   10785             }
   10786 
   10787             Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
   10788             for (int i=0; i<pids.length; i++) {
   10789                 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
   10790                 if (proc == null) {
   10791                     continue;
   10792                 }
   10793                 int adj = proc.setAdj;
   10794                 if (adj >= worstType && !proc.killedByAm) {
   10795                     proc.kill(reason, true);
   10796                     killed = true;
   10797                 }
   10798             }
   10799         }
   10800         return killed;
   10801     }
   10802 
   10803     @Override
   10804     public void killUid(int uid, String reason) {
   10805         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
   10806             throw new SecurityException("killUid only available to the system");
   10807         }
   10808         synchronized (this) {
   10809             killPackageProcessesLocked(null, UserHandle.getAppId(uid), UserHandle.getUserId(uid),
   10810                     ProcessList.FOREGROUND_APP_ADJ-1, false, true, true, false,
   10811                     reason != null ? reason : "kill uid");
   10812         }
   10813     }
   10814 
   10815     @Override
   10816     public boolean killProcessesBelowForeground(String reason) {
   10817         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
   10818             throw new SecurityException("killProcessesBelowForeground() only available to system");
   10819         }
   10820 
   10821         return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason);
   10822     }
   10823 
   10824     private boolean killProcessesBelowAdj(int belowAdj, String reason) {
   10825         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
   10826             throw new SecurityException("killProcessesBelowAdj() only available to system");
   10827         }
   10828 
   10829         boolean killed = false;
   10830         synchronized (mPidsSelfLocked) {
   10831             final int size = mPidsSelfLocked.size();
   10832             for (int i = 0; i < size; i++) {
   10833                 final int pid = mPidsSelfLocked.keyAt(i);
   10834                 final ProcessRecord proc = mPidsSelfLocked.valueAt(i);
   10835                 if (proc == null) continue;
   10836 
   10837                 final int adj = proc.setAdj;
   10838                 if (adj > belowAdj && !proc.killedByAm) {
   10839                     proc.kill(reason, true);
   10840                     killed = true;
   10841                 }
   10842             }
   10843         }
   10844         return killed;
   10845     }
   10846 
   10847     @Override
   10848     public void hang(final IBinder who, boolean allowRestart) {
   10849         if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
   10850                 != PackageManager.PERMISSION_GRANTED) {
   10851             throw new SecurityException("Requires permission "
   10852                     + android.Manifest.permission.SET_ACTIVITY_WATCHER);
   10853         }
   10854 
   10855         final IBinder.DeathRecipient death = new DeathRecipient() {
   10856             @Override
   10857             public void binderDied() {
   10858                 synchronized (this) {
   10859                     notifyAll();
   10860                 }
   10861             }
   10862         };
   10863 
   10864         try {
   10865             who.linkToDeath(death, 0);
   10866         } catch (RemoteException e) {
   10867             Slog.w(TAG, "hang: given caller IBinder is already dead.");
   10868             return;
   10869         }
   10870 
   10871         synchronized (this) {
   10872             Watchdog.getInstance().setAllowRestart(allowRestart);
   10873             Slog.i(TAG, "Hanging system process at request of pid " + Binder.getCallingPid());
   10874             synchronized (death) {
   10875                 while (who.isBinderAlive()) {
   10876                     try {
   10877                         death.wait();
   10878                     } catch (InterruptedException e) {
   10879                     }
   10880                 }
   10881             }
   10882             Watchdog.getInstance().setAllowRestart(true);
   10883         }
   10884     }
   10885 
   10886     @Override
   10887     public void restart() {
   10888         if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
   10889                 != PackageManager.PERMISSION_GRANTED) {
   10890             throw new SecurityException("Requires permission "
   10891                     + android.Manifest.permission.SET_ACTIVITY_WATCHER);
   10892         }
   10893 
   10894         Log.i(TAG, "Sending shutdown broadcast...");
   10895 
   10896         BroadcastReceiver br = new BroadcastReceiver() {
   10897             @Override public void onReceive(Context context, Intent intent) {
   10898                 // Now the broadcast is done, finish up the low-level shutdown.
   10899                 Log.i(TAG, "Shutting down activity manager...");
   10900                 shutdown(10000);
   10901                 Log.i(TAG, "Shutdown complete, restarting!");
   10902                 Process.killProcess(Process.myPid());
   10903                 System.exit(10);
   10904             }
   10905         };
   10906 
   10907         // First send the high-level shut down broadcast.
   10908         Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
   10909         intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
   10910         intent.putExtra(Intent.EXTRA_SHUTDOWN_USERSPACE_ONLY, true);
   10911         /* For now we are not doing a clean shutdown, because things seem to get unhappy.
   10912         mContext.sendOrderedBroadcastAsUser(intent,
   10913                 UserHandle.ALL, null, br, mHandler, 0, null, null);
   10914         */
   10915         br.onReceive(mContext, intent);
   10916     }
   10917 
   10918     private long getLowRamTimeSinceIdle(long now) {
   10919         return mLowRamTimeSinceLastIdle + (mLowRamStartTime > 0 ? (now-mLowRamStartTime) : 0);
   10920     }
   10921 
   10922     @Override
   10923     public void performIdleMaintenance() {
   10924         if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
   10925                 != PackageManager.PERMISSION_GRANTED) {
   10926             throw new SecurityException("Requires permission "
   10927                     + android.Manifest.permission.SET_ACTIVITY_WATCHER);
   10928         }
   10929 
   10930         synchronized (this) {
   10931             final long now = SystemClock.uptimeMillis();
   10932             final long timeSinceLastIdle = now - mLastIdleTime;
   10933             final long lowRamSinceLastIdle = getLowRamTimeSinceIdle(now);
   10934             mLastIdleTime = now;
   10935             mLowRamTimeSinceLastIdle = 0;
   10936             if (mLowRamStartTime != 0) {
   10937                 mLowRamStartTime = now;
   10938             }
   10939 
   10940             StringBuilder sb = new StringBuilder(128);
   10941             sb.append("Idle maintenance over ");
   10942             TimeUtils.formatDuration(timeSinceLastIdle, sb);
   10943             sb.append(" low RAM for ");
   10944             TimeUtils.formatDuration(lowRamSinceLastIdle, sb);
   10945             Slog.i(TAG, sb.toString());
   10946 
   10947             // If at least 1/3 of our time since the last idle period has been spent
   10948             // with RAM low, then we want to kill processes.
   10949             boolean doKilling = lowRamSinceLastIdle > (timeSinceLastIdle/3);
   10950 
   10951             for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
   10952                 ProcessRecord proc = mLruProcesses.get(i);
   10953                 if (proc.notCachedSinceIdle) {
   10954                     if (proc.setProcState > ActivityManager.PROCESS_STATE_TOP
   10955                             && proc.setProcState <= ActivityManager.PROCESS_STATE_SERVICE) {
   10956                         if (doKilling && proc.initialIdlePss != 0
   10957                                 && proc.lastPss > ((proc.initialIdlePss*3)/2)) {
   10958                             sb = new StringBuilder(128);
   10959                             sb.append("Kill");
   10960                             sb.append(proc.processName);
   10961                             sb.append(" in idle maint: pss=");
   10962                             sb.append(proc.lastPss);
   10963                             sb.append(", initialPss=");
   10964                             sb.append(proc.initialIdlePss);
   10965                             sb.append(", period=");
   10966                             TimeUtils.formatDuration(timeSinceLastIdle, sb);
   10967                             sb.append(", lowRamPeriod=");
   10968                             TimeUtils.formatDuration(lowRamSinceLastIdle, sb);
   10969                             Slog.wtfQuiet(TAG, sb.toString());
   10970                             proc.kill("idle maint (pss " + proc.lastPss
   10971                                     + " from " + proc.initialIdlePss + ")", true);
   10972                         }
   10973                     }
   10974                 } else if (proc.setProcState < ActivityManager.PROCESS_STATE_HOME) {
   10975                     proc.notCachedSinceIdle = true;
   10976                     proc.initialIdlePss = 0;
   10977                     proc.nextPssTime = ProcessList.computeNextPssTime(proc.curProcState, true,
   10978                             mTestPssMode, isSleeping(), now);
   10979                 }
   10980             }
   10981 
   10982             mHandler.removeMessages(REQUEST_ALL_PSS_MSG);
   10983             mHandler.sendEmptyMessageDelayed(REQUEST_ALL_PSS_MSG, 2*60*1000);
   10984         }
   10985     }
   10986 
   10987     private void retrieveSettings() {
   10988         final ContentResolver resolver = mContext.getContentResolver();
   10989         String debugApp = Settings.Global.getString(
   10990             resolver, Settings.Global.DEBUG_APP);
   10991         boolean waitForDebugger = Settings.Global.getInt(
   10992             resolver, Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0;
   10993         boolean alwaysFinishActivities = Settings.Global.getInt(
   10994             resolver, Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
   10995         boolean forceRtl = Settings.Global.getInt(
   10996                 resolver, Settings.Global.DEVELOPMENT_FORCE_RTL, 0) != 0;
   10997         // Transfer any global setting for forcing RTL layout, into a System Property
   10998         SystemProperties.set(Settings.Global.DEVELOPMENT_FORCE_RTL, forceRtl ? "1":"0");
   10999 
   11000         Configuration configuration = new Configuration();
   11001         Settings.System.getConfiguration(resolver, configuration);
   11002         if (forceRtl) {
   11003             // This will take care of setting the correct layout direction flags
   11004             configuration.setLayoutDirection(configuration.locale);
   11005         }
   11006 
   11007         synchronized (this) {
   11008             mDebugApp = mOrigDebugApp = debugApp;
   11009             mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
   11010             mAlwaysFinishActivities = alwaysFinishActivities;
   11011             // This happens before any activities are started, so we can
   11012             // change mConfiguration in-place.
   11013             updateConfigurationLocked(configuration, null, false, true);
   11014             if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
   11015         }
   11016     }
   11017 
   11018     /** Loads resources after the current configuration has been set. */
   11019     private void loadResourcesOnSystemReady() {
   11020         final Resources res = mContext.getResources();
   11021         mHasRecents = res.getBoolean(com.android.internal.R.bool.config_hasRecents);
   11022         mThumbnailWidth = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
   11023         mThumbnailHeight = res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
   11024     }
   11025 
   11026     public boolean testIsSystemReady() {
   11027         // no need to synchronize(this) just to read & return the value
   11028         return mSystemReady;
   11029     }
   11030 
   11031     private static File getCalledPreBootReceiversFile() {
   11032         File dataDir = Environment.getDataDirectory();
   11033         File systemDir = new File(dataDir, "system");
   11034         File fname = new File(systemDir, CALLED_PRE_BOOTS_FILENAME);
   11035         return fname;
   11036     }
   11037 
   11038     private static ArrayList<ComponentName> readLastDonePreBootReceivers() {
   11039         ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>();
   11040         File file = getCalledPreBootReceiversFile();
   11041         FileInputStream fis = null;
   11042         try {
   11043             fis = new FileInputStream(file);
   11044             DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048));
   11045             int fvers = dis.readInt();
   11046             if (fvers == LAST_PREBOOT_DELIVERED_FILE_VERSION) {
   11047                 String vers = dis.readUTF();
   11048                 String codename = dis.readUTF();
   11049                 String build = dis.readUTF();
   11050                 if (android.os.Build.VERSION.RELEASE.equals(vers)
   11051                         && android.os.Build.VERSION.CODENAME.equals(codename)
   11052                         && android.os.Build.VERSION.INCREMENTAL.equals(build)) {
   11053                     int num = dis.readInt();
   11054                     while (num > 0) {
   11055                         num--;
   11056                         String pkg = dis.readUTF();
   11057                         String cls = dis.readUTF();
   11058                         lastDoneReceivers.add(new ComponentName(pkg, cls));
   11059                     }
   11060                 }
   11061             }
   11062         } catch (FileNotFoundException e) {
   11063         } catch (IOException e) {
   11064             Slog.w(TAG, "Failure reading last done pre-boot receivers", e);
   11065         } finally {
   11066             if (fis != null) {
   11067                 try {
   11068                     fis.close();
   11069                 } catch (IOException e) {
   11070                 }
   11071             }
   11072         }
   11073         return lastDoneReceivers;
   11074     }
   11075 
   11076     private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) {
   11077         File file = getCalledPreBootReceiversFile();
   11078         FileOutputStream fos = null;
   11079         DataOutputStream dos = null;
   11080         try {
   11081             fos = new FileOutputStream(file);
   11082             dos = new DataOutputStream(new BufferedOutputStream(fos, 2048));
   11083             dos.writeInt(LAST_PREBOOT_DELIVERED_FILE_VERSION);
   11084             dos.writeUTF(android.os.Build.VERSION.RELEASE);
   11085             dos.writeUTF(android.os.Build.VERSION.CODENAME);
   11086             dos.writeUTF(android.os.Build.VERSION.INCREMENTAL);
   11087             dos.writeInt(list.size());
   11088             for (int i=0; i<list.size(); i++) {
   11089                 dos.writeUTF(list.get(i).getPackageName());
   11090                 dos.writeUTF(list.get(i).getClassName());
   11091             }
   11092         } catch (IOException e) {
   11093             Slog.w(TAG, "Failure writing last done pre-boot receivers", e);
   11094             file.delete();
   11095         } finally {
   11096             FileUtils.sync(fos);
   11097             if (dos != null) {
   11098                 try {
   11099                     dos.close();
   11100                 } catch (IOException e) {
   11101                     // TODO Auto-generated catch block
   11102                     e.printStackTrace();
   11103                 }
   11104             }
   11105         }
   11106     }
   11107 
   11108     private boolean deliverPreBootCompleted(final Runnable onFinishCallback,
   11109             ArrayList<ComponentName> doneReceivers, int userId) {
   11110         boolean waitingUpdate = false;
   11111         Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
   11112         List<ResolveInfo> ris = null;
   11113         try {
   11114             ris = AppGlobals.getPackageManager().queryIntentReceivers(
   11115                     intent, null, 0, userId);
   11116         } catch (RemoteException e) {
   11117         }
   11118         if (ris != null) {
   11119             for (int i=ris.size()-1; i>=0; i--) {
   11120                 if ((ris.get(i).activityInfo.applicationInfo.flags
   11121                         &ApplicationInfo.FLAG_SYSTEM) == 0) {
   11122                     ris.remove(i);
   11123                 }
   11124             }
   11125             intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
   11126 
   11127             // For User 0, load the version number. When delivering to a new user, deliver
   11128             // to all receivers.
   11129             if (userId == UserHandle.USER_OWNER) {
   11130                 ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers();
   11131                 for (int i=0; i<ris.size(); i++) {
   11132                     ActivityInfo ai = ris.get(i).activityInfo;
   11133                     ComponentName comp = new ComponentName(ai.packageName, ai.name);
   11134                     if (lastDoneReceivers.contains(comp)) {
   11135                         // We already did the pre boot receiver for this app with the current
   11136                         // platform version, so don't do it again...
   11137                         ris.remove(i);
   11138                         i--;
   11139                         // ...however, do keep it as one that has been done, so we don't
   11140                         // forget about it when rewriting the file of last done receivers.
   11141                         doneReceivers.add(comp);
   11142                     }
   11143                 }
   11144             }
   11145 
   11146             // If primary user, send broadcast to all available users, else just to userId
   11147             final int[] users = userId == UserHandle.USER_OWNER ? getUsersLocked()
   11148                     : new int[] { userId };
   11149             for (int i = 0; i < ris.size(); i++) {
   11150                 ActivityInfo ai = ris.get(i).activityInfo;
   11151                 ComponentName comp = new ComponentName(ai.packageName, ai.name);
   11152                 doneReceivers.add(comp);
   11153                 intent.setComponent(comp);
   11154                 for (int j=0; j<users.length; j++) {
   11155                     IIntentReceiver finisher = null;
   11156                     // On last receiver and user, set up a completion callback
   11157                     if (i == ris.size() - 1 && j == users.length - 1 && onFinishCallback != null) {
   11158                         finisher = new IIntentReceiver.Stub() {
   11159                             public void performReceive(Intent intent, int resultCode,
   11160                                     String data, Bundle extras, boolean ordered,
   11161                                     boolean sticky, int sendingUser) {
   11162                                 // The raw IIntentReceiver interface is called
   11163                                 // with the AM lock held, so redispatch to
   11164                                 // execute our code without the lock.
   11165                                 mHandler.post(onFinishCallback);
   11166                             }
   11167                         };
   11168                     }
   11169                     Slog.i(TAG, "Sending system update to " + intent.getComponent()
   11170                             + " for user " + users[j]);
   11171                     broadcastIntentLocked(null, null, intent, null, finisher,
   11172                             0, null, null, null, AppOpsManager.OP_NONE,
   11173                             true, false, MY_PID, Process.SYSTEM_UID,
   11174                             users[j]);
   11175                     if (finisher != null) {
   11176                         waitingUpdate = true;
   11177                     }
   11178                 }
   11179             }
   11180         }
   11181 
   11182         return waitingUpdate;
   11183     }
   11184 
   11185     public void systemReady(final Runnable goingCallback) {
   11186         synchronized(this) {
   11187             if (mSystemReady) {
   11188                 // If we're done calling all the receivers, run the next "boot phase" passed in
   11189                 // by the SystemServer
   11190                 if (goingCallback != null) {
   11191                     goingCallback.run();
   11192                 }
   11193                 return;
   11194             }
   11195 
   11196             // Make sure we have the current profile info, since it is needed for
   11197             // security checks.
   11198             updateCurrentProfileIdsLocked();
   11199 
   11200             if (mRecentTasks == null) {
   11201                 mRecentTasks = mTaskPersister.restoreTasksLocked();
   11202                 mTaskPersister.restoreTasksFromOtherDeviceLocked();
   11203                 cleanupRecentTasksLocked(UserHandle.USER_ALL);
   11204                 mTaskPersister.startPersisting();
   11205             }
   11206 
   11207             // Check to see if there are any update receivers to run.
   11208             if (!mDidUpdate) {
   11209                 if (mWaitingUpdate) {
   11210                     return;
   11211                 }
   11212                 final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>();
   11213                 mWaitingUpdate = deliverPreBootCompleted(new Runnable() {
   11214                     public void run() {
   11215                         synchronized (ActivityManagerService.this) {
   11216                             mDidUpdate = true;
   11217                         }
   11218                         writeLastDonePreBootReceivers(doneReceivers);
   11219                         showBootMessage(mContext.getText(R.string.android_upgrading_complete),
   11220                                 false);
   11221                         systemReady(goingCallback);
   11222                     }
   11223                 }, doneReceivers, UserHandle.USER_OWNER);
   11224 
   11225                 if (mWaitingUpdate) {
   11226                     return;
   11227                 }
   11228                 mDidUpdate = true;
   11229             }
   11230 
   11231             mAppOpsService.systemReady();
   11232             mSystemReady = true;
   11233         }
   11234 
   11235         ArrayList<ProcessRecord> procsToKill = null;
   11236         synchronized(mPidsSelfLocked) {
   11237             for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
   11238                 ProcessRecord proc = mPidsSelfLocked.valueAt(i);
   11239                 if (!isAllowedWhileBooting(proc.info)){
   11240                     if (procsToKill == null) {
   11241                         procsToKill = new ArrayList<ProcessRecord>();
   11242                     }
   11243                     procsToKill.add(proc);
   11244                 }
   11245             }
   11246         }
   11247 
   11248         synchronized(this) {
   11249             if (procsToKill != null) {
   11250                 for (int i=procsToKill.size()-1; i>=0; i--) {
   11251                     ProcessRecord proc = procsToKill.get(i);
   11252                     Slog.i(TAG, "Removing system update proc: " + proc);
   11253                     removeProcessLocked(proc, true, false, "system update done");
   11254                 }
   11255             }
   11256 
   11257             // Now that we have cleaned up any update processes, we
   11258             // are ready to start launching real processes and know that
   11259             // we won't trample on them any more.
   11260             mProcessesReady = true;
   11261         }
   11262 
   11263         Slog.i(TAG, "System now ready");
   11264         EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
   11265             SystemClock.uptimeMillis());
   11266 
   11267         synchronized(this) {
   11268             // Make sure we have no pre-ready processes sitting around.
   11269 
   11270             if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
   11271                 ResolveInfo ri = mContext.getPackageManager()
   11272                         .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
   11273                                 STOCK_PM_FLAGS);
   11274                 CharSequence errorMsg = null;
   11275                 if (ri != null) {
   11276                     ActivityInfo ai = ri.activityInfo;
   11277                     ApplicationInfo app = ai.applicationInfo;
   11278                     if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
   11279                         mTopAction = Intent.ACTION_FACTORY_TEST;
   11280                         mTopData = null;
   11281                         mTopComponent = new ComponentName(app.packageName,
   11282                                 ai.name);
   11283                     } else {
   11284                         errorMsg = mContext.getResources().getText(
   11285                                 com.android.internal.R.string.factorytest_not_system);
   11286                     }
   11287                 } else {
   11288                     errorMsg = mContext.getResources().getText(
   11289                             com.android.internal.R.string.factorytest_no_action);
   11290