• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.development.tare;
18 
19 import static com.android.settings.development.tare.DropdownActivity.EXTRA_POLICY;
20 import static com.android.settings.development.tare.DropdownActivity.POLICY_ALARM_MANAGER;
21 import static com.android.settings.development.tare.DropdownActivity.POLICY_JOB_SCHEDULER;
22 
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.database.ContentObserver;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Looper;
30 import android.provider.Settings;
31 import android.view.View;
32 import android.widget.Button;
33 import android.widget.CompoundButton;
34 import android.widget.CompoundButton.OnCheckedChangeListener;
35 import android.widget.Switch;
36 import android.widget.TextView;
37 import android.widget.Toast;
38 
39 import com.android.settings.R;
40 
41 /** Class for creating the TARE homepage in settings */
42 public class TareHomePage extends Activity {
43     private Switch mOnSwitch;
44     private Button mRevButton;
45     private TextView mAlarmManagerView;
46     private TextView mJobSchedulerView;
47     private ConfigObserver mConfigObserver;
48 
49     private static final int SETTING_VALUE_DEFAULT = -1;
50     private static final int SETTING_VALUE_OFF = 0;
51     private static final int SETTING_VALUE_ON = 1;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         setContentView(R.layout.tare_homepage);
57 
58         mOnSwitch = findViewById(R.id.on_switch);
59         mRevButton = findViewById(R.id.revert_button);
60         mAlarmManagerView = findViewById(R.id.alarmmanager);
61         mJobSchedulerView = findViewById(R.id.jobscheduler);
62 
63         mConfigObserver = new ConfigObserver(new Handler(Looper.getMainLooper()));
64 
65         mOnSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
66             @Override
67             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
68                 if (mConfigObserver.mEnableTareSetting == SETTING_VALUE_DEFAULT
69                         && isChecked == (Settings.Global.DEFAULT_ENABLE_TARE == SETTING_VALUE_ON)) {
70                     // Don't bother writing something that's not new information. It would make
71                     // it hard to use DeviceConfig if we did.
72                     return;
73                 }
74                 Settings.Global.putInt(getContentResolver(),
75                         Settings.Global.ENABLE_TARE,
76                         isChecked ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
77             }
78         });
79     }
80 
81     @Override
onResume()82     protected void onResume() {
83         super.onResume();
84         mConfigObserver.start();
85     }
86 
87     @Override
onPause()88     protected void onPause() {
89         mConfigObserver.stop();
90         super.onPause();
91     }
92 
93     /** Reverts the TARE settings to the original default settings */
revertSettings(View v)94     public void revertSettings(View v) {
95         Toast.makeText(this, R.string.tare_settings_reverted_toast, Toast.LENGTH_LONG).show();
96         Settings.Global.putString(getApplicationContext().getContentResolver(),
97                 Settings.Global.ENABLE_TARE, null);
98         Settings.Global.putString(getApplicationContext().getContentResolver(),
99                 Settings.Global.TARE_ALARM_MANAGER_CONSTANTS, null);
100         Settings.Global.putString(getApplicationContext().getContentResolver(),
101                 Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS, null);
102     }
103 
104     /** Opens up the AlarmManager TARE policy page with its factors to view and edit */
launchAlarmManagerPage(View v)105     public void launchAlarmManagerPage(View v) {
106         Intent i = new Intent(getApplicationContext(), DropdownActivity.class);
107         i.putExtra(EXTRA_POLICY, POLICY_ALARM_MANAGER);
108         startActivity(i);
109     }
110 
111     /** Opens up the JobScheduler TARE policy page with its factors to view and edit */
launchJobSchedulerPage(View v)112     public void launchJobSchedulerPage(View v) {
113         Intent i = new Intent(getApplicationContext(), DropdownActivity.class);
114         i.putExtra(EXTRA_POLICY, POLICY_JOB_SCHEDULER);
115         startActivity(i);
116     }
117 
118     /** Changes the enabled state of the TARE homepage buttons based on global toggle */
setEnabled(boolean tareStatus)119     private void setEnabled(boolean tareStatus) {
120         mRevButton.setEnabled(tareStatus);
121         mAlarmManagerView.setEnabled(tareStatus);
122         mJobSchedulerView.setEnabled(tareStatus);
123         mOnSwitch.setChecked(tareStatus);
124     }
125 
126     private class ConfigObserver extends ContentObserver {
127         private int mEnableTareSetting;
128 
ConfigObserver(Handler handler)129         ConfigObserver(Handler handler) {
130             super(handler);
131         }
132 
start()133         public void start() {
134             getContentResolver().registerContentObserver(
135                     Settings.Global.getUriFor(Settings.Global.ENABLE_TARE), false, this);
136             processEnableTareChange();
137         }
138 
stop()139         public void stop() {
140             getContentResolver().unregisterContentObserver(this);
141         }
142 
143         @Override
onChange(boolean selfChange, Uri uri)144         public void onChange(boolean selfChange, Uri uri) {
145             processEnableTareChange();
146         }
147 
processEnableTareChange()148         private void processEnableTareChange() {
149             final String setting =
150                     Settings.Global.getString(getContentResolver(), Settings.Global.ENABLE_TARE);
151             if (setting == null ) {
152                 mEnableTareSetting = SETTING_VALUE_DEFAULT;
153             } else {
154                 try {
155                     mEnableTareSetting = Integer.parseInt(setting);
156                 } catch (NumberFormatException e) {
157                     mEnableTareSetting = Settings.Global.DEFAULT_ENABLE_TARE;
158                 }
159             }
160             setEnabled(mEnableTareSetting == SETTING_VALUE_ON);
161         }
162     }
163 }
164