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