summaryrefslogtreecommitdiff
path: root/src/com/google/android/iwlan/IwlanCarrierConfig.java
blob: e68341a3b5b3d6ed2ba56f9e259699264ceb1453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.iwlan;

import android.content.Context;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.telephony.CarrierConfigManager;

import androidx.annotation.VisibleForTesting;

/** Class for handling IWLAN carrier configuration. */
public class IwlanCarrierConfig {
    static final String PREFIX = "iwlan.";

    /**
     * Key for setting the delay in seconds to release the IWLAN connection after a handover to
     * WWAN. Refer to {@link #DEFAULT_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT} for the default
     * value.
     */
    public static final String KEY_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT =
            PREFIX + "handover_to_wwan_release_delay_second_int";

    /**
     * Key to exclude IKE N1_MODE_CAPABILITY Notify payload during emergency session setup without
     * affecting normal sessions. See {@link #DEFAULT_N1_MODE_EXCLUSION_FOR_EMERGENCY_SESSION_BOOL}
     * for the default value.
     */
    public static final String KEY_N1_MODE_EXCLUSION_FOR_EMERGENCY_SESSION_BOOL =
            PREFIX + "n1_mode_exclusion_for_emergency_session";

    /**
     * Key to decide whether N1 mode shall be enabled or disabled depending on 5G enabling status
     * via the UI/UX. See {@link #DEFAULT_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL} for the default value.
     */
    public static final String KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL =
            PREFIX + "update_n1_mode_on_ui_change_bool";

    /**
     * Default delay in seconds for releasing the IWLAN connection after a WWAN handover. This is
     * the default value for {@link #KEY_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT}.
     */
    public static final int DEFAULT_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT = 0;

    /**
     * The default value for determining whether the IKE N1_MODE_CAPABILITY Notify payload is
     * excluded during emergency session setup.
     */
    public static final boolean DEFAULT_N1_MODE_EXCLUSION_FOR_EMERGENCY_SESSION_BOOL = false;

    /**
     * The default value for determining whether N1 mode shall be enabled or disabled depending on
     * 5G enabling status via the UI/UX.
     */
    public static final boolean DEFAULT_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL = true;

    private static PersistableBundle sTestBundle = new PersistableBundle();

    private static PersistableBundle sHiddenBundle = new PersistableBundle();

    static {
        sHiddenBundle = createHiddenDefaultConfig();
    }

    /**
     * Creates a hidden default configuration.
     *
     * @return a PersistableBundle containing the hidden default configuration
     */
    private static @NonNull PersistableBundle createHiddenDefaultConfig() {
        PersistableBundle bundle = new PersistableBundle();
        bundle.putInt(
                KEY_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT,
                DEFAULT_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT);
        bundle.putBoolean(
                KEY_N1_MODE_EXCLUSION_FOR_EMERGENCY_SESSION_BOOL,
                DEFAULT_N1_MODE_EXCLUSION_FOR_EMERGENCY_SESSION_BOOL);
        bundle.putBoolean(
                KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, DEFAULT_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL);
        return bundle;
    }

    private static PersistableBundle getConfig(Context context, int slotId, String key) {
        CarrierConfigManager carrierConfigManager =
                context.getSystemService(CarrierConfigManager.class);
        if (carrierConfigManager == null) {
            return getDefaultConfig(key);
        }

        int subId = IwlanHelper.getSubId(context, slotId);
        PersistableBundle bundle = carrierConfigManager.getConfigForSubId(subId, key);
        return bundle.containsKey(key) ? bundle : getDefaultConfig(key);
    }

    private static PersistableBundle getDefaultConfig(String key) {
        if (sTestBundle.containsKey(key)) {
            return sTestBundle;
        }

        PersistableBundle bundle = CarrierConfigManager.getDefaultConfig();
        if (bundle.containsKey(key)) {
            return bundle;
        }

        if (sHiddenBundle.containsKey(key)) {
            return sHiddenBundle;
        }

        throw new IllegalArgumentException("Default config not found for key: " + key);
    }

    /**
     * Gets a configuration int value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration int value
     */
    public static int getConfigInt(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getInt(key);
    }

    /**
     * Gets a configuration long value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration long value
     */
    public static long getConfigLong(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getLong(key);
    }

    /**
     * Gets a configuration double value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration double value
     */
    public static double getConfigDouble(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getDouble(key);
    }

    /**
     * Gets a configuration boolean value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration boolean value
     */
    public static boolean getConfigBoolean(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getBoolean(key);
    }

    /**
     * Gets a configuration string value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration string value
     */
    public static String getConfigString(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getString(key);
    }

    /**
     * Gets a configuration int[] value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration int[] value
     */
    public static int[] getConfigIntArray(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getIntArray(key);
    }

    /**
     * Gets a configuration long[] value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration long[] value
     */
    public static long[] getConfigLongArray(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getLongArray(key);
    }

    /**
     * Gets a configuration double[] value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration double[] value
     */
    public static double[] getConfigDoubleArray(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getDoubleArray(key);
    }

    /**
     * Gets a configuration boolean[] value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration boolean[] value
     */
    public static boolean[] getConfigBooleanArray(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getBooleanArray(key);
    }

    /**
     * Gets a configuration string[] value for a given slot ID and key.
     *
     * @param context the application context
     * @param slotId the slot ID
     * @param key the configuration key
     * @return the configuration string[] value
     */
    public static String[] getConfigStringArray(Context context, int slotId, String key) {
        return getConfig(context, slotId, key).getStringArray(key);
    }

    /**
     * Gets the default configuration int value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration int value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static int getDefaultConfigInt(String key) {
        return getDefaultConfig(key).getInt(key);
    }

    /**
     * Gets the default configuration long value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration long value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static long getDefaultConfigLong(String key) {
        return getDefaultConfig(key).getLong(key);
    }

    /**
     * Gets the default configuration double value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration double value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static double getDefaultConfigDouble(String key) {
        return getDefaultConfig(key).getDouble(key);
    }

    /**
     * Gets the default configuration string value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration string value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static String getDefaultConfigString(String key) {
        return getDefaultConfig(key).getString(key);
    }

    /**
     * Gets the default configuration boolean value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration boolean value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static boolean getDefaultConfigBoolean(String key) {
        return getDefaultConfig(key).getBoolean(key);
    }

    /**
     * Gets the default configuration int[] value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration int[] value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static int[] getDefaultConfigIntArray(String key) {
        return getDefaultConfig(key).getIntArray(key);
    }

    /**
     * Gets the default configuration long value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration long value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static long[] getDefaultConfigLongArray(String key) {
        return getDefaultConfig(key).getLongArray(key);
    }

    /**
     * Gets the default configuration double[] value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration double[] value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static double[] getDefaultConfigDoubleArray(String key) {
        return getDefaultConfig(key).getDoubleArray(key);
    }

    /**
     * Gets the default configuration string[] value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration string[] value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static String[] getDefaultConfigStringArray(String key) {
        return getDefaultConfig(key).getStringArray(key);
    }

    /**
     * Gets the default configuration boolean[] value for a given key.
     *
     * @param key the configuration key
     * @return the default configuration boolean[] value
     * @throws IllegalArgumentException if the default configuration is null for the given key
     */
    public static boolean[] getDefaultConfigBooleanArray(String key) {
        return getDefaultConfig(key).getBooleanArray(key);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigBundle(PersistableBundle bundle) {
        sTestBundle.putAll(bundle);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigInt(@NonNull String key, int value) {
        sTestBundle.putInt(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigLong(@NonNull String key, long value) {
        sTestBundle.putLong(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigDouble(@NonNull String key, double value) {
        sTestBundle.putDouble(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigBoolean(@NonNull String key, boolean value) {
        sTestBundle.putBoolean(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigString(@NonNull String key, String value) {
        sTestBundle.putString(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigIntArray(@NonNull String key, @NonNull int[] value) {
        sTestBundle.putIntArray(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigLongArray(@NonNull String key, @NonNull long[] value) {
        sTestBundle.putLongArray(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigDoubleArray(@NonNull String key, @NonNull double[] value) {
        sTestBundle.putDoubleArray(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigBooleanArray(@NonNull String key, @NonNull boolean[] value) {
        sTestBundle.putBooleanArray(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void putTestConfigStringArray(@NonNull String key, @NonNull String[] value) {
        sTestBundle.putStringArray(key, value);
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void resetTestConfig() {
        sTestBundle.clear();
    }
}