1 /*
2  * Copyright (C) 2022 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.SystemProperties;
21 import android.text.TextUtils;
22 
23 import androidx.fragment.app.Fragment;
24 import androidx.preference.Preference;
25 
26 import com.android.settings.R;
27 import com.android.settings.Utils;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settings.security.MemtagHelper;
31 import com.android.settingslib.development.DevelopmentSettingsEnabler;
32 
33 public class RebootWithMtePreferenceController extends BasePreferenceController
34         implements PreferenceControllerMixin {
35     private static final String KEY_REBOOT_WITH_MTE = "reboot_with_mte";
36 
37     private Fragment mFragment;
38 
RebootWithMtePreferenceController(Context context)39     public RebootWithMtePreferenceController(Context context) {
40         super(context, KEY_REBOOT_WITH_MTE);
41     }
42 
43     @Override
getAvailabilityStatus()44     public int getAvailabilityStatus() {
45         return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)
46                         && SystemProperties.getBoolean("ro.arm64.memtag.bootctl_supported", false)
47                 ? BasePreferenceController.AVAILABLE
48                 : BasePreferenceController.UNSUPPORTED_ON_DEVICE;
49     }
50 
51     @Override
getSummary()52     public CharSequence getSummary() {
53         if (MemtagHelper.isChecked()) {
54             return mContext.getResources().getString(R.string.reboot_with_mte_already_enabled);
55         }
56         return mContext.getResources().getString(R.string.reboot_with_mte_summary);
57     }
58 
59     @Override
updateState(Preference preference)60     public void updateState(Preference preference) {
61         super.updateState(preference);
62         preference.setEnabled(!MemtagHelper.isChecked());
63     }
64 
65     @Override
getPreferenceKey()66     public String getPreferenceKey() {
67         return KEY_REBOOT_WITH_MTE;
68     }
69 
setFragment(Fragment fragment)70     public void setFragment(Fragment fragment) {
71         mFragment = fragment;
72     }
73 
74     @Override
handlePreferenceTreeClick(Preference preference)75     public boolean handlePreferenceTreeClick(Preference preference) {
76         if (Utils.isMonkeyRunning()) {
77             return false;
78         }
79 
80         if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
81             RebootWithMteDialog.show(mContext, mFragment);
82             return true;
83         }
84         return false;
85     }
86 }
87