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

Commit 13198e7a authored by Geoffrey Boullanger's avatar Geoffrey Boullanger
Browse files

Added implementation for time zone change notifications

In this change, we are adding a change listener that keeps track of automatic and manual time zone changes and send notifications to the user if required.

BYPASS_LARGE_CHANGE_WARNING

Design doc: go/android-tznotifications

Test: atest services/tests/timetests/src/com/android/server/timezonedetector/NotifyingTimeZoneChangeListenerTest.java
Test: atest services/tests/timetests/src/com/android/server/timezonedetector/TimeZoneDetectorStrategyImplTest.java
Bug: 283267917
Flag: com.android.server.flags.datetime_notifications
Change-Id: I9ba54e6be0df768b784dd4c5d71d81c2a5695dfd
parent c63c81ae
Loading
Loading
Loading
Loading
+649 −0

File added.

Preview size limit exceeded, changes collapsed.

+24 −0
Original line number Diff line number Diff line
@@ -102,5 +102,29 @@ public interface TimeZoneChangeListener {
                    + ", mCause='" + mCause + '\''
                    + '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o instanceof TimeZoneChangeEvent that) {
                return mElapsedRealtimeMillis == that.mElapsedRealtimeMillis
                        && mUnixEpochTimeMillis == that.mUnixEpochTimeMillis
                        && mOrigin == that.mOrigin
                        && mUserId == that.mUserId
                        && Objects.equals(mOldZoneId, that.mOldZoneId)
                        && Objects.equals(mNewZoneId, that.mNewZoneId)
                        && mNewConfidence == that.mNewConfidence
                        && Objects.equals(mCause, that.mCause);
            }
            return false;
        }

        @Override
        public int hashCode() {
            return Objects.hash(mElapsedRealtimeMillis, mUnixEpochTimeMillis, mOrigin, mUserId,
                    mOldZoneId, mNewZoneId, mNewConfidence, mCause);
        }
    }
}
+35 −2
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.app.timezonedetector.ManualTimeZoneSuggestion;
import android.app.timezonedetector.TelephonyTimeZoneSuggestion;
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.os.TimestampedValue;
import android.os.UserHandle;
import android.util.IndentingPrintWriter;
@@ -50,6 +51,7 @@ import android.util.Slog;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.SystemTimeZone.TimeZoneConfidence;
import com.android.server.flags.Flags;
import com.android.server.timezonedetector.ConfigurationInternal.DetectionMode;

import java.io.PrintWriter;
@@ -214,6 +216,13 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
    @NonNull
    private final List<StateChangeListener> mStateChangeListeners = new ArrayList<>();

    /**
     * A component adjunct to the detection behavior that tracks time zone changes and implements
     * behavior associated with time zone changes.
     */
    @NonNull
    private final TimeZoneChangeListener mChangeTracker;

    /**
     * A snapshot of the current detector status. A local copy is cached because it is relatively
     * heavyweight to obtain and is used more often than it is expected to change.
@@ -256,15 +265,20 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
            @NonNull ServiceConfigAccessor serviceConfigAccessor) {

        Environment environment = new EnvironmentImpl(handler);
        return new TimeZoneDetectorStrategyImpl(serviceConfigAccessor, environment);
        TimeZoneChangeListener changeEventTracker =
                NotifyingTimeZoneChangeListener.create(handler, context, serviceConfigAccessor);
        return new TimeZoneDetectorStrategyImpl(
                serviceConfigAccessor, environment, changeEventTracker);
    }

    @VisibleForTesting
    public TimeZoneDetectorStrategyImpl(
            @NonNull ServiceConfigAccessor serviceConfigAccessor,
            @NonNull Environment environment) {
            @NonNull Environment environment,
            @NonNull TimeZoneChangeListener changeEventTracker) {
        mEnvironment = Objects.requireNonNull(environment);
        mServiceConfigAccessor = Objects.requireNonNull(serviceConfigAccessor);
        mChangeTracker = Objects.requireNonNull(changeEventTracker);

        // Start with telephony fallback enabled.
        mTelephonyTimeZoneFallbackEnabled =
@@ -833,6 +847,17 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
            Slog.d(LOG_TAG, logInfo);
        }
        mEnvironment.setDeviceTimeZoneAndConfidence(newZoneId, newConfidence, logInfo);

        if (Flags.datetimeNotifications()) {
            // Record the fact that the time zone was changed so that it can be tracked, i.e.
            // whether the device / user sticks with it.
            TimeZoneChangeListener.TimeZoneChangeEvent changeEvent =
                    new TimeZoneChangeListener.TimeZoneChangeEvent(
                            SystemClock.elapsedRealtime(), System.currentTimeMillis(), origin,
                            userId,
                            currentZoneId, newZoneId, newConfidence, cause);
            mChangeTracker.process(changeEvent);
        }
    }

    @GuardedBy("this")
@@ -947,6 +972,14 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat
        ipw.increaseIndent(); // level 2
        mTelephonySuggestionsBySlotIndex.dump(ipw);
        ipw.decreaseIndent(); // level 2

        if (Flags.datetimeNotifications()) {
            ipw.println("Time zone change tracker:");
            ipw.increaseIndent(); // level 2
            mChangeTracker.dump(ipw);
            ipw.decreaseIndent(); // level 2
        }

        ipw.decreaseIndent(); // level 1
    }

+527 −0

File added.

Preview size limit exceeded, changes collapsed.

+285 −1

File changed.

Preview size limit exceeded, changes collapsed.