1 /* 2 * Copyright 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.bluetooth.le; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.RetentionPolicy; 27 import java.util.Objects; 28 29 /** 30 * Method of distance measurement. A list of this class will be returned by 31 * {@link DistanceMeasurementManager#getSupportedMethods()} to indicate the supported methods and 32 * their capability about angle measurement. 33 * 34 * @hide 35 */ 36 @SystemApi 37 public final class DistanceMeasurementMethod implements Parcelable { 38 39 private final int mId; 40 private final boolean mIsAzimuthAngleSupported; 41 private final boolean mIsAltitudeAngleSupported; 42 43 /** 44 * @hide 45 */ 46 @Retention(RetentionPolicy.SOURCE) 47 @IntDef(value = { 48 DISTANCE_MEASUREMENT_METHOD_AUTO, 49 DISTANCE_MEASUREMENT_METHOD_RSSI}) 50 @interface DistanceMeasurementMethodId {} 51 52 /** 53 * Choose method automatically, Bluetooth will use the most accurate method that local 54 * device supported to measurement distance. 55 * 56 * @hide 57 */ 58 @SystemApi 59 public static final int DISTANCE_MEASUREMENT_METHOD_AUTO = 0; 60 61 /** 62 * Use remote RSSI and transmit power to measure the distance. 63 * 64 * @hide 65 */ 66 @SystemApi 67 public static final int DISTANCE_MEASUREMENT_METHOD_RSSI = 1; 68 DistanceMeasurementMethod(int id, boolean isAzimuthAngleSupported, boolean isAltitudeAngleSupported)69 private DistanceMeasurementMethod(int id, boolean isAzimuthAngleSupported, 70 boolean isAltitudeAngleSupported) { 71 mId = id; 72 mIsAzimuthAngleSupported = isAzimuthAngleSupported; 73 mIsAltitudeAngleSupported = isAltitudeAngleSupported; 74 } 75 76 /** 77 * Id of the method used for {@link DistanceMeasurementParams.Builder#setMethod(int)} 78 * 79 * @return id of the method 80 * 81 * @hide 82 */ 83 @SystemApi getId()84 public @DistanceMeasurementMethodId double getId() { 85 return mId; 86 } 87 88 /** 89 * Checks whether the azimuth angle is supported for this method. 90 * 91 * @return true if azimuth angle is supported, false otherwise 92 * 93 * @hide 94 */ 95 @SystemApi isAzimuthAngleSupported()96 public boolean isAzimuthAngleSupported() { 97 return mIsAzimuthAngleSupported; 98 } 99 100 /** 101 * Checks whether the altitude angle is supported for this method. 102 * 103 * @return true if altitude angle is supported, false otherwise 104 * 105 * @hide 106 */ 107 @SystemApi isAltitudeAngleSupported()108 public boolean isAltitudeAngleSupported() { 109 return mIsAltitudeAngleSupported; 110 } 111 112 /** 113 * {@inheritDoc} 114 * @hide 115 */ 116 @Override describeContents()117 public int describeContents() { 118 return 0; 119 } 120 121 /** 122 * {@inheritDoc} 123 * @hide 124 */ 125 @Override writeToParcel(Parcel out, int flags)126 public void writeToParcel(Parcel out, int flags) { 127 out.writeInt(mId); 128 out.writeBoolean(mIsAzimuthAngleSupported); 129 out.writeBoolean(mIsAltitudeAngleSupported); 130 } 131 132 /** @hide **/ 133 @Override toString()134 public String toString() { 135 return "DistanceMeasurementMethod[" 136 + "id: " + mId 137 + ", isAzimuthAngleSupported: " + mIsAzimuthAngleSupported 138 + ", isAltitudeAngleSupported: " + mIsAltitudeAngleSupported 139 + "]"; 140 } 141 142 @Override equals(Object o)143 public boolean equals(Object o) { 144 if (o == null) return false; 145 146 if (!(o instanceof DistanceMeasurementMethod)) return false; 147 148 final DistanceMeasurementMethod u = (DistanceMeasurementMethod) o; 149 150 if (mId != u.getId()) { 151 return false; 152 } 153 154 return true; 155 } 156 157 @Override hashCode()158 public int hashCode() { 159 return Objects.hash(mId); 160 } 161 162 /** 163 * A {@link Parcelable.Creator} to create {@link DistanceMeasurementMethod} from parcel. 164 * 165 */ 166 public static final @NonNull Parcelable.Creator<DistanceMeasurementMethod> CREATOR = 167 new Parcelable.Creator<DistanceMeasurementMethod>() { 168 @Override 169 public @NonNull DistanceMeasurementMethod createFromParcel(@NonNull Parcel in) { 170 return new Builder(in.readInt()).setAzimuthAngleSupported(in.readBoolean()) 171 .setAltitudeAngleSupported(in.readBoolean()).build(); 172 } 173 174 @Override 175 public @NonNull DistanceMeasurementMethod[] newArray(int size) { 176 return new DistanceMeasurementMethod[size]; 177 } 178 }; 179 180 /** 181 * Builder for {@link DistanceMeasurementMethod}. 182 * 183 * @hide 184 */ 185 @SystemApi 186 public static final class Builder { 187 private int mId; 188 private boolean mIsAzimuthAngleSupported = false; 189 private boolean mIsAltitudeAngleSupported = false; 190 191 /** 192 * Constructor of the Builder. 193 * 194 * @param id id of the method 195 */ Builder(@istanceMeasurementMethodId int id)196 public Builder(@DistanceMeasurementMethodId int id) { 197 switch (id) { 198 case DISTANCE_MEASUREMENT_METHOD_AUTO: 199 case DISTANCE_MEASUREMENT_METHOD_RSSI: 200 mId = id; 201 break; 202 default: 203 throw new IllegalArgumentException("unknown method id " + id); 204 } 205 } 206 207 /** 208 * Set if azimuth angle supported or not. 209 * 210 * @param supported {@code true} if azimuth angle supported, {@code false} otherwise 211 * 212 * @hide 213 */ 214 @SystemApi 215 @NonNull setAzimuthAngleSupported(boolean supported)216 public Builder setAzimuthAngleSupported(boolean supported) { 217 mIsAzimuthAngleSupported = supported; 218 return this; 219 } 220 221 /** 222 * Set if altitude angle supported or not. 223 * 224 * @param supported {@code true} if altitude angle supported, {@code false} otherwise 225 * 226 * @hide 227 */ 228 @SystemApi 229 @NonNull setAltitudeAngleSupported(boolean supported)230 public Builder setAltitudeAngleSupported(boolean supported) { 231 mIsAltitudeAngleSupported = supported; 232 return this; 233 } 234 235 /** 236 * Builds the {@link DistanceMeasurementMethod} object. 237 * 238 * @hide 239 */ 240 @SystemApi 241 @NonNull build()242 public DistanceMeasurementMethod build() { 243 return new DistanceMeasurementMethod(mId, mIsAzimuthAngleSupported, 244 mIsAltitudeAngleSupported); 245 } 246 } 247 } 248