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

Commit 99002362 authored by Geoffrey Boullanger's avatar Geoffrey Boullanger
Browse files

Extract QualifiedTelephonyTimeZoneSuggestion to its own file.

Moves the `QualifiedTelephonyTimeZoneSuggestion` inner class from `TimeZoneDetectorStrategyImpl` into a new top-level file and converts it to a Java record.

This will be used by the Fused Time Zone Detector (FTZD) as well, so a separate file seems appropriate.

go/android-tz-detector
go/ftzd-algo
go/ftzd-cases
go/ftzd-scenarios

Test: atest FrameworksTimeCoreTests
Test: atest FrameworksTimeServicesTests
Test: atest FrameworksTelephonyTests
Flag: android.timezone.flags.enable_fused_time_zone_detector
Bug: 394770805
Change-Id: I1beae9fb9262347291fc2bf5e1d06359af3ca42b
parent 4059daaa
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.timezonedetector;

import android.app.timezonedetector.TelephonyTimeZoneSuggestion;

/**
 * A {@link TelephonyTimeZoneSuggestion} with additional qualifying metadata.
 *
 * @param suggestion The suggestion.
 * @param score The score the suggestion has been given. This can be used to rank against other
 *     suggestions of the same type.
 */
public record QualifiedTelephonyTimeZoneSuggestion(
        TelephonyTimeZoneSuggestion suggestion, int score) {}
+8 −57
Original line number Diff line number Diff line
@@ -518,7 +518,8 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
                findBestTelephonySuggestion();
        TelephonyTimeZoneSuggestion telephonySuggestion =
                bestQualifiedTelephonySuggestion == null
                        ? null : bestQualifiedTelephonySuggestion.suggestion;
                        ? null
                        : bestQualifiedTelephonySuggestion.suggestion();
        // A new generator is created each time: we don't want / require consistency.
        OrdinalGenerator<String> tzIdOrdinalGenerator =
                new OrdinalGenerator<>(new TimeZoneCanonicalizer());
@@ -739,7 +740,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
        }

        boolean suggestionGoodEnough =
                bestTelephonySuggestion.score >= TELEPHONY_SCORE_USAGE_THRESHOLD;
                bestTelephonySuggestion.score() >= TELEPHONY_SCORE_USAGE_THRESHOLD;
        if (!suggestionGoodEnough) {
            if (DBG) {
                Slog.d(LOG_TAG, "Best suggestion not good enough:"
@@ -751,7 +752,7 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat

        // Paranoia: Every suggestion above the SCORE_USAGE_THRESHOLD should have a non-null time
        // zone ID.
        String timeZoneId = bestTelephonySuggestion.suggestion.getZoneId();
        String timeZoneId = bestTelephonySuggestion.suggestion().getZoneId();
        if (timeZoneId == null) {
            Slog.w(LOG_TAG, "Empty zone suggestion scored higher than expected. This is an error:"
                    + " bestTelephonySuggestion=" + bestTelephonySuggestion
@@ -834,12 +835,12 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat

            if (bestSuggestion == null) {
                bestSuggestion = candidateSuggestion;
            } else if (candidateSuggestion.score > bestSuggestion.score) {
            } else if (candidateSuggestion.score() > bestSuggestion.score()) {
                bestSuggestion = candidateSuggestion;
            } else if (candidateSuggestion.score == bestSuggestion.score) {
            } else if (candidateSuggestion.score() == bestSuggestion.score()) {
                // Tie! Use the suggestion with the lowest slotIndex.
                int candidateSlotIndex = candidateSuggestion.suggestion.getSlotIndex();
                int bestSlotIndex = bestSuggestion.suggestion.getSlotIndex();
                int candidateSlotIndex = candidateSuggestion.suggestion().getSlotIndex();
                int bestSlotIndex = bestSuggestion.suggestion().getSlotIndex();
                if (candidateSlotIndex < bestSlotIndex) {
                    bestSuggestion = candidateSuggestion;
                }
@@ -981,56 +982,6 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
        return mDetectorStatus;
    }

    /**
     * A {@link TelephonyTimeZoneSuggestion} with additional qualifying metadata.
     */
    @VisibleForTesting
    public static final class QualifiedTelephonyTimeZoneSuggestion {

        @VisibleForTesting
        public final TelephonyTimeZoneSuggestion suggestion;

        /**
         * The score the suggestion has been given. This can be used to rank against other
         * suggestions of the same type.
         */
        @VisibleForTesting
        public final int score;

        @VisibleForTesting
        public QualifiedTelephonyTimeZoneSuggestion(
                TelephonyTimeZoneSuggestion suggestion, int score) {
            this.suggestion = suggestion;
            this.score = score;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            QualifiedTelephonyTimeZoneSuggestion that = (QualifiedTelephonyTimeZoneSuggestion) o;
            return score == that.score
                    && suggestion.equals(that.suggestion);
        }

        @Override
        public int hashCode() {
            return Objects.hash(score, suggestion);
        }

        @Override
        public String toString() {
            return "QualifiedTelephonyTimeZoneSuggestion{"
                    + "suggestion=" + suggestion
                    + ", score=" + score
                    + '}';
        }
    }

    private static String formatDebugString(TimestampedValue<?> value) {
        return value.getValue() + " @ " + Duration.ofMillis(value.getReferenceTimeMillis());
    }
+4 −3
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ import android.util.IndentingPrintWriter;

import com.android.server.SystemTimeZone.TimeZoneConfidence;
import com.android.server.flags.Flags;
import com.android.server.timezonedetector.TimeZoneDetectorStrategyImpl.QualifiedTelephonyTimeZoneSuggestion;
import com.android.server.timezonedetector.QualifiedTelephonyTimeZoneSuggestion;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
@@ -2073,8 +2073,9 @@ public class TimeZoneDetectorStrategyImplTest {

        Script verifyLatestTelephonySuggestionReceived(int slotIndex,
                TelephonyTimeZoneSuggestion expectedSuggestion) {
            assertEquals(expectedSuggestion,
                    mTimeZoneDetectorStrategy.getLatestTelephonySuggestion(slotIndex).suggestion);
            assertEquals(
                    expectedSuggestion,
                    mTimeZoneDetectorStrategy.getLatestTelephonySuggestion(slotIndex).suggestion());
            return this;
        }