blob: 6e11671016b42f488d971d40c3c8231fc16ae510 [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;
26import com.android.internal.R;
27
28import java.util.List;
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030/**
Jeff Brown98365d72012-08-19 20:30:52 -070031 * 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 Project9066cfe2009-03-03 19:31:44 -080041 *
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 Brown98365d72012-08-19 20:30:52 -070048 * provides a window manager for adding windows that are associated with that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * activity -- the window manager will not normally allow you to add arbitrary
50 * windows that are not associated with an activity.
Jeff Brown98365d72012-08-19 20:30:52 -070051 *
52 * @see WindowManager
53 * @see WindowManagerGlobal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * @hide
55 */
Jeff Brown98365d72012-08-19 20:30:52 -070056public final class WindowManagerImpl implements WindowManager {
57 private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
Jeff Brown98365d72012-08-19 20:30:52 -070058 private final Display mDisplay;
Jeff Brownd32460c2012-07-20 16:15:36 -070059 private final Window mParentWindow;
Dianne Hackborna53de062012-05-08 18:53:51 -070060
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080061 private IBinder mDefaultToken;
62
Jeff Browna492c3a2012-08-23 19:48:44 -070063 public WindowManagerImpl(Display display) {
64 this(display, null);
Jeff Brown98365d72012-08-19 20:30:52 -070065 }
66
Jeff Browna492c3a2012-08-23 19:48:44 -070067 private WindowManagerImpl(Display display, Window parentWindow) {
Jeff Brown98365d72012-08-19 20:30:52 -070068 mDisplay = display;
Jeff Brownd32460c2012-07-20 16:15:36 -070069 mParentWindow = parentWindow;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070070 }
71
Jeff Brown98365d72012-08-19 20:30:52 -070072 public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
Jeff Browna492c3a2012-08-23 19:48:44 -070073 return new WindowManagerImpl(mDisplay, parentWindow);
74 }
75
76 public WindowManagerImpl createPresentationWindowManager(Display display) {
77 return new WindowManagerImpl(display, mParentWindow);
Jeff Brownd32460c2012-07-20 16:15:36 -070078 }
79
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080080 /**
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 Brownd32460c2012-07-20 16:15:36 -070090 @Override
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080091 public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
92 applyDefaultToken(params);
Jeff Brown98365d72012-08-19 20:30:52 -070093 mGlobal.addView(view, params, mDisplay, mParentWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
95
Jeff Brownd32460c2012-07-20 16:15:36 -070096 @Override
Alan Viverette7c9746d4e2014-11-19 17:02:16 -080097 public void updateViewLayout(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
98 applyDefaultToken(params);
Jeff Brown98365d72012-08-19 20:30:52 -070099 mGlobal.updateViewLayout(view, params);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 }
101
Alan Viverette7c9746d4e2014-11-19 17:02:16 -0800102 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 Brownd32460c2012-07-20 16:15:36 -0700117 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public void removeView(View view) {
Jeff Brown98365d72012-08-19 20:30:52 -0700119 mGlobal.removeView(view, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 }
121
Jeff Brownd32460c2012-07-20 16:15:36 -0700122 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public void removeViewImmediate(View view) {
Jeff Brown98365d72012-08-19 20:30:52 -0700124 mGlobal.removeView(view, true);
Jeff Brown23e7c352012-07-20 12:45:47 -0700125 }
126
Jeff Brownd32460c2012-07-20 16:15:36 -0700127 @Override
Clara Bayarri75e09792015-07-29 16:20:40 +0100128 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 Brownd32460c2012-07-20 16:15:36 -0700145 public Display getDefaultDisplay() {
Jeff Brown98365d72012-08-19 20:30:52 -0700146 return mDisplay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148}