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 
17 package android.service.ambientcontext;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.app.ambientcontext.AmbientContextManager;
22 import android.app.ambientcontext.AmbientContextManager.StatusCode;
23 import android.os.Parcelable;
24 
25 import com.android.internal.util.AnnotationValidations;
26 
27 import java.util.Objects;
28 
29 /**
30  * Represents a status for the {@code AmbientContextDetectionService}.
31  *
32  * @hide
33  */
34 @SystemApi
35 public final class AmbientContextDetectionServiceStatus implements Parcelable {
36     /**
37      * The bundle key for this class of object, used in {@code RemoteCallback#sendResult}.
38      *
39      * @hide
40      */
41     public static final String STATUS_RESPONSE_BUNDLE_KEY =
42             "android.app.ambientcontext.AmbientContextServiceStatusBundleKey";
43 
44     @StatusCode private final int mStatusCode;
45     @NonNull private final String mPackageName;
46 
AmbientContextDetectionServiceStatus( @tatusCode int statusCode, @NonNull String packageName)47     AmbientContextDetectionServiceStatus(
48             @StatusCode int statusCode,
49             @NonNull String packageName) {
50         this.mStatusCode = statusCode;
51         AnnotationValidations.validate(StatusCode.class, null, mStatusCode);
52         this.mPackageName = packageName;
53     }
54 
55     /**
56      * The status of the service.
57      */
getStatusCode()58     public @StatusCode int getStatusCode() {
59         return mStatusCode;
60     }
61 
62     /**
63      * The package to deliver the response to.
64      */
getPackageName()65     public @NonNull String getPackageName() {
66         return mPackageName;
67     }
68 
69     @Override
toString()70     public String toString() {
71         return "AmbientContextDetectionServiceStatus { " + "statusCode = " + mStatusCode + ", "
72                 + "packageName = " + mPackageName + " }";
73     }
74 
75     @Override
writeToParcel(@onNull android.os.Parcel dest, int flags)76     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
77         byte flg = 0;
78         dest.writeByte(flg);
79         dest.writeInt(mStatusCode);
80         dest.writeString(mPackageName);
81     }
82 
83     @Override
describeContents()84     public int describeContents() {
85         return 0;
86     }
87 
88     /** @hide */
89     @SuppressWarnings({"unchecked", "RedundantCast"})
AmbientContextDetectionServiceStatus(@onNull android.os.Parcel in)90     AmbientContextDetectionServiceStatus(@NonNull android.os.Parcel in) {
91         byte flg = in.readByte();
92         int statusCode = in.readInt();
93         String packageName = in.readString();
94 
95         this.mStatusCode = statusCode;
96         AnnotationValidations.validate(
97                 StatusCode.class, null, mStatusCode);
98         this.mPackageName = packageName;
99         AnnotationValidations.validate(
100                 NonNull.class, null, mPackageName);
101     }
102 
103     public static final @NonNull Creator<AmbientContextDetectionServiceStatus> CREATOR =
104             new Creator<AmbientContextDetectionServiceStatus>() {
105         @Override
106         public AmbientContextDetectionServiceStatus[] newArray(int size) {
107             return new AmbientContextDetectionServiceStatus[size];
108         }
109 
110         @Override
111         public AmbientContextDetectionServiceStatus createFromParcel(
112                 @NonNull android.os.Parcel in) {
113             return new AmbientContextDetectionServiceStatus(in);
114         }
115     };
116 
117     /**
118      * A builder for {@link AmbientContextDetectionServiceStatus}
119      */
120     @SuppressWarnings("WeakerAccess")
121     public static final class Builder {
122         private @StatusCode int mStatusCode;
123         private @NonNull String mPackageName;
124         private long mBuilderFieldsSet = 0L;
125 
Builder(@onNull String packageName)126         public Builder(@NonNull String packageName) {
127             Objects.requireNonNull(packageName);
128             mPackageName = packageName;
129         }
130 
131         /**
132          * Sets the status of the service.
133          */
setStatusCode(@tatusCode int value)134         public @NonNull Builder setStatusCode(@StatusCode int value) {
135             checkNotUsed();
136             mBuilderFieldsSet |= 0x1;
137             mStatusCode = value;
138             return this;
139         }
140 
141         /** Builds the instance. This builder should not be touched after calling this! */
build()142         public @NonNull AmbientContextDetectionServiceStatus build() {
143             checkNotUsed();
144             mBuilderFieldsSet |= 0x2; // Mark builder used
145 
146             if ((mBuilderFieldsSet & 0x1) == 0) {
147                 mStatusCode = AmbientContextManager.STATUS_UNKNOWN;
148             }
149             AmbientContextDetectionServiceStatus o = new AmbientContextDetectionServiceStatus(
150                     mStatusCode,
151                     mPackageName);
152             return o;
153         }
154 
checkNotUsed()155         private void checkNotUsed() {
156             if ((mBuilderFieldsSet & 0x2) != 0) {
157                 throw new IllegalStateException(
158                         "This Builder should not be reused. Use a new Builder instance instead");
159             }
160         }
161     }
162 }
163