Home | History | Annotate | Download | only in am
      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 
     17 package com.android.server.am;
     18 
     19 import com.android.internal.app.ProcessStats;
     20 import com.android.internal.os.BatteryStatsImpl;
     21 import com.android.server.NotificationManagerService;
     22 
     23 import android.app.INotificationManager;
     24 import android.app.Notification;
     25 import android.app.NotificationManager;
     26 import android.app.PendingIntent;
     27 import android.content.ComponentName;
     28 import android.content.Context;
     29 import android.content.Intent;
     30 import android.content.pm.ApplicationInfo;
     31 import android.content.pm.PackageManager;
     32 import android.content.pm.ServiceInfo;
     33 import android.net.Uri;
     34 import android.os.Binder;
     35 import android.os.IBinder;
     36 import android.os.RemoteException;
     37 import android.os.SystemClock;
     38 import android.os.UserHandle;
     39 import android.provider.Settings;
     40 import android.util.ArrayMap;
     41 import android.util.Slog;
     42 import android.util.TimeUtils;
     43 
     44 import java.io.PrintWriter;
     45 import java.util.ArrayList;
     46 import java.util.List;
     47 
     48 /**
     49  * A running application service.
     50  */
     51 final class ServiceRecord extends Binder {
     52     // Maximum number of delivery attempts before giving up.
     53     static final int MAX_DELIVERY_COUNT = 3;
     54 
     55     // Maximum number of times it can fail during execution before giving up.
     56     static final int MAX_DONE_EXECUTING_COUNT = 6;
     57 
     58     final ActivityManagerService ams;
     59     final BatteryStatsImpl.Uid.Pkg.Serv stats;
     60     final ComponentName name; // service component.
     61     final String shortName; // name.flattenToShortString().
     62     final Intent.FilterComparison intent;
     63                             // original intent used to find service.
     64     final ServiceInfo serviceInfo;
     65                             // all information about the service.
     66     final ApplicationInfo appInfo;
     67                             // information about service's app.
     68     final int userId;       // user that this service is running as
     69     final String packageName; // the package implementing intent's component
     70     final String processName; // process where this component wants to run
     71     final String permission;// permission needed to access service
     72     final String baseDir;   // where activity source (resources etc) located
     73     final String resDir;   // where public activity source (public resources etc) located
     74     final String dataDir;   // where activity data should go
     75     final boolean exported; // from ServiceInfo.exported
     76     final Runnable restarter; // used to schedule retries of starting the service
     77     final long createTime;  // when this service was created
     78     final ArrayMap<Intent.FilterComparison, IntentBindRecord> bindings
     79             = new ArrayMap<Intent.FilterComparison, IntentBindRecord>();
     80                             // All active bindings to the service.
     81     final ArrayMap<IBinder, ArrayList<ConnectionRecord>> connections
     82             = new ArrayMap<IBinder, ArrayList<ConnectionRecord>>();
     83                             // IBinder -> ConnectionRecord of all bound clients
     84 
     85     ProcessRecord app;      // where this service is running or null.
     86     ProcessRecord isolatedProc; // keep track of isolated process, if requested
     87     ProcessStats.ServiceState tracker; // tracking service execution, may be null
     88     ProcessStats.ServiceState restartTracker; // tracking service restart
     89     boolean delayed;        // are we waiting to start this service in the background?
     90     boolean isForeground;   // is service currently in foreground mode?
     91     int foregroundId;       // Notification ID of last foreground req.
     92     Notification foregroundNoti; // Notification record of foreground state.
     93     long lastActivity;      // last time there was some activity on the service.
     94     long startingBgTimeout;  // time at which we scheduled this for a delayed start.
     95     boolean startRequested; // someone explicitly called start?
     96     boolean delayedStop;    // service has been stopped but is in a delayed start?
     97     boolean stopIfKilled;   // last onStart() said to stop if service killed?
     98     boolean callStart;      // last onStart() has asked to alway be called on restart.
     99     int executeNesting;     // number of outstanding operations keeping foreground.
    100     boolean executeFg;      // should we be executing in the foreground?
    101     long executingStart;    // start time of last execute request.
    102     boolean createdFromFg;  // was this service last created due to a foreground process call?
    103     int crashCount;         // number of times proc has crashed with service running
    104     int totalRestartCount;  // number of times we have had to restart.
    105     int restartCount;       // number of restarts performed in a row.
    106     long restartDelay;      // delay until next restart attempt.
    107     long restartTime;       // time of last restart.
    108     long nextRestartTime;   // time when restartDelay will expire.
    109 
    110     String stringName;      // caching of toString
    111 
    112     private int lastStartId;    // identifier of most recent start request.
    113 
    114     static class StartItem {
    115         final ServiceRecord sr;
    116         final boolean taskRemoved;
    117         final int id;
    118         final Intent intent;
    119         final ActivityManagerService.NeededUriGrants neededGrants;
    120         long deliveredTime;
    121         int deliveryCount;
    122         int doneExecutingCount;
    123         UriPermissionOwner uriPermissions;
    124 
    125         String stringName;      // caching of toString
    126 
    127         StartItem(ServiceRecord _sr, boolean _taskRemoved, int _id, Intent _intent,
    128                 ActivityManagerService.NeededUriGrants _neededGrants) {
    129             sr = _sr;
    130             taskRemoved = _taskRemoved;
    131             id = _id;
    132             intent = _intent;
    133             neededGrants = _neededGrants;
    134         }
    135 
    136         UriPermissionOwner getUriPermissionsLocked() {
    137             if (uriPermissions == null) {
    138                 uriPermissions = new UriPermissionOwner(sr.ams, this);
    139             }
    140             return uriPermissions;
    141         }
    142 
    143         void removeUriPermissionsLocked() {
    144             if (uriPermissions != null) {
    145                 uriPermissions.removeUriPermissionsLocked();
    146                 uriPermissions = null;
    147             }
    148         }
    149 
    150         public String toString() {
    151             if (stringName != null) {
    152                 return stringName;
    153             }
    154             StringBuilder sb = new StringBuilder(128);
    155             sb.append("ServiceRecord{")
    156                 .append(Integer.toHexString(System.identityHashCode(sr)))
    157                 .append(' ').append(sr.shortName)
    158                 .append(" StartItem ")
    159                 .append(Integer.toHexString(System.identityHashCode(this)))
    160                 .append(" id=").append(id).append('}');
    161             return stringName = sb.toString();
    162         }
    163     }
    164 
    165     final ArrayList<StartItem> deliveredStarts = new ArrayList<StartItem>();
    166                             // start() arguments which been delivered.
    167     final ArrayList<StartItem> pendingStarts = new ArrayList<StartItem>();
    168                             // start() arguments that haven't yet been delivered.
    169 
    170     void dumpStartList(PrintWriter pw, String prefix, List<StartItem> list, long now) {
    171         final int N = list.size();
    172         for (int i=0; i<N; i++) {
    173             StartItem si = list.get(i);
    174             pw.print(prefix); pw.print("#"); pw.print(i);
    175                     pw.print(" id="); pw.print(si.id);
    176                     if (now != 0) {
    177                         pw.print(" dur=");
    178                         TimeUtils.formatDuration(si.deliveredTime, now, pw);
    179                     }
    180                     if (si.deliveryCount != 0) {
    181                         pw.print(" dc="); pw.print(si.deliveryCount);
    182                     }
    183                     if (si.doneExecutingCount != 0) {
    184                         pw.print(" dxc="); pw.print(si.doneExecutingCount);
    185                     }
    186                     pw.println("");
    187             pw.print(prefix); pw.print("  intent=");
    188                     if (si.intent != null) pw.println(si.intent.toString());
    189                     else pw.println("null");
    190             if (si.neededGrants != null) {
    191                 pw.print(prefix); pw.print("  neededGrants=");
    192                         pw.println(si.neededGrants);
    193             }
    194             if (si.uriPermissions != null) {
    195                 if (si.uriPermissions.readUriPermissions != null) {
    196                     pw.print(prefix); pw.print("  readUriPermissions=");
    197                             pw.println(si.uriPermissions.readUriPermissions);
    198                 }
    199                 if (si.uriPermissions.writeUriPermissions != null) {
    200                     pw.print(prefix); pw.print("  writeUriPermissions=");
    201                             pw.println(si.uriPermissions.writeUriPermissions);
    202                 }
    203             }
    204         }
    205     }
    206 
    207     void dump(PrintWriter pw, String prefix) {
    208         pw.print(prefix); pw.print("intent={");
    209                 pw.print(intent.getIntent().toShortString(false, true, false, true));
    210                 pw.println('}');
    211         pw.print(prefix); pw.print("packageName="); pw.println(packageName);
    212         pw.print(prefix); pw.print("processName="); pw.println(processName);
    213         if (permission != null) {
    214             pw.print(prefix); pw.print("permission="); pw.println(permission);
    215         }
    216         long now = SystemClock.uptimeMillis();
    217         long nowReal = SystemClock.elapsedRealtime();
    218         pw.print(prefix); pw.print("baseDir="); pw.println(baseDir);
    219         if (!resDir.equals(baseDir)) {
    220             pw.print(prefix); pw.print("resDir="); pw.println(resDir);
    221         }
    222         pw.print(prefix); pw.print("dataDir="); pw.println(dataDir);
    223         pw.print(prefix); pw.print("app="); pw.println(app);
    224         if (isolatedProc != null) {
    225             pw.print(prefix); pw.print("isolatedProc="); pw.println(isolatedProc);
    226         }
    227         if (delayed) {
    228             pw.print(prefix); pw.print("delayed="); pw.println(delayed);
    229         }
    230         if (isForeground || foregroundId != 0) {
    231             pw.print(prefix); pw.print("isForeground="); pw.print(isForeground);
    232                     pw.print(" foregroundId="); pw.print(foregroundId);
    233                     pw.print(" foregroundNoti="); pw.println(foregroundNoti);
    234         }
    235         pw.print(prefix); pw.print("createTime=");
    236                 TimeUtils.formatDuration(createTime, nowReal, pw);
    237                 pw.print(" startingBgTimeout=");
    238                 TimeUtils.formatDuration(startingBgTimeout, now, pw);
    239                 pw.println();
    240         pw.print(prefix); pw.print("lastActivity=");
    241                 TimeUtils.formatDuration(lastActivity, now, pw);
    242                 pw.print(" restartTime=");
    243                 TimeUtils.formatDuration(restartTime, now, pw);
    244                 pw.print(" createdFromFg="); pw.println(createdFromFg);
    245         if (startRequested || delayedStop || lastStartId != 0) {
    246             pw.print(prefix); pw.print("startRequested="); pw.print(startRequested);
    247                     pw.print(" delayedStop="); pw.print(delayedStop);
    248                     pw.print(" stopIfKilled="); pw.print(stopIfKilled);
    249                     pw.print(" callStart="); pw.print(callStart);
    250                     pw.print(" lastStartId="); pw.println(lastStartId);
    251         }
    252         if (executeNesting != 0) {
    253             pw.print(prefix); pw.print("executeNesting="); pw.print(executeNesting);
    254                     pw.print(" executeFg="); pw.print(executeFg);
    255                     pw.print(" executingStart=");
    256                     TimeUtils.formatDuration(executingStart, now, pw);
    257                     pw.println();
    258         }
    259         if (crashCount != 0 || restartCount != 0
    260                 || restartDelay != 0 || nextRestartTime != 0) {
    261             pw.print(prefix); pw.print("restartCount="); pw.print(restartCount);
    262                     pw.print(" restartDelay=");
    263                     TimeUtils.formatDuration(restartDelay, now, pw);
    264                     pw.print(" nextRestartTime=");
    265                     TimeUtils.formatDuration(nextRestartTime, now, pw);
    266                     pw.print(" crashCount="); pw.println(crashCount);
    267         }
    268         if (deliveredStarts.size() > 0) {
    269             pw.print(prefix); pw.println("Delivered Starts:");
    270             dumpStartList(pw, prefix, deliveredStarts, now);
    271         }
    272         if (pendingStarts.size() > 0) {
    273             pw.print(prefix); pw.println("Pending Starts:");
    274             dumpStartList(pw, prefix, pendingStarts, 0);
    275         }
    276         if (bindings.size() > 0) {
    277             pw.print(prefix); pw.println("Bindings:");
    278             for (int i=0; i<bindings.size(); i++) {
    279                 IntentBindRecord b = bindings.valueAt(i);
    280                 pw.print(prefix); pw.print("* IntentBindRecord{");
    281                         pw.print(Integer.toHexString(System.identityHashCode(b)));
    282                         if ((b.collectFlags()&Context.BIND_AUTO_CREATE) != 0) {
    283                             pw.append(" CREATE");
    284                         }
    285                         pw.println("}:");
    286                 b.dumpInService(pw, prefix + "  ");
    287             }
    288         }
    289         if (connections.size() > 0) {
    290             pw.print(prefix); pw.println("All Connections:");
    291             for (int conni=0; conni<connections.size(); conni++) {
    292                 ArrayList<ConnectionRecord> c = connections.valueAt(conni);
    293                 for (int i=0; i<c.size(); i++) {
    294                     pw.print(prefix); pw.print("  "); pw.println(c.get(i));
    295                 }
    296             }
    297         }
    298     }
    299 
    300     ServiceRecord(ActivityManagerService ams,
    301             BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name,
    302             Intent.FilterComparison intent, ServiceInfo sInfo, boolean callerIsFg,
    303             Runnable restarter) {
    304         this.ams = ams;
    305         this.stats = servStats;
    306         this.name = name;
    307         shortName = name.flattenToShortString();
    308         this.intent = intent;
    309         serviceInfo = sInfo;
    310         appInfo = sInfo.applicationInfo;
    311         packageName = sInfo.applicationInfo.packageName;
    312         processName = sInfo.processName;
    313         permission = sInfo.permission;
    314         baseDir = sInfo.applicationInfo.sourceDir;
    315         resDir = sInfo.applicationInfo.publicSourceDir;
    316         dataDir = sInfo.applicationInfo.dataDir;
    317         exported = sInfo.exported;
    318         this.restarter = restarter;
    319         createTime = SystemClock.elapsedRealtime();
    320         lastActivity = SystemClock.uptimeMillis();
    321         userId = UserHandle.getUserId(appInfo.uid);
    322         createdFromFg = callerIsFg;
    323     }
    324 
    325     public ProcessStats.ServiceState getTracker() {
    326         if (tracker != null) {
    327             return tracker;
    328         }
    329         if ((serviceInfo.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) == 0) {
    330             tracker = ams.mProcessStats.getServiceStateLocked(serviceInfo.packageName,
    331                     serviceInfo.applicationInfo.uid, serviceInfo.processName, serviceInfo.name);
    332             tracker.applyNewOwner(this);
    333         }
    334         return tracker;
    335     }
    336 
    337     public void forceClearTracker() {
    338         if (tracker != null) {
    339             tracker.clearCurrentOwner(this, true);
    340             tracker = null;
    341         }
    342     }
    343 
    344     public void makeRestarting(int memFactor, long now) {
    345         if (restartTracker == null) {
    346             if ((serviceInfo.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) == 0) {
    347                 restartTracker = ams.mProcessStats.getServiceStateLocked(serviceInfo.packageName,
    348                         serviceInfo.applicationInfo.uid, serviceInfo.processName, serviceInfo.name);
    349             }
    350             if (restartTracker == null) {
    351                 return;
    352             }
    353         }
    354         restartTracker.setRestarting(true, memFactor, now);
    355     }
    356 
    357     public AppBindRecord retrieveAppBindingLocked(Intent intent,
    358             ProcessRecord app) {
    359         Intent.FilterComparison filter = new Intent.FilterComparison(intent);
    360         IntentBindRecord i = bindings.get(filter);
    361         if (i == null) {
    362             i = new IntentBindRecord(this, filter);
    363             bindings.put(filter, i);
    364         }
    365         AppBindRecord a = i.apps.get(app);
    366         if (a != null) {
    367             return a;
    368         }
    369         a = new AppBindRecord(this, i, app);
    370         i.apps.put(app, a);
    371         return a;
    372     }
    373 
    374     public boolean hasAutoCreateConnections() {
    375         // XXX should probably keep a count of the number of auto-create
    376         // connections directly in the service.
    377         for (int conni=connections.size()-1; conni>=0; conni--) {
    378             ArrayList<ConnectionRecord> cr = connections.valueAt(conni);
    379             for (int i=0; i<cr.size(); i++) {
    380                 if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) {
    381                     return true;
    382                 }
    383             }
    384         }
    385         return false;
    386     }
    387 
    388     public void resetRestartCounter() {
    389         restartCount = 0;
    390         restartDelay = 0;
    391         restartTime = 0;
    392     }
    393 
    394     public StartItem findDeliveredStart(int id, boolean remove) {
    395         final int N = deliveredStarts.size();
    396         for (int i=0; i<N; i++) {
    397             StartItem si = deliveredStarts.get(i);
    398             if (si.id == id) {
    399                 if (remove) deliveredStarts.remove(i);
    400                 return si;
    401             }
    402         }
    403 
    404         return null;
    405     }
    406 
    407     public int getLastStartId() {
    408         return lastStartId;
    409     }
    410 
    411     public int makeNextStartId() {
    412         lastStartId++;
    413         if (lastStartId < 1) {
    414             lastStartId = 1;
    415         }
    416         return lastStartId;
    417     }
    418 
    419     public void postNotification() {
    420         final int appUid = appInfo.uid;
    421         final int appPid = app.pid;
    422         if (foregroundId != 0 && foregroundNoti != null) {
    423             // Do asynchronous communication with notification manager to
    424             // avoid deadlocks.
    425             final String localPackageName = packageName;
    426             final int localForegroundId = foregroundId;
    427             final Notification localForegroundNoti = foregroundNoti;
    428             ams.mHandler.post(new Runnable() {
    429                 public void run() {
    430                     NotificationManagerService nm =
    431                             (NotificationManagerService) NotificationManager.getService();
    432                     if (nm == null) {
    433                         return;
    434                     }
    435                     try {
    436                         if (localForegroundNoti.icon == 0) {
    437                             // It is not correct for the caller to supply a notification
    438                             // icon, but this used to be able to slip through, so for
    439                             // those dirty apps give it the app's icon.
    440                             localForegroundNoti.icon = appInfo.icon;
    441 
    442                             // Do not allow apps to present a sneaky invisible content view either.
    443                             localForegroundNoti.contentView = null;
    444                             localForegroundNoti.bigContentView = null;
    445                             CharSequence appName = appInfo.loadLabel(
    446                                     ams.mContext.getPackageManager());
    447                             if (appName == null) {
    448                                 appName = appInfo.packageName;
    449                             }
    450                             Context ctx = null;
    451                             try {
    452                                 ctx = ams.mContext.createPackageContext(
    453                                         appInfo.packageName, 0);
    454                                 Intent runningIntent = new Intent(
    455                                         Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    456                                 runningIntent.setData(Uri.fromParts("package",
    457                                         appInfo.packageName, null));
    458                                 PendingIntent pi = PendingIntent.getActivity(ams.mContext, 0,
    459                                         runningIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    460                                 localForegroundNoti.setLatestEventInfo(ctx,
    461                                         ams.mContext.getString(
    462                                                 com.android.internal.R.string
    463                                                         .app_running_notification_title,
    464                                                 appName),
    465                                         ams.mContext.getString(
    466                                                 com.android.internal.R.string
    467                                                         .app_running_notification_text,
    468                                                 appName),
    469                                         pi);
    470                             } catch (PackageManager.NameNotFoundException e) {
    471                                 localForegroundNoti.icon = 0;
    472                             }
    473                         }
    474                         if (localForegroundNoti.icon == 0) {
    475                             // Notifications whose icon is 0 are defined to not show
    476                             // a notification, silently ignoring it.  We don't want to
    477                             // just ignore it, we want to prevent the service from
    478                             // being foreground.
    479                             throw new RuntimeException("icon must be non-zero");
    480                         }
    481                         int[] outId = new int[1];
    482                         nm.enqueueNotificationInternal(localPackageName, localPackageName,
    483                                 appUid, appPid, null, localForegroundId, localForegroundNoti,
    484                                 outId, userId);
    485                     } catch (RuntimeException e) {
    486                         Slog.w(ActivityManagerService.TAG,
    487                                 "Error showing notification for service", e);
    488                         // If it gave us a garbage notification, it doesn't
    489                         // get to be foreground.
    490                         ams.setServiceForeground(name, ServiceRecord.this,
    491                                 0, null, true);
    492                         ams.crashApplication(appUid, appPid, localPackageName,
    493                                 "Bad notification for startForeground: " + e);
    494                     }
    495                 }
    496             });
    497         }
    498     }
    499 
    500     public void cancelNotification() {
    501         if (foregroundId != 0) {
    502             // Do asynchronous communication with notification manager to
    503             // avoid deadlocks.
    504             final String localPackageName = packageName;
    505             final int localForegroundId = foregroundId;
    506             ams.mHandler.post(new Runnable() {
    507                 public void run() {
    508                     INotificationManager inm = NotificationManager.getService();
    509                     if (inm == null) {
    510                         return;
    511                     }
    512                     try {
    513                         inm.cancelNotificationWithTag(localPackageName, null,
    514                                 localForegroundId, userId);
    515                     } catch (RuntimeException e) {
    516                         Slog.w(ActivityManagerService.TAG,
    517                                 "Error canceling notification for service", e);
    518                     } catch (RemoteException e) {
    519                     }
    520                 }
    521             });
    522         }
    523     }
    524 
    525     public void clearDeliveredStartsLocked() {
    526         for (int i=deliveredStarts.size()-1; i>=0; i--) {
    527             deliveredStarts.get(i).removeUriPermissionsLocked();
    528         }
    529         deliveredStarts.clear();
    530     }
    531 
    532     public String toString() {
    533         if (stringName != null) {
    534             return stringName;
    535         }
    536         StringBuilder sb = new StringBuilder(128);
    537         sb.append("ServiceRecord{")
    538             .append(Integer.toHexString(System.identityHashCode(this)))
    539             .append(" u").append(userId)
    540             .append(' ').append(shortName).append('}');
    541         return stringName = sb.toString();
    542     }
    543 }
    544