1 /* 2 * Copyright (C) 2023 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.radar; 18 19 import android.os.PersistableBundle; 20 import android.uwb.UwbManager; 21 22 import com.google.uwb.support.base.FlagEnum; 23 24 import java.util.Collection; 25 import java.util.EnumSet; 26 27 /** 28 * Defines parameters for Radar capability reports 29 * 30 * <p>This is returned as a bundle from the service API {@link UwbManager#getSpecificationInfo}. 31 */ 32 public class RadarSpecificationParams extends RadarParams { 33 private static final int BUNDLE_VERSION_1 = 1; 34 private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1; 35 36 private final EnumSet<RadarCapabilityFlag> mRadarCapabilities; 37 38 private static final String KEY_RADAR_CAPABILITIES = "radar_capabilities"; 39 RadarSpecificationParams(EnumSet<RadarCapabilityFlag> radarCapabilities)40 private RadarSpecificationParams(EnumSet<RadarCapabilityFlag> radarCapabilities) { 41 mRadarCapabilities = radarCapabilities; 42 } 43 44 @Override getBundleVersion()45 protected int getBundleVersion() { 46 return BUNDLE_VERSION_CURRENT; 47 } 48 49 @Override toBundle()50 public PersistableBundle toBundle() { 51 PersistableBundle bundle = super.toBundle(); 52 bundle.putInt(KEY_RADAR_CAPABILITIES, FlagEnum.toInt(mRadarCapabilities)); 53 return bundle; 54 } 55 56 /** Unpack the {@link PersistableBundle} to a {@link RadarSpecificationParams} */ fromBundle(PersistableBundle bundle)57 public static RadarSpecificationParams fromBundle(PersistableBundle bundle) { 58 if (!isCorrectProtocol(bundle)) { 59 throw new IllegalArgumentException("Invalid protocol"); 60 } 61 62 switch (getBundleVersion(bundle)) { 63 case BUNDLE_VERSION_1: 64 return parseVersion1(bundle); 65 default: 66 throw new IllegalArgumentException("Invalid bundle version"); 67 } 68 } 69 parseVersion1(PersistableBundle bundle)70 private static RadarSpecificationParams parseVersion1(PersistableBundle bundle) { 71 return new RadarSpecificationParams.Builder() 72 .setRadarCapabilities( 73 FlagEnum.toEnumSet( 74 bundle.getInt(KEY_RADAR_CAPABILITIES), 75 RadarCapabilityFlag.values())) 76 .build(); 77 } 78 getRadarCapabilities()79 public EnumSet<RadarCapabilityFlag> getRadarCapabilities() { 80 return mRadarCapabilities; 81 } 82 83 /** Builder */ 84 public static final class Builder { 85 private final EnumSet<RadarCapabilityFlag> mRadarCapabilities = 86 EnumSet.noneOf(RadarCapabilityFlag.class); 87 88 /** Adds a collection of {@link RadarCapabilityFlag} */ setRadarCapabilities(Collection<RadarCapabilityFlag> radarCapabilities)89 public Builder setRadarCapabilities(Collection<RadarCapabilityFlag> radarCapabilities) { 90 mRadarCapabilities.addAll(radarCapabilities); 91 return this; 92 } 93 94 /** Adds a single {@link RadarCapabilityFlag} */ addRadarCapability(RadarCapabilityFlag radarCapability)95 public Builder addRadarCapability(RadarCapabilityFlag radarCapability) { 96 mRadarCapabilities.add(radarCapability); 97 return this; 98 } 99 100 /** Build {@link RadarSpecificationParams} */ build()101 public RadarSpecificationParams build() { 102 return new RadarSpecificationParams(mRadarCapabilities); 103 } 104 } 105 } 106