Filip Gruszczynski | 466f321 | 2015-09-21 17:57:57 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 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.server.wm; |
| 18 | |
| 19 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 20 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 21 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; |
| 22 | import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; |
| 23 | import static android.view.WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING; |
| 24 | import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; |
| 25 | import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER; |
| 26 | |
| 27 | import android.content.Context; |
| 28 | import android.graphics.PixelFormat; |
| 29 | import android.graphics.Rect; |
| 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
| 32 | import android.view.WindowManager; |
| 33 | import android.view.WindowManagerGlobal; |
| 34 | |
| 35 | /** |
| 36 | * Controls showing and hiding of a docked stack divider on the display. |
| 37 | */ |
| 38 | public class DockedStackDividerController { |
| 39 | private static final String TAG = "DockedStackDivider"; |
| 40 | private final Context mContext; |
| 41 | private final int mDividerWidth; |
| 42 | private final DisplayContent mDisplayContent; |
| 43 | private View mView; |
| 44 | private Rect mTmpRect = new Rect(); |
| 45 | |
| 46 | DockedStackDividerController(Context context, DisplayContent displayContent) { |
| 47 | mContext = context; |
| 48 | mDisplayContent = displayContent; |
| 49 | mDividerWidth = context.getResources().getDimensionPixelSize( |
| 50 | com.android.internal.R.dimen.docked_stack_divider_thickness); |
| 51 | } |
| 52 | |
| 53 | private void addDivider() { |
| 54 | View view = LayoutInflater.from(mContext).inflate( |
| 55 | com.android.internal.R.layout.docked_stack_divider, null); |
| 56 | WindowManagerGlobal manager = WindowManagerGlobal.getInstance(); |
| 57 | WindowManager.LayoutParams params = new WindowManager.LayoutParams( |
| 58 | mDividerWidth, MATCH_PARENT, TYPE_DOCK_DIVIDER, |
| 59 | FLAG_TOUCHABLE_WHEN_WAKING | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL |
| 60 | | FLAG_WATCH_OUTSIDE_TOUCH | FLAG_SPLIT_TOUCH, |
| 61 | PixelFormat.OPAQUE); |
| 62 | params.setTitle(TAG); |
| 63 | manager.addView(view, params, mDisplayContent.getDisplay(), null); |
| 64 | mView = view; |
| 65 | } |
| 66 | |
| 67 | private void removeDivider() { |
| 68 | WindowManagerGlobal manager = WindowManagerGlobal.getInstance(); |
| 69 | manager.removeView(mView, true /* immediate */); |
| 70 | mView = null; |
| 71 | } |
| 72 | |
| 73 | boolean hasDivider() { |
| 74 | return mView != null; |
| 75 | } |
| 76 | |
| 77 | void update() { |
| 78 | TaskStack stack = mDisplayContent.getDockedStack(); |
| 79 | if (stack != null && mView == null) { |
| 80 | addDivider(); |
| 81 | } else if (stack == null && mView != null) { |
| 82 | removeDivider(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | int getWidth() { |
| 87 | return mDividerWidth; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void positionDockedStackedDivider(Rect frame) { |
| 92 | TaskStack stack = mDisplayContent.getDockedStack(); |
| 93 | if (stack == null) { |
| 94 | // Unfortunately we might end up with still having a divider, even though the underlying |
| 95 | // stack was already removed. This is because we are on AM thread and the removal of the |
| 96 | // divider was deferred to WM thread and hasn't happened yet. |
| 97 | return; |
| 98 | } |
| 99 | final @TaskStack.DockSide int side = stack.getDockSide(); |
| 100 | stack.getBounds(mTmpRect); |
| 101 | switch (side) { |
| 102 | case TaskStack.DOCKED_LEFT: |
| 103 | frame.set(mTmpRect.right, frame.top, mTmpRect.right + frame.width(), frame.bottom); |
| 104 | break; |
| 105 | case TaskStack.DOCKED_TOP: |
| 106 | frame.set(frame.left, mTmpRect.bottom, mTmpRect.right, |
| 107 | mTmpRect.bottom + frame.height()); |
| 108 | break; |
| 109 | case TaskStack.DOCKED_RIGHT: |
| 110 | frame.set(mTmpRect.left - frame.width(), frame.top, mTmpRect.left, frame.bottom); |
| 111 | break; |
| 112 | case TaskStack.DOCKED_BOTTOM: |
| 113 | frame.set(frame.left, mTmpRect.top - frame.height(), frame.right, mTmpRect.top); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |