1 /*
2  * Copyright (C) 2023 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 package com.android.launcher3.taskbar;
17 
18 import static com.android.launcher3.Utilities.dpToPx;
19 import static com.android.launcher3.Utilities.dpiFromPx;
20 
21 import android.content.res.Resources;
22 import android.util.DisplayMetrics;
23 
24 import androidx.core.content.res.ResourcesCompat;
25 
26 import com.android.launcher3.DeviceProfile;
27 import com.android.launcher3.R;
28 import com.android.launcher3.config.FeatureFlags;
29 
30 /**
31  * Utility class that contains the different taskbar thresholds logic.
32  */
33 public class TaskbarThresholdUtils {
34 
35     // We divide the screen into this many parts, and use the result to scale the thresholds to
36     // any size device. Note that this value was calculated arbitrarily by using two tablet devices
37     // as data points.
38     private static final float SCREEN_UNITS = 1 / 80f;
39 
getThreshold(Resources r, DeviceProfile dp, int thresholdDimen, int multiplierDimen)40     private static int getThreshold(Resources r, DeviceProfile dp, int thresholdDimen,
41             int multiplierDimen) {
42         if (!FeatureFlags.ENABLE_DYNAMIC_TASKBAR_THRESHOLDS.get()) {
43             return r.getDimensionPixelSize(thresholdDimen);
44         }
45 
46         float landscapeScreenHeight = dp.isLandscape ? dp.heightPx : dp.widthPx;
47         float screenPart = (landscapeScreenHeight * SCREEN_UNITS);
48         float defaultDp = dpiFromPx(screenPart, DisplayMetrics.DENSITY_DEVICE_STABLE);
49         float thisDp = dpToPx(defaultDp);
50         float multiplier = ResourcesCompat.getFloat(r, multiplierDimen);
51         float value = (thisDp) * multiplier;
52 
53         return Math.round(value);
54     }
55 
56     /**
57      * Returns the threshold that determines if we should show taskbar.
58      */
getFromNavThreshold(Resources r, DeviceProfile dp)59     public static int getFromNavThreshold(Resources r, DeviceProfile dp) {
60         return getThreshold(r, dp, R.dimen.taskbar_from_nav_threshold,
61                 R.dimen.taskbar_nav_threshold_mult);
62     }
63 
64     /**
65      * Returns the threshold that we start moving the app window.
66      */
getAppWindowThreshold(Resources r, DeviceProfile dp)67     public static int getAppWindowThreshold(Resources r, DeviceProfile dp) {
68         return getThreshold(r, dp, R.dimen.taskbar_app_window_threshold,
69                 R.dimen.taskbar_app_window_threshold_mult);
70     }
71 
72     /**
73      * Returns the threshold for whether we land in home or overview.
74      */
getHomeOverviewThreshold(Resources r, DeviceProfile dp)75     public static int getHomeOverviewThreshold(Resources r, DeviceProfile dp) {
76         return getThreshold(r, dp, R.dimen.taskbar_home_overview_threshold,
77                 R.dimen.taskbar_home_overview_threshold_mult);
78     }
79 
80     /**
81      * Returns the threshold that we use to allow swipe to catch up to finger.
82      */
getCatchUpThreshold(Resources r, DeviceProfile dp)83     public static int getCatchUpThreshold(Resources r, DeviceProfile dp) {
84         return getThreshold(r, dp, R.dimen.taskbar_catch_up_threshold,
85                 R.dimen.taskbar_catch_up_threshold_mult);
86     }
87 }
88