1/*
2 * Copyright (C) 2016 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.ComponentName;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.os.UserManager;
23import android.provider.Settings;
24import android.support.v14.preference.SwitchPreference;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27
28import com.android.settings.core.PreferenceControllerMixin;
29import com.android.settingslib.core.AbstractPreferenceController;
30
31public class BugReportInPowerPreferenceController extends AbstractPreferenceController
32        implements PreferenceControllerMixin {
33
34    private static final String KEY_BUGREPORT_IN_POWER = "bugreport_in_power";
35
36    private UserManager mUserManager;
37    private SwitchPreference mPreference;
38
39    public BugReportInPowerPreferenceController(Context context) {
40        super(context);
41        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
42    }
43
44    @Override
45    public boolean handlePreferenceTreeClick(Preference preference) {
46        if (KEY_BUGREPORT_IN_POWER.equals(preference.getKey())) {
47            final SwitchPreference switchPreference = (SwitchPreference) preference;
48            Settings.Secure.putInt(mContext.getContentResolver(),
49                Settings.Global.BUGREPORT_IN_POWER_MENU,
50                switchPreference.isChecked() ? 1 : 0);
51            setBugreportStorageProviderStatus();
52            return true;
53        }
54        return false;
55    }
56
57    @Override
58    public void displayPreference(PreferenceScreen screen) {
59        super.displayPreference(screen);
60        if (isAvailable()) {
61            mPreference = (SwitchPreference) screen.findPreference(KEY_BUGREPORT_IN_POWER);
62        }
63    }
64
65    @Override
66    public String getPreferenceKey() {
67        return KEY_BUGREPORT_IN_POWER;
68    }
69
70    @Override
71    public boolean isAvailable() {
72        return !mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
73    }
74
75    @Override
76    public void updateState(Preference preference) {
77        updatePreference();
78    }
79
80    public void enablePreference(boolean enabled) {
81        if (isAvailable()) {
82            mPreference.setEnabled(enabled);
83        }
84    }
85
86    public void resetPreference() {
87        if (mPreference.isChecked()) {
88            mPreference.setChecked(false);
89            handlePreferenceTreeClick(mPreference);
90        }
91    }
92
93    public boolean updatePreference() {
94        if (!isAvailable()) {
95            return false;
96        }
97        final boolean enabled = Settings.Secure.getInt(
98            mContext.getContentResolver(), Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0;
99        mPreference.setChecked(enabled);
100        return enabled;
101    }
102
103    public void updateBugreportOptions() {
104        if (!isAvailable()) {
105            return;
106        }
107        mPreference.setEnabled(true);
108        setBugreportStorageProviderStatus();
109    }
110
111    private void setBugreportStorageProviderStatus() {
112        final ComponentName componentName = new ComponentName("com.android.shell",
113            "com.android.shell.BugreportStorageProvider");
114        final boolean enabled = mPreference.isChecked();
115        mContext.getPackageManager().setComponentEnabledSetting(componentName,
116            enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
117                : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
118            0);
119    }
120
121}
122