1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.server.location.gnss; 17 18 import android.annotation.CurrentTimeMillisLong; 19 import android.annotation.ElapsedRealtimeLong; 20 import android.annotation.NonNull; 21 import android.content.Context; 22 import android.location.flags.Flags; 23 import android.os.Looper; 24 25 import java.io.PrintWriter; 26 27 /** 28 * An abstraction for use by {@link GnssLocationProvider}. This class allows switching between 29 * implementations with a compile-time constant change, which is less risky than rolling back a 30 * whole class. When there is a single implementation again this class can be replaced by that 31 * implementation. 32 */ 33 abstract class NetworkTimeHelper { 34 35 /** 36 * This compile-time value can be changed to switch between new and old ways to obtain network 37 * time for GNSS. If you have to turn this from {@code true} to {@code false} then please create 38 * a platform bug. This switch will be removed in a future release. If there are problems with 39 * the new impl we'd like to hear about them. 40 */ 41 static final boolean USE_TIME_DETECTOR_IMPL = true; 42 43 /** 44 * The callback interface used by {@link NetworkTimeHelper} to report the time to {@link 45 * GnssLocationProvider}. The callback can happen at any time using the thread associated with 46 * the looper passed to {@link #create(Context, Looper, InjectTimeCallback)}. 47 */ 48 interface InjectTimeCallback { injectTime(@urrentTimeMillisLong long unixEpochTimeMillis, @ElapsedRealtimeLong long elapsedRealtimeMillis, int uncertaintyMillis)49 void injectTime(@CurrentTimeMillisLong long unixEpochTimeMillis, 50 @ElapsedRealtimeLong long elapsedRealtimeMillis, int uncertaintyMillis); 51 } 52 53 /** 54 * Creates the {@link NetworkTimeHelper} instance for use by {@link GnssLocationProvider}. 55 */ create( @onNull Context context, @NonNull Looper looper, @NonNull InjectTimeCallback injectTimeCallback)56 static NetworkTimeHelper create( 57 @NonNull Context context, @NonNull Looper looper, 58 @NonNull InjectTimeCallback injectTimeCallback) { 59 if (!Flags.useLegacyNtpTime()) { 60 TimeDetectorNetworkTimeHelper.Environment environment = 61 new TimeDetectorNetworkTimeHelper.EnvironmentImpl(looper); 62 return new TimeDetectorNetworkTimeHelper(environment, injectTimeCallback); 63 } else { 64 return new NtpNetworkTimeHelper(context, looper, injectTimeCallback); 65 } 66 } 67 68 /** 69 * Sets the "on demand time injection" mode. 70 * 71 * <p>Called by {@link GnssLocationProvider} to set the expected time injection behavior. 72 * When {@code enablePeriodicTimeInjection == true}, the time helper should periodically send 73 * the time on an undefined schedule. The time can be injected at other times for other reasons 74 * as well as be requested via {@link #demandUtcTimeInjection()}. 75 * 76 * @param periodicTimeInjectionEnabled {@code true} if the GNSS implementation requires periodic 77 * time signals 78 */ setPeriodicTimeInjectionMode(boolean periodicTimeInjectionEnabled)79 abstract void setPeriodicTimeInjectionMode(boolean periodicTimeInjectionEnabled); 80 81 /** 82 * Requests an asynchronous time injection via {@link InjectTimeCallback#injectTime}, if a 83 * network time is available. {@link InjectTimeCallback#injectTime} may not be called if a 84 * network time is not available. 85 */ demandUtcTimeInjection()86 abstract void demandUtcTimeInjection(); 87 88 /** 89 * Notifies that network connectivity has been established. 90 * 91 * <p>Called by {@link GnssLocationProvider} when the device establishes a data network 92 * connection. This call should be removed eventually because it should be handled by the {@link 93 * NetworkTimeHelper} implementation itself, but has been retained for compatibility while 94 * switching implementations. 95 */ onNetworkAvailable()96 abstract void onNetworkAvailable(); 97 98 /** 99 * Dumps internal state during bugreports useful for debugging. 100 */ dump(@onNull PrintWriter pw)101 abstract void dump(@NonNull PrintWriter pw); 102 103 } 104