• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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.android.phone.testapps.gbatestapp;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.os.RemoteException;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.TelephonyFrameworkInitializer;
24 import android.telephony.TelephonyManager;
25 import android.text.TextUtils;
26 import android.util.Log;
27 
28 import com.android.internal.telephony.ITelephony;
29 
30 import java.util.Locale;
31 
32 /** class to load and save the settings */
33 public class Settings {
34 
35     private static final String TAG = "SETTINGS";
36 
37     private static final String PREF_CARRIER_CONFIG = "pref_carrier_config";
38     private static final String KEY_SERVICE_PACKAGE = "key_service_package";
39     private static final String KEY_RELEASE_TIME = "key_release_time";
40 
41     private static final String PREF_TEST_CONFIG = "pref_test_config";
42     private static final String KEY_APP_TYPE = "key_app_type";
43     private static final String KEY_NAF_URL = "key_naf_url";
44     private static final String KEY_FORCE_BT = "key_force_bt";
45     private static final String KEY_ORG = "org";
46     private static final String KEY_SP_ID = "key_sp_id";
47     private static final String KEY_TLS_CS = "key_tls_cs";
48 
49     private static final String PREF_SERVICE_CONFIG = "pref_carrier_config";
50     private static final String KEY_AUTH_RESULT = "key_auth_result";
51     private static final String KEY_GBA_KEY = "key_gba_key";
52     private static final String KEY_B_TID = "key_b_tid";
53     private static final String KEY_FAIL_REASON = "key_fail_reason";
54 
55     private ITelephony mTelephony;
56     private int mSubId;
57     private String mServicePackageName;
58     private int mReleaseTime;
59     private int mAppType;
60     private String mNafUrl;
61     private boolean mForceBootstrap;
62     private int mOrg;
63     private int mSpId;
64     private int mTlsCs;
65     private boolean mIsAuthSuccess;
66     private String mGbaKey;
67     private String mBTid;
68     private int mFailReason;
69 
70     private static Settings sInstance;
71 
Settings(Context cxt)72     private Settings(Context cxt) {
73         mTelephony = ITelephony.Stub.asInterface(TelephonyFrameworkInitializer
74                 .getTelephonyServiceManager().getTelephonyServiceRegisterer().get());
75         mSubId = SubscriptionManager.getDefaultSubscriptionId();
76         SharedPreferences sharedPref = cxt.getSharedPreferences(
77                 PREF_CARRIER_CONFIG, Context.MODE_PRIVATE);
78         mServicePackageName = loadServicePackageName(mSubId, sharedPref);
79         mReleaseTime = loadReleaseTime(mSubId, sharedPref);
80 
81         sharedPref = cxt.getSharedPreferences(PREF_TEST_CONFIG, Context.MODE_PRIVATE);
82         mAppType = sharedPref.getInt(KEY_APP_TYPE, TelephonyManager.APPTYPE_SIM);
83         mNafUrl = sharedPref.getString(KEY_NAF_URL, null);
84         mForceBootstrap = sharedPref.getBoolean(KEY_FORCE_BT, false);
85         mOrg = sharedPref.getInt(KEY_ORG, 0);
86         mSpId = sharedPref.getInt(KEY_SP_ID, 0);
87         mTlsCs = sharedPref.getInt(KEY_TLS_CS, 0);
88 
89         sharedPref = cxt.getSharedPreferences(PREF_SERVICE_CONFIG, Context.MODE_PRIVATE);
90         mIsAuthSuccess = sharedPref.getBoolean(KEY_AUTH_RESULT, false);
91         mFailReason = sharedPref.getInt(KEY_FAIL_REASON, 0);
92         mGbaKey = sharedPref.getString(KEY_GBA_KEY, null);
93         mBTid = sharedPref.getString(KEY_B_TID, null);
94     }
95 
96     /** Get the instance of Settings*/
getSettings(Context cxt)97     public static Settings getSettings(Context cxt) {
98         if (sInstance == null) {
99             sInstance = new Settings(cxt);
100         }
101 
102         return sInstance;
103     }
104 
105     /** update carrier config settings */
updateCarrierConfig(Context cxt, String packageName, int releaseTime)106     public void updateCarrierConfig(Context cxt, String packageName, int releaseTime) {
107         new Thread(() -> {
108             synchronized (PREF_CARRIER_CONFIG) {
109 
110                 if (TextUtils.equals(mServicePackageName, packageName)
111                         && (mReleaseTime == releaseTime)) {
112                     return;
113                 }
114 
115                 if (!TextUtils.equals(mServicePackageName, packageName)) {
116                     mServicePackageName = packageName;
117 
118                     try {
119                         mTelephony.setBoundGbaServiceOverride(mSubId, packageName);
120                     } catch (RemoteException e) {
121                         Log.e(TAG, "fail to set package name due to " + e);
122                     }
123 
124                 }
125 
126                 if (mReleaseTime != releaseTime) {
127                     mReleaseTime = releaseTime;
128 
129                     try {
130                         mTelephony.setGbaReleaseTimeOverride(mSubId, releaseTime);
131                     } catch (RemoteException e) {
132                         Log.e(TAG, "fail to set release time due to " + e);
133                     }
134                 }
135 
136                 SharedPreferences sharedPref = cxt.getSharedPreferences(
137                         PREF_CARRIER_CONFIG, Context.MODE_PRIVATE);
138                 SharedPreferences.Editor editor = sharedPref.edit();
139                 editor.putString(KEY_SERVICE_PACKAGE, packageName);
140                 editor.putInt(KEY_RELEASE_TIME, releaseTime);
141                 editor.commit();
142             }
143         }).start();
144     }
145 
146     /** get the config of gba service package name */
getServicePackageName()147     public String getServicePackageName() {
148         synchronized (PREF_CARRIER_CONFIG) {
149             return mServicePackageName;
150         }
151     }
152 
153     /** get the config of gba release time */
getReleaseTime()154     public int getReleaseTime() {
155         synchronized (PREF_CARRIER_CONFIG) {
156             return mReleaseTime;
157         }
158     }
159 
160     /** get the config of gba service package name used for now*/
loadServicePackageName(int subId, SharedPreferences sharedPref)161     public String loadServicePackageName(int subId, SharedPreferences sharedPref) {
162         try {
163             return mTelephony.getBoundGbaService(subId);
164         } catch (RemoteException e) {
165             Log.e(TAG, "fail to get package name due to " + e);
166         }
167         return sharedPref != null ? sharedPref.getString(KEY_SERVICE_PACKAGE, null) : null;
168     }
169 
170     /** get the config of gba release time used for now */
loadReleaseTime(int subId, SharedPreferences sharedPref)171     public int loadReleaseTime(int subId, SharedPreferences sharedPref) {
172         try {
173             return mTelephony.getGbaReleaseTime(subId);
174         } catch (RemoteException e) {
175             Log.e(TAG, "fail to get package name due to " + e);
176         }
177         return sharedPref != null ? sharedPref.getInt(KEY_RELEASE_TIME, 0) : 0;
178     }
179 
180     /** update the config of test gba service */
updateServiceConfig(Context cxt, boolean success, int reason, String key, String btId)181     public void updateServiceConfig(Context cxt, boolean success, int reason,
182                 String key, String btId) {
183         new Thread(() -> {
184             synchronized (PREF_SERVICE_CONFIG) {
185                 mIsAuthSuccess = success;
186                 mFailReason = reason;
187                 mGbaKey = key;
188                 mBTid = btId;
189                 SharedPreferences sharedPref = cxt.getSharedPreferences(
190                         PREF_SERVICE_CONFIG, Context.MODE_PRIVATE);
191                 SharedPreferences.Editor editor = sharedPref.edit();
192                 editor.putBoolean(KEY_AUTH_RESULT, success);
193                 editor.putInt(KEY_FAIL_REASON, reason);
194                 editor.putString(KEY_GBA_KEY, key);
195                 editor.putString(KEY_B_TID, btId);
196                 editor.commit();
197             }
198         }).start();
199     }
200 
201     /** get the config of the authentication result */
getAuthResult()202     public boolean getAuthResult() {
203         synchronized (PREF_SERVICE_CONFIG) {
204             return mIsAuthSuccess;
205         }
206     }
207 
208     /** get the config of authentication fail cause */
getFailReason()209     public int getFailReason() {
210         synchronized (PREF_SERVICE_CONFIG) {
211             return mFailReason;
212         }
213     }
214 
215     /** get the config of GBA key */
getGbaKey()216     public String getGbaKey() {
217         synchronized (PREF_SERVICE_CONFIG) {
218             return mGbaKey;
219         }
220     }
221 
222     /** get the config of B-Tid */
getBTid()223     public String getBTid() {
224         synchronized (PREF_SERVICE_CONFIG) {
225             return mBTid;
226         }
227     }
228 
229     /** update the config of the test */
updateTestConfig(Context cxt, int appType, String url, boolean force, int org, int spId, int tlsCs)230     public void updateTestConfig(Context cxt, int appType, String url,
231             boolean force, int org, int spId, int tlsCs) {
232         new Thread(() -> {
233             synchronized (PREF_TEST_CONFIG) {
234                 mAppType = appType;
235                 mNafUrl = url;
236                 mForceBootstrap = force;
237                 mOrg = org;
238                 mSpId = spId;
239                 mTlsCs = tlsCs;
240 
241                 SharedPreferences sharedPref = cxt.getSharedPreferences(
242                         PREF_TEST_CONFIG, Context.MODE_PRIVATE);
243                 SharedPreferences.Editor editor = sharedPref.edit();
244                 editor.putInt(KEY_APP_TYPE, appType);
245                 editor.putString(KEY_NAF_URL, url);
246                 editor.putBoolean(KEY_FORCE_BT, force);
247                 editor.putInt(KEY_ORG, org);
248                 editor.putInt(KEY_SP_ID, spId);
249                 editor.putInt(KEY_TLS_CS, tlsCs);
250                 editor.commit();
251             }
252         }).start();
253     }
254 
255     /** get the config of the uicc application type*/
getAppType()256     public int getAppType() {
257         synchronized (PREF_TEST_CONFIG) {
258             return mAppType;
259         }
260     }
261 
262     /** get the config of NAF url */
getNafUrl()263     public String getNafUrl() {
264         synchronized (PREF_TEST_CONFIG) {
265             return mNafUrl;
266         }
267     }
268 
269     /** get the config if bootstrap is forced */
isForceBootstrap()270     public boolean isForceBootstrap() {
271         synchronized (PREF_TEST_CONFIG) {
272             return mForceBootstrap;
273         }
274     }
275 
276     /** get the config of the organization code */
getOrg()277     public int getOrg() {
278         synchronized (PREF_TEST_CONFIG) {
279             return mOrg;
280         }
281     }
282 
283     /** get the config of the security protocol id */
getSpId()284     public int getSpId() {
285         synchronized (PREF_TEST_CONFIG) {
286             return mSpId;
287         }
288     }
289 
290     /** get the config of the tls ciper suite id */
getTlsCs()291     public int getTlsCs() {
292         synchronized (PREF_TEST_CONFIG) {
293             return mTlsCs;
294         }
295     }
296 
297     /** convert byte arry to hex string */
byteArrayToHexString(byte[] data)298     public static String byteArrayToHexString(byte[] data) {
299         if (data == null || data.length == 0) {
300             return "";
301         }
302 
303         StringBuilder sb = new StringBuilder();
304         for (byte b : data) {
305             sb.append(String.format(Locale.US, "%02X", b));
306         }
307         return sb.toString();
308     }
309 }
310