Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8b69e60c authored by Jack Yu's avatar Jack Yu Committed by android-build-merger
Browse files

Merge "Fixed delay time overflow issue" am: 15d535ee

am: 57f740ea

Change-Id: Ibd290890512223ba5b7e0f9034eb71d019579af3
parents 6871a7c2 57f740ea
Loading
Loading
Loading
Loading
+11 −9
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.LocalLog;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;

import java.io.FileDescriptor;
@@ -83,6 +84,9 @@ public class LocaleTracker extends Handler {
    /** The delay for periodically getting cell info from the modem */
    private static final long CELL_INFO_PERIODIC_POLLING_DELAY_MS = 10 * MINUTE_IN_MILLIS;

    /** The maximum fail count to prevent delay time overflow */
    private static final int MAX_FAIL_COUNT = 30;

    private final Phone mPhone;

    /** SIM card state. Must be one of TelephonyManager.SIM_STATE_XXX */
@@ -310,15 +314,13 @@ public class LocaleTracker extends Handler {
     * @param failCount Count of invalid cell info we've got so far.
     * @return The delay time for next get cell info
     */
    private long getCellInfoDelayTime(int failCount) {
        // Exponentially grow the delay time
        long delay = CELL_INFO_MIN_DELAY_MS * (long) Math.pow(2, failCount - 1);
        if (delay < CELL_INFO_MIN_DELAY_MS) {
            delay = CELL_INFO_MIN_DELAY_MS;
        } else if (delay > CELL_INFO_MAX_DELAY_MS) {
            delay = CELL_INFO_MAX_DELAY_MS;
        }
        return delay;
    @VisibleForTesting
    public static long getCellInfoDelayTime(int failCount) {
        // Exponentially grow the delay time. Note we limit the fail count to MAX_FAIL_COUNT to
        // prevent overflow in Math.pow().
        long delay = CELL_INFO_MIN_DELAY_MS
                * (long) Math.pow(2, Math.min(failCount, MAX_FAIL_COUNT) - 1);
        return Math.min(Math.max(delay, CELL_INFO_MIN_DELAY_MS), CELL_INFO_MAX_DELAY_MS);
    }

    /**
+19 −0
Original line number Diff line number Diff line
@@ -181,4 +181,23 @@ public class LocaleTrackerTest extends TelephonyTest {
        waitForHandlerAction(mLocaleTracker, 100);
        assertEquals(US_COUNTRY_CODE, mLocaleTracker.getCurrentCountry());
    }

    @Test
    @SmallTest
    public void testGetCellInfoDelayTime() throws Exception {
        assertEquals(2000, LocaleTracker.getCellInfoDelayTime(0));
        assertEquals(2000, LocaleTracker.getCellInfoDelayTime(1));
        assertEquals(4000, LocaleTracker.getCellInfoDelayTime(2));
        assertEquals(8000, LocaleTracker.getCellInfoDelayTime(3));
        assertEquals(16000, LocaleTracker.getCellInfoDelayTime(4));
        assertEquals(32000, LocaleTracker.getCellInfoDelayTime(5));
        assertEquals(64000, LocaleTracker.getCellInfoDelayTime(6));
        assertEquals(128000, LocaleTracker.getCellInfoDelayTime(7));
        assertEquals(256000, LocaleTracker.getCellInfoDelayTime(8));
        assertEquals(512000, LocaleTracker.getCellInfoDelayTime(9));

        for (int i = 10; i <= 2000; i++) {
            assertEquals(600000, LocaleTracker.getCellInfoDelayTime(i));
        }
    }
}