Merge "Ensure original connection extras are propagated to telecom." into mnc-dr-dev
diff --git a/res/values-ml-rIN/strings.xml b/res/values-ml-rIN/strings.xml
index d0c5a2f..63e2b9a 100644
--- a/res/values-ml-rIN/strings.xml
+++ b/res/values-ml-rIN/strings.xml
@@ -349,7 +349,7 @@
     <string name="auto_retry_mode_summary" msgid="4973886004067532288">"യാന്ത്രികമായി വീണ്ടും ശ്രമിക്കൽ മോഡ് പ്രവർത്തനക്ഷമമാക്കുക"</string>
     <string name="tty_mode_not_allowed_video_call" msgid="3795846787901909176">"വീഡിയോ കോൾ സമയത്ത് TTY മോഡ് മാറ്റം അനുവദനീയമല്ല"</string>
     <string name="menu_add" msgid="1882023737425114762">"കോൺടാക്റ്റ് ചേർക്കുക"</string>
-    <string name="menu_edit" msgid="7143003705504672374">"കോൺടാക്റ്റ് എഡിറ്റുചെയ്യുക"</string>
+    <string name="menu_edit" msgid="7143003705504672374">"കോൺടാ‌ക്‌റ്റ് തിരുത്തുക"</string>
     <string name="menu_delete" msgid="3977150783449642851">"കോണ്‍‌ടാക്റ്റ് ഇല്ലാതാക്കുക"</string>
     <string name="get_pin2" msgid="8204677063922225311">"PIN2 ടൈപ്പുചെയ്യുക"</string>
     <string name="name" msgid="7329028332786872378">"പേര്"</string>
diff --git a/res/values-uz-rUZ/strings.xml b/res/values-uz-rUZ/strings.xml
index 917b771..5f5b0a4 100644
--- a/res/values-uz-rUZ/strings.xml
+++ b/res/values-uz-rUZ/strings.xml
@@ -17,7 +17,7 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <string name="phoneAppLabel" product="tablet" msgid="1107073389495104784">"Mobil internet"</string>
-    <string name="phoneAppLabel" product="default" msgid="6790717591729922998">"Telefon xizmatlari"</string>
+    <string name="phoneAppLabel" product="default" msgid="6790717591729922998">"Telefon – xizmatlar"</string>
     <string name="emergencyDialerIconLabel" msgid="7812140032168171053">"Favqulodda tergich"</string>
     <string name="phoneIconLabel" msgid="2331230813161304895">"Telefon"</string>
     <string name="fdnListLabel" msgid="8630418672279521003">"Ruxsat etilgan raqamlar"</string>
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index 1faf0ee..315929b 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -344,12 +344,8 @@
             startService(intent);
 
             mCM = CallManager.getInstance();
-            boolean hasCdmaPhoneType = false;
             for (Phone phone : PhoneFactory.getPhones()) {
                 mCM.registerPhone(phone);
-                if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
-                    hasCdmaPhoneType = true;
-                }
             }
 
             // Create the NotificationMgr singleton, which is used to display
@@ -358,11 +354,9 @@
 
             mHandler.sendEmptyMessage(EVENT_START_SIP_SERVICE);
 
-            if (hasCdmaPhoneType) {
-                // Create an instance of CdmaPhoneCallState and initialize it to IDLE
-                cdmaPhoneCallState = new CdmaPhoneCallState();
-                cdmaPhoneCallState.CdmaPhoneCallStateInit();
-            }
+            // Create an instance of CdmaPhoneCallState and initialize it to IDLE
+            cdmaPhoneCallState = new CdmaPhoneCallState();
+            cdmaPhoneCallState.CdmaPhoneCallStateInit();
 
             // before registering for phone state changes
             mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
@@ -962,6 +956,15 @@
     }
 
     /**
+     * Dismisses the message waiting (voicemail) indicator.
+     *
+     * @param subId the subscription id we should dismiss the notification for.
+     */
+    public void clearMwiIndicator(int subId) {
+        notificationMgr.updateMwi(subId, false);
+    }
+
+    /**
      * "Call origin" may be used by Contacts app to specify where the phone call comes from.
      * Currently, the only permitted value for this extra is {@link #ALLOWED_EXTRA_CALL_ORIGIN}.
      * Any other value will be ignored, to make sure that malicious apps can't trick the in-call
diff --git a/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java b/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
index 45ad1cb..888a5f9 100644
--- a/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
+++ b/src/com/android/phone/settings/VisualVoicemailSettingsUtil.java
@@ -39,6 +39,10 @@
     // 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";
+    // Record the timestamp of the last full sync so that duplicate syncs can be reduced.
+    private static final String LAST_FULL_SYNC_TIMESTAMP = "last_full_sync_timestamp";
+    // Constant indicating that there has never been a full sync.
+    public static final long NO_PRIOR_FULL_SYNC = -1;
 
     // Setting for how often retries should be done.
     private static final String SYNC_RETRY_INTERVAL = "sync_retry_interval";
@@ -142,6 +146,24 @@
         editor.commit();
     }
 
+    public static void setVisualVoicemailLastFullSyncTime(Context context,
+            PhoneAccountHandle phoneAccount, long timestamp) {
+        SharedPreferences.Editor editor =
+                PreferenceManager.getDefaultSharedPreferences(context).edit();
+        editor.putLong(getVisualVoicemailSharedPrefsKey(LAST_FULL_SYNC_TIMESTAMP, phoneAccount),
+                timestamp);
+        editor.commit();
+
+    }
+
+    public static long getVisualVoicemailLastFullSyncTime(Context context,
+            PhoneAccountHandle phoneAccount) {
+        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+        return prefs.getLong(
+                getVisualVoicemailSharedPrefsKey(LAST_FULL_SYNC_TIMESTAMP, phoneAccount),
+                NO_PRIOR_FULL_SYNC);
+    }
+
     private static String getVisualVoicemailSharedPrefsKey(String key,
             PhoneAccountHandle phoneAccount) {
         return VISUAL_VOICEMAIL_SHARED_PREFS_KEY_PREFIX + key + "_" + phoneAccount.getId();
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/VvmPhoneStateListener.java b/src/com/android/phone/vvm/omtp/VvmPhoneStateListener.java
similarity index 67%
rename from src/com/android/phone/VvmPhoneStateListener.java
rename to src/com/android/phone/vvm/omtp/VvmPhoneStateListener.java
index d2d84b2..1b2e34e 100644
--- a/src/com/android/phone/VvmPhoneStateListener.java
+++ b/src/com/android/phone/vvm/omtp/VvmPhoneStateListener.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License
  */
-package com.android.phone;
+package com.android.phone.vvm.omtp;
 
 import android.content.Context;
 import android.content.Intent;
@@ -21,8 +21,10 @@
 import android.telecom.PhoneAccountHandle;
 import android.telephony.PhoneStateListener;
 import android.telephony.ServiceState;
+import android.util.Log;
 
-import com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper;
+import com.android.phone.PhoneGlobals;
+import com.android.phone.PhoneUtils;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
 import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
@@ -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,21 +47,34 @@
 
     @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,
                             VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_OK);
-                    PhoneGlobals.getInstance().notificationMgr.updateMwi(
-                            PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount), false);
+                    PhoneGlobals.getInstance().clearMwiIndicator(
+                            PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount));
                 }
             }
 
             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,20 @@
                 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 */));
+
+            if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
+                return;
+            }
+
             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/sms/OmtpMessageReceiver.java b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
index d768e17..3eefbee 100644
--- a/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
+++ b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
@@ -23,11 +23,14 @@
 import android.telecom.PhoneAccountHandle;
 import android.telecom.Voicemail;
 import android.telephony.SmsMessage;
+import android.telephony.SubscriptionManager;
 import android.util.Log;
 
 import com.android.internal.telephony.PhoneConstants;
+import com.android.phone.PhoneGlobals;
 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;
@@ -48,6 +51,11 @@
         mPhoneAccount = PhoneUtils.makePstnPhoneAccountHandle(
                 intent.getExtras().getInt(PhoneConstants.PHONE_KEY));
 
+        if (mPhoneAccount == null) {
+            Log.w(TAG, "Received message for null phone account");
+            return;
+        }
+
         if (!VisualVoicemailSettingsUtil.isVisualVoicemailEnabled(mContext, mPhoneAccount)) {
             Log.v(TAG, "Received vvm message for disabled vvm source.");
             return;
@@ -69,10 +77,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 {
@@ -141,6 +151,9 @@
                     mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
                     true /* firstAttempt */);
             mContext.startService(serviceIntent);
+
+            PhoneGlobals.getInstance().clearMwiIndicator(
+                    PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount));
         } else {
             Log.w(TAG, "Visual voicemail not available for subscriber.");
             // Override default isEnabled setting to false since visual voicemail is unable to
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
index 286dde3..0520098 100644
--- a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
+++ b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
@@ -24,12 +24,12 @@
 
 import com.android.internal.telephony.Phone;
 import com.android.phone.PhoneUtils;
-import com.android.phone.VvmPhoneStateListener;
+import com.android.phone.vvm.omtp.VvmPhoneStateListener;
 
-import java.util.HashMap;
-import java.util.HashSet;
+import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * A singleton class designed to remember the active OMTP visual voicemail sources. Because a
@@ -69,8 +69,10 @@
             mSubscriptionManager = SubscriptionManager.from(context);
             mTelephonyManager = (TelephonyManager)
                     mContext.getSystemService(Context.TELEPHONY_SERVICE);
-            mActiveVvmSources = new HashSet<PhoneAccountHandle>();
-            mPhoneStateListenerMap = new HashMap<PhoneAccountHandle, PhoneStateListener>();
+            mActiveVvmSources = Collections.newSetFromMap(
+                    new ConcurrentHashMap<PhoneAccountHandle, Boolean>(8, 0.9f, 1));
+            mPhoneStateListenerMap =
+                    new ConcurrentHashMap<PhoneAccountHandle, PhoneStateListener>(8, 0.9f, 1);
         }
     }
 
@@ -89,6 +91,13 @@
                 removeSource(phoneAccount);
             }
         }
+
+        // Remove any orphaned phone state listeners as well.
+        for (PhoneAccountHandle phoneAccount : mPhoneStateListenerMap.keySet()) {
+            if (!PhoneUtils.isPhoneAccountActive(mSubscriptionManager, phoneAccount)) {
+                removePhoneStateListener(phoneAccount);
+            }
+        }
     }
 
     public void removeSource(Phone phone) {
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSyncService.java
index 363aab9..3c47975 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;
@@ -57,6 +58,9 @@
     // Timeout used to call ConnectivityManager.requestNetwork
     private static final int NETWORK_REQUEST_TIMEOUT_MILLIS = 60 * 1000;
 
+    // Minimum time allowed between full syncs
+    private static final int MINIMUM_FULL_SYNC_INTERVAL_MILLIS = 60 * 1000;
+
     // Number of retries
     private static final int NETWORK_RETRY_COUNT = 6;
 
@@ -137,7 +141,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);
@@ -158,6 +167,20 @@
             return;
         }
 
+        if (SYNC_FULL_SYNC.equals(action)) {
+            long lastSyncTime = VisualVoicemailSettingsUtil.getVisualVoicemailLastFullSyncTime(
+                    this, phoneAccount);
+            long currentTime = System.currentTimeMillis();
+            if (currentTime - lastSyncTime < MINIMUM_FULL_SYNC_INTERVAL_MILLIS) {
+                // If it's been less than a minute since the last sync, bail.
+                Log.v(TAG, "Avoiding duplicate full sync: synced recently for "
+                        + phoneAccount.getId());
+                return;
+            }
+            VisualVoicemailSettingsUtil.setVisualVoicemailLastFullSyncTime(
+                    this, phoneAccount, currentTime);
+        }
+
         OmtpVvmNetworkRequestCallback networkCallback = new OmtpVvmNetworkRequestCallback(this,
                 phoneAccount, action);
         requestNetwork(networkCallback);
@@ -231,6 +254,7 @@
                     }
 
                     Log.v(TAG, "Retrying " + mAction);
+                    LocalLogHelper.log(TAG, "Immediately retrying " + mAction);
                 } else {
                     // Nothing more to do here, just exit.
                     releaseNetwork(this);
@@ -247,25 +271,11 @@
         @Override
         public void onLost(Network network) {
             releaseNetwork(this);
-
-            if (mRetryCount > 0) {
-                mRetryCount--;
-                requestNetwork(this);
-            } else {
-                setRetryAlarm(mPhoneAccount, mAction);
-            }
         }
 
         @Override
         public void onUnavailable() {
             releaseNetwork(this);
-
-            if (mRetryCount> 0) {
-                mRetryCount--;
-                requestNetwork(this);
-            } else {
-                setRetryAlarm(mPhoneAccount, mAction);
-            }
         }
     }
 
@@ -295,6 +305,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);