Switch auto time setting to use TimeManager APIs instead of low-level direct settings access. This makes the AutoTimePreferenceController approach the same as AutoTimeZonePreferenceController. Most of the logic about what to display and whether toggles are enabled is moved to the service. This change has the side effect of adding support in SettingsUI for devices with no auto time detection mechanism configured at all, i.e. the auto time toggle will stop being shown in SettingsUI. There are no known devices that require this currently, but it is a theoretical possibility if config.xml is configured in particular ways. Bug: 172891783 Test: Manual testing with different settings for "time origin priorities" (i.e. empty, non-empty) Test: m ROBOTEST_FILTER=com.android.settings.datetime RunSettingsRoboTests -j40 Change-Id: I4112fb51adb0d06a1dbc39a44ea109d6df899153
146 lines
5.1 KiB
Java
146 lines
5.1 KiB
Java
/*
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.settings.datetime;
|
|
|
|
import static android.app.time.Capabilities.CAPABILITY_POSSESSED;
|
|
|
|
import android.app.Activity;
|
|
import android.app.DatePickerDialog;
|
|
import android.app.time.TimeCapabilities;
|
|
import android.app.time.TimeManager;
|
|
import android.app.timedetector.ManualTimeSuggestion;
|
|
import android.app.timedetector.TimeDetector;
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
import android.text.format.DateFormat;
|
|
import android.widget.DatePicker;
|
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.preference.Preference;
|
|
|
|
import com.android.settings.core.PreferenceControllerMixin;
|
|
import com.android.settingslib.RestrictedPreference;
|
|
import com.android.settingslib.core.AbstractPreferenceController;
|
|
|
|
import java.util.Calendar;
|
|
|
|
public class DatePreferenceController extends AbstractPreferenceController
|
|
implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener {
|
|
|
|
public interface DatePreferenceHost extends UpdateTimeAndDateCallback {
|
|
void showDatePicker();
|
|
}
|
|
|
|
public static final int DIALOG_DATEPICKER = 0;
|
|
|
|
private static final String KEY_DATE = "date";
|
|
|
|
private final DatePreferenceHost mHost;
|
|
private final TimeManager mTimeManager;
|
|
|
|
public DatePreferenceController(Context context, DatePreferenceHost host) {
|
|
super(context);
|
|
mHost = host;
|
|
mTimeManager = context.getSystemService(TimeManager.class);
|
|
}
|
|
|
|
@Override
|
|
public boolean isAvailable() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void updateState(Preference preference) {
|
|
if (!(preference instanceof RestrictedPreference)) {
|
|
return;
|
|
}
|
|
final Calendar now = Calendar.getInstance();
|
|
preference.setSummary(DateFormat.getLongDateFormat(mContext).format(now.getTime()));
|
|
if (!((RestrictedPreference) preference).isDisabledByAdmin()) {
|
|
boolean enableManualDateSelection = isEnabled();
|
|
preference.setEnabled(enableManualDateSelection);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean handlePreferenceTreeClick(Preference preference) {
|
|
if (!TextUtils.equals(preference.getKey(), KEY_DATE)) {
|
|
return false;
|
|
}
|
|
mHost.showDatePicker();
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public String getPreferenceKey() {
|
|
return KEY_DATE;
|
|
}
|
|
|
|
@Override
|
|
public void onDateSet(DatePicker view, int year, int month, int day) {
|
|
setDate(year, month, day);
|
|
mHost.updateTimeAndDateDisplay(mContext);
|
|
}
|
|
|
|
public DatePickerDialog buildDatePicker(Activity activity) {
|
|
final Calendar calendar = Calendar.getInstance();
|
|
final DatePickerDialog d = new DatePickerDialog(
|
|
activity,
|
|
this,
|
|
calendar.get(Calendar.YEAR),
|
|
calendar.get(Calendar.MONTH),
|
|
calendar.get(Calendar.DAY_OF_MONTH));
|
|
// The system clock can't represent dates outside this range.
|
|
calendar.clear();
|
|
calendar.set(2007, Calendar.JANUARY, 1);
|
|
d.getDatePicker().setMinDate(calendar.getTimeInMillis());
|
|
calendar.clear();
|
|
calendar.set(2037, Calendar.DECEMBER, 31);
|
|
d.getDatePicker().setMaxDate(calendar.getTimeInMillis());
|
|
return d;
|
|
}
|
|
|
|
@VisibleForTesting
|
|
void setDate(int year, int month, int day) {
|
|
Calendar c = Calendar.getInstance();
|
|
|
|
c.set(Calendar.YEAR, year);
|
|
c.set(Calendar.MONTH, month);
|
|
c.set(Calendar.DAY_OF_MONTH, day);
|
|
long when = Math.max(c.getTimeInMillis(), DatePreferenceHost.MIN_DATE);
|
|
|
|
if (when / 1000 < Integer.MAX_VALUE) {
|
|
TimeDetector timeDetector = mContext.getSystemService(TimeDetector.class);
|
|
ManualTimeSuggestion manualTimeSuggestion =
|
|
TimeDetector.createManualTimeSuggestion(when, "Settings: Set date");
|
|
timeDetector.suggestManualTime(manualTimeSuggestion);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns whether selecting the preference should prompt for the user to enter the date
|
|
* manually. Exposed as public so that the time controller can easily share the same logic as
|
|
* the rules are identical for time.
|
|
*/
|
|
public boolean isEnabled() {
|
|
TimeCapabilities timeZoneCapabilities =
|
|
mTimeManager.getTimeCapabilitiesAndConfig().getCapabilities();
|
|
int suggestManualTimeCapability = timeZoneCapabilities.getSuggestManualTimeCapability();
|
|
return suggestManualTimeCapability == CAPABILITY_POSSESSED;
|
|
}
|
|
}
|