ServiceRecord.java revision 0c5001d776d56bae02a5cc2663286a125d99bc5e
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
19import com.android.internal.os.BatteryStatsImpl;
20import com.android.server.NotificationManagerService;
21
22import android.app.INotificationManager;
23import android.app.Notification;
24import android.app.NotificationManager;
25import android.content.ComponentName;
26import android.content.Intent;
27import android.content.pm.ApplicationInfo;
28import android.content.pm.ServiceInfo;
29import android.os.Binder;
30import android.os.IBinder;
31import android.os.RemoteException;
32import android.os.SystemClock;
33import android.util.Slog;
34import android.util.TimeUtils;
35
36import java.io.PrintWriter;
37import java.util.ArrayList;
38import java.util.HashMap;
39import java.util.HashSet;
40import java.util.Iterator;
41import java.util.List;
42
43/**
44 * A running application service.
45 */
46class ServiceRecord extends Binder {
47    // Maximum number of delivery attempts before giving up.
48    static final int MAX_DELIVERY_COUNT = 3;
49
50    // Maximum number of times it can fail during execution before giving up.
51    static final int MAX_DONE_EXECUTING_COUNT = 6;
52
53    final ActivityManagerService ams;
54    final BatteryStatsImpl.Uid.Pkg.Serv stats;
55    final ComponentName name; // service component.
56    final String shortName; // name.flattenToShortString().
57    final Intent.FilterComparison intent;
58                            // original intent used to find service.
59    final ServiceInfo serviceInfo;
60                            // all information about the service.
61    final ApplicationInfo appInfo;
62                            // information about service's app.
63    final String packageName; // the package implementing intent's component
64    final String processName; // process where this component wants to run
65    final String permission;// permission needed to access service
66    final String baseDir;   // where activity source (resources etc) located
67    final String resDir;   // where public activity source (public resources etc) located
68    final String dataDir;   // where activity data should go
69    final boolean exported; // from ServiceInfo.exported
70    final Runnable restarter; // used to schedule retries of starting the service
71    final long createTime;  // when this service was created
72    final HashMap<Intent.FilterComparison, IntentBindRecord> bindings
73            = new HashMap<Intent.FilterComparison, IntentBindRecord>();
74                            // All active bindings to the service.
75    final HashMap<IBinder, ArrayList<ConnectionRecord>> connections
76            = new HashMap<IBinder, ArrayList<ConnectionRecord>>();
77                            // IBinder -> ConnectionRecord of all bound clients
78
79    ProcessRecord app;      // where this service is running or null.
80    boolean isForeground;   // is service currently in foreground mode?
81    int foregroundId;       // Notification ID of last foreground req.
82    Notification foregroundNoti; // Notification record of foreground state.
83    long lastActivity;      // last time there was some activity on the service.
84    boolean startRequested; // someone explicitly called start?
85    boolean stopIfKilled;   // last onStart() said to stop if service killed?
86    boolean callStart;      // last onStart() has asked to alway be called on restart.
87    int executeNesting;     // number of outstanding operations keeping foreground.
88    long executingStart;    // start time of last execute request.
89    int crashCount;         // number of times proc has crashed with service running
90    int totalRestartCount;  // number of times we have had to restart.
91    int restartCount;       // number of restarts performed in a row.
92    long restartDelay;      // delay until next restart attempt.
93    long restartTime;       // time of last restart.
94    long nextRestartTime;   // time when restartDelay will expire.
95
96    String stringName;      // caching of toString
97
98    private int lastStartId;    // identifier of most recent start request.
99
100    static class StartItem {
101        final ServiceRecord sr;
102        final boolean taskRemoved;
103        final int id;
104        final Intent intent;
105        final int targetPermissionUid;
106        long deliveredTime;
107        int deliveryCount;
108        int doneExecutingCount;
109        UriPermissionOwner uriPermissions;
110
111        String stringName;      // caching of toString
112
113        StartItem(ServiceRecord _sr, boolean _taskRemoved, int _id, Intent _intent,
114                int _targetPermissionUid) {
115            sr = _sr;
116            taskRemoved = _taskRemoved;
117            id = _id;
118            intent = _intent;
119            targetPermissionUid = _targetPermissionUid;
120        }
121
122        UriPermissionOwner getUriPermissionsLocked() {
123            if (uriPermissions == null) {
124                uriPermissions = new UriPermissionOwner(sr.ams, this);
125            }
126            return uriPermissions;
127        }
128
129        void removeUriPermissionsLocked() {
130            if (uriPermissions != null) {
131                uriPermissions.removeUriPermissionsLocked();
132                uriPermissions = null;
133            }
134        }
135
136        public String toString() {
137            if (stringName != null) {
138                return stringName;
139            }
140            StringBuilder sb = new StringBuilder(128);
141            sb.append("ServiceRecord{")
142                .append(Integer.toHexString(System.identityHashCode(sr)))
143                .append(' ').append(sr.shortName)
144                .append(" StartItem ")
145                .append(Integer.toHexString(System.identityHashCode(this)))
146                .append(" id=").append(id).append('}');
147            return stringName = sb.toString();
148        }
149    }
150
151    final ArrayList<StartItem> deliveredStarts = new ArrayList<StartItem>();
152                            // start() arguments which been delivered.
153    final ArrayList<StartItem> pendingStarts = new ArrayList<StartItem>();
154                            // start() arguments that haven't yet been delivered.
155
156    void dumpStartList(PrintWriter pw, String prefix, List<StartItem> list, long now) {
157        final int N = list.size();
158        for (int i=0; i<N; i++) {
159            StartItem si = list.get(i);
160            pw.print(prefix); pw.print("#"); pw.print(i);
161                    pw.print(" id="); pw.print(si.id);
162                    if (now != 0) {
163                        pw.print(" dur=");
164                        TimeUtils.formatDuration(si.deliveredTime, now, pw);
165                    }
166                    if (si.deliveryCount != 0) {
167                        pw.print(" dc="); pw.print(si.deliveryCount);
168                    }
169                    if (si.doneExecutingCount != 0) {
170                        pw.print(" dxc="); pw.print(si.doneExecutingCount);
171                    }
172                    pw.println("");
173            pw.print(prefix); pw.print("  intent=");
174                    if (si.intent != null) pw.println(si.intent.toString());
175                    else pw.println("null");
176            if (si.targetPermissionUid >= 0) {
177                pw.print(prefix); pw.print("  targetPermissionUid=");
178                        pw.println(si.targetPermissionUid);
179            }
180            if (si.uriPermissions != null) {
181                if (si.uriPermissions.readUriPermissions != null) {
182                    pw.print(prefix); pw.print("  readUriPermissions=");
183                            pw.println(si.uriPermissions.readUriPermissions);
184                }
185                if (si.uriPermissions.writeUriPermissions != null) {
186                    pw.print(prefix); pw.print("  writeUriPermissions=");
187                            pw.println(si.uriPermissions.writeUriPermissions);
188                }
189            }
190        }
191    }
192
193    void dump(PrintWriter pw, String prefix) {
194        pw.print(prefix); pw.print("intent={");
195                pw.print(intent.getIntent().toShortString(true, false));
196                pw.println('}');
197        pw.print(prefix); pw.print("packageName="); pw.println(packageName);
198        pw.print(prefix); pw.print("processName="); pw.println(processName);
199        if (permission != null) {
200            pw.print(prefix); pw.print("permission="); pw.println(permission);
201        }
202        long now = SystemClock.uptimeMillis();
203        long nowReal = SystemClock.elapsedRealtime();
204        pw.print(prefix); pw.print("baseDir="); pw.println(baseDir);
205        if (!resDir.equals(baseDir)) pw.print(prefix); pw.print("resDir="); pw.println(resDir);
206        pw.print(prefix); pw.print("dataDir="); pw.println(dataDir);
207        pw.print(prefix); pw.print("app="); pw.println(app);
208        if (isForeground || foregroundId != 0) {
209            pw.print(prefix); pw.print("isForeground="); pw.print(isForeground);
210                    pw.print(" foregroundId="); pw.print(foregroundId);
211                    pw.print(" foregroundNoti="); pw.println(foregroundNoti);
212        }
213        pw.print(prefix); pw.print("createTime=");
214                TimeUtils.formatDuration(createTime, nowReal, pw);
215                pw.print(" lastActivity=");
216                TimeUtils.formatDuration(lastActivity, now, pw);
217                pw.println("");
218        pw.print(prefix); pw.print("executingStart=");
219                TimeUtils.formatDuration(executingStart, now, pw);
220                pw.print(" restartTime=");
221                TimeUtils.formatDuration(restartTime, now, pw);
222                pw.println("");
223        if (startRequested || lastStartId != 0) {
224            pw.print(prefix); pw.print("startRequested="); pw.print(startRequested);
225                    pw.print(" stopIfKilled="); pw.print(stopIfKilled);
226                    pw.print(" callStart="); pw.print(callStart);
227                    pw.print(" lastStartId="); pw.println(lastStartId);
228        }
229        if (executeNesting != 0 || crashCount != 0 || restartCount != 0
230                || restartDelay != 0 || nextRestartTime != 0) {
231            pw.print(prefix); pw.print("executeNesting="); pw.print(executeNesting);
232                    pw.print(" restartCount="); pw.print(restartCount);
233                    pw.print(" restartDelay=");
234                    TimeUtils.formatDuration(restartDelay, now, pw);
235                    pw.print(" nextRestartTime=");
236                    TimeUtils.formatDuration(nextRestartTime, now, pw);
237                    pw.print(" crashCount="); pw.println(crashCount);
238        }
239        if (deliveredStarts.size() > 0) {
240            pw.print(prefix); pw.println("Delivered Starts:");
241            dumpStartList(pw, prefix, deliveredStarts, now);
242        }
243        if (pendingStarts.size() > 0) {
244            pw.print(prefix); pw.println("Pending Starts:");
245            dumpStartList(pw, prefix, pendingStarts, 0);
246        }
247        if (bindings.size() > 0) {
248            Iterator<IntentBindRecord> it = bindings.values().iterator();
249            pw.print(prefix); pw.println("Bindings:");
250            while (it.hasNext()) {
251                IntentBindRecord b = it.next();
252                pw.print(prefix); pw.print("* IntentBindRecord{");
253                        pw.print(Integer.toHexString(System.identityHashCode(b)));
254                        pw.println("}:");
255                b.dumpInService(pw, prefix + "  ");
256            }
257        }
258        if (connections.size() > 0) {
259            pw.print(prefix); pw.println("All Connections:");
260            Iterator<ArrayList<ConnectionRecord>> it = connections.values().iterator();
261            while (it.hasNext()) {
262                ArrayList<ConnectionRecord> c = it.next();
263                for (int i=0; i<c.size(); i++) {
264                    pw.print(prefix); pw.print("  "); pw.println(c.get(i));
265                }
266            }
267        }
268    }
269
270    ServiceRecord(ActivityManagerService ams,
271            BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name,
272            Intent.FilterComparison intent, ServiceInfo sInfo, Runnable restarter) {
273        this.ams = ams;
274        this.stats = servStats;
275        this.name = name;
276        shortName = name.flattenToShortString();
277        this.intent = intent;
278        serviceInfo = sInfo;
279        appInfo = sInfo.applicationInfo;
280        packageName = sInfo.applicationInfo.packageName;
281        processName = sInfo.processName;
282        permission = sInfo.permission;
283        baseDir = sInfo.applicationInfo.sourceDir;
284        resDir = sInfo.applicationInfo.publicSourceDir;
285        dataDir = sInfo.applicationInfo.dataDir;
286        exported = sInfo.exported;
287        this.restarter = restarter;
288        createTime = SystemClock.elapsedRealtime();
289        lastActivity = SystemClock.uptimeMillis();
290    }
291
292    public AppBindRecord retrieveAppBindingLocked(Intent intent,
293            ProcessRecord app) {
294        Intent.FilterComparison filter = new Intent.FilterComparison(intent);
295        IntentBindRecord i = bindings.get(filter);
296        if (i == null) {
297            i = new IntentBindRecord(this, filter);
298            bindings.put(filter, i);
299        }
300        AppBindRecord a = i.apps.get(app);
301        if (a != null) {
302            return a;
303        }
304        a = new AppBindRecord(this, i, app);
305        i.apps.put(app, a);
306        return a;
307    }
308
309    public void resetRestartCounter() {
310        restartCount = 0;
311        restartDelay = 0;
312        restartTime = 0;
313    }
314
315    public StartItem findDeliveredStart(int id, boolean remove) {
316        final int N = deliveredStarts.size();
317        for (int i=0; i<N; i++) {
318            StartItem si = deliveredStarts.get(i);
319            if (si.id == id) {
320                if (remove) deliveredStarts.remove(i);
321                return si;
322            }
323        }
324
325        return null;
326    }
327
328    public int getLastStartId() {
329        return lastStartId;
330    }
331
332    public int makeNextStartId() {
333        lastStartId++;
334        if (lastStartId < 1) {
335            lastStartId = 1;
336        }
337        return lastStartId;
338    }
339
340    public void postNotification() {
341        final int appUid = appInfo.uid;
342        final int appPid = app.pid;
343        if (foregroundId != 0 && foregroundNoti != null) {
344            // Do asynchronous communication with notification manager to
345            // avoid deadlocks.
346            final String localPackageName = packageName;
347            final int localForegroundId = foregroundId;
348            final Notification localForegroundNoti = foregroundNoti;
349            ams.mHandler.post(new Runnable() {
350                public void run() {
351                    NotificationManagerService nm =
352                            (NotificationManagerService) NotificationManager.getService();
353                    if (nm == null) {
354                        return;
355                    }
356                    try {
357                        int[] outId = new int[1];
358                        nm.enqueueNotificationInternal(localPackageName, appUid, appPid,
359                                null, localForegroundId, localForegroundNoti, outId);
360                    } catch (RuntimeException e) {
361                        Slog.w(ActivityManagerService.TAG,
362                                "Error showing notification for service", e);
363                        // If it gave us a garbage notification, it doesn't
364                        // get to be foreground.
365                        ams.setServiceForeground(name, ServiceRecord.this,
366                                0, null, true);
367                        ams.crashApplication(appUid, appPid, localPackageName,
368                                "Bad notification for startForeground: " + e);
369                    }
370                }
371            });
372        }
373    }
374
375    public void cancelNotification() {
376        if (foregroundId != 0) {
377            // Do asynchronous communication with notification manager to
378            // avoid deadlocks.
379            final String localPackageName = packageName;
380            final int localForegroundId = foregroundId;
381            ams.mHandler.post(new Runnable() {
382                public void run() {
383                    INotificationManager inm = NotificationManager.getService();
384                    if (inm == null) {
385                        return;
386                    }
387                    try {
388                        inm.cancelNotification(localPackageName, localForegroundId);
389                    } catch (RuntimeException e) {
390                        Slog.w(ActivityManagerService.TAG,
391                                "Error canceling notification for service", e);
392                    } catch (RemoteException e) {
393                    }
394                }
395            });
396        }
397    }
398
399    public void clearDeliveredStartsLocked() {
400        for (int i=deliveredStarts.size()-1; i>=0; i--) {
401            deliveredStarts.get(i).removeUriPermissionsLocked();
402        }
403        deliveredStarts.clear();
404    }
405
406    public String toString() {
407        if (stringName != null) {
408            return stringName;
409        }
410        StringBuilder sb = new StringBuilder(128);
411        sb.append("ServiceRecord{")
412            .append(Integer.toHexString(System.identityHashCode(this)))
413            .append(' ').append(shortName).append('}');
414        return stringName = sb.toString();
415    }
416}
417