The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.view; |
| 18 | |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Mathew Inwood | e5ad598 | 2018-08-17 15:07:52 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 21 | import android.content.Context; |
Albert Chaulk | 2ccb0b7 | 2017-06-20 14:39:29 -0400 | [diff] [blame] | 22 | import android.graphics.Region; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 23 | import android.os.Bundle; |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 24 | import android.os.IBinder; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | |
| 27 | import com.android.internal.os.IResultReceiver; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 28 | |
| 29 | import java.util.List; |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /** |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 32 | * Provides low-level communication with the system window manager for |
| 33 | * operations that are bound to a particular context, display or parent window. |
| 34 | * Instances of this object are sensitive to the compatibility info associated |
| 35 | * with the running application. |
| 36 | * |
| 37 | * This object implements the {@link ViewManager} interface, |
| 38 | * allowing you to add any View subclass as a top-level window on the screen. |
| 39 | * Additional window manager specific layout parameters are defined for |
| 40 | * control over how windows are displayed. It also implements the {@link WindowManager} |
| 41 | * interface, allowing you to control the displays attached to the device. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 42 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | * <p>Applications will not normally use WindowManager directly, instead relying |
| 44 | * on the higher-level facilities in {@link android.app.Activity} and |
| 45 | * {@link android.app.Dialog}. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 46 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | * <p>Even for low-level window manager access, it is almost never correct to use |
| 48 | * this class. For example, {@link android.app.Activity#getWindowManager} |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 49 | * provides a window manager for adding windows that are associated with that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * activity -- the window manager will not normally allow you to add arbitrary |
| 51 | * windows that are not associated with an activity. |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 52 | * |
| 53 | * @see WindowManager |
| 54 | * @see WindowManagerGlobal |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | * @hide |
| 56 | */ |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 57 | public final class WindowManagerImpl implements WindowManager { |
Mathew Inwood | e5ad598 | 2018-08-17 15:07:52 +0100 | [diff] [blame] | 58 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 59 | private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance(); |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 60 | private final Context mContext; |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 61 | private final Window mParentWindow; |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 62 | |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 63 | private IBinder mDefaultToken; |
| 64 | |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 65 | public WindowManagerImpl(Context context) { |
| 66 | this(context, null); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 69 | private WindowManagerImpl(Context context, Window parentWindow) { |
| 70 | mContext = context; |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 71 | mParentWindow = parentWindow; |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 74 | public WindowManagerImpl createLocalWindowManager(Window parentWindow) { |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 75 | return new WindowManagerImpl(mContext, parentWindow); |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 78 | public WindowManagerImpl createPresentationWindowManager(Context displayContext) { |
| 79 | return new WindowManagerImpl(displayContext, mParentWindow); |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Sets the window token to assign when none is specified by the client or |
| 84 | * available from the parent window. |
| 85 | * |
| 86 | * @param token The default token to assign. |
| 87 | */ |
| 88 | public void setDefaultToken(IBinder token) { |
| 89 | mDefaultToken = token; |
| 90 | } |
| 91 | |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 92 | @Override |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 93 | public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) { |
| 94 | applyDefaultToken(params); |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 95 | mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 98 | @Override |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 99 | public void updateViewLayout(@NonNull View view, @NonNull ViewGroup.LayoutParams params) { |
| 100 | applyDefaultToken(params); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 101 | mGlobal.updateViewLayout(view, params); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Alan Viverette | 7c9746d4e | 2014-11-19 17:02:16 -0800 | [diff] [blame] | 104 | private void applyDefaultToken(@NonNull ViewGroup.LayoutParams params) { |
| 105 | // Only use the default token if we don't have a parent window. |
| 106 | if (mDefaultToken != null && mParentWindow == null) { |
| 107 | if (!(params instanceof WindowManager.LayoutParams)) { |
| 108 | throw new IllegalArgumentException("Params must be WindowManager.LayoutParams"); |
| 109 | } |
| 110 | |
| 111 | // Only use the default token if we don't already have a token. |
| 112 | final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params; |
| 113 | if (wparams.token == null) { |
| 114 | wparams.token = mDefaultToken; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 119 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | public void removeView(View view) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 121 | mGlobal.removeView(view, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 124 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | public void removeViewImmediate(View view) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 126 | mGlobal.removeView(view, true); |
Jeff Brown | 23e7c35 | 2012-07-20 12:45:47 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 129 | @Override |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 130 | public void requestAppKeyboardShortcuts( |
| 131 | final KeyboardShortcutsReceiver receiver, int deviceId) { |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 132 | IResultReceiver resultReceiver = new IResultReceiver.Stub() { |
| 133 | @Override |
| 134 | public void send(int resultCode, Bundle resultData) throws RemoteException { |
| 135 | List<KeyboardShortcutGroup> result = |
| 136 | resultData.getParcelableArrayList(PARCEL_KEY_SHORTCUTS_ARRAY); |
| 137 | receiver.onKeyboardShortcutsReceived(result); |
| 138 | } |
| 139 | }; |
| 140 | try { |
| 141 | WindowManagerGlobal.getWindowManagerService() |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 142 | .requestAppKeyboardShortcuts(resultReceiver, deviceId); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 143 | } catch (RemoteException e) { |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 148 | public Display getDefaultDisplay() { |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 149 | return mContext.getDisplay(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
Albert Chaulk | 2ccb0b7 | 2017-06-20 14:39:29 -0400 | [diff] [blame] | 151 | |
| 152 | @Override |
| 153 | public Region getCurrentImeTouchRegion() { |
| 154 | try { |
| 155 | return WindowManagerGlobal.getWindowManagerService().getCurrentImeTouchRegion(); |
| 156 | } catch (RemoteException e) { |
| 157 | } |
| 158 | return null; |
| 159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |