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