blob: 7a8c2f91b4e9bc0576e52b37ae08ebc654421a5e [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
2 * Copyright (C) 2011 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 com.android.server.wm;
18
Wale Ogunwale01ad4342017-06-30 07:07:01 -070019import static android.Manifest.permission.HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
Wale Ogunwale943002b2017-02-15 19:34:01 -080020import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
21import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwale4958ad22017-06-22 09:08:14 -070022import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Wale Ogunwaled993a572017-02-05 13:52:09 -080023import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
Wale Ogunwale943002b2017-02-15 19:34:01 -080024import static android.view.WindowManager.LayoutParams.isSystemAlertWindowType;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
27import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
28import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080030
31import android.content.ClipData;
32import android.content.Context;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080033import android.graphics.Rect;
34import android.graphics.Region;
35import android.os.Binder;
36import android.os.Bundle;
37import android.os.IBinder;
38import android.os.Parcel;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070039import android.os.Process;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080040import android.os.RemoteException;
41import android.os.ServiceManager;
Wale Ogunwale4958ad22017-06-22 09:08:14 -070042import android.os.Trace;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070043import android.os.UserHandle;
Andrii Kulian44607962017-03-16 11:06:24 -070044import android.util.MergedConfiguration;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080045import android.util.Slog;
Craig Mautner6881a102012-07-27 13:04:51 -070046import android.view.Display;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080047import android.view.IWindow;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080048import android.view.IWindowId;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080049import android.view.IWindowSession;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080050import android.view.IWindowSessionCallback;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080051import android.view.InputChannel;
52import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080053import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080054import android.view.SurfaceSession;
55import android.view.WindowManager;
56
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080057import com.android.internal.view.IInputContext;
58import com.android.internal.view.IInputMethodClient;
59import com.android.internal.view.IInputMethodManager;
60import com.android.server.wm.WindowManagerService.H;
61
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080062import java.io.PrintWriter;
Wale Ogunwale943002b2017-02-15 19:34:01 -080063import java.util.HashSet;
64import java.util.Set;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080065
66/**
67 * This class represents an active client session. There is generally one
68 * Session object per process that is interacting with the window manager.
69 */
Wale Ogunwalecfca2582016-10-19 09:53:25 -070070// Needs to be public and not final so we can mock during tests...sucks I know :(
71public class Session extends IWindowSession.Stub
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080072 implements IBinder.DeathRecipient {
73 final WindowManagerService mService;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070074 final IWindowSessionCallback mCallback;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080075 final IInputMethodClient mClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080076 final int mUid;
77 final int mPid;
Wale Ogunwalecfca2582016-10-19 09:53:25 -070078 private final String mStringName;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080079 SurfaceSession mSurfaceSession;
Wale Ogunwalecfca2582016-10-19 09:53:25 -070080 private int mNumWindow = 0;
Wale Ogunwale943002b2017-02-15 19:34:01 -080081 // Set of visible application overlay window surfaces connected to this session.
82 private final Set<WindowSurfaceController> mAppOverlaySurfaces = new HashSet<>();
83 // Set of visible alert window surfaces connected to this session.
84 private final Set<WindowSurfaceController> mAlertWindowSurfaces = new HashSet<>();
85 final boolean mCanAddInternalSystemWindow;
Wale Ogunwale01ad4342017-06-30 07:07:01 -070086 final boolean mCanHideNonSystemOverlayWindows;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080087 private AlertWindowNotification mAlertWindowNotification;
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070088 private boolean mShowingAlertWindowNotificationAllowed;
Wale Ogunwalecfca2582016-10-19 09:53:25 -070089 private boolean mClientDead = false;
90 private float mLastReportedAnimatorScale;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080091 private String mPackageName;
Wale Ogunwale4958ad22017-06-22 09:08:14 -070092 private String mRelayoutTag;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080093
Dianne Hackborneb94fa72014-06-03 17:48:12 -070094 public Session(WindowManagerService service, IWindowSessionCallback callback,
95 IInputMethodClient client, IInputContext inputContext) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080096 mService = service;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070097 mCallback = callback;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080098 mClient = client;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080099 mUid = Binder.getCallingUid();
100 mPid = Binder.getCallingPid();
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700101 mLastReportedAnimatorScale = service.getCurrentAnimatorScale();
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800102 mCanAddInternalSystemWindow = service.mContext.checkCallingOrSelfPermission(
Wale Ogunwale943002b2017-02-15 19:34:01 -0800103 INTERNAL_SYSTEM_WINDOW) == PERMISSION_GRANTED;
Wale Ogunwale01ad4342017-06-30 07:07:01 -0700104 mCanHideNonSystemOverlayWindows = service.mContext.checkCallingOrSelfPermission(
105 HIDE_NON_SYSTEM_OVERLAY_WINDOWS) == PERMISSION_GRANTED;
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -0700106 mShowingAlertWindowNotificationAllowed = mService.mShowAlertWindowNotifications;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800107 StringBuilder sb = new StringBuilder();
108 sb.append("Session{");
109 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700110 sb.append(" ");
111 sb.append(mPid);
112 if (mUid < Process.FIRST_APPLICATION_UID) {
113 sb.append(":");
114 sb.append(mUid);
115 } else {
116 sb.append(":u");
117 sb.append(UserHandle.getUserId(mUid));
118 sb.append('a');
119 sb.append(UserHandle.getAppId(mUid));
120 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800121 sb.append("}");
122 mStringName = sb.toString();
123
124 synchronized (mService.mWindowMap) {
125 if (mService.mInputMethodManager == null && mService.mHaveInputMethods) {
126 IBinder b = ServiceManager.getService(
127 Context.INPUT_METHOD_SERVICE);
128 mService.mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
129 }
130 }
131 long ident = Binder.clearCallingIdentity();
132 try {
133 // Note: it is safe to call in to the input method manager
134 // here because we are not holding our lock.
135 if (mService.mInputMethodManager != null) {
136 mService.mInputMethodManager.addClient(client, inputContext,
137 mUid, mPid);
138 } else {
139 client.setUsingInputMethod(false);
140 }
141 client.asBinder().linkToDeath(this, 0);
142 } catch (RemoteException e) {
143 // The caller has died, so we can just forget about this.
144 try {
145 if (mService.mInputMethodManager != null) {
146 mService.mInputMethodManager.removeClient(client);
147 }
148 } catch (RemoteException ee) {
149 }
150 } finally {
151 Binder.restoreCallingIdentity(ident);
152 }
153 }
154
155 @Override
156 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
157 throws RemoteException {
158 try {
159 return super.onTransact(code, data, reply, flags);
160 } catch (RuntimeException e) {
161 // Log all 'real' exceptions thrown to the caller
162 if (!(e instanceof SecurityException)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800163 Slog.wtf(TAG_WM, "Window Session Crash", e);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800164 }
165 throw e;
166 }
167 }
168
Andrew Scull26f830d2017-05-19 12:16:10 +0100169 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800170 public void binderDied() {
171 // Note: it is safe to call in to the input method manager
172 // here because we are not holding our lock.
173 try {
174 if (mService.mInputMethodManager != null) {
175 mService.mInputMethodManager.removeClient(mClient);
176 }
177 } catch (RemoteException e) {
178 }
179 synchronized(mService.mWindowMap) {
180 mClient.asBinder().unlinkToDeath(this, 0);
181 mClientDead = true;
182 killSessionLocked();
183 }
184 }
185
Craig Mautner6881a102012-07-27 13:04:51 -0700186 @Override
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700187 public int add(IWindow window, int seq, WindowManager.LayoutParams attrs,
Adrian Roos37d7a682014-11-06 18:15:16 +0100188 int viewVisibility, Rect outContentInsets, Rect outStableInsets,
189 InputChannel outInputChannel) {
Craig Mautner6881a102012-07-27 13:04:51 -0700190 return addToDisplay(window, seq, attrs, viewVisibility, Display.DEFAULT_DISPLAY,
Filip Gruszczynski0ec13282015-06-25 11:26:01 -0700191 outContentInsets, outStableInsets, null /* outOutsets */, outInputChannel);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800192 }
Craig Mautner6881a102012-07-27 13:04:51 -0700193
194 @Override
195 public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
Adrian Roos37d7a682014-11-06 18:15:16 +0100196 int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets,
Filip Gruszczynski0ec13282015-06-25 11:26:01 -0700197 Rect outOutsets, InputChannel outInputChannel) {
Craig Mautner6881a102012-07-27 13:04:51 -0700198 return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
Filip Gruszczynski0ec13282015-06-25 11:26:01 -0700199 outContentInsets, outStableInsets, outOutsets, outInputChannel);
Craig Mautner6881a102012-07-27 13:04:51 -0700200 }
201
202 @Override
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700203 public int addWithoutInputChannel(IWindow window, int seq, WindowManager.LayoutParams attrs,
Adrian Roos37d7a682014-11-06 18:15:16 +0100204 int viewVisibility, Rect outContentInsets, Rect outStableInsets) {
Craig Mautner6881a102012-07-27 13:04:51 -0700205 return addToDisplayWithoutInputChannel(window, seq, attrs, viewVisibility,
Adrian Roos37d7a682014-11-06 18:15:16 +0100206 Display.DEFAULT_DISPLAY, outContentInsets, outStableInsets);
Craig Mautner6881a102012-07-27 13:04:51 -0700207 }
208
209 @Override
210 public int addToDisplayWithoutInputChannel(IWindow window, int seq, WindowManager.LayoutParams attrs,
Adrian Roos37d7a682014-11-06 18:15:16 +0100211 int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets) {
Craig Mautner6881a102012-07-27 13:04:51 -0700212 return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
Filip Gruszczynski0ec13282015-06-25 11:26:01 -0700213 outContentInsets, outStableInsets, null /* outOutsets */, null);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800214 }
215
Andrew Scull26f830d2017-05-19 12:16:10 +0100216 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800217 public void remove(IWindow window) {
218 mService.removeWindow(this, window);
219 }
220
Rob Carr64e516f2015-10-29 00:20:45 +0000221 @Override
Robert Carr77bdfb52016-05-02 18:18:31 -0700222 public void prepareToReplaceWindows(IBinder appToken, boolean childrenOnly) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700223 mService.setWillReplaceWindows(appToken, childrenOnly);
Robert Carr23fa16b2016-01-13 13:19:58 -0800224 }
225
Andrew Scull26f830d2017-05-19 12:16:10 +0100226 @Override
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700227 public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800228 int requestedWidth, int requestedHeight, int viewFlags,
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800229 int flags, Rect outFrame, Rect outOverscanInsets, Rect outContentInsets,
Jorim Jaggi2e95a482016-01-14 17:36:55 -0800230 Rect outVisibleInsets, Rect outStableInsets, Rect outsets, Rect outBackdropFrame,
Andrii Kulian44607962017-03-16 11:06:24 -0700231 MergedConfiguration mergedConfiguration, Surface outSurface) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800232 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED relayout from "
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700233 + Binder.getCallingPid());
Wale Ogunwale4958ad22017-06-22 09:08:14 -0700234 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, mRelayoutTag);
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700235 int res = mService.relayoutWindow(this, window, seq, attrs,
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800236 requestedWidth, requestedHeight, viewFlags, flags,
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800237 outFrame, outOverscanInsets, outContentInsets, outVisibleInsets,
Andrii Kulian44607962017-03-16 11:06:24 -0700238 outStableInsets, outsets, outBackdropFrame, mergedConfiguration, outSurface);
Wale Ogunwale4958ad22017-06-22 09:08:14 -0700239 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800240 if (false) Slog.d(TAG_WM, "<<<<<< EXITING relayout to "
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700241 + Binder.getCallingPid());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800242 return res;
243 }
244
Andrew Scull26f830d2017-05-19 12:16:10 +0100245 @Override
Dianne Hackborn64825172011-03-02 21:32:58 -0800246 public boolean outOfMemory(IWindow window) {
247 return mService.outOfMemoryWindow(this, window);
248 }
249
Andrew Scull26f830d2017-05-19 12:16:10 +0100250 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800251 public void setTransparentRegion(IWindow window, Region region) {
252 mService.setTransparentRegionWindow(this, window, region);
253 }
254
Andrew Scull26f830d2017-05-19 12:16:10 +0100255 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800256 public void setInsets(IWindow window, int touchableInsets,
257 Rect contentInsets, Rect visibleInsets, Region touchableArea) {
258 mService.setInsetsWindow(this, window, touchableInsets, contentInsets,
259 visibleInsets, touchableArea);
260 }
261
Andrew Scull26f830d2017-05-19 12:16:10 +0100262 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800263 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
264 mService.getWindowDisplayFrame(this, window, outDisplayFrame);
265 }
266
Andrew Scull26f830d2017-05-19 12:16:10 +0100267 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800268 public void finishDrawing(IWindow window) {
269 if (WindowManagerService.localLOGV) Slog.v(
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800270 TAG_WM, "IWindow finishDrawing called for " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800271 mService.finishDrawingWindow(this, window);
272 }
273
Andrew Scull26f830d2017-05-19 12:16:10 +0100274 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800275 public void setInTouchMode(boolean mode) {
276 synchronized(mService.mWindowMap) {
277 mService.mInTouchMode = mode;
278 }
279 }
280
Andrew Scull26f830d2017-05-19 12:16:10 +0100281 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800282 public boolean getInTouchMode() {
283 synchronized(mService.mWindowMap) {
284 return mService.mInTouchMode;
285 }
286 }
287
Andrew Scull26f830d2017-05-19 12:16:10 +0100288 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800289 public boolean performHapticFeedback(IWindow window, int effectId,
290 boolean always) {
291 synchronized(mService.mWindowMap) {
292 long ident = Binder.clearCallingIdentity();
293 try {
294 return mService.mPolicy.performHapticFeedbackLw(
295 mService.windowForClientLocked(this, window, true),
296 effectId, always);
297 } finally {
298 Binder.restoreCallingIdentity(ident);
299 }
300 }
301 }
302
303 /* Drag/drop */
Andrew Scull26f830d2017-05-19 12:16:10 +0100304 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800305 public IBinder prepareDrag(IWindow window, int flags,
306 int width, int height, Surface outSurface) {
307 return mService.prepareDragSurface(window, mSurfaceSession, flags,
308 width, height, outSurface);
309 }
310
Andrew Scull26f830d2017-05-19 12:16:10 +0100311 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800312 public boolean performDrag(IWindow window, IBinder dragToken,
Vladislav Kaznacheevba761122016-01-22 12:09:45 -0800313 int touchSource, float touchX, float touchY, float thumbCenterX, float thumbCenterY,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800314 ClipData data) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800315 if (DEBUG_DRAG) {
316 Slog.d(TAG_WM, "perform drag: win=" + window + " data=" + data);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800317 }
318
319 synchronized (mService.mWindowMap) {
320 if (mService.mDragState == null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800321 Slog.w(TAG_WM, "No drag prepared");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800322 throw new IllegalStateException("performDrag() without prepareDrag()");
323 }
324
325 if (dragToken != mService.mDragState.mToken) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800326 Slog.w(TAG_WM, "Performing mismatched drag");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800327 throw new IllegalStateException("performDrag() does not match prepareDrag()");
328 }
329
330 WindowState callingWin = mService.windowForClientLocked(null, window, false);
331 if (callingWin == null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800332 Slog.w(TAG_WM, "Bad requesting window " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800333 return false; // !!! TODO: throw here?
334 }
335
336 // !!! TODO: if input is not still focused on the initiating window, fail
337 // the drag initiation (e.g. an alarm window popped up just as the application
338 // called performDrag()
339
340 mService.mH.removeMessages(H.DRAG_START_TIMEOUT, window.asBinder());
341
342 // !!! TODO: extract the current touch (x, y) in screen coordinates. That
343 // will let us eliminate the (touchX,touchY) parameters from the API.
344
345 // !!! FIXME: put all this heavy stuff onto the mH looper, as well as
346 // the actual drag event dispatch stuff in the dragstate
347
Craig Mautnerdf88d732014-01-27 09:21:32 -0800348 final DisplayContent displayContent = callingWin.getDisplayContent();
349 if (displayContent == null) {
350 return false;
351 }
352 Display display = displayContent.getDisplay();
Jeff Brown14a9f2b2012-09-24 14:36:44 -0700353 mService.mDragState.register(display);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800354 if (!mService.mInputManager.transferTouchFocus(callingWin.mInputChannel,
Vladislav Kaznacheev64b103a2016-08-10 11:49:08 -0700355 mService.mDragState.getInputChannel())) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800356 Slog.e(TAG_WM, "Unable to transfer touch focus");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800357 mService.mDragState.unregister();
Vladislav Kaznacheev64b103a2016-08-10 11:49:08 -0700358 mService.mDragState.reset();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800359 mService.mDragState = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800360 return false;
361 }
362
Vladislav Kaznacheev64b103a2016-08-10 11:49:08 -0700363 mService.mDragState.mDisplayContent = displayContent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800364 mService.mDragState.mData = data;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800365 mService.mDragState.broadcastDragStartedLw(touchX, touchY);
Vladislav Kaznacheevba761122016-01-22 12:09:45 -0800366 mService.mDragState.overridePointerIconLw(touchSource);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800367
368 // remember the thumb offsets for later
369 mService.mDragState.mThumbOffsetX = thumbCenterX;
370 mService.mDragState.mThumbOffsetY = thumbCenterY;
371
372 // Make the surface visible at the proper location
Mathias Agopian29479eb2013-02-14 14:36:04 -0800373 final SurfaceControl surfaceControl = mService.mDragState.mSurfaceControl;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800374 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(
375 TAG_WM, ">>> OPEN TRANSACTION performDrag");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700376 mService.openSurfaceTransaction();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800377 try {
Mathias Agopian29479eb2013-02-14 14:36:04 -0800378 surfaceControl.setPosition(touchX - thumbCenterX,
Dianne Hackbornd040edb2011-08-31 12:47:58 -0700379 touchY - thumbCenterY);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800380 surfaceControl.setLayer(mService.mDragState.getDragLayerLw());
381 surfaceControl.setLayerStack(display.getLayerStack());
382 surfaceControl.show();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800383 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700384 mService.closeSurfaceTransaction();
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800385 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(
386 TAG_WM, "<<< CLOSE TRANSACTION performDrag");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800387 }
Vladislav Kaznacheevd0ed8932016-01-15 15:37:05 -0800388
389 mService.mDragState.notifyLocationLw(touchX, touchY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800390 }
391
392 return true; // success!
393 }
394
Andrew Scull26f830d2017-05-19 12:16:10 +0100395 @Override
Chong Zhang8e89b312015-09-09 15:09:30 -0700396 public boolean startMovingTask(IWindow window, float startX, float startY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800397 if (DEBUG_TASK_POSITIONING) Slog.d(
398 TAG_WM, "startMovingTask: {" + startX + "," + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700399
Wale Ogunwale09e1b8d2016-02-23 10:38:35 -0800400 long ident = Binder.clearCallingIdentity();
401 try {
402 return mService.startMovingTask(window, startX, startY);
403 } finally {
404 Binder.restoreCallingIdentity(ident);
405 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700406 }
407
Andrew Scull26f830d2017-05-19 12:16:10 +0100408 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800409 public void reportDropResult(IWindow window, boolean consumed) {
410 IBinder token = window.asBinder();
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800411 if (DEBUG_DRAG) {
412 Slog.d(TAG_WM, "Drop result=" + consumed + " reported by " + token);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800413 }
414
415 synchronized (mService.mWindowMap) {
416 long ident = Binder.clearCallingIdentity();
417 try {
Christopher Tate05e9c652011-10-20 12:34:36 -0700418 if (mService.mDragState == null) {
419 // Most likely the drop recipient ANRed and we ended the drag
420 // out from under it. Log the issue and move on.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800421 Slog.w(TAG_WM, "Drop result given but no drag in progress");
Christopher Tate05e9c652011-10-20 12:34:36 -0700422 return;
423 }
424
425 if (mService.mDragState.mToken != token) {
426 // We're in a drag, but the wrong window has responded.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800427 Slog.w(TAG_WM, "Invalid drop-result claim by " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800428 throw new IllegalStateException("reportDropResult() by non-recipient");
429 }
430
431 // The right window has responded, even if it's no longer around,
432 // so be sure to halt the timeout even if the later WindowState
433 // lookup fails.
434 mService.mH.removeMessages(H.DRAG_END_TIMEOUT, window.asBinder());
435 WindowState callingWin = mService.windowForClientLocked(null, window, false);
436 if (callingWin == null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800437 Slog.w(TAG_WM, "Bad result-reporting window " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800438 return; // !!! TODO: throw here?
439 }
440
441 mService.mDragState.mDragResult = consumed;
442 mService.mDragState.endDragLw();
443 } finally {
444 Binder.restoreCallingIdentity(ident);
445 }
446 }
447 }
448
Andrew Scull26f830d2017-05-19 12:16:10 +0100449 @Override
Vladislav Kaznacheev82063912015-11-20 14:20:13 -0800450 public void cancelDragAndDrop(IBinder dragToken) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800451 if (DEBUG_DRAG) {
452 Slog.d(TAG_WM, "cancelDragAndDrop");
Vladislav Kaznacheev82063912015-11-20 14:20:13 -0800453 }
454
455 synchronized (mService.mWindowMap) {
456 long ident = Binder.clearCallingIdentity();
457 try {
458 if (mService.mDragState == null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800459 Slog.w(TAG_WM, "cancelDragAndDrop() without prepareDrag()");
Vladislav Kaznacheev82063912015-11-20 14:20:13 -0800460 throw new IllegalStateException("cancelDragAndDrop() without prepareDrag()");
461 }
462
463 if (mService.mDragState.mToken != dragToken) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800464 Slog.w(TAG_WM,
Vladislav Kaznacheev82063912015-11-20 14:20:13 -0800465 "cancelDragAndDrop() does not match prepareDrag()");
466 throw new IllegalStateException(
467 "cancelDragAndDrop() does not match prepareDrag()");
468 }
469
470 mService.mDragState.mDragResult = false;
Vladislav Kaznacheevce2aef92015-11-20 18:49:59 -0800471 mService.mDragState.cancelDragLw();
Vladislav Kaznacheev82063912015-11-20 14:20:13 -0800472 } finally {
473 Binder.restoreCallingIdentity(ident);
474 }
475 }
476 }
477
Andrew Scull26f830d2017-05-19 12:16:10 +0100478 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800479 public void dragRecipientEntered(IWindow window) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800480 if (DEBUG_DRAG) {
481 Slog.d(TAG_WM, "Drag into new candidate view @ " + window.asBinder());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800482 }
483 }
484
Andrew Scull26f830d2017-05-19 12:16:10 +0100485 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800486 public void dragRecipientExited(IWindow window) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800487 if (DEBUG_DRAG) {
488 Slog.d(TAG_WM, "Drag from old candidate view @ " + window.asBinder());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800489 }
490 }
491
Andrew Scull26f830d2017-05-19 12:16:10 +0100492 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800493 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
494 synchronized(mService.mWindowMap) {
495 long ident = Binder.clearCallingIdentity();
496 try {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700497 mService.mRoot.mWallpaperController.setWindowWallpaperPosition(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800498 mService.windowForClientLocked(this, window, true),
499 x, y, xStep, yStep);
500 } finally {
501 Binder.restoreCallingIdentity(ident);
502 }
503 }
504 }
505
Andrew Scull26f830d2017-05-19 12:16:10 +0100506 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800507 public void wallpaperOffsetsComplete(IBinder window) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700508 synchronized (mService.mWindowMap) {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700509 mService.mRoot.mWallpaperController.wallpaperOffsetsComplete(window);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700510 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800511 }
512
Andrew Scull26f830d2017-05-19 12:16:10 +0100513 @Override
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700514 public void setWallpaperDisplayOffset(IBinder window, int x, int y) {
515 synchronized(mService.mWindowMap) {
516 long ident = Binder.clearCallingIdentity();
517 try {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700518 mService.mRoot.mWallpaperController.setWindowWallpaperDisplayOffset(
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700519 mService.windowForClientLocked(this, window, true), x, y);
520 } finally {
521 Binder.restoreCallingIdentity(ident);
522 }
523 }
524 }
525
Andrew Scull26f830d2017-05-19 12:16:10 +0100526 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800527 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
528 int z, Bundle extras, boolean sync) {
529 synchronized(mService.mWindowMap) {
530 long ident = Binder.clearCallingIdentity();
531 try {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700532 return mService.mRoot.mWallpaperController.sendWindowWallpaperCommand(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800533 mService.windowForClientLocked(this, window, true),
534 action, x, y, z, extras, sync);
535 } finally {
536 Binder.restoreCallingIdentity(ident);
537 }
538 }
539 }
540
Andrew Scull26f830d2017-05-19 12:16:10 +0100541 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800542 public void wallpaperCommandComplete(IBinder window, Bundle result) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700543 synchronized (mService.mWindowMap) {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700544 mService.mRoot.mWallpaperController.wallpaperCommandComplete(window);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700545 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800546 }
547
Andrew Scull26f830d2017-05-19 12:16:10 +0100548 @Override
Svetoslavf7174e82014-06-12 11:29:35 -0700549 public void onRectangleOnScreenRequested(IBinder token, Rect rectangle) {
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700550 synchronized(mService.mWindowMap) {
551 final long identity = Binder.clearCallingIdentity();
552 try {
Svetoslavf7174e82014-06-12 11:29:35 -0700553 mService.onRectangleOnScreenRequested(token, rectangle);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700554 } finally {
555 Binder.restoreCallingIdentity(identity);
556 }
557 }
558 }
559
Andrew Scull26f830d2017-05-19 12:16:10 +0100560 @Override
Dianne Hackborne3f23a32013-03-01 13:25:35 -0800561 public IWindowId getWindowId(IBinder window) {
562 return mService.getWindowId(window);
563 }
564
Jeff Brownc2932a12014-11-20 18:04:05 -0800565 @Override
566 public void pokeDrawLock(IBinder window) {
567 final long identity = Binder.clearCallingIdentity();
568 try {
569 mService.pokeDrawLock(this, window);
570 } finally {
571 Binder.restoreCallingIdentity(identity);
572 }
573 }
574
Vladislav Kaznacheev989b58a2016-02-10 12:19:33 -0800575 @Override
576 public void updatePointerIcon(IWindow window) {
577 final long identity = Binder.clearCallingIdentity();
578 try {
579 mService.updatePointerIcon(window);
580 } finally {
581 Binder.restoreCallingIdentity(identity);
582 }
583 }
584
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800585 void windowAddedLocked(String packageName) {
586 mPackageName = packageName;
Wale Ogunwale4958ad22017-06-22 09:08:14 -0700587 mRelayoutTag = "relayoutWindow: " + mPackageName;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800588 if (mSurfaceSession == null) {
589 if (WindowManagerService.localLOGV) Slog.v(
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800590 TAG_WM, "First window added to " + this + ", creating SurfaceSession");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800591 mSurfaceSession = new SurfaceSession();
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800592 if (SHOW_TRANSACTIONS) Slog.i(
593 TAG_WM, " NEW SURFACE SESSION " + mSurfaceSession);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800594 mService.mSessions.add(this);
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700595 if (mLastReportedAnimatorScale != mService.getCurrentAnimatorScale()) {
596 mService.dispatchNewAnimatorScaleLocked(this);
597 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800598 }
599 mNumWindow++;
600 }
601
Wale Ogunwale943002b2017-02-15 19:34:01 -0800602 void windowRemovedLocked() {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800603 mNumWindow--;
Wale Ogunwale943002b2017-02-15 19:34:01 -0800604 killSessionLocked();
605 }
606
607
608 void onWindowSurfaceVisibilityChanged(WindowSurfaceController surfaceController,
609 boolean visible, int type) {
610
611 if (!isSystemAlertWindowType(type)) {
612 return;
613 }
614
615 boolean changed;
616
617 if (!mCanAddInternalSystemWindow) {
618 // We want to track non-system signature apps adding alert windows so we can post an
619 // on-going notification for the user to control their visibility.
620 if (visible) {
621 changed = mAlertWindowSurfaces.add(surfaceController);
622 } else {
623 changed = mAlertWindowSurfaces.remove(surfaceController);
624 }
625
626 if (changed) {
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800627 if (mAlertWindowSurfaces.isEmpty()) {
628 cancelAlertWindowNotification();
629 } else if (mAlertWindowNotification == null){
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800630 mAlertWindowNotification = new AlertWindowNotification(mService, mPackageName);
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -0700631 if (mShowingAlertWindowNotificationAllowed) {
632 mAlertWindowNotification.post();
633 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800634 }
Wale Ogunwaled993a572017-02-05 13:52:09 -0800635 }
636 }
Wale Ogunwale943002b2017-02-15 19:34:01 -0800637
638 if (type != TYPE_APPLICATION_OVERLAY) {
639 return;
640 }
641
642 if (visible) {
643 changed = mAppOverlaySurfaces.add(surfaceController);
644 } else {
645 changed = mAppOverlaySurfaces.remove(surfaceController);
646 }
647
648 if (changed) {
649 // Notify activity manager of changes to app overlay windows so it can adjust the
650 // importance score for the process.
651 setHasOverlayUi(!mAppOverlaySurfaces.isEmpty());
652 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800653 }
654
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -0700655 void setShowingAlertWindowNotificationAllowed(boolean allowed) {
656 mShowingAlertWindowNotificationAllowed = allowed;
657 if (mAlertWindowNotification != null) {
658 if (allowed) {
659 mAlertWindowNotification.post();
660 } else {
661 mAlertWindowNotification.cancel();
662 }
663 }
664 }
665
Wale Ogunwaled993a572017-02-05 13:52:09 -0800666 private void killSessionLocked() {
667 if (mNumWindow > 0 || !mClientDead) {
668 return;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800669 }
Wale Ogunwaled993a572017-02-05 13:52:09 -0800670
671 mService.mSessions.remove(this);
672 if (mSurfaceSession == null) {
673 return;
674 }
675
676 if (WindowManagerService.localLOGV) Slog.v(TAG_WM, "Last window removed from " + this
677 + ", destroying " + mSurfaceSession);
678 if (SHOW_TRANSACTIONS) Slog.i(TAG_WM, " KILL SURFACE SESSION " + mSurfaceSession);
679 try {
680 mSurfaceSession.kill();
681 } catch (Exception e) {
682 Slog.w(TAG_WM, "Exception thrown when killing surface session " + mSurfaceSession
683 + " in session " + this + ": " + e.toString());
684 }
685 mSurfaceSession = null;
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800686 mAlertWindowSurfaces.clear();
687 mAppOverlaySurfaces.clear();
Wale Ogunwaled993a572017-02-05 13:52:09 -0800688 setHasOverlayUi(false);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800689 cancelAlertWindowNotification();
Wale Ogunwaled993a572017-02-05 13:52:09 -0800690 }
691
692 private void setHasOverlayUi(boolean hasOverlayUi) {
693 mService.mH.obtainMessage(H.SET_HAS_OVERLAY_UI, mPid, hasOverlayUi ? 1 : 0).sendToTarget();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800694 }
695
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800696 private void cancelAlertWindowNotification() {
697 if (mAlertWindowNotification == null) {
698 return;
699 }
700 mAlertWindowNotification.cancel();
701 mAlertWindowNotification = null;
702 }
703
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800704 void dump(PrintWriter pw, String prefix) {
705 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800706 pw.print(" mCanAddInternalSystemWindow="); pw.print(mCanAddInternalSystemWindow);
Wale Ogunwale943002b2017-02-15 19:34:01 -0800707 pw.print(" mAppOverlaySurfaces="); pw.print(mAppOverlaySurfaces);
708 pw.print(" mAlertWindowSurfaces="); pw.print(mAlertWindowSurfaces);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800709 pw.print(" mClientDead="); pw.print(mClientDead);
710 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
Wale Ogunwale4958ad22017-06-22 09:08:14 -0700711 pw.print(prefix); pw.print("mPackageName="); pw.println(mPackageName);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800712 }
713
714 @Override
715 public String toString() {
716 return mStringName;
717 }
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700718}