blob: 5e0cda829445d2e1d67574362feefcd2b71bb4f7 [file] [log] [blame]
Andrew Sapperstein5cd49562018-01-15 13:46:41 -08001/*
2 * Copyright (C) 2018 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
17package com.android.settings.development;
18
19import android.content.Context;
20import android.provider.Settings;
Andrew Sapperstein5cd49562018-01-15 13:46:41 -080021
Fan Zhangc7162cd2018-06-18 15:21:41 -070022import androidx.annotation.VisibleForTesting;
23import androidx.preference.Preference;
24import androidx.preference.SwitchPreference;
25
Fan Zhang23f8d592018-08-28 15:11:40 -070026import com.android.settings.core.PreferenceControllerMixin;
27import com.android.settingslib.development.DeveloperOptionsPreferenceController;
28
Doris Ling4fbf04c2018-03-01 10:33:14 -080029public class ShowFirstCrashDialogPreferenceController extends DeveloperOptionsPreferenceController
30 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
Andrew Sapperstein5cd49562018-01-15 13:46:41 -080031
32 private static final String SHOW_FIRST_CRASH_DIALOG_KEY = "show_first_crash_dialog";
33
34 @VisibleForTesting
35 static final int SETTING_VALUE_ON = 1;
36 @VisibleForTesting
37 static final int SETTING_VALUE_OFF = 0;
38
Andrew Sapperstein5cd49562018-01-15 13:46:41 -080039 public ShowFirstCrashDialogPreferenceController(Context context) {
40 super(context);
41 }
42
43 @Override
44 public String getPreferenceKey() {
45 return SHOW_FIRST_CRASH_DIALOG_KEY;
46 }
47
48 @Override
49 public boolean isAvailable() {
50 // If the global setting is on, hide this preference since the global overrides
51 // any user preference.
52 return Settings.Global.getInt(mContext.getContentResolver(),
53 Settings.Global.SHOW_FIRST_CRASH_DIALOG, SETTING_VALUE_OFF) == SETTING_VALUE_OFF;
54 }
55
56 @Override
Andrew Sapperstein5cd49562018-01-15 13:46:41 -080057 public boolean onPreferenceChange(Preference preference, Object newValue) {
58 final boolean isEnabled = (Boolean) newValue;
59 Settings.Secure.putInt(mContext.getContentResolver(),
60 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION,
61 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
62 return true;
63 }
64
65 @Override
66 public void updateState(Preference preference) {
67 final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
68 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, SETTING_VALUE_OFF);
Doris Ling4fbf04c2018-03-01 10:33:14 -080069 ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
Andrew Sapperstein5cd49562018-01-15 13:46:41 -080070 }
71
72 @Override
73 protected void onDeveloperOptionsSwitchDisabled() {
Doris Ling4fbf04c2018-03-01 10:33:14 -080074 super.onDeveloperOptionsSwitchDisabled();
Andrew Sapperstein5cd49562018-01-15 13:46:41 -080075 Settings.Secure.putInt(mContext.getContentResolver(),
76 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, SETTING_VALUE_OFF);
Doris Ling4fbf04c2018-03-01 10:33:14 -080077 ((SwitchPreference) mPreference).setChecked(false);
Andrew Sapperstein5cd49562018-01-15 13:46:41 -080078 }
79}