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.net.sip.SipManager;
22 import android.provider.Settings;
23 import android.provider.Settings.SettingNotFoundException;
24 import android.util.Log;
25 
26 /**
27  * Wrapper for SIP's preferences.
28  */
29 public class SipPreferences {
30     private static final String PREFIX = "[SipPreferences] ";
31     private static final boolean VERBOSE = false; /* STOP SHIP if true */
32 
33     // Used to clear out old SharedPreferences file during SipProfile Database Migration
34     private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
35 
36     private Context mContext;
37 
SipPreferences(Context context)38     public SipPreferences(Context context) {
39         mContext = context;
40     }
41 
setSipCallOption(String option)42     public void setSipCallOption(String option) {
43         Settings.System.putString(mContext.getContentResolver(),
44                 Settings.System.SIP_CALL_OPTIONS, option);
45 
46         // Notify SipAccountRegistry in the telephony layer that the configuration has changed.
47         // This causes the SIP PhoneAccounts to be re-registered.  This ensures the supported URI
48         // schemes for the SIP PhoneAccounts matches the new SIP_CALL_OPTIONS setting.
49         Intent intent = new Intent(SipManager.ACTION_SIP_CALL_OPTION_CHANGED);
50         mContext.sendBroadcast(intent);
51     }
52 
getSipCallOption()53     public String getSipCallOption() {
54         String option = Settings.System.getString(mContext.getContentResolver(),
55                 Settings.System.SIP_CALL_OPTIONS);
56         return (option != null) ? option
57                                 : mContext.getString(R.string.sip_address_only);
58     }
59 
setReceivingCallsEnabled(boolean enabled)60     public void setReceivingCallsEnabled(boolean enabled) {
61         Settings.System.putInt(mContext.getContentResolver(),
62                 Settings.System.SIP_RECEIVE_CALLS, (enabled ? 1 : 0));
63     }
64 
isReceivingCallsEnabled()65     public boolean isReceivingCallsEnabled() {
66         try {
67             return (Settings.System.getInt(mContext.getContentResolver(),
68                     Settings.System.SIP_RECEIVE_CALLS) != 0);
69         } catch (SettingNotFoundException e) {
70             log("isReceivingCallsEnabled, option not set; use default value, exception: " + e);
71             return false;
72         }
73     }
74 
75     /**
76      * Remove obsolete SharedPreferences File upon upgrade from M->N.
77      */
clearSharedPreferences()78     public void clearSharedPreferences() {
79         mContext.deleteSharedPreferences(SIP_SHARED_PREFERENCES);
80     }
81 
82     // TODO: back up to Android Backup
83 
log(String msg)84     private static void log(String msg) {
85         Log.d(SipUtil.LOG_TAG, PREFIX + msg);
86     }
87 }
88