1 /*
2  * Copyright (C) 2020 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.emergency;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ResolveInfo;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settingslib.emergencynumber.EmergencyNumberUtils;
30 
31 /**
32  * Preference controller for emergency gesture setting's entyrpoint preference
33  */
34 public class EmergencyGestureEntrypointPreferenceController extends BasePreferenceController {
35 
36     @VisibleForTesting
37     static final String ACTION_EMERGENCY_GESTURE_SETTINGS =
38             "com.android.settings.action.emergency_gesture_settings";
39     @VisibleForTesting
40     Intent mIntent;
41 
42     private final EmergencyNumberUtils mEmergencyNumberUtils;
43     private boolean mUseCustomIntent;
44 
EmergencyGestureEntrypointPreferenceController(Context context, String key)45     public EmergencyGestureEntrypointPreferenceController(Context context, String key) {
46         super(context, key);
47         mEmergencyNumberUtils = new EmergencyNumberUtils(context);
48         final String emergencyGestureSettingsPackageName = context.getResources().getString(
49                 R.string.emergency_gesture_settings_package);
50         if (!TextUtils.isEmpty(emergencyGestureSettingsPackageName)) {
51             final Intent intent = new Intent(ACTION_EMERGENCY_GESTURE_SETTINGS)
52                     .setPackage(emergencyGestureSettingsPackageName);
53             if (canResolveIntent(intent)) {
54                 // Use custom intent if it's configured and system can resolve it.
55                 mUseCustomIntent = true;
56                 mIntent = intent;
57             }
58         }
59     }
60 
61     @Override
updateState(Preference preference)62     public void updateState(Preference preference) {
63         super.updateState(preference);
64         final boolean canHandleClicks = canHandleClicks();
65         if (preference != null) {
66             preference.setEnabled(canHandleClicks);
67         }
68     }
69 
70     @Override
handlePreferenceTreeClick(Preference preference)71     public boolean handlePreferenceTreeClick(Preference preference) {
72         if (TextUtils.equals(getPreferenceKey(), preference.getKey()) && mIntent != null) {
73             mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
74             mContext.startActivity(mIntent);
75             return true;
76         }
77         return super.handlePreferenceTreeClick(preference);
78     }
79 
80     @Override
getAvailabilityStatus()81     public int getAvailabilityStatus() {
82         final boolean isConfigEnabled = mContext.getResources()
83                 .getBoolean(R.bool.config_show_emergency_gesture_settings);
84 
85         if (!isConfigEnabled) {
86             return UNSUPPORTED_ON_DEVICE;
87         }
88         if (!canHandleClicks()) {
89             return UNSUPPORTED_ON_DEVICE;
90         }
91         return AVAILABLE;
92     }
93 
94     @Override
getSummary()95     public CharSequence getSummary() {
96         return mContext.getText(
97                 mEmergencyNumberUtils.getEmergencyGestureEnabled()
98                         ? R.string.gesture_setting_on
99                         : R.string.gesture_setting_off);
100     }
101 
102     /**
103      * Whether or not gesture page content should be suppressed from search.
104      */
shouldSuppressFromSearch()105     public boolean shouldSuppressFromSearch() {
106         return mUseCustomIntent;
107     }
108 
109     /**
110      * Whether or not this setting can react to user click
111      */
canHandleClicks()112     private boolean canHandleClicks() {
113         return !mUseCustomIntent || mIntent != null;
114     }
115 
canResolveIntent(Intent intent)116     private boolean canResolveIntent(Intent intent) {
117         final ResolveInfo resolveActivity = mContext.getPackageManager()
118                 .resolveActivity(intent, 0);
119         return resolveActivity != null;
120     }
121 }
122