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