• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.settings.backup;
18 
19 import android.app.backup.IBackupManager;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import android.provider.Settings;
25 import android.util.Log;
26 
27 import androidx.preference.Preference;
28 import androidx.preference.SwitchPreference;
29 
30 import com.android.settings.core.TogglePreferenceController;
31 
32 public class AutoRestorePreferenceController extends TogglePreferenceController {
33     private static final String TAG = "AutoRestorePrefCtrler";
34 
35     private PrivacySettingsConfigData mPSCD;
36     private Preference mPreference;
37 
AutoRestorePreferenceController(Context context, String key)38     public AutoRestorePreferenceController(Context context, String key) {
39         super(context, key);
40         mPSCD = PrivacySettingsConfigData.getInstance();
41     }
42 
43     @Override
getAvailabilityStatus()44     public int getAvailabilityStatus() {
45         if (!PrivacySettingsUtils.isAdminUser(mContext)) {
46             return DISABLED_FOR_USER;
47         }
48         if (PrivacySettingsUtils.isInvisibleKey(mContext, PrivacySettingsUtils.AUTO_RESTORE)) {
49             return UNSUPPORTED_ON_DEVICE;
50         }
51         return AVAILABLE;
52     }
53 
54     @Override
updateState(Preference preference)55     public void updateState(Preference preference) {
56         super.updateState(preference);
57         mPreference = preference;
58         preference.setEnabled(mPSCD.isBackupEnabled());
59     }
60 
61     @Override
isChecked()62     public boolean isChecked() {
63         final ContentResolver res = mContext.getContentResolver();
64 
65         return Settings.Secure.getInt(res,
66                 Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1;
67     }
68 
69     @Override
setChecked(boolean isChecked)70     public boolean setChecked(boolean isChecked) {
71         final boolean nextValue = isChecked;
72         boolean result = false;
73 
74         final IBackupManager backupManager = IBackupManager.Stub.asInterface(
75                 ServiceManager.getService(Context.BACKUP_SERVICE));
76 
77         try {
78             backupManager.setAutoRestore(nextValue);
79             result = true;
80         } catch (RemoteException e) {
81             ((SwitchPreference) mPreference).setChecked(!nextValue);
82             Log.e(TAG, "Error can't set setAutoRestore", e);
83         }
84 
85         return result;
86     }
87 }