diff options
| -rw-r--r-- | api/current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/app/IActivityManager.aidl | 1 | ||||
| -rw-r--r-- | core/java/android/app/Service.java | 27 | ||||
| -rw-r--r-- | services/core/java/com/android/server/am/ActiveServices.java | 30 | ||||
| -rw-r--r-- | services/core/java/com/android/server/am/ActivityManagerService.java | 7 |
5 files changed, 61 insertions, 5 deletions
diff --git a/api/current.txt b/api/current.txt index ee89c591fd40..22802e33fecb 100644 --- a/api/current.txt +++ b/api/current.txt @@ -6169,6 +6169,7 @@ package android.app { ctor public Service(); method protected void dump(java.io.FileDescriptor, java.io.PrintWriter, String[]); method public final android.app.Application getApplication(); + method public final int getForegroundServiceType(); method @Nullable public abstract android.os.IBinder onBind(android.content.Intent); method public void onConfigurationChanged(android.content.res.Configuration); method public void onCreate(); diff --git a/core/java/android/app/IActivityManager.aidl b/core/java/android/app/IActivityManager.aidl index 3aa9fa7fedee..720909e5fcbf 100644 --- a/core/java/android/app/IActivityManager.aidl +++ b/core/java/android/app/IActivityManager.aidl @@ -204,6 +204,7 @@ interface IActivityManager { void setProcessImportant(in IBinder token, int pid, boolean isForeground, String reason); void setServiceForeground(in ComponentName className, in IBinder token, int id, in Notification notification, int flags, int foregroundServiceType); + int getForegroundServiceType(in ComponentName className, in IBinder token); boolean moveActivityTaskToBack(in IBinder token, boolean nonRoot); void getMemoryInfo(out ActivityManager.MemoryInfo outInfo); List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState(); diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java index f116e133e338..1f91b3f431a1 100644 --- a/core/java/android/app/Service.java +++ b/core/java/android/app/Service.java @@ -27,6 +27,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; +import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo.ForegroundServiceType; import android.content.res.Configuration; import android.os.Build; @@ -733,7 +734,7 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac * {@link android.R.attr#foregroundServiceType} flags. * @throws IllegalArgumentException if param foregroundServiceType is not subset of manifest * attribute {@link android.R.attr#foregroundServiceType}. - * @see {@link android.content.pm.ServiceInfo} for the set of FOREGROUND_SERVICE_TYPE flags. + * @see android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_MANIFEST */ public final void startForeground(int id, @NonNull Notification notification, @ForegroundServiceType int foregroundServiceType) { @@ -775,6 +776,30 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac } /** + * If the service has become a foreground service by calling + * {@link #startForeground(int, Notification)} + * or {@link #startForeground(int, Notification, int)}, {@link #getForegroundServiceType()} + * returns the current foreground service type. + * + * <p>If there is no foregroundServiceType specified + * in manifest, {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE} is returned. </p> + * + * <p>If the service is not a foreground service, + * {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE} is returned.</p> + * + * @return current foreground service type flags. + */ + public final @ForegroundServiceType int getForegroundServiceType() { + int ret = ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE; + try { + ret = mActivityManager.getForegroundServiceType( + new ComponentName(this, mClassName), mToken); + } catch (RemoteException ex) { + } + return ret; + } + + /** * Print the Service's state into the given stream. This gets invoked if * you run "adb shell dumpsys activity service <yourservicename>" * (note that for this command to work, the service must be running, and diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index aebe2b78a853..346492fccc21 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -933,6 +933,27 @@ public final class ActiveServices { } } + /** + * Return the current foregroundServiceType of the ServiceRecord. + * @param className ComponentName of the Service class. + * @param token IBinder token. + * @return current foreground service type. + */ + public int getForegroundServiceTypeLocked(ComponentName className, IBinder token) { + final int userId = UserHandle.getCallingUserId(); + final long origId = Binder.clearCallingIdentity(); + int ret = ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE; + try { + ServiceRecord r = findServiceLocked(className, token, userId); + if (r != null) { + ret = r.foregroundServiceType; + } + } finally { + Binder.restoreCallingIdentity(origId); + } + return ret; + } + boolean foregroundAppShownEnoughLocked(ActiveForegroundApp aa, long nowElapsed) { if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Shown enough: pkg=" + aa.mPackageName + ", uid=" + aa.mUid); @@ -1260,10 +1281,11 @@ public final class ActiveServices { // Check the passed in foreground service type flags is a subset of manifest // foreground service type flags. if ((foregroundServiceType & manifestType) != foregroundServiceType) { - // STOPSHIP(b/120611119): replace log message with IllegalArgumentException. - Slog.w(TAG, "foregroundServiceType must be a subset of " - + "foregroundServiceType attribute in " - + "service element of manifest file"); + throw new IllegalArgumentException("foregroundServiceType " + + String.format("0x%08X", foregroundServiceType) + + " is not a subset of foregroundServiceType attribute " + + String.format("0x%08X", manifestType) + + " in service element of manifest file"); } } boolean alreadyStartedOp = false; diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 3c0430fe0adf..f60207b7808b 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -13572,6 +13572,13 @@ public class ActivityManagerService extends IActivityManager.Stub } @Override + public int getForegroundServiceType(ComponentName className, IBinder token) { + synchronized (this) { + return mServices.getForegroundServiceTypeLocked(className, token); + } + } + + @Override public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, boolean requireFull, String name, String callerPackage) { return mUserController.handleIncomingUser(callingPid, callingUid, userId, allowAll, |