blob: c1b8f04a6865c605fbafc48c84e92ff93a92bc14 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.view;
18
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080019import android.annotation.NonNull;
Clara Bayarri75e09792015-07-29 16:20:40 +010020import android.content.Context;
21import android.os.Bundle;
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080022import android.os.IBinder;
Clara Bayarri75e09792015-07-29 16:20:40 +010023import android.os.RemoteException;
24
25import com.android.internal.os.IResultReceiver;
Clara Bayarri75e09792015-07-29 16:20:40 +010026
27import java.util.List;
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029/**
Jeff Brown98365d72012-08-19 20:30:52 -070030 * 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 Liutikas67e2ae82016-10-11 18:17:42 -070040 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 * <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 Liutikas67e2ae82016-10-11 18:17:42 -070044 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 * <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 Brown98365d72012-08-19 20:30:52 -070047 * provides a window manager for adding windows that are associated with that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 * activity -- the window manager will not normally allow you to add arbitrary
49 * windows that are not associated with an activity.
Jeff Brown98365d72012-08-19 20:30:52 -070050 *
51 * @see WindowManager
52 * @see WindowManagerGlobal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 * @hide
54 */
Jeff Brown98365d72012-08-19 20:30:52 -070055public final class WindowManagerImpl implements WindowManager {
56 private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
Adam Lesinski4ece3d62016-06-16 18:05:41 -070057 private final Context mContext;
Jeff Brownd32460c2012-07-20 16:15:36 -070058 private final Window mParentWindow;
Dianne Hackborna53de062012-05-08 18:53:51 -070059
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080060 private IBinder mDefaultToken;
61
Adam Lesinski4ece3d62016-06-16 18:05:41 -070062 public WindowManagerImpl(Context context) {
63 this(context, null);
Jeff Brown98365d72012-08-19 20:30:52 -070064 }
65
Adam Lesinski4ece3d62016-06-16 18:05:41 -070066 private WindowManagerImpl(Context context, Window parentWindow) {
67 mContext = context;
Jeff Brownd32460c2012-07-20 16:15:36 -070068 mParentWindow = parentWindow;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070069 }
70
Jeff Brown98365d72012-08-19 20:30:52 -070071 public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -070072 return new WindowManagerImpl(mContext, parentWindow);
Jeff Browna492c3a2012-08-23 19:48:44 -070073 }
74
Adam Lesinski4ece3d62016-06-16 18:05:41 -070075 public WindowManagerImpl createPresentationWindowManager(Context displayContext) {
76 return new WindowManagerImpl(displayContext, mParentWindow);
Jeff Brownd32460c2012-07-20 16:15:36 -070077 }
78
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080079 /**
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 Brownd32460c2012-07-20 16:15:36 -070089 @Override
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080090 public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
91 applyDefaultToken(params);
Adam Lesinski4ece3d62016-06-16 18:05:41 -070092 mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
94
Jeff Brownd32460c2012-07-20 16:15:36 -070095 @Override
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080096 public void updateViewLayout(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
97 applyDefaultToken(params);
Jeff Brown98365d72012-08-19 20:30:52 -070098 mGlobal.updateViewLayout(view, params);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100
Alan Viverette7c9746d4e2014-11-19 17:02:16 -0800101 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 Brownd32460c2012-07-20 16:15:36 -0700116 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public void removeView(View view) {
Jeff Brown98365d72012-08-19 20:30:52 -0700118 mGlobal.removeView(view, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 }
120
Jeff Brownd32460c2012-07-20 16:15:36 -0700121 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public void removeViewImmediate(View view) {
Jeff Brown98365d72012-08-19 20:30:52 -0700123 mGlobal.removeView(view, true);
Jeff Brown23e7c352012-07-20 12:45:47 -0700124 }
125
Jeff Brownd32460c2012-07-20 16:15:36 -0700126 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +0000127 public void requestAppKeyboardShortcuts(
128 final KeyboardShortcutsReceiver receiver, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +0100129 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 Bayarrifcd7e802016-03-10 12:58:18 +0000139 .requestAppKeyboardShortcuts(resultReceiver, deviceId);
Clara Bayarri75e09792015-07-29 16:20:40 +0100140 } catch (RemoteException e) {
141 }
142 }
143
144 @Override
Jeff Brownd32460c2012-07-20 16:15:36 -0700145 public Display getDefaultDisplay() {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700146 return mContext.getDisplay();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148}