blob: cfa625c594486c718c57cbba06b6b79f32f64749 [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()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003410 IRotationWatcher removed = mRotationWatchers.remove(i);
3411 if (removed != null) {
3412 removed.asBinder().unlinkToDeath(this, 0);
3413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 i--;
3415 }
3416 }
3417 }
3418 }
3419 };
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 synchronized (mWindowMap) {
3422 try {
3423 watcher.asBinder().linkToDeath(dr, 0);
3424 mRotationWatchers.add(watcher);
3425 } catch (RemoteException e) {
3426 // Client died, no cleanup needed.
3427 }
Romain Guy06882f82009-06-10 13:36:04 -07003428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 return mRotation;
3430 }
3431 }
3432
3433 /**
3434 * Starts the view server on the specified port.
3435 *
3436 * @param port The port to listener to.
3437 *
3438 * @return True if the server was successfully started, false otherwise.
3439 *
3440 * @see com.android.server.ViewServer
3441 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3442 */
3443 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003444 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 return false;
3446 }
3447
3448 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3449 return false;
3450 }
3451
3452 if (port < 1024) {
3453 return false;
3454 }
3455
3456 if (mViewServer != null) {
3457 if (!mViewServer.isRunning()) {
3458 try {
3459 return mViewServer.start();
3460 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003461 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 }
3463 }
3464 return false;
3465 }
3466
3467 try {
3468 mViewServer = new ViewServer(this, port);
3469 return mViewServer.start();
3470 } catch (IOException e) {
3471 Log.w(TAG, "View server did not start");
3472 }
3473 return false;
3474 }
3475
Romain Guy06882f82009-06-10 13:36:04 -07003476 private boolean isSystemSecure() {
3477 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3478 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3479 }
3480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 /**
3482 * Stops the view server if it exists.
3483 *
3484 * @return True if the server stopped, false if it wasn't started or
3485 * couldn't be stopped.
3486 *
3487 * @see com.android.server.ViewServer
3488 */
3489 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003490 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 return false;
3492 }
3493
3494 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3495 return false;
3496 }
3497
3498 if (mViewServer != null) {
3499 return mViewServer.stop();
3500 }
3501 return false;
3502 }
3503
3504 /**
3505 * Indicates whether the view server is running.
3506 *
3507 * @return True if the server is running, false otherwise.
3508 *
3509 * @see com.android.server.ViewServer
3510 */
3511 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003512 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 return false;
3514 }
3515
3516 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3517 return false;
3518 }
3519
3520 return mViewServer != null && mViewServer.isRunning();
3521 }
3522
3523 /**
3524 * Lists all availble windows in the system. The listing is written in the
3525 * specified Socket's output stream with the following syntax:
3526 * windowHashCodeInHexadecimal windowName
3527 * Each line of the ouput represents a different window.
3528 *
3529 * @param client The remote client to send the listing to.
3530 * @return False if an error occured, true otherwise.
3531 */
3532 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003533 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 return false;
3535 }
3536
3537 boolean result = true;
3538
3539 Object[] windows;
3540 synchronized (mWindowMap) {
3541 windows = new Object[mWindows.size()];
3542 //noinspection unchecked
3543 windows = mWindows.toArray(windows);
3544 }
3545
3546 BufferedWriter out = null;
3547
3548 // Any uncaught exception will crash the system process
3549 try {
3550 OutputStream clientStream = client.getOutputStream();
3551 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3552
3553 final int count = windows.length;
3554 for (int i = 0; i < count; i++) {
3555 final WindowState w = (WindowState) windows[i];
3556 out.write(Integer.toHexString(System.identityHashCode(w)));
3557 out.write(' ');
3558 out.append(w.mAttrs.getTitle());
3559 out.write('\n');
3560 }
3561
3562 out.write("DONE.\n");
3563 out.flush();
3564 } catch (Exception e) {
3565 result = false;
3566 } finally {
3567 if (out != null) {
3568 try {
3569 out.close();
3570 } catch (IOException e) {
3571 result = false;
3572 }
3573 }
3574 }
3575
3576 return result;
3577 }
3578
3579 /**
3580 * Sends a command to a target window. The result of the command, if any, will be
3581 * written in the output stream of the specified socket.
3582 *
3583 * The parameters must follow this syntax:
3584 * windowHashcode extra
3585 *
3586 * Where XX is the length in characeters of the windowTitle.
3587 *
3588 * The first parameter is the target window. The window with the specified hashcode
3589 * will be the target. If no target can be found, nothing happens. The extra parameters
3590 * will be delivered to the target window and as parameters to the command itself.
3591 *
3592 * @param client The remote client to sent the result, if any, to.
3593 * @param command The command to execute.
3594 * @param parameters The command parameters.
3595 *
3596 * @return True if the command was successfully delivered, false otherwise. This does
3597 * not indicate whether the command itself was successful.
3598 */
3599 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003600 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 return false;
3602 }
3603
3604 boolean success = true;
3605 Parcel data = null;
3606 Parcel reply = null;
3607
3608 // Any uncaught exception will crash the system process
3609 try {
3610 // Find the hashcode of the window
3611 int index = parameters.indexOf(' ');
3612 if (index == -1) {
3613 index = parameters.length();
3614 }
3615 final String code = parameters.substring(0, index);
3616 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3617
3618 // Extract the command's parameter after the window description
3619 if (index < parameters.length()) {
3620 parameters = parameters.substring(index + 1);
3621 } else {
3622 parameters = "";
3623 }
3624
3625 final WindowManagerService.WindowState window = findWindow(hashCode);
3626 if (window == null) {
3627 return false;
3628 }
3629
3630 data = Parcel.obtain();
3631 data.writeInterfaceToken("android.view.IWindow");
3632 data.writeString(command);
3633 data.writeString(parameters);
3634 data.writeInt(1);
3635 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3636
3637 reply = Parcel.obtain();
3638
3639 final IBinder binder = window.mClient.asBinder();
3640 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3641 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3642
3643 reply.readException();
3644
3645 } catch (Exception e) {
3646 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3647 success = false;
3648 } finally {
3649 if (data != null) {
3650 data.recycle();
3651 }
3652 if (reply != null) {
3653 reply.recycle();
3654 }
3655 }
3656
3657 return success;
3658 }
3659
3660 private WindowState findWindow(int hashCode) {
3661 if (hashCode == -1) {
3662 return getFocusedWindow();
3663 }
3664
3665 synchronized (mWindowMap) {
3666 final ArrayList windows = mWindows;
3667 final int count = windows.size();
3668
3669 for (int i = 0; i < count; i++) {
3670 WindowState w = (WindowState) windows.get(i);
3671 if (System.identityHashCode(w) == hashCode) {
3672 return w;
3673 }
3674 }
3675 }
3676
3677 return null;
3678 }
3679
3680 /*
3681 * Instruct the Activity Manager to fetch the current configuration and broadcast
3682 * that to config-changed listeners if appropriate.
3683 */
3684 void sendNewConfiguration() {
3685 try {
3686 mActivityManager.updateConfiguration(null);
3687 } catch (RemoteException e) {
3688 }
3689 }
Romain Guy06882f82009-06-10 13:36:04 -07003690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 public Configuration computeNewConfiguration() {
3692 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003693 return computeNewConfigurationLocked();
3694 }
3695 }
Romain Guy06882f82009-06-10 13:36:04 -07003696
Dianne Hackbornc485a602009-03-24 22:39:49 -07003697 Configuration computeNewConfigurationLocked() {
3698 Configuration config = new Configuration();
3699 if (!computeNewConfigurationLocked(config)) {
3700 return null;
3701 }
3702 Log.i(TAG, "Config changed: " + config);
3703 long now = SystemClock.uptimeMillis();
3704 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3705 if (mFreezeGcPending != 0) {
3706 if (now > (mFreezeGcPending+1000)) {
3707 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3708 mH.removeMessages(H.FORCE_GC);
3709 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 mFreezeGcPending = now;
3711 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003712 } else {
3713 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003715 return config;
3716 }
Romain Guy06882f82009-06-10 13:36:04 -07003717
Dianne Hackbornc485a602009-03-24 22:39:49 -07003718 boolean computeNewConfigurationLocked(Configuration config) {
3719 if (mDisplay == null) {
3720 return false;
3721 }
3722 mQueue.getInputConfiguration(config);
3723 final int dw = mDisplay.getWidth();
3724 final int dh = mDisplay.getHeight();
3725 int orientation = Configuration.ORIENTATION_SQUARE;
3726 if (dw < dh) {
3727 orientation = Configuration.ORIENTATION_PORTRAIT;
3728 } else if (dw > dh) {
3729 orientation = Configuration.ORIENTATION_LANDSCAPE;
3730 }
3731 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003732
3733 if (screenLayout == Configuration.SCREENLAYOUT_UNDEFINED) {
3734 // Note we only do this once because at this point we don't
3735 // expect the screen to change in this way at runtime, and want
3736 // to avoid all of this computation for every config change.
3737 DisplayMetrics dm = new DisplayMetrics();
3738 mDisplay.getMetrics(dm);
3739 int longSize = dw;
3740 int shortSize = dh;
3741 if (longSize < shortSize) {
3742 int tmp = longSize;
3743 longSize = shortSize;
3744 shortSize = tmp;
3745 }
3746 longSize = (int)(longSize/dm.density);
3747 shortSize = (int)(shortSize/dm.density);
3748
3749 // These semi-magic numbers define our compatibility modes for
3750 // applications with different screens. Don't change unless you
3751 // make sure to test lots and lots of apps!
3752 if (longSize < 470) {
3753 // This is shorter than an HVGA normal density screen (which
3754 // is 480 pixels on its long side).
3755 screenLayout = Configuration.SCREENLAYOUT_SMALL;
3756 } else if (longSize > 490 && shortSize > 330) {
3757 // This is larger than an HVGA normal density screen (which
3758 // is 480x320 pixels).
3759 screenLayout = Configuration.SCREENLAYOUT_LARGE;
3760 } else {
3761 screenLayout = Configuration.SCREENLAYOUT_NORMAL;
3762 }
3763 }
3764 config.screenLayout = screenLayout;
3765
Dianne Hackbornc485a602009-03-24 22:39:49 -07003766 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3767 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3768 mPolicy.adjustConfigurationLw(config);
3769 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
Romain Guy06882f82009-06-10 13:36:04 -07003771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 // -------------------------------------------------------------
3773 // Input Events and Focus Management
3774 // -------------------------------------------------------------
3775
3776 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003777 long curTime = SystemClock.uptimeMillis();
3778
3779 if (eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
3780 if (mLastTouchEventType == eventType &&
3781 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3782 return;
3783 }
3784 mLastUserActivityCallTime = curTime;
3785 mLastTouchEventType = eventType;
3786 }
3787
3788 if (targetWin == null
3789 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3790 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792 }
3793
3794 // tells if it's a cheek event or not -- this function is stateful
3795 private static final int EVENT_NONE = 0;
3796 private static final int EVENT_UNKNOWN = 0;
3797 private static final int EVENT_CHEEK = 0;
3798 private static final int EVENT_IGNORE_DURATION = 300; // ms
3799 private static final float CHEEK_THRESHOLD = 0.6f;
3800 private int mEventState = EVENT_NONE;
3801 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 private int eventType(MotionEvent ev) {
3804 float size = ev.getSize();
3805 switch (ev.getAction()) {
3806 case MotionEvent.ACTION_DOWN:
3807 mEventSize = size;
3808 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3809 case MotionEvent.ACTION_UP:
3810 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003811 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 case MotionEvent.ACTION_MOVE:
3813 final int N = ev.getHistorySize();
3814 if (size > mEventSize) mEventSize = size;
3815 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3816 for (int i=0; i<N; i++) {
3817 size = ev.getHistoricalSize(i);
3818 if (size > mEventSize) mEventSize = size;
3819 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3820 }
3821 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3822 return TOUCH_EVENT;
3823 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003824 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 }
3826 default:
3827 // not good
3828 return OTHER_EVENT;
3829 }
3830 }
3831
3832 /**
3833 * @return Returns true if event was dispatched, false if it was dropped for any reason
3834 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003835 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3837 "dispatchPointer " + ev);
3838
3839 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003840 ev, true, false, pid, uid);
Romain Guy06882f82009-06-10 13:36:04 -07003841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 if (action == MotionEvent.ACTION_UP) {
3845 // let go of our target
3846 mKeyWaiter.mMotionTarget = null;
3847 mPowerManager.logPointerUpEvent();
3848 } else if (action == MotionEvent.ACTION_DOWN) {
3849 mPowerManager.logPointerDownEvent();
3850 }
3851
3852 if (targetObj == null) {
3853 // In this case we are either dropping the event, or have received
3854 // a move or up without a down. It is common to receive move
3855 // events in such a way, since this means the user is moving the
3856 // pointer without actually pressing down. All other cases should
3857 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003858 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3860 }
3861 if (qev != null) {
3862 mQueue.recycleEvent(qev);
3863 }
3864 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003865 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 }
3867 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3868 if (qev != null) {
3869 mQueue.recycleEvent(qev);
3870 }
3871 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003872 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 }
Romain Guy06882f82009-06-10 13:36:04 -07003874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07003878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 //Log.i(TAG, "Sending " + ev + " to " + target);
3880
3881 if (uid != 0 && uid != target.mSession.mUid) {
3882 if (mContext.checkPermission(
3883 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3884 != PackageManager.PERMISSION_GRANTED) {
3885 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3886 + pid + " uid " + uid + " to window " + target
3887 + " owned by uid " + target.mSession.mUid);
3888 if (qev != null) {
3889 mQueue.recycleEvent(qev);
3890 }
3891 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003892 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 }
3894 }
Romain Guy06882f82009-06-10 13:36:04 -07003895
3896 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3898 //target wants to ignore fat touch events
3899 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3900 //explicit flag to return without processing event further
3901 boolean returnFlag = false;
3902 if((action == MotionEvent.ACTION_DOWN)) {
3903 mFatTouch = false;
3904 if(cheekPress) {
3905 mFatTouch = true;
3906 returnFlag = true;
3907 }
3908 } else {
3909 if(action == MotionEvent.ACTION_UP) {
3910 if(mFatTouch) {
3911 //earlier even was invalid doesnt matter if current up is cheekpress or not
3912 mFatTouch = false;
3913 returnFlag = true;
3914 } else if(cheekPress) {
3915 //cancel the earlier event
3916 ev.setAction(MotionEvent.ACTION_CANCEL);
3917 action = MotionEvent.ACTION_CANCEL;
3918 }
3919 } else if(action == MotionEvent.ACTION_MOVE) {
3920 if(mFatTouch) {
3921 //two cases here
3922 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003923 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 returnFlag = true;
3925 } else if(cheekPress) {
3926 //valid down followed by invalid moves
3927 //an invalid move have to cancel earlier action
3928 ev.setAction(MotionEvent.ACTION_CANCEL);
3929 action = MotionEvent.ACTION_CANCEL;
3930 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3931 //note that the subsequent invalid moves will not get here
3932 mFatTouch = true;
3933 }
3934 }
3935 } //else if action
3936 if(returnFlag) {
3937 //recycle que, ev
3938 if (qev != null) {
3939 mQueue.recycleEvent(qev);
3940 }
3941 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003942 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 }
3944 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07003945
3946 // TODO remove once we settle on a value or make it app specific
3947 if (action == MotionEvent.ACTION_DOWN) {
3948 int max_events_per_sec = 35;
3949 try {
3950 max_events_per_sec = Integer.parseInt(SystemProperties
3951 .get("windowsmgr.max_events_per_sec"));
3952 if (max_events_per_sec < 1) {
3953 max_events_per_sec = 35;
3954 }
3955 } catch (NumberFormatException e) {
3956 }
3957 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
3958 }
3959
3960 /*
3961 * Throttle events to minimize CPU usage when there's a flood of events
3962 * e.g. constant contact with the screen
3963 */
3964 if (action == MotionEvent.ACTION_MOVE) {
3965 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
3966 long now = SystemClock.uptimeMillis();
3967 if (now < nextEventTime) {
3968 try {
3969 Thread.sleep(nextEventTime - now);
3970 } catch (InterruptedException e) {
3971 }
3972 mLastTouchEventTime = nextEventTime;
3973 } else {
3974 mLastTouchEventTime = now;
3975 }
3976 }
3977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 synchronized(mWindowMap) {
3979 if (qev != null && action == MotionEvent.ACTION_MOVE) {
3980 mKeyWaiter.bindTargetWindowLocked(target,
3981 KeyWaiter.RETURN_PENDING_POINTER, qev);
3982 ev = null;
3983 } else {
3984 if (action == MotionEvent.ACTION_DOWN) {
3985 WindowState out = mKeyWaiter.mOutsideTouchTargets;
3986 if (out != null) {
3987 MotionEvent oev = MotionEvent.obtain(ev);
3988 oev.setAction(MotionEvent.ACTION_OUTSIDE);
3989 do {
3990 final Rect frame = out.mFrame;
3991 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
3992 try {
3993 out.mClient.dispatchPointer(oev, eventTime);
3994 } catch (android.os.RemoteException e) {
3995 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
3996 }
3997 oev.offsetLocation((float)frame.left, (float)frame.top);
3998 out = out.mNextOutsideTouch;
3999 } while (out != null);
4000 mKeyWaiter.mOutsideTouchTargets = null;
4001 }
4002 }
4003 final Rect frame = target.mFrame;
4004 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4005 mKeyWaiter.bindTargetWindowLocked(target);
4006 }
4007 }
Romain Guy06882f82009-06-10 13:36:04 -07004008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 // finally offset the event to the target's coordinate system and
4010 // dispatch the event.
4011 try {
4012 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4013 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4014 }
4015 target.mClient.dispatchPointer(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004016 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 } catch (android.os.RemoteException e) {
4018 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4019 mKeyWaiter.mMotionTarget = null;
4020 try {
4021 removeWindow(target.mSession, target.mClient);
4022 } catch (java.util.NoSuchElementException ex) {
4023 // This will happen if the window has already been
4024 // removed.
4025 }
4026 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004027 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 }
Romain Guy06882f82009-06-10 13:36:04 -07004029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 /**
4031 * @return Returns true if event was dispatched, false if it was dropped for any reason
4032 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004033 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 if (DEBUG_INPUT) Log.v(
4035 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004038 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 if (focusObj == null) {
4040 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4041 if (qev != null) {
4042 mQueue.recycleEvent(qev);
4043 }
4044 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004045 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 }
4047 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4048 if (qev != null) {
4049 mQueue.recycleEvent(qev);
4050 }
4051 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004052 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 }
Romain Guy06882f82009-06-10 13:36:04 -07004054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 if (uid != 0 && uid != focus.mSession.mUid) {
4058 if (mContext.checkPermission(
4059 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4060 != PackageManager.PERMISSION_GRANTED) {
4061 Log.w(TAG, "Permission denied: injecting key event from pid "
4062 + pid + " uid " + uid + " to window " + focus
4063 + " owned by uid " + focus.mSession.mUid);
4064 if (qev != null) {
4065 mQueue.recycleEvent(qev);
4066 }
4067 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004068 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 }
4070 }
Romain Guy06882f82009-06-10 13:36:04 -07004071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 synchronized(mWindowMap) {
4075 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4076 mKeyWaiter.bindTargetWindowLocked(focus,
4077 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4078 // We don't deliver movement events to the client, we hold
4079 // them and wait for them to call back.
4080 ev = null;
4081 } else {
4082 mKeyWaiter.bindTargetWindowLocked(focus);
4083 }
4084 }
Romain Guy06882f82009-06-10 13:36:04 -07004085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 try {
4087 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004088 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 } catch (android.os.RemoteException e) {
4090 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4091 try {
4092 removeWindow(focus.mSession, focus.mClient);
4093 } catch (java.util.NoSuchElementException ex) {
4094 // This will happen if the window has already been
4095 // removed.
4096 }
4097 }
Romain Guy06882f82009-06-10 13:36:04 -07004098
Dianne Hackborncfaef692009-06-15 14:24:44 -07004099 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 }
Romain Guy06882f82009-06-10 13:36:04 -07004101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 /**
4103 * @return Returns true if event was dispatched, false if it was dropped for any reason
4104 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004105 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4107
4108 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004109 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 if (focusObj == null) {
4111 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004112 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 }
4114 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004115 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 }
Romain Guy06882f82009-06-10 13:36:04 -07004117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 if (DEBUG_INPUT) Log.v(
4121 TAG, "Dispatching to " + focus + ": " + event);
4122
4123 if (uid != 0 && uid != focus.mSession.mUid) {
4124 if (mContext.checkPermission(
4125 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4126 != PackageManager.PERMISSION_GRANTED) {
4127 Log.w(TAG, "Permission denied: injecting key event from pid "
4128 + pid + " uid " + uid + " to window " + focus
4129 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004130 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 }
4132 }
Romain Guy06882f82009-06-10 13:36:04 -07004133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 synchronized(mWindowMap) {
4135 mKeyWaiter.bindTargetWindowLocked(focus);
4136 }
4137
4138 // NOSHIP extra state logging
4139 mKeyWaiter.recordDispatchState(event, focus);
4140 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 try {
4143 if (DEBUG_INPUT || DEBUG_FOCUS) {
4144 Log.v(TAG, "Delivering key " + event.getKeyCode()
4145 + " to " + focus);
4146 }
4147 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004148 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 } catch (android.os.RemoteException e) {
4150 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4151 try {
4152 removeWindow(focus.mSession, focus.mClient);
4153 } catch (java.util.NoSuchElementException ex) {
4154 // This will happen if the window has already been
4155 // removed.
4156 }
4157 }
Romain Guy06882f82009-06-10 13:36:04 -07004158
Dianne Hackborncfaef692009-06-15 14:24:44 -07004159 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
Romain Guy06882f82009-06-10 13:36:04 -07004161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 public void pauseKeyDispatching(IBinder _token) {
4163 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4164 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004165 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 }
4167
4168 synchronized (mWindowMap) {
4169 WindowToken token = mTokenMap.get(_token);
4170 if (token != null) {
4171 mKeyWaiter.pauseDispatchingLocked(token);
4172 }
4173 }
4174 }
4175
4176 public void resumeKeyDispatching(IBinder _token) {
4177 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4178 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004179 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
4181
4182 synchronized (mWindowMap) {
4183 WindowToken token = mTokenMap.get(_token);
4184 if (token != null) {
4185 mKeyWaiter.resumeDispatchingLocked(token);
4186 }
4187 }
4188 }
4189
4190 public void setEventDispatching(boolean enabled) {
4191 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4192 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004193 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 }
4195
4196 synchronized (mWindowMap) {
4197 mKeyWaiter.setEventDispatchingLocked(enabled);
4198 }
4199 }
Romain Guy06882f82009-06-10 13:36:04 -07004200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 /**
4202 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004203 *
4204 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 * {@link SystemClock#uptimeMillis()} as the timebase.)
4206 * @param sync If true, wait for the event to be completed before returning to the caller.
4207 * @return Returns true if event was dispatched, false if it was dropped for any reason
4208 */
4209 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4210 long downTime = ev.getDownTime();
4211 long eventTime = ev.getEventTime();
4212
4213 int action = ev.getAction();
4214 int code = ev.getKeyCode();
4215 int repeatCount = ev.getRepeatCount();
4216 int metaState = ev.getMetaState();
4217 int deviceId = ev.getDeviceId();
4218 int scancode = ev.getScanCode();
4219
4220 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4221 if (downTime == 0) downTime = eventTime;
4222
4223 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004224 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004226 final int pid = Binder.getCallingPid();
4227 final int uid = Binder.getCallingUid();
4228 final long ident = Binder.clearCallingIdentity();
4229 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004231 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004233 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004234 switch (result) {
4235 case INJECT_NO_PERMISSION:
4236 throw new SecurityException(
4237 "Injecting to another application requires INJECT_EVENT permission");
4238 case INJECT_SUCCEEDED:
4239 return true;
4240 }
4241 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 }
4243
4244 /**
4245 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004246 *
4247 * @param ev A motion event describing the pointer (touch) action. (As noted in
4248 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 * {@link SystemClock#uptimeMillis()} as the timebase.)
4250 * @param sync If true, wait for the event to be completed before returning to the caller.
4251 * @return Returns true if event was dispatched, false if it was dropped for any reason
4252 */
4253 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004254 final int pid = Binder.getCallingPid();
4255 final int uid = Binder.getCallingUid();
4256 final long ident = Binder.clearCallingIdentity();
4257 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004259 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004261 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004262 switch (result) {
4263 case INJECT_NO_PERMISSION:
4264 throw new SecurityException(
4265 "Injecting to another application requires INJECT_EVENT permission");
4266 case INJECT_SUCCEEDED:
4267 return true;
4268 }
4269 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 }
Romain Guy06882f82009-06-10 13:36:04 -07004271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 /**
4273 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004274 *
4275 * @param ev A motion event describing the trackball action. (As noted in
4276 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 * {@link SystemClock#uptimeMillis()} as the timebase.)
4278 * @param sync If true, wait for the event to be completed before returning to the caller.
4279 * @return Returns true if event was dispatched, false if it was dropped for any reason
4280 */
4281 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004282 final int pid = Binder.getCallingPid();
4283 final int uid = Binder.getCallingUid();
4284 final long ident = Binder.clearCallingIdentity();
4285 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004287 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004289 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004290 switch (result) {
4291 case INJECT_NO_PERMISSION:
4292 throw new SecurityException(
4293 "Injecting to another application requires INJECT_EVENT permission");
4294 case INJECT_SUCCEEDED:
4295 return true;
4296 }
4297 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 }
Romain Guy06882f82009-06-10 13:36:04 -07004299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 private WindowState getFocusedWindow() {
4301 synchronized (mWindowMap) {
4302 return getFocusedWindowLocked();
4303 }
4304 }
4305
4306 private WindowState getFocusedWindowLocked() {
4307 return mCurrentFocus;
4308 }
Romain Guy06882f82009-06-10 13:36:04 -07004309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 /**
4311 * This class holds the state for dispatching key events. This state
4312 * is protected by the KeyWaiter instance, NOT by the window lock. You
4313 * can be holding the main window lock while acquire the KeyWaiter lock,
4314 * but not the other way around.
4315 */
4316 final class KeyWaiter {
4317 // NOSHIP debugging
4318 public class DispatchState {
4319 private KeyEvent event;
4320 private WindowState focus;
4321 private long time;
4322 private WindowState lastWin;
4323 private IBinder lastBinder;
4324 private boolean finished;
4325 private boolean gotFirstWindow;
4326 private boolean eventDispatching;
4327 private long timeToSwitch;
4328 private boolean wasFrozen;
4329 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004330 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4333 focus = theFocus;
4334 event = theEvent;
4335 time = System.currentTimeMillis();
4336 // snapshot KeyWaiter state
4337 lastWin = mLastWin;
4338 lastBinder = mLastBinder;
4339 finished = mFinished;
4340 gotFirstWindow = mGotFirstWindow;
4341 eventDispatching = mEventDispatching;
4342 timeToSwitch = mTimeToSwitch;
4343 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004344 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 // cache the paused state at ctor time as well
4346 if (theFocus == null || theFocus.mToken == null) {
4347 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4348 focusPaused = false;
4349 } else {
4350 focusPaused = theFocus.mToken.paused;
4351 }
4352 }
Romain Guy06882f82009-06-10 13:36:04 -07004353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 public String toString() {
4355 return "{{" + event + " to " + focus + " @ " + time
4356 + " lw=" + lastWin + " lb=" + lastBinder
4357 + " fin=" + finished + " gfw=" + gotFirstWindow
4358 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004359 + " wf=" + wasFrozen + " fp=" + focusPaused
4360 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 }
4362 };
4363 private DispatchState mDispatchState = null;
4364 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4365 mDispatchState = new DispatchState(theEvent, theFocus);
4366 }
4367 // END NOSHIP
4368
4369 public static final int RETURN_NOTHING = 0;
4370 public static final int RETURN_PENDING_POINTER = 1;
4371 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 final Object SKIP_TARGET_TOKEN = new Object();
4374 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 private WindowState mLastWin = null;
4377 private IBinder mLastBinder = null;
4378 private boolean mFinished = true;
4379 private boolean mGotFirstWindow = false;
4380 private boolean mEventDispatching = true;
4381 private long mTimeToSwitch = 0;
4382 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 // Target of Motion events
4385 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 // Windows above the target who would like to receive an "outside"
4388 // touch event for any down events outside of them.
4389 WindowState mOutsideTouchTargets;
4390
4391 /**
4392 * Wait for the last event dispatch to complete, then find the next
4393 * target that should receive the given event and wait for that one
4394 * to be ready to receive it.
4395 */
4396 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4397 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004398 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 long startTime = SystemClock.uptimeMillis();
4400 long keyDispatchingTimeout = 5 * 1000;
4401 long waitedFor = 0;
4402
4403 while (true) {
4404 // Figure out which window we care about. It is either the
4405 // last window we are waiting to have process the event or,
4406 // if none, then the next window we think the event should go
4407 // to. Note: we retrieve mLastWin outside of the lock, so
4408 // it may change before we lock. Thus we must check it again.
4409 WindowState targetWin = mLastWin;
4410 boolean targetIsNew = targetWin == null;
4411 if (DEBUG_INPUT) Log.v(
4412 TAG, "waitForLastKey: mFinished=" + mFinished +
4413 ", mLastWin=" + mLastWin);
4414 if (targetIsNew) {
4415 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004416 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 if (target == SKIP_TARGET_TOKEN) {
4418 // The user has pressed a special key, and we are
4419 // dropping all pending events before it.
4420 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4421 + " " + nextMotion);
4422 return null;
4423 }
4424 if (target == CONSUMED_EVENT_TOKEN) {
4425 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4426 + " " + nextMotion);
4427 return target;
4428 }
4429 targetWin = (WindowState)target;
4430 }
Romain Guy06882f82009-06-10 13:36:04 -07004431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 // Now: is it okay to send the next event to this window?
4435 synchronized (this) {
4436 // First: did we come here based on the last window not
4437 // being null, but it changed by the time we got here?
4438 // If so, try again.
4439 if (!targetIsNew && mLastWin == null) {
4440 continue;
4441 }
Romain Guy06882f82009-06-10 13:36:04 -07004442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 // We never dispatch events if not finished with the
4444 // last one, or the display is frozen.
4445 if (mFinished && !mDisplayFrozen) {
4446 // If event dispatching is disabled, then we
4447 // just consume the events.
4448 if (!mEventDispatching) {
4449 if (DEBUG_INPUT) Log.v(TAG,
4450 "Skipping event; dispatching disabled: "
4451 + nextKey + " " + nextMotion);
4452 return null;
4453 }
4454 if (targetWin != null) {
4455 // If this is a new target, and that target is not
4456 // paused or unresponsive, then all looks good to
4457 // handle the event.
4458 if (targetIsNew && !targetWin.mToken.paused) {
4459 return targetWin;
4460 }
Romain Guy06882f82009-06-10 13:36:04 -07004461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 // If we didn't find a target window, and there is no
4463 // focused app window, then just eat the events.
4464 } else if (mFocusedApp == null) {
4465 if (DEBUG_INPUT) Log.v(TAG,
4466 "Skipping event; no focused app: "
4467 + nextKey + " " + nextMotion);
4468 return null;
4469 }
4470 }
Romain Guy06882f82009-06-10 13:36:04 -07004471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 if (DEBUG_INPUT) Log.v(
4473 TAG, "Waiting for last key in " + mLastBinder
4474 + " target=" + targetWin
4475 + " mFinished=" + mFinished
4476 + " mDisplayFrozen=" + mDisplayFrozen
4477 + " targetIsNew=" + targetIsNew
4478 + " paused="
4479 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004480 + " mFocusedApp=" + mFocusedApp
4481 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 targetApp = targetWin != null
4484 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 long curTimeout = keyDispatchingTimeout;
4487 if (mTimeToSwitch != 0) {
4488 long now = SystemClock.uptimeMillis();
4489 if (mTimeToSwitch <= now) {
4490 // If an app switch key has been pressed, and we have
4491 // waited too long for the current app to finish
4492 // processing keys, then wait no more!
4493 doFinishedKeyLocked(true);
4494 continue;
4495 }
4496 long switchTimeout = mTimeToSwitch - now;
4497 if (curTimeout > switchTimeout) {
4498 curTimeout = switchTimeout;
4499 }
4500 }
Romain Guy06882f82009-06-10 13:36:04 -07004501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 try {
4503 // after that continue
4504 // processing keys, so we don't get stuck.
4505 if (DEBUG_INPUT) Log.v(
4506 TAG, "Waiting for key dispatch: " + curTimeout);
4507 wait(curTimeout);
4508 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4509 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004510 + startTime + " switchTime=" + mTimeToSwitch
4511 + " target=" + targetWin + " mLW=" + mLastWin
4512 + " mLB=" + mLastBinder + " fin=" + mFinished
4513 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 } catch (InterruptedException e) {
4515 }
4516 }
4517
4518 // If we were frozen during configuration change, restart the
4519 // timeout checks from now; otherwise look at whether we timed
4520 // out before awakening.
4521 if (mWasFrozen) {
4522 waitedFor = 0;
4523 mWasFrozen = false;
4524 } else {
4525 waitedFor = SystemClock.uptimeMillis() - startTime;
4526 }
4527
4528 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4529 IApplicationToken at = null;
4530 synchronized (this) {
4531 Log.w(TAG, "Key dispatching timed out sending to " +
4532 (targetWin != null ? targetWin.mAttrs.getTitle()
4533 : "<null>"));
4534 // NOSHIP debugging
4535 Log.w(TAG, "Dispatch state: " + mDispatchState);
4536 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4537 // END NOSHIP
4538 //dump();
4539 if (targetWin != null) {
4540 at = targetWin.getAppToken();
4541 } else if (targetApp != null) {
4542 at = targetApp.appToken;
4543 }
4544 }
4545
4546 boolean abort = true;
4547 if (at != null) {
4548 try {
4549 long timeout = at.getKeyDispatchingTimeout();
4550 if (timeout > waitedFor) {
4551 // we did not wait the proper amount of time for this application.
4552 // set the timeout to be the real timeout and wait again.
4553 keyDispatchingTimeout = timeout - waitedFor;
4554 continue;
4555 } else {
4556 abort = at.keyDispatchingTimedOut();
4557 }
4558 } catch (RemoteException ex) {
4559 }
4560 }
4561
4562 synchronized (this) {
4563 if (abort && (mLastWin == targetWin || targetWin == null)) {
4564 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004565 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 if (DEBUG_INPUT) Log.v(TAG,
4567 "Window " + mLastWin +
4568 " timed out on key input");
4569 if (mLastWin.mToken.paused) {
4570 Log.w(TAG, "Un-pausing dispatching to this window");
4571 mLastWin.mToken.paused = false;
4572 }
4573 }
4574 if (mMotionTarget == targetWin) {
4575 mMotionTarget = null;
4576 }
4577 mLastWin = null;
4578 mLastBinder = null;
4579 if (failIfTimeout || targetWin == null) {
4580 return null;
4581 }
4582 } else {
4583 Log.w(TAG, "Continuing to wait for key to be dispatched");
4584 startTime = SystemClock.uptimeMillis();
4585 }
4586 }
4587 }
4588 }
4589 }
Romain Guy06882f82009-06-10 13:36:04 -07004590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004592 MotionEvent nextMotion, boolean isPointerEvent,
4593 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 if (nextKey != null) {
4597 // Find the target window for a normal key event.
4598 final int keycode = nextKey.getKeyCode();
4599 final int repeatCount = nextKey.getRepeatCount();
4600 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4601 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004604 if (callingUid == 0 ||
4605 mContext.checkPermission(
4606 android.Manifest.permission.INJECT_EVENTS,
4607 callingPid, callingUid)
4608 == PackageManager.PERMISSION_GRANTED) {
4609 mPolicy.interceptKeyTi(null, keycode,
4610 nextKey.getMetaState(), down, repeatCount);
4611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 Log.w(TAG, "Event timeout during app switch: dropping "
4613 + nextKey);
4614 return SKIP_TARGET_TOKEN;
4615 }
Romain Guy06882f82009-06-10 13:36:04 -07004616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 WindowState focus = null;
4620 synchronized(mWindowMap) {
4621 focus = getFocusedWindowLocked();
4622 }
Romain Guy06882f82009-06-10 13:36:04 -07004623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004625
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004626 if (callingUid == 0 ||
4627 (focus != null && callingUid == focus.mSession.mUid) ||
4628 mContext.checkPermission(
4629 android.Manifest.permission.INJECT_EVENTS,
4630 callingPid, callingUid)
4631 == PackageManager.PERMISSION_GRANTED) {
4632 if (mPolicy.interceptKeyTi(focus,
4633 keycode, nextKey.getMetaState(), down, repeatCount)) {
4634 return CONSUMED_EVENT_TOKEN;
4635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 }
Romain Guy06882f82009-06-10 13:36:04 -07004637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 } else if (!isPointerEvent) {
4641 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4642 if (!dispatch) {
4643 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4644 + nextMotion);
4645 return SKIP_TARGET_TOKEN;
4646 }
Romain Guy06882f82009-06-10 13:36:04 -07004647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 WindowState focus = null;
4649 synchronized(mWindowMap) {
4650 focus = getFocusedWindowLocked();
4651 }
Romain Guy06882f82009-06-10 13:36:04 -07004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4654 return focus;
4655 }
Romain Guy06882f82009-06-10 13:36:04 -07004656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 if (nextMotion == null) {
4658 return SKIP_TARGET_TOKEN;
4659 }
Romain Guy06882f82009-06-10 13:36:04 -07004660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4662 KeyEvent.KEYCODE_UNKNOWN);
4663 if (!dispatch) {
4664 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4665 + nextMotion);
4666 return SKIP_TARGET_TOKEN;
4667 }
Romain Guy06882f82009-06-10 13:36:04 -07004668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 // Find the target window for a pointer event.
4670 int action = nextMotion.getAction();
4671 final float xf = nextMotion.getX();
4672 final float yf = nextMotion.getY();
4673 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 final boolean screenWasOff = qev != null
4676 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 synchronized(mWindowMap) {
4681 synchronized (this) {
4682 if (action == MotionEvent.ACTION_DOWN) {
4683 if (mMotionTarget != null) {
4684 // this is weird, we got a pen down, but we thought it was
4685 // already down!
4686 // XXX: We should probably send an ACTION_UP to the current
4687 // target.
4688 Log.w(TAG, "Pointer down received while already down in: "
4689 + mMotionTarget);
4690 mMotionTarget = null;
4691 }
Romain Guy06882f82009-06-10 13:36:04 -07004692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 // ACTION_DOWN is special, because we need to lock next events to
4694 // the window we'll land onto.
4695 final int x = (int)xf;
4696 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 final ArrayList windows = mWindows;
4699 final int N = windows.size();
4700 WindowState topErrWindow = null;
4701 final Rect tmpRect = mTempRect;
4702 for (int i=N-1; i>=0; i--) {
4703 WindowState child = (WindowState)windows.get(i);
4704 //Log.i(TAG, "Checking dispatch to: " + child);
4705 final int flags = child.mAttrs.flags;
4706 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4707 if (topErrWindow == null) {
4708 topErrWindow = child;
4709 }
4710 }
4711 if (!child.isVisibleLw()) {
4712 //Log.i(TAG, "Not visible!");
4713 continue;
4714 }
4715 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4716 //Log.i(TAG, "Not touchable!");
4717 if ((flags & WindowManager.LayoutParams
4718 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4719 child.mNextOutsideTouch = mOutsideTouchTargets;
4720 mOutsideTouchTargets = child;
4721 }
4722 continue;
4723 }
4724 tmpRect.set(child.mFrame);
4725 if (child.mTouchableInsets == ViewTreeObserver
4726 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4727 // The touch is inside of the window if it is
4728 // inside the frame, AND the content part of that
4729 // frame that was given by the application.
4730 tmpRect.left += child.mGivenContentInsets.left;
4731 tmpRect.top += child.mGivenContentInsets.top;
4732 tmpRect.right -= child.mGivenContentInsets.right;
4733 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4734 } else if (child.mTouchableInsets == ViewTreeObserver
4735 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4736 // The touch is inside of the window if it is
4737 // inside the frame, AND the visible part of that
4738 // frame that was given by the application.
4739 tmpRect.left += child.mGivenVisibleInsets.left;
4740 tmpRect.top += child.mGivenVisibleInsets.top;
4741 tmpRect.right -= child.mGivenVisibleInsets.right;
4742 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4743 }
4744 final int touchFlags = flags &
4745 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4746 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4747 if (tmpRect.contains(x, y) || touchFlags == 0) {
4748 //Log.i(TAG, "Using this target!");
4749 if (!screenWasOff || (flags &
4750 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4751 mMotionTarget = child;
4752 } else {
4753 //Log.i(TAG, "Waking, skip!");
4754 mMotionTarget = null;
4755 }
4756 break;
4757 }
Romain Guy06882f82009-06-10 13:36:04 -07004758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 if ((flags & WindowManager.LayoutParams
4760 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4761 child.mNextOutsideTouch = mOutsideTouchTargets;
4762 mOutsideTouchTargets = child;
4763 //Log.i(TAG, "Adding to outside target list: " + child);
4764 }
4765 }
4766
4767 // if there's an error window but it's not accepting
4768 // focus (typically because it is not yet visible) just
4769 // wait for it -- any other focused window may in fact
4770 // be in ANR state.
4771 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4772 mMotionTarget = null;
4773 }
4774 }
Romain Guy06882f82009-06-10 13:36:04 -07004775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 target = mMotionTarget;
4777 }
4778 }
Romain Guy06882f82009-06-10 13:36:04 -07004779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 // Pointer events are a little different -- if there isn't a
4783 // target found for any event, then just drop it.
4784 return target != null ? target : SKIP_TARGET_TOKEN;
4785 }
Romain Guy06882f82009-06-10 13:36:04 -07004786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 boolean checkShouldDispatchKey(int keycode) {
4788 synchronized (this) {
4789 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4790 mTimeToSwitch = 0;
4791 return true;
4792 }
4793 if (mTimeToSwitch != 0
4794 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4795 return false;
4796 }
4797 return true;
4798 }
4799 }
Romain Guy06882f82009-06-10 13:36:04 -07004800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 void bindTargetWindowLocked(WindowState win,
4802 int pendingWhat, QueuedEvent pendingMotion) {
4803 synchronized (this) {
4804 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4805 }
4806 }
Romain Guy06882f82009-06-10 13:36:04 -07004807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 void bindTargetWindowLocked(WindowState win) {
4809 synchronized (this) {
4810 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4811 }
4812 }
4813
4814 void bindTargetWindowLockedLocked(WindowState win,
4815 int pendingWhat, QueuedEvent pendingMotion) {
4816 mLastWin = win;
4817 mLastBinder = win.mClient.asBinder();
4818 mFinished = false;
4819 if (pendingMotion != null) {
4820 final Session s = win.mSession;
4821 if (pendingWhat == RETURN_PENDING_POINTER) {
4822 releasePendingPointerLocked(s);
4823 s.mPendingPointerMove = pendingMotion;
4824 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004825 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 "bindTargetToWindow " + s.mPendingPointerMove);
4827 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4828 releasePendingTrackballLocked(s);
4829 s.mPendingTrackballMove = pendingMotion;
4830 s.mPendingTrackballWindow = win;
4831 }
4832 }
4833 }
Romain Guy06882f82009-06-10 13:36:04 -07004834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 void releasePendingPointerLocked(Session s) {
4836 if (DEBUG_INPUT) Log.v(TAG,
4837 "releasePendingPointer " + s.mPendingPointerMove);
4838 if (s.mPendingPointerMove != null) {
4839 mQueue.recycleEvent(s.mPendingPointerMove);
4840 s.mPendingPointerMove = null;
4841 }
4842 }
Romain Guy06882f82009-06-10 13:36:04 -07004843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 void releasePendingTrackballLocked(Session s) {
4845 if (s.mPendingTrackballMove != null) {
4846 mQueue.recycleEvent(s.mPendingTrackballMove);
4847 s.mPendingTrackballMove = null;
4848 }
4849 }
Romain Guy06882f82009-06-10 13:36:04 -07004850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4852 int returnWhat) {
4853 if (DEBUG_INPUT) Log.v(
4854 TAG, "finishedKey: client=" + client + ", force=" + force);
4855
4856 if (client == null) {
4857 return null;
4858 }
4859
4860 synchronized (this) {
4861 if (DEBUG_INPUT) Log.v(
4862 TAG, "finishedKey: client=" + client.asBinder()
4863 + ", force=" + force + ", last=" + mLastBinder
4864 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4865
4866 QueuedEvent qev = null;
4867 WindowState win = null;
4868 if (returnWhat == RETURN_PENDING_POINTER) {
4869 qev = session.mPendingPointerMove;
4870 win = session.mPendingPointerWindow;
4871 session.mPendingPointerMove = null;
4872 session.mPendingPointerWindow = null;
4873 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4874 qev = session.mPendingTrackballMove;
4875 win = session.mPendingTrackballWindow;
4876 session.mPendingTrackballMove = null;
4877 session.mPendingTrackballWindow = null;
4878 }
Romain Guy06882f82009-06-10 13:36:04 -07004879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 if (mLastBinder == client.asBinder()) {
4881 if (DEBUG_INPUT) Log.v(
4882 TAG, "finishedKey: last paused="
4883 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4884 if (mLastWin != null && (!mLastWin.mToken.paused || force
4885 || !mEventDispatching)) {
4886 doFinishedKeyLocked(false);
4887 } else {
4888 // Make sure to wake up anyone currently waiting to
4889 // dispatch a key, so they can re-evaluate their
4890 // current situation.
4891 mFinished = true;
4892 notifyAll();
4893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 }
Romain Guy06882f82009-06-10 13:36:04 -07004895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 if (qev != null) {
4897 MotionEvent res = (MotionEvent)qev.event;
4898 if (DEBUG_INPUT) Log.v(TAG,
4899 "Returning pending motion: " + res);
4900 mQueue.recycleEvent(qev);
4901 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4902 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4903 }
4904 return res;
4905 }
4906 return null;
4907 }
4908 }
4909
4910 void tickle() {
4911 synchronized (this) {
4912 notifyAll();
4913 }
4914 }
Romain Guy06882f82009-06-10 13:36:04 -07004915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 void handleNewWindowLocked(WindowState newWindow) {
4917 if (!newWindow.canReceiveKeys()) {
4918 return;
4919 }
4920 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004921 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 TAG, "New key dispatch window: win="
4923 + newWindow.mClient.asBinder()
4924 + ", last=" + mLastBinder
4925 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4926 + "), finished=" + mFinished + ", paused="
4927 + newWindow.mToken.paused);
4928
4929 // Displaying a window implicitly causes dispatching to
4930 // be unpaused. (This is to protect against bugs if someone
4931 // pauses dispatching but forgets to resume.)
4932 newWindow.mToken.paused = false;
4933
4934 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935
4936 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4937 if (DEBUG_INPUT) Log.v(TAG,
4938 "New SYSTEM_ERROR window; resetting state");
4939 mLastWin = null;
4940 mLastBinder = null;
4941 mMotionTarget = null;
4942 mFinished = true;
4943 } else if (mLastWin != null) {
4944 // If the new window is above the window we are
4945 // waiting on, then stop waiting and let key dispatching
4946 // start on the new guy.
4947 if (DEBUG_INPUT) Log.v(
4948 TAG, "Last win layer=" + mLastWin.mLayer
4949 + ", new win layer=" + newWindow.mLayer);
4950 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004951 // The new window is above the old; finish pending input to the last
4952 // window and start directing it to the new one.
4953 mLastWin.mToken.paused = false;
4954 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004956 // Either the new window is lower, so there is no need to wake key waiters,
4957 // or we just finished key input to the previous window, which implicitly
4958 // notified the key waiters. In both cases, we don't need to issue the
4959 // notification here.
4960 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004961 }
4962
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004963 // Now that we've put a new window state in place, make the event waiter
4964 // take notice and retarget its attentions.
4965 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 }
4967 }
4968
4969 void pauseDispatchingLocked(WindowToken token) {
4970 synchronized (this)
4971 {
4972 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
4973 token.paused = true;
4974
4975 /*
4976 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
4977 mPaused = true;
4978 } else {
4979 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07004980 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07004982 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07004984 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 }
4986 }
4987 */
4988 }
4989 }
4990
4991 void resumeDispatchingLocked(WindowToken token) {
4992 synchronized (this) {
4993 if (token.paused) {
4994 if (DEBUG_INPUT) Log.v(
4995 TAG, "Resuming WindowToken " + token
4996 + ", last=" + mLastBinder
4997 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4998 + "), finished=" + mFinished + ", paused="
4999 + token.paused);
5000 token.paused = false;
5001 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5002 doFinishedKeyLocked(true);
5003 } else {
5004 notifyAll();
5005 }
5006 }
5007 }
5008 }
5009
5010 void setEventDispatchingLocked(boolean enabled) {
5011 synchronized (this) {
5012 mEventDispatching = enabled;
5013 notifyAll();
5014 }
5015 }
Romain Guy06882f82009-06-10 13:36:04 -07005016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 void appSwitchComing() {
5018 synchronized (this) {
5019 // Don't wait for more than .5 seconds for app to finish
5020 // processing the pending events.
5021 long now = SystemClock.uptimeMillis() + 500;
5022 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5023 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5024 mTimeToSwitch = now;
5025 }
5026 notifyAll();
5027 }
5028 }
Romain Guy06882f82009-06-10 13:36:04 -07005029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 private final void doFinishedKeyLocked(boolean doRecycle) {
5031 if (mLastWin != null) {
5032 releasePendingPointerLocked(mLastWin.mSession);
5033 releasePendingTrackballLocked(mLastWin.mSession);
5034 }
Romain Guy06882f82009-06-10 13:36:04 -07005035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 if (mLastWin == null || !mLastWin.mToken.paused
5037 || !mLastWin.isVisibleLw()) {
5038 // If the current window has been paused, we aren't -really-
5039 // finished... so let the waiters still wait.
5040 mLastWin = null;
5041 mLastBinder = null;
5042 }
5043 mFinished = true;
5044 notifyAll();
5045 }
5046 }
5047
5048 private class KeyQ extends KeyInputQueue
5049 implements KeyInputQueue.FilterCallback {
5050 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 KeyQ() {
5053 super(mContext);
5054 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5055 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5056 "KEEP_SCREEN_ON_FLAG");
5057 mHoldingScreen.setReferenceCounted(false);
5058 }
5059
5060 @Override
5061 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5062 if (mPolicy.preprocessInputEventTq(event)) {
5063 return true;
5064 }
Romain Guy06882f82009-06-10 13:36:04 -07005065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 switch (event.type) {
5067 case RawInputEvent.EV_KEY: {
5068 // XXX begin hack
5069 if (DEBUG) {
5070 if (event.keycode == KeyEvent.KEYCODE_G) {
5071 if (event.value != 0) {
5072 // G down
5073 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5074 }
5075 return false;
5076 }
5077 if (event.keycode == KeyEvent.KEYCODE_D) {
5078 if (event.value != 0) {
5079 //dump();
5080 }
5081 return false;
5082 }
5083 }
5084 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 boolean screenIsOff = !mPowerManager.screenIsOn();
5087 boolean screenIsDim = !mPowerManager.screenIsBright();
5088 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5091 mPowerManager.goToSleep(event.when);
5092 }
5093
5094 if (screenIsOff) {
5095 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5096 }
5097 if (screenIsDim) {
5098 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5099 }
5100 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5101 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005102 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 }
Romain Guy06882f82009-06-10 13:36:04 -07005104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5106 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5107 filterQueue(this);
5108 mKeyWaiter.appSwitchComing();
5109 }
5110 return true;
5111 } else {
5112 return false;
5113 }
5114 }
Romain Guy06882f82009-06-10 13:36:04 -07005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 case RawInputEvent.EV_REL: {
5117 boolean screenIsOff = !mPowerManager.screenIsOn();
5118 boolean screenIsDim = !mPowerManager.screenIsBright();
5119 if (screenIsOff) {
5120 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5121 device.classes, event)) {
5122 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5123 return false;
5124 }
5125 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5126 }
5127 if (screenIsDim) {
5128 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5129 }
5130 return true;
5131 }
Romain Guy06882f82009-06-10 13:36:04 -07005132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 case RawInputEvent.EV_ABS: {
5134 boolean screenIsOff = !mPowerManager.screenIsOn();
5135 boolean screenIsDim = !mPowerManager.screenIsBright();
5136 if (screenIsOff) {
5137 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5138 device.classes, event)) {
5139 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5140 return false;
5141 }
5142 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5143 }
5144 if (screenIsDim) {
5145 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5146 }
5147 return true;
5148 }
Romain Guy06882f82009-06-10 13:36:04 -07005149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 default:
5151 return true;
5152 }
5153 }
5154
5155 public int filterEvent(QueuedEvent ev) {
5156 switch (ev.classType) {
5157 case RawInputEvent.CLASS_KEYBOARD:
5158 KeyEvent ke = (KeyEvent)ev.event;
5159 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5160 Log.w(TAG, "Dropping movement key during app switch: "
5161 + ke.getKeyCode() + ", action=" + ke.getAction());
5162 return FILTER_REMOVE;
5163 }
5164 return FILTER_ABORT;
5165 default:
5166 return FILTER_KEEP;
5167 }
5168 }
Romain Guy06882f82009-06-10 13:36:04 -07005169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 /**
5171 * Must be called with the main window manager lock held.
5172 */
5173 void setHoldScreenLocked(boolean holding) {
5174 boolean state = mHoldingScreen.isHeld();
5175 if (holding != state) {
5176 if (holding) {
5177 mHoldingScreen.acquire();
5178 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005179 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 mHoldingScreen.release();
5181 }
5182 }
5183 }
5184 };
5185
5186 public boolean detectSafeMode() {
5187 mSafeMode = mPolicy.detectSafeMode();
5188 return mSafeMode;
5189 }
Romain Guy06882f82009-06-10 13:36:04 -07005190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 public void systemReady() {
5192 mPolicy.systemReady();
5193 }
Romain Guy06882f82009-06-10 13:36:04 -07005194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 private final class InputDispatcherThread extends Thread {
5196 // Time to wait when there is nothing to do: 9999 seconds.
5197 static final int LONG_WAIT=9999*1000;
5198
5199 public InputDispatcherThread() {
5200 super("InputDispatcher");
5201 }
Romain Guy06882f82009-06-10 13:36:04 -07005202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 @Override
5204 public void run() {
5205 while (true) {
5206 try {
5207 process();
5208 } catch (Exception e) {
5209 Log.e(TAG, "Exception in input dispatcher", e);
5210 }
5211 }
5212 }
Romain Guy06882f82009-06-10 13:36:04 -07005213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 private void process() {
5215 android.os.Process.setThreadPriority(
5216 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 // The last key event we saw
5219 KeyEvent lastKey = null;
5220
5221 // Last keydown time for auto-repeating keys
5222 long lastKeyTime = SystemClock.uptimeMillis();
5223 long nextKeyTime = lastKeyTime+LONG_WAIT;
5224
Romain Guy06882f82009-06-10 13:36:04 -07005225 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 int keyRepeatCount = 0;
5227
5228 // Need to report that configuration has changed?
5229 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 while (true) {
5232 long curTime = SystemClock.uptimeMillis();
5233
5234 if (DEBUG_INPUT) Log.v(
5235 TAG, "Waiting for next key: now=" + curTime
5236 + ", repeat @ " + nextKeyTime);
5237
5238 // Retrieve next event, waiting only as long as the next
5239 // repeat timeout. If the configuration has changed, then
5240 // don't wait at all -- we'll report the change as soon as
5241 // we have processed all events.
5242 QueuedEvent ev = mQueue.getEvent(
5243 (int)((!configChanged && curTime < nextKeyTime)
5244 ? (nextKeyTime-curTime) : 0));
5245
5246 if (DEBUG_INPUT && ev != null) Log.v(
5247 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5248
5249 try {
5250 if (ev != null) {
5251 curTime = ev.when;
5252 int eventType;
5253 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5254 eventType = eventType((MotionEvent)ev.event);
5255 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5256 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5257 eventType = LocalPowerManager.BUTTON_EVENT;
5258 } else {
5259 eventType = LocalPowerManager.OTHER_EVENT;
5260 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005261 try {
Michael Chane96440f2009-05-06 10:27:36 -07005262 long now = SystemClock.uptimeMillis();
5263
5264 if ((now - mLastBatteryStatsCallTime)
5265 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5266 mLastBatteryStatsCallTime = now;
5267 mBatteryStats.noteInputEvent();
5268 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005269 } catch (RemoteException e) {
5270 // Ignore
5271 }
Michael Chane96440f2009-05-06 10:27:36 -07005272 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 switch (ev.classType) {
5274 case RawInputEvent.CLASS_KEYBOARD:
5275 KeyEvent ke = (KeyEvent)ev.event;
5276 if (ke.isDown()) {
5277 lastKey = ke;
5278 keyRepeatCount = 0;
5279 lastKeyTime = curTime;
5280 nextKeyTime = lastKeyTime
5281 + KEY_REPEAT_FIRST_DELAY;
5282 if (DEBUG_INPUT) Log.v(
5283 TAG, "Received key down: first repeat @ "
5284 + nextKeyTime);
5285 } else {
5286 lastKey = null;
5287 // Arbitrary long timeout.
5288 lastKeyTime = curTime;
5289 nextKeyTime = curTime + LONG_WAIT;
5290 if (DEBUG_INPUT) Log.v(
5291 TAG, "Received key up: ignore repeat @ "
5292 + nextKeyTime);
5293 }
5294 dispatchKey((KeyEvent)ev.event, 0, 0);
5295 mQueue.recycleEvent(ev);
5296 break;
5297 case RawInputEvent.CLASS_TOUCHSCREEN:
5298 //Log.i(TAG, "Read next event " + ev);
5299 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5300 break;
5301 case RawInputEvent.CLASS_TRACKBALL:
5302 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5303 break;
5304 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5305 configChanged = true;
5306 break;
5307 default:
5308 mQueue.recycleEvent(ev);
5309 break;
5310 }
Romain Guy06882f82009-06-10 13:36:04 -07005311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 } else if (configChanged) {
5313 configChanged = false;
5314 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 } else if (lastKey != null) {
5317 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 // Timeout occurred while key was down. If it is at or
5320 // past the key repeat time, dispatch the repeat.
5321 if (DEBUG_INPUT) Log.v(
5322 TAG, "Key timeout: repeat=" + nextKeyTime
5323 + ", now=" + curTime);
5324 if (curTime < nextKeyTime) {
5325 continue;
5326 }
Romain Guy06882f82009-06-10 13:36:04 -07005327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 lastKeyTime = nextKeyTime;
5329 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5330 keyRepeatCount++;
5331 if (DEBUG_INPUT) Log.v(
5332 TAG, "Key repeat: count=" + keyRepeatCount
5333 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005334 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 } else {
5337 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 lastKeyTime = curTime;
5340 nextKeyTime = curTime + LONG_WAIT;
5341 }
Romain Guy06882f82009-06-10 13:36:04 -07005342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 } catch (Exception e) {
5344 Log.e(TAG,
5345 "Input thread received uncaught exception: " + e, e);
5346 }
5347 }
5348 }
5349 }
5350
5351 // -------------------------------------------------------------
5352 // Client Session State
5353 // -------------------------------------------------------------
5354
5355 private final class Session extends IWindowSession.Stub
5356 implements IBinder.DeathRecipient {
5357 final IInputMethodClient mClient;
5358 final IInputContext mInputContext;
5359 final int mUid;
5360 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005361 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 SurfaceSession mSurfaceSession;
5363 int mNumWindow = 0;
5364 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 /**
5367 * Current pointer move event being dispatched to client window... must
5368 * hold key lock to access.
5369 */
5370 QueuedEvent mPendingPointerMove;
5371 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 /**
5374 * Current trackball move event being dispatched to client window... must
5375 * hold key lock to access.
5376 */
5377 QueuedEvent mPendingTrackballMove;
5378 WindowState mPendingTrackballWindow;
5379
5380 public Session(IInputMethodClient client, IInputContext inputContext) {
5381 mClient = client;
5382 mInputContext = inputContext;
5383 mUid = Binder.getCallingUid();
5384 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005385 StringBuilder sb = new StringBuilder();
5386 sb.append("Session{");
5387 sb.append(Integer.toHexString(System.identityHashCode(this)));
5388 sb.append(" uid ");
5389 sb.append(mUid);
5390 sb.append("}");
5391 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 synchronized (mWindowMap) {
5394 if (mInputMethodManager == null && mHaveInputMethods) {
5395 IBinder b = ServiceManager.getService(
5396 Context.INPUT_METHOD_SERVICE);
5397 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5398 }
5399 }
5400 long ident = Binder.clearCallingIdentity();
5401 try {
5402 // Note: it is safe to call in to the input method manager
5403 // here because we are not holding our lock.
5404 if (mInputMethodManager != null) {
5405 mInputMethodManager.addClient(client, inputContext,
5406 mUid, mPid);
5407 } else {
5408 client.setUsingInputMethod(false);
5409 }
5410 client.asBinder().linkToDeath(this, 0);
5411 } catch (RemoteException e) {
5412 // The caller has died, so we can just forget about this.
5413 try {
5414 if (mInputMethodManager != null) {
5415 mInputMethodManager.removeClient(client);
5416 }
5417 } catch (RemoteException ee) {
5418 }
5419 } finally {
5420 Binder.restoreCallingIdentity(ident);
5421 }
5422 }
Romain Guy06882f82009-06-10 13:36:04 -07005423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 @Override
5425 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5426 throws RemoteException {
5427 try {
5428 return super.onTransact(code, data, reply, flags);
5429 } catch (RuntimeException e) {
5430 // Log all 'real' exceptions thrown to the caller
5431 if (!(e instanceof SecurityException)) {
5432 Log.e(TAG, "Window Session Crash", e);
5433 }
5434 throw e;
5435 }
5436 }
5437
5438 public void binderDied() {
5439 // Note: it is safe to call in to the input method manager
5440 // here because we are not holding our lock.
5441 try {
5442 if (mInputMethodManager != null) {
5443 mInputMethodManager.removeClient(mClient);
5444 }
5445 } catch (RemoteException e) {
5446 }
5447 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005448 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 mClientDead = true;
5450 killSessionLocked();
5451 }
5452 }
5453
5454 public int add(IWindow window, WindowManager.LayoutParams attrs,
5455 int viewVisibility, Rect outContentInsets) {
5456 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5457 }
Romain Guy06882f82009-06-10 13:36:04 -07005458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 public void remove(IWindow window) {
5460 removeWindow(this, window);
5461 }
Romain Guy06882f82009-06-10 13:36:04 -07005462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5464 int requestedWidth, int requestedHeight, int viewFlags,
5465 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5466 Rect outVisibleInsets, Surface outSurface) {
5467 return relayoutWindow(this, window, attrs,
5468 requestedWidth, requestedHeight, viewFlags, insetsPending,
5469 outFrame, outContentInsets, outVisibleInsets, outSurface);
5470 }
Romain Guy06882f82009-06-10 13:36:04 -07005471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 public void setTransparentRegion(IWindow window, Region region) {
5473 setTransparentRegionWindow(this, window, region);
5474 }
Romain Guy06882f82009-06-10 13:36:04 -07005475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 public void setInsets(IWindow window, int touchableInsets,
5477 Rect contentInsets, Rect visibleInsets) {
5478 setInsetsWindow(this, window, touchableInsets, contentInsets,
5479 visibleInsets);
5480 }
Romain Guy06882f82009-06-10 13:36:04 -07005481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5483 getWindowDisplayFrame(this, window, outDisplayFrame);
5484 }
Romain Guy06882f82009-06-10 13:36:04 -07005485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 public void finishDrawing(IWindow window) {
5487 if (localLOGV) Log.v(
5488 TAG, "IWindow finishDrawing called for " + window);
5489 finishDrawingWindow(this, window);
5490 }
5491
5492 public void finishKey(IWindow window) {
5493 if (localLOGV) Log.v(
5494 TAG, "IWindow finishKey called for " + window);
5495 mKeyWaiter.finishedKey(this, window, false,
5496 KeyWaiter.RETURN_NOTHING);
5497 }
5498
5499 public MotionEvent getPendingPointerMove(IWindow window) {
5500 if (localLOGV) Log.v(
5501 TAG, "IWindow getPendingMotionEvent called for " + window);
5502 return mKeyWaiter.finishedKey(this, window, false,
5503 KeyWaiter.RETURN_PENDING_POINTER);
5504 }
Romain Guy06882f82009-06-10 13:36:04 -07005505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 public MotionEvent getPendingTrackballMove(IWindow window) {
5507 if (localLOGV) Log.v(
5508 TAG, "IWindow getPendingMotionEvent called for " + window);
5509 return mKeyWaiter.finishedKey(this, window, false,
5510 KeyWaiter.RETURN_PENDING_TRACKBALL);
5511 }
5512
5513 public void setInTouchMode(boolean mode) {
5514 synchronized(mWindowMap) {
5515 mInTouchMode = mode;
5516 }
5517 }
5518
5519 public boolean getInTouchMode() {
5520 synchronized(mWindowMap) {
5521 return mInTouchMode;
5522 }
5523 }
5524
5525 public boolean performHapticFeedback(IWindow window, int effectId,
5526 boolean always) {
5527 synchronized(mWindowMap) {
5528 long ident = Binder.clearCallingIdentity();
5529 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005530 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 windowForClientLocked(this, window), effectId, always);
5532 } finally {
5533 Binder.restoreCallingIdentity(ident);
5534 }
5535 }
5536 }
Romain Guy06882f82009-06-10 13:36:04 -07005537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 void windowAddedLocked() {
5539 if (mSurfaceSession == null) {
5540 if (localLOGV) Log.v(
5541 TAG, "First window added to " + this + ", creating SurfaceSession");
5542 mSurfaceSession = new SurfaceSession();
5543 mSessions.add(this);
5544 }
5545 mNumWindow++;
5546 }
5547
5548 void windowRemovedLocked() {
5549 mNumWindow--;
5550 killSessionLocked();
5551 }
Romain Guy06882f82009-06-10 13:36:04 -07005552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 void killSessionLocked() {
5554 if (mNumWindow <= 0 && mClientDead) {
5555 mSessions.remove(this);
5556 if (mSurfaceSession != null) {
5557 if (localLOGV) Log.v(
5558 TAG, "Last window removed from " + this
5559 + ", destroying " + mSurfaceSession);
5560 try {
5561 mSurfaceSession.kill();
5562 } catch (Exception e) {
5563 Log.w(TAG, "Exception thrown when killing surface session "
5564 + mSurfaceSession + " in session " + this
5565 + ": " + e.toString());
5566 }
5567 mSurfaceSession = null;
5568 }
5569 }
5570 }
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005573 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5574 pw.print(" mClientDead="); pw.print(mClientDead);
5575 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5576 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5577 pw.print(prefix);
5578 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5579 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5580 }
5581 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5582 pw.print(prefix);
5583 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5584 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005586 }
5587
5588 @Override
5589 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005590 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 }
5592 }
5593
5594 // -------------------------------------------------------------
5595 // Client Window State
5596 // -------------------------------------------------------------
5597
5598 private final class WindowState implements WindowManagerPolicy.WindowState {
5599 final Session mSession;
5600 final IWindow mClient;
5601 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005602 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 AppWindowToken mAppToken;
5604 AppWindowToken mTargetAppToken;
5605 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5606 final DeathRecipient mDeathRecipient;
5607 final WindowState mAttachedWindow;
5608 final ArrayList mChildWindows = new ArrayList();
5609 final int mBaseLayer;
5610 final int mSubLayer;
5611 final boolean mLayoutAttached;
5612 final boolean mIsImWindow;
5613 int mViewVisibility;
5614 boolean mPolicyVisibility = true;
5615 boolean mPolicyVisibilityAfterAnim = true;
5616 boolean mAppFreezing;
5617 Surface mSurface;
5618 boolean mAttachedHidden; // is our parent window hidden?
5619 boolean mLastHidden; // was this window last hidden?
5620 int mRequestedWidth;
5621 int mRequestedHeight;
5622 int mLastRequestedWidth;
5623 int mLastRequestedHeight;
5624 int mReqXPos;
5625 int mReqYPos;
5626 int mLayer;
5627 int mAnimLayer;
5628 int mLastLayer;
5629 boolean mHaveFrame;
5630
5631 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 // Actual frame shown on-screen (may be modified by animation)
5634 final Rect mShownFrame = new Rect();
5635 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 /**
5638 * Insets that determine the actually visible area
5639 */
5640 final Rect mVisibleInsets = new Rect();
5641 final Rect mLastVisibleInsets = new Rect();
5642 boolean mVisibleInsetsChanged;
5643
5644 /**
5645 * Insets that are covered by system windows
5646 */
5647 final Rect mContentInsets = new Rect();
5648 final Rect mLastContentInsets = new Rect();
5649 boolean mContentInsetsChanged;
5650
5651 /**
5652 * Set to true if we are waiting for this window to receive its
5653 * given internal insets before laying out other windows based on it.
5654 */
5655 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 /**
5658 * These are the content insets that were given during layout for
5659 * this window, to be applied to windows behind it.
5660 */
5661 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663 /**
5664 * These are the visible insets that were given during layout for
5665 * this window, to be applied to windows behind it.
5666 */
5667 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 /**
5670 * Flag indicating whether the touchable region should be adjusted by
5671 * the visible insets; if false the area outside the visible insets is
5672 * NOT touchable, so we must use those to adjust the frame during hit
5673 * tests.
5674 */
5675 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 // Current transformation being applied.
5678 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5679 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5680 float mHScale=1, mVScale=1;
5681 float mLastHScale=1, mLastVScale=1;
5682 final Matrix mTmpMatrix = new Matrix();
5683
5684 // "Real" frame that the application sees.
5685 final Rect mFrame = new Rect();
5686 final Rect mLastFrame = new Rect();
5687
5688 final Rect mContainingFrame = new Rect();
5689 final Rect mDisplayFrame = new Rect();
5690 final Rect mContentFrame = new Rect();
5691 final Rect mVisibleFrame = new Rect();
5692
5693 float mShownAlpha = 1;
5694 float mAlpha = 1;
5695 float mLastAlpha = 1;
5696
5697 // Set to true if, when the window gets displayed, it should perform
5698 // an enter animation.
5699 boolean mEnterAnimationPending;
5700
5701 // Currently running animation.
5702 boolean mAnimating;
5703 boolean mLocalAnimating;
5704 Animation mAnimation;
5705 boolean mAnimationIsEntrance;
5706 boolean mHasTransformation;
5707 boolean mHasLocalTransformation;
5708 final Transformation mTransformation = new Transformation();
5709
5710 // This is set after IWindowSession.relayout() has been called at
5711 // least once for the window. It allows us to detect the situation
5712 // where we don't yet have a surface, but should have one soon, so
5713 // we can give the window focus before waiting for the relayout.
5714 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 // This is set after the Surface has been created but before the
5717 // window has been drawn. During this time the surface is hidden.
5718 boolean mDrawPending;
5719
5720 // This is set after the window has finished drawing for the first
5721 // time but before its surface is shown. The surface will be
5722 // displayed when the next layout is run.
5723 boolean mCommitDrawPending;
5724
5725 // This is set during the time after the window's drawing has been
5726 // committed, and before its surface is actually shown. It is used
5727 // to delay showing the surface until all windows in a token are ready
5728 // to be shown.
5729 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 // Set when the window has been shown in the screen the first time.
5732 boolean mHasDrawn;
5733
5734 // Currently running an exit animation?
5735 boolean mExiting;
5736
5737 // Currently on the mDestroySurface list?
5738 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 // Completely remove from window manager after exit animation?
5741 boolean mRemoveOnExit;
5742
5743 // Set when the orientation is changing and this window has not yet
5744 // been updated for the new orientation.
5745 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 // Is this window now (or just being) removed?
5748 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 WindowState(Session s, IWindow c, WindowToken token,
5751 WindowState attachedWindow, WindowManager.LayoutParams a,
5752 int viewVisibility) {
5753 mSession = s;
5754 mClient = c;
5755 mToken = token;
5756 mAttrs.copyFrom(a);
5757 mViewVisibility = viewVisibility;
5758 DeathRecipient deathRecipient = new DeathRecipient();
5759 mAlpha = a.alpha;
5760 if (localLOGV) Log.v(
5761 TAG, "Window " + this + " client=" + c.asBinder()
5762 + " token=" + token + " (" + mAttrs.token + ")");
5763 try {
5764 c.asBinder().linkToDeath(deathRecipient, 0);
5765 } catch (RemoteException e) {
5766 mDeathRecipient = null;
5767 mAttachedWindow = null;
5768 mLayoutAttached = false;
5769 mIsImWindow = false;
5770 mBaseLayer = 0;
5771 mSubLayer = 0;
5772 return;
5773 }
5774 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5777 mAttrs.type <= LAST_SUB_WINDOW)) {
5778 // The multiplier here is to reserve space for multiple
5779 // windows in the same type layer.
5780 mBaseLayer = mPolicy.windowTypeToLayerLw(
5781 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5782 + TYPE_LAYER_OFFSET;
5783 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5784 mAttachedWindow = attachedWindow;
5785 mAttachedWindow.mChildWindows.add(this);
5786 mLayoutAttached = mAttrs.type !=
5787 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5788 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5789 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5790 } else {
5791 // The multiplier here is to reserve space for multiple
5792 // windows in the same type layer.
5793 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5794 * TYPE_LAYER_MULTIPLIER
5795 + TYPE_LAYER_OFFSET;
5796 mSubLayer = 0;
5797 mAttachedWindow = null;
5798 mLayoutAttached = false;
5799 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5800 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5801 }
5802
5803 WindowState appWin = this;
5804 while (appWin.mAttachedWindow != null) {
5805 appWin = mAttachedWindow;
5806 }
5807 WindowToken appToken = appWin.mToken;
5808 while (appToken.appWindowToken == null) {
5809 WindowToken parent = mTokenMap.get(appToken.token);
5810 if (parent == null || appToken == parent) {
5811 break;
5812 }
5813 appToken = parent;
5814 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005815 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 mAppToken = appToken.appWindowToken;
5817
5818 mSurface = null;
5819 mRequestedWidth = 0;
5820 mRequestedHeight = 0;
5821 mLastRequestedWidth = 0;
5822 mLastRequestedHeight = 0;
5823 mReqXPos = 0;
5824 mReqYPos = 0;
5825 mLayer = 0;
5826 mAnimLayer = 0;
5827 mLastLayer = 0;
5828 }
5829
5830 void attach() {
5831 if (localLOGV) Log.v(
5832 TAG, "Attaching " + this + " token=" + mToken
5833 + ", list=" + mToken.windows);
5834 mSession.windowAddedLocked();
5835 }
5836
5837 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5838 mHaveFrame = true;
5839
5840 final int pw = pf.right-pf.left;
5841 final int ph = pf.bottom-pf.top;
5842
5843 int w,h;
5844 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5845 w = mAttrs.width < 0 ? pw : mAttrs.width;
5846 h = mAttrs.height< 0 ? ph : mAttrs.height;
5847 } else {
5848 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5849 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5850 }
Romain Guy06882f82009-06-10 13:36:04 -07005851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 final Rect container = mContainingFrame;
5853 container.set(pf);
5854
5855 final Rect display = mDisplayFrame;
5856 display.set(df);
5857
5858 final Rect content = mContentFrame;
5859 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 final Rect visible = mVisibleFrame;
5862 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5867 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5868
5869 Gravity.apply(mAttrs.gravity, w, h, container,
5870 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5871 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5872
5873 //System.out.println("Out: " + mFrame);
5874
5875 // Now make sure the window fits in the overall display.
5876 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Romain Guy06882f82009-06-10 13:36:04 -07005877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 // Make sure the content and visible frames are inside of the
5879 // final window frame.
5880 if (content.left < frame.left) content.left = frame.left;
5881 if (content.top < frame.top) content.top = frame.top;
5882 if (content.right > frame.right) content.right = frame.right;
5883 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5884 if (visible.left < frame.left) visible.left = frame.left;
5885 if (visible.top < frame.top) visible.top = frame.top;
5886 if (visible.right > frame.right) visible.right = frame.right;
5887 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 final Rect contentInsets = mContentInsets;
5890 contentInsets.left = content.left-frame.left;
5891 contentInsets.top = content.top-frame.top;
5892 contentInsets.right = frame.right-content.right;
5893 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 final Rect visibleInsets = mVisibleInsets;
5896 visibleInsets.left = visible.left-frame.left;
5897 visibleInsets.top = visible.top-frame.top;
5898 visibleInsets.right = frame.right-visible.right;
5899 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 if (localLOGV) {
5902 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5903 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5904 Log.v(TAG, "Resolving (mRequestedWidth="
5905 + mRequestedWidth + ", mRequestedheight="
5906 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5907 + "): frame=" + mFrame.toShortString()
5908 + " ci=" + contentInsets.toShortString()
5909 + " vi=" + visibleInsets.toShortString());
5910 //}
5911 }
5912 }
Romain Guy06882f82009-06-10 13:36:04 -07005913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005914 public Rect getFrameLw() {
5915 return mFrame;
5916 }
5917
5918 public Rect getShownFrameLw() {
5919 return mShownFrame;
5920 }
5921
5922 public Rect getDisplayFrameLw() {
5923 return mDisplayFrame;
5924 }
5925
5926 public Rect getContentFrameLw() {
5927 return mContentFrame;
5928 }
5929
5930 public Rect getVisibleFrameLw() {
5931 return mVisibleFrame;
5932 }
5933
5934 public boolean getGivenInsetsPendingLw() {
5935 return mGivenInsetsPending;
5936 }
5937
5938 public Rect getGivenContentInsetsLw() {
5939 return mGivenContentInsets;
5940 }
Romain Guy06882f82009-06-10 13:36:04 -07005941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 public Rect getGivenVisibleInsetsLw() {
5943 return mGivenVisibleInsets;
5944 }
Romain Guy06882f82009-06-10 13:36:04 -07005945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 public WindowManager.LayoutParams getAttrs() {
5947 return mAttrs;
5948 }
5949
5950 public int getSurfaceLayer() {
5951 return mLayer;
5952 }
Romain Guy06882f82009-06-10 13:36:04 -07005953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 public IApplicationToken getAppToken() {
5955 return mAppToken != null ? mAppToken.appToken : null;
5956 }
5957
5958 public boolean hasAppShownWindows() {
5959 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
5960 }
5961
5962 public boolean hasAppStartingIcon() {
5963 return mAppToken != null ? (mAppToken.startingData != null) : false;
5964 }
5965
5966 public WindowManagerPolicy.WindowState getAppStartingWindow() {
5967 return mAppToken != null ? mAppToken.startingWindow : null;
5968 }
5969
5970 public void setAnimation(Animation anim) {
5971 if (localLOGV) Log.v(
5972 TAG, "Setting animation in " + this + ": " + anim);
5973 mAnimating = false;
5974 mLocalAnimating = false;
5975 mAnimation = anim;
5976 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
5977 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
5978 }
5979
5980 public void clearAnimation() {
5981 if (mAnimation != null) {
5982 mAnimating = true;
5983 mLocalAnimating = false;
5984 mAnimation = null;
5985 }
5986 }
Romain Guy06882f82009-06-10 13:36:04 -07005987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 Surface createSurfaceLocked() {
5989 if (mSurface == null) {
5990 mDrawPending = true;
5991 mCommitDrawPending = false;
5992 mReadyToShow = false;
5993 if (mAppToken != null) {
5994 mAppToken.allDrawn = false;
5995 }
5996
5997 int flags = 0;
5998 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
5999 flags |= Surface.HARDWARE;
6000 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6001 flags |= Surface.GPU;
6002 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6003 flags |= Surface.PUSH_BUFFERS;
6004 }
6005
6006 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6007 flags |= Surface.SECURE;
6008 }
6009 if (DEBUG_VISIBILITY) Log.v(
6010 TAG, "Creating surface in session "
6011 + mSession.mSurfaceSession + " window " + this
6012 + " w=" + mFrame.width()
6013 + " h=" + mFrame.height() + " format="
6014 + mAttrs.format + " flags=" + flags);
6015
6016 int w = mFrame.width();
6017 int h = mFrame.height();
6018 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6019 // for a scaled surface, we always want the requested
6020 // size.
6021 w = mRequestedWidth;
6022 h = mRequestedHeight;
6023 }
6024
6025 try {
6026 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006027 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 0, w, h, mAttrs.format, flags);
6029 } catch (Surface.OutOfResourcesException e) {
6030 Log.w(TAG, "OutOfResourcesException creating surface");
6031 reclaimSomeSurfaceMemoryLocked(this, "create");
6032 return null;
6033 } catch (Exception e) {
6034 Log.e(TAG, "Exception creating surface", e);
6035 return null;
6036 }
Romain Guy06882f82009-06-10 13:36:04 -07006037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038 if (localLOGV) Log.v(
6039 TAG, "Got surface: " + mSurface
6040 + ", set left=" + mFrame.left + " top=" + mFrame.top
6041 + ", animLayer=" + mAnimLayer);
6042 if (SHOW_TRANSACTIONS) {
6043 Log.i(TAG, ">>> OPEN TRANSACTION");
6044 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6045 + mAttrs.getTitle() + ") pos=(" +
6046 mFrame.left + "," + mFrame.top + ") (" +
6047 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6048 mAnimLayer + " HIDE");
6049 }
6050 Surface.openTransaction();
6051 try {
6052 try {
6053 mSurface.setPosition(mFrame.left, mFrame.top);
6054 mSurface.setLayer(mAnimLayer);
6055 mSurface.hide();
6056 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6057 mSurface.setFlags(Surface.SURFACE_DITHER,
6058 Surface.SURFACE_DITHER);
6059 }
6060 } catch (RuntimeException e) {
6061 Log.w(TAG, "Error creating surface in " + w, e);
6062 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6063 }
6064 mLastHidden = true;
6065 } finally {
6066 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6067 Surface.closeTransaction();
6068 }
6069 if (localLOGV) Log.v(
6070 TAG, "Created surface " + this);
6071 }
6072 return mSurface;
6073 }
Romain Guy06882f82009-06-10 13:36:04 -07006074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 void destroySurfaceLocked() {
6076 // Window is no longer on-screen, so can no longer receive
6077 // key events... if we were waiting for it to finish
6078 // handling a key event, the wait is over!
6079 mKeyWaiter.finishedKey(mSession, mClient, true,
6080 KeyWaiter.RETURN_NOTHING);
6081 mKeyWaiter.releasePendingPointerLocked(mSession);
6082 mKeyWaiter.releasePendingTrackballLocked(mSession);
6083
6084 if (mAppToken != null && this == mAppToken.startingWindow) {
6085 mAppToken.startingDisplayed = false;
6086 }
Romain Guy06882f82009-06-10 13:36:04 -07006087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 if (localLOGV) Log.v(
6089 TAG, "Window " + this
6090 + " destroying surface " + mSurface + ", session " + mSession);
6091 if (mSurface != null) {
6092 try {
6093 if (SHOW_TRANSACTIONS) {
6094 RuntimeException ex = new RuntimeException();
6095 ex.fillInStackTrace();
6096 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6097 + mAttrs.getTitle() + ")", ex);
6098 }
6099 mSurface.clear();
6100 } catch (RuntimeException e) {
6101 Log.w(TAG, "Exception thrown when destroying Window " + this
6102 + " surface " + mSurface + " session " + mSession
6103 + ": " + e.toString());
6104 }
6105 mSurface = null;
6106 mDrawPending = false;
6107 mCommitDrawPending = false;
6108 mReadyToShow = false;
6109
6110 int i = mChildWindows.size();
6111 while (i > 0) {
6112 i--;
6113 WindowState c = (WindowState)mChildWindows.get(i);
6114 c.mAttachedHidden = true;
6115 }
6116 }
6117 }
6118
6119 boolean finishDrawingLocked() {
6120 if (mDrawPending) {
6121 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6122 TAG, "finishDrawingLocked: " + mSurface);
6123 mCommitDrawPending = true;
6124 mDrawPending = false;
6125 return true;
6126 }
6127 return false;
6128 }
6129
6130 // This must be called while inside a transaction.
6131 void commitFinishDrawingLocked(long currentTime) {
6132 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6133 if (!mCommitDrawPending) {
6134 return;
6135 }
6136 mCommitDrawPending = false;
6137 mReadyToShow = true;
6138 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6139 final AppWindowToken atoken = mAppToken;
6140 if (atoken == null || atoken.allDrawn || starting) {
6141 performShowLocked();
6142 }
6143 }
6144
6145 // This must be called while inside a transaction.
6146 boolean performShowLocked() {
6147 if (DEBUG_VISIBILITY) {
6148 RuntimeException e = new RuntimeException();
6149 e.fillInStackTrace();
6150 Log.v(TAG, "performShow on " + this
6151 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6152 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6153 }
6154 if (mReadyToShow && isReadyForDisplay()) {
6155 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6156 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6157 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6158 + " during animation: policyVis=" + mPolicyVisibility
6159 + " attHidden=" + mAttachedHidden
6160 + " tok.hiddenRequested="
6161 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6162 + " tok.idden="
6163 + (mAppToken != null ? mAppToken.hidden : false)
6164 + " animating=" + mAnimating
6165 + " tok animating="
6166 + (mAppToken != null ? mAppToken.animating : false));
6167 if (!showSurfaceRobustlyLocked(this)) {
6168 return false;
6169 }
6170 mLastAlpha = -1;
6171 mHasDrawn = true;
6172 mLastHidden = false;
6173 mReadyToShow = false;
6174 enableScreenIfNeededLocked();
6175
6176 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 int i = mChildWindows.size();
6179 while (i > 0) {
6180 i--;
6181 WindowState c = (WindowState)mChildWindows.get(i);
6182 if (c.mSurface != null && c.mAttachedHidden) {
6183 c.mAttachedHidden = false;
6184 c.performShowLocked();
6185 }
6186 }
Romain Guy06882f82009-06-10 13:36:04 -07006187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 if (mAttrs.type != TYPE_APPLICATION_STARTING
6189 && mAppToken != null) {
6190 mAppToken.firstWindowDrawn = true;
6191 if (mAnimation == null && mAppToken.startingData != null) {
6192 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6193 + mToken
6194 + ": first real window is shown, no animation");
6195 mFinishedStarting.add(mAppToken);
6196 mH.sendEmptyMessage(H.FINISHED_STARTING);
6197 }
6198 mAppToken.updateReportedVisibilityLocked();
6199 }
6200 }
6201 return true;
6202 }
Romain Guy06882f82009-06-10 13:36:04 -07006203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204 // This must be called while inside a transaction. Returns true if
6205 // there is more animation to run.
6206 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6207 if (!mDisplayFrozen) {
6208 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006210 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6211 mHasTransformation = true;
6212 mHasLocalTransformation = true;
6213 if (!mLocalAnimating) {
6214 if (DEBUG_ANIM) Log.v(
6215 TAG, "Starting animation in " + this +
6216 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6217 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6218 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6219 mAnimation.setStartTime(currentTime);
6220 mLocalAnimating = true;
6221 mAnimating = true;
6222 }
6223 mTransformation.clear();
6224 final boolean more = mAnimation.getTransformation(
6225 currentTime, mTransformation);
6226 if (DEBUG_ANIM) Log.v(
6227 TAG, "Stepped animation in " + this +
6228 ": more=" + more + ", xform=" + mTransformation);
6229 if (more) {
6230 // we're not done!
6231 return true;
6232 }
6233 if (DEBUG_ANIM) Log.v(
6234 TAG, "Finished animation in " + this +
6235 " @ " + currentTime);
6236 mAnimation = null;
6237 //WindowManagerService.this.dump();
6238 }
6239 mHasLocalTransformation = false;
6240 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6241 && mAppToken.hasTransformation) {
6242 // When our app token is animating, we kind-of pretend like
6243 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6244 // part of this check means that we will only do this if
6245 // our window is not currently exiting, or it is not
6246 // locally animating itself. The idea being that one that
6247 // is exiting and doing a local animation should be removed
6248 // once that animation is done.
6249 mAnimating = true;
6250 mHasTransformation = true;
6251 mTransformation.clear();
6252 return false;
6253 } else if (mHasTransformation) {
6254 // Little trick to get through the path below to act like
6255 // we have finished an animation.
6256 mAnimating = true;
6257 } else if (isAnimating()) {
6258 mAnimating = true;
6259 }
6260 } else if (mAnimation != null) {
6261 // If the display is frozen, and there is a pending animation,
6262 // clear it and make sure we run the cleanup code.
6263 mAnimating = true;
6264 mLocalAnimating = true;
6265 mAnimation = null;
6266 }
Romain Guy06882f82009-06-10 13:36:04 -07006267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 if (!mAnimating && !mLocalAnimating) {
6269 return false;
6270 }
6271
6272 if (DEBUG_ANIM) Log.v(
6273 TAG, "Animation done in " + this + ": exiting=" + mExiting
6274 + ", reportedVisible="
6275 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006277 mAnimating = false;
6278 mLocalAnimating = false;
6279 mAnimation = null;
6280 mAnimLayer = mLayer;
6281 if (mIsImWindow) {
6282 mAnimLayer += mInputMethodAnimLayerAdjustment;
6283 }
6284 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6285 + " anim layer: " + mAnimLayer);
6286 mHasTransformation = false;
6287 mHasLocalTransformation = false;
6288 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6289 mTransformation.clear();
6290 if (mHasDrawn
6291 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6292 && mAppToken != null
6293 && mAppToken.firstWindowDrawn
6294 && mAppToken.startingData != null) {
6295 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6296 + mToken + ": first real window done animating");
6297 mFinishedStarting.add(mAppToken);
6298 mH.sendEmptyMessage(H.FINISHED_STARTING);
6299 }
Romain Guy06882f82009-06-10 13:36:04 -07006300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 finishExit();
6302
6303 if (mAppToken != null) {
6304 mAppToken.updateReportedVisibilityLocked();
6305 }
6306
6307 return false;
6308 }
6309
6310 void finishExit() {
6311 if (DEBUG_ANIM) Log.v(
6312 TAG, "finishExit in " + this
6313 + ": exiting=" + mExiting
6314 + " remove=" + mRemoveOnExit
6315 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006317 final int N = mChildWindows.size();
6318 for (int i=0; i<N; i++) {
6319 ((WindowState)mChildWindows.get(i)).finishExit();
6320 }
Romain Guy06882f82009-06-10 13:36:04 -07006321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 if (!mExiting) {
6323 return;
6324 }
Romain Guy06882f82009-06-10 13:36:04 -07006325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326 if (isWindowAnimating()) {
6327 return;
6328 }
6329
6330 if (localLOGV) Log.v(
6331 TAG, "Exit animation finished in " + this
6332 + ": remove=" + mRemoveOnExit);
6333 if (mSurface != null) {
6334 mDestroySurface.add(this);
6335 mDestroying = true;
6336 if (SHOW_TRANSACTIONS) Log.i(
6337 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6338 try {
6339 mSurface.hide();
6340 } catch (RuntimeException e) {
6341 Log.w(TAG, "Error hiding surface in " + this, e);
6342 }
6343 mLastHidden = true;
6344 mKeyWaiter.releasePendingPointerLocked(mSession);
6345 }
6346 mExiting = false;
6347 if (mRemoveOnExit) {
6348 mPendingRemove.add(this);
6349 mRemoveOnExit = false;
6350 }
6351 }
Romain Guy06882f82009-06-10 13:36:04 -07006352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6354 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6355 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6356 if (dtdx < -.000001f || dtdx > .000001f) return false;
6357 if (dsdy < -.000001f || dsdy > .000001f) return false;
6358 return true;
6359 }
Romain Guy06882f82009-06-10 13:36:04 -07006360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 void computeShownFrameLocked() {
6362 final boolean selfTransformation = mHasLocalTransformation;
6363 Transformation attachedTransformation =
6364 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6365 ? mAttachedWindow.mTransformation : null;
6366 Transformation appTransformation =
6367 (mAppToken != null && mAppToken.hasTransformation)
6368 ? mAppToken.transformation : null;
6369 if (selfTransformation || attachedTransformation != null
6370 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006371 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 final Rect frame = mFrame;
6373 final float tmpFloats[] = mTmpFloats;
6374 final Matrix tmpMatrix = mTmpMatrix;
6375
6376 // Compute the desired transformation.
6377 tmpMatrix.setTranslate(frame.left, frame.top);
6378 if (selfTransformation) {
6379 tmpMatrix.preConcat(mTransformation.getMatrix());
6380 }
6381 if (attachedTransformation != null) {
6382 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6383 }
6384 if (appTransformation != null) {
6385 tmpMatrix.preConcat(appTransformation.getMatrix());
6386 }
6387
6388 // "convert" it into SurfaceFlinger's format
6389 // (a 2x2 matrix + an offset)
6390 // Here we must not transform the position of the surface
6391 // since it is already included in the transformation.
6392 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 tmpMatrix.getValues(tmpFloats);
6395 mDsDx = tmpFloats[Matrix.MSCALE_X];
6396 mDtDx = tmpFloats[Matrix.MSKEW_X];
6397 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6398 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6399 int x = (int)tmpFloats[Matrix.MTRANS_X];
6400 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6401 int w = frame.width();
6402 int h = frame.height();
6403 mShownFrame.set(x, y, x+w, y+h);
6404
6405 // Now set the alpha... but because our current hardware
6406 // can't do alpha transformation on a non-opaque surface,
6407 // turn it off if we are running an animation that is also
6408 // transforming since it is more important to have that
6409 // animation be smooth.
6410 mShownAlpha = mAlpha;
6411 if (!mLimitedAlphaCompositing
6412 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6413 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6414 && x == frame.left && y == frame.top))) {
6415 //Log.i(TAG, "Applying alpha transform");
6416 if (selfTransformation) {
6417 mShownAlpha *= mTransformation.getAlpha();
6418 }
6419 if (attachedTransformation != null) {
6420 mShownAlpha *= attachedTransformation.getAlpha();
6421 }
6422 if (appTransformation != null) {
6423 mShownAlpha *= appTransformation.getAlpha();
6424 }
6425 } else {
6426 //Log.i(TAG, "Not applying alpha transform");
6427 }
Romain Guy06882f82009-06-10 13:36:04 -07006428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 if (localLOGV) Log.v(
6430 TAG, "Continuing animation in " + this +
6431 ": " + mShownFrame +
6432 ", alpha=" + mTransformation.getAlpha());
6433 return;
6434 }
Romain Guy06882f82009-06-10 13:36:04 -07006435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006436 mShownFrame.set(mFrame);
6437 mShownAlpha = mAlpha;
6438 mDsDx = 1;
6439 mDtDx = 0;
6440 mDsDy = 0;
6441 mDtDy = 1;
6442 }
Romain Guy06882f82009-06-10 13:36:04 -07006443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 /**
6445 * Is this window visible? It is not visible if there is no
6446 * surface, or we are in the process of running an exit animation
6447 * that will remove the surface, or its app token has been hidden.
6448 */
6449 public boolean isVisibleLw() {
6450 final AppWindowToken atoken = mAppToken;
6451 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6452 && (atoken == null || !atoken.hiddenRequested)
6453 && !mExiting && !mDestroying;
6454 }
6455
6456 /**
6457 * Is this window visible, ignoring its app token? It is not visible
6458 * if there is no surface, or we are in the process of running an exit animation
6459 * that will remove the surface.
6460 */
6461 public boolean isWinVisibleLw() {
6462 final AppWindowToken atoken = mAppToken;
6463 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6464 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6465 && !mExiting && !mDestroying;
6466 }
6467
6468 /**
6469 * The same as isVisible(), but follows the current hidden state of
6470 * the associated app token, not the pending requested hidden state.
6471 */
6472 boolean isVisibleNow() {
6473 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006474 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 }
6476
6477 /**
6478 * Same as isVisible(), but we also count it as visible between the
6479 * call to IWindowSession.add() and the first relayout().
6480 */
6481 boolean isVisibleOrAdding() {
6482 final AppWindowToken atoken = mAppToken;
6483 return (mSurface != null
6484 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6485 && mPolicyVisibility && !mAttachedHidden
6486 && (atoken == null || !atoken.hiddenRequested)
6487 && !mExiting && !mDestroying;
6488 }
6489
6490 /**
6491 * Is this window currently on-screen? It is on-screen either if it
6492 * is visible or it is currently running an animation before no longer
6493 * being visible.
6494 */
6495 boolean isOnScreen() {
6496 final AppWindowToken atoken = mAppToken;
6497 if (atoken != null) {
6498 return mSurface != null && mPolicyVisibility && !mDestroying
6499 && ((!mAttachedHidden && !atoken.hiddenRequested)
6500 || mAnimating || atoken.animating);
6501 } else {
6502 return mSurface != null && mPolicyVisibility && !mDestroying
6503 && (!mAttachedHidden || mAnimating);
6504 }
6505 }
Romain Guy06882f82009-06-10 13:36:04 -07006506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 /**
6508 * Like isOnScreen(), but we don't return true if the window is part
6509 * of a transition that has not yet been started.
6510 */
6511 boolean isReadyForDisplay() {
6512 final AppWindowToken atoken = mAppToken;
6513 final boolean animating = atoken != null ? atoken.animating : false;
6514 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006515 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 || mAnimating || animating);
6517 }
6518
6519 /** Is the window or its container currently animating? */
6520 boolean isAnimating() {
6521 final WindowState attached = mAttachedWindow;
6522 final AppWindowToken atoken = mAppToken;
6523 return mAnimation != null
6524 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006525 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 (atoken.animation != null
6527 || atoken.inPendingTransaction));
6528 }
6529
6530 /** Is this window currently animating? */
6531 boolean isWindowAnimating() {
6532 return mAnimation != null;
6533 }
6534
6535 /**
6536 * Like isOnScreen, but returns false if the surface hasn't yet
6537 * been drawn.
6538 */
6539 public boolean isDisplayedLw() {
6540 final AppWindowToken atoken = mAppToken;
6541 return mSurface != null && mPolicyVisibility && !mDestroying
6542 && !mDrawPending && !mCommitDrawPending
6543 && ((!mAttachedHidden &&
6544 (atoken == null || !atoken.hiddenRequested))
6545 || mAnimating);
6546 }
6547
6548 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6549 boolean shownFrame, boolean onlyOpaque) {
6550 if (mSurface == null) {
6551 return false;
6552 }
6553 if (mAppToken != null && !mAppToken.appFullscreen) {
6554 return false;
6555 }
6556 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6557 return false;
6558 }
6559 final Rect frame = shownFrame ? mShownFrame : mFrame;
6560 if (frame.left <= 0 && frame.top <= 0
6561 && frame.right >= screenWidth
6562 && frame.bottom >= screenHeight) {
6563 return true;
6564 }
6565 return false;
6566 }
Romain Guy06882f82009-06-10 13:36:04 -07006567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006568 boolean isFullscreenOpaque(int screenWidth, int screenHeight) {
6569 if (mAttrs.format != PixelFormat.OPAQUE || mSurface == null
6570 || mAnimation != null || mDrawPending || mCommitDrawPending) {
6571 return false;
6572 }
6573 if (mFrame.left <= 0 && mFrame.top <= 0 &&
6574 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight) {
6575 return true;
6576 }
6577 return false;
6578 }
6579
6580 void removeLocked() {
6581 if (mAttachedWindow != null) {
6582 mAttachedWindow.mChildWindows.remove(this);
6583 }
6584 destroySurfaceLocked();
6585 mSession.windowRemovedLocked();
6586 try {
6587 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6588 } catch (RuntimeException e) {
6589 // Ignore if it has already been removed (usually because
6590 // we are doing this as part of processing a death note.)
6591 }
6592 }
6593
6594 private class DeathRecipient implements IBinder.DeathRecipient {
6595 public void binderDied() {
6596 try {
6597 synchronized(mWindowMap) {
6598 WindowState win = windowForClientLocked(mSession, mClient);
6599 Log.i(TAG, "WIN DEATH: " + win);
6600 if (win != null) {
6601 removeWindowLocked(mSession, win);
6602 }
6603 }
6604 } catch (IllegalArgumentException ex) {
6605 // This will happen if the window has already been
6606 // removed.
6607 }
6608 }
6609 }
6610
6611 /** Returns true if this window desires key events. */
6612 public final boolean canReceiveKeys() {
6613 return isVisibleOrAdding()
6614 && (mViewVisibility == View.VISIBLE)
6615 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6616 }
6617
6618 public boolean hasDrawnLw() {
6619 return mHasDrawn;
6620 }
6621
6622 public boolean showLw(boolean doAnimation) {
6623 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6624 mPolicyVisibility = true;
6625 mPolicyVisibilityAfterAnim = true;
6626 if (doAnimation) {
6627 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6628 }
6629 requestAnimationLocked(0);
6630 return true;
6631 }
6632 return false;
6633 }
6634
6635 public boolean hideLw(boolean doAnimation) {
6636 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6637 : mPolicyVisibility;
6638 if (current) {
6639 if (doAnimation) {
6640 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6641 if (mAnimation == null) {
6642 doAnimation = false;
6643 }
6644 }
6645 if (doAnimation) {
6646 mPolicyVisibilityAfterAnim = false;
6647 } else {
6648 mPolicyVisibilityAfterAnim = false;
6649 mPolicyVisibility = false;
6650 }
6651 requestAnimationLocked(0);
6652 return true;
6653 }
6654 return false;
6655 }
6656
6657 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006658 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006659
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006660 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6661 pw.print(" mClient="); pw.println(mClient.asBinder());
6662 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6663 if (mAttachedWindow != null || mLayoutAttached) {
6664 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6665 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6666 }
6667 if (mIsImWindow) {
6668 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6669 }
6670 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6671 pw.print(" mSubLayer="); pw.print(mSubLayer);
6672 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6673 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6674 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6675 pw.print("="); pw.print(mAnimLayer);
6676 pw.print(" mLastLayer="); pw.println(mLastLayer);
6677 if (mSurface != null) {
6678 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6679 }
6680 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6681 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6682 if (mAppToken != null) {
6683 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6684 }
6685 if (mTargetAppToken != null) {
6686 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6687 }
6688 pw.print(prefix); pw.print("mViewVisibility=0x");
6689 pw.print(Integer.toHexString(mViewVisibility));
6690 pw.print(" mLastHidden="); pw.print(mLastHidden);
6691 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6692 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6693 pw.print(prefix); pw.print("mPolicyVisibility=");
6694 pw.print(mPolicyVisibility);
6695 pw.print(" mPolicyVisibilityAfterAnim=");
6696 pw.print(mPolicyVisibilityAfterAnim);
6697 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6698 }
6699 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6700 pw.print(" h="); pw.print(mRequestedHeight);
6701 pw.print(" x="); pw.print(mReqXPos);
6702 pw.print(" y="); pw.println(mReqYPos);
6703 pw.print(prefix); pw.print("mGivenContentInsets=");
6704 mGivenContentInsets.printShortString(pw);
6705 pw.print(" mGivenVisibleInsets=");
6706 mGivenVisibleInsets.printShortString(pw);
6707 pw.println();
6708 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6709 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6710 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6711 }
6712 pw.print(prefix); pw.print("mShownFrame=");
6713 mShownFrame.printShortString(pw);
6714 pw.print(" last="); mLastShownFrame.printShortString(pw);
6715 pw.println();
6716 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6717 pw.print(" last="); mLastFrame.printShortString(pw);
6718 pw.println();
6719 pw.print(prefix); pw.print("mContainingFrame=");
6720 mContainingFrame.printShortString(pw);
6721 pw.print(" mDisplayFrame=");
6722 mDisplayFrame.printShortString(pw);
6723 pw.println();
6724 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6725 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6726 pw.println();
6727 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6728 pw.print(" last="); mLastContentInsets.printShortString(pw);
6729 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6730 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6731 pw.println();
6732 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6733 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6734 pw.print(" mAlpha="); pw.print(mAlpha);
6735 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6736 }
6737 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6738 || mAnimation != null) {
6739 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6740 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6741 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6742 pw.print(" mAnimation="); pw.println(mAnimation);
6743 }
6744 if (mHasTransformation || mHasLocalTransformation) {
6745 pw.print(prefix); pw.print("XForm: has=");
6746 pw.print(mHasTransformation);
6747 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6748 pw.print(" "); mTransformation.printShortString(pw);
6749 pw.println();
6750 }
6751 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6752 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6753 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6754 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6755 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6756 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6757 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6758 pw.print(" mDestroying="); pw.print(mDestroying);
6759 pw.print(" mRemoved="); pw.println(mRemoved);
6760 }
6761 if (mOrientationChanging || mAppFreezing) {
6762 pw.print(prefix); pw.print("mOrientationChanging=");
6763 pw.print(mOrientationChanging);
6764 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 }
6767
6768 @Override
6769 public String toString() {
6770 return "Window{"
6771 + Integer.toHexString(System.identityHashCode(this))
6772 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6773 }
6774 }
Romain Guy06882f82009-06-10 13:36:04 -07006775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 // -------------------------------------------------------------
6777 // Window Token State
6778 // -------------------------------------------------------------
6779
6780 class WindowToken {
6781 // The actual token.
6782 final IBinder token;
6783
6784 // The type of window this token is for, as per WindowManager.LayoutParams.
6785 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006787 // Set if this token was explicitly added by a client, so should
6788 // not be removed when all windows are removed.
6789 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006790
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006791 // For printing.
6792 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 // If this is an AppWindowToken, this is non-null.
6795 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 // All of the windows associated with this token.
6798 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6799
6800 // Is key dispatching paused for this token?
6801 boolean paused = false;
6802
6803 // Should this token's windows be hidden?
6804 boolean hidden;
6805
6806 // Temporary for finding which tokens no longer have visible windows.
6807 boolean hasVisible;
6808
6809 WindowToken(IBinder _token, int type, boolean _explicit) {
6810 token = _token;
6811 windowType = type;
6812 explicit = _explicit;
6813 }
6814
6815 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006816 pw.print(prefix); pw.print("token="); pw.println(token);
6817 pw.print(prefix); pw.print("windows="); pw.println(windows);
6818 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6819 pw.print(" hidden="); pw.print(hidden);
6820 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821 }
6822
6823 @Override
6824 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006825 if (stringName == null) {
6826 StringBuilder sb = new StringBuilder();
6827 sb.append("WindowToken{");
6828 sb.append(Integer.toHexString(System.identityHashCode(this)));
6829 sb.append(" token="); sb.append(token); sb.append('}');
6830 stringName = sb.toString();
6831 }
6832 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 }
6834 };
6835
6836 class AppWindowToken extends WindowToken {
6837 // Non-null only for application tokens.
6838 final IApplicationToken appToken;
6839
6840 // All of the windows and child windows that are included in this
6841 // application token. Note this list is NOT sorted!
6842 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6843
6844 int groupId = -1;
6845 boolean appFullscreen;
6846 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 // These are used for determining when all windows associated with
6849 // an activity have been drawn, so they can be made visible together
6850 // at the same time.
6851 int lastTransactionSequence = mTransactionSequence-1;
6852 int numInterestingWindows;
6853 int numDrawnWindows;
6854 boolean inPendingTransaction;
6855 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 // Is this token going to be hidden in a little while? If so, it
6858 // won't be taken into account for setting the screen orientation.
6859 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 // Is this window's surface needed? This is almost like hidden, except
6862 // it will sometimes be true a little earlier: when the token has
6863 // been shown, but is still waiting for its app transition to execute
6864 // before making its windows shown.
6865 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006867 // Have we told the window clients to hide themselves?
6868 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 // Last visibility state we reported to the app token.
6871 boolean reportedVisible;
6872
6873 // Set to true when the token has been removed from the window mgr.
6874 boolean removed;
6875
6876 // Have we been asked to have this token keep the screen frozen?
6877 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 boolean animating;
6880 Animation animation;
6881 boolean hasTransformation;
6882 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07006883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006884 // Offset to the window of all layers in the token, for use by
6885 // AppWindowToken animations.
6886 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07006887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 // Information about an application starting window if displayed.
6889 StartingData startingData;
6890 WindowState startingWindow;
6891 View startingView;
6892 boolean startingDisplayed;
6893 boolean startingMoved;
6894 boolean firstWindowDrawn;
6895
6896 AppWindowToken(IApplicationToken _token) {
6897 super(_token.asBinder(),
6898 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6899 appWindowToken = this;
6900 appToken = _token;
6901 }
Romain Guy06882f82009-06-10 13:36:04 -07006902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 public void setAnimation(Animation anim) {
6904 if (localLOGV) Log.v(
6905 TAG, "Setting animation in " + this + ": " + anim);
6906 animation = anim;
6907 animating = false;
6908 anim.restrictDuration(MAX_ANIMATION_DURATION);
6909 anim.scaleCurrentDuration(mTransitionAnimationScale);
6910 int zorder = anim.getZAdjustment();
6911 int adj = 0;
6912 if (zorder == Animation.ZORDER_TOP) {
6913 adj = TYPE_LAYER_OFFSET;
6914 } else if (zorder == Animation.ZORDER_BOTTOM) {
6915 adj = -TYPE_LAYER_OFFSET;
6916 }
Romain Guy06882f82009-06-10 13:36:04 -07006917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 if (animLayerAdjustment != adj) {
6919 animLayerAdjustment = adj;
6920 updateLayers();
6921 }
6922 }
Romain Guy06882f82009-06-10 13:36:04 -07006923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 public void setDummyAnimation() {
6925 if (animation == null) {
6926 if (localLOGV) Log.v(
6927 TAG, "Setting dummy animation in " + this);
6928 animation = sDummyAnimation;
6929 }
6930 }
6931
6932 public void clearAnimation() {
6933 if (animation != null) {
6934 animation = null;
6935 animating = true;
6936 }
6937 }
Romain Guy06882f82009-06-10 13:36:04 -07006938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 void updateLayers() {
6940 final int N = allAppWindows.size();
6941 final int adj = animLayerAdjustment;
6942 for (int i=0; i<N; i++) {
6943 WindowState w = allAppWindows.get(i);
6944 w.mAnimLayer = w.mLayer + adj;
6945 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
6946 + w.mAnimLayer);
6947 if (w == mInputMethodTarget) {
6948 setInputMethodAnimLayerAdjustment(adj);
6949 }
6950 }
6951 }
Romain Guy06882f82009-06-10 13:36:04 -07006952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 void sendAppVisibilityToClients() {
6954 final int N = allAppWindows.size();
6955 for (int i=0; i<N; i++) {
6956 WindowState win = allAppWindows.get(i);
6957 if (win == startingWindow && clientHidden) {
6958 // Don't hide the starting window.
6959 continue;
6960 }
6961 try {
6962 if (DEBUG_VISIBILITY) Log.v(TAG,
6963 "Setting visibility of " + win + ": " + (!clientHidden));
6964 win.mClient.dispatchAppVisibility(!clientHidden);
6965 } catch (RemoteException e) {
6966 }
6967 }
6968 }
Romain Guy06882f82009-06-10 13:36:04 -07006969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 void showAllWindowsLocked() {
6971 final int NW = allAppWindows.size();
6972 for (int i=0; i<NW; i++) {
6973 WindowState w = allAppWindows.get(i);
6974 if (DEBUG_VISIBILITY) Log.v(TAG,
6975 "performing show on: " + w);
6976 w.performShowLocked();
6977 }
6978 }
Romain Guy06882f82009-06-10 13:36:04 -07006979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 // This must be called while inside a transaction.
6981 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6982 if (!mDisplayFrozen) {
6983 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 if (animation == sDummyAnimation) {
6986 // This guy is going to animate, but not yet. For now count
6987 // it is not animating for purposes of scheduling transactions;
6988 // when it is really time to animate, this will be set to
6989 // a real animation and the next call will execute normally.
6990 return false;
6991 }
Romain Guy06882f82009-06-10 13:36:04 -07006992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 if ((allDrawn || animating || startingDisplayed) && animation != null) {
6994 if (!animating) {
6995 if (DEBUG_ANIM) Log.v(
6996 TAG, "Starting animation in " + this +
6997 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
6998 + " scale=" + mTransitionAnimationScale
6999 + " allDrawn=" + allDrawn + " animating=" + animating);
7000 animation.initialize(dw, dh, dw, dh);
7001 animation.setStartTime(currentTime);
7002 animating = true;
7003 }
7004 transformation.clear();
7005 final boolean more = animation.getTransformation(
7006 currentTime, transformation);
7007 if (DEBUG_ANIM) Log.v(
7008 TAG, "Stepped animation in " + this +
7009 ": more=" + more + ", xform=" + transformation);
7010 if (more) {
7011 // we're done!
7012 hasTransformation = true;
7013 return true;
7014 }
7015 if (DEBUG_ANIM) Log.v(
7016 TAG, "Finished animation in " + this +
7017 " @ " + currentTime);
7018 animation = null;
7019 }
7020 } else if (animation != null) {
7021 // If the display is frozen, and there is a pending animation,
7022 // clear it and make sure we run the cleanup code.
7023 animating = true;
7024 animation = null;
7025 }
7026
7027 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 if (!animating) {
7030 return false;
7031 }
7032
7033 clearAnimation();
7034 animating = false;
7035 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7036 moveInputMethodWindowsIfNeededLocked(true);
7037 }
Romain Guy06882f82009-06-10 13:36:04 -07007038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 if (DEBUG_ANIM) Log.v(
7040 TAG, "Animation done in " + this
7041 + ": reportedVisible=" + reportedVisible);
7042
7043 transformation.clear();
7044 if (animLayerAdjustment != 0) {
7045 animLayerAdjustment = 0;
7046 updateLayers();
7047 }
Romain Guy06882f82009-06-10 13:36:04 -07007048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007049 final int N = windows.size();
7050 for (int i=0; i<N; i++) {
7051 ((WindowState)windows.get(i)).finishExit();
7052 }
7053 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007055 return false;
7056 }
7057
7058 void updateReportedVisibilityLocked() {
7059 if (appToken == null) {
7060 return;
7061 }
Romain Guy06882f82009-06-10 13:36:04 -07007062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007063 int numInteresting = 0;
7064 int numVisible = 0;
7065 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7068 final int N = allAppWindows.size();
7069 for (int i=0; i<N; i++) {
7070 WindowState win = allAppWindows.get(i);
7071 if (win == startingWindow || win.mAppFreezing) {
7072 continue;
7073 }
7074 if (DEBUG_VISIBILITY) {
7075 Log.v(TAG, "Win " + win + ": isDisplayed="
7076 + win.isDisplayedLw()
7077 + ", isAnimating=" + win.isAnimating());
7078 if (!win.isDisplayedLw()) {
7079 Log.v(TAG, "Not displayed: s=" + win.mSurface
7080 + " pv=" + win.mPolicyVisibility
7081 + " dp=" + win.mDrawPending
7082 + " cdp=" + win.mCommitDrawPending
7083 + " ah=" + win.mAttachedHidden
7084 + " th="
7085 + (win.mAppToken != null
7086 ? win.mAppToken.hiddenRequested : false)
7087 + " a=" + win.mAnimating);
7088 }
7089 }
7090 numInteresting++;
7091 if (win.isDisplayedLw()) {
7092 if (!win.isAnimating()) {
7093 numVisible++;
7094 }
7095 nowGone = false;
7096 } else if (win.isAnimating()) {
7097 nowGone = false;
7098 }
7099 }
Romain Guy06882f82009-06-10 13:36:04 -07007100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7102 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7103 + numInteresting + " visible=" + numVisible);
7104 if (nowVisible != reportedVisible) {
7105 if (DEBUG_VISIBILITY) Log.v(
7106 TAG, "Visibility changed in " + this
7107 + ": vis=" + nowVisible);
7108 reportedVisible = nowVisible;
7109 Message m = mH.obtainMessage(
7110 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7111 nowVisible ? 1 : 0,
7112 nowGone ? 1 : 0,
7113 this);
7114 mH.sendMessage(m);
7115 }
7116 }
Romain Guy06882f82009-06-10 13:36:04 -07007117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 void dump(PrintWriter pw, String prefix) {
7119 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007120 if (appToken != null) {
7121 pw.print(prefix); pw.println("app=true");
7122 }
7123 if (allAppWindows.size() > 0) {
7124 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7125 }
7126 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7127 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7128 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7129 pw.print(" clientHidden="); pw.print(clientHidden);
7130 pw.print(" willBeHidden="); pw.print(willBeHidden);
7131 pw.print(" reportedVisible="); pw.println(reportedVisible);
7132 if (paused || freezingScreen) {
7133 pw.print(prefix); pw.print("paused="); pw.print(paused);
7134 pw.print(" freezingScreen="); pw.println(freezingScreen);
7135 }
7136 if (numInterestingWindows != 0 || numDrawnWindows != 0
7137 || inPendingTransaction || allDrawn) {
7138 pw.print(prefix); pw.print("numInterestingWindows=");
7139 pw.print(numInterestingWindows);
7140 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7141 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7142 pw.print(" allDrawn="); pw.println(allDrawn);
7143 }
7144 if (animating || animation != null) {
7145 pw.print(prefix); pw.print("animating="); pw.print(animating);
7146 pw.print(" animation="); pw.println(animation);
7147 }
7148 if (animLayerAdjustment != 0) {
7149 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7150 }
7151 if (hasTransformation) {
7152 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7153 pw.print(" transformation="); transformation.printShortString(pw);
7154 pw.println();
7155 }
7156 if (startingData != null || removed || firstWindowDrawn) {
7157 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7158 pw.print(" removed="); pw.print(removed);
7159 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7160 }
7161 if (startingWindow != null || startingView != null
7162 || startingDisplayed || startingMoved) {
7163 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7164 pw.print(" startingView="); pw.print(startingView);
7165 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7166 pw.print(" startingMoved"); pw.println(startingMoved);
7167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 }
7169
7170 @Override
7171 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007172 if (stringName == null) {
7173 StringBuilder sb = new StringBuilder();
7174 sb.append("AppWindowToken{");
7175 sb.append(Integer.toHexString(System.identityHashCode(this)));
7176 sb.append(" token="); sb.append(token); sb.append('}');
7177 stringName = sb.toString();
7178 }
7179 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 }
7181 }
Romain Guy06882f82009-06-10 13:36:04 -07007182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 public static WindowManager.LayoutParams findAnimations(
7184 ArrayList<AppWindowToken> order,
7185 ArrayList<AppWindowToken> tokenList1,
7186 ArrayList<AppWindowToken> tokenList2) {
7187 // We need to figure out which animation to use...
7188 WindowManager.LayoutParams animParams = null;
7189 int animSrc = 0;
Romain Guy06882f82009-06-10 13:36:04 -07007190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 //Log.i(TAG, "Looking for animations...");
7192 for (int i=order.size()-1; i>=0; i--) {
7193 AppWindowToken wtoken = order.get(i);
7194 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
7195 if (tokenList1.contains(wtoken) || tokenList2.contains(wtoken)) {
7196 int j = wtoken.windows.size();
7197 while (j > 0) {
7198 j--;
7199 WindowState win = wtoken.windows.get(j);
7200 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7201 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7202 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7203 //Log.i(TAG, "Found base or application window, done!");
7204 if (wtoken.appFullscreen) {
7205 return win.mAttrs;
7206 }
7207 if (animSrc < 2) {
7208 animParams = win.mAttrs;
7209 animSrc = 2;
7210 }
7211 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7212 //Log.i(TAG, "Found normal window, we may use this...");
7213 animParams = win.mAttrs;
7214 animSrc = 1;
7215 }
7216 }
7217 }
7218 }
Romain Guy06882f82009-06-10 13:36:04 -07007219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 return animParams;
7221 }
Romain Guy06882f82009-06-10 13:36:04 -07007222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007223 // -------------------------------------------------------------
7224 // DummyAnimation
7225 // -------------------------------------------------------------
7226
7227 // This is an animation that does nothing: it just immediately finishes
7228 // itself every time it is called. It is used as a stub animation in cases
7229 // where we want to synchronize multiple things that may be animating.
7230 static final class DummyAnimation extends Animation {
7231 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7232 return false;
7233 }
7234 }
7235 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 // -------------------------------------------------------------
7238 // Async Handler
7239 // -------------------------------------------------------------
7240
7241 static final class StartingData {
7242 final String pkg;
7243 final int theme;
7244 final CharSequence nonLocalizedLabel;
7245 final int labelRes;
7246 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7249 int _labelRes, int _icon) {
7250 pkg = _pkg;
7251 theme = _theme;
7252 nonLocalizedLabel = _nonLocalizedLabel;
7253 labelRes = _labelRes;
7254 icon = _icon;
7255 }
7256 }
7257
7258 private final class H extends Handler {
7259 public static final int REPORT_FOCUS_CHANGE = 2;
7260 public static final int REPORT_LOSING_FOCUS = 3;
7261 public static final int ANIMATE = 4;
7262 public static final int ADD_STARTING = 5;
7263 public static final int REMOVE_STARTING = 6;
7264 public static final int FINISHED_STARTING = 7;
7265 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007266 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7267 public static final int HOLD_SCREEN_CHANGED = 12;
7268 public static final int APP_TRANSITION_TIMEOUT = 13;
7269 public static final int PERSIST_ANIMATION_SCALE = 14;
7270 public static final int FORCE_GC = 15;
7271 public static final int ENABLE_SCREEN = 16;
7272 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007273 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007275 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007277 public H() {
7278 }
Romain Guy06882f82009-06-10 13:36:04 -07007279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 @Override
7281 public void handleMessage(Message msg) {
7282 switch (msg.what) {
7283 case REPORT_FOCUS_CHANGE: {
7284 WindowState lastFocus;
7285 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 synchronized(mWindowMap) {
7288 lastFocus = mLastFocus;
7289 newFocus = mCurrentFocus;
7290 if (lastFocus == newFocus) {
7291 // Focus is not changing, so nothing to do.
7292 return;
7293 }
7294 mLastFocus = newFocus;
7295 //Log.i(TAG, "Focus moving from " + lastFocus
7296 // + " to " + newFocus);
7297 if (newFocus != null && lastFocus != null
7298 && !newFocus.isDisplayedLw()) {
7299 //Log.i(TAG, "Delaying loss of focus...");
7300 mLosingFocus.add(lastFocus);
7301 lastFocus = null;
7302 }
7303 }
7304
7305 if (lastFocus != newFocus) {
7306 //System.out.println("Changing focus from " + lastFocus
7307 // + " to " + newFocus);
7308 if (newFocus != null) {
7309 try {
7310 //Log.i(TAG, "Gaining focus: " + newFocus);
7311 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7312 } catch (RemoteException e) {
7313 // Ignore if process has died.
7314 }
7315 }
7316
7317 if (lastFocus != null) {
7318 try {
7319 //Log.i(TAG, "Losing focus: " + lastFocus);
7320 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7321 } catch (RemoteException e) {
7322 // Ignore if process has died.
7323 }
7324 }
7325 }
7326 } break;
7327
7328 case REPORT_LOSING_FOCUS: {
7329 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 synchronized(mWindowMap) {
7332 losers = mLosingFocus;
7333 mLosingFocus = new ArrayList<WindowState>();
7334 }
7335
7336 final int N = losers.size();
7337 for (int i=0; i<N; i++) {
7338 try {
7339 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7340 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7341 } catch (RemoteException e) {
7342 // Ignore if process has died.
7343 }
7344 }
7345 } break;
7346
7347 case ANIMATE: {
7348 synchronized(mWindowMap) {
7349 mAnimationPending = false;
7350 performLayoutAndPlaceSurfacesLocked();
7351 }
7352 } break;
7353
7354 case ADD_STARTING: {
7355 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7356 final StartingData sd = wtoken.startingData;
7357
7358 if (sd == null) {
7359 // Animation has been canceled... do nothing.
7360 return;
7361 }
Romain Guy06882f82009-06-10 13:36:04 -07007362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7364 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 View view = null;
7367 try {
7368 view = mPolicy.addStartingWindow(
7369 wtoken.token, sd.pkg,
7370 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7371 sd.icon);
7372 } catch (Exception e) {
7373 Log.w(TAG, "Exception when adding starting window", e);
7374 }
7375
7376 if (view != null) {
7377 boolean abort = false;
7378
7379 synchronized(mWindowMap) {
7380 if (wtoken.removed || wtoken.startingData == null) {
7381 // If the window was successfully added, then
7382 // we need to remove it.
7383 if (wtoken.startingWindow != null) {
7384 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7385 "Aborted starting " + wtoken
7386 + ": removed=" + wtoken.removed
7387 + " startingData=" + wtoken.startingData);
7388 wtoken.startingWindow = null;
7389 wtoken.startingData = null;
7390 abort = true;
7391 }
7392 } else {
7393 wtoken.startingView = view;
7394 }
7395 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7396 "Added starting " + wtoken
7397 + ": startingWindow="
7398 + wtoken.startingWindow + " startingView="
7399 + wtoken.startingView);
7400 }
7401
7402 if (abort) {
7403 try {
7404 mPolicy.removeStartingWindow(wtoken.token, view);
7405 } catch (Exception e) {
7406 Log.w(TAG, "Exception when removing starting window", e);
7407 }
7408 }
7409 }
7410 } break;
7411
7412 case REMOVE_STARTING: {
7413 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7414 IBinder token = null;
7415 View view = null;
7416 synchronized (mWindowMap) {
7417 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7418 + wtoken + ": startingWindow="
7419 + wtoken.startingWindow + " startingView="
7420 + wtoken.startingView);
7421 if (wtoken.startingWindow != null) {
7422 view = wtoken.startingView;
7423 token = wtoken.token;
7424 wtoken.startingData = null;
7425 wtoken.startingView = null;
7426 wtoken.startingWindow = null;
7427 }
7428 }
7429 if (view != null) {
7430 try {
7431 mPolicy.removeStartingWindow(token, view);
7432 } catch (Exception e) {
7433 Log.w(TAG, "Exception when removing starting window", e);
7434 }
7435 }
7436 } break;
7437
7438 case FINISHED_STARTING: {
7439 IBinder token = null;
7440 View view = null;
7441 while (true) {
7442 synchronized (mWindowMap) {
7443 final int N = mFinishedStarting.size();
7444 if (N <= 0) {
7445 break;
7446 }
7447 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7448
7449 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7450 "Finished starting " + wtoken
7451 + ": startingWindow=" + wtoken.startingWindow
7452 + " startingView=" + wtoken.startingView);
7453
7454 if (wtoken.startingWindow == null) {
7455 continue;
7456 }
7457
7458 view = wtoken.startingView;
7459 token = wtoken.token;
7460 wtoken.startingData = null;
7461 wtoken.startingView = null;
7462 wtoken.startingWindow = null;
7463 }
7464
7465 try {
7466 mPolicy.removeStartingWindow(token, view);
7467 } catch (Exception e) {
7468 Log.w(TAG, "Exception when removing starting window", e);
7469 }
7470 }
7471 } break;
7472
7473 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7474 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7475
7476 boolean nowVisible = msg.arg1 != 0;
7477 boolean nowGone = msg.arg2 != 0;
7478
7479 try {
7480 if (DEBUG_VISIBILITY) Log.v(
7481 TAG, "Reporting visible in " + wtoken
7482 + " visible=" + nowVisible
7483 + " gone=" + nowGone);
7484 if (nowVisible) {
7485 wtoken.appToken.windowsVisible();
7486 } else {
7487 wtoken.appToken.windowsGone();
7488 }
7489 } catch (RemoteException ex) {
7490 }
7491 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 case WINDOW_FREEZE_TIMEOUT: {
7494 synchronized (mWindowMap) {
7495 Log.w(TAG, "Window freeze timeout expired.");
7496 int i = mWindows.size();
7497 while (i > 0) {
7498 i--;
7499 WindowState w = (WindowState)mWindows.get(i);
7500 if (w.mOrientationChanging) {
7501 w.mOrientationChanging = false;
7502 Log.w(TAG, "Force clearing orientation change: " + w);
7503 }
7504 }
7505 performLayoutAndPlaceSurfacesLocked();
7506 }
7507 break;
7508 }
Romain Guy06882f82009-06-10 13:36:04 -07007509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 case HOLD_SCREEN_CHANGED: {
7511 Session oldHold;
7512 Session newHold;
7513 synchronized (mWindowMap) {
7514 oldHold = mLastReportedHold;
7515 newHold = (Session)msg.obj;
7516 mLastReportedHold = newHold;
7517 }
Romain Guy06882f82009-06-10 13:36:04 -07007518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 if (oldHold != newHold) {
7520 try {
7521 if (oldHold != null) {
7522 mBatteryStats.noteStopWakelock(oldHold.mUid,
7523 "window",
7524 BatteryStats.WAKE_TYPE_WINDOW);
7525 }
7526 if (newHold != null) {
7527 mBatteryStats.noteStartWakelock(newHold.mUid,
7528 "window",
7529 BatteryStats.WAKE_TYPE_WINDOW);
7530 }
7531 } catch (RemoteException e) {
7532 }
7533 }
7534 break;
7535 }
Romain Guy06882f82009-06-10 13:36:04 -07007536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 case APP_TRANSITION_TIMEOUT: {
7538 synchronized (mWindowMap) {
7539 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7540 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7541 "*** APP TRANSITION TIMEOUT");
7542 mAppTransitionReady = true;
7543 mAppTransitionTimeout = true;
7544 performLayoutAndPlaceSurfacesLocked();
7545 }
7546 }
7547 break;
7548 }
Romain Guy06882f82009-06-10 13:36:04 -07007549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007550 case PERSIST_ANIMATION_SCALE: {
7551 Settings.System.putFloat(mContext.getContentResolver(),
7552 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7553 Settings.System.putFloat(mContext.getContentResolver(),
7554 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7555 break;
7556 }
Romain Guy06882f82009-06-10 13:36:04 -07007557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 case FORCE_GC: {
7559 synchronized(mWindowMap) {
7560 if (mAnimationPending) {
7561 // If we are animating, don't do the gc now but
7562 // delay a bit so we don't interrupt the animation.
7563 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7564 2000);
7565 return;
7566 }
7567 // If we are currently rotating the display, it will
7568 // schedule a new message when done.
7569 if (mDisplayFrozen) {
7570 return;
7571 }
7572 mFreezeGcPending = 0;
7573 }
7574 Runtime.getRuntime().gc();
7575 break;
7576 }
Romain Guy06882f82009-06-10 13:36:04 -07007577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 case ENABLE_SCREEN: {
7579 performEnableScreen();
7580 break;
7581 }
Romain Guy06882f82009-06-10 13:36:04 -07007582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 case APP_FREEZE_TIMEOUT: {
7584 synchronized (mWindowMap) {
7585 Log.w(TAG, "App freeze timeout expired.");
7586 int i = mAppTokens.size();
7587 while (i > 0) {
7588 i--;
7589 AppWindowToken tok = mAppTokens.get(i);
7590 if (tok.freezingScreen) {
7591 Log.w(TAG, "Force clearing freeze: " + tok);
7592 unsetAppFreezingScreenLocked(tok, true, true);
7593 }
7594 }
7595 }
7596 break;
7597 }
Romain Guy06882f82009-06-10 13:36:04 -07007598
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007599 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007600 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007601 sendNewConfiguration();
7602 }
7603 break;
7604 }
Romain Guy06882f82009-06-10 13:36:04 -07007605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 }
7607 }
7608 }
7609
7610 // -------------------------------------------------------------
7611 // IWindowManager API
7612 // -------------------------------------------------------------
7613
7614 public IWindowSession openSession(IInputMethodClient client,
7615 IInputContext inputContext) {
7616 if (client == null) throw new IllegalArgumentException("null client");
7617 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7618 return new Session(client, inputContext);
7619 }
7620
7621 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7622 synchronized (mWindowMap) {
7623 // The focus for the client is the window immediately below
7624 // where we would place the input method window.
7625 int idx = findDesiredInputMethodWindowIndexLocked(false);
7626 WindowState imFocus;
7627 if (idx > 0) {
7628 imFocus = (WindowState)mWindows.get(idx-1);
7629 if (imFocus != null) {
7630 if (imFocus.mSession.mClient != null &&
7631 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7632 return true;
7633 }
7634 }
7635 }
7636 }
7637 return false;
7638 }
Romain Guy06882f82009-06-10 13:36:04 -07007639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640 // -------------------------------------------------------------
7641 // Internals
7642 // -------------------------------------------------------------
7643
7644 final WindowState windowForClientLocked(Session session, IWindow client) {
7645 return windowForClientLocked(session, client.asBinder());
7646 }
Romain Guy06882f82009-06-10 13:36:04 -07007647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 final WindowState windowForClientLocked(Session session, IBinder client) {
7649 WindowState win = mWindowMap.get(client);
7650 if (localLOGV) Log.v(
7651 TAG, "Looking up client " + client + ": " + win);
7652 if (win == null) {
7653 RuntimeException ex = new RuntimeException();
7654 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7655 return null;
7656 }
7657 if (session != null && win.mSession != session) {
7658 RuntimeException ex = new RuntimeException();
7659 Log.w(TAG, "Requested window " + client + " is in session " +
7660 win.mSession + ", not " + session, ex);
7661 return null;
7662 }
7663
7664 return win;
7665 }
7666
7667 private final void assignLayersLocked() {
7668 int N = mWindows.size();
7669 int curBaseLayer = 0;
7670 int curLayer = 0;
7671 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 for (i=0; i<N; i++) {
7674 WindowState w = (WindowState)mWindows.get(i);
7675 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7676 curLayer += WINDOW_LAYER_MULTIPLIER;
7677 w.mLayer = curLayer;
7678 } else {
7679 curBaseLayer = curLayer = w.mBaseLayer;
7680 w.mLayer = curLayer;
7681 }
7682 if (w.mTargetAppToken != null) {
7683 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7684 } else if (w.mAppToken != null) {
7685 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7686 } else {
7687 w.mAnimLayer = w.mLayer;
7688 }
7689 if (w.mIsImWindow) {
7690 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7691 }
7692 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7693 + w.mAnimLayer);
7694 //System.out.println(
7695 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7696 }
7697 }
7698
7699 private boolean mInLayout = false;
7700 private final void performLayoutAndPlaceSurfacesLocked() {
7701 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007702 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 throw new RuntimeException("Recursive call!");
7704 }
7705 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7706 return;
7707 }
7708
7709 boolean recoveringMemory = false;
7710 if (mForceRemoves != null) {
7711 recoveringMemory = true;
7712 // Wait a little it for things to settle down, and off we go.
7713 for (int i=0; i<mForceRemoves.size(); i++) {
7714 WindowState ws = mForceRemoves.get(i);
7715 Log.i(TAG, "Force removing: " + ws);
7716 removeWindowInnerLocked(ws.mSession, ws);
7717 }
7718 mForceRemoves = null;
7719 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7720 Object tmp = new Object();
7721 synchronized (tmp) {
7722 try {
7723 tmp.wait(250);
7724 } catch (InterruptedException e) {
7725 }
7726 }
7727 }
Romain Guy06882f82009-06-10 13:36:04 -07007728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 mInLayout = true;
7730 try {
7731 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733 int i = mPendingRemove.size()-1;
7734 if (i >= 0) {
7735 while (i >= 0) {
7736 WindowState w = mPendingRemove.get(i);
7737 removeWindowInnerLocked(w.mSession, w);
7738 i--;
7739 }
7740 mPendingRemove.clear();
7741
7742 mInLayout = false;
7743 assignLayersLocked();
7744 mLayoutNeeded = true;
7745 performLayoutAndPlaceSurfacesLocked();
7746
7747 } else {
7748 mInLayout = false;
7749 if (mLayoutNeeded) {
7750 requestAnimationLocked(0);
7751 }
7752 }
7753 } catch (RuntimeException e) {
7754 mInLayout = false;
7755 Log.e(TAG, "Unhandled exception while layout out windows", e);
7756 }
7757 }
7758
7759 private final void performLayoutLockedInner() {
7760 final int dw = mDisplay.getWidth();
7761 final int dh = mDisplay.getHeight();
7762
7763 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007764 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 int i;
7766
7767 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007768
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007769 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 mPolicy.beginLayoutLw(dw, dh);
7771
7772 // First perform layout of any root windows (not attached
7773 // to another window).
7774 int topAttached = -1;
7775 for (i = N-1; i >= 0; i--) {
7776 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007777
7778 // Don't do layout of a window if it is not visible, or
7779 // soon won't be visible, to avoid wasting time and funky
7780 // changes while a window is animating away.
7781 final AppWindowToken atoken = win.mAppToken;
7782 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007784 || win.mRootToken.hidden
7785 || (atoken != null && atoken.hiddenRequested)
7786 || !win.mPolicyVisibility
7787 || win.mAttachedHidden
7788 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789
7790 // If this view is GONE, then skip it -- keep the current
7791 // frame, and let the caller know so they can ignore it
7792 // if they want. (We do the normal layout for INVISIBLE
7793 // windows, since that means "perform layout as normal,
7794 // just don't display").
7795 if (!gone || !win.mHaveFrame) {
7796 if (!win.mLayoutAttached) {
7797 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7798 } else {
7799 if (topAttached < 0) topAttached = i;
7800 }
7801 }
7802 }
Romain Guy06882f82009-06-10 13:36:04 -07007803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 // Now perform layout of attached windows, which usually
7805 // depend on the position of the window they are attached to.
7806 // XXX does not deal with windows that are attached to windows
7807 // that are themselves attached.
7808 for (i = topAttached; i >= 0; i--) {
7809 WindowState win = (WindowState) mWindows.get(i);
7810
7811 // If this view is GONE, then skip it -- keep the current
7812 // frame, and let the caller know so they can ignore it
7813 // if they want. (We do the normal layout for INVISIBLE
7814 // windows, since that means "perform layout as normal,
7815 // just don't display").
7816 if (win.mLayoutAttached) {
7817 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7818 || !win.mHaveFrame) {
7819 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7820 }
7821 }
7822 }
7823
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007824 if (!mPolicy.finishLayoutLw()) {
7825 mLayoutNeeded = false;
7826 } else if (repeats > 2) {
7827 Log.w(TAG, "Layout repeat aborted after too many iterations");
7828 mLayoutNeeded = false;
7829 } else {
7830 repeats++;
7831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 }
7833 }
Romain Guy06882f82009-06-10 13:36:04 -07007834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 private final void performLayoutAndPlaceSurfacesLockedInner(
7836 boolean recoveringMemory) {
7837 final long currentTime = SystemClock.uptimeMillis();
7838 final int dw = mDisplay.getWidth();
7839 final int dh = mDisplay.getHeight();
7840
7841 final int N = mWindows.size();
7842 int i;
7843
7844 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 if (mFxSession == null) {
7848 mFxSession = new SurfaceSession();
7849 }
Romain Guy06882f82009-06-10 13:36:04 -07007850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7852
7853 // Initialize state of exiting tokens.
7854 for (i=mExitingTokens.size()-1; i>=0; i--) {
7855 mExitingTokens.get(i).hasVisible = false;
7856 }
7857
7858 // Initialize state of exiting applications.
7859 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7860 mExitingAppTokens.get(i).hasVisible = false;
7861 }
7862
7863 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 boolean orientationChangeComplete = true;
7865 Session holdScreen = null;
7866 float screenBrightness = -1;
7867 boolean focusDisplayed = false;
7868 boolean animating = false;
7869
7870 Surface.openTransaction();
7871 try {
7872 boolean restart;
7873
7874 do {
7875 final int transactionSequence = ++mTransactionSequence;
7876
7877 // Update animations of all applications, including those
7878 // associated with exiting/removed apps
7879 boolean tokensAnimating = false;
7880 final int NAT = mAppTokens.size();
7881 for (i=0; i<NAT; i++) {
7882 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7883 tokensAnimating = true;
7884 }
7885 }
7886 final int NEAT = mExitingAppTokens.size();
7887 for (i=0; i<NEAT; i++) {
7888 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7889 tokensAnimating = true;
7890 }
7891 }
7892
7893 animating = tokensAnimating;
7894 restart = false;
7895
7896 boolean tokenMayBeDrawn = false;
7897
7898 mPolicy.beginAnimationLw(dw, dh);
7899
7900 for (i=N-1; i>=0; i--) {
7901 WindowState w = (WindowState)mWindows.get(i);
7902
7903 final WindowManager.LayoutParams attrs = w.mAttrs;
7904
7905 if (w.mSurface != null) {
7906 // Execute animation.
7907 w.commitFinishDrawingLocked(currentTime);
7908 if (w.stepAnimationLocked(currentTime, dw, dh)) {
7909 animating = true;
7910 //w.dump(" ");
7911 }
7912
7913 mPolicy.animatingWindowLw(w, attrs);
7914 }
7915
7916 final AppWindowToken atoken = w.mAppToken;
7917 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
7918 if (atoken.lastTransactionSequence != transactionSequence) {
7919 atoken.lastTransactionSequence = transactionSequence;
7920 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
7921 atoken.startingDisplayed = false;
7922 }
7923 if ((w.isOnScreen() || w.mAttrs.type
7924 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
7925 && !w.mExiting && !w.mDestroying) {
7926 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
7927 Log.v(TAG, "Eval win " + w + ": isDisplayed="
7928 + w.isDisplayedLw()
7929 + ", isAnimating=" + w.isAnimating());
7930 if (!w.isDisplayedLw()) {
7931 Log.v(TAG, "Not displayed: s=" + w.mSurface
7932 + " pv=" + w.mPolicyVisibility
7933 + " dp=" + w.mDrawPending
7934 + " cdp=" + w.mCommitDrawPending
7935 + " ah=" + w.mAttachedHidden
7936 + " th=" + atoken.hiddenRequested
7937 + " a=" + w.mAnimating);
7938 }
7939 }
7940 if (w != atoken.startingWindow) {
7941 if (!atoken.freezingScreen || !w.mAppFreezing) {
7942 atoken.numInterestingWindows++;
7943 if (w.isDisplayedLw()) {
7944 atoken.numDrawnWindows++;
7945 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
7946 "tokenMayBeDrawn: " + atoken
7947 + " freezingScreen=" + atoken.freezingScreen
7948 + " mAppFreezing=" + w.mAppFreezing);
7949 tokenMayBeDrawn = true;
7950 }
7951 }
7952 } else if (w.isDisplayedLw()) {
7953 atoken.startingDisplayed = true;
7954 }
7955 }
7956 } else if (w.mReadyToShow) {
7957 w.performShowLocked();
7958 }
7959 }
7960
7961 if (mPolicy.finishAnimationLw()) {
7962 restart = true;
7963 }
7964
7965 if (tokenMayBeDrawn) {
7966 // See if any windows have been drawn, so they (and others
7967 // associated with them) can now be shown.
7968 final int NT = mTokenList.size();
7969 for (i=0; i<NT; i++) {
7970 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
7971 if (wtoken == null) {
7972 continue;
7973 }
7974 if (wtoken.freezingScreen) {
7975 int numInteresting = wtoken.numInterestingWindows;
7976 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
7977 if (DEBUG_VISIBILITY) Log.v(TAG,
7978 "allDrawn: " + wtoken
7979 + " interesting=" + numInteresting
7980 + " drawn=" + wtoken.numDrawnWindows);
7981 wtoken.showAllWindowsLocked();
7982 unsetAppFreezingScreenLocked(wtoken, false, true);
7983 orientationChangeComplete = true;
7984 }
7985 } else if (!wtoken.allDrawn) {
7986 int numInteresting = wtoken.numInterestingWindows;
7987 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
7988 if (DEBUG_VISIBILITY) Log.v(TAG,
7989 "allDrawn: " + wtoken
7990 + " interesting=" + numInteresting
7991 + " drawn=" + wtoken.numDrawnWindows);
7992 wtoken.allDrawn = true;
7993 restart = true;
7994
7995 // We can now show all of the drawn windows!
7996 if (!mOpeningApps.contains(wtoken)) {
7997 wtoken.showAllWindowsLocked();
7998 }
7999 }
8000 }
8001 }
8002 }
8003
8004 // If we are ready to perform an app transition, check through
8005 // all of the app tokens to be shown and see if they are ready
8006 // to go.
8007 if (mAppTransitionReady) {
8008 int NN = mOpeningApps.size();
8009 boolean goodToGo = true;
8010 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8011 "Checking " + NN + " opening apps (frozen="
8012 + mDisplayFrozen + " timeout="
8013 + mAppTransitionTimeout + ")...");
8014 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8015 // If the display isn't frozen, wait to do anything until
8016 // all of the apps are ready. Otherwise just go because
8017 // we'll unfreeze the display when everyone is ready.
8018 for (i=0; i<NN && goodToGo; i++) {
8019 AppWindowToken wtoken = mOpeningApps.get(i);
8020 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8021 "Check opening app" + wtoken + ": allDrawn="
8022 + wtoken.allDrawn + " startingDisplayed="
8023 + wtoken.startingDisplayed);
8024 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8025 && !wtoken.startingMoved) {
8026 goodToGo = false;
8027 }
8028 }
8029 }
8030 if (goodToGo) {
8031 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8032 int transit = mNextAppTransition;
8033 if (mSkipAppTransitionAnimation) {
8034 transit = WindowManagerPolicy.TRANSIT_NONE;
8035 }
8036 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8037 mAppTransitionReady = false;
8038 mAppTransitionTimeout = false;
8039 mStartingIconInTransition = false;
8040 mSkipAppTransitionAnimation = false;
8041
8042 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8043
8044 // We need to figure out which animation to use...
8045 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8046 mOpeningApps, mClosingApps);
8047
8048 NN = mOpeningApps.size();
8049 for (i=0; i<NN; i++) {
8050 AppWindowToken wtoken = mOpeningApps.get(i);
8051 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8052 "Now opening app" + wtoken);
8053 wtoken.reportedVisible = false;
8054 wtoken.inPendingTransaction = false;
8055 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8056 wtoken.updateReportedVisibilityLocked();
8057 wtoken.showAllWindowsLocked();
8058 }
8059 NN = mClosingApps.size();
8060 for (i=0; i<NN; i++) {
8061 AppWindowToken wtoken = mClosingApps.get(i);
8062 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8063 "Now closing app" + wtoken);
8064 wtoken.inPendingTransaction = false;
8065 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8066 wtoken.updateReportedVisibilityLocked();
8067 // Force the allDrawn flag, because we want to start
8068 // this guy's animations regardless of whether it's
8069 // gotten drawn.
8070 wtoken.allDrawn = true;
8071 }
8072
8073 mOpeningApps.clear();
8074 mClosingApps.clear();
8075
8076 // This has changed the visibility of windows, so perform
8077 // a new layout to get them all up-to-date.
8078 mLayoutNeeded = true;
8079 moveInputMethodWindowsIfNeededLocked(true);
8080 performLayoutLockedInner();
8081 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8082
8083 restart = true;
8084 }
8085 }
8086 } while (restart);
8087
8088 // THIRD LOOP: Update the surfaces of all windows.
8089
8090 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8091
8092 boolean obscured = false;
8093 boolean blurring = false;
8094 boolean dimming = false;
8095 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008096 boolean syswin = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097
8098 for (i=N-1; i>=0; i--) {
8099 WindowState w = (WindowState)mWindows.get(i);
8100
8101 boolean displayed = false;
8102 final WindowManager.LayoutParams attrs = w.mAttrs;
8103 final int attrFlags = attrs.flags;
8104
8105 if (w.mSurface != null) {
8106 w.computeShownFrameLocked();
8107 if (localLOGV) Log.v(
8108 TAG, "Placing surface #" + i + " " + w.mSurface
8109 + ": new=" + w.mShownFrame + ", old="
8110 + w.mLastShownFrame);
8111
8112 boolean resize;
8113 int width, height;
8114 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8115 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8116 w.mLastRequestedHeight != w.mRequestedHeight;
8117 // for a scaled surface, we just want to use
8118 // the requested size.
8119 width = w.mRequestedWidth;
8120 height = w.mRequestedHeight;
8121 w.mLastRequestedWidth = width;
8122 w.mLastRequestedHeight = height;
8123 w.mLastShownFrame.set(w.mShownFrame);
8124 try {
8125 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8126 } catch (RuntimeException e) {
8127 Log.w(TAG, "Error positioning surface in " + w, e);
8128 if (!recoveringMemory) {
8129 reclaimSomeSurfaceMemoryLocked(w, "position");
8130 }
8131 }
8132 } else {
8133 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8134 width = w.mShownFrame.width();
8135 height = w.mShownFrame.height();
8136 w.mLastShownFrame.set(w.mShownFrame);
8137 if (resize) {
8138 if (SHOW_TRANSACTIONS) Log.i(
8139 TAG, " SURFACE " + w.mSurface + ": ("
8140 + w.mShownFrame.left + ","
8141 + w.mShownFrame.top + ") ("
8142 + w.mShownFrame.width() + "x"
8143 + w.mShownFrame.height() + ")");
8144 }
8145 }
8146
8147 if (resize) {
8148 if (width < 1) width = 1;
8149 if (height < 1) height = 1;
8150 if (w.mSurface != null) {
8151 try {
8152 w.mSurface.setSize(width, height);
8153 w.mSurface.setPosition(w.mShownFrame.left,
8154 w.mShownFrame.top);
8155 } catch (RuntimeException e) {
8156 // If something goes wrong with the surface (such
8157 // as running out of memory), don't take down the
8158 // entire system.
8159 Log.e(TAG, "Failure updating surface of " + w
8160 + "size=(" + width + "x" + height
8161 + "), pos=(" + w.mShownFrame.left
8162 + "," + w.mShownFrame.top + ")", e);
8163 if (!recoveringMemory) {
8164 reclaimSomeSurfaceMemoryLocked(w, "size");
8165 }
8166 }
8167 }
8168 }
8169 if (!w.mAppFreezing) {
8170 w.mContentInsetsChanged =
8171 !w.mLastContentInsets.equals(w.mContentInsets);
8172 w.mVisibleInsetsChanged =
8173 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008174 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 || w.mContentInsetsChanged
8176 || w.mVisibleInsetsChanged) {
8177 w.mLastFrame.set(w.mFrame);
8178 w.mLastContentInsets.set(w.mContentInsets);
8179 w.mLastVisibleInsets.set(w.mVisibleInsets);
8180 // If the orientation is changing, then we need to
8181 // hold off on unfreezing the display until this
8182 // window has been redrawn; to do that, we need
8183 // to go through the process of getting informed
8184 // by the application when it has finished drawing.
8185 if (w.mOrientationChanging) {
8186 if (DEBUG_ORIENTATION) Log.v(TAG,
8187 "Orientation start waiting for draw in "
8188 + w + ", surface " + w.mSurface);
8189 w.mDrawPending = true;
8190 w.mCommitDrawPending = false;
8191 w.mReadyToShow = false;
8192 if (w.mAppToken != null) {
8193 w.mAppToken.allDrawn = false;
8194 }
8195 }
Romain Guy06882f82009-06-10 13:36:04 -07008196 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 "Resizing window " + w + " to " + w.mFrame);
8198 mResizingWindows.add(w);
8199 } else if (w.mOrientationChanging) {
8200 if (!w.mDrawPending && !w.mCommitDrawPending) {
8201 if (DEBUG_ORIENTATION) Log.v(TAG,
8202 "Orientation not waiting for draw in "
8203 + w + ", surface " + w.mSurface);
8204 w.mOrientationChanging = false;
8205 }
8206 }
8207 }
8208
8209 if (w.mAttachedHidden) {
8210 if (!w.mLastHidden) {
8211 //dump();
8212 w.mLastHidden = true;
8213 if (SHOW_TRANSACTIONS) Log.i(
8214 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8215 if (w.mSurface != null) {
8216 try {
8217 w.mSurface.hide();
8218 } catch (RuntimeException e) {
8219 Log.w(TAG, "Exception hiding surface in " + w);
8220 }
8221 }
8222 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8223 }
8224 // If we are waiting for this window to handle an
8225 // orientation change, well, it is hidden, so
8226 // doesn't really matter. Note that this does
8227 // introduce a potential glitch if the window
8228 // becomes unhidden before it has drawn for the
8229 // new orientation.
8230 if (w.mOrientationChanging) {
8231 w.mOrientationChanging = false;
8232 if (DEBUG_ORIENTATION) Log.v(TAG,
8233 "Orientation change skips hidden " + w);
8234 }
8235 } else if (!w.isReadyForDisplay()) {
8236 if (!w.mLastHidden) {
8237 //dump();
8238 w.mLastHidden = true;
8239 if (SHOW_TRANSACTIONS) Log.i(
8240 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8241 if (w.mSurface != null) {
8242 try {
8243 w.mSurface.hide();
8244 } catch (RuntimeException e) {
8245 Log.w(TAG, "Exception exception hiding surface in " + w);
8246 }
8247 }
8248 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8249 }
8250 // If we are waiting for this window to handle an
8251 // orientation change, well, it is hidden, so
8252 // doesn't really matter. Note that this does
8253 // introduce a potential glitch if the window
8254 // becomes unhidden before it has drawn for the
8255 // new orientation.
8256 if (w.mOrientationChanging) {
8257 w.mOrientationChanging = false;
8258 if (DEBUG_ORIENTATION) Log.v(TAG,
8259 "Orientation change skips hidden " + w);
8260 }
8261 } else if (w.mLastLayer != w.mAnimLayer
8262 || w.mLastAlpha != w.mShownAlpha
8263 || w.mLastDsDx != w.mDsDx
8264 || w.mLastDtDx != w.mDtDx
8265 || w.mLastDsDy != w.mDsDy
8266 || w.mLastDtDy != w.mDtDy
8267 || w.mLastHScale != w.mHScale
8268 || w.mLastVScale != w.mVScale
8269 || w.mLastHidden) {
8270 displayed = true;
8271 w.mLastAlpha = w.mShownAlpha;
8272 w.mLastLayer = w.mAnimLayer;
8273 w.mLastDsDx = w.mDsDx;
8274 w.mLastDtDx = w.mDtDx;
8275 w.mLastDsDy = w.mDsDy;
8276 w.mLastDtDy = w.mDtDy;
8277 w.mLastHScale = w.mHScale;
8278 w.mLastVScale = w.mVScale;
8279 if (SHOW_TRANSACTIONS) Log.i(
8280 TAG, " SURFACE " + w.mSurface + ": alpha="
8281 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8282 if (w.mSurface != null) {
8283 try {
8284 w.mSurface.setAlpha(w.mShownAlpha);
8285 w.mSurface.setLayer(w.mAnimLayer);
8286 w.mSurface.setMatrix(
8287 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8288 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8289 } catch (RuntimeException e) {
8290 Log.w(TAG, "Error updating surface in " + w, e);
8291 if (!recoveringMemory) {
8292 reclaimSomeSurfaceMemoryLocked(w, "update");
8293 }
8294 }
8295 }
8296
8297 if (w.mLastHidden && !w.mDrawPending
8298 && !w.mCommitDrawPending
8299 && !w.mReadyToShow) {
8300 if (SHOW_TRANSACTIONS) Log.i(
8301 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8302 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8303 + " during relayout");
8304 if (showSurfaceRobustlyLocked(w)) {
8305 w.mHasDrawn = true;
8306 w.mLastHidden = false;
8307 } else {
8308 w.mOrientationChanging = false;
8309 }
8310 }
8311 if (w.mSurface != null) {
8312 w.mToken.hasVisible = true;
8313 }
8314 } else {
8315 displayed = true;
8316 }
8317
8318 if (displayed) {
8319 if (!covered) {
8320 if (attrs.width == LayoutParams.FILL_PARENT
8321 && attrs.height == LayoutParams.FILL_PARENT) {
8322 covered = true;
8323 }
8324 }
8325 if (w.mOrientationChanging) {
8326 if (w.mDrawPending || w.mCommitDrawPending) {
8327 orientationChangeComplete = false;
8328 if (DEBUG_ORIENTATION) Log.v(TAG,
8329 "Orientation continue waiting for draw in " + w);
8330 } else {
8331 w.mOrientationChanging = false;
8332 if (DEBUG_ORIENTATION) Log.v(TAG,
8333 "Orientation change complete in " + w);
8334 }
8335 }
8336 w.mToken.hasVisible = true;
8337 }
8338 } else if (w.mOrientationChanging) {
8339 if (DEBUG_ORIENTATION) Log.v(TAG,
8340 "Orientation change skips hidden " + w);
8341 w.mOrientationChanging = false;
8342 }
8343
8344 final boolean canBeSeen = w.isDisplayedLw();
8345
8346 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8347 focusDisplayed = true;
8348 }
8349
8350 // Update effect.
8351 if (!obscured) {
8352 if (w.mSurface != null) {
8353 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8354 holdScreen = w.mSession;
8355 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008356 if (!syswin && w.mAttrs.screenBrightness >= 0
8357 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 screenBrightness = w.mAttrs.screenBrightness;
8359 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008360 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8361 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8362 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8363 syswin = true;
8364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 }
8366 if (w.isFullscreenOpaque(dw, dh)) {
8367 // This window completely covers everything behind it,
8368 // so we want to leave all of them as unblurred (for
8369 // performance reasons).
8370 obscured = true;
8371 } else if (canBeSeen && !obscured &&
8372 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8373 if (localLOGV) Log.v(TAG, "Win " + w
8374 + ": blurring=" + blurring
8375 + " obscured=" + obscured
8376 + " displayed=" + displayed);
8377 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8378 if (!dimming) {
8379 //Log.i(TAG, "DIM BEHIND: " + w);
8380 dimming = true;
8381 mDimShown = true;
8382 if (mDimSurface == null) {
8383 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8384 + mDimSurface + ": CREATE");
8385 try {
Romain Guy06882f82009-06-10 13:36:04 -07008386 mDimSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 -1, 16, 16,
8388 PixelFormat.OPAQUE,
8389 Surface.FX_SURFACE_DIM);
8390 } catch (Exception e) {
8391 Log.e(TAG, "Exception creating Dim surface", e);
8392 }
8393 }
8394 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8395 + mDimSurface + ": SHOW pos=(0,0) (" +
8396 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8397 if (mDimSurface != null) {
8398 try {
8399 mDimSurface.setPosition(0, 0);
8400 mDimSurface.setSize(dw, dh);
8401 mDimSurface.show();
8402 } catch (RuntimeException e) {
8403 Log.w(TAG, "Failure showing dim surface", e);
8404 }
8405 }
8406 }
8407 mDimSurface.setLayer(w.mAnimLayer-1);
8408 final float target = w.mExiting ? 0 : attrs.dimAmount;
8409 if (mDimTargetAlpha != target) {
8410 // If the desired dim level has changed, then
8411 // start an animation to it.
8412 mLastDimAnimTime = currentTime;
8413 long duration = (w.mAnimating && w.mAnimation != null)
8414 ? w.mAnimation.computeDurationHint()
8415 : DEFAULT_DIM_DURATION;
8416 if (target > mDimTargetAlpha) {
8417 // This is happening behind the activity UI,
8418 // so we can make it run a little longer to
8419 // give a stronger impression without disrupting
8420 // the user.
8421 duration *= DIM_DURATION_MULTIPLIER;
8422 }
8423 if (duration < 1) {
8424 // Don't divide by zero
8425 duration = 1;
8426 }
8427 mDimTargetAlpha = target;
8428 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha)
8429 / duration;
8430 }
8431 }
8432 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8433 if (!blurring) {
8434 //Log.i(TAG, "BLUR BEHIND: " + w);
8435 blurring = true;
8436 mBlurShown = true;
8437 if (mBlurSurface == null) {
8438 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8439 + mBlurSurface + ": CREATE");
8440 try {
Romain Guy06882f82009-06-10 13:36:04 -07008441 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 -1, 16, 16,
8443 PixelFormat.OPAQUE,
8444 Surface.FX_SURFACE_BLUR);
8445 } catch (Exception e) {
8446 Log.e(TAG, "Exception creating Blur surface", e);
8447 }
8448 }
8449 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8450 + mBlurSurface + ": SHOW pos=(0,0) (" +
8451 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8452 if (mBlurSurface != null) {
8453 mBlurSurface.setPosition(0, 0);
8454 mBlurSurface.setSize(dw, dh);
8455 try {
8456 mBlurSurface.show();
8457 } catch (RuntimeException e) {
8458 Log.w(TAG, "Failure showing blur surface", e);
8459 }
8460 }
8461 }
8462 mBlurSurface.setLayer(w.mAnimLayer-2);
8463 }
8464 }
8465 }
8466 }
8467
8468 if (!dimming && mDimShown) {
8469 // Time to hide the dim surface... start fading.
8470 if (mDimTargetAlpha != 0) {
8471 mLastDimAnimTime = currentTime;
8472 mDimTargetAlpha = 0;
8473 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
8474 }
8475 }
8476
8477 if (mDimShown && mLastDimAnimTime != 0) {
8478 mDimCurrentAlpha += mDimDeltaPerMs
8479 * (currentTime-mLastDimAnimTime);
8480 boolean more = true;
8481 if (mDisplayFrozen) {
8482 // If the display is frozen, there is no reason to animate.
8483 more = false;
8484 } else if (mDimDeltaPerMs > 0) {
8485 if (mDimCurrentAlpha > mDimTargetAlpha) {
8486 more = false;
8487 }
8488 } else if (mDimDeltaPerMs < 0) {
8489 if (mDimCurrentAlpha < mDimTargetAlpha) {
8490 more = false;
8491 }
8492 } else {
8493 more = false;
8494 }
Romain Guy06882f82009-06-10 13:36:04 -07008495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 // Do we need to continue animating?
8497 if (more) {
8498 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8499 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
8500 mLastDimAnimTime = currentTime;
8501 mDimSurface.setAlpha(mDimCurrentAlpha);
8502 animating = true;
8503 } else {
8504 mDimCurrentAlpha = mDimTargetAlpha;
8505 mLastDimAnimTime = 0;
8506 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8507 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
8508 mDimSurface.setAlpha(mDimCurrentAlpha);
8509 if (!dimming) {
8510 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
8511 + ": HIDE");
8512 try {
8513 mDimSurface.hide();
8514 } catch (RuntimeException e) {
8515 Log.w(TAG, "Illegal argument exception hiding dim surface");
8516 }
8517 mDimShown = false;
8518 }
8519 }
8520 }
Romain Guy06882f82009-06-10 13:36:04 -07008521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008522 if (!blurring && mBlurShown) {
8523 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8524 + ": HIDE");
8525 try {
8526 mBlurSurface.hide();
8527 } catch (IllegalArgumentException e) {
8528 Log.w(TAG, "Illegal argument exception hiding blur surface");
8529 }
8530 mBlurShown = false;
8531 }
8532
8533 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8534 } catch (RuntimeException e) {
8535 Log.e(TAG, "Unhandled exception in Window Manager", e);
8536 }
8537
8538 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8541 "With display frozen, orientationChangeComplete="
8542 + orientationChangeComplete);
8543 if (orientationChangeComplete) {
8544 if (mWindowsFreezingScreen) {
8545 mWindowsFreezingScreen = false;
8546 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8547 }
8548 if (mAppsFreezingScreen == 0) {
8549 stopFreezingDisplayLocked();
8550 }
8551 }
Romain Guy06882f82009-06-10 13:36:04 -07008552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 i = mResizingWindows.size();
8554 if (i > 0) {
8555 do {
8556 i--;
8557 WindowState win = mResizingWindows.get(i);
8558 try {
8559 win.mClient.resized(win.mFrame.width(),
8560 win.mFrame.height(), win.mLastContentInsets,
8561 win.mLastVisibleInsets, win.mDrawPending);
8562 win.mContentInsetsChanged = false;
8563 win.mVisibleInsetsChanged = false;
8564 } catch (RemoteException e) {
8565 win.mOrientationChanging = false;
8566 }
8567 } while (i > 0);
8568 mResizingWindows.clear();
8569 }
Romain Guy06882f82009-06-10 13:36:04 -07008570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 // Destroy the surface of any windows that are no longer visible.
8572 i = mDestroySurface.size();
8573 if (i > 0) {
8574 do {
8575 i--;
8576 WindowState win = mDestroySurface.get(i);
8577 win.mDestroying = false;
8578 if (mInputMethodWindow == win) {
8579 mInputMethodWindow = null;
8580 }
8581 win.destroySurfaceLocked();
8582 } while (i > 0);
8583 mDestroySurface.clear();
8584 }
8585
8586 // Time to remove any exiting tokens?
8587 for (i=mExitingTokens.size()-1; i>=0; i--) {
8588 WindowToken token = mExitingTokens.get(i);
8589 if (!token.hasVisible) {
8590 mExitingTokens.remove(i);
8591 }
8592 }
8593
8594 // Time to remove any exiting applications?
8595 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8596 AppWindowToken token = mExitingAppTokens.get(i);
8597 if (!token.hasVisible && !mClosingApps.contains(token)) {
8598 mAppTokens.remove(token);
8599 mExitingAppTokens.remove(i);
8600 }
8601 }
8602
8603 if (focusDisplayed) {
8604 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8605 }
8606 if (animating) {
8607 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8608 }
8609 mQueue.setHoldScreenLocked(holdScreen != null);
8610 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8611 mPowerManager.setScreenBrightnessOverride(-1);
8612 } else {
8613 mPowerManager.setScreenBrightnessOverride((int)
8614 (screenBrightness * Power.BRIGHTNESS_ON));
8615 }
8616 if (holdScreen != mHoldingScreenOn) {
8617 mHoldingScreenOn = holdScreen;
8618 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8619 mH.sendMessage(m);
8620 }
8621 }
8622
8623 void requestAnimationLocked(long delay) {
8624 if (!mAnimationPending) {
8625 mAnimationPending = true;
8626 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8627 }
8628 }
Romain Guy06882f82009-06-10 13:36:04 -07008629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008630 /**
8631 * Have the surface flinger show a surface, robustly dealing with
8632 * error conditions. In particular, if there is not enough memory
8633 * to show the surface, then we will try to get rid of other surfaces
8634 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008635 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 * @return Returns true if the surface was successfully shown.
8637 */
8638 boolean showSurfaceRobustlyLocked(WindowState win) {
8639 try {
8640 if (win.mSurface != null) {
8641 win.mSurface.show();
8642 }
8643 return true;
8644 } catch (RuntimeException e) {
8645 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8646 }
Romain Guy06882f82009-06-10 13:36:04 -07008647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008650 return false;
8651 }
Romain Guy06882f82009-06-10 13:36:04 -07008652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8654 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008656 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8657 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 if (mForceRemoves == null) {
8660 mForceRemoves = new ArrayList<WindowState>();
8661 }
Romain Guy06882f82009-06-10 13:36:04 -07008662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 long callingIdentity = Binder.clearCallingIdentity();
8664 try {
8665 // There was some problem... first, do a sanity check of the
8666 // window list to make sure we haven't left any dangling surfaces
8667 // around.
8668 int N = mWindows.size();
8669 boolean leakedSurface = false;
8670 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8671 for (int i=0; i<N; i++) {
8672 WindowState ws = (WindowState)mWindows.get(i);
8673 if (ws.mSurface != null) {
8674 if (!mSessions.contains(ws.mSession)) {
8675 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8676 + ws + " surface=" + ws.mSurface
8677 + " token=" + win.mToken
8678 + " pid=" + ws.mSession.mPid
8679 + " uid=" + ws.mSession.mUid);
8680 ws.mSurface.clear();
8681 ws.mSurface = null;
8682 mForceRemoves.add(ws);
8683 i--;
8684 N--;
8685 leakedSurface = true;
8686 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8687 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8688 + ws + " surface=" + ws.mSurface
8689 + " token=" + win.mAppToken);
8690 ws.mSurface.clear();
8691 ws.mSurface = null;
8692 leakedSurface = true;
8693 }
8694 }
8695 }
Romain Guy06882f82009-06-10 13:36:04 -07008696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 boolean killedApps = false;
8698 if (!leakedSurface) {
8699 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8700 SparseIntArray pidCandidates = new SparseIntArray();
8701 for (int i=0; i<N; i++) {
8702 WindowState ws = (WindowState)mWindows.get(i);
8703 if (ws.mSurface != null) {
8704 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8705 }
8706 }
8707 if (pidCandidates.size() > 0) {
8708 int[] pids = new int[pidCandidates.size()];
8709 for (int i=0; i<pids.length; i++) {
8710 pids[i] = pidCandidates.keyAt(i);
8711 }
8712 try {
8713 if (mActivityManager.killPidsForMemory(pids)) {
8714 killedApps = true;
8715 }
8716 } catch (RemoteException e) {
8717 }
8718 }
8719 }
Romain Guy06882f82009-06-10 13:36:04 -07008720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 if (leakedSurface || killedApps) {
8722 // We managed to reclaim some memory, so get rid of the trouble
8723 // surface and ask the app to request another one.
8724 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8725 if (surface != null) {
8726 surface.clear();
8727 win.mSurface = null;
8728 }
Romain Guy06882f82009-06-10 13:36:04 -07008729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 try {
8731 win.mClient.dispatchGetNewSurface();
8732 } catch (RemoteException e) {
8733 }
8734 }
8735 } finally {
8736 Binder.restoreCallingIdentity(callingIdentity);
8737 }
8738 }
Romain Guy06882f82009-06-10 13:36:04 -07008739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 private boolean updateFocusedWindowLocked(int mode) {
8741 WindowState newFocus = computeFocusedWindowLocked();
8742 if (mCurrentFocus != newFocus) {
8743 // This check makes sure that we don't already have the focus
8744 // change message pending.
8745 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8746 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8747 if (localLOGV) Log.v(
8748 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8749 final WindowState oldFocus = mCurrentFocus;
8750 mCurrentFocus = newFocus;
8751 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 final WindowState imWindow = mInputMethodWindow;
8754 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008755 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008757 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8758 mLayoutNeeded = true;
8759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008760 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8761 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008762 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8763 // Client will do the layout, but we need to assign layers
8764 // for handleNewWindowLocked() below.
8765 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 }
8767 }
Romain Guy06882f82009-06-10 13:36:04 -07008768
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008769 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8770 mKeyWaiter.handleNewWindowLocked(newFocus);
8771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 return true;
8773 }
8774 return false;
8775 }
8776
8777 private WindowState computeFocusedWindowLocked() {
8778 WindowState result = null;
8779 WindowState win;
8780
8781 int i = mWindows.size() - 1;
8782 int nextAppIndex = mAppTokens.size()-1;
8783 WindowToken nextApp = nextAppIndex >= 0
8784 ? mAppTokens.get(nextAppIndex) : null;
8785
8786 while (i >= 0) {
8787 win = (WindowState)mWindows.get(i);
8788
8789 if (localLOGV || DEBUG_FOCUS) Log.v(
8790 TAG, "Looking for focus: " + i
8791 + " = " + win
8792 + ", flags=" + win.mAttrs.flags
8793 + ", canReceive=" + win.canReceiveKeys());
8794
8795 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 // If this window's application has been removed, just skip it.
8798 if (thisApp != null && thisApp.removed) {
8799 i--;
8800 continue;
8801 }
Romain Guy06882f82009-06-10 13:36:04 -07008802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 // If there is a focused app, don't allow focus to go to any
8804 // windows below it. If this is an application window, step
8805 // through the app tokens until we find its app.
8806 if (thisApp != null && nextApp != null && thisApp != nextApp
8807 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8808 int origAppIndex = nextAppIndex;
8809 while (nextAppIndex > 0) {
8810 if (nextApp == mFocusedApp) {
8811 // Whoops, we are below the focused app... no focus
8812 // for you!
8813 if (localLOGV || DEBUG_FOCUS) Log.v(
8814 TAG, "Reached focused app: " + mFocusedApp);
8815 return null;
8816 }
8817 nextAppIndex--;
8818 nextApp = mAppTokens.get(nextAppIndex);
8819 if (nextApp == thisApp) {
8820 break;
8821 }
8822 }
8823 if (thisApp != nextApp) {
8824 // Uh oh, the app token doesn't exist! This shouldn't
8825 // happen, but if it does we can get totally hosed...
8826 // so restart at the original app.
8827 nextAppIndex = origAppIndex;
8828 nextApp = mAppTokens.get(nextAppIndex);
8829 }
8830 }
8831
8832 // Dispatch to this window if it is wants key events.
8833 if (win.canReceiveKeys()) {
8834 if (DEBUG_FOCUS) Log.v(
8835 TAG, "Found focus @ " + i + " = " + win);
8836 result = win;
8837 break;
8838 }
8839
8840 i--;
8841 }
8842
8843 return result;
8844 }
8845
8846 private void startFreezingDisplayLocked() {
8847 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008848 // Freezing the display also suspends key event delivery, to
8849 // keep events from going astray while the display is reconfigured.
8850 // If someone has changed orientation again while the screen is
8851 // still frozen, the events will continue to be blocked while the
8852 // successive orientation change is processed. To prevent spurious
8853 // ANRs, we reset the event dispatch timeout in this case.
8854 synchronized (mKeyWaiter) {
8855 mKeyWaiter.mWasFrozen = true;
8856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 return;
8858 }
Romain Guy06882f82009-06-10 13:36:04 -07008859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008862 long now = SystemClock.uptimeMillis();
8863 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8864 if (mFreezeGcPending != 0) {
8865 if (now > (mFreezeGcPending+1000)) {
8866 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8867 mH.removeMessages(H.FORCE_GC);
8868 Runtime.getRuntime().gc();
8869 mFreezeGcPending = now;
8870 }
8871 } else {
8872 mFreezeGcPending = now;
8873 }
Romain Guy06882f82009-06-10 13:36:04 -07008874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008875 mDisplayFrozen = true;
8876 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8877 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8878 mAppTransitionReady = true;
8879 }
Romain Guy06882f82009-06-10 13:36:04 -07008880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 if (PROFILE_ORIENTATION) {
8882 File file = new File("/data/system/frozen");
8883 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8884 }
8885 Surface.freezeDisplay(0);
8886 }
Romain Guy06882f82009-06-10 13:36:04 -07008887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008888 private void stopFreezingDisplayLocked() {
8889 if (!mDisplayFrozen) {
8890 return;
8891 }
Romain Guy06882f82009-06-10 13:36:04 -07008892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008893 mDisplayFrozen = false;
8894 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8895 if (PROFILE_ORIENTATION) {
8896 Debug.stopMethodTracing();
8897 }
8898 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008899
Chris Tate2ad63a92009-03-25 17:36:48 -07008900 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8901 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008902 synchronized (mKeyWaiter) {
8903 mKeyWaiter.mWasFrozen = true;
8904 mKeyWaiter.notifyAll();
8905 }
8906
8907 // A little kludge: a lot could have happened while the
8908 // display was frozen, so now that we are coming back we
8909 // do a gc so that any remote references the system
8910 // processes holds on others can be released if they are
8911 // no longer needed.
8912 mH.removeMessages(H.FORCE_GC);
8913 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8914 2000);
Romain Guy06882f82009-06-10 13:36:04 -07008915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 mScreenFrozenLock.release();
8917 }
Romain Guy06882f82009-06-10 13:36:04 -07008918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 @Override
8920 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
8921 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
8922 != PackageManager.PERMISSION_GRANTED) {
8923 pw.println("Permission Denial: can't dump WindowManager from from pid="
8924 + Binder.getCallingPid()
8925 + ", uid=" + Binder.getCallingUid());
8926 return;
8927 }
Romain Guy06882f82009-06-10 13:36:04 -07008928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008929 synchronized(mWindowMap) {
8930 pw.println("Current Window Manager state:");
8931 for (int i=mWindows.size()-1; i>=0; i--) {
8932 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008933 pw.print(" Window #"); pw.print(i); pw.print(' ');
8934 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008935 w.dump(pw, " ");
8936 }
8937 if (mInputMethodDialogs.size() > 0) {
8938 pw.println(" ");
8939 pw.println(" Input method dialogs:");
8940 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
8941 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008942 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 }
8944 }
8945 if (mPendingRemove.size() > 0) {
8946 pw.println(" ");
8947 pw.println(" Remove pending for:");
8948 for (int i=mPendingRemove.size()-1; i>=0; i--) {
8949 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008950 pw.print(" Remove #"); pw.print(i); pw.print(' ');
8951 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 w.dump(pw, " ");
8953 }
8954 }
8955 if (mForceRemoves != null && mForceRemoves.size() > 0) {
8956 pw.println(" ");
8957 pw.println(" Windows force removing:");
8958 for (int i=mForceRemoves.size()-1; i>=0; i--) {
8959 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008960 pw.print(" Removing #"); pw.print(i); pw.print(' ');
8961 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 w.dump(pw, " ");
8963 }
8964 }
8965 if (mDestroySurface.size() > 0) {
8966 pw.println(" ");
8967 pw.println(" Windows waiting to destroy their surface:");
8968 for (int i=mDestroySurface.size()-1; i>=0; i--) {
8969 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008970 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
8971 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008972 w.dump(pw, " ");
8973 }
8974 }
8975 if (mLosingFocus.size() > 0) {
8976 pw.println(" ");
8977 pw.println(" Windows losing focus:");
8978 for (int i=mLosingFocus.size()-1; i>=0; i--) {
8979 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008980 pw.print(" Losing #"); pw.print(i); pw.print(' ');
8981 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008982 w.dump(pw, " ");
8983 }
8984 }
8985 if (mSessions.size() > 0) {
8986 pw.println(" ");
8987 pw.println(" All active sessions:");
8988 Iterator<Session> it = mSessions.iterator();
8989 while (it.hasNext()) {
8990 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008991 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008992 s.dump(pw, " ");
8993 }
8994 }
8995 if (mTokenMap.size() > 0) {
8996 pw.println(" ");
8997 pw.println(" All tokens:");
8998 Iterator<WindowToken> it = mTokenMap.values().iterator();
8999 while (it.hasNext()) {
9000 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009001 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002 token.dump(pw, " ");
9003 }
9004 }
9005 if (mTokenList.size() > 0) {
9006 pw.println(" ");
9007 pw.println(" Window token list:");
9008 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009009 pw.print(" #"); pw.print(i); pw.print(": ");
9010 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011 }
9012 }
9013 if (mAppTokens.size() > 0) {
9014 pw.println(" ");
9015 pw.println(" Application tokens in Z order:");
9016 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009017 pw.print(" App #"); pw.print(i); pw.print(": ");
9018 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 }
9020 }
9021 if (mFinishedStarting.size() > 0) {
9022 pw.println(" ");
9023 pw.println(" Finishing start of application tokens:");
9024 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9025 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009026 pw.print(" Finished Starting #"); pw.print(i);
9027 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 token.dump(pw, " ");
9029 }
9030 }
9031 if (mExitingTokens.size() > 0) {
9032 pw.println(" ");
9033 pw.println(" Exiting tokens:");
9034 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9035 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009036 pw.print(" Exiting #"); pw.print(i);
9037 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038 token.dump(pw, " ");
9039 }
9040 }
9041 if (mExitingAppTokens.size() > 0) {
9042 pw.println(" ");
9043 pw.println(" Exiting application tokens:");
9044 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9045 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009046 pw.print(" Exiting App #"); pw.print(i);
9047 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009048 token.dump(pw, " ");
9049 }
9050 }
9051 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009052 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9053 pw.print(" mLastFocus="); pw.println(mLastFocus);
9054 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9055 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9056 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9057 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9058 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9059 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9060 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9061 pw.print(" mBlurShown="); pw.println(mBlurShown);
9062 pw.print(" mDimShown="); pw.print(mDimShown);
9063 pw.print(" current="); pw.print(mDimCurrentAlpha);
9064 pw.print(" target="); pw.print(mDimTargetAlpha);
9065 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9066 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9067 pw.print(" mInputMethodAnimLayerAdjustment=");
9068 pw.println(mInputMethodAnimLayerAdjustment);
9069 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9070 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9071 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9072 pw.print(" mRotation="); pw.print(mRotation);
9073 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9074 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9075 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9076 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9077 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9078 pw.print(" mNextAppTransition=0x");
9079 pw.print(Integer.toHexString(mNextAppTransition));
9080 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9081 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9082 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9083 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9084 if (mOpeningApps.size() > 0) {
9085 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9086 }
9087 if (mClosingApps.size() > 0) {
9088 pw.print(" mClosingApps="); pw.println(mClosingApps);
9089 }
9090 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9091 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009093 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9094 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9095 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9096 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9097 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9098 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 }
9100 }
9101
9102 public void monitor() {
9103 synchronized (mWindowMap) { }
9104 synchronized (mKeyguardDisabled) { }
9105 synchronized (mKeyWaiter) { }
9106 }
9107}