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 import android.annotation.Nullable;
21 import android.content.ComponentName;
22 import android.os.Bundle;
23 import android.util.AndroidRuntimeException;
24 
25 /**
26  * Exception used by {@link ActivityThread} to crash an app process for an unknown cause.
27  * An exception of this class is no longer supposed to be thrown. Instead, we use fine-grained
28  * sub-exceptions.
29  *
30  * Subclasses must be registered in
31  * {@link android.app.ActivityThread#throwRemoteServiceException(java.lang.String, int)}.
32  *
33  * @hide
34  */
35 public class RemoteServiceException extends AndroidRuntimeException {
RemoteServiceException(String msg)36     public RemoteServiceException(String msg) {
37         super(msg);
38     }
39 
RemoteServiceException(String msg, Throwable cause)40     public RemoteServiceException(String msg, Throwable cause) {
41         super(msg, cause);
42     }
43 
44     /**
45      * Exception used to crash an app process when it didn't call {@link Service#startForeground}
46      * in time after the service was started with
47      * {@link android.content.Context#startForegroundService}.
48      *
49      * @hide
50      */
51     public static class ForegroundServiceDidNotStartInTimeException extends RemoteServiceException {
52         /** The type ID passed to {@link IApplicationThread#scheduleCrash}. */
53         public static final int TYPE_ID = 1;
54 
55         private static final String KEY_SERVICE_CLASS_NAME = "serviceclassname";
56 
ForegroundServiceDidNotStartInTimeException(String msg, Throwable cause)57         public ForegroundServiceDidNotStartInTimeException(String msg, Throwable cause) {
58             super(msg, cause);
59         }
60 
createExtrasForService(@onNull ComponentName service)61         public static Bundle createExtrasForService(@NonNull ComponentName service) {
62             Bundle b = new Bundle();
63             b.putString(KEY_SERVICE_CLASS_NAME, service.getClassName());
64             return b;
65         }
66 
67         @Nullable
getServiceClassNameFromExtras(@ullable Bundle extras)68         public static String getServiceClassNameFromExtras(@Nullable Bundle extras) {
69             return (extras == null) ? null : extras.getString(KEY_SERVICE_CLASS_NAME);
70         }
71     }
72 
73     /**
74      * Exception used to crash an app process when the system received a RemoteException
75      * while posting a notification of a foreground service.
76      *
77      * @hide
78      */
79     public static class CannotPostForegroundServiceNotificationException
80             extends RemoteServiceException {
81         /** The type ID passed to {@link IApplicationThread#scheduleCrash}. */
82         public static final int TYPE_ID = 2;
83 
CannotPostForegroundServiceNotificationException(String msg)84         public CannotPostForegroundServiceNotificationException(String msg) {
85             super(msg);
86         }
87     }
88 
89     /**
90      * Exception used to crash an app process when the system finds an error in a foreground service
91      * notification.
92      *
93      * @hide
94      */
95     public static class BadForegroundServiceNotificationException extends RemoteServiceException {
96         /** The type ID passed to {@link IApplicationThread#scheduleCrash}. */
97         public static final int TYPE_ID = 3;
98 
BadForegroundServiceNotificationException(String msg)99         public BadForegroundServiceNotificationException(String msg) {
100             super(msg);
101         }
102     }
103 
104     /**
105      * Exception used to crash an app process when the system finds an error in a user-initiated job
106      * notification.
107      *
108      * @hide
109      */
110     public static class BadUserInitiatedJobNotificationException extends RemoteServiceException {
111         /** The type ID passed to {@link IApplicationThread#scheduleCrash}. */
112         public static final int TYPE_ID = 6;
113 
BadUserInitiatedJobNotificationException(String msg)114         public BadUserInitiatedJobNotificationException(String msg) {
115             super(msg);
116         }
117     }
118 
119     /**
120      * Exception used to crash an app process when it calls a setting activity that requires
121      * the {@code REQUEST_PASSWORD_COMPLEXITY} permission.
122      *
123      * @hide
124      */
125     public static class MissingRequestPasswordComplexityPermissionException
126             extends RemoteServiceException {
127         /** The type ID passed to {@link IApplicationThread#scheduleCrash}. */
128         public static final int TYPE_ID = 4;
129 
MissingRequestPasswordComplexityPermissionException(String msg)130         public MissingRequestPasswordComplexityPermissionException(String msg) {
131             super(msg);
132         }
133     }
134 
135     /**
136      * Exception used to crash an app process by {@code adb shell am crash}.
137      *
138      * @hide
139      */
140     public static class CrashedByAdbException extends RemoteServiceException {
141         /** The type ID passed to {@link IApplicationThread#scheduleCrash}. */
142         public static final int TYPE_ID = 5;
143 
CrashedByAdbException(String msg)144         public CrashedByAdbException(String msg) {
145             super(msg);
146         }
147     }
148 }
149