ActivityManager.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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.IPackageDataObserver;
23import android.graphics.Bitmap;
24import android.os.RemoteException;
25import android.os.Handler;
26import android.os.IBinder;
27import android.os.Parcel;
28import android.os.Parcelable;
29import android.os.Parcelable.Creator;
30import android.text.TextUtils;
31import android.util.Log;
32import java.util.List;
33
34/**
35 * Interact with the overall activities running in the system.
36 */
37public class ActivityManager {
38    private static String TAG = "ActivityManager";
39    private static boolean DEBUG = false;
40    private static boolean localLOGV = DEBUG || android.util.Config.LOGV;
41
42    private final Context mContext;
43    private final Handler mHandler;
44
45    /*package*/ ActivityManager(Context context, Handler handler) {
46        mContext = context;
47        mHandler = handler;
48    }
49
50    /**
51     * Information you can retrieve about tasks that the user has most recently
52     * started or visited.
53     */
54    public static class RecentTaskInfo implements Parcelable {
55        /**
56         * If this task is currently running, this is the identifier for it.
57         * If it is not running, this will be -1.
58         */
59        public int id;
60
61        /**
62         * The original Intent used to launch the task.  You can use this
63         * Intent to re-launch the task (if it is no longer running) or bring
64         * the current task to the front.
65         */
66        public Intent baseIntent;
67
68        /**
69         * If this task was started from an alias, this is the actual
70         * activity component that was initially started; the component of
71         * the baseIntent in this case is the name of the actual activity
72         * implementation that the alias referred to.  Otherwise, this is null.
73         */
74        public ComponentName origActivity;
75
76        public RecentTaskInfo() {
77        }
78
79        public int describeContents() {
80            return 0;
81        }
82
83        public void writeToParcel(Parcel dest, int flags) {
84            dest.writeInt(id);
85            if (baseIntent != null) {
86                dest.writeInt(1);
87                baseIntent.writeToParcel(dest, 0);
88            } else {
89                dest.writeInt(0);
90            }
91            ComponentName.writeToParcel(origActivity, dest);
92        }
93
94        public void readFromParcel(Parcel source) {
95            id = source.readInt();
96            if (source.readInt() != 0) {
97                baseIntent = Intent.CREATOR.createFromParcel(source);
98            } else {
99                baseIntent = null;
100            }
101            origActivity = ComponentName.readFromParcel(source);
102        }
103
104        public static final Creator<RecentTaskInfo> CREATOR
105                = new Creator<RecentTaskInfo>() {
106            public RecentTaskInfo createFromParcel(Parcel source) {
107                return new RecentTaskInfo(source);
108            }
109            public RecentTaskInfo[] newArray(int size) {
110                return new RecentTaskInfo[size];
111            }
112        };
113
114        private RecentTaskInfo(Parcel source) {
115            readFromParcel(source);
116        }
117    }
118
119    /**
120     * Flag for use with {@link #getRecentTasks}: return all tasks, even those
121     * that have set their
122     * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
123     */
124    public static final int RECENT_WITH_EXCLUDED = 0x0001;
125
126    /**
127     * Return a list of the tasks that the user has recently launched, with
128     * the most recent being first and older ones after in order.
129     *
130     * @param maxNum The maximum number of entries to return in the list.  The
131     * actual number returned may be smaller, depending on how many tasks the
132     * user has started and the maximum number the system can remember.
133     *
134     * @return Returns a list of RecentTaskInfo records describing each of
135     * the recent tasks.
136     *
137     * @throws SecurityException Throws SecurityException if the caller does
138     * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
139     */
140    public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
141            throws SecurityException {
142        try {
143            return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
144                    flags);
145        } catch (RemoteException e) {
146            // System dead, we will be dead too soon!
147            return null;
148        }
149    }
150
151    /**
152     * Information you can retrieve about a particular task that is currently
153     * "running" in the system.  Note that a running task does not mean the
154     * given task actual has a process it is actively running in; it simply
155     * means that the user has gone to it and never closed it, but currently
156     * the system may have killed its process and is only holding on to its
157     * last state in order to restart it when the user returns.
158     */
159    public static class RunningTaskInfo implements Parcelable {
160        /**
161         * A unique identifier for this task.
162         */
163        public int id;
164
165        /**
166         * The component launched as the first activity in the task.  This can
167         * be considered the "application" of this task.
168         */
169        public ComponentName baseActivity;
170
171        /**
172         * The activity component at the top of the history stack of the task.
173         * This is what the user is currently doing.
174         */
175        public ComponentName topActivity;
176
177        /**
178         * Thumbnail representation of the task's current state.
179         */
180        public Bitmap thumbnail;
181
182        /**
183         * Description of the task's current state.
184         */
185        public CharSequence description;
186
187        /**
188         * Number of activities in this task.
189         */
190        public int numActivities;
191
192        /**
193         * Number of activities that are currently running (not stopped
194         * and persisted) in this task.
195         */
196        public int numRunning;
197
198        public RunningTaskInfo() {
199        }
200
201        public int describeContents() {
202            return 0;
203        }
204
205        public void writeToParcel(Parcel dest, int flags) {
206            dest.writeInt(id);
207            ComponentName.writeToParcel(baseActivity, dest);
208            ComponentName.writeToParcel(topActivity, dest);
209            if (thumbnail != null) {
210                dest.writeInt(1);
211                thumbnail.writeToParcel(dest, 0);
212            } else {
213                dest.writeInt(0);
214            }
215            TextUtils.writeToParcel(description, dest,
216                    Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
217            dest.writeInt(numActivities);
218            dest.writeInt(numRunning);
219        }
220
221        public void readFromParcel(Parcel source) {
222            id = source.readInt();
223            baseActivity = ComponentName.readFromParcel(source);
224            topActivity = ComponentName.readFromParcel(source);
225            if (source.readInt() != 0) {
226                thumbnail = Bitmap.CREATOR.createFromParcel(source);
227            } else {
228                thumbnail = null;
229            }
230            description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
231            numActivities = source.readInt();
232            numRunning = source.readInt();
233        }
234
235        public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
236            public RunningTaskInfo createFromParcel(Parcel source) {
237                return new RunningTaskInfo(source);
238            }
239            public RunningTaskInfo[] newArray(int size) {
240                return new RunningTaskInfo[size];
241            }
242        };
243
244        private RunningTaskInfo(Parcel source) {
245            readFromParcel(source);
246        }
247    }
248
249    /**
250     * Return a list of the tasks that are currently running, with
251     * the most recent being first and older ones after in order.  Note that
252     * "running" does not mean any of the task's code is currently loaded or
253     * activity -- the task may have been frozen by the system, so that it
254     * can be restarted in its previous state when next brought to the
255     * foreground.
256     *
257     * @param maxNum The maximum number of entries to return in the list.  The
258     * actual number returned may be smaller, depending on how many tasks the
259     * user has started.
260     *
261     * @return Returns a list of RunningTaskInfo records describing each of
262     * the running tasks.
263     *
264     * @throws SecurityException Throws SecurityException if the caller does
265     * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
266     */
267    public List<RunningTaskInfo> getRunningTasks(int maxNum)
268            throws SecurityException {
269        try {
270            return (List<RunningTaskInfo>)ActivityManagerNative.getDefault()
271                    .getTasks(maxNum, 0, null);
272        } catch (RemoteException e) {
273            // System dead, we will be dead too soon!
274            return null;
275        }
276    }
277
278    /**
279     * Information you can retrieve about a particular Service that is
280     * currently running in the system.
281     */
282    public static class RunningServiceInfo implements Parcelable {
283        /**
284         * The service component.
285         */
286        public ComponentName service;
287
288        /**
289         * If non-zero, this is the process the service is running in.
290         */
291        public int pid;
292
293        /**
294         * The name of the process this service runs in.
295         */
296        public String process;
297
298        /**
299         * Set to true if the service has asked to run as a foreground process.
300         */
301        public boolean foreground;
302
303        /**
304         * The time when the service was first made activity, either by someone
305         * starting or binding to it.
306         */
307        public long activeSince;
308
309        /**
310         * Set to true if this service has been explicitly started.
311         */
312        public boolean started;
313
314        /**
315         * Number of clients connected to the service.
316         */
317        public int clientCount;
318
319        /**
320         * Number of times the service's process has crashed while the service
321         * is running.
322         */
323        public int crashCount;
324
325        /**
326         * The time when there was last activity in the service (either
327         * explicit requests to start it or clients binding to it).
328         */
329        public long lastActivityTime;
330
331        /**
332         * If non-zero, this service is not currently running, but scheduled to
333         * restart at the given time.
334         */
335        public long restarting;
336
337        public RunningServiceInfo() {
338        }
339
340        public int describeContents() {
341            return 0;
342        }
343
344        public void writeToParcel(Parcel dest, int flags) {
345            ComponentName.writeToParcel(service, dest);
346            dest.writeInt(pid);
347            dest.writeString(process);
348            dest.writeInt(foreground ? 1 : 0);
349            dest.writeLong(activeSince);
350            dest.writeInt(started ? 1 : 0);
351            dest.writeInt(clientCount);
352            dest.writeInt(crashCount);
353            dest.writeLong(lastActivityTime);
354            dest.writeLong(restarting);
355        }
356
357        public void readFromParcel(Parcel source) {
358            service = ComponentName.readFromParcel(source);
359            pid = source.readInt();
360            process = source.readString();
361            foreground = source.readInt() != 0;
362            activeSince = source.readLong();
363            started = source.readInt() != 0;
364            clientCount = source.readInt();
365            crashCount = source.readInt();
366            lastActivityTime = source.readLong();
367            restarting = source.readLong();
368        }
369
370        public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
371            public RunningServiceInfo createFromParcel(Parcel source) {
372                return new RunningServiceInfo(source);
373            }
374            public RunningServiceInfo[] newArray(int size) {
375                return new RunningServiceInfo[size];
376            }
377        };
378
379        private RunningServiceInfo(Parcel source) {
380            readFromParcel(source);
381        }
382    }
383
384    /**
385     * Return a list of the services that are currently running.
386     *
387     * @param maxNum The maximum number of entries to return in the list.  The
388     * actual number returned may be smaller, depending on how many services
389     * are running.
390     *
391     * @return Returns a list of RunningServiceInfo records describing each of
392     * the running tasks.
393     */
394    public List<RunningServiceInfo> getRunningServices(int maxNum)
395            throws SecurityException {
396        try {
397            return (List<RunningServiceInfo>)ActivityManagerNative.getDefault()
398                    .getServices(maxNum, 0);
399        } catch (RemoteException e) {
400            // System dead, we will be dead too soon!
401            return null;
402        }
403    }
404
405    /**
406     * Information you can retrieve about the available memory through
407     * {@link ActivityManager#getMemoryInfo}.
408     */
409    public static class MemoryInfo implements Parcelable {
410        /**
411         * The total available memory on the system.  This number should not
412         * be considered absolute: due to the nature of the kernel, a significant
413         * portion of this memory is actually in use and needed for the overall
414         * system to run well.
415         */
416        public long availMem;
417
418        /**
419         * The threshold of {@link #availMem} at which we consider memory to be
420         * low and start killing background services and other non-extraneous
421         * processes.
422         */
423        public long threshold;
424
425        /**
426         * Set to true if the system considers itself to currently be in a low
427         * memory situation.
428         */
429        public boolean lowMemory;
430
431        public MemoryInfo() {
432        }
433
434        public int describeContents() {
435            return 0;
436        }
437
438        public void writeToParcel(Parcel dest, int flags) {
439            dest.writeLong(availMem);
440            dest.writeLong(threshold);
441            dest.writeInt(lowMemory ? 1 : 0);
442        }
443
444        public void readFromParcel(Parcel source) {
445            availMem = source.readLong();
446            threshold = source.readLong();
447            lowMemory = source.readInt() != 0;
448        }
449
450        public static final Creator<MemoryInfo> CREATOR
451                = new Creator<MemoryInfo>() {
452            public MemoryInfo createFromParcel(Parcel source) {
453                return new MemoryInfo(source);
454            }
455            public MemoryInfo[] newArray(int size) {
456                return new MemoryInfo[size];
457            }
458        };
459
460        private MemoryInfo(Parcel source) {
461            readFromParcel(source);
462        }
463    }
464
465    public void getMemoryInfo(MemoryInfo outInfo) {
466        try {
467            ActivityManagerNative.getDefault().getMemoryInfo(outInfo);
468        } catch (RemoteException e) {
469        }
470    }
471
472    /**
473     * @hide
474     */
475    public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
476        try {
477            return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
478                    observer);
479        } catch (RemoteException e) {
480            return false;
481        }
482    }
483
484    /**
485     * Information you can retrieve about any processes that are in an error condition.
486     */
487    public static class ProcessErrorStateInfo implements Parcelable {
488        /**
489         * Condition codes
490         */
491        public static final int NO_ERROR = 0;
492        public static final int CRASHED = 1;
493        public static final int NOT_RESPONDING = 2;
494
495        /**
496         * The condition that the process is in.
497         */
498        public int condition;
499
500        /**
501         * The process name in which the crash or error occurred.
502         */
503        public String processName;
504
505        /**
506         * The pid of this process; 0 if none
507         */
508        public int pid;
509
510        /**
511         * The kernel user-ID that has been assigned to this process;
512         * currently this is not a unique ID (multiple applications can have
513         * the same uid).
514         */
515        public int uid;
516
517        /**
518         * The tag that was provided when the process crashed.
519         */
520        public String tag;
521
522        /**
523         * A short message describing the error condition.
524         */
525        public String shortMsg;
526
527        /**
528         * A long message describing the error condition.
529         */
530        public String longMsg;
531
532        /**
533         * Raw data about the crash (typically a stack trace).
534         */
535        public byte[] crashData;
536
537        public ProcessErrorStateInfo() {
538        }
539
540        public int describeContents() {
541            return 0;
542        }
543
544        public void writeToParcel(Parcel dest, int flags) {
545            dest.writeInt(condition);
546            dest.writeString(processName);
547            dest.writeInt(pid);
548            dest.writeInt(uid);
549            dest.writeString(tag);
550            dest.writeString(shortMsg);
551            dest.writeString(longMsg);
552            dest.writeInt(crashData == null ? -1 : crashData.length);
553            dest.writeByteArray(crashData);
554        }
555
556        public void readFromParcel(Parcel source) {
557            condition = source.readInt();
558            processName = source.readString();
559            pid = source.readInt();
560            uid = source.readInt();
561            tag = source.readString();
562            shortMsg = source.readString();
563            longMsg = source.readString();
564            int cdLen = source.readInt();
565            if (cdLen == -1) {
566                crashData = null;
567            } else {
568                crashData = new byte[cdLen];
569                source.readByteArray(crashData);
570            }
571        }
572
573        public static final Creator<ProcessErrorStateInfo> CREATOR =
574                new Creator<ProcessErrorStateInfo>() {
575            public ProcessErrorStateInfo createFromParcel(Parcel source) {
576                return new ProcessErrorStateInfo(source);
577            }
578            public ProcessErrorStateInfo[] newArray(int size) {
579                return new ProcessErrorStateInfo[size];
580            }
581        };
582
583        private ProcessErrorStateInfo(Parcel source) {
584            readFromParcel(source);
585        }
586    }
587
588    /**
589     * Returns a list of any processes that are currently in an error condition.  The result
590     * will be null if all processes are running properly at this time.
591     *
592     * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
593     * current error conditions (it will not return an empty list).  This list ordering is not
594     * specified.
595     */
596    public List<ProcessErrorStateInfo> getProcessesInErrorState() {
597        try {
598            return ActivityManagerNative.getDefault().getProcessesInErrorState();
599        } catch (RemoteException e) {
600            return null;
601        }
602    }
603
604    /**
605     * Information you can retrieve about a running process.
606     */
607    public static class RunningAppProcessInfo implements Parcelable {
608        /**
609         * The name of the process that this object is associated with
610         */
611        public String processName;
612
613        /**
614         * The pid of this process; 0 if none
615         */
616        public int pid;
617
618        public String pkgList[];
619
620        public RunningAppProcessInfo() {
621        }
622
623        public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
624            processName = pProcessName;
625            pid = pPid;
626            pkgList = pArr;
627        }
628
629        public int describeContents() {
630            return 0;
631        }
632
633        public void writeToParcel(Parcel dest, int flags) {
634            dest.writeString(processName);
635            dest.writeInt(pid);
636            dest.writeStringArray(pkgList);
637        }
638
639        public void readFromParcel(Parcel source) {
640            processName = source.readString();
641            pid = source.readInt();
642            pkgList = source.readStringArray();
643        }
644
645        public static final Creator<RunningAppProcessInfo> CREATOR =
646            new Creator<RunningAppProcessInfo>() {
647            public RunningAppProcessInfo createFromParcel(Parcel source) {
648                return new RunningAppProcessInfo(source);
649            }
650            public RunningAppProcessInfo[] newArray(int size) {
651                return new RunningAppProcessInfo[size];
652            }
653        };
654
655        private RunningAppProcessInfo(Parcel source) {
656            readFromParcel(source);
657        }
658    }
659
660    /**
661     * Returns a list of application processes that are running on the device.
662     *
663     * @return Returns a list of RunningAppProcessInfo records, or null if there are no
664     * running processes (it will not return an empty list).  This list ordering is not
665     * specified.
666     */
667    public List<RunningAppProcessInfo> getRunningAppProcesses() {
668        try {
669            return ActivityManagerNative.getDefault().getRunningAppProcesses();
670        } catch (RemoteException e) {
671            return null;
672        }
673    }
674}
675