Merge "Persist user enable/disable setting through reboot." into mnc-dev
diff --git a/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java b/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
index 5dd3166..d2e797b 100644
--- a/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
+++ b/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
@@ -26,7 +26,8 @@
 import com.android.phone.vvm.omtp.sms.StatusMessage;
 
 /**
- * Save visual voicemail login values in shared preferences to be retrieved later.
+ * Save visual voicemail login values and whether or not a particular account is enabled in shared
+ * preferences to be retrieved later.
  * Because a voicemail source is tied 1:1 to a phone account, the phone account handle is used in
  * the key for each voicemail source and the associated data.
  */
@@ -35,18 +36,25 @@
             "visual_voicemail_";
 
     private static final String IS_ENABLED_KEY = "is_enabled";
+    // If a carrier vvm app is installed, Google visual voicemail is automatically switched off
+    // however, the user can override this setting.
+    private static final String IS_USER_SET = "is_user_set";
 
-    public static void setVisualVoicemailEnabled(Phone phone, boolean isEnabled) {
+    public static void setVisualVoicemailEnabled(Phone phone, boolean isEnabled,
+            boolean isUserSet) {
         setVisualVoicemailEnabled(phone.getContext(), PhoneUtils.makePstnPhoneAccountHandle(phone),
-                isEnabled);
+                isEnabled, isUserSet);
     }
 
     public static void setVisualVoicemailEnabled(Context context, PhoneAccountHandle phoneAccount,
-            boolean isEnabled) {
+            boolean isEnabled, boolean isUserSet) {
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
         SharedPreferences.Editor editor = prefs.edit();
         editor.putBoolean(
                 getVisualVoicemailSharedPrefsKey(IS_ENABLED_KEY, phoneAccount), isEnabled);
+        editor.putBoolean(
+                getVisualVoicemailSharedPrefsKey(IS_USER_SET, phoneAccount),
+                isUserSet);
         editor.commit();
     }
 
@@ -65,6 +73,28 @@
                 PhoneUtils.makePstnPhoneAccountHandle(phone));
     }
 
+    public static boolean isEnabledByUserOverride(Context context,
+            PhoneAccountHandle phoneAccount) {
+        return isVisualVoicemailUserSet(context, phoneAccount) &&
+                isVisualVoicemailEnabled(context, phoneAccount);
+    }
+
+    /**
+     * Differentiate user-enabled/disabled to know whether to ignore automatic enabling and
+     * disabling by the system. This is relevant when a carrier vvm app is installed and the user
+     * manually enables dialer visual voicemail. In that case we would want that setting to persist.
+     */
+    public static boolean isVisualVoicemailUserSet(Context context,
+            PhoneAccountHandle phoneAccount) {
+        if (phoneAccount == null) {
+            return false;
+        }
+        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+        return prefs.getBoolean(
+                getVisualVoicemailSharedPrefsKey(IS_USER_SET, phoneAccount),
+                false);
+    }
+
     public static void setVisualVoicemailCredentialsFromStatusMessage(Context context,
             PhoneAccountHandle phoneAccount, StatusMessage message) {
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
diff --git a/src/com/android/phone/settings/VoicemailSettingsActivity.java b/src/com/android/phone/settings/VoicemailSettingsActivity.java
index 7485124..149cf0a 100644
--- a/src/com/android/phone/settings/VoicemailSettingsActivity.java
+++ b/src/com/android/phone/settings/VoicemailSettingsActivity.java
@@ -389,8 +389,9 @@
             VoicemailNotificationSettingsUtil.setVibrationEnabled(
                     mPhone, Boolean.TRUE.equals(objValue));
         } else if (preference.getKey().equals(mVoicemailVisualVoicemail.getKey())) {
-            if ((Boolean) objValue) {
-                VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(mPhone, true);
+            boolean isEnabled = (Boolean) objValue;
+            VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(mPhone, isEnabled, true);
+            if (isEnabled) {
                 mOmtpVvmCarrierConfigHelper.startActivation();
             } else {
                 OmtpVvmSourceManager.getInstance(mPhone.getContext()).removeSource(mPhone);
diff --git a/src/com/android/phone/vvm/omtp/SimChangeReceiver.java b/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
index 3ac1096..0823552 100644
--- a/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
+++ b/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
@@ -70,11 +70,18 @@
 
                     if (carrierConfigHelper.isEnabledByDefault()) {
                         VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(
-                                context, phoneAccount, true);
+                                context, phoneAccount, true, false);
+                    }
+
+                    if (carrierConfigHelper.isEnabledByDefault() ||
+                            VisualVoicemailSettingsUtil.isEnabledByUserOverride(
+                                    context, phoneAccount)) {
                         carrierConfigHelper.startActivation();
                     } else {
                         // It may be that the source was not registered to begin with but we want
                         // to run through the steps to remove the source just in case.
+                        VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(
+                                context, phoneAccount, false, false);
                         OmtpVvmSourceManager.getInstance(context).removeSource(phoneAccount);
                         carrierConfigHelper.startDeactivation();
                     }
diff --git a/src/com/android/phone/vvm/omtp/VvmPackageInstallReceiver.java b/src/com/android/phone/vvm/omtp/VvmPackageInstallReceiver.java
index 1541f2d..5559feb 100644
--- a/src/com/android/phone/vvm/omtp/VvmPackageInstallReceiver.java
+++ b/src/com/android/phone/vvm/omtp/VvmPackageInstallReceiver.java
@@ -21,6 +21,7 @@
 import android.telecom.PhoneAccountHandle;
 
 import com.android.phone.PhoneUtils;
+import com.android.phone.settings.VisualVoicemailSettingsUtil;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
 
 import java.util.Set;
@@ -44,11 +45,17 @@
         OmtpVvmSourceManager vvmSourceManager = OmtpVvmSourceManager.getInstance(context);
         Set<PhoneAccountHandle> phoneAccounts = vvmSourceManager.getOmtpVvmSources();
         for (PhoneAccountHandle phoneAccount : phoneAccounts) {
+            if (VisualVoicemailSettingsUtil.isEnabledByUserOverride(context, phoneAccount)) {
+                // Skip the check if this voicemail source is enabled by the user.
+                continue;
+            }
+
             OmtpVvmCarrierConfigHelper carrierConfigHelper = new OmtpVvmCarrierConfigHelper(
                     context, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccount));
             if (packageName.equals(carrierConfigHelper.getCarrierVvmPackageName())) {
+                VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(
+                        context, phoneAccount, false, false);
                 OmtpVvmSourceManager.getInstance(context).removeSource(phoneAccount);
-                carrierConfigHelper.startDeactivation();
             }
         }
     }
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
index e7a189d..37fb5e4 100644
--- a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
+++ b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
@@ -95,7 +95,6 @@
                 VoicemailContract.Status.DATA_CHANNEL_STATE_NO_CONNECTION,
                 VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);
         removePhoneStateListener(phoneAccount);
-        VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(mContext, phoneAccount, false);
     }
 
     public void addPhoneStateListener(PhoneAccountHandle phoneAccount) {