1 /*
2  * Copyright (C) 2020 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.car.occupantawareness;
18 
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20 
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.car.Car;
25 import android.car.annotation.RequiredFeature;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 
29 import java.lang.annotation.Retention;
30 
31 /**
32  * Complete detection result for a single detected person. Includes presence detection, {@link
33  * GazeDetection} and {@link DriverMonitoringDetection}.
34  *
35  * <p>Register to listen to events via {@link OccupantAwarenessManager}.
36  *
37  * @hide
38  */
39 @RequiredFeature(Car.OCCUPANT_AWARENESS_SERVICE)
40 public final class OccupantAwarenessDetection implements Parcelable {
41     /** Empty occupant flag. */
42     public static final int VEHICLE_OCCUPANT_NONE = 0;
43 
44     /** Occupants that the system detects as the driver. */
45     public static final int VEHICLE_OCCUPANT_DRIVER = 1 << 2;
46 
47     /** Occupants that the system detects as front seat passengers. */
48     public static final int VEHICLE_OCCUPANT_FRONT_PASSENGER = 1 << 1;
49 
50     /** Occupants that the system detects in the second vehicle row, on the left. */
51     public static final int VEHICLE_OCCUPANT_ROW_2_PASSENGER_LEFT = 1 << 3;
52 
53     /** Occupants that the system detects in the second vehicle row, in the center. */
54     public static final int VEHICLE_OCCUPANT_ROW_2_PASSENGER_CENTER = 1 << 4;
55 
56     /** Occupants that the system detects in the second vehicle row, on the right. */
57     public static final int VEHICLE_OCCUPANT_ROW_2_PASSENGER_RIGHT = 1 << 5;
58 
59     /** Occupants that the system detects in the third vehicle row, on the left. */
60     public static final int VEHICLE_OCCUPANT_ROW_3_PASSENGER_LEFT = 1 << 6;
61 
62     /** Occupants that the system detects in the third vehicle row, in the middle. */
63     public static final int VEHICLE_OCCUPANT_ROW_3_PASSENGER_CENTER = 1 << 7;
64 
65     /** Occupants that the system detects in the third vehicle row, on the right. */
66     public static final int VEHICLE_OCCUPANT_ROW_3_PASSENGER_RIGHT = 1 << 8;
67 
68     /** All occupants that the system detects in the front row of the vehicle. */
69     public static final int VEHICLE_OCCUPANT_ALL_FRONT_OCCUPANTS =
70             VEHICLE_OCCUPANT_DRIVER | VEHICLE_OCCUPANT_FRONT_PASSENGER;
71 
72     /** All occupants that the system detects in the second row of the vehicle. */
73     public static final int VEHICLE_OCCUPANT_ALL_ROW_2_OCCUPANTS =
74             VEHICLE_OCCUPANT_ROW_2_PASSENGER_LEFT
75                     | VEHICLE_OCCUPANT_ROW_2_PASSENGER_RIGHT
76                     | VEHICLE_OCCUPANT_ROW_2_PASSENGER_CENTER;
77 
78     /** All occupants that the system detects in the third row of the vehicle. */
79     public static final int VEHICLE_OCCUPANT_ALL_ROW_3_OCCUPANTS =
80             VEHICLE_OCCUPANT_ROW_3_PASSENGER_LEFT
81                     | VEHICLE_OCCUPANT_ROW_3_PASSENGER_RIGHT
82                     | VEHICLE_OCCUPANT_ROW_3_PASSENGER_CENTER;
83 
84     /** All occupants that the system detects in the vehicle. */
85     public static final int VEHICLE_OCCUPANT_ALL_OCCUPANTS =
86             VEHICLE_OCCUPANT_ALL_FRONT_OCCUPANTS
87                     | VEHICLE_OCCUPANT_ALL_ROW_2_OCCUPANTS
88                     | VEHICLE_OCCUPANT_ALL_ROW_3_OCCUPANTS;
89 
90     /**
91      * Vehicle occupant roles based on their location in the vehicle.
92      *
93      * @hide
94      */
95     @Retention(SOURCE)
96     @IntDef(
97             flag = true,
98             value = {
99                 VEHICLE_OCCUPANT_NONE,
100                 VEHICLE_OCCUPANT_DRIVER,
101                 VEHICLE_OCCUPANT_FRONT_PASSENGER,
102                 VEHICLE_OCCUPANT_ROW_2_PASSENGER_LEFT,
103                 VEHICLE_OCCUPANT_ROW_2_PASSENGER_CENTER,
104                 VEHICLE_OCCUPANT_ROW_2_PASSENGER_RIGHT,
105                 VEHICLE_OCCUPANT_ROW_3_PASSENGER_LEFT,
106                 VEHICLE_OCCUPANT_ROW_3_PASSENGER_CENTER,
107                 VEHICLE_OCCUPANT_ROW_3_PASSENGER_RIGHT,
108                 VEHICLE_OCCUPANT_ALL_FRONT_OCCUPANTS,
109                 VEHICLE_OCCUPANT_ALL_ROW_2_OCCUPANTS,
110                 VEHICLE_OCCUPANT_ALL_ROW_3_OCCUPANTS
111             })
112     public @interface VehicleOccupantRole {}
113 
114     /** No prediction could be made. */
115     public static final int CONFIDENCE_LEVEL_NONE = 0;
116 
117     /**
118      * Best-guess, low-confidence prediction. Predictions exceeding this threshold are adequate for
119      * non-critical applications.
120      */
121     public static final int CONFIDENCE_LEVEL_LOW = 1;
122 
123     /**
124      * High-confidence prediction. Predictions exceeding this threshold are adequate for
125      * applications that require reliable predictions.
126      */
127     public static final int CONFIDENCE_LEVEL_HIGH = 2;
128 
129     /** Highest confidence rate achievable. */
130     public static final int CONFIDENCE_LEVEL_MAX = 3;
131 
132     /**
133      * Confidence scores for predictions.
134      *
135      * @hide
136      */
137     @Retention(SOURCE)
138     @IntDef(
139             value = {
140                 CONFIDENCE_LEVEL_NONE,
141                 CONFIDENCE_LEVEL_LOW,
142                 CONFIDENCE_LEVEL_HIGH,
143                 CONFIDENCE_LEVEL_MAX
144             })
145     public @interface ConfidenceLevel {}
146 
147     /** The {@link VehicleOccupantRole} of the face associated with this event. */
148     public final @VehicleOccupantRole int role;
149 
150     /** Timestamp when the underlying detection data was detected, in milliseconds since boot. */
151     public final long timestampMillis;
152 
153     /** Indicates whether any person was detected for the given role. */
154     public final boolean isPresent;
155 
156     /**
157      * {@link GazeDetection} data for the requested role, or {@code null} if no person was found.
158      */
159     public final @Nullable GazeDetection gazeDetection;
160 
161     /**
162      * {@link DriverMonitoringDetection} data for the driver, or {@code null} if the role was
163      * non-driver or if the detection could not be computed.
164      */
165     public final @Nullable DriverMonitoringDetection driverMonitoringDetection;
166 
OccupantAwarenessDetection( @ehicleOccupantRole int role, long timestampMillis, boolean isPresent, @Nullable GazeDetection gazeDetection, @Nullable DriverMonitoringDetection driverMonitoringDetection)167     public OccupantAwarenessDetection(
168             @VehicleOccupantRole int role,
169             long timestampMillis,
170             boolean isPresent,
171             @Nullable GazeDetection gazeDetection,
172             @Nullable DriverMonitoringDetection driverMonitoringDetection) {
173         this.role = role;
174         this.timestampMillis = timestampMillis;
175         this.isPresent = isPresent;
176         this.gazeDetection = gazeDetection;
177         this.driverMonitoringDetection = driverMonitoringDetection;
178     }
179 
180     @Override
describeContents()181     public int describeContents() {
182         return 0;
183     }
184 
185     @Override
writeToParcel(@onNull Parcel dest, int flags)186     public void writeToParcel(@NonNull Parcel dest, int flags) {
187         dest.writeInt(role);
188         dest.writeLong(timestampMillis);
189         dest.writeBoolean(isPresent);
190         dest.writeParcelable(gazeDetection, flags);
191         dest.writeParcelable(driverMonitoringDetection, flags);
192     }
193 
194     @Override
toString()195     public String toString() {
196         return "OccupantAwarenessDetection{"
197                 + "role="  + role
198                 + ", timestampMillis=" + timestampMillis
199                 + ", isPresent=" + isPresent
200                 + ", gazeDetection="
201                 + (gazeDetection == null ? "(null)" : gazeDetection.toString())
202                 + ", driverMonitoringDetection="
203                 + (driverMonitoringDetection == null
204                         ? "(null)" : driverMonitoringDetection.toString())
205                 + "}";
206     }
207 
208     public static final @NonNull Parcelable.Creator<OccupantAwarenessDetection> CREATOR =
209             new Parcelable.Creator<OccupantAwarenessDetection>() {
210                 public OccupantAwarenessDetection createFromParcel(Parcel in) {
211                     return new OccupantAwarenessDetection(in);
212                 }
213 
214                 public OccupantAwarenessDetection[] newArray(int size) {
215                     return new OccupantAwarenessDetection[size];
216                 }
217             };
218 
OccupantAwarenessDetection(Parcel in)219     private OccupantAwarenessDetection(Parcel in) {
220         role = in.readInt();
221         timestampMillis = in.readLong();
222         isPresent = in.readBoolean();
223         gazeDetection = in.readParcelable(GazeDetection.class.getClassLoader());
224         driverMonitoringDetection =
225                 in.readParcelable(DriverMonitoringDetection.class.getClassLoader());
226     }
227 }
228