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     private final String mProtocolName;
40     public static final String KEY_BUNDLE_VERSION = "bundle_version";
41     public static final String SESSION_ID = "session_id";
42     public static final String STATE = "state";
43     public static final String REASON_CODE = "reason_code";
44     public static final String APP_PACKAGE_NAME = "app_package_name";
45     public static final String SESSION_TOKEN = "session_token";
46     public static final String PROTOCOL_NAME = "protocol_name";
47 
getBundleVersion()48     public static int getBundleVersion() {
49         return BUNDLE_VERSION_CURRENT;
50     }
51 
getSessionId()52     public long getSessionId() {
53         return mSessionId;
54     }
55 
getState()56     public int getState() {
57         return mState;
58     }
59 
getReasonCode()60     public int getReasonCode() {
61         return mReasonCode;
62     }
63 
getAppPackageName()64     public String getAppPackageName() {
65         return mAppPackageName;
66     }
67 
68     // Gets session handle for FiRa 2.0+ device or gets session id for FiRa 1.0 device.
getSessionToken()69     public int getSessionToken() {
70         return mSessionToken;
71     }
72 
getProtocolName()73     public String getProtocolName() {
74         return mProtocolName;
75     }
76 
SessionStatus(long sessionId, int state, int reasonCode, String appPackageName, int sessionToken, String protocolName)77     private SessionStatus(long sessionId, int state, int reasonCode, String appPackageName,
78             int sessionToken, String protocolName) {
79         mSessionId = sessionId;
80         mState = state;
81         mReasonCode = reasonCode;
82         mAppPackageName = appPackageName;
83         mSessionToken = sessionToken;
84         mProtocolName = protocolName;
85     }
86 
toBundle()87     public PersistableBundle toBundle() {
88         PersistableBundle bundle = new PersistableBundle();
89         bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion());
90         bundle.putLong(SESSION_ID, mSessionId);
91         bundle.putInt(STATE, mState);
92         bundle.putInt(REASON_CODE, mReasonCode);
93         bundle.putString(APP_PACKAGE_NAME, mAppPackageName);
94         bundle.putInt(SESSION_TOKEN, mSessionToken);
95         bundle.putString(PROTOCOL_NAME, mProtocolName);
96         return bundle;
97     }
98 
fromBundle(PersistableBundle bundle)99     public static SessionStatus fromBundle(PersistableBundle bundle) {
100         switch (bundle.getInt(KEY_BUNDLE_VERSION)) {
101             case BUNDLE_VERSION_1:
102                 return parseVersion1(bundle);
103             default:
104                 throw new IllegalArgumentException("Invalid bundle version");
105         }
106     }
107 
parseVersion1(PersistableBundle bundle)108     private static SessionStatus parseVersion1(PersistableBundle bundle) {
109         return new SessionStatus.Builder()
110                 .setSessionId(bundle.getLong(SESSION_ID))
111                 .setState(bundle.getInt(STATE))
112                 .setReasonCode(bundle.getInt(REASON_CODE))
113                 .setAppPackageName(bundle.getString(APP_PACKAGE_NAME))
114                 .setSessiontoken(bundle.getInt(SESSION_TOKEN))
115                 .setProtocolName(bundle.getString(PROTOCOL_NAME, "UnknownProtocolName"))
116                 .build();
117     }
118 
119     /** Builder */
120     public static class Builder {
121         private final RequiredParam<Long> mSessionId = new RequiredParam<>();
122         private final RequiredParam<Integer> mState = new RequiredParam<>();
123         private final RequiredParam<Integer> mReasonCode = new RequiredParam<>();
124         private String mAppPackageName = "UnknownPackageName";
125         private int mSessionToken = 0;
126         private String mProtocolName = "UnknownProtocolName";
127 
setSessionId(long sessionId)128         public SessionStatus.Builder setSessionId(long sessionId) {
129             mSessionId.set(sessionId);
130             return this;
131         }
132 
setState(int state)133         public SessionStatus.Builder setState(int state) {
134             mState.set(state);
135             return this;
136         }
137 
setReasonCode(int reasonCode)138         public SessionStatus.Builder setReasonCode(int reasonCode) {
139             mReasonCode.set(reasonCode);
140             return this;
141         }
142 
setAppPackageName(String appPackageName)143         public SessionStatus.Builder setAppPackageName(String appPackageName) {
144             mAppPackageName = appPackageName;
145             return this;
146         }
147 
setSessiontoken(int sessionToken)148         public SessionStatus.Builder setSessiontoken(int sessionToken) {
149             mSessionToken = sessionToken;
150             return this;
151         }
152 
setProtocolName(String protocolName)153         public SessionStatus.Builder setProtocolName(String protocolName) {
154             mProtocolName = protocolName;
155             return this;
156         }
157 
build()158         public SessionStatus build() {
159             return new SessionStatus(
160                     mSessionId.get(),
161                     mState.get(),
162                     mReasonCode.get(),
163                     mAppPackageName,
164                     mSessionToken,
165                     mProtocolName);
166         }
167     }
168 }
169