|
| 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 com.google.android.material.internal; |
| 18 | + |
| 19 | +import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; |
| 20 | + |
| 21 | +import android.content.Context; |
| 22 | +import android.graphics.Point; |
| 23 | +import android.graphics.Rect; |
| 24 | +import android.os.Build; |
| 25 | +import android.util.Log; |
| 26 | +import android.view.Display; |
| 27 | +import android.view.WindowManager; |
| 28 | +import androidx.annotation.NonNull; |
| 29 | +import androidx.annotation.RequiresApi; |
| 30 | +import androidx.annotation.RestrictTo; |
| 31 | +import java.lang.reflect.InvocationTargetException; |
| 32 | +import java.lang.reflect.Method; |
| 33 | + |
| 34 | +/** |
| 35 | + * A util class for window operations. |
| 36 | + * |
| 37 | + * @hide |
| 38 | + */ |
| 39 | +@RestrictTo(LIBRARY_GROUP) |
| 40 | +public class WindowUtils { |
| 41 | + |
| 42 | + private static final String TAG = WindowUtils.class.getSimpleName(); |
| 43 | + |
| 44 | + private WindowUtils() {} |
| 45 | + |
| 46 | + @NonNull |
| 47 | + public static Rect getCurrentWindowBounds(@NonNull Context context) { |
| 48 | + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 49 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 50 | + return Api30Impl.getCurrentWindowBounds(windowManager); |
| 51 | + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 52 | + return Api17Impl.getCurrentWindowBounds(windowManager); |
| 53 | + } else { |
| 54 | + return Api14Impl.getCurrentWindowBounds(windowManager); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @RequiresApi(api = Build.VERSION_CODES.R) |
| 59 | + private static class Api30Impl { |
| 60 | + |
| 61 | + @NonNull |
| 62 | + static Rect getCurrentWindowBounds(@NonNull WindowManager windowManager) { |
| 63 | + return windowManager.getCurrentWindowMetrics().getBounds(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) |
| 68 | + private static class Api17Impl { |
| 69 | + |
| 70 | + @NonNull |
| 71 | + static Rect getCurrentWindowBounds(@NonNull WindowManager windowManager) { |
| 72 | + Display defaultDisplay = windowManager.getDefaultDisplay(); |
| 73 | + |
| 74 | + Point defaultDisplaySize = new Point(); |
| 75 | + defaultDisplay.getRealSize(defaultDisplaySize); |
| 76 | + |
| 77 | + Rect bounds = new Rect(); |
| 78 | + bounds.right = defaultDisplaySize.x; |
| 79 | + bounds.bottom = defaultDisplaySize.y; |
| 80 | + |
| 81 | + return bounds; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static class Api14Impl { |
| 86 | + |
| 87 | + @NonNull |
| 88 | + static Rect getCurrentWindowBounds(@NonNull WindowManager windowManager) { |
| 89 | + Display defaultDisplay = windowManager.getDefaultDisplay(); |
| 90 | + Point defaultDisplaySize = getRealSizeForDisplay(defaultDisplay); |
| 91 | + |
| 92 | + Rect bounds = new Rect(); |
| 93 | + if (defaultDisplaySize.x == 0 || defaultDisplaySize.y == 0) { |
| 94 | + defaultDisplay.getRectSize(bounds); |
| 95 | + } else { |
| 96 | + bounds.right = defaultDisplaySize.x; |
| 97 | + bounds.bottom = defaultDisplaySize.y; |
| 98 | + } |
| 99 | + |
| 100 | + return bounds; |
| 101 | + } |
| 102 | + |
| 103 | + private static Point getRealSizeForDisplay(Display display) { |
| 104 | + Point size = new Point(); |
| 105 | + try { |
| 106 | + Method getRealSizeMethod = Display.class.getDeclaredMethod("getRealSize", Point.class); |
| 107 | + getRealSizeMethod.setAccessible(true); |
| 108 | + getRealSizeMethod.invoke(display, size); |
| 109 | + } catch (NoSuchMethodException e) { |
| 110 | + Log.w(TAG, e); |
| 111 | + } catch (IllegalAccessException e) { |
| 112 | + Log.w(TAG, e); |
| 113 | + } catch (InvocationTargetException e) { |
| 114 | + Log.w(TAG, e); |
| 115 | + } |
| 116 | + return size; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments