1 /*
2  * Copyright (C) 2022 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 package android.telecom;
17 import android.annotation.IntDef;
18 import android.annotation.Nullable;
19 import android.os.OutcomeReceiver;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.NonNull;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 import java.util.concurrent.Executor;
29 
30 /**
31  * This class represents a set of exceptions that can occur when requesting a
32  * {@link Connection#queryLocationForEmergency(long, String, Executor, OutcomeReceiver)}
33  */
34 public final class QueryLocationException extends RuntimeException implements Parcelable {
35     /** @hide */
36     public static final String QUERY_LOCATION_ERROR = "QueryLocationErrorKey";
37 
38     /**
39      * The operation was not completed on time.
40      */
41     public static final int ERROR_REQUEST_TIME_OUT = 1;
42     /**
43      * The operation was rejected due to an existing request.
44      */
45     public static final int ERROR_PREVIOUS_REQUEST_EXISTS = 2;
46     /**
47      * The operation has failed because it is not permitted.
48      */
49     public static final int ERROR_NOT_PERMITTED = 3;
50     /**
51      * The operation has failed due to a location query being requested for a non-emergency
52      * connection.
53      */
54     public static final int ERROR_NOT_ALLOWED_FOR_NON_EMERGENCY_CONNECTIONS = 4;
55     /**
56      * The operation has failed due to the service is not available.
57      */
58     public static final int ERROR_SERVICE_UNAVAILABLE = 5;
59     /**
60      * The operation has failed due to an unknown or unspecified error.
61      */
62     public static final int ERROR_UNSPECIFIED = 6;
63 
64     private int mCode = ERROR_UNSPECIFIED;
65     private final String mMessage;
66     @Override
describeContents()67     public int describeContents() {
68         return 0;
69     }
70     @Override
writeToParcel(@onNull Parcel dest, int flags)71     public void writeToParcel(@NonNull Parcel dest, int flags) {
72         dest.writeString8(mMessage);
73         dest.writeInt(mCode);
74     }
75     /**
76      * Responsible for creating QueryLocationException objects for deserialized Parcels.
77      */
78     public static final
79             @android.annotation.NonNull Parcelable.Creator<QueryLocationException> CREATOR =
80             new Parcelable.Creator<>() {
81                 @Override
82                 public QueryLocationException createFromParcel(Parcel source) {
83                     return new QueryLocationException(source.readString8(), source.readInt());
84                 }
85                 @Override
86                 public QueryLocationException[] newArray(int size) {
87                     return new QueryLocationException[size];
88                 }
89             };
90     /** @hide */
91     @Retention(RetentionPolicy.SOURCE)
92     @IntDef({ERROR_REQUEST_TIME_OUT,
93             ERROR_PREVIOUS_REQUEST_EXISTS,
94             ERROR_NOT_PERMITTED,
95             ERROR_NOT_ALLOWED_FOR_NON_EMERGENCY_CONNECTIONS,
96             ERROR_SERVICE_UNAVAILABLE,
97             ERROR_UNSPECIFIED})
98     public @interface QueryLocationErrorCode {}
QueryLocationException(@ullable String message)99     public QueryLocationException(@Nullable String message) {
100         super(getMessage(message, ERROR_UNSPECIFIED));
101         mMessage = message;
102     }
QueryLocationException(@ullable String message, @QueryLocationErrorCode int code)103     public QueryLocationException(@Nullable String message, @QueryLocationErrorCode int code) {
104         super(getMessage(message, code));
105         mCode = code;
106         mMessage = message;
107     }
QueryLocationException( @ullable String message, @QueryLocationErrorCode int code, @Nullable Throwable cause)108     public QueryLocationException(
109             @Nullable String message, @QueryLocationErrorCode int code, @Nullable Throwable cause) {
110         super(getMessage(message, code), cause);
111         mCode = code;
112         mMessage = message;
113     }
getCode()114     public @QueryLocationErrorCode int getCode() {
115         return mCode;
116     }
getMessage(String message, int code)117     private static String getMessage(String message, int code) {
118         StringBuilder builder;
119         if (!TextUtils.isEmpty(message)) {
120             builder = new StringBuilder(message);
121             builder.append(" (code: ");
122             builder.append(code);
123             builder.append(")");
124             return builder.toString();
125         } else {
126             return "code: " + code;
127         }
128     }
129 }
130