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 com.google.uwb.support.base.RequiredParam; 22 import com.google.uwb.support.fira.FiraParams.ServiceID; 23 24 /** UWB service configuration for FiRa profile. */ 25 public class ServiceProfile { 26 private static final int BUNDLE_VERSION_1 = 1; 27 private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1; 28 29 @ServiceID 30 private final int mServiceID; 31 public static final String KEY_BUNDLE_VERSION = "bundle_version"; 32 public static final String SERVICE_ID = "service_id"; 33 getBundleVersion()34 public static int getBundleVersion() { 35 return BUNDLE_VERSION_CURRENT; 36 } 37 38 @ServiceID getServiceID()39 public int getServiceID() { 40 return mServiceID; 41 } 42 ServiceProfile(int serviceID)43 public ServiceProfile(int serviceID) { 44 mServiceID = serviceID; 45 } 46 toBundle()47 public PersistableBundle toBundle() { 48 PersistableBundle bundle = new PersistableBundle(); 49 bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion()); 50 bundle.putInt(SERVICE_ID, mServiceID); 51 return bundle; 52 } 53 fromBundle(PersistableBundle bundle)54 public static ServiceProfile fromBundle(PersistableBundle bundle) { 55 switch (bundle.getInt(KEY_BUNDLE_VERSION)) { 56 case BUNDLE_VERSION_1: 57 return parseVersion1(bundle); 58 59 default: 60 throw new IllegalArgumentException("Invalid bundle version"); 61 } 62 } 63 parseVersion1(PersistableBundle bundle)64 private static ServiceProfile parseVersion1(PersistableBundle bundle) { 65 return new ServiceProfile.Builder() 66 .setServiceID(bundle.getInt(SERVICE_ID)) 67 .build(); 68 } 69 70 /** Builder */ 71 public static class Builder { 72 private final RequiredParam<Integer> mServiceID = new RequiredParam<>(); 73 setServiceID(int serviceID)74 public ServiceProfile.Builder setServiceID(int serviceID) { 75 mServiceID.set(serviceID); 76 return this; 77 } 78 build()79 public ServiceProfile build() { 80 return new ServiceProfile( 81 mServiceID.get()); 82 } 83 } 84 } 85