1 /*
2  * Copyright (C) 2010 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.services.telephony.sip;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.net.sip.SipManager;
23 import android.provider.Settings;
24 import android.provider.Settings.SettingNotFoundException;
25 import android.util.Log;
26 
27 /**
28  * Wrapper for SIP's shared preferences.
29  */
30 public class SipSharedPreferences {
31     private static final String PREFIX = "[SipSharedPreferences] ";
32     private static final boolean VERBOSE = false; /* STOP SHIP if true */
33 
34     private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
35 
36     /**
37      * @deprecated Primary account selection for SIP accounts is no longer relevant.
38      */
39     @Deprecated
40     private static final String KEY_PRIMARY_ACCOUNT = "primary";
41 
42     private static final String KEY_NUMBER_OF_PROFILES = "profiles";
43 
44     private SharedPreferences mPreferences;
45     private Context mContext;
46 
SipSharedPreferences(Context context)47     public SipSharedPreferences(Context context) {
48         mPreferences = context.getSharedPreferences(
49                 SIP_SHARED_PREFERENCES, Context.MODE_WORLD_READABLE);
50         mContext = context;
51     }
52 
53     /**
54      * Returns the primary account URI or null if it does not exist.
55      * @deprecated The primary account setting is no longer used.
56      */
57     @Deprecated
getPrimaryAccount()58     public String getPrimaryAccount() {
59         return mPreferences.getString(KEY_PRIMARY_ACCOUNT, null);
60     }
61 
setProfilesCount(int number)62     public void setProfilesCount(int number) {
63         SharedPreferences.Editor editor = mPreferences.edit();
64         editor.putInt(KEY_NUMBER_OF_PROFILES, number);
65         editor.apply();
66     }
67 
getProfilesCount()68     public int getProfilesCount() {
69         return mPreferences.getInt(KEY_NUMBER_OF_PROFILES, 0);
70     }
71 
setSipCallOption(String option)72     public void setSipCallOption(String option) {
73         Settings.System.putString(mContext.getContentResolver(),
74                 Settings.System.SIP_CALL_OPTIONS, option);
75 
76         // Notify SipAccountRegistry in the telephony layer that the configuration has changed.
77         // This causes the SIP PhoneAccounts to be re-registered.  This ensures the supported URI
78         // schemes for the SIP PhoneAccounts matches the new SIP_CALL_OPTIONS setting.
79         Intent intent = new Intent(SipManager.ACTION_SIP_CALL_OPTION_CHANGED);
80         mContext.sendBroadcast(intent);
81     }
82 
getSipCallOption()83     public String getSipCallOption() {
84         String option = Settings.System.getString(mContext.getContentResolver(),
85                 Settings.System.SIP_CALL_OPTIONS);
86         return (option != null) ? option
87                                 : mContext.getString(R.string.sip_address_only);
88     }
89 
setReceivingCallsEnabled(boolean enabled)90     public void setReceivingCallsEnabled(boolean enabled) {
91         Settings.System.putInt(mContext.getContentResolver(),
92                 Settings.System.SIP_RECEIVE_CALLS, (enabled ? 1 : 0));
93     }
94 
isReceivingCallsEnabled()95     public boolean isReceivingCallsEnabled() {
96         try {
97             return (Settings.System.getInt(mContext.getContentResolver(),
98                     Settings.System.SIP_RECEIVE_CALLS) != 0);
99         } catch (SettingNotFoundException e) {
100             log("isReceivingCallsEnabled, option not set; use default value, exception: " + e);
101             return false;
102         }
103     }
104 
105     /**
106      * Performs cleanup of the shared preferences, removing the deprecated primary account key if
107      * it exists.
108      */
cleanupPrimaryAccountSetting()109     public void cleanupPrimaryAccountSetting() {
110         if (mPreferences.contains(KEY_PRIMARY_ACCOUNT)) {
111             SharedPreferences.Editor editor = mPreferences.edit();
112             editor.remove(KEY_PRIMARY_ACCOUNT);
113             editor.apply();
114         }
115     }
116 
117     // TODO: back up to Android Backup
118 
log(String msg)119     private static void log(String msg) {
120         Log.d(SipUtil.LOG_TAG, PREFIX + msg);
121     }
122 }
123