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.oob; 18 19 import android.annotation.FlaggedApi; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.ranging.RangingDevice; 23 24 import androidx.annotation.NonNull; 25 26 import com.android.ranging.flags.Flags; 27 28 import java.util.Objects; 29 30 /** 31 * Represents a handle to a ranging device, containing information about the device 32 * and a transport handle for out-of-band communication. 33 * 34 */ 35 @FlaggedApi(Flags.FLAG_RANGING_STACK_ENABLED) 36 public final class DeviceHandle implements Parcelable { 37 38 private final RangingDevice mRangingDevice; 39 40 private final TransportHandle mTransportHandle; 41 DeviceHandle(Builder builder)42 private DeviceHandle(Builder builder) { 43 mRangingDevice = builder.mRangingDevice; 44 mTransportHandle = builder.mTransportHandle; 45 } 46 DeviceHandle(Parcel in)47 private DeviceHandle(Parcel in) { 48 mRangingDevice = in.readParcelable(RangingDevice.class.getClassLoader()); 49 // Not need in service layer. 50 mTransportHandle = null; 51 } 52 53 @NonNull 54 public static final Creator<DeviceHandle> CREATOR = new Creator<DeviceHandle>() { 55 @Override 56 public DeviceHandle createFromParcel(Parcel in) { 57 return new DeviceHandle(in); 58 } 59 60 @Override 61 public DeviceHandle[] newArray(int size) { 62 return new DeviceHandle[size]; 63 } 64 }; 65 66 /** 67 * Returns the ranging device associated with this handle. 68 * 69 * @return The {@link RangingDevice} instance. 70 */ 71 @NonNull getRangingDevice()72 public RangingDevice getRangingDevice() { 73 return mRangingDevice; 74 } 75 76 /** 77 * Returns the transport handle, if set, for communication. 78 * 79 * @return The {@link TransportHandle} instance. 80 */ 81 @NonNull getTransportHandle()82 public TransportHandle getTransportHandle() { 83 return mTransportHandle; 84 } 85 86 @Override describeContents()87 public int describeContents() { 88 return 0; 89 } 90 91 @Override writeToParcel(@onNull Parcel dest, int flags)92 public void writeToParcel(@NonNull Parcel dest, int flags) { 93 dest.writeParcelable(mRangingDevice, flags); 94 } 95 96 /** 97 * Builder class for creating instances of {@link DeviceHandle}. 98 */ 99 public static final class Builder { 100 private RangingDevice mRangingDevice; 101 private TransportHandle mTransportHandle; 102 103 /** 104 * Constructs a new {@link Builder} with the required {@link RangingDevice} 105 * and {@link TransportHandle}. 106 * 107 * @param rangingDevice the {@link RangingDevice} 108 * @param transportHandle Implementation of {@link TransportHandle} for sending/receiving 109 * OOB data from peer 110 * @throws NullPointerException if either parameter is {@code null}. 111 */ Builder(@onNull RangingDevice rangingDevice, @NonNull TransportHandle transportHandle)112 public Builder(@NonNull RangingDevice rangingDevice, 113 @NonNull TransportHandle transportHandle) { 114 Objects.requireNonNull(rangingDevice); 115 Objects.requireNonNull(transportHandle); 116 mRangingDevice = rangingDevice; 117 mTransportHandle = transportHandle; 118 } 119 120 /** 121 * Builds and returns a new {@link DeviceHandle} instance using the 122 * parameters provided to this builder. 123 * 124 * @return a newly created {@link DeviceHandle} instance. 125 */ 126 @NonNull build()127 public DeviceHandle build() { 128 return new DeviceHandle(this); 129 } 130 } 131 132 @Override toString()133 public String toString() { 134 return "DeviceHandle{ " 135 + "mRangingDevice=" 136 + mRangingDevice 137 + ", mTransportHandle=" 138 + mTransportHandle + " }"; 139 } 140 } 141