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

Commit 83ad73ec authored by Neil Fuller's avatar Neil Fuller
Browse files

Move network time logic out of AlarmManagerService

Move network time logic out of AlarmManagerService in preparation for
a future refactoring. The TimeDetectorService is now a better home for
functionality associated with time signals, as it should receive most
time signals from various sources so it can determine whether the
device's system clock needs to change.

Although the TimeDetectorService is not currently informed every time
the NtpTrustedTime obtains a new time, it should be in future after
further work in later commits.

This change modifies the logic in SystemClock to match the GNSS time
logic in the same class, i.e. rather than calculating the "current time"
in the system server and then sending it over binder (introducing a
delay), the latest information needed to compute the time is sent to the
client, which then calculates the current time.

Bug: 219693030
Bug: 222295093
Test: atest services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java
Test: atest cts/tests/tests/os/src/android/os/cts/SystemClockSntpTest.java
Test: Treehugger
Change-Id: I80ea52877352122b52ac54daed8b092f3cc18e02
parent 2ddcf674
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ interface IAlarmManager {
    long getNextWakeFromIdleTime();
    @UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553)
    AlarmManager.AlarmClockInfo getNextAlarmClock(int userId);
    long currentNetworkTimeMillis();
    boolean canScheduleExactAlarms(String packageName);
    boolean hasScheduleExactAlarm(String packageName, int userId);
    int getConfigVersion();
+0 −14
Original line number Diff line number Diff line
@@ -90,7 +90,6 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelableException;
import android.os.PowerExemptionManager;
import android.os.PowerManager;
import android.os.Process;
@@ -116,7 +115,6 @@ import android.util.EventLog;
import android.util.IndentingPrintWriter;
import android.util.Log;
import android.util.LongArrayQueue;
import android.util.NtpTrustedTime;
import android.util.Pair;
import android.util.Slog;
import android.util.SparseArray;
@@ -161,7 +159,6 @@ import libcore.util.EmptyArray;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@@ -3023,17 +3020,6 @@ public class AlarmManagerService extends SystemService {
            return getNextAlarmClockImpl(userId);
        }

        @Override
        public long currentNetworkTimeMillis() {
            final NtpTrustedTime time = NtpTrustedTime.getInstance(getContext());
            NtpTrustedTime.TimeResult ntpResult = time.getCachedTimeResult();
            if (ntpResult != null) {
                return ntpResult.currentTimeMillis();
            } else {
                throw new ParcelableException(new DateTimeException("Missing NTP fix"));
            }
        }

        @Override
        public int getConfigVersion() {
            getContext().enforceCallingOrSelfPermission(Manifest.permission.DUMP,
+4 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.app.timedetector.GnssTimeSuggestion;
import android.app.timedetector.ManualTimeSuggestion;
import android.app.timedetector.NetworkTimeSuggestion;
import android.app.timedetector.TelephonyTimeSuggestion;
import android.app.timedetector.TimePoint;

/**
 * System private API to communicate with time detector service.
@@ -50,4 +51,6 @@ interface ITimeDetectorService {
  boolean suggestManualTime(in ManualTimeSuggestion timeSuggestion);
  void suggestNetworkTime(in NetworkTimeSuggestion timeSuggestion);
  void suggestTelephonyTime(in TelephonyTimeSuggestion timeSuggestion);

  TimePoint latestNetworkTime();
}
+19 −0
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;
+103 −0
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];
                }
            };
}
Loading