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.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 status for oem extension callback 26 * 27 * <p> This is passed as a bundle to oem extension API 28 * {@link UwbManager.UwbOemExtensionCallback#onSessionStatusNotificationReceived(PersistableBundle)} 29 */ 30 public class SessionStatus { 31 private static final int BUNDLE_VERSION_1 = 1; 32 private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1; 33 34 private final long mSessionId; 35 private final int mState; 36 private final int mReasonCode; 37 private final String mAppPackageName; 38 private final int mSessionToken; 39 public static final String KEY_BUNDLE_VERSION = "bundle_version"; 40 public static final String SESSION_ID = "session_id"; 41 public static final String STATE = "state"; 42 public static final String REASON_CODE = "reason_code"; 43 public static final String APP_PACKAGE_NAME = "app_package_name"; 44 public static final String SESSION_TOKEN = "session_token"; 45 getBundleVersion()46 public static int getBundleVersion() { 47 return BUNDLE_VERSION_CURRENT; 48 } 49 getSessionId()50 public long getSessionId() { 51 return mSessionId; 52 } 53 getState()54 public int getState() { 55 return mState; 56 } 57 getReasonCode()58 public int getReasonCode() { 59 return mReasonCode; 60 } 61 getAppPackageName()62 public String getAppPackageName() { 63 return mAppPackageName; 64 } 65 66 // Gets session handle for FiRa 2.0+ device or gets session id for FiRa 1.0 device. getSessionToken()67 public int getSessionToken() { 68 return mSessionToken; 69 } 70 SessionStatus(long sessionId, int state, int reasonCode, String appPackageName, int sessionToken)71 private SessionStatus(long sessionId, int state, int reasonCode, String appPackageName, 72 int sessionToken) { 73 mSessionId = sessionId; 74 mState = state; 75 mReasonCode = reasonCode; 76 mAppPackageName = appPackageName; 77 mSessionToken = sessionToken; 78 } 79 toBundle()80 public PersistableBundle toBundle() { 81 PersistableBundle bundle = new PersistableBundle(); 82 bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion()); 83 bundle.putLong(SESSION_ID, mSessionId); 84 bundle.putInt(STATE, mState); 85 bundle.putInt(REASON_CODE, mReasonCode); 86 bundle.putString(APP_PACKAGE_NAME, mAppPackageName); 87 bundle.putInt(SESSION_TOKEN, mSessionToken); 88 return bundle; 89 } 90 fromBundle(PersistableBundle bundle)91 public static SessionStatus fromBundle(PersistableBundle bundle) { 92 switch (bundle.getInt(KEY_BUNDLE_VERSION)) { 93 case BUNDLE_VERSION_1: 94 return parseVersion1(bundle); 95 default: 96 throw new IllegalArgumentException("Invalid bundle version"); 97 } 98 } 99 parseVersion1(PersistableBundle bundle)100 private static SessionStatus parseVersion1(PersistableBundle bundle) { 101 return new SessionStatus.Builder() 102 .setSessionId(bundle.getLong(SESSION_ID)) 103 .setState(bundle.getInt(STATE)) 104 .setReasonCode(bundle.getInt(REASON_CODE)) 105 .setAppPackageName(bundle.getString(APP_PACKAGE_NAME)) 106 .setSessiontoken(bundle.getInt(SESSION_TOKEN)) 107 .build(); 108 } 109 110 /** Builder */ 111 public static class Builder { 112 private final RequiredParam<Long> mSessionId = new RequiredParam<>(); 113 private final RequiredParam<Integer> mState = new RequiredParam<>(); 114 private final RequiredParam<Integer> mReasonCode = new RequiredParam<>(); 115 private String mAppPackageName = "UnknownPackageName"; 116 private int mSessionToken = 0; 117 setSessionId(long sessionId)118 public SessionStatus.Builder setSessionId(long sessionId) { 119 mSessionId.set(sessionId); 120 return this; 121 } 122 setState(int state)123 public SessionStatus.Builder setState(int state) { 124 mState.set(state); 125 return this; 126 } 127 setReasonCode(int reasonCode)128 public SessionStatus.Builder setReasonCode(int reasonCode) { 129 mReasonCode.set(reasonCode); 130 return this; 131 } 132 setAppPackageName(String appPackageName)133 public SessionStatus.Builder setAppPackageName(String appPackageName) { 134 mAppPackageName = appPackageName; 135 return this; 136 } 137 setSessiontoken(int sessionToken)138 public SessionStatus.Builder setSessiontoken(int sessionToken) { 139 mSessionToken = sessionToken; 140 return this; 141 } 142 build()143 public SessionStatus build() { 144 return new SessionStatus( 145 mSessionId.get(), 146 mState.get(), 147 mReasonCode.get(), 148 mAppPackageName, 149 mSessionToken); 150 } 151 } 152 } 153