• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
20 import android.os.RemoteException;
21 import android.os.ServiceManager;
22 import android.os.SystemProperties;
23 import android.os.storage.IStorageManager;
24 import android.support.annotation.VisibleForTesting;
25 import android.support.v7.preference.Preference;
26 import android.text.TextUtils;
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 FileEncryptionPreferenceController extends DeveloperOptionsPreferenceController
33         implements PreferenceControllerMixin {
34 
35     private static final String KEY_CONVERT_FBE = "convert_to_file_encryption";
36     private static final String KEY_STORAGE_MANAGER = "mount";
37 
38     @VisibleForTesting
39     static final String FILE_ENCRYPTION_PROPERTY_KEY = "ro.crypto.type";
40 
41     private final IStorageManager mStorageManager;
42 
FileEncryptionPreferenceController(Context context)43     public FileEncryptionPreferenceController(Context context) {
44         super(context);
45 
46         mStorageManager = getStorageManager();
47     }
48 
49     @Override
isAvailable()50     public boolean isAvailable() {
51         if (mStorageManager == null) {
52             return false;
53         }
54 
55         try {
56             return mStorageManager.isConvertibleToFBE();
57         } catch (RemoteException e) {
58             return false;
59         }
60     }
61 
62     @Override
getPreferenceKey()63     public String getPreferenceKey() {
64         return KEY_CONVERT_FBE;
65     }
66 
67     @Override
updateState(Preference preference)68     public void updateState(Preference preference) {
69         if (!TextUtils.equals("file",
70                 SystemProperties.get(FILE_ENCRYPTION_PROPERTY_KEY, "none" /* default */))) {
71             return;
72         }
73 
74         mPreference.setEnabled(false);
75         mPreference.setSummary(
76                 mContext.getResources().getString(R.string.convert_to_file_encryption_done));
77     }
78 
getStorageManager()79     private IStorageManager getStorageManager() {
80         try {
81             return IStorageManager.Stub.asInterface(
82                     ServiceManager.getService(KEY_STORAGE_MANAGER));
83         } catch (VerifyError e) {
84             // Used for tests since Robolectric cannot initialize this class.
85             return null;
86         }
87     }
88 }