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

Commit 5cfa0922 authored by Neil Fuller's avatar Neil Fuller
Browse files

Tidy up clock adjustment code / logging

Tidy up some of the clock adjustment code. On a structural
level this removes a few unusual/edge cases that wouldn't
yield particularly useful results:

1) Looking for a UTC-based time zone in a country known not to
use UTC (unlikely to yield a result).
2) Adjusting the system clock when all that is known is the country
(this would previously have set the device to the time zone
the device is using, so probably a no-op).

In the actual adjustment code, rather than adjust the current
device system clock, the time from the NITZ signal is used instead.
The original purpose of this code was apparently to handle NITZ
data where the network is pushing out local time rather than
UTC so it makes sense to base the time off the NITZ data.

This change also makes some non-functional logging changes.

Test: atest FrameworksTelephonyTests
Bug: 63743683
Bug: 37494871
Change-Id: Id0fd20cbe9cf97911ec43eee847aa84b1b32b42f
Merged-In: Id0fd20cbe9cf97911ec43eee847aa84b1b32b42f
(cherry picked from commit 85f8b089)
parent 4ce44fde
Loading
Loading
Loading
Loading
+132 −115
Original line number Original line Diff line number Diff line
@@ -131,13 +131,13 @@ public class NitzStateMachine {
    // Time Zone detection state.
    // Time Zone detection state.


    /**
    /**
     * Sometimes we get the NITZ time before we know what country we
     * Sometimes we get the NITZ time before we know what country we are in. We keep the time zone
     * are in. Keep the time zone information from the NITZ string in
     * information from the NITZ string in mLatestNitzSignal so we can fix the time zone once we
     * mNitzData so we can fix the time zone once know the country.
     * know the country.
     */
     */
    private boolean mNeedCountryCodeForNitz = false;
    private boolean mNeedCountryCodeForNitz = false;


    private NitzData mNitzData;
    private TimeStampedValue<NitzData> mLatestNitzSignal;
    private boolean mGotCountryCode = false;
    private boolean mGotCountryCode = false;
    private String mSavedTimeZoneId;
    private String mSavedTimeZoneId;


@@ -225,65 +225,77 @@ public class NitzStateMachine {
            final boolean isTimeZoneSettingInitialized =
            final boolean isTimeZoneSettingInitialized =
                    mTimeServiceHelper.isTimeZoneSettingInitialized();
                    mTimeServiceHelper.isTimeZoneSettingInitialized();
            if (DBG) {
            if (DBG) {
                Rlog.d(LOG_TAG, "fixTimeZone"
                Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet:"
                        + " isTimeZoneSettingInitialized=" + isTimeZoneSettingInitialized
                        + " isTimeZoneSettingInitialized=" + isTimeZoneSettingInitialized
                        + " mNitzData=" + mNitzData
                        + " mLatestNitzSignal=" + mLatestNitzSignal
                        + " iso-cc='" + isoCountryCode
                        + " isoCountryCode=" + isoCountryCode);
                        + "' countryUsesUtc="
                        + mTimeZoneLookupHelper.countryUsesUtc(isoCountryCode));
            }
            }
            String zoneId;
            String zoneId;
            if (TextUtils.isEmpty(isoCountryCode) && mNeedCountryCodeForNitz) {
            if (TextUtils.isEmpty(isoCountryCode) && mNeedCountryCodeForNitz) {
                // Country code not found.  This is likely a test network.
                // Country code not found.  This is likely a test network.
                // Get a TimeZone based only on the NITZ parameters (best guess).
                // Get a TimeZone based only on the NITZ parameters (best guess).


                // mNeedCountryCodeForNitz is only set to true when mNitzData is set so there's no
                // mNeedCountryCodeForNitz is only set to true when mLatestNitzSignal is set so
                // need to check mNitzData == null.
                // there's no need to check mLatestNitzSignal == null.
                zoneId = mTimeZoneLookupHelper.guessZoneIdByNitz(mNitzData);
                zoneId = mTimeZoneLookupHelper.guessZoneIdByNitz(mLatestNitzSignal.mValue);
                if (DBG) {
                if (DBG) {
                    Rlog.d(LOG_TAG, "fixTimeZone(): guessNitzTimeZone returned " + zoneId);
                    Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet: guessZoneIdByNitz() returned"
                            + " zoneId=" + zoneId);
                }
                }
            } else if ((mNitzData == null || nitzOffsetMightBeBogus(mNitzData))
            } else if (mLatestNitzSignal == null) {
                zoneId = null;
                if (DBG) {
                    Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet: No cached NITZ data available,"
                            + " not setting zone");
                }
            } else { // mLatestNitzSignal != null
                if (nitzOffsetMightBeBogus(mLatestNitzSignal.mValue)
                        && isTimeZoneSettingInitialized
                        && isTimeZoneSettingInitialized
                    && !mTimeZoneLookupHelper.countryUsesUtc(isoCountryCode)) {
                        && !countryUsesUtc(isoCountryCode, mLatestNitzSignal)) {


                // This case means that (1) the device received no NITZ signal yet or received an
                    // This case means that (1) the device received an NITZ signal that could be
                // NITZ signal that looks bogus due to having a zero offset from UTC, (2) the device
                    // bogus due to having a zero offset from UTC, (2) the device has a time zone
                // has a time zone set explicitly, and (3) the iso tells us the country is NOT one
                    // set explicitly and (3) the iso tells us the country is NOT one that uses a
                // that uses a zero offset. This is interpreted as being NITZ incorrectly reporting
                    // zero offset. This is interpreted as being NITZ incorrectly reporting a local
                // a local time and not a UTC time. The zone is left as the current device's zone
                    // time and not a UTC time. The zone is left as the current device's zone
                // setting, and the time may be adjusted by assuming the current zone setting is
                    // setting, and the system clock may be adjusted by taking the NITZ time and
                // correct.
                    // assuming the current zone setting is correct.
                    TimeZone zone = TimeZone.getDefault();
                    TimeZone zone = TimeZone.getDefault();
                    zoneId = zone.getID();
                    zoneId = zone.getID();


                // Note that mNeedCountryCodeForNitz => (implies) { mNitzData != null }. Therefore,
                // if mNitzData == null, mNeedCountryCodeForNitz cannot be true. The code in this
                // section therefore means that when mNitzData == null (and the country is one that
                // doesn't use a zero UTC offset) the device will retain the existing time zone
                // setting and not try to derive one from the isoCountryCode.
                    if (mNeedCountryCodeForNitz) {
                    if (mNeedCountryCodeForNitz) {
                        NitzData nitzData = mLatestNitzSignal.mValue;
                        try {
                        try {
                        // Acquire the wakelock as we're reading the system clock here.
                            // Acquire the wakelock as we're reading the elapsed realtime clock
                            // here.
                            mWakeLock.acquire();
                            mWakeLock.acquire();


                        long ctm = mDeviceState.currentTimeMillis();
                            // Use the time that came with the NITZ offset that we think is bogus:
                        long tzOffset = zone.getOffset(ctm);
                            // we just interpret it as local time.
                            long ctm = nitzData.getCurrentTimeInMillis();
                            long delayAdjustedCtm = ctm + (mDeviceState.elapsedRealtime()
                                    - mLatestNitzSignal.mElapsedRealtime);
                            long tzOffset = zone.getOffset(delayAdjustedCtm);
                            if (DBG) {
                            if (DBG) {
                            Rlog.d(LOG_TAG, "fixTimeZone: tzOffset=" + tzOffset
                                Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet:"
                                    + " ltod=" + TimeUtils.logTimeOfDay(ctm));
                                        + " tzOffset=" + tzOffset
                                        + " delayAdjustedCtm="
                                        + TimeUtils.logTimeOfDay(delayAdjustedCtm));
                            }
                            }
                            if (mTimeServiceHelper.isTimeDetectionEnabled()) {
                            if (mTimeServiceHelper.isTimeDetectionEnabled()) {
                            long adj = ctm - tzOffset;
                                long timeZoneAdjustedCtm = delayAdjustedCtm - tzOffset;
                            String msg = "fixTimeZone: adj ltod=" + TimeUtils.logTimeOfDay(adj);
                                String msg = "handleNetworkCountryCodeSet: setting time"
                            setAndBroadcastNetworkSetTime(msg, adj);
                                        + " timeZoneAdjustedCtm="
                                        + TimeUtils.logTimeOfDay(timeZoneAdjustedCtm);
                                setAndBroadcastNetworkSetTime(msg, timeZoneAdjustedCtm);
                            } else {
                            } else {
                                // Adjust the saved NITZ time to account for tzOffset.
                                // Adjust the saved NITZ time to account for tzOffset.
                                mSavedNitzTime = new TimeStampedValue<>(
                                mSavedNitzTime = new TimeStampedValue<>(
                                        mSavedNitzTime.mValue - tzOffset,
                                        mSavedNitzTime.mValue - tzOffset,
                                        mSavedNitzTime.mElapsedRealtime);
                                        mSavedNitzTime.mElapsedRealtime);
                                if (DBG) {
                                if (DBG) {
                                Rlog.d(LOG_TAG, "fixTimeZone: adj mSavedTime=" + mSavedNitzTime);
                                    Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet:"
                                            + "adjusting time mSavedNitzTime=" + mSavedNitzTime);
                                }
                                }
                            }
                            }
                        } finally {
                        } finally {
@@ -291,48 +303,53 @@ public class NitzStateMachine {
                        }
                        }
                    }
                    }
                    if (DBG) {
                    if (DBG) {
                    Rlog.d(LOG_TAG, "fixTimeZone: using default TimeZone");
                        Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet: using default TimeZone");
                }
            } else if (mNitzData == null) {
                // The use of 1/1/1970 UTC is unusual but consistent with historical behavior when
                // it wasn't possible to detect whether a previous NITZ signal had been saved.
                zoneId = mTimeZoneLookupHelper.guessZoneIdByInstantOffsetDstCountry(
                        0 /* when */, 0 /* offset */, false /* dst */, isoCountryCode);
                if (DBG) {
                    Rlog.d(LOG_TAG, "fixTimeZone: No cached NITZ data available, using only country"
                            + " code. zoneId=" + zoneId);
                    }
                    }
                } else {
                } else {
                zoneId = mTimeZoneLookupHelper.guessZoneIdByNitzCountry(mNitzData, isoCountryCode);
                    NitzData nitzData = mLatestNitzSignal.mValue;
                    zoneId = mTimeZoneLookupHelper.guessZoneIdByNitzCountry(
                            nitzData, isoCountryCode);
                    if (DBG) {
                    if (DBG) {
                    Rlog.d(LOG_TAG, "fixTimeZone: using getTimeZone(off, dst, time, iso)");
                        Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet: using"
                                + " guessZoneIdByNitzCountry(nitzData, isoCountryCode),"
                                + " nitzData=" + nitzData
                                + " isoCountryCode=" + isoCountryCode);
                    }
                }
                }
            }
            }
            final String tmpLog = "fixTimeZone:"
            final String tmpLog = "handleNetworkCountryCodeSet:"
                    + " isTimeZoneSettingInitialized=" + isTimeZoneSettingInitialized
                    + " isTimeZoneSettingInitialized=" + isTimeZoneSettingInitialized
                    + " mNitzData=" + mNitzData
                    + " mLatestNitzSignal=" + mLatestNitzSignal
                    + " iso-cc=" + isoCountryCode
                    + " iso-cc=" + isoCountryCode
                    + " mNeedCountryCodeForNitz=" + mNeedCountryCodeForNitz
                    + " mNeedCountryCodeForNitz=" + mNeedCountryCodeForNitz
                    + " zoneId=" + zoneId;
                    + " zoneId=" + zoneId;
            mTimeZoneLog.log(tmpLog);
            mTimeZoneLog.log(tmpLog);


            if (zoneId != null) {
            if (zoneId != null) {
                Rlog.d(LOG_TAG, "fixTimeZone: zone != null zoneId=" + zoneId);
                Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet: zoneId != null, zoneId=" + zoneId);
                if (mTimeServiceHelper.isTimeZoneDetectionEnabled()) {
                if (mTimeServiceHelper.isTimeZoneDetectionEnabled()) {
                    setAndBroadcastNetworkSetTimeZone(zoneId);
                    setAndBroadcastNetworkSetTimeZone(zoneId);
                } else {
                } else {
                    Rlog.d(LOG_TAG, "fixTimeZone: skip changing zone as getAutoTimeZone was false");
                    Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet: skip changing zone as"
                            + " isTimeZoneDetectionEnabled() is false");
                }
                }
                if (mNeedCountryCodeForNitz) {
                if (mNeedCountryCodeForNitz) {
                    saveNitzTimeZone(zoneId);
                    saveNitzTimeZone(zoneId);
                }
                }
            } else {
            } else {
                Rlog.d(LOG_TAG, "fixTimeZone: zone == null, do nothing for zone");
                Rlog.d(LOG_TAG, "handleNetworkCountryCodeSet: zoneId == null, do nothing");
            }
            }
            mNeedCountryCodeForNitz = false;
            mNeedCountryCodeForNitz = false;
        }
        }
    }
    }


    private boolean countryUsesUtc(
            String isoCountryCode, TimeStampedValue<NitzData> nitzSignal) {
        return mTimeZoneLookupHelper.countryUsesUtc(
                isoCountryCode,
                nitzSignal.mValue.getCurrentTimeInMillis());
    }

    /**
    /**
     * Informs the {@link NitzStateMachine} that the network has become available.
     * Informs the {@link NitzStateMachine} that the network has become available.
     */
     */
@@ -392,33 +409,23 @@ public class NitzStateMachine {
                    // based only on the NITZ parameters.
                    // based only on the NITZ parameters.
                    zoneId = mTimeZoneLookupHelper.guessZoneIdByNitz(newNitzData);
                    zoneId = mTimeZoneLookupHelper.guessZoneIdByNitz(newNitzData);
                    if (DBG) {
                    if (DBG) {
                        Rlog.d(LOG_TAG, "setTimeZoneFromNitz(): findByNitz returned " + zoneId);
                        Rlog.d(LOG_TAG, "handleTimeZoneFromNitz: guessZoneIdByNitz returned"
                                + " zoneId=" + zoneId);
                    }
                    }
                }
                }
            }
            }


            int previousUtcOffset;
            boolean previousIsDst;
            if (mNitzData == null) {
                // No previously saved NITZ data. Use the same defaults as Android would have done
                // before it was possible to detect this case.
                previousUtcOffset = 0;
                previousIsDst = false;
            } else {
                previousUtcOffset = mNitzData.getLocalOffsetMillis();
                previousIsDst = mNitzData.isDst();
            }
            if ((zoneId == null)
            if ((zoneId == null)
                    || (newNitzData.getLocalOffsetMillis() != previousUtcOffset)
                    || mLatestNitzSignal == null
                    || (newNitzData.isDst() != previousIsDst)) {
                    || offsetInfoDiffers(newNitzData, mLatestNitzSignal.mValue)) {
                // We got the time before the country or the zone has changed
                // We got the time before the country, or the zone has changed
                // so we don't know how to identify the DST rules yet.  Save
                // so we don't know how to identify the DST rules yet.  Save
                // the information and hope to fix it up later.
                // the information and hope to fix it up later.
                mNeedCountryCodeForNitz = true;
                mNeedCountryCodeForNitz = true;
                mNitzData = newNitzData;
                mLatestNitzSignal = nitzSignal;
            }
            }


            String tmpLog = "NITZ: nitzTimeSignal=" + nitzSignal
            String tmpLog = "handleTimeZoneFromNitz: nitzTimeSignal=" + nitzSignal
                    + " zoneId=" + zoneId
                    + " zoneId=" + zoneId
                    + " iso=" + iso + " mGotCountryCode=" + mGotCountryCode
                    + " iso=" + iso + " mGotCountryCode=" + mGotCountryCode
                    + " mNeedCountryCodeForNitz=" + mNeedCountryCodeForNitz
                    + " mNeedCountryCodeForNitz=" + mNeedCountryCodeForNitz
@@ -437,16 +444,23 @@ public class NitzStateMachine {
                saveNitzTimeZone(zoneId);
                saveNitzTimeZone(zoneId);
            }
            }
        } catch (RuntimeException ex) {
        } catch (RuntimeException ex) {
            Rlog.e(LOG_TAG, "NITZ: Processing NITZ data " + nitzSignal + " ex=" + ex);
            Rlog.e(LOG_TAG, "handleTimeZoneFromNitz: Processing NITZ data"
                    + " nitzSignal=" + nitzSignal
                    + " ex=" + ex);
        }
        }
    }
    }


    private static boolean offsetInfoDiffers(NitzData one, NitzData two) {
        return one.getLocalOffsetMillis() != two.getLocalOffsetMillis()
                || one.isDst() != two.isDst();
    }

    private void handleTimeFromNitz(TimeStampedValue<NitzData> nitzTimeSignal) {
    private void handleTimeFromNitz(TimeStampedValue<NitzData> nitzTimeSignal) {
        try {
        try {
            boolean ignoreNitz = mDeviceState.getIgnoreNitz();
            boolean ignoreNitz = mDeviceState.getIgnoreNitz();
            if (ignoreNitz) {
            if (ignoreNitz) {
                Rlog.d(LOG_TAG,
                Rlog.d(LOG_TAG,
                        "setTimeFromNitz: Not setting clock because gsm.ignore-nitz is set");
                        "handleTimeFromNitz: Not setting clock because gsm.ignore-nitz is set");
                return;
                return;
            }
            }


@@ -460,7 +474,7 @@ public class NitzStateMachine {
                long millisSinceNitzReceived = elapsedRealtime - nitzTimeSignal.mElapsedRealtime;
                long millisSinceNitzReceived = elapsedRealtime - nitzTimeSignal.mElapsedRealtime;
                if (millisSinceNitzReceived < 0 || millisSinceNitzReceived > Integer.MAX_VALUE) {
                if (millisSinceNitzReceived < 0 || millisSinceNitzReceived > Integer.MAX_VALUE) {
                    if (DBG) {
                    if (DBG) {
                        Rlog.d(LOG_TAG, "setTimeFromNitz: not setting time, unexpected"
                        Rlog.d(LOG_TAG, "handleTimeFromNitz: not setting time, unexpected"
                                + " elapsedRealtime=" + elapsedRealtime
                                + " elapsedRealtime=" + elapsedRealtime
                                + " nitzTimeSignal=" + nitzTimeSignal);
                                + " nitzTimeSignal=" + nitzTimeSignal);
                    }
                    }
@@ -473,7 +487,7 @@ public class NitzStateMachine {
                long gained = adjustedCurrentTimeMillis - mDeviceState.currentTimeMillis();
                long gained = adjustedCurrentTimeMillis - mDeviceState.currentTimeMillis();


                if (mTimeServiceHelper.isTimeDetectionEnabled()) {
                if (mTimeServiceHelper.isTimeDetectionEnabled()) {
                    String logMsg = "(setTimeFromNitz)"
                    String logMsg = "handleTimeFromNitz:"
                            + " nitzTimeSignal=" + nitzTimeSignal
                            + " nitzTimeSignal=" + nitzTimeSignal
                            + " adjustedCurrentTimeMillis=" + adjustedCurrentTimeMillis
                            + " adjustedCurrentTimeMillis=" + adjustedCurrentTimeMillis
                            + " millisSinceNitzReceived= " + millisSinceNitzReceived
                            + " millisSinceNitzReceived= " + millisSinceNitzReceived
@@ -517,7 +531,8 @@ public class NitzStateMachine {
                mWakeLock.release();
                mWakeLock.release();
            }
            }
        } catch (RuntimeException ex) {
        } catch (RuntimeException ex) {
            Rlog.e(LOG_TAG, "setTimeFromNitz: Processing NITZ data " + nitzTimeSignal
            Rlog.e(LOG_TAG, "handleTimeFromNitz: Processing NITZ data"
                    + " nitzTimeSignal=" + nitzTimeSignal
                    + " ex=" + ex);
                    + " ex=" + ex);
        }
        }
    }
    }
@@ -533,17 +548,18 @@ public class NitzStateMachine {
        mTimeServiceHelper.setDeviceTimeZone(zoneId);
        mTimeServiceHelper.setDeviceTimeZone(zoneId);
        if (DBG) {
        if (DBG) {
            Rlog.d(LOG_TAG,
            Rlog.d(LOG_TAG,
                    "setAndBroadcastNetworkSetTimeZone: call alarm.setTimeZone and broadcast"
                    "setAndBroadcastNetworkSetTimeZone: called setDeviceTimeZone()"
                            + " zoneId=" + zoneId);
                            + " zoneId=" + zoneId);
        }
        }
    }
    }


    private void setAndBroadcastNetworkSetTime(String msg, long time) {
    private void setAndBroadcastNetworkSetTime(String msg, long time) {
        if (!mWakeLock.isHeld()) {
        if (!mWakeLock.isHeld()) {
            Rlog.w(LOG_TAG, "Wake lock not held while setting device time (msg=" + msg + ")");
            Rlog.w(LOG_TAG, "setAndBroadcastNetworkSetTime: Wake lock not held while setting device"
                    + " time (msg=" + msg + ")");
        }
        }


        msg = "setAndBroadcastNetworkSetTime: [setting time to time=" + time + "]:" + msg;
        msg = "setAndBroadcastNetworkSetTime: [Setting time to time=" + time + "]:" + msg;
        if (DBG) {
        if (DBG) {
            Rlog.d(LOG_TAG, msg);
            Rlog.d(LOG_TAG, msg);
        }
        }
@@ -554,7 +570,8 @@ public class NitzStateMachine {


    private void handleAutoTimeEnabled() {
    private void handleAutoTimeEnabled() {
        if (DBG) {
        if (DBG) {
            Rlog.d(LOG_TAG, "Reverting to NITZ Time: mSavedTime=" + mSavedNitzTime);
            Rlog.d(LOG_TAG, "handleAutoTimeEnabled: Reverting to NITZ Time:"
                    + " mSavedNitzTime=" + mSavedNitzTime);
        }
        }
        if (mSavedNitzTime != null) {
        if (mSavedNitzTime != null) {
            try {
            try {
@@ -562,7 +579,8 @@ public class NitzStateMachine {
                mWakeLock.acquire();
                mWakeLock.acquire();


                long elapsedRealtime = mDeviceState.elapsedRealtime();
                long elapsedRealtime = mDeviceState.elapsedRealtime();
                String msg = "Reverting to NITZ time, elapsedRealtime=" + elapsedRealtime
                String msg = "mSavedNitzTime: Reverting to NITZ time"
                        + " elapsedRealtime=" + elapsedRealtime
                        + " mSavedNitzTime=" + mSavedNitzTime;
                        + " mSavedNitzTime=" + mSavedNitzTime;
                long adjustedCurrentTimeMillis =
                long adjustedCurrentTimeMillis =
                        mSavedNitzTime.mValue + (elapsedRealtime - mSavedNitzTime.mElapsedRealtime);
                        mSavedNitzTime.mValue + (elapsedRealtime - mSavedNitzTime.mElapsedRealtime);
@@ -574,7 +592,8 @@ public class NitzStateMachine {
    }
    }


    private void handleAutoTimeZoneEnabled() {
    private void handleAutoTimeZoneEnabled() {
        String tmpLog = "Reverting to NITZ TimeZone: tz=" + mSavedTimeZoneId;
        String tmpLog = "handleAutoTimeZoneEnabled: Reverting to NITZ TimeZone:"
                + " mSavedTimeZoneId=" + mSavedTimeZoneId;
        if (DBG) {
        if (DBG) {
            Rlog.d(LOG_TAG, tmpLog);
            Rlog.d(LOG_TAG, tmpLog);
        }
        }
@@ -598,7 +617,7 @@ public class NitzStateMachine {


        // Time Zone Detection State
        // Time Zone Detection State
        pw.println(" mNeedCountryCodeForNitz=" + mNeedCountryCodeForNitz);
        pw.println(" mNeedCountryCodeForNitz=" + mNeedCountryCodeForNitz);
        pw.println(" mNitzData=" + mNitzData);
        pw.println(" mLatestNitzSignal=" + mLatestNitzSignal);
        pw.println(" mGotCountryCode=" + mGotCountryCode);
        pw.println(" mGotCountryCode=" + mGotCountryCode);
        pw.println(" mSavedTimeZone=" + mSavedTimeZoneId);
        pw.println(" mSavedTimeZone=" + mSavedTimeZoneId);
        pw.println(" mNitzTimeZoneDetectionSuccessful=" + mNitzTimeZoneDetectionSuccessful);
        pw.println(" mNitzTimeZoneDetectionSuccessful=" + mNitzTimeZoneDetectionSuccessful);
@@ -633,19 +652,17 @@ public class NitzStateMachine {
        String zoneId = mTimeZoneLookupHelper.guessZoneIdByCountry(
        String zoneId = mTimeZoneLookupHelper.guessZoneIdByCountry(
                iso, mDeviceState.currentTimeMillis());
                iso, mDeviceState.currentTimeMillis());
        if (zoneId != null) {
        if (zoneId != null) {
            String logMsg = "updateTimeZoneByNetworkCountryCode: set time"
                    + " zoneId=" + zoneId
                    + " iso=" + iso;
            if (DBG) {
            if (DBG) {
                Rlog.d(LOG_TAG, "updateTimeZoneByNetworkCountryCode: set time zone=" + zoneId
                Rlog.d(LOG_TAG, logMsg);
                        + " iso=" + iso);
            }
            }

            mTimeZoneLog.log(logMsg);
            mTimeZoneLog.log("updateTimeZoneByNetworkCountryCode: set time zone=" + zoneId
                    + " iso=" + iso);
            setAndBroadcastNetworkSetTimeZone(zoneId);
            setAndBroadcastNetworkSetTimeZone(zoneId);
        } else {
        } else {
            if (DBG) {
            if (DBG) {
                Rlog.d(LOG_TAG,
                Rlog.d(LOG_TAG, "updateTimeZoneByNetworkCountryCode: no good zone for iso=" + iso);
                        "updateTimeZoneByNetworkCountryCode: no good zone for iso-cc='" + iso
                                + "', do nothing");
            }
            }
        }
        }
    }
    }
@@ -661,7 +678,7 @@ public class NitzStateMachine {
     * Returns the last NITZ data that was cached.
     * Returns the last NITZ data that was cached.
     */
     */
    public NitzData getCachedNitzData() {
    public NitzData getCachedNitzData() {
        return mNitzData;
        return mLatestNitzSignal != null ? mLatestNitzSignal.mValue : null;
    }
    }


    /**
    /**
+13 −33
Original line number Original line Diff line number Diff line
@@ -16,12 +16,12 @@


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


import android.text.TextUtils;
import android.util.TimeUtils;
import android.util.TimeUtils;


import libcore.util.CountryTimeZones;
import libcore.util.CountryTimeZones;
import libcore.util.TimeZoneFinder;
import libcore.util.TimeZoneFinder;


import java.util.Arrays;
import java.util.Date;
import java.util.Date;
import java.util.TimeZone;
import java.util.TimeZone;


@@ -32,35 +32,6 @@ import java.util.TimeZone;
public class TimeZoneLookupHelper {
public class TimeZoneLookupHelper {
    private static final int MS_PER_HOUR = 60 * 60 * 1000;
    private static final int MS_PER_HOUR = 60 * 60 * 1000;


    /**
     * List of ISO codes for countries that can have an offset of
     * GMT+0 when not in daylight savings time.  This ignores some
     * small places such as the Canary Islands (Spain) and
     * Danmarkshavn (Denmark).  The list must be sorted by code.
     */
    private static final String[] GMT_COUNTRY_CODES = {
            "bf", // Burkina Faso
            "ci", // Cote d'Ivoire
            "eh", // Western Sahara
            "fo", // Faroe Islands, Denmark
            "gb", // United Kingdom of Great Britain and Northern Ireland
            "gh", // Ghana
            "gm", // Gambia
            "gn", // Guinea
            "gw", // Guinea Bissau
            "ie", // Ireland
            "lr", // Liberia
            "is", // Iceland
            "ma", // Morocco
            "ml", // Mali
            "mr", // Mauritania
            "pt", // Portugal
            "sl", // Sierra Leone
            "sn", // Senegal
            "st", // Sao Tome and Principe
            "tg", // Togo
    };

    public TimeZoneLookupHelper() {}
    public TimeZoneLookupHelper() {}


    /**
    /**
@@ -165,9 +136,18 @@ public class TimeZoneLookupHelper {


    /**
    /**
     * Returns {@code true} if the supplied (lower-case) ISO country code is for a country known to
     * Returns {@code true} if the supplied (lower-case) ISO country code is for a country known to
     * use a raw offset of zero from UTC.
     * use a raw offset of zero from UTC at the time specified.
     */
     */
    public boolean countryUsesUtc(String isoCountryCode) {
    public boolean countryUsesUtc(String isoCountryCode, long whenMillis) {
        return Arrays.binarySearch(GMT_COUNTRY_CODES, isoCountryCode) >= 0;
        if (TextUtils.isEmpty(isoCountryCode)) {
            return false;
        }

        CountryTimeZones countryTimeZones =
                TimeZoneFinder.getInstance().lookupCountryTimeZones(isoCountryCode);
        if (countryTimeZones == null) {
            return false;
        }
        return countryTimeZones.hasUtcZone(whenMillis);
    }
    }
}
}
+15 −12

File changed.

Preview size limit exceeded, changes collapsed.