1 /*
2  * Copyright (C) 2017 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.development;
18 
19 import android.app.backup.IBackupManager;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 import android.os.UserManager;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
31 
32 public class LocalBackupPasswordPreferenceController extends DeveloperOptionsPreferenceController
33         implements PreferenceControllerMixin {
34 
35     private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password";
36 
37     private final UserManager mUserManager;
38     private final IBackupManager mBackupManager;
39 
LocalBackupPasswordPreferenceController(Context context)40     public LocalBackupPasswordPreferenceController(Context context) {
41         super(context);
42 
43         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
44         mBackupManager = IBackupManager.Stub.asInterface(
45                 ServiceManager.getService(Context.BACKUP_SERVICE));
46     }
47 
48     @Override
getPreferenceKey()49     public String getPreferenceKey() {
50         return LOCAL_BACKUP_PASSWORD;
51     }
52 
53     @Override
updateState(Preference preference)54     public void updateState(Preference preference) {
55         updatePasswordSummary(preference);
56     }
57 
updatePasswordSummary(Preference preference)58     private void updatePasswordSummary(Preference preference) {
59         preference.setEnabled(isAdminUser() && mBackupManager != null);
60         if (mBackupManager == null) {
61             return;
62         }
63         try {
64             if (mBackupManager.hasBackupPassword()) {
65                 preference.setSummary(R.string.local_backup_password_summary_change);
66             } else {
67                 preference.setSummary(R.string.local_backup_password_summary_none);
68             }
69         } catch (RemoteException e) {
70             // Not much we can do here
71         }
72     }
73 
74     @VisibleForTesting
isAdminUser()75     boolean isAdminUser() {
76         return mUserManager.isAdminUser();
77     }
78 }
79