Refactor Date & Time Settings

- Rid off AbstractPreferenceController

Test: robotest
Bug: 235445309
Change-Id: I61118a0ff580231973509c06e84e7088dba897f5
This commit is contained in:
Edgar Wang
2023-11-16 20:34:31 +08:00
parent 6bb2c73579
commit fdab44f9e7
16 changed files with 339 additions and 434 deletions

View File

@@ -16,7 +16,11 @@
package com.android.settings.datetime;
import static android.app.time.Capabilities.CAPABILITY_POSSESSED;
import android.app.TimePickerDialog;
import android.app.time.TimeCapabilities;
import android.app.time.TimeManager;
import android.app.timedetector.ManualTimeSuggestion;
import android.app.timedetector.TimeDetector;
import android.content.Context;
@@ -27,14 +31,12 @@ import android.widget.TimePicker;
import androidx.preference.Preference;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.BasePreferenceController;
import java.util.Calendar;
public class TimePreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, TimePickerDialog.OnTimeSetListener {
public class TimePreferenceController extends BasePreferenceController
implements TimePickerDialog.OnTimeSetListener {
public interface TimePreferenceHost extends UpdateTimeAndDateCallback {
void showTimePicker();
@@ -43,52 +45,45 @@ public class TimePreferenceController extends AbstractPreferenceController
public static final int DIALOG_TIMEPICKER = 1;
private static final String TAG = "TimePreferenceController";
private static final String KEY_TIME = "time";
private final DatePreferenceController mDatePreferenceController;
private final TimePreferenceHost mHost;
private TimePreferenceHost mHost;
private TimeManager mTimeManager;
public TimePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mTimeManager = context.getSystemService(TimeManager.class);
}
public TimePreferenceController(Context context,
TimePreferenceHost callback,
DatePreferenceController datePreferenceController) {
super(context);
mHost = callback;
mDatePreferenceController = datePreferenceController;
public void setHost(TimePreferenceHost host) {
mHost = host;
}
@Override
public boolean isAvailable() {
return true;
public int getAvailabilityStatus() {
return isEnabled() ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
}
@Override
public void updateState(Preference preference) {
if (!(preference instanceof RestrictedPreference)) {
return;
}
final Calendar now = Calendar.getInstance();
preference.setSummary(DateFormat.getTimeFormat(mContext).format(now.getTime()));
if (!((RestrictedPreference) preference).isDisabledByAdmin()) {
boolean enableManualTimeSelection = mDatePreferenceController.isEnabled();
preference.setEnabled(enableManualTimeSelection);
}
super.updateState(preference);
preference.setEnabled(isEnabled());
}
@Override
public CharSequence getSummary() {
Calendar now = Calendar.getInstance();
return DateFormat.getTimeFormat(mContext).format(now.getTime());
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (!TextUtils.equals(KEY_TIME, preference.getKey())) {
if (!TextUtils.equals(getPreferenceKey(), preference.getKey())) {
return false;
}
mHost.showTimePicker();
return true;
}
@Override
public String getPreferenceKey() {
return KEY_TIME;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (mContext != null) {
@@ -132,4 +127,16 @@ public class TimePreferenceController extends AbstractPreferenceController
Log.w(TAG, "Unable to set time with suggestion=" + 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.getSetManualTimeCapability();
return suggestManualTimeCapability == CAPABILITY_POSSESSED;
}
}