ActivityManager.java revision 9adb9c3b10991ef315c270993f4155709c8a232d
1/*
2 * Copyright (C) 2007 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.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ApplicationInfo;
23import android.content.pm.ConfigurationInfo;
24import android.content.pm.IPackageDataObserver;
25import android.graphics.Bitmap;
26import android.os.Debug;
27import android.os.RemoteException;
28import android.os.Handler;
29import android.os.Parcel;
30import android.os.Parcelable;
31import android.os.SystemProperties;
32import android.text.TextUtils;
33import java.util.List;
34
35/**
36 * Interact with the overall activities running in the system.
37 */
38public class ActivityManager {
39    private static String TAG = "ActivityManager";
40    private static boolean DEBUG = false;
41    private static boolean localLOGV = DEBUG || android.util.Config.LOGV;
42
43    private final Context mContext;
44    private final Handler mHandler;
45
46    /*package*/ ActivityManager(Context context, Handler handler) {
47        mContext = context;
48        mHandler = handler;
49    }
50
51    /**
52     * Return the approximate per-application memory class of the current
53     * device.  This gives you an idea of how hard a memory limit you should
54     * impose on your application to let the overall system work best.  The
55     * returned value is in megabytes; the baseline Android memory class is
56     * 16 (which happens to be the Java heap limit of those devices); some
57     * device with more memory may return 24 or even higher numbers.
58     */
59    public int getMemoryClass() {
60        return staticGetMemoryClass();
61    }
62
63    /** @hide */
64    static public int staticGetMemoryClass() {
65        // Really brain dead right now -- just take this from the configured
66        // vm heap size, and assume it is in megabytes and thus ends with "m".
67        String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
68        return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
69    }
70
71    /**
72     * Information you can retrieve about tasks that the user has most recently
73     * started or visited.
74     */
75    public static class RecentTaskInfo implements Parcelable {
76        /**
77         * If this task is currently running, this is the identifier for it.
78         * If it is not running, this will be -1.
79         */
80        public int id;
81
82        /**
83         * The original Intent used to launch the task.  You can use this
84         * Intent to re-launch the task (if it is no longer running) or bring
85         * the current task to the front.
86         */
87        public Intent baseIntent;
88
89        /**
90         * If this task was started from an alias, this is the actual
91         * activity component that was initially started; the component of
92         * the baseIntent in this case is the name of the actual activity
93         * implementation that the alias referred to.  Otherwise, this is null.
94         */
95        public ComponentName origActivity;
96
97        public RecentTaskInfo() {
98        }
99
100        public int describeContents() {
101            return 0;
102        }
103
104        public void writeToParcel(Parcel dest, int flags) {
105            dest.writeInt(id);
106            if (baseIntent != null) {
107                dest.writeInt(1);
108                baseIntent.writeToParcel(dest, 0);
109            } else {
110                dest.writeInt(0);
111            }
112            ComponentName.writeToParcel(origActivity, dest);
113        }
114
115        public void readFromParcel(Parcel source) {
116            id = source.readInt();
117            if (source.readInt() != 0) {
118                baseIntent = Intent.CREATOR.createFromParcel(source);
119            } else {
120                baseIntent = null;
121            }
122            origActivity = ComponentName.readFromParcel(source);
123        }
124
125        public static final Creator<RecentTaskInfo> CREATOR
126                = new Creator<RecentTaskInfo>() {
127            public RecentTaskInfo createFromParcel(Parcel source) {
128                return new RecentTaskInfo(source);
129            }
130            public RecentTaskInfo[] newArray(int size) {
131                return new RecentTaskInfo[size];
132            }
133        };
134
135        private RecentTaskInfo(Parcel source) {
136            readFromParcel(source);
137        }
138    }
139
140    /**
141     * Flag for use with {@link #getRecentTasks}: return all tasks, even those
142     * that have set their
143     * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
144     */
145    public static final int RECENT_WITH_EXCLUDED = 0x0001;
146
147    /**
148     * @hide
149     * TODO: Make this public.  Provides a list that does not contain any
150     * recent tasks that currently are not available to the user.
151     */
152    public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002;
153
154    /**
155     * Return a list of the tasks that the user has recently launched, with
156     * the most recent being first and older ones after in order.
157     *
158     * @param maxNum The maximum number of entries to return in the list.  The
159     * actual number returned may be smaller, depending on how many tasks the
160     * user has started and the maximum number the system can remember.
161     *
162     * @return Returns a list of RecentTaskInfo records describing each of
163     * the recent tasks.
164     *
165     * @throws SecurityException Throws SecurityException if the caller does
166     * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
167     */
168    public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
169            throws SecurityException {
170        try {
171            return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
172                    flags);
173        } catch (RemoteException e) {
174            // System dead, we will be dead too soon!
175            return null;
176        }
177    }
178
179    /**
180     * Information you can retrieve about a particular task that is currently
181     * "running" in the system.  Note that a running task does not mean the
182     * given task actual has a process it is actively running in; it simply
183     * means that the user has gone to it and never closed it, but currently
184     * the system may have killed its process and is only holding on to its
185     * last state in order to restart it when the user returns.
186     */
187    public static class RunningTaskInfo implements Parcelable {
188        /**
189         * A unique identifier for this task.
190         */
191        public int id;
192
193        /**
194         * The component launched as the first activity in the task.  This can
195         * be considered the "application" of this task.
196         */
197        public ComponentName baseActivity;
198
199        /**
200         * The activity component at the top of the history stack of the task.
201         * This is what the user is currently doing.
202         */
203        public ComponentName topActivity;
204
205        /**
206         * Thumbnail representation of the task's current state.
207         */
208        public Bitmap thumbnail;
209
210        /**
211         * Description of the task's current state.
212         */
213        public CharSequence description;
214
215        /**
216         * Number of activities in this task.
217         */
218        public int numActivities;
219
220        /**
221         * Number of activities that are currently running (not stopped
222         * and persisted) in this task.
223         */
224        public int numRunning;
225
226        public RunningTaskInfo() {
227        }
228
229        public int describeContents() {
230            return 0;
231        }
232
233        public void writeToParcel(Parcel dest, int flags) {
234            dest.writeInt(id);
235            ComponentName.writeToParcel(baseActivity, dest);
236            ComponentName.writeToParcel(topActivity, dest);
237            if (thumbnail != null) {
238                dest.writeInt(1);
239                thumbnail.writeToParcel(dest, 0);
240            } else {
241                dest.writeInt(0);
242            }
243            TextUtils.writeToParcel(description, dest,
244                    Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
245            dest.writeInt(numActivities);
246            dest.writeInt(numRunning);
247        }
248
249        public void readFromParcel(Parcel source) {
250            id = source.readInt();
251            baseActivity = ComponentName.readFromParcel(source);
252            topActivity = ComponentName.readFromParcel(source);
253            if (source.readInt() != 0) {
254                thumbnail = Bitmap.CREATOR.createFromParcel(source);
255            } else {
256                thumbnail = null;
257            }
258            description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
259            numActivities = source.readInt();
260            numRunning = source.readInt();
261        }
262
263        public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
264            public RunningTaskInfo createFromParcel(Parcel source) {
265                return new RunningTaskInfo(source);
266            }
267            public RunningTaskInfo[] newArray(int size) {
268                return new RunningTaskInfo[size];
269            }
270        };
271
272        private RunningTaskInfo(Parcel source) {
273            readFromParcel(source);
274        }
275    }
276
277    /**
278     * Return a list of the tasks that are currently running, with
279     * the most recent being first and older ones after in order.  Note that
280     * "running" does not mean any of the task's code is currently loaded or
281     * activity -- the task may have been frozen by the system, so that it
282     * can be restarted in its previous state when next brought to the
283     * foreground.
284     *
285     * @param maxNum The maximum number of entries to return in the list.  The
286     * actual number returned may be smaller, depending on how many tasks the
287     * user has started.
288     *
289     * @param flags Optional flags
290     * @param receiver Optional receiver for delayed thumbnails
291     *
292     * @return Returns a list of RunningTaskInfo records describing each of
293     * the running tasks.
294     *
295     * Some thumbnails may not be available at the time of this call. The optional
296     * receiver may be used to receive those thumbnails.
297     *
298     * @throws SecurityException Throws SecurityException if the caller does
299     * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
300     *
301     * @hide
302     */
303    public List<RunningTaskInfo> getRunningTasks(int maxNum, int flags, IThumbnailReceiver receiver)
304            throws SecurityException {
305        try {
306            return ActivityManagerNative.getDefault().getTasks(maxNum, flags, receiver);
307        } catch (RemoteException e) {
308            // System dead, we will be dead too soon!
309            return null;
310        }
311    }
312
313    /**
314     * Return a list of the tasks that are currently running, with
315     * the most recent being first and older ones after in order.  Note that
316     * "running" does not mean any of the task's code is currently loaded or
317     * activity -- the task may have been frozen by the system, so that it
318     * can be restarted in its previous state when next brought to the
319     * foreground.
320     *
321     * @param maxNum The maximum number of entries to return in the list.  The
322     * actual number returned may be smaller, depending on how many tasks the
323     * user has started.
324     *
325     * @return Returns a list of RunningTaskInfo records describing each of
326     * the running tasks.
327     *
328     * @throws SecurityException Throws SecurityException if the caller does
329     * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
330     */
331    public List<RunningTaskInfo> getRunningTasks(int maxNum)
332            throws SecurityException {
333        return getRunningTasks(maxNum, 0, null);
334    }
335
336    /**
337     * Information you can retrieve about a particular Service that is
338     * currently running in the system.
339     */
340    public static class RunningServiceInfo implements Parcelable {
341        /**
342         * The service component.
343         */
344        public ComponentName service;
345
346        /**
347         * If non-zero, this is the process the service is running in.
348         */
349        public int pid;
350
351        /**
352         * The UID that owns this service.
353         */
354        public int uid;
355
356        /**
357         * The name of the process this service runs in.
358         */
359        public String process;
360
361        /**
362         * Set to true if the service has asked to run as a foreground process.
363         */
364        public boolean foreground;
365
366        /**
367         * The time when the service was first made active, either by someone
368         * starting or binding to it.  This
369         * is in units of {@link android.os.SystemClock#elapsedRealtime()}.
370         */
371        public long activeSince;
372
373        /**
374         * Set to true if this service has been explicitly started.
375         */
376        public boolean started;
377
378        /**
379         * Number of clients connected to the service.
380         */
381        public int clientCount;
382
383        /**
384         * Number of times the service's process has crashed while the service
385         * is running.
386         */
387        public int crashCount;
388
389        /**
390         * The time when there was last activity in the service (either
391         * explicit requests to start it or clients binding to it).  This
392         * is in units of {@link android.os.SystemClock#uptimeMillis()}.
393         */
394        public long lastActivityTime;
395
396        /**
397         * If non-zero, this service is not currently running, but scheduled to
398         * restart at the given time.
399         */
400        public long restarting;
401
402        /**
403         * Bit for {@link #flags}: set if this service has been
404         * explicitly started.
405         */
406        public static final int FLAG_STARTED = 1<<0;
407
408        /**
409         * Bit for {@link #flags}: set if the service has asked to
410         * run as a foreground process.
411         */
412        public static final int FLAG_FOREGROUND = 1<<1;
413
414        /**
415         * Bit for {@link #flags): set if the service is running in a
416         * core system process.
417         */
418        public static final int FLAG_SYSTEM_PROCESS = 1<<2;
419
420        /**
421         * Bit for {@link #flags): set if the service is running in a
422         * persistent process.
423         */
424        public static final int FLAG_PERSISTENT_PROCESS = 1<<3;
425
426        /**
427         * Running flags.
428         */
429        public int flags;
430
431        /**
432         * For special services that are bound to by system code, this is
433         * the package that holds the binding.
434         */
435        public String clientPackage;
436
437        /**
438         * For special services that are bound to by system code, this is
439         * a string resource providing a user-visible label for who the
440         * client is.
441         */
442        public int clientLabel;
443
444        public RunningServiceInfo() {
445        }
446
447        public int describeContents() {
448            return 0;
449        }
450
451        public void writeToParcel(Parcel dest, int flags) {
452            ComponentName.writeToParcel(service, dest);
453            dest.writeInt(pid);
454            dest.writeInt(uid);
455            dest.writeString(process);
456            dest.writeInt(foreground ? 1 : 0);
457            dest.writeLong(activeSince);
458            dest.writeInt(started ? 1 : 0);
459            dest.writeInt(clientCount);
460            dest.writeInt(crashCount);
461            dest.writeLong(lastActivityTime);
462            dest.writeLong(restarting);
463            dest.writeInt(this.flags);
464            dest.writeString(clientPackage);
465            dest.writeInt(clientLabel);
466        }
467
468        public void readFromParcel(Parcel source) {
469            service = ComponentName.readFromParcel(source);
470            pid = source.readInt();
471            uid = source.readInt();
472            process = source.readString();
473            foreground = source.readInt() != 0;
474            activeSince = source.readLong();
475            started = source.readInt() != 0;
476            clientCount = source.readInt();
477            crashCount = source.readInt();
478            lastActivityTime = source.readLong();
479            restarting = source.readLong();
480            flags = source.readInt();
481            clientPackage = source.readString();
482            clientLabel = source.readInt();
483        }
484
485        public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
486            public RunningServiceInfo createFromParcel(Parcel source) {
487                return new RunningServiceInfo(source);
488            }
489            public RunningServiceInfo[] newArray(int size) {
490                return new RunningServiceInfo[size];
491            }
492        };
493
494        private RunningServiceInfo(Parcel source) {
495            readFromParcel(source);
496        }
497    }
498
499    /**
500     * Return a list of the services that are currently running.
501     *
502     * @param maxNum The maximum number of entries to return in the list.  The
503     * actual number returned may be smaller, depending on how many services
504     * are running.
505     *
506     * @return Returns a list of RunningServiceInfo records describing each of
507     * the running tasks.
508     */
509    public List<RunningServiceInfo> getRunningServices(int maxNum)
510            throws SecurityException {
511        try {
512            return (List<RunningServiceInfo>)ActivityManagerNative.getDefault()
513                    .getServices(maxNum, 0);
514        } catch (RemoteException e) {
515            // System dead, we will be dead too soon!
516            return null;
517        }
518    }
519
520    /**
521     * Returns a PendingIntent you can start to show a control panel for the
522     * given running service.  If the service does not have a control panel,
523     * null is returned.
524     */
525    public PendingIntent getRunningServiceControlPanel(ComponentName service)
526            throws SecurityException {
527        try {
528            return ActivityManagerNative.getDefault()
529                    .getRunningServiceControlPanel(service);
530        } catch (RemoteException e) {
531            // System dead, we will be dead too soon!
532            return null;
533        }
534    }
535
536    /**
537     * Information you can retrieve about the available memory through
538     * {@link ActivityManager#getMemoryInfo}.
539     */
540    public static class MemoryInfo implements Parcelable {
541        /**
542         * The total available memory on the system.  This number should not
543         * be considered absolute: due to the nature of the kernel, a significant
544         * portion of this memory is actually in use and needed for the overall
545         * system to run well.
546         */
547        public long availMem;
548
549        /**
550         * The threshold of {@link #availMem} at which we consider memory to be
551         * low and start killing background services and other non-extraneous
552         * processes.
553         */
554        public long threshold;
555
556        /**
557         * Set to true if the system considers itself to currently be in a low
558         * memory situation.
559         */
560        public boolean lowMemory;
561
562        public MemoryInfo() {
563        }
564
565        public int describeContents() {
566            return 0;
567        }
568
569        public void writeToParcel(Parcel dest, int flags) {
570            dest.writeLong(availMem);
571            dest.writeLong(threshold);
572            dest.writeInt(lowMemory ? 1 : 0);
573        }
574
575        public void readFromParcel(Parcel source) {
576            availMem = source.readLong();
577            threshold = source.readLong();
578            lowMemory = source.readInt() != 0;
579        }
580
581        public static final Creator<MemoryInfo> CREATOR
582                = new Creator<MemoryInfo>() {
583            public MemoryInfo createFromParcel(Parcel source) {
584                return new MemoryInfo(source);
585            }
586            public MemoryInfo[] newArray(int size) {
587                return new MemoryInfo[size];
588            }
589        };
590
591        private MemoryInfo(Parcel source) {
592            readFromParcel(source);
593        }
594    }
595
596    public void getMemoryInfo(MemoryInfo outInfo) {
597        try {
598            ActivityManagerNative.getDefault().getMemoryInfo(outInfo);
599        } catch (RemoteException e) {
600        }
601    }
602
603    /**
604     * @hide
605     */
606    public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
607        try {
608            return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
609                    observer);
610        } catch (RemoteException e) {
611            return false;
612        }
613    }
614
615    /**
616     * Information you can retrieve about any processes that are in an error condition.
617     */
618    public static class ProcessErrorStateInfo implements Parcelable {
619        /**
620         * Condition codes
621         */
622        public static final int NO_ERROR = 0;
623        public static final int CRASHED = 1;
624        public static final int NOT_RESPONDING = 2;
625
626        /**
627         * The condition that the process is in.
628         */
629        public int condition;
630
631        /**
632         * The process name in which the crash or error occurred.
633         */
634        public String processName;
635
636        /**
637         * The pid of this process; 0 if none
638         */
639        public int pid;
640
641        /**
642         * The kernel user-ID that has been assigned to this process;
643         * currently this is not a unique ID (multiple applications can have
644         * the same uid).
645         */
646        public int uid;
647
648        /**
649         * The activity name associated with the error, if known.  May be null.
650         */
651        public String tag;
652
653        /**
654         * A short message describing the error condition.
655         */
656        public String shortMsg;
657
658        /**
659         * A long message describing the error condition.
660         */
661        public String longMsg;
662
663        /**
664         * The stack trace where the error originated.  May be null.
665         */
666        public String stackTrace;
667
668        /**
669         * to be deprecated: This value will always be null.
670         */
671        public byte[] crashData = null;
672
673        public ProcessErrorStateInfo() {
674        }
675
676        public int describeContents() {
677            return 0;
678        }
679
680        public void writeToParcel(Parcel dest, int flags) {
681            dest.writeInt(condition);
682            dest.writeString(processName);
683            dest.writeInt(pid);
684            dest.writeInt(uid);
685            dest.writeString(tag);
686            dest.writeString(shortMsg);
687            dest.writeString(longMsg);
688            dest.writeString(stackTrace);
689        }
690
691        public void readFromParcel(Parcel source) {
692            condition = source.readInt();
693            processName = source.readString();
694            pid = source.readInt();
695            uid = source.readInt();
696            tag = source.readString();
697            shortMsg = source.readString();
698            longMsg = source.readString();
699            stackTrace = source.readString();
700        }
701
702        public static final Creator<ProcessErrorStateInfo> CREATOR =
703                new Creator<ProcessErrorStateInfo>() {
704            public ProcessErrorStateInfo createFromParcel(Parcel source) {
705                return new ProcessErrorStateInfo(source);
706            }
707            public ProcessErrorStateInfo[] newArray(int size) {
708                return new ProcessErrorStateInfo[size];
709            }
710        };
711
712        private ProcessErrorStateInfo(Parcel source) {
713            readFromParcel(source);
714        }
715    }
716
717    /**
718     * Returns a list of any processes that are currently in an error condition.  The result
719     * will be null if all processes are running properly at this time.
720     *
721     * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
722     * current error conditions (it will not return an empty list).  This list ordering is not
723     * specified.
724     */
725    public List<ProcessErrorStateInfo> getProcessesInErrorState() {
726        try {
727            return ActivityManagerNative.getDefault().getProcessesInErrorState();
728        } catch (RemoteException e) {
729            return null;
730        }
731    }
732
733    /**
734     * Information you can retrieve about a running process.
735     */
736    public static class RunningAppProcessInfo implements Parcelable {
737        /**
738         * The name of the process that this object is associated with
739         */
740        public String processName;
741
742        /**
743         * The pid of this process; 0 if none
744         */
745        public int pid;
746
747        /**
748         * The user id of this process.
749         */
750        public int uid;
751
752        /**
753         * All packages that have been loaded into the process.
754         */
755        public String pkgList[];
756
757        /**
758         * Constant for {@link #flags}: this is a heavy-weight process,
759         * meaning it will not be killed while in the background.
760         */
761        public static final int FLAG_HEAVY_WEIGHT = 1<<0;
762
763        /**
764         * Flags of information.  May be any of
765         * {@link #FLAG_HEAVY_WEIGHT}.
766         */
767        public int flags;
768
769        /**
770         * Constant for {@link #importance}: this process is running the
771         * foreground UI.
772         */
773        public static final int IMPORTANCE_FOREGROUND = 100;
774
775        /**
776         * Constant for {@link #importance}: this process is running something
777         * that is actively visible to the user, though not in the immediate
778         * foreground.
779         */
780        public static final int IMPORTANCE_VISIBLE = 200;
781
782        /**
783         * Constant for {@link #importance}: this process is running something
784         * that is considered to be actively perceptible to the user.  An
785         * example would be an application performing background music playback.
786         */
787        public static final int IMPORTANCE_PERCEPTIBLE = 130;
788
789        /**
790         * Constant for {@link #importance}: this process is running a
791         * heavy-weight application and thus should not be killed.
792         */
793        public static final int IMPORTANCE_HEAVY_WEIGHT = 170;
794
795        /**
796         * Constant for {@link #importance}: this process is contains services
797         * that should remain running.
798         */
799        public static final int IMPORTANCE_SERVICE = 300;
800
801        /**
802         * Constant for {@link #importance}: this process process contains
803         * background code that is expendable.
804         */
805        public static final int IMPORTANCE_BACKGROUND = 400;
806
807        /**
808         * Constant for {@link #importance}: this process is empty of any
809         * actively running code.
810         */
811        public static final int IMPORTANCE_EMPTY = 500;
812
813        /**
814         * The relative importance level that the system places on this
815         * process.  May be one of {@link #IMPORTANCE_FOREGROUND},
816         * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE},
817         * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}.  These
818         * constants are numbered so that "more important" values are always
819         * smaller than "less important" values.
820         */
821        public int importance;
822
823        /**
824         * An additional ordering within a particular {@link #importance}
825         * category, providing finer-grained information about the relative
826         * utility of processes within a category.  This number means nothing
827         * except that a smaller values are more recently used (and thus
828         * more important).  Currently an LRU value is only maintained for
829         * the {@link #IMPORTANCE_BACKGROUND} category, though others may
830         * be maintained in the future.
831         */
832        public int lru;
833
834        /**
835         * Constant for {@link #importanceReasonCode}: nothing special has
836         * been specified for the reason for this level.
837         */
838        public static final int REASON_UNKNOWN = 0;
839
840        /**
841         * Constant for {@link #importanceReasonCode}: one of the application's
842         * content providers is being used by another process.  The pid of
843         * the client process is in {@link #importanceReasonPid} and the
844         * target provider in this process is in
845         * {@link #importanceReasonComponent}.
846         */
847        public static final int REASON_PROVIDER_IN_USE = 1;
848
849        /**
850         * Constant for {@link #importanceReasonCode}: one of the application's
851         * content providers is being used by another process.  The pid of
852         * the client process is in {@link #importanceReasonPid} and the
853         * target provider in this process is in
854         * {@link #importanceReasonComponent}.
855         */
856        public static final int REASON_SERVICE_IN_USE = 2;
857
858        /**
859         * The reason for {@link #importance}, if any.
860         */
861        public int importanceReasonCode;
862
863        /**
864         * For the specified values of {@link #importanceReasonCode}, this
865         * is the process ID of the other process that is a client of this
866         * process.  This will be 0 if no other process is using this one.
867         */
868        public int importanceReasonPid;
869
870        /**
871         * For the specified values of {@link #importanceReasonCode}, this
872         * is the name of the component that is being used in this process.
873         */
874        public ComponentName importanceReasonComponent;
875
876        public RunningAppProcessInfo() {
877            importance = IMPORTANCE_FOREGROUND;
878            importanceReasonCode = REASON_UNKNOWN;
879        }
880
881        public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
882            processName = pProcessName;
883            pid = pPid;
884            pkgList = pArr;
885        }
886
887        public int describeContents() {
888            return 0;
889        }
890
891        public void writeToParcel(Parcel dest, int flags) {
892            dest.writeString(processName);
893            dest.writeInt(pid);
894            dest.writeInt(uid);
895            dest.writeStringArray(pkgList);
896            dest.writeInt(this.flags);
897            dest.writeInt(importance);
898            dest.writeInt(lru);
899            dest.writeInt(importanceReasonCode);
900            dest.writeInt(importanceReasonPid);
901            ComponentName.writeToParcel(importanceReasonComponent, dest);
902        }
903
904        public void readFromParcel(Parcel source) {
905            processName = source.readString();
906            pid = source.readInt();
907            uid = source.readInt();
908            pkgList = source.readStringArray();
909            flags = source.readInt();
910            importance = source.readInt();
911            lru = source.readInt();
912            importanceReasonCode = source.readInt();
913            importanceReasonPid = source.readInt();
914            importanceReasonComponent = ComponentName.readFromParcel(source);
915        }
916
917        public static final Creator<RunningAppProcessInfo> CREATOR =
918            new Creator<RunningAppProcessInfo>() {
919            public RunningAppProcessInfo createFromParcel(Parcel source) {
920                return new RunningAppProcessInfo(source);
921            }
922            public RunningAppProcessInfo[] newArray(int size) {
923                return new RunningAppProcessInfo[size];
924            }
925        };
926
927        private RunningAppProcessInfo(Parcel source) {
928            readFromParcel(source);
929        }
930    }
931
932    /**
933     * Returns a list of application processes installed on external media
934     * that are running on the device.
935     *
936     * @return Returns a list of ApplicationInfo records, or null if none
937     * This list ordering is not specified.
938     * @hide
939     */
940    public List<ApplicationInfo> getRunningExternalApplications() {
941        try {
942            return ActivityManagerNative.getDefault().getRunningExternalApplications();
943        } catch (RemoteException e) {
944            return null;
945        }
946    }
947
948    /**
949     * Returns a list of application processes that are running on the device.
950     *
951     * @return Returns a list of RunningAppProcessInfo records, or null if there are no
952     * running processes (it will not return an empty list).  This list ordering is not
953     * specified.
954     */
955    public List<RunningAppProcessInfo> getRunningAppProcesses() {
956        try {
957            return ActivityManagerNative.getDefault().getRunningAppProcesses();
958        } catch (RemoteException e) {
959            return null;
960        }
961    }
962
963    /**
964     * Return information about the memory usage of one or more processes.
965     *
966     * @param pids The pids of the processes whose memory usage is to be
967     * retrieved.
968     * @return Returns an array of memory information, one for each
969     * requested pid.
970     */
971    public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
972        try {
973            return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids);
974        } catch (RemoteException e) {
975            return null;
976        }
977    }
978
979    /**
980     * @deprecated This is now just a wrapper for
981     * {@link #killBackgroundProcesses(String)}; the previous behavior here
982     * is no longer available to applications because it allows them to
983     * break other applications by removing their alarms, stopping their
984     * services, etc.
985     */
986    @Deprecated
987    public void restartPackage(String packageName) {
988        killBackgroundProcesses(packageName);
989    }
990
991    /**
992     * Have the system immediately kill all background processes associated
993     * with the given package.  This is the same as the kernel killing those
994     * processes to reclaim memory; the system will take care of restarting
995     * these processes in the future as needed.
996     *
997     * <p>You must hold the permission
998     * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to
999     * call this method.
1000     *
1001     * @param packageName The name of the package whose processes are to
1002     * be killed.
1003     */
1004    public void killBackgroundProcesses(String packageName) {
1005        try {
1006            ActivityManagerNative.getDefault().killBackgroundProcesses(packageName);
1007        } catch (RemoteException e) {
1008        }
1009    }
1010
1011    /**
1012     * Have the system perform a force stop of everything associated with
1013     * the given application package.  All processes that share its uid
1014     * will be killed, all services it has running stopped, all activities
1015     * removed, etc.  In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
1016     * broadcast will be sent, so that any of its registered alarms can
1017     * be stopped, notifications removed, etc.
1018     *
1019     * <p>You must hold the permission
1020     * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
1021     * call this method.
1022     *
1023     * @param packageName The name of the package to be stopped.
1024     *
1025     * @hide This is not available to third party applications due to
1026     * it allowing them to break other applications by stopping their
1027     * services, removing their alarms, etc.
1028     */
1029    public void forceStopPackage(String packageName) {
1030        try {
1031            ActivityManagerNative.getDefault().forceStopPackage(packageName);
1032        } catch (RemoteException e) {
1033        }
1034    }
1035
1036    /**
1037     * Get the device configuration attributes.
1038     */
1039    public ConfigurationInfo getDeviceConfigurationInfo() {
1040        try {
1041            return ActivityManagerNative.getDefault().getDeviceConfigurationInfo();
1042        } catch (RemoteException e) {
1043        }
1044        return null;
1045    }
1046
1047    /**
1048     * Returns "true" if the user interface is currently being messed with
1049     * by a monkey.
1050     */
1051    public static boolean isUserAMonkey() {
1052        try {
1053            return ActivityManagerNative.getDefault().isUserAMonkey();
1054        } catch (RemoteException e) {
1055        }
1056        return false;
1057    }
1058}
1059