ActivityThread.java revision c68c913d357e2955d4bd7ca52829071e531c7825
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
19import android.app.backup.BackupAgent;
20import android.content.BroadcastReceiver;
21import android.content.ComponentCallbacks2;
22import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.Context;
25import android.content.IContentProvider;
26import android.content.Intent;
27import android.content.IIntentReceiver;
28import android.content.pm.ActivityInfo;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.IPackageManager;
31import android.content.pm.InstrumentationInfo;
32import android.content.pm.PackageManager;
33import android.content.pm.PackageManager.NameNotFoundException;
34import android.content.pm.ProviderInfo;
35import android.content.pm.ServiceInfo;
36import android.content.res.AssetManager;
37import android.content.res.CompatibilityInfo;
38import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.sqlite.SQLiteDatabase;
41import android.database.sqlite.SQLiteDebug;
42import android.database.sqlite.SQLiteDebug.DbStats;
43import android.graphics.Bitmap;
44import android.graphics.Canvas;
45import android.net.IConnectivityManager;
46import android.net.Proxy;
47import android.net.ProxyProperties;
48import android.os.AsyncTask;
49import android.os.Bundle;
50import android.os.Debug;
51import android.os.Handler;
52import android.os.IBinder;
53import android.os.Looper;
54import android.os.Message;
55import android.os.MessageQueue;
56import android.os.ParcelFileDescriptor;
57import android.os.Process;
58import android.os.RemoteException;
59import android.os.ServiceManager;
60import android.os.StrictMode;
61import android.os.SystemClock;
62import android.util.AndroidRuntimeException;
63import android.util.DisplayMetrics;
64import android.util.EventLog;
65import android.util.Log;
66import android.util.LogPrinter;
67import android.util.Slog;
68import android.view.Display;
69import android.view.HardwareRenderer;
70import android.view.View;
71import android.view.ViewDebug;
72import android.view.ViewManager;
73import android.view.ViewRootImpl;
74import android.view.Window;
75import android.view.WindowManager;
76import android.view.WindowManagerImpl;
77
78import com.android.internal.os.BinderInternal;
79import com.android.internal.os.RuntimeInit;
80import com.android.internal.os.SamplingProfilerIntegration;
81
82import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
83
84import java.io.File;
85import java.io.FileDescriptor;
86import java.io.FileOutputStream;
87import java.io.IOException;
88import java.io.PrintWriter;
89import java.lang.ref.WeakReference;
90import java.net.InetAddress;
91import java.util.ArrayList;
92import java.util.HashMap;
93import java.util.Iterator;
94import java.util.List;
95import java.util.Locale;
96import java.util.Map;
97import java.util.TimeZone;
98import java.util.regex.Pattern;
99
100import dalvik.system.CloseGuard;
101
102final class SuperNotCalledException extends AndroidRuntimeException {
103    public SuperNotCalledException(String msg) {
104        super(msg);
105    }
106}
107
108final class RemoteServiceException extends AndroidRuntimeException {
109    public RemoteServiceException(String msg) {
110        super(msg);
111    }
112}
113
114/**
115 * This manages the execution of the main thread in an
116 * application process, scheduling and executing activities,
117 * broadcasts, and other operations on it as the activity
118 * manager requests.
119 *
120 * {@hide}
121 */
122public final class ActivityThread {
123    /** @hide */
124    public static final String TAG = "ActivityThread";
125    private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
126    static final boolean localLOGV = false;
127    static final boolean DEBUG_MESSAGES = false;
128    /** @hide */
129    public static final boolean DEBUG_BROADCAST = false;
130    private static final boolean DEBUG_RESULTS = false;
131    private static final boolean DEBUG_BACKUP = true;
132    private static final boolean DEBUG_CONFIGURATION = false;
133    private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
134    private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
135    private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
136    private static final int LOG_ON_PAUSE_CALLED = 30021;
137    private static final int LOG_ON_RESUME_CALLED = 30022;
138
139    static ContextImpl mSystemContext = null;
140
141    static IPackageManager sPackageManager;
142
143    final ApplicationThread mAppThread = new ApplicationThread();
144    final Looper mLooper = Looper.myLooper();
145    final H mH = new H();
146    final HashMap<IBinder, ActivityClientRecord> mActivities
147            = new HashMap<IBinder, ActivityClientRecord>();
148    // List of new activities (via ActivityRecord.nextIdle) that should
149    // be reported when next we idle.
150    ActivityClientRecord mNewActivities = null;
151    // Number of activities that are currently visible on-screen.
152    int mNumVisibleActivities = 0;
153    final HashMap<IBinder, Service> mServices
154            = new HashMap<IBinder, Service>();
155    AppBindData mBoundApplication;
156    Configuration mConfiguration;
157    Configuration mCompatConfiguration;
158    Configuration mResConfiguration;
159    CompatibilityInfo mResCompatibilityInfo;
160    Application mInitialApplication;
161    final ArrayList<Application> mAllApplications
162            = new ArrayList<Application>();
163    // set of instantiated backup agents, keyed by package name
164    final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
165    static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
166    Instrumentation mInstrumentation;
167    String mInstrumentationAppDir = null;
168    String mInstrumentationAppPackage = null;
169    String mInstrumentedAppDir = null;
170    boolean mSystemThread = false;
171    boolean mJitEnabled = false;
172
173    // These can be accessed by multiple threads; mPackages is the lock.
174    // XXX For now we keep around information about all packages we have
175    // seen, not removing entries from this map.
176    // NOTE: The activity manager in its process needs to call in to
177    // ActivityThread to do things like update resource configurations,
178    // which means this lock gets held while the activity manager holds its
179    // own lock.  Thus you MUST NEVER call back into the activity manager
180    // or anything that depends on it while holding this lock.
181    final HashMap<String, WeakReference<LoadedApk>> mPackages
182            = new HashMap<String, WeakReference<LoadedApk>>();
183    final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
184            = new HashMap<String, WeakReference<LoadedApk>>();
185    final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
186            = new HashMap<CompatibilityInfo, DisplayMetrics>();
187    final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
188            = new HashMap<ResourcesKey, WeakReference<Resources> >();
189    final ArrayList<ActivityClientRecord> mRelaunchingActivities
190            = new ArrayList<ActivityClientRecord>();
191    Configuration mPendingConfiguration = null;
192
193    // The lock of mProviderMap protects the following variables.
194    final HashMap<String, ProviderClientRecord> mProviderMap
195        = new HashMap<String, ProviderClientRecord>();
196    final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
197        = new HashMap<IBinder, ProviderRefCount>();
198    final HashMap<IBinder, ProviderClientRecord> mLocalProviders
199        = new HashMap<IBinder, ProviderClientRecord>();
200
201    final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
202        = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
203
204    final GcIdler mGcIdler = new GcIdler();
205    boolean mGcIdlerScheduled = false;
206
207    static Handler sMainThreadHandler;  // set once in main()
208
209    Bundle mCoreSettings = null;
210
211    static final class ActivityClientRecord {
212        IBinder token;
213        int ident;
214        Intent intent;
215        Bundle state;
216        Activity activity;
217        Window window;
218        Activity parent;
219        String embeddedID;
220        Activity.NonConfigurationInstances lastNonConfigurationInstances;
221        boolean paused;
222        boolean stopped;
223        boolean hideForNow;
224        Configuration newConfig;
225        Configuration createdConfig;
226        ActivityClientRecord nextIdle;
227
228        ActivityInfo activityInfo;
229        CompatibilityInfo compatInfo;
230        LoadedApk packageInfo;
231
232        List<ResultInfo> pendingResults;
233        List<Intent> pendingIntents;
234
235        boolean startsNotResumed;
236        boolean isForward;
237        int pendingConfigChanges;
238        boolean onlyLocalRequest;
239
240        View mPendingRemoveWindow;
241        WindowManager mPendingRemoveWindowManager;
242
243        ActivityClientRecord() {
244            parent = null;
245            embeddedID = null;
246            paused = false;
247            stopped = false;
248            hideForNow = false;
249            nextIdle = null;
250        }
251
252        public boolean isPreHoneycomb() {
253            if (activity != null) {
254                return activity.getApplicationInfo().targetSdkVersion
255                        < android.os.Build.VERSION_CODES.HONEYCOMB;
256            }
257            return false;
258        }
259
260        public String toString() {
261            ComponentName componentName = intent.getComponent();
262            return "ActivityRecord{"
263                + Integer.toHexString(System.identityHashCode(this))
264                + " token=" + token + " " + (componentName == null
265                        ? "no component name" : componentName.toShortString())
266                + "}";
267        }
268    }
269
270    final class ProviderClientRecord implements IBinder.DeathRecipient {
271        final String mName;
272        final IContentProvider mProvider;
273        final ContentProvider mLocalProvider;
274
275        ProviderClientRecord(String name, IContentProvider provider,
276                ContentProvider localProvider) {
277            mName = name;
278            mProvider = provider;
279            mLocalProvider = localProvider;
280        }
281
282        public void binderDied() {
283            removeDeadProvider(mName, mProvider);
284        }
285    }
286
287    static final class NewIntentData {
288        List<Intent> intents;
289        IBinder token;
290        public String toString() {
291            return "NewIntentData{intents=" + intents + " token=" + token + "}";
292        }
293    }
294
295    static final class ReceiverData extends BroadcastReceiver.PendingResult {
296        public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
297                boolean ordered, boolean sticky, IBinder token) {
298            super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
299            this.intent = intent;
300        }
301
302        Intent intent;
303        ActivityInfo info;
304        CompatibilityInfo compatInfo;
305        public String toString() {
306            return "ReceiverData{intent=" + intent + " packageName=" +
307                    info.packageName + " resultCode=" + getResultCode()
308                    + " resultData=" + getResultData() + " resultExtras="
309                    + getResultExtras(false) + "}";
310        }
311    }
312
313    static final class CreateBackupAgentData {
314        ApplicationInfo appInfo;
315        CompatibilityInfo compatInfo;
316        int backupMode;
317        public String toString() {
318            return "CreateBackupAgentData{appInfo=" + appInfo
319                    + " backupAgent=" + appInfo.backupAgentName
320                    + " mode=" + backupMode + "}";
321        }
322    }
323
324    static final class CreateServiceData {
325        IBinder token;
326        ServiceInfo info;
327        CompatibilityInfo compatInfo;
328        Intent intent;
329        public String toString() {
330            return "CreateServiceData{token=" + token + " className="
331            + info.name + " packageName=" + info.packageName
332            + " intent=" + intent + "}";
333        }
334    }
335
336    static final class BindServiceData {
337        IBinder token;
338        Intent intent;
339        boolean rebind;
340        public String toString() {
341            return "BindServiceData{token=" + token + " intent=" + intent + "}";
342        }
343    }
344
345    static final class ServiceArgsData {
346        IBinder token;
347        boolean taskRemoved;
348        int startId;
349        int flags;
350        Intent args;
351        public String toString() {
352            return "ServiceArgsData{token=" + token + " startId=" + startId
353            + " args=" + args + "}";
354        }
355    }
356
357    static final class AppBindData {
358        LoadedApk info;
359        String processName;
360        ApplicationInfo appInfo;
361        List<ProviderInfo> providers;
362        ComponentName instrumentationName;
363        String profileFile;
364        Bundle instrumentationArgs;
365        IInstrumentationWatcher instrumentationWatcher;
366        int debugMode;
367        boolean restrictedBackupMode;
368        Configuration config;
369        CompatibilityInfo compatInfo;
370        boolean handlingProfiling;
371        public String toString() {
372            return "AppBindData{appInfo=" + appInfo + "}";
373        }
374    }
375
376    static final class DumpComponentInfo {
377        ParcelFileDescriptor fd;
378        IBinder token;
379        String prefix;
380        String[] args;
381    }
382
383    static final class ResultData {
384        IBinder token;
385        List<ResultInfo> results;
386        public String toString() {
387            return "ResultData{token=" + token + " results" + results + "}";
388        }
389    }
390
391    static final class ContextCleanupInfo {
392        ContextImpl context;
393        String what;
394        String who;
395    }
396
397    static final class ProfilerControlData {
398        String path;
399        ParcelFileDescriptor fd;
400    }
401
402    static final class DumpHeapData {
403        String path;
404        ParcelFileDescriptor fd;
405    }
406
407    static final class UpdateCompatibilityData {
408        String pkg;
409        CompatibilityInfo info;
410    }
411
412    private native void dumpGraphicsInfo(FileDescriptor fd);
413
414    private class ApplicationThread extends ApplicationThreadNative {
415        private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
416        private static final String ONE_COUNT_COLUMN = "%21s %8d";
417        private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
418        private static final String TWO_COUNT_COLUMNS_DB = "%21s %8d %21s %8d";
419        private static final String DB_INFO_FORMAT = "  %8s %8s %14s %14s  %s";
420
421        // Formatting for checkin service - update version if row format changes
422        private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
423
424        public final void schedulePauseActivity(IBinder token, boolean finished,
425                boolean userLeaving, int configChanges) {
426            queueOrSendMessage(
427                    finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
428                    token,
429                    (userLeaving ? 1 : 0),
430                    configChanges);
431        }
432
433        public final void scheduleStopActivity(IBinder token, boolean showWindow,
434                int configChanges) {
435           queueOrSendMessage(
436                showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
437                token, 0, configChanges);
438        }
439
440        public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
441            queueOrSendMessage(
442                showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
443                token);
444        }
445
446        public final void scheduleSleeping(IBinder token, boolean sleeping) {
447            queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
448        }
449
450        public final void scheduleResumeActivity(IBinder token, boolean isForward) {
451            queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
452        }
453
454        public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
455            ResultData res = new ResultData();
456            res.token = token;
457            res.results = results;
458            queueOrSendMessage(H.SEND_RESULT, res);
459        }
460
461        // we use token to identify this activity without having to send the
462        // activity itself back to the activity manager. (matters more with ipc)
463        public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
464                ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
465                List<ResultInfo> pendingResults,
466                List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
467            ActivityClientRecord r = new ActivityClientRecord();
468
469            r.token = token;
470            r.ident = ident;
471            r.intent = intent;
472            r.activityInfo = info;
473            r.compatInfo = compatInfo;
474            r.state = state;
475
476            r.pendingResults = pendingResults;
477            r.pendingIntents = pendingNewIntents;
478
479            r.startsNotResumed = notResumed;
480            r.isForward = isForward;
481
482            queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
483        }
484
485        public final void scheduleRelaunchActivity(IBinder token,
486                List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
487                int configChanges, boolean notResumed, Configuration config) {
488            requestRelaunchActivity(token, pendingResults, pendingNewIntents,
489                    configChanges, notResumed, config, true);
490        }
491
492        public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
493            NewIntentData data = new NewIntentData();
494            data.intents = intents;
495            data.token = token;
496
497            queueOrSendMessage(H.NEW_INTENT, data);
498        }
499
500        public final void scheduleDestroyActivity(IBinder token, boolean finishing,
501                int configChanges) {
502            queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
503                    configChanges);
504        }
505
506        public final void scheduleReceiver(Intent intent, ActivityInfo info,
507                CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
508                boolean sync) {
509            ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
510                    sync, false, mAppThread.asBinder());
511            r.info = info;
512            r.compatInfo = compatInfo;
513            queueOrSendMessage(H.RECEIVER, r);
514        }
515
516        public final void scheduleCreateBackupAgent(ApplicationInfo app,
517                CompatibilityInfo compatInfo, int backupMode) {
518            CreateBackupAgentData d = new CreateBackupAgentData();
519            d.appInfo = app;
520            d.compatInfo = compatInfo;
521            d.backupMode = backupMode;
522
523            queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
524        }
525
526        public final void scheduleDestroyBackupAgent(ApplicationInfo app,
527                CompatibilityInfo compatInfo) {
528            CreateBackupAgentData d = new CreateBackupAgentData();
529            d.appInfo = app;
530            d.compatInfo = compatInfo;
531
532            queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
533        }
534
535        public final void scheduleCreateService(IBinder token,
536                ServiceInfo info, CompatibilityInfo compatInfo) {
537            CreateServiceData s = new CreateServiceData();
538            s.token = token;
539            s.info = info;
540            s.compatInfo = compatInfo;
541
542            queueOrSendMessage(H.CREATE_SERVICE, s);
543        }
544
545        public final void scheduleBindService(IBinder token, Intent intent,
546                boolean rebind) {
547            BindServiceData s = new BindServiceData();
548            s.token = token;
549            s.intent = intent;
550            s.rebind = rebind;
551
552            queueOrSendMessage(H.BIND_SERVICE, s);
553        }
554
555        public final void scheduleUnbindService(IBinder token, Intent intent) {
556            BindServiceData s = new BindServiceData();
557            s.token = token;
558            s.intent = intent;
559
560            queueOrSendMessage(H.UNBIND_SERVICE, s);
561        }
562
563        public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
564            int flags ,Intent args) {
565            ServiceArgsData s = new ServiceArgsData();
566            s.token = token;
567            s.taskRemoved = taskRemoved;
568            s.startId = startId;
569            s.flags = flags;
570            s.args = args;
571
572            queueOrSendMessage(H.SERVICE_ARGS, s);
573        }
574
575        public final void scheduleStopService(IBinder token) {
576            queueOrSendMessage(H.STOP_SERVICE, token);
577        }
578
579        public final void bindApplication(String processName,
580                ApplicationInfo appInfo, List<ProviderInfo> providers,
581                ComponentName instrumentationName, String profileFile,
582                Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
583                int debugMode, boolean isRestrictedBackupMode, Configuration config,
584                CompatibilityInfo compatInfo, Map<String, IBinder> services,
585                Bundle coreSettings) {
586
587            if (services != null) {
588                // Setup the service cache in the ServiceManager
589                ServiceManager.initServiceCache(services);
590            }
591
592            setCoreSettings(coreSettings);
593
594            AppBindData data = new AppBindData();
595            data.processName = processName;
596            data.appInfo = appInfo;
597            data.providers = providers;
598            data.instrumentationName = instrumentationName;
599            data.profileFile = profileFile;
600            data.instrumentationArgs = instrumentationArgs;
601            data.instrumentationWatcher = instrumentationWatcher;
602            data.debugMode = debugMode;
603            data.restrictedBackupMode = isRestrictedBackupMode;
604            data.config = config;
605            data.compatInfo = compatInfo;
606            queueOrSendMessage(H.BIND_APPLICATION, data);
607        }
608
609        public final void scheduleExit() {
610            queueOrSendMessage(H.EXIT_APPLICATION, null);
611        }
612
613        public final void scheduleSuicide() {
614            queueOrSendMessage(H.SUICIDE, null);
615        }
616
617        public void requestThumbnail(IBinder token) {
618            queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
619        }
620
621        public void scheduleConfigurationChanged(Configuration config) {
622            synchronized (mPackages) {
623                if (mPendingConfiguration == null ||
624                        mPendingConfiguration.isOtherSeqNewer(config)) {
625                    mPendingConfiguration = config;
626                }
627            }
628            queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
629        }
630
631        public void updateTimeZone() {
632            TimeZone.setDefault(null);
633        }
634
635        public void clearDnsCache() {
636            // a non-standard API to get this to libcore
637            InetAddress.clearDnsCache();
638        }
639
640        public void setHttpProxy(String host, String port, String exclList) {
641            Proxy.setHttpProxySystemProperty(host, port, exclList);
642        }
643
644        public void processInBackground() {
645            mH.removeMessages(H.GC_WHEN_IDLE);
646            mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
647        }
648
649        public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
650            DumpComponentInfo data = new DumpComponentInfo();
651            try {
652                data.fd = ParcelFileDescriptor.dup(fd);
653                data.token = servicetoken;
654                data.args = args;
655                queueOrSendMessage(H.DUMP_SERVICE, data);
656            } catch (IOException e) {
657                Slog.w(TAG, "dumpService failed", e);
658            }
659        }
660
661        // This function exists to make sure all receiver dispatching is
662        // correctly ordered, since these are one-way calls and the binder driver
663        // applies transaction ordering per object for such calls.
664        public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
665                int resultCode, String dataStr, Bundle extras, boolean ordered,
666                boolean sticky) throws RemoteException {
667            receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
668        }
669
670        public void scheduleLowMemory() {
671            queueOrSendMessage(H.LOW_MEMORY, null);
672        }
673
674        public void scheduleActivityConfigurationChanged(IBinder token) {
675            queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
676        }
677
678        public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
679                int profileType) {
680            ProfilerControlData pcd = new ProfilerControlData();
681            pcd.path = path;
682            pcd.fd = fd;
683            queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
684        }
685
686        public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
687            DumpHeapData dhd = new DumpHeapData();
688            dhd.path = path;
689            dhd.fd = fd;
690            queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
691        }
692
693        public void setSchedulingGroup(int group) {
694            // Note: do this immediately, since going into the foreground
695            // should happen regardless of what pending work we have to do
696            // and the activity manager will wait for us to report back that
697            // we are done before sending us to the background.
698            try {
699                Process.setProcessGroup(Process.myPid(), group);
700            } catch (Exception e) {
701                Slog.w(TAG, "Failed setting process group to " + group, e);
702            }
703        }
704
705        public void getMemoryInfo(Debug.MemoryInfo outInfo) {
706            Debug.getMemoryInfo(outInfo);
707        }
708
709        public void dispatchPackageBroadcast(int cmd, String[] packages) {
710            queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
711        }
712
713        public void scheduleCrash(String msg) {
714            queueOrSendMessage(H.SCHEDULE_CRASH, msg);
715        }
716
717        public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
718                String prefix, String[] args) {
719            DumpComponentInfo data = new DumpComponentInfo();
720            try {
721                data.fd = ParcelFileDescriptor.dup(fd);
722                data.token = activitytoken;
723                data.prefix = prefix;
724                data.args = args;
725                queueOrSendMessage(H.DUMP_ACTIVITY, data);
726            } catch (IOException e) {
727                Slog.w(TAG, "dumpActivity failed", e);
728            }
729        }
730
731        @Override
732        public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, String[] args) {
733            FileOutputStream fout = new FileOutputStream(fd);
734            PrintWriter pw = new PrintWriter(fout);
735            try {
736                return dumpMemInfo(pw, args);
737            } finally {
738                pw.flush();
739            }
740        }
741
742        private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, String[] args) {
743            long nativeMax = Debug.getNativeHeapSize() / 1024;
744            long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
745            long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
746
747            Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
748            Debug.getMemoryInfo(memInfo);
749
750            Runtime runtime = Runtime.getRuntime();
751
752            long dalvikMax = runtime.totalMemory() / 1024;
753            long dalvikFree = runtime.freeMemory() / 1024;
754            long dalvikAllocated = dalvikMax - dalvikFree;
755            long viewInstanceCount = ViewDebug.getViewInstanceCount();
756            long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
757            long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
758            long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
759            int globalAssetCount = AssetManager.getGlobalAssetCount();
760            int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
761            int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
762            int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
763            int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
764            long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
765            long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
766            SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
767
768            // Check to see if we were called by checkin server. If so, print terse format.
769            boolean doCheckinFormat = false;
770            if (args != null) {
771                for (String arg : args) {
772                    if ("-c".equals(arg)) doCheckinFormat = true;
773                }
774            }
775
776            // For checkin, we print one long comma-separated list of values
777            if (doCheckinFormat) {
778                // NOTE: if you change anything significant below, also consider changing
779                // ACTIVITY_THREAD_CHECKIN_VERSION.
780                String processName = (mBoundApplication != null)
781                        ? mBoundApplication.processName : "unknown";
782
783                // Header
784                pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
785                pw.print(Process.myPid()); pw.print(',');
786                pw.print(processName); pw.print(',');
787
788                // Heap info - max
789                pw.print(nativeMax); pw.print(',');
790                pw.print(dalvikMax); pw.print(',');
791                pw.print("N/A,");
792                pw.print(nativeMax + dalvikMax); pw.print(',');
793
794                // Heap info - allocated
795                pw.print(nativeAllocated); pw.print(',');
796                pw.print(dalvikAllocated); pw.print(',');
797                pw.print("N/A,");
798                pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
799
800                // Heap info - free
801                pw.print(nativeFree); pw.print(',');
802                pw.print(dalvikFree); pw.print(',');
803                pw.print("N/A,");
804                pw.print(nativeFree + dalvikFree); pw.print(',');
805
806                // Heap info - proportional set size
807                pw.print(memInfo.nativePss); pw.print(',');
808                pw.print(memInfo.dalvikPss); pw.print(',');
809                pw.print(memInfo.otherPss); pw.print(',');
810                pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
811
812                // Heap info - shared
813                pw.print(memInfo.nativeSharedDirty); pw.print(',');
814                pw.print(memInfo.dalvikSharedDirty); pw.print(',');
815                pw.print(memInfo.otherSharedDirty); pw.print(',');
816                pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
817                        + memInfo.otherSharedDirty); pw.print(',');
818
819                // Heap info - private
820                pw.print(memInfo.nativePrivateDirty); pw.print(',');
821                pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
822                pw.print(memInfo.otherPrivateDirty); pw.print(',');
823                pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
824                        + memInfo.otherPrivateDirty); pw.print(',');
825
826                // Object counts
827                pw.print(viewInstanceCount); pw.print(',');
828                pw.print(viewRootInstanceCount); pw.print(',');
829                pw.print(appContextInstanceCount); pw.print(',');
830                pw.print(activityInstanceCount); pw.print(',');
831
832                pw.print(globalAssetCount); pw.print(',');
833                pw.print(globalAssetManagerCount); pw.print(',');
834                pw.print(binderLocalObjectCount); pw.print(',');
835                pw.print(binderProxyObjectCount); pw.print(',');
836
837                pw.print(binderDeathObjectCount); pw.print(',');
838                pw.print(openSslSocketCount); pw.print(',');
839
840                // SQL
841                pw.print(sqliteAllocated); pw.print(',');
842                pw.print(stats.memoryUsed / 1024); pw.print(',');
843                pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
844                pw.print(stats.largestMemAlloc / 1024); pw.print(',');
845                for (int i = 0; i < stats.dbStats.size(); i++) {
846                    DbStats dbStats = stats.dbStats.get(i);
847                    printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize,
848                            dbStats.lookaside, dbStats.cache, dbStats.dbName);
849                    pw.print(',');
850                }
851
852                return memInfo;
853            }
854
855            // otherwise, show human-readable format
856            printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
857            printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
858            printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
859                    "------");
860            printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
861                    memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
862            printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
863                    memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
864
865            int otherPss = memInfo.otherPss;
866            int otherSharedDirty = memInfo.otherSharedDirty;
867            int otherPrivateDirty = memInfo.otherPrivateDirty;
868
869            for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
870                printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
871                        memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
872                        memInfo.getOtherPrivateDirty(i), "", "", "");
873                otherPss -= memInfo.getOtherPss(i);
874                otherSharedDirty -= memInfo.getOtherSharedDirty(i);
875                otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
876            }
877
878            printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
879                    otherPrivateDirty, "", "", "");
880            printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
881                    memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
882                    nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
883                    nativeFree+dalvikFree);
884
885            pw.println(" ");
886            pw.println(" Objects");
887            printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
888                    viewRootInstanceCount);
889
890            printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
891                    "Activities:", activityInstanceCount);
892
893            printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
894                    "AssetManagers:", globalAssetManagerCount);
895
896            printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
897                    "Proxy Binders:", binderProxyObjectCount);
898            printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
899
900            printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
901
902            // SQLite mem info
903            pw.println(" ");
904            pw.println(" SQL");
905            printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
906                    stats.memoryUsed / 1024);
907            printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
908                    stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
909            pw.println(" ");
910            int N = stats.dbStats.size();
911            if (N > 0) {
912                pw.println(" DATABASES");
913                printRow(pw, "  %8s %8s %14s %14s  %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
914                        "Dbname");
915                for (int i = 0; i < N; i++) {
916                    DbStats dbStats = stats.dbStats.get(i);
917                    printRow(pw, DB_INFO_FORMAT,
918                            (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
919                            (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
920                            (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
921                            dbStats.cache, dbStats.dbName);
922                }
923            }
924
925            // Asset details.
926            String assetAlloc = AssetManager.getAssetAllocations();
927            if (assetAlloc != null) {
928                pw.println(" ");
929                pw.println(" Asset Allocations");
930                pw.print(assetAlloc);
931            }
932
933            return memInfo;
934        }
935
936        @Override
937        public void dumpGfxInfo(FileDescriptor fd, String[] args) {
938            dumpGraphicsInfo(fd);
939            WindowManagerImpl.getDefault().dumpGfxInfo(fd);
940        }
941
942        private void printRow(PrintWriter pw, String format, Object...objs) {
943            pw.println(String.format(format, objs));
944        }
945
946        public void setCoreSettings(Bundle coreSettings) {
947            queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
948        }
949
950        public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
951            UpdateCompatibilityData ucd = new UpdateCompatibilityData();
952            ucd.pkg = pkg;
953            ucd.info = info;
954            queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
955        }
956
957        public void scheduleTrimMemory(int level) {
958            queueOrSendMessage(H.TRIM_MEMORY, null, level);
959        }
960    }
961
962    private class H extends Handler {
963        public static final int LAUNCH_ACTIVITY         = 100;
964        public static final int PAUSE_ACTIVITY          = 101;
965        public static final int PAUSE_ACTIVITY_FINISHING= 102;
966        public static final int STOP_ACTIVITY_SHOW      = 103;
967        public static final int STOP_ACTIVITY_HIDE      = 104;
968        public static final int SHOW_WINDOW             = 105;
969        public static final int HIDE_WINDOW             = 106;
970        public static final int RESUME_ACTIVITY         = 107;
971        public static final int SEND_RESULT             = 108;
972        public static final int DESTROY_ACTIVITY        = 109;
973        public static final int BIND_APPLICATION        = 110;
974        public static final int EXIT_APPLICATION        = 111;
975        public static final int NEW_INTENT              = 112;
976        public static final int RECEIVER                = 113;
977        public static final int CREATE_SERVICE          = 114;
978        public static final int SERVICE_ARGS            = 115;
979        public static final int STOP_SERVICE            = 116;
980        public static final int REQUEST_THUMBNAIL       = 117;
981        public static final int CONFIGURATION_CHANGED   = 118;
982        public static final int CLEAN_UP_CONTEXT        = 119;
983        public static final int GC_WHEN_IDLE            = 120;
984        public static final int BIND_SERVICE            = 121;
985        public static final int UNBIND_SERVICE          = 122;
986        public static final int DUMP_SERVICE            = 123;
987        public static final int LOW_MEMORY              = 124;
988        public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
989        public static final int RELAUNCH_ACTIVITY       = 126;
990        public static final int PROFILER_CONTROL        = 127;
991        public static final int CREATE_BACKUP_AGENT     = 128;
992        public static final int DESTROY_BACKUP_AGENT    = 129;
993        public static final int SUICIDE                 = 130;
994        public static final int REMOVE_PROVIDER         = 131;
995        public static final int ENABLE_JIT              = 132;
996        public static final int DISPATCH_PACKAGE_BROADCAST = 133;
997        public static final int SCHEDULE_CRASH          = 134;
998        public static final int DUMP_HEAP               = 135;
999        public static final int DUMP_ACTIVITY           = 136;
1000        public static final int SLEEPING                = 137;
1001        public static final int SET_CORE_SETTINGS       = 138;
1002        public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
1003        public static final int TRIM_MEMORY             = 140;
1004        String codeToString(int code) {
1005            if (DEBUG_MESSAGES) {
1006                switch (code) {
1007                    case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1008                    case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1009                    case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1010                    case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1011                    case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1012                    case SHOW_WINDOW: return "SHOW_WINDOW";
1013                    case HIDE_WINDOW: return "HIDE_WINDOW";
1014                    case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1015                    case SEND_RESULT: return "SEND_RESULT";
1016                    case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1017                    case BIND_APPLICATION: return "BIND_APPLICATION";
1018                    case EXIT_APPLICATION: return "EXIT_APPLICATION";
1019                    case NEW_INTENT: return "NEW_INTENT";
1020                    case RECEIVER: return "RECEIVER";
1021                    case CREATE_SERVICE: return "CREATE_SERVICE";
1022                    case SERVICE_ARGS: return "SERVICE_ARGS";
1023                    case STOP_SERVICE: return "STOP_SERVICE";
1024                    case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1025                    case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1026                    case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1027                    case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1028                    case BIND_SERVICE: return "BIND_SERVICE";
1029                    case UNBIND_SERVICE: return "UNBIND_SERVICE";
1030                    case DUMP_SERVICE: return "DUMP_SERVICE";
1031                    case LOW_MEMORY: return "LOW_MEMORY";
1032                    case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1033                    case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
1034                    case PROFILER_CONTROL: return "PROFILER_CONTROL";
1035                    case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1036                    case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
1037                    case SUICIDE: return "SUICIDE";
1038                    case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
1039                    case ENABLE_JIT: return "ENABLE_JIT";
1040                    case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
1041                    case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
1042                    case DUMP_HEAP: return "DUMP_HEAP";
1043                    case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
1044                    case SLEEPING: return "SLEEPING";
1045                    case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
1046                    case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
1047                    case TRIM_MEMORY: return "TRIM_MEMORY";
1048                }
1049            }
1050            return "(unknown)";
1051        }
1052        public void handleMessage(Message msg) {
1053            if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
1054            switch (msg.what) {
1055                case LAUNCH_ACTIVITY: {
1056                    ActivityClientRecord r = (ActivityClientRecord)msg.obj;
1057
1058                    r.packageInfo = getPackageInfoNoCheck(
1059                            r.activityInfo.applicationInfo, r.compatInfo);
1060                    handleLaunchActivity(r, null);
1061                } break;
1062                case RELAUNCH_ACTIVITY: {
1063                    ActivityClientRecord r = (ActivityClientRecord)msg.obj;
1064                    handleRelaunchActivity(r);
1065                } break;
1066                case PAUSE_ACTIVITY:
1067                    handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
1068                    maybeSnapshot();
1069                    break;
1070                case PAUSE_ACTIVITY_FINISHING:
1071                    handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1072                    break;
1073                case STOP_ACTIVITY_SHOW:
1074                    handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1075                    break;
1076                case STOP_ACTIVITY_HIDE:
1077                    handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1078                    break;
1079                case SHOW_WINDOW:
1080                    handleWindowVisibility((IBinder)msg.obj, true);
1081                    break;
1082                case HIDE_WINDOW:
1083                    handleWindowVisibility((IBinder)msg.obj, false);
1084                    break;
1085                case RESUME_ACTIVITY:
1086                    handleResumeActivity((IBinder)msg.obj, true,
1087                            msg.arg1 != 0);
1088                    break;
1089                case SEND_RESULT:
1090                    handleSendResult((ResultData)msg.obj);
1091                    break;
1092                case DESTROY_ACTIVITY:
1093                    handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1094                            msg.arg2, false);
1095                    break;
1096                case BIND_APPLICATION:
1097                    AppBindData data = (AppBindData)msg.obj;
1098                    handleBindApplication(data);
1099                    break;
1100                case EXIT_APPLICATION:
1101                    if (mInitialApplication != null) {
1102                        mInitialApplication.onTerminate();
1103                    }
1104                    Looper.myLooper().quit();
1105                    break;
1106                case NEW_INTENT:
1107                    handleNewIntent((NewIntentData)msg.obj);
1108                    break;
1109                case RECEIVER:
1110                    handleReceiver((ReceiverData)msg.obj);
1111                    maybeSnapshot();
1112                    break;
1113                case CREATE_SERVICE:
1114                    handleCreateService((CreateServiceData)msg.obj);
1115                    break;
1116                case BIND_SERVICE:
1117                    handleBindService((BindServiceData)msg.obj);
1118                    break;
1119                case UNBIND_SERVICE:
1120                    handleUnbindService((BindServiceData)msg.obj);
1121                    break;
1122                case SERVICE_ARGS:
1123                    handleServiceArgs((ServiceArgsData)msg.obj);
1124                    break;
1125                case STOP_SERVICE:
1126                    handleStopService((IBinder)msg.obj);
1127                    maybeSnapshot();
1128                    break;
1129                case REQUEST_THUMBNAIL:
1130                    handleRequestThumbnail((IBinder)msg.obj);
1131                    break;
1132                case CONFIGURATION_CHANGED:
1133                    handleConfigurationChanged((Configuration)msg.obj, null);
1134                    break;
1135                case CLEAN_UP_CONTEXT:
1136                    ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1137                    cci.context.performFinalCleanup(cci.who, cci.what);
1138                    break;
1139                case GC_WHEN_IDLE:
1140                    scheduleGcIdler();
1141                    break;
1142                case DUMP_SERVICE:
1143                    handleDumpService((DumpComponentInfo)msg.obj);
1144                    break;
1145                case LOW_MEMORY:
1146                    handleLowMemory();
1147                    break;
1148                case ACTIVITY_CONFIGURATION_CHANGED:
1149                    handleActivityConfigurationChanged((IBinder)msg.obj);
1150                    break;
1151                case PROFILER_CONTROL:
1152                    handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
1153                    break;
1154                case CREATE_BACKUP_AGENT:
1155                    handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1156                    break;
1157                case DESTROY_BACKUP_AGENT:
1158                    handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1159                    break;
1160                case SUICIDE:
1161                    Process.killProcess(Process.myPid());
1162                    break;
1163                case REMOVE_PROVIDER:
1164                    completeRemoveProvider((IContentProvider)msg.obj);
1165                    break;
1166                case ENABLE_JIT:
1167                    ensureJitEnabled();
1168                    break;
1169                case DISPATCH_PACKAGE_BROADCAST:
1170                    handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1171                    break;
1172                case SCHEDULE_CRASH:
1173                    throw new RemoteServiceException((String)msg.obj);
1174                case DUMP_HEAP:
1175                    handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1176                    break;
1177                case DUMP_ACTIVITY:
1178                    handleDumpActivity((DumpComponentInfo)msg.obj);
1179                    break;
1180                case SLEEPING:
1181                    handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1182                    break;
1183                case SET_CORE_SETTINGS:
1184                    handleSetCoreSettings((Bundle) msg.obj);
1185                    break;
1186                case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1187                    handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
1188                    break;
1189                case TRIM_MEMORY:
1190                    handleTrimMemory(msg.arg1);
1191                    break;
1192            }
1193            if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
1194        }
1195
1196        private void maybeSnapshot() {
1197            if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
1198                // convert the *private* ActivityThread.PackageInfo to *public* known
1199                // android.content.pm.PackageInfo
1200                String packageName = mBoundApplication.info.mPackageName;
1201                android.content.pm.PackageInfo packageInfo = null;
1202                try {
1203                    Context context = getSystemContext();
1204                    if(context == null) {
1205                        Log.e(TAG, "cannot get a valid context");
1206                        return;
1207                    }
1208                    PackageManager pm = context.getPackageManager();
1209                    if(pm == null) {
1210                        Log.e(TAG, "cannot get a valid PackageManager");
1211                        return;
1212                    }
1213                    packageInfo = pm.getPackageInfo(
1214                            packageName, PackageManager.GET_ACTIVITIES);
1215                } catch (NameNotFoundException e) {
1216                    Log.e(TAG, "cannot get package info for " + packageName, e);
1217                }
1218                SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
1219            }
1220        }
1221    }
1222
1223    private class Idler implements MessageQueue.IdleHandler {
1224        public final boolean queueIdle() {
1225            ActivityClientRecord a = mNewActivities;
1226            if (a != null) {
1227                mNewActivities = null;
1228                IActivityManager am = ActivityManagerNative.getDefault();
1229                ActivityClientRecord prev;
1230                do {
1231                    if (localLOGV) Slog.v(
1232                        TAG, "Reporting idle of " + a +
1233                        " finished=" +
1234                        (a.activity != null && a.activity.mFinished));
1235                    if (a.activity != null && !a.activity.mFinished) {
1236                        try {
1237                            am.activityIdle(a.token, a.createdConfig);
1238                            a.createdConfig = null;
1239                        } catch (RemoteException ex) {
1240                            // Ignore
1241                        }
1242                    }
1243                    prev = a;
1244                    a = a.nextIdle;
1245                    prev.nextIdle = null;
1246                } while (a != null);
1247            }
1248            ensureJitEnabled();
1249            return false;
1250        }
1251    }
1252
1253    final class GcIdler implements MessageQueue.IdleHandler {
1254        public final boolean queueIdle() {
1255            doGcIfNeeded();
1256            return false;
1257        }
1258    }
1259
1260    private static class ResourcesKey {
1261        final private String mResDir;
1262        final private float mScale;
1263        final private int mHash;
1264
1265        ResourcesKey(String resDir, float scale) {
1266            mResDir = resDir;
1267            mScale = scale;
1268            mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1269        }
1270
1271        @Override
1272        public int hashCode() {
1273            return mHash;
1274        }
1275
1276        @Override
1277        public boolean equals(Object obj) {
1278            if (!(obj instanceof ResourcesKey)) {
1279                return false;
1280            }
1281            ResourcesKey peer = (ResourcesKey) obj;
1282            return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1283        }
1284    }
1285
1286    public static ActivityThread currentActivityThread() {
1287        return sThreadLocal.get();
1288    }
1289
1290    public static String currentPackageName() {
1291        ActivityThread am = currentActivityThread();
1292        return (am != null && am.mBoundApplication != null)
1293            ? am.mBoundApplication.processName : null;
1294    }
1295
1296    public static Application currentApplication() {
1297        ActivityThread am = currentActivityThread();
1298        return am != null ? am.mInitialApplication : null;
1299    }
1300
1301    public static IPackageManager getPackageManager() {
1302        if (sPackageManager != null) {
1303            //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1304            return sPackageManager;
1305        }
1306        IBinder b = ServiceManager.getService("package");
1307        //Slog.v("PackageManager", "default service binder = " + b);
1308        sPackageManager = IPackageManager.Stub.asInterface(b);
1309        //Slog.v("PackageManager", "default service = " + sPackageManager);
1310        return sPackageManager;
1311    }
1312
1313    DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1314        DisplayMetrics dm = mDisplayMetrics.get(ci);
1315        if (dm != null && !forceUpdate) {
1316            return dm;
1317        }
1318        if (dm == null) {
1319            dm = new DisplayMetrics();
1320            mDisplayMetrics.put(ci, dm);
1321        }
1322        Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1323        d.getMetrics(dm);
1324        //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1325        //        + metrics.heightPixels + " den=" + metrics.density
1326        //        + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
1327        return dm;
1328    }
1329
1330    static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1331        if (config == null) {
1332            return null;
1333        }
1334        if (compat != null && !compat.supportsScreen()) {
1335            config = new Configuration(config);
1336            compat.applyToConfiguration(config);
1337        }
1338        return config;
1339    }
1340
1341    private Configuration mMainThreadConfig = new Configuration();
1342    Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1343        if (config == null) {
1344            return null;
1345        }
1346        if (compat != null && !compat.supportsScreen()) {
1347            mMainThreadConfig.setTo(config);
1348            config = mMainThreadConfig;
1349            compat.applyToConfiguration(config);
1350        }
1351        return config;
1352    }
1353
1354    /**
1355     * Creates the top level Resources for applications with the given compatibility info.
1356     *
1357     * @param resDir the resource directory.
1358     * @param compInfo the compability info. It will use the default compatibility info when it's
1359     * null.
1360     */
1361    Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1362        ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1363        Resources r;
1364        synchronized (mPackages) {
1365            // Resources is app scale dependent.
1366            if (false) {
1367                Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1368                        + compInfo.applicationScale);
1369            }
1370            WeakReference<Resources> wr = mActiveResources.get(key);
1371            r = wr != null ? wr.get() : null;
1372            //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1373            if (r != null && r.getAssets().isUpToDate()) {
1374                if (false) {
1375                    Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1376                            + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1377                }
1378                return r;
1379            }
1380        }
1381
1382        //if (r != null) {
1383        //    Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1384        //            + r + " " + resDir);
1385        //}
1386
1387        AssetManager assets = new AssetManager();
1388        if (assets.addAssetPath(resDir) == 0) {
1389            return null;
1390        }
1391
1392        //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
1393        DisplayMetrics metrics = getDisplayMetricsLocked(compInfo, false);
1394        r = new Resources(assets, metrics, getConfiguration(), compInfo);
1395        if (false) {
1396            Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1397                    + r.getConfiguration() + " appScale="
1398                    + r.getCompatibilityInfo().applicationScale);
1399        }
1400
1401        synchronized (mPackages) {
1402            WeakReference<Resources> wr = mActiveResources.get(key);
1403            Resources existing = wr != null ? wr.get() : null;
1404            if (existing != null && existing.getAssets().isUpToDate()) {
1405                // Someone else already created the resources while we were
1406                // unlocked; go ahead and use theirs.
1407                r.getAssets().close();
1408                return existing;
1409            }
1410
1411            // XXX need to remove entries when weak references go away
1412            mActiveResources.put(key, new WeakReference<Resources>(r));
1413            return r;
1414        }
1415    }
1416
1417    /**
1418     * Creates the top level resources for the given package.
1419     */
1420    Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
1421        return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
1422    }
1423
1424    final Handler getHandler() {
1425        return mH;
1426    }
1427
1428    public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1429            int flags) {
1430        synchronized (mPackages) {
1431            WeakReference<LoadedApk> ref;
1432            if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1433                ref = mPackages.get(packageName);
1434            } else {
1435                ref = mResourcePackages.get(packageName);
1436            }
1437            LoadedApk packageInfo = ref != null ? ref.get() : null;
1438            //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
1439            //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1440            //        + ": " + packageInfo.mResources.getAssets().isUpToDate());
1441            if (packageInfo != null && (packageInfo.mResources == null
1442                    || packageInfo.mResources.getAssets().isUpToDate())) {
1443                if (packageInfo.isSecurityViolation()
1444                        && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1445                    throw new SecurityException(
1446                            "Requesting code from " + packageName
1447                            + " to be run in process "
1448                            + mBoundApplication.processName
1449                            + "/" + mBoundApplication.appInfo.uid);
1450                }
1451                return packageInfo;
1452            }
1453        }
1454
1455        ApplicationInfo ai = null;
1456        try {
1457            ai = getPackageManager().getApplicationInfo(packageName,
1458                    PackageManager.GET_SHARED_LIBRARY_FILES);
1459        } catch (RemoteException e) {
1460            // Ignore
1461        }
1462
1463        if (ai != null) {
1464            return getPackageInfo(ai, compatInfo, flags);
1465        }
1466
1467        return null;
1468    }
1469
1470    public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1471            int flags) {
1472        boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1473        boolean securityViolation = includeCode && ai.uid != 0
1474                && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1475                        ? ai.uid != mBoundApplication.appInfo.uid : true);
1476        if ((flags&(Context.CONTEXT_INCLUDE_CODE
1477                |Context.CONTEXT_IGNORE_SECURITY))
1478                == Context.CONTEXT_INCLUDE_CODE) {
1479            if (securityViolation) {
1480                String msg = "Requesting code from " + ai.packageName
1481                        + " (with uid " + ai.uid + ")";
1482                if (mBoundApplication != null) {
1483                    msg = msg + " to be run in process "
1484                        + mBoundApplication.processName + " (with uid "
1485                        + mBoundApplication.appInfo.uid + ")";
1486                }
1487                throw new SecurityException(msg);
1488            }
1489        }
1490        return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
1491    }
1492
1493    public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1494            CompatibilityInfo compatInfo) {
1495        return getPackageInfo(ai, compatInfo, null, false, true);
1496    }
1497
1498    public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1499        synchronized (mPackages) {
1500            WeakReference<LoadedApk> ref;
1501            if (includeCode) {
1502                ref = mPackages.get(packageName);
1503            } else {
1504                ref = mResourcePackages.get(packageName);
1505            }
1506            return ref != null ? ref.get() : null;
1507        }
1508    }
1509
1510    private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
1511            ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1512        synchronized (mPackages) {
1513            WeakReference<LoadedApk> ref;
1514            if (includeCode) {
1515                ref = mPackages.get(aInfo.packageName);
1516            } else {
1517                ref = mResourcePackages.get(aInfo.packageName);
1518            }
1519            LoadedApk packageInfo = ref != null ? ref.get() : null;
1520            if (packageInfo == null || (packageInfo.mResources != null
1521                    && !packageInfo.mResources.getAssets().isUpToDate())) {
1522                if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
1523                        : "Loading resource-only package ") + aInfo.packageName
1524                        + " (in " + (mBoundApplication != null
1525                                ? mBoundApplication.processName : null)
1526                        + ")");
1527                packageInfo =
1528                    new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
1529                            securityViolation, includeCode &&
1530                            (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1531                if (includeCode) {
1532                    mPackages.put(aInfo.packageName,
1533                            new WeakReference<LoadedApk>(packageInfo));
1534                } else {
1535                    mResourcePackages.put(aInfo.packageName,
1536                            new WeakReference<LoadedApk>(packageInfo));
1537                }
1538            }
1539            return packageInfo;
1540        }
1541    }
1542
1543    ActivityThread() {
1544    }
1545
1546    public ApplicationThread getApplicationThread()
1547    {
1548        return mAppThread;
1549    }
1550
1551    public Instrumentation getInstrumentation()
1552    {
1553        return mInstrumentation;
1554    }
1555
1556    public Configuration getConfiguration() {
1557        return mResConfiguration;
1558    }
1559
1560    public boolean isProfiling() {
1561        return mBoundApplication != null && mBoundApplication.profileFile != null;
1562    }
1563
1564    public String getProfileFilePath() {
1565        return mBoundApplication.profileFile;
1566    }
1567
1568    public Looper getLooper() {
1569        return mLooper;
1570    }
1571
1572    public Application getApplication() {
1573        return mInitialApplication;
1574    }
1575
1576    public String getProcessName() {
1577        return mBoundApplication.processName;
1578    }
1579
1580    public ContextImpl getSystemContext() {
1581        synchronized (this) {
1582            if (mSystemContext == null) {
1583                ContextImpl context =
1584                    ContextImpl.createSystemContext(this);
1585                LoadedApk info = new LoadedApk(this, "android", context, null,
1586                        CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
1587                context.init(info, null, this);
1588                context.getResources().updateConfiguration(
1589                        getConfiguration(), getDisplayMetricsLocked(
1590                                CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
1591                mSystemContext = context;
1592                //Slog.i(TAG, "Created system resources " + context.getResources()
1593                //        + ": " + context.getResources().getConfiguration());
1594            }
1595        }
1596        return mSystemContext;
1597    }
1598
1599    public void installSystemApplicationInfo(ApplicationInfo info) {
1600        synchronized (this) {
1601            ContextImpl context = getSystemContext();
1602            context.init(new LoadedApk(this, "android", context, info,
1603                    CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
1604        }
1605    }
1606
1607    void ensureJitEnabled() {
1608        if (!mJitEnabled) {
1609            mJitEnabled = true;
1610            dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1611        }
1612    }
1613
1614    void scheduleGcIdler() {
1615        if (!mGcIdlerScheduled) {
1616            mGcIdlerScheduled = true;
1617            Looper.myQueue().addIdleHandler(mGcIdler);
1618        }
1619        mH.removeMessages(H.GC_WHEN_IDLE);
1620    }
1621
1622    void unscheduleGcIdler() {
1623        if (mGcIdlerScheduled) {
1624            mGcIdlerScheduled = false;
1625            Looper.myQueue().removeIdleHandler(mGcIdler);
1626        }
1627        mH.removeMessages(H.GC_WHEN_IDLE);
1628    }
1629
1630    void doGcIfNeeded() {
1631        mGcIdlerScheduled = false;
1632        final long now = SystemClock.uptimeMillis();
1633        //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
1634        //        + "m now=" + now);
1635        if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
1636            //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
1637            BinderInternal.forceGc("bg");
1638        }
1639    }
1640
1641    public void registerOnActivityPausedListener(Activity activity,
1642            OnActivityPausedListener listener) {
1643        synchronized (mOnPauseListeners) {
1644            ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1645            if (list == null) {
1646                list = new ArrayList<OnActivityPausedListener>();
1647                mOnPauseListeners.put(activity, list);
1648            }
1649            list.add(listener);
1650        }
1651    }
1652
1653    public void unregisterOnActivityPausedListener(Activity activity,
1654            OnActivityPausedListener listener) {
1655        synchronized (mOnPauseListeners) {
1656            ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1657            if (list != null) {
1658                list.remove(listener);
1659            }
1660        }
1661    }
1662
1663    public final ActivityInfo resolveActivityInfo(Intent intent) {
1664        ActivityInfo aInfo = intent.resolveActivityInfo(
1665                mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1666        if (aInfo == null) {
1667            // Throw an exception.
1668            Instrumentation.checkStartActivityResult(
1669                    IActivityManager.START_CLASS_NOT_FOUND, intent);
1670        }
1671        return aInfo;
1672    }
1673
1674    public final Activity startActivityNow(Activity parent, String id,
1675        Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
1676        Activity.NonConfigurationInstances lastNonConfigurationInstances) {
1677        ActivityClientRecord r = new ActivityClientRecord();
1678            r.token = token;
1679            r.ident = 0;
1680            r.intent = intent;
1681            r.state = state;
1682            r.parent = parent;
1683            r.embeddedID = id;
1684            r.activityInfo = activityInfo;
1685            r.lastNonConfigurationInstances = lastNonConfigurationInstances;
1686        if (localLOGV) {
1687            ComponentName compname = intent.getComponent();
1688            String name;
1689            if (compname != null) {
1690                name = compname.toShortString();
1691            } else {
1692                name = "(Intent " + intent + ").getComponent() returned null";
1693            }
1694            Slog.v(TAG, "Performing launch: action=" + intent.getAction()
1695                    + ", comp=" + name
1696                    + ", token=" + token);
1697        }
1698        return performLaunchActivity(r, null);
1699    }
1700
1701    public final Activity getActivity(IBinder token) {
1702        return mActivities.get(token).activity;
1703    }
1704
1705    public final void sendActivityResult(
1706            IBinder token, String id, int requestCode,
1707            int resultCode, Intent data) {
1708        if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
1709                + " req=" + requestCode + " res=" + resultCode + " data=" + data);
1710        ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1711        list.add(new ResultInfo(id, requestCode, resultCode, data));
1712        mAppThread.scheduleSendResult(token, list);
1713    }
1714
1715    // if the thread hasn't started yet, we don't have the handler, so just
1716    // save the messages until we're ready.
1717    private void queueOrSendMessage(int what, Object obj) {
1718        queueOrSendMessage(what, obj, 0, 0);
1719    }
1720
1721    private void queueOrSendMessage(int what, Object obj, int arg1) {
1722        queueOrSendMessage(what, obj, arg1, 0);
1723    }
1724
1725    private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
1726        synchronized (this) {
1727            if (DEBUG_MESSAGES) Slog.v(
1728                TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1729                + ": " + arg1 + " / " + obj);
1730            Message msg = Message.obtain();
1731            msg.what = what;
1732            msg.obj = obj;
1733            msg.arg1 = arg1;
1734            msg.arg2 = arg2;
1735            mH.sendMessage(msg);
1736        }
1737    }
1738
1739    final void scheduleContextCleanup(ContextImpl context, String who,
1740            String what) {
1741        ContextCleanupInfo cci = new ContextCleanupInfo();
1742        cci.context = context;
1743        cci.who = who;
1744        cci.what = what;
1745        queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1746    }
1747
1748    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
1749        // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1750
1751        ActivityInfo aInfo = r.activityInfo;
1752        if (r.packageInfo == null) {
1753            r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
1754                    Context.CONTEXT_INCLUDE_CODE);
1755        }
1756
1757        ComponentName component = r.intent.getComponent();
1758        if (component == null) {
1759            component = r.intent.resolveActivity(
1760                mInitialApplication.getPackageManager());
1761            r.intent.setComponent(component);
1762        }
1763
1764        if (r.activityInfo.targetActivity != null) {
1765            component = new ComponentName(r.activityInfo.packageName,
1766                    r.activityInfo.targetActivity);
1767        }
1768
1769        Activity activity = null;
1770        try {
1771            java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1772            activity = mInstrumentation.newActivity(
1773                    cl, component.getClassName(), r.intent);
1774            StrictMode.incrementExpectedActivityCount(activity.getClass());
1775            r.intent.setExtrasClassLoader(cl);
1776            if (r.state != null) {
1777                r.state.setClassLoader(cl);
1778            }
1779        } catch (Exception e) {
1780            if (!mInstrumentation.onException(activity, e)) {
1781                throw new RuntimeException(
1782                    "Unable to instantiate activity " + component
1783                    + ": " + e.toString(), e);
1784            }
1785        }
1786
1787        try {
1788            Application app = r.packageInfo.makeApplication(false, mInstrumentation);
1789
1790            if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1791            if (localLOGV) Slog.v(
1792                    TAG, r + ": app=" + app
1793                    + ", appName=" + app.getPackageName()
1794                    + ", pkg=" + r.packageInfo.getPackageName()
1795                    + ", comp=" + r.intent.getComponent().toShortString()
1796                    + ", dir=" + r.packageInfo.getAppDir());
1797
1798            if (activity != null) {
1799                ContextImpl appContext = new ContextImpl();
1800                appContext.init(r.packageInfo, r.token, this);
1801                appContext.setOuterContext(activity);
1802                CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
1803                Configuration config = new Configuration(mCompatConfiguration);
1804                if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
1805                        + r.activityInfo.name + " with config " + config);
1806                activity.attach(appContext, this, getInstrumentation(), r.token,
1807                        r.ident, app, r.intent, r.activityInfo, title, r.parent,
1808                        r.embeddedID, r.lastNonConfigurationInstances, config);
1809
1810                if (customIntent != null) {
1811                    activity.mIntent = customIntent;
1812                }
1813                r.lastNonConfigurationInstances = null;
1814                activity.mStartedActivity = false;
1815                int theme = r.activityInfo.getThemeResource();
1816                if (theme != 0) {
1817                    activity.setTheme(theme);
1818                }
1819
1820                activity.mCalled = false;
1821                mInstrumentation.callActivityOnCreate(activity, r.state);
1822                if (!activity.mCalled) {
1823                    throw new SuperNotCalledException(
1824                        "Activity " + r.intent.getComponent().toShortString() +
1825                        " did not call through to super.onCreate()");
1826                }
1827                r.activity = activity;
1828                r.stopped = true;
1829                if (!r.activity.mFinished) {
1830                    activity.performStart();
1831                    r.stopped = false;
1832                }
1833                if (!r.activity.mFinished) {
1834                    if (r.state != null) {
1835                        mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1836                    }
1837                }
1838                if (!r.activity.mFinished) {
1839                    activity.mCalled = false;
1840                    mInstrumentation.callActivityOnPostCreate(activity, r.state);
1841                    if (!activity.mCalled) {
1842                        throw new SuperNotCalledException(
1843                            "Activity " + r.intent.getComponent().toShortString() +
1844                            " did not call through to super.onPostCreate()");
1845                    }
1846                }
1847            }
1848            r.paused = true;
1849
1850            mActivities.put(r.token, r);
1851
1852        } catch (SuperNotCalledException e) {
1853            throw e;
1854
1855        } catch (Exception e) {
1856            if (!mInstrumentation.onException(activity, e)) {
1857                throw new RuntimeException(
1858                    "Unable to start activity " + component
1859                    + ": " + e.toString(), e);
1860            }
1861        }
1862
1863        return activity;
1864    }
1865
1866    private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
1867        // If we are getting ready to gc after going to the background, well
1868        // we are back active so skip it.
1869        unscheduleGcIdler();
1870
1871        if (localLOGV) Slog.v(
1872            TAG, "Handling launch of " + r);
1873        Activity a = performLaunchActivity(r, customIntent);
1874
1875        if (a != null) {
1876            r.createdConfig = new Configuration(mConfiguration);
1877            Bundle oldState = r.state;
1878            handleResumeActivity(r.token, false, r.isForward);
1879
1880            if (!r.activity.mFinished && r.startsNotResumed) {
1881                // The activity manager actually wants this one to start out
1882                // paused, because it needs to be visible but isn't in the
1883                // foreground.  We accomplish this by going through the
1884                // normal startup (because activities expect to go through
1885                // onResume() the first time they run, before their window
1886                // is displayed), and then pausing it.  However, in this case
1887                // we do -not- need to do the full pause cycle (of freezing
1888                // and such) because the activity manager assumes it can just
1889                // retain the current state it has.
1890                try {
1891                    r.activity.mCalled = false;
1892                    mInstrumentation.callActivityOnPause(r.activity);
1893                    // We need to keep around the original state, in case
1894                    // we need to be created again.
1895                    r.state = oldState;
1896                    if (!r.activity.mCalled) {
1897                        throw new SuperNotCalledException(
1898                            "Activity " + r.intent.getComponent().toShortString() +
1899                            " did not call through to super.onPause()");
1900                    }
1901
1902                } catch (SuperNotCalledException e) {
1903                    throw e;
1904
1905                } catch (Exception e) {
1906                    if (!mInstrumentation.onException(r.activity, e)) {
1907                        throw new RuntimeException(
1908                                "Unable to pause activity "
1909                                + r.intent.getComponent().toShortString()
1910                                + ": " + e.toString(), e);
1911                    }
1912                }
1913                r.paused = true;
1914            }
1915        } else {
1916            // If there was an error, for any reason, tell the activity
1917            // manager to stop us.
1918            try {
1919                ActivityManagerNative.getDefault()
1920                    .finishActivity(r.token, Activity.RESULT_CANCELED, null);
1921            } catch (RemoteException ex) {
1922                // Ignore
1923            }
1924        }
1925    }
1926
1927    private void deliverNewIntents(ActivityClientRecord r,
1928            List<Intent> intents) {
1929        final int N = intents.size();
1930        for (int i=0; i<N; i++) {
1931            Intent intent = intents.get(i);
1932            intent.setExtrasClassLoader(r.activity.getClassLoader());
1933            r.activity.mFragments.noteStateNotSaved();
1934            mInstrumentation.callActivityOnNewIntent(r.activity, intent);
1935        }
1936    }
1937
1938    public final void performNewIntents(IBinder token,
1939            List<Intent> intents) {
1940        ActivityClientRecord r = mActivities.get(token);
1941        if (r != null) {
1942            final boolean resumed = !r.paused;
1943            if (resumed) {
1944                r.activity.mTemporaryPause = true;
1945                mInstrumentation.callActivityOnPause(r.activity);
1946            }
1947            deliverNewIntents(r, intents);
1948            if (resumed) {
1949                r.activity.performResume();
1950                r.activity.mTemporaryPause = false;
1951            }
1952        }
1953    }
1954
1955    private void handleNewIntent(NewIntentData data) {
1956        performNewIntents(data.token, data.intents);
1957    }
1958
1959    private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
1960
1961    /**
1962     * Return the Intent that's currently being handled by a
1963     * BroadcastReceiver on this thread, or null if none.
1964     * @hide
1965     */
1966    public static Intent getIntentBeingBroadcast() {
1967        return sCurrentBroadcastIntent.get();
1968    }
1969
1970    private void handleReceiver(ReceiverData data) {
1971        // If we are getting ready to gc after going to the background, well
1972        // we are back active so skip it.
1973        unscheduleGcIdler();
1974
1975        String component = data.intent.getComponent().getClassName();
1976
1977        LoadedApk packageInfo = getPackageInfoNoCheck(
1978                data.info.applicationInfo, data.compatInfo);
1979
1980        IActivityManager mgr = ActivityManagerNative.getDefault();
1981
1982        BroadcastReceiver receiver;
1983        try {
1984            java.lang.ClassLoader cl = packageInfo.getClassLoader();
1985            data.intent.setExtrasClassLoader(cl);
1986            data.setExtrasClassLoader(cl);
1987            receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
1988        } catch (Exception e) {
1989            if (DEBUG_BROADCAST) Slog.i(TAG,
1990                    "Finishing failed broadcast to " + data.intent.getComponent());
1991            data.sendFinished(mgr);
1992            throw new RuntimeException(
1993                "Unable to instantiate receiver " + component
1994                + ": " + e.toString(), e);
1995        }
1996
1997        try {
1998            Application app = packageInfo.makeApplication(false, mInstrumentation);
1999
2000            if (localLOGV) Slog.v(
2001                TAG, "Performing receive of " + data.intent
2002                + ": app=" + app
2003                + ", appName=" + app.getPackageName()
2004                + ", pkg=" + packageInfo.getPackageName()
2005                + ", comp=" + data.intent.getComponent().toShortString()
2006                + ", dir=" + packageInfo.getAppDir());
2007
2008            ContextImpl context = (ContextImpl)app.getBaseContext();
2009            sCurrentBroadcastIntent.set(data.intent);
2010            receiver.setPendingResult(data);
2011            receiver.onReceive(context.getReceiverRestrictedContext(),
2012                    data.intent);
2013        } catch (Exception e) {
2014            if (DEBUG_BROADCAST) Slog.i(TAG,
2015                    "Finishing failed broadcast to " + data.intent.getComponent());
2016            data.sendFinished(mgr);
2017            if (!mInstrumentation.onException(receiver, e)) {
2018                throw new RuntimeException(
2019                    "Unable to start receiver " + component
2020                    + ": " + e.toString(), e);
2021            }
2022        } finally {
2023            sCurrentBroadcastIntent.set(null);
2024        }
2025
2026        if (receiver.getPendingResult() != null) {
2027            data.finish();
2028        }
2029    }
2030
2031    // Instantiate a BackupAgent and tell it that it's alive
2032    private void handleCreateBackupAgent(CreateBackupAgentData data) {
2033        if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
2034
2035        // no longer idle; we have backup work to do
2036        unscheduleGcIdler();
2037
2038        // instantiate the BackupAgent class named in the manifest
2039        LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
2040        String packageName = packageInfo.mPackageName;
2041        if (mBackupAgents.get(packageName) != null) {
2042            Slog.d(TAG, "BackupAgent " + "  for " + packageName
2043                    + " already exists");
2044            return;
2045        }
2046
2047        BackupAgent agent = null;
2048        String classname = data.appInfo.backupAgentName;
2049
2050        // full backup operation but no app-supplied agent?  use the default implementation
2051        if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2052                || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
2053            classname = "android.app.backup.FullBackupAgent";
2054        }
2055
2056        try {
2057            IBinder binder = null;
2058            try {
2059                if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2060
2061                java.lang.ClassLoader cl = packageInfo.getClassLoader();
2062                agent = (BackupAgent) cl.loadClass(classname).newInstance();
2063
2064                // set up the agent's context
2065                ContextImpl context = new ContextImpl();
2066                context.init(packageInfo, null, this);
2067                context.setOuterContext(agent);
2068                agent.attach(context);
2069
2070                agent.onCreate();
2071                binder = agent.onBind();
2072                mBackupAgents.put(packageName, agent);
2073            } catch (Exception e) {
2074                // If this is during restore, fail silently; otherwise go
2075                // ahead and let the user see the crash.
2076                Slog.e(TAG, "Agent threw during creation: " + e);
2077                if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2078                        && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
2079                    throw e;
2080                }
2081                // falling through with 'binder' still null
2082            }
2083
2084            // tell the OS that we're live now
2085            try {
2086                ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2087            } catch (RemoteException e) {
2088                // nothing to do.
2089            }
2090        } catch (Exception e) {
2091            throw new RuntimeException("Unable to create BackupAgent "
2092                    + classname + ": " + e.toString(), e);
2093        }
2094    }
2095
2096    // Tear down a BackupAgent
2097    private void handleDestroyBackupAgent(CreateBackupAgentData data) {
2098        if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
2099
2100        LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
2101        String packageName = packageInfo.mPackageName;
2102        BackupAgent agent = mBackupAgents.get(packageName);
2103        if (agent != null) {
2104            try {
2105                agent.onDestroy();
2106            } catch (Exception e) {
2107                Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
2108                e.printStackTrace();
2109            }
2110            mBackupAgents.remove(packageName);
2111        } else {
2112            Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
2113        }
2114    }
2115
2116    private void handleCreateService(CreateServiceData data) {
2117        // If we are getting ready to gc after going to the background, well
2118        // we are back active so skip it.
2119        unscheduleGcIdler();
2120
2121        LoadedApk packageInfo = getPackageInfoNoCheck(
2122                data.info.applicationInfo, data.compatInfo);
2123        Service service = null;
2124        try {
2125            java.lang.ClassLoader cl = packageInfo.getClassLoader();
2126            service = (Service) cl.loadClass(data.info.name).newInstance();
2127        } catch (Exception e) {
2128            if (!mInstrumentation.onException(service, e)) {
2129                throw new RuntimeException(
2130                    "Unable to instantiate service " + data.info.name
2131                    + ": " + e.toString(), e);
2132            }
2133        }
2134
2135        try {
2136            if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
2137
2138            ContextImpl context = new ContextImpl();
2139            context.init(packageInfo, null, this);
2140
2141            Application app = packageInfo.makeApplication(false, mInstrumentation);
2142            context.setOuterContext(service);
2143            service.attach(context, this, data.info.name, data.token, app,
2144                    ActivityManagerNative.getDefault());
2145            service.onCreate();
2146            mServices.put(data.token, service);
2147            try {
2148                ActivityManagerNative.getDefault().serviceDoneExecuting(
2149                        data.token, 0, 0, 0);
2150            } catch (RemoteException e) {
2151                // nothing to do.
2152            }
2153        } catch (Exception e) {
2154            if (!mInstrumentation.onException(service, e)) {
2155                throw new RuntimeException(
2156                    "Unable to create service " + data.info.name
2157                    + ": " + e.toString(), e);
2158            }
2159        }
2160    }
2161
2162    private void handleBindService(BindServiceData data) {
2163        Service s = mServices.get(data.token);
2164        if (s != null) {
2165            try {
2166                data.intent.setExtrasClassLoader(s.getClassLoader());
2167                try {
2168                    if (!data.rebind) {
2169                        IBinder binder = s.onBind(data.intent);
2170                        ActivityManagerNative.getDefault().publishService(
2171                                data.token, data.intent, binder);
2172                    } else {
2173                        s.onRebind(data.intent);
2174                        ActivityManagerNative.getDefault().serviceDoneExecuting(
2175                                data.token, 0, 0, 0);
2176                    }
2177                    ensureJitEnabled();
2178                } catch (RemoteException ex) {
2179                }
2180            } catch (Exception e) {
2181                if (!mInstrumentation.onException(s, e)) {
2182                    throw new RuntimeException(
2183                            "Unable to bind to service " + s
2184                            + " with " + data.intent + ": " + e.toString(), e);
2185                }
2186            }
2187        }
2188    }
2189
2190    private void handleUnbindService(BindServiceData data) {
2191        Service s = mServices.get(data.token);
2192        if (s != null) {
2193            try {
2194                data.intent.setExtrasClassLoader(s.getClassLoader());
2195                boolean doRebind = s.onUnbind(data.intent);
2196                try {
2197                    if (doRebind) {
2198                        ActivityManagerNative.getDefault().unbindFinished(
2199                                data.token, data.intent, doRebind);
2200                    } else {
2201                        ActivityManagerNative.getDefault().serviceDoneExecuting(
2202                                data.token, 0, 0, 0);
2203                    }
2204                } catch (RemoteException ex) {
2205                }
2206            } catch (Exception e) {
2207                if (!mInstrumentation.onException(s, e)) {
2208                    throw new RuntimeException(
2209                            "Unable to unbind to service " + s
2210                            + " with " + data.intent + ": " + e.toString(), e);
2211                }
2212            }
2213        }
2214    }
2215
2216    private void handleDumpService(DumpComponentInfo info) {
2217        Service s = mServices.get(info.token);
2218        if (s != null) {
2219            PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2220            s.dump(info.fd.getFileDescriptor(), pw, info.args);
2221            pw.flush();
2222            try {
2223                info.fd.close();
2224            } catch (IOException e) {
2225            }
2226        }
2227    }
2228
2229    private void handleDumpActivity(DumpComponentInfo info) {
2230        ActivityClientRecord r = mActivities.get(info.token);
2231        if (r != null && r.activity != null) {
2232            PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2233            r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2234            pw.flush();
2235            try {
2236                info.fd.close();
2237            } catch (IOException e) {
2238            }
2239        }
2240    }
2241
2242    private void handleServiceArgs(ServiceArgsData data) {
2243        Service s = mServices.get(data.token);
2244        if (s != null) {
2245            try {
2246                if (data.args != null) {
2247                    data.args.setExtrasClassLoader(s.getClassLoader());
2248                }
2249                int res;
2250                if (!data.taskRemoved) {
2251                    res = s.onStartCommand(data.args, data.flags, data.startId);
2252                } else {
2253                    s.onTaskRemoved(data.args);
2254                    res = Service.START_TASK_REMOVED_COMPLETE;
2255                }
2256
2257                QueuedWork.waitToFinish();
2258
2259                try {
2260                    ActivityManagerNative.getDefault().serviceDoneExecuting(
2261                            data.token, 1, data.startId, res);
2262                } catch (RemoteException e) {
2263                    // nothing to do.
2264                }
2265                ensureJitEnabled();
2266            } catch (Exception e) {
2267                if (!mInstrumentation.onException(s, e)) {
2268                    throw new RuntimeException(
2269                            "Unable to start service " + s
2270                            + " with " + data.args + ": " + e.toString(), e);
2271                }
2272            }
2273        }
2274    }
2275
2276    private void handleStopService(IBinder token) {
2277        Service s = mServices.remove(token);
2278        if (s != null) {
2279            try {
2280                if (localLOGV) Slog.v(TAG, "Destroying service " + s);
2281                s.onDestroy();
2282                Context context = s.getBaseContext();
2283                if (context instanceof ContextImpl) {
2284                    final String who = s.getClassName();
2285                    ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
2286                }
2287
2288                QueuedWork.waitToFinish();
2289
2290                try {
2291                    ActivityManagerNative.getDefault().serviceDoneExecuting(
2292                            token, 0, 0, 0);
2293                } catch (RemoteException e) {
2294                    // nothing to do.
2295                }
2296            } catch (Exception e) {
2297                if (!mInstrumentation.onException(s, e)) {
2298                    throw new RuntimeException(
2299                            "Unable to stop service " + s
2300                            + ": " + e.toString(), e);
2301                }
2302            }
2303        }
2304        //Slog.i(TAG, "Running services: " + mServices);
2305    }
2306
2307    public final ActivityClientRecord performResumeActivity(IBinder token,
2308            boolean clearHide) {
2309        ActivityClientRecord r = mActivities.get(token);
2310        if (localLOGV) Slog.v(TAG, "Performing resume of " + r
2311                + " finished=" + r.activity.mFinished);
2312        if (r != null && !r.activity.mFinished) {
2313            if (clearHide) {
2314                r.hideForNow = false;
2315                r.activity.mStartedActivity = false;
2316            }
2317            try {
2318                if (r.pendingIntents != null) {
2319                    deliverNewIntents(r, r.pendingIntents);
2320                    r.pendingIntents = null;
2321                }
2322                if (r.pendingResults != null) {
2323                    deliverResults(r, r.pendingResults);
2324                    r.pendingResults = null;
2325                }
2326                r.activity.performResume();
2327
2328                EventLog.writeEvent(LOG_ON_RESUME_CALLED,
2329                        r.activity.getComponentName().getClassName());
2330
2331                r.paused = false;
2332                r.stopped = false;
2333                r.state = null;
2334            } catch (Exception e) {
2335                if (!mInstrumentation.onException(r.activity, e)) {
2336                    throw new RuntimeException(
2337                        "Unable to resume activity "
2338                        + r.intent.getComponent().toShortString()
2339                        + ": " + e.toString(), e);
2340                }
2341            }
2342        }
2343        return r;
2344    }
2345
2346    final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2347        if (r.mPendingRemoveWindow != null) {
2348            r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2349            IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2350            if (wtoken != null) {
2351                WindowManagerImpl.getDefault().closeAll(wtoken,
2352                        r.activity.getClass().getName(), "Activity");
2353            }
2354        }
2355        r.mPendingRemoveWindow = null;
2356        r.mPendingRemoveWindowManager = null;
2357    }
2358
2359    final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2360        // If we are getting ready to gc after going to the background, well
2361        // we are back active so skip it.
2362        unscheduleGcIdler();
2363
2364        ActivityClientRecord r = performResumeActivity(token, clearHide);
2365
2366        if (r != null) {
2367            final Activity a = r.activity;
2368
2369            if (localLOGV) Slog.v(
2370                TAG, "Resume " + r + " started activity: " +
2371                a.mStartedActivity + ", hideForNow: " + r.hideForNow
2372                + ", finished: " + a.mFinished);
2373
2374            final int forwardBit = isForward ?
2375                    WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
2376
2377            // If the window hasn't yet been added to the window manager,
2378            // and this guy didn't finish itself or start another activity,
2379            // then go ahead and add the window.
2380            boolean willBeVisible = !a.mStartedActivity;
2381            if (!willBeVisible) {
2382                try {
2383                    willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2384                            a.getActivityToken());
2385                } catch (RemoteException e) {
2386                }
2387            }
2388            if (r.window == null && !a.mFinished && willBeVisible) {
2389                r.window = r.activity.getWindow();
2390                View decor = r.window.getDecorView();
2391                decor.setVisibility(View.INVISIBLE);
2392                ViewManager wm = a.getWindowManager();
2393                WindowManager.LayoutParams l = r.window.getAttributes();
2394                a.mDecor = decor;
2395                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2396                l.softInputMode |= forwardBit;
2397                if (a.mVisibleFromClient) {
2398                    a.mWindowAdded = true;
2399                    wm.addView(decor, l);
2400                }
2401
2402            // If the window has already been added, but during resume
2403            // we started another activity, then don't yet make the
2404            // window visible.
2405            } else if (!willBeVisible) {
2406                if (localLOGV) Slog.v(
2407                    TAG, "Launch " + r + " mStartedActivity set");
2408                r.hideForNow = true;
2409            }
2410
2411            // Get rid of anything left hanging around.
2412            cleanUpPendingRemoveWindows(r);
2413
2414            // The window is now visible if it has been added, we are not
2415            // simply finishing, and we are not starting another activity.
2416            if (!r.activity.mFinished && willBeVisible
2417                    && r.activity.mDecor != null && !r.hideForNow) {
2418                if (r.newConfig != null) {
2419                    if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
2420                            + r.activityInfo.name + " with newConfig " + r.newConfig);
2421                    performConfigurationChanged(r.activity, r.newConfig);
2422                    r.newConfig = null;
2423                }
2424                if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
2425                        + isForward);
2426                WindowManager.LayoutParams l = r.window.getAttributes();
2427                if ((l.softInputMode
2428                        & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2429                        != forwardBit) {
2430                    l.softInputMode = (l.softInputMode
2431                            & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2432                            | forwardBit;
2433                    if (r.activity.mVisibleFromClient) {
2434                        ViewManager wm = a.getWindowManager();
2435                        View decor = r.window.getDecorView();
2436                        wm.updateViewLayout(decor, l);
2437                    }
2438                }
2439                r.activity.mVisibleFromServer = true;
2440                mNumVisibleActivities++;
2441                if (r.activity.mVisibleFromClient) {
2442                    r.activity.makeVisible();
2443                }
2444            }
2445
2446            if (!r.onlyLocalRequest) {
2447                r.nextIdle = mNewActivities;
2448                mNewActivities = r;
2449                if (localLOGV) Slog.v(
2450                    TAG, "Scheduling idle handler for " + r);
2451                Looper.myQueue().addIdleHandler(new Idler());
2452            }
2453            r.onlyLocalRequest = false;
2454
2455        } else {
2456            // If an exception was thrown when trying to resume, then
2457            // just end this activity.
2458            try {
2459                ActivityManagerNative.getDefault()
2460                    .finishActivity(token, Activity.RESULT_CANCELED, null);
2461            } catch (RemoteException ex) {
2462            }
2463        }
2464    }
2465
2466    private int mThumbnailWidth = -1;
2467    private int mThumbnailHeight = -1;
2468    private Bitmap mAvailThumbnailBitmap = null;
2469    private Canvas mThumbnailCanvas = null;
2470
2471    private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
2472        Bitmap thumbnail = mAvailThumbnailBitmap;
2473        try {
2474            if (thumbnail == null) {
2475                int w = mThumbnailWidth;
2476                int h;
2477                if (w < 0) {
2478                    Resources res = r.activity.getResources();
2479                    mThumbnailHeight = h =
2480                        res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
2481
2482                    mThumbnailWidth = w =
2483                        res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2484                } else {
2485                    h = mThumbnailHeight;
2486                }
2487
2488                // On platforms where we don't want thumbnails, set dims to (0,0)
2489                if ((w > 0) && (h > 0)) {
2490                    thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2491                    thumbnail.eraseColor(0);
2492                }
2493            }
2494
2495            if (thumbnail != null) {
2496                Canvas cv = mThumbnailCanvas;
2497                if (cv == null) {
2498                    mThumbnailCanvas = cv = new Canvas();
2499                }
2500
2501                cv.setBitmap(thumbnail);
2502                if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2503                    mAvailThumbnailBitmap = thumbnail;
2504                    thumbnail = null;
2505                }
2506            }
2507
2508        } catch (Exception e) {
2509            if (!mInstrumentation.onException(r.activity, e)) {
2510                throw new RuntimeException(
2511                        "Unable to create thumbnail of "
2512                        + r.intent.getComponent().toShortString()
2513                        + ": " + e.toString(), e);
2514            }
2515            thumbnail = null;
2516        }
2517
2518        return thumbnail;
2519    }
2520
2521    private void handlePauseActivity(IBinder token, boolean finished,
2522            boolean userLeaving, int configChanges) {
2523        ActivityClientRecord r = mActivities.get(token);
2524        if (r != null) {
2525            //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
2526            if (userLeaving) {
2527                performUserLeavingActivity(r);
2528            }
2529
2530            r.activity.mConfigChangeFlags |= configChanges;
2531            performPauseActivity(token, finished, r.isPreHoneycomb());
2532
2533            // Make sure any pending writes are now committed.
2534            if (r.isPreHoneycomb()) {
2535                QueuedWork.waitToFinish();
2536            }
2537
2538            // Tell the activity manager we have paused.
2539            try {
2540                ActivityManagerNative.getDefault().activityPaused(token);
2541            } catch (RemoteException ex) {
2542            }
2543        }
2544    }
2545
2546    final void performUserLeavingActivity(ActivityClientRecord r) {
2547        mInstrumentation.callActivityOnUserLeaving(r.activity);
2548    }
2549
2550    final Bundle performPauseActivity(IBinder token, boolean finished,
2551            boolean saveState) {
2552        ActivityClientRecord r = mActivities.get(token);
2553        return r != null ? performPauseActivity(r, finished, saveState) : null;
2554    }
2555
2556    final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
2557            boolean saveState) {
2558        if (r.paused) {
2559            if (r.activity.mFinished) {
2560                // If we are finishing, we won't call onResume() in certain cases.
2561                // So here we likewise don't want to call onPause() if the activity
2562                // isn't resumed.
2563                return null;
2564            }
2565            RuntimeException e = new RuntimeException(
2566                    "Performing pause of activity that is not resumed: "
2567                    + r.intent.getComponent().toShortString());
2568            Slog.e(TAG, e.getMessage(), e);
2569        }
2570        Bundle state = null;
2571        if (finished) {
2572            r.activity.mFinished = true;
2573        }
2574        try {
2575            // Next have the activity save its current state and managed dialogs...
2576            if (!r.activity.mFinished && saveState) {
2577                state = new Bundle();
2578                mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2579                r.state = state;
2580            }
2581            // Now we are idle.
2582            r.activity.mCalled = false;
2583            mInstrumentation.callActivityOnPause(r.activity);
2584            EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2585            if (!r.activity.mCalled) {
2586                throw new SuperNotCalledException(
2587                    "Activity " + r.intent.getComponent().toShortString() +
2588                    " did not call through to super.onPause()");
2589            }
2590
2591        } catch (SuperNotCalledException e) {
2592            throw e;
2593
2594        } catch (Exception e) {
2595            if (!mInstrumentation.onException(r.activity, e)) {
2596                throw new RuntimeException(
2597                        "Unable to pause activity "
2598                        + r.intent.getComponent().toShortString()
2599                        + ": " + e.toString(), e);
2600            }
2601        }
2602        r.paused = true;
2603
2604        // Notify any outstanding on paused listeners
2605        ArrayList<OnActivityPausedListener> listeners;
2606        synchronized (mOnPauseListeners) {
2607            listeners = mOnPauseListeners.remove(r.activity);
2608        }
2609        int size = (listeners != null ? listeners.size() : 0);
2610        for (int i = 0; i < size; i++) {
2611            listeners.get(i).onPaused(r.activity);
2612        }
2613
2614        return state;
2615    }
2616
2617    final void performStopActivity(IBinder token, boolean saveState) {
2618        ActivityClientRecord r = mActivities.get(token);
2619        performStopActivityInner(r, null, false, saveState);
2620    }
2621
2622    private static class StopInfo {
2623        Bitmap thumbnail;
2624        CharSequence description;
2625    }
2626
2627    private class ProviderRefCount {
2628        public int count;
2629        ProviderRefCount(int pCount) {
2630            count = pCount;
2631        }
2632    }
2633
2634    /**
2635     * Core implementation of stopping an activity.  Note this is a little
2636     * tricky because the server's meaning of stop is slightly different
2637     * than our client -- for the server, stop means to save state and give
2638     * it the result when it is done, but the window may still be visible.
2639     * For the client, we want to call onStop()/onStart() to indicate when
2640     * the activity's UI visibillity changes.
2641     */
2642    private void performStopActivityInner(ActivityClientRecord r,
2643            StopInfo info, boolean keepShown, boolean saveState) {
2644        if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
2645        Bundle state = null;
2646        if (r != null) {
2647            if (!keepShown && r.stopped) {
2648                if (r.activity.mFinished) {
2649                    // If we are finishing, we won't call onResume() in certain
2650                    // cases.  So here we likewise don't want to call onStop()
2651                    // if the activity isn't resumed.
2652                    return;
2653                }
2654                RuntimeException e = new RuntimeException(
2655                        "Performing stop of activity that is not resumed: "
2656                        + r.intent.getComponent().toShortString());
2657                Slog.e(TAG, e.getMessage(), e);
2658            }
2659
2660            if (info != null) {
2661                try {
2662                    // First create a thumbnail for the activity...
2663                    info.thumbnail = createThumbnailBitmap(r);
2664                    info.description = r.activity.onCreateDescription();
2665                } catch (Exception e) {
2666                    if (!mInstrumentation.onException(r.activity, e)) {
2667                        throw new RuntimeException(
2668                                "Unable to save state of activity "
2669                                + r.intent.getComponent().toShortString()
2670                                + ": " + e.toString(), e);
2671                    }
2672                }
2673            }
2674
2675            // Next have the activity save its current state and managed dialogs...
2676            if (!r.activity.mFinished && saveState) {
2677                if (r.state == null) {
2678                    state = new Bundle();
2679                    mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2680                    r.state = state;
2681                } else {
2682                    state = r.state;
2683                }
2684            }
2685
2686            if (!keepShown) {
2687                try {
2688                    // Now we are idle.
2689                    r.activity.performStop();
2690                } catch (Exception e) {
2691                    if (!mInstrumentation.onException(r.activity, e)) {
2692                        throw new RuntimeException(
2693                                "Unable to stop activity "
2694                                + r.intent.getComponent().toShortString()
2695                                + ": " + e.toString(), e);
2696                    }
2697                }
2698                r.stopped = true;
2699            }
2700
2701            r.paused = true;
2702        }
2703    }
2704
2705    private void updateVisibility(ActivityClientRecord r, boolean show) {
2706        View v = r.activity.mDecor;
2707        if (v != null) {
2708            if (show) {
2709                if (!r.activity.mVisibleFromServer) {
2710                    r.activity.mVisibleFromServer = true;
2711                    mNumVisibleActivities++;
2712                    if (r.activity.mVisibleFromClient) {
2713                        r.activity.makeVisible();
2714                    }
2715                }
2716                if (r.newConfig != null) {
2717                    if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
2718                            + r.activityInfo.name + " with new config " + r.newConfig);
2719                    performConfigurationChanged(r.activity, r.newConfig);
2720                    r.newConfig = null;
2721                }
2722            } else {
2723                if (r.activity.mVisibleFromServer) {
2724                    r.activity.mVisibleFromServer = false;
2725                    mNumVisibleActivities--;
2726                    v.setVisibility(View.INVISIBLE);
2727                }
2728            }
2729        }
2730    }
2731
2732    private void handleStopActivity(IBinder token, boolean show, int configChanges) {
2733        ActivityClientRecord r = mActivities.get(token);
2734        r.activity.mConfigChangeFlags |= configChanges;
2735
2736        StopInfo info = new StopInfo();
2737        performStopActivityInner(r, info, show, true);
2738
2739        if (localLOGV) Slog.v(
2740            TAG, "Finishing stop of " + r + ": show=" + show
2741            + " win=" + r.window);
2742
2743        updateVisibility(r, show);
2744
2745        // Make sure any pending writes are now committed.
2746        if (!r.isPreHoneycomb()) {
2747            QueuedWork.waitToFinish();
2748        }
2749
2750        // Tell activity manager we have been stopped.
2751        try {
2752            ActivityManagerNative.getDefault().activityStopped(
2753                r.token, r.state, info.thumbnail, info.description);
2754        } catch (RemoteException ex) {
2755        }
2756    }
2757
2758    final void performRestartActivity(IBinder token) {
2759        ActivityClientRecord r = mActivities.get(token);
2760        if (r.stopped) {
2761            r.activity.performRestart();
2762            r.stopped = false;
2763        }
2764    }
2765
2766    private void handleWindowVisibility(IBinder token, boolean show) {
2767        ActivityClientRecord r = mActivities.get(token);
2768
2769        if (r == null) {
2770            Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2771            return;
2772        }
2773
2774        if (!show && !r.stopped) {
2775            performStopActivityInner(r, null, show, false);
2776        } else if (show && r.stopped) {
2777            // If we are getting ready to gc after going to the background, well
2778            // we are back active so skip it.
2779            unscheduleGcIdler();
2780
2781            r.activity.performRestart();
2782            r.stopped = false;
2783        }
2784        if (r.activity.mDecor != null) {
2785            if (false) Slog.v(
2786                TAG, "Handle window " + r + " visibility: " + show);
2787            updateVisibility(r, show);
2788        }
2789    }
2790
2791    private void handleSleeping(IBinder token, boolean sleeping) {
2792        ActivityClientRecord r = mActivities.get(token);
2793
2794        if (r == null) {
2795            Log.w(TAG, "handleSleeping: no activity for token " + token);
2796            return;
2797        }
2798
2799        if (sleeping) {
2800            if (!r.stopped && !r.isPreHoneycomb()) {
2801                try {
2802                    // Now we are idle.
2803                    r.activity.performStop();
2804                } catch (Exception e) {
2805                    if (!mInstrumentation.onException(r.activity, e)) {
2806                        throw new RuntimeException(
2807                                "Unable to stop activity "
2808                                + r.intent.getComponent().toShortString()
2809                                + ": " + e.toString(), e);
2810                    }
2811                }
2812                r.stopped = true;
2813            }
2814
2815            // Make sure any pending writes are now committed.
2816            if (!r.isPreHoneycomb()) {
2817                QueuedWork.waitToFinish();
2818            }
2819
2820            // Tell activity manager we slept.
2821            try {
2822                ActivityManagerNative.getDefault().activitySlept(r.token);
2823            } catch (RemoteException ex) {
2824            }
2825        } else {
2826            if (r.stopped && r.activity.mVisibleFromServer) {
2827                r.activity.performRestart();
2828                r.stopped = false;
2829            }
2830        }
2831    }
2832
2833    private void handleSetCoreSettings(Bundle coreSettings) {
2834        synchronized (mPackages) {
2835            mCoreSettings = coreSettings;
2836        }
2837    }
2838
2839    private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2840        LoadedApk apk = peekPackageInfo(data.pkg, false);
2841        if (apk != null) {
2842            apk.mCompatibilityInfo.set(data.info);
2843        }
2844        apk = peekPackageInfo(data.pkg, true);
2845        if (apk != null) {
2846            apk.mCompatibilityInfo.set(data.info);
2847        }
2848        handleConfigurationChanged(mConfiguration, data.info);
2849        WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
2850    }
2851
2852    private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
2853        final int N = results.size();
2854        for (int i=0; i<N; i++) {
2855            ResultInfo ri = results.get(i);
2856            try {
2857                if (ri.mData != null) {
2858                    ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2859                }
2860                if (DEBUG_RESULTS) Slog.v(TAG,
2861                        "Delivering result to activity " + r + " : " + ri);
2862                r.activity.dispatchActivityResult(ri.mResultWho,
2863                        ri.mRequestCode, ri.mResultCode, ri.mData);
2864            } catch (Exception e) {
2865                if (!mInstrumentation.onException(r.activity, e)) {
2866                    throw new RuntimeException(
2867                            "Failure delivering result " + ri + " to activity "
2868                            + r.intent.getComponent().toShortString()
2869                            + ": " + e.toString(), e);
2870                }
2871            }
2872        }
2873    }
2874
2875    private void handleSendResult(ResultData res) {
2876        ActivityClientRecord r = mActivities.get(res.token);
2877        if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
2878        if (r != null) {
2879            final boolean resumed = !r.paused;
2880            if (!r.activity.mFinished && r.activity.mDecor != null
2881                    && r.hideForNow && resumed) {
2882                // We had hidden the activity because it started another
2883                // one...  we have gotten a result back and we are not
2884                // paused, so make sure our window is visible.
2885                updateVisibility(r, true);
2886            }
2887            if (resumed) {
2888                try {
2889                    // Now we are idle.
2890                    r.activity.mCalled = false;
2891                    r.activity.mTemporaryPause = true;
2892                    mInstrumentation.callActivityOnPause(r.activity);
2893                    if (!r.activity.mCalled) {
2894                        throw new SuperNotCalledException(
2895                            "Activity " + r.intent.getComponent().toShortString()
2896                            + " did not call through to super.onPause()");
2897                    }
2898                } catch (SuperNotCalledException e) {
2899                    throw e;
2900                } catch (Exception e) {
2901                    if (!mInstrumentation.onException(r.activity, e)) {
2902                        throw new RuntimeException(
2903                                "Unable to pause activity "
2904                                + r.intent.getComponent().toShortString()
2905                                + ": " + e.toString(), e);
2906                    }
2907                }
2908            }
2909            deliverResults(r, res.results);
2910            if (resumed) {
2911                r.activity.performResume();
2912                r.activity.mTemporaryPause = false;
2913            }
2914        }
2915    }
2916
2917    public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
2918        return performDestroyActivity(token, finishing, 0, false);
2919    }
2920
2921    private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
2922            int configChanges, boolean getNonConfigInstance) {
2923        ActivityClientRecord r = mActivities.get(token);
2924        Class activityClass = null;
2925        if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
2926        if (r != null) {
2927            activityClass = r.activity.getClass();
2928            r.activity.mConfigChangeFlags |= configChanges;
2929            if (finishing) {
2930                r.activity.mFinished = true;
2931            }
2932            if (!r.paused) {
2933                try {
2934                    r.activity.mCalled = false;
2935                    mInstrumentation.callActivityOnPause(r.activity);
2936                    EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
2937                            r.activity.getComponentName().getClassName());
2938                    if (!r.activity.mCalled) {
2939                        throw new SuperNotCalledException(
2940                            "Activity " + safeToComponentShortString(r.intent)
2941                            + " did not call through to super.onPause()");
2942                    }
2943                } catch (SuperNotCalledException e) {
2944                    throw e;
2945                } catch (Exception e) {
2946                    if (!mInstrumentation.onException(r.activity, e)) {
2947                        throw new RuntimeException(
2948                                "Unable to pause activity "
2949                                + safeToComponentShortString(r.intent)
2950                                + ": " + e.toString(), e);
2951                    }
2952                }
2953                r.paused = true;
2954            }
2955            if (!r.stopped) {
2956                try {
2957                    r.activity.performStop();
2958                } catch (SuperNotCalledException e) {
2959                    throw e;
2960                } catch (Exception e) {
2961                    if (!mInstrumentation.onException(r.activity, e)) {
2962                        throw new RuntimeException(
2963                                "Unable to stop activity "
2964                                + safeToComponentShortString(r.intent)
2965                                + ": " + e.toString(), e);
2966                    }
2967                }
2968                r.stopped = true;
2969            }
2970            if (getNonConfigInstance) {
2971                try {
2972                    r.lastNonConfigurationInstances
2973                            = r.activity.retainNonConfigurationInstances();
2974                } catch (Exception e) {
2975                    if (!mInstrumentation.onException(r.activity, e)) {
2976                        throw new RuntimeException(
2977                                "Unable to retain activity "
2978                                + r.intent.getComponent().toShortString()
2979                                + ": " + e.toString(), e);
2980                    }
2981                }
2982            }
2983            try {
2984                r.activity.mCalled = false;
2985                mInstrumentation.callActivityOnDestroy(r.activity);
2986                if (!r.activity.mCalled) {
2987                    throw new SuperNotCalledException(
2988                        "Activity " + safeToComponentShortString(r.intent) +
2989                        " did not call through to super.onDestroy()");
2990                }
2991                if (r.window != null) {
2992                    r.window.closeAllPanels();
2993                }
2994            } catch (SuperNotCalledException e) {
2995                throw e;
2996            } catch (Exception e) {
2997                if (!mInstrumentation.onException(r.activity, e)) {
2998                    throw new RuntimeException(
2999                            "Unable to destroy activity " + safeToComponentShortString(r.intent)
3000                            + ": " + e.toString(), e);
3001                }
3002            }
3003        }
3004        mActivities.remove(token);
3005        StrictMode.decrementExpectedActivityCount(activityClass);
3006        return r;
3007    }
3008
3009    private static String safeToComponentShortString(Intent intent) {
3010        ComponentName component = intent.getComponent();
3011        return component == null ? "[Unknown]" : component.toShortString();
3012    }
3013
3014    private void handleDestroyActivity(IBinder token, boolean finishing,
3015            int configChanges, boolean getNonConfigInstance) {
3016        ActivityClientRecord r = performDestroyActivity(token, finishing,
3017                configChanges, getNonConfigInstance);
3018        if (r != null) {
3019            cleanUpPendingRemoveWindows(r);
3020            WindowManager wm = r.activity.getWindowManager();
3021            View v = r.activity.mDecor;
3022            if (v != null) {
3023                if (r.activity.mVisibleFromServer) {
3024                    mNumVisibleActivities--;
3025                }
3026                IBinder wtoken = v.getWindowToken();
3027                if (r.activity.mWindowAdded) {
3028                    if (r.onlyLocalRequest) {
3029                        // Hold off on removing this until the new activity's
3030                        // window is being added.
3031                        r.mPendingRemoveWindow = v;
3032                        r.mPendingRemoveWindowManager = wm;
3033                    } else {
3034                        wm.removeViewImmediate(v);
3035                    }
3036                }
3037                if (wtoken != null && r.mPendingRemoveWindow == null) {
3038                    WindowManagerImpl.getDefault().closeAll(wtoken,
3039                            r.activity.getClass().getName(), "Activity");
3040                }
3041                r.activity.mDecor = null;
3042            }
3043            if (r.mPendingRemoveWindow == null) {
3044                // If we are delaying the removal of the activity window, then
3045                // we can't clean up all windows here.  Note that we can't do
3046                // so later either, which means any windows that aren't closed
3047                // by the app will leak.  Well we try to warning them a lot
3048                // about leaking windows, because that is a bug, so if they are
3049                // using this recreate facility then they get to live with leaks.
3050                WindowManagerImpl.getDefault().closeAll(token,
3051                        r.activity.getClass().getName(), "Activity");
3052            }
3053
3054            // Mocked out contexts won't be participating in the normal
3055            // process lifecycle, but if we're running with a proper
3056            // ApplicationContext we need to have it tear down things
3057            // cleanly.
3058            Context c = r.activity.getBaseContext();
3059            if (c instanceof ContextImpl) {
3060                ((ContextImpl) c).scheduleFinalCleanup(
3061                        r.activity.getClass().getName(), "Activity");
3062            }
3063        }
3064        if (finishing) {
3065            try {
3066                ActivityManagerNative.getDefault().activityDestroyed(token);
3067            } catch (RemoteException ex) {
3068                // If the system process has died, it's game over for everyone.
3069            }
3070        }
3071    }
3072
3073    public final void requestRelaunchActivity(IBinder token,
3074            List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3075            int configChanges, boolean notResumed, Configuration config,
3076            boolean fromServer) {
3077        ActivityClientRecord target = null;
3078
3079        synchronized (mPackages) {
3080            for (int i=0; i<mRelaunchingActivities.size(); i++) {
3081                ActivityClientRecord r = mRelaunchingActivities.get(i);
3082                if (r.token == token) {
3083                    target = r;
3084                    if (pendingResults != null) {
3085                        if (r.pendingResults != null) {
3086                            r.pendingResults.addAll(pendingResults);
3087                        } else {
3088                            r.pendingResults = pendingResults;
3089                        }
3090                    }
3091                    if (pendingNewIntents != null) {
3092                        if (r.pendingIntents != null) {
3093                            r.pendingIntents.addAll(pendingNewIntents);
3094                        } else {
3095                            r.pendingIntents = pendingNewIntents;
3096                        }
3097                    }
3098                    break;
3099                }
3100            }
3101
3102            if (target == null) {
3103                target = new ActivityClientRecord();
3104                target.token = token;
3105                target.pendingResults = pendingResults;
3106                target.pendingIntents = pendingNewIntents;
3107                if (!fromServer) {
3108                    ActivityClientRecord existing = mActivities.get(token);
3109                    if (existing != null) {
3110                        target.startsNotResumed = existing.paused;
3111                    }
3112                    target.onlyLocalRequest = true;
3113                }
3114                mRelaunchingActivities.add(target);
3115                queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3116            }
3117
3118            if (fromServer) {
3119                target.startsNotResumed = notResumed;
3120                target.onlyLocalRequest = false;
3121            }
3122            if (config != null) {
3123                target.createdConfig = config;
3124            }
3125            target.pendingConfigChanges |= configChanges;
3126        }
3127    }
3128
3129    private void handleRelaunchActivity(ActivityClientRecord tmp) {
3130        // If we are getting ready to gc after going to the background, well
3131        // we are back active so skip it.
3132        unscheduleGcIdler();
3133
3134        Configuration changedConfig = null;
3135        int configChanges = 0;
3136
3137        // First: make sure we have the most recent configuration and most
3138        // recent version of the activity, or skip it if some previous call
3139        // had taken a more recent version.
3140        synchronized (mPackages) {
3141            int N = mRelaunchingActivities.size();
3142            IBinder token = tmp.token;
3143            tmp = null;
3144            for (int i=0; i<N; i++) {
3145                ActivityClientRecord r = mRelaunchingActivities.get(i);
3146                if (r.token == token) {
3147                    tmp = r;
3148                    configChanges |= tmp.pendingConfigChanges;
3149                    mRelaunchingActivities.remove(i);
3150                    i--;
3151                    N--;
3152                }
3153            }
3154
3155            if (tmp == null) {
3156                if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
3157                return;
3158            }
3159
3160            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3161                    + tmp.token + " with configChanges=0x"
3162                    + Integer.toHexString(configChanges));
3163
3164            if (mPendingConfiguration != null) {
3165                changedConfig = mPendingConfiguration;
3166                mPendingConfiguration = null;
3167            }
3168        }
3169
3170        if (tmp.createdConfig != null) {
3171            // If the activity manager is passing us its current config,
3172            // assume that is really what we want regardless of what we
3173            // may have pending.
3174            if (mConfiguration == null
3175                    || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3176                            && mConfiguration.diff(tmp.createdConfig) != 0)) {
3177                if (changedConfig == null
3178                        || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3179                    changedConfig = tmp.createdConfig;
3180                }
3181            }
3182        }
3183
3184        if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3185                + tmp.token + ": changedConfig=" + changedConfig);
3186
3187        // If there was a pending configuration change, execute it first.
3188        if (changedConfig != null) {
3189            handleConfigurationChanged(changedConfig, null);
3190        }
3191
3192        ActivityClientRecord r = mActivities.get(tmp.token);
3193        if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
3194        if (r == null) {
3195            return;
3196        }
3197
3198        r.activity.mConfigChangeFlags |= configChanges;
3199        r.onlyLocalRequest = tmp.onlyLocalRequest;
3200        Intent currentIntent = r.activity.mIntent;
3201
3202        r.activity.mChangingConfigurations = true;
3203
3204        // Need to ensure state is saved.
3205        if (!r.paused) {
3206            performPauseActivity(r.token, false, r.isPreHoneycomb());
3207        }
3208        if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3209            r.state = new Bundle();
3210            mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
3211        }
3212
3213        handleDestroyActivity(r.token, false, configChanges, true);
3214
3215        r.activity = null;
3216        r.window = null;
3217        r.hideForNow = false;
3218        r.nextIdle = null;
3219        // Merge any pending results and pending intents; don't just replace them
3220        if (tmp.pendingResults != null) {
3221            if (r.pendingResults == null) {
3222                r.pendingResults = tmp.pendingResults;
3223            } else {
3224                r.pendingResults.addAll(tmp.pendingResults);
3225            }
3226        }
3227        if (tmp.pendingIntents != null) {
3228            if (r.pendingIntents == null) {
3229                r.pendingIntents = tmp.pendingIntents;
3230            } else {
3231                r.pendingIntents.addAll(tmp.pendingIntents);
3232            }
3233        }
3234        r.startsNotResumed = tmp.startsNotResumed;
3235
3236        handleLaunchActivity(r, currentIntent);
3237    }
3238
3239    private void handleRequestThumbnail(IBinder token) {
3240        ActivityClientRecord r = mActivities.get(token);
3241        Bitmap thumbnail = createThumbnailBitmap(r);
3242        CharSequence description = null;
3243        try {
3244            description = r.activity.onCreateDescription();
3245        } catch (Exception e) {
3246            if (!mInstrumentation.onException(r.activity, e)) {
3247                throw new RuntimeException(
3248                        "Unable to create description of activity "
3249                        + r.intent.getComponent().toShortString()
3250                        + ": " + e.toString(), e);
3251            }
3252        }
3253        //System.out.println("Reporting top thumbnail " + thumbnail);
3254        try {
3255            ActivityManagerNative.getDefault().reportThumbnail(
3256                token, thumbnail, description);
3257        } catch (RemoteException ex) {
3258        }
3259    }
3260
3261    ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
3262            boolean allActivities, Configuration newConfig) {
3263        ArrayList<ComponentCallbacks2> callbacks
3264                = new ArrayList<ComponentCallbacks2>();
3265
3266        if (mActivities.size() > 0) {
3267            Iterator<ActivityClientRecord> it = mActivities.values().iterator();
3268            while (it.hasNext()) {
3269                ActivityClientRecord ar = it.next();
3270                Activity a = ar.activity;
3271                if (a != null) {
3272                    Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3273                            ar.packageInfo.mCompatibilityInfo.getIfNeeded());
3274                    if (!ar.activity.mFinished && (allActivities ||
3275                            (a != null && !ar.paused))) {
3276                        // If the activity is currently resumed, its configuration
3277                        // needs to change right now.
3278                        callbacks.add(a);
3279                    } else if (thisConfig != null) {
3280                        // Otherwise, we will tell it about the change
3281                        // the next time it is resumed or shown.  Note that
3282                        // the activity manager may, before then, decide the
3283                        // activity needs to be destroyed to handle its new
3284                        // configuration.
3285                        if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
3286                                + ar.activityInfo.name + " newConfig=" + thisConfig);
3287                        ar.newConfig = thisConfig;
3288                    }
3289                }
3290            }
3291        }
3292        if (mServices.size() > 0) {
3293            Iterator<Service> it = mServices.values().iterator();
3294            while (it.hasNext()) {
3295                callbacks.add(it.next());
3296            }
3297        }
3298        synchronized (mProviderMap) {
3299            if (mLocalProviders.size() > 0) {
3300                Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
3301                while (it.hasNext()) {
3302                    callbacks.add(it.next().mLocalProvider);
3303                }
3304            }
3305        }
3306        final int N = mAllApplications.size();
3307        for (int i=0; i<N; i++) {
3308            callbacks.add(mAllApplications.get(i));
3309        }
3310
3311        return callbacks;
3312    }
3313
3314    private final void performConfigurationChanged(
3315            ComponentCallbacks2 cb, Configuration config) {
3316        // Only for Activity objects, check that they actually call up to their
3317        // superclass implementation.  ComponentCallbacks2 is an interface, so
3318        // we check the runtime type and act accordingly.
3319        Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3320        if (activity != null) {
3321            activity.mCalled = false;
3322        }
3323
3324        boolean shouldChangeConfig = false;
3325        if ((activity == null) || (activity.mCurrentConfig == null)) {
3326            shouldChangeConfig = true;
3327        } else {
3328
3329            // If the new config is the same as the config this Activity
3330            // is already running with then don't bother calling
3331            // onConfigurationChanged
3332            int diff = activity.mCurrentConfig.diff(config);
3333            if (diff != 0) {
3334                // If this activity doesn't handle any of the config changes
3335                // then don't bother calling onConfigurationChanged as we're
3336                // going to destroy it.
3337                if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
3338                    shouldChangeConfig = true;
3339                }
3340            }
3341        }
3342
3343        if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
3344                + ": shouldChangeConfig=" + shouldChangeConfig);
3345        if (shouldChangeConfig) {
3346            cb.onConfigurationChanged(config);
3347
3348            if (activity != null) {
3349                if (!activity.mCalled) {
3350                    throw new SuperNotCalledException(
3351                            "Activity " + activity.getLocalClassName() +
3352                        " did not call through to super.onConfigurationChanged()");
3353                }
3354                activity.mConfigChangeFlags = 0;
3355                activity.mCurrentConfig = new Configuration(config);
3356            }
3357        }
3358    }
3359
3360    public final void applyConfigurationToResources(Configuration config) {
3361        synchronized (mPackages) {
3362            applyConfigurationToResourcesLocked(config, null);
3363        }
3364    }
3365
3366    final boolean applyConfigurationToResourcesLocked(Configuration config,
3367            CompatibilityInfo compat) {
3368        if (mResConfiguration == null) {
3369            mResConfiguration = new Configuration();
3370        }
3371        if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
3372            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
3373                    + mResConfiguration.seq + ", newSeq=" + config.seq);
3374            return false;
3375        }
3376        int changes = mResConfiguration.updateFrom(config);
3377        DisplayMetrics dm = getDisplayMetricsLocked(compat, true);
3378
3379        if (compat != null && (mResCompatibilityInfo == null ||
3380                !mResCompatibilityInfo.equals(compat))) {
3381            mResCompatibilityInfo = compat;
3382            changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3383                    | ActivityInfo.CONFIG_SCREEN_SIZE
3384                    | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3385        }
3386
3387        // set it for java, this also affects newly created Resources
3388        if (config.locale != null) {
3389            Locale.setDefault(config.locale);
3390        }
3391
3392        Resources.updateSystemConfiguration(config, dm, compat);
3393
3394        ApplicationPackageManager.configurationChanged();
3395        //Slog.i(TAG, "Configuration changed in " + currentPackageName());
3396
3397        Iterator<WeakReference<Resources>> it =
3398            mActiveResources.values().iterator();
3399        //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3400        //    mActiveResources.entrySet().iterator();
3401        while (it.hasNext()) {
3402            WeakReference<Resources> v = it.next();
3403            Resources r = v.get();
3404            if (r != null) {
3405                if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
3406                        + r + " config to: " + config);
3407                r.updateConfiguration(config, dm, compat);
3408                //Slog.i(TAG, "Updated app resources " + v.getKey()
3409                //        + " " + r + ": " + r.getConfiguration());
3410            } else {
3411                //Slog.i(TAG, "Removing old resources " + v.getKey());
3412                it.remove();
3413            }
3414        }
3415
3416        return changes != 0;
3417    }
3418
3419    final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
3420
3421        ArrayList<ComponentCallbacks2> callbacks = null;
3422
3423        synchronized (mPackages) {
3424            if (mPendingConfiguration != null) {
3425                if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3426                    config = mPendingConfiguration;
3427                }
3428                mPendingConfiguration = null;
3429            }
3430
3431            if (config == null) {
3432                return;
3433            }
3434
3435            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
3436                    + config);
3437
3438            applyConfigurationToResourcesLocked(config, compat);
3439
3440            if (mConfiguration == null) {
3441                mConfiguration = new Configuration();
3442            }
3443            if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
3444                return;
3445            }
3446            mConfiguration.updateFrom(config);
3447            if (mCompatConfiguration == null) {
3448                mCompatConfiguration = new Configuration();
3449            }
3450            mCompatConfiguration.setTo(mConfiguration);
3451            if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3452                mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3453                config = mCompatConfiguration;
3454            }
3455            callbacks = collectComponentCallbacksLocked(false, config);
3456        }
3457
3458        // Cleanup hardware accelerated stuff
3459        WindowManagerImpl.getDefault().trimLocalMemory();
3460
3461        if (callbacks != null) {
3462            final int N = callbacks.size();
3463            for (int i=0; i<N; i++) {
3464                performConfigurationChanged(callbacks.get(i), config);
3465            }
3466        }
3467    }
3468
3469    final void handleActivityConfigurationChanged(IBinder token) {
3470        ActivityClientRecord r = mActivities.get(token);
3471        if (r == null || r.activity == null) {
3472            return;
3473        }
3474
3475        if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
3476                + r.activityInfo.name);
3477
3478        performConfigurationChanged(r.activity, mCompatConfiguration);
3479    }
3480
3481    final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
3482        if (start) {
3483            try {
3484                switch (profileType) {
3485                    case 1:
3486                        ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3487                        break;
3488                    default:
3489                        Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3490                                8 * 1024 * 1024, 0);
3491                        break;
3492                }
3493            } catch (RuntimeException e) {
3494                Slog.w(TAG, "Profiling failed on path " + pcd.path
3495                        + " -- can the process access this path?");
3496            } finally {
3497                try {
3498                    pcd.fd.close();
3499                } catch (IOException e) {
3500                    Slog.w(TAG, "Failure closing profile fd", e);
3501                }
3502            }
3503        } else {
3504            switch (profileType) {
3505                case 1:
3506                    ViewDebug.stopLooperProfiling();
3507                    break;
3508                default:
3509                    Debug.stopMethodTracing();
3510                    break;
3511
3512            }
3513        }
3514    }
3515
3516    final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3517        if (managed) {
3518            try {
3519                Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3520            } catch (IOException e) {
3521                Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3522                        + " -- can the process access this path?");
3523            } finally {
3524                try {
3525                    dhd.fd.close();
3526                } catch (IOException e) {
3527                    Slog.w(TAG, "Failure closing profile fd", e);
3528                }
3529            }
3530        } else {
3531            Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
3532        }
3533    }
3534
3535    final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3536        boolean hasPkgInfo = false;
3537        if (packages != null) {
3538            for (int i=packages.length-1; i>=0; i--) {
3539                //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3540                if (!hasPkgInfo) {
3541                    WeakReference<LoadedApk> ref;
3542                    ref = mPackages.get(packages[i]);
3543                    if (ref != null && ref.get() != null) {
3544                        hasPkgInfo = true;
3545                    } else {
3546                        ref = mResourcePackages.get(packages[i]);
3547                        if (ref != null && ref.get() != null) {
3548                            hasPkgInfo = true;
3549                        }
3550                    }
3551                }
3552                mPackages.remove(packages[i]);
3553                mResourcePackages.remove(packages[i]);
3554            }
3555        }
3556        ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
3557                hasPkgInfo);
3558    }
3559
3560    final void handleLowMemory() {
3561        ArrayList<ComponentCallbacks2> callbacks;
3562
3563        synchronized (mPackages) {
3564            callbacks = collectComponentCallbacksLocked(true, null);
3565        }
3566
3567        final int N = callbacks.size();
3568        for (int i=0; i<N; i++) {
3569            callbacks.get(i).onLowMemory();
3570        }
3571
3572        // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3573        if (Process.myUid() != Process.SYSTEM_UID) {
3574            int sqliteReleased = SQLiteDatabase.releaseMemory();
3575            EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3576        }
3577
3578        // Ask graphics to free up as much as possible (font/image caches)
3579        Canvas.freeCaches();
3580
3581        BinderInternal.forceGc("mem");
3582    }
3583
3584    final void handleTrimMemory(int level) {
3585        WindowManagerImpl.getDefault().trimMemory(level);
3586        ArrayList<ComponentCallbacks2> callbacks;
3587
3588        synchronized (mPackages) {
3589            callbacks = collectComponentCallbacksLocked(true, null);
3590        }
3591
3592        final int N = callbacks.size();
3593        for (int i=0; i<N; i++) {
3594            callbacks.get(i).onTrimMemory(level);
3595        }
3596    }
3597
3598    private void handleBindApplication(AppBindData data) {
3599        mBoundApplication = data;
3600        mConfiguration = new Configuration(data.config);
3601        mCompatConfiguration = new Configuration(data.config);
3602
3603        // send up app name; do this *before* waiting for debugger
3604        Process.setArgV0(data.processName);
3605        android.ddm.DdmHandleAppName.setAppName(data.processName);
3606
3607        // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3608        // implementation to use the pool executor.  Normally, we use the
3609        // serialized executor as the default. This has to happen in the
3610        // main thread so the main looper is set right.
3611        if (data.appInfo.targetSdkVersion <= 12) {
3612            AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3613        }
3614
3615        /*
3616         * Before spawning a new process, reset the time zone to be the system time zone.
3617         * This needs to be done because the system time zone could have changed after the
3618         * the spawning of this process. Without doing this this process would have the incorrect
3619         * system time zone.
3620         */
3621        TimeZone.setDefault(null);
3622
3623        /*
3624         * Initialize the default locale in this process for the reasons we set the time zone.
3625         */
3626        Locale.setDefault(data.config.locale);
3627
3628        /*
3629         * Update the system configuration since its preloaded and might not
3630         * reflect configuration changes. The configuration object passed
3631         * in AppBindData can be safely assumed to be up to date
3632         */
3633        applyConfigurationToResourcesLocked(data.config, data.compatInfo);
3634
3635        data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
3636
3637        /**
3638         * For system applications on userdebug/eng builds, log stack
3639         * traces of disk and network access to dropbox for analysis.
3640         */
3641        if ((data.appInfo.flags &
3642             (ApplicationInfo.FLAG_SYSTEM |
3643              ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3644            StrictMode.conditionallyEnableDebugLogging();
3645        }
3646
3647        /**
3648         * For apps targetting SDK Honeycomb or later, we don't allow
3649         * network usage on the main event loop / UI thread.
3650         *
3651         * Note to those grepping:  this is what ultimately throws
3652         * NetworkOnMainThreadException ...
3653         */
3654        if (data.appInfo.targetSdkVersion > 9) {
3655            StrictMode.enableDeathOnNetwork();
3656        }
3657
3658        /**
3659         * Switch this process to density compatibility mode if needed.
3660         */
3661        if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3662                == 0) {
3663            Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3664        }
3665
3666        if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3667            // XXX should have option to change the port.
3668            Debug.changeDebugPort(8100);
3669            if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
3670                Slog.w(TAG, "Application " + data.info.getPackageName()
3671                      + " is waiting for the debugger on port 8100...");
3672
3673                IActivityManager mgr = ActivityManagerNative.getDefault();
3674                try {
3675                    mgr.showWaitingForDebugger(mAppThread, true);
3676                } catch (RemoteException ex) {
3677                }
3678
3679                Debug.waitForDebugger();
3680
3681                try {
3682                    mgr.showWaitingForDebugger(mAppThread, false);
3683                } catch (RemoteException ex) {
3684                }
3685
3686            } else {
3687                Slog.w(TAG, "Application " + data.info.getPackageName()
3688                      + " can be debugged on port 8100...");
3689            }
3690        }
3691
3692        /**
3693         * Initialize the default http proxy in this process for the reasons we set the time zone.
3694         */
3695        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3696        IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3697        try {
3698            ProxyProperties proxyProperties = service.getProxy();
3699            Proxy.setHttpProxySystemProperty(proxyProperties);
3700        } catch (RemoteException e) {}
3701
3702        if (data.instrumentationName != null) {
3703            ContextImpl appContext = new ContextImpl();
3704            appContext.init(data.info, null, this);
3705            InstrumentationInfo ii = null;
3706            try {
3707                ii = appContext.getPackageManager().
3708                    getInstrumentationInfo(data.instrumentationName, 0);
3709            } catch (PackageManager.NameNotFoundException e) {
3710            }
3711            if (ii == null) {
3712                throw new RuntimeException(
3713                    "Unable to find instrumentation info for: "
3714                    + data.instrumentationName);
3715            }
3716
3717            mInstrumentationAppDir = ii.sourceDir;
3718            mInstrumentationAppPackage = ii.packageName;
3719            mInstrumentedAppDir = data.info.getAppDir();
3720
3721            ApplicationInfo instrApp = new ApplicationInfo();
3722            instrApp.packageName = ii.packageName;
3723            instrApp.sourceDir = ii.sourceDir;
3724            instrApp.publicSourceDir = ii.publicSourceDir;
3725            instrApp.dataDir = ii.dataDir;
3726            instrApp.nativeLibraryDir = ii.nativeLibraryDir;
3727            LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
3728                    appContext.getClassLoader(), false, true);
3729            ContextImpl instrContext = new ContextImpl();
3730            instrContext.init(pi, null, this);
3731
3732            try {
3733                java.lang.ClassLoader cl = instrContext.getClassLoader();
3734                mInstrumentation = (Instrumentation)
3735                    cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3736            } catch (Exception e) {
3737                throw new RuntimeException(
3738                    "Unable to instantiate instrumentation "
3739                    + data.instrumentationName + ": " + e.toString(), e);
3740            }
3741
3742            mInstrumentation.init(this, instrContext, appContext,
3743                    new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3744
3745            if (data.profileFile != null && !ii.handleProfiling) {
3746                data.handlingProfiling = true;
3747                File file = new File(data.profileFile);
3748                file.getParentFile().mkdirs();
3749                Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3750            }
3751
3752            try {
3753                mInstrumentation.onCreate(data.instrumentationArgs);
3754            }
3755            catch (Exception e) {
3756                throw new RuntimeException(
3757                    "Exception thrown in onCreate() of "
3758                    + data.instrumentationName + ": " + e.toString(), e);
3759            }
3760
3761        } else {
3762            mInstrumentation = new Instrumentation();
3763        }
3764
3765        if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
3766            dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
3767        }
3768
3769        // If the app is being launched for full backup or restore, bring it up in
3770        // a restricted environment with the base application class.
3771        Application app = data.info.makeApplication(data.restrictedBackupMode, null);
3772        mInitialApplication = app;
3773
3774        // don't bring up providers in restricted mode; they may depend on the
3775        // app's custom Application class
3776        if (!data.restrictedBackupMode){
3777            List<ProviderInfo> providers = data.providers;
3778            if (providers != null) {
3779                installContentProviders(app, providers);
3780                // For process that contains content providers, we want to
3781                // ensure that the JIT is enabled "at some point".
3782                mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
3783            }
3784        }
3785
3786        try {
3787            mInstrumentation.callApplicationOnCreate(app);
3788        } catch (Exception e) {
3789            if (!mInstrumentation.onException(app, e)) {
3790                throw new RuntimeException(
3791                    "Unable to create application " + app.getClass().getName()
3792                    + ": " + e.toString(), e);
3793            }
3794        }
3795    }
3796
3797    /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3798        IActivityManager am = ActivityManagerNative.getDefault();
3799        if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3800            Debug.stopMethodTracing();
3801        }
3802        //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
3803        //      + ", app thr: " + mAppThread);
3804        try {
3805            am.finishInstrumentation(mAppThread, resultCode, results);
3806        } catch (RemoteException ex) {
3807        }
3808    }
3809
3810    private void installContentProviders(
3811            Context context, List<ProviderInfo> providers) {
3812        final ArrayList<IActivityManager.ContentProviderHolder> results =
3813            new ArrayList<IActivityManager.ContentProviderHolder>();
3814
3815        Iterator<ProviderInfo> i = providers.iterator();
3816        while (i.hasNext()) {
3817            ProviderInfo cpi = i.next();
3818            StringBuilder buf = new StringBuilder(128);
3819            buf.append("Pub ");
3820            buf.append(cpi.authority);
3821            buf.append(": ");
3822            buf.append(cpi.name);
3823            Log.i(TAG, buf.toString());
3824            IContentProvider cp = installProvider(context, null, cpi, false);
3825            if (cp != null) {
3826                IActivityManager.ContentProviderHolder cph =
3827                    new IActivityManager.ContentProviderHolder(cpi);
3828                cph.provider = cp;
3829                results.add(cph);
3830                // Don't ever unload this provider from the process.
3831                synchronized(mProviderMap) {
3832                    mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3833                }
3834            }
3835        }
3836
3837        try {
3838            ActivityManagerNative.getDefault().publishContentProviders(
3839                getApplicationThread(), results);
3840        } catch (RemoteException ex) {
3841        }
3842    }
3843
3844    private IContentProvider getExistingProvider(Context context, String name) {
3845        synchronized(mProviderMap) {
3846            final ProviderClientRecord pr = mProviderMap.get(name);
3847            if (pr != null) {
3848                return pr.mProvider;
3849            }
3850            return null;
3851        }
3852    }
3853
3854    private IContentProvider getProvider(Context context, String name) {
3855        IContentProvider existing = getExistingProvider(context, name);
3856        if (existing != null) {
3857            return existing;
3858        }
3859
3860        IActivityManager.ContentProviderHolder holder = null;
3861        try {
3862            holder = ActivityManagerNative.getDefault().getContentProvider(
3863                getApplicationThread(), name);
3864        } catch (RemoteException ex) {
3865        }
3866        if (holder == null) {
3867            Slog.e(TAG, "Failed to find provider info for " + name);
3868            return null;
3869        }
3870
3871        IContentProvider prov = installProvider(context, holder.provider,
3872                holder.info, true);
3873        //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
3874        if (holder.noReleaseNeeded || holder.provider == null) {
3875            // We are not going to release the provider if it is an external
3876            // provider that doesn't care about being released, or if it is
3877            // a local provider running in this process.
3878            //Slog.i(TAG, "*** NO RELEASE NEEDED");
3879            synchronized(mProviderMap) {
3880                mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3881            }
3882        }
3883        return prov;
3884    }
3885
3886    public final IContentProvider acquireProvider(Context c, String name) {
3887        IContentProvider provider = getProvider(c, name);
3888        if(provider == null)
3889            return null;
3890        IBinder jBinder = provider.asBinder();
3891        synchronized(mProviderMap) {
3892            ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3893            if(prc == null) {
3894                mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3895            } else {
3896                prc.count++;
3897            } //end else
3898        } //end synchronized
3899        return provider;
3900    }
3901
3902    public final IContentProvider acquireExistingProvider(Context c, String name) {
3903        IContentProvider provider = getExistingProvider(c, name);
3904        if(provider == null)
3905            return null;
3906        IBinder jBinder = provider.asBinder();
3907        synchronized(mProviderMap) {
3908            ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3909            if(prc == null) {
3910                mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3911            } else {
3912                prc.count++;
3913            } //end else
3914        } //end synchronized
3915        return provider;
3916    }
3917
3918    public final boolean releaseProvider(IContentProvider provider) {
3919        if(provider == null) {
3920            return false;
3921        }
3922        IBinder jBinder = provider.asBinder();
3923        synchronized(mProviderMap) {
3924            ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3925            if(prc == null) {
3926                if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
3927                return false;
3928            } else {
3929                prc.count--;
3930                if(prc.count == 0) {
3931                    // Schedule the actual remove asynchronously, since we
3932                    // don't know the context this will be called in.
3933                    // TODO: it would be nice to post a delayed message, so
3934                    // if we come back and need the same provider quickly
3935                    // we will still have it available.
3936                    Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3937                    mH.sendMessage(msg);
3938                } //end if
3939            } //end else
3940        } //end synchronized
3941        return true;
3942    }
3943
3944    final void completeRemoveProvider(IContentProvider provider) {
3945        IBinder jBinder = provider.asBinder();
3946        String name = null;
3947        synchronized(mProviderMap) {
3948            ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3949            if(prc != null && prc.count == 0) {
3950                mProviderRefCountMap.remove(jBinder);
3951                //invoke removeProvider to dereference provider
3952                name = removeProviderLocked(provider);
3953            }
3954        }
3955
3956        if (name != null) {
3957            try {
3958                if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
3959                        "ActivityManagerNative.removeContentProvider(" + name);
3960                ActivityManagerNative.getDefault().removeContentProvider(
3961                        getApplicationThread(), name);
3962            } catch (RemoteException e) {
3963                //do nothing content provider object is dead any way
3964            } //end catch
3965        }
3966    }
3967
3968    public final String removeProviderLocked(IContentProvider provider) {
3969        if (provider == null) {
3970            return null;
3971        }
3972        IBinder providerBinder = provider.asBinder();
3973
3974        String name = null;
3975
3976        // remove the provider from mProviderMap
3977        Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
3978        while (iter.hasNext()) {
3979            ProviderClientRecord pr = iter.next();
3980            IBinder myBinder = pr.mProvider.asBinder();
3981            if (myBinder == providerBinder) {
3982                //find if its published by this process itself
3983                if(pr.mLocalProvider != null) {
3984                    if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
3985                    return name;
3986                }
3987                if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
3988                        "death recipient");
3989                //content provider is in another process
3990                myBinder.unlinkToDeath(pr, 0);
3991                iter.remove();
3992                //invoke remove only once for the very first name seen
3993                if(name == null) {
3994                    name = pr.mName;
3995                }
3996            } //end if myBinder
3997        }  //end while iter
3998
3999        return name;
4000    }
4001
4002    final void removeDeadProvider(String name, IContentProvider provider) {
4003        synchronized(mProviderMap) {
4004            ProviderClientRecord pr = mProviderMap.get(name);
4005            if (pr.mProvider.asBinder() == provider.asBinder()) {
4006                Slog.i(TAG, "Removing dead content provider: " + name);
4007                ProviderClientRecord removed = mProviderMap.remove(name);
4008                if (removed != null) {
4009                    removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4010                }
4011            }
4012        }
4013    }
4014
4015    final void removeDeadProviderLocked(String name, IContentProvider provider) {
4016        ProviderClientRecord pr = mProviderMap.get(name);
4017        if (pr.mProvider.asBinder() == provider.asBinder()) {
4018            Slog.i(TAG, "Removing dead content provider: " + name);
4019            ProviderClientRecord removed = mProviderMap.remove(name);
4020            if (removed != null) {
4021                removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4022            }
4023        }
4024    }
4025
4026    private IContentProvider installProvider(Context context,
4027            IContentProvider provider, ProviderInfo info, boolean noisy) {
4028        ContentProvider localProvider = null;
4029        if (provider == null) {
4030            if (noisy) {
4031                Slog.d(TAG, "Loading provider " + info.authority + ": "
4032                        + info.name);
4033            }
4034            Context c = null;
4035            ApplicationInfo ai = info.applicationInfo;
4036            if (context.getPackageName().equals(ai.packageName)) {
4037                c = context;
4038            } else if (mInitialApplication != null &&
4039                    mInitialApplication.getPackageName().equals(ai.packageName)) {
4040                c = mInitialApplication;
4041            } else {
4042                try {
4043                    c = context.createPackageContext(ai.packageName,
4044                            Context.CONTEXT_INCLUDE_CODE);
4045                } catch (PackageManager.NameNotFoundException e) {
4046                    // Ignore
4047                }
4048            }
4049            if (c == null) {
4050                Slog.w(TAG, "Unable to get context for package " +
4051                      ai.packageName +
4052                      " while loading content provider " +
4053                      info.name);
4054                return null;
4055            }
4056            try {
4057                final java.lang.ClassLoader cl = c.getClassLoader();
4058                localProvider = (ContentProvider)cl.
4059                    loadClass(info.name).newInstance();
4060                provider = localProvider.getIContentProvider();
4061                if (provider == null) {
4062                    Slog.e(TAG, "Failed to instantiate class " +
4063                          info.name + " from sourceDir " +
4064                          info.applicationInfo.sourceDir);
4065                    return null;
4066                }
4067                if (false) Slog.v(
4068                    TAG, "Instantiating local provider " + info.name);
4069                // XXX Need to create the correct context for this provider.
4070                localProvider.attachInfo(c, info);
4071            } catch (java.lang.Exception e) {
4072                if (!mInstrumentation.onException(null, e)) {
4073                    throw new RuntimeException(
4074                            "Unable to get provider " + info.name
4075                            + ": " + e.toString(), e);
4076                }
4077                return null;
4078            }
4079        } else if (localLOGV) {
4080            Slog.v(TAG, "Installing external provider " + info.authority + ": "
4081                    + info.name);
4082        }
4083
4084        synchronized (mProviderMap) {
4085            // Cache the pointer for the remote provider.
4086            String names[] = PATTERN_SEMICOLON.split(info.authority);
4087            for (int i=0; i<names.length; i++) {
4088                ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
4089                        localProvider);
4090                try {
4091                    provider.asBinder().linkToDeath(pr, 0);
4092                    mProviderMap.put(names[i], pr);
4093                } catch (RemoteException e) {
4094                    return null;
4095                }
4096            }
4097            if (localProvider != null) {
4098                mLocalProviders.put(provider.asBinder(),
4099                        new ProviderClientRecord(null, provider, localProvider));
4100            }
4101        }
4102
4103        return provider;
4104    }
4105
4106    private void attach(boolean system) {
4107        sThreadLocal.set(this);
4108        mSystemThread = system;
4109        if (!system) {
4110            ViewRootImpl.addFirstDrawHandler(new Runnable() {
4111                public void run() {
4112                    ensureJitEnabled();
4113                }
4114            });
4115            android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4116            RuntimeInit.setApplicationObject(mAppThread.asBinder());
4117            IActivityManager mgr = ActivityManagerNative.getDefault();
4118            try {
4119                mgr.attachApplication(mAppThread);
4120            } catch (RemoteException ex) {
4121                // Ignore
4122            }
4123        } else {
4124            // Don't set application object here -- if the system crashes,
4125            // we can't display an alert, we just want to die die die.
4126            android.ddm.DdmHandleAppName.setAppName("system_process");
4127            try {
4128                mInstrumentation = new Instrumentation();
4129                ContextImpl context = new ContextImpl();
4130                context.init(getSystemContext().mPackageInfo, null, this);
4131                Application app = Instrumentation.newApplication(Application.class, context);
4132                mAllApplications.add(app);
4133                mInitialApplication = app;
4134                app.onCreate();
4135            } catch (Exception e) {
4136                throw new RuntimeException(
4137                        "Unable to instantiate Application():" + e.toString(), e);
4138            }
4139        }
4140
4141        ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
4142            public void onConfigurationChanged(Configuration newConfig) {
4143                synchronized (mPackages) {
4144                    // We need to apply this change to the resources
4145                    // immediately, because upon returning the view
4146                    // hierarchy will be informed about it.
4147                    if (applyConfigurationToResourcesLocked(newConfig, null)) {
4148                        // This actually changed the resources!  Tell
4149                        // everyone about it.
4150                        if (mPendingConfiguration == null ||
4151                                mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4152                            mPendingConfiguration = newConfig;
4153
4154                            queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4155                        }
4156                    }
4157                }
4158            }
4159            public void onLowMemory() {
4160            }
4161            public void onTrimMemory(int level) {
4162            }
4163        });
4164    }
4165
4166    public static final ActivityThread systemMain() {
4167        HardwareRenderer.disable();
4168        ActivityThread thread = new ActivityThread();
4169        thread.attach(true);
4170        return thread;
4171    }
4172
4173    public final void installSystemProviders(List<ProviderInfo> providers) {
4174        if (providers != null) {
4175            installContentProviders(mInitialApplication, providers);
4176        }
4177    }
4178
4179    public int getIntCoreSetting(String key, int defaultValue) {
4180        synchronized (mPackages) {
4181            if (mCoreSettings != null) {
4182                return mCoreSettings.getInt(key, defaultValue);
4183            } else {
4184                return defaultValue;
4185            }
4186        }
4187    }
4188
4189    public static void main(String[] args) {
4190        SamplingProfilerIntegration.start();
4191
4192        // CloseGuard defaults to true and can be quite spammy.  We
4193        // disable it here, but selectively enable it later (via
4194        // StrictMode) on debug builds, but using DropBox, not logs.
4195        CloseGuard.setEnabled(false);
4196
4197        Process.setArgV0("<pre-initialized>");
4198
4199        Looper.prepareMainLooper();
4200        if (sMainThreadHandler == null) {
4201            sMainThreadHandler = new Handler();
4202        }
4203
4204        ActivityThread thread = new ActivityThread();
4205        thread.attach(false);
4206
4207        if (false) {
4208            Looper.myLooper().setMessageLogging(new
4209                    LogPrinter(Log.DEBUG, "ActivityThread"));
4210        }
4211
4212        Looper.loop();
4213
4214        throw new RuntimeException("Main thread loop unexpectedly exited");
4215    }
4216}
4217