1 /*
2  * Copyright (C) 2019 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.gestures;
18 
19 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
21 
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 
30 public class SystemNavigationPreferenceController extends BasePreferenceController {
31 
32     static final String PREF_KEY_SYSTEM_NAVIGATION = "gesture_system_navigation";
33     private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE";
34 
SystemNavigationPreferenceController(Context context, String key)35     public SystemNavigationPreferenceController(Context context, String key) {
36         super(context, key);
37     }
38 
39     @Override
getAvailabilityStatus()40     public int getAvailabilityStatus() {
41         return isGestureAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
42     }
43 
44     @Override
getSummary()45     public CharSequence getSummary() {
46         if (isEdgeToEdgeEnabled(mContext)) {
47             return mContext.getText(R.string.edge_to_edge_navigation_title);
48         } else if (isSwipeUpEnabled(mContext)) {
49             return mContext.getText(R.string.swipe_up_to_switch_apps_title);
50         } else {
51             return mContext.getText(R.string.legacy_navigation_title);
52         }
53     }
54 
isGestureAvailable(Context context)55     static boolean isGestureAvailable(Context context) {
56         // Skip if the swipe up settings are not available
57         if (!context.getResources().getBoolean(
58                 com.android.internal.R.bool.config_swipe_up_gesture_setting_available)) {
59             return false;
60         }
61 
62         // Skip if the recents component is not defined
63         final ComponentName recentsComponentName = ComponentName.unflattenFromString(
64                 context.getString(com.android.internal.R.string.config_recentsComponentName));
65         if (recentsComponentName == null) {
66             return false;
67         }
68 
69         // Skip if the overview proxy service exists
70         final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP)
71                 .setPackage(recentsComponentName.getPackageName());
72         if (context.getPackageManager().resolveService(quickStepIntent,
73                 PackageManager.MATCH_SYSTEM_ONLY) == null) {
74             return false;
75         }
76 
77         return true;
78     }
79 
isOverlayPackageAvailable(Context context, String overlayPackage)80     static boolean isOverlayPackageAvailable(Context context, String overlayPackage) {
81         try {
82             return context.getPackageManager().getPackageInfo(overlayPackage, 0) != null;
83         } catch (PackageManager.NameNotFoundException e) {
84             // Not found, just return unavailable
85             return false;
86         }
87     }
88 
isSwipeUpEnabled(Context context)89     static boolean isSwipeUpEnabled(Context context) {
90         if (isEdgeToEdgeEnabled(context)) {
91             return false;
92         }
93         return NAV_BAR_MODE_2BUTTON == context.getResources().getInteger(
94                 com.android.internal.R.integer.config_navBarInteractionMode);
95     }
96 
isEdgeToEdgeEnabled(Context context)97     static boolean isEdgeToEdgeEnabled(Context context) {
98         return NAV_BAR_MODE_GESTURAL == context.getResources().getInteger(
99                 com.android.internal.R.integer.config_navBarInteractionMode);
100     }
101 }
102