1/*
2 * Copyright (C) 2015 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 */
16package com.android.phone.settings;
17
18import android.content.Context;
19import android.content.SharedPreferences;
20import android.preference.PreferenceManager;
21import android.telecom.PhoneAccountHandle;
22
23import com.android.internal.telephony.Phone;
24import com.android.phone.PhoneUtils;
25import com.android.phone.vvm.omtp.OmtpConstants;
26import com.android.phone.vvm.omtp.sms.StatusMessage;
27
28/**
29 * Save visual voicemail login values and whether or not a particular account is enabled in shared
30 * preferences to be retrieved later.
31 * Because a voicemail source is tied 1:1 to a phone account, the phone account handle is used in
32 * the key for each voicemail source and the associated data.
33 */
34public class VisualVoicemailSettingsUtil {
35    private static final String VISUAL_VOICEMAIL_SHARED_PREFS_KEY_PREFIX =
36            "visual_voicemail_";
37
38    private static final String IS_ENABLED_KEY = "is_enabled";
39    // If a carrier vvm app is installed, Google visual voicemail is automatically switched off
40    // however, the user can override this setting.
41    private static final String IS_USER_SET = "is_user_set";
42    // Record the timestamp of the last full sync so that duplicate syncs can be reduced.
43    private static final String LAST_FULL_SYNC_TIMESTAMP = "last_full_sync_timestamp";
44    // Constant indicating that there has never been a full sync.
45    public static final long NO_PRIOR_FULL_SYNC = -1;
46
47    // Setting for how often retries should be done.
48    private static final String SYNC_RETRY_INTERVAL = "sync_retry_interval";
49    private static final long MAX_SYNC_RETRY_INTERVAL_MS = 86400000;   // 24 hours
50    private static final long DEFAULT_SYNC_RETRY_INTERVAL_MS = 900000; // 15 minutes
51
52
53    public static void setVisualVoicemailEnabled(Phone phone, boolean isEnabled,
54            boolean isUserSet) {
55        setVisualVoicemailEnabled(phone.getContext(), PhoneUtils.makePstnPhoneAccountHandle(phone),
56                isEnabled, isUserSet);
57    }
58
59    public static void setVisualVoicemailEnabled(Context context, PhoneAccountHandle phoneAccount,
60            boolean isEnabled, boolean isUserSet) {
61        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
62        SharedPreferences.Editor editor = prefs.edit();
63        editor.putBoolean(
64                getVisualVoicemailSharedPrefsKey(IS_ENABLED_KEY, phoneAccount), isEnabled);
65        editor.putBoolean(
66                getVisualVoicemailSharedPrefsKey(IS_USER_SET, phoneAccount),
67                isUserSet);
68        editor.commit();
69    }
70
71    public static boolean isVisualVoicemailEnabled(Context context,
72            PhoneAccountHandle phoneAccount) {
73        if (phoneAccount == null) {
74            return false;
75        }
76        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
77        return prefs.getBoolean(getVisualVoicemailSharedPrefsKey(IS_ENABLED_KEY, phoneAccount),
78                false);
79    }
80
81    public static boolean isVisualVoicemailEnabled(Phone phone) {
82        return isVisualVoicemailEnabled(phone.getContext(),
83                PhoneUtils.makePstnPhoneAccountHandle(phone));
84    }
85
86    /**
87     * Differentiate user-enabled/disabled to know whether to ignore automatic enabling and
88     * disabling by the system. This is relevant when a carrier vvm app is installed and the user
89     * manually enables dialer visual voicemail. In that case we would want that setting to persist.
90     */
91    public static boolean isVisualVoicemailUserSet(Context context,
92            PhoneAccountHandle phoneAccount) {
93        if (phoneAccount == null) {
94            return false;
95        }
96        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
97        return prefs.getBoolean(
98                getVisualVoicemailSharedPrefsKey(IS_USER_SET, phoneAccount),
99                false);
100    }
101
102    public static void setVisualVoicemailCredentialsFromStatusMessage(Context context,
103            PhoneAccountHandle phoneAccount, StatusMessage message) {
104        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
105        SharedPreferences.Editor editor = prefs.edit();
106
107        editor.putString(
108                getVisualVoicemailSharedPrefsKey(OmtpConstants.IMAP_PORT, phoneAccount),
109                message.getImapPort());
110        editor.putString(
111                getVisualVoicemailSharedPrefsKey(OmtpConstants.SERVER_ADDRESS, phoneAccount),
112                message.getServerAddress());
113        editor.putString(
114                getVisualVoicemailSharedPrefsKey(OmtpConstants.IMAP_USER_NAME, phoneAccount),
115                message.getImapUserName());
116        editor.putString(
117                getVisualVoicemailSharedPrefsKey(OmtpConstants.IMAP_PASSWORD, phoneAccount),
118                message.getImapPassword());
119        editor.commit();
120    }
121
122    public static String getVisualVoicemailCredentials(Context context, String key,
123            PhoneAccountHandle phoneAccount) {
124        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
125        return prefs.getString(getVisualVoicemailSharedPrefsKey(key, phoneAccount), null);
126    }
127
128    public static long getVisualVoicemailRetryInterval(Context context,
129            PhoneAccountHandle phoneAccount) {
130        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
131        return prefs.getLong(getVisualVoicemailSharedPrefsKey(SYNC_RETRY_INTERVAL, phoneAccount),
132                DEFAULT_SYNC_RETRY_INTERVAL_MS);
133    }
134
135    public static void resetVisualVoicemailRetryInterval(Context context,
136            PhoneAccountHandle phoneAccount) {
137        setVisualVoicemailRetryInterval(context, phoneAccount, DEFAULT_SYNC_RETRY_INTERVAL_MS);
138    }
139
140    public static void setVisualVoicemailRetryInterval(Context context,
141            PhoneAccountHandle phoneAccount, long interval) {
142        SharedPreferences.Editor editor =
143                PreferenceManager.getDefaultSharedPreferences(context).edit();
144        editor.putLong(getVisualVoicemailSharedPrefsKey(SYNC_RETRY_INTERVAL, phoneAccount),
145                Math.min(interval, MAX_SYNC_RETRY_INTERVAL_MS));
146        editor.commit();
147    }
148
149    public static void setVisualVoicemailLastFullSyncTime(Context context,
150            PhoneAccountHandle phoneAccount, long timestamp) {
151        SharedPreferences.Editor editor =
152                PreferenceManager.getDefaultSharedPreferences(context).edit();
153        editor.putLong(getVisualVoicemailSharedPrefsKey(LAST_FULL_SYNC_TIMESTAMP, phoneAccount),
154                timestamp);
155        editor.commit();
156
157    }
158
159    public static long getVisualVoicemailLastFullSyncTime(Context context,
160            PhoneAccountHandle phoneAccount) {
161        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
162        return prefs.getLong(
163                getVisualVoicemailSharedPrefsKey(LAST_FULL_SYNC_TIMESTAMP, phoneAccount),
164                NO_PRIOR_FULL_SYNC);
165    }
166
167    private static String getVisualVoicemailSharedPrefsKey(String key,
168            PhoneAccountHandle phoneAccount) {
169        return VISUAL_VOICEMAIL_SHARED_PREFS_KEY_PREFIX + key + "_" + phoneAccount.getId();
170    }
171}
172