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 
17 package android.provider.settings.backup;
18 
19 import android.content.Context;
20 import android.graphics.Rect;
21 import android.util.DisplayMetrics;
22 import android.view.WindowManager;
23 
24 /** Settings that should not be restored when target device is a large screen
25  *  i.e. tablets and foldables in unfolded state
26  */
27 public class LargeScreenSettings {
28     private static final float LARGE_SCREEN_MIN_DPS = 600;
29     private static final String LARGE_SCREEN_DO_NOT_RESTORE = "accelerometer_rotation";
30 
31    /**
32     * Autorotation setting should not be restored when the target device is a large screen.
33     * (b/243489549)
34     */
doNotRestoreIfLargeScreenSetting(String key, Context context)35     public static boolean doNotRestoreIfLargeScreenSetting(String key, Context context) {
36         return isLargeScreen(context) && LARGE_SCREEN_DO_NOT_RESTORE.equals(key);
37     }
38 
39     // copied from systemui/shared/...Utilities.java
40     // since we don't want to add compile time dependency on sys ui package
isLargeScreen(Context context)41     private static boolean isLargeScreen(Context context) {
42         final WindowManager windowManager = context.getSystemService(WindowManager.class);
43         final Rect bounds = windowManager.getCurrentWindowMetrics().getBounds();
44         float smallestWidth = dpiFromPx(Math.min(bounds.width(), bounds.height()),
45                 context.getResources().getConfiguration().densityDpi);
46         return smallestWidth >= LARGE_SCREEN_MIN_DPS;
47     }
48 
dpiFromPx(float size, int densityDpi)49     private static float dpiFromPx(float size, int densityDpi) {
50         float densityRatio = (float) densityDpi / DisplayMetrics.DENSITY_DEFAULT;
51         return (size / densityRatio);
52     }
53 }
54