Make profile selection only if above threshold

Make profile selection only if above threshold
Fix the filtered array to be added infinitely.

Bug: 120847469
Test: verified manually
Change-Id: Ie5db72fbd014710c402498ad8634540cd90a0802
diff --git a/src/com/android/ons/ONSNetworkScanCtlr.java b/src/com/android/ons/ONSNetworkScanCtlr.java
index 0547dc7..dab0992 100644
--- a/src/com/android/ons/ONSNetworkScanCtlr.java
+++ b/src/com/android/ons/ONSNetworkScanCtlr.java
@@ -19,7 +19,9 @@
 import android.content.Context;
 import android.os.Handler;
 import android.os.Message;
+import android.os.PersistableBundle;
 import android.telephony.AccessNetworkConstants;
+import android.telephony.CarrierConfigManager;
 import android.telephony.CellInfo;
 import android.telephony.CellInfoLte;
 import android.telephony.NetworkScan;
@@ -47,6 +49,7 @@
     private static final int SEARCH_PERIODICITY_SLOW = (int) TimeUnit.MINUTES.toSeconds(5);
     private static final int SEARCH_PERIODICITY_FAST = (int) TimeUnit.MINUTES.toSeconds(1);
     private static final int MAX_SEARCH_TIME = (int) TimeUnit.MINUTES.toSeconds(1);
+    private static final int SCAN_RESTART_TIME = (int) TimeUnit.MINUTES.toMillis(1);
     private final Object mLock = new Object();
 
     /* message  to handle scan responses from modem */
@@ -60,6 +63,9 @@
     private NetworkScanRequest mCurrentScanRequest;
     private List<String> mMccMncs;
     private TelephonyManager mTelephonyManager;
+    private CarrierConfigManager configManager;
+    private int mRsrpEntryThreshold;
+    private int mRssnrEntryThreshold;
     @VisibleForTesting
     protected NetworkAvailableCallBack mNetworkAvailableCallBack;
 
@@ -106,7 +112,7 @@
         public void onComplete() {
             logDebug("Scan completed!");
             Message message = Message.obtain(mHandler, MSG_SCAN_COMPLETE, NetworkScan.SUCCESS);
-            message.sendToTarget();
+            mHandler.sendMessageDelayed(message, SCAN_RESTART_TIME);
         }
 
         @Override
@@ -134,6 +140,19 @@
         void onError(int error);
     }
 
+    private int getIntCarrierConfig(String key) {
+        PersistableBundle b = null;
+        if (configManager != null) {
+            // If an invalid subId is used, this bundle will contain default values.
+            b = configManager.getConfig();
+        }
+        if (b != null) {
+            return b.getInt(key);
+        } else {
+            // Return static default defined in CarrierConfigManager.
+            return CarrierConfigManager.getDefaultConfig().getInt(key);
+        }
+    }
 
     /**
      * analyze scan results
@@ -141,20 +160,27 @@
      */
     public void analyzeScanResults(List<CellInfo> results) {
         /* Inform registrants about availability of network */
-        if (mIsScanActive && results != null) {
-            List<CellInfo> filteredResults = new ArrayList<CellInfo>();
-            synchronized (mLock) {
-                for (CellInfo cellInfo : results) {
-                    if (mMccMncs.contains(getMccMnc(cellInfo))) {
-                        filteredResults.add(cellInfo);
+        if (!mIsScanActive || results == null) {
+          return;
+        }
+        List<CellInfo> filteredResults = new ArrayList<CellInfo>();
+        synchronized (mLock) {
+            for (CellInfo cellInfo : results) {
+                if (mMccMncs.contains(getMccMnc(cellInfo))) {
+                    if (cellInfo instanceof CellInfoLte) {
+                        int rsrp = ((CellInfoLte) cellInfo).getCellSignalStrength().getRsrp();
+                        logDebug("cell info rsrp: " + rsrp);
+                        // Todo(b/122917491)
+                        if (rsrp >= mRsrpEntryThreshold) {
+                            filteredResults.add(cellInfo);
+                        }
                     }
                 }
             }
-
-            if ((filteredResults.size() >= 1) && (mNetworkAvailableCallBack != null)) {
-                /* Todo: change to aggregate results on success. */
-                mNetworkAvailableCallBack.onNetworkAvailability(filteredResults);
-            }
+        }
+        if ((filteredResults.size() >= 1) && (mNetworkAvailableCallBack != null)) {
+            /* Todo: change to aggregate results on success. */
+            mNetworkAvailableCallBack.onNetworkAvailability(filteredResults);
         }
     }
 
@@ -181,11 +207,13 @@
      * @param telephonyManager Telephony manager instance
      * @param networkAvailableCallBack callback to be called when network selection is done
      */
-    public void init(Context c, TelephonyManager telephonyManager,
+    public void init(Context context, TelephonyManager telephonyManager,
             NetworkAvailableCallBack networkAvailableCallBack) {
         log("init called");
         mTelephonyManager = telephonyManager;
         mNetworkAvailableCallBack = networkAvailableCallBack;
+        configManager = (CarrierConfigManager) context.getSystemService(
+                Context.CARRIER_CONFIG_SERVICE);
     }
 
     /* get mcc mnc from cell info if the cell is for LTE */
@@ -247,6 +275,13 @@
             /* Need to stop current scan if we already have one */
             stopNetworkScan();
 
+            mRsrpEntryThreshold =
+                getIntCarrierConfig(
+                    CarrierConfigManager.KEY_OPPORTUNISTIC_NETWORK_ENTRY_THRESHOLD_RSRP_INT);
+            mRssnrEntryThreshold =
+                getIntCarrierConfig(
+                    CarrierConfigManager.KEY_OPPORTUNISTIC_NETWORK_ENTRY_THRESHOLD_RSSNR_INT);
+
             /* start new scan */
             networkScan = mTelephonyManager.requestNetworkScan(networkScanRequest,
                     mNetworkScanCallback);
@@ -262,6 +297,7 @@
 
     private void restartScan() {
         NetworkScan networkScan;
+        logDebug("restartScan");
         synchronized (mLock) {
             if (mCurrentScanRequest != null) {
                 networkScan = mTelephonyManager.requestNetworkScan(mCurrentScanRequest,
diff --git a/src/com/android/ons/ONSProfileSelector.java b/src/com/android/ons/ONSProfileSelector.java
index a9824b4..16be7fe 100644
--- a/src/com/android/ons/ONSProfileSelector.java
+++ b/src/com/android/ons/ONSProfileSelector.java
@@ -352,19 +352,21 @@
         Collections.sort(availableNetworks, new SortAvailableNetworks());
         int availableNetworksIndex = 0;
         int subscriptionInfoListIndex = 0;
-        SubscriptionInfo subscriptionInfo = subscriptionInfoList.get(subscriptionInfoListIndex);
-        AvailableNetworkInfo availableNetwork = availableNetworks.get(availableNetworksIndex);
+        SubscriptionInfo subscriptionInfo;
+        AvailableNetworkInfo availableNetwork;
 
-        while (availableNetworksIndex <= availableNetworks.size()
-                && subscriptionInfoListIndex <= subscriptionInfoList.size()) {
+        while (availableNetworksIndex < availableNetworks.size()
+                && subscriptionInfoListIndex < subscriptionInfoList.size()) {
+            subscriptionInfo = subscriptionInfoList.get(subscriptionInfoListIndex);
+            availableNetwork = availableNetworks.get(availableNetworksIndex);
             if (subscriptionInfo.getSubscriptionId() == availableNetwork.getSubId()) {
                 filteredAvailableNetworks.add(availableNetwork);
+                subscriptionInfoListIndex++;
+                availableNetworksIndex++;
             } else if (subscriptionInfo.getSubscriptionId() < availableNetwork.getSubId()) {
                 subscriptionInfoListIndex++;
-                subscriptionInfo = subscriptionInfoList.get(subscriptionInfoListIndex);
             } else {
                 availableNetworksIndex++;
-                availableNetwork = availableNetworks.get(availableNetworksIndex);
             }
         }
         return filteredAvailableNetworks;
@@ -404,6 +406,7 @@
             }
         } else if (mOppSubscriptionInfos.size() == 0) {
             /* check if no profile */
+            logDebug("stopping scan");
             mNetworkScanCtlr.stopNetworkScan();
         }
     }
@@ -494,6 +497,7 @@
             Collections.sort(mAvailableNetworkInfos, new SortAvailableNetworksInPriority());
             mIsEnabled = true;
         }
+        logDebug("startProfileSelection availableNetworks: " + availableNetworks);
         Message message = Message.obtain(mHandler, MSG_START_PROFILE_SELECTION,
                 availableNetworks);
         message.sendToTarget();
@@ -524,6 +528,7 @@
      * stop profile selection procedure
      */
     public void stopProfileSelection() {
+        logDebug("stopProfileSelection");
         mNetworkScanCtlr.stopNetworkScan();
         /* Todo : bring down the stack */