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 (isGestureNavigationEnabled(mContext)) {
47             return mContext.getText(R.string.edge_to_edge_navigation_title);
48         } else if (is2ButtonNavigationEnabled(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 
55     /** Returns {@code true} if gesture is available. */
isGestureAvailable(Context context)56     public static boolean isGestureAvailable(Context context) {
57         // Skip if the swipe up settings are not available
58         if (!context.getResources().getBoolean(
59                 com.android.internal.R.bool.config_swipe_up_gesture_setting_available)) {
60             return false;
61         }
62 
63         // Skip if the recents component is not defined
64         final ComponentName recentsComponentName = ComponentName.unflattenFromString(
65                 context.getString(com.android.internal.R.string.config_recentsComponentName));
66         if (recentsComponentName == null) {
67             return false;
68         }
69 
70         // Skip if the overview proxy service exists
71         final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP)
72                 .setPackage(recentsComponentName.getPackageName());
73         if (context.getPackageManager().resolveService(quickStepIntent,
74                 PackageManager.MATCH_SYSTEM_ONLY) == null) {
75             return false;
76         }
77 
78         return true;
79     }
80 
isOverlayPackageAvailable(Context context, String overlayPackage)81     static boolean isOverlayPackageAvailable(Context context, String overlayPackage) {
82         try {
83             return context.getPackageManager().getPackageInfo(overlayPackage, 0) != null;
84         } catch (PackageManager.NameNotFoundException e) {
85             // Not found, just return unavailable
86             return false;
87         }
88     }
89 
is2ButtonNavigationEnabled(Context context)90     static boolean is2ButtonNavigationEnabled(Context context) {
91         return NAV_BAR_MODE_2BUTTON == context.getResources().getInteger(
92                 com.android.internal.R.integer.config_navBarInteractionMode);
93     }
94 
isGestureNavigationEnabled(Context context)95     static boolean isGestureNavigationEnabled(Context context) {
96         return NAV_BAR_MODE_GESTURAL == context.getResources().getInteger(
97                 com.android.internal.R.integer.config_navBarInteractionMode);
98     }
99 }
100