Rename android.app.ForegroundServiceTypeNotAllowedException to
..android.app.InvalidForegroundServiceTypeException. And also add MissingForegroundServiceTypeException. Bug: 254662240 Bug: 246792057 Test: atest CtsAppFgsTestCases Change-Id: I9d39cb7fa75a14eb1b8cc97af13b612491803b61
This commit is contained in:
@@ -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();
|
||||
|
||||
31
core/java/android/app/ForegroundServiceTypeException.java
Normal file
31
core/java/android/app/ForegroundServiceTypeException.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -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];
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 "
|
||||
|
||||
Reference in New Issue
Block a user