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.storage.IStorageManager;
23 import android.text.TextUtils;
24 import android.sysprop.CryptoProperties;
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 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     private final IStorageManager mStorageManager;
39 
FileEncryptionPreferenceController(Context context)40     public FileEncryptionPreferenceController(Context context) {
41         super(context);
42 
43         mStorageManager = getStorageManager();
44     }
45 
46     @Override
isAvailable()47     public boolean isAvailable() {
48         if (mStorageManager == null) {
49             return false;
50         }
51 
52         try {
53             return mStorageManager.isConvertibleToFBE();
54         } catch (RemoteException e) {
55             return false;
56         }
57     }
58 
59     @Override
getPreferenceKey()60     public String getPreferenceKey() {
61         return KEY_CONVERT_FBE;
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         if (CryptoProperties.type().orElse(CryptoProperties.type_values.NONE) !=
67             CryptoProperties.type_values.FILE) {
68             return;
69         }
70 
71         mPreference.setEnabled(false);
72         mPreference.setSummary(
73                 mContext.getResources().getString(R.string.convert_to_file_encryption_done));
74     }
75 
getStorageManager()76     private IStorageManager getStorageManager() {
77         try {
78             return IStorageManager.Stub.asInterface(
79                     ServiceManager.getService(KEY_STORAGE_MANAGER));
80         } catch (VerifyError e) {
81             // Used for tests since Robolectric cannot initialize this class.
82             return null;
83         }
84     }
85 }