summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jing Ji <jji@google.com> 2022-11-20 01:04:11 -0800
committer Jing Ji <jji@google.com> 2022-11-23 21:31:12 -0800
commit3d3659bc1d59034448718a00f969dee86bd1cf39 (patch)
tree9e240aa140aa3bd6b2c98bcf3bd9fcbf39d6352d
parent5bb5cd952abf8e8501b92223a9d6fdfb6a1624aa (diff)
Rename android.app.ForegroundServiceTypeNotAllowedException to
..android.app.InvalidForegroundServiceTypeException. And also add MissingForegroundServiceTypeException. Bug: 254662240 Bug: 246792057 Test: atest CtsAppFgsTestCases Change-Id: I9d39cb7fa75a14eb1b8cc97af13b612491803b61
-rw-r--r--core/api/current.txt21
-rw-r--r--core/java/android/app/ForegroundServiceTypeException.java31
-rw-r--r--core/java/android/app/ForegroundServiceTypePolicy.java17
-rw-r--r--core/java/android/app/InvalidForegroundServiceTypeException.java (renamed from core/java/android/app/ForegroundServiceTypeNotAllowedException.java)22
-rw-r--r--core/java/android/app/MissingForegroundServiceTypeException.java61
-rw-r--r--core/java/android/app/Service.java20
-rw-r--r--core/java/android/content/pm/ServiceInfo.java4
-rw-r--r--core/res/res/values/attrs_manifest.xml2
-rw-r--r--services/core/java/com/android/server/am/ActiveServices.java33
9 files changed, 177 insertions, 34 deletions
diff --git a/core/api/current.txt b/core/api/current.txt
index 3af4394bab9d..0331cb869db5 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -5291,11 +5291,8 @@ package android.app {
field @NonNull public static final android.os.Parcelable.Creator<android.app.ForegroundServiceStartNotAllowedException> CREATOR;
}
- public final class ForegroundServiceTypeNotAllowedException extends android.app.ServiceStartNotAllowedException implements android.os.Parcelable {
- ctor public ForegroundServiceTypeNotAllowedException(@NonNull String);
- method public int describeContents();
- method public void writeToParcel(@NonNull android.os.Parcel, int);
- field @NonNull public static final android.os.Parcelable.Creator<android.app.ForegroundServiceTypeNotAllowedException> CREATOR;
+ public abstract class ForegroundServiceTypeException extends android.app.ServiceStartNotAllowedException {
+ ctor public ForegroundServiceTypeException(@NonNull String);
}
@Deprecated public class Fragment implements android.content.ComponentCallbacks2 android.view.View.OnCreateContextMenuListener {
@@ -5738,6 +5735,13 @@ package android.app {
method @Deprecated public void setIntentRedelivery(boolean);
}
+ public final class InvalidForegroundServiceTypeException extends android.app.ForegroundServiceTypeException implements android.os.Parcelable {
+ ctor public InvalidForegroundServiceTypeException(@NonNull String);
+ method public int describeContents();
+ method public void writeToParcel(@NonNull android.os.Parcel, int);
+ field @NonNull public static final android.os.Parcelable.Creator<android.app.InvalidForegroundServiceTypeException> CREATOR;
+ }
+
public class KeyguardManager {
method @RequiresPermission(android.Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE) public void addKeyguardLockedStateListener(@NonNull java.util.concurrent.Executor, @NonNull android.app.KeyguardManager.KeyguardLockedStateListener);
method @Deprecated public android.content.Intent createConfirmDeviceCredentialIntent(CharSequence, CharSequence);
@@ -5892,6 +5896,13 @@ package android.app {
method public void showDialog();
}
+ public final class MissingForegroundServiceTypeException extends android.app.ForegroundServiceTypeException implements android.os.Parcelable {
+ ctor public MissingForegroundServiceTypeException(@NonNull String);
+ method public int describeContents();
+ method public void writeToParcel(@NonNull android.os.Parcel, int);
+ field @NonNull public static final android.os.Parcelable.Creator<android.app.MissingForegroundServiceTypeException> CREATOR;
+ }
+
public class NativeActivity extends android.app.Activity implements android.view.InputQueue.Callback android.view.SurfaceHolder.Callback2 android.view.ViewTreeObserver.OnGlobalLayoutListener {
ctor public NativeActivity();
method public void onGlobalLayout();
diff --git a/core/java/android/app/ForegroundServiceTypeException.java b/core/java/android/app/ForegroundServiceTypeException.java
new file mode 100644
index 000000000000..9a9180efdaf6
--- /dev/null
+++ b/core/java/android/app/ForegroundServiceTypeException.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.app;
+
+import android.annotation.NonNull;
+
+/**
+ * Base exception thrown when an app tries to start a foreground {@link Service}
+ * without a valid type.
+ */
+public abstract class ForegroundServiceTypeException extends ServiceStartNotAllowedException {
+ /**
+ * Constructor.
+ */
+ public ForegroundServiceTypeException(@NonNull String message) {
+ super(message);
+ }
+}
diff --git a/core/java/android/app/ForegroundServiceTypePolicy.java b/core/java/android/app/ForegroundServiceTypePolicy.java
index e419e0602f2a..818bdc26523c 100644
--- a/core/java/android/app/ForegroundServiceTypePolicy.java
+++ b/core/java/android/app/ForegroundServiceTypePolicy.java
@@ -25,6 +25,7 @@ import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_D
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_HEALTH;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION;
+import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE;
@@ -153,6 +154,20 @@ public abstract class ForegroundServiceTypePolicy {
public static final long FGS_TYPE_PERMISSION_CHANGE_ID = 254662522L;
/**
+ * The policy for the {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_MANIFEST}.
+ *
+ * @hide
+ */
+ public static final @NonNull ForegroundServiceTypePolicyInfo FGS_TYPE_POLICY_MANIFEST =
+ new ForegroundServiceTypePolicyInfo(
+ FOREGROUND_SERVICE_TYPE_MANIFEST,
+ FGS_TYPE_NONE_DEPRECATION_CHANGE_ID,
+ FGS_TYPE_NONE_DISABLED_CHANGE_ID,
+ null,
+ null
+ );
+
+ /**
* The policy for the {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE}.
*
* @hide
@@ -954,6 +969,8 @@ public abstract class ForegroundServiceTypePolicy {
* Constructor
*/
public DefaultForegroundServiceTypePolicy() {
+ mForegroundServiceTypePolicies.put(FOREGROUND_SERVICE_TYPE_MANIFEST,
+ FGS_TYPE_POLICY_MANIFEST);
mForegroundServiceTypePolicies.put(FOREGROUND_SERVICE_TYPE_NONE,
FGS_TYPE_POLICY_NONE);
mForegroundServiceTypePolicies.put(FOREGROUND_SERVICE_TYPE_DATA_SYNC,
diff --git a/core/java/android/app/ForegroundServiceTypeNotAllowedException.java b/core/java/android/app/InvalidForegroundServiceTypeException.java
index c258242627c9..6ff02626d052 100644
--- a/core/java/android/app/ForegroundServiceTypeNotAllowedException.java
+++ b/core/java/android/app/InvalidForegroundServiceTypeException.java
@@ -20,18 +20,18 @@ import android.os.Parcel;
import android.os.Parcelable;
/**
- * Exception thrown when an app tries to start a foreground {@link Service} without a valid type.
+ * Exception thrown when an app tries to start a foreground {@link Service} with an invalid type.
*/
-public final class ForegroundServiceTypeNotAllowedException
- extends ServiceStartNotAllowedException implements Parcelable {
+public final class InvalidForegroundServiceTypeException
+ extends ForegroundServiceTypeException implements Parcelable {
/**
* Constructor.
*/
- public ForegroundServiceTypeNotAllowedException(@NonNull String message) {
+ public InvalidForegroundServiceTypeException(@NonNull String message) {
super(message);
}
- ForegroundServiceTypeNotAllowedException(@NonNull Parcel source) {
+ InvalidForegroundServiceTypeException(@NonNull Parcel source) {
super(source.readString());
}
@@ -45,17 +45,17 @@ public final class ForegroundServiceTypeNotAllowedException
dest.writeString(getMessage());
}
- public static final @NonNull Creator<android.app.ForegroundServiceTypeNotAllowedException>
- CREATOR = new Creator<android.app.ForegroundServiceTypeNotAllowedException>() {
+ public static final @NonNull Creator<android.app.InvalidForegroundServiceTypeException>
+ CREATOR = new Creator<android.app.InvalidForegroundServiceTypeException>() {
@NonNull
- public android.app.ForegroundServiceTypeNotAllowedException createFromParcel(
+ public android.app.InvalidForegroundServiceTypeException createFromParcel(
Parcel source) {
- return new android.app.ForegroundServiceTypeNotAllowedException(source);
+ return new android.app.InvalidForegroundServiceTypeException(source);
}
@NonNull
- public android.app.ForegroundServiceTypeNotAllowedException[] newArray(int size) {
- return new android.app.ForegroundServiceTypeNotAllowedException[size];
+ public android.app.InvalidForegroundServiceTypeException[] newArray(int size) {
+ return new android.app.InvalidForegroundServiceTypeException[size];
}
};
}
diff --git a/core/java/android/app/MissingForegroundServiceTypeException.java b/core/java/android/app/MissingForegroundServiceTypeException.java
new file mode 100644
index 000000000000..c9b200699ac0
--- /dev/null
+++ b/core/java/android/app/MissingForegroundServiceTypeException.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.app;
+
+import android.annotation.NonNull;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+/**
+ * Exception thrown when an app tries to start a foreground {@link Service} without a type.
+ */
+public final class MissingForegroundServiceTypeException
+ extends ForegroundServiceTypeException implements Parcelable {
+ /**
+ * Constructor.
+ */
+ public MissingForegroundServiceTypeException(@NonNull String message) {
+ super(message);
+ }
+
+ MissingForegroundServiceTypeException(@NonNull Parcel source) {
+ super(source.readString());
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Override
+ public void writeToParcel(@NonNull Parcel dest, int flags) {
+ dest.writeString(getMessage());
+ }
+
+ public static final @NonNull Creator<android.app.MissingForegroundServiceTypeException>
+ CREATOR = new Creator<android.app.MissingForegroundServiceTypeException>() {
+ @NonNull
+ public android.app.MissingForegroundServiceTypeException createFromParcel(
+ Parcel source) {
+ return new android.app.MissingForegroundServiceTypeException(source);
+ }
+
+ @NonNull
+ public android.app.MissingForegroundServiceTypeException[] newArray(int size) {
+ return new android.app.MissingForegroundServiceTypeException[size];
+ }
+ };
+}
diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java
index 291edbb74c35..6d7a161d1687 100644
--- a/core/java/android/app/Service.java
+++ b/core/java/android/app/Service.java
@@ -748,7 +748,12 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
* If the app targeting API is
* {@link android.os.Build.VERSION_CODES#S} or later, and the service is restricted from
* becoming foreground service due to background restriction.
- * @throws ForegroundServiceTypeNotAllowedException
+ * @throws InvalidForegroundServiceTypeException
+ * If the app targeting API is
+ * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} or later, and the manifest attribute
+ * {@link android.R.attr#foregroundServiceType} is set to invalid types(i.e.
+ * {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE}).
+ * @throws MissingForegroundServiceTypeException
* If the app targeting API is
* {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} or later, and the manifest attribute
* {@link android.R.attr#foregroundServiceType} is not set.
@@ -761,7 +766,7 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
* {@link NotificationManager#notify(int, Notification)
* NotificationManager.notify(int, Notification)}; must not be 0.
* @param notification The Notification to be displayed.
- *
+ *
* @see #stopForeground(boolean)
*/
public final void startForeground(int id, Notification notification) {
@@ -832,11 +837,16 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
* If the app targeting API is
* {@link android.os.Build.VERSION_CODES#S} or later, and the service is restricted from
* becoming foreground service due to background restriction.
- * @throws ForegroundServiceTypeNotAllowedException
+ * @throws InvalidForegroundServiceTypeException
+ * If the app targeting API is
+ * {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} or later, and the manifest attribute
+ * {@link android.R.attr#foregroundServiceType} or the param {@code foregroundServiceType}
+ * is set to invalid types(i.e.{@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE}).
+ * @throws MissingForegroundServiceTypeException
* If the app targeting API is
* {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} or later, and the manifest attribute
- * {@link android.R.attr#foregroundServiceType} is not set, or the param
- * {@code foregroundServiceType} is {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE}.
+ * {@link android.R.attr#foregroundServiceType} is not set and the param
+ * {@code foregroundServiceType} is set to {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_MANIFEST}.
* @throws SecurityException If the app targeting API is
* {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} or later and doesn't have the
* permission to start the foreground service with the specified type in
diff --git a/core/java/android/content/pm/ServiceInfo.java b/core/java/android/content/pm/ServiceInfo.java
index 14f03eae8bf1..3b7ed07c41bc 100644
--- a/core/java/android/content/pm/ServiceInfo.java
+++ b/core/java/android/content/pm/ServiceInfo.java
@@ -106,7 +106,7 @@ public class ServiceInfo extends ComponentInfo
* <p>Apps targeting API level {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} and
* later should NOT use this type,
* calling {@link android.app.Service#startForeground(int, android.app.Notification, int)} with
- * this type will get a {@link android.app.ForegroundServiceTypeNotAllowedException}.</p>
+ * this type will get a {@link android.app.InvalidForegroundServiceTypeException}.</p>
*
* @deprecated Do not use.
*/
@@ -124,7 +124,7 @@ public class ServiceInfo extends ComponentInfo
* calling {@link android.app.Service#startForeground(int, android.app.Notification, int)} with
* this type on devices running {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE} is still
* allowed, but calling it with this type on devices running future platform releases may get a
- * {@link android.app.ForegroundServiceTypeNotAllowedException}.</p>
+ * {@link android.app.InvalidForegroundServiceTypeException}.</p>
*
* @deprecated Use {@link android.app.job.JobInfo.Builder} data transfer APIs instead.
*/
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index a5c082723046..6460007b52de 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -1594,7 +1594,7 @@
{@link android.app.Service#startForeground(int, android.app.Notification, int)} with
this type on devices running {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE}
is still allowed, but calling it with this type on devices running future platform
- releases may get a {@link android.app.ForegroundServiceTypeNotAllowedException}.
+ releases may get a {@link android.app.InvalidForegroundServiceTypeException}.
-->
<flag name="dataSync" value="0x01" />
<!-- Music, video, news or other media play.
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 1534e635f1b2..dd3020a9a2ad 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -32,6 +32,7 @@ import static android.app.ForegroundServiceTypePolicy.FGS_TYPE_POLICY_CHECK_UNKN
import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST;
+import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE;
import static android.os.PowerExemptionManager.REASON_ACTIVE_DEVICE_ADMIN;
import static android.os.PowerExemptionManager.REASON_ACTIVITY_STARTER;
import static android.os.PowerExemptionManager.REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD;
@@ -110,7 +111,6 @@ import android.app.ActivityThread;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.ForegroundServiceStartNotAllowedException;
-import android.app.ForegroundServiceTypeNotAllowedException;
import android.app.ForegroundServiceTypePolicy;
import android.app.ForegroundServiceTypePolicy.ForegroundServicePolicyCheckCode;
import android.app.ForegroundServiceTypePolicy.ForegroundServiceTypePermission;
@@ -118,6 +118,8 @@ import android.app.ForegroundServiceTypePolicy.ForegroundServiceTypePolicyInfo;
import android.app.IApplicationThread;
import android.app.IForegroundServiceObserver;
import android.app.IServiceConnection;
+import android.app.InvalidForegroundServiceTypeException;
+import android.app.MissingForegroundServiceTypeException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
@@ -1781,6 +1783,7 @@ public final class ActiveServices {
if (notification == null) {
throw new IllegalArgumentException("null notification");
}
+ final int foregroundServiceStartType = foregroundServiceType;
// Instant apps need permission to create foreground services.
if (r.appInfo.isInstantApp()) {
final int mode = mAm.getAppOpsManager().checkOpNoThrow(
@@ -1985,7 +1988,8 @@ public final class ActiveServices {
if (foregroundServiceType == ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE) {
fgsTypeResult = validateForegroundServiceType(r,
foregroundServiceType,
- ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE);
+ ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE,
+ foregroundServiceStartType);
} else {
int fgsTypes = foregroundServiceType;
// If the service has declared some unknown types which might be coming
@@ -2000,7 +2004,7 @@ public final class ActiveServices {
serviceType != 0;
serviceType = Integer.highestOneBit(fgsTypes)) {
fgsTypeResult = validateForegroundServiceType(r,
- serviceType, defaultFgsTypes);
+ serviceType, defaultFgsTypes, foregroundServiceStartType);
fgsTypes &= ~serviceType;
if (fgsTypeResult.first != FGS_TYPE_POLICY_CHECK_OK) {
break;
@@ -2227,7 +2231,8 @@ public final class ActiveServices {
@NonNull
private Pair<Integer, RuntimeException> validateForegroundServiceType(ServiceRecord r,
@ForegroundServiceType int type,
- @ForegroundServiceType int defaultToType) {
+ @ForegroundServiceType int defaultToType,
+ @ForegroundServiceType int startType) {
final ForegroundServiceTypePolicy policy = ForegroundServiceTypePolicy.getDefaultPolicy();
final ForegroundServiceTypePolicyInfo policyInfo =
policy.getForegroundServiceTypePolicyInfo(type, defaultToType);
@@ -2246,12 +2251,20 @@ public final class ActiveServices {
Slog.w(TAG, msg);
} break;
case FGS_TYPE_POLICY_CHECK_DISABLED: {
- exception = new ForegroundServiceTypeNotAllowedException(
- "Starting FGS with type "
- + ServiceInfo.foregroundServiceTypeToLabel(type)
- + " callerApp=" + r.app
- + " targetSDK=" + r.app.info.targetSdkVersion
- + " has been prohibited");
+ if (startType == FOREGROUND_SERVICE_TYPE_MANIFEST
+ && type == FOREGROUND_SERVICE_TYPE_NONE) {
+ exception = new MissingForegroundServiceTypeException(
+ "Starting FGS without a type "
+ + " callerApp=" + r.app
+ + " targetSDK=" + r.app.info.targetSdkVersion);
+ } else {
+ exception = new InvalidForegroundServiceTypeException(
+ "Starting FGS with type "
+ + ServiceInfo.foregroundServiceTypeToLabel(type)
+ + " callerApp=" + r.app
+ + " targetSDK=" + r.app.info.targetSdkVersion
+ + " has been prohibited");
+ }
} break;
case FGS_TYPE_POLICY_CHECK_PERMISSION_DENIED_PERMISSIVE: {
final String msg = "Starting FGS with type "