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.app.ambientcontext;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.Parcelable;
23 import android.os.PersistableBundle;
24 
25 import com.android.internal.util.DataClass;
26 import com.android.internal.util.Parcelling;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.time.Instant;
31 
32 
33 /**
34  * Represents a detected ambient event. Each event has a type, start time, end time,
35  * plus some optional data.
36  *
37  * @hide
38  */
39 @SystemApi
40 @DataClass(
41         genBuilder = true,
42         genConstructor = false,
43         genHiddenConstDefs = true,
44         genParcelable = true,
45         genToString = true
46 )
47 public final class AmbientContextEvent implements Parcelable {
48     /**
49      * The integer indicating an unknown event was detected.
50      */
51     public static final int EVENT_UNKNOWN = 0;
52 
53     /**
54      * The integer indicating a cough event was detected.
55      */
56     public static final int EVENT_COUGH = 1;
57 
58     /**
59      * The integer indicating a snore event was detected.
60      */
61     public static final int EVENT_SNORE = 2;
62 
63     /**
64      * The integer indicating a double-tap event was detected.
65      * For detecting this event type, there's no specific consent activity to request access, but
66      * the consent is implied through the double tap toggle in the Settings app.
67      */
68     public static final int EVENT_BACK_DOUBLE_TAP = 3;
69 
70     /**
71      * Integer indicating the start of wearable vendor defined events that can be detected.
72      * These depend on the vendor implementation.
73      */
74     public static final int EVENT_VENDOR_WEARABLE_START = 100000;
75 
76     /**
77      * Name for the mVendorData object for this AmbientContextEvent. The mVendorData must be present
78      * in the object, or it will be rejected.
79      */
80     public static final String KEY_VENDOR_WEARABLE_EVENT_NAME = "wearable_event_name";
81 
82     /** @hide */
83     @IntDef(prefix = { "EVENT_" }, value = {
84             EVENT_UNKNOWN,
85             EVENT_COUGH,
86             EVENT_SNORE,
87             EVENT_BACK_DOUBLE_TAP,
88             EVENT_VENDOR_WEARABLE_START,
89     }) public @interface EventCode {}
90 
91     /** The integer indicating an unknown level. */
92     public static final int LEVEL_UNKNOWN = 0;
93 
94     /** The integer indicating a low level. */
95     public static final int LEVEL_LOW = 1;
96 
97     /** The integer indicating a medium low level. */
98     public static final int LEVEL_MEDIUM_LOW = 2;
99 
100     /** The integer indicating a medium Level. */
101     public static final int LEVEL_MEDIUM = 3;
102 
103     /** The integer indicating a medium high level. */
104     public static final int LEVEL_MEDIUM_HIGH = 4;
105 
106     /** The integer indicating a high level. */
107     public static final int LEVEL_HIGH = 5;
108 
109     /** @hide */
110     @IntDef(prefix = {"LEVEL_"}, value = {
111             LEVEL_UNKNOWN,
112             LEVEL_LOW,
113             LEVEL_MEDIUM_LOW,
114             LEVEL_MEDIUM,
115             LEVEL_MEDIUM_HIGH,
116             LEVEL_HIGH
117     }) public @interface LevelValue {}
118 
119     @EventCode private final int mEventType;
defaultEventType()120     private static int defaultEventType() {
121         return EVENT_UNKNOWN;
122     }
123 
124     /** Event start time */
125     @DataClass.ParcelWith(Parcelling.BuiltIn.ForInstant.class)
126     @NonNull private final Instant mStartTime;
defaultStartTime()127     @NonNull private static Instant defaultStartTime() {
128         return Instant.MIN;
129     }
130 
131     /** Event end time */
132     @DataClass.ParcelWith(Parcelling.BuiltIn.ForInstant.class)
133     @NonNull private final Instant mEndTime;
defaultEndTime()134     @NonNull private static Instant defaultEndTime() {
135         return Instant.MAX;
136     }
137 
138     /**
139      * Confidence level from LEVEL_LOW to LEVEL_HIGH, or LEVEL_NONE if not available.
140      * Apps can add post-processing filter using this value if needed.
141      */
142     @LevelValue private final int mConfidenceLevel;
defaultConfidenceLevel()143     private static int defaultConfidenceLevel() {
144         return LEVEL_UNKNOWN;
145     }
146 
147     /**
148      * Density level from LEVEL_LOW to LEVEL_HIGH, or LEVEL_NONE if not available.
149      * Apps can add post-processing filter using this value if needed.
150      */
151     @LevelValue private final int mDensityLevel;
defaultDensityLevel()152     private static int defaultDensityLevel() {
153         return LEVEL_UNKNOWN;
154     }
155 
156     /**
157      * Vendor defined specific values for vendor event types.
158      *
159      * <p> The use of this vendor data is discouraged. For data defined in the range above
160      * {@code EVENT_VENDOR_WEARABLE_START} this bundle must include the
161      * {@link KEY_VENDOR_WEARABLE_EVENT_NAME} field or it will be rejected. In addition, to increase
162      * transparency of this data contents of this bundle will be logged to logcat.</p>
163      */
164     private final @NonNull PersistableBundle mVendorData;
defaultVendorData()165     private static PersistableBundle defaultVendorData() {
166         return new PersistableBundle();
167     }
168 
169 
170 
171     // Code below generated by codegen v1.0.23.
172     //
173     // DO NOT MODIFY!
174     // CHECKSTYLE:OFF Generated code
175     //
176     // To regenerate run:
177     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/app/ambientcontext/AmbientContextEvent.java
178     //
179     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
180     //   Settings > Editor > Code Style > Formatter Control
181     //@formatter:off
182 
183 
184     /** @hide */
185     @IntDef(prefix = "EVENT_", value = {
186         EVENT_UNKNOWN,
187         EVENT_COUGH,
188         EVENT_SNORE,
189         EVENT_BACK_DOUBLE_TAP,
190         EVENT_VENDOR_WEARABLE_START
191     })
192     @Retention(RetentionPolicy.SOURCE)
193     @DataClass.Generated.Member
194     public @interface Event {}
195 
196     /** @hide */
197     @DataClass.Generated.Member
eventToString(@vent int value)198     public static String eventToString(@Event int value) {
199         switch (value) {
200             case EVENT_UNKNOWN:
201                     return "EVENT_UNKNOWN";
202             case EVENT_COUGH:
203                     return "EVENT_COUGH";
204             case EVENT_SNORE:
205                     return "EVENT_SNORE";
206             case EVENT_BACK_DOUBLE_TAP:
207                     return "EVENT_BACK_DOUBLE_TAP";
208             case EVENT_VENDOR_WEARABLE_START:
209                     return "EVENT_VENDOR_WEARABLE_START";
210             default: return Integer.toHexString(value);
211         }
212     }
213 
214     /** @hide */
215     @IntDef(prefix = "LEVEL_", value = {
216         LEVEL_UNKNOWN,
217         LEVEL_LOW,
218         LEVEL_MEDIUM_LOW,
219         LEVEL_MEDIUM,
220         LEVEL_MEDIUM_HIGH,
221         LEVEL_HIGH
222     })
223     @Retention(RetentionPolicy.SOURCE)
224     @DataClass.Generated.Member
225     public @interface Level {}
226 
227     /** @hide */
228     @DataClass.Generated.Member
levelToString(@evel int value)229     public static String levelToString(@Level int value) {
230         switch (value) {
231             case LEVEL_UNKNOWN:
232                     return "LEVEL_UNKNOWN";
233             case LEVEL_LOW:
234                     return "LEVEL_LOW";
235             case LEVEL_MEDIUM_LOW:
236                     return "LEVEL_MEDIUM_LOW";
237             case LEVEL_MEDIUM:
238                     return "LEVEL_MEDIUM";
239             case LEVEL_MEDIUM_HIGH:
240                     return "LEVEL_MEDIUM_HIGH";
241             case LEVEL_HIGH:
242                     return "LEVEL_HIGH";
243             default: return Integer.toHexString(value);
244         }
245     }
246 
247     @DataClass.Generated.Member
AmbientContextEvent( @ventCode int eventType, @NonNull Instant startTime, @NonNull Instant endTime, @LevelValue int confidenceLevel, @LevelValue int densityLevel, @NonNull PersistableBundle vendorData)248     /* package-private */ AmbientContextEvent(
249             @EventCode int eventType,
250             @NonNull Instant startTime,
251             @NonNull Instant endTime,
252             @LevelValue int confidenceLevel,
253             @LevelValue int densityLevel,
254             @NonNull PersistableBundle vendorData) {
255         this.mEventType = eventType;
256         com.android.internal.util.AnnotationValidations.validate(
257                 EventCode.class, null, mEventType);
258         this.mStartTime = startTime;
259         com.android.internal.util.AnnotationValidations.validate(
260                 NonNull.class, null, mStartTime);
261         this.mEndTime = endTime;
262         com.android.internal.util.AnnotationValidations.validate(
263                 NonNull.class, null, mEndTime);
264         this.mConfidenceLevel = confidenceLevel;
265         com.android.internal.util.AnnotationValidations.validate(
266                 LevelValue.class, null, mConfidenceLevel);
267         this.mDensityLevel = densityLevel;
268         com.android.internal.util.AnnotationValidations.validate(
269                 LevelValue.class, null, mDensityLevel);
270         this.mVendorData = vendorData;
271         com.android.internal.util.AnnotationValidations.validate(
272                 NonNull.class, null, mVendorData);
273 
274         // onConstructed(); // You can define this method to get a callback
275     }
276 
277     @DataClass.Generated.Member
getEventType()278     public @EventCode int getEventType() {
279         return mEventType;
280     }
281 
282     /**
283      * Event start time
284      */
285     @DataClass.Generated.Member
getStartTime()286     public @NonNull Instant getStartTime() {
287         return mStartTime;
288     }
289 
290     /**
291      * Event end time
292      */
293     @DataClass.Generated.Member
getEndTime()294     public @NonNull Instant getEndTime() {
295         return mEndTime;
296     }
297 
298     /**
299      * Confidence level from LEVEL_LOW to LEVEL_HIGH, or LEVEL_NONE if not available.
300      * Apps can add post-processing filter using this value if needed.
301      */
302     @DataClass.Generated.Member
getConfidenceLevel()303     public @LevelValue int getConfidenceLevel() {
304         return mConfidenceLevel;
305     }
306 
307     /**
308      * Density level from LEVEL_LOW to LEVEL_HIGH, or LEVEL_NONE if not available.
309      * Apps can add post-processing filter using this value if needed.
310      */
311     @DataClass.Generated.Member
getDensityLevel()312     public @LevelValue int getDensityLevel() {
313         return mDensityLevel;
314     }
315 
316     /**
317      * Vendor defined specific values for vendor event types.
318      *
319      * <p> The use of this vendor data is discouraged. For data defined in the range above
320      * {@code EVENT_VENDOR_WEARABLE_START} this bundle must include the
321      * {@link KEY_VENDOR_WEARABLE_EVENT_NAME} field or it will be rejected. In addition, to increase
322      * transparency of this data contents of this bundle will be logged to logcat.</p>
323      */
324     @DataClass.Generated.Member
getVendorData()325     public @NonNull PersistableBundle getVendorData() {
326         return mVendorData;
327     }
328 
329     @Override
330     @DataClass.Generated.Member
toString()331     public String toString() {
332         // You can override field toString logic by defining methods like:
333         // String fieldNameToString() { ... }
334 
335         return "AmbientContextEvent { " +
336                 "eventType = " + mEventType + ", " +
337                 "startTime = " + mStartTime + ", " +
338                 "endTime = " + mEndTime + ", " +
339                 "confidenceLevel = " + mConfidenceLevel + ", " +
340                 "densityLevel = " + mDensityLevel + ", " +
341                 "vendorData = " + mVendorData +
342         " }";
343     }
344 
345     @DataClass.Generated.Member
346     static Parcelling<Instant> sParcellingForStartTime =
347             Parcelling.Cache.get(
348                     Parcelling.BuiltIn.ForInstant.class);
349     static {
350         if (sParcellingForStartTime == null) {
351             sParcellingForStartTime = Parcelling.Cache.put(
352                     new Parcelling.BuiltIn.ForInstant());
353         }
354     }
355 
356     @DataClass.Generated.Member
357     static Parcelling<Instant> sParcellingForEndTime =
358             Parcelling.Cache.get(
359                     Parcelling.BuiltIn.ForInstant.class);
360     static {
361         if (sParcellingForEndTime == null) {
362             sParcellingForEndTime = Parcelling.Cache.put(
363                     new Parcelling.BuiltIn.ForInstant());
364         }
365     }
366 
367     @Override
368     @DataClass.Generated.Member
writeToParcel(@onNull android.os.Parcel dest, int flags)369     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
370         // You can override field parcelling by defining methods like:
371         // void parcelFieldName(Parcel dest, int flags) { ... }
372 
373         dest.writeInt(mEventType);
374         sParcellingForStartTime.parcel(mStartTime, dest, flags);
375         sParcellingForEndTime.parcel(mEndTime, dest, flags);
376         dest.writeInt(mConfidenceLevel);
377         dest.writeInt(mDensityLevel);
378         dest.writeTypedObject(mVendorData, flags);
379     }
380 
381     @Override
382     @DataClass.Generated.Member
describeContents()383     public int describeContents() { return 0; }
384 
385     /** @hide */
386     @SuppressWarnings({"unchecked", "RedundantCast"})
387     @DataClass.Generated.Member
AmbientContextEvent(@onNull android.os.Parcel in)388     /* package-private */ AmbientContextEvent(@NonNull android.os.Parcel in) {
389         // You can override field unparcelling by defining methods like:
390         // static FieldType unparcelFieldName(Parcel in) { ... }
391 
392         int eventType = in.readInt();
393         Instant startTime = sParcellingForStartTime.unparcel(in);
394         Instant endTime = sParcellingForEndTime.unparcel(in);
395         int confidenceLevel = in.readInt();
396         int densityLevel = in.readInt();
397         PersistableBundle vendorData = (PersistableBundle) in.readTypedObject(PersistableBundle.CREATOR);
398 
399         this.mEventType = eventType;
400         com.android.internal.util.AnnotationValidations.validate(
401                 EventCode.class, null, mEventType);
402         this.mStartTime = startTime;
403         com.android.internal.util.AnnotationValidations.validate(
404                 NonNull.class, null, mStartTime);
405         this.mEndTime = endTime;
406         com.android.internal.util.AnnotationValidations.validate(
407                 NonNull.class, null, mEndTime);
408         this.mConfidenceLevel = confidenceLevel;
409         com.android.internal.util.AnnotationValidations.validate(
410                 LevelValue.class, null, mConfidenceLevel);
411         this.mDensityLevel = densityLevel;
412         com.android.internal.util.AnnotationValidations.validate(
413                 LevelValue.class, null, mDensityLevel);
414         this.mVendorData = vendorData;
415         com.android.internal.util.AnnotationValidations.validate(
416                 NonNull.class, null, mVendorData);
417 
418         // onConstructed(); // You can define this method to get a callback
419     }
420 
421     @DataClass.Generated.Member
422     public static final @NonNull Parcelable.Creator<AmbientContextEvent> CREATOR
423             = new Parcelable.Creator<AmbientContextEvent>() {
424         @Override
425         public AmbientContextEvent[] newArray(int size) {
426             return new AmbientContextEvent[size];
427         }
428 
429         @Override
430         public AmbientContextEvent createFromParcel(@NonNull android.os.Parcel in) {
431             return new AmbientContextEvent(in);
432         }
433     };
434 
435     /**
436      * A builder for {@link AmbientContextEvent}
437      */
438     @SuppressWarnings("WeakerAccess")
439     @DataClass.Generated.Member
440     public static final class Builder {
441 
442         private @EventCode int mEventType;
443         private @NonNull Instant mStartTime;
444         private @NonNull Instant mEndTime;
445         private @LevelValue int mConfidenceLevel;
446         private @LevelValue int mDensityLevel;
447         private @NonNull PersistableBundle mVendorData;
448 
449         private long mBuilderFieldsSet = 0L;
450 
Builder()451         public Builder() {
452         }
453 
454         @DataClass.Generated.Member
setEventType(@ventCode int value)455         public @NonNull Builder setEventType(@EventCode int value) {
456             checkNotUsed();
457             mBuilderFieldsSet |= 0x1;
458             mEventType = value;
459             return this;
460         }
461 
462         /**
463          * Event start time
464          */
465         @DataClass.Generated.Member
setStartTime(@onNull Instant value)466         public @NonNull Builder setStartTime(@NonNull Instant value) {
467             checkNotUsed();
468             mBuilderFieldsSet |= 0x2;
469             mStartTime = value;
470             return this;
471         }
472 
473         /**
474          * Event end time
475          */
476         @DataClass.Generated.Member
setEndTime(@onNull Instant value)477         public @NonNull Builder setEndTime(@NonNull Instant value) {
478             checkNotUsed();
479             mBuilderFieldsSet |= 0x4;
480             mEndTime = value;
481             return this;
482         }
483 
484         /**
485          * Confidence level from LEVEL_LOW to LEVEL_HIGH, or LEVEL_NONE if not available.
486          * Apps can add post-processing filter using this value if needed.
487          */
488         @DataClass.Generated.Member
setConfidenceLevel(@evelValue int value)489         public @NonNull Builder setConfidenceLevel(@LevelValue int value) {
490             checkNotUsed();
491             mBuilderFieldsSet |= 0x8;
492             mConfidenceLevel = value;
493             return this;
494         }
495 
496         /**
497          * Density level from LEVEL_LOW to LEVEL_HIGH, or LEVEL_NONE if not available.
498          * Apps can add post-processing filter using this value if needed.
499          */
500         @DataClass.Generated.Member
setDensityLevel(@evelValue int value)501         public @NonNull Builder setDensityLevel(@LevelValue int value) {
502             checkNotUsed();
503             mBuilderFieldsSet |= 0x10;
504             mDensityLevel = value;
505             return this;
506         }
507 
508         /**
509          * Vendor defined specific values for vendor event types.
510          *
511          * <p> The use of this vendor data is discouraged. For data defined in the range above
512          * {@code EVENT_VENDOR_WEARABLE_START} this bundle must include the
513          * {@link KEY_VENDOR_WEARABLE_EVENT_NAME} field or it will be rejected. In addition, to increase
514          * transparency of this data contents of this bundle will be logged to logcat.</p>
515          */
516         @DataClass.Generated.Member
setVendorData(@onNull PersistableBundle value)517         public @NonNull Builder setVendorData(@NonNull PersistableBundle value) {
518             checkNotUsed();
519             mBuilderFieldsSet |= 0x20;
520             mVendorData = value;
521             return this;
522         }
523 
524         /** Builds the instance. This builder should not be touched after calling this! */
build()525         public @NonNull AmbientContextEvent build() {
526             checkNotUsed();
527             mBuilderFieldsSet |= 0x40; // Mark builder used
528 
529             if ((mBuilderFieldsSet & 0x1) == 0) {
530                 mEventType = defaultEventType();
531             }
532             if ((mBuilderFieldsSet & 0x2) == 0) {
533                 mStartTime = defaultStartTime();
534             }
535             if ((mBuilderFieldsSet & 0x4) == 0) {
536                 mEndTime = defaultEndTime();
537             }
538             if ((mBuilderFieldsSet & 0x8) == 0) {
539                 mConfidenceLevel = defaultConfidenceLevel();
540             }
541             if ((mBuilderFieldsSet & 0x10) == 0) {
542                 mDensityLevel = defaultDensityLevel();
543             }
544             if ((mBuilderFieldsSet & 0x20) == 0) {
545                 mVendorData = defaultVendorData();
546             }
547             AmbientContextEvent o = new AmbientContextEvent(
548                     mEventType,
549                     mStartTime,
550                     mEndTime,
551                     mConfidenceLevel,
552                     mDensityLevel,
553                     mVendorData);
554             return o;
555         }
556 
checkNotUsed()557         private void checkNotUsed() {
558             if ((mBuilderFieldsSet & 0x40) != 0) {
559                 throw new IllegalStateException(
560                         "This Builder should not be reused. Use a new Builder instance instead");
561             }
562         }
563     }
564 
565     @DataClass.Generated(
566             time = 1671217108067L,
567             codegenVersion = "1.0.23",
568             sourceFile = "frameworks/base/core/java/android/app/ambientcontext/AmbientContextEvent.java",
569             inputSignatures = "public static final  int EVENT_UNKNOWN\npublic static final  int EVENT_COUGH\npublic static final  int EVENT_SNORE\npublic static final  int EVENT_BACK_DOUBLE_TAP\npublic static final  int EVENT_VENDOR_WEARABLE_START\npublic static final  java.lang.String KEY_VENDOR_WEARABLE_EVENT_NAME\npublic static final  int LEVEL_UNKNOWN\npublic static final  int LEVEL_LOW\npublic static final  int LEVEL_MEDIUM_LOW\npublic static final  int LEVEL_MEDIUM\npublic static final  int LEVEL_MEDIUM_HIGH\npublic static final  int LEVEL_HIGH\nprivate final @android.app.ambientcontext.AmbientContextEvent.EventCode int mEventType\nprivate final @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInstant.class) @android.annotation.NonNull java.time.Instant mStartTime\nprivate final @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInstant.class) @android.annotation.NonNull java.time.Instant mEndTime\nprivate final @android.app.ambientcontext.AmbientContextEvent.LevelValue int mConfidenceLevel\nprivate final @android.app.ambientcontext.AmbientContextEvent.LevelValue int mDensityLevel\nprivate final @android.annotation.NonNull android.os.PersistableBundle mVendorData\nprivate static  int defaultEventType()\nprivate static @android.annotation.NonNull java.time.Instant defaultStartTime()\nprivate static @android.annotation.NonNull java.time.Instant defaultEndTime()\nprivate static  int defaultConfidenceLevel()\nprivate static  int defaultDensityLevel()\nprivate static  android.os.PersistableBundle defaultVendorData()\nclass AmbientContextEvent extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genBuilder=true, genConstructor=false, genHiddenConstDefs=true, genParcelable=true, genToString=true)")
570     @Deprecated
__metadata()571     private void __metadata() {}
572 
573 
574     //@formatter:on
575     // End of generated code
576 
577 }
578