Merge "Move signal strength logic from DeviceStateMonitor to SignalStrengthController"
diff --git a/src/java/com/android/internal/telephony/DeviceStateMonitor.java b/src/java/com/android/internal/telephony/DeviceStateMonitor.java
index 4acbe41..3967604 100644
--- a/src/java/com/android/internal/telephony/DeviceStateMonitor.java
+++ b/src/java/com/android/internal/telephony/DeviceStateMonitor.java
@@ -41,9 +41,7 @@
 import android.os.RegistrantList;
 import android.provider.Settings;
 import android.telephony.AccessNetworkConstants.AccessNetworkType;
-import android.telephony.CarrierConfigManager;
 import android.telephony.NetworkRegistrationInfo;
-import android.telephony.SignalThresholdInfo;
 import android.util.LocalLog;
 import android.view.Display;
 
@@ -590,7 +588,6 @@
         sendDeviceState(LOW_DATA_EXPECTED, mIsLowDataExpected);
         sendDeviceState(POWER_SAVE_MODE, mIsPowerSaveOn);
         setUnsolResponseFilter(mUnsolicitedResponseFilter, true);
-        setSignalStrengthReportingCriteria();
         setLinkCapacityReportingCriteria();
         setCellInfoMinInterval(mCellInfoMinInterval);
     }
@@ -635,36 +632,6 @@
         }
     }
 
-    private void setSignalStrengthReportingCriteria() {
-        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
-                AccessNetworkThresholds.GERAN, AccessNetworkType.GERAN, true);
-        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP,
-                AccessNetworkThresholds.UTRAN, AccessNetworkType.UTRAN, true);
-        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP,
-                AccessNetworkThresholds.EUTRAN_RSRP, AccessNetworkType.EUTRAN, true);
-        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
-                AccessNetworkThresholds.CDMA2000, AccessNetworkType.CDMA2000, true);
-        if (mPhone.getHalVersion().greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
-            mPhone.setSignalStrengthReportingCriteria(
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ,
-                    AccessNetworkThresholds.EUTRAN_RSRQ, AccessNetworkType.EUTRAN, false);
-            mPhone.setSignalStrengthReportingCriteria(
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR,
-                    AccessNetworkThresholds.EUTRAN_RSSNR, AccessNetworkType.EUTRAN, true);
-
-            // Defaultly we only need SSRSRP for NGRAN signal criteria reporting
-            mPhone.setSignalStrengthReportingCriteria(
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
-                    AccessNetworkThresholds.NGRAN_RSRSRP, AccessNetworkType.NGRAN, true);
-            mPhone.setSignalStrengthReportingCriteria(
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ,
-                    AccessNetworkThresholds.NGRAN_RSRSRQ, AccessNetworkType.NGRAN, false);
-            mPhone.setSignalStrengthReportingCriteria(
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR,
-                    AccessNetworkThresholds.NGRAN_SSSINR, AccessNetworkType.NGRAN, false);
-        }
-    }
-
     private void setLinkCapacityReportingCriteria() {
         mPhone.setLinkCapacityReportingCriteria(LINK_CAPACITY_DOWNLINK_THRESHOLDS,
                 LINK_CAPACITY_UPLINK_THRESHOLDS, AccessNetworkType.GERAN);
@@ -814,115 +781,6 @@
     }
 
     /**
-     * dBm thresholds that correspond to changes in signal strength indications.
-     */
-    private static final class AccessNetworkThresholds {
-
-        /**
-         * List of dBm thresholds for GERAN {@link AccessNetworkType}.
-         *
-         * Calculated from GSM asu level thresholds - TS 27.007 Sec 8.5
-         */
-        public static final int[] GERAN = new int[] {
-            -109,
-            -103,
-            -97,
-            -89,
-        };
-
-        /**
-         * List of default dBm thresholds for UTRAN {@link AccessNetworkType}.
-         *
-         * These thresholds are taken from the WCDMA RSCP defaults in {@link CarrierConfigManager}.
-         * See TS 27.007 Sec 8.69.
-         */
-        public static final int[] UTRAN = new int[] {
-            -114, /* SIGNAL_STRENGTH_POOR */
-            -104, /* SIGNAL_STRENGTH_MODERATE */
-            -94,  /* SIGNAL_STRENGTH_GOOD */
-            -84   /* SIGNAL_STRENGTH_GREAT */
-        };
-
-        /**
-         * List of default dBm RSRP thresholds for EUTRAN {@link AccessNetworkType}.
-         *
-         * These thresholds are taken from the LTE RSRP defaults in {@link CarrierConfigManager}.
-         */
-        public static final int[] EUTRAN_RSRP = new int[] {
-            -128, /* SIGNAL_STRENGTH_POOR */
-            -118, /* SIGNAL_STRENGTH_MODERATE */
-            -108, /* SIGNAL_STRENGTH_GOOD */
-            -98,  /* SIGNAL_STRENGTH_GREAT */
-        };
-
-        /**
-         * List of default dB RSRQ thresholds for EUTRAN {@link AccessNetworkType}.
-         *
-         * These thresholds are taken from the LTE RSRQ defaults in {@link CarrierConfigManager}.
-         */
-        public static final int[] EUTRAN_RSRQ = new int[] {
-            -20,  /* SIGNAL_STRENGTH_POOR */
-            -17,  /* SIGNAL_STRENGTH_MODERATE */
-            -14,  /* SIGNAL_STRENGTH_GOOD */
-            -11   /* SIGNAL_STRENGTH_GREAT */
-        };
-
-        /**
-         * List of default dB RSSNR thresholds for EUTRAN {@link AccessNetworkType}.
-         *
-         * These thresholds are taken from the LTE RSSNR defaults in {@link CarrierConfigManager}.
-         */
-        public static final int[] EUTRAN_RSSNR = new int[] {
-            -3,  /* SIGNAL_STRENGTH_POOR */
-            1,   /* SIGNAL_STRENGTH_MODERATE */
-            5,   /* SIGNAL_STRENGTH_GOOD */
-            13   /* SIGNAL_STRENGTH_GREAT */
-        };
-
-        /**
-         * List of dBm thresholds for CDMA2000 {@link AccessNetworkType}.
-         *
-         * These correspond to EVDO level thresholds.
-         */
-        public static final int[] CDMA2000 = new int[] {
-            -105,
-            -90,
-            -75,
-            -65
-        };
-
-        /**
-         * List of dB thresholds for NGRAN {@link AccessNetworkType} RSRSRP
-         */
-        public static final int[] NGRAN_RSRSRP = new int[] {
-            -110, /* SIGNAL_STRENGTH_POOR */
-            -90, /* SIGNAL_STRENGTH_MODERATE */
-            -80, /* SIGNAL_STRENGTH_GOOD */
-            -65,  /* SIGNAL_STRENGTH_GREAT */
-        };
-
-        /**
-         * List of dB thresholds for NGRAN {@link AccessNetworkType} RSRSRP
-         */
-        public static final int[] NGRAN_RSRSRQ = new int[] {
-            -31, /* SIGNAL_STRENGTH_POOR */
-            -19, /* SIGNAL_STRENGTH_MODERATE */
-            -7, /* SIGNAL_STRENGTH_GOOD */
-            6  /* SIGNAL_STRENGTH_GREAT */
-        };
-
-        /**
-         * List of dB thresholds for NGRAN {@link AccessNetworkType} SSSINR
-         */
-        public static final int[] NGRAN_SSSINR = new int[] {
-            -5, /* SIGNAL_STRENGTH_POOR */
-            5, /* SIGNAL_STRENGTH_MODERATE */
-            15, /* SIGNAL_STRENGTH_GOOD */
-            30  /* SIGNAL_STRENGTH_GREAT */
-        };
-    }
-
-    /**
      * Downlink reporting thresholds in kbps
      *
      * <p>Threshold values taken from FCC Speed Guide when available
diff --git a/src/java/com/android/internal/telephony/SignalStrengthController.java b/src/java/com/android/internal/telephony/SignalStrengthController.java
index 858b9c7..48d91c7 100644
--- a/src/java/com/android/internal/telephony/SignalStrengthController.java
+++ b/src/java/com/android/internal/telephony/SignalStrengthController.java
@@ -52,9 +52,12 @@
     protected static final boolean DBG = false; /* STOPSHIP if true */
     protected static final String TAG = "SSCtr";
 
-    private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 1;
+    private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST   = 1;
     private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 2;
-    private static final int EVENT_ON_DEVICE_IDLE_STATE_CHANGED        = 3;
+    private static final int EVENT_ON_DEVICE_IDLE_STATE_CHANGED         = 3;
+    private static final int EVENT_RIL_CONNECTED                        = 4;
+    private static final int EVENT_RADIO_AVAILABLE                      = 5;
+
 
     private final Phone mPhone;
     private final CommandsInterface mCi;
@@ -64,6 +67,9 @@
     public SignalStrengthController(Phone phone) {
         mPhone = phone;
         mCi = mPhone.mCi;
+
+        mCi.registerForRilConnected(this, EVENT_RIL_CONNECTED, null);
+        mCi.registerForAvailable(this, EVENT_RADIO_AVAILABLE, null);
     }
 
     @Override
@@ -71,6 +77,10 @@
         if (DBG) log("received event " + msg.what);
 
         switch (msg.what) {
+            case EVENT_RIL_CONNECTED: // fall through
+            case EVENT_RADIO_AVAILABLE:
+                onReset();
+                break;
             case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
                 Pair<SignalRequestRecord, Message> pair =
                         (Pair<SignalRequestRecord, Message>) msg.obj;
@@ -324,6 +334,158 @@
         return false;
     }
 
+    private void setDefaultSignalStrengthReportingCriteria() {
+        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
+                AccessNetworkThresholds.GERAN, AccessNetworkConstants.AccessNetworkType.GERAN,
+                true);
+        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP,
+                AccessNetworkThresholds.UTRAN, AccessNetworkConstants.AccessNetworkType.UTRAN,
+                true);
+        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP,
+                AccessNetworkThresholds.EUTRAN_RSRP,
+                AccessNetworkConstants.AccessNetworkType.EUTRAN, true);
+        mPhone.setSignalStrengthReportingCriteria(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
+                AccessNetworkThresholds.CDMA2000, AccessNetworkConstants.AccessNetworkType.CDMA2000,
+                true);
+        if (mPhone.getHalVersion().greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
+            mPhone.setSignalStrengthReportingCriteria(
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ,
+                    AccessNetworkThresholds.EUTRAN_RSRQ,
+                    AccessNetworkConstants.AccessNetworkType.EUTRAN, false);
+            mPhone.setSignalStrengthReportingCriteria(
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR,
+                    AccessNetworkThresholds.EUTRAN_RSSNR,
+                    AccessNetworkConstants.AccessNetworkType.EUTRAN, true);
+
+            // Defaultly we only need SSRSRP for NGRAN signal criteria reporting
+            mPhone.setSignalStrengthReportingCriteria(
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
+                    AccessNetworkThresholds.NGRAN_RSRSRP,
+                    AccessNetworkConstants.AccessNetworkType.NGRAN, true);
+            mPhone.setSignalStrengthReportingCriteria(
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ,
+                    AccessNetworkThresholds.NGRAN_RSRSRQ,
+                    AccessNetworkConstants.AccessNetworkType.NGRAN, false);
+            mPhone.setSignalStrengthReportingCriteria(
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR,
+                    AccessNetworkThresholds.NGRAN_SSSINR,
+                    AccessNetworkConstants.AccessNetworkType.NGRAN, false);
+        }
+    }
+
+    /**
+     * dBm thresholds that correspond to changes in signal strength indications.
+     */
+    private static final class AccessNetworkThresholds {
+
+        /**
+         * List of dBm thresholds for GERAN {@link AccessNetworkConstants.AccessNetworkType}.
+         *
+         * Calculated from GSM asu level thresholds - TS 27.007 Sec 8.5
+         */
+        public static final int[] GERAN = new int[]{
+                -109,
+                -103,
+                -97,
+                -89,
+        };
+
+        /**
+         * List of default dBm thresholds for UTRAN
+         * {@link AccessNetworkConstants.AccessNetworkType}.
+         *
+         * These thresholds are taken from the WCDMA RSCP defaults in {@link CarrierConfigManager}.
+         * See TS 27.007 Sec 8.69.
+         */
+        public static final int[] UTRAN = new int[]{
+                -114, /* SIGNAL_STRENGTH_POOR */
+                -104, /* SIGNAL_STRENGTH_MODERATE */
+                -94,  /* SIGNAL_STRENGTH_GOOD */
+                -84   /* SIGNAL_STRENGTH_GREAT */
+        };
+
+        /**
+         * List of default dBm RSRP thresholds for EUTRAN
+         * {@link AccessNetworkConstants.AccessNetworkType}.
+         *
+         * These thresholds are taken from the LTE RSRP defaults in {@link CarrierConfigManager}.
+         */
+        public static final int[] EUTRAN_RSRP = new int[]{
+                -128, /* SIGNAL_STRENGTH_POOR */
+                -118, /* SIGNAL_STRENGTH_MODERATE */
+                -108, /* SIGNAL_STRENGTH_GOOD */
+                -98,  /* SIGNAL_STRENGTH_GREAT */
+        };
+
+        /**
+         * List of default dB RSRQ thresholds for EUTRAN
+         * {@link AccessNetworkConstants.AccessNetworkType}.
+         *
+         * These thresholds are taken from the LTE RSRQ defaults in {@link CarrierConfigManager}.
+         */
+        public static final int[] EUTRAN_RSRQ = new int[]{
+                -20,  /* SIGNAL_STRENGTH_POOR */
+                -17,  /* SIGNAL_STRENGTH_MODERATE */
+                -14,  /* SIGNAL_STRENGTH_GOOD */
+                -11   /* SIGNAL_STRENGTH_GREAT */
+        };
+
+        /**
+         * List of default dB RSSNR thresholds for EUTRAN
+         * {@link AccessNetworkConstants.AccessNetworkType}.
+         *
+         * These thresholds are taken from the LTE RSSNR defaults in {@link CarrierConfigManager}.
+         */
+        public static final int[] EUTRAN_RSSNR = new int[]{
+                -3,  /* SIGNAL_STRENGTH_POOR */
+                1,   /* SIGNAL_STRENGTH_MODERATE */
+                5,   /* SIGNAL_STRENGTH_GOOD */
+                13   /* SIGNAL_STRENGTH_GREAT */
+        };
+
+        /**
+         * List of dBm thresholds for CDMA2000 {@link AccessNetworkConstants.AccessNetworkType}.
+         *
+         * These correspond to EVDO level thresholds.
+         */
+        public static final int[] CDMA2000 = new int[]{
+                -105,
+                -90,
+                -75,
+                -65
+        };
+
+        /**
+         * List of dB thresholds for NGRAN {@link AccessNetworkConstants.AccessNetworkType} RSRSRP
+         */
+        public static final int[] NGRAN_RSRSRP = new int[]{
+                -110, /* SIGNAL_STRENGTH_POOR */
+                -90, /* SIGNAL_STRENGTH_MODERATE */
+                -80, /* SIGNAL_STRENGTH_GOOD */
+                -65,  /* SIGNAL_STRENGTH_GREAT */
+        };
+
+        /**
+         * List of dB thresholds for NGRAN {@link AccessNetworkConstants.AccessNetworkType} RSRSRP
+         */
+        public static final int[] NGRAN_RSRSRQ = new int[]{
+                -31, /* SIGNAL_STRENGTH_POOR */
+                -19, /* SIGNAL_STRENGTH_MODERATE */
+                -7, /* SIGNAL_STRENGTH_GOOD */
+                6  /* SIGNAL_STRENGTH_GREAT */
+        };
+
+        /**
+         * List of dB thresholds for NGRAN {@link AccessNetworkConstants.AccessNetworkType} SSSINR
+         */
+        public static final int[] NGRAN_SSSINR = new int[]{
+                -5, /* SIGNAL_STRENGTH_POOR */
+                5, /* SIGNAL_STRENGTH_MODERATE */
+                15, /* SIGNAL_STRENGTH_GOOD */
+                30  /* SIGNAL_STRENGTH_GREAT */
+        };
+    }
+
     private static boolean isRanAndSignalMeasurementTypeMatch(
             @AccessNetworkConstants.RadioAccessNetworkType int ran,
             @SignalThresholdInfo.SignalMeasurementType int measurement,
@@ -394,6 +556,14 @@
         mPhone.setAlwaysReportSignalStrength(alwaysReport);
     }
 
+    /**
+     * Called when RIL is connected during boot up or after modem restart. Set the default criteria
+     * so that modem can start with default state before updated criteria is ready.
+     */
+    private void onReset() {
+        setDefaultSignalStrengthReportingCriteria();
+    }
+
     private void log(String msg) {
         if (DBG) Rlog.d(TAG, msg);
     }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/DeviceStateMonitorTest.java b/tests/telephonytests/src/com/android/internal/telephony/DeviceStateMonitorTest.java
index 44f3aa5..aa52e71 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/DeviceStateMonitorTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/DeviceStateMonitorTest.java
@@ -62,7 +62,7 @@
 public class DeviceStateMonitorTest extends TelephonyTest {
     private static final int INDICATION_FILTERS_MINIMUM = IndicationFilter.REGISTRATION_FAILURE;
 
-    // All implemented indiation filters set so far
+    // All implemented indication filters set so far
     // which is a subset of IndicationFilter.ALL
     private static final int INDICATION_FILTERS_ALL =
             IndicationFilter.SIGNAL_STRENGTH
@@ -93,7 +93,7 @@
     @Retention(RetentionPolicy.SOURCE)
     private @interface StateType {}
 
-    // Keep the same value as correspoinding event
+    // Keep the same value as corresponding event
     // See state2Event() for detail
     private static final int STATE_TYPE_RIL_CONNECTED = 0;
     // EVENT_UPDATE_NODE_CHANGED is not here, it will be removed in aosp soon
@@ -135,6 +135,7 @@
     @Mock
     UiModeManager mUiModeManager;
     private DeviceStateMonitor mDSM;
+
     // Given a stateType, return the event type that can change the state
     private int state2Event(@StateType int stateType) {
         // As long as we keep the same value, we can directly return the stateType
@@ -169,7 +170,7 @@
         // Initialize with ALL states off
         updateAllStatesToOff();
 
-        // eliminate the accumuted impact on Mockito.verify()
+        // eliminate the accumulated impact on Mockito.verify()
         reset(mSimulatedCommandsVerifier);
     }