Matthew Ng | 605ea47 | 2018-08-30 21:30:06 -0700 | [diff] [blame] | 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 | |
Tiger Huang | 7c610aa | 2018-10-27 00:01:01 +0800 | [diff] [blame] | 17 | package com.android.server.wm; |
Matthew Ng | 605ea47 | 2018-08-30 21:30:06 -0700 | [diff] [blame] | 18 | |
| 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME; |
| 20 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
| 21 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_BOTTOM; |
| 22 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT; |
| 23 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_RIGHT; |
| 24 | |
| 25 | import android.content.Context; |
| 26 | import android.graphics.Rect; |
| 27 | |
Matthew Ng | 605ea47 | 2018-08-30 21:30:06 -0700 | [diff] [blame] | 28 | /** |
| 29 | * This class acts as a proxy for Navigation Bar experiments enabled with custom overlays |
| 30 | * {@see OverlayManagerService}. By default with no overlays, this class will essentially do nothing |
| 31 | * and pass the original resource data back. By default the navigation bar height/width is the same |
| 32 | * as the frame height/width and therefore any offsets calculated will cancel out and do nothing. |
| 33 | * TODO(b/113952590): Remove class once experiment in bug is completed |
| 34 | */ |
| 35 | public class NavigationBarExperiments { |
| 36 | |
| 37 | private int mNavigationBarHeight; |
| 38 | private int mNavigationBarWidth; |
| 39 | |
| 40 | /** |
| 41 | * This represents the height of the navigation bar buttons. With no experiments or overlays |
| 42 | * enabled, the frame height is the same as the normal navigation bar height. |
| 43 | */ |
| 44 | private int mNavigationBarFrameHeight; |
| 45 | |
| 46 | /** |
| 47 | * This represents the width of the navigation bar buttons. With no experiments or overlays |
| 48 | * enabled, the frame width is the same as the normal navigation bar width. |
| 49 | */ |
| 50 | private int mNavigationBarFrameWidth; |
| 51 | |
| 52 | /** |
| 53 | * Call when configuration change to refresh resource dimensions |
| 54 | * @param systemUiContext to get the resource values |
| 55 | */ |
| 56 | public void onConfigurationChanged(Context systemUiContext) { |
| 57 | // Cache all the values again |
| 58 | mNavigationBarHeight = systemUiContext.getResources().getDimensionPixelSize( |
| 59 | com.android.internal.R.dimen.navigation_bar_height); |
| 60 | mNavigationBarWidth = systemUiContext.getResources().getDimensionPixelSize( |
| 61 | com.android.internal.R.dimen.navigation_bar_width); |
| 62 | mNavigationBarFrameHeight = systemUiContext.getResources().getDimensionPixelSize( |
| 63 | com.android.internal.R.dimen.navigation_bar_frame_height); |
| 64 | mNavigationBarFrameWidth = systemUiContext.getResources().getDimensionPixelSize( |
| 65 | com.android.internal.R.dimen.navigation_bar_frame_width); |
| 66 | } |
| 67 | |
| 68 | public int getNavigationBarHeight() { |
| 69 | return mNavigationBarHeight; |
| 70 | } |
| 71 | |
| 72 | public int getNavigationBarWidth() { |
| 73 | return mNavigationBarWidth; |
| 74 | } |
| 75 | |
| 76 | public int getNavigationBarFrameHeight() { |
| 77 | return mNavigationBarFrameHeight; |
| 78 | } |
| 79 | |
| 80 | public int getNavigationBarFrameWidth() { |
| 81 | return mNavigationBarFrameWidth; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * If navigation frame width/height is different than navigation bar width/height then only |
| 86 | * offset the ime's and home activity's window rects depending on the navigation bar position to |
| 87 | * add a gap where the navigation bar would have been drawn. With no experiments or overlays |
| 88 | * enabled, the height/width is the same as the frame height/width and the offsets calculated |
| 89 | * will be 0 and this function will do nothing. |
| 90 | * @param navPosition position of navigation bar (left, right or bottom) |
| 91 | * @param w the window that is being offset by experiment |
| 92 | */ |
| 93 | public void offsetWindowFramesForNavBar(int navPosition, WindowState w) { |
| 94 | if (w.getAttrs().type != TYPE_INPUT_METHOD && w.getActivityType() != ACTIVITY_TYPE_HOME) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | final WindowFrames windowFrames = w.getWindowFrames(); |
| 99 | final Rect cf = windowFrames.mContentFrame; |
| 100 | switch (navPosition) { |
| 101 | case NAV_BAR_BOTTOM: |
| 102 | int navHeight = getNavigationBarFrameHeight() - getNavigationBarHeight(); |
| 103 | if (navHeight > 0) { |
| 104 | cf.bottom -= navHeight; |
| 105 | } |
| 106 | break; |
| 107 | case NAV_BAR_LEFT: |
| 108 | case NAV_BAR_RIGHT: |
| 109 | int navWidth = getNavigationBarFrameWidth() - getNavigationBarWidth(); |
| 110 | if (navWidth > 0) { |
| 111 | if (navPosition == NAV_BAR_LEFT) { |
| 112 | cf.left += navWidth; |
| 113 | } else { |
| 114 | cf.right -= navWidth; |
| 115 | } |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |