blob: 77575f2f52514d7be0b9a2dfcef1de6de144dd27 [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;
33import android.os.RemoteException;
34import android.os.ServiceManager;
35import android.util.Slog;
36import android.view.IWindow;
37import android.view.IWindowSession;
38import android.view.InputChannel;
39import android.view.Surface;
40import android.view.SurfaceSession;
41import android.view.WindowManager;
42
43import java.io.PrintWriter;
44
45/**
46 * This class represents an active client session. There is generally one
47 * Session object per process that is interacting with the window manager.
48 */
49final class Session extends IWindowSession.Stub
50 implements IBinder.DeathRecipient {
51 final WindowManagerService mService;
52 final IInputMethodClient mClient;
53 final IInputContext mInputContext;
54 final int mUid;
55 final int mPid;
56 final String mStringName;
57 SurfaceSession mSurfaceSession;
58 int mNumWindow = 0;
59 boolean mClientDead = false;
60
61 public Session(WindowManagerService service, IInputMethodClient client,
62 IInputContext inputContext) {
63 mService = service;
64 mClient = client;
65 mInputContext = inputContext;
66 mUid = Binder.getCallingUid();
67 mPid = Binder.getCallingPid();
68 StringBuilder sb = new StringBuilder();
69 sb.append("Session{");
70 sb.append(Integer.toHexString(System.identityHashCode(this)));
71 sb.append(" uid ");
72 sb.append(mUid);
73 sb.append("}");
74 mStringName = sb.toString();
75
76 synchronized (mService.mWindowMap) {
77 if (mService.mInputMethodManager == null && mService.mHaveInputMethods) {
78 IBinder b = ServiceManager.getService(
79 Context.INPUT_METHOD_SERVICE);
80 mService.mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
81 }
82 }
83 long ident = Binder.clearCallingIdentity();
84 try {
85 // Note: it is safe to call in to the input method manager
86 // here because we are not holding our lock.
87 if (mService.mInputMethodManager != null) {
88 mService.mInputMethodManager.addClient(client, inputContext,
89 mUid, mPid);
90 } else {
91 client.setUsingInputMethod(false);
92 }
93 client.asBinder().linkToDeath(this, 0);
94 } catch (RemoteException e) {
95 // The caller has died, so we can just forget about this.
96 try {
97 if (mService.mInputMethodManager != null) {
98 mService.mInputMethodManager.removeClient(client);
99 }
100 } catch (RemoteException ee) {
101 }
102 } finally {
103 Binder.restoreCallingIdentity(ident);
104 }
105 }
106
107 @Override
108 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
109 throws RemoteException {
110 try {
111 return super.onTransact(code, data, reply, flags);
112 } catch (RuntimeException e) {
113 // Log all 'real' exceptions thrown to the caller
114 if (!(e instanceof SecurityException)) {
115 Slog.e(WindowManagerService.TAG, "Window Session Crash", e);
116 }
117 throw e;
118 }
119 }
120
121 public void binderDied() {
122 // Note: it is safe to call in to the input method manager
123 // here because we are not holding our lock.
124 try {
125 if (mService.mInputMethodManager != null) {
126 mService.mInputMethodManager.removeClient(mClient);
127 }
128 } catch (RemoteException e) {
129 }
130 synchronized(mService.mWindowMap) {
131 mClient.asBinder().unlinkToDeath(this, 0);
132 mClientDead = true;
133 killSessionLocked();
134 }
135 }
136
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700137 public int add(IWindow window, int seq, WindowManager.LayoutParams attrs,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800138 int viewVisibility, Rect outContentInsets, InputChannel outInputChannel) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700139 return mService.addWindow(this, window, seq, attrs, viewVisibility, outContentInsets,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800140 outInputChannel);
141 }
142
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700143 public int addWithoutInputChannel(IWindow window, int seq, WindowManager.LayoutParams attrs,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800144 int viewVisibility, Rect outContentInsets) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700145 return mService.addWindow(this, window, seq, attrs, viewVisibility, outContentInsets, null);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800146 }
147
148 public void remove(IWindow window) {
149 mService.removeWindow(this, window);
150 }
151
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700152 public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800153 int requestedWidth, int requestedHeight, int viewFlags,
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800154 int flags, Rect outFrame, Rect outContentInsets,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800155 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700156 if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED relayout from "
157 + Binder.getCallingPid());
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700158 int res = mService.relayoutWindow(this, window, seq, attrs,
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800159 requestedWidth, requestedHeight, viewFlags, flags,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800160 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700161 if (false) Slog.d(WindowManagerService.TAG, "<<<<<< EXITING relayout to "
162 + Binder.getCallingPid());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800163 return res;
164 }
165
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800166 public void performDeferredDestroy(IWindow window) {
167 mService.performDeferredDestroyWindow(this, window);
168 }
169
Dianne Hackborn64825172011-03-02 21:32:58 -0800170 public boolean outOfMemory(IWindow window) {
171 return mService.outOfMemoryWindow(this, window);
172 }
173
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800174 public void setTransparentRegion(IWindow window, Region region) {
175 mService.setTransparentRegionWindow(this, window, region);
176 }
177
178 public void setInsets(IWindow window, int touchableInsets,
179 Rect contentInsets, Rect visibleInsets, Region touchableArea) {
180 mService.setInsetsWindow(this, window, touchableInsets, contentInsets,
181 visibleInsets, touchableArea);
182 }
183
184 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
185 mService.getWindowDisplayFrame(this, window, outDisplayFrame);
186 }
187
188 public void finishDrawing(IWindow window) {
189 if (WindowManagerService.localLOGV) Slog.v(
190 WindowManagerService.TAG, "IWindow finishDrawing called for " + window);
191 mService.finishDrawingWindow(this, window);
192 }
193
194 public void setInTouchMode(boolean mode) {
195 synchronized(mService.mWindowMap) {
196 mService.mInTouchMode = mode;
197 }
198 }
199
200 public boolean getInTouchMode() {
201 synchronized(mService.mWindowMap) {
202 return mService.mInTouchMode;
203 }
204 }
205
206 public boolean performHapticFeedback(IWindow window, int effectId,
207 boolean always) {
208 synchronized(mService.mWindowMap) {
209 long ident = Binder.clearCallingIdentity();
210 try {
211 return mService.mPolicy.performHapticFeedbackLw(
212 mService.windowForClientLocked(this, window, true),
213 effectId, always);
214 } finally {
215 Binder.restoreCallingIdentity(ident);
216 }
217 }
218 }
219
220 /* Drag/drop */
221 public IBinder prepareDrag(IWindow window, int flags,
222 int width, int height, Surface outSurface) {
223 return mService.prepareDragSurface(window, mSurfaceSession, flags,
224 width, height, outSurface);
225 }
226
227 public boolean performDrag(IWindow window, IBinder dragToken,
228 float touchX, float touchY, float thumbCenterX, float thumbCenterY,
229 ClipData data) {
230 if (WindowManagerService.DEBUG_DRAG) {
231 Slog.d(WindowManagerService.TAG, "perform drag: win=" + window + " data=" + data);
232 }
233
234 synchronized (mService.mWindowMap) {
235 if (mService.mDragState == null) {
236 Slog.w(WindowManagerService.TAG, "No drag prepared");
237 throw new IllegalStateException("performDrag() without prepareDrag()");
238 }
239
240 if (dragToken != mService.mDragState.mToken) {
241 Slog.w(WindowManagerService.TAG, "Performing mismatched drag");
242 throw new IllegalStateException("performDrag() does not match prepareDrag()");
243 }
244
245 WindowState callingWin = mService.windowForClientLocked(null, window, false);
246 if (callingWin == null) {
247 Slog.w(WindowManagerService.TAG, "Bad requesting window " + window);
248 return false; // !!! TODO: throw here?
249 }
250
251 // !!! TODO: if input is not still focused on the initiating window, fail
252 // the drag initiation (e.g. an alarm window popped up just as the application
253 // called performDrag()
254
255 mService.mH.removeMessages(H.DRAG_START_TIMEOUT, window.asBinder());
256
257 // !!! TODO: extract the current touch (x, y) in screen coordinates. That
258 // will let us eliminate the (touchX,touchY) parameters from the API.
259
260 // !!! FIXME: put all this heavy stuff onto the mH looper, as well as
261 // the actual drag event dispatch stuff in the dragstate
262
263 mService.mDragState.register();
264 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
265 if (!mService.mInputManager.transferTouchFocus(callingWin.mInputChannel,
266 mService.mDragState.mServerChannel)) {
267 Slog.e(WindowManagerService.TAG, "Unable to transfer touch focus");
268 mService.mDragState.unregister();
269 mService.mDragState = null;
270 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
271 return false;
272 }
273
274 mService.mDragState.mData = data;
275 mService.mDragState.mCurrentX = touchX;
276 mService.mDragState.mCurrentY = touchY;
277 mService.mDragState.broadcastDragStartedLw(touchX, touchY);
278
279 // remember the thumb offsets for later
280 mService.mDragState.mThumbOffsetX = thumbCenterX;
281 mService.mDragState.mThumbOffsetY = thumbCenterY;
282
283 // Make the surface visible at the proper location
284 final Surface surface = mService.mDragState.mSurface;
Dianne Hackborn36991742011-10-11 21:35:26 -0700285 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(
286 WindowManagerService.TAG, ">>> OPEN TRANSACTION performDrag");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800287 Surface.openTransaction();
288 try {
Dianne Hackbornd040edb2011-08-31 12:47:58 -0700289 surface.setPosition(touchX - thumbCenterX,
290 touchY - thumbCenterY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800291 surface.setAlpha(.7071f);
292 surface.setLayer(mService.mDragState.getDragLayerLw());
293 surface.show();
294 } finally {
295 Surface.closeTransaction();
Dianne Hackborn36991742011-10-11 21:35:26 -0700296 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(
297 WindowManagerService.TAG, "<<< CLOSE TRANSACTION performDrag");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800298 }
299 }
300
301 return true; // success!
302 }
303
304 public void reportDropResult(IWindow window, boolean consumed) {
305 IBinder token = window.asBinder();
306 if (WindowManagerService.DEBUG_DRAG) {
307 Slog.d(WindowManagerService.TAG, "Drop result=" + consumed + " reported by " + token);
308 }
309
310 synchronized (mService.mWindowMap) {
311 long ident = Binder.clearCallingIdentity();
312 try {
Christopher Tate05e9c652011-10-20 12:34:36 -0700313 if (mService.mDragState == null) {
314 // Most likely the drop recipient ANRed and we ended the drag
315 // out from under it. Log the issue and move on.
316 Slog.w(WindowManagerService.TAG, "Drop result given but no drag in progress");
317 return;
318 }
319
320 if (mService.mDragState.mToken != token) {
321 // We're in a drag, but the wrong window has responded.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800322 Slog.w(WindowManagerService.TAG, "Invalid drop-result claim by " + window);
323 throw new IllegalStateException("reportDropResult() by non-recipient");
324 }
325
326 // The right window has responded, even if it's no longer around,
327 // so be sure to halt the timeout even if the later WindowState
328 // lookup fails.
329 mService.mH.removeMessages(H.DRAG_END_TIMEOUT, window.asBinder());
330 WindowState callingWin = mService.windowForClientLocked(null, window, false);
331 if (callingWin == null) {
332 Slog.w(WindowManagerService.TAG, "Bad result-reporting window " + window);
333 return; // !!! TODO: throw here?
334 }
335
336 mService.mDragState.mDragResult = consumed;
337 mService.mDragState.endDragLw();
338 } finally {
339 Binder.restoreCallingIdentity(ident);
340 }
341 }
342 }
343
344 public void dragRecipientEntered(IWindow window) {
345 if (WindowManagerService.DEBUG_DRAG) {
346 Slog.d(WindowManagerService.TAG, "Drag into new candidate view @ " + window.asBinder());
347 }
348 }
349
350 public void dragRecipientExited(IWindow window) {
351 if (WindowManagerService.DEBUG_DRAG) {
352 Slog.d(WindowManagerService.TAG, "Drag from old candidate view @ " + window.asBinder());
353 }
354 }
355
356 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
357 synchronized(mService.mWindowMap) {
358 long ident = Binder.clearCallingIdentity();
359 try {
360 mService.setWindowWallpaperPositionLocked(
361 mService.windowForClientLocked(this, window, true),
362 x, y, xStep, yStep);
363 } finally {
364 Binder.restoreCallingIdentity(ident);
365 }
366 }
367 }
368
369 public void wallpaperOffsetsComplete(IBinder window) {
370 mService.wallpaperOffsetsComplete(window);
371 }
372
373 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
374 int z, Bundle extras, boolean sync) {
375 synchronized(mService.mWindowMap) {
376 long ident = Binder.clearCallingIdentity();
377 try {
378 return mService.sendWindowWallpaperCommandLocked(
379 mService.windowForClientLocked(this, window, true),
380 action, x, y, z, extras, sync);
381 } finally {
382 Binder.restoreCallingIdentity(ident);
383 }
384 }
385 }
386
387 public void wallpaperCommandComplete(IBinder window, Bundle result) {
388 mService.wallpaperCommandComplete(window, result);
389 }
390
391 void windowAddedLocked() {
392 if (mSurfaceSession == null) {
393 if (WindowManagerService.localLOGV) Slog.v(
394 WindowManagerService.TAG, "First window added to " + this + ", creating SurfaceSession");
395 mSurfaceSession = new SurfaceSession();
396 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
397 WindowManagerService.TAG, " NEW SURFACE SESSION " + mSurfaceSession);
398 mService.mSessions.add(this);
399 }
400 mNumWindow++;
401 }
402
403 void windowRemovedLocked() {
404 mNumWindow--;
405 killSessionLocked();
406 }
407
408 void killSessionLocked() {
409 if (mNumWindow <= 0 && mClientDead) {
410 mService.mSessions.remove(this);
411 if (mSurfaceSession != null) {
412 if (WindowManagerService.localLOGV) Slog.v(
413 WindowManagerService.TAG, "Last window removed from " + this
414 + ", destroying " + mSurfaceSession);
415 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
416 WindowManagerService.TAG, " KILL SURFACE SESSION " + mSurfaceSession);
417 try {
418 mSurfaceSession.kill();
419 } catch (Exception e) {
420 Slog.w(WindowManagerService.TAG, "Exception thrown when killing surface session "
421 + mSurfaceSession + " in session " + this
422 + ": " + e.toString());
423 }
424 mSurfaceSession = null;
425 }
426 }
427 }
428
429 void dump(PrintWriter pw, String prefix) {
430 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
431 pw.print(" mClientDead="); pw.print(mClientDead);
432 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
433 }
434
435 @Override
436 public String toString() {
437 return mStringName;
438 }
439}