Disable MWI when VVM is available.

Since we don't want to see two separate notifications each time we get a
new voicemail. Ignore MWI notifications when a corresponding pstn visual
voicemail source is available.

Bug: 20121229

Change-Id: I963e5eea5d66f10656a7c34926e4b10c175434f5
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index 54dfb75..c5a9f12 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -52,6 +52,7 @@
 import com.android.internal.telephony.PhoneBase;
 import com.android.internal.telephony.TelephonyCapabilities;
 import com.android.phone.settings.VoicemailSettingsActivity;
+import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
 import com.android.phone.settings.VoicemailNotificationSettingsUtil;
 import com.android.phone.settings.VoicemailProviderSettingsUtil;
 
@@ -299,11 +300,20 @@
             return;
         }
 
+        Phone phone = PhoneGlobals.getPhone(subId);
+        if (visible && phone != null) {
+            VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
+            PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
+            if (queryHelper.isNotificationsChannelActive(phoneAccount)) {
+                Log.v(LOG_TAG, "Notifications channel active for visual voicemail, hiding mwi.");
+                visible = false;
+            }
+        }
+
         Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
         mMwiVisible.put(subId, visible);
 
         if (visible) {
-            Phone phone = PhoneGlobals.getPhone(subId);
             if (phone == null) {
                 Log.w(LOG_TAG, "Found null phone for: " + subId);
                 return;
diff --git a/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
index 2d18ee7..89f69e0 100644
--- a/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
+++ b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
@@ -136,5 +136,8 @@
         // Save the IMAP credentials in the corresponding account object so they are
         // persistent and can be retrieved.
         vvmAccountSyncManager.setAccountCredentialsFromStatusMessage(account, message);
+
+        // Add a phone state listener so that changes to the communication channels can be recorded.
+        vvmAccountSyncManager.addPhoneStateListener(account);
     }
 }
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncAccountManager.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncAccountManager.java
index 4ac21b4..fcb284e 100644
--- a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncAccountManager.java
+++ b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncAccountManager.java
@@ -21,13 +21,18 @@
 import android.content.Context;
 import android.provider.VoicemailContract;
 import android.telecom.PhoneAccountHandle;
+import android.telephony.PhoneStateListener;
 import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
 import android.util.Log;
 
 import com.android.phone.PhoneUtils;
 import com.android.phone.vvm.omtp.OmtpConstants;
 import com.android.phone.vvm.omtp.sms.StatusMessage;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * A singleton class designed to assist in OMTP visual voicemail sync behavior.
  */
@@ -44,6 +49,10 @@
     private Context mContext;
     private SubscriptionManager mSubscriptionManager;
     private AccountManager mAccountManager;
+    private TelephonyManager mTelephonyManager;
+    // Each account is associated with a phone state listener for updates to whether the device
+    // is able to sync.
+    private Map<Account, PhoneStateListener> mPhoneStateListenerMap;
 
     /**
      * Private constructor. Instance should only be acquired through getInstance().
@@ -64,6 +73,9 @@
             mContext = context;
             mSubscriptionManager = SubscriptionManager.from(context);
             mAccountManager = AccountManager.get(context);
+            mTelephonyManager = (TelephonyManager)
+                    mContext.getSystemService(Context.TELEPHONY_SERVICE);
+            mPhoneStateListenerMap = new HashMap<Account, PhoneStateListener>();
         }
     }
 
@@ -100,10 +112,27 @@
                         VoicemailContract.Status.CONFIGURATION_STATE_NOT_CONFIGURED,
                         VoicemailContract.Status.DATA_CHANNEL_STATE_NO_CONNECTION,
                         VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);
+
+                removePhoneStateListener(registeredAccounts[i]);
             }
         }
     }
 
+    public void addPhoneStateListener(Account account) {
+        if (!mPhoneStateListenerMap.containsKey(account)) {
+            VvmPhoneStateListener phoneStateListener = new VvmPhoneStateListener(mContext,
+                    PhoneUtils.makePstnPhoneAccountHandle(account.name));
+            mPhoneStateListenerMap.put(account, phoneStateListener);
+            mTelephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
+        }
+    }
+
+    public void removePhoneStateListener(Account account) {
+        PhoneStateListener phoneStateListener =
+                mPhoneStateListenerMap.remove(account);
+        mTelephonyManager.listen(phoneStateListener, 0);
+    }
+
     /**
      * Check if a certain account is registered.
      *
diff --git a/src/com/android/phone/vvm/omtp/sync/VoicemailStatusQueryHelper.java b/src/com/android/phone/vvm/omtp/sync/VoicemailStatusQueryHelper.java
new file mode 100644
index 0000000..fa87a59
--- /dev/null
+++ b/src/com/android/phone/vvm/omtp/sync/VoicemailStatusQueryHelper.java
@@ -0,0 +1,88 @@
+/*
+ * 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.sync;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.VoicemailContract;
+import android.provider.VoicemailContract.Status;
+import android.telecom.PhoneAccountHandle;
+import android.util.Log;
+
+/**
+ * Construct queries to interact with the voicemail status table.
+ */
+public class VoicemailStatusQueryHelper {
+    private static final String TAG = "VoicemailStatusQueryHelper";
+
+    final static String[] PROJECTION = new String[] {
+            Status._ID,                        // 0
+            Status.NOTIFICATION_CHANNEL_STATE, // 1
+            Status.SOURCE_PACKAGE              // 2
+   };
+
+    public static final int _ID = 0;
+    public static final int NOTIFICATION_CHANNEL_STATE = 1;
+    public static final int SOURCE_PACKAGE = 2;
+
+    private Context mContext;
+    private ContentResolver mContentResolver;
+    private Uri mSourceUri;
+
+    public VoicemailStatusQueryHelper(Context context) {
+        mContext = context;
+        mContentResolver = context.getContentResolver();
+        mSourceUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName());
+    }
+
+    /**
+     * Check if the notifications channel of a voicemail source is active. That is, when a new
+     * voicemail is available, if the server able to notify the device.
+     *
+     * @return {@code true} if notifications channel is active, {@code false} otherwise.
+     */
+    public boolean isNotificationsChannelActive(PhoneAccountHandle phoneAccount) {
+        Cursor cursor = null;
+        if (phoneAccount != null) {
+            String phoneAccountComponentName = phoneAccount.getComponentName().flattenToString();
+            String phoneAccountId = phoneAccount.getId();
+            if (phoneAccountComponentName == null || phoneAccountId == null) {
+                return false;
+            }
+            try {
+                String whereClause =
+                        Status.PHONE_ACCOUNT_COMPONENT_NAME + "=? AND " +
+                        Status.PHONE_ACCOUNT_ID + "=? AND " + Status.SOURCE_PACKAGE + "=?";
+                String[] whereArgs = { phoneAccountComponentName, phoneAccountId,
+                        mContext.getPackageName()};
+                cursor = mContentResolver.query(
+                        mSourceUri, PROJECTION, whereClause, whereArgs, null);
+                if (cursor != null && cursor.moveToFirst()) {
+                    return cursor.getInt(NOTIFICATION_CHANNEL_STATE) ==
+                            Status.NOTIFICATION_CHANNEL_STATE_OK;
+                }
+            }
+            finally {
+                if (cursor != null) {
+                    cursor.close();
+                }
+            }
+        }
+        return false;
+    }
+}
diff --git a/src/com/android/phone/vvm/omtp/sync/VoicemailsQueryHelper.java b/src/com/android/phone/vvm/omtp/sync/VoicemailsQueryHelper.java
index f844e36..f45adf7 100644
--- a/src/com/android/phone/vvm/omtp/sync/VoicemailsQueryHelper.java
+++ b/src/com/android/phone/vvm/omtp/sync/VoicemailsQueryHelper.java
@@ -31,7 +31,7 @@
 import java.util.List;
 
 /**
- * Construct a queries to interact with the voicemails table.
+ * Construct queries to interact with the voicemails table.
  */
 public class VoicemailsQueryHelper {
     private static final String TAG = "VoicemailsQueryHelper";
@@ -102,8 +102,8 @@
         try {
             List<Voicemail> voicemails = new ArrayList<Voicemail>();
             while (cursor.moveToNext()) {
-                final long id = cursor.getLong(VoicemailsQueryHelper._ID);
-                final String sourceData = cursor.getString(VoicemailsQueryHelper.SOURCE_DATA);
+                final long id = cursor.getLong(_ID);
+                final String sourceData = cursor.getString(SOURCE_DATA);
                 Voicemail voicemail = Voicemail.createForUpdate(id, sourceData).build();
                 voicemails.add(voicemail);
             }
diff --git a/src/com/android/phone/vvm/omtp/sync/VvmPhoneStateListener.java b/src/com/android/phone/vvm/omtp/sync/VvmPhoneStateListener.java
new file mode 100644
index 0000000..c7c1116
--- /dev/null
+++ b/src/com/android/phone/vvm/omtp/sync/VvmPhoneStateListener.java
@@ -0,0 +1,52 @@
+/*
+ * 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.sync;
+
+import android.content.Context;
+import android.provider.VoicemailContract;
+import android.telecom.PhoneAccountHandle;
+import android.telephony.PhoneStateListener;
+import android.telephony.ServiceState;
+
+import com.android.phone.PhoneUtils;
+
+/**
+ * Check if service is lost and indicate this in the voicemail status.
+ */
+public class VvmPhoneStateListener extends PhoneStateListener {
+    private PhoneAccountHandle mPhoneAccount;
+    private Context mContext;
+    public VvmPhoneStateListener(Context context, PhoneAccountHandle accountHandle) {
+        super(PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle));
+        mContext = context;
+        mPhoneAccount = accountHandle;
+    }
+
+    @Override
+    public void onServiceStateChanged(ServiceState serviceState) {
+        if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
+            VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
+                    VoicemailContract.Status.CONFIGURATION_STATE_OK,
+                    VoicemailContract.Status.DATA_CHANNEL_STATE_OK,
+                    VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_OK);
+        } else {
+            VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
+                    VoicemailContract.Status.CONFIGURATION_STATE_OK,
+                    VoicemailContract.Status.DATA_CHANNEL_STATE_NO_CONNECTION,
+                    VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);
+        }
+    }
+}