• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.os.UserManager;
24 import android.provider.Settings;
25 import android.support.annotation.VisibleForTesting;
26 import android.text.TextUtils;
27 
28 import com.android.internal.R;
29 
30 public class SwipeUpPreferenceController extends GesturePreferenceController {
31 
32     private final int ON = 1;
33     private final int OFF = 0;
34 
35     private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE";
36     private static final String PREF_KEY_VIDEO = "gesture_swipe_up_video";
37     private final UserManager mUserManager;
38 
SwipeUpPreferenceController(Context context, String key)39     public SwipeUpPreferenceController(Context context, String key) {
40         super(context, key);
41         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
42     }
43 
isGestureAvailable(Context context)44     static boolean isGestureAvailable(Context context) {
45         if (!context.getResources().getBoolean(R.bool.config_swipe_up_gesture_setting_available)) {
46             return false;
47         }
48 
49         final ComponentName recentsComponentName = ComponentName.unflattenFromString(
50                 context.getString(R.string.config_recentsComponentName));
51         final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP)
52                 .setPackage(recentsComponentName.getPackageName());
53         if (context.getPackageManager().resolveService(quickStepIntent,
54                 PackageManager.MATCH_SYSTEM_ONLY) == null) {
55             return false;
56         }
57         return true;
58     }
59 
60     @Override
getAvailabilityStatus()61     public int getAvailabilityStatus() {
62         return isGestureAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
63     }
64 
65     @Override
isSliceable()66     public boolean isSliceable() {
67         return TextUtils.equals(getPreferenceKey(), "gesture_swipe_up");
68     }
69 
70     @Override
getVideoPrefKey()71     protected String getVideoPrefKey() {
72         return PREF_KEY_VIDEO;
73     }
74 
75     @Override
setChecked(boolean isChecked)76     public boolean setChecked(boolean isChecked) {
77         setSwipeUpPreference(mContext, mUserManager, isChecked ? ON : OFF);
78         return true;
79     }
80 
setSwipeUpPreference(Context context, UserManager userManager, int enabled)81     public static void setSwipeUpPreference(Context context, UserManager userManager,
82             int enabled) {
83         Settings.Secure.putInt(context.getContentResolver(),
84                 Settings.Secure.SWIPE_UP_TO_SWITCH_APPS_ENABLED, enabled);
85     }
86 
87     @Override
isChecked()88     public boolean isChecked() {
89         final int defaultValue = mContext.getResources()
90                 .getBoolean(R.bool.config_swipe_up_gesture_default) ? ON : OFF;
91         final int swipeUpEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
92                 Settings.Secure.SWIPE_UP_TO_SWITCH_APPS_ENABLED, defaultValue);
93         return swipeUpEnabled != OFF;
94     }
95 }
96