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