diff --git a/core/api/current.txt b/core/api/current.txt index 3af4394bab9da..0331cb869db5e 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 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 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 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 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 0000000000000..9a9180efdaf68 --- /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 e419e0602f2a0..818bdc26523cd 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; @@ -152,6 +153,20 @@ public abstract class ForegroundServiceTypePolicy { @Overridable 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}. * @@ -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 similarity index 58% rename from core/java/android/app/ForegroundServiceTypeNotAllowedException.java rename to core/java/android/app/InvalidForegroundServiceTypeException.java index c258242627c9a..6ff02626d052e 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 - CREATOR = new Creator() { + public static final @NonNull Creator + CREATOR = new Creator() { @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 0000000000000..c9b200699ac07 --- /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 + CREATOR = new Creator() { + @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 291edbb74c35b..6d7a161d16874 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} is not set, or the param - * {@code foregroundServiceType} is {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE}. + * {@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 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 14f03eae8bf15..3b7ed07c41bc8 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 *

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}.

+ * this type will get a {@link android.app.InvalidForegroundServiceTypeException}.

* * @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}.

+ * {@link android.app.InvalidForegroundServiceTypeException}.

* * @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 a5c0827230463..6460007b52de4 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}. -->