blob: e034ad4375553e8d9b75282ace6ac318d38e547c [file] [log] [blame]
Neil Fuller3352cfc2019-11-07 15:35:05 +00001/*
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
17package com.android.server.timezonedetector;
18
19import android.annotation.Nullable;
20import android.app.AlarmManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.os.SystemProperties;
25import android.os.UserHandle;
26import android.provider.Settings;
27
28import com.android.internal.telephony.TelephonyIntents;
29
30/**
31 * The real implementation of {@link TimeZoneDetectorStrategy.Callback}.
32 */
33public 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 Fuller2c6d5102019-11-29 09:02:39 +000046 public boolean isAutoTimeZoneDetectionEnabled() {
Neil Fuller3352cfc2019-11-07 15:35:05 +000047 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 Fuller2c6d5102019-11-29 09:02:39 +000069 public void setDeviceTimeZone(String zoneId, boolean sendNetworkBroadcast) {
Neil Fuller3352cfc2019-11-07 15:35:05 +000070 AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class);
71 alarmManager.setTimeZone(zoneId);
72
Neil Fuller2c6d5102019-11-29 09:02:39 +000073 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 Fuller3352cfc2019-11-07 15:35:05 +000080 }
81}