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

Commit 2f106404 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Small telephony fixes before more radical changes"

parents f8f57c29 8473fe54
Loading
Loading
Loading
Loading
+2 −6
Original line number Original line Diff line number Diff line
@@ -30,9 +30,6 @@ import android.util.Slog;
import com.android.internal.app.LocaleStore;
import com.android.internal.app.LocaleStore;
import com.android.internal.app.LocaleStore.LocaleInfo;
import com.android.internal.app.LocaleStore.LocaleInfo;


import libcore.timezone.CountryTimeZones;
import libcore.timezone.TimeZoneFinder;

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collections;
@@ -97,9 +94,8 @@ public final class MccTable {
            return null;
            return null;
        }
        }
        final String lowerCaseCountryCode = entry.mIso;
        final String lowerCaseCountryCode = entry.mIso;
        CountryTimeZones countryTimeZones =
        TimeZoneLookupHelper timeZoneLookupHelper = new TimeZoneLookupHelper();
                TimeZoneFinder.getInstance().lookupCountryTimeZones(lowerCaseCountryCode);
        return timeZoneLookupHelper.lookupDefaultTimeZoneIdByCountry(lowerCaseCountryCode);
        return countryTimeZones == null ? null : countryTimeZones.getDefaultTimeZoneId();
    }
    }


    /**
    /**
+2 −2
Original line number Original line Diff line number Diff line
@@ -184,7 +184,7 @@ public final class NitzStateMachineImpl implements NitzStateMachine {
                // We log this in the time zone log because it has been a source of bugs.
                // We log this in the time zone log because it has been a source of bugs.
                mTimeZoneLog.log(logMsg);
                mTimeZoneLog.log(logMsg);


                zoneId = lookupResult != null ? lookupResult.zoneId : null;
                zoneId = lookupResult != null ? lookupResult.getTimeZone().getID() : null;
            } else if (mLatestNitzSignal == null) {
            } else if (mLatestNitzSignal == null) {
                if (DBG) {
                if (DBG) {
                    Rlog.d(LOG_TAG,
                    Rlog.d(LOG_TAG,
@@ -213,7 +213,7 @@ public final class NitzStateMachineImpl implements NitzStateMachine {
                            + " isoCountryCode=" + isoCountryCode
                            + " isoCountryCode=" + isoCountryCode
                            + " lookupResult=" + lookupResult);
                            + " lookupResult=" + lookupResult);
                }
                }
                zoneId = lookupResult != null ? lookupResult.zoneId : null;
                zoneId = lookupResult != null ? lookupResult.getTimeZone().getID() : null;
            }
            }


            String logMsg = "updateTimeZoneFromCountryAndNitz:"
            String logMsg = "updateTimeZoneFromCountryAndNitz:"
+1 −1
Original line number Original line Diff line number Diff line
@@ -38,6 +38,7 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.Resources;
import android.hardware.radio.V1_0.CellInfoType;
import android.hardware.radio.V1_0.CellInfoType;
import android.icu.util.TimeZone;
import android.net.NetworkCapabilities;
import android.net.NetworkCapabilities;
import android.os.AsyncResult;
import android.os.AsyncResult;
import android.os.BaseBundle;
import android.os.BaseBundle;
@@ -119,7 +120,6 @@ import java.util.Arrays;
import java.util.LinkedList;
import java.util.LinkedList;
import java.util.List;
import java.util.List;
import java.util.Objects;
import java.util.Objects;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.Pattern;
+52 −31
Original line number Original line Diff line number Diff line
@@ -16,13 +16,14 @@


package com.android.internal.telephony;
package com.android.internal.telephony;


import android.icu.util.TimeZone;
import android.text.TextUtils;
import android.text.TextUtils;


import libcore.timezone.CountryTimeZones;
import libcore.timezone.CountryTimeZones;
import libcore.timezone.TimeZoneFinder;
import libcore.timezone.TimeZoneFinder;


import java.util.Date;
import java.util.Date;
import java.util.TimeZone;
import java.util.Objects;


/**
/**
 * An interface to various time zone lookup behaviors.
 * An interface to various time zone lookup behaviors.
@@ -35,15 +36,29 @@ public class TimeZoneLookupHelper {
     */
     */
    public static final class OffsetResult {
    public static final class OffsetResult {


        /** A zone that matches the supplied criteria. See also {@link #isOnlyMatch}. */
        /** A zone that matches the supplied criteria. See also {@link #mIsOnlyMatch}. */
        public final String zoneId;
        private final TimeZone mTimeZone;


        /** True if there is only one matching time zone for the supplied criteria. */
        /** True if there is only one matching time zone for the supplied criteria. */
        public final boolean isOnlyMatch;
        private final boolean mIsOnlyMatch;


        public OffsetResult(String zoneId, boolean isOnlyMatch) {
        public OffsetResult(TimeZone timeZone, boolean isOnlyMatch) {
            this.zoneId = zoneId;
            mTimeZone = Objects.requireNonNull(timeZone);
            this.isOnlyMatch = isOnlyMatch;
            mIsOnlyMatch = isOnlyMatch;
        }

        /**
         * Returns a time zone that matches the supplied criteria.
         */
        public TimeZone getTimeZone() {
            return mTimeZone;
        }

        /**
         * Returns {@code true} if there is only one matching time zone for the supplied criteria.
         */
        public boolean getIsOnlyMatch() {
            return mIsOnlyMatch;
        }
        }


        @Override
        @Override
@@ -54,27 +69,21 @@ public class TimeZoneLookupHelper {
            if (o == null || getClass() != o.getClass()) {
            if (o == null || getClass() != o.getClass()) {
                return false;
                return false;
            }
            }

            OffsetResult that = (OffsetResult) o;
            OffsetResult result = (OffsetResult) o;
            return mIsOnlyMatch == that.mIsOnlyMatch

                    && mTimeZone.getID().equals(that.mTimeZone.getID());
            if (isOnlyMatch != result.isOnlyMatch) {
                return false;
            }
            return zoneId.equals(result.zoneId);
        }
        }


        @Override
        @Override
        public int hashCode() {
        public int hashCode() {
            int result = zoneId.hashCode();
            return Objects.hash(mTimeZone, mIsOnlyMatch);
            result = 31 * result + (isOnlyMatch ? 1 : 0);
            return result;
        }
        }


        @Override
        @Override
        public String toString() {
        public String toString() {
            return "Result{"
            return "OffsetResult{"
                    + "zoneId='" + zoneId + '\''
                    + "mTimeZone=" + mTimeZone
                    + ", isOnlyMatch=" + isOnlyMatch
                    + ", mIsOnlyMatch=" + mIsOnlyMatch
                    + '}';
                    + '}';
        }
        }
    }
    }
@@ -84,7 +93,7 @@ public class TimeZoneLookupHelper {
     */
     */
    public static final class CountryResult {
    public static final class CountryResult {


        /** A time zone for the country. */
        /** A time zone to use for the country. */
        public final String zoneId;
        public final String zoneId;


        /**
        /**
@@ -159,7 +168,7 @@ public class TimeZoneLookupHelper {
        if (countryTimeZones == null) {
        if (countryTimeZones == null) {
            return null;
            return null;
        }
        }
        android.icu.util.TimeZone bias = android.icu.util.TimeZone.getDefault();
        TimeZone bias = TimeZone.getDefault();


        CountryTimeZones.OffsetResult offsetResult = countryTimeZones.lookupByOffsetWithBias(
        CountryTimeZones.OffsetResult offsetResult = countryTimeZones.lookupByOffsetWithBias(
                nitzData.getLocalOffsetMillis(), nitzData.isDst(),
                nitzData.getLocalOffsetMillis(), nitzData.isDst(),
@@ -168,7 +177,7 @@ public class TimeZoneLookupHelper {
        if (offsetResult == null) {
        if (offsetResult == null) {
            return null;
            return null;
        }
        }
        return new OffsetResult(offsetResult.mTimeZone.getID(), offsetResult.mOneMatch);
        return new OffsetResult(offsetResult.mTimeZone, offsetResult.mOneMatch);
    }
    }


    /**
    /**
@@ -185,11 +194,10 @@ public class TimeZoneLookupHelper {
    }
    }


    /**
    /**
     * Returns a time zone ID for the country if possible. For counties that use a single time zone
     * Returns information about the time zones used in a country at a given time.
     * this will provide a good choice. For countries with multiple time zones, a time zone is
     *
     * returned if all time zones used in the country currently have the same offset (currently ==
     * {@code null} can be returned if a problem occurs during lookup, e.g. if the country code is
     * according to the device's current system clock time). If this is not the case then
     * unrecognized, if the country is uninhabited, or if there is a problem with the data.
     * {@code null} can be returned.
     */
     */
    public CountryResult lookupByCountry(String isoCountryCode, long whenMillis) {
    public CountryResult lookupByCountry(String isoCountryCode, long whenMillis) {
        CountryTimeZones countryTimeZones = getCountryTimeZones(isoCountryCode);
        CountryTimeZones countryTimeZones = getCountryTimeZones(isoCountryCode);
@@ -207,6 +215,19 @@ public class TimeZoneLookupHelper {
                whenMillis);
                whenMillis);
    }
    }


    /**
     * Returns a time zone ID for the country if possible. For counties that use a single time zone
     * this will provide a good choice. For countries with multiple time zones, a time zone is
     * returned but it may be appropriate for only part of the country. {@code null} can be returned
     * if a problem occurs during lookup, e.g. if the country code is unrecognized, if the country
     * is uninhabited, or if there is a problem with the data.
     */
    public String lookupDefaultTimeZoneIdByCountry(String isoCountryCode) {
        CountryTimeZones countryTimeZones =
                TimeZoneFinder.getInstance().lookupCountryTimeZones(isoCountryCode);
        return countryTimeZones == null ? null : countryTimeZones.getDefaultTimeZoneId();
    }

    /**
    /**
     * Finds a time zone using only information present in the supplied {@link NitzData} object.
     * Finds a time zone using only information present in the supplied {@link NitzData} object.
     * This is a static method for use by {@link ServiceStateTracker}.
     * This is a static method for use by {@link ServiceStateTracker}.
@@ -219,7 +240,7 @@ public class TimeZoneLookupHelper {
     */
     */
    static TimeZone guessZoneByNitzStatic(NitzData nitzData) {
    static TimeZone guessZoneByNitzStatic(NitzData nitzData) {
        OffsetResult result = lookupByNitzStatic(nitzData);
        OffsetResult result = lookupByNitzStatic(nitzData);
        return result != null ? TimeZone.getTimeZone(result.zoneId) : null;
        return result != null ? result.getTimeZone() : null;
    }
    }


    private static OffsetResult lookupByNitzStatic(NitzData nitzData) {
    private static OffsetResult lookupByNitzStatic(NitzData nitzData) {
@@ -246,7 +267,7 @@ public class TimeZoneLookupHelper {
        Date d = new Date(timeMillis);
        Date d = new Date(timeMillis);
        boolean isOnlyMatch = true;
        boolean isOnlyMatch = true;
        for (String zone : zones) {
        for (String zone : zones) {
            TimeZone tz = TimeZone.getTimeZone(zone);
            TimeZone tz = TimeZone.getFrozenTimeZone(zone);
            if (tz.getOffset(timeMillis) == utcOffsetMillis && tz.inDaylightTime(d) == isDst) {
            if (tz.getOffset(timeMillis) == utcOffsetMillis && tz.inDaylightTime(d) == isDst) {
                if (match == null) {
                if (match == null) {
                    match = tz;
                    match = tz;
@@ -260,7 +281,7 @@ public class TimeZoneLookupHelper {
        if (match == null) {
        if (match == null) {
            return null;
            return null;
        }
        }
        return new OffsetResult(match.getID(), isOnlyMatch);
        return new OffsetResult(match, isOnlyMatch);
    }
    }


    /**
    /**
+8 −4
Original line number Original line Diff line number Diff line
@@ -121,7 +121,7 @@ public class NitzStateMachineImplTest extends TelephonyTest {


        // isOnlyMatch == true, so the combination of country + NITZ should be enough.
        // isOnlyMatch == true, so the combination of country + NITZ should be enough.
        OffsetResult expectedLookupResult =
        OffsetResult expectedLookupResult =
                new OffsetResult("America/Los_Angeles", true /* isOnlyMatch */);
                new OffsetResult(zone("America/Los_Angeles"), true /* isOnlyMatch */);
        OffsetResult actualLookupResult = mRealTimeZoneLookupHelper.lookupByNitzCountry(
        OffsetResult actualLookupResult = mRealTimeZoneLookupHelper.lookupByNitzCountry(
                UNIQUE_US_ZONE_SCENARIO.getNitzSignal().getValue(),
                UNIQUE_US_ZONE_SCENARIO.getNitzSignal().getValue(),
                UNIQUE_US_ZONE_SCENARIO.getNetworkCountryIsoCode());
                UNIQUE_US_ZONE_SCENARIO.getNetworkCountryIsoCode());
@@ -144,7 +144,7 @@ public class NitzStateMachineImplTest extends TelephonyTest {
        assertEquals(expectedCountryLookupResult, actualCountryLookupResult);
        assertEquals(expectedCountryLookupResult, actualCountryLookupResult);


        OffsetResult expectedLookupResult =
        OffsetResult expectedLookupResult =
                new OffsetResult("Europe/London", true /* isOnlyMatch */);
                new OffsetResult(zone("Europe/London"), true /* isOnlyMatch */);
        OffsetResult actualLookupResult = mRealTimeZoneLookupHelper.lookupByNitzCountry(
        OffsetResult actualLookupResult = mRealTimeZoneLookupHelper.lookupByNitzCountry(
                UNITED_KINGDOM_SCENARIO.getNitzSignal().getValue(),
                UNITED_KINGDOM_SCENARIO.getNitzSignal().getValue(),
                UNITED_KINGDOM_SCENARIO.getNetworkCountryIsoCode());
                UNITED_KINGDOM_SCENARIO.getNetworkCountryIsoCode());
@@ -653,10 +653,10 @@ public class NitzStateMachineImplTest extends TelephonyTest {
    private String checkNitzOnlyLookupIsAmbiguousAndReturnZoneId(Scenario scenario) {
    private String checkNitzOnlyLookupIsAmbiguousAndReturnZoneId(Scenario scenario) {
        OffsetResult result =
        OffsetResult result =
                mRealTimeZoneLookupHelper.lookupByNitz(scenario.getNitzSignal().getValue());
                mRealTimeZoneLookupHelper.lookupByNitz(scenario.getNitzSignal().getValue());
        String expectedZoneId = result.zoneId;
        String expectedZoneId = result.getTimeZone().getID();
        // All our scenarios should return multiple matches. The only cases where this wouldn't be
        // All our scenarios should return multiple matches. The only cases where this wouldn't be
        // true are places that use offsets like XX:15, XX:30 and XX:45.
        // true are places that use offsets like XX:15, XX:30 and XX:45.
        assertFalse(result.isOnlyMatch);
        assertFalse(result.getIsOnlyMatch());
        assertSameOffset(scenario.getActualTimeMillis(), expectedZoneId, scenario.getTimeZoneId());
        assertSameOffset(scenario.getActualTimeMillis(), expectedZoneId, scenario.getTimeZoneId());
        return expectedZoneId;
        return expectedZoneId;
    }
    }
@@ -967,4 +967,8 @@ public class NitzStateMachineImplTest extends TelephonyTest {
        verify(mTimeServiceHelper, atLeast(0)).currentTimeMillis();
        verify(mTimeServiceHelper, atLeast(0)).currentTimeMillis();
        verifyNoMoreInteractions(mTimeServiceHelper);
        verifyNoMoreInteractions(mTimeServiceHelper);
    }
    }

    private static TimeZone zone(String zoneId) {
        return TimeZone.getFrozenTimeZone(zoneId);
    }
}
}
Loading