• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.ranging;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.IntDef;
21 import android.annotation.IntRange;
22 import android.annotation.NonNull;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.ranging.flags.Flags;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.Objects;
31 
32 /**
33  * Represents the configuration for data notifications in ranging operations.
34  *
35  * <p>This class holds the configuration settings for how notifications are sent
36  * regarding the proximity of ranging devices.
37  */
38 @FlaggedApi(Flags.FLAG_RANGING_STACK_ENABLED)
39 public final class DataNotificationConfig implements Parcelable {
40 
41     private final @NotificationConfigType int mNotificationConfigType;
42     private final int mProximityNearCm;
43     private final int mProximityFarCm;
44 
DataNotificationConfig(Parcel in)45     private DataNotificationConfig(Parcel in) {
46         mNotificationConfigType = in.readInt();
47         mProximityNearCm = in.readInt();
48         mProximityFarCm = in.readInt();
49     }
50 
51     @NonNull
52     public static final Creator<DataNotificationConfig> CREATOR = new Creator<>() {
53         @Override
54         public DataNotificationConfig createFromParcel(Parcel in) {
55             return new DataNotificationConfig(in);
56         }
57 
58         @Override
59         public DataNotificationConfig[] newArray(int size) {
60             return new DataNotificationConfig[size];
61         }
62     };
63 
64     @Override
describeContents()65     public int describeContents() {
66         return 0;
67     }
68 
69     @Override
writeToParcel(@onNull Parcel dest, int flags)70     public void writeToParcel(@NonNull Parcel dest, int flags) {
71         dest.writeInt(mNotificationConfigType);
72         dest.writeInt(mProximityNearCm);
73         dest.writeInt(mProximityFarCm);
74     }
75 
76     /**
77      * @hide
78      */
79     @Retention(RetentionPolicy.SOURCE)
80     @IntDef({
81             NOTIFICATION_CONFIG_DISABLE,
82             NOTIFICATION_CONFIG_ENABLE,
83             NOTIFICATION_CONFIG_PROXIMITY_LEVEL,
84             NOTIFICATION_CONFIG_PROXIMITY_EDGE,
85     })
86     public @interface NotificationConfigType {
87     }
88 
89     // Range data notification will be disabled.
90     public static final int NOTIFICATION_CONFIG_DISABLE = 0;
91     // Range data notification will be enabled (default).
92     public static final int NOTIFICATION_CONFIG_ENABLE = 1;
93     // Range data notification is enabled when peer device is in the configured range - [near, far].
94     public static final int NOTIFICATION_CONFIG_PROXIMITY_LEVEL = 2;
95     //Range data notification is enabled when peer device enters or exits the configured range -
96     // [near, far].
97     public static final int NOTIFICATION_CONFIG_PROXIMITY_EDGE = 3;
98 
99 
DataNotificationConfig(Builder builder)100     private DataNotificationConfig(Builder builder) {
101         if (builder.mProximityNearCm > builder.mProximityFarCm) {
102             throw new IllegalArgumentException(
103                     "Ntf proximity near cannot be greater than Ntf proximity far");
104         }
105         mNotificationConfigType = builder.mNotificationConfigType;
106         mProximityNearCm = builder.mProximityNearCm;
107         mProximityFarCm = builder.mProximityFarCm;
108     }
109 
110     /**
111      * Returns the configured notification configuration type.
112      *
113      * @return the notification configuration type.
114      *
115      */
116     @NotificationConfigType
getNotificationConfigType()117     public int getNotificationConfigType() {
118         return mNotificationConfigType;
119     }
120 
121     /**
122      * Returns the near proximity threshold in centimeters.
123      *
124      * @return the near proximity in centimeters.
125      */
126     @IntRange(from = 0, to = 20000)
getProximityNearCm()127     public int getProximityNearCm() {
128         return mProximityNearCm;
129     }
130 
131     /**
132      * Returns the far proximity threshold in centimeters.
133      *
134      * @return the far proximity in centimeters.
135      */
136     @IntRange(from = 0, to = 20000)
getProximityFarCm()137     public int getProximityFarCm() {
138         return mProximityFarCm;
139     }
140 
141     /** Builder for {@link DataNotificationConfig} */
142     public static final class Builder {
143         @NotificationConfigType
144         private int mNotificationConfigType = NOTIFICATION_CONFIG_ENABLE;
145         private int mProximityNearCm = 0;
146         private int mProximityFarCm = 20_000;
147 
148         /**
149          * Sets the notification configuration type.
150          *  <p> defaults to {@link NotificationConfigType#NOTIFICATION_CONFIG_ENABLE}
151          *
152          * @param config The notification configuration type to set.
153          * @return this Builder instance.
154          */
155         @NonNull
setNotificationConfigType(@otificationConfigType int config)156         public Builder setNotificationConfigType(@NotificationConfigType int config) {
157             mNotificationConfigType = config;
158             return this;
159         }
160 
161         /**
162          * Sets the near proximity threshold in centimeters.
163          * <p> defaults to 0 cm.
164          *
165          * @param proximityCm The near proximity to set, in centimeters.
166          * @return this Builder instance.
167          */
168         @NonNull
setProximityNearCm(@ntRangefrom = 0, to = 20000) int proximityCm)169         public Builder setProximityNearCm(@IntRange(from = 0, to = 20000) int proximityCm) {
170             mProximityNearCm = proximityCm;
171             return this;
172         }
173 
174         /**
175          * Sets the far proximity threshold in centimeters.
176          * <p> defaults to 20000 cm.
177          *
178          * @param proximityCm The far proximity to set, in centimeters.
179          * @return this Builder instance.
180          */
181         @NonNull
setProximityFarCm(@ntRangefrom = 0, to = 20000) int proximityCm)182         public Builder setProximityFarCm(@IntRange(from = 0, to = 20000) int proximityCm) {
183             mProximityFarCm = proximityCm;
184             return this;
185         }
186 
187         /**
188          * Builds a new instance of {@link DataNotificationConfig}.
189          *
190          * @return a new {@link DataNotificationConfig} instance created using the current state of
191          * the builder.
192          */
193         @NonNull
build()194         public DataNotificationConfig build() {
195             return new DataNotificationConfig(this);
196         }
197     }
198 
199     @Override
toString()200     public String toString() {
201         return "DataNotificationConfig{ "
202                 + "mNotificationConfigType="
203                 + mNotificationConfigType
204                 + ", mProximityNearCm="
205                 + mProximityNearCm
206                 + ", mProximityFarCm="
207                 + mProximityFarCm
208                 + " }";
209     }
210 
211     /**
212      * @hide
213      */
214     @Override
equals(Object o)215     public boolean equals(Object o) {
216         if (this == o) return true;
217         if (!(o instanceof DataNotificationConfig that)) return false;
218         return mNotificationConfigType == that.mNotificationConfigType
219                 && mProximityNearCm == that.mProximityNearCm
220                 && mProximityFarCm == that.mProximityFarCm;
221     }
222 
223     /**
224      * @hide
225      */
226     @Override
hashCode()227     public int hashCode() {
228         return Objects.hash(mNotificationConfigType, mProximityNearCm, mProximityFarCm);
229     }
230 }
231