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