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.os.Looper; 23 24 import java.io.PrintWriter; 25 26 /** 27 * An abstraction for use by {@link GnssLocationProvider}. This class allows switching between 28 * implementations with a compile-time constant change, which is less risky than rolling back a 29 * whole class. When there is a single implementation again this class can be replaced by that 30 * implementation. 31 */ 32 abstract class NetworkTimeHelper { 33 34 /** 35 * This compile-time value can be changed to switch between new and old ways to obtain network 36 * time for GNSS. If you have to turn this from {@code true} to {@code false} then please create 37 * a platform bug. This switch will be removed in a future release. If there are problems with 38 * the new impl we'd like to hear about them. 39 */ 40 static final boolean USE_TIME_DETECTOR_IMPL = false; 41 42 /** 43 * The callback interface used by {@link NetworkTimeHelper} to report the time to {@link 44 * GnssLocationProvider}. The callback can happen at any time using the thread associated with 45 * the looper passed to {@link #create(Context, Looper, InjectTimeCallback)}. 46 */ 47 interface InjectTimeCallback { injectTime(@urrentTimeMillisLong long unixEpochTimeMillis, @ElapsedRealtimeLong long elapsedRealtimeMillis, int uncertaintyMillis)48 void injectTime(@CurrentTimeMillisLong long unixEpochTimeMillis, 49 @ElapsedRealtimeLong long elapsedRealtimeMillis, int uncertaintyMillis); 50 } 51 52 /** 53 * Creates the {@link NetworkTimeHelper} instance for use by {@link GnssLocationProvider}. 54 */ create( @onNull Context context, @NonNull Looper looper, @NonNull InjectTimeCallback injectTimeCallback)55 static NetworkTimeHelper create( 56 @NonNull Context context, @NonNull Looper looper, 57 @NonNull InjectTimeCallback injectTimeCallback) { 58 if (USE_TIME_DETECTOR_IMPL) { 59 TimeDetectorNetworkTimeHelper.Environment environment = 60 new TimeDetectorNetworkTimeHelper.EnvironmentImpl(looper); 61 return new TimeDetectorNetworkTimeHelper(environment, injectTimeCallback); 62 } else { 63 return new NtpNetworkTimeHelper(context, looper, injectTimeCallback); 64 } 65 } 66 67 /** 68 * Sets the "on demand time injection" mode. 69 * 70 * <p>Called by {@link GnssLocationProvider} to set the expected time injection behavior. 71 * When {@code enablePeriodicTimeInjection == true}, the time helper should periodically send 72 * the time on an undefined schedule. The time can be injected at other times for other reasons 73 * as well as be requested via {@link #demandUtcTimeInjection()}. 74 * 75 * @param periodicTimeInjectionEnabled {@code true} if the GNSS implementation requires periodic 76 * time signals 77 */ setPeriodicTimeInjectionMode(boolean periodicTimeInjectionEnabled)78 abstract void setPeriodicTimeInjectionMode(boolean periodicTimeInjectionEnabled); 79 80 /** 81 * Requests an asynchronous time injection via {@link InjectTimeCallback#injectTime}, if a 82 * network time is available. {@link InjectTimeCallback#injectTime} may not be called if a 83 * network time is not available. 84 */ demandUtcTimeInjection()85 abstract void demandUtcTimeInjection(); 86 87 /** 88 * Notifies that network connectivity has been established. 89 * 90 * <p>Called by {@link GnssLocationProvider} when the device establishes a data network 91 * connection. This call should be removed eventually because it should be handled by the {@link 92 * NetworkTimeHelper} implementation itself, but has been retained for compatibility while 93 * switching implementations. 94 */ onNetworkAvailable()95 abstract void onNetworkAvailable(); 96 97 /** 98 * Dumps internal state during bugreports useful for debugging. 99 */ dump(@onNull PrintWriter pw)100 abstract void dump(@NonNull PrintWriter pw); 101 102 } 103