blob: 3611f3ebf692bbfdfc71c62b9e288c9dee8630e0 [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
19import com.android.internal.view.IInputContext;
20import com.android.internal.view.IInputMethodClient;
21import com.android.internal.view.IInputMethodManager;
22import com.android.server.wm.WindowManagerService.H;
23
24import android.content.ClipData;
25import android.content.Context;
26import android.content.res.Configuration;
27import android.graphics.Rect;
28import android.graphics.Region;
29import android.os.Binder;
30import android.os.Bundle;
31import android.os.IBinder;
32import android.os.Parcel;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070033import android.os.Process;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080034import android.os.RemoteException;
35import android.os.ServiceManager;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070036import android.os.UserHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080037import android.util.Slog;
Craig Mautner6881a102012-07-27 13:04:51 -070038import android.view.Display;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080039import android.view.IWindow;
40import android.view.IWindowSession;
41import android.view.InputChannel;
42import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080043import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080044import android.view.SurfaceSession;
45import android.view.WindowManager;
46
47import java.io.PrintWriter;
48
49/**
50 * This class represents an active client session. There is generally one
51 * Session object per process that is interacting with the window manager.
52 */
53final class Session extends IWindowSession.Stub
54 implements IBinder.DeathRecipient {
55 final WindowManagerService mService;
56 final IInputMethodClient mClient;
57 final IInputContext mInputContext;
58 final int mUid;
59 final int mPid;
60 final String mStringName;
61 SurfaceSession mSurfaceSession;
62 int mNumWindow = 0;
63 boolean mClientDead = false;
64
65 public Session(WindowManagerService service, IInputMethodClient client,
66 IInputContext inputContext) {
67 mService = service;
68 mClient = client;
69 mInputContext = inputContext;
70 mUid = Binder.getCallingUid();
71 mPid = Binder.getCallingPid();
72 StringBuilder sb = new StringBuilder();
73 sb.append("Session{");
74 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -070075 sb.append(" ");
76 sb.append(mPid);
77 if (mUid < Process.FIRST_APPLICATION_UID) {
78 sb.append(":");
79 sb.append(mUid);
80 } else {
81 sb.append(":u");
82 sb.append(UserHandle.getUserId(mUid));
83 sb.append('a');
84 sb.append(UserHandle.getAppId(mUid));
85 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080086 sb.append("}");
87 mStringName = sb.toString();
88
89 synchronized (mService.mWindowMap) {
90 if (mService.mInputMethodManager == null && mService.mHaveInputMethods) {
91 IBinder b = ServiceManager.getService(
92 Context.INPUT_METHOD_SERVICE);
93 mService.mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
94 }
95 }
96 long ident = Binder.clearCallingIdentity();
97 try {
98 // Note: it is safe to call in to the input method manager
99 // here because we are not holding our lock.
100 if (mService.mInputMethodManager != null) {
101 mService.mInputMethodManager.addClient(client, inputContext,
102 mUid, mPid);
103 } else {
104 client.setUsingInputMethod(false);
105 }
106 client.asBinder().linkToDeath(this, 0);
107 } catch (RemoteException e) {
108 // The caller has died, so we can just forget about this.
109 try {
110 if (mService.mInputMethodManager != null) {
111 mService.mInputMethodManager.removeClient(client);
112 }
113 } catch (RemoteException ee) {
114 }
115 } finally {
116 Binder.restoreCallingIdentity(ident);
117 }
118 }
119
120 @Override
121 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
122 throws RemoteException {
123 try {
124 return super.onTransact(code, data, reply, flags);
125 } catch (RuntimeException e) {
126 // Log all 'real' exceptions thrown to the caller
127 if (!(e instanceof SecurityException)) {
128 Slog.e(WindowManagerService.TAG, "Window Session Crash", e);
129 }
130 throw e;
131 }
132 }
133
134 public void binderDied() {
135 // Note: it is safe to call in to the input method manager
136 // here because we are not holding our lock.
137 try {
138 if (mService.mInputMethodManager != null) {
139 mService.mInputMethodManager.removeClient(mClient);
140 }
141 } catch (RemoteException e) {
142 }
143 synchronized(mService.mWindowMap) {
144 mClient.asBinder().unlinkToDeath(this, 0);
145 mClientDead = true;
146 killSessionLocked();
147 }
148 }
149
Craig Mautner6881a102012-07-27 13:04:51 -0700150 @Override
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700151 public int add(IWindow window, int seq, WindowManager.LayoutParams attrs,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800152 int viewVisibility, Rect outContentInsets, InputChannel outInputChannel) {
Craig Mautner6881a102012-07-27 13:04:51 -0700153 return addToDisplay(window, seq, attrs, viewVisibility, Display.DEFAULT_DISPLAY,
154 outContentInsets, outInputChannel);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800155 }
Craig Mautner6881a102012-07-27 13:04:51 -0700156
157 @Override
158 public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
159 int viewVisibility, int displayId, Rect outContentInsets,
160 InputChannel outInputChannel) {
161 return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
162 outContentInsets, outInputChannel);
163 }
164
165 @Override
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700166 public int addWithoutInputChannel(IWindow window, int seq, WindowManager.LayoutParams attrs,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800167 int viewVisibility, Rect outContentInsets) {
Craig Mautner6881a102012-07-27 13:04:51 -0700168 return addToDisplayWithoutInputChannel(window, seq, attrs, viewVisibility,
169 Display.DEFAULT_DISPLAY, outContentInsets);
170 }
171
172 @Override
173 public int addToDisplayWithoutInputChannel(IWindow window, int seq, WindowManager.LayoutParams attrs,
174 int viewVisibility, int displayId, Rect outContentInsets) {
175 return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
176 outContentInsets, null);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800177 }
178
179 public void remove(IWindow window) {
180 mService.removeWindow(this, window);
181 }
182
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700183 public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800184 int requestedWidth, int requestedHeight, int viewFlags,
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800185 int flags, Rect outFrame, Rect outOverscanInsets, Rect outContentInsets,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800186 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700187 if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED relayout from "
188 + Binder.getCallingPid());
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700189 int res = mService.relayoutWindow(this, window, seq, attrs,
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800190 requestedWidth, requestedHeight, viewFlags, flags,
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800191 outFrame, outOverscanInsets, outContentInsets, outVisibleInsets,
Dianne Hackborn5c58de32012-04-28 19:52:37 -0700192 outConfig, outSurface);
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700193 if (false) Slog.d(WindowManagerService.TAG, "<<<<<< EXITING relayout to "
194 + Binder.getCallingPid());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800195 return res;
196 }
197
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800198 public void performDeferredDestroy(IWindow window) {
199 mService.performDeferredDestroyWindow(this, window);
200 }
201
Dianne Hackborn64825172011-03-02 21:32:58 -0800202 public boolean outOfMemory(IWindow window) {
203 return mService.outOfMemoryWindow(this, window);
204 }
205
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800206 public void setTransparentRegion(IWindow window, Region region) {
207 mService.setTransparentRegionWindow(this, window, region);
208 }
209
210 public void setInsets(IWindow window, int touchableInsets,
211 Rect contentInsets, Rect visibleInsets, Region touchableArea) {
212 mService.setInsetsWindow(this, window, touchableInsets, contentInsets,
213 visibleInsets, touchableArea);
214 }
215
216 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
217 mService.getWindowDisplayFrame(this, window, outDisplayFrame);
218 }
219
220 public void finishDrawing(IWindow window) {
221 if (WindowManagerService.localLOGV) Slog.v(
222 WindowManagerService.TAG, "IWindow finishDrawing called for " + window);
223 mService.finishDrawingWindow(this, window);
224 }
225
226 public void setInTouchMode(boolean mode) {
227 synchronized(mService.mWindowMap) {
228 mService.mInTouchMode = mode;
229 }
230 }
231
232 public boolean getInTouchMode() {
233 synchronized(mService.mWindowMap) {
234 return mService.mInTouchMode;
235 }
236 }
237
238 public boolean performHapticFeedback(IWindow window, int effectId,
239 boolean always) {
240 synchronized(mService.mWindowMap) {
241 long ident = Binder.clearCallingIdentity();
242 try {
243 return mService.mPolicy.performHapticFeedbackLw(
244 mService.windowForClientLocked(this, window, true),
245 effectId, always);
246 } finally {
247 Binder.restoreCallingIdentity(ident);
248 }
249 }
250 }
251
252 /* Drag/drop */
253 public IBinder prepareDrag(IWindow window, int flags,
254 int width, int height, Surface outSurface) {
255 return mService.prepareDragSurface(window, mSurfaceSession, flags,
256 width, height, outSurface);
257 }
258
259 public boolean performDrag(IWindow window, IBinder dragToken,
260 float touchX, float touchY, float thumbCenterX, float thumbCenterY,
261 ClipData data) {
262 if (WindowManagerService.DEBUG_DRAG) {
263 Slog.d(WindowManagerService.TAG, "perform drag: win=" + window + " data=" + data);
264 }
265
266 synchronized (mService.mWindowMap) {
267 if (mService.mDragState == null) {
268 Slog.w(WindowManagerService.TAG, "No drag prepared");
269 throw new IllegalStateException("performDrag() without prepareDrag()");
270 }
271
272 if (dragToken != mService.mDragState.mToken) {
273 Slog.w(WindowManagerService.TAG, "Performing mismatched drag");
274 throw new IllegalStateException("performDrag() does not match prepareDrag()");
275 }
276
277 WindowState callingWin = mService.windowForClientLocked(null, window, false);
278 if (callingWin == null) {
279 Slog.w(WindowManagerService.TAG, "Bad requesting window " + window);
280 return false; // !!! TODO: throw here?
281 }
282
283 // !!! TODO: if input is not still focused on the initiating window, fail
284 // the drag initiation (e.g. an alarm window popped up just as the application
285 // called performDrag()
286
287 mService.mH.removeMessages(H.DRAG_START_TIMEOUT, window.asBinder());
288
289 // !!! TODO: extract the current touch (x, y) in screen coordinates. That
290 // will let us eliminate the (touchX,touchY) parameters from the API.
291
292 // !!! FIXME: put all this heavy stuff onto the mH looper, as well as
293 // the actual drag event dispatch stuff in the dragstate
294
Jeff Brown14a9f2b2012-09-24 14:36:44 -0700295 Display display = callingWin.mDisplayContent.getDisplay();
296 mService.mDragState.register(display);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800297 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
298 if (!mService.mInputManager.transferTouchFocus(callingWin.mInputChannel,
299 mService.mDragState.mServerChannel)) {
300 Slog.e(WindowManagerService.TAG, "Unable to transfer touch focus");
301 mService.mDragState.unregister();
302 mService.mDragState = null;
303 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
304 return false;
305 }
306
307 mService.mDragState.mData = data;
308 mService.mDragState.mCurrentX = touchX;
309 mService.mDragState.mCurrentY = touchY;
310 mService.mDragState.broadcastDragStartedLw(touchX, touchY);
311
312 // remember the thumb offsets for later
313 mService.mDragState.mThumbOffsetX = thumbCenterX;
314 mService.mDragState.mThumbOffsetY = thumbCenterY;
315
316 // Make the surface visible at the proper location
Mathias Agopian29479eb2013-02-14 14:36:04 -0800317 final SurfaceControl surfaceControl = mService.mDragState.mSurfaceControl;
Dianne Hackborn36991742011-10-11 21:35:26 -0700318 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(
319 WindowManagerService.TAG, ">>> OPEN TRANSACTION performDrag");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800320 SurfaceControl.openTransaction();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800321 try {
Mathias Agopian29479eb2013-02-14 14:36:04 -0800322 surfaceControl.setPosition(touchX - thumbCenterX,
Dianne Hackbornd040edb2011-08-31 12:47:58 -0700323 touchY - thumbCenterY);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800324 surfaceControl.setAlpha(.7071f);
325 surfaceControl.setLayer(mService.mDragState.getDragLayerLw());
326 surfaceControl.setLayerStack(display.getLayerStack());
327 surfaceControl.show();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800328 } finally {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800329 SurfaceControl.closeTransaction();
Dianne Hackborn36991742011-10-11 21:35:26 -0700330 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(
331 WindowManagerService.TAG, "<<< CLOSE TRANSACTION performDrag");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800332 }
333 }
334
335 return true; // success!
336 }
337
338 public void reportDropResult(IWindow window, boolean consumed) {
339 IBinder token = window.asBinder();
340 if (WindowManagerService.DEBUG_DRAG) {
341 Slog.d(WindowManagerService.TAG, "Drop result=" + consumed + " reported by " + token);
342 }
343
344 synchronized (mService.mWindowMap) {
345 long ident = Binder.clearCallingIdentity();
346 try {
Christopher Tate05e9c652011-10-20 12:34:36 -0700347 if (mService.mDragState == null) {
348 // Most likely the drop recipient ANRed and we ended the drag
349 // out from under it. Log the issue and move on.
350 Slog.w(WindowManagerService.TAG, "Drop result given but no drag in progress");
351 return;
352 }
353
354 if (mService.mDragState.mToken != token) {
355 // We're in a drag, but the wrong window has responded.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800356 Slog.w(WindowManagerService.TAG, "Invalid drop-result claim by " + window);
357 throw new IllegalStateException("reportDropResult() by non-recipient");
358 }
359
360 // The right window has responded, even if it's no longer around,
361 // so be sure to halt the timeout even if the later WindowState
362 // lookup fails.
363 mService.mH.removeMessages(H.DRAG_END_TIMEOUT, window.asBinder());
364 WindowState callingWin = mService.windowForClientLocked(null, window, false);
365 if (callingWin == null) {
366 Slog.w(WindowManagerService.TAG, "Bad result-reporting window " + window);
367 return; // !!! TODO: throw here?
368 }
369
370 mService.mDragState.mDragResult = consumed;
371 mService.mDragState.endDragLw();
372 } finally {
373 Binder.restoreCallingIdentity(ident);
374 }
375 }
376 }
377
378 public void dragRecipientEntered(IWindow window) {
379 if (WindowManagerService.DEBUG_DRAG) {
380 Slog.d(WindowManagerService.TAG, "Drag into new candidate view @ " + window.asBinder());
381 }
382 }
383
384 public void dragRecipientExited(IWindow window) {
385 if (WindowManagerService.DEBUG_DRAG) {
386 Slog.d(WindowManagerService.TAG, "Drag from old candidate view @ " + window.asBinder());
387 }
388 }
389
390 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
391 synchronized(mService.mWindowMap) {
392 long ident = Binder.clearCallingIdentity();
393 try {
394 mService.setWindowWallpaperPositionLocked(
395 mService.windowForClientLocked(this, window, true),
396 x, y, xStep, yStep);
397 } finally {
398 Binder.restoreCallingIdentity(ident);
399 }
400 }
401 }
402
403 public void wallpaperOffsetsComplete(IBinder window) {
404 mService.wallpaperOffsetsComplete(window);
405 }
406
407 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
408 int z, Bundle extras, boolean sync) {
409 synchronized(mService.mWindowMap) {
410 long ident = Binder.clearCallingIdentity();
411 try {
412 return mService.sendWindowWallpaperCommandLocked(
413 mService.windowForClientLocked(this, window, true),
414 action, x, y, z, extras, sync);
415 } finally {
416 Binder.restoreCallingIdentity(ident);
417 }
418 }
419 }
420
421 public void wallpaperCommandComplete(IBinder window, Bundle result) {
422 mService.wallpaperCommandComplete(window, result);
423 }
424
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700425 public void setUniverseTransform(IBinder window, float alpha, float offx, float offy,
426 float dsdx, float dtdx, float dsdy, float dtdy) {
427 synchronized(mService.mWindowMap) {
428 long ident = Binder.clearCallingIdentity();
429 try {
430 mService.setUniverseTransformLocked(
431 mService.windowForClientLocked(this, window, true),
432 alpha, offx, offy, dsdx, dtdx, dsdy, dtdy);
433 } finally {
434 Binder.restoreCallingIdentity(ident);
435 }
436 }
437 }
438
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700439 public void onRectangleOnScreenRequested(IBinder token, Rect rectangle, boolean immediate) {
440 synchronized(mService.mWindowMap) {
441 final long identity = Binder.clearCallingIdentity();
442 try {
443 mService.onRectangleOnScreenRequested(token, rectangle, immediate);
444 } finally {
445 Binder.restoreCallingIdentity(identity);
446 }
447 }
448 }
449
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800450 void windowAddedLocked() {
451 if (mSurfaceSession == null) {
452 if (WindowManagerService.localLOGV) Slog.v(
453 WindowManagerService.TAG, "First window added to " + this + ", creating SurfaceSession");
454 mSurfaceSession = new SurfaceSession();
455 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
456 WindowManagerService.TAG, " NEW SURFACE SESSION " + mSurfaceSession);
457 mService.mSessions.add(this);
458 }
459 mNumWindow++;
460 }
461
462 void windowRemovedLocked() {
463 mNumWindow--;
464 killSessionLocked();
465 }
466
467 void killSessionLocked() {
468 if (mNumWindow <= 0 && mClientDead) {
469 mService.mSessions.remove(this);
470 if (mSurfaceSession != null) {
471 if (WindowManagerService.localLOGV) Slog.v(
472 WindowManagerService.TAG, "Last window removed from " + this
473 + ", destroying " + mSurfaceSession);
474 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
475 WindowManagerService.TAG, " KILL SURFACE SESSION " + mSurfaceSession);
476 try {
477 mSurfaceSession.kill();
478 } catch (Exception e) {
479 Slog.w(WindowManagerService.TAG, "Exception thrown when killing surface session "
480 + mSurfaceSession + " in session " + this
481 + ": " + e.toString());
482 }
483 mSurfaceSession = null;
484 }
485 }
486 }
487
488 void dump(PrintWriter pw, String prefix) {
489 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
490 pw.print(" mClientDead="); pw.print(mClientDead);
491 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
492 }
493
494 @Override
495 public String toString() {
496 return mStringName;
497 }
498}