1 /*
2  * Copyright (C) 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 com.google.uwb.support.oemextension;
18 
19 import android.os.PersistableBundle;
20 import android.uwb.UwbAddress;
21 import android.uwb.UwbManager;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.google.uwb.support.base.RequiredParam;
26 
27 import java.util.Objects;
28 
29 /**
30  * Check pointed target for oem extension callback
31  *
32  * <p> This is passed as a bundle to oem extension API
33  * {@link UwbManager.UwbOemExtensionCallback#onCheckPointedTarget(PersistableBundle)}
34  */
35 public class AdvertisePointedTarget {
36     private static final int BUNDLE_VERSION_1 = 1;
37     private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1;
38 
39     private final UwbAddress mMacAddress;
40     private final boolean mAdvertisePointingResult;
41     public static final String KEY_BUNDLE_VERSION = "bundle_version";
42     public static final String MAC_ADDRESS = "mac_address";
43     public static final String ADVERTISE_POINTING_RESULT = "advertise_pointing_result";
44 
45 
AdvertisePointedTarget(UwbAddress macAddress, boolean advertisePointingResult)46     public AdvertisePointedTarget(UwbAddress macAddress, boolean advertisePointingResult) {
47         mMacAddress = macAddress;
48         mAdvertisePointingResult = advertisePointingResult;
49     }
50 
getBundleVersion()51     public static int getBundleVersion() {
52         return BUNDLE_VERSION_CURRENT;
53     }
54 
getMacAddress()55     public UwbAddress getMacAddress() {
56         return mMacAddress;
57     }
58 
isAdvertisePointingResult()59     public boolean isAdvertisePointingResult() {
60         return mAdvertisePointingResult;
61     }
62 
63     @Nullable
byteArrayToIntArray(@ullable byte[] bytes)64     private static int[] byteArrayToIntArray(@Nullable byte[] bytes) {
65         if (bytes == null) {
66             return null;
67         }
68         int[] values = new int[bytes.length];
69         for (int i = 0; i < values.length; i++) {
70             values[i] = (bytes[i]);
71         }
72         return values;
73     }
74 
75     @Nullable
intArrayToByteArray(@ullable int[] values)76     private static byte[] intArrayToByteArray(@Nullable int[] values) {
77         if (values == null) {
78             return null;
79         }
80         byte[] bytes = new byte[values.length];
81         for (int i = 0; i < values.length; i++) {
82             bytes[i] = (byte) values[i];
83         }
84         return bytes;
85     }
86 
toBundle()87     public PersistableBundle toBundle() {
88         PersistableBundle bundle = new PersistableBundle();
89         bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion());
90         bundle.putIntArray(MAC_ADDRESS, byteArrayToIntArray(mMacAddress.toBytes()));
91         bundle.putBoolean(ADVERTISE_POINTING_RESULT, mAdvertisePointingResult);
92         return bundle;
93     }
94 
fromBundle(PersistableBundle bundle)95     public static AdvertisePointedTarget fromBundle(PersistableBundle bundle) {
96         switch (bundle.getInt(KEY_BUNDLE_VERSION)) {
97             case BUNDLE_VERSION_1:
98                 return parseVersion1(bundle);
99             default:
100                 throw new IllegalArgumentException("Invalid bundle version");
101         }
102     }
103 
parseVersion1(PersistableBundle bundle)104     private static AdvertisePointedTarget parseVersion1(PersistableBundle bundle) {
105         return new Builder()
106                 .setMacAddress(
107                         Objects.requireNonNull(
108                                 intArrayToByteArray(bundle.getIntArray(MAC_ADDRESS))))
109                 .setAdvertisePointingResult(bundle.getBoolean(ADVERTISE_POINTING_RESULT))
110                 .build();
111     }
112 
113     /** Builder */
114     public static class Builder {
115         private final RequiredParam<UwbAddress> mMacAddress = new RequiredParam<>();
116         private final RequiredParam<Boolean> mAdvertisePointingResult = new RequiredParam<>();
117 
setMacAddress(byte[] macAddress)118         public AdvertisePointedTarget.Builder setMacAddress(byte[] macAddress) {
119             mMacAddress.set(UwbAddress.fromBytes(macAddress));
120             return this;
121         }
122 
setAdvertisePointingResult( boolean advertisePointingResult)123         public AdvertisePointedTarget.Builder setAdvertisePointingResult(
124                 boolean advertisePointingResult) {
125             mAdvertisePointingResult.set(advertisePointingResult);
126             return this;
127         }
128 
build()129         public AdvertisePointedTarget build() {
130             return new AdvertisePointedTarget(
131                     mMacAddress.get(),
132                     mAdvertisePointingResult.get()
133             );
134         }
135     }
136 }
137