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.TwoStatePreference; 29 30 import com.android.settings.R; 31 import com.android.settings.core.TogglePreferenceController; 32 33 public class AutoRestorePreferenceController extends TogglePreferenceController { 34 private static final String TAG = "AutoRestorePrefCtrler"; 35 36 private PrivacySettingsConfigData mPSCD; 37 private Preference mPreference; 38 AutoRestorePreferenceController(Context context, String key)39 public AutoRestorePreferenceController(Context context, String key) { 40 super(context, key); 41 mPSCD = PrivacySettingsConfigData.getInstance(); 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 if (!PrivacySettingsUtils.isAdminUser(mContext)) { 47 return DISABLED_FOR_USER; 48 } 49 if (PrivacySettingsUtils.isInvisibleKey(mContext, PrivacySettingsUtils.AUTO_RESTORE)) { 50 return UNSUPPORTED_ON_DEVICE; 51 } 52 return AVAILABLE; 53 } 54 55 @Override updateState(Preference preference)56 public void updateState(Preference preference) { 57 super.updateState(preference); 58 mPreference = preference; 59 preference.setEnabled(mPSCD.isBackupEnabled()); 60 } 61 62 @Override isChecked()63 public boolean isChecked() { 64 final ContentResolver res = mContext.getContentResolver(); 65 66 return Settings.Secure.getInt(res, 67 Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1; 68 } 69 70 @Override setChecked(boolean isChecked)71 public boolean setChecked(boolean isChecked) { 72 final boolean nextValue = isChecked; 73 boolean result = false; 74 75 final IBackupManager backupManager = IBackupManager.Stub.asInterface( 76 ServiceManager.getService(Context.BACKUP_SERVICE)); 77 78 try { 79 backupManager.setAutoRestore(nextValue); 80 result = true; 81 } catch (RemoteException e) { 82 ((TwoStatePreference) mPreference).setChecked(!nextValue); 83 Log.e(TAG, "Error can't set setAutoRestore", e); 84 } 85 86 return result; 87 } 88 89 @Override getSliceHighlightMenuRes()90 public int getSliceHighlightMenuRes() { 91 return R.string.menu_key_system; 92 } 93 }