Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.timezonedetector; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Neil Fuller | 106f18a | 2019-12-04 13:27:04 +0000 | [diff] [blame] | 20 | import android.annotation.RequiresPermission; |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 21 | import android.annotation.SystemService; |
| 22 | import android.content.Context; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
| 25 | import android.os.ServiceManager.ServiceNotFoundException; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | /** |
| 29 | * The interface through which system components can send signals to the TimeZoneDetectorService. |
Neil Fuller | 106f18a | 2019-12-04 13:27:04 +0000 | [diff] [blame] | 30 | * |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 31 | * @hide |
| 32 | */ |
| 33 | @SystemService(Context.TIME_ZONE_DETECTOR_SERVICE) |
Neil Fuller | 106f18a | 2019-12-04 13:27:04 +0000 | [diff] [blame] | 34 | public class TimeZoneDetector { |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 35 | private static final String TAG = "timezonedetector.TimeZoneDetector"; |
| 36 | private static final boolean DEBUG = false; |
| 37 | |
| 38 | private final ITimeZoneDetectorService mITimeZoneDetectorService; |
| 39 | |
| 40 | public TimeZoneDetector() throws ServiceNotFoundException { |
| 41 | mITimeZoneDetectorService = ITimeZoneDetectorService.Stub.asInterface( |
| 42 | ServiceManager.getServiceOrThrow(Context.TIME_ZONE_DETECTOR_SERVICE)); |
| 43 | } |
| 44 | |
| 45 | /** |
Neil Fuller | 106f18a | 2019-12-04 13:27:04 +0000 | [diff] [blame] | 46 | * Suggests the current time zone, determined using telephony signals, to the detector. The |
| 47 | * detector may ignore the signal based on system settings, whether better information is |
| 48 | * available, and so on. |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 49 | */ |
Neil Fuller | 106f18a | 2019-12-04 13:27:04 +0000 | [diff] [blame] | 50 | @RequiresPermission(android.Manifest.permission.SET_TIME_ZONE) |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 51 | public void suggestPhoneTimeZone(@NonNull PhoneTimeZoneSuggestion timeZoneSuggestion) { |
| 52 | if (DEBUG) { |
| 53 | Log.d(TAG, "suggestPhoneTimeZone called: " + timeZoneSuggestion); |
| 54 | } |
| 55 | try { |
| 56 | mITimeZoneDetectorService.suggestPhoneTimeZone(timeZoneSuggestion); |
| 57 | } catch (RemoteException e) { |
| 58 | throw e.rethrowFromSystemServer(); |
| 59 | } |
| 60 | } |
| 61 | |
Neil Fuller | 106f18a | 2019-12-04 13:27:04 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Suggests the current time zone, determined for the user's manually information, to the |
| 64 | * detector. The detector may ignore the signal based on system settings. |
| 65 | */ |
| 66 | @RequiresPermission(android.Manifest.permission.SET_TIME_ZONE) |
| 67 | public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { |
| 68 | if (DEBUG) { |
| 69 | Log.d(TAG, "suggestManualTimeZone called: " + timeZoneSuggestion); |
| 70 | } |
| 71 | try { |
| 72 | mITimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion); |
| 73 | } catch (RemoteException e) { |
| 74 | throw e.rethrowFromSystemServer(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * A shared utility method to create a {@link ManualTimeZoneSuggestion}. |
| 80 | */ |
| 81 | public static ManualTimeZoneSuggestion createManualTimeZoneSuggestion(String tzId, String why) { |
| 82 | ManualTimeZoneSuggestion suggestion = new ManualTimeZoneSuggestion(tzId); |
| 83 | suggestion.addDebugInfo(why); |
| 84 | return suggestion; |
| 85 | } |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 86 | } |