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 */ 16 package com.android.phone.settings; 17 18 import android.content.Context; 19 import android.telecom.PhoneAccountHandle; 20 import com.android.internal.telephony.Phone; 21 import com.android.phone.PhoneUtils; 22 import com.android.phone.R; 23 import com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper; 24 import com.android.phone.vvm.omtp.VisualVoicemailPreferences; 25 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager; 26 import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter; 27 28 /** 29 * Save whether or not a particular account is enabled in shared to be retrieved later. 30 */ 31 public class VisualVoicemailSettingsUtil { 32 33 private static final String IS_ENABLED_KEY = "is_enabled"; 34 35 36 public static void setEnabled(Context context, PhoneAccountHandle phoneAccount, 37 boolean isEnabled) { 38 new VisualVoicemailPreferences(context, phoneAccount).edit() 39 .putBoolean(IS_ENABLED_KEY, isEnabled) 40 .apply(); 41 OmtpVvmCarrierConfigHelper config = new OmtpVvmCarrierConfigHelper(context, phoneAccount); 42 if (isEnabled) { 43 OmtpVvmSourceManager.getInstance(context).addPhoneStateListener(phoneAccount); 44 config.startActivation(); 45 } else { 46 OmtpVvmSourceManager.getInstance(context).removeSource(phoneAccount); 47 config.startDeactivation(); 48 } 49 } 50 51 public static boolean isEnabled(Context context, 52 PhoneAccountHandle phoneAccount) { 53 if (phoneAccount == null) { 54 return false; 55 } 56 if (!context.getResources().getBoolean(R.bool.allow_visual_voicemail)) { 57 return false; 58 } 59 60 VisualVoicemailPreferences prefs = new VisualVoicemailPreferences(context, phoneAccount); 61 if (prefs.contains(IS_ENABLED_KEY)) { 62 // isEnableByDefault is a bit expensive, so don't use it as default value of 63 // getBoolean(). The "false" here should never be actually used. 64 return prefs.getBoolean(IS_ENABLED_KEY, false); 65 } 66 return new OmtpVvmCarrierConfigHelper(context, 67 PhoneAccountHandleConverter.toSubId(phoneAccount)).isEnabledByDefault(); 68 } 69 70 public static boolean isEnabled(Phone phone) { 71 return isEnabled(phone.getContext(), 72 PhoneUtils.makePstnPhoneAccountHandle(phone)); 73 } 74 75 /** 76 * Whether the client enabled status is explicitly set by user or by default(Whether carrier VVM 77 * app is installed). This is used to determine whether to disable the client when the carrier 78 * VVM app is installed. If the carrier VVM app is installed the client should give priority to 79 * it if the settings are not touched. 80 */ 81 public static boolean isEnabledUserSet(Context context, 82 PhoneAccountHandle phoneAccount) { 83 if (phoneAccount == null) { 84 return false; 85 } 86 VisualVoicemailPreferences prefs = new VisualVoicemailPreferences(context, phoneAccount); 87 return prefs.contains(IS_ENABLED_KEY); 88 } 89 } 90