blob: b9db17702a810c789962a5d6061780b53b812d2a [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
137 public int add(IWindow window, WindowManager.LayoutParams attrs,
138 int viewVisibility, Rect outContentInsets, InputChannel outInputChannel) {
139 return mService.addWindow(this, window, attrs, viewVisibility, outContentInsets,
140 outInputChannel);
141 }
142
143 public int addWithoutInputChannel(IWindow window, WindowManager.LayoutParams attrs,
144 int viewVisibility, Rect outContentInsets) {
145 return mService.addWindow(this, window, attrs, viewVisibility, outContentInsets, null);
146 }
147
148 public void remove(IWindow window) {
149 mService.removeWindow(this, window);
150 }
151
152 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
153 int requestedWidth, int requestedHeight, int viewFlags,
154 boolean insetsPending, Rect outFrame, Rect outContentInsets,
155 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
156 //Log.d(TAG, ">>>>>> ENTERED relayout from " + Binder.getCallingPid());
157 int res = mService.relayoutWindow(this, window, attrs,
158 requestedWidth, requestedHeight, viewFlags, insetsPending,
159 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
160 //Log.d(TAG, "<<<<<< EXITING relayout to " + Binder.getCallingPid());
161 return res;
162 }
163
164 public void setTransparentRegion(IWindow window, Region region) {
165 mService.setTransparentRegionWindow(this, window, region);
166 }
167
168 public void setInsets(IWindow window, int touchableInsets,
169 Rect contentInsets, Rect visibleInsets, Region touchableArea) {
170 mService.setInsetsWindow(this, window, touchableInsets, contentInsets,
171 visibleInsets, touchableArea);
172 }
173
174 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
175 mService.getWindowDisplayFrame(this, window, outDisplayFrame);
176 }
177
178 public void finishDrawing(IWindow window) {
179 if (WindowManagerService.localLOGV) Slog.v(
180 WindowManagerService.TAG, "IWindow finishDrawing called for " + window);
181 mService.finishDrawingWindow(this, window);
182 }
183
184 public void setInTouchMode(boolean mode) {
185 synchronized(mService.mWindowMap) {
186 mService.mInTouchMode = mode;
187 }
188 }
189
190 public boolean getInTouchMode() {
191 synchronized(mService.mWindowMap) {
192 return mService.mInTouchMode;
193 }
194 }
195
196 public boolean performHapticFeedback(IWindow window, int effectId,
197 boolean always) {
198 synchronized(mService.mWindowMap) {
199 long ident = Binder.clearCallingIdentity();
200 try {
201 return mService.mPolicy.performHapticFeedbackLw(
202 mService.windowForClientLocked(this, window, true),
203 effectId, always);
204 } finally {
205 Binder.restoreCallingIdentity(ident);
206 }
207 }
208 }
209
210 /* Drag/drop */
211 public IBinder prepareDrag(IWindow window, int flags,
212 int width, int height, Surface outSurface) {
213 return mService.prepareDragSurface(window, mSurfaceSession, flags,
214 width, height, outSurface);
215 }
216
217 public boolean performDrag(IWindow window, IBinder dragToken,
218 float touchX, float touchY, float thumbCenterX, float thumbCenterY,
219 ClipData data) {
220 if (WindowManagerService.DEBUG_DRAG) {
221 Slog.d(WindowManagerService.TAG, "perform drag: win=" + window + " data=" + data);
222 }
223
224 synchronized (mService.mWindowMap) {
225 if (mService.mDragState == null) {
226 Slog.w(WindowManagerService.TAG, "No drag prepared");
227 throw new IllegalStateException("performDrag() without prepareDrag()");
228 }
229
230 if (dragToken != mService.mDragState.mToken) {
231 Slog.w(WindowManagerService.TAG, "Performing mismatched drag");
232 throw new IllegalStateException("performDrag() does not match prepareDrag()");
233 }
234
235 WindowState callingWin = mService.windowForClientLocked(null, window, false);
236 if (callingWin == null) {
237 Slog.w(WindowManagerService.TAG, "Bad requesting window " + window);
238 return false; // !!! TODO: throw here?
239 }
240
241 // !!! TODO: if input is not still focused on the initiating window, fail
242 // the drag initiation (e.g. an alarm window popped up just as the application
243 // called performDrag()
244
245 mService.mH.removeMessages(H.DRAG_START_TIMEOUT, window.asBinder());
246
247 // !!! TODO: extract the current touch (x, y) in screen coordinates. That
248 // will let us eliminate the (touchX,touchY) parameters from the API.
249
250 // !!! FIXME: put all this heavy stuff onto the mH looper, as well as
251 // the actual drag event dispatch stuff in the dragstate
252
253 mService.mDragState.register();
254 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
255 if (!mService.mInputManager.transferTouchFocus(callingWin.mInputChannel,
256 mService.mDragState.mServerChannel)) {
257 Slog.e(WindowManagerService.TAG, "Unable to transfer touch focus");
258 mService.mDragState.unregister();
259 mService.mDragState = null;
260 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
261 return false;
262 }
263
264 mService.mDragState.mData = data;
265 mService.mDragState.mCurrentX = touchX;
266 mService.mDragState.mCurrentY = touchY;
267 mService.mDragState.broadcastDragStartedLw(touchX, touchY);
268
269 // remember the thumb offsets for later
270 mService.mDragState.mThumbOffsetX = thumbCenterX;
271 mService.mDragState.mThumbOffsetY = thumbCenterY;
272
273 // Make the surface visible at the proper location
274 final Surface surface = mService.mDragState.mSurface;
275 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, ">>> OPEN TRANSACTION performDrag");
276 Surface.openTransaction();
277 try {
278 surface.setPosition((int)(touchX - thumbCenterX),
279 (int)(touchY - thumbCenterY));
280 surface.setAlpha(.7071f);
281 surface.setLayer(mService.mDragState.getDragLayerLw());
282 surface.show();
283 } finally {
284 Surface.closeTransaction();
285 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, "<<< CLOSE TRANSACTION performDrag");
286 }
287 }
288
289 return true; // success!
290 }
291
292 public void reportDropResult(IWindow window, boolean consumed) {
293 IBinder token = window.asBinder();
294 if (WindowManagerService.DEBUG_DRAG) {
295 Slog.d(WindowManagerService.TAG, "Drop result=" + consumed + " reported by " + token);
296 }
297
298 synchronized (mService.mWindowMap) {
299 long ident = Binder.clearCallingIdentity();
300 try {
301 if (mService.mDragState == null || mService.mDragState.mToken != token) {
302 Slog.w(WindowManagerService.TAG, "Invalid drop-result claim by " + window);
303 throw new IllegalStateException("reportDropResult() by non-recipient");
304 }
305
306 // The right window has responded, even if it's no longer around,
307 // so be sure to halt the timeout even if the later WindowState
308 // lookup fails.
309 mService.mH.removeMessages(H.DRAG_END_TIMEOUT, window.asBinder());
310 WindowState callingWin = mService.windowForClientLocked(null, window, false);
311 if (callingWin == null) {
312 Slog.w(WindowManagerService.TAG, "Bad result-reporting window " + window);
313 return; // !!! TODO: throw here?
314 }
315
316 mService.mDragState.mDragResult = consumed;
317 mService.mDragState.endDragLw();
318 } finally {
319 Binder.restoreCallingIdentity(ident);
320 }
321 }
322 }
323
324 public void dragRecipientEntered(IWindow window) {
325 if (WindowManagerService.DEBUG_DRAG) {
326 Slog.d(WindowManagerService.TAG, "Drag into new candidate view @ " + window.asBinder());
327 }
328 }
329
330 public void dragRecipientExited(IWindow window) {
331 if (WindowManagerService.DEBUG_DRAG) {
332 Slog.d(WindowManagerService.TAG, "Drag from old candidate view @ " + window.asBinder());
333 }
334 }
335
336 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
337 synchronized(mService.mWindowMap) {
338 long ident = Binder.clearCallingIdentity();
339 try {
340 mService.setWindowWallpaperPositionLocked(
341 mService.windowForClientLocked(this, window, true),
342 x, y, xStep, yStep);
343 } finally {
344 Binder.restoreCallingIdentity(ident);
345 }
346 }
347 }
348
349 public void wallpaperOffsetsComplete(IBinder window) {
350 mService.wallpaperOffsetsComplete(window);
351 }
352
353 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
354 int z, Bundle extras, boolean sync) {
355 synchronized(mService.mWindowMap) {
356 long ident = Binder.clearCallingIdentity();
357 try {
358 return mService.sendWindowWallpaperCommandLocked(
359 mService.windowForClientLocked(this, window, true),
360 action, x, y, z, extras, sync);
361 } finally {
362 Binder.restoreCallingIdentity(ident);
363 }
364 }
365 }
366
367 public void wallpaperCommandComplete(IBinder window, Bundle result) {
368 mService.wallpaperCommandComplete(window, result);
369 }
370
371 void windowAddedLocked() {
372 if (mSurfaceSession == null) {
373 if (WindowManagerService.localLOGV) Slog.v(
374 WindowManagerService.TAG, "First window added to " + this + ", creating SurfaceSession");
375 mSurfaceSession = new SurfaceSession();
376 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
377 WindowManagerService.TAG, " NEW SURFACE SESSION " + mSurfaceSession);
378 mService.mSessions.add(this);
379 }
380 mNumWindow++;
381 }
382
383 void windowRemovedLocked() {
384 mNumWindow--;
385 killSessionLocked();
386 }
387
388 void killSessionLocked() {
389 if (mNumWindow <= 0 && mClientDead) {
390 mService.mSessions.remove(this);
391 if (mSurfaceSession != null) {
392 if (WindowManagerService.localLOGV) Slog.v(
393 WindowManagerService.TAG, "Last window removed from " + this
394 + ", destroying " + mSurfaceSession);
395 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
396 WindowManagerService.TAG, " KILL SURFACE SESSION " + mSurfaceSession);
397 try {
398 mSurfaceSession.kill();
399 } catch (Exception e) {
400 Slog.w(WindowManagerService.TAG, "Exception thrown when killing surface session "
401 + mSurfaceSession + " in session " + this
402 + ": " + e.toString());
403 }
404 mSurfaceSession = null;
405 }
406 }
407 }
408
409 void dump(PrintWriter pw, String prefix) {
410 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
411 pw.print(" mClientDead="); pw.print(mClientDead);
412 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
413 }
414
415 @Override
416 public String toString() {
417 return mStringName;
418 }
419}