am 3da1ff65: Merge "Disable visual voicemail when corresponding carrier app is installed." into mnc-dev

* commit '3da1ff65477f873a40916cc973cb32bc5c4fd2c0':
  Disable visual voicemail when corresponding carrier app is installed.
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 9e54312..50741ac 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -667,5 +667,12 @@
             android:name="com.android.phone.vvm.omtp.sync.OmtpVvmSyncService"
             android:exported="false"
        />
+       <receiver android:name="com.android.phone.vvm.omtp.VvmPackageInstallReceiver">
+          <intent-filter>
+              <action android:name="android.intent.action.PACKAGE_INSTALL" />
+              <action android:name="android.intent.action.PACKAGE_ADDED" />
+              <data android:scheme="package"/>
+          </intent-filter>
+       </receiver>
     </application>
 </manifest>
diff --git a/src/com/android/phone/settings/VoicemailSettingsActivity.java b/src/com/android/phone/settings/VoicemailSettingsActivity.java
index 7df0c98..f70ee65 100644
--- a/src/com/android/phone/settings/VoicemailSettingsActivity.java
+++ b/src/com/android/phone/settings/VoicemailSettingsActivity.java
@@ -268,7 +268,6 @@
             prefSet.removePreference(mVoicemailVisualVoicemail);
         }
 
-
         updateVMPreferenceWidgets(mVoicemailProviders.getValue());
 
         // check the intent that started this activity and pop up the voicemail
@@ -388,9 +387,8 @@
             VoicemailNotificationSettingsUtil.setVibrationEnabled(
                     mPhone, Boolean.TRUE.equals(objValue));
         } else if (preference.getKey().equals(mVoicemailVisualVoicemail.getKey())) {
-            boolean isEnabled = (Boolean) objValue;
-            VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(mPhone, isEnabled);
-            if (isEnabled) {
+            if ((Boolean) objValue) {
+                VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(mPhone, true);
                 mOmtpVvmCarrierConfigHelper.startActivation();
             } else {
                 OmtpVvmSourceManager.getInstance(mPhone.getContext()).removeSource(mPhone);
diff --git a/src/com/android/phone/vvm/omtp/OmtpVvmCarrierConfigHelper.java b/src/com/android/phone/vvm/omtp/OmtpVvmCarrierConfigHelper.java
index 6fe2059..6300622 100644
--- a/src/com/android/phone/vvm/omtp/OmtpVvmCarrierConfigHelper.java
+++ b/src/com/android/phone/vvm/omtp/OmtpVvmCarrierConfigHelper.java
@@ -16,6 +16,7 @@
 package com.android.phone.vvm.omtp;
 
 import android.content.Context;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.os.PersistableBundle;
 import android.telephony.CarrierConfigManager;
 import android.telephony.SmsManager;
@@ -55,11 +56,38 @@
                 CarrierConfigManager.STRING_VVM_TYPE, null);
     }
 
+    public String getCarrierVvmPackageName() {
+        if (mCarrierConfig == null) {
+            return null;
+        }
+
+        return mCarrierConfig.getString(
+                CarrierConfigManager.STRING_CARRIER_VVM_PACKAGE_NAME, null);
+    }
+
     public boolean isOmtpVvmType() {
         return (TelephonyManager.VVM_TYPE_OMTP.equals(mVvmType) ||
                 TelephonyManager.VVM_TYPE_CVVM.equals(mVvmType));
     }
 
+    /**
+     * For checking upon sim insertion whether visual voicemail should be enabled. This method does
+     * so by checking if the carrier's voicemail app is installed.
+     */
+    public boolean isEnabledByDefault() {
+        String packageName = mCarrierConfig.getString(
+                CarrierConfigManager.STRING_CARRIER_VVM_PACKAGE_NAME);
+        if (packageName == null) {
+            return true;
+        }
+        try {
+            mContext.getPackageManager().getPackageInfo(packageName, 0);
+            return false;
+        } catch (NameNotFoundException e) {
+            return true;
+        }
+    }
+
     public void startActivation() {
         OmtpMessageSender messageSender = getMessageSender();
         if (messageSender != null) {
diff --git a/src/com/android/phone/vvm/omtp/SimChangeReceiver.java b/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
index 940f3a0..3ac1096 100644
--- a/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
+++ b/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
@@ -68,13 +68,15 @@
                     PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(
                             SubscriptionManager.getPhoneId(subId));
 
-                    if (TelephonyManager.VVM_TYPE_OMTP.equals(carrierConfigHelper.getVvmType())) {
-                        carrierConfigHelper.startActivation();
+                    if (carrierConfigHelper.isEnabledByDefault()) {
                         VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(
                                 context, phoneAccount, true);
+                        carrierConfigHelper.startActivation();
                     } else {
-                        VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(
-                                context, phoneAccount, false);
+                        // 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.
+                        OmtpVvmSourceManager.getInstance(context).removeSource(phoneAccount);
+                        carrierConfigHelper.startDeactivation();
                     }
                 }
                 break;
diff --git a/src/com/android/phone/vvm/omtp/VvmPackageInstallReceiver.java b/src/com/android/phone/vvm/omtp/VvmPackageInstallReceiver.java
new file mode 100644
index 0000000..1541f2d
--- /dev/null
+++ b/src/com/android/phone/vvm/omtp/VvmPackageInstallReceiver.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+package com.android.phone.vvm.omtp;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.telecom.PhoneAccountHandle;
+
+import com.android.phone.PhoneUtils;
+import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
+
+import java.util.Set;
+
+/**
+ * When a new package is installed, check if it matches any of the vvm carrier apps of the currently
+ * enabled dialer vvm sources.
+ */
+public class VvmPackageInstallReceiver extends BroadcastReceiver {
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (intent.getData() == null) {
+            return;
+        }
+
+        String packageName = intent.getData().getSchemeSpecificPart();
+        if (packageName == null) {
+            return;
+        }
+
+        OmtpVvmSourceManager vvmSourceManager = OmtpVvmSourceManager.getInstance(context);
+        Set<PhoneAccountHandle> phoneAccounts = vvmSourceManager.getOmtpVvmSources();
+        for (PhoneAccountHandle phoneAccount : phoneAccounts) {
+            OmtpVvmCarrierConfigHelper carrierConfigHelper = new OmtpVvmCarrierConfigHelper(
+                    context, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccount));
+            if (packageName.equals(carrierConfigHelper.getCarrierVvmPackageName())) {
+                OmtpVvmSourceManager.getInstance(context).removeSource(phoneAccount);
+                carrierConfigHelper.startDeactivation();
+            }
+        }
+    }
+}