1 /*
2  * Copyright (C) 2020 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 
17 package android.app.timedetector;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 import android.os.ServiceManager.ServiceNotFoundException;
24 import android.util.Log;
25 
26 /**
27  * The real implementation of {@link TimeDetector}.
28  *
29  * @hide
30  */
31 public final class TimeDetectorImpl implements TimeDetector {
32     private static final String TAG = "timedetector.TimeDetector";
33     private static final boolean DEBUG = false;
34 
35     private final ITimeDetectorService mITimeDetectorService;
36 
TimeDetectorImpl()37     public TimeDetectorImpl() throws ServiceNotFoundException {
38         mITimeDetectorService = ITimeDetectorService.Stub.asInterface(
39                 ServiceManager.getServiceOrThrow(Context.TIME_DETECTOR_SERVICE));
40     }
41 
42     @Override
suggestTelephonyTime(@onNull TelephonyTimeSuggestion timeSuggestion)43     public void suggestTelephonyTime(@NonNull TelephonyTimeSuggestion timeSuggestion) {
44         if (DEBUG) {
45             Log.d(TAG, "suggestTelephonyTime called: " + timeSuggestion);
46         }
47         try {
48             mITimeDetectorService.suggestTelephonyTime(timeSuggestion);
49         } catch (RemoteException e) {
50             throw e.rethrowFromSystemServer();
51         }
52     }
53 
54     @Override
suggestManualTime(@onNull ManualTimeSuggestion timeSuggestion)55     public boolean suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion) {
56         if (DEBUG) {
57             Log.d(TAG, "suggestManualTime called: " + timeSuggestion);
58         }
59         try {
60             return mITimeDetectorService.suggestManualTime(timeSuggestion);
61         } catch (RemoteException e) {
62             throw e.rethrowFromSystemServer();
63         }
64     }
65 
66     @Override
suggestNetworkTime(NetworkTimeSuggestion timeSuggestion)67     public void suggestNetworkTime(NetworkTimeSuggestion timeSuggestion) {
68         if (DEBUG) {
69             Log.d(TAG, "suggestNetworkTime called: " + timeSuggestion);
70         }
71         try {
72             mITimeDetectorService.suggestNetworkTime(timeSuggestion);
73         } catch (RemoteException e) {
74             throw e.rethrowFromSystemServer();
75         }
76     }
77 
78     @Override
suggestGnssTime(GnssTimeSuggestion timeSuggestion)79     public void suggestGnssTime(GnssTimeSuggestion timeSuggestion) {
80         if (DEBUG) {
81             Log.d(TAG, "suggestGnssTime called: " + timeSuggestion);
82         }
83         try {
84             mITimeDetectorService.suggestGnssTime(timeSuggestion);
85         } catch (RemoteException e) {
86             throw e.rethrowFromSystemServer();
87         }
88     }
89 }
90