blob: 9bad1532251571661818700fe3e15129d7ea9b40 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
27import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
28import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
29import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
30import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
31import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
32import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
33import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
34import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
35import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
36import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
37import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
38import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
39import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
41
42import com.android.internal.app.IBatteryStats;
43import com.android.internal.policy.PolicyManager;
44import com.android.internal.view.IInputContext;
45import com.android.internal.view.IInputMethodClient;
46import com.android.internal.view.IInputMethodManager;
47import com.android.server.KeyInputQueue.QueuedEvent;
48import com.android.server.am.BatteryStatsService;
49
50import android.Manifest;
51import android.app.ActivityManagerNative;
52import android.app.IActivityManager;
53import android.content.Context;
54import android.content.pm.ActivityInfo;
55import android.content.pm.PackageManager;
56import android.content.res.Configuration;
57import android.graphics.Matrix;
58import android.graphics.PixelFormat;
59import android.graphics.Rect;
60import android.graphics.Region;
61import android.os.BatteryStats;
62import android.os.Binder;
63import android.os.Debug;
64import android.os.Handler;
65import android.os.IBinder;
66import android.os.LocalPowerManager;
67import android.os.Looper;
68import android.os.Message;
69import android.os.Parcel;
70import android.os.ParcelFileDescriptor;
71import android.os.Power;
72import android.os.PowerManager;
73import android.os.Process;
74import android.os.RemoteException;
75import android.os.ServiceManager;
76import android.os.SystemClock;
77import android.os.SystemProperties;
78import android.os.TokenWatcher;
79import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070080import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.util.EventLog;
82import android.util.Log;
83import android.util.SparseIntArray;
84import android.view.Display;
85import android.view.Gravity;
86import android.view.IApplicationToken;
87import android.view.IOnKeyguardExitResult;
88import android.view.IRotationWatcher;
89import android.view.IWindow;
90import android.view.IWindowManager;
91import android.view.IWindowSession;
92import android.view.KeyEvent;
93import android.view.MotionEvent;
94import android.view.RawInputEvent;
95import android.view.Surface;
96import android.view.SurfaceSession;
97import android.view.View;
98import android.view.ViewTreeObserver;
99import android.view.WindowManager;
100import android.view.WindowManagerImpl;
101import android.view.WindowManagerPolicy;
102import android.view.WindowManager.LayoutParams;
103import android.view.animation.Animation;
104import android.view.animation.AnimationUtils;
105import android.view.animation.Transformation;
106
107import java.io.BufferedWriter;
108import java.io.File;
109import java.io.FileDescriptor;
110import java.io.IOException;
111import java.io.OutputStream;
112import java.io.OutputStreamWriter;
113import java.io.PrintWriter;
114import java.io.StringWriter;
115import java.net.Socket;
116import java.util.ArrayList;
117import java.util.HashMap;
118import java.util.HashSet;
119import java.util.Iterator;
120import java.util.List;
121
122/** {@hide} */
123public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor {
124 static final String TAG = "WindowManager";
125 static final boolean DEBUG = false;
126 static final boolean DEBUG_FOCUS = false;
127 static final boolean DEBUG_ANIM = false;
128 static final boolean DEBUG_LAYERS = false;
129 static final boolean DEBUG_INPUT = false;
130 static final boolean DEBUG_INPUT_METHOD = false;
131 static final boolean DEBUG_VISIBILITY = false;
132 static final boolean DEBUG_ORIENTATION = false;
133 static final boolean DEBUG_APP_TRANSITIONS = false;
134 static final boolean DEBUG_STARTING_WINDOW = false;
135 static final boolean DEBUG_REORDER = false;
136 static final boolean SHOW_TRANSACTIONS = false;
Romain Guy06882f82009-06-10 13:36:04 -0700137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final boolean PROFILE_ORIENTATION = false;
139 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700140 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 /** How long to wait for first key repeat, in milliseconds */
145 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 /** How long to wait for subsequent key repeats, in milliseconds */
148 static final int KEY_REPEAT_DELAY = 50;
149
150 /** How much to multiply the policy's type layer, to reserve room
151 * for multiple windows of the same type and Z-ordering adjustment
152 * with TYPE_LAYER_OFFSET. */
153 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
156 * or below others in the same layer. */
157 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /** How much to increment the layer for each window, to reserve room
160 * for effect surfaces between them.
161 */
162 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /** The maximum length we will accept for a loaded animation duration:
165 * this is 10 seconds.
166 */
167 static final int MAX_ANIMATION_DURATION = 10*1000;
168
169 /** Amount of time (in milliseconds) to animate the dim surface from one
170 * value to another, when no window animation is driving it.
171 */
172 static final int DEFAULT_DIM_DURATION = 200;
173
174 /** Adjustment to time to perform a dim, to make it more dramatic.
175 */
176 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700177
Dianne Hackborncfaef692009-06-15 14:24:44 -0700178 static final int INJECT_FAILED = 0;
179 static final int INJECT_SUCCEEDED = 1;
180 static final int INJECT_NO_PERMISSION = -1;
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 static final int UPDATE_FOCUS_NORMAL = 0;
183 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
184 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
185 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700186
Michael Chane96440f2009-05-06 10:27:36 -0700187 /** The minimum time between dispatching touch events. */
188 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
189
190 // Last touch event time
191 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700192
Michael Chane96440f2009-05-06 10:27:36 -0700193 // Last touch event type
194 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700195
Michael Chane96440f2009-05-06 10:27:36 -0700196 // Time to wait before calling useractivity again. This saves CPU usage
197 // when we get a flood of touch events.
198 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
199
200 // Last time we call user activity
201 long mLastUserActivityCallTime = 0;
202
Romain Guy06882f82009-06-10 13:36:04 -0700203 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700204 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700207 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
209 /**
210 * Condition waited on by {@link #reenableKeyguard} to know the call to
211 * the window policy has finished.
212 */
213 private boolean mWaitingUntilKeyguardReenabled = false;
214
215
216 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
217 new Handler(), "WindowManagerService.mKeyguardDisabled") {
218 public void acquired() {
219 mPolicy.enableKeyguard(false);
220 }
221 public void released() {
222 synchronized (mKeyguardDisabled) {
223 mPolicy.enableKeyguard(true);
224 mWaitingUntilKeyguardReenabled = false;
225 mKeyguardDisabled.notifyAll();
226 }
227 }
228 };
229
230 final Context mContext;
231
232 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
237
238 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
243 * All currently active sessions with clients.
244 */
245 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 /**
248 * Mapping from an IWindow IBinder to the server's Window object.
249 * This is also used as the lock for all of our state.
250 */
251 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
252
253 /**
254 * Mapping from a token IBinder to a WindowToken object.
255 */
256 final HashMap<IBinder, WindowToken> mTokenMap =
257 new HashMap<IBinder, WindowToken>();
258
259 /**
260 * The same tokens as mTokenMap, stored in a list for efficient iteration
261 * over them.
262 */
263 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * Window tokens that are in the process of exiting, but still
267 * on screen for animations.
268 */
269 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
270
271 /**
272 * Z-ordered (bottom-most first) list of all application tokens, for
273 * controlling the ordering of windows in different applications. This
274 * contains WindowToken objects.
275 */
276 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
277
278 /**
279 * Application tokens that are in the process of exiting, but still
280 * on screen for animations.
281 */
282 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
283
284 /**
285 * List of window tokens that have finished starting their application,
286 * and now need to have the policy remove their windows.
287 */
288 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
289
290 /**
291 * Z-ordered (bottom-most first) list of all Window objects.
292 */
293 final ArrayList mWindows = new ArrayList();
294
295 /**
296 * Windows that are being resized. Used so we can tell the client about
297 * the resize after closing the transaction in which we resized the
298 * underlying surface.
299 */
300 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
301
302 /**
303 * Windows whose animations have ended and now must be removed.
304 */
305 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
306
307 /**
308 * Windows whose surface should be destroyed.
309 */
310 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
311
312 /**
313 * Windows that have lost input focus and are waiting for the new
314 * focus window to be displayed before they are told about this.
315 */
316 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
317
318 /**
319 * This is set when we have run out of memory, and will either be an empty
320 * list or contain windows that need to be force removed.
321 */
322 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 SurfaceSession mFxSession;
327 Surface mDimSurface;
328 boolean mDimShown;
329 float mDimCurrentAlpha;
330 float mDimTargetAlpha;
331 float mDimDeltaPerMs;
332 long mLastDimAnimTime;
333 Surface mBlurSurface;
334 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 final float[] mTmpFloats = new float[9];
339
340 boolean mSafeMode;
341 boolean mDisplayEnabled = false;
342 boolean mSystemBooted = false;
343 int mRotation = 0;
344 int mRequestedRotation = 0;
345 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700346 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 ArrayList<IRotationWatcher> mRotationWatchers
348 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 boolean mLayoutNeeded = true;
351 boolean mAnimationPending = false;
352 boolean mDisplayFrozen = false;
353 boolean mWindowsFreezingScreen = false;
354 long mFreezeGcPending = 0;
355 int mAppsFreezingScreen = 0;
356
357 // This is held as long as we have the screen frozen, to give us time to
358 // perform a rotation animation when turning off shows the lock screen which
359 // changes the orientation.
360 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 // State management of app transitions. When we are preparing for a
363 // transition, mNextAppTransition will be the kind of transition to
364 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
365 // mOpeningApps and mClosingApps are the lists of tokens that will be
366 // made visible or hidden at the next transition.
367 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
368 boolean mAppTransitionReady = false;
369 boolean mAppTransitionTimeout = false;
370 boolean mStartingIconInTransition = false;
371 boolean mSkipAppTransitionAnimation = false;
372 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
373 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 //flag to detect fat touch events
376 boolean mFatTouch = false;
377 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 H mH = new H();
380
381 WindowState mCurrentFocus = null;
382 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 // This just indicates the window the input method is on top of, not
385 // necessarily the window its input is going to.
386 WindowState mInputMethodTarget = null;
387 WindowState mUpcomingInputMethodTarget = null;
388 boolean mInputMethodTargetWaitingAnim;
389 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 WindowState mInputMethodWindow = null;
392 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
393
394 AppWindowToken mFocusedApp = null;
395
396 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 float mWindowAnimationScale = 1.0f;
399 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 final KeyWaiter mKeyWaiter = new KeyWaiter();
402 final KeyQ mQueue;
403 final InputDispatcherThread mInputThread;
404
405 // Who is holding the screen on.
406 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Whether the UI is currently running in touch mode (not showing
410 * navigational focus because the user is directly pressing the screen).
411 */
412 boolean mInTouchMode = false;
413
414 private ViewServer mViewServer;
415
416 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700417
Dianne Hackbornc485a602009-03-24 22:39:49 -0700418 final Configuration mTempConfiguration = new Configuration();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700419 int screenLayout = Configuration.SCREENLAYOUT_UNDEFINED;
420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 public static WindowManagerService main(Context context,
422 PowerManagerService pm, boolean haveInputMethods) {
423 WMThread thr = new WMThread(context, pm, haveInputMethods);
424 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 synchronized (thr) {
427 while (thr.mService == null) {
428 try {
429 thr.wait();
430 } catch (InterruptedException e) {
431 }
432 }
433 }
Romain Guy06882f82009-06-10 13:36:04 -0700434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 return thr.mService;
436 }
Romain Guy06882f82009-06-10 13:36:04 -0700437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 static class WMThread extends Thread {
439 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 private final Context mContext;
442 private final PowerManagerService mPM;
443 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 public WMThread(Context context, PowerManagerService pm,
446 boolean haveInputMethods) {
447 super("WindowManager");
448 mContext = context;
449 mPM = pm;
450 mHaveInputMethods = haveInputMethods;
451 }
Romain Guy06882f82009-06-10 13:36:04 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 public void run() {
454 Looper.prepare();
455 WindowManagerService s = new WindowManagerService(mContext, mPM,
456 mHaveInputMethods);
457 android.os.Process.setThreadPriority(
458 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 synchronized (this) {
461 mService = s;
462 notifyAll();
463 }
Romain Guy06882f82009-06-10 13:36:04 -0700464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 Looper.loop();
466 }
467 }
468
469 static class PolicyThread extends Thread {
470 private final WindowManagerPolicy mPolicy;
471 private final WindowManagerService mService;
472 private final Context mContext;
473 private final PowerManagerService mPM;
474 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 public PolicyThread(WindowManagerPolicy policy,
477 WindowManagerService service, Context context,
478 PowerManagerService pm) {
479 super("WindowManagerPolicy");
480 mPolicy = policy;
481 mService = service;
482 mContext = context;
483 mPM = pm;
484 }
Romain Guy06882f82009-06-10 13:36:04 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public void run() {
487 Looper.prepare();
488 //Looper.myLooper().setMessageLogging(new LogPrinter(
489 // Log.VERBOSE, "WindowManagerPolicy"));
490 android.os.Process.setThreadPriority(
491 android.os.Process.THREAD_PRIORITY_FOREGROUND);
492 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 synchronized (this) {
495 mRunning = true;
496 notifyAll();
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 Looper.loop();
500 }
501 }
502
503 private WindowManagerService(Context context, PowerManagerService pm,
504 boolean haveInputMethods) {
505 mContext = context;
506 mHaveInputMethods = haveInputMethods;
507 mLimitedAlphaCompositing = context.getResources().getBoolean(
508 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 mPowerManager = pm;
511 mPowerManager.setPolicy(mPolicy);
512 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
513 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
514 "SCREEN_FROZEN");
515 mScreenFrozenLock.setReferenceCounted(false);
516
517 mActivityManager = ActivityManagerNative.getDefault();
518 mBatteryStats = BatteryStatsService.getService();
519
520 // Get persisted window scale setting
521 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
522 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
523 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
524 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 mQueue = new KeyQ();
527
528 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
531 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 synchronized (thr) {
534 while (!thr.mRunning) {
535 try {
536 thr.wait();
537 } catch (InterruptedException e) {
538 }
539 }
540 }
Romain Guy06882f82009-06-10 13:36:04 -0700541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 // Add ourself to the Watchdog monitors.
545 Watchdog.getInstance().addMonitor(this);
546 }
547
548 @Override
549 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
550 throws RemoteException {
551 try {
552 return super.onTransact(code, data, reply, flags);
553 } catch (RuntimeException e) {
554 // The window manager only throws security exceptions, so let's
555 // log all others.
556 if (!(e instanceof SecurityException)) {
557 Log.e(TAG, "Window Manager Crash", e);
558 }
559 throw e;
560 }
561 }
562
563 private void placeWindowAfter(Object pos, WindowState window) {
564 final int i = mWindows.indexOf(pos);
565 if (localLOGV || DEBUG_FOCUS) Log.v(
566 TAG, "Adding window " + window + " at "
567 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
568 mWindows.add(i+1, window);
569 }
570
571 private void placeWindowBefore(Object pos, WindowState window) {
572 final int i = mWindows.indexOf(pos);
573 if (localLOGV || DEBUG_FOCUS) Log.v(
574 TAG, "Adding window " + window + " at "
575 + i + " of " + mWindows.size() + " (before " + pos + ")");
576 mWindows.add(i, window);
577 }
578
579 //This method finds out the index of a window that has the same app token as
580 //win. used for z ordering the windows in mWindows
581 private int findIdxBasedOnAppTokens(WindowState win) {
582 //use a local variable to cache mWindows
583 ArrayList localmWindows = mWindows;
584 int jmax = localmWindows.size();
585 if(jmax == 0) {
586 return -1;
587 }
588 for(int j = (jmax-1); j >= 0; j--) {
589 WindowState wentry = (WindowState)localmWindows.get(j);
590 if(wentry.mAppToken == win.mAppToken) {
591 return j;
592 }
593 }
594 return -1;
595 }
Romain Guy06882f82009-06-10 13:36:04 -0700596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
598 final IWindow client = win.mClient;
599 final WindowToken token = win.mToken;
600 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 final int N = localmWindows.size();
603 final WindowState attached = win.mAttachedWindow;
604 int i;
605 if (attached == null) {
606 int tokenWindowsPos = token.windows.size();
607 if (token.appWindowToken != null) {
608 int index = tokenWindowsPos-1;
609 if (index >= 0) {
610 // If this application has existing windows, we
611 // simply place the new window on top of them... but
612 // keep the starting window on top.
613 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
614 // Base windows go behind everything else.
615 placeWindowBefore(token.windows.get(0), win);
616 tokenWindowsPos = 0;
617 } else {
618 AppWindowToken atoken = win.mAppToken;
619 if (atoken != null &&
620 token.windows.get(index) == atoken.startingWindow) {
621 placeWindowBefore(token.windows.get(index), win);
622 tokenWindowsPos--;
623 } else {
624 int newIdx = findIdxBasedOnAppTokens(win);
625 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700626 //there is a window above this one associated with the same
627 //apptoken note that the window could be a floating window
628 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 //windows associated with this token.
630 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633 }
634 } else {
635 if (localLOGV) Log.v(
636 TAG, "Figuring out where to add app window "
637 + client.asBinder() + " (token=" + token + ")");
638 // Figure out where the window should go, based on the
639 // order of applications.
640 final int NA = mAppTokens.size();
641 Object pos = null;
642 for (i=NA-1; i>=0; i--) {
643 AppWindowToken t = mAppTokens.get(i);
644 if (t == token) {
645 i--;
646 break;
647 }
648 if (t.windows.size() > 0) {
649 pos = t.windows.get(0);
650 }
651 }
652 // We now know the index into the apps. If we found
653 // an app window above, that gives us the position; else
654 // we need to look some more.
655 if (pos != null) {
656 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700657 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 mTokenMap.get(((WindowState)pos).mClient.asBinder());
659 if (atoken != null) {
660 final int NC = atoken.windows.size();
661 if (NC > 0) {
662 WindowState bottom = atoken.windows.get(0);
663 if (bottom.mSubLayer < 0) {
664 pos = bottom;
665 }
666 }
667 }
668 placeWindowBefore(pos, win);
669 } else {
670 while (i >= 0) {
671 AppWindowToken t = mAppTokens.get(i);
672 final int NW = t.windows.size();
673 if (NW > 0) {
674 pos = t.windows.get(NW-1);
675 break;
676 }
677 i--;
678 }
679 if (pos != null) {
680 // Move in front of any windows attached to this
681 // one.
682 WindowToken atoken =
683 mTokenMap.get(((WindowState)pos).mClient.asBinder());
684 if (atoken != null) {
685 final int NC = atoken.windows.size();
686 if (NC > 0) {
687 WindowState top = atoken.windows.get(NC-1);
688 if (top.mSubLayer >= 0) {
689 pos = top;
690 }
691 }
692 }
693 placeWindowAfter(pos, win);
694 } else {
695 // Just search for the start of this layer.
696 final int myLayer = win.mBaseLayer;
697 for (i=0; i<N; i++) {
698 WindowState w = (WindowState)localmWindows.get(i);
699 if (w.mBaseLayer > myLayer) {
700 break;
701 }
702 }
703 if (localLOGV || DEBUG_FOCUS) Log.v(
704 TAG, "Adding window " + win + " at "
705 + i + " of " + N);
706 localmWindows.add(i, win);
707 }
708 }
709 }
710 } else {
711 // Figure out where window should go, based on layer.
712 final int myLayer = win.mBaseLayer;
713 for (i=N-1; i>=0; i--) {
714 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
715 i++;
716 break;
717 }
718 }
719 if (i < 0) i = 0;
720 if (localLOGV || DEBUG_FOCUS) Log.v(
721 TAG, "Adding window " + win + " at "
722 + i + " of " + N);
723 localmWindows.add(i, win);
724 }
725 if (addToToken) {
726 token.windows.add(tokenWindowsPos, win);
727 }
728
729 } else {
730 // Figure out this window's ordering relative to the window
731 // it is attached to.
732 final int NA = token.windows.size();
733 final int sublayer = win.mSubLayer;
734 int largestSublayer = Integer.MIN_VALUE;
735 WindowState windowWithLargestSublayer = null;
736 for (i=0; i<NA; i++) {
737 WindowState w = token.windows.get(i);
738 final int wSublayer = w.mSubLayer;
739 if (wSublayer >= largestSublayer) {
740 largestSublayer = wSublayer;
741 windowWithLargestSublayer = w;
742 }
743 if (sublayer < 0) {
744 // For negative sublayers, we go below all windows
745 // in the same sublayer.
746 if (wSublayer >= sublayer) {
747 if (addToToken) {
748 token.windows.add(i, win);
749 }
750 placeWindowBefore(
751 wSublayer >= 0 ? attached : w, win);
752 break;
753 }
754 } else {
755 // For positive sublayers, we go above all windows
756 // in the same sublayer.
757 if (wSublayer > sublayer) {
758 if (addToToken) {
759 token.windows.add(i, win);
760 }
761 placeWindowBefore(w, win);
762 break;
763 }
764 }
765 }
766 if (i >= NA) {
767 if (addToToken) {
768 token.windows.add(win);
769 }
770 if (sublayer < 0) {
771 placeWindowBefore(attached, win);
772 } else {
773 placeWindowAfter(largestSublayer >= 0
774 ? windowWithLargestSublayer
775 : attached,
776 win);
777 }
778 }
779 }
Romain Guy06882f82009-06-10 13:36:04 -0700780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 if (win.mAppToken != null && addToToken) {
782 win.mAppToken.allAppWindows.add(win);
783 }
784 }
Romain Guy06882f82009-06-10 13:36:04 -0700785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 static boolean canBeImeTarget(WindowState w) {
787 final int fl = w.mAttrs.flags
788 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
789 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
790 return w.isVisibleOrAdding();
791 }
792 return false;
793 }
Romain Guy06882f82009-06-10 13:36:04 -0700794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
796 final ArrayList localmWindows = mWindows;
797 final int N = localmWindows.size();
798 WindowState w = null;
799 int i = N;
800 while (i > 0) {
801 i--;
802 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
805 // + Integer.toHexString(w.mAttrs.flags));
806 if (canBeImeTarget(w)) {
807 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 // Yet more tricksyness! If this window is a "starting"
810 // window, we do actually want to be on top of it, but
811 // it is not -really- where input will go. So if the caller
812 // is not actually looking to move the IME, look down below
813 // for a real window to target...
814 if (!willMove
815 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
816 && i > 0) {
817 WindowState wb = (WindowState)localmWindows.get(i-1);
818 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
819 i--;
820 w = wb;
821 }
822 }
823 break;
824 }
825 }
Romain Guy06882f82009-06-10 13:36:04 -0700826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
830 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 if (willMove && w != null) {
833 final WindowState curTarget = mInputMethodTarget;
834 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 // Now some fun for dealing with window animations that
837 // modify the Z order. We need to look at all windows below
838 // the current target that are in this app, finding the highest
839 // visible one in layering.
840 AppWindowToken token = curTarget.mAppToken;
841 WindowState highestTarget = null;
842 int highestPos = 0;
843 if (token.animating || token.animation != null) {
844 int pos = 0;
845 pos = localmWindows.indexOf(curTarget);
846 while (pos >= 0) {
847 WindowState win = (WindowState)localmWindows.get(pos);
848 if (win.mAppToken != token) {
849 break;
850 }
851 if (!win.mRemoved) {
852 if (highestTarget == null || win.mAnimLayer >
853 highestTarget.mAnimLayer) {
854 highestTarget = win;
855 highestPos = pos;
856 }
857 }
858 pos--;
859 }
860 }
Romain Guy06882f82009-06-10 13:36:04 -0700861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700863 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 + mNextAppTransition + " " + highestTarget
865 + " animating=" + highestTarget.isAnimating()
866 + " layer=" + highestTarget.mAnimLayer
867 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
870 // If we are currently setting up for an animation,
871 // hold everything until we can find out what will happen.
872 mInputMethodTargetWaitingAnim = true;
873 mInputMethodTarget = highestTarget;
874 return highestPos + 1;
875 } else if (highestTarget.isAnimating() &&
876 highestTarget.mAnimLayer > w.mAnimLayer) {
877 // If the window we are currently targeting is involved
878 // with an animation, and it is on top of the next target
879 // we will be over, then hold off on moving until
880 // that is done.
881 mInputMethodTarget = highestTarget;
882 return highestPos + 1;
883 }
884 }
885 }
886 }
Romain Guy06882f82009-06-10 13:36:04 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 //Log.i(TAG, "Placing input method @" + (i+1));
889 if (w != null) {
890 if (willMove) {
891 RuntimeException e = new RuntimeException();
892 e.fillInStackTrace();
893 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
894 + mInputMethodTarget + " to " + w, e);
895 mInputMethodTarget = w;
896 if (w.mAppToken != null) {
897 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
898 } else {
899 setInputMethodAnimLayerAdjustment(0);
900 }
901 }
902 return i+1;
903 }
904 if (willMove) {
905 RuntimeException e = new RuntimeException();
906 e.fillInStackTrace();
907 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
908 + mInputMethodTarget + " to null", e);
909 mInputMethodTarget = null;
910 setInputMethodAnimLayerAdjustment(0);
911 }
912 return -1;
913 }
Romain Guy06882f82009-06-10 13:36:04 -0700914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 void addInputMethodWindowToListLocked(WindowState win) {
916 int pos = findDesiredInputMethodWindowIndexLocked(true);
917 if (pos >= 0) {
918 win.mTargetAppToken = mInputMethodTarget.mAppToken;
919 mWindows.add(pos, win);
920 moveInputMethodDialogsLocked(pos+1);
921 return;
922 }
923 win.mTargetAppToken = null;
924 addWindowToListInOrderLocked(win, true);
925 moveInputMethodDialogsLocked(pos);
926 }
Romain Guy06882f82009-06-10 13:36:04 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 void setInputMethodAnimLayerAdjustment(int adj) {
929 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
930 mInputMethodAnimLayerAdjustment = adj;
931 WindowState imw = mInputMethodWindow;
932 if (imw != null) {
933 imw.mAnimLayer = imw.mLayer + adj;
934 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
935 + " anim layer: " + imw.mAnimLayer);
936 int wi = imw.mChildWindows.size();
937 while (wi > 0) {
938 wi--;
939 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
940 cw.mAnimLayer = cw.mLayer + adj;
941 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
942 + " anim layer: " + cw.mAnimLayer);
943 }
944 }
945 int di = mInputMethodDialogs.size();
946 while (di > 0) {
947 di --;
948 imw = mInputMethodDialogs.get(di);
949 imw.mAnimLayer = imw.mLayer + adj;
950 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
951 + " anim layer: " + imw.mAnimLayer);
952 }
953 }
Romain Guy06882f82009-06-10 13:36:04 -0700954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
956 int wpos = mWindows.indexOf(win);
957 if (wpos >= 0) {
958 if (wpos < interestingPos) interestingPos--;
959 mWindows.remove(wpos);
960 int NC = win.mChildWindows.size();
961 while (NC > 0) {
962 NC--;
963 WindowState cw = (WindowState)win.mChildWindows.get(NC);
964 int cpos = mWindows.indexOf(cw);
965 if (cpos >= 0) {
966 if (cpos < interestingPos) interestingPos--;
967 mWindows.remove(cpos);
968 }
969 }
970 }
971 return interestingPos;
972 }
Romain Guy06882f82009-06-10 13:36:04 -0700973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 private void reAddWindowToListInOrderLocked(WindowState win) {
975 addWindowToListInOrderLocked(win, false);
976 // This is a hack to get all of the child windows added as well
977 // at the right position. Child windows should be rare and
978 // this case should be rare, so it shouldn't be that big a deal.
979 int wpos = mWindows.indexOf(win);
980 if (wpos >= 0) {
981 mWindows.remove(wpos);
982 reAddWindowLocked(wpos, win);
983 }
984 }
Romain Guy06882f82009-06-10 13:36:04 -0700985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 void logWindowList(String prefix) {
987 int N = mWindows.size();
988 while (N > 0) {
989 N--;
990 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
991 }
992 }
Romain Guy06882f82009-06-10 13:36:04 -0700993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 void moveInputMethodDialogsLocked(int pos) {
995 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -0700996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 final int N = dialogs.size();
998 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
999 for (int i=0; i<N; i++) {
1000 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1001 }
1002 if (DEBUG_INPUT_METHOD) {
1003 Log.v(TAG, "Window list w/pos=" + pos);
1004 logWindowList(" ");
1005 }
Romain Guy06882f82009-06-10 13:36:04 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (pos >= 0) {
1008 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1009 if (pos < mWindows.size()) {
1010 WindowState wp = (WindowState)mWindows.get(pos);
1011 if (wp == mInputMethodWindow) {
1012 pos++;
1013 }
1014 }
1015 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1016 for (int i=0; i<N; i++) {
1017 WindowState win = dialogs.get(i);
1018 win.mTargetAppToken = targetAppToken;
1019 pos = reAddWindowLocked(pos, win);
1020 }
1021 if (DEBUG_INPUT_METHOD) {
1022 Log.v(TAG, "Final window list:");
1023 logWindowList(" ");
1024 }
1025 return;
1026 }
1027 for (int i=0; i<N; i++) {
1028 WindowState win = dialogs.get(i);
1029 win.mTargetAppToken = null;
1030 reAddWindowToListInOrderLocked(win);
1031 if (DEBUG_INPUT_METHOD) {
1032 Log.v(TAG, "No IM target, final list:");
1033 logWindowList(" ");
1034 }
1035 }
1036 }
Romain Guy06882f82009-06-10 13:36:04 -07001037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1039 final WindowState imWin = mInputMethodWindow;
1040 final int DN = mInputMethodDialogs.size();
1041 if (imWin == null && DN == 0) {
1042 return false;
1043 }
Romain Guy06882f82009-06-10 13:36:04 -07001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1046 if (imPos >= 0) {
1047 // In this case, the input method windows are to be placed
1048 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 // First check to see if the input method windows are already
1051 // located here, and contiguous.
1052 final int N = mWindows.size();
1053 WindowState firstImWin = imPos < N
1054 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 // Figure out the actual input method window that should be
1057 // at the bottom of their stack.
1058 WindowState baseImWin = imWin != null
1059 ? imWin : mInputMethodDialogs.get(0);
1060 if (baseImWin.mChildWindows.size() > 0) {
1061 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1062 if (cw.mSubLayer < 0) baseImWin = cw;
1063 }
Romain Guy06882f82009-06-10 13:36:04 -07001064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 if (firstImWin == baseImWin) {
1066 // The windows haven't moved... but are they still contiguous?
1067 // First find the top IM window.
1068 int pos = imPos+1;
1069 while (pos < N) {
1070 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1071 break;
1072 }
1073 pos++;
1074 }
1075 pos++;
1076 // Now there should be no more input method windows above.
1077 while (pos < N) {
1078 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1079 break;
1080 }
1081 pos++;
1082 }
1083 if (pos >= N) {
1084 // All is good!
1085 return false;
1086 }
1087 }
Romain Guy06882f82009-06-10 13:36:04 -07001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 if (imWin != null) {
1090 if (DEBUG_INPUT_METHOD) {
1091 Log.v(TAG, "Moving IM from " + imPos);
1092 logWindowList(" ");
1093 }
1094 imPos = tmpRemoveWindowLocked(imPos, imWin);
1095 if (DEBUG_INPUT_METHOD) {
1096 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1097 logWindowList(" ");
1098 }
1099 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1100 reAddWindowLocked(imPos, imWin);
1101 if (DEBUG_INPUT_METHOD) {
1102 Log.v(TAG, "List after moving IM to " + imPos + ":");
1103 logWindowList(" ");
1104 }
1105 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1106 } else {
1107 moveInputMethodDialogsLocked(imPos);
1108 }
Romain Guy06882f82009-06-10 13:36:04 -07001109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 } else {
1111 // In this case, the input method windows go in a fixed layer,
1112 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 if (imWin != null) {
1115 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1116 tmpRemoveWindowLocked(0, imWin);
1117 imWin.mTargetAppToken = null;
1118 reAddWindowToListInOrderLocked(imWin);
1119 if (DEBUG_INPUT_METHOD) {
1120 Log.v(TAG, "List with no IM target:");
1121 logWindowList(" ");
1122 }
1123 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1124 } else {
1125 moveInputMethodDialogsLocked(-1);;
1126 }
Romain Guy06882f82009-06-10 13:36:04 -07001127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
Romain Guy06882f82009-06-10 13:36:04 -07001129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 if (needAssignLayers) {
1131 assignLayersLocked();
1132 }
Romain Guy06882f82009-06-10 13:36:04 -07001133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 return true;
1135 }
Romain Guy06882f82009-06-10 13:36:04 -07001136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 void adjustInputMethodDialogsLocked() {
1138 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1139 }
Romain Guy06882f82009-06-10 13:36:04 -07001140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public int addWindow(Session session, IWindow client,
1142 WindowManager.LayoutParams attrs, int viewVisibility,
1143 Rect outContentInsets) {
1144 int res = mPolicy.checkAddPermission(attrs);
1145 if (res != WindowManagerImpl.ADD_OKAY) {
1146 return res;
1147 }
Romain Guy06882f82009-06-10 13:36:04 -07001148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 boolean reportNewConfig = false;
1150 WindowState attachedWindow = null;
1151 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 synchronized(mWindowMap) {
1154 // Instantiating a Display requires talking with the simulator,
1155 // so don't do it until we know the system is mostly up and
1156 // running.
1157 if (mDisplay == null) {
1158 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1159 mDisplay = wm.getDefaultDisplay();
1160 mQueue.setDisplay(mDisplay);
1161 reportNewConfig = true;
1162 }
Romain Guy06882f82009-06-10 13:36:04 -07001163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 if (mWindowMap.containsKey(client.asBinder())) {
1165 Log.w(TAG, "Window " + client + " is already added");
1166 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1167 }
1168
1169 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001170 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 if (attachedWindow == null) {
1172 Log.w(TAG, "Attempted to add window with token that is not a window: "
1173 + attrs.token + ". Aborting.");
1174 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1175 }
1176 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1177 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1178 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1179 + attrs.token + ". Aborting.");
1180 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1181 }
1182 }
1183
1184 boolean addToken = false;
1185 WindowToken token = mTokenMap.get(attrs.token);
1186 if (token == null) {
1187 if (attrs.type >= FIRST_APPLICATION_WINDOW
1188 && attrs.type <= LAST_APPLICATION_WINDOW) {
1189 Log.w(TAG, "Attempted to add application window with unknown token "
1190 + attrs.token + ". Aborting.");
1191 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1192 }
1193 if (attrs.type == TYPE_INPUT_METHOD) {
1194 Log.w(TAG, "Attempted to add input method window with unknown token "
1195 + attrs.token + ". Aborting.");
1196 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1197 }
1198 token = new WindowToken(attrs.token, -1, false);
1199 addToken = true;
1200 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1201 && attrs.type <= LAST_APPLICATION_WINDOW) {
1202 AppWindowToken atoken = token.appWindowToken;
1203 if (atoken == null) {
1204 Log.w(TAG, "Attempted to add window with non-application token "
1205 + token + ". Aborting.");
1206 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1207 } else if (atoken.removed) {
1208 Log.w(TAG, "Attempted to add window with exiting application token "
1209 + token + ". Aborting.");
1210 return WindowManagerImpl.ADD_APP_EXITING;
1211 }
1212 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1213 // No need for this guy!
1214 if (localLOGV) Log.v(
1215 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1216 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1217 }
1218 } else if (attrs.type == TYPE_INPUT_METHOD) {
1219 if (token.windowType != TYPE_INPUT_METHOD) {
1220 Log.w(TAG, "Attempted to add input method window with bad token "
1221 + attrs.token + ". Aborting.");
1222 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1223 }
1224 }
1225
1226 win = new WindowState(session, client, token,
1227 attachedWindow, attrs, viewVisibility);
1228 if (win.mDeathRecipient == null) {
1229 // Client has apparently died, so there is no reason to
1230 // continue.
1231 Log.w(TAG, "Adding window client " + client.asBinder()
1232 + " that is dead, aborting.");
1233 return WindowManagerImpl.ADD_APP_EXITING;
1234 }
1235
1236 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 res = mPolicy.prepareAddWindowLw(win, attrs);
1239 if (res != WindowManagerImpl.ADD_OKAY) {
1240 return res;
1241 }
1242
1243 // From now on, no exceptions or errors allowed!
1244
1245 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 if (addToken) {
1250 mTokenMap.put(attrs.token, token);
1251 mTokenList.add(token);
1252 }
1253 win.attach();
1254 mWindowMap.put(client.asBinder(), win);
1255
1256 if (attrs.type == TYPE_APPLICATION_STARTING &&
1257 token.appWindowToken != null) {
1258 token.appWindowToken.startingWindow = win;
1259 }
1260
1261 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 if (attrs.type == TYPE_INPUT_METHOD) {
1264 mInputMethodWindow = win;
1265 addInputMethodWindowToListLocked(win);
1266 imMayMove = false;
1267 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1268 mInputMethodDialogs.add(win);
1269 addWindowToListInOrderLocked(win, true);
1270 adjustInputMethodDialogsLocked();
1271 imMayMove = false;
1272 } else {
1273 addWindowToListInOrderLocked(win, true);
1274 }
Romain Guy06882f82009-06-10 13:36:04 -07001275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 if (mInTouchMode) {
1281 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1282 }
1283 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1284 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1285 }
Romain Guy06882f82009-06-10 13:36:04 -07001286
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001287 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001289 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1290 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 imMayMove = false;
1292 }
1293 }
Romain Guy06882f82009-06-10 13:36:04 -07001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001296 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
Romain Guy06882f82009-06-10 13:36:04 -07001298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 assignLayersLocked();
1300 // Don't do layout here, the window must call
1301 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 //dump();
1304
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001305 if (focusChanged) {
1306 if (mCurrentFocus != null) {
1307 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1308 }
1309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 if (localLOGV) Log.v(
1311 TAG, "New client " + client.asBinder()
1312 + ": window=" + win);
1313 }
1314
1315 // sendNewConfiguration() checks caller permissions so we must call it with
1316 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1317 // identity anyway, so it's safe to just clear & restore around this whole
1318 // block.
1319 final long origId = Binder.clearCallingIdentity();
1320 if (reportNewConfig) {
1321 sendNewConfiguration();
1322 } else {
1323 // Update Orientation after adding a window, only if the window needs to be
1324 // displayed right away
1325 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001326 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 sendNewConfiguration();
1328 }
1329 }
1330 }
1331 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 return res;
1334 }
Romain Guy06882f82009-06-10 13:36:04 -07001335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 public void removeWindow(Session session, IWindow client) {
1337 synchronized(mWindowMap) {
1338 WindowState win = windowForClientLocked(session, client);
1339 if (win == null) {
1340 return;
1341 }
1342 removeWindowLocked(session, win);
1343 }
1344 }
Romain Guy06882f82009-06-10 13:36:04 -07001345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 public void removeWindowLocked(Session session, WindowState win) {
1347
1348 if (localLOGV || DEBUG_FOCUS) Log.v(
1349 TAG, "Remove " + win + " client="
1350 + Integer.toHexString(System.identityHashCode(
1351 win.mClient.asBinder()))
1352 + ", surface=" + win.mSurface);
1353
1354 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 if (DEBUG_APP_TRANSITIONS) Log.v(
1357 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1358 + " mExiting=" + win.mExiting
1359 + " isAnimating=" + win.isAnimating()
1360 + " app-animation="
1361 + (win.mAppToken != null ? win.mAppToken.animation : null)
1362 + " inPendingTransaction="
1363 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1364 + " mDisplayFrozen=" + mDisplayFrozen);
1365 // Visibility of the removed window. Will be used later to update orientation later on.
1366 boolean wasVisible = false;
1367 // First, see if we need to run an animation. If we do, we have
1368 // to hold off on removing the window until the animation is done.
1369 // If the display is frozen, just remove immediately, since the
1370 // animation wouldn't be seen.
1371 if (win.mSurface != null && !mDisplayFrozen) {
1372 // If we are not currently running the exit animation, we
1373 // need to see about starting one.
1374 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1377 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1378 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1379 }
1380 // Try starting an animation.
1381 if (applyAnimationLocked(win, transit, false)) {
1382 win.mExiting = true;
1383 }
1384 }
1385 if (win.mExiting || win.isAnimating()) {
1386 // The exit animation is running... wait for it!
1387 //Log.i(TAG, "*** Running exit animation...");
1388 win.mExiting = true;
1389 win.mRemoveOnExit = true;
1390 mLayoutNeeded = true;
1391 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1392 performLayoutAndPlaceSurfacesLocked();
1393 if (win.mAppToken != null) {
1394 win.mAppToken.updateReportedVisibilityLocked();
1395 }
1396 //dump();
1397 Binder.restoreCallingIdentity(origId);
1398 return;
1399 }
1400 }
1401
1402 removeWindowInnerLocked(session, win);
1403 // Removing a visible window will effect the computed orientation
1404 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001405 if (wasVisible && computeForcedAppOrientationLocked()
1406 != mForcedAppOrientation) {
1407 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1410 Binder.restoreCallingIdentity(origId);
1411 }
Romain Guy06882f82009-06-10 13:36:04 -07001412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 private void removeWindowInnerLocked(Session session, WindowState win) {
1414 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1415 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 if (mInputMethodTarget == win) {
1420 moveInputMethodWindowsIfNeededLocked(false);
1421 }
Romain Guy06882f82009-06-10 13:36:04 -07001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 mPolicy.removeWindowLw(win);
1424 win.removeLocked();
1425
1426 mWindowMap.remove(win.mClient.asBinder());
1427 mWindows.remove(win);
1428
1429 if (mInputMethodWindow == win) {
1430 mInputMethodWindow = null;
1431 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1432 mInputMethodDialogs.remove(win);
1433 }
Romain Guy06882f82009-06-10 13:36:04 -07001434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 final WindowToken token = win.mToken;
1436 final AppWindowToken atoken = win.mAppToken;
1437 token.windows.remove(win);
1438 if (atoken != null) {
1439 atoken.allAppWindows.remove(win);
1440 }
1441 if (localLOGV) Log.v(
1442 TAG, "**** Removing window " + win + ": count="
1443 + token.windows.size());
1444 if (token.windows.size() == 0) {
1445 if (!token.explicit) {
1446 mTokenMap.remove(token.token);
1447 mTokenList.remove(token);
1448 } else if (atoken != null) {
1449 atoken.firstWindowDrawn = false;
1450 }
1451 }
1452
1453 if (atoken != null) {
1454 if (atoken.startingWindow == win) {
1455 atoken.startingWindow = null;
1456 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1457 // If this is the last window and we had requested a starting
1458 // transition window, well there is no point now.
1459 atoken.startingData = null;
1460 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1461 // If this is the last window except for a starting transition
1462 // window, we need to get rid of the starting transition.
1463 if (DEBUG_STARTING_WINDOW) {
1464 Log.v(TAG, "Schedule remove starting " + token
1465 + ": no more real windows");
1466 }
1467 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1468 mH.sendMessage(m);
1469 }
1470 }
Romain Guy06882f82009-06-10 13:36:04 -07001471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 if (!mInLayout) {
1473 assignLayersLocked();
1474 mLayoutNeeded = true;
1475 performLayoutAndPlaceSurfacesLocked();
1476 if (win.mAppToken != null) {
1477 win.mAppToken.updateReportedVisibilityLocked();
1478 }
1479 }
1480 }
1481
1482 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1483 long origId = Binder.clearCallingIdentity();
1484 try {
1485 synchronized (mWindowMap) {
1486 WindowState w = windowForClientLocked(session, client);
1487 if ((w != null) && (w.mSurface != null)) {
1488 Surface.openTransaction();
1489 try {
1490 w.mSurface.setTransparentRegionHint(region);
1491 } finally {
1492 Surface.closeTransaction();
1493 }
1494 }
1495 }
1496 } finally {
1497 Binder.restoreCallingIdentity(origId);
1498 }
1499 }
1500
1501 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001502 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 Rect visibleInsets) {
1504 long origId = Binder.clearCallingIdentity();
1505 try {
1506 synchronized (mWindowMap) {
1507 WindowState w = windowForClientLocked(session, client);
1508 if (w != null) {
1509 w.mGivenInsetsPending = false;
1510 w.mGivenContentInsets.set(contentInsets);
1511 w.mGivenVisibleInsets.set(visibleInsets);
1512 w.mTouchableInsets = touchableInsets;
1513 mLayoutNeeded = true;
1514 performLayoutAndPlaceSurfacesLocked();
1515 }
1516 }
1517 } finally {
1518 Binder.restoreCallingIdentity(origId);
1519 }
1520 }
Romain Guy06882f82009-06-10 13:36:04 -07001521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 public void getWindowDisplayFrame(Session session, IWindow client,
1523 Rect outDisplayFrame) {
1524 synchronized(mWindowMap) {
1525 WindowState win = windowForClientLocked(session, client);
1526 if (win == null) {
1527 outDisplayFrame.setEmpty();
1528 return;
1529 }
1530 outDisplayFrame.set(win.mDisplayFrame);
1531 }
1532 }
1533
1534 public int relayoutWindow(Session session, IWindow client,
1535 WindowManager.LayoutParams attrs, int requestedWidth,
1536 int requestedHeight, int viewVisibility, boolean insetsPending,
1537 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1538 Surface outSurface) {
1539 boolean displayed = false;
1540 boolean inTouchMode;
1541 Configuration newConfig = null;
1542 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 synchronized(mWindowMap) {
1545 WindowState win = windowForClientLocked(session, client);
1546 if (win == null) {
1547 return 0;
1548 }
1549 win.mRequestedWidth = requestedWidth;
1550 win.mRequestedHeight = requestedHeight;
1551
1552 if (attrs != null) {
1553 mPolicy.adjustWindowParamsLw(attrs);
1554 }
Romain Guy06882f82009-06-10 13:36:04 -07001555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 int attrChanges = 0;
1557 int flagChanges = 0;
1558 if (attrs != null) {
1559 flagChanges = win.mAttrs.flags ^= attrs.flags;
1560 attrChanges = win.mAttrs.copyFrom(attrs);
1561 }
1562
1563 if (localLOGV) Log.v(
1564 TAG, "Relayout given client " + client.asBinder()
1565 + " (" + win.mAttrs.getTitle() + ")");
1566
1567
1568 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1569 win.mAlpha = attrs.alpha;
1570 }
1571
1572 final boolean scaledWindow =
1573 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1574
1575 if (scaledWindow) {
1576 // requested{Width|Height} Surface's physical size
1577 // attrs.{width|height} Size on screen
1578 win.mHScale = (attrs.width != requestedWidth) ?
1579 (attrs.width / (float)requestedWidth) : 1.0f;
1580 win.mVScale = (attrs.height != requestedHeight) ?
1581 (attrs.height / (float)requestedHeight) : 1.0f;
1582 }
1583
1584 boolean imMayMove = (flagChanges&(
1585 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1586 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 boolean focusMayChange = win.mViewVisibility != viewVisibility
1589 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1590 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 win.mRelayoutCalled = true;
1593 final int oldVisibility = win.mViewVisibility;
1594 win.mViewVisibility = viewVisibility;
1595 if (viewVisibility == View.VISIBLE &&
1596 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1597 displayed = !win.isVisibleLw();
1598 if (win.mExiting) {
1599 win.mExiting = false;
1600 win.mAnimation = null;
1601 }
1602 if (win.mDestroying) {
1603 win.mDestroying = false;
1604 mDestroySurface.remove(win);
1605 }
1606 if (oldVisibility == View.GONE) {
1607 win.mEnterAnimationPending = true;
1608 }
1609 if (displayed && win.mSurface != null && !win.mDrawPending
1610 && !win.mCommitDrawPending && !mDisplayFrozen) {
1611 applyEnterAnimationLocked(win);
1612 }
1613 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1614 // To change the format, we need to re-build the surface.
1615 win.destroySurfaceLocked();
1616 displayed = true;
1617 }
1618 try {
1619 Surface surface = win.createSurfaceLocked();
1620 if (surface != null) {
1621 outSurface.copyFrom(surface);
1622 } else {
1623 outSurface.clear();
1624 }
1625 } catch (Exception e) {
1626 Log.w(TAG, "Exception thrown when creating surface for client "
1627 + client + " (" + win.mAttrs.getTitle() + ")",
1628 e);
1629 Binder.restoreCallingIdentity(origId);
1630 return 0;
1631 }
1632 if (displayed) {
1633 focusMayChange = true;
1634 }
1635 if (win.mAttrs.type == TYPE_INPUT_METHOD
1636 && mInputMethodWindow == null) {
1637 mInputMethodWindow = win;
1638 imMayMove = true;
1639 }
1640 } else {
1641 win.mEnterAnimationPending = false;
1642 if (win.mSurface != null) {
1643 // If we are not currently running the exit animation, we
1644 // need to see about starting one.
1645 if (!win.mExiting) {
1646 // Try starting an animation; if there isn't one, we
1647 // can destroy the surface right away.
1648 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1649 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1650 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1651 }
1652 if (win.isWinVisibleLw() &&
1653 applyAnimationLocked(win, transit, false)) {
1654 win.mExiting = true;
1655 mKeyWaiter.finishedKey(session, client, true,
1656 KeyWaiter.RETURN_NOTHING);
1657 } else if (win.isAnimating()) {
1658 // Currently in a hide animation... turn this into
1659 // an exit.
1660 win.mExiting = true;
1661 } else {
1662 if (mInputMethodWindow == win) {
1663 mInputMethodWindow = null;
1664 }
1665 win.destroySurfaceLocked();
1666 }
1667 }
1668 }
1669 outSurface.clear();
1670 }
1671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (focusMayChange) {
1673 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1674 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 imMayMove = false;
1676 }
1677 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1678 }
Romain Guy06882f82009-06-10 13:36:04 -07001679
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001680 // updateFocusedWindowLocked() already assigned layers so we only need to
1681 // reassign them at this point if the IM window state gets shuffled
1682 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (imMayMove) {
1685 if (moveInputMethodWindowsIfNeededLocked(false)) {
1686 assignLayers = true;
1687 }
1688 }
Romain Guy06882f82009-06-10 13:36:04 -07001689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 mLayoutNeeded = true;
1691 win.mGivenInsetsPending = insetsPending;
1692 if (assignLayers) {
1693 assignLayersLocked();
1694 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001695 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 performLayoutAndPlaceSurfacesLocked();
1697 if (win.mAppToken != null) {
1698 win.mAppToken.updateReportedVisibilityLocked();
1699 }
1700 outFrame.set(win.mFrame);
1701 outContentInsets.set(win.mContentInsets);
1702 outVisibleInsets.set(win.mVisibleInsets);
1703 if (localLOGV) Log.v(
1704 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001705 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 + ", requestedHeight=" + requestedHeight
1707 + ", viewVisibility=" + viewVisibility
1708 + "\nRelayout returning frame=" + outFrame
1709 + ", surface=" + outSurface);
1710
1711 if (localLOGV || DEBUG_FOCUS) Log.v(
1712 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1713
1714 inTouchMode = mInTouchMode;
1715 }
1716
1717 if (newConfig != null) {
1718 sendNewConfiguration();
1719 }
Romain Guy06882f82009-06-10 13:36:04 -07001720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1724 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1725 }
1726
1727 public void finishDrawingWindow(Session session, IWindow client) {
1728 final long origId = Binder.clearCallingIdentity();
1729 synchronized(mWindowMap) {
1730 WindowState win = windowForClientLocked(session, client);
1731 if (win != null && win.finishDrawingLocked()) {
1732 mLayoutNeeded = true;
1733 performLayoutAndPlaceSurfacesLocked();
1734 }
1735 }
1736 Binder.restoreCallingIdentity(origId);
1737 }
1738
1739 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1740 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1741 + (lp != null ? lp.packageName : null)
1742 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1743 if (lp != null && lp.windowAnimations != 0) {
1744 // If this is a system resource, don't try to load it from the
1745 // application resources. It is nice to avoid loading application
1746 // resources if we can.
1747 String packageName = lp.packageName != null ? lp.packageName : "android";
1748 int resId = lp.windowAnimations;
1749 if ((resId&0xFF000000) == 0x01000000) {
1750 packageName = "android";
1751 }
1752 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1753 + packageName);
1754 return AttributeCache.instance().get(packageName, resId,
1755 com.android.internal.R.styleable.WindowAnimation);
1756 }
1757 return null;
1758 }
Romain Guy06882f82009-06-10 13:36:04 -07001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 private void applyEnterAnimationLocked(WindowState win) {
1761 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1762 if (win.mEnterAnimationPending) {
1763 win.mEnterAnimationPending = false;
1764 transit = WindowManagerPolicy.TRANSIT_ENTER;
1765 }
1766
1767 applyAnimationLocked(win, transit, true);
1768 }
1769
1770 private boolean applyAnimationLocked(WindowState win,
1771 int transit, boolean isEntrance) {
1772 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1773 // If we are trying to apply an animation, but already running
1774 // an animation of the same type, then just leave that one alone.
1775 return true;
1776 }
Romain Guy06882f82009-06-10 13:36:04 -07001777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 // Only apply an animation if the display isn't frozen. If it is
1779 // frozen, there is no reason to animate and it can cause strange
1780 // artifacts when we unfreeze the display if some different animation
1781 // is running.
1782 if (!mDisplayFrozen) {
1783 int anim = mPolicy.selectAnimationLw(win, transit);
1784 int attr = -1;
1785 Animation a = null;
1786 if (anim != 0) {
1787 a = AnimationUtils.loadAnimation(mContext, anim);
1788 } else {
1789 switch (transit) {
1790 case WindowManagerPolicy.TRANSIT_ENTER:
1791 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1792 break;
1793 case WindowManagerPolicy.TRANSIT_EXIT:
1794 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1795 break;
1796 case WindowManagerPolicy.TRANSIT_SHOW:
1797 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1798 break;
1799 case WindowManagerPolicy.TRANSIT_HIDE:
1800 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1801 break;
1802 }
1803 if (attr >= 0) {
1804 a = loadAnimation(win.mAttrs, attr);
1805 }
1806 }
1807 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1808 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1809 + " mAnimation=" + win.mAnimation
1810 + " isEntrance=" + isEntrance);
1811 if (a != null) {
1812 if (DEBUG_ANIM) {
1813 RuntimeException e = new RuntimeException();
1814 e.fillInStackTrace();
1815 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1816 }
1817 win.setAnimation(a);
1818 win.mAnimationIsEntrance = isEntrance;
1819 }
1820 } else {
1821 win.clearAnimation();
1822 }
1823
1824 return win.mAnimation != null;
1825 }
1826
1827 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1828 int anim = 0;
1829 Context context = mContext;
1830 if (animAttr >= 0) {
1831 AttributeCache.Entry ent = getCachedAnimations(lp);
1832 if (ent != null) {
1833 context = ent.context;
1834 anim = ent.array.getResourceId(animAttr, 0);
1835 }
1836 }
1837 if (anim != 0) {
1838 return AnimationUtils.loadAnimation(context, anim);
1839 }
1840 return null;
1841 }
Romain Guy06882f82009-06-10 13:36:04 -07001842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 private boolean applyAnimationLocked(AppWindowToken wtoken,
1844 WindowManager.LayoutParams lp, int transit, boolean enter) {
1845 // Only apply an animation if the display isn't frozen. If it is
1846 // frozen, there is no reason to animate and it can cause strange
1847 // artifacts when we unfreeze the display if some different animation
1848 // is running.
1849 if (!mDisplayFrozen) {
1850 int animAttr = 0;
1851 switch (transit) {
1852 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1853 animAttr = enter
1854 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1855 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1856 break;
1857 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1858 animAttr = enter
1859 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1860 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1861 break;
1862 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1863 animAttr = enter
1864 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1865 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1866 break;
1867 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1868 animAttr = enter
1869 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1870 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1871 break;
1872 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1873 animAttr = enter
1874 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1875 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1876 break;
1877 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1878 animAttr = enter
1879 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
1880 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1881 break;
1882 }
1883 Animation a = loadAnimation(lp, animAttr);
1884 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1885 + " anim=" + a
1886 + " animAttr=0x" + Integer.toHexString(animAttr)
1887 + " transit=" + transit);
1888 if (a != null) {
1889 if (DEBUG_ANIM) {
1890 RuntimeException e = new RuntimeException();
1891 e.fillInStackTrace();
1892 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1893 }
1894 wtoken.setAnimation(a);
1895 }
1896 } else {
1897 wtoken.clearAnimation();
1898 }
1899
1900 return wtoken.animation != null;
1901 }
1902
1903 // -------------------------------------------------------------
1904 // Application Window Tokens
1905 // -------------------------------------------------------------
1906
1907 public void validateAppTokens(List tokens) {
1908 int v = tokens.size()-1;
1909 int m = mAppTokens.size()-1;
1910 while (v >= 0 && m >= 0) {
1911 AppWindowToken wtoken = mAppTokens.get(m);
1912 if (wtoken.removed) {
1913 m--;
1914 continue;
1915 }
1916 if (tokens.get(v) != wtoken.token) {
1917 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1918 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1919 }
1920 v--;
1921 m--;
1922 }
1923 while (v >= 0) {
1924 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1925 v--;
1926 }
1927 while (m >= 0) {
1928 AppWindowToken wtoken = mAppTokens.get(m);
1929 if (!wtoken.removed) {
1930 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1931 }
1932 m--;
1933 }
1934 }
1935
1936 boolean checkCallingPermission(String permission, String func) {
1937 // Quick check: if the calling permission is me, it's all okay.
1938 if (Binder.getCallingPid() == Process.myPid()) {
1939 return true;
1940 }
Romain Guy06882f82009-06-10 13:36:04 -07001941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 if (mContext.checkCallingPermission(permission)
1943 == PackageManager.PERMISSION_GRANTED) {
1944 return true;
1945 }
1946 String msg = "Permission Denial: " + func + " from pid="
1947 + Binder.getCallingPid()
1948 + ", uid=" + Binder.getCallingUid()
1949 + " requires " + permission;
1950 Log.w(TAG, msg);
1951 return false;
1952 }
Romain Guy06882f82009-06-10 13:36:04 -07001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 AppWindowToken findAppWindowToken(IBinder token) {
1955 WindowToken wtoken = mTokenMap.get(token);
1956 if (wtoken == null) {
1957 return null;
1958 }
1959 return wtoken.appWindowToken;
1960 }
Romain Guy06882f82009-06-10 13:36:04 -07001961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 public void addWindowToken(IBinder token, int type) {
1963 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1964 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001965 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
Romain Guy06882f82009-06-10 13:36:04 -07001967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 synchronized(mWindowMap) {
1969 WindowToken wtoken = mTokenMap.get(token);
1970 if (wtoken != null) {
1971 Log.w(TAG, "Attempted to add existing input method token: " + token);
1972 return;
1973 }
1974 wtoken = new WindowToken(token, type, true);
1975 mTokenMap.put(token, wtoken);
1976 mTokenList.add(wtoken);
1977 }
1978 }
Romain Guy06882f82009-06-10 13:36:04 -07001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 public void removeWindowToken(IBinder token) {
1981 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1982 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001983 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985
1986 final long origId = Binder.clearCallingIdentity();
1987 synchronized(mWindowMap) {
1988 WindowToken wtoken = mTokenMap.remove(token);
1989 mTokenList.remove(wtoken);
1990 if (wtoken != null) {
1991 boolean delayed = false;
1992 if (!wtoken.hidden) {
1993 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07001994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 final int N = wtoken.windows.size();
1996 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 for (int i=0; i<N; i++) {
1999 WindowState win = wtoken.windows.get(i);
2000
2001 if (win.isAnimating()) {
2002 delayed = true;
2003 }
Romain Guy06882f82009-06-10 13:36:04 -07002004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 if (win.isVisibleNow()) {
2006 applyAnimationLocked(win,
2007 WindowManagerPolicy.TRANSIT_EXIT, false);
2008 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2009 KeyWaiter.RETURN_NOTHING);
2010 changed = true;
2011 }
2012 }
2013
2014 if (changed) {
2015 mLayoutNeeded = true;
2016 performLayoutAndPlaceSurfacesLocked();
2017 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2018 }
Romain Guy06882f82009-06-10 13:36:04 -07002019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 if (delayed) {
2021 mExitingTokens.add(wtoken);
2022 }
2023 }
Romain Guy06882f82009-06-10 13:36:04 -07002024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 } else {
2026 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2027 }
2028 }
2029 Binder.restoreCallingIdentity(origId);
2030 }
2031
2032 public void addAppToken(int addPos, IApplicationToken token,
2033 int groupId, int requestedOrientation, boolean fullscreen) {
2034 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2035 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002036 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
Romain Guy06882f82009-06-10 13:36:04 -07002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 synchronized(mWindowMap) {
2040 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2041 if (wtoken != null) {
2042 Log.w(TAG, "Attempted to add existing app token: " + token);
2043 return;
2044 }
2045 wtoken = new AppWindowToken(token);
2046 wtoken.groupId = groupId;
2047 wtoken.appFullscreen = fullscreen;
2048 wtoken.requestedOrientation = requestedOrientation;
2049 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002050 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 mTokenMap.put(token.asBinder(), wtoken);
2052 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 // Application tokens start out hidden.
2055 wtoken.hidden = true;
2056 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 //dump();
2059 }
2060 }
Romain Guy06882f82009-06-10 13:36:04 -07002061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 public void setAppGroupId(IBinder token, int groupId) {
2063 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2064 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002065 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
2067
2068 synchronized(mWindowMap) {
2069 AppWindowToken wtoken = findAppWindowToken(token);
2070 if (wtoken == null) {
2071 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2072 return;
2073 }
2074 wtoken.groupId = groupId;
2075 }
2076 }
Romain Guy06882f82009-06-10 13:36:04 -07002077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 public int getOrientationFromWindowsLocked() {
2079 int pos = mWindows.size() - 1;
2080 while (pos >= 0) {
2081 WindowState wtoken = (WindowState) mWindows.get(pos);
2082 pos--;
2083 if (wtoken.mAppToken != null) {
2084 // We hit an application window. so the orientation will be determined by the
2085 // app window. No point in continuing further.
2086 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2087 }
2088 if (!wtoken.isVisibleLw()) {
2089 continue;
2090 }
2091 int req = wtoken.mAttrs.screenOrientation;
2092 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2093 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2094 continue;
2095 } else {
2096 return req;
2097 }
2098 }
2099 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2100 }
Romain Guy06882f82009-06-10 13:36:04 -07002101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 public int getOrientationFromAppTokensLocked() {
2103 int pos = mAppTokens.size() - 1;
2104 int curGroup = 0;
2105 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002106 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002108 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 while (pos >= 0) {
2110 AppWindowToken wtoken = mAppTokens.get(pos);
2111 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002112 // if we're about to tear down this window and not seek for
2113 // the behind activity, don't use it for orientation
2114 if (!findingBehind
2115 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002116 continue;
2117 }
2118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 if (!haveGroup) {
2120 // We ignore any hidden applications on the top.
2121 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2122 continue;
2123 }
2124 haveGroup = true;
2125 curGroup = wtoken.groupId;
2126 lastOrientation = wtoken.requestedOrientation;
2127 } else if (curGroup != wtoken.groupId) {
2128 // If we have hit a new application group, and the bottom
2129 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002130 // the orientation behind it, and the last app was
2131 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002133 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2134 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 return lastOrientation;
2136 }
2137 }
2138 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002139 // If this application is fullscreen, and didn't explicitly say
2140 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002142 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002143 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002144 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 return or;
2146 }
2147 // If this application has requested an explicit orientation,
2148 // then use it.
2149 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2150 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2151 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2152 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2153 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2154 return or;
2155 }
Owen Lin3413b892009-05-01 17:12:32 -07002156 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2159 }
Romain Guy06882f82009-06-10 13:36:04 -07002160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002162 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002163 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2164 "updateOrientationFromAppTokens()")) {
2165 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2166 }
2167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 Configuration config;
2169 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002170 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2171 freezeThisOneIfNeeded);
2172 Binder.restoreCallingIdentity(ident);
2173 return config;
2174 }
2175
2176 Configuration updateOrientationFromAppTokensUnchecked(
2177 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2178 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002180 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
2182 if (config != null) {
2183 mLayoutNeeded = true;
2184 performLayoutAndPlaceSurfacesLocked();
2185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 return config;
2187 }
Romain Guy06882f82009-06-10 13:36:04 -07002188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 /*
2190 * The orientation is computed from non-application windows first. If none of
2191 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002192 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2194 * android.os.IBinder)
2195 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002196 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002197 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 long ident = Binder.clearCallingIdentity();
2200 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002201 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 if (req != mForcedAppOrientation) {
2204 changed = true;
2205 mForcedAppOrientation = req;
2206 //send a message to Policy indicating orientation change to take
2207 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002208 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
Romain Guy06882f82009-06-10 13:36:04 -07002210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 if (changed) {
2212 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002213 WindowManagerPolicy.USE_LAST_ROTATION,
2214 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 if (changed) {
2216 if (freezeThisOneIfNeeded != null) {
2217 AppWindowToken wtoken = findAppWindowToken(
2218 freezeThisOneIfNeeded);
2219 if (wtoken != null) {
2220 startAppFreezingScreenLocked(wtoken,
2221 ActivityInfo.CONFIG_ORIENTATION);
2222 }
2223 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002224 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 }
2226 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002227
2228 // No obvious action we need to take, but if our current
2229 // state mismatches the activity maanager's, update it
2230 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002231 mTempConfiguration.setToDefaults();
2232 if (computeNewConfigurationLocked(mTempConfiguration)) {
2233 if (appConfig.diff(mTempConfiguration) != 0) {
2234 Log.i(TAG, "Config changed: " + mTempConfiguration);
2235 return new Configuration(mTempConfiguration);
2236 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002237 }
2238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 } finally {
2240 Binder.restoreCallingIdentity(ident);
2241 }
Romain Guy06882f82009-06-10 13:36:04 -07002242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 return null;
2244 }
Romain Guy06882f82009-06-10 13:36:04 -07002245
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002246 int computeForcedAppOrientationLocked() {
2247 int req = getOrientationFromWindowsLocked();
2248 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2249 req = getOrientationFromAppTokensLocked();
2250 }
2251 return req;
2252 }
Romain Guy06882f82009-06-10 13:36:04 -07002253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2255 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2256 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002257 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 }
Romain Guy06882f82009-06-10 13:36:04 -07002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 synchronized(mWindowMap) {
2261 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2262 if (wtoken == null) {
2263 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2264 return;
2265 }
Romain Guy06882f82009-06-10 13:36:04 -07002266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 wtoken.requestedOrientation = requestedOrientation;
2268 }
2269 }
Romain Guy06882f82009-06-10 13:36:04 -07002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 public int getAppOrientation(IApplicationToken token) {
2272 synchronized(mWindowMap) {
2273 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2274 if (wtoken == null) {
2275 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2276 }
Romain Guy06882f82009-06-10 13:36:04 -07002277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 return wtoken.requestedOrientation;
2279 }
2280 }
Romain Guy06882f82009-06-10 13:36:04 -07002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2283 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2284 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002285 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
2287
2288 synchronized(mWindowMap) {
2289 boolean changed = false;
2290 if (token == null) {
2291 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2292 changed = mFocusedApp != null;
2293 mFocusedApp = null;
2294 mKeyWaiter.tickle();
2295 } else {
2296 AppWindowToken newFocus = findAppWindowToken(token);
2297 if (newFocus == null) {
2298 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2299 return;
2300 }
2301 changed = mFocusedApp != newFocus;
2302 mFocusedApp = newFocus;
2303 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2304 mKeyWaiter.tickle();
2305 }
2306
2307 if (moveFocusNow && changed) {
2308 final long origId = Binder.clearCallingIdentity();
2309 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2310 Binder.restoreCallingIdentity(origId);
2311 }
2312 }
2313 }
2314
2315 public void prepareAppTransition(int transit) {
2316 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2317 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002318 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
Romain Guy06882f82009-06-10 13:36:04 -07002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 synchronized(mWindowMap) {
2322 if (DEBUG_APP_TRANSITIONS) Log.v(
2323 TAG, "Prepare app transition: transit=" + transit
2324 + " mNextAppTransition=" + mNextAppTransition);
2325 if (!mDisplayFrozen) {
2326 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2327 mNextAppTransition = transit;
2328 }
2329 mAppTransitionReady = false;
2330 mAppTransitionTimeout = false;
2331 mStartingIconInTransition = false;
2332 mSkipAppTransitionAnimation = false;
2333 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2334 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2335 5000);
2336 }
2337 }
2338 }
2339
2340 public int getPendingAppTransition() {
2341 return mNextAppTransition;
2342 }
Romain Guy06882f82009-06-10 13:36:04 -07002343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 public void executeAppTransition() {
2345 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2346 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002347 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
Romain Guy06882f82009-06-10 13:36:04 -07002349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 synchronized(mWindowMap) {
2351 if (DEBUG_APP_TRANSITIONS) Log.v(
2352 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2353 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2354 mAppTransitionReady = true;
2355 final long origId = Binder.clearCallingIdentity();
2356 performLayoutAndPlaceSurfacesLocked();
2357 Binder.restoreCallingIdentity(origId);
2358 }
2359 }
2360 }
2361
2362 public void setAppStartingWindow(IBinder token, String pkg,
2363 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2364 IBinder transferFrom, boolean createIfNeeded) {
2365 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2366 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002367 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
2369
2370 synchronized(mWindowMap) {
2371 if (DEBUG_STARTING_WINDOW) Log.v(
2372 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2373 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 AppWindowToken wtoken = findAppWindowToken(token);
2376 if (wtoken == null) {
2377 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2378 return;
2379 }
2380
2381 // If the display is frozen, we won't do anything until the
2382 // actual window is displayed so there is no reason to put in
2383 // the starting window.
2384 if (mDisplayFrozen) {
2385 return;
2386 }
Romain Guy06882f82009-06-10 13:36:04 -07002387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 if (wtoken.startingData != null) {
2389 return;
2390 }
Romain Guy06882f82009-06-10 13:36:04 -07002391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 if (transferFrom != null) {
2393 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2394 if (ttoken != null) {
2395 WindowState startingWindow = ttoken.startingWindow;
2396 if (startingWindow != null) {
2397 if (mStartingIconInTransition) {
2398 // In this case, the starting icon has already
2399 // been displayed, so start letting windows get
2400 // shown immediately without any more transitions.
2401 mSkipAppTransitionAnimation = true;
2402 }
2403 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2404 "Moving existing starting from " + ttoken
2405 + " to " + wtoken);
2406 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 // Transfer the starting window over to the new
2409 // token.
2410 wtoken.startingData = ttoken.startingData;
2411 wtoken.startingView = ttoken.startingView;
2412 wtoken.startingWindow = startingWindow;
2413 ttoken.startingData = null;
2414 ttoken.startingView = null;
2415 ttoken.startingWindow = null;
2416 ttoken.startingMoved = true;
2417 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002418 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 startingWindow.mAppToken = wtoken;
2420 mWindows.remove(startingWindow);
2421 ttoken.windows.remove(startingWindow);
2422 ttoken.allAppWindows.remove(startingWindow);
2423 addWindowToListInOrderLocked(startingWindow, true);
2424 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 // Propagate other interesting state between the
2427 // tokens. If the old token is displayed, we should
2428 // immediately force the new one to be displayed. If
2429 // it is animating, we need to move that animation to
2430 // the new one.
2431 if (ttoken.allDrawn) {
2432 wtoken.allDrawn = true;
2433 }
2434 if (ttoken.firstWindowDrawn) {
2435 wtoken.firstWindowDrawn = true;
2436 }
2437 if (!ttoken.hidden) {
2438 wtoken.hidden = false;
2439 wtoken.hiddenRequested = false;
2440 wtoken.willBeHidden = false;
2441 }
2442 if (wtoken.clientHidden != ttoken.clientHidden) {
2443 wtoken.clientHidden = ttoken.clientHidden;
2444 wtoken.sendAppVisibilityToClients();
2445 }
2446 if (ttoken.animation != null) {
2447 wtoken.animation = ttoken.animation;
2448 wtoken.animating = ttoken.animating;
2449 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2450 ttoken.animation = null;
2451 ttoken.animLayerAdjustment = 0;
2452 wtoken.updateLayers();
2453 ttoken.updateLayers();
2454 }
Romain Guy06882f82009-06-10 13:36:04 -07002455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 mLayoutNeeded = true;
2458 performLayoutAndPlaceSurfacesLocked();
2459 Binder.restoreCallingIdentity(origId);
2460 return;
2461 } else if (ttoken.startingData != null) {
2462 // The previous app was getting ready to show a
2463 // starting window, but hasn't yet done so. Steal it!
2464 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2465 "Moving pending starting from " + ttoken
2466 + " to " + wtoken);
2467 wtoken.startingData = ttoken.startingData;
2468 ttoken.startingData = null;
2469 ttoken.startingMoved = true;
2470 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2471 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2472 // want to process the message ASAP, before any other queued
2473 // messages.
2474 mH.sendMessageAtFrontOfQueue(m);
2475 return;
2476 }
2477 }
2478 }
2479
2480 // There is no existing starting window, and the caller doesn't
2481 // want us to create one, so that's it!
2482 if (!createIfNeeded) {
2483 return;
2484 }
Romain Guy06882f82009-06-10 13:36:04 -07002485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 mStartingIconInTransition = true;
2487 wtoken.startingData = new StartingData(
2488 pkg, theme, nonLocalizedLabel,
2489 labelRes, icon);
2490 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2491 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2492 // want to process the message ASAP, before any other queued
2493 // messages.
2494 mH.sendMessageAtFrontOfQueue(m);
2495 }
2496 }
2497
2498 public void setAppWillBeHidden(IBinder token) {
2499 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2500 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002501 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 }
2503
2504 AppWindowToken wtoken;
2505
2506 synchronized(mWindowMap) {
2507 wtoken = findAppWindowToken(token);
2508 if (wtoken == null) {
2509 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2510 return;
2511 }
2512 wtoken.willBeHidden = true;
2513 }
2514 }
Romain Guy06882f82009-06-10 13:36:04 -07002515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2517 boolean visible, int transit, boolean performLayout) {
2518 boolean delayed = false;
2519
2520 if (wtoken.clientHidden == visible) {
2521 wtoken.clientHidden = !visible;
2522 wtoken.sendAppVisibilityToClients();
2523 }
Romain Guy06882f82009-06-10 13:36:04 -07002524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 wtoken.willBeHidden = false;
2526 if (wtoken.hidden == visible) {
2527 final int N = wtoken.allAppWindows.size();
2528 boolean changed = false;
2529 if (DEBUG_APP_TRANSITIONS) Log.v(
2530 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2531 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2536 if (wtoken.animation == sDummyAnimation) {
2537 wtoken.animation = null;
2538 }
2539 applyAnimationLocked(wtoken, lp, transit, visible);
2540 changed = true;
2541 if (wtoken.animation != null) {
2542 delayed = runningAppAnimation = true;
2543 }
2544 }
Romain Guy06882f82009-06-10 13:36:04 -07002545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 for (int i=0; i<N; i++) {
2547 WindowState win = wtoken.allAppWindows.get(i);
2548 if (win == wtoken.startingWindow) {
2549 continue;
2550 }
2551
2552 if (win.isAnimating()) {
2553 delayed = true;
2554 }
Romain Guy06882f82009-06-10 13:36:04 -07002555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2557 //win.dump(" ");
2558 if (visible) {
2559 if (!win.isVisibleNow()) {
2560 if (!runningAppAnimation) {
2561 applyAnimationLocked(win,
2562 WindowManagerPolicy.TRANSIT_ENTER, true);
2563 }
2564 changed = true;
2565 }
2566 } else if (win.isVisibleNow()) {
2567 if (!runningAppAnimation) {
2568 applyAnimationLocked(win,
2569 WindowManagerPolicy.TRANSIT_EXIT, false);
2570 }
2571 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2572 KeyWaiter.RETURN_NOTHING);
2573 changed = true;
2574 }
2575 }
2576
2577 wtoken.hidden = wtoken.hiddenRequested = !visible;
2578 if (!visible) {
2579 unsetAppFreezingScreenLocked(wtoken, true, true);
2580 } else {
2581 // If we are being set visible, and the starting window is
2582 // not yet displayed, then make sure it doesn't get displayed.
2583 WindowState swin = wtoken.startingWindow;
2584 if (swin != null && (swin.mDrawPending
2585 || swin.mCommitDrawPending)) {
2586 swin.mPolicyVisibility = false;
2587 swin.mPolicyVisibilityAfterAnim = false;
2588 }
2589 }
Romain Guy06882f82009-06-10 13:36:04 -07002590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2592 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2593 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 if (changed && performLayout) {
2596 mLayoutNeeded = true;
2597 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 performLayoutAndPlaceSurfacesLocked();
2599 }
2600 }
2601
2602 if (wtoken.animation != null) {
2603 delayed = true;
2604 }
Romain Guy06882f82009-06-10 13:36:04 -07002605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 return delayed;
2607 }
2608
2609 public void setAppVisibility(IBinder token, boolean visible) {
2610 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2611 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002612 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 }
2614
2615 AppWindowToken wtoken;
2616
2617 synchronized(mWindowMap) {
2618 wtoken = findAppWindowToken(token);
2619 if (wtoken == null) {
2620 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2621 return;
2622 }
2623
2624 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2625 RuntimeException e = new RuntimeException();
2626 e.fillInStackTrace();
2627 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2628 + "): mNextAppTransition=" + mNextAppTransition
2629 + " hidden=" + wtoken.hidden
2630 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2631 }
Romain Guy06882f82009-06-10 13:36:04 -07002632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 // If we are preparing an app transition, then delay changing
2634 // the visibility of this token until we execute that transition.
2635 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2636 // Already in requested state, don't do anything more.
2637 if (wtoken.hiddenRequested != visible) {
2638 return;
2639 }
2640 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 if (DEBUG_APP_TRANSITIONS) Log.v(
2643 TAG, "Setting dummy animation on: " + wtoken);
2644 wtoken.setDummyAnimation();
2645 mOpeningApps.remove(wtoken);
2646 mClosingApps.remove(wtoken);
2647 wtoken.inPendingTransaction = true;
2648 if (visible) {
2649 mOpeningApps.add(wtoken);
2650 wtoken.allDrawn = false;
2651 wtoken.startingDisplayed = false;
2652 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 if (wtoken.clientHidden) {
2655 // In the case where we are making an app visible
2656 // but holding off for a transition, we still need
2657 // to tell the client to make its windows visible so
2658 // they get drawn. Otherwise, we will wait on
2659 // performing the transition until all windows have
2660 // been drawn, they never will be, and we are sad.
2661 wtoken.clientHidden = false;
2662 wtoken.sendAppVisibilityToClients();
2663 }
2664 } else {
2665 mClosingApps.add(wtoken);
2666 }
2667 return;
2668 }
Romain Guy06882f82009-06-10 13:36:04 -07002669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 final long origId = Binder.clearCallingIdentity();
2671 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2672 wtoken.updateReportedVisibilityLocked();
2673 Binder.restoreCallingIdentity(origId);
2674 }
2675 }
2676
2677 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2678 boolean unfreezeSurfaceNow, boolean force) {
2679 if (wtoken.freezingScreen) {
2680 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2681 + " force=" + force);
2682 final int N = wtoken.allAppWindows.size();
2683 boolean unfrozeWindows = false;
2684 for (int i=0; i<N; i++) {
2685 WindowState w = wtoken.allAppWindows.get(i);
2686 if (w.mAppFreezing) {
2687 w.mAppFreezing = false;
2688 if (w.mSurface != null && !w.mOrientationChanging) {
2689 w.mOrientationChanging = true;
2690 }
2691 unfrozeWindows = true;
2692 }
2693 }
2694 if (force || unfrozeWindows) {
2695 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2696 wtoken.freezingScreen = false;
2697 mAppsFreezingScreen--;
2698 }
2699 if (unfreezeSurfaceNow) {
2700 if (unfrozeWindows) {
2701 mLayoutNeeded = true;
2702 performLayoutAndPlaceSurfacesLocked();
2703 }
2704 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2705 stopFreezingDisplayLocked();
2706 }
2707 }
2708 }
2709 }
Romain Guy06882f82009-06-10 13:36:04 -07002710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2712 int configChanges) {
2713 if (DEBUG_ORIENTATION) {
2714 RuntimeException e = new RuntimeException();
2715 e.fillInStackTrace();
2716 Log.i(TAG, "Set freezing of " + wtoken.appToken
2717 + ": hidden=" + wtoken.hidden + " freezing="
2718 + wtoken.freezingScreen, e);
2719 }
2720 if (!wtoken.hiddenRequested) {
2721 if (!wtoken.freezingScreen) {
2722 wtoken.freezingScreen = true;
2723 mAppsFreezingScreen++;
2724 if (mAppsFreezingScreen == 1) {
2725 startFreezingDisplayLocked();
2726 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2727 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2728 5000);
2729 }
2730 }
2731 final int N = wtoken.allAppWindows.size();
2732 for (int i=0; i<N; i++) {
2733 WindowState w = wtoken.allAppWindows.get(i);
2734 w.mAppFreezing = true;
2735 }
2736 }
2737 }
Romain Guy06882f82009-06-10 13:36:04 -07002738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 public void startAppFreezingScreen(IBinder token, int configChanges) {
2740 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2741 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002742 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 }
2744
2745 synchronized(mWindowMap) {
2746 if (configChanges == 0 && !mDisplayFrozen) {
2747 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2748 return;
2749 }
Romain Guy06882f82009-06-10 13:36:04 -07002750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 AppWindowToken wtoken = findAppWindowToken(token);
2752 if (wtoken == null || wtoken.appToken == null) {
2753 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2754 return;
2755 }
2756 final long origId = Binder.clearCallingIdentity();
2757 startAppFreezingScreenLocked(wtoken, configChanges);
2758 Binder.restoreCallingIdentity(origId);
2759 }
2760 }
Romain Guy06882f82009-06-10 13:36:04 -07002761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 public void stopAppFreezingScreen(IBinder token, boolean force) {
2763 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2764 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002765 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 }
2767
2768 synchronized(mWindowMap) {
2769 AppWindowToken wtoken = findAppWindowToken(token);
2770 if (wtoken == null || wtoken.appToken == null) {
2771 return;
2772 }
2773 final long origId = Binder.clearCallingIdentity();
2774 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2775 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2776 unsetAppFreezingScreenLocked(wtoken, true, force);
2777 Binder.restoreCallingIdentity(origId);
2778 }
2779 }
Romain Guy06882f82009-06-10 13:36:04 -07002780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 public void removeAppToken(IBinder token) {
2782 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2783 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002784 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 }
2786
2787 AppWindowToken wtoken = null;
2788 AppWindowToken startingToken = null;
2789 boolean delayed = false;
2790
2791 final long origId = Binder.clearCallingIdentity();
2792 synchronized(mWindowMap) {
2793 WindowToken basewtoken = mTokenMap.remove(token);
2794 mTokenList.remove(basewtoken);
2795 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2796 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2797 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2798 wtoken.inPendingTransaction = false;
2799 mOpeningApps.remove(wtoken);
2800 if (mClosingApps.contains(wtoken)) {
2801 delayed = true;
2802 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2803 mClosingApps.add(wtoken);
2804 delayed = true;
2805 }
2806 if (DEBUG_APP_TRANSITIONS) Log.v(
2807 TAG, "Removing app " + wtoken + " delayed=" + delayed
2808 + " animation=" + wtoken.animation
2809 + " animating=" + wtoken.animating);
2810 if (delayed) {
2811 // set the token aside because it has an active animation to be finished
2812 mExitingAppTokens.add(wtoken);
2813 }
2814 mAppTokens.remove(wtoken);
2815 wtoken.removed = true;
2816 if (wtoken.startingData != null) {
2817 startingToken = wtoken;
2818 }
2819 unsetAppFreezingScreenLocked(wtoken, true, true);
2820 if (mFocusedApp == wtoken) {
2821 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2822 mFocusedApp = null;
2823 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2824 mKeyWaiter.tickle();
2825 }
2826 } else {
2827 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2828 }
Romain Guy06882f82009-06-10 13:36:04 -07002829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 if (!delayed && wtoken != null) {
2831 wtoken.updateReportedVisibilityLocked();
2832 }
2833 }
2834 Binder.restoreCallingIdentity(origId);
2835
2836 if (startingToken != null) {
2837 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2838 + startingToken + ": app token removed");
2839 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2840 mH.sendMessage(m);
2841 }
2842 }
2843
2844 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2845 final int NW = token.windows.size();
2846 for (int i=0; i<NW; i++) {
2847 WindowState win = token.windows.get(i);
2848 mWindows.remove(win);
2849 int j = win.mChildWindows.size();
2850 while (j > 0) {
2851 j--;
2852 mWindows.remove(win.mChildWindows.get(j));
2853 }
2854 }
2855 return NW > 0;
2856 }
2857
2858 void dumpAppTokensLocked() {
2859 for (int i=mAppTokens.size()-1; i>=0; i--) {
2860 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2861 }
2862 }
Romain Guy06882f82009-06-10 13:36:04 -07002863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 void dumpWindowsLocked() {
2865 for (int i=mWindows.size()-1; i>=0; i--) {
2866 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2867 }
2868 }
Romain Guy06882f82009-06-10 13:36:04 -07002869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 private int findWindowOffsetLocked(int tokenPos) {
2871 final int NW = mWindows.size();
2872
2873 if (tokenPos >= mAppTokens.size()) {
2874 int i = NW;
2875 while (i > 0) {
2876 i--;
2877 WindowState win = (WindowState)mWindows.get(i);
2878 if (win.getAppToken() != null) {
2879 return i+1;
2880 }
2881 }
2882 }
2883
2884 while (tokenPos > 0) {
2885 // Find the first app token below the new position that has
2886 // a window displayed.
2887 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2888 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2889 + tokenPos + " -- " + wtoken.token);
2890 int i = wtoken.windows.size();
2891 while (i > 0) {
2892 i--;
2893 WindowState win = wtoken.windows.get(i);
2894 int j = win.mChildWindows.size();
2895 while (j > 0) {
2896 j--;
2897 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2898 if (cwin.mSubLayer >= 0 ) {
2899 for (int pos=NW-1; pos>=0; pos--) {
2900 if (mWindows.get(pos) == cwin) {
2901 if (DEBUG_REORDER) Log.v(TAG,
2902 "Found child win @" + (pos+1));
2903 return pos+1;
2904 }
2905 }
2906 }
2907 }
2908 for (int pos=NW-1; pos>=0; pos--) {
2909 if (mWindows.get(pos) == win) {
2910 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2911 return pos+1;
2912 }
2913 }
2914 }
2915 tokenPos--;
2916 }
2917
2918 return 0;
2919 }
2920
2921 private final int reAddWindowLocked(int index, WindowState win) {
2922 final int NCW = win.mChildWindows.size();
2923 boolean added = false;
2924 for (int j=0; j<NCW; j++) {
2925 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2926 if (!added && cwin.mSubLayer >= 0) {
2927 mWindows.add(index, win);
2928 index++;
2929 added = true;
2930 }
2931 mWindows.add(index, cwin);
2932 index++;
2933 }
2934 if (!added) {
2935 mWindows.add(index, win);
2936 index++;
2937 }
2938 return index;
2939 }
Romain Guy06882f82009-06-10 13:36:04 -07002940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2942 final int NW = token.windows.size();
2943 for (int i=0; i<NW; i++) {
2944 index = reAddWindowLocked(index, token.windows.get(i));
2945 }
2946 return index;
2947 }
2948
2949 public void moveAppToken(int index, IBinder token) {
2950 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2951 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002952 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954
2955 synchronized(mWindowMap) {
2956 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2957 if (DEBUG_REORDER) dumpAppTokensLocked();
2958 final AppWindowToken wtoken = findAppWindowToken(token);
2959 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2960 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2961 + token + " (" + wtoken + ")");
2962 return;
2963 }
2964 mAppTokens.add(index, wtoken);
2965 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2966 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 final long origId = Binder.clearCallingIdentity();
2969 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
2970 if (DEBUG_REORDER) dumpWindowsLocked();
2971 if (tmpRemoveAppWindowsLocked(wtoken)) {
2972 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
2973 if (DEBUG_REORDER) dumpWindowsLocked();
2974 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
2975 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
2976 if (DEBUG_REORDER) dumpWindowsLocked();
2977 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 mLayoutNeeded = true;
2979 performLayoutAndPlaceSurfacesLocked();
2980 }
2981 Binder.restoreCallingIdentity(origId);
2982 }
2983 }
2984
2985 private void removeAppTokensLocked(List<IBinder> tokens) {
2986 // XXX This should be done more efficiently!
2987 // (take advantage of the fact that both lists should be
2988 // ordered in the same way.)
2989 int N = tokens.size();
2990 for (int i=0; i<N; i++) {
2991 IBinder token = tokens.get(i);
2992 final AppWindowToken wtoken = findAppWindowToken(token);
2993 if (!mAppTokens.remove(wtoken)) {
2994 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2995 + token + " (" + wtoken + ")");
2996 i--;
2997 N--;
2998 }
2999 }
3000 }
3001
3002 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3003 // First remove all of the windows from the list.
3004 final int N = tokens.size();
3005 int i;
3006 for (i=0; i<N; i++) {
3007 WindowToken token = mTokenMap.get(tokens.get(i));
3008 if (token != null) {
3009 tmpRemoveAppWindowsLocked(token);
3010 }
3011 }
3012
3013 // Where to start adding?
3014 int pos = findWindowOffsetLocked(tokenPos);
3015
3016 // And now add them back at the correct place.
3017 for (i=0; i<N; i++) {
3018 WindowToken token = mTokenMap.get(tokens.get(i));
3019 if (token != null) {
3020 pos = reAddAppWindowsLocked(pos, token);
3021 }
3022 }
3023
3024 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 mLayoutNeeded = true;
3026 performLayoutAndPlaceSurfacesLocked();
3027
3028 //dump();
3029 }
3030
3031 public void moveAppTokensToTop(List<IBinder> tokens) {
3032 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3033 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003034 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 }
3036
3037 final long origId = Binder.clearCallingIdentity();
3038 synchronized(mWindowMap) {
3039 removeAppTokensLocked(tokens);
3040 final int N = tokens.size();
3041 for (int i=0; i<N; i++) {
3042 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3043 if (wt != null) {
3044 mAppTokens.add(wt);
3045 }
3046 }
3047 moveAppWindowsLocked(tokens, mAppTokens.size());
3048 }
3049 Binder.restoreCallingIdentity(origId);
3050 }
3051
3052 public void moveAppTokensToBottom(List<IBinder> tokens) {
3053 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3054 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003055 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 }
3057
3058 final long origId = Binder.clearCallingIdentity();
3059 synchronized(mWindowMap) {
3060 removeAppTokensLocked(tokens);
3061 final int N = tokens.size();
3062 int pos = 0;
3063 for (int i=0; i<N; i++) {
3064 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3065 if (wt != null) {
3066 mAppTokens.add(pos, wt);
3067 pos++;
3068 }
3069 }
3070 moveAppWindowsLocked(tokens, 0);
3071 }
3072 Binder.restoreCallingIdentity(origId);
3073 }
3074
3075 // -------------------------------------------------------------
3076 // Misc IWindowSession methods
3077 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 public void disableKeyguard(IBinder token, String tag) {
3080 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3081 != PackageManager.PERMISSION_GRANTED) {
3082 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3083 }
3084 mKeyguardDisabled.acquire(token, tag);
3085 }
3086
3087 public void reenableKeyguard(IBinder token) {
3088 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3089 != PackageManager.PERMISSION_GRANTED) {
3090 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3091 }
3092 synchronized (mKeyguardDisabled) {
3093 mKeyguardDisabled.release(token);
3094
3095 if (!mKeyguardDisabled.isAcquired()) {
3096 // if we are the last one to reenable the keyguard wait until
3097 // we have actaully finished reenabling until returning
3098 mWaitingUntilKeyguardReenabled = true;
3099 while (mWaitingUntilKeyguardReenabled) {
3100 try {
3101 mKeyguardDisabled.wait();
3102 } catch (InterruptedException e) {
3103 Thread.currentThread().interrupt();
3104 }
3105 }
3106 }
3107 }
3108 }
3109
3110 /**
3111 * @see android.app.KeyguardManager#exitKeyguardSecurely
3112 */
3113 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3114 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3115 != PackageManager.PERMISSION_GRANTED) {
3116 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3117 }
3118 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3119 public void onKeyguardExitResult(boolean success) {
3120 try {
3121 callback.onKeyguardExitResult(success);
3122 } catch (RemoteException e) {
3123 // Client has died, we don't care.
3124 }
3125 }
3126 });
3127 }
3128
3129 public boolean inKeyguardRestrictedInputMode() {
3130 return mPolicy.inKeyguardRestrictedKeyInputMode();
3131 }
Romain Guy06882f82009-06-10 13:36:04 -07003132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 static float fixScale(float scale) {
3134 if (scale < 0) scale = 0;
3135 else if (scale > 20) scale = 20;
3136 return Math.abs(scale);
3137 }
Romain Guy06882f82009-06-10 13:36:04 -07003138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 public void setAnimationScale(int which, float scale) {
3140 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3141 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003142 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 }
3144
3145 if (scale < 0) scale = 0;
3146 else if (scale > 20) scale = 20;
3147 scale = Math.abs(scale);
3148 switch (which) {
3149 case 0: mWindowAnimationScale = fixScale(scale); break;
3150 case 1: mTransitionAnimationScale = fixScale(scale); break;
3151 }
Romain Guy06882f82009-06-10 13:36:04 -07003152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 // Persist setting
3154 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3155 }
Romain Guy06882f82009-06-10 13:36:04 -07003156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 public void setAnimationScales(float[] scales) {
3158 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3159 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003160 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 }
3162
3163 if (scales != null) {
3164 if (scales.length >= 1) {
3165 mWindowAnimationScale = fixScale(scales[0]);
3166 }
3167 if (scales.length >= 2) {
3168 mTransitionAnimationScale = fixScale(scales[1]);
3169 }
3170 }
Romain Guy06882f82009-06-10 13:36:04 -07003171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 // Persist setting
3173 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3174 }
Romain Guy06882f82009-06-10 13:36:04 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 public float getAnimationScale(int which) {
3177 switch (which) {
3178 case 0: return mWindowAnimationScale;
3179 case 1: return mTransitionAnimationScale;
3180 }
3181 return 0;
3182 }
Romain Guy06882f82009-06-10 13:36:04 -07003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 public float[] getAnimationScales() {
3185 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3186 }
Romain Guy06882f82009-06-10 13:36:04 -07003187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 public int getSwitchState(int sw) {
3189 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3190 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003191 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 }
3193 return KeyInputQueue.getSwitchState(sw);
3194 }
Romain Guy06882f82009-06-10 13:36:04 -07003195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 public int getSwitchStateForDevice(int devid, int sw) {
3197 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3198 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003199 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 }
3201 return KeyInputQueue.getSwitchState(devid, sw);
3202 }
Romain Guy06882f82009-06-10 13:36:04 -07003203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 public int getScancodeState(int sw) {
3205 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3206 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003207 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 }
3209 return KeyInputQueue.getScancodeState(sw);
3210 }
Romain Guy06882f82009-06-10 13:36:04 -07003211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 public int getScancodeStateForDevice(int devid, int sw) {
3213 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3214 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003215 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
3217 return KeyInputQueue.getScancodeState(devid, sw);
3218 }
Romain Guy06882f82009-06-10 13:36:04 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 public int getKeycodeState(int sw) {
3221 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3222 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003223 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 return KeyInputQueue.getKeycodeState(sw);
3226 }
Romain Guy06882f82009-06-10 13:36:04 -07003227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 public int getKeycodeStateForDevice(int devid, int sw) {
3229 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3230 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003231 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 }
3233 return KeyInputQueue.getKeycodeState(devid, sw);
3234 }
Romain Guy06882f82009-06-10 13:36:04 -07003235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3237 return KeyInputQueue.hasKeys(keycodes, keyExists);
3238 }
Romain Guy06882f82009-06-10 13:36:04 -07003239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 public void enableScreenAfterBoot() {
3241 synchronized(mWindowMap) {
3242 if (mSystemBooted) {
3243 return;
3244 }
3245 mSystemBooted = true;
3246 }
Romain Guy06882f82009-06-10 13:36:04 -07003247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 performEnableScreen();
3249 }
Romain Guy06882f82009-06-10 13:36:04 -07003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 public void enableScreenIfNeededLocked() {
3252 if (mDisplayEnabled) {
3253 return;
3254 }
3255 if (!mSystemBooted) {
3256 return;
3257 }
3258 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 public void performEnableScreen() {
3262 synchronized(mWindowMap) {
3263 if (mDisplayEnabled) {
3264 return;
3265 }
3266 if (!mSystemBooted) {
3267 return;
3268 }
Romain Guy06882f82009-06-10 13:36:04 -07003269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 // Don't enable the screen until all existing windows
3271 // have been drawn.
3272 final int N = mWindows.size();
3273 for (int i=0; i<N; i++) {
3274 WindowState w = (WindowState)mWindows.get(i);
3275 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3276 return;
3277 }
3278 }
Romain Guy06882f82009-06-10 13:36:04 -07003279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 mDisplayEnabled = true;
3281 if (false) {
3282 Log.i(TAG, "ENABLING SCREEN!");
3283 StringWriter sw = new StringWriter();
3284 PrintWriter pw = new PrintWriter(sw);
3285 this.dump(null, pw, null);
3286 Log.i(TAG, sw.toString());
3287 }
3288 try {
3289 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3290 if (surfaceFlinger != null) {
3291 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3292 Parcel data = Parcel.obtain();
3293 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3294 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3295 data, null, 0);
3296 data.recycle();
3297 }
3298 } catch (RemoteException ex) {
3299 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3300 }
3301 }
Romain Guy06882f82009-06-10 13:36:04 -07003302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003306 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3307 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 }
Romain Guy06882f82009-06-10 13:36:04 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 public void setInTouchMode(boolean mode) {
3311 synchronized(mWindowMap) {
3312 mInTouchMode = mode;
3313 }
3314 }
3315
Romain Guy06882f82009-06-10 13:36:04 -07003316 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003317 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003319 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003320 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 }
3322
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003323 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 }
Romain Guy06882f82009-06-10 13:36:04 -07003325
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003326 public void setRotationUnchecked(int rotation,
3327 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 if(DEBUG_ORIENTATION) Log.v(TAG,
3329 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 long origId = Binder.clearCallingIdentity();
3332 boolean changed;
3333 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003334 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 }
Romain Guy06882f82009-06-10 13:36:04 -07003336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 if (changed) {
3338 sendNewConfiguration();
3339 synchronized(mWindowMap) {
3340 mLayoutNeeded = true;
3341 performLayoutAndPlaceSurfacesLocked();
3342 }
3343 } else if (alwaysSendConfiguration) {
3344 //update configuration ignoring orientation change
3345 sendNewConfiguration();
3346 }
Romain Guy06882f82009-06-10 13:36:04 -07003347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 Binder.restoreCallingIdentity(origId);
3349 }
Romain Guy06882f82009-06-10 13:36:04 -07003350
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003351 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 boolean changed;
3353 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3354 rotation = mRequestedRotation;
3355 } else {
3356 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003357 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
3359 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003360 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 mRotation, mDisplayEnabled);
3362 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3363 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003366 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 "Rotation changed to " + rotation
3368 + " from " + mRotation
3369 + " (forceApp=" + mForcedAppOrientation
3370 + ", req=" + mRequestedRotation + ")");
3371 mRotation = rotation;
3372 mWindowsFreezingScreen = true;
3373 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3374 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3375 2000);
3376 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003377 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 mQueue.setOrientation(rotation);
3379 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003380 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 }
3382 for (int i=mWindows.size()-1; i>=0; i--) {
3383 WindowState w = (WindowState)mWindows.get(i);
3384 if (w.mSurface != null) {
3385 w.mOrientationChanging = true;
3386 }
3387 }
3388 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3389 try {
3390 mRotationWatchers.get(i).onRotationChanged(rotation);
3391 } catch (RemoteException e) {
3392 }
3393 }
3394 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 return changed;
3397 }
Romain Guy06882f82009-06-10 13:36:04 -07003398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 public int getRotation() {
3400 return mRotation;
3401 }
3402
3403 public int watchRotation(IRotationWatcher watcher) {
3404 final IBinder watcherBinder = watcher.asBinder();
3405 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3406 public void binderDied() {
3407 synchronized (mWindowMap) {
3408 for (int i=0; i<mRotationWatchers.size(); i++) {
3409 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
3410 mRotationWatchers.remove(i);
3411 i--;
3412 }
3413 }
3414 }
3415 }
3416 };
Romain Guy06882f82009-06-10 13:36:04 -07003417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 synchronized (mWindowMap) {
3419 try {
3420 watcher.asBinder().linkToDeath(dr, 0);
3421 mRotationWatchers.add(watcher);
3422 } catch (RemoteException e) {
3423 // Client died, no cleanup needed.
3424 }
Romain Guy06882f82009-06-10 13:36:04 -07003425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 return mRotation;
3427 }
3428 }
3429
3430 /**
3431 * Starts the view server on the specified port.
3432 *
3433 * @param port The port to listener to.
3434 *
3435 * @return True if the server was successfully started, false otherwise.
3436 *
3437 * @see com.android.server.ViewServer
3438 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3439 */
3440 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003441 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 return false;
3443 }
3444
3445 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3446 return false;
3447 }
3448
3449 if (port < 1024) {
3450 return false;
3451 }
3452
3453 if (mViewServer != null) {
3454 if (!mViewServer.isRunning()) {
3455 try {
3456 return mViewServer.start();
3457 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003458 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 }
3460 }
3461 return false;
3462 }
3463
3464 try {
3465 mViewServer = new ViewServer(this, port);
3466 return mViewServer.start();
3467 } catch (IOException e) {
3468 Log.w(TAG, "View server did not start");
3469 }
3470 return false;
3471 }
3472
Romain Guy06882f82009-06-10 13:36:04 -07003473 private boolean isSystemSecure() {
3474 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3475 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3476 }
3477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 /**
3479 * Stops the view server if it exists.
3480 *
3481 * @return True if the server stopped, false if it wasn't started or
3482 * couldn't be stopped.
3483 *
3484 * @see com.android.server.ViewServer
3485 */
3486 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003487 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 return false;
3489 }
3490
3491 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3492 return false;
3493 }
3494
3495 if (mViewServer != null) {
3496 return mViewServer.stop();
3497 }
3498 return false;
3499 }
3500
3501 /**
3502 * Indicates whether the view server is running.
3503 *
3504 * @return True if the server is running, false otherwise.
3505 *
3506 * @see com.android.server.ViewServer
3507 */
3508 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003509 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 return false;
3511 }
3512
3513 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3514 return false;
3515 }
3516
3517 return mViewServer != null && mViewServer.isRunning();
3518 }
3519
3520 /**
3521 * Lists all availble windows in the system. The listing is written in the
3522 * specified Socket's output stream with the following syntax:
3523 * windowHashCodeInHexadecimal windowName
3524 * Each line of the ouput represents a different window.
3525 *
3526 * @param client The remote client to send the listing to.
3527 * @return False if an error occured, true otherwise.
3528 */
3529 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003530 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 return false;
3532 }
3533
3534 boolean result = true;
3535
3536 Object[] windows;
3537 synchronized (mWindowMap) {
3538 windows = new Object[mWindows.size()];
3539 //noinspection unchecked
3540 windows = mWindows.toArray(windows);
3541 }
3542
3543 BufferedWriter out = null;
3544
3545 // Any uncaught exception will crash the system process
3546 try {
3547 OutputStream clientStream = client.getOutputStream();
3548 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3549
3550 final int count = windows.length;
3551 for (int i = 0; i < count; i++) {
3552 final WindowState w = (WindowState) windows[i];
3553 out.write(Integer.toHexString(System.identityHashCode(w)));
3554 out.write(' ');
3555 out.append(w.mAttrs.getTitle());
3556 out.write('\n');
3557 }
3558
3559 out.write("DONE.\n");
3560 out.flush();
3561 } catch (Exception e) {
3562 result = false;
3563 } finally {
3564 if (out != null) {
3565 try {
3566 out.close();
3567 } catch (IOException e) {
3568 result = false;
3569 }
3570 }
3571 }
3572
3573 return result;
3574 }
3575
3576 /**
3577 * Sends a command to a target window. The result of the command, if any, will be
3578 * written in the output stream of the specified socket.
3579 *
3580 * The parameters must follow this syntax:
3581 * windowHashcode extra
3582 *
3583 * Where XX is the length in characeters of the windowTitle.
3584 *
3585 * The first parameter is the target window. The window with the specified hashcode
3586 * will be the target. If no target can be found, nothing happens. The extra parameters
3587 * will be delivered to the target window and as parameters to the command itself.
3588 *
3589 * @param client The remote client to sent the result, if any, to.
3590 * @param command The command to execute.
3591 * @param parameters The command parameters.
3592 *
3593 * @return True if the command was successfully delivered, false otherwise. This does
3594 * not indicate whether the command itself was successful.
3595 */
3596 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003597 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 return false;
3599 }
3600
3601 boolean success = true;
3602 Parcel data = null;
3603 Parcel reply = null;
3604
3605 // Any uncaught exception will crash the system process
3606 try {
3607 // Find the hashcode of the window
3608 int index = parameters.indexOf(' ');
3609 if (index == -1) {
3610 index = parameters.length();
3611 }
3612 final String code = parameters.substring(0, index);
3613 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3614
3615 // Extract the command's parameter after the window description
3616 if (index < parameters.length()) {
3617 parameters = parameters.substring(index + 1);
3618 } else {
3619 parameters = "";
3620 }
3621
3622 final WindowManagerService.WindowState window = findWindow(hashCode);
3623 if (window == null) {
3624 return false;
3625 }
3626
3627 data = Parcel.obtain();
3628 data.writeInterfaceToken("android.view.IWindow");
3629 data.writeString(command);
3630 data.writeString(parameters);
3631 data.writeInt(1);
3632 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3633
3634 reply = Parcel.obtain();
3635
3636 final IBinder binder = window.mClient.asBinder();
3637 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3638 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3639
3640 reply.readException();
3641
3642 } catch (Exception e) {
3643 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3644 success = false;
3645 } finally {
3646 if (data != null) {
3647 data.recycle();
3648 }
3649 if (reply != null) {
3650 reply.recycle();
3651 }
3652 }
3653
3654 return success;
3655 }
3656
3657 private WindowState findWindow(int hashCode) {
3658 if (hashCode == -1) {
3659 return getFocusedWindow();
3660 }
3661
3662 synchronized (mWindowMap) {
3663 final ArrayList windows = mWindows;
3664 final int count = windows.size();
3665
3666 for (int i = 0; i < count; i++) {
3667 WindowState w = (WindowState) windows.get(i);
3668 if (System.identityHashCode(w) == hashCode) {
3669 return w;
3670 }
3671 }
3672 }
3673
3674 return null;
3675 }
3676
3677 /*
3678 * Instruct the Activity Manager to fetch the current configuration and broadcast
3679 * that to config-changed listeners if appropriate.
3680 */
3681 void sendNewConfiguration() {
3682 try {
3683 mActivityManager.updateConfiguration(null);
3684 } catch (RemoteException e) {
3685 }
3686 }
Romain Guy06882f82009-06-10 13:36:04 -07003687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 public Configuration computeNewConfiguration() {
3689 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003690 return computeNewConfigurationLocked();
3691 }
3692 }
Romain Guy06882f82009-06-10 13:36:04 -07003693
Dianne Hackbornc485a602009-03-24 22:39:49 -07003694 Configuration computeNewConfigurationLocked() {
3695 Configuration config = new Configuration();
3696 if (!computeNewConfigurationLocked(config)) {
3697 return null;
3698 }
3699 Log.i(TAG, "Config changed: " + config);
3700 long now = SystemClock.uptimeMillis();
3701 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3702 if (mFreezeGcPending != 0) {
3703 if (now > (mFreezeGcPending+1000)) {
3704 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3705 mH.removeMessages(H.FORCE_GC);
3706 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 mFreezeGcPending = now;
3708 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003709 } else {
3710 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003712 return config;
3713 }
Romain Guy06882f82009-06-10 13:36:04 -07003714
Dianne Hackbornc485a602009-03-24 22:39:49 -07003715 boolean computeNewConfigurationLocked(Configuration config) {
3716 if (mDisplay == null) {
3717 return false;
3718 }
3719 mQueue.getInputConfiguration(config);
3720 final int dw = mDisplay.getWidth();
3721 final int dh = mDisplay.getHeight();
3722 int orientation = Configuration.ORIENTATION_SQUARE;
3723 if (dw < dh) {
3724 orientation = Configuration.ORIENTATION_PORTRAIT;
3725 } else if (dw > dh) {
3726 orientation = Configuration.ORIENTATION_LANDSCAPE;
3727 }
3728 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003729
3730 if (screenLayout == Configuration.SCREENLAYOUT_UNDEFINED) {
3731 // Note we only do this once because at this point we don't
3732 // expect the screen to change in this way at runtime, and want
3733 // to avoid all of this computation for every config change.
3734 DisplayMetrics dm = new DisplayMetrics();
3735 mDisplay.getMetrics(dm);
3736 int longSize = dw;
3737 int shortSize = dh;
3738 if (longSize < shortSize) {
3739 int tmp = longSize;
3740 longSize = shortSize;
3741 shortSize = tmp;
3742 }
3743 longSize = (int)(longSize/dm.density);
3744 shortSize = (int)(shortSize/dm.density);
3745
3746 // These semi-magic numbers define our compatibility modes for
3747 // applications with different screens. Don't change unless you
3748 // make sure to test lots and lots of apps!
3749 if (longSize < 470) {
3750 // This is shorter than an HVGA normal density screen (which
3751 // is 480 pixels on its long side).
3752 screenLayout = Configuration.SCREENLAYOUT_SMALL;
3753 } else if (longSize > 490 && shortSize > 330) {
3754 // This is larger than an HVGA normal density screen (which
3755 // is 480x320 pixels).
3756 screenLayout = Configuration.SCREENLAYOUT_LARGE;
3757 } else {
3758 screenLayout = Configuration.SCREENLAYOUT_NORMAL;
3759 }
3760 }
3761 config.screenLayout = screenLayout;
3762
Dianne Hackbornc485a602009-03-24 22:39:49 -07003763 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3764 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3765 mPolicy.adjustConfigurationLw(config);
3766 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 }
Romain Guy06882f82009-06-10 13:36:04 -07003768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 // -------------------------------------------------------------
3770 // Input Events and Focus Management
3771 // -------------------------------------------------------------
3772
3773 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003774 long curTime = SystemClock.uptimeMillis();
3775
3776 if (eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
3777 if (mLastTouchEventType == eventType &&
3778 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3779 return;
3780 }
3781 mLastUserActivityCallTime = curTime;
3782 mLastTouchEventType = eventType;
3783 }
3784
3785 if (targetWin == null
3786 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3787 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 }
3789 }
3790
3791 // tells if it's a cheek event or not -- this function is stateful
3792 private static final int EVENT_NONE = 0;
3793 private static final int EVENT_UNKNOWN = 0;
3794 private static final int EVENT_CHEEK = 0;
3795 private static final int EVENT_IGNORE_DURATION = 300; // ms
3796 private static final float CHEEK_THRESHOLD = 0.6f;
3797 private int mEventState = EVENT_NONE;
3798 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 private int eventType(MotionEvent ev) {
3801 float size = ev.getSize();
3802 switch (ev.getAction()) {
3803 case MotionEvent.ACTION_DOWN:
3804 mEventSize = size;
3805 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3806 case MotionEvent.ACTION_UP:
3807 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003808 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 case MotionEvent.ACTION_MOVE:
3810 final int N = ev.getHistorySize();
3811 if (size > mEventSize) mEventSize = size;
3812 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3813 for (int i=0; i<N; i++) {
3814 size = ev.getHistoricalSize(i);
3815 if (size > mEventSize) mEventSize = size;
3816 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3817 }
3818 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3819 return TOUCH_EVENT;
3820 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003821 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 }
3823 default:
3824 // not good
3825 return OTHER_EVENT;
3826 }
3827 }
3828
3829 /**
3830 * @return Returns true if event was dispatched, false if it was dropped for any reason
3831 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003832 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3834 "dispatchPointer " + ev);
3835
3836 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003837 ev, true, false, pid, uid);
Romain Guy06882f82009-06-10 13:36:04 -07003838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 if (action == MotionEvent.ACTION_UP) {
3842 // let go of our target
3843 mKeyWaiter.mMotionTarget = null;
3844 mPowerManager.logPointerUpEvent();
3845 } else if (action == MotionEvent.ACTION_DOWN) {
3846 mPowerManager.logPointerDownEvent();
3847 }
3848
3849 if (targetObj == null) {
3850 // In this case we are either dropping the event, or have received
3851 // a move or up without a down. It is common to receive move
3852 // events in such a way, since this means the user is moving the
3853 // pointer without actually pressing down. All other cases should
3854 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003855 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3857 }
3858 if (qev != null) {
3859 mQueue.recycleEvent(qev);
3860 }
3861 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003862 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 }
3864 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3865 if (qev != null) {
3866 mQueue.recycleEvent(qev);
3867 }
3868 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003869 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 }
Romain Guy06882f82009-06-10 13:36:04 -07003871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07003875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 //Log.i(TAG, "Sending " + ev + " to " + target);
3877
3878 if (uid != 0 && uid != target.mSession.mUid) {
3879 if (mContext.checkPermission(
3880 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3881 != PackageManager.PERMISSION_GRANTED) {
3882 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3883 + pid + " uid " + uid + " to window " + target
3884 + " owned by uid " + target.mSession.mUid);
3885 if (qev != null) {
3886 mQueue.recycleEvent(qev);
3887 }
3888 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003889 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 }
3891 }
Romain Guy06882f82009-06-10 13:36:04 -07003892
3893 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3895 //target wants to ignore fat touch events
3896 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3897 //explicit flag to return without processing event further
3898 boolean returnFlag = false;
3899 if((action == MotionEvent.ACTION_DOWN)) {
3900 mFatTouch = false;
3901 if(cheekPress) {
3902 mFatTouch = true;
3903 returnFlag = true;
3904 }
3905 } else {
3906 if(action == MotionEvent.ACTION_UP) {
3907 if(mFatTouch) {
3908 //earlier even was invalid doesnt matter if current up is cheekpress or not
3909 mFatTouch = false;
3910 returnFlag = true;
3911 } else if(cheekPress) {
3912 //cancel the earlier event
3913 ev.setAction(MotionEvent.ACTION_CANCEL);
3914 action = MotionEvent.ACTION_CANCEL;
3915 }
3916 } else if(action == MotionEvent.ACTION_MOVE) {
3917 if(mFatTouch) {
3918 //two cases here
3919 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003920 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 returnFlag = true;
3922 } else if(cheekPress) {
3923 //valid down followed by invalid moves
3924 //an invalid move have to cancel earlier action
3925 ev.setAction(MotionEvent.ACTION_CANCEL);
3926 action = MotionEvent.ACTION_CANCEL;
3927 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3928 //note that the subsequent invalid moves will not get here
3929 mFatTouch = true;
3930 }
3931 }
3932 } //else if action
3933 if(returnFlag) {
3934 //recycle que, ev
3935 if (qev != null) {
3936 mQueue.recycleEvent(qev);
3937 }
3938 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003939 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 }
3941 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07003942
3943 // TODO remove once we settle on a value or make it app specific
3944 if (action == MotionEvent.ACTION_DOWN) {
3945 int max_events_per_sec = 35;
3946 try {
3947 max_events_per_sec = Integer.parseInt(SystemProperties
3948 .get("windowsmgr.max_events_per_sec"));
3949 if (max_events_per_sec < 1) {
3950 max_events_per_sec = 35;
3951 }
3952 } catch (NumberFormatException e) {
3953 }
3954 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
3955 }
3956
3957 /*
3958 * Throttle events to minimize CPU usage when there's a flood of events
3959 * e.g. constant contact with the screen
3960 */
3961 if (action == MotionEvent.ACTION_MOVE) {
3962 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
3963 long now = SystemClock.uptimeMillis();
3964 if (now < nextEventTime) {
3965 try {
3966 Thread.sleep(nextEventTime - now);
3967 } catch (InterruptedException e) {
3968 }
3969 mLastTouchEventTime = nextEventTime;
3970 } else {
3971 mLastTouchEventTime = now;
3972 }
3973 }
3974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 synchronized(mWindowMap) {
3976 if (qev != null && action == MotionEvent.ACTION_MOVE) {
3977 mKeyWaiter.bindTargetWindowLocked(target,
3978 KeyWaiter.RETURN_PENDING_POINTER, qev);
3979 ev = null;
3980 } else {
3981 if (action == MotionEvent.ACTION_DOWN) {
3982 WindowState out = mKeyWaiter.mOutsideTouchTargets;
3983 if (out != null) {
3984 MotionEvent oev = MotionEvent.obtain(ev);
3985 oev.setAction(MotionEvent.ACTION_OUTSIDE);
3986 do {
3987 final Rect frame = out.mFrame;
3988 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
3989 try {
3990 out.mClient.dispatchPointer(oev, eventTime);
3991 } catch (android.os.RemoteException e) {
3992 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
3993 }
3994 oev.offsetLocation((float)frame.left, (float)frame.top);
3995 out = out.mNextOutsideTouch;
3996 } while (out != null);
3997 mKeyWaiter.mOutsideTouchTargets = null;
3998 }
3999 }
4000 final Rect frame = target.mFrame;
4001 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4002 mKeyWaiter.bindTargetWindowLocked(target);
4003 }
4004 }
Romain Guy06882f82009-06-10 13:36:04 -07004005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 // finally offset the event to the target's coordinate system and
4007 // dispatch the event.
4008 try {
4009 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4010 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4011 }
4012 target.mClient.dispatchPointer(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004013 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 } catch (android.os.RemoteException e) {
4015 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4016 mKeyWaiter.mMotionTarget = null;
4017 try {
4018 removeWindow(target.mSession, target.mClient);
4019 } catch (java.util.NoSuchElementException ex) {
4020 // This will happen if the window has already been
4021 // removed.
4022 }
4023 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004024 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 }
Romain Guy06882f82009-06-10 13:36:04 -07004026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 /**
4028 * @return Returns true if event was dispatched, false if it was dropped for any reason
4029 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004030 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 if (DEBUG_INPUT) Log.v(
4032 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004035 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 if (focusObj == null) {
4037 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4038 if (qev != null) {
4039 mQueue.recycleEvent(qev);
4040 }
4041 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004042 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 }
4044 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4045 if (qev != null) {
4046 mQueue.recycleEvent(qev);
4047 }
4048 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004049 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 }
Romain Guy06882f82009-06-10 13:36:04 -07004051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 if (uid != 0 && uid != focus.mSession.mUid) {
4055 if (mContext.checkPermission(
4056 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4057 != PackageManager.PERMISSION_GRANTED) {
4058 Log.w(TAG, "Permission denied: injecting key event from pid "
4059 + pid + " uid " + uid + " to window " + focus
4060 + " owned by uid " + focus.mSession.mUid);
4061 if (qev != null) {
4062 mQueue.recycleEvent(qev);
4063 }
4064 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004065 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
4067 }
Romain Guy06882f82009-06-10 13:36:04 -07004068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 synchronized(mWindowMap) {
4072 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4073 mKeyWaiter.bindTargetWindowLocked(focus,
4074 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4075 // We don't deliver movement events to the client, we hold
4076 // them and wait for them to call back.
4077 ev = null;
4078 } else {
4079 mKeyWaiter.bindTargetWindowLocked(focus);
4080 }
4081 }
Romain Guy06882f82009-06-10 13:36:04 -07004082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 try {
4084 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004085 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 } catch (android.os.RemoteException e) {
4087 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4088 try {
4089 removeWindow(focus.mSession, focus.mClient);
4090 } catch (java.util.NoSuchElementException ex) {
4091 // This will happen if the window has already been
4092 // removed.
4093 }
4094 }
Romain Guy06882f82009-06-10 13:36:04 -07004095
Dianne Hackborncfaef692009-06-15 14:24:44 -07004096 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 }
Romain Guy06882f82009-06-10 13:36:04 -07004098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 /**
4100 * @return Returns true if event was dispatched, false if it was dropped for any reason
4101 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004102 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4104
4105 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004106 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 if (focusObj == null) {
4108 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004109 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 }
4111 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004112 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 }
Romain Guy06882f82009-06-10 13:36:04 -07004114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 if (DEBUG_INPUT) Log.v(
4118 TAG, "Dispatching to " + focus + ": " + event);
4119
4120 if (uid != 0 && uid != focus.mSession.mUid) {
4121 if (mContext.checkPermission(
4122 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4123 != PackageManager.PERMISSION_GRANTED) {
4124 Log.w(TAG, "Permission denied: injecting key event from pid "
4125 + pid + " uid " + uid + " to window " + focus
4126 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004127 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 }
4129 }
Romain Guy06882f82009-06-10 13:36:04 -07004130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 synchronized(mWindowMap) {
4132 mKeyWaiter.bindTargetWindowLocked(focus);
4133 }
4134
4135 // NOSHIP extra state logging
4136 mKeyWaiter.recordDispatchState(event, focus);
4137 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 try {
4140 if (DEBUG_INPUT || DEBUG_FOCUS) {
4141 Log.v(TAG, "Delivering key " + event.getKeyCode()
4142 + " to " + focus);
4143 }
4144 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004145 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 } catch (android.os.RemoteException e) {
4147 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4148 try {
4149 removeWindow(focus.mSession, focus.mClient);
4150 } catch (java.util.NoSuchElementException ex) {
4151 // This will happen if the window has already been
4152 // removed.
4153 }
4154 }
Romain Guy06882f82009-06-10 13:36:04 -07004155
Dianne Hackborncfaef692009-06-15 14:24:44 -07004156 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 }
Romain Guy06882f82009-06-10 13:36:04 -07004158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 public void pauseKeyDispatching(IBinder _token) {
4160 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4161 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004162 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 }
4164
4165 synchronized (mWindowMap) {
4166 WindowToken token = mTokenMap.get(_token);
4167 if (token != null) {
4168 mKeyWaiter.pauseDispatchingLocked(token);
4169 }
4170 }
4171 }
4172
4173 public void resumeKeyDispatching(IBinder _token) {
4174 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4175 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004176 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 }
4178
4179 synchronized (mWindowMap) {
4180 WindowToken token = mTokenMap.get(_token);
4181 if (token != null) {
4182 mKeyWaiter.resumeDispatchingLocked(token);
4183 }
4184 }
4185 }
4186
4187 public void setEventDispatching(boolean enabled) {
4188 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4189 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004190 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 }
4192
4193 synchronized (mWindowMap) {
4194 mKeyWaiter.setEventDispatchingLocked(enabled);
4195 }
4196 }
Romain Guy06882f82009-06-10 13:36:04 -07004197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 /**
4199 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004200 *
4201 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 * {@link SystemClock#uptimeMillis()} as the timebase.)
4203 * @param sync If true, wait for the event to be completed before returning to the caller.
4204 * @return Returns true if event was dispatched, false if it was dropped for any reason
4205 */
4206 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4207 long downTime = ev.getDownTime();
4208 long eventTime = ev.getEventTime();
4209
4210 int action = ev.getAction();
4211 int code = ev.getKeyCode();
4212 int repeatCount = ev.getRepeatCount();
4213 int metaState = ev.getMetaState();
4214 int deviceId = ev.getDeviceId();
4215 int scancode = ev.getScanCode();
4216
4217 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4218 if (downTime == 0) downTime = eventTime;
4219
4220 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004221 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004223 final int pid = Binder.getCallingPid();
4224 final int uid = Binder.getCallingUid();
4225 final long ident = Binder.clearCallingIdentity();
4226 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004228 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004230 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004231 switch (result) {
4232 case INJECT_NO_PERMISSION:
4233 throw new SecurityException(
4234 "Injecting to another application requires INJECT_EVENT permission");
4235 case INJECT_SUCCEEDED:
4236 return true;
4237 }
4238 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 }
4240
4241 /**
4242 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004243 *
4244 * @param ev A motion event describing the pointer (touch) action. (As noted in
4245 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 * {@link SystemClock#uptimeMillis()} as the timebase.)
4247 * @param sync If true, wait for the event to be completed before returning to the caller.
4248 * @return Returns true if event was dispatched, false if it was dropped for any reason
4249 */
4250 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004251 final int pid = Binder.getCallingPid();
4252 final int uid = Binder.getCallingUid();
4253 final long ident = Binder.clearCallingIdentity();
4254 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004256 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004258 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004259 switch (result) {
4260 case INJECT_NO_PERMISSION:
4261 throw new SecurityException(
4262 "Injecting to another application requires INJECT_EVENT permission");
4263 case INJECT_SUCCEEDED:
4264 return true;
4265 }
4266 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 }
Romain Guy06882f82009-06-10 13:36:04 -07004268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 /**
4270 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004271 *
4272 * @param ev A motion event describing the trackball action. (As noted in
4273 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 * {@link SystemClock#uptimeMillis()} as the timebase.)
4275 * @param sync If true, wait for the event to be completed before returning to the caller.
4276 * @return Returns true if event was dispatched, false if it was dropped for any reason
4277 */
4278 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004279 final int pid = Binder.getCallingPid();
4280 final int uid = Binder.getCallingUid();
4281 final long ident = Binder.clearCallingIdentity();
4282 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004284 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004286 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004287 switch (result) {
4288 case INJECT_NO_PERMISSION:
4289 throw new SecurityException(
4290 "Injecting to another application requires INJECT_EVENT permission");
4291 case INJECT_SUCCEEDED:
4292 return true;
4293 }
4294 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 }
Romain Guy06882f82009-06-10 13:36:04 -07004296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 private WindowState getFocusedWindow() {
4298 synchronized (mWindowMap) {
4299 return getFocusedWindowLocked();
4300 }
4301 }
4302
4303 private WindowState getFocusedWindowLocked() {
4304 return mCurrentFocus;
4305 }
Romain Guy06882f82009-06-10 13:36:04 -07004306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 /**
4308 * This class holds the state for dispatching key events. This state
4309 * is protected by the KeyWaiter instance, NOT by the window lock. You
4310 * can be holding the main window lock while acquire the KeyWaiter lock,
4311 * but not the other way around.
4312 */
4313 final class KeyWaiter {
4314 // NOSHIP debugging
4315 public class DispatchState {
4316 private KeyEvent event;
4317 private WindowState focus;
4318 private long time;
4319 private WindowState lastWin;
4320 private IBinder lastBinder;
4321 private boolean finished;
4322 private boolean gotFirstWindow;
4323 private boolean eventDispatching;
4324 private long timeToSwitch;
4325 private boolean wasFrozen;
4326 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004327 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4330 focus = theFocus;
4331 event = theEvent;
4332 time = System.currentTimeMillis();
4333 // snapshot KeyWaiter state
4334 lastWin = mLastWin;
4335 lastBinder = mLastBinder;
4336 finished = mFinished;
4337 gotFirstWindow = mGotFirstWindow;
4338 eventDispatching = mEventDispatching;
4339 timeToSwitch = mTimeToSwitch;
4340 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004341 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 // cache the paused state at ctor time as well
4343 if (theFocus == null || theFocus.mToken == null) {
4344 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4345 focusPaused = false;
4346 } else {
4347 focusPaused = theFocus.mToken.paused;
4348 }
4349 }
Romain Guy06882f82009-06-10 13:36:04 -07004350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 public String toString() {
4352 return "{{" + event + " to " + focus + " @ " + time
4353 + " lw=" + lastWin + " lb=" + lastBinder
4354 + " fin=" + finished + " gfw=" + gotFirstWindow
4355 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004356 + " wf=" + wasFrozen + " fp=" + focusPaused
4357 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
4359 };
4360 private DispatchState mDispatchState = null;
4361 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4362 mDispatchState = new DispatchState(theEvent, theFocus);
4363 }
4364 // END NOSHIP
4365
4366 public static final int RETURN_NOTHING = 0;
4367 public static final int RETURN_PENDING_POINTER = 1;
4368 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 final Object SKIP_TARGET_TOKEN = new Object();
4371 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 private WindowState mLastWin = null;
4374 private IBinder mLastBinder = null;
4375 private boolean mFinished = true;
4376 private boolean mGotFirstWindow = false;
4377 private boolean mEventDispatching = true;
4378 private long mTimeToSwitch = 0;
4379 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 // Target of Motion events
4382 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 // Windows above the target who would like to receive an "outside"
4385 // touch event for any down events outside of them.
4386 WindowState mOutsideTouchTargets;
4387
4388 /**
4389 * Wait for the last event dispatch to complete, then find the next
4390 * target that should receive the given event and wait for that one
4391 * to be ready to receive it.
4392 */
4393 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4394 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004395 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 long startTime = SystemClock.uptimeMillis();
4397 long keyDispatchingTimeout = 5 * 1000;
4398 long waitedFor = 0;
4399
4400 while (true) {
4401 // Figure out which window we care about. It is either the
4402 // last window we are waiting to have process the event or,
4403 // if none, then the next window we think the event should go
4404 // to. Note: we retrieve mLastWin outside of the lock, so
4405 // it may change before we lock. Thus we must check it again.
4406 WindowState targetWin = mLastWin;
4407 boolean targetIsNew = targetWin == null;
4408 if (DEBUG_INPUT) Log.v(
4409 TAG, "waitForLastKey: mFinished=" + mFinished +
4410 ", mLastWin=" + mLastWin);
4411 if (targetIsNew) {
4412 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004413 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 if (target == SKIP_TARGET_TOKEN) {
4415 // The user has pressed a special key, and we are
4416 // dropping all pending events before it.
4417 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4418 + " " + nextMotion);
4419 return null;
4420 }
4421 if (target == CONSUMED_EVENT_TOKEN) {
4422 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4423 + " " + nextMotion);
4424 return target;
4425 }
4426 targetWin = (WindowState)target;
4427 }
Romain Guy06882f82009-06-10 13:36:04 -07004428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 // Now: is it okay to send the next event to this window?
4432 synchronized (this) {
4433 // First: did we come here based on the last window not
4434 // being null, but it changed by the time we got here?
4435 // If so, try again.
4436 if (!targetIsNew && mLastWin == null) {
4437 continue;
4438 }
Romain Guy06882f82009-06-10 13:36:04 -07004439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 // We never dispatch events if not finished with the
4441 // last one, or the display is frozen.
4442 if (mFinished && !mDisplayFrozen) {
4443 // If event dispatching is disabled, then we
4444 // just consume the events.
4445 if (!mEventDispatching) {
4446 if (DEBUG_INPUT) Log.v(TAG,
4447 "Skipping event; dispatching disabled: "
4448 + nextKey + " " + nextMotion);
4449 return null;
4450 }
4451 if (targetWin != null) {
4452 // If this is a new target, and that target is not
4453 // paused or unresponsive, then all looks good to
4454 // handle the event.
4455 if (targetIsNew && !targetWin.mToken.paused) {
4456 return targetWin;
4457 }
Romain Guy06882f82009-06-10 13:36:04 -07004458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 // If we didn't find a target window, and there is no
4460 // focused app window, then just eat the events.
4461 } else if (mFocusedApp == null) {
4462 if (DEBUG_INPUT) Log.v(TAG,
4463 "Skipping event; no focused app: "
4464 + nextKey + " " + nextMotion);
4465 return null;
4466 }
4467 }
Romain Guy06882f82009-06-10 13:36:04 -07004468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 if (DEBUG_INPUT) Log.v(
4470 TAG, "Waiting for last key in " + mLastBinder
4471 + " target=" + targetWin
4472 + " mFinished=" + mFinished
4473 + " mDisplayFrozen=" + mDisplayFrozen
4474 + " targetIsNew=" + targetIsNew
4475 + " paused="
4476 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004477 + " mFocusedApp=" + mFocusedApp
4478 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 targetApp = targetWin != null
4481 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 long curTimeout = keyDispatchingTimeout;
4484 if (mTimeToSwitch != 0) {
4485 long now = SystemClock.uptimeMillis();
4486 if (mTimeToSwitch <= now) {
4487 // If an app switch key has been pressed, and we have
4488 // waited too long for the current app to finish
4489 // processing keys, then wait no more!
4490 doFinishedKeyLocked(true);
4491 continue;
4492 }
4493 long switchTimeout = mTimeToSwitch - now;
4494 if (curTimeout > switchTimeout) {
4495 curTimeout = switchTimeout;
4496 }
4497 }
Romain Guy06882f82009-06-10 13:36:04 -07004498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 try {
4500 // after that continue
4501 // processing keys, so we don't get stuck.
4502 if (DEBUG_INPUT) Log.v(
4503 TAG, "Waiting for key dispatch: " + curTimeout);
4504 wait(curTimeout);
4505 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4506 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004507 + startTime + " switchTime=" + mTimeToSwitch
4508 + " target=" + targetWin + " mLW=" + mLastWin
4509 + " mLB=" + mLastBinder + " fin=" + mFinished
4510 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 } catch (InterruptedException e) {
4512 }
4513 }
4514
4515 // If we were frozen during configuration change, restart the
4516 // timeout checks from now; otherwise look at whether we timed
4517 // out before awakening.
4518 if (mWasFrozen) {
4519 waitedFor = 0;
4520 mWasFrozen = false;
4521 } else {
4522 waitedFor = SystemClock.uptimeMillis() - startTime;
4523 }
4524
4525 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4526 IApplicationToken at = null;
4527 synchronized (this) {
4528 Log.w(TAG, "Key dispatching timed out sending to " +
4529 (targetWin != null ? targetWin.mAttrs.getTitle()
4530 : "<null>"));
4531 // NOSHIP debugging
4532 Log.w(TAG, "Dispatch state: " + mDispatchState);
4533 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4534 // END NOSHIP
4535 //dump();
4536 if (targetWin != null) {
4537 at = targetWin.getAppToken();
4538 } else if (targetApp != null) {
4539 at = targetApp.appToken;
4540 }
4541 }
4542
4543 boolean abort = true;
4544 if (at != null) {
4545 try {
4546 long timeout = at.getKeyDispatchingTimeout();
4547 if (timeout > waitedFor) {
4548 // we did not wait the proper amount of time for this application.
4549 // set the timeout to be the real timeout and wait again.
4550 keyDispatchingTimeout = timeout - waitedFor;
4551 continue;
4552 } else {
4553 abort = at.keyDispatchingTimedOut();
4554 }
4555 } catch (RemoteException ex) {
4556 }
4557 }
4558
4559 synchronized (this) {
4560 if (abort && (mLastWin == targetWin || targetWin == null)) {
4561 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004562 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 if (DEBUG_INPUT) Log.v(TAG,
4564 "Window " + mLastWin +
4565 " timed out on key input");
4566 if (mLastWin.mToken.paused) {
4567 Log.w(TAG, "Un-pausing dispatching to this window");
4568 mLastWin.mToken.paused = false;
4569 }
4570 }
4571 if (mMotionTarget == targetWin) {
4572 mMotionTarget = null;
4573 }
4574 mLastWin = null;
4575 mLastBinder = null;
4576 if (failIfTimeout || targetWin == null) {
4577 return null;
4578 }
4579 } else {
4580 Log.w(TAG, "Continuing to wait for key to be dispatched");
4581 startTime = SystemClock.uptimeMillis();
4582 }
4583 }
4584 }
4585 }
4586 }
Romain Guy06882f82009-06-10 13:36:04 -07004587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004589 MotionEvent nextMotion, boolean isPointerEvent,
4590 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 if (nextKey != null) {
4594 // Find the target window for a normal key event.
4595 final int keycode = nextKey.getKeyCode();
4596 final int repeatCount = nextKey.getRepeatCount();
4597 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4598 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004601 if (callingUid == 0 ||
4602 mContext.checkPermission(
4603 android.Manifest.permission.INJECT_EVENTS,
4604 callingPid, callingUid)
4605 == PackageManager.PERMISSION_GRANTED) {
4606 mPolicy.interceptKeyTi(null, keycode,
4607 nextKey.getMetaState(), down, repeatCount);
4608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 Log.w(TAG, "Event timeout during app switch: dropping "
4610 + nextKey);
4611 return SKIP_TARGET_TOKEN;
4612 }
Romain Guy06882f82009-06-10 13:36:04 -07004613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 WindowState focus = null;
4617 synchronized(mWindowMap) {
4618 focus = getFocusedWindowLocked();
4619 }
Romain Guy06882f82009-06-10 13:36:04 -07004620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004622
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004623 if (callingUid == 0 ||
4624 (focus != null && callingUid == focus.mSession.mUid) ||
4625 mContext.checkPermission(
4626 android.Manifest.permission.INJECT_EVENTS,
4627 callingPid, callingUid)
4628 == PackageManager.PERMISSION_GRANTED) {
4629 if (mPolicy.interceptKeyTi(focus,
4630 keycode, nextKey.getMetaState(), down, repeatCount)) {
4631 return CONSUMED_EVENT_TOKEN;
4632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 }
Romain Guy06882f82009-06-10 13:36:04 -07004634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 } else if (!isPointerEvent) {
4638 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4639 if (!dispatch) {
4640 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4641 + nextMotion);
4642 return SKIP_TARGET_TOKEN;
4643 }
Romain Guy06882f82009-06-10 13:36:04 -07004644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 WindowState focus = null;
4646 synchronized(mWindowMap) {
4647 focus = getFocusedWindowLocked();
4648 }
Romain Guy06882f82009-06-10 13:36:04 -07004649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4651 return focus;
4652 }
Romain Guy06882f82009-06-10 13:36:04 -07004653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 if (nextMotion == null) {
4655 return SKIP_TARGET_TOKEN;
4656 }
Romain Guy06882f82009-06-10 13:36:04 -07004657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4659 KeyEvent.KEYCODE_UNKNOWN);
4660 if (!dispatch) {
4661 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4662 + nextMotion);
4663 return SKIP_TARGET_TOKEN;
4664 }
Romain Guy06882f82009-06-10 13:36:04 -07004665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 // Find the target window for a pointer event.
4667 int action = nextMotion.getAction();
4668 final float xf = nextMotion.getX();
4669 final float yf = nextMotion.getY();
4670 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 final boolean screenWasOff = qev != null
4673 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 synchronized(mWindowMap) {
4678 synchronized (this) {
4679 if (action == MotionEvent.ACTION_DOWN) {
4680 if (mMotionTarget != null) {
4681 // this is weird, we got a pen down, but we thought it was
4682 // already down!
4683 // XXX: We should probably send an ACTION_UP to the current
4684 // target.
4685 Log.w(TAG, "Pointer down received while already down in: "
4686 + mMotionTarget);
4687 mMotionTarget = null;
4688 }
Romain Guy06882f82009-06-10 13:36:04 -07004689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 // ACTION_DOWN is special, because we need to lock next events to
4691 // the window we'll land onto.
4692 final int x = (int)xf;
4693 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 final ArrayList windows = mWindows;
4696 final int N = windows.size();
4697 WindowState topErrWindow = null;
4698 final Rect tmpRect = mTempRect;
4699 for (int i=N-1; i>=0; i--) {
4700 WindowState child = (WindowState)windows.get(i);
4701 //Log.i(TAG, "Checking dispatch to: " + child);
4702 final int flags = child.mAttrs.flags;
4703 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4704 if (topErrWindow == null) {
4705 topErrWindow = child;
4706 }
4707 }
4708 if (!child.isVisibleLw()) {
4709 //Log.i(TAG, "Not visible!");
4710 continue;
4711 }
4712 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4713 //Log.i(TAG, "Not touchable!");
4714 if ((flags & WindowManager.LayoutParams
4715 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4716 child.mNextOutsideTouch = mOutsideTouchTargets;
4717 mOutsideTouchTargets = child;
4718 }
4719 continue;
4720 }
4721 tmpRect.set(child.mFrame);
4722 if (child.mTouchableInsets == ViewTreeObserver
4723 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4724 // The touch is inside of the window if it is
4725 // inside the frame, AND the content part of that
4726 // frame that was given by the application.
4727 tmpRect.left += child.mGivenContentInsets.left;
4728 tmpRect.top += child.mGivenContentInsets.top;
4729 tmpRect.right -= child.mGivenContentInsets.right;
4730 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4731 } else if (child.mTouchableInsets == ViewTreeObserver
4732 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4733 // The touch is inside of the window if it is
4734 // inside the frame, AND the visible part of that
4735 // frame that was given by the application.
4736 tmpRect.left += child.mGivenVisibleInsets.left;
4737 tmpRect.top += child.mGivenVisibleInsets.top;
4738 tmpRect.right -= child.mGivenVisibleInsets.right;
4739 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4740 }
4741 final int touchFlags = flags &
4742 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4743 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4744 if (tmpRect.contains(x, y) || touchFlags == 0) {
4745 //Log.i(TAG, "Using this target!");
4746 if (!screenWasOff || (flags &
4747 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4748 mMotionTarget = child;
4749 } else {
4750 //Log.i(TAG, "Waking, skip!");
4751 mMotionTarget = null;
4752 }
4753 break;
4754 }
Romain Guy06882f82009-06-10 13:36:04 -07004755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004756 if ((flags & WindowManager.LayoutParams
4757 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4758 child.mNextOutsideTouch = mOutsideTouchTargets;
4759 mOutsideTouchTargets = child;
4760 //Log.i(TAG, "Adding to outside target list: " + child);
4761 }
4762 }
4763
4764 // if there's an error window but it's not accepting
4765 // focus (typically because it is not yet visible) just
4766 // wait for it -- any other focused window may in fact
4767 // be in ANR state.
4768 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4769 mMotionTarget = null;
4770 }
4771 }
Romain Guy06882f82009-06-10 13:36:04 -07004772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 target = mMotionTarget;
4774 }
4775 }
Romain Guy06882f82009-06-10 13:36:04 -07004776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 // Pointer events are a little different -- if there isn't a
4780 // target found for any event, then just drop it.
4781 return target != null ? target : SKIP_TARGET_TOKEN;
4782 }
Romain Guy06882f82009-06-10 13:36:04 -07004783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 boolean checkShouldDispatchKey(int keycode) {
4785 synchronized (this) {
4786 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4787 mTimeToSwitch = 0;
4788 return true;
4789 }
4790 if (mTimeToSwitch != 0
4791 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4792 return false;
4793 }
4794 return true;
4795 }
4796 }
Romain Guy06882f82009-06-10 13:36:04 -07004797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004798 void bindTargetWindowLocked(WindowState win,
4799 int pendingWhat, QueuedEvent pendingMotion) {
4800 synchronized (this) {
4801 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4802 }
4803 }
Romain Guy06882f82009-06-10 13:36:04 -07004804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 void bindTargetWindowLocked(WindowState win) {
4806 synchronized (this) {
4807 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4808 }
4809 }
4810
4811 void bindTargetWindowLockedLocked(WindowState win,
4812 int pendingWhat, QueuedEvent pendingMotion) {
4813 mLastWin = win;
4814 mLastBinder = win.mClient.asBinder();
4815 mFinished = false;
4816 if (pendingMotion != null) {
4817 final Session s = win.mSession;
4818 if (pendingWhat == RETURN_PENDING_POINTER) {
4819 releasePendingPointerLocked(s);
4820 s.mPendingPointerMove = pendingMotion;
4821 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004822 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 "bindTargetToWindow " + s.mPendingPointerMove);
4824 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4825 releasePendingTrackballLocked(s);
4826 s.mPendingTrackballMove = pendingMotion;
4827 s.mPendingTrackballWindow = win;
4828 }
4829 }
4830 }
Romain Guy06882f82009-06-10 13:36:04 -07004831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 void releasePendingPointerLocked(Session s) {
4833 if (DEBUG_INPUT) Log.v(TAG,
4834 "releasePendingPointer " + s.mPendingPointerMove);
4835 if (s.mPendingPointerMove != null) {
4836 mQueue.recycleEvent(s.mPendingPointerMove);
4837 s.mPendingPointerMove = null;
4838 }
4839 }
Romain Guy06882f82009-06-10 13:36:04 -07004840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 void releasePendingTrackballLocked(Session s) {
4842 if (s.mPendingTrackballMove != null) {
4843 mQueue.recycleEvent(s.mPendingTrackballMove);
4844 s.mPendingTrackballMove = null;
4845 }
4846 }
Romain Guy06882f82009-06-10 13:36:04 -07004847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4849 int returnWhat) {
4850 if (DEBUG_INPUT) Log.v(
4851 TAG, "finishedKey: client=" + client + ", force=" + force);
4852
4853 if (client == null) {
4854 return null;
4855 }
4856
4857 synchronized (this) {
4858 if (DEBUG_INPUT) Log.v(
4859 TAG, "finishedKey: client=" + client.asBinder()
4860 + ", force=" + force + ", last=" + mLastBinder
4861 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4862
4863 QueuedEvent qev = null;
4864 WindowState win = null;
4865 if (returnWhat == RETURN_PENDING_POINTER) {
4866 qev = session.mPendingPointerMove;
4867 win = session.mPendingPointerWindow;
4868 session.mPendingPointerMove = null;
4869 session.mPendingPointerWindow = null;
4870 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4871 qev = session.mPendingTrackballMove;
4872 win = session.mPendingTrackballWindow;
4873 session.mPendingTrackballMove = null;
4874 session.mPendingTrackballWindow = null;
4875 }
Romain Guy06882f82009-06-10 13:36:04 -07004876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 if (mLastBinder == client.asBinder()) {
4878 if (DEBUG_INPUT) Log.v(
4879 TAG, "finishedKey: last paused="
4880 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4881 if (mLastWin != null && (!mLastWin.mToken.paused || force
4882 || !mEventDispatching)) {
4883 doFinishedKeyLocked(false);
4884 } else {
4885 // Make sure to wake up anyone currently waiting to
4886 // dispatch a key, so they can re-evaluate their
4887 // current situation.
4888 mFinished = true;
4889 notifyAll();
4890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004891 }
Romain Guy06882f82009-06-10 13:36:04 -07004892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004893 if (qev != null) {
4894 MotionEvent res = (MotionEvent)qev.event;
4895 if (DEBUG_INPUT) Log.v(TAG,
4896 "Returning pending motion: " + res);
4897 mQueue.recycleEvent(qev);
4898 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4899 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4900 }
4901 return res;
4902 }
4903 return null;
4904 }
4905 }
4906
4907 void tickle() {
4908 synchronized (this) {
4909 notifyAll();
4910 }
4911 }
Romain Guy06882f82009-06-10 13:36:04 -07004912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 void handleNewWindowLocked(WindowState newWindow) {
4914 if (!newWindow.canReceiveKeys()) {
4915 return;
4916 }
4917 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004918 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 TAG, "New key dispatch window: win="
4920 + newWindow.mClient.asBinder()
4921 + ", last=" + mLastBinder
4922 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4923 + "), finished=" + mFinished + ", paused="
4924 + newWindow.mToken.paused);
4925
4926 // Displaying a window implicitly causes dispatching to
4927 // be unpaused. (This is to protect against bugs if someone
4928 // pauses dispatching but forgets to resume.)
4929 newWindow.mToken.paused = false;
4930
4931 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932
4933 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4934 if (DEBUG_INPUT) Log.v(TAG,
4935 "New SYSTEM_ERROR window; resetting state");
4936 mLastWin = null;
4937 mLastBinder = null;
4938 mMotionTarget = null;
4939 mFinished = true;
4940 } else if (mLastWin != null) {
4941 // If the new window is above the window we are
4942 // waiting on, then stop waiting and let key dispatching
4943 // start on the new guy.
4944 if (DEBUG_INPUT) Log.v(
4945 TAG, "Last win layer=" + mLastWin.mLayer
4946 + ", new win layer=" + newWindow.mLayer);
4947 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004948 // The new window is above the old; finish pending input to the last
4949 // window and start directing it to the new one.
4950 mLastWin.mToken.paused = false;
4951 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004953 // Either the new window is lower, so there is no need to wake key waiters,
4954 // or we just finished key input to the previous window, which implicitly
4955 // notified the key waiters. In both cases, we don't need to issue the
4956 // notification here.
4957 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 }
4959
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004960 // Now that we've put a new window state in place, make the event waiter
4961 // take notice and retarget its attentions.
4962 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 }
4964 }
4965
4966 void pauseDispatchingLocked(WindowToken token) {
4967 synchronized (this)
4968 {
4969 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
4970 token.paused = true;
4971
4972 /*
4973 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
4974 mPaused = true;
4975 } else {
4976 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07004977 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07004979 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07004981 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 }
4983 }
4984 */
4985 }
4986 }
4987
4988 void resumeDispatchingLocked(WindowToken token) {
4989 synchronized (this) {
4990 if (token.paused) {
4991 if (DEBUG_INPUT) Log.v(
4992 TAG, "Resuming WindowToken " + token
4993 + ", last=" + mLastBinder
4994 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4995 + "), finished=" + mFinished + ", paused="
4996 + token.paused);
4997 token.paused = false;
4998 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
4999 doFinishedKeyLocked(true);
5000 } else {
5001 notifyAll();
5002 }
5003 }
5004 }
5005 }
5006
5007 void setEventDispatchingLocked(boolean enabled) {
5008 synchronized (this) {
5009 mEventDispatching = enabled;
5010 notifyAll();
5011 }
5012 }
Romain Guy06882f82009-06-10 13:36:04 -07005013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 void appSwitchComing() {
5015 synchronized (this) {
5016 // Don't wait for more than .5 seconds for app to finish
5017 // processing the pending events.
5018 long now = SystemClock.uptimeMillis() + 500;
5019 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5020 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5021 mTimeToSwitch = now;
5022 }
5023 notifyAll();
5024 }
5025 }
Romain Guy06882f82009-06-10 13:36:04 -07005026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 private final void doFinishedKeyLocked(boolean doRecycle) {
5028 if (mLastWin != null) {
5029 releasePendingPointerLocked(mLastWin.mSession);
5030 releasePendingTrackballLocked(mLastWin.mSession);
5031 }
Romain Guy06882f82009-06-10 13:36:04 -07005032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 if (mLastWin == null || !mLastWin.mToken.paused
5034 || !mLastWin.isVisibleLw()) {
5035 // If the current window has been paused, we aren't -really-
5036 // finished... so let the waiters still wait.
5037 mLastWin = null;
5038 mLastBinder = null;
5039 }
5040 mFinished = true;
5041 notifyAll();
5042 }
5043 }
5044
5045 private class KeyQ extends KeyInputQueue
5046 implements KeyInputQueue.FilterCallback {
5047 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005049 KeyQ() {
5050 super(mContext);
5051 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5052 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5053 "KEEP_SCREEN_ON_FLAG");
5054 mHoldingScreen.setReferenceCounted(false);
5055 }
5056
5057 @Override
5058 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5059 if (mPolicy.preprocessInputEventTq(event)) {
5060 return true;
5061 }
Romain Guy06882f82009-06-10 13:36:04 -07005062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 switch (event.type) {
5064 case RawInputEvent.EV_KEY: {
5065 // XXX begin hack
5066 if (DEBUG) {
5067 if (event.keycode == KeyEvent.KEYCODE_G) {
5068 if (event.value != 0) {
5069 // G down
5070 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5071 }
5072 return false;
5073 }
5074 if (event.keycode == KeyEvent.KEYCODE_D) {
5075 if (event.value != 0) {
5076 //dump();
5077 }
5078 return false;
5079 }
5080 }
5081 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 boolean screenIsOff = !mPowerManager.screenIsOn();
5084 boolean screenIsDim = !mPowerManager.screenIsBright();
5085 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5088 mPowerManager.goToSleep(event.when);
5089 }
5090
5091 if (screenIsOff) {
5092 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5093 }
5094 if (screenIsDim) {
5095 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5096 }
5097 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5098 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005099 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 }
Romain Guy06882f82009-06-10 13:36:04 -07005101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005102 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5103 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5104 filterQueue(this);
5105 mKeyWaiter.appSwitchComing();
5106 }
5107 return true;
5108 } else {
5109 return false;
5110 }
5111 }
Romain Guy06882f82009-06-10 13:36:04 -07005112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 case RawInputEvent.EV_REL: {
5114 boolean screenIsOff = !mPowerManager.screenIsOn();
5115 boolean screenIsDim = !mPowerManager.screenIsBright();
5116 if (screenIsOff) {
5117 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5118 device.classes, event)) {
5119 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5120 return false;
5121 }
5122 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5123 }
5124 if (screenIsDim) {
5125 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5126 }
5127 return true;
5128 }
Romain Guy06882f82009-06-10 13:36:04 -07005129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 case RawInputEvent.EV_ABS: {
5131 boolean screenIsOff = !mPowerManager.screenIsOn();
5132 boolean screenIsDim = !mPowerManager.screenIsBright();
5133 if (screenIsOff) {
5134 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5135 device.classes, event)) {
5136 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5137 return false;
5138 }
5139 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5140 }
5141 if (screenIsDim) {
5142 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5143 }
5144 return true;
5145 }
Romain Guy06882f82009-06-10 13:36:04 -07005146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 default:
5148 return true;
5149 }
5150 }
5151
5152 public int filterEvent(QueuedEvent ev) {
5153 switch (ev.classType) {
5154 case RawInputEvent.CLASS_KEYBOARD:
5155 KeyEvent ke = (KeyEvent)ev.event;
5156 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5157 Log.w(TAG, "Dropping movement key during app switch: "
5158 + ke.getKeyCode() + ", action=" + ke.getAction());
5159 return FILTER_REMOVE;
5160 }
5161 return FILTER_ABORT;
5162 default:
5163 return FILTER_KEEP;
5164 }
5165 }
Romain Guy06882f82009-06-10 13:36:04 -07005166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 /**
5168 * Must be called with the main window manager lock held.
5169 */
5170 void setHoldScreenLocked(boolean holding) {
5171 boolean state = mHoldingScreen.isHeld();
5172 if (holding != state) {
5173 if (holding) {
5174 mHoldingScreen.acquire();
5175 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005176 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 mHoldingScreen.release();
5178 }
5179 }
5180 }
5181 };
5182
5183 public boolean detectSafeMode() {
5184 mSafeMode = mPolicy.detectSafeMode();
5185 return mSafeMode;
5186 }
Romain Guy06882f82009-06-10 13:36:04 -07005187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 public void systemReady() {
5189 mPolicy.systemReady();
5190 }
Romain Guy06882f82009-06-10 13:36:04 -07005191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 private final class InputDispatcherThread extends Thread {
5193 // Time to wait when there is nothing to do: 9999 seconds.
5194 static final int LONG_WAIT=9999*1000;
5195
5196 public InputDispatcherThread() {
5197 super("InputDispatcher");
5198 }
Romain Guy06882f82009-06-10 13:36:04 -07005199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 @Override
5201 public void run() {
5202 while (true) {
5203 try {
5204 process();
5205 } catch (Exception e) {
5206 Log.e(TAG, "Exception in input dispatcher", e);
5207 }
5208 }
5209 }
Romain Guy06882f82009-06-10 13:36:04 -07005210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 private void process() {
5212 android.os.Process.setThreadPriority(
5213 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 // The last key event we saw
5216 KeyEvent lastKey = null;
5217
5218 // Last keydown time for auto-repeating keys
5219 long lastKeyTime = SystemClock.uptimeMillis();
5220 long nextKeyTime = lastKeyTime+LONG_WAIT;
5221
Romain Guy06882f82009-06-10 13:36:04 -07005222 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 int keyRepeatCount = 0;
5224
5225 // Need to report that configuration has changed?
5226 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 while (true) {
5229 long curTime = SystemClock.uptimeMillis();
5230
5231 if (DEBUG_INPUT) Log.v(
5232 TAG, "Waiting for next key: now=" + curTime
5233 + ", repeat @ " + nextKeyTime);
5234
5235 // Retrieve next event, waiting only as long as the next
5236 // repeat timeout. If the configuration has changed, then
5237 // don't wait at all -- we'll report the change as soon as
5238 // we have processed all events.
5239 QueuedEvent ev = mQueue.getEvent(
5240 (int)((!configChanged && curTime < nextKeyTime)
5241 ? (nextKeyTime-curTime) : 0));
5242
5243 if (DEBUG_INPUT && ev != null) Log.v(
5244 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5245
5246 try {
5247 if (ev != null) {
5248 curTime = ev.when;
5249 int eventType;
5250 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5251 eventType = eventType((MotionEvent)ev.event);
5252 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5253 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5254 eventType = LocalPowerManager.BUTTON_EVENT;
5255 } else {
5256 eventType = LocalPowerManager.OTHER_EVENT;
5257 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005258 try {
Michael Chane96440f2009-05-06 10:27:36 -07005259 long now = SystemClock.uptimeMillis();
5260
5261 if ((now - mLastBatteryStatsCallTime)
5262 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5263 mLastBatteryStatsCallTime = now;
5264 mBatteryStats.noteInputEvent();
5265 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005266 } catch (RemoteException e) {
5267 // Ignore
5268 }
Michael Chane96440f2009-05-06 10:27:36 -07005269 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 switch (ev.classType) {
5271 case RawInputEvent.CLASS_KEYBOARD:
5272 KeyEvent ke = (KeyEvent)ev.event;
5273 if (ke.isDown()) {
5274 lastKey = ke;
5275 keyRepeatCount = 0;
5276 lastKeyTime = curTime;
5277 nextKeyTime = lastKeyTime
5278 + KEY_REPEAT_FIRST_DELAY;
5279 if (DEBUG_INPUT) Log.v(
5280 TAG, "Received key down: first repeat @ "
5281 + nextKeyTime);
5282 } else {
5283 lastKey = null;
5284 // Arbitrary long timeout.
5285 lastKeyTime = curTime;
5286 nextKeyTime = curTime + LONG_WAIT;
5287 if (DEBUG_INPUT) Log.v(
5288 TAG, "Received key up: ignore repeat @ "
5289 + nextKeyTime);
5290 }
5291 dispatchKey((KeyEvent)ev.event, 0, 0);
5292 mQueue.recycleEvent(ev);
5293 break;
5294 case RawInputEvent.CLASS_TOUCHSCREEN:
5295 //Log.i(TAG, "Read next event " + ev);
5296 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5297 break;
5298 case RawInputEvent.CLASS_TRACKBALL:
5299 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5300 break;
5301 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5302 configChanged = true;
5303 break;
5304 default:
5305 mQueue.recycleEvent(ev);
5306 break;
5307 }
Romain Guy06882f82009-06-10 13:36:04 -07005308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 } else if (configChanged) {
5310 configChanged = false;
5311 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 } else if (lastKey != null) {
5314 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 // Timeout occurred while key was down. If it is at or
5317 // past the key repeat time, dispatch the repeat.
5318 if (DEBUG_INPUT) Log.v(
5319 TAG, "Key timeout: repeat=" + nextKeyTime
5320 + ", now=" + curTime);
5321 if (curTime < nextKeyTime) {
5322 continue;
5323 }
Romain Guy06882f82009-06-10 13:36:04 -07005324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 lastKeyTime = nextKeyTime;
5326 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5327 keyRepeatCount++;
5328 if (DEBUG_INPUT) Log.v(
5329 TAG, "Key repeat: count=" + keyRepeatCount
5330 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005331 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 } else {
5334 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 lastKeyTime = curTime;
5337 nextKeyTime = curTime + LONG_WAIT;
5338 }
Romain Guy06882f82009-06-10 13:36:04 -07005339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 } catch (Exception e) {
5341 Log.e(TAG,
5342 "Input thread received uncaught exception: " + e, e);
5343 }
5344 }
5345 }
5346 }
5347
5348 // -------------------------------------------------------------
5349 // Client Session State
5350 // -------------------------------------------------------------
5351
5352 private final class Session extends IWindowSession.Stub
5353 implements IBinder.DeathRecipient {
5354 final IInputMethodClient mClient;
5355 final IInputContext mInputContext;
5356 final int mUid;
5357 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005358 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 SurfaceSession mSurfaceSession;
5360 int mNumWindow = 0;
5361 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 /**
5364 * Current pointer move event being dispatched to client window... must
5365 * hold key lock to access.
5366 */
5367 QueuedEvent mPendingPointerMove;
5368 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 /**
5371 * Current trackball move event being dispatched to client window... must
5372 * hold key lock to access.
5373 */
5374 QueuedEvent mPendingTrackballMove;
5375 WindowState mPendingTrackballWindow;
5376
5377 public Session(IInputMethodClient client, IInputContext inputContext) {
5378 mClient = client;
5379 mInputContext = inputContext;
5380 mUid = Binder.getCallingUid();
5381 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005382 StringBuilder sb = new StringBuilder();
5383 sb.append("Session{");
5384 sb.append(Integer.toHexString(System.identityHashCode(this)));
5385 sb.append(" uid ");
5386 sb.append(mUid);
5387 sb.append("}");
5388 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 synchronized (mWindowMap) {
5391 if (mInputMethodManager == null && mHaveInputMethods) {
5392 IBinder b = ServiceManager.getService(
5393 Context.INPUT_METHOD_SERVICE);
5394 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5395 }
5396 }
5397 long ident = Binder.clearCallingIdentity();
5398 try {
5399 // Note: it is safe to call in to the input method manager
5400 // here because we are not holding our lock.
5401 if (mInputMethodManager != null) {
5402 mInputMethodManager.addClient(client, inputContext,
5403 mUid, mPid);
5404 } else {
5405 client.setUsingInputMethod(false);
5406 }
5407 client.asBinder().linkToDeath(this, 0);
5408 } catch (RemoteException e) {
5409 // The caller has died, so we can just forget about this.
5410 try {
5411 if (mInputMethodManager != null) {
5412 mInputMethodManager.removeClient(client);
5413 }
5414 } catch (RemoteException ee) {
5415 }
5416 } finally {
5417 Binder.restoreCallingIdentity(ident);
5418 }
5419 }
Romain Guy06882f82009-06-10 13:36:04 -07005420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 @Override
5422 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5423 throws RemoteException {
5424 try {
5425 return super.onTransact(code, data, reply, flags);
5426 } catch (RuntimeException e) {
5427 // Log all 'real' exceptions thrown to the caller
5428 if (!(e instanceof SecurityException)) {
5429 Log.e(TAG, "Window Session Crash", e);
5430 }
5431 throw e;
5432 }
5433 }
5434
5435 public void binderDied() {
5436 // Note: it is safe to call in to the input method manager
5437 // here because we are not holding our lock.
5438 try {
5439 if (mInputMethodManager != null) {
5440 mInputMethodManager.removeClient(mClient);
5441 }
5442 } catch (RemoteException e) {
5443 }
5444 synchronized(mWindowMap) {
5445 mClientDead = true;
5446 killSessionLocked();
5447 }
5448 }
5449
5450 public int add(IWindow window, WindowManager.LayoutParams attrs,
5451 int viewVisibility, Rect outContentInsets) {
5452 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5453 }
Romain Guy06882f82009-06-10 13:36:04 -07005454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 public void remove(IWindow window) {
5456 removeWindow(this, window);
5457 }
Romain Guy06882f82009-06-10 13:36:04 -07005458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5460 int requestedWidth, int requestedHeight, int viewFlags,
5461 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5462 Rect outVisibleInsets, Surface outSurface) {
5463 return relayoutWindow(this, window, attrs,
5464 requestedWidth, requestedHeight, viewFlags, insetsPending,
5465 outFrame, outContentInsets, outVisibleInsets, outSurface);
5466 }
Romain Guy06882f82009-06-10 13:36:04 -07005467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 public void setTransparentRegion(IWindow window, Region region) {
5469 setTransparentRegionWindow(this, window, region);
5470 }
Romain Guy06882f82009-06-10 13:36:04 -07005471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 public void setInsets(IWindow window, int touchableInsets,
5473 Rect contentInsets, Rect visibleInsets) {
5474 setInsetsWindow(this, window, touchableInsets, contentInsets,
5475 visibleInsets);
5476 }
Romain Guy06882f82009-06-10 13:36:04 -07005477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5479 getWindowDisplayFrame(this, window, outDisplayFrame);
5480 }
Romain Guy06882f82009-06-10 13:36:04 -07005481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 public void finishDrawing(IWindow window) {
5483 if (localLOGV) Log.v(
5484 TAG, "IWindow finishDrawing called for " + window);
5485 finishDrawingWindow(this, window);
5486 }
5487
5488 public void finishKey(IWindow window) {
5489 if (localLOGV) Log.v(
5490 TAG, "IWindow finishKey called for " + window);
5491 mKeyWaiter.finishedKey(this, window, false,
5492 KeyWaiter.RETURN_NOTHING);
5493 }
5494
5495 public MotionEvent getPendingPointerMove(IWindow window) {
5496 if (localLOGV) Log.v(
5497 TAG, "IWindow getPendingMotionEvent called for " + window);
5498 return mKeyWaiter.finishedKey(this, window, false,
5499 KeyWaiter.RETURN_PENDING_POINTER);
5500 }
Romain Guy06882f82009-06-10 13:36:04 -07005501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 public MotionEvent getPendingTrackballMove(IWindow window) {
5503 if (localLOGV) Log.v(
5504 TAG, "IWindow getPendingMotionEvent called for " + window);
5505 return mKeyWaiter.finishedKey(this, window, false,
5506 KeyWaiter.RETURN_PENDING_TRACKBALL);
5507 }
5508
5509 public void setInTouchMode(boolean mode) {
5510 synchronized(mWindowMap) {
5511 mInTouchMode = mode;
5512 }
5513 }
5514
5515 public boolean getInTouchMode() {
5516 synchronized(mWindowMap) {
5517 return mInTouchMode;
5518 }
5519 }
5520
5521 public boolean performHapticFeedback(IWindow window, int effectId,
5522 boolean always) {
5523 synchronized(mWindowMap) {
5524 long ident = Binder.clearCallingIdentity();
5525 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005526 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527 windowForClientLocked(this, window), effectId, always);
5528 } finally {
5529 Binder.restoreCallingIdentity(ident);
5530 }
5531 }
5532 }
Romain Guy06882f82009-06-10 13:36:04 -07005533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 void windowAddedLocked() {
5535 if (mSurfaceSession == null) {
5536 if (localLOGV) Log.v(
5537 TAG, "First window added to " + this + ", creating SurfaceSession");
5538 mSurfaceSession = new SurfaceSession();
5539 mSessions.add(this);
5540 }
5541 mNumWindow++;
5542 }
5543
5544 void windowRemovedLocked() {
5545 mNumWindow--;
5546 killSessionLocked();
5547 }
Romain Guy06882f82009-06-10 13:36:04 -07005548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 void killSessionLocked() {
5550 if (mNumWindow <= 0 && mClientDead) {
5551 mSessions.remove(this);
5552 if (mSurfaceSession != null) {
5553 if (localLOGV) Log.v(
5554 TAG, "Last window removed from " + this
5555 + ", destroying " + mSurfaceSession);
5556 try {
5557 mSurfaceSession.kill();
5558 } catch (Exception e) {
5559 Log.w(TAG, "Exception thrown when killing surface session "
5560 + mSurfaceSession + " in session " + this
5561 + ": " + e.toString());
5562 }
5563 mSurfaceSession = null;
5564 }
5565 }
5566 }
Romain Guy06882f82009-06-10 13:36:04 -07005567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005569 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5570 pw.print(" mClientDead="); pw.print(mClientDead);
5571 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5572 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5573 pw.print(prefix);
5574 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5575 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5576 }
5577 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5578 pw.print(prefix);
5579 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5580 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 }
5583
5584 @Override
5585 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005586 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005587 }
5588 }
5589
5590 // -------------------------------------------------------------
5591 // Client Window State
5592 // -------------------------------------------------------------
5593
5594 private final class WindowState implements WindowManagerPolicy.WindowState {
5595 final Session mSession;
5596 final IWindow mClient;
5597 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005598 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 AppWindowToken mAppToken;
5600 AppWindowToken mTargetAppToken;
5601 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5602 final DeathRecipient mDeathRecipient;
5603 final WindowState mAttachedWindow;
5604 final ArrayList mChildWindows = new ArrayList();
5605 final int mBaseLayer;
5606 final int mSubLayer;
5607 final boolean mLayoutAttached;
5608 final boolean mIsImWindow;
5609 int mViewVisibility;
5610 boolean mPolicyVisibility = true;
5611 boolean mPolicyVisibilityAfterAnim = true;
5612 boolean mAppFreezing;
5613 Surface mSurface;
5614 boolean mAttachedHidden; // is our parent window hidden?
5615 boolean mLastHidden; // was this window last hidden?
5616 int mRequestedWidth;
5617 int mRequestedHeight;
5618 int mLastRequestedWidth;
5619 int mLastRequestedHeight;
5620 int mReqXPos;
5621 int mReqYPos;
5622 int mLayer;
5623 int mAnimLayer;
5624 int mLastLayer;
5625 boolean mHaveFrame;
5626
5627 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 // Actual frame shown on-screen (may be modified by animation)
5630 final Rect mShownFrame = new Rect();
5631 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 /**
5634 * Insets that determine the actually visible area
5635 */
5636 final Rect mVisibleInsets = new Rect();
5637 final Rect mLastVisibleInsets = new Rect();
5638 boolean mVisibleInsetsChanged;
5639
5640 /**
5641 * Insets that are covered by system windows
5642 */
5643 final Rect mContentInsets = new Rect();
5644 final Rect mLastContentInsets = new Rect();
5645 boolean mContentInsetsChanged;
5646
5647 /**
5648 * Set to true if we are waiting for this window to receive its
5649 * given internal insets before laying out other windows based on it.
5650 */
5651 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653 /**
5654 * These are the content insets that were given during layout for
5655 * this window, to be applied to windows behind it.
5656 */
5657 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 /**
5660 * These are the visible insets that were given during layout for
5661 * this window, to be applied to windows behind it.
5662 */
5663 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 /**
5666 * Flag indicating whether the touchable region should be adjusted by
5667 * the visible insets; if false the area outside the visible insets is
5668 * NOT touchable, so we must use those to adjust the frame during hit
5669 * tests.
5670 */
5671 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005673 // Current transformation being applied.
5674 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5675 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5676 float mHScale=1, mVScale=1;
5677 float mLastHScale=1, mLastVScale=1;
5678 final Matrix mTmpMatrix = new Matrix();
5679
5680 // "Real" frame that the application sees.
5681 final Rect mFrame = new Rect();
5682 final Rect mLastFrame = new Rect();
5683
5684 final Rect mContainingFrame = new Rect();
5685 final Rect mDisplayFrame = new Rect();
5686 final Rect mContentFrame = new Rect();
5687 final Rect mVisibleFrame = new Rect();
5688
5689 float mShownAlpha = 1;
5690 float mAlpha = 1;
5691 float mLastAlpha = 1;
5692
5693 // Set to true if, when the window gets displayed, it should perform
5694 // an enter animation.
5695 boolean mEnterAnimationPending;
5696
5697 // Currently running animation.
5698 boolean mAnimating;
5699 boolean mLocalAnimating;
5700 Animation mAnimation;
5701 boolean mAnimationIsEntrance;
5702 boolean mHasTransformation;
5703 boolean mHasLocalTransformation;
5704 final Transformation mTransformation = new Transformation();
5705
5706 // This is set after IWindowSession.relayout() has been called at
5707 // least once for the window. It allows us to detect the situation
5708 // where we don't yet have a surface, but should have one soon, so
5709 // we can give the window focus before waiting for the relayout.
5710 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 // This is set after the Surface has been created but before the
5713 // window has been drawn. During this time the surface is hidden.
5714 boolean mDrawPending;
5715
5716 // This is set after the window has finished drawing for the first
5717 // time but before its surface is shown. The surface will be
5718 // displayed when the next layout is run.
5719 boolean mCommitDrawPending;
5720
5721 // This is set during the time after the window's drawing has been
5722 // committed, and before its surface is actually shown. It is used
5723 // to delay showing the surface until all windows in a token are ready
5724 // to be shown.
5725 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 // Set when the window has been shown in the screen the first time.
5728 boolean mHasDrawn;
5729
5730 // Currently running an exit animation?
5731 boolean mExiting;
5732
5733 // Currently on the mDestroySurface list?
5734 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 // Completely remove from window manager after exit animation?
5737 boolean mRemoveOnExit;
5738
5739 // Set when the orientation is changing and this window has not yet
5740 // been updated for the new orientation.
5741 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 // Is this window now (or just being) removed?
5744 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 WindowState(Session s, IWindow c, WindowToken token,
5747 WindowState attachedWindow, WindowManager.LayoutParams a,
5748 int viewVisibility) {
5749 mSession = s;
5750 mClient = c;
5751 mToken = token;
5752 mAttrs.copyFrom(a);
5753 mViewVisibility = viewVisibility;
5754 DeathRecipient deathRecipient = new DeathRecipient();
5755 mAlpha = a.alpha;
5756 if (localLOGV) Log.v(
5757 TAG, "Window " + this + " client=" + c.asBinder()
5758 + " token=" + token + " (" + mAttrs.token + ")");
5759 try {
5760 c.asBinder().linkToDeath(deathRecipient, 0);
5761 } catch (RemoteException e) {
5762 mDeathRecipient = null;
5763 mAttachedWindow = null;
5764 mLayoutAttached = false;
5765 mIsImWindow = false;
5766 mBaseLayer = 0;
5767 mSubLayer = 0;
5768 return;
5769 }
5770 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5773 mAttrs.type <= LAST_SUB_WINDOW)) {
5774 // The multiplier here is to reserve space for multiple
5775 // windows in the same type layer.
5776 mBaseLayer = mPolicy.windowTypeToLayerLw(
5777 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5778 + TYPE_LAYER_OFFSET;
5779 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5780 mAttachedWindow = attachedWindow;
5781 mAttachedWindow.mChildWindows.add(this);
5782 mLayoutAttached = mAttrs.type !=
5783 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5784 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5785 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5786 } else {
5787 // The multiplier here is to reserve space for multiple
5788 // windows in the same type layer.
5789 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5790 * TYPE_LAYER_MULTIPLIER
5791 + TYPE_LAYER_OFFSET;
5792 mSubLayer = 0;
5793 mAttachedWindow = null;
5794 mLayoutAttached = false;
5795 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5796 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5797 }
5798
5799 WindowState appWin = this;
5800 while (appWin.mAttachedWindow != null) {
5801 appWin = mAttachedWindow;
5802 }
5803 WindowToken appToken = appWin.mToken;
5804 while (appToken.appWindowToken == null) {
5805 WindowToken parent = mTokenMap.get(appToken.token);
5806 if (parent == null || appToken == parent) {
5807 break;
5808 }
5809 appToken = parent;
5810 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005811 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 mAppToken = appToken.appWindowToken;
5813
5814 mSurface = null;
5815 mRequestedWidth = 0;
5816 mRequestedHeight = 0;
5817 mLastRequestedWidth = 0;
5818 mLastRequestedHeight = 0;
5819 mReqXPos = 0;
5820 mReqYPos = 0;
5821 mLayer = 0;
5822 mAnimLayer = 0;
5823 mLastLayer = 0;
5824 }
5825
5826 void attach() {
5827 if (localLOGV) Log.v(
5828 TAG, "Attaching " + this + " token=" + mToken
5829 + ", list=" + mToken.windows);
5830 mSession.windowAddedLocked();
5831 }
5832
5833 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5834 mHaveFrame = true;
5835
5836 final int pw = pf.right-pf.left;
5837 final int ph = pf.bottom-pf.top;
5838
5839 int w,h;
5840 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5841 w = mAttrs.width < 0 ? pw : mAttrs.width;
5842 h = mAttrs.height< 0 ? ph : mAttrs.height;
5843 } else {
5844 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5845 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5846 }
Romain Guy06882f82009-06-10 13:36:04 -07005847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 final Rect container = mContainingFrame;
5849 container.set(pf);
5850
5851 final Rect display = mDisplayFrame;
5852 display.set(df);
5853
5854 final Rect content = mContentFrame;
5855 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 final Rect visible = mVisibleFrame;
5858 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5863 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5864
5865 Gravity.apply(mAttrs.gravity, w, h, container,
5866 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5867 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5868
5869 //System.out.println("Out: " + mFrame);
5870
5871 // Now make sure the window fits in the overall display.
5872 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Romain Guy06882f82009-06-10 13:36:04 -07005873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 // Make sure the content and visible frames are inside of the
5875 // final window frame.
5876 if (content.left < frame.left) content.left = frame.left;
5877 if (content.top < frame.top) content.top = frame.top;
5878 if (content.right > frame.right) content.right = frame.right;
5879 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5880 if (visible.left < frame.left) visible.left = frame.left;
5881 if (visible.top < frame.top) visible.top = frame.top;
5882 if (visible.right > frame.right) visible.right = frame.right;
5883 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 final Rect contentInsets = mContentInsets;
5886 contentInsets.left = content.left-frame.left;
5887 contentInsets.top = content.top-frame.top;
5888 contentInsets.right = frame.right-content.right;
5889 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 final Rect visibleInsets = mVisibleInsets;
5892 visibleInsets.left = visible.left-frame.left;
5893 visibleInsets.top = visible.top-frame.top;
5894 visibleInsets.right = frame.right-visible.right;
5895 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 if (localLOGV) {
5898 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5899 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5900 Log.v(TAG, "Resolving (mRequestedWidth="
5901 + mRequestedWidth + ", mRequestedheight="
5902 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5903 + "): frame=" + mFrame.toShortString()
5904 + " ci=" + contentInsets.toShortString()
5905 + " vi=" + visibleInsets.toShortString());
5906 //}
5907 }
5908 }
Romain Guy06882f82009-06-10 13:36:04 -07005909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 public Rect getFrameLw() {
5911 return mFrame;
5912 }
5913
5914 public Rect getShownFrameLw() {
5915 return mShownFrame;
5916 }
5917
5918 public Rect getDisplayFrameLw() {
5919 return mDisplayFrame;
5920 }
5921
5922 public Rect getContentFrameLw() {
5923 return mContentFrame;
5924 }
5925
5926 public Rect getVisibleFrameLw() {
5927 return mVisibleFrame;
5928 }
5929
5930 public boolean getGivenInsetsPendingLw() {
5931 return mGivenInsetsPending;
5932 }
5933
5934 public Rect getGivenContentInsetsLw() {
5935 return mGivenContentInsets;
5936 }
Romain Guy06882f82009-06-10 13:36:04 -07005937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 public Rect getGivenVisibleInsetsLw() {
5939 return mGivenVisibleInsets;
5940 }
Romain Guy06882f82009-06-10 13:36:04 -07005941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 public WindowManager.LayoutParams getAttrs() {
5943 return mAttrs;
5944 }
5945
5946 public int getSurfaceLayer() {
5947 return mLayer;
5948 }
Romain Guy06882f82009-06-10 13:36:04 -07005949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 public IApplicationToken getAppToken() {
5951 return mAppToken != null ? mAppToken.appToken : null;
5952 }
5953
5954 public boolean hasAppShownWindows() {
5955 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
5956 }
5957
5958 public boolean hasAppStartingIcon() {
5959 return mAppToken != null ? (mAppToken.startingData != null) : false;
5960 }
5961
5962 public WindowManagerPolicy.WindowState getAppStartingWindow() {
5963 return mAppToken != null ? mAppToken.startingWindow : null;
5964 }
5965
5966 public void setAnimation(Animation anim) {
5967 if (localLOGV) Log.v(
5968 TAG, "Setting animation in " + this + ": " + anim);
5969 mAnimating = false;
5970 mLocalAnimating = false;
5971 mAnimation = anim;
5972 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
5973 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
5974 }
5975
5976 public void clearAnimation() {
5977 if (mAnimation != null) {
5978 mAnimating = true;
5979 mLocalAnimating = false;
5980 mAnimation = null;
5981 }
5982 }
Romain Guy06882f82009-06-10 13:36:04 -07005983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005984 Surface createSurfaceLocked() {
5985 if (mSurface == null) {
5986 mDrawPending = true;
5987 mCommitDrawPending = false;
5988 mReadyToShow = false;
5989 if (mAppToken != null) {
5990 mAppToken.allDrawn = false;
5991 }
5992
5993 int flags = 0;
5994 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
5995 flags |= Surface.HARDWARE;
5996 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
5997 flags |= Surface.GPU;
5998 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
5999 flags |= Surface.PUSH_BUFFERS;
6000 }
6001
6002 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6003 flags |= Surface.SECURE;
6004 }
6005 if (DEBUG_VISIBILITY) Log.v(
6006 TAG, "Creating surface in session "
6007 + mSession.mSurfaceSession + " window " + this
6008 + " w=" + mFrame.width()
6009 + " h=" + mFrame.height() + " format="
6010 + mAttrs.format + " flags=" + flags);
6011
6012 int w = mFrame.width();
6013 int h = mFrame.height();
6014 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6015 // for a scaled surface, we always want the requested
6016 // size.
6017 w = mRequestedWidth;
6018 h = mRequestedHeight;
6019 }
6020
6021 try {
6022 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006023 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 0, w, h, mAttrs.format, flags);
6025 } catch (Surface.OutOfResourcesException e) {
6026 Log.w(TAG, "OutOfResourcesException creating surface");
6027 reclaimSomeSurfaceMemoryLocked(this, "create");
6028 return null;
6029 } catch (Exception e) {
6030 Log.e(TAG, "Exception creating surface", e);
6031 return null;
6032 }
Romain Guy06882f82009-06-10 13:36:04 -07006033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 if (localLOGV) Log.v(
6035 TAG, "Got surface: " + mSurface
6036 + ", set left=" + mFrame.left + " top=" + mFrame.top
6037 + ", animLayer=" + mAnimLayer);
6038 if (SHOW_TRANSACTIONS) {
6039 Log.i(TAG, ">>> OPEN TRANSACTION");
6040 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6041 + mAttrs.getTitle() + ") pos=(" +
6042 mFrame.left + "," + mFrame.top + ") (" +
6043 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6044 mAnimLayer + " HIDE");
6045 }
6046 Surface.openTransaction();
6047 try {
6048 try {
6049 mSurface.setPosition(mFrame.left, mFrame.top);
6050 mSurface.setLayer(mAnimLayer);
6051 mSurface.hide();
6052 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6053 mSurface.setFlags(Surface.SURFACE_DITHER,
6054 Surface.SURFACE_DITHER);
6055 }
6056 } catch (RuntimeException e) {
6057 Log.w(TAG, "Error creating surface in " + w, e);
6058 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6059 }
6060 mLastHidden = true;
6061 } finally {
6062 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6063 Surface.closeTransaction();
6064 }
6065 if (localLOGV) Log.v(
6066 TAG, "Created surface " + this);
6067 }
6068 return mSurface;
6069 }
Romain Guy06882f82009-06-10 13:36:04 -07006070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071 void destroySurfaceLocked() {
6072 // Window is no longer on-screen, so can no longer receive
6073 // key events... if we were waiting for it to finish
6074 // handling a key event, the wait is over!
6075 mKeyWaiter.finishedKey(mSession, mClient, true,
6076 KeyWaiter.RETURN_NOTHING);
6077 mKeyWaiter.releasePendingPointerLocked(mSession);
6078 mKeyWaiter.releasePendingTrackballLocked(mSession);
6079
6080 if (mAppToken != null && this == mAppToken.startingWindow) {
6081 mAppToken.startingDisplayed = false;
6082 }
Romain Guy06882f82009-06-10 13:36:04 -07006083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 if (localLOGV) Log.v(
6085 TAG, "Window " + this
6086 + " destroying surface " + mSurface + ", session " + mSession);
6087 if (mSurface != null) {
6088 try {
6089 if (SHOW_TRANSACTIONS) {
6090 RuntimeException ex = new RuntimeException();
6091 ex.fillInStackTrace();
6092 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6093 + mAttrs.getTitle() + ")", ex);
6094 }
6095 mSurface.clear();
6096 } catch (RuntimeException e) {
6097 Log.w(TAG, "Exception thrown when destroying Window " + this
6098 + " surface " + mSurface + " session " + mSession
6099 + ": " + e.toString());
6100 }
6101 mSurface = null;
6102 mDrawPending = false;
6103 mCommitDrawPending = false;
6104 mReadyToShow = false;
6105
6106 int i = mChildWindows.size();
6107 while (i > 0) {
6108 i--;
6109 WindowState c = (WindowState)mChildWindows.get(i);
6110 c.mAttachedHidden = true;
6111 }
6112 }
6113 }
6114
6115 boolean finishDrawingLocked() {
6116 if (mDrawPending) {
6117 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6118 TAG, "finishDrawingLocked: " + mSurface);
6119 mCommitDrawPending = true;
6120 mDrawPending = false;
6121 return true;
6122 }
6123 return false;
6124 }
6125
6126 // This must be called while inside a transaction.
6127 void commitFinishDrawingLocked(long currentTime) {
6128 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6129 if (!mCommitDrawPending) {
6130 return;
6131 }
6132 mCommitDrawPending = false;
6133 mReadyToShow = true;
6134 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6135 final AppWindowToken atoken = mAppToken;
6136 if (atoken == null || atoken.allDrawn || starting) {
6137 performShowLocked();
6138 }
6139 }
6140
6141 // This must be called while inside a transaction.
6142 boolean performShowLocked() {
6143 if (DEBUG_VISIBILITY) {
6144 RuntimeException e = new RuntimeException();
6145 e.fillInStackTrace();
6146 Log.v(TAG, "performShow on " + this
6147 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6148 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6149 }
6150 if (mReadyToShow && isReadyForDisplay()) {
6151 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6152 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6153 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6154 + " during animation: policyVis=" + mPolicyVisibility
6155 + " attHidden=" + mAttachedHidden
6156 + " tok.hiddenRequested="
6157 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6158 + " tok.idden="
6159 + (mAppToken != null ? mAppToken.hidden : false)
6160 + " animating=" + mAnimating
6161 + " tok animating="
6162 + (mAppToken != null ? mAppToken.animating : false));
6163 if (!showSurfaceRobustlyLocked(this)) {
6164 return false;
6165 }
6166 mLastAlpha = -1;
6167 mHasDrawn = true;
6168 mLastHidden = false;
6169 mReadyToShow = false;
6170 enableScreenIfNeededLocked();
6171
6172 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006174 int i = mChildWindows.size();
6175 while (i > 0) {
6176 i--;
6177 WindowState c = (WindowState)mChildWindows.get(i);
6178 if (c.mSurface != null && c.mAttachedHidden) {
6179 c.mAttachedHidden = false;
6180 c.performShowLocked();
6181 }
6182 }
Romain Guy06882f82009-06-10 13:36:04 -07006183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006184 if (mAttrs.type != TYPE_APPLICATION_STARTING
6185 && mAppToken != null) {
6186 mAppToken.firstWindowDrawn = true;
6187 if (mAnimation == null && mAppToken.startingData != null) {
6188 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6189 + mToken
6190 + ": first real window is shown, no animation");
6191 mFinishedStarting.add(mAppToken);
6192 mH.sendEmptyMessage(H.FINISHED_STARTING);
6193 }
6194 mAppToken.updateReportedVisibilityLocked();
6195 }
6196 }
6197 return true;
6198 }
Romain Guy06882f82009-06-10 13:36:04 -07006199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 // This must be called while inside a transaction. Returns true if
6201 // there is more animation to run.
6202 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6203 if (!mDisplayFrozen) {
6204 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6207 mHasTransformation = true;
6208 mHasLocalTransformation = true;
6209 if (!mLocalAnimating) {
6210 if (DEBUG_ANIM) Log.v(
6211 TAG, "Starting animation in " + this +
6212 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6213 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6214 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6215 mAnimation.setStartTime(currentTime);
6216 mLocalAnimating = true;
6217 mAnimating = true;
6218 }
6219 mTransformation.clear();
6220 final boolean more = mAnimation.getTransformation(
6221 currentTime, mTransformation);
6222 if (DEBUG_ANIM) Log.v(
6223 TAG, "Stepped animation in " + this +
6224 ": more=" + more + ", xform=" + mTransformation);
6225 if (more) {
6226 // we're not done!
6227 return true;
6228 }
6229 if (DEBUG_ANIM) Log.v(
6230 TAG, "Finished animation in " + this +
6231 " @ " + currentTime);
6232 mAnimation = null;
6233 //WindowManagerService.this.dump();
6234 }
6235 mHasLocalTransformation = false;
6236 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6237 && mAppToken.hasTransformation) {
6238 // When our app token is animating, we kind-of pretend like
6239 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6240 // part of this check means that we will only do this if
6241 // our window is not currently exiting, or it is not
6242 // locally animating itself. The idea being that one that
6243 // is exiting and doing a local animation should be removed
6244 // once that animation is done.
6245 mAnimating = true;
6246 mHasTransformation = true;
6247 mTransformation.clear();
6248 return false;
6249 } else if (mHasTransformation) {
6250 // Little trick to get through the path below to act like
6251 // we have finished an animation.
6252 mAnimating = true;
6253 } else if (isAnimating()) {
6254 mAnimating = true;
6255 }
6256 } else if (mAnimation != null) {
6257 // If the display is frozen, and there is a pending animation,
6258 // clear it and make sure we run the cleanup code.
6259 mAnimating = true;
6260 mLocalAnimating = true;
6261 mAnimation = null;
6262 }
Romain Guy06882f82009-06-10 13:36:04 -07006263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 if (!mAnimating && !mLocalAnimating) {
6265 return false;
6266 }
6267
6268 if (DEBUG_ANIM) Log.v(
6269 TAG, "Animation done in " + this + ": exiting=" + mExiting
6270 + ", reportedVisible="
6271 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 mAnimating = false;
6274 mLocalAnimating = false;
6275 mAnimation = null;
6276 mAnimLayer = mLayer;
6277 if (mIsImWindow) {
6278 mAnimLayer += mInputMethodAnimLayerAdjustment;
6279 }
6280 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6281 + " anim layer: " + mAnimLayer);
6282 mHasTransformation = false;
6283 mHasLocalTransformation = false;
6284 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6285 mTransformation.clear();
6286 if (mHasDrawn
6287 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6288 && mAppToken != null
6289 && mAppToken.firstWindowDrawn
6290 && mAppToken.startingData != null) {
6291 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6292 + mToken + ": first real window done animating");
6293 mFinishedStarting.add(mAppToken);
6294 mH.sendEmptyMessage(H.FINISHED_STARTING);
6295 }
Romain Guy06882f82009-06-10 13:36:04 -07006296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006297 finishExit();
6298
6299 if (mAppToken != null) {
6300 mAppToken.updateReportedVisibilityLocked();
6301 }
6302
6303 return false;
6304 }
6305
6306 void finishExit() {
6307 if (DEBUG_ANIM) Log.v(
6308 TAG, "finishExit in " + this
6309 + ": exiting=" + mExiting
6310 + " remove=" + mRemoveOnExit
6311 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 final int N = mChildWindows.size();
6314 for (int i=0; i<N; i++) {
6315 ((WindowState)mChildWindows.get(i)).finishExit();
6316 }
Romain Guy06882f82009-06-10 13:36:04 -07006317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 if (!mExiting) {
6319 return;
6320 }
Romain Guy06882f82009-06-10 13:36:04 -07006321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 if (isWindowAnimating()) {
6323 return;
6324 }
6325
6326 if (localLOGV) Log.v(
6327 TAG, "Exit animation finished in " + this
6328 + ": remove=" + mRemoveOnExit);
6329 if (mSurface != null) {
6330 mDestroySurface.add(this);
6331 mDestroying = true;
6332 if (SHOW_TRANSACTIONS) Log.i(
6333 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6334 try {
6335 mSurface.hide();
6336 } catch (RuntimeException e) {
6337 Log.w(TAG, "Error hiding surface in " + this, e);
6338 }
6339 mLastHidden = true;
6340 mKeyWaiter.releasePendingPointerLocked(mSession);
6341 }
6342 mExiting = false;
6343 if (mRemoveOnExit) {
6344 mPendingRemove.add(this);
6345 mRemoveOnExit = false;
6346 }
6347 }
Romain Guy06882f82009-06-10 13:36:04 -07006348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6350 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6351 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6352 if (dtdx < -.000001f || dtdx > .000001f) return false;
6353 if (dsdy < -.000001f || dsdy > .000001f) return false;
6354 return true;
6355 }
Romain Guy06882f82009-06-10 13:36:04 -07006356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 void computeShownFrameLocked() {
6358 final boolean selfTransformation = mHasLocalTransformation;
6359 Transformation attachedTransformation =
6360 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6361 ? mAttachedWindow.mTransformation : null;
6362 Transformation appTransformation =
6363 (mAppToken != null && mAppToken.hasTransformation)
6364 ? mAppToken.transformation : null;
6365 if (selfTransformation || attachedTransformation != null
6366 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006367 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 final Rect frame = mFrame;
6369 final float tmpFloats[] = mTmpFloats;
6370 final Matrix tmpMatrix = mTmpMatrix;
6371
6372 // Compute the desired transformation.
6373 tmpMatrix.setTranslate(frame.left, frame.top);
6374 if (selfTransformation) {
6375 tmpMatrix.preConcat(mTransformation.getMatrix());
6376 }
6377 if (attachedTransformation != null) {
6378 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6379 }
6380 if (appTransformation != null) {
6381 tmpMatrix.preConcat(appTransformation.getMatrix());
6382 }
6383
6384 // "convert" it into SurfaceFlinger's format
6385 // (a 2x2 matrix + an offset)
6386 // Here we must not transform the position of the surface
6387 // since it is already included in the transformation.
6388 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 tmpMatrix.getValues(tmpFloats);
6391 mDsDx = tmpFloats[Matrix.MSCALE_X];
6392 mDtDx = tmpFloats[Matrix.MSKEW_X];
6393 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6394 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6395 int x = (int)tmpFloats[Matrix.MTRANS_X];
6396 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6397 int w = frame.width();
6398 int h = frame.height();
6399 mShownFrame.set(x, y, x+w, y+h);
6400
6401 // Now set the alpha... but because our current hardware
6402 // can't do alpha transformation on a non-opaque surface,
6403 // turn it off if we are running an animation that is also
6404 // transforming since it is more important to have that
6405 // animation be smooth.
6406 mShownAlpha = mAlpha;
6407 if (!mLimitedAlphaCompositing
6408 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6409 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6410 && x == frame.left && y == frame.top))) {
6411 //Log.i(TAG, "Applying alpha transform");
6412 if (selfTransformation) {
6413 mShownAlpha *= mTransformation.getAlpha();
6414 }
6415 if (attachedTransformation != null) {
6416 mShownAlpha *= attachedTransformation.getAlpha();
6417 }
6418 if (appTransformation != null) {
6419 mShownAlpha *= appTransformation.getAlpha();
6420 }
6421 } else {
6422 //Log.i(TAG, "Not applying alpha transform");
6423 }
Romain Guy06882f82009-06-10 13:36:04 -07006424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 if (localLOGV) Log.v(
6426 TAG, "Continuing animation in " + this +
6427 ": " + mShownFrame +
6428 ", alpha=" + mTransformation.getAlpha());
6429 return;
6430 }
Romain Guy06882f82009-06-10 13:36:04 -07006431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 mShownFrame.set(mFrame);
6433 mShownAlpha = mAlpha;
6434 mDsDx = 1;
6435 mDtDx = 0;
6436 mDsDy = 0;
6437 mDtDy = 1;
6438 }
Romain Guy06882f82009-06-10 13:36:04 -07006439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 /**
6441 * Is this window visible? It is not visible if there is no
6442 * surface, or we are in the process of running an exit animation
6443 * that will remove the surface, or its app token has been hidden.
6444 */
6445 public boolean isVisibleLw() {
6446 final AppWindowToken atoken = mAppToken;
6447 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6448 && (atoken == null || !atoken.hiddenRequested)
6449 && !mExiting && !mDestroying;
6450 }
6451
6452 /**
6453 * Is this window visible, ignoring its app token? It is not visible
6454 * if there is no surface, or we are in the process of running an exit animation
6455 * that will remove the surface.
6456 */
6457 public boolean isWinVisibleLw() {
6458 final AppWindowToken atoken = mAppToken;
6459 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6460 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6461 && !mExiting && !mDestroying;
6462 }
6463
6464 /**
6465 * The same as isVisible(), but follows the current hidden state of
6466 * the associated app token, not the pending requested hidden state.
6467 */
6468 boolean isVisibleNow() {
6469 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006470 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 }
6472
6473 /**
6474 * Same as isVisible(), but we also count it as visible between the
6475 * call to IWindowSession.add() and the first relayout().
6476 */
6477 boolean isVisibleOrAdding() {
6478 final AppWindowToken atoken = mAppToken;
6479 return (mSurface != null
6480 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6481 && mPolicyVisibility && !mAttachedHidden
6482 && (atoken == null || !atoken.hiddenRequested)
6483 && !mExiting && !mDestroying;
6484 }
6485
6486 /**
6487 * Is this window currently on-screen? It is on-screen either if it
6488 * is visible or it is currently running an animation before no longer
6489 * being visible.
6490 */
6491 boolean isOnScreen() {
6492 final AppWindowToken atoken = mAppToken;
6493 if (atoken != null) {
6494 return mSurface != null && mPolicyVisibility && !mDestroying
6495 && ((!mAttachedHidden && !atoken.hiddenRequested)
6496 || mAnimating || atoken.animating);
6497 } else {
6498 return mSurface != null && mPolicyVisibility && !mDestroying
6499 && (!mAttachedHidden || mAnimating);
6500 }
6501 }
Romain Guy06882f82009-06-10 13:36:04 -07006502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503 /**
6504 * Like isOnScreen(), but we don't return true if the window is part
6505 * of a transition that has not yet been started.
6506 */
6507 boolean isReadyForDisplay() {
6508 final AppWindowToken atoken = mAppToken;
6509 final boolean animating = atoken != null ? atoken.animating : false;
6510 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006511 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 || mAnimating || animating);
6513 }
6514
6515 /** Is the window or its container currently animating? */
6516 boolean isAnimating() {
6517 final WindowState attached = mAttachedWindow;
6518 final AppWindowToken atoken = mAppToken;
6519 return mAnimation != null
6520 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006521 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006522 (atoken.animation != null
6523 || atoken.inPendingTransaction));
6524 }
6525
6526 /** Is this window currently animating? */
6527 boolean isWindowAnimating() {
6528 return mAnimation != null;
6529 }
6530
6531 /**
6532 * Like isOnScreen, but returns false if the surface hasn't yet
6533 * been drawn.
6534 */
6535 public boolean isDisplayedLw() {
6536 final AppWindowToken atoken = mAppToken;
6537 return mSurface != null && mPolicyVisibility && !mDestroying
6538 && !mDrawPending && !mCommitDrawPending
6539 && ((!mAttachedHidden &&
6540 (atoken == null || !atoken.hiddenRequested))
6541 || mAnimating);
6542 }
6543
6544 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6545 boolean shownFrame, boolean onlyOpaque) {
6546 if (mSurface == null) {
6547 return false;
6548 }
6549 if (mAppToken != null && !mAppToken.appFullscreen) {
6550 return false;
6551 }
6552 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6553 return false;
6554 }
6555 final Rect frame = shownFrame ? mShownFrame : mFrame;
6556 if (frame.left <= 0 && frame.top <= 0
6557 && frame.right >= screenWidth
6558 && frame.bottom >= screenHeight) {
6559 return true;
6560 }
6561 return false;
6562 }
Romain Guy06882f82009-06-10 13:36:04 -07006563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006564 boolean isFullscreenOpaque(int screenWidth, int screenHeight) {
6565 if (mAttrs.format != PixelFormat.OPAQUE || mSurface == null
6566 || mAnimation != null || mDrawPending || mCommitDrawPending) {
6567 return false;
6568 }
6569 if (mFrame.left <= 0 && mFrame.top <= 0 &&
6570 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight) {
6571 return true;
6572 }
6573 return false;
6574 }
6575
6576 void removeLocked() {
6577 if (mAttachedWindow != null) {
6578 mAttachedWindow.mChildWindows.remove(this);
6579 }
6580 destroySurfaceLocked();
6581 mSession.windowRemovedLocked();
6582 try {
6583 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6584 } catch (RuntimeException e) {
6585 // Ignore if it has already been removed (usually because
6586 // we are doing this as part of processing a death note.)
6587 }
6588 }
6589
6590 private class DeathRecipient implements IBinder.DeathRecipient {
6591 public void binderDied() {
6592 try {
6593 synchronized(mWindowMap) {
6594 WindowState win = windowForClientLocked(mSession, mClient);
6595 Log.i(TAG, "WIN DEATH: " + win);
6596 if (win != null) {
6597 removeWindowLocked(mSession, win);
6598 }
6599 }
6600 } catch (IllegalArgumentException ex) {
6601 // This will happen if the window has already been
6602 // removed.
6603 }
6604 }
6605 }
6606
6607 /** Returns true if this window desires key events. */
6608 public final boolean canReceiveKeys() {
6609 return isVisibleOrAdding()
6610 && (mViewVisibility == View.VISIBLE)
6611 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6612 }
6613
6614 public boolean hasDrawnLw() {
6615 return mHasDrawn;
6616 }
6617
6618 public boolean showLw(boolean doAnimation) {
6619 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6620 mPolicyVisibility = true;
6621 mPolicyVisibilityAfterAnim = true;
6622 if (doAnimation) {
6623 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6624 }
6625 requestAnimationLocked(0);
6626 return true;
6627 }
6628 return false;
6629 }
6630
6631 public boolean hideLw(boolean doAnimation) {
6632 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6633 : mPolicyVisibility;
6634 if (current) {
6635 if (doAnimation) {
6636 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6637 if (mAnimation == null) {
6638 doAnimation = false;
6639 }
6640 }
6641 if (doAnimation) {
6642 mPolicyVisibilityAfterAnim = false;
6643 } else {
6644 mPolicyVisibilityAfterAnim = false;
6645 mPolicyVisibility = false;
6646 }
6647 requestAnimationLocked(0);
6648 return true;
6649 }
6650 return false;
6651 }
6652
6653 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006654 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006655
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006656 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6657 pw.print(" mClient="); pw.println(mClient.asBinder());
6658 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6659 if (mAttachedWindow != null || mLayoutAttached) {
6660 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6661 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6662 }
6663 if (mIsImWindow) {
6664 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6665 }
6666 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6667 pw.print(" mSubLayer="); pw.print(mSubLayer);
6668 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6669 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6670 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6671 pw.print("="); pw.print(mAnimLayer);
6672 pw.print(" mLastLayer="); pw.println(mLastLayer);
6673 if (mSurface != null) {
6674 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6675 }
6676 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6677 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6678 if (mAppToken != null) {
6679 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6680 }
6681 if (mTargetAppToken != null) {
6682 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6683 }
6684 pw.print(prefix); pw.print("mViewVisibility=0x");
6685 pw.print(Integer.toHexString(mViewVisibility));
6686 pw.print(" mLastHidden="); pw.print(mLastHidden);
6687 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6688 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6689 pw.print(prefix); pw.print("mPolicyVisibility=");
6690 pw.print(mPolicyVisibility);
6691 pw.print(" mPolicyVisibilityAfterAnim=");
6692 pw.print(mPolicyVisibilityAfterAnim);
6693 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6694 }
6695 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6696 pw.print(" h="); pw.print(mRequestedHeight);
6697 pw.print(" x="); pw.print(mReqXPos);
6698 pw.print(" y="); pw.println(mReqYPos);
6699 pw.print(prefix); pw.print("mGivenContentInsets=");
6700 mGivenContentInsets.printShortString(pw);
6701 pw.print(" mGivenVisibleInsets=");
6702 mGivenVisibleInsets.printShortString(pw);
6703 pw.println();
6704 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6705 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6706 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6707 }
6708 pw.print(prefix); pw.print("mShownFrame=");
6709 mShownFrame.printShortString(pw);
6710 pw.print(" last="); mLastShownFrame.printShortString(pw);
6711 pw.println();
6712 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6713 pw.print(" last="); mLastFrame.printShortString(pw);
6714 pw.println();
6715 pw.print(prefix); pw.print("mContainingFrame=");
6716 mContainingFrame.printShortString(pw);
6717 pw.print(" mDisplayFrame=");
6718 mDisplayFrame.printShortString(pw);
6719 pw.println();
6720 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6721 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6722 pw.println();
6723 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6724 pw.print(" last="); mLastContentInsets.printShortString(pw);
6725 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6726 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6727 pw.println();
6728 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6729 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6730 pw.print(" mAlpha="); pw.print(mAlpha);
6731 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6732 }
6733 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6734 || mAnimation != null) {
6735 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6736 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6737 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6738 pw.print(" mAnimation="); pw.println(mAnimation);
6739 }
6740 if (mHasTransformation || mHasLocalTransformation) {
6741 pw.print(prefix); pw.print("XForm: has=");
6742 pw.print(mHasTransformation);
6743 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6744 pw.print(" "); mTransformation.printShortString(pw);
6745 pw.println();
6746 }
6747 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6748 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6749 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6750 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6751 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6752 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6753 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6754 pw.print(" mDestroying="); pw.print(mDestroying);
6755 pw.print(" mRemoved="); pw.println(mRemoved);
6756 }
6757 if (mOrientationChanging || mAppFreezing) {
6758 pw.print(prefix); pw.print("mOrientationChanging=");
6759 pw.print(mOrientationChanging);
6760 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 }
6763
6764 @Override
6765 public String toString() {
6766 return "Window{"
6767 + Integer.toHexString(System.identityHashCode(this))
6768 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6769 }
6770 }
Romain Guy06882f82009-06-10 13:36:04 -07006771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 // -------------------------------------------------------------
6773 // Window Token State
6774 // -------------------------------------------------------------
6775
6776 class WindowToken {
6777 // The actual token.
6778 final IBinder token;
6779
6780 // The type of window this token is for, as per WindowManager.LayoutParams.
6781 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 // Set if this token was explicitly added by a client, so should
6784 // not be removed when all windows are removed.
6785 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006786
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006787 // For printing.
6788 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006790 // If this is an AppWindowToken, this is non-null.
6791 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 // All of the windows associated with this token.
6794 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6795
6796 // Is key dispatching paused for this token?
6797 boolean paused = false;
6798
6799 // Should this token's windows be hidden?
6800 boolean hidden;
6801
6802 // Temporary for finding which tokens no longer have visible windows.
6803 boolean hasVisible;
6804
6805 WindowToken(IBinder _token, int type, boolean _explicit) {
6806 token = _token;
6807 windowType = type;
6808 explicit = _explicit;
6809 }
6810
6811 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006812 pw.print(prefix); pw.print("token="); pw.println(token);
6813 pw.print(prefix); pw.print("windows="); pw.println(windows);
6814 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6815 pw.print(" hidden="); pw.print(hidden);
6816 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 }
6818
6819 @Override
6820 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006821 if (stringName == null) {
6822 StringBuilder sb = new StringBuilder();
6823 sb.append("WindowToken{");
6824 sb.append(Integer.toHexString(System.identityHashCode(this)));
6825 sb.append(" token="); sb.append(token); sb.append('}');
6826 stringName = sb.toString();
6827 }
6828 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 }
6830 };
6831
6832 class AppWindowToken extends WindowToken {
6833 // Non-null only for application tokens.
6834 final IApplicationToken appToken;
6835
6836 // All of the windows and child windows that are included in this
6837 // application token. Note this list is NOT sorted!
6838 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6839
6840 int groupId = -1;
6841 boolean appFullscreen;
6842 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 // These are used for determining when all windows associated with
6845 // an activity have been drawn, so they can be made visible together
6846 // at the same time.
6847 int lastTransactionSequence = mTransactionSequence-1;
6848 int numInterestingWindows;
6849 int numDrawnWindows;
6850 boolean inPendingTransaction;
6851 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 // Is this token going to be hidden in a little while? If so, it
6854 // won't be taken into account for setting the screen orientation.
6855 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 // Is this window's surface needed? This is almost like hidden, except
6858 // it will sometimes be true a little earlier: when the token has
6859 // been shown, but is still waiting for its app transition to execute
6860 // before making its windows shown.
6861 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 // Have we told the window clients to hide themselves?
6864 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 // Last visibility state we reported to the app token.
6867 boolean reportedVisible;
6868
6869 // Set to true when the token has been removed from the window mgr.
6870 boolean removed;
6871
6872 // Have we been asked to have this token keep the screen frozen?
6873 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 boolean animating;
6876 Animation animation;
6877 boolean hasTransformation;
6878 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07006879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 // Offset to the window of all layers in the token, for use by
6881 // AppWindowToken animations.
6882 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07006883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006884 // Information about an application starting window if displayed.
6885 StartingData startingData;
6886 WindowState startingWindow;
6887 View startingView;
6888 boolean startingDisplayed;
6889 boolean startingMoved;
6890 boolean firstWindowDrawn;
6891
6892 AppWindowToken(IApplicationToken _token) {
6893 super(_token.asBinder(),
6894 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6895 appWindowToken = this;
6896 appToken = _token;
6897 }
Romain Guy06882f82009-06-10 13:36:04 -07006898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 public void setAnimation(Animation anim) {
6900 if (localLOGV) Log.v(
6901 TAG, "Setting animation in " + this + ": " + anim);
6902 animation = anim;
6903 animating = false;
6904 anim.restrictDuration(MAX_ANIMATION_DURATION);
6905 anim.scaleCurrentDuration(mTransitionAnimationScale);
6906 int zorder = anim.getZAdjustment();
6907 int adj = 0;
6908 if (zorder == Animation.ZORDER_TOP) {
6909 adj = TYPE_LAYER_OFFSET;
6910 } else if (zorder == Animation.ZORDER_BOTTOM) {
6911 adj = -TYPE_LAYER_OFFSET;
6912 }
Romain Guy06882f82009-06-10 13:36:04 -07006913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 if (animLayerAdjustment != adj) {
6915 animLayerAdjustment = adj;
6916 updateLayers();
6917 }
6918 }
Romain Guy06882f82009-06-10 13:36:04 -07006919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920 public void setDummyAnimation() {
6921 if (animation == null) {
6922 if (localLOGV) Log.v(
6923 TAG, "Setting dummy animation in " + this);
6924 animation = sDummyAnimation;
6925 }
6926 }
6927
6928 public void clearAnimation() {
6929 if (animation != null) {
6930 animation = null;
6931 animating = true;
6932 }
6933 }
Romain Guy06882f82009-06-10 13:36:04 -07006934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 void updateLayers() {
6936 final int N = allAppWindows.size();
6937 final int adj = animLayerAdjustment;
6938 for (int i=0; i<N; i++) {
6939 WindowState w = allAppWindows.get(i);
6940 w.mAnimLayer = w.mLayer + adj;
6941 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
6942 + w.mAnimLayer);
6943 if (w == mInputMethodTarget) {
6944 setInputMethodAnimLayerAdjustment(adj);
6945 }
6946 }
6947 }
Romain Guy06882f82009-06-10 13:36:04 -07006948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 void sendAppVisibilityToClients() {
6950 final int N = allAppWindows.size();
6951 for (int i=0; i<N; i++) {
6952 WindowState win = allAppWindows.get(i);
6953 if (win == startingWindow && clientHidden) {
6954 // Don't hide the starting window.
6955 continue;
6956 }
6957 try {
6958 if (DEBUG_VISIBILITY) Log.v(TAG,
6959 "Setting visibility of " + win + ": " + (!clientHidden));
6960 win.mClient.dispatchAppVisibility(!clientHidden);
6961 } catch (RemoteException e) {
6962 }
6963 }
6964 }
Romain Guy06882f82009-06-10 13:36:04 -07006965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 void showAllWindowsLocked() {
6967 final int NW = allAppWindows.size();
6968 for (int i=0; i<NW; i++) {
6969 WindowState w = allAppWindows.get(i);
6970 if (DEBUG_VISIBILITY) Log.v(TAG,
6971 "performing show on: " + w);
6972 w.performShowLocked();
6973 }
6974 }
Romain Guy06882f82009-06-10 13:36:04 -07006975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 // This must be called while inside a transaction.
6977 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6978 if (!mDisplayFrozen) {
6979 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 if (animation == sDummyAnimation) {
6982 // This guy is going to animate, but not yet. For now count
6983 // it is not animating for purposes of scheduling transactions;
6984 // when it is really time to animate, this will be set to
6985 // a real animation and the next call will execute normally.
6986 return false;
6987 }
Romain Guy06882f82009-06-10 13:36:04 -07006988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 if ((allDrawn || animating || startingDisplayed) && animation != null) {
6990 if (!animating) {
6991 if (DEBUG_ANIM) Log.v(
6992 TAG, "Starting animation in " + this +
6993 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
6994 + " scale=" + mTransitionAnimationScale
6995 + " allDrawn=" + allDrawn + " animating=" + animating);
6996 animation.initialize(dw, dh, dw, dh);
6997 animation.setStartTime(currentTime);
6998 animating = true;
6999 }
7000 transformation.clear();
7001 final boolean more = animation.getTransformation(
7002 currentTime, transformation);
7003 if (DEBUG_ANIM) Log.v(
7004 TAG, "Stepped animation in " + this +
7005 ": more=" + more + ", xform=" + transformation);
7006 if (more) {
7007 // we're done!
7008 hasTransformation = true;
7009 return true;
7010 }
7011 if (DEBUG_ANIM) Log.v(
7012 TAG, "Finished animation in " + this +
7013 " @ " + currentTime);
7014 animation = null;
7015 }
7016 } else if (animation != null) {
7017 // If the display is frozen, and there is a pending animation,
7018 // clear it and make sure we run the cleanup code.
7019 animating = true;
7020 animation = null;
7021 }
7022
7023 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025 if (!animating) {
7026 return false;
7027 }
7028
7029 clearAnimation();
7030 animating = false;
7031 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7032 moveInputMethodWindowsIfNeededLocked(true);
7033 }
Romain Guy06882f82009-06-10 13:36:04 -07007034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 if (DEBUG_ANIM) Log.v(
7036 TAG, "Animation done in " + this
7037 + ": reportedVisible=" + reportedVisible);
7038
7039 transformation.clear();
7040 if (animLayerAdjustment != 0) {
7041 animLayerAdjustment = 0;
7042 updateLayers();
7043 }
Romain Guy06882f82009-06-10 13:36:04 -07007044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 final int N = windows.size();
7046 for (int i=0; i<N; i++) {
7047 ((WindowState)windows.get(i)).finishExit();
7048 }
7049 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 return false;
7052 }
7053
7054 void updateReportedVisibilityLocked() {
7055 if (appToken == null) {
7056 return;
7057 }
Romain Guy06882f82009-06-10 13:36:04 -07007058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 int numInteresting = 0;
7060 int numVisible = 0;
7061 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007063 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7064 final int N = allAppWindows.size();
7065 for (int i=0; i<N; i++) {
7066 WindowState win = allAppWindows.get(i);
7067 if (win == startingWindow || win.mAppFreezing) {
7068 continue;
7069 }
7070 if (DEBUG_VISIBILITY) {
7071 Log.v(TAG, "Win " + win + ": isDisplayed="
7072 + win.isDisplayedLw()
7073 + ", isAnimating=" + win.isAnimating());
7074 if (!win.isDisplayedLw()) {
7075 Log.v(TAG, "Not displayed: s=" + win.mSurface
7076 + " pv=" + win.mPolicyVisibility
7077 + " dp=" + win.mDrawPending
7078 + " cdp=" + win.mCommitDrawPending
7079 + " ah=" + win.mAttachedHidden
7080 + " th="
7081 + (win.mAppToken != null
7082 ? win.mAppToken.hiddenRequested : false)
7083 + " a=" + win.mAnimating);
7084 }
7085 }
7086 numInteresting++;
7087 if (win.isDisplayedLw()) {
7088 if (!win.isAnimating()) {
7089 numVisible++;
7090 }
7091 nowGone = false;
7092 } else if (win.isAnimating()) {
7093 nowGone = false;
7094 }
7095 }
Romain Guy06882f82009-06-10 13:36:04 -07007096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7098 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7099 + numInteresting + " visible=" + numVisible);
7100 if (nowVisible != reportedVisible) {
7101 if (DEBUG_VISIBILITY) Log.v(
7102 TAG, "Visibility changed in " + this
7103 + ": vis=" + nowVisible);
7104 reportedVisible = nowVisible;
7105 Message m = mH.obtainMessage(
7106 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7107 nowVisible ? 1 : 0,
7108 nowGone ? 1 : 0,
7109 this);
7110 mH.sendMessage(m);
7111 }
7112 }
Romain Guy06882f82009-06-10 13:36:04 -07007113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 void dump(PrintWriter pw, String prefix) {
7115 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007116 if (appToken != null) {
7117 pw.print(prefix); pw.println("app=true");
7118 }
7119 if (allAppWindows.size() > 0) {
7120 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7121 }
7122 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7123 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7124 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7125 pw.print(" clientHidden="); pw.print(clientHidden);
7126 pw.print(" willBeHidden="); pw.print(willBeHidden);
7127 pw.print(" reportedVisible="); pw.println(reportedVisible);
7128 if (paused || freezingScreen) {
7129 pw.print(prefix); pw.print("paused="); pw.print(paused);
7130 pw.print(" freezingScreen="); pw.println(freezingScreen);
7131 }
7132 if (numInterestingWindows != 0 || numDrawnWindows != 0
7133 || inPendingTransaction || allDrawn) {
7134 pw.print(prefix); pw.print("numInterestingWindows=");
7135 pw.print(numInterestingWindows);
7136 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7137 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7138 pw.print(" allDrawn="); pw.println(allDrawn);
7139 }
7140 if (animating || animation != null) {
7141 pw.print(prefix); pw.print("animating="); pw.print(animating);
7142 pw.print(" animation="); pw.println(animation);
7143 }
7144 if (animLayerAdjustment != 0) {
7145 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7146 }
7147 if (hasTransformation) {
7148 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7149 pw.print(" transformation="); transformation.printShortString(pw);
7150 pw.println();
7151 }
7152 if (startingData != null || removed || firstWindowDrawn) {
7153 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7154 pw.print(" removed="); pw.print(removed);
7155 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7156 }
7157 if (startingWindow != null || startingView != null
7158 || startingDisplayed || startingMoved) {
7159 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7160 pw.print(" startingView="); pw.print(startingView);
7161 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7162 pw.print(" startingMoved"); pw.println(startingMoved);
7163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 }
7165
7166 @Override
7167 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007168 if (stringName == null) {
7169 StringBuilder sb = new StringBuilder();
7170 sb.append("AppWindowToken{");
7171 sb.append(Integer.toHexString(System.identityHashCode(this)));
7172 sb.append(" token="); sb.append(token); sb.append('}');
7173 stringName = sb.toString();
7174 }
7175 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 }
7177 }
Romain Guy06882f82009-06-10 13:36:04 -07007178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 public static WindowManager.LayoutParams findAnimations(
7180 ArrayList<AppWindowToken> order,
7181 ArrayList<AppWindowToken> tokenList1,
7182 ArrayList<AppWindowToken> tokenList2) {
7183 // We need to figure out which animation to use...
7184 WindowManager.LayoutParams animParams = null;
7185 int animSrc = 0;
Romain Guy06882f82009-06-10 13:36:04 -07007186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007187 //Log.i(TAG, "Looking for animations...");
7188 for (int i=order.size()-1; i>=0; i--) {
7189 AppWindowToken wtoken = order.get(i);
7190 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
7191 if (tokenList1.contains(wtoken) || tokenList2.contains(wtoken)) {
7192 int j = wtoken.windows.size();
7193 while (j > 0) {
7194 j--;
7195 WindowState win = wtoken.windows.get(j);
7196 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7197 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7198 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7199 //Log.i(TAG, "Found base or application window, done!");
7200 if (wtoken.appFullscreen) {
7201 return win.mAttrs;
7202 }
7203 if (animSrc < 2) {
7204 animParams = win.mAttrs;
7205 animSrc = 2;
7206 }
7207 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7208 //Log.i(TAG, "Found normal window, we may use this...");
7209 animParams = win.mAttrs;
7210 animSrc = 1;
7211 }
7212 }
7213 }
7214 }
Romain Guy06882f82009-06-10 13:36:04 -07007215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 return animParams;
7217 }
Romain Guy06882f82009-06-10 13:36:04 -07007218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 // -------------------------------------------------------------
7220 // DummyAnimation
7221 // -------------------------------------------------------------
7222
7223 // This is an animation that does nothing: it just immediately finishes
7224 // itself every time it is called. It is used as a stub animation in cases
7225 // where we want to synchronize multiple things that may be animating.
7226 static final class DummyAnimation extends Animation {
7227 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7228 return false;
7229 }
7230 }
7231 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007233 // -------------------------------------------------------------
7234 // Async Handler
7235 // -------------------------------------------------------------
7236
7237 static final class StartingData {
7238 final String pkg;
7239 final int theme;
7240 final CharSequence nonLocalizedLabel;
7241 final int labelRes;
7242 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7245 int _labelRes, int _icon) {
7246 pkg = _pkg;
7247 theme = _theme;
7248 nonLocalizedLabel = _nonLocalizedLabel;
7249 labelRes = _labelRes;
7250 icon = _icon;
7251 }
7252 }
7253
7254 private final class H extends Handler {
7255 public static final int REPORT_FOCUS_CHANGE = 2;
7256 public static final int REPORT_LOSING_FOCUS = 3;
7257 public static final int ANIMATE = 4;
7258 public static final int ADD_STARTING = 5;
7259 public static final int REMOVE_STARTING = 6;
7260 public static final int FINISHED_STARTING = 7;
7261 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7263 public static final int HOLD_SCREEN_CHANGED = 12;
7264 public static final int APP_TRANSITION_TIMEOUT = 13;
7265 public static final int PERSIST_ANIMATION_SCALE = 14;
7266 public static final int FORCE_GC = 15;
7267 public static final int ENABLE_SCREEN = 16;
7268 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007269 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 public H() {
7274 }
Romain Guy06882f82009-06-10 13:36:04 -07007275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 @Override
7277 public void handleMessage(Message msg) {
7278 switch (msg.what) {
7279 case REPORT_FOCUS_CHANGE: {
7280 WindowState lastFocus;
7281 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 synchronized(mWindowMap) {
7284 lastFocus = mLastFocus;
7285 newFocus = mCurrentFocus;
7286 if (lastFocus == newFocus) {
7287 // Focus is not changing, so nothing to do.
7288 return;
7289 }
7290 mLastFocus = newFocus;
7291 //Log.i(TAG, "Focus moving from " + lastFocus
7292 // + " to " + newFocus);
7293 if (newFocus != null && lastFocus != null
7294 && !newFocus.isDisplayedLw()) {
7295 //Log.i(TAG, "Delaying loss of focus...");
7296 mLosingFocus.add(lastFocus);
7297 lastFocus = null;
7298 }
7299 }
7300
7301 if (lastFocus != newFocus) {
7302 //System.out.println("Changing focus from " + lastFocus
7303 // + " to " + newFocus);
7304 if (newFocus != null) {
7305 try {
7306 //Log.i(TAG, "Gaining focus: " + newFocus);
7307 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7308 } catch (RemoteException e) {
7309 // Ignore if process has died.
7310 }
7311 }
7312
7313 if (lastFocus != null) {
7314 try {
7315 //Log.i(TAG, "Losing focus: " + lastFocus);
7316 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7317 } catch (RemoteException e) {
7318 // Ignore if process has died.
7319 }
7320 }
7321 }
7322 } break;
7323
7324 case REPORT_LOSING_FOCUS: {
7325 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 synchronized(mWindowMap) {
7328 losers = mLosingFocus;
7329 mLosingFocus = new ArrayList<WindowState>();
7330 }
7331
7332 final int N = losers.size();
7333 for (int i=0; i<N; i++) {
7334 try {
7335 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7336 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7337 } catch (RemoteException e) {
7338 // Ignore if process has died.
7339 }
7340 }
7341 } break;
7342
7343 case ANIMATE: {
7344 synchronized(mWindowMap) {
7345 mAnimationPending = false;
7346 performLayoutAndPlaceSurfacesLocked();
7347 }
7348 } break;
7349
7350 case ADD_STARTING: {
7351 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7352 final StartingData sd = wtoken.startingData;
7353
7354 if (sd == null) {
7355 // Animation has been canceled... do nothing.
7356 return;
7357 }
Romain Guy06882f82009-06-10 13:36:04 -07007358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7360 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 View view = null;
7363 try {
7364 view = mPolicy.addStartingWindow(
7365 wtoken.token, sd.pkg,
7366 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7367 sd.icon);
7368 } catch (Exception e) {
7369 Log.w(TAG, "Exception when adding starting window", e);
7370 }
7371
7372 if (view != null) {
7373 boolean abort = false;
7374
7375 synchronized(mWindowMap) {
7376 if (wtoken.removed || wtoken.startingData == null) {
7377 // If the window was successfully added, then
7378 // we need to remove it.
7379 if (wtoken.startingWindow != null) {
7380 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7381 "Aborted starting " + wtoken
7382 + ": removed=" + wtoken.removed
7383 + " startingData=" + wtoken.startingData);
7384 wtoken.startingWindow = null;
7385 wtoken.startingData = null;
7386 abort = true;
7387 }
7388 } else {
7389 wtoken.startingView = view;
7390 }
7391 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7392 "Added starting " + wtoken
7393 + ": startingWindow="
7394 + wtoken.startingWindow + " startingView="
7395 + wtoken.startingView);
7396 }
7397
7398 if (abort) {
7399 try {
7400 mPolicy.removeStartingWindow(wtoken.token, view);
7401 } catch (Exception e) {
7402 Log.w(TAG, "Exception when removing starting window", e);
7403 }
7404 }
7405 }
7406 } break;
7407
7408 case REMOVE_STARTING: {
7409 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7410 IBinder token = null;
7411 View view = null;
7412 synchronized (mWindowMap) {
7413 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7414 + wtoken + ": startingWindow="
7415 + wtoken.startingWindow + " startingView="
7416 + wtoken.startingView);
7417 if (wtoken.startingWindow != null) {
7418 view = wtoken.startingView;
7419 token = wtoken.token;
7420 wtoken.startingData = null;
7421 wtoken.startingView = null;
7422 wtoken.startingWindow = null;
7423 }
7424 }
7425 if (view != null) {
7426 try {
7427 mPolicy.removeStartingWindow(token, view);
7428 } catch (Exception e) {
7429 Log.w(TAG, "Exception when removing starting window", e);
7430 }
7431 }
7432 } break;
7433
7434 case FINISHED_STARTING: {
7435 IBinder token = null;
7436 View view = null;
7437 while (true) {
7438 synchronized (mWindowMap) {
7439 final int N = mFinishedStarting.size();
7440 if (N <= 0) {
7441 break;
7442 }
7443 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7444
7445 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7446 "Finished starting " + wtoken
7447 + ": startingWindow=" + wtoken.startingWindow
7448 + " startingView=" + wtoken.startingView);
7449
7450 if (wtoken.startingWindow == null) {
7451 continue;
7452 }
7453
7454 view = wtoken.startingView;
7455 token = wtoken.token;
7456 wtoken.startingData = null;
7457 wtoken.startingView = null;
7458 wtoken.startingWindow = null;
7459 }
7460
7461 try {
7462 mPolicy.removeStartingWindow(token, view);
7463 } catch (Exception e) {
7464 Log.w(TAG, "Exception when removing starting window", e);
7465 }
7466 }
7467 } break;
7468
7469 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7470 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7471
7472 boolean nowVisible = msg.arg1 != 0;
7473 boolean nowGone = msg.arg2 != 0;
7474
7475 try {
7476 if (DEBUG_VISIBILITY) Log.v(
7477 TAG, "Reporting visible in " + wtoken
7478 + " visible=" + nowVisible
7479 + " gone=" + nowGone);
7480 if (nowVisible) {
7481 wtoken.appToken.windowsVisible();
7482 } else {
7483 wtoken.appToken.windowsGone();
7484 }
7485 } catch (RemoteException ex) {
7486 }
7487 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 case WINDOW_FREEZE_TIMEOUT: {
7490 synchronized (mWindowMap) {
7491 Log.w(TAG, "Window freeze timeout expired.");
7492 int i = mWindows.size();
7493 while (i > 0) {
7494 i--;
7495 WindowState w = (WindowState)mWindows.get(i);
7496 if (w.mOrientationChanging) {
7497 w.mOrientationChanging = false;
7498 Log.w(TAG, "Force clearing orientation change: " + w);
7499 }
7500 }
7501 performLayoutAndPlaceSurfacesLocked();
7502 }
7503 break;
7504 }
Romain Guy06882f82009-06-10 13:36:04 -07007505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 case HOLD_SCREEN_CHANGED: {
7507 Session oldHold;
7508 Session newHold;
7509 synchronized (mWindowMap) {
7510 oldHold = mLastReportedHold;
7511 newHold = (Session)msg.obj;
7512 mLastReportedHold = newHold;
7513 }
Romain Guy06882f82009-06-10 13:36:04 -07007514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 if (oldHold != newHold) {
7516 try {
7517 if (oldHold != null) {
7518 mBatteryStats.noteStopWakelock(oldHold.mUid,
7519 "window",
7520 BatteryStats.WAKE_TYPE_WINDOW);
7521 }
7522 if (newHold != null) {
7523 mBatteryStats.noteStartWakelock(newHold.mUid,
7524 "window",
7525 BatteryStats.WAKE_TYPE_WINDOW);
7526 }
7527 } catch (RemoteException e) {
7528 }
7529 }
7530 break;
7531 }
Romain Guy06882f82009-06-10 13:36:04 -07007532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 case APP_TRANSITION_TIMEOUT: {
7534 synchronized (mWindowMap) {
7535 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7536 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7537 "*** APP TRANSITION TIMEOUT");
7538 mAppTransitionReady = true;
7539 mAppTransitionTimeout = true;
7540 performLayoutAndPlaceSurfacesLocked();
7541 }
7542 }
7543 break;
7544 }
Romain Guy06882f82009-06-10 13:36:04 -07007545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007546 case PERSIST_ANIMATION_SCALE: {
7547 Settings.System.putFloat(mContext.getContentResolver(),
7548 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7549 Settings.System.putFloat(mContext.getContentResolver(),
7550 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7551 break;
7552 }
Romain Guy06882f82009-06-10 13:36:04 -07007553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007554 case FORCE_GC: {
7555 synchronized(mWindowMap) {
7556 if (mAnimationPending) {
7557 // If we are animating, don't do the gc now but
7558 // delay a bit so we don't interrupt the animation.
7559 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7560 2000);
7561 return;
7562 }
7563 // If we are currently rotating the display, it will
7564 // schedule a new message when done.
7565 if (mDisplayFrozen) {
7566 return;
7567 }
7568 mFreezeGcPending = 0;
7569 }
7570 Runtime.getRuntime().gc();
7571 break;
7572 }
Romain Guy06882f82009-06-10 13:36:04 -07007573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 case ENABLE_SCREEN: {
7575 performEnableScreen();
7576 break;
7577 }
Romain Guy06882f82009-06-10 13:36:04 -07007578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 case APP_FREEZE_TIMEOUT: {
7580 synchronized (mWindowMap) {
7581 Log.w(TAG, "App freeze timeout expired.");
7582 int i = mAppTokens.size();
7583 while (i > 0) {
7584 i--;
7585 AppWindowToken tok = mAppTokens.get(i);
7586 if (tok.freezingScreen) {
7587 Log.w(TAG, "Force clearing freeze: " + tok);
7588 unsetAppFreezingScreenLocked(tok, true, true);
7589 }
7590 }
7591 }
7592 break;
7593 }
Romain Guy06882f82009-06-10 13:36:04 -07007594
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007595 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007596 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007597 sendNewConfiguration();
7598 }
7599 break;
7600 }
Romain Guy06882f82009-06-10 13:36:04 -07007601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007602 }
7603 }
7604 }
7605
7606 // -------------------------------------------------------------
7607 // IWindowManager API
7608 // -------------------------------------------------------------
7609
7610 public IWindowSession openSession(IInputMethodClient client,
7611 IInputContext inputContext) {
7612 if (client == null) throw new IllegalArgumentException("null client");
7613 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7614 return new Session(client, inputContext);
7615 }
7616
7617 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7618 synchronized (mWindowMap) {
7619 // The focus for the client is the window immediately below
7620 // where we would place the input method window.
7621 int idx = findDesiredInputMethodWindowIndexLocked(false);
7622 WindowState imFocus;
7623 if (idx > 0) {
7624 imFocus = (WindowState)mWindows.get(idx-1);
7625 if (imFocus != null) {
7626 if (imFocus.mSession.mClient != null &&
7627 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7628 return true;
7629 }
7630 }
7631 }
7632 }
7633 return false;
7634 }
Romain Guy06882f82009-06-10 13:36:04 -07007635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 // -------------------------------------------------------------
7637 // Internals
7638 // -------------------------------------------------------------
7639
7640 final WindowState windowForClientLocked(Session session, IWindow client) {
7641 return windowForClientLocked(session, client.asBinder());
7642 }
Romain Guy06882f82009-06-10 13:36:04 -07007643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 final WindowState windowForClientLocked(Session session, IBinder client) {
7645 WindowState win = mWindowMap.get(client);
7646 if (localLOGV) Log.v(
7647 TAG, "Looking up client " + client + ": " + win);
7648 if (win == null) {
7649 RuntimeException ex = new RuntimeException();
7650 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7651 return null;
7652 }
7653 if (session != null && win.mSession != session) {
7654 RuntimeException ex = new RuntimeException();
7655 Log.w(TAG, "Requested window " + client + " is in session " +
7656 win.mSession + ", not " + session, ex);
7657 return null;
7658 }
7659
7660 return win;
7661 }
7662
7663 private final void assignLayersLocked() {
7664 int N = mWindows.size();
7665 int curBaseLayer = 0;
7666 int curLayer = 0;
7667 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 for (i=0; i<N; i++) {
7670 WindowState w = (WindowState)mWindows.get(i);
7671 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7672 curLayer += WINDOW_LAYER_MULTIPLIER;
7673 w.mLayer = curLayer;
7674 } else {
7675 curBaseLayer = curLayer = w.mBaseLayer;
7676 w.mLayer = curLayer;
7677 }
7678 if (w.mTargetAppToken != null) {
7679 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7680 } else if (w.mAppToken != null) {
7681 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7682 } else {
7683 w.mAnimLayer = w.mLayer;
7684 }
7685 if (w.mIsImWindow) {
7686 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7687 }
7688 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7689 + w.mAnimLayer);
7690 //System.out.println(
7691 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7692 }
7693 }
7694
7695 private boolean mInLayout = false;
7696 private final void performLayoutAndPlaceSurfacesLocked() {
7697 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007698 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 throw new RuntimeException("Recursive call!");
7700 }
7701 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7702 return;
7703 }
7704
7705 boolean recoveringMemory = false;
7706 if (mForceRemoves != null) {
7707 recoveringMemory = true;
7708 // Wait a little it for things to settle down, and off we go.
7709 for (int i=0; i<mForceRemoves.size(); i++) {
7710 WindowState ws = mForceRemoves.get(i);
7711 Log.i(TAG, "Force removing: " + ws);
7712 removeWindowInnerLocked(ws.mSession, ws);
7713 }
7714 mForceRemoves = null;
7715 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7716 Object tmp = new Object();
7717 synchronized (tmp) {
7718 try {
7719 tmp.wait(250);
7720 } catch (InterruptedException e) {
7721 }
7722 }
7723 }
Romain Guy06882f82009-06-10 13:36:04 -07007724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 mInLayout = true;
7726 try {
7727 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 int i = mPendingRemove.size()-1;
7730 if (i >= 0) {
7731 while (i >= 0) {
7732 WindowState w = mPendingRemove.get(i);
7733 removeWindowInnerLocked(w.mSession, w);
7734 i--;
7735 }
7736 mPendingRemove.clear();
7737
7738 mInLayout = false;
7739 assignLayersLocked();
7740 mLayoutNeeded = true;
7741 performLayoutAndPlaceSurfacesLocked();
7742
7743 } else {
7744 mInLayout = false;
7745 if (mLayoutNeeded) {
7746 requestAnimationLocked(0);
7747 }
7748 }
7749 } catch (RuntimeException e) {
7750 mInLayout = false;
7751 Log.e(TAG, "Unhandled exception while layout out windows", e);
7752 }
7753 }
7754
7755 private final void performLayoutLockedInner() {
7756 final int dw = mDisplay.getWidth();
7757 final int dh = mDisplay.getHeight();
7758
7759 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007760 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 int i;
7762
7763 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007764
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007765 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 mPolicy.beginLayoutLw(dw, dh);
7767
7768 // First perform layout of any root windows (not attached
7769 // to another window).
7770 int topAttached = -1;
7771 for (i = N-1; i >= 0; i--) {
7772 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007773
7774 // Don't do layout of a window if it is not visible, or
7775 // soon won't be visible, to avoid wasting time and funky
7776 // changes while a window is animating away.
7777 final AppWindowToken atoken = win.mAppToken;
7778 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007780 || win.mRootToken.hidden
7781 || (atoken != null && atoken.hiddenRequested)
7782 || !win.mPolicyVisibility
7783 || win.mAttachedHidden
7784 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785
7786 // If this view is GONE, then skip it -- keep the current
7787 // frame, and let the caller know so they can ignore it
7788 // if they want. (We do the normal layout for INVISIBLE
7789 // windows, since that means "perform layout as normal,
7790 // just don't display").
7791 if (!gone || !win.mHaveFrame) {
7792 if (!win.mLayoutAttached) {
7793 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7794 } else {
7795 if (topAttached < 0) topAttached = i;
7796 }
7797 }
7798 }
Romain Guy06882f82009-06-10 13:36:04 -07007799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 // Now perform layout of attached windows, which usually
7801 // depend on the position of the window they are attached to.
7802 // XXX does not deal with windows that are attached to windows
7803 // that are themselves attached.
7804 for (i = topAttached; i >= 0; i--) {
7805 WindowState win = (WindowState) mWindows.get(i);
7806
7807 // If this view is GONE, then skip it -- keep the current
7808 // frame, and let the caller know so they can ignore it
7809 // if they want. (We do the normal layout for INVISIBLE
7810 // windows, since that means "perform layout as normal,
7811 // just don't display").
7812 if (win.mLayoutAttached) {
7813 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7814 || !win.mHaveFrame) {
7815 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7816 }
7817 }
7818 }
7819
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007820 if (!mPolicy.finishLayoutLw()) {
7821 mLayoutNeeded = false;
7822 } else if (repeats > 2) {
7823 Log.w(TAG, "Layout repeat aborted after too many iterations");
7824 mLayoutNeeded = false;
7825 } else {
7826 repeats++;
7827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 }
7829 }
Romain Guy06882f82009-06-10 13:36:04 -07007830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 private final void performLayoutAndPlaceSurfacesLockedInner(
7832 boolean recoveringMemory) {
7833 final long currentTime = SystemClock.uptimeMillis();
7834 final int dw = mDisplay.getWidth();
7835 final int dh = mDisplay.getHeight();
7836
7837 final int N = mWindows.size();
7838 int i;
7839
7840 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007841 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 if (mFxSession == null) {
7844 mFxSession = new SurfaceSession();
7845 }
Romain Guy06882f82009-06-10 13:36:04 -07007846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7848
7849 // Initialize state of exiting tokens.
7850 for (i=mExitingTokens.size()-1; i>=0; i--) {
7851 mExitingTokens.get(i).hasVisible = false;
7852 }
7853
7854 // Initialize state of exiting applications.
7855 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7856 mExitingAppTokens.get(i).hasVisible = false;
7857 }
7858
7859 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 boolean orientationChangeComplete = true;
7861 Session holdScreen = null;
7862 float screenBrightness = -1;
7863 boolean focusDisplayed = false;
7864 boolean animating = false;
7865
7866 Surface.openTransaction();
7867 try {
7868 boolean restart;
7869
7870 do {
7871 final int transactionSequence = ++mTransactionSequence;
7872
7873 // Update animations of all applications, including those
7874 // associated with exiting/removed apps
7875 boolean tokensAnimating = false;
7876 final int NAT = mAppTokens.size();
7877 for (i=0; i<NAT; i++) {
7878 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7879 tokensAnimating = true;
7880 }
7881 }
7882 final int NEAT = mExitingAppTokens.size();
7883 for (i=0; i<NEAT; i++) {
7884 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7885 tokensAnimating = true;
7886 }
7887 }
7888
7889 animating = tokensAnimating;
7890 restart = false;
7891
7892 boolean tokenMayBeDrawn = false;
7893
7894 mPolicy.beginAnimationLw(dw, dh);
7895
7896 for (i=N-1; i>=0; i--) {
7897 WindowState w = (WindowState)mWindows.get(i);
7898
7899 final WindowManager.LayoutParams attrs = w.mAttrs;
7900
7901 if (w.mSurface != null) {
7902 // Execute animation.
7903 w.commitFinishDrawingLocked(currentTime);
7904 if (w.stepAnimationLocked(currentTime, dw, dh)) {
7905 animating = true;
7906 //w.dump(" ");
7907 }
7908
7909 mPolicy.animatingWindowLw(w, attrs);
7910 }
7911
7912 final AppWindowToken atoken = w.mAppToken;
7913 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
7914 if (atoken.lastTransactionSequence != transactionSequence) {
7915 atoken.lastTransactionSequence = transactionSequence;
7916 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
7917 atoken.startingDisplayed = false;
7918 }
7919 if ((w.isOnScreen() || w.mAttrs.type
7920 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
7921 && !w.mExiting && !w.mDestroying) {
7922 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
7923 Log.v(TAG, "Eval win " + w + ": isDisplayed="
7924 + w.isDisplayedLw()
7925 + ", isAnimating=" + w.isAnimating());
7926 if (!w.isDisplayedLw()) {
7927 Log.v(TAG, "Not displayed: s=" + w.mSurface
7928 + " pv=" + w.mPolicyVisibility
7929 + " dp=" + w.mDrawPending
7930 + " cdp=" + w.mCommitDrawPending
7931 + " ah=" + w.mAttachedHidden
7932 + " th=" + atoken.hiddenRequested
7933 + " a=" + w.mAnimating);
7934 }
7935 }
7936 if (w != atoken.startingWindow) {
7937 if (!atoken.freezingScreen || !w.mAppFreezing) {
7938 atoken.numInterestingWindows++;
7939 if (w.isDisplayedLw()) {
7940 atoken.numDrawnWindows++;
7941 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
7942 "tokenMayBeDrawn: " + atoken
7943 + " freezingScreen=" + atoken.freezingScreen
7944 + " mAppFreezing=" + w.mAppFreezing);
7945 tokenMayBeDrawn = true;
7946 }
7947 }
7948 } else if (w.isDisplayedLw()) {
7949 atoken.startingDisplayed = true;
7950 }
7951 }
7952 } else if (w.mReadyToShow) {
7953 w.performShowLocked();
7954 }
7955 }
7956
7957 if (mPolicy.finishAnimationLw()) {
7958 restart = true;
7959 }
7960
7961 if (tokenMayBeDrawn) {
7962 // See if any windows have been drawn, so they (and others
7963 // associated with them) can now be shown.
7964 final int NT = mTokenList.size();
7965 for (i=0; i<NT; i++) {
7966 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
7967 if (wtoken == null) {
7968 continue;
7969 }
7970 if (wtoken.freezingScreen) {
7971 int numInteresting = wtoken.numInterestingWindows;
7972 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
7973 if (DEBUG_VISIBILITY) Log.v(TAG,
7974 "allDrawn: " + wtoken
7975 + " interesting=" + numInteresting
7976 + " drawn=" + wtoken.numDrawnWindows);
7977 wtoken.showAllWindowsLocked();
7978 unsetAppFreezingScreenLocked(wtoken, false, true);
7979 orientationChangeComplete = true;
7980 }
7981 } else if (!wtoken.allDrawn) {
7982 int numInteresting = wtoken.numInterestingWindows;
7983 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
7984 if (DEBUG_VISIBILITY) Log.v(TAG,
7985 "allDrawn: " + wtoken
7986 + " interesting=" + numInteresting
7987 + " drawn=" + wtoken.numDrawnWindows);
7988 wtoken.allDrawn = true;
7989 restart = true;
7990
7991 // We can now show all of the drawn windows!
7992 if (!mOpeningApps.contains(wtoken)) {
7993 wtoken.showAllWindowsLocked();
7994 }
7995 }
7996 }
7997 }
7998 }
7999
8000 // If we are ready to perform an app transition, check through
8001 // all of the app tokens to be shown and see if they are ready
8002 // to go.
8003 if (mAppTransitionReady) {
8004 int NN = mOpeningApps.size();
8005 boolean goodToGo = true;
8006 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8007 "Checking " + NN + " opening apps (frozen="
8008 + mDisplayFrozen + " timeout="
8009 + mAppTransitionTimeout + ")...");
8010 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8011 // If the display isn't frozen, wait to do anything until
8012 // all of the apps are ready. Otherwise just go because
8013 // we'll unfreeze the display when everyone is ready.
8014 for (i=0; i<NN && goodToGo; i++) {
8015 AppWindowToken wtoken = mOpeningApps.get(i);
8016 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8017 "Check opening app" + wtoken + ": allDrawn="
8018 + wtoken.allDrawn + " startingDisplayed="
8019 + wtoken.startingDisplayed);
8020 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8021 && !wtoken.startingMoved) {
8022 goodToGo = false;
8023 }
8024 }
8025 }
8026 if (goodToGo) {
8027 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8028 int transit = mNextAppTransition;
8029 if (mSkipAppTransitionAnimation) {
8030 transit = WindowManagerPolicy.TRANSIT_NONE;
8031 }
8032 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8033 mAppTransitionReady = false;
8034 mAppTransitionTimeout = false;
8035 mStartingIconInTransition = false;
8036 mSkipAppTransitionAnimation = false;
8037
8038 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8039
8040 // We need to figure out which animation to use...
8041 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8042 mOpeningApps, mClosingApps);
8043
8044 NN = mOpeningApps.size();
8045 for (i=0; i<NN; i++) {
8046 AppWindowToken wtoken = mOpeningApps.get(i);
8047 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8048 "Now opening app" + wtoken);
8049 wtoken.reportedVisible = false;
8050 wtoken.inPendingTransaction = false;
8051 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8052 wtoken.updateReportedVisibilityLocked();
8053 wtoken.showAllWindowsLocked();
8054 }
8055 NN = mClosingApps.size();
8056 for (i=0; i<NN; i++) {
8057 AppWindowToken wtoken = mClosingApps.get(i);
8058 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8059 "Now closing app" + wtoken);
8060 wtoken.inPendingTransaction = false;
8061 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8062 wtoken.updateReportedVisibilityLocked();
8063 // Force the allDrawn flag, because we want to start
8064 // this guy's animations regardless of whether it's
8065 // gotten drawn.
8066 wtoken.allDrawn = true;
8067 }
8068
8069 mOpeningApps.clear();
8070 mClosingApps.clear();
8071
8072 // This has changed the visibility of windows, so perform
8073 // a new layout to get them all up-to-date.
8074 mLayoutNeeded = true;
8075 moveInputMethodWindowsIfNeededLocked(true);
8076 performLayoutLockedInner();
8077 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8078
8079 restart = true;
8080 }
8081 }
8082 } while (restart);
8083
8084 // THIRD LOOP: Update the surfaces of all windows.
8085
8086 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8087
8088 boolean obscured = false;
8089 boolean blurring = false;
8090 boolean dimming = false;
8091 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008092 boolean syswin = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008093
8094 for (i=N-1; i>=0; i--) {
8095 WindowState w = (WindowState)mWindows.get(i);
8096
8097 boolean displayed = false;
8098 final WindowManager.LayoutParams attrs = w.mAttrs;
8099 final int attrFlags = attrs.flags;
8100
8101 if (w.mSurface != null) {
8102 w.computeShownFrameLocked();
8103 if (localLOGV) Log.v(
8104 TAG, "Placing surface #" + i + " " + w.mSurface
8105 + ": new=" + w.mShownFrame + ", old="
8106 + w.mLastShownFrame);
8107
8108 boolean resize;
8109 int width, height;
8110 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8111 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8112 w.mLastRequestedHeight != w.mRequestedHeight;
8113 // for a scaled surface, we just want to use
8114 // the requested size.
8115 width = w.mRequestedWidth;
8116 height = w.mRequestedHeight;
8117 w.mLastRequestedWidth = width;
8118 w.mLastRequestedHeight = height;
8119 w.mLastShownFrame.set(w.mShownFrame);
8120 try {
8121 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8122 } catch (RuntimeException e) {
8123 Log.w(TAG, "Error positioning surface in " + w, e);
8124 if (!recoveringMemory) {
8125 reclaimSomeSurfaceMemoryLocked(w, "position");
8126 }
8127 }
8128 } else {
8129 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8130 width = w.mShownFrame.width();
8131 height = w.mShownFrame.height();
8132 w.mLastShownFrame.set(w.mShownFrame);
8133 if (resize) {
8134 if (SHOW_TRANSACTIONS) Log.i(
8135 TAG, " SURFACE " + w.mSurface + ": ("
8136 + w.mShownFrame.left + ","
8137 + w.mShownFrame.top + ") ("
8138 + w.mShownFrame.width() + "x"
8139 + w.mShownFrame.height() + ")");
8140 }
8141 }
8142
8143 if (resize) {
8144 if (width < 1) width = 1;
8145 if (height < 1) height = 1;
8146 if (w.mSurface != null) {
8147 try {
8148 w.mSurface.setSize(width, height);
8149 w.mSurface.setPosition(w.mShownFrame.left,
8150 w.mShownFrame.top);
8151 } catch (RuntimeException e) {
8152 // If something goes wrong with the surface (such
8153 // as running out of memory), don't take down the
8154 // entire system.
8155 Log.e(TAG, "Failure updating surface of " + w
8156 + "size=(" + width + "x" + height
8157 + "), pos=(" + w.mShownFrame.left
8158 + "," + w.mShownFrame.top + ")", e);
8159 if (!recoveringMemory) {
8160 reclaimSomeSurfaceMemoryLocked(w, "size");
8161 }
8162 }
8163 }
8164 }
8165 if (!w.mAppFreezing) {
8166 w.mContentInsetsChanged =
8167 !w.mLastContentInsets.equals(w.mContentInsets);
8168 w.mVisibleInsetsChanged =
8169 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008170 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 || w.mContentInsetsChanged
8172 || w.mVisibleInsetsChanged) {
8173 w.mLastFrame.set(w.mFrame);
8174 w.mLastContentInsets.set(w.mContentInsets);
8175 w.mLastVisibleInsets.set(w.mVisibleInsets);
8176 // If the orientation is changing, then we need to
8177 // hold off on unfreezing the display until this
8178 // window has been redrawn; to do that, we need
8179 // to go through the process of getting informed
8180 // by the application when it has finished drawing.
8181 if (w.mOrientationChanging) {
8182 if (DEBUG_ORIENTATION) Log.v(TAG,
8183 "Orientation start waiting for draw in "
8184 + w + ", surface " + w.mSurface);
8185 w.mDrawPending = true;
8186 w.mCommitDrawPending = false;
8187 w.mReadyToShow = false;
8188 if (w.mAppToken != null) {
8189 w.mAppToken.allDrawn = false;
8190 }
8191 }
Romain Guy06882f82009-06-10 13:36:04 -07008192 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 "Resizing window " + w + " to " + w.mFrame);
8194 mResizingWindows.add(w);
8195 } else if (w.mOrientationChanging) {
8196 if (!w.mDrawPending && !w.mCommitDrawPending) {
8197 if (DEBUG_ORIENTATION) Log.v(TAG,
8198 "Orientation not waiting for draw in "
8199 + w + ", surface " + w.mSurface);
8200 w.mOrientationChanging = false;
8201 }
8202 }
8203 }
8204
8205 if (w.mAttachedHidden) {
8206 if (!w.mLastHidden) {
8207 //dump();
8208 w.mLastHidden = true;
8209 if (SHOW_TRANSACTIONS) Log.i(
8210 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8211 if (w.mSurface != null) {
8212 try {
8213 w.mSurface.hide();
8214 } catch (RuntimeException e) {
8215 Log.w(TAG, "Exception hiding surface in " + w);
8216 }
8217 }
8218 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8219 }
8220 // If we are waiting for this window to handle an
8221 // orientation change, well, it is hidden, so
8222 // doesn't really matter. Note that this does
8223 // introduce a potential glitch if the window
8224 // becomes unhidden before it has drawn for the
8225 // new orientation.
8226 if (w.mOrientationChanging) {
8227 w.mOrientationChanging = false;
8228 if (DEBUG_ORIENTATION) Log.v(TAG,
8229 "Orientation change skips hidden " + w);
8230 }
8231 } else if (!w.isReadyForDisplay()) {
8232 if (!w.mLastHidden) {
8233 //dump();
8234 w.mLastHidden = true;
8235 if (SHOW_TRANSACTIONS) Log.i(
8236 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8237 if (w.mSurface != null) {
8238 try {
8239 w.mSurface.hide();
8240 } catch (RuntimeException e) {
8241 Log.w(TAG, "Exception exception hiding surface in " + w);
8242 }
8243 }
8244 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8245 }
8246 // If we are waiting for this window to handle an
8247 // orientation change, well, it is hidden, so
8248 // doesn't really matter. Note that this does
8249 // introduce a potential glitch if the window
8250 // becomes unhidden before it has drawn for the
8251 // new orientation.
8252 if (w.mOrientationChanging) {
8253 w.mOrientationChanging = false;
8254 if (DEBUG_ORIENTATION) Log.v(TAG,
8255 "Orientation change skips hidden " + w);
8256 }
8257 } else if (w.mLastLayer != w.mAnimLayer
8258 || w.mLastAlpha != w.mShownAlpha
8259 || w.mLastDsDx != w.mDsDx
8260 || w.mLastDtDx != w.mDtDx
8261 || w.mLastDsDy != w.mDsDy
8262 || w.mLastDtDy != w.mDtDy
8263 || w.mLastHScale != w.mHScale
8264 || w.mLastVScale != w.mVScale
8265 || w.mLastHidden) {
8266 displayed = true;
8267 w.mLastAlpha = w.mShownAlpha;
8268 w.mLastLayer = w.mAnimLayer;
8269 w.mLastDsDx = w.mDsDx;
8270 w.mLastDtDx = w.mDtDx;
8271 w.mLastDsDy = w.mDsDy;
8272 w.mLastDtDy = w.mDtDy;
8273 w.mLastHScale = w.mHScale;
8274 w.mLastVScale = w.mVScale;
8275 if (SHOW_TRANSACTIONS) Log.i(
8276 TAG, " SURFACE " + w.mSurface + ": alpha="
8277 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8278 if (w.mSurface != null) {
8279 try {
8280 w.mSurface.setAlpha(w.mShownAlpha);
8281 w.mSurface.setLayer(w.mAnimLayer);
8282 w.mSurface.setMatrix(
8283 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8284 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8285 } catch (RuntimeException e) {
8286 Log.w(TAG, "Error updating surface in " + w, e);
8287 if (!recoveringMemory) {
8288 reclaimSomeSurfaceMemoryLocked(w, "update");
8289 }
8290 }
8291 }
8292
8293 if (w.mLastHidden && !w.mDrawPending
8294 && !w.mCommitDrawPending
8295 && !w.mReadyToShow) {
8296 if (SHOW_TRANSACTIONS) Log.i(
8297 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8298 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8299 + " during relayout");
8300 if (showSurfaceRobustlyLocked(w)) {
8301 w.mHasDrawn = true;
8302 w.mLastHidden = false;
8303 } else {
8304 w.mOrientationChanging = false;
8305 }
8306 }
8307 if (w.mSurface != null) {
8308 w.mToken.hasVisible = true;
8309 }
8310 } else {
8311 displayed = true;
8312 }
8313
8314 if (displayed) {
8315 if (!covered) {
8316 if (attrs.width == LayoutParams.FILL_PARENT
8317 && attrs.height == LayoutParams.FILL_PARENT) {
8318 covered = true;
8319 }
8320 }
8321 if (w.mOrientationChanging) {
8322 if (w.mDrawPending || w.mCommitDrawPending) {
8323 orientationChangeComplete = false;
8324 if (DEBUG_ORIENTATION) Log.v(TAG,
8325 "Orientation continue waiting for draw in " + w);
8326 } else {
8327 w.mOrientationChanging = false;
8328 if (DEBUG_ORIENTATION) Log.v(TAG,
8329 "Orientation change complete in " + w);
8330 }
8331 }
8332 w.mToken.hasVisible = true;
8333 }
8334 } else if (w.mOrientationChanging) {
8335 if (DEBUG_ORIENTATION) Log.v(TAG,
8336 "Orientation change skips hidden " + w);
8337 w.mOrientationChanging = false;
8338 }
8339
8340 final boolean canBeSeen = w.isDisplayedLw();
8341
8342 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8343 focusDisplayed = true;
8344 }
8345
8346 // Update effect.
8347 if (!obscured) {
8348 if (w.mSurface != null) {
8349 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8350 holdScreen = w.mSession;
8351 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008352 if (!syswin && w.mAttrs.screenBrightness >= 0
8353 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 screenBrightness = w.mAttrs.screenBrightness;
8355 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008356 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8357 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8358 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8359 syswin = true;
8360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 }
8362 if (w.isFullscreenOpaque(dw, dh)) {
8363 // This window completely covers everything behind it,
8364 // so we want to leave all of them as unblurred (for
8365 // performance reasons).
8366 obscured = true;
8367 } else if (canBeSeen && !obscured &&
8368 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8369 if (localLOGV) Log.v(TAG, "Win " + w
8370 + ": blurring=" + blurring
8371 + " obscured=" + obscured
8372 + " displayed=" + displayed);
8373 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8374 if (!dimming) {
8375 //Log.i(TAG, "DIM BEHIND: " + w);
8376 dimming = true;
8377 mDimShown = true;
8378 if (mDimSurface == null) {
8379 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8380 + mDimSurface + ": CREATE");
8381 try {
Romain Guy06882f82009-06-10 13:36:04 -07008382 mDimSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008383 -1, 16, 16,
8384 PixelFormat.OPAQUE,
8385 Surface.FX_SURFACE_DIM);
8386 } catch (Exception e) {
8387 Log.e(TAG, "Exception creating Dim surface", e);
8388 }
8389 }
8390 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8391 + mDimSurface + ": SHOW pos=(0,0) (" +
8392 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8393 if (mDimSurface != null) {
8394 try {
8395 mDimSurface.setPosition(0, 0);
8396 mDimSurface.setSize(dw, dh);
8397 mDimSurface.show();
8398 } catch (RuntimeException e) {
8399 Log.w(TAG, "Failure showing dim surface", e);
8400 }
8401 }
8402 }
8403 mDimSurface.setLayer(w.mAnimLayer-1);
8404 final float target = w.mExiting ? 0 : attrs.dimAmount;
8405 if (mDimTargetAlpha != target) {
8406 // If the desired dim level has changed, then
8407 // start an animation to it.
8408 mLastDimAnimTime = currentTime;
8409 long duration = (w.mAnimating && w.mAnimation != null)
8410 ? w.mAnimation.computeDurationHint()
8411 : DEFAULT_DIM_DURATION;
8412 if (target > mDimTargetAlpha) {
8413 // This is happening behind the activity UI,
8414 // so we can make it run a little longer to
8415 // give a stronger impression without disrupting
8416 // the user.
8417 duration *= DIM_DURATION_MULTIPLIER;
8418 }
8419 if (duration < 1) {
8420 // Don't divide by zero
8421 duration = 1;
8422 }
8423 mDimTargetAlpha = target;
8424 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha)
8425 / duration;
8426 }
8427 }
8428 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8429 if (!blurring) {
8430 //Log.i(TAG, "BLUR BEHIND: " + w);
8431 blurring = true;
8432 mBlurShown = true;
8433 if (mBlurSurface == null) {
8434 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8435 + mBlurSurface + ": CREATE");
8436 try {
Romain Guy06882f82009-06-10 13:36:04 -07008437 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008438 -1, 16, 16,
8439 PixelFormat.OPAQUE,
8440 Surface.FX_SURFACE_BLUR);
8441 } catch (Exception e) {
8442 Log.e(TAG, "Exception creating Blur surface", e);
8443 }
8444 }
8445 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8446 + mBlurSurface + ": SHOW pos=(0,0) (" +
8447 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8448 if (mBlurSurface != null) {
8449 mBlurSurface.setPosition(0, 0);
8450 mBlurSurface.setSize(dw, dh);
8451 try {
8452 mBlurSurface.show();
8453 } catch (RuntimeException e) {
8454 Log.w(TAG, "Failure showing blur surface", e);
8455 }
8456 }
8457 }
8458 mBlurSurface.setLayer(w.mAnimLayer-2);
8459 }
8460 }
8461 }
8462 }
8463
8464 if (!dimming && mDimShown) {
8465 // Time to hide the dim surface... start fading.
8466 if (mDimTargetAlpha != 0) {
8467 mLastDimAnimTime = currentTime;
8468 mDimTargetAlpha = 0;
8469 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
8470 }
8471 }
8472
8473 if (mDimShown && mLastDimAnimTime != 0) {
8474 mDimCurrentAlpha += mDimDeltaPerMs
8475 * (currentTime-mLastDimAnimTime);
8476 boolean more = true;
8477 if (mDisplayFrozen) {
8478 // If the display is frozen, there is no reason to animate.
8479 more = false;
8480 } else if (mDimDeltaPerMs > 0) {
8481 if (mDimCurrentAlpha > mDimTargetAlpha) {
8482 more = false;
8483 }
8484 } else if (mDimDeltaPerMs < 0) {
8485 if (mDimCurrentAlpha < mDimTargetAlpha) {
8486 more = false;
8487 }
8488 } else {
8489 more = false;
8490 }
Romain Guy06882f82009-06-10 13:36:04 -07008491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 // Do we need to continue animating?
8493 if (more) {
8494 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8495 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
8496 mLastDimAnimTime = currentTime;
8497 mDimSurface.setAlpha(mDimCurrentAlpha);
8498 animating = true;
8499 } else {
8500 mDimCurrentAlpha = mDimTargetAlpha;
8501 mLastDimAnimTime = 0;
8502 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8503 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
8504 mDimSurface.setAlpha(mDimCurrentAlpha);
8505 if (!dimming) {
8506 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
8507 + ": HIDE");
8508 try {
8509 mDimSurface.hide();
8510 } catch (RuntimeException e) {
8511 Log.w(TAG, "Illegal argument exception hiding dim surface");
8512 }
8513 mDimShown = false;
8514 }
8515 }
8516 }
Romain Guy06882f82009-06-10 13:36:04 -07008517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 if (!blurring && mBlurShown) {
8519 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8520 + ": HIDE");
8521 try {
8522 mBlurSurface.hide();
8523 } catch (IllegalArgumentException e) {
8524 Log.w(TAG, "Illegal argument exception hiding blur surface");
8525 }
8526 mBlurShown = false;
8527 }
8528
8529 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8530 } catch (RuntimeException e) {
8531 Log.e(TAG, "Unhandled exception in Window Manager", e);
8532 }
8533
8534 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8537 "With display frozen, orientationChangeComplete="
8538 + orientationChangeComplete);
8539 if (orientationChangeComplete) {
8540 if (mWindowsFreezingScreen) {
8541 mWindowsFreezingScreen = false;
8542 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8543 }
8544 if (mAppsFreezingScreen == 0) {
8545 stopFreezingDisplayLocked();
8546 }
8547 }
Romain Guy06882f82009-06-10 13:36:04 -07008548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 i = mResizingWindows.size();
8550 if (i > 0) {
8551 do {
8552 i--;
8553 WindowState win = mResizingWindows.get(i);
8554 try {
8555 win.mClient.resized(win.mFrame.width(),
8556 win.mFrame.height(), win.mLastContentInsets,
8557 win.mLastVisibleInsets, win.mDrawPending);
8558 win.mContentInsetsChanged = false;
8559 win.mVisibleInsetsChanged = false;
8560 } catch (RemoteException e) {
8561 win.mOrientationChanging = false;
8562 }
8563 } while (i > 0);
8564 mResizingWindows.clear();
8565 }
Romain Guy06882f82009-06-10 13:36:04 -07008566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 // Destroy the surface of any windows that are no longer visible.
8568 i = mDestroySurface.size();
8569 if (i > 0) {
8570 do {
8571 i--;
8572 WindowState win = mDestroySurface.get(i);
8573 win.mDestroying = false;
8574 if (mInputMethodWindow == win) {
8575 mInputMethodWindow = null;
8576 }
8577 win.destroySurfaceLocked();
8578 } while (i > 0);
8579 mDestroySurface.clear();
8580 }
8581
8582 // Time to remove any exiting tokens?
8583 for (i=mExitingTokens.size()-1; i>=0; i--) {
8584 WindowToken token = mExitingTokens.get(i);
8585 if (!token.hasVisible) {
8586 mExitingTokens.remove(i);
8587 }
8588 }
8589
8590 // Time to remove any exiting applications?
8591 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8592 AppWindowToken token = mExitingAppTokens.get(i);
8593 if (!token.hasVisible && !mClosingApps.contains(token)) {
8594 mAppTokens.remove(token);
8595 mExitingAppTokens.remove(i);
8596 }
8597 }
8598
8599 if (focusDisplayed) {
8600 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8601 }
8602 if (animating) {
8603 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8604 }
8605 mQueue.setHoldScreenLocked(holdScreen != null);
8606 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8607 mPowerManager.setScreenBrightnessOverride(-1);
8608 } else {
8609 mPowerManager.setScreenBrightnessOverride((int)
8610 (screenBrightness * Power.BRIGHTNESS_ON));
8611 }
8612 if (holdScreen != mHoldingScreenOn) {
8613 mHoldingScreenOn = holdScreen;
8614 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8615 mH.sendMessage(m);
8616 }
8617 }
8618
8619 void requestAnimationLocked(long delay) {
8620 if (!mAnimationPending) {
8621 mAnimationPending = true;
8622 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8623 }
8624 }
Romain Guy06882f82009-06-10 13:36:04 -07008625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008626 /**
8627 * Have the surface flinger show a surface, robustly dealing with
8628 * error conditions. In particular, if there is not enough memory
8629 * to show the surface, then we will try to get rid of other surfaces
8630 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008632 * @return Returns true if the surface was successfully shown.
8633 */
8634 boolean showSurfaceRobustlyLocked(WindowState win) {
8635 try {
8636 if (win.mSurface != null) {
8637 win.mSurface.show();
8638 }
8639 return true;
8640 } catch (RuntimeException e) {
8641 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8642 }
Romain Guy06882f82009-06-10 13:36:04 -07008643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008644 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008646 return false;
8647 }
Romain Guy06882f82009-06-10 13:36:04 -07008648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008649 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8650 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8653 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 if (mForceRemoves == null) {
8656 mForceRemoves = new ArrayList<WindowState>();
8657 }
Romain Guy06882f82009-06-10 13:36:04 -07008658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 long callingIdentity = Binder.clearCallingIdentity();
8660 try {
8661 // There was some problem... first, do a sanity check of the
8662 // window list to make sure we haven't left any dangling surfaces
8663 // around.
8664 int N = mWindows.size();
8665 boolean leakedSurface = false;
8666 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8667 for (int i=0; i<N; i++) {
8668 WindowState ws = (WindowState)mWindows.get(i);
8669 if (ws.mSurface != null) {
8670 if (!mSessions.contains(ws.mSession)) {
8671 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8672 + ws + " surface=" + ws.mSurface
8673 + " token=" + win.mToken
8674 + " pid=" + ws.mSession.mPid
8675 + " uid=" + ws.mSession.mUid);
8676 ws.mSurface.clear();
8677 ws.mSurface = null;
8678 mForceRemoves.add(ws);
8679 i--;
8680 N--;
8681 leakedSurface = true;
8682 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8683 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8684 + ws + " surface=" + ws.mSurface
8685 + " token=" + win.mAppToken);
8686 ws.mSurface.clear();
8687 ws.mSurface = null;
8688 leakedSurface = true;
8689 }
8690 }
8691 }
Romain Guy06882f82009-06-10 13:36:04 -07008692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 boolean killedApps = false;
8694 if (!leakedSurface) {
8695 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8696 SparseIntArray pidCandidates = new SparseIntArray();
8697 for (int i=0; i<N; i++) {
8698 WindowState ws = (WindowState)mWindows.get(i);
8699 if (ws.mSurface != null) {
8700 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8701 }
8702 }
8703 if (pidCandidates.size() > 0) {
8704 int[] pids = new int[pidCandidates.size()];
8705 for (int i=0; i<pids.length; i++) {
8706 pids[i] = pidCandidates.keyAt(i);
8707 }
8708 try {
8709 if (mActivityManager.killPidsForMemory(pids)) {
8710 killedApps = true;
8711 }
8712 } catch (RemoteException e) {
8713 }
8714 }
8715 }
Romain Guy06882f82009-06-10 13:36:04 -07008716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 if (leakedSurface || killedApps) {
8718 // We managed to reclaim some memory, so get rid of the trouble
8719 // surface and ask the app to request another one.
8720 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8721 if (surface != null) {
8722 surface.clear();
8723 win.mSurface = null;
8724 }
Romain Guy06882f82009-06-10 13:36:04 -07008725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008726 try {
8727 win.mClient.dispatchGetNewSurface();
8728 } catch (RemoteException e) {
8729 }
8730 }
8731 } finally {
8732 Binder.restoreCallingIdentity(callingIdentity);
8733 }
8734 }
Romain Guy06882f82009-06-10 13:36:04 -07008735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008736 private boolean updateFocusedWindowLocked(int mode) {
8737 WindowState newFocus = computeFocusedWindowLocked();
8738 if (mCurrentFocus != newFocus) {
8739 // This check makes sure that we don't already have the focus
8740 // change message pending.
8741 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8742 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8743 if (localLOGV) Log.v(
8744 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8745 final WindowState oldFocus = mCurrentFocus;
8746 mCurrentFocus = newFocus;
8747 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 final WindowState imWindow = mInputMethodWindow;
8750 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008751 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008753 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8754 mLayoutNeeded = true;
8755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8757 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008758 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8759 // Client will do the layout, but we need to assign layers
8760 // for handleNewWindowLocked() below.
8761 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 }
8763 }
Romain Guy06882f82009-06-10 13:36:04 -07008764
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008765 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8766 mKeyWaiter.handleNewWindowLocked(newFocus);
8767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 return true;
8769 }
8770 return false;
8771 }
8772
8773 private WindowState computeFocusedWindowLocked() {
8774 WindowState result = null;
8775 WindowState win;
8776
8777 int i = mWindows.size() - 1;
8778 int nextAppIndex = mAppTokens.size()-1;
8779 WindowToken nextApp = nextAppIndex >= 0
8780 ? mAppTokens.get(nextAppIndex) : null;
8781
8782 while (i >= 0) {
8783 win = (WindowState)mWindows.get(i);
8784
8785 if (localLOGV || DEBUG_FOCUS) Log.v(
8786 TAG, "Looking for focus: " + i
8787 + " = " + win
8788 + ", flags=" + win.mAttrs.flags
8789 + ", canReceive=" + win.canReceiveKeys());
8790
8791 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 // If this window's application has been removed, just skip it.
8794 if (thisApp != null && thisApp.removed) {
8795 i--;
8796 continue;
8797 }
Romain Guy06882f82009-06-10 13:36:04 -07008798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008799 // If there is a focused app, don't allow focus to go to any
8800 // windows below it. If this is an application window, step
8801 // through the app tokens until we find its app.
8802 if (thisApp != null && nextApp != null && thisApp != nextApp
8803 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8804 int origAppIndex = nextAppIndex;
8805 while (nextAppIndex > 0) {
8806 if (nextApp == mFocusedApp) {
8807 // Whoops, we are below the focused app... no focus
8808 // for you!
8809 if (localLOGV || DEBUG_FOCUS) Log.v(
8810 TAG, "Reached focused app: " + mFocusedApp);
8811 return null;
8812 }
8813 nextAppIndex--;
8814 nextApp = mAppTokens.get(nextAppIndex);
8815 if (nextApp == thisApp) {
8816 break;
8817 }
8818 }
8819 if (thisApp != nextApp) {
8820 // Uh oh, the app token doesn't exist! This shouldn't
8821 // happen, but if it does we can get totally hosed...
8822 // so restart at the original app.
8823 nextAppIndex = origAppIndex;
8824 nextApp = mAppTokens.get(nextAppIndex);
8825 }
8826 }
8827
8828 // Dispatch to this window if it is wants key events.
8829 if (win.canReceiveKeys()) {
8830 if (DEBUG_FOCUS) Log.v(
8831 TAG, "Found focus @ " + i + " = " + win);
8832 result = win;
8833 break;
8834 }
8835
8836 i--;
8837 }
8838
8839 return result;
8840 }
8841
8842 private void startFreezingDisplayLocked() {
8843 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008844 // Freezing the display also suspends key event delivery, to
8845 // keep events from going astray while the display is reconfigured.
8846 // If someone has changed orientation again while the screen is
8847 // still frozen, the events will continue to be blocked while the
8848 // successive orientation change is processed. To prevent spurious
8849 // ANRs, we reset the event dispatch timeout in this case.
8850 synchronized (mKeyWaiter) {
8851 mKeyWaiter.mWasFrozen = true;
8852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 return;
8854 }
Romain Guy06882f82009-06-10 13:36:04 -07008855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008856 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008858 long now = SystemClock.uptimeMillis();
8859 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8860 if (mFreezeGcPending != 0) {
8861 if (now > (mFreezeGcPending+1000)) {
8862 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8863 mH.removeMessages(H.FORCE_GC);
8864 Runtime.getRuntime().gc();
8865 mFreezeGcPending = now;
8866 }
8867 } else {
8868 mFreezeGcPending = now;
8869 }
Romain Guy06882f82009-06-10 13:36:04 -07008870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008871 mDisplayFrozen = true;
8872 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8873 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8874 mAppTransitionReady = true;
8875 }
Romain Guy06882f82009-06-10 13:36:04 -07008876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008877 if (PROFILE_ORIENTATION) {
8878 File file = new File("/data/system/frozen");
8879 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8880 }
8881 Surface.freezeDisplay(0);
8882 }
Romain Guy06882f82009-06-10 13:36:04 -07008883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 private void stopFreezingDisplayLocked() {
8885 if (!mDisplayFrozen) {
8886 return;
8887 }
Romain Guy06882f82009-06-10 13:36:04 -07008888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 mDisplayFrozen = false;
8890 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8891 if (PROFILE_ORIENTATION) {
8892 Debug.stopMethodTracing();
8893 }
8894 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008895
Chris Tate2ad63a92009-03-25 17:36:48 -07008896 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8897 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 synchronized (mKeyWaiter) {
8899 mKeyWaiter.mWasFrozen = true;
8900 mKeyWaiter.notifyAll();
8901 }
8902
8903 // A little kludge: a lot could have happened while the
8904 // display was frozen, so now that we are coming back we
8905 // do a gc so that any remote references the system
8906 // processes holds on others can be released if they are
8907 // no longer needed.
8908 mH.removeMessages(H.FORCE_GC);
8909 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8910 2000);
Romain Guy06882f82009-06-10 13:36:04 -07008911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008912 mScreenFrozenLock.release();
8913 }
Romain Guy06882f82009-06-10 13:36:04 -07008914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008915 @Override
8916 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
8917 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
8918 != PackageManager.PERMISSION_GRANTED) {
8919 pw.println("Permission Denial: can't dump WindowManager from from pid="
8920 + Binder.getCallingPid()
8921 + ", uid=" + Binder.getCallingUid());
8922 return;
8923 }
Romain Guy06882f82009-06-10 13:36:04 -07008924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 synchronized(mWindowMap) {
8926 pw.println("Current Window Manager state:");
8927 for (int i=mWindows.size()-1; i>=0; i--) {
8928 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008929 pw.print(" Window #"); pw.print(i); pw.print(' ');
8930 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008931 w.dump(pw, " ");
8932 }
8933 if (mInputMethodDialogs.size() > 0) {
8934 pw.println(" ");
8935 pw.println(" Input method dialogs:");
8936 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
8937 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008938 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939 }
8940 }
8941 if (mPendingRemove.size() > 0) {
8942 pw.println(" ");
8943 pw.println(" Remove pending for:");
8944 for (int i=mPendingRemove.size()-1; i>=0; i--) {
8945 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008946 pw.print(" Remove #"); pw.print(i); pw.print(' ');
8947 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 w.dump(pw, " ");
8949 }
8950 }
8951 if (mForceRemoves != null && mForceRemoves.size() > 0) {
8952 pw.println(" ");
8953 pw.println(" Windows force removing:");
8954 for (int i=mForceRemoves.size()-1; i>=0; i--) {
8955 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008956 pw.print(" Removing #"); pw.print(i); pw.print(' ');
8957 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008958 w.dump(pw, " ");
8959 }
8960 }
8961 if (mDestroySurface.size() > 0) {
8962 pw.println(" ");
8963 pw.println(" Windows waiting to destroy their surface:");
8964 for (int i=mDestroySurface.size()-1; i>=0; i--) {
8965 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008966 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
8967 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 w.dump(pw, " ");
8969 }
8970 }
8971 if (mLosingFocus.size() > 0) {
8972 pw.println(" ");
8973 pw.println(" Windows losing focus:");
8974 for (int i=mLosingFocus.size()-1; i>=0; i--) {
8975 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008976 pw.print(" Losing #"); pw.print(i); pw.print(' ');
8977 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008978 w.dump(pw, " ");
8979 }
8980 }
8981 if (mSessions.size() > 0) {
8982 pw.println(" ");
8983 pw.println(" All active sessions:");
8984 Iterator<Session> it = mSessions.iterator();
8985 while (it.hasNext()) {
8986 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008987 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008988 s.dump(pw, " ");
8989 }
8990 }
8991 if (mTokenMap.size() > 0) {
8992 pw.println(" ");
8993 pw.println(" All tokens:");
8994 Iterator<WindowToken> it = mTokenMap.values().iterator();
8995 while (it.hasNext()) {
8996 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008997 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998 token.dump(pw, " ");
8999 }
9000 }
9001 if (mTokenList.size() > 0) {
9002 pw.println(" ");
9003 pw.println(" Window token list:");
9004 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009005 pw.print(" #"); pw.print(i); pw.print(": ");
9006 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007 }
9008 }
9009 if (mAppTokens.size() > 0) {
9010 pw.println(" ");
9011 pw.println(" Application tokens in Z order:");
9012 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009013 pw.print(" App #"); pw.print(i); pw.print(": ");
9014 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015 }
9016 }
9017 if (mFinishedStarting.size() > 0) {
9018 pw.println(" ");
9019 pw.println(" Finishing start of application tokens:");
9020 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9021 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009022 pw.print(" Finished Starting #"); pw.print(i);
9023 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009024 token.dump(pw, " ");
9025 }
9026 }
9027 if (mExitingTokens.size() > 0) {
9028 pw.println(" ");
9029 pw.println(" Exiting tokens:");
9030 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9031 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009032 pw.print(" Exiting #"); pw.print(i);
9033 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 token.dump(pw, " ");
9035 }
9036 }
9037 if (mExitingAppTokens.size() > 0) {
9038 pw.println(" ");
9039 pw.println(" Exiting application tokens:");
9040 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9041 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009042 pw.print(" Exiting App #"); pw.print(i);
9043 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009044 token.dump(pw, " ");
9045 }
9046 }
9047 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009048 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9049 pw.print(" mLastFocus="); pw.println(mLastFocus);
9050 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9051 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9052 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9053 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9054 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9055 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9056 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9057 pw.print(" mBlurShown="); pw.println(mBlurShown);
9058 pw.print(" mDimShown="); pw.print(mDimShown);
9059 pw.print(" current="); pw.print(mDimCurrentAlpha);
9060 pw.print(" target="); pw.print(mDimTargetAlpha);
9061 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9062 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9063 pw.print(" mInputMethodAnimLayerAdjustment=");
9064 pw.println(mInputMethodAnimLayerAdjustment);
9065 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9066 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9067 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9068 pw.print(" mRotation="); pw.print(mRotation);
9069 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9070 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9071 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9072 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9073 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9074 pw.print(" mNextAppTransition=0x");
9075 pw.print(Integer.toHexString(mNextAppTransition));
9076 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9077 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9078 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9079 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9080 if (mOpeningApps.size() > 0) {
9081 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9082 }
9083 if (mClosingApps.size() > 0) {
9084 pw.print(" mClosingApps="); pw.println(mClosingApps);
9085 }
9086 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9087 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009089 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9090 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9091 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9092 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9093 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9094 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 }
9096 }
9097
9098 public void monitor() {
9099 synchronized (mWindowMap) { }
9100 synchronized (mKeyguardDisabled) { }
9101 synchronized (mKeyWaiter) { }
9102 }
9103}