1 /*
2  * Copyright 2019 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.internal.telephony;
18 
19 import android.content.Context;
20 import android.provider.Settings;
21 import android.provider.Settings.SettingNotFoundException;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.TelephonyManager;
24 
25 /**
26  * Helper class that reads and writes Global.Setting values for Telephony. It will:
27  * For Single SIM case, read from or write to the singleton setting value.
28  * For Multi-SIM case, read from or write to the per subscription value.
29  */
30 public class GlobalSettingsHelper {
31     /**
32      * Helper function to get integer value.
33      */
getInt(Context context, String settingName, int subId, int defaultValue)34     public static int getInt(Context context, String settingName, int subId,
35             int defaultValue) {
36         settingName = getSettingName(context, settingName, subId);
37         return Settings.Global.getInt(context.getContentResolver(), settingName, defaultValue);
38     }
39 
40     /**
41      * Helper function to get boolean value.
42      */
getBoolean(Context context, String settingName, int subId, boolean defaultValue)43     public static boolean getBoolean(Context context, String settingName, int subId,
44             boolean defaultValue) {
45         settingName = getSettingName(context, settingName, subId);
46         return Settings.Global.getInt(context.getContentResolver(), settingName,
47                 defaultValue ? 1 : 0) == 1;
48     }
49 
50     /**
51      * Helper function to get boolean value or throws SettingNotFoundException if not set.
52      */
getBoolean(Context context, String settingName, int subId)53     public static boolean getBoolean(Context context, String settingName, int subId)
54             throws SettingNotFoundException {
55         settingName = getSettingName(context, settingName, subId);
56         return Settings.Global.getInt(context.getContentResolver(), settingName) == 1;
57     }
58 
59     /**
60      * Helper function to set integer value.
61      * Returns whether the value is changed or initially set.
62      */
setInt(Context context, String settingName, int subId, int value)63     public static boolean setInt(Context context, String settingName, int subId, int value) {
64         settingName = getSettingName(context, settingName, subId);
65 
66         boolean needChange;
67         try {
68             needChange = Settings.Global.getInt(context.getContentResolver(), settingName) != value;
69         } catch (SettingNotFoundException exception) {
70             needChange = true;
71         }
72         if (needChange) Settings.Global.putInt(context.getContentResolver(), settingName, value);
73 
74         return needChange;
75     }
76 
77     /**
78      * Helper function to set boolean value.
79      * Returns whether the value is changed or initially set.
80      */
setBoolean(Context context, String settingName, int subId, boolean value)81     public static boolean setBoolean(Context context, String settingName, int subId,
82             boolean value) {
83         return setInt(context, settingName, subId, value ? 1 : 0);
84     }
85 
getSettingName(Context context, String settingName, int subId)86     private static String getSettingName(Context context, String settingName, int subId) {
87         // For single SIM phones, this is a per phone property. Or if it's invalid subId, we
88         // read default setting.
89         if (TelephonyManager.from(context).getSimCount() > 1
90                 && SubscriptionManager.isValidSubscriptionId(subId)) {
91             return settingName + subId;
92         } else {
93             return settingName;
94         }
95     }
96 }
97