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

Commit 5b284377 authored by jiayanhong's avatar jiayanhong Committed by Jack Yu
Browse files

Fixed delay time overflow issue

If locale tracker keeps failing to get the cell info
(e.g. in no coverage area), and the fail count exceeds
a certain number (approximately 8 hours), the exponentionaly
grown retry delay time will overflow. Then the retry delay
time will be incorrectly calculated as the minimum retry
time, which is 2 seconds. This can cause significant
power consumption issue.

Fixed it by limiting the maximum fail count to 30.

Test: manual + unit test
Bug: 116514046
Change-Id: I6e35e983165a23eff3169c5e234ef9bdebb65c68
parent 272b083b
Loading
Loading
Loading
Loading
+11 −9
Original line number Original line Diff line number Diff line
@@ -40,6 +40,7 @@ import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.LocalLog;
import android.util.LocalLog;


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


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


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


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