blob: a44e18253f0c4fbc55daf3babc20e7d1bebc6771 [file] [log] [blame]
Amith Yamasanie65c9432009-09-22 13:50:19 -07001/*
2 * Copyright (C) 2009 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;
18
Christopher Tate616c3ee2010-03-05 15:48:39 -080019import android.app.backup.IBackupManager;
Amith Yamasanie65c9432009-09-22 13:50:19 -070020import android.content.ContentResolver;
21import android.content.Context;
Christopher Tatedf6a7e52010-12-08 18:46:38 -080022import android.content.Intent;
Christopher Tate4a6c2592016-03-16 13:45:03 -070023import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Amith Yamasanie65c9432009-09-22 13:50:19 -070025import android.os.Bundle;
26import android.os.RemoteException;
27import android.os.ServiceManager;
Amith Yamasani4339a472014-10-22 13:51:53 -070028import android.os.UserHandle;
Julia Reynolds2c539332014-06-11 12:56:02 -040029import android.os.UserManager;
Amith Yamasani4339a472014-10-22 13:51:53 -070030import android.provider.SearchIndexableResource;
Amith Yamasanie65c9432009-09-22 13:50:19 -070031import android.provider.Settings;
Fan Zhangac81f262016-12-21 17:06:38 +000032import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040033import android.support.v14.preference.SwitchPreference;
34import android.support.v7.preference.Preference;
35import android.support.v7.preference.Preference.OnPreferenceChangeListener;
36import android.support.v7.preference.PreferenceScreen;
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +000037import android.util.Log;
Amith Yamasanie65c9432009-09-22 13:50:19 -070038
Tamas Berghammer265d3c22016-06-22 15:34:45 +010039import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Amith Yamasani4339a472014-10-22 13:51:53 -070040import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Megliod34dd4f2014-11-06 16:57:04 -080041import com.android.settings.search.Indexable;
Sudheer Shanka9e9e63b2016-02-11 18:48:14 +000042import com.android.settingslib.RestrictedLockUtils;
Jason Monk39b46742015-09-10 15:52:51 -040043
Amith Yamasani4339a472014-10-22 13:51:53 -070044import java.util.ArrayList;
Stuart Scottbe903412014-07-24 19:22:06 -070045import java.util.Collection;
46import java.util.HashSet;
Amith Yamasani4339a472014-10-22 13:51:53 -070047import java.util.List;
Stuart Scottbe903412014-07-24 19:22:06 -070048import java.util.Set;
Amith Yamasani4339a472014-10-22 13:51:53 -070049
Amith Yamasanie65c9432009-09-22 13:50:19 -070050/**
51 * Gesture lock pattern settings.
52 */
Anton Philippov9a5220e2017-03-01 21:08:56 +000053public class PrivacySettings extends SettingsPreferenceFragment {
Amith Yamasanie65c9432009-09-22 13:50:19 -070054
55 // Vendor specific
Bjorn Bringertecfeaff2010-01-25 20:30:39 +000056 private static final String GSETTINGS_PROVIDER = "com.google.settings";
Fan Zhangac81f262016-12-21 17:06:38 +000057 @VisibleForTesting
58 static final String BACKUP_DATA = "backup_data";
59 @VisibleForTesting
60 static final String AUTO_RESTORE = "auto_restore";
61 @VisibleForTesting
62 static final String CONFIGURE_ACCOUNT = "configure_account";
63 @VisibleForTesting
64 static final String DATA_MANAGEMENT = "data_management";
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +000065 private static final String BACKUP_INACTIVE = "backup_inactive";
Stuart Scottbe903412014-07-24 19:22:06 -070066 private static final String FACTORY_RESET = "factory_reset";
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +000067 private static final String TAG = "PrivacySettings";
Christopher Tatedf6a7e52010-12-08 18:46:38 -080068 private IBackupManager mBackupManager;
Fan Zhangac81f262016-12-21 17:06:38 +000069 private Preference mBackup;
aretece543832014-08-12 16:33:52 -070070 private SwitchPreference mAutoRestore;
Fan Zhangac81f262016-12-21 17:06:38 +000071 private Preference mConfigure;
72 private Preference mManageData;
Amith Yamasani4339a472014-10-22 13:51:53 -070073 private boolean mEnabled;
Amith Yamasanie65c9432009-09-22 13:50:19 -070074
Amith Yamasanie65c9432009-09-22 13:50:19 -070075 @Override
Fan Zhang65076132016-08-08 10:25:13 -070076 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -050077 return MetricsEvent.PRIVACY;
Chris Wren8a963ba2015-03-20 10:29:14 -040078 }
79
80 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070081 public void onCreate(Bundle savedInstanceState) {
Amith Yamasanie65c9432009-09-22 13:50:19 -070082 super.onCreate(savedInstanceState);
Xiaohui Chen44879a32015-07-22 13:53:22 -070083 // Don't allow any access if this is not an admin user.
84 // TODO: backup/restore currently only works with owner user b/22760572
85 mEnabled = UserManager.get(getActivity()).isAdminUser();
Amith Yamasani4339a472014-10-22 13:51:53 -070086 if (!mEnabled) {
87 return;
88 }
89
Amith Yamasanie65c9432009-09-22 13:50:19 -070090 addPreferencesFromResource(R.xml.privacy_settings);
Christopher Tatea5a67302010-02-03 12:54:31 -080091 final PreferenceScreen screen = getPreferenceScreen();
Christopher Tatedf6a7e52010-12-08 18:46:38 -080092 mBackupManager = IBackupManager.Stub.asInterface(
93 ServiceManager.getService(Context.BACKUP_SERVICE));
94
Fan Zhangac81f262016-12-21 17:06:38 +000095 setPreferenceReferences(screen);
Amith Yamasanie65c9432009-09-22 13:50:19 -070096
Stuart Scottbe903412014-07-24 19:22:06 -070097 Set<String> keysToRemove = new HashSet<>();
98 getNonVisibleKeys(getActivity(), keysToRemove);
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +000099 final int screenPreferenceCount = screen.getPreferenceCount();
100 for (int i = screenPreferenceCount - 1; i >= 0; --i) {
101 Preference preference = screen.getPreference(i);
102 if (keysToRemove.contains(preference.getKey())) {
103 screen.removePreference(preference);
104 }
Julia Reynolds2c539332014-06-11 12:56:02 -0400105 }
Stuart Scottbe903412014-07-24 19:22:06 -0700106
Amith Yamasanie65c9432009-09-22 13:50:19 -0700107 updateToggles();
Amith Yamasanie65c9432009-09-22 13:50:19 -0700108 }
109
110 @Override
Fabrice Di Meglio70bc7972011-01-09 19:56:51 -0800111 public void onResume() {
112 super.onResume();
113
114 // Refresh UI
Amith Yamasani4339a472014-10-22 13:51:53 -0700115 if (mEnabled) {
116 updateToggles();
117 }
Fabrice Di Meglio70bc7972011-01-09 19:56:51 -0800118 }
119
Fan Zhangac81f262016-12-21 17:06:38 +0000120 @VisibleForTesting
121 void setPreferenceReferences(PreferenceScreen screen) {
122 mBackup = screen.findPreference(BACKUP_DATA);
123
124 mAutoRestore = (SwitchPreference) screen.findPreference(AUTO_RESTORE);
125 mAutoRestore.setOnPreferenceChangeListener(preferenceChangeListener);
126
127 mConfigure = screen.findPreference(CONFIGURE_ACCOUNT);
128 mManageData = screen.findPreference(DATA_MANAGEMENT);
129 }
130
aretece543832014-08-12 16:33:52 -0700131 private OnPreferenceChangeListener preferenceChangeListener = new OnPreferenceChangeListener() {
132 @Override
133 public boolean onPreferenceChange(Preference preference, Object newValue) {
134 if (!(preference instanceof SwitchPreference)) {
135 return true;
Amith Yamasanie65c9432009-09-22 13:50:19 -0700136 }
aretece543832014-08-12 16:33:52 -0700137 boolean nextValue = (Boolean) newValue;
138 boolean result = false;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700139 if (preference == mAutoRestore) {
aretece543832014-08-12 16:33:52 -0700140 try {
141 mBackupManager.setAutoRestore(nextValue);
142 result = true;
143 } catch (RemoteException e) {
144 mAutoRestore.setChecked(!nextValue);
145 }
Christopher Tatea5a67302010-02-03 12:54:31 -0800146 }
aretece543832014-08-12 16:33:52 -0700147 return result;
Amith Yamasanie65c9432009-09-22 13:50:19 -0700148 }
aretece543832014-08-12 16:33:52 -0700149 };
Amith Yamasanie65c9432009-09-22 13:50:19 -0700150
Amith Yamasanie65c9432009-09-22 13:50:19 -0700151
152 /*
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700153 * Creates toggles for each backup/reset preference.
Amith Yamasanie65c9432009-09-22 13:50:19 -0700154 */
155 private void updateToggles() {
156 ContentResolver res = getContentResolver();
Christopher Tatea5a67302010-02-03 12:54:31 -0800157
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800158 boolean backupEnabled = false;
159 Intent configIntent = null;
160 String configSummary = null;
Vladislav Kuzkokovddb953f2016-01-25 17:51:55 +0100161 Intent manageIntent = null;
162 String manageLabel = null;
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800163 try {
164 backupEnabled = mBackupManager.isBackupEnabled();
165 String transport = mBackupManager.getCurrentTransport();
Christopher Tate4a6c2592016-03-16 13:45:03 -0700166 configIntent = validatedActivityIntent(
167 mBackupManager.getConfigurationIntent(transport), "config");
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800168 configSummary = mBackupManager.getDestinationString(transport);
Christopher Tate4a6c2592016-03-16 13:45:03 -0700169 manageIntent = validatedActivityIntent(
170 mBackupManager.getDataManagementIntent(transport), "management");
Vladislav Kuzkokovddb953f2016-01-25 17:51:55 +0100171 manageLabel = mBackupManager.getDataManagementLabel(transport);
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700172
173 mBackup.setSummary(backupEnabled
174 ? R.string.accessibility_feature_state_on
175 : R.string.accessibility_feature_state_off);
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800176 } catch (RemoteException e) {
177 // leave it 'false' and disable the UI; there's no backup manager
178 mBackup.setEnabled(false);
179 }
Christopher Tatea5a67302010-02-03 12:54:31 -0800180
181 mAutoRestore.setChecked(Settings.Secure.getInt(res,
Christopher Tatec99cffb2010-02-25 18:00:29 -0800182 Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1);
Christopher Tatea5a67302010-02-03 12:54:31 -0800183 mAutoRestore.setEnabled(backupEnabled);
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800184
Fabrice Di Meglio70bc7972011-01-09 19:56:51 -0800185 final boolean configureEnabled = (configIntent != null) && backupEnabled;
186 mConfigure.setEnabled(configureEnabled);
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800187 mConfigure.setIntent(configIntent);
Fabrice Di Meglio70bc7972011-01-09 19:56:51 -0800188 setConfigureSummary(configSummary);
Sudheer Shanka682a9162016-01-13 22:10:18 +0000189
Vladislav Kuzkokovddb953f2016-01-25 17:51:55 +0100190 final boolean manageEnabled = (manageIntent != null) && backupEnabled;
191 if (manageEnabled) {
192 mManageData.setIntent(manageIntent);
193 if (manageLabel != null) {
194 mManageData.setTitle(manageLabel);
195 }
196 } else {
197 // Hide the item if data management intent is not supported by transport.
198 getPreferenceScreen().removePreference(mManageData);
199 }
aretece543832014-08-12 16:33:52 -0700200 }
Fabrice Di Meglio70bc7972011-01-09 19:56:51 -0800201
Christopher Tate4a6c2592016-03-16 13:45:03 -0700202 private Intent validatedActivityIntent(Intent intent, String logLabel) {
203 if (intent != null) {
204 PackageManager pm = getPackageManager();
205 List<ResolveInfo> resolved = pm.queryIntentActivities(intent, 0);
206 if (resolved == null || resolved.isEmpty()) {
207 intent = null;
208 Log.e(TAG, "Backup " + logLabel + " intent " + intent
209 + " fails to resolve; ignoring");
210 }
211 }
212 return intent;
213 }
214
Fabrice Di Meglio70bc7972011-01-09 19:56:51 -0800215 private void setConfigureSummary(String summary) {
216 if (summary != null) {
217 mConfigure.setSummary(summary);
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800218 } else {
Stuart Scott1da842a2015-06-03 17:01:42 -0700219 mConfigure.setSummary(R.string.backup_configure_account_default_summary);
Christopher Tatedf6a7e52010-12-08 18:46:38 -0800220 }
Amith Yamasanie65c9432009-09-22 13:50:19 -0700221 }
222
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700223 @Override
224 protected int getHelpResource() {
225 return R.string.help_url_backup_reset;
226 }
Amith Yamasani4339a472014-10-22 13:51:53 -0700227
Stuart Scottbe903412014-07-24 19:22:06 -0700228 private static void getNonVisibleKeys(Context context, Collection<String> nonVisibleKeys) {
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +0000229 final IBackupManager backupManager = IBackupManager.Stub.asInterface(
230 ServiceManager.getService(Context.BACKUP_SERVICE));
231 boolean isServiceActive = false;
232 try {
233 isServiceActive = backupManager.isBackupServiceActive(UserHandle.myUserId());
234 } catch (RemoteException e) {
235 Log.w(TAG, "Failed querying backup manager service activity status. " +
236 "Assuming it is inactive.");
237 }
Stuart Scottbe903412014-07-24 19:22:06 -0700238 boolean vendorSpecific = context.getPackageManager().
239 resolveContentProvider(GSETTINGS_PROVIDER, 0) == null;
240 if (vendorSpecific || isServiceActive) {
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +0000241 nonVisibleKeys.add(BACKUP_INACTIVE);
Stuart Scottbe903412014-07-24 19:22:06 -0700242 }
243 if (vendorSpecific || !isServiceActive) {
244 nonVisibleKeys.add(BACKUP_DATA);
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +0000245 nonVisibleKeys.add(AUTO_RESTORE);
246 nonVisibleKeys.add(CONFIGURE_ACCOUNT);
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +0000247 }
Sudheer Shanka9e9e63b2016-02-11 18:48:14 +0000248 if (RestrictedLockUtils.hasBaseUserRestriction(context,
249 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
250 nonVisibleKeys.add(FACTORY_RESET);
251 }
Zoltan Szatmary-Ban876a21b2014-11-12 23:46:52 +0000252 }
253}