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

Commit 4f84bb7b authored by Neil Fuller's avatar Neil Fuller
Browse files

Switch from TimePoint to UnixEpochTime

TimePoint can be replaced by SDK API UnixEpochTime.

Bug: 222295093
Test: treehugger only
Change-Id: I099de6727b9f95e2232ead06abb2735e8af16146
parent 3b951831
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.app.time.TimeState;
import android.app.time.UnixEpochTime;
import android.app.timedetector.ManualTimeSuggestion;
import android.app.timedetector.TelephonyTimeSuggestion;
import android.app.timedetector.TimePoint;

/**
 * Binder APIs to communicate with the time detector service.
@@ -55,5 +54,5 @@ interface ITimeDetectorService {
  boolean suggestManualTime(in ManualTimeSuggestion timeSuggestion);
  void suggestTelephonyTime(in TelephonyTimeSuggestion timeSuggestion);

  TimePoint latestNetworkTime();
  UnixEpochTime latestNetworkTime();
}
+0 −19
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022, 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 android.app.timedetector;

parcelable TimePoint;
+0 −103
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 android.app.timedetector;

import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Objects;

/**
 * Data class for passing a Unix epoch time anchored to the elapsed realtime clock.
 *
 * @hide
 */
public final class TimePoint implements Parcelable {

    private final long mUnixEpochTimeMillis;
    private final long mElapsedRealtimeMillis;

    public TimePoint(long unixEpochTimeMillis, long elapsedRealtimeMillis) {
        mUnixEpochTimeMillis = unixEpochTimeMillis;
        mElapsedRealtimeMillis = elapsedRealtimeMillis;
    }

    /**
     * The current Unix epoch time, according to the external source.
     */
    public long getUnixEpochTimeMillis() {
        return mUnixEpochTimeMillis;
    }

    /**
     * The elapsed millis since boot when {@link #getUnixEpochTimeMillis} was computed.
     */
    public long getElapsedRealtimeMillis() {
        return mElapsedRealtimeMillis;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeLong(mUnixEpochTimeMillis);
        out.writeLong(mElapsedRealtimeMillis);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof TimePoint)) {
            return false;
        }
        TimePoint timePoint = (TimePoint) o;
        return mUnixEpochTimeMillis == timePoint.mUnixEpochTimeMillis
                && mElapsedRealtimeMillis == timePoint.mElapsedRealtimeMillis;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mUnixEpochTimeMillis, mElapsedRealtimeMillis);
    }

    @Override
    public String toString() {
        return "TimePoint{"
                + "mUnixEpochTimeMillis=" + mUnixEpochTimeMillis
                + ", mElapsedRealtimeMillis=" + mElapsedRealtimeMillis
                + '}';
    }

    public static final @NonNull Creator<TimePoint> CREATOR =
            new Creator<TimePoint>() {
                public TimePoint createFromParcel(Parcel in) {
                    long unixEpochTime = in.readLong();
                    long elapsedRealtimeMillis = in.readLong();
                    return new TimePoint(unixEpochTime, elapsedRealtimeMillis);
                }

                public TimePoint[] newArray(int size) {
                    return new TimePoint[size];
                }
            };
}
+2 −2
Original line number Diff line number Diff line
@@ -18,8 +18,8 @@ package android.os;

import android.annotation.NonNull;
import android.app.IAlarmManager;
import android.app.time.UnixEpochTime;
import android.app.timedetector.ITimeDetectorService;
import android.app.timedetector.TimePoint;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.location.ILocationManager;
@@ -300,7 +300,7 @@ public final class SystemClock {
        ITimeDetectorService timeDetectorService = ITimeDetectorService.Stub
                .asInterface(ServiceManager.getService(Context.TIME_DETECTOR_SERVICE));
        if (timeDetectorService != null) {
            TimePoint time;
            UnixEpochTime time;
            try {
                time = timeDetectorService.latestNetworkTime();
            } catch (ParcelableException e) {
+29 −15
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import android.app.time.UnixEpochTime;
import android.app.timedetector.ITimeDetectorService;
import android.app.timedetector.ManualTimeSuggestion;
import android.app.timedetector.TelephonyTimeSuggestion;
import android.app.timedetector.TimePoint;
import android.content.Context;
import android.os.Binder;
import android.os.Handler;
@@ -361,6 +360,34 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        mHandler.post(() -> mTimeDetectorStrategy.suggestNetworkTime(timeSignal));
    }

    @Override
    public UnixEpochTime latestNetworkTime() {
        NetworkTimeSuggestion suggestion = getLatestNetworkSuggestion();
        if (suggestion != null) {
            return suggestion.getUnixEpochTime();
        } else {
            throw new ParcelableException(new DateTimeException("Missing network time fix"));
        }
    }

    /**
     * Returns the latest network suggestion accepted. For use by {@link TimeDetectorShellCommand}.
     */
    @Nullable
    NetworkTimeSuggestion getLatestNetworkSuggestion() {
        // TODO(b/222295093): Return the latest network time from mTimeDetectorStrategy once we can
        //  be sure that all uses of NtpTrustedTime results in a suggestion being made to the time
        //  detector. mNtpTrustedTime can be removed once this happens.
        NtpTrustedTime.TimeResult ntpResult = mNtpTrustedTime.getCachedTimeResult();
        if (ntpResult != null) {
            UnixEpochTime unixEpochTime = new UnixEpochTime(
                    ntpResult.getElapsedRealtimeMillis(), ntpResult.getTimeMillis());
            return new NetworkTimeSuggestion(unixEpochTime, ntpResult.getUncertaintyMillis());
        } else {
            return null;
        }
    }

    void suggestGnssTime(@NonNull GnssTimeSuggestion timeSignal) {
        enforceSuggestGnssTimePermission();
        Objects.requireNonNull(timeSignal);
@@ -376,19 +403,6 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        mHandler.post(() -> mTimeDetectorStrategy.suggestExternalTime(timeSignal));
    }

    @Override
    public TimePoint latestNetworkTime() {
        // TODO(b/222295093): Return the latest network time from mTimeDetectorStrategy once we can
        //  be sure that all uses of NtpTrustedTime results in a suggestion being made to the time
        //  detector. mNtpTrustedTime can be removed once this happens.
        NtpTrustedTime.TimeResult ntpResult = mNtpTrustedTime.getCachedTimeResult();
        if (ntpResult != null) {
            return new TimePoint(ntpResult.getTimeMillis(), ntpResult.getElapsedRealtimeMillis());
        } else {
            throw new ParcelableException(new DateTimeException("Missing network time fix"));
        }
    }

    @Override
    protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw,
            @Nullable String[] args) {
@@ -421,7 +435,7 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
    private void enforceSuggestNetworkTimePermission() {
        mContext.enforceCallingPermission(
                android.Manifest.permission.SET_TIME,
                "set time");
                "suggest network time");
    }

    private void enforceSuggestGnssTimePermission() {
Loading