1 /*
2  * Copyright (C) 2015 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 package com.android.settings.applications;
17 
18 import android.annotation.Nullable;
19 import android.app.Activity;
20 import android.app.settings.SettingsEnums;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.Button;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.InstrumentedFragment;
31 import com.android.settings.core.SubSettingLauncher;
32 import com.android.settings.password.ChooseLockSettingsHelper;
33 
34 /* Class to prompt for conversion of userdata to file based encryption
35  */
36 public class ConvertToFbe extends InstrumentedFragment {
37     static final String TAG = "ConvertToFBE";
38     private static final int KEYGUARD_REQUEST = 55;
39 
runKeyguardConfirmation(int request)40     private boolean runKeyguardConfirmation(int request) {
41         Resources res = getActivity().getResources();
42         return new ChooseLockSettingsHelper(getActivity(), this)
43                 .launchConfirmationActivity(request,
44                         res.getText(R.string.convert_to_file_encryption));
45     }
46 
47     @Override
onCreate(@ullable Bundle savedInstanceState)48     public void onCreate(@Nullable Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         getActivity().setTitle(R.string.convert_to_file_encryption);
51     }
52 
53     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)54     public View onCreateView(LayoutInflater inflater, ViewGroup container,
55             Bundle savedInstanceState) {
56         View rootView = inflater.inflate(R.layout.convert_fbe, null);
57 
58         final Button button = rootView.findViewById(R.id.button_convert_fbe);
59         button.setOnClickListener(v -> {
60             if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
61                 convert();
62             }
63         });
64 
65         return rootView;
66     }
67 
68     @Override
onActivityResult(int requestCode, int resultCode, Intent data)69     public void onActivityResult(int requestCode, int resultCode, Intent data) {
70         super.onActivityResult(requestCode, resultCode, data);
71 
72         if (requestCode != KEYGUARD_REQUEST) {
73             return;
74         }
75 
76         // If the user entered a valid keyguard credential, start the conversion
77         // process
78         if (resultCode == Activity.RESULT_OK) {
79             convert();
80         }
81     }
82 
convert()83     private void convert() {
84         new SubSettingLauncher(getContext())
85                 .setDestination(ConfirmConvertToFbe.class.getName())
86                 .setTitleRes(R.string.convert_to_file_encryption)
87                 .setSourceMetricsCategory(getMetricsCategory())
88                 .launch();
89     }
90 
91     @Override
getMetricsCategory()92     public int getMetricsCategory() {
93         return SettingsEnums.CONVERT_FBE;
94     }
95 }
96