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

Commit 4a938d32 authored by Almaz Mingaleev's avatar Almaz Mingaleev Committed by Gerrit Code Review
Browse files

Merge "Refactor NitzData."

parents 4832c35c 6c3df43f
Loading
Loading
Loading
Loading
+19 −28
Original line number Diff line number Diff line
@@ -21,8 +21,11 @@ import static com.android.internal.annotations.VisibleForTesting.Visibility.PACK
import com.android.internal.annotations.VisibleForTesting;
import com.android.telephony.Rlog;

import java.util.Calendar;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Objects;
import java.util.TimeZone;
import java.util.regex.Pattern;

/**
 * Represents NITZ data. Various static methods are provided to help with parsing and interpretation
@@ -39,6 +42,8 @@ public final class NitzData {
    /* Time stamp after 19 January 2038 is not supported under 32 bit */
    private static final int MAX_NITZ_YEAR = 2037;

    private static final Pattern NITZ_SPLIT_PATTERN = Pattern.compile("[/:,+-]");

    // Stored For logging / debugging only.
    private final String mOriginalString;

@@ -70,13 +75,7 @@ public final class NitzData {
        // tz, dt are in number of quarter-hours

        try {
            /* NITZ time (hour:min:sec) will be in UTC but it supplies the timezone
             * offset as well (which we won't worry about until later) */
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
            c.clear();
            c.set(Calendar.DST_OFFSET, 0);

            String[] nitzSubs = nitz.split("[/:,+-]");
            String[] nitzSubs = NITZ_SPLIT_PATTERN.split(nitz);

            int year = 2000 + Integer.parseInt(nitzSubs[0]);
            if (year > MAX_NITZ_YEAR) {
@@ -85,23 +84,18 @@ public final class NitzData {
                }
                return null;
            }
            c.set(Calendar.YEAR, year);

            // month is 0 based!
            int month = Integer.parseInt(nitzSubs[1]) - 1;
            c.set(Calendar.MONTH, month);

            int month = Integer.parseInt(nitzSubs[1]);
            int date = Integer.parseInt(nitzSubs[2]);
            c.set(Calendar.DATE, date);

            int hour = Integer.parseInt(nitzSubs[3]);
            c.set(Calendar.HOUR, hour);

            int minute = Integer.parseInt(nitzSubs[4]);
            c.set(Calendar.MINUTE, minute);

            int second = Integer.parseInt(nitzSubs[5]);
            c.set(Calendar.SECOND, second);

            /* NITZ time (hour:min:sec) will be in UTC but it supplies the timezone
             * offset as well (which we won't worry about until later) */
            long epochMillis = LocalDateTime.of(year, month, date, hour, minute, second)
                    .toInstant(ZoneOffset.UTC)
                    .toEpochMilli();

            // The offset received from NITZ is the offset to add to get current local time.
            boolean sign = (nitz.indexOf('-') == -1);
@@ -119,7 +113,7 @@ public final class NitzData {
            }

            // As a special extension, the Android emulator appends the name of
            // the host computer's timezone to the nitz string. this is zoneinfo
            // the host computer's timezone to the nitz string. This is zoneinfo
            // timezone name of the form Area!Location or Area!Location!SubLocation
            // so we need to convert the ! into /
            TimeZone zone = null;
@@ -127,8 +121,7 @@ public final class NitzData {
                String tzname = nitzSubs[8].replace('!', '/');
                zone = TimeZone.getTimeZone(tzname);
            }
            return new NitzData(nitz, totalUtcOffsetMillis, dstAdjustmentMillis,
                    c.getTimeInMillis(), zone);
            return new NitzData(nitz, totalUtcOffsetMillis, dstAdjustmentMillis, epochMillis, zone);
        } catch (RuntimeException ex) {
            Rlog.e(LOG_TAG, "NITZ: Parsing NITZ time " + nitz + " ex=" + ex);
            return null;
@@ -227,12 +220,10 @@ public final class NitzData {
        if (!mOriginalString.equals(nitzData.mOriginalString)) {
            return false;
        }
        if (mDstOffset != null ? !mDstOffset.equals(nitzData.mDstOffset)
                : nitzData.mDstOffset != null) {
        if (!Objects.equals(mDstOffset, nitzData.mDstOffset)) {
            return false;
        }
        return mEmulatorHostTimeZone != null ? mEmulatorHostTimeZone
                .equals(nitzData.mEmulatorHostTimeZone) : nitzData.mEmulatorHostTimeZone == null;
        return Objects.equals(mEmulatorHostTimeZone, nitzData.mEmulatorHostTimeZone);
    }

    @Override
@@ -240,7 +231,7 @@ public final class NitzData {
        int result = mOriginalString.hashCode();
        result = 31 * result + mZoneOffset;
        result = 31 * result + (mDstOffset != null ? mDstOffset.hashCode() : 0);
        result = 31 * result + (int) (mCurrentTimeMillis ^ (mCurrentTimeMillis >>> 32));
        result = 31 * result + Long.hashCode(mCurrentTimeMillis);
        result = 31 * result + (mEmulatorHostTimeZone != null ? mEmulatorHostTimeZone.hashCode()
                : 0);
        return result;