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 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.LocaleInfo;

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

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

    /**
+2 −2
Original line number 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.
                mTimeZoneLog.log(logMsg);

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

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

package com.android.internal.telephony;

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

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

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

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

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

        /** 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) {
            this.zoneId = zoneId;
            this.isOnlyMatch = isOnlyMatch;
        public OffsetResult(TimeZone timeZone, boolean isOnlyMatch) {
            mTimeZone = Objects.requireNonNull(timeZone);
            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
@@ -54,27 +69,21 @@ public class TimeZoneLookupHelper {
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            OffsetResult result = (OffsetResult) o;

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

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

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

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

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

        CountryTimeZones.OffsetResult offsetResult = countryTimeZones.lookupByOffsetWithBias(
                nitzData.getLocalOffsetMillis(), nitzData.isDst(),
@@ -168,7 +177,7 @@ public class TimeZoneLookupHelper {
        if (offsetResult == 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
     * 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 ==
     * according to the device's current system clock time). If this is not the case then
     * {@code null} can be returned.
     * Returns information about the time zones used in a country at a given time.
     *
     * {@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 CountryResult lookupByCountry(String isoCountryCode, long whenMillis) {
        CountryTimeZones countryTimeZones = getCountryTimeZones(isoCountryCode);
@@ -207,6 +215,19 @@ public class TimeZoneLookupHelper {
                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.
     * This is a static method for use by {@link ServiceStateTracker}.
@@ -219,7 +240,7 @@ public class TimeZoneLookupHelper {
     */
    static TimeZone guessZoneByNitzStatic(NitzData 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) {
@@ -246,7 +267,7 @@ public class TimeZoneLookupHelper {
        Date d = new Date(timeMillis);
        boolean isOnlyMatch = true;
        for (String zone : zones) {
            TimeZone tz = TimeZone.getTimeZone(zone);
            TimeZone tz = TimeZone.getFrozenTimeZone(zone);
            if (tz.getOffset(timeMillis) == utcOffsetMillis && tz.inDaylightTime(d) == isDst) {
                if (match == null) {
                    match = tz;
@@ -260,7 +281,7 @@ public class TimeZoneLookupHelper {
        if (match == null) {
            return null;
        }
        return new OffsetResult(match.getID(), isOnlyMatch);
        return new OffsetResult(match, isOnlyMatch);
    }

    /**
+8 −4
Original line number 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.
        OffsetResult expectedLookupResult =
                new OffsetResult("America/Los_Angeles", true /* isOnlyMatch */);
                new OffsetResult(zone("America/Los_Angeles"), true /* isOnlyMatch */);
        OffsetResult actualLookupResult = mRealTimeZoneLookupHelper.lookupByNitzCountry(
                UNIQUE_US_ZONE_SCENARIO.getNitzSignal().getValue(),
                UNIQUE_US_ZONE_SCENARIO.getNetworkCountryIsoCode());
@@ -144,7 +144,7 @@ public class NitzStateMachineImplTest extends TelephonyTest {
        assertEquals(expectedCountryLookupResult, actualCountryLookupResult);

        OffsetResult expectedLookupResult =
                new OffsetResult("Europe/London", true /* isOnlyMatch */);
                new OffsetResult(zone("Europe/London"), true /* isOnlyMatch */);
        OffsetResult actualLookupResult = mRealTimeZoneLookupHelper.lookupByNitzCountry(
                UNITED_KINGDOM_SCENARIO.getNitzSignal().getValue(),
                UNITED_KINGDOM_SCENARIO.getNetworkCountryIsoCode());
@@ -653,10 +653,10 @@ public class NitzStateMachineImplTest extends TelephonyTest {
    private String checkNitzOnlyLookupIsAmbiguousAndReturnZoneId(Scenario scenario) {
        OffsetResult result =
                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
        // 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());
        return expectedZoneId;
    }
@@ -967,4 +967,8 @@ public class NitzStateMachineImplTest extends TelephonyTest {
        verify(mTimeServiceHelper, atLeast(0)).currentTimeMillis();
        verifyNoMoreInteractions(mTimeServiceHelper);
    }

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