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.RangingReport;
21 
22 import androidx.annotation.Nullable;
23 
24 /**
25  * Ranging report metadata for post-processing with oem extension
26  *
27  * <p> This is passed as bundle with RangingReport
28  * {@link RangingReport#getRangingReportMetadata()}
29  */
30 public class RangingReportMetadata {
31     private static final int BUNDLE_VERSION_1 = 1;
32     private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1;
33 
34     public static final String KEY_BUNDLE_VERSION = "bundle_version";
35     public static final String SESSION_ID = "session_id";
36     public static final String RAW_NTF_DATA = "raw_ntf_data";
37 
38     private final long mSessionId;
39     private final byte[] mRawNtfData;
40 
getBundleVersion()41     public static int getBundleVersion() {
42         return BUNDLE_VERSION_CURRENT;
43     }
44 
getSessionId()45     public long getSessionId() {
46         return mSessionId;
47     }
48 
getRawNtfData()49     public byte[] getRawNtfData() {
50         return mRawNtfData;
51     }
52 
RangingReportMetadata(long sessionId, byte[] rawNtfData)53     private RangingReportMetadata(long sessionId, byte[] rawNtfData) {
54         mSessionId = sessionId;
55         mRawNtfData = rawNtfData;
56     }
57 
58     @Nullable
byteArrayToIntArray(@ullable byte[] bytes)59     private static int[] byteArrayToIntArray(@Nullable byte[] bytes) {
60         if (bytes == null) {
61             return null;
62         }
63         int[] values = new int[bytes.length];
64         for (int i = 0; i < values.length; i++) {
65             values[i] = (bytes[i]);
66         }
67         return values;
68     }
69 
70     @Nullable
intArrayToByteArray(@ullable int[] values)71     private static byte[] intArrayToByteArray(@Nullable int[] values) {
72         if (values == null) {
73             return null;
74         }
75         byte[] bytes = new byte[values.length];
76         for (int i = 0; i < values.length; i++) {
77             bytes[i] = (byte) values[i];
78         }
79         return bytes;
80     }
81 
toBundle()82     public PersistableBundle toBundle() {
83         PersistableBundle bundle = new PersistableBundle();
84         bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion());
85         bundle.putLong(SESSION_ID, mSessionId);
86         bundle.putIntArray(RAW_NTF_DATA, byteArrayToIntArray(mRawNtfData));
87         return bundle;
88     }
89 
fromBundle(PersistableBundle bundle)90     public static RangingReportMetadata fromBundle(PersistableBundle bundle) {
91         switch (bundle.getInt(KEY_BUNDLE_VERSION)) {
92             case BUNDLE_VERSION_1:
93                 return parseVersion1(bundle);
94             default:
95                 throw new IllegalArgumentException("Invalid bundle version");
96         }
97     }
98 
parseVersion1(PersistableBundle bundle)99     private static RangingReportMetadata parseVersion1(PersistableBundle bundle) {
100         return new RangingReportMetadata.Builder()
101                 .setSessionId(bundle.getLong(SESSION_ID))
102                 .setRawNtfData(intArrayToByteArray(bundle.getIntArray(RAW_NTF_DATA)))
103                 .build();
104     }
105 
106     /** Builder */
107     public static class Builder {
108         private long mSessionId;
109         private byte[] mRawNtfData;
110 
setSessionId(long sessionId)111         public RangingReportMetadata.Builder setSessionId(long sessionId) {
112             mSessionId = sessionId;
113             return this;
114         }
115 
setRawNtfData(byte[] rawNtfData)116         public RangingReportMetadata.Builder setRawNtfData(byte[] rawNtfData) {
117             mRawNtfData = rawNtfData;
118             return this;
119         }
120 
build()121         public RangingReportMetadata build() {
122             return new RangingReportMetadata(mSessionId, mRawNtfData);
123         }
124     }
125 }
126