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
17package com.android.phone.sip;
18
19import com.android.phone.R;
20
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.SharedPreferences;
24import android.provider.Settings;
25import android.provider.Settings.SettingNotFoundException;
26import android.text.TextUtils;
27import android.util.Log;
28
29/**
30 * Wrapper for SIP's shared preferences.
31 */
32public class SipSharedPreferences {
33    private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
34    private static final String KEY_PRIMARY_ACCOUNT = "primary";
35    private static final String KEY_NUMBER_OF_PROFILES = "profiles";
36
37    private SharedPreferences mPreferences;
38    private Context mContext;
39
40    public SipSharedPreferences(Context context) {
41        mPreferences = context.getSharedPreferences(
42                SIP_SHARED_PREFERENCES, Context.MODE_WORLD_READABLE);
43        mContext = context;
44    }
45
46    public void setPrimaryAccount(String accountUri) {
47        SharedPreferences.Editor editor = mPreferences.edit();
48        editor.putString(KEY_PRIMARY_ACCOUNT, accountUri);
49        editor.apply();
50    }
51
52    public void unsetPrimaryAccount() {
53        setPrimaryAccount(null);
54    }
55
56    /** Returns the primary account URI or null if it does not exist. */
57    public String getPrimaryAccount() {
58        return mPreferences.getString(KEY_PRIMARY_ACCOUNT, null);
59    }
60
61    public boolean isPrimaryAccount(String accountUri) {
62        return accountUri.equals(
63                mPreferences.getString(KEY_PRIMARY_ACCOUNT, null));
64    }
65
66    public boolean hasPrimaryAccount() {
67        return !TextUtils.isEmpty(
68                mPreferences.getString(KEY_PRIMARY_ACCOUNT, null));
69    }
70
71    public void setProfilesCount(int number) {
72        SharedPreferences.Editor editor = mPreferences.edit();
73        editor.putInt(KEY_NUMBER_OF_PROFILES, number);
74        editor.apply();
75    }
76
77    public int getProfilesCount() {
78        return mPreferences.getInt(KEY_NUMBER_OF_PROFILES, 0);
79    }
80
81    public void setSipCallOption(String option) {
82        Settings.System.putString(mContext.getContentResolver(),
83                Settings.System.SIP_CALL_OPTIONS, option);
84    }
85
86    public String getSipCallOption() {
87        String option = Settings.System.getString(mContext.getContentResolver(),
88                Settings.System.SIP_CALL_OPTIONS);
89        return (option != null) ? option
90                                : mContext.getString(R.string.sip_address_only);
91    }
92
93    public void setReceivingCallsEnabled(boolean enabled) {
94        Settings.System.putInt(mContext.getContentResolver(),
95                Settings.System.SIP_RECEIVE_CALLS, (enabled ? 1 : 0));
96    }
97
98    public boolean isReceivingCallsEnabled() {
99        try {
100            return (Settings.System.getInt(mContext.getContentResolver(),
101                    Settings.System.SIP_RECEIVE_CALLS) != 0);
102        } catch (SettingNotFoundException e) {
103            Log.d("SIP", "ReceiveCall option is not set; use default value");
104            return false;
105        }
106    }
107
108    // TODO: back up to Android Backup
109}
110