Add better logs. Ignore all duplicate phone state changes.

We're only interested when the phone goes in and out of service so
ignore all other state changes. Cancel syncs if the phone goes out of
service.

Bug: 22607570
Change-Id: I63c84c29a7ca8eb9d303f29a1fe97c82db190041
diff --git a/src/com/android/phone/VvmPhoneStateListener.java b/src/com/android/phone/VvmPhoneStateListener.java
index d2d84b2..fb267cd 100644
--- a/src/com/android/phone/VvmPhoneStateListener.java
+++ b/src/com/android/phone/VvmPhoneStateListener.java
@@ -21,7 +21,9 @@
 import android.telecom.PhoneAccountHandle;
 import android.telephony.PhoneStateListener;
 import android.telephony.ServiceState;
+import android.util.Log;
 
+import com.android.phone.vvm.omtp.LocalLogHelper;
 import com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
@@ -31,9 +33,12 @@
  * Check if service is lost and indicate this in the voicemail status.
  */
 public class VvmPhoneStateListener extends PhoneStateListener {
+    private static final String TAG = "VvmPhoneStateListener";
 
     private PhoneAccountHandle mPhoneAccount;
     private Context mContext;
+    private int mPreviousState = -1;
+
     public VvmPhoneStateListener(Context context, PhoneAccountHandle accountHandle) {
         super(PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle));
         mContext = context;
@@ -42,11 +47,21 @@
 
     @Override
     public void onServiceStateChanged(ServiceState serviceState) {
-        if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
+        int state = serviceState.getState();
+        if (state == mPreviousState || (state != ServiceState.STATE_IN_SERVICE
+                && mPreviousState != ServiceState.STATE_IN_SERVICE)) {
+            // Only interested in state changes or transitioning into or out of "in service".
+            // Otherwise just quit.
+            mPreviousState = state;
+            return;
+        }
+
+        if (state == ServiceState.STATE_IN_SERVICE) {
             VoicemailStatusQueryHelper voicemailStatusQueryHelper =
                     new VoicemailStatusQueryHelper(mContext);
             if (voicemailStatusQueryHelper.isVoicemailSourceConfigured(mPhoneAccount)) {
                 if (!voicemailStatusQueryHelper.isNotificationsChannelActive(mPhoneAccount)) {
+                    Log.v(TAG, "Notifications channel is active for " + mPhoneAccount.getId());
                     VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
                             VoicemailContract.Status.CONFIGURATION_STATE_OK,
                             VoicemailContract.Status.DATA_CHANNEL_STATE_OK,
@@ -57,6 +72,9 @@
             }
 
             if (OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
+                Log.v(TAG, "Signal returned: requesting resync for " + mPhoneAccount.getId());
+                LocalLogHelper.log(TAG,
+                        "Signal returned: requesting resync for " + mPhoneAccount.getId());
                 // If the source is already registered, run a full sync in case something was missed
                 // while signal was down.
                 Intent serviceIntent = OmtpVvmSyncService.getSyncIntent(
@@ -64,6 +82,9 @@
                         true /* firstAttempt */);
                 mContext.startService(serviceIntent);
             } else {
+                Log.v(TAG, "Signal returned: reattempting activation for " + mPhoneAccount.getId());
+                LocalLogHelper.log(TAG,
+                        "Signal returned: reattempting activation for " + mPhoneAccount.getId());
                 // Otherwise initiate an activation because this means that an OMTP source was
                 // recognized but either the activation text was not successfully sent or a response
                 // was not received.
@@ -72,10 +93,16 @@
                 carrierConfigHelper.startActivation();
             }
         } else {
+            Log.v(TAG, "Notifications channel is inactive for " + mPhoneAccount.getId());
+            mContext.stopService(OmtpVvmSyncService.getSyncIntent(
+                    mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
+                    true /* firstAttempt */));
+
             VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
                     VoicemailContract.Status.CONFIGURATION_STATE_OK,
                     VoicemailContract.Status.DATA_CHANNEL_STATE_NO_CONNECTION,
                     VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);
         }
+        mPreviousState = state;
     }
 }
diff --git a/src/com/android/phone/vvm/omtp/LocalLogHelper.java b/src/com/android/phone/vvm/omtp/LocalLogHelper.java
new file mode 100644
index 0000000..a3de74f
--- /dev/null
+++ b/src/com/android/phone/vvm/omtp/LocalLogHelper.java
@@ -0,0 +1,35 @@
+/*
+ * 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 com.android.internal.telephony.PhoneFactory;
+
+/**
+ * Helper methods for adding to Telephony local logs.
+ */
+public class LocalLogHelper {
+    public static final String KEY = "OmtpVvm";
+    private static final int MAX_OMTP_VVM_LOGS = 20;
+
+    public static void log(String tag, String log) {
+        try {
+            PhoneFactory.addLocalLog(KEY, MAX_OMTP_VVM_LOGS);
+        } catch (IllegalArgumentException e){
+        } finally {
+            PhoneFactory.localLog(KEY, tag + ": " + log);
+        }
+    }
+}
diff --git a/src/com/android/phone/vvm/omtp/SimChangeReceiver.java b/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
index 0b92c59..9aff2d7 100644
--- a/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
+++ b/src/com/android/phone/vvm/omtp/SimChangeReceiver.java
@@ -89,6 +89,9 @@
                     }
 
                     if (isEnabled) {
+                        LocalLogHelper.log(TAG, "Sim state or carrier config changed: requesting"
+                                + " activation for " + phoneAccount.getId());
+
                         // Add a phone state listener so that changes to the communication channels
                         // can be recorded.
                         OmtpVvmSourceManager.getInstance(context).addPhoneStateListener(
diff --git a/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
index d768e17..1caefe6 100644
--- a/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
+++ b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
@@ -28,6 +28,7 @@
 import com.android.internal.telephony.PhoneConstants;
 import com.android.phone.PhoneUtils;
 import com.android.phone.settings.VisualVoicemailSettingsUtil;
+import com.android.phone.vvm.omtp.LocalLogHelper;
 import com.android.phone.vvm.omtp.OmtpConstants;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
@@ -69,10 +70,12 @@
 
                 Log.v(TAG, "Received SYNC sms for " + mPhoneAccount.getId() +
                         " with event" + message.getSyncTriggerEvent());
-
+                LocalLogHelper.log(TAG, "Received SYNC sms for " + mPhoneAccount.getId() +
+                        " with event" + message.getSyncTriggerEvent());
                 processSync(message);
             } else if (messageData.getPrefix() == OmtpConstants.STATUS_SMS_PREFIX) {
                 Log.v(TAG, "Received STATUS sms for " + mPhoneAccount.getId());
+                LocalLogHelper.log(TAG, "Received Status sms for " + mPhoneAccount.getId());
                 StatusMessage message = new StatusMessage(messageData);
                 updateSource(message);
             } else {
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java
index 363aab9..199e97d 100644
--- a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java
+++ b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java
@@ -32,6 +32,7 @@
 
 import com.android.phone.PhoneUtils;
 import com.android.phone.settings.VisualVoicemailSettingsUtil;
+import com.android.phone.vvm.omtp.LocalLogHelper;
 import com.android.phone.vvm.omtp.imap.ImapHelper;
 
 import java.util.HashMap;
@@ -137,7 +138,12 @@
         }
 
         String action = intent.getAction();
+
         PhoneAccountHandle phoneAccount = intent.getParcelableExtra(EXTRA_PHONE_ACCOUNT);
+
+        LocalLogHelper.log(TAG, "Sync requested: " + action +
+                " for all accounts: " + String.valueOf(phoneAccount == null));
+
         if (phoneAccount != null) {
             Log.v(TAG, "Sync requested: " + action + " - for account: " + phoneAccount);
             setupAndSendNetworkRequest(phoneAccount, action);
@@ -231,6 +237,7 @@
                     }
 
                     Log.v(TAG, "Retrying " + mAction);
+                    LocalLogHelper.log(TAG, "Immediately retrying " + mAction);
                 } else {
                     // Nothing more to do here, just exit.
                     releaseNetwork(this);
@@ -295,6 +302,7 @@
                 phoneAccount);
 
         Log.v(TAG, "Retrying "+ action + " in " + retryInterval + "ms");
+        LocalLogHelper.log(TAG, "Retrying "+ action + " in " + retryInterval + "ms");
 
         AlarmManager alarmManager = (AlarmManager)
                 this.getSystemService(Context.ALARM_SERVICE);