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.oemextension; 18 19 import android.os.PersistableBundle; 20 import android.uwb.UwbManager; 21 22 import com.google.uwb.support.base.RequiredParam; 23 24 /** 25 * Session config parameters for oem extension callback. 26 * 27 * <p> This is passed as a bundle to oem extension API 28 * {@link UwbManager.UwbOemExtensionCallback#onSessionConfigurationComplete(PersistableBundle)} 29 */ 30 public class SessionConfigParams { 31 32 private static final int BUNDLE_VERSION_1 = 1; 33 private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1; 34 private final long mSessionId; 35 private final int mSessionToken; 36 private final PersistableBundle mFiraOpenSessionParamsBundle; 37 38 public static final String KEY_BUNDLE_VERSION = "bundle_version"; 39 public static final String SESSION_ID = "session_id"; 40 public static final String SESSION_TOKEN = "session_token"; 41 public static final String OPEN_SESSION_PARAMS_BUNDLE = "open_session_params_bundle"; 42 SessionConfigParams(long sessionId, int sessionToken, PersistableBundle firaOpenSessionParamsBundle)43 public SessionConfigParams(long sessionId, int sessionToken, 44 PersistableBundle firaOpenSessionParamsBundle) { 45 mSessionId = sessionId; 46 mSessionToken = sessionToken; 47 mFiraOpenSessionParamsBundle = firaOpenSessionParamsBundle; 48 } 49 getBundleVersion()50 public static int getBundleVersion() { 51 return BUNDLE_VERSION_CURRENT; 52 } 53 getSessionId()54 public long getSessionId() { 55 return mSessionId; 56 } 57 getSessionToken()58 public int getSessionToken() { 59 return mSessionToken; 60 } 61 getFiraOpenSessionParamsBundle()62 public PersistableBundle getFiraOpenSessionParamsBundle() { 63 return mFiraOpenSessionParamsBundle; 64 } 65 toBundle()66 public PersistableBundle toBundle() { 67 PersistableBundle bundle = new PersistableBundle(); 68 bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion()); 69 bundle.putLong(SESSION_ID, mSessionId); 70 bundle.putInt(SESSION_TOKEN, mSessionToken); 71 bundle.putPersistableBundle(OPEN_SESSION_PARAMS_BUNDLE, mFiraOpenSessionParamsBundle); 72 return bundle; 73 } 74 isSessionConfigParams(PersistableBundle bundle)75 public static boolean isSessionConfigParams(PersistableBundle bundle) { 76 return bundle.containsKey(OPEN_SESSION_PARAMS_BUNDLE); 77 } 78 fromBundle(PersistableBundle bundle)79 public static SessionConfigParams fromBundle(PersistableBundle bundle) { 80 switch (bundle.getInt(KEY_BUNDLE_VERSION)) { 81 case BUNDLE_VERSION_1: 82 return parseVersion1(bundle); 83 default: 84 throw new IllegalArgumentException("Invalid bundle version"); 85 } 86 } 87 parseVersion1(PersistableBundle bundle)88 private static SessionConfigParams parseVersion1(PersistableBundle bundle) { 89 return new SessionConfigParams.Builder() 90 .setSessionId(bundle.getLong(SESSION_ID)) 91 .setSessiontoken(bundle.getInt(SESSION_TOKEN)) 92 .setOpenSessionParamsBundle(bundle.getPersistableBundle(OPEN_SESSION_PARAMS_BUNDLE)) 93 .build(); 94 } 95 96 /** Builder */ 97 public static class Builder { 98 private final RequiredParam<Long> mSessionId = new RequiredParam<>(); 99 private int mSessionToken = 0; 100 private RequiredParam<PersistableBundle> mOpenSessionParamsBundle = new RequiredParam<>(); 101 setSessionId(long sessionId)102 public SessionConfigParams.Builder setSessionId(long sessionId) { 103 mSessionId.set(sessionId); 104 return this; 105 } 106 setSessiontoken(int sessionToken)107 public SessionConfigParams.Builder setSessiontoken(int sessionToken) { 108 mSessionToken = sessionToken; 109 return this; 110 } 111 setOpenSessionParamsBundle(PersistableBundle bundle)112 public SessionConfigParams.Builder setOpenSessionParamsBundle(PersistableBundle bundle) { 113 mOpenSessionParamsBundle.set(bundle); 114 return this; 115 } 116 build()117 public SessionConfigParams build() { 118 return new SessionConfigParams(mSessionId.get(), mSessionToken, 119 mOpenSessionParamsBundle.get()); 120 } 121 } 122 } 123