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