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