Merge "Fixed that RAT_UNKNOWN incorretly reported in metrics data." into nyc-mr1-dev
diff --git a/src/java/com/android/internal/telephony/TelephonyEventLog.java b/src/java/com/android/internal/telephony/TelephonyEventLog.java
index 3568624..1235701 100644
--- a/src/java/com/android/internal/telephony/TelephonyEventLog.java
+++ b/src/java/com/android/internal/telephony/TelephonyEventLog.java
@@ -6,20 +6,12 @@
 import com.android.internal.telephony.dataconnection.DataCallResponse;
 import com.android.internal.telephony.imsphone.ImsPhoneCall;
 
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.Intent;
-import android.content.ServiceConnection;
 import android.net.ConnectivityMetricsLogger;
 import android.os.Bundle;
-import android.os.IBinder;
-import android.os.Parcel;
-import android.os.RemoteException;
 import android.telephony.ServiceState;
-import android.util.Log;
-import android.util.SparseArray;
 
 import java.util.ArrayList;
+import java.util.Objects;
 
 import static com.android.internal.telephony.RILConstants.*;
 
@@ -145,10 +137,10 @@
     public static final String SERVICE_STATE_VOICE_ROAMING_TYPE = "roamingType";
     public static final String SERVICE_STATE_DATA_ROAMING_TYPE = "dataRoamingType";
     public static final String SERVICE_STATE_VOICE_ALPHA_LONG = "alphaLong";
-    public static final String SERVICE_STATE_VOICE_ALPNA_SHORT = "alphaShort";
+    public static final String SERVICE_STATE_VOICE_ALPHA_SHORT = "alphaShort";
     public static final String SERVICE_STATE_VOICE_NUMERIC = "operator";
     public static final String SERVICE_STATE_DATA_ALPHA_LONG = "dataAlphaLong";
-    public static final String SERVICE_STATE_DATA_ALPNA_SHORT = "dataAlphaShort";
+    public static final String SERVICE_STATE_DATA_ALPHA_SHORT = "dataAlphaShort";
     public static final String SERVICE_STATE_DATA_NUMERIC = "dataOperator";
     public static final String SERVICE_STATE_VOICE_RAT = "rat";
     public static final String SERVICE_STATE_DATA_RAT = "dataRat";
@@ -197,71 +189,80 @@
     private int mRilVoiceRadioTechnology = -1;
     private int mRilDataRadioTechnology = -1;
     private boolean mEmergencyOnly = false;
+    private Bundle mLastServiceStateBundle = null;
 
     public static boolean equals(Object a, Object b) {
         return (a == null ? b == null : a.equals(b));
     }
 
-    public void writeServiceStateChanged(ServiceState serviceState) {
-        Bundle b = new Bundle();
-        boolean changed = false;
-        if (mVoiceRegState != serviceState.getVoiceRegState()) {
-            mVoiceRegState = serviceState.getVoiceRegState();
-            b.putInt(SERVICE_STATE_VOICE_REG_STATE, mVoiceRegState);
-            changed = true;
-        }
-        if (mDataRegState != serviceState.getDataRegState()) {
-            mDataRegState = serviceState.getDataRegState();
-            b.putInt(SERVICE_STATE_DATA_REG_STATE, mDataRegState);
-            changed = true;
-        }
-        if (mVoiceRoamingType != serviceState.getVoiceRoamingType()) {
-            mVoiceRoamingType = serviceState.getVoiceRoamingType();
-            b.putInt(SERVICE_STATE_VOICE_ROAMING_TYPE, mVoiceRoamingType);
-            changed = true;
-        }
-        if (mDataRoamingType != serviceState.getDataRoamingType()) {
-            mDataRoamingType = serviceState.getDataRoamingType();
-            b.putInt(SERVICE_STATE_DATA_ROAMING_TYPE, mDataRoamingType);
-            changed = true;
-        }
-        if (!equals(mVoiceOperatorAlphaShort, serviceState.getVoiceOperatorAlphaShort())
-                || !equals(mVoiceOperatorNumeric, serviceState.getVoiceOperatorNumeric())) {
-            // TODO: Evaluate if we need to send AlphaLong. AlphaShort+Numeric might be enough.
-            //b.putString(SERVICE_STATE_VOICE_ALPHA_LONG, serviceState.getVoiceOperatorAlphaLong());
-            mVoiceOperatorAlphaShort = serviceState.getVoiceOperatorAlphaShort();
-            mVoiceOperatorNumeric = serviceState.getVoiceOperatorNumeric();
-            b.putString(SERVICE_STATE_VOICE_ALPNA_SHORT, mVoiceOperatorAlphaShort);
-            b.putString(SERVICE_STATE_VOICE_NUMERIC, mVoiceOperatorNumeric);
-            changed = true;
-        }
-        if (!equals(mDataOperatorAlphaShort, serviceState.getDataOperatorAlphaShort())
-                || !equals(mDataOperatorNumeric, serviceState.getDataOperatorNumeric())) {
-            // TODO: Evaluate if we need to send AlphaLong. AlphaShort+Numeric might be enough.
-            //b.putString(SERVICE_STATE_DATA_ALPHA_LONG, serviceState.getDataOperatorAlphaLong());
-            mDataOperatorAlphaShort = serviceState.getDataOperatorAlphaShort();
-            mDataOperatorNumeric = serviceState.getDataOperatorNumeric();
-            b.putString(SERVICE_STATE_DATA_ALPNA_SHORT, mDataOperatorAlphaShort);
-            b.putString(SERVICE_STATE_DATA_NUMERIC, mDataOperatorNumeric);
-            changed = true;
-        }
-        if (mRilVoiceRadioTechnology != serviceState.getRilVoiceRadioTechnology()) {
-            mRilVoiceRadioTechnology = serviceState.getRilVoiceRadioTechnology();
-            b.putInt(SERVICE_STATE_VOICE_RAT, mRilVoiceRadioTechnology);
-            changed = true;
-        }
-        if (mRilDataRadioTechnology != serviceState.getRilDataRadioTechnology()) {
-            mRilDataRadioTechnology = serviceState.getRilDataRadioTechnology();
-            b.putInt(SERVICE_STATE_DATA_RAT, mRilDataRadioTechnology);
-            changed = true;
-        }
-        if (mEmergencyOnly != serviceState.isEmergencyOnly()) {
-            mEmergencyOnly = serviceState.isEmergencyOnly();
-            b.putBoolean(SERVICE_STATE_EMERGENCY_ONLY, mEmergencyOnly);
-            changed = true;
+    private static boolean areBundlesEqual(Bundle first, Bundle second) {
+        if (first == null || second == null) {
+            return first == second;
         }
 
-        if (changed) {
+        if (first.size() != second.size()) {
+            return false;
+        }
+
+        for(String key : first.keySet()) {
+            if (key != null) {
+                final Object firstValue = first.get(key);
+                final Object secondValue = second.get(key);
+                if (!Objects.equals(firstValue, secondValue)) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    public void writeServiceStateChanged(ServiceState serviceState) {
+        Bundle b = new Bundle();
+
+        mVoiceRegState = serviceState.getVoiceRegState();
+        b.putInt(SERVICE_STATE_VOICE_REG_STATE, mVoiceRegState);
+
+        mDataRegState = serviceState.getDataRegState();
+        b.putInt(SERVICE_STATE_DATA_REG_STATE, mDataRegState);
+
+        mVoiceRoamingType = serviceState.getVoiceRoamingType();
+        b.putInt(SERVICE_STATE_VOICE_ROAMING_TYPE, mVoiceRoamingType);
+
+        mDataRoamingType = serviceState.getDataRoamingType();
+        b.putInt(SERVICE_STATE_DATA_ROAMING_TYPE, mDataRoamingType);
+
+        if (serviceState.getVoiceOperatorAlphaShort() != null) {
+            mVoiceOperatorAlphaShort = serviceState.getVoiceOperatorAlphaShort();
+            b.putString(SERVICE_STATE_VOICE_ALPHA_SHORT, mVoiceOperatorAlphaShort);
+        }
+
+        if (serviceState.getVoiceOperatorNumeric() != null) {
+            mVoiceOperatorNumeric = serviceState.getVoiceOperatorNumeric();
+            b.putString(SERVICE_STATE_VOICE_NUMERIC, mVoiceOperatorNumeric);
+        }
+
+        if (serviceState.getDataOperatorAlphaShort() != null) {
+            mDataOperatorAlphaShort = serviceState.getDataOperatorAlphaShort();
+            b.putString(SERVICE_STATE_DATA_ALPHA_SHORT, mDataOperatorAlphaShort);
+        }
+
+        if (serviceState.getDataOperatorNumeric() != null) {
+            mDataOperatorNumeric = serviceState.getDataOperatorNumeric();
+            b.putString(SERVICE_STATE_DATA_NUMERIC, mDataOperatorNumeric);
+        }
+
+        mRilVoiceRadioTechnology = serviceState.getRilVoiceRadioTechnology();
+        b.putInt(SERVICE_STATE_VOICE_RAT, mRilVoiceRadioTechnology);
+
+        mRilDataRadioTechnology = serviceState.getRilDataRadioTechnology();
+        b.putInt(SERVICE_STATE_DATA_RAT, mRilDataRadioTechnology);
+
+        mEmergencyOnly = serviceState.isEmergencyOnly();
+        b.putBoolean(SERVICE_STATE_EMERGENCY_ONLY, mEmergencyOnly);
+
+        // Only write the service state changed event if there is any field changed.
+        if (!areBundlesEqual(b, mLastServiceStateBundle)) {
+            mLastServiceStateBundle = b;
             writeEvent(TAG_SERVICE_STATE, b);
         }
     }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/TelephonyEventLogTest.java b/tests/telephonytests/src/com/android/internal/telephony/TelephonyEventLogTest.java
index 9356311..09ae0ae 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/TelephonyEventLogTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/TelephonyEventLogTest.java
@@ -132,10 +132,10 @@
         m.put(TelephonyEventLog.SERVICE_STATE_DATA_ROAMING_TYPE,
                 ServiceState.ROAMING_TYPE_NOT_ROAMING);
         //m.put(TelephonyEventLog.SERVICE_STATE_VOICE_ALPHA_LONG, "Test Voice Long");
-        m.put(TelephonyEventLog.SERVICE_STATE_VOICE_ALPNA_SHORT, "TestVoice");
+        m.put(TelephonyEventLog.SERVICE_STATE_VOICE_ALPHA_SHORT, "TestVoice");
         m.put(TelephonyEventLog.SERVICE_STATE_VOICE_NUMERIC, "12345");
         //m.put(TelephonyEventLog.SERVICE_STATE_DATA_ALPHA_LONG, "Test Date Long");
-        m.put(TelephonyEventLog.SERVICE_STATE_DATA_ALPNA_SHORT, "TestData");
+        m.put(TelephonyEventLog.SERVICE_STATE_DATA_ALPHA_SHORT, "TestData");
         m.put(TelephonyEventLog.SERVICE_STATE_DATA_NUMERIC, "67890");
         m.put(TelephonyEventLog.SERVICE_STATE_VOICE_RAT, ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
         m.put(TelephonyEventLog.SERVICE_STATE_DATA_RAT, ServiceState.RIL_RADIO_TECHNOLOGY_LTE);