Merge "Properly reflect the default value."

This commit is contained in:
TreeHugger Robot
2022-08-23 01:10:25 +00:00
committed by Android (Google) Code Review

View File

@@ -27,6 +27,7 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.provider.DeviceConfig;
import android.provider.Settings; import android.provider.Settings;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
@@ -66,7 +67,7 @@ public class TareHomePage extends Activity {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mConfigObserver.mEnableTareSetting == SETTING_VALUE_DEFAULT if (mConfigObserver.mEnableTareSetting == SETTING_VALUE_DEFAULT
&& isChecked == (Settings.Global.DEFAULT_ENABLE_TARE == SETTING_VALUE_ON)) { && isChecked == mConfigObserver.getDefaultEnabledStatus()) {
// Don't bother writing something that's not new information. It would make // Don't bother writing something that's not new information. It would make
// it hard to use DeviceConfig if we did. // it hard to use DeviceConfig if we did.
return; return;
@@ -93,12 +94,19 @@ public class TareHomePage extends Activity {
/** Reverts the TARE settings to the original default settings */ /** Reverts the TARE settings to the original default settings */
public void revertSettings(View v) { public void revertSettings(View v) {
Toast.makeText(this, R.string.tare_settings_reverted_toast, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.tare_settings_reverted_toast, Toast.LENGTH_LONG).show();
final boolean wasSettingsDefault =
mConfigObserver.mEnableTareSetting == SETTING_VALUE_DEFAULT;
Settings.Global.putString(getApplicationContext().getContentResolver(), Settings.Global.putString(getApplicationContext().getContentResolver(),
Settings.Global.ENABLE_TARE, null); Settings.Global.ENABLE_TARE, null);
Settings.Global.putString(getApplicationContext().getContentResolver(), Settings.Global.putString(getApplicationContext().getContentResolver(),
Settings.Global.TARE_ALARM_MANAGER_CONSTANTS, null); Settings.Global.TARE_ALARM_MANAGER_CONSTANTS, null);
Settings.Global.putString(getApplicationContext().getContentResolver(), Settings.Global.putString(getApplicationContext().getContentResolver(),
Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS, null); Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS, null);
if (wasSettingsDefault) {
// Only do this manually here to force a DeviceConfig check if the settings value isn't
// actually changing.
setEnabled(mConfigObserver.getDefaultEnabledStatus());
}
} }
/** Opens up the AlarmManager TARE policy page with its factors to view and edit */ /** Opens up the AlarmManager TARE policy page with its factors to view and edit */
@@ -117,13 +125,14 @@ public class TareHomePage extends Activity {
/** Changes the enabled state of the TARE homepage buttons based on global toggle */ /** Changes the enabled state of the TARE homepage buttons based on global toggle */
private void setEnabled(boolean tareStatus) { private void setEnabled(boolean tareStatus) {
mRevButton.setEnabled(tareStatus);
mAlarmManagerView.setEnabled(tareStatus); mAlarmManagerView.setEnabled(tareStatus);
mJobSchedulerView.setEnabled(tareStatus); mJobSchedulerView.setEnabled(tareStatus);
mOnSwitch.setChecked(tareStatus); mOnSwitch.setChecked(tareStatus);
} }
private class ConfigObserver extends ContentObserver { private class ConfigObserver extends ContentObserver {
private static final String KEY_DC_ENABLE_TARE = "enable_tare";
private int mEnableTareSetting; private int mEnableTareSetting;
ConfigObserver(Handler handler) { ConfigObserver(Handler handler) {
@@ -148,7 +157,7 @@ public class TareHomePage extends Activity {
private void processEnableTareChange() { private void processEnableTareChange() {
final String setting = final String setting =
Settings.Global.getString(getContentResolver(), Settings.Global.ENABLE_TARE); Settings.Global.getString(getContentResolver(), Settings.Global.ENABLE_TARE);
if (setting == null ) { if (setting == null) {
mEnableTareSetting = SETTING_VALUE_DEFAULT; mEnableTareSetting = SETTING_VALUE_DEFAULT;
} else { } else {
try { try {
@@ -157,7 +166,21 @@ public class TareHomePage extends Activity {
mEnableTareSetting = Settings.Global.DEFAULT_ENABLE_TARE; mEnableTareSetting = Settings.Global.DEFAULT_ENABLE_TARE;
} }
} }
setEnabled(mEnableTareSetting == SETTING_VALUE_ON); final boolean enabled;
if (mEnableTareSetting == SETTING_VALUE_ON) {
enabled = true;
} else if (mEnableTareSetting == SETTING_VALUE_OFF) {
enabled = false;
} else {
enabled = getDefaultEnabledStatus();
}
setEnabled(enabled);
} }
private boolean getDefaultEnabledStatus() {
return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TARE, KEY_DC_ENABLE_TARE,
Settings.Global.DEFAULT_ENABLE_TARE == SETTING_VALUE_ON);
}
} }
} }