Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 com.android.server.timezonedetector; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.app.AlarmManager; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.os.SystemProperties; |
| 25 | import android.os.UserHandle; |
| 26 | import android.provider.Settings; |
| 27 | |
| 28 | import com.android.internal.telephony.TelephonyIntents; |
| 29 | |
| 30 | /** |
| 31 | * The real implementation of {@link TimeZoneDetectorStrategy.Callback}. |
| 32 | */ |
| 33 | public final class TimeZoneDetectorCallbackImpl implements TimeZoneDetectorStrategy.Callback { |
| 34 | |
| 35 | private static final String TIMEZONE_PROPERTY = "persist.sys.timezone"; |
| 36 | |
| 37 | private final Context mContext; |
| 38 | private final ContentResolver mCr; |
| 39 | |
| 40 | TimeZoneDetectorCallbackImpl(Context context) { |
| 41 | mContext = context; |
| 42 | mCr = context.getContentResolver(); |
| 43 | } |
| 44 | |
| 45 | @Override |
Neil Fuller | 2c6d510 | 2019-11-29 09:02:39 +0000 | [diff] [blame] | 46 | public boolean isAutoTimeZoneDetectionEnabled() { |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 47 | return Settings.Global.getInt(mCr, Settings.Global.AUTO_TIME_ZONE, 1 /* default */) > 0; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public boolean isDeviceTimeZoneInitialized() { |
| 52 | // timezone.equals("GMT") will be true and only true if the time zone was |
| 53 | // set to a default value by the system server (when starting, system server |
| 54 | // sets the persist.sys.timezone to "GMT" if it's not set). "GMT" is not used by |
| 55 | // any code that sets it explicitly (in case where something sets GMT explicitly, |
| 56 | // "Etc/GMT" Olson ID would be used). |
| 57 | |
| 58 | String timeZoneId = getDeviceTimeZone(); |
| 59 | return timeZoneId != null && timeZoneId.length() > 0 && !timeZoneId.equals("GMT"); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | @Nullable |
| 64 | public String getDeviceTimeZone() { |
| 65 | return SystemProperties.get(TIMEZONE_PROPERTY); |
| 66 | } |
| 67 | |
| 68 | @Override |
Neil Fuller | 2c6d510 | 2019-11-29 09:02:39 +0000 | [diff] [blame] | 69 | public void setDeviceTimeZone(String zoneId, boolean sendNetworkBroadcast) { |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 70 | AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class); |
| 71 | alarmManager.setTimeZone(zoneId); |
| 72 | |
Neil Fuller | 2c6d510 | 2019-11-29 09:02:39 +0000 | [diff] [blame] | 73 | if (sendNetworkBroadcast) { |
| 74 | // TODO Nothing in the platform appears to listen for this. Remove it. |
| 75 | Intent intent = new Intent(TelephonyIntents.ACTION_NETWORK_SET_TIMEZONE); |
| 76 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 77 | intent.putExtra("time-zone", zoneId); |
| 78 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 79 | } |
Neil Fuller | 3352cfc | 2019-11-07 15:35:05 +0000 | [diff] [blame] | 80 | } |
| 81 | } |