Files
app_Settings/src/com/android/settings/development/tare/TareHomePage.java
Kweku Adams 058796b7eb 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
2022-03-23 16:43:39 +00:00

164 lines
6.1 KiB
Java

/*
* Copyright (C) 2021 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.development.tare;
import static com.android.settings.development.tare.DropdownActivity.EXTRA_POLICY;
import static com.android.settings.development.tare.DropdownActivity.POLICY_ALARM_MANAGER;
import static com.android.settings.development.tare.DropdownActivity.POLICY_JOB_SCHEDULER;
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;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.android.settings.R;
/** Class for creating the TARE homepage in settings */
public class TareHomePage extends Activity {
private Switch mOnSwitch;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tare_homepage);
mOnSwitch = findViewById(R.id.on_switch);
mRevButton = findViewById(R.id.revert_button);
mAlarmManagerView = findViewById(R.id.alarmmanager);
mJobSchedulerView = findViewById(R.id.jobscheduler);
mConfigObserver = new ConfigObserver(new Handler(Looper.getMainLooper()));
mOnSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean 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);
}
});
}
@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();
Settings.Global.putString(getApplicationContext().getContentResolver(),
Settings.Global.ENABLE_TARE, null);
Settings.Global.putString(getApplicationContext().getContentResolver(),
Settings.Global.TARE_ALARM_MANAGER_CONSTANTS, null);
Settings.Global.putString(getApplicationContext().getContentResolver(),
Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS, null);
}
/** Opens up the AlarmManager TARE policy page with its factors to view and edit */
public void launchAlarmManagerPage(View v) {
Intent i = new Intent(getApplicationContext(), DropdownActivity.class);
i.putExtra(EXTRA_POLICY, POLICY_ALARM_MANAGER);
startActivity(i);
}
/** Opens up the JobScheduler TARE policy page with its factors to view and edit */
public void launchJobSchedulerPage(View v) {
Intent i = new Intent(getApplicationContext(), DropdownActivity.class);
i.putExtra(EXTRA_POLICY, POLICY_JOB_SCHEDULER);
startActivity(i);
}
/** Changes the enabled state of the TARE homepage buttons based on global toggle */
private void setEnabled(boolean tareStatus) {
mRevButton.setEnabled(tareStatus);
mAlarmManagerView.setEnabled(tareStatus);
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);
}
}
}