Update enable toggle processing.
1. Use a ContentObserver so the UI is always up-to-date. 2. Don't write a value when it would effectively be putting the default value and the current value is null. This allows us to have a "use default" option so that DeviceConfig can take over if a user ever resets to default settings. Bug: 158300259 Bug: 189850067 Bug: 205624100 Test: manually toggle and look at logcat for expected values Change-Id: I429977d37cd8afc2ce12b89cbac002503484ad7e
This commit is contained in:
@@ -22,7 +22,11 @@ import static com.android.settings.development.tare.DropdownActivity.POLICY_JOB_
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
@@ -40,6 +44,9 @@ public class TareHomePage extends Activity {
|
||||
private Button mRevButton;
|
||||
private TextView mAlarmManagerView;
|
||||
private TextView mJobSchedulerView;
|
||||
private ConfigObserver mConfigObserver;
|
||||
|
||||
private static final int SETTING_VALUE_DEFAULT = -1;
|
||||
private static final int SETTING_VALUE_OFF = 0;
|
||||
private static final int SETTING_VALUE_ON = 1;
|
||||
|
||||
@@ -53,14 +60,17 @@ public class TareHomePage extends Activity {
|
||||
mAlarmManagerView = findViewById(R.id.alarmmanager);
|
||||
mJobSchedulerView = findViewById(R.id.jobscheduler);
|
||||
|
||||
final boolean isTareEnabled = Settings.Global.getInt(getContentResolver(),
|
||||
Settings.Global.ENABLE_TARE, Settings.Global.DEFAULT_ENABLE_TARE) == 1;
|
||||
setEnabled(isTareEnabled);
|
||||
mConfigObserver = new ConfigObserver(new Handler(Looper.getMainLooper()));
|
||||
|
||||
mOnSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
setEnabled(isChecked);
|
||||
if (mConfigObserver.mEnableTareSetting == SETTING_VALUE_DEFAULT
|
||||
&& isChecked == (Settings.Global.DEFAULT_ENABLE_TARE == SETTING_VALUE_ON)) {
|
||||
// Don't bother writing something that's not new information. It would make
|
||||
// it hard to use DeviceConfig if we did.
|
||||
return;
|
||||
}
|
||||
Settings.Global.putInt(getContentResolver(),
|
||||
Settings.Global.ENABLE_TARE,
|
||||
isChecked ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
|
||||
@@ -68,6 +78,18 @@ public class TareHomePage extends Activity {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mConfigObserver.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
mConfigObserver.stop();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
/** Reverts the TARE settings to the original default settings */
|
||||
public void revertSettings(View v) {
|
||||
Toast.makeText(this, R.string.tare_settings_reverted_toast, Toast.LENGTH_LONG).show();
|
||||
@@ -77,7 +99,6 @@ public class TareHomePage extends Activity {
|
||||
Settings.Global.TARE_ALARM_MANAGER_CONSTANTS, null);
|
||||
Settings.Global.putString(getApplicationContext().getContentResolver(),
|
||||
Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS, null);
|
||||
setEnabled(Settings.Global.DEFAULT_ENABLE_TARE == SETTING_VALUE_ON);
|
||||
}
|
||||
|
||||
/** Opens up the AlarmManager TARE policy page with its factors to view and edit */
|
||||
@@ -101,4 +122,42 @@ public class TareHomePage extends Activity {
|
||||
mJobSchedulerView.setEnabled(tareStatus);
|
||||
mOnSwitch.setChecked(tareStatus);
|
||||
}
|
||||
|
||||
private class ConfigObserver extends ContentObserver {
|
||||
private int mEnableTareSetting;
|
||||
|
||||
ConfigObserver(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
getContentResolver().registerContentObserver(
|
||||
Settings.Global.getUriFor(Settings.Global.ENABLE_TARE), false, this);
|
||||
processEnableTareChange();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
getContentResolver().unregisterContentObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
processEnableTareChange();
|
||||
}
|
||||
|
||||
private void processEnableTareChange() {
|
||||
final String setting =
|
||||
Settings.Global.getString(getContentResolver(), Settings.Global.ENABLE_TARE);
|
||||
if (setting == null ) {
|
||||
mEnableTareSetting = SETTING_VALUE_DEFAULT;
|
||||
} else {
|
||||
try {
|
||||
mEnableTareSetting = Integer.parseInt(setting);
|
||||
} catch (NumberFormatException e) {
|
||||
mEnableTareSetting = Settings.Global.DEFAULT_ENABLE_TARE;
|
||||
}
|
||||
}
|
||||
setEnabled(mEnableTareSetting == SETTING_VALUE_ON);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user