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