1 /* 2 * Copyright (C) 2021 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.base; 18 19 import android.os.Build.VERSION_CODES; 20 import android.os.PersistableBundle; 21 22 import androidx.annotation.RequiresApi; 23 24 /** Provides common parameter operations. */ 25 @RequiresApi(VERSION_CODES.LOLLIPOP) 26 public abstract class Params { 27 private static final String KEY_BUNDLE_VERSION = "bundle_version"; 28 protected static final int BUNDLE_VERSION_UNKNOWN = -1; 29 30 protected static final String KEY_PROTOCOL_NAME = "protocol_name"; 31 protected static final String PROTOCOL_NAME_UNKNOWN = "unknown"; 32 toBundle()33 public PersistableBundle toBundle() { 34 PersistableBundle bundle = new PersistableBundle(); 35 bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion()); 36 bundle.putString(KEY_PROTOCOL_NAME, getProtocolName()); 37 return bundle; 38 } 39 getProtocolName()40 public abstract String getProtocolName(); 41 getBundleVersion()42 protected abstract int getBundleVersion(); 43 getBundleVersion(PersistableBundle bundle)44 public static int getBundleVersion(PersistableBundle bundle) { 45 return bundle.getInt(KEY_BUNDLE_VERSION, BUNDLE_VERSION_UNKNOWN); 46 } 47 isProtocol(PersistableBundle bundle, String protocol)48 public static boolean isProtocol(PersistableBundle bundle, String protocol) { 49 return bundle.getString(KEY_PROTOCOL_NAME, PROTOCOL_NAME_UNKNOWN).equals(protocol); 50 } 51 } 52