Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0549b1f7 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Throw more descriptive exceptions when service start isn't allowed.

Fix: 180518252
Test: atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
Change-Id: Ia38a159f9ededdc9885ce6b8dc61640c3af5ef72
parent a7f6e5ea
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -4704,6 +4704,13 @@ package android.app {
    field @NonNull public static final android.os.Parcelable.Creator<android.app.AutomaticZenRule> CREATOR;
  }
  public final class BackgroundServiceStartNotAllowedException extends android.app.ServiceStartNotAllowedException implements android.os.Parcelable {
    ctor public BackgroundServiceStartNotAllowedException(@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.BackgroundServiceStartNotAllowedException> CREATOR;
  }
  public class DatePickerDialog extends android.app.AlertDialog implements android.widget.DatePicker.OnDateChangedListener android.content.DialogInterface.OnClickListener {
    ctor public DatePickerDialog(@NonNull android.content.Context);
    ctor public DatePickerDialog(@NonNull android.content.Context, @StyleRes int);
@@ -4949,6 +4956,13 @@ package android.app {
    method @Deprecated public void setSelectedGroup(int);
  }
  public final class ForegroundServiceStartNotAllowedException extends android.app.ServiceStartNotAllowedException implements android.os.Parcelable {
    ctor public ForegroundServiceStartNotAllowedException(@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.ForegroundServiceStartNotAllowedException> CREATOR;
  }
  @Deprecated public class Fragment implements android.content.ComponentCallbacks2 android.view.View.OnCreateContextMenuListener {
    ctor @Deprecated public Fragment();
    method @Deprecated public void dump(String, java.io.FileDescriptor, java.io.PrintWriter, String[]);
@@ -6524,6 +6538,9 @@ package android.app {
    field public static final int STOP_FOREGROUND_REMOVE = 1; // 0x1
  }
  public abstract class ServiceStartNotAllowedException extends java.lang.IllegalStateException {
  }
  public abstract class SharedElementCallback {
    ctor public SharedElementCallback();
    method public android.os.Parcelable onCaptureSharedElementSnapshot(android.view.View, android.graphics.Matrix, android.graphics.RectF);
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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 background {@link Service} when it's not allowed to
 * do so.
 */
public final class BackgroundServiceStartNotAllowedException
        extends ServiceStartNotAllowedException implements Parcelable {
    /**
     * Constructor.
     */
    public BackgroundServiceStartNotAllowedException(@NonNull String message) {
        super(message);
    }

    BackgroundServiceStartNotAllowedException(@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.BackgroundServiceStartNotAllowedException>
            CREATOR = new Creator<android.app.BackgroundServiceStartNotAllowedException>() {
                @NonNull
                public android.app.BackgroundServiceStartNotAllowedException createFromParcel(
                        Parcel source) {
                    return new android.app.BackgroundServiceStartNotAllowedException(source);
                }

                @NonNull
                public android.app.BackgroundServiceStartNotAllowedException[] newArray(int size) {
                    return new android.app.BackgroundServiceStartNotAllowedException[size];
                }
            };
}
+1 −1
Original line number Diff line number Diff line
@@ -1794,7 +1794,7 @@ class ContextImpl extends Context {
                            "Unable to start service " + service
                            + ": " + cn.getClassName());
                } else if (cn.getPackageName().equals("?")) {
                    throw new IllegalStateException(
                    throw ServiceStartNotAllowedException.newInstance(requireForeground,
                            "Not allowed to start service " + service + ": " + cn.getClassName());
                }
            }
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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} when it's not allowed to
 * do so.
 */
public final class ForegroundServiceStartNotAllowedException
        extends ServiceStartNotAllowedException implements Parcelable {
    /**
     * Constructor.
     */
    public ForegroundServiceStartNotAllowedException(@NonNull String message) {
        super(message);
    }

    ForegroundServiceStartNotAllowedException(@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.ForegroundServiceStartNotAllowedException>
            CREATOR = new Creator<android.app.ForegroundServiceStartNotAllowedException>() {
                @NonNull
                public android.app.ForegroundServiceStartNotAllowedException createFromParcel(
                        Parcel source) {
                    return new android.app.ForegroundServiceStartNotAllowedException(source);
                }

                @NonNull
                public android.app.ForegroundServiceStartNotAllowedException[] newArray(int size) {
                    return new android.app.ForegroundServiceStartNotAllowedException[size];
                }
            };
}
+8 −1
Original line number Diff line number Diff line
@@ -697,7 +697,8 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
     * service element of manifest file. The value of attribute
     * {@link android.R.attr#foregroundServiceType} can be multiple flags ORed together.</p>
     *
     * @throws IllegalStateException If the app targeting API is
     * @throws ForegroundServiceStartNotAllowedException
     * 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.
     *
@@ -738,8 +739,14 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
   * @param notification The Notification to be displayed.
   * @param foregroundServiceType must be a subset flags of manifest attribute
   * {@link android.R.attr#foregroundServiceType} flags.
   *
   * @throws IllegalArgumentException if param foregroundServiceType is not subset of manifest
   *     attribute {@link android.R.attr#foregroundServiceType}.
   * @throws ForegroundServiceStartNotAllowedException
   * 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.
   *
   * @see android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_MANIFEST
   */
    public final void startForeground(int id, @NonNull Notification notification,
Loading