1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app;
18 
19 import android.annotation.NonNull;
20 
21 /**
22  * Exception thrown when an app tries to start a {@link Service} when it's not allowed to do so.
23  */
24 public abstract class ServiceStartNotAllowedException extends IllegalStateException {
ServiceStartNotAllowedException(@onNull String message)25     ServiceStartNotAllowedException(@NonNull String message) {
26         super(message);
27     }
28 
29     /**
30      * Return either {@link ForegroundServiceStartNotAllowedException} or
31      * {@link BackgroundServiceStartNotAllowedException}
32      * @hide
33      */
34     @NonNull
newInstance(boolean foreground, @NonNull String message)35     public static ServiceStartNotAllowedException newInstance(boolean foreground,
36             @NonNull String message) {
37         if (foreground) {
38             return new ForegroundServiceStartNotAllowedException(message);
39         } else {
40             return new BackgroundServiceStartNotAllowedException(message);
41         }
42     }
43 
44     @Override
getCause()45     public synchronized Throwable getCause() {
46         // "Cause" is often used for clustering exceptions, and developers don't want to have it
47         // for this exception. b/210890426
48         return null;
49     }
50 }
51