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.profile; 18 19 import android.os.PersistableBundle; 20 21 import java.util.Optional; 22 import java.util.UUID; 23 24 /* UWB return status */ 25 public class UuidBundleWrapper { 26 27 private static final int BUNDLE_VERSION_1 = 1; 28 private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1; 29 30 private final Optional<UUID> mServiceInstanceID; 31 32 public static final String KEY_BUNDLE_VERSION = "bundle_version"; 33 public static final String SERVICE_INSTANCE_ID = "service_instance_id"; 34 UuidBundleWrapper(Optional<UUID> serviceInstanceID)35 public UuidBundleWrapper(Optional<UUID> serviceInstanceID) { 36 mServiceInstanceID = serviceInstanceID; 37 } 38 getBundleVersion()39 public static int getBundleVersion() { 40 return BUNDLE_VERSION_CURRENT; 41 } 42 getServiceInstanceID()43 public Optional<UUID> getServiceInstanceID() { 44 return mServiceInstanceID; 45 } 46 toBundle()47 public PersistableBundle toBundle() { 48 PersistableBundle bundle = new PersistableBundle(); 49 bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion()); 50 if (mServiceInstanceID.isPresent()) { 51 bundle.putString(SERVICE_INSTANCE_ID, mServiceInstanceID.get().toString()); 52 } 53 return bundle; 54 } 55 isUuidBundle(PersistableBundle bundle)56 public static boolean isUuidBundle(PersistableBundle bundle) { 57 return bundle.containsKey(SERVICE_INSTANCE_ID); 58 } 59 fromBundle(PersistableBundle bundle)60 public static UuidBundleWrapper fromBundle(PersistableBundle bundle) { 61 switch (bundle.getInt(KEY_BUNDLE_VERSION)) { 62 case BUNDLE_VERSION_1: 63 return parseVersion1(bundle); 64 65 default: 66 throw new IllegalArgumentException("Invalid bundle version"); 67 } 68 } 69 parseVersion1(PersistableBundle bundle)70 private static UuidBundleWrapper parseVersion1(PersistableBundle bundle) { 71 UuidBundleWrapper.Builder builder = new UuidBundleWrapper.Builder(); 72 if (bundle.containsKey(SERVICE_INSTANCE_ID)) { 73 builder.setServiceInstanceID(Optional.of(UUID.fromString( 74 bundle.getString(SERVICE_INSTANCE_ID)))); 75 } 76 return builder.build(); 77 } 78 79 /** Builder */ 80 public static class Builder { 81 private Optional<UUID> mServiceInstanceID = Optional.empty(); 82 setServiceInstanceID(Optional<UUID> serviceInstanceID)83 public UuidBundleWrapper.Builder setServiceInstanceID(Optional<UUID> serviceInstanceID) { 84 mServiceInstanceID = serviceInstanceID; 85 return this; 86 } 87 build()88 public UuidBundleWrapper build() { 89 return new UuidBundleWrapper(mServiceInstanceID); 90 } 91 } 92 } 93