blob: 828b8aabf33cba68bcdea3d700b8f246afc7b15a [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.EventLog;
81import android.util.Log;
82import android.util.SparseIntArray;
83import android.view.Display;
84import android.view.Gravity;
85import android.view.IApplicationToken;
86import android.view.IOnKeyguardExitResult;
87import android.view.IRotationWatcher;
88import android.view.IWindow;
89import android.view.IWindowManager;
90import android.view.IWindowSession;
91import android.view.KeyEvent;
92import android.view.MotionEvent;
93import android.view.RawInputEvent;
94import android.view.Surface;
95import android.view.SurfaceSession;
96import android.view.View;
97import android.view.ViewTreeObserver;
98import android.view.WindowManager;
99import android.view.WindowManagerImpl;
100import android.view.WindowManagerPolicy;
101import android.view.WindowManager.LayoutParams;
102import android.view.animation.Animation;
103import android.view.animation.AnimationUtils;
104import android.view.animation.Transformation;
105
106import java.io.BufferedWriter;
107import java.io.File;
108import java.io.FileDescriptor;
109import java.io.IOException;
110import java.io.OutputStream;
111import java.io.OutputStreamWriter;
112import java.io.PrintWriter;
113import java.io.StringWriter;
114import java.net.Socket;
115import java.util.ArrayList;
116import java.util.HashMap;
117import java.util.HashSet;
118import java.util.Iterator;
119import java.util.List;
120
121/** {@hide} */
122public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor {
123 static final String TAG = "WindowManager";
124 static final boolean DEBUG = false;
125 static final boolean DEBUG_FOCUS = false;
126 static final boolean DEBUG_ANIM = false;
127 static final boolean DEBUG_LAYERS = false;
128 static final boolean DEBUG_INPUT = false;
129 static final boolean DEBUG_INPUT_METHOD = false;
130 static final boolean DEBUG_VISIBILITY = false;
131 static final boolean DEBUG_ORIENTATION = false;
132 static final boolean DEBUG_APP_TRANSITIONS = false;
133 static final boolean DEBUG_STARTING_WINDOW = false;
134 static final boolean DEBUG_REORDER = false;
135 static final boolean SHOW_TRANSACTIONS = false;
Romain Guy06882f82009-06-10 13:36:04 -0700136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final boolean PROFILE_ORIENTATION = false;
138 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700139 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /** How long to wait for first key repeat, in milliseconds */
144 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 /** How long to wait for subsequent key repeats, in milliseconds */
147 static final int KEY_REPEAT_DELAY = 50;
148
149 /** How much to multiply the policy's type layer, to reserve room
150 * for multiple windows of the same type and Z-ordering adjustment
151 * with TYPE_LAYER_OFFSET. */
152 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
155 * or below others in the same layer. */
156 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 /** How much to increment the layer for each window, to reserve room
159 * for effect surfaces between them.
160 */
161 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /** The maximum length we will accept for a loaded animation duration:
164 * this is 10 seconds.
165 */
166 static final int MAX_ANIMATION_DURATION = 10*1000;
167
168 /** Amount of time (in milliseconds) to animate the dim surface from one
169 * value to another, when no window animation is driving it.
170 */
171 static final int DEFAULT_DIM_DURATION = 200;
172
173 /** Adjustment to time to perform a dim, to make it more dramatic.
174 */
175 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 static final int UPDATE_FOCUS_NORMAL = 0;
178 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
179 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
180 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700181
Michael Chane96440f2009-05-06 10:27:36 -0700182 /** The minimum time between dispatching touch events. */
183 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
184
185 // Last touch event time
186 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700187
Michael Chane96440f2009-05-06 10:27:36 -0700188 // Last touch event type
189 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700190
Michael Chane96440f2009-05-06 10:27:36 -0700191 // Time to wait before calling useractivity again. This saves CPU usage
192 // when we get a flood of touch events.
193 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
194
195 // Last time we call user activity
196 long mLastUserActivityCallTime = 0;
197
Romain Guy06882f82009-06-10 13:36:04 -0700198 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700199 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700202 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203
204 /**
205 * Condition waited on by {@link #reenableKeyguard} to know the call to
206 * the window policy has finished.
207 */
208 private boolean mWaitingUntilKeyguardReenabled = false;
209
210
211 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
212 new Handler(), "WindowManagerService.mKeyguardDisabled") {
213 public void acquired() {
214 mPolicy.enableKeyguard(false);
215 }
216 public void released() {
217 synchronized (mKeyguardDisabled) {
218 mPolicy.enableKeyguard(true);
219 mWaitingUntilKeyguardReenabled = false;
220 mKeyguardDisabled.notifyAll();
221 }
222 }
223 };
224
225 final Context mContext;
226
227 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
232
233 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /**
238 * All currently active sessions with clients.
239 */
240 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
243 * Mapping from an IWindow IBinder to the server's Window object.
244 * This is also used as the lock for all of our state.
245 */
246 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
247
248 /**
249 * Mapping from a token IBinder to a WindowToken object.
250 */
251 final HashMap<IBinder, WindowToken> mTokenMap =
252 new HashMap<IBinder, WindowToken>();
253
254 /**
255 * The same tokens as mTokenMap, stored in a list for efficient iteration
256 * over them.
257 */
258 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Window tokens that are in the process of exiting, but still
262 * on screen for animations.
263 */
264 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
265
266 /**
267 * Z-ordered (bottom-most first) list of all application tokens, for
268 * controlling the ordering of windows in different applications. This
269 * contains WindowToken objects.
270 */
271 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
272
273 /**
274 * Application tokens that are in the process of exiting, but still
275 * on screen for animations.
276 */
277 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
278
279 /**
280 * List of window tokens that have finished starting their application,
281 * and now need to have the policy remove their windows.
282 */
283 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
284
285 /**
286 * Z-ordered (bottom-most first) list of all Window objects.
287 */
288 final ArrayList mWindows = new ArrayList();
289
290 /**
291 * Windows that are being resized. Used so we can tell the client about
292 * the resize after closing the transaction in which we resized the
293 * underlying surface.
294 */
295 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
296
297 /**
298 * Windows whose animations have ended and now must be removed.
299 */
300 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
301
302 /**
303 * Windows whose surface should be destroyed.
304 */
305 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
306
307 /**
308 * Windows that have lost input focus and are waiting for the new
309 * focus window to be displayed before they are told about this.
310 */
311 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
312
313 /**
314 * This is set when we have run out of memory, and will either be an empty
315 * list or contain windows that need to be force removed.
316 */
317 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 SurfaceSession mFxSession;
322 Surface mDimSurface;
323 boolean mDimShown;
324 float mDimCurrentAlpha;
325 float mDimTargetAlpha;
326 float mDimDeltaPerMs;
327 long mLastDimAnimTime;
328 Surface mBlurSurface;
329 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 final float[] mTmpFloats = new float[9];
334
335 boolean mSafeMode;
336 boolean mDisplayEnabled = false;
337 boolean mSystemBooted = false;
338 int mRotation = 0;
339 int mRequestedRotation = 0;
340 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700341 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 ArrayList<IRotationWatcher> mRotationWatchers
343 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 boolean mLayoutNeeded = true;
346 boolean mAnimationPending = false;
347 boolean mDisplayFrozen = false;
348 boolean mWindowsFreezingScreen = false;
349 long mFreezeGcPending = 0;
350 int mAppsFreezingScreen = 0;
351
352 // This is held as long as we have the screen frozen, to give us time to
353 // perform a rotation animation when turning off shows the lock screen which
354 // changes the orientation.
355 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 // State management of app transitions. When we are preparing for a
358 // transition, mNextAppTransition will be the kind of transition to
359 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
360 // mOpeningApps and mClosingApps are the lists of tokens that will be
361 // made visible or hidden at the next transition.
362 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
363 boolean mAppTransitionReady = false;
364 boolean mAppTransitionTimeout = false;
365 boolean mStartingIconInTransition = false;
366 boolean mSkipAppTransitionAnimation = false;
367 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
368 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 //flag to detect fat touch events
371 boolean mFatTouch = false;
372 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 H mH = new H();
375
376 WindowState mCurrentFocus = null;
377 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 // This just indicates the window the input method is on top of, not
380 // necessarily the window its input is going to.
381 WindowState mInputMethodTarget = null;
382 WindowState mUpcomingInputMethodTarget = null;
383 boolean mInputMethodTargetWaitingAnim;
384 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 WindowState mInputMethodWindow = null;
387 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
388
389 AppWindowToken mFocusedApp = null;
390
391 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 float mWindowAnimationScale = 1.0f;
394 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 final KeyWaiter mKeyWaiter = new KeyWaiter();
397 final KeyQ mQueue;
398 final InputDispatcherThread mInputThread;
399
400 // Who is holding the screen on.
401 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
404 * Whether the UI is currently running in touch mode (not showing
405 * navigational focus because the user is directly pressing the screen).
406 */
407 boolean mInTouchMode = false;
408
409 private ViewServer mViewServer;
410
411 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700412
Dianne Hackbornc485a602009-03-24 22:39:49 -0700413 final Configuration mTempConfiguration = new Configuration();
Romain Guy06882f82009-06-10 13:36:04 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 public static WindowManagerService main(Context context,
416 PowerManagerService pm, boolean haveInputMethods) {
417 WMThread thr = new WMThread(context, pm, haveInputMethods);
418 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 synchronized (thr) {
421 while (thr.mService == null) {
422 try {
423 thr.wait();
424 } catch (InterruptedException e) {
425 }
426 }
427 }
Romain Guy06882f82009-06-10 13:36:04 -0700428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 return thr.mService;
430 }
Romain Guy06882f82009-06-10 13:36:04 -0700431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 static class WMThread extends Thread {
433 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 private final Context mContext;
436 private final PowerManagerService mPM;
437 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 public WMThread(Context context, PowerManagerService pm,
440 boolean haveInputMethods) {
441 super("WindowManager");
442 mContext = context;
443 mPM = pm;
444 mHaveInputMethods = haveInputMethods;
445 }
Romain Guy06882f82009-06-10 13:36:04 -0700446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 public void run() {
448 Looper.prepare();
449 WindowManagerService s = new WindowManagerService(mContext, mPM,
450 mHaveInputMethods);
451 android.os.Process.setThreadPriority(
452 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 synchronized (this) {
455 mService = s;
456 notifyAll();
457 }
Romain Guy06882f82009-06-10 13:36:04 -0700458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 Looper.loop();
460 }
461 }
462
463 static class PolicyThread extends Thread {
464 private final WindowManagerPolicy mPolicy;
465 private final WindowManagerService mService;
466 private final Context mContext;
467 private final PowerManagerService mPM;
468 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 public PolicyThread(WindowManagerPolicy policy,
471 WindowManagerService service, Context context,
472 PowerManagerService pm) {
473 super("WindowManagerPolicy");
474 mPolicy = policy;
475 mService = service;
476 mContext = context;
477 mPM = pm;
478 }
Romain Guy06882f82009-06-10 13:36:04 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 public void run() {
481 Looper.prepare();
482 //Looper.myLooper().setMessageLogging(new LogPrinter(
483 // Log.VERBOSE, "WindowManagerPolicy"));
484 android.os.Process.setThreadPriority(
485 android.os.Process.THREAD_PRIORITY_FOREGROUND);
486 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 synchronized (this) {
489 mRunning = true;
490 notifyAll();
491 }
Romain Guy06882f82009-06-10 13:36:04 -0700492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 Looper.loop();
494 }
495 }
496
497 private WindowManagerService(Context context, PowerManagerService pm,
498 boolean haveInputMethods) {
499 mContext = context;
500 mHaveInputMethods = haveInputMethods;
501 mLimitedAlphaCompositing = context.getResources().getBoolean(
502 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 mPowerManager = pm;
505 mPowerManager.setPolicy(mPolicy);
506 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
507 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
508 "SCREEN_FROZEN");
509 mScreenFrozenLock.setReferenceCounted(false);
510
511 mActivityManager = ActivityManagerNative.getDefault();
512 mBatteryStats = BatteryStatsService.getService();
513
514 // Get persisted window scale setting
515 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
516 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
517 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
518 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 mQueue = new KeyQ();
521
522 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
525 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 synchronized (thr) {
528 while (!thr.mRunning) {
529 try {
530 thr.wait();
531 } catch (InterruptedException e) {
532 }
533 }
534 }
Romain Guy06882f82009-06-10 13:36:04 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 // Add ourself to the Watchdog monitors.
539 Watchdog.getInstance().addMonitor(this);
540 }
541
542 @Override
543 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
544 throws RemoteException {
545 try {
546 return super.onTransact(code, data, reply, flags);
547 } catch (RuntimeException e) {
548 // The window manager only throws security exceptions, so let's
549 // log all others.
550 if (!(e instanceof SecurityException)) {
551 Log.e(TAG, "Window Manager Crash", e);
552 }
553 throw e;
554 }
555 }
556
557 private void placeWindowAfter(Object pos, WindowState window) {
558 final int i = mWindows.indexOf(pos);
559 if (localLOGV || DEBUG_FOCUS) Log.v(
560 TAG, "Adding window " + window + " at "
561 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
562 mWindows.add(i+1, window);
563 }
564
565 private void placeWindowBefore(Object pos, WindowState window) {
566 final int i = mWindows.indexOf(pos);
567 if (localLOGV || DEBUG_FOCUS) Log.v(
568 TAG, "Adding window " + window + " at "
569 + i + " of " + mWindows.size() + " (before " + pos + ")");
570 mWindows.add(i, window);
571 }
572
573 //This method finds out the index of a window that has the same app token as
574 //win. used for z ordering the windows in mWindows
575 private int findIdxBasedOnAppTokens(WindowState win) {
576 //use a local variable to cache mWindows
577 ArrayList localmWindows = mWindows;
578 int jmax = localmWindows.size();
579 if(jmax == 0) {
580 return -1;
581 }
582 for(int j = (jmax-1); j >= 0; j--) {
583 WindowState wentry = (WindowState)localmWindows.get(j);
584 if(wentry.mAppToken == win.mAppToken) {
585 return j;
586 }
587 }
588 return -1;
589 }
Romain Guy06882f82009-06-10 13:36:04 -0700590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
592 final IWindow client = win.mClient;
593 final WindowToken token = win.mToken;
594 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 final int N = localmWindows.size();
597 final WindowState attached = win.mAttachedWindow;
598 int i;
599 if (attached == null) {
600 int tokenWindowsPos = token.windows.size();
601 if (token.appWindowToken != null) {
602 int index = tokenWindowsPos-1;
603 if (index >= 0) {
604 // If this application has existing windows, we
605 // simply place the new window on top of them... but
606 // keep the starting window on top.
607 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
608 // Base windows go behind everything else.
609 placeWindowBefore(token.windows.get(0), win);
610 tokenWindowsPos = 0;
611 } else {
612 AppWindowToken atoken = win.mAppToken;
613 if (atoken != null &&
614 token.windows.get(index) == atoken.startingWindow) {
615 placeWindowBefore(token.windows.get(index), win);
616 tokenWindowsPos--;
617 } else {
618 int newIdx = findIdxBasedOnAppTokens(win);
619 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700620 //there is a window above this one associated with the same
621 //apptoken note that the window could be a floating window
622 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 //windows associated with this token.
624 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
627 }
628 } else {
629 if (localLOGV) Log.v(
630 TAG, "Figuring out where to add app window "
631 + client.asBinder() + " (token=" + token + ")");
632 // Figure out where the window should go, based on the
633 // order of applications.
634 final int NA = mAppTokens.size();
635 Object pos = null;
636 for (i=NA-1; i>=0; i--) {
637 AppWindowToken t = mAppTokens.get(i);
638 if (t == token) {
639 i--;
640 break;
641 }
642 if (t.windows.size() > 0) {
643 pos = t.windows.get(0);
644 }
645 }
646 // We now know the index into the apps. If we found
647 // an app window above, that gives us the position; else
648 // we need to look some more.
649 if (pos != null) {
650 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700651 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 mTokenMap.get(((WindowState)pos).mClient.asBinder());
653 if (atoken != null) {
654 final int NC = atoken.windows.size();
655 if (NC > 0) {
656 WindowState bottom = atoken.windows.get(0);
657 if (bottom.mSubLayer < 0) {
658 pos = bottom;
659 }
660 }
661 }
662 placeWindowBefore(pos, win);
663 } else {
664 while (i >= 0) {
665 AppWindowToken t = mAppTokens.get(i);
666 final int NW = t.windows.size();
667 if (NW > 0) {
668 pos = t.windows.get(NW-1);
669 break;
670 }
671 i--;
672 }
673 if (pos != null) {
674 // Move in front of any windows attached to this
675 // one.
676 WindowToken atoken =
677 mTokenMap.get(((WindowState)pos).mClient.asBinder());
678 if (atoken != null) {
679 final int NC = atoken.windows.size();
680 if (NC > 0) {
681 WindowState top = atoken.windows.get(NC-1);
682 if (top.mSubLayer >= 0) {
683 pos = top;
684 }
685 }
686 }
687 placeWindowAfter(pos, win);
688 } else {
689 // Just search for the start of this layer.
690 final int myLayer = win.mBaseLayer;
691 for (i=0; i<N; i++) {
692 WindowState w = (WindowState)localmWindows.get(i);
693 if (w.mBaseLayer > myLayer) {
694 break;
695 }
696 }
697 if (localLOGV || DEBUG_FOCUS) Log.v(
698 TAG, "Adding window " + win + " at "
699 + i + " of " + N);
700 localmWindows.add(i, win);
701 }
702 }
703 }
704 } else {
705 // Figure out where window should go, based on layer.
706 final int myLayer = win.mBaseLayer;
707 for (i=N-1; i>=0; i--) {
708 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
709 i++;
710 break;
711 }
712 }
713 if (i < 0) i = 0;
714 if (localLOGV || DEBUG_FOCUS) Log.v(
715 TAG, "Adding window " + win + " at "
716 + i + " of " + N);
717 localmWindows.add(i, win);
718 }
719 if (addToToken) {
720 token.windows.add(tokenWindowsPos, win);
721 }
722
723 } else {
724 // Figure out this window's ordering relative to the window
725 // it is attached to.
726 final int NA = token.windows.size();
727 final int sublayer = win.mSubLayer;
728 int largestSublayer = Integer.MIN_VALUE;
729 WindowState windowWithLargestSublayer = null;
730 for (i=0; i<NA; i++) {
731 WindowState w = token.windows.get(i);
732 final int wSublayer = w.mSubLayer;
733 if (wSublayer >= largestSublayer) {
734 largestSublayer = wSublayer;
735 windowWithLargestSublayer = w;
736 }
737 if (sublayer < 0) {
738 // For negative sublayers, we go below all windows
739 // in the same sublayer.
740 if (wSublayer >= sublayer) {
741 if (addToToken) {
742 token.windows.add(i, win);
743 }
744 placeWindowBefore(
745 wSublayer >= 0 ? attached : w, win);
746 break;
747 }
748 } else {
749 // For positive sublayers, we go above all windows
750 // in the same sublayer.
751 if (wSublayer > sublayer) {
752 if (addToToken) {
753 token.windows.add(i, win);
754 }
755 placeWindowBefore(w, win);
756 break;
757 }
758 }
759 }
760 if (i >= NA) {
761 if (addToToken) {
762 token.windows.add(win);
763 }
764 if (sublayer < 0) {
765 placeWindowBefore(attached, win);
766 } else {
767 placeWindowAfter(largestSublayer >= 0
768 ? windowWithLargestSublayer
769 : attached,
770 win);
771 }
772 }
773 }
Romain Guy06882f82009-06-10 13:36:04 -0700774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 if (win.mAppToken != null && addToToken) {
776 win.mAppToken.allAppWindows.add(win);
777 }
778 }
Romain Guy06882f82009-06-10 13:36:04 -0700779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 static boolean canBeImeTarget(WindowState w) {
781 final int fl = w.mAttrs.flags
782 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
783 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
784 return w.isVisibleOrAdding();
785 }
786 return false;
787 }
Romain Guy06882f82009-06-10 13:36:04 -0700788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
790 final ArrayList localmWindows = mWindows;
791 final int N = localmWindows.size();
792 WindowState w = null;
793 int i = N;
794 while (i > 0) {
795 i--;
796 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
799 // + Integer.toHexString(w.mAttrs.flags));
800 if (canBeImeTarget(w)) {
801 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 // Yet more tricksyness! If this window is a "starting"
804 // window, we do actually want to be on top of it, but
805 // it is not -really- where input will go. So if the caller
806 // is not actually looking to move the IME, look down below
807 // for a real window to target...
808 if (!willMove
809 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
810 && i > 0) {
811 WindowState wb = (WindowState)localmWindows.get(i-1);
812 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
813 i--;
814 w = wb;
815 }
816 }
817 break;
818 }
819 }
Romain Guy06882f82009-06-10 13:36:04 -0700820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
824 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 if (willMove && w != null) {
827 final WindowState curTarget = mInputMethodTarget;
828 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 // Now some fun for dealing with window animations that
831 // modify the Z order. We need to look at all windows below
832 // the current target that are in this app, finding the highest
833 // visible one in layering.
834 AppWindowToken token = curTarget.mAppToken;
835 WindowState highestTarget = null;
836 int highestPos = 0;
837 if (token.animating || token.animation != null) {
838 int pos = 0;
839 pos = localmWindows.indexOf(curTarget);
840 while (pos >= 0) {
841 WindowState win = (WindowState)localmWindows.get(pos);
842 if (win.mAppToken != token) {
843 break;
844 }
845 if (!win.mRemoved) {
846 if (highestTarget == null || win.mAnimLayer >
847 highestTarget.mAnimLayer) {
848 highestTarget = win;
849 highestPos = pos;
850 }
851 }
852 pos--;
853 }
854 }
Romain Guy06882f82009-06-10 13:36:04 -0700855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700857 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 + mNextAppTransition + " " + highestTarget
859 + " animating=" + highestTarget.isAnimating()
860 + " layer=" + highestTarget.mAnimLayer
861 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
864 // If we are currently setting up for an animation,
865 // hold everything until we can find out what will happen.
866 mInputMethodTargetWaitingAnim = true;
867 mInputMethodTarget = highestTarget;
868 return highestPos + 1;
869 } else if (highestTarget.isAnimating() &&
870 highestTarget.mAnimLayer > w.mAnimLayer) {
871 // If the window we are currently targeting is involved
872 // with an animation, and it is on top of the next target
873 // we will be over, then hold off on moving until
874 // that is done.
875 mInputMethodTarget = highestTarget;
876 return highestPos + 1;
877 }
878 }
879 }
880 }
Romain Guy06882f82009-06-10 13:36:04 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 //Log.i(TAG, "Placing input method @" + (i+1));
883 if (w != null) {
884 if (willMove) {
885 RuntimeException e = new RuntimeException();
886 e.fillInStackTrace();
887 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
888 + mInputMethodTarget + " to " + w, e);
889 mInputMethodTarget = w;
890 if (w.mAppToken != null) {
891 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
892 } else {
893 setInputMethodAnimLayerAdjustment(0);
894 }
895 }
896 return i+1;
897 }
898 if (willMove) {
899 RuntimeException e = new RuntimeException();
900 e.fillInStackTrace();
901 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
902 + mInputMethodTarget + " to null", e);
903 mInputMethodTarget = null;
904 setInputMethodAnimLayerAdjustment(0);
905 }
906 return -1;
907 }
Romain Guy06882f82009-06-10 13:36:04 -0700908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 void addInputMethodWindowToListLocked(WindowState win) {
910 int pos = findDesiredInputMethodWindowIndexLocked(true);
911 if (pos >= 0) {
912 win.mTargetAppToken = mInputMethodTarget.mAppToken;
913 mWindows.add(pos, win);
914 moveInputMethodDialogsLocked(pos+1);
915 return;
916 }
917 win.mTargetAppToken = null;
918 addWindowToListInOrderLocked(win, true);
919 moveInputMethodDialogsLocked(pos);
920 }
Romain Guy06882f82009-06-10 13:36:04 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 void setInputMethodAnimLayerAdjustment(int adj) {
923 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
924 mInputMethodAnimLayerAdjustment = adj;
925 WindowState imw = mInputMethodWindow;
926 if (imw != null) {
927 imw.mAnimLayer = imw.mLayer + adj;
928 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
929 + " anim layer: " + imw.mAnimLayer);
930 int wi = imw.mChildWindows.size();
931 while (wi > 0) {
932 wi--;
933 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
934 cw.mAnimLayer = cw.mLayer + adj;
935 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
936 + " anim layer: " + cw.mAnimLayer);
937 }
938 }
939 int di = mInputMethodDialogs.size();
940 while (di > 0) {
941 di --;
942 imw = mInputMethodDialogs.get(di);
943 imw.mAnimLayer = imw.mLayer + adj;
944 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
945 + " anim layer: " + imw.mAnimLayer);
946 }
947 }
Romain Guy06882f82009-06-10 13:36:04 -0700948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
950 int wpos = mWindows.indexOf(win);
951 if (wpos >= 0) {
952 if (wpos < interestingPos) interestingPos--;
953 mWindows.remove(wpos);
954 int NC = win.mChildWindows.size();
955 while (NC > 0) {
956 NC--;
957 WindowState cw = (WindowState)win.mChildWindows.get(NC);
958 int cpos = mWindows.indexOf(cw);
959 if (cpos >= 0) {
960 if (cpos < interestingPos) interestingPos--;
961 mWindows.remove(cpos);
962 }
963 }
964 }
965 return interestingPos;
966 }
Romain Guy06882f82009-06-10 13:36:04 -0700967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 private void reAddWindowToListInOrderLocked(WindowState win) {
969 addWindowToListInOrderLocked(win, false);
970 // This is a hack to get all of the child windows added as well
971 // at the right position. Child windows should be rare and
972 // this case should be rare, so it shouldn't be that big a deal.
973 int wpos = mWindows.indexOf(win);
974 if (wpos >= 0) {
975 mWindows.remove(wpos);
976 reAddWindowLocked(wpos, win);
977 }
978 }
Romain Guy06882f82009-06-10 13:36:04 -0700979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 void logWindowList(String prefix) {
981 int N = mWindows.size();
982 while (N > 0) {
983 N--;
984 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
985 }
986 }
Romain Guy06882f82009-06-10 13:36:04 -0700987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 void moveInputMethodDialogsLocked(int pos) {
989 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -0700990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 final int N = dialogs.size();
992 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
993 for (int i=0; i<N; i++) {
994 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
995 }
996 if (DEBUG_INPUT_METHOD) {
997 Log.v(TAG, "Window list w/pos=" + pos);
998 logWindowList(" ");
999 }
Romain Guy06882f82009-06-10 13:36:04 -07001000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if (pos >= 0) {
1002 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1003 if (pos < mWindows.size()) {
1004 WindowState wp = (WindowState)mWindows.get(pos);
1005 if (wp == mInputMethodWindow) {
1006 pos++;
1007 }
1008 }
1009 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1010 for (int i=0; i<N; i++) {
1011 WindowState win = dialogs.get(i);
1012 win.mTargetAppToken = targetAppToken;
1013 pos = reAddWindowLocked(pos, win);
1014 }
1015 if (DEBUG_INPUT_METHOD) {
1016 Log.v(TAG, "Final window list:");
1017 logWindowList(" ");
1018 }
1019 return;
1020 }
1021 for (int i=0; i<N; i++) {
1022 WindowState win = dialogs.get(i);
1023 win.mTargetAppToken = null;
1024 reAddWindowToListInOrderLocked(win);
1025 if (DEBUG_INPUT_METHOD) {
1026 Log.v(TAG, "No IM target, final list:");
1027 logWindowList(" ");
1028 }
1029 }
1030 }
Romain Guy06882f82009-06-10 13:36:04 -07001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1033 final WindowState imWin = mInputMethodWindow;
1034 final int DN = mInputMethodDialogs.size();
1035 if (imWin == null && DN == 0) {
1036 return false;
1037 }
Romain Guy06882f82009-06-10 13:36:04 -07001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1040 if (imPos >= 0) {
1041 // In this case, the input method windows are to be placed
1042 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 // First check to see if the input method windows are already
1045 // located here, and contiguous.
1046 final int N = mWindows.size();
1047 WindowState firstImWin = imPos < N
1048 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 // Figure out the actual input method window that should be
1051 // at the bottom of their stack.
1052 WindowState baseImWin = imWin != null
1053 ? imWin : mInputMethodDialogs.get(0);
1054 if (baseImWin.mChildWindows.size() > 0) {
1055 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1056 if (cw.mSubLayer < 0) baseImWin = cw;
1057 }
Romain Guy06882f82009-06-10 13:36:04 -07001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 if (firstImWin == baseImWin) {
1060 // The windows haven't moved... but are they still contiguous?
1061 // First find the top IM window.
1062 int pos = imPos+1;
1063 while (pos < N) {
1064 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1065 break;
1066 }
1067 pos++;
1068 }
1069 pos++;
1070 // Now there should be no more input method windows above.
1071 while (pos < N) {
1072 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1073 break;
1074 }
1075 pos++;
1076 }
1077 if (pos >= N) {
1078 // All is good!
1079 return false;
1080 }
1081 }
Romain Guy06882f82009-06-10 13:36:04 -07001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 if (imWin != null) {
1084 if (DEBUG_INPUT_METHOD) {
1085 Log.v(TAG, "Moving IM from " + imPos);
1086 logWindowList(" ");
1087 }
1088 imPos = tmpRemoveWindowLocked(imPos, imWin);
1089 if (DEBUG_INPUT_METHOD) {
1090 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1091 logWindowList(" ");
1092 }
1093 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1094 reAddWindowLocked(imPos, imWin);
1095 if (DEBUG_INPUT_METHOD) {
1096 Log.v(TAG, "List after moving IM to " + imPos + ":");
1097 logWindowList(" ");
1098 }
1099 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1100 } else {
1101 moveInputMethodDialogsLocked(imPos);
1102 }
Romain Guy06882f82009-06-10 13:36:04 -07001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 } else {
1105 // In this case, the input method windows go in a fixed layer,
1106 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if (imWin != null) {
1109 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1110 tmpRemoveWindowLocked(0, imWin);
1111 imWin.mTargetAppToken = null;
1112 reAddWindowToListInOrderLocked(imWin);
1113 if (DEBUG_INPUT_METHOD) {
1114 Log.v(TAG, "List with no IM target:");
1115 logWindowList(" ");
1116 }
1117 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1118 } else {
1119 moveInputMethodDialogsLocked(-1);;
1120 }
Romain Guy06882f82009-06-10 13:36:04 -07001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
Romain Guy06882f82009-06-10 13:36:04 -07001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (needAssignLayers) {
1125 assignLayersLocked();
1126 }
Romain Guy06882f82009-06-10 13:36:04 -07001127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 return true;
1129 }
Romain Guy06882f82009-06-10 13:36:04 -07001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 void adjustInputMethodDialogsLocked() {
1132 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1133 }
Romain Guy06882f82009-06-10 13:36:04 -07001134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 public int addWindow(Session session, IWindow client,
1136 WindowManager.LayoutParams attrs, int viewVisibility,
1137 Rect outContentInsets) {
1138 int res = mPolicy.checkAddPermission(attrs);
1139 if (res != WindowManagerImpl.ADD_OKAY) {
1140 return res;
1141 }
Romain Guy06882f82009-06-10 13:36:04 -07001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 boolean reportNewConfig = false;
1144 WindowState attachedWindow = null;
1145 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 synchronized(mWindowMap) {
1148 // Instantiating a Display requires talking with the simulator,
1149 // so don't do it until we know the system is mostly up and
1150 // running.
1151 if (mDisplay == null) {
1152 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1153 mDisplay = wm.getDefaultDisplay();
1154 mQueue.setDisplay(mDisplay);
1155 reportNewConfig = true;
1156 }
Romain Guy06882f82009-06-10 13:36:04 -07001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 if (mWindowMap.containsKey(client.asBinder())) {
1159 Log.w(TAG, "Window " + client + " is already added");
1160 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1161 }
1162
1163 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001164 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if (attachedWindow == null) {
1166 Log.w(TAG, "Attempted to add window with token that is not a window: "
1167 + attrs.token + ". Aborting.");
1168 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1169 }
1170 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1171 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1172 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1173 + attrs.token + ". Aborting.");
1174 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1175 }
1176 }
1177
1178 boolean addToken = false;
1179 WindowToken token = mTokenMap.get(attrs.token);
1180 if (token == null) {
1181 if (attrs.type >= FIRST_APPLICATION_WINDOW
1182 && attrs.type <= LAST_APPLICATION_WINDOW) {
1183 Log.w(TAG, "Attempted to add application window with unknown token "
1184 + attrs.token + ". Aborting.");
1185 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1186 }
1187 if (attrs.type == TYPE_INPUT_METHOD) {
1188 Log.w(TAG, "Attempted to add input method window with unknown token "
1189 + attrs.token + ". Aborting.");
1190 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1191 }
1192 token = new WindowToken(attrs.token, -1, false);
1193 addToken = true;
1194 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1195 && attrs.type <= LAST_APPLICATION_WINDOW) {
1196 AppWindowToken atoken = token.appWindowToken;
1197 if (atoken == null) {
1198 Log.w(TAG, "Attempted to add window with non-application token "
1199 + token + ". Aborting.");
1200 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1201 } else if (atoken.removed) {
1202 Log.w(TAG, "Attempted to add window with exiting application token "
1203 + token + ". Aborting.");
1204 return WindowManagerImpl.ADD_APP_EXITING;
1205 }
1206 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1207 // No need for this guy!
1208 if (localLOGV) Log.v(
1209 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1210 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1211 }
1212 } else if (attrs.type == TYPE_INPUT_METHOD) {
1213 if (token.windowType != TYPE_INPUT_METHOD) {
1214 Log.w(TAG, "Attempted to add input method window with bad token "
1215 + attrs.token + ". Aborting.");
1216 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1217 }
1218 }
1219
1220 win = new WindowState(session, client, token,
1221 attachedWindow, attrs, viewVisibility);
1222 if (win.mDeathRecipient == null) {
1223 // Client has apparently died, so there is no reason to
1224 // continue.
1225 Log.w(TAG, "Adding window client " + client.asBinder()
1226 + " that is dead, aborting.");
1227 return WindowManagerImpl.ADD_APP_EXITING;
1228 }
1229
1230 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 res = mPolicy.prepareAddWindowLw(win, attrs);
1233 if (res != WindowManagerImpl.ADD_OKAY) {
1234 return res;
1235 }
1236
1237 // From now on, no exceptions or errors allowed!
1238
1239 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 if (addToken) {
1244 mTokenMap.put(attrs.token, token);
1245 mTokenList.add(token);
1246 }
1247 win.attach();
1248 mWindowMap.put(client.asBinder(), win);
1249
1250 if (attrs.type == TYPE_APPLICATION_STARTING &&
1251 token.appWindowToken != null) {
1252 token.appWindowToken.startingWindow = win;
1253 }
1254
1255 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 if (attrs.type == TYPE_INPUT_METHOD) {
1258 mInputMethodWindow = win;
1259 addInputMethodWindowToListLocked(win);
1260 imMayMove = false;
1261 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1262 mInputMethodDialogs.add(win);
1263 addWindowToListInOrderLocked(win, true);
1264 adjustInputMethodDialogsLocked();
1265 imMayMove = false;
1266 } else {
1267 addWindowToListInOrderLocked(win, true);
1268 }
Romain Guy06882f82009-06-10 13:36:04 -07001269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 if (mInTouchMode) {
1275 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1276 }
1277 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1278 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1279 }
Romain Guy06882f82009-06-10 13:36:04 -07001280
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001281 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001283 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1284 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 imMayMove = false;
1286 }
1287 }
Romain Guy06882f82009-06-10 13:36:04 -07001288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001290 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
Romain Guy06882f82009-06-10 13:36:04 -07001292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 assignLayersLocked();
1294 // Don't do layout here, the window must call
1295 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 //dump();
1298
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001299 if (focusChanged) {
1300 if (mCurrentFocus != null) {
1301 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1302 }
1303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 if (localLOGV) Log.v(
1305 TAG, "New client " + client.asBinder()
1306 + ": window=" + win);
1307 }
1308
1309 // sendNewConfiguration() checks caller permissions so we must call it with
1310 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1311 // identity anyway, so it's safe to just clear & restore around this whole
1312 // block.
1313 final long origId = Binder.clearCallingIdentity();
1314 if (reportNewConfig) {
1315 sendNewConfiguration();
1316 } else {
1317 // Update Orientation after adding a window, only if the window needs to be
1318 // displayed right away
1319 if (win.isVisibleOrAdding()) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001320 if (updateOrientationFromAppTokens(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 sendNewConfiguration();
1322 }
1323 }
1324 }
1325 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 return res;
1328 }
Romain Guy06882f82009-06-10 13:36:04 -07001329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 public void removeWindow(Session session, IWindow client) {
1331 synchronized(mWindowMap) {
1332 WindowState win = windowForClientLocked(session, client);
1333 if (win == null) {
1334 return;
1335 }
1336 removeWindowLocked(session, win);
1337 }
1338 }
Romain Guy06882f82009-06-10 13:36:04 -07001339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 public void removeWindowLocked(Session session, WindowState win) {
1341
1342 if (localLOGV || DEBUG_FOCUS) Log.v(
1343 TAG, "Remove " + win + " client="
1344 + Integer.toHexString(System.identityHashCode(
1345 win.mClient.asBinder()))
1346 + ", surface=" + win.mSurface);
1347
1348 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 if (DEBUG_APP_TRANSITIONS) Log.v(
1351 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1352 + " mExiting=" + win.mExiting
1353 + " isAnimating=" + win.isAnimating()
1354 + " app-animation="
1355 + (win.mAppToken != null ? win.mAppToken.animation : null)
1356 + " inPendingTransaction="
1357 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1358 + " mDisplayFrozen=" + mDisplayFrozen);
1359 // Visibility of the removed window. Will be used later to update orientation later on.
1360 boolean wasVisible = false;
1361 // First, see if we need to run an animation. If we do, we have
1362 // to hold off on removing the window until the animation is done.
1363 // If the display is frozen, just remove immediately, since the
1364 // animation wouldn't be seen.
1365 if (win.mSurface != null && !mDisplayFrozen) {
1366 // If we are not currently running the exit animation, we
1367 // need to see about starting one.
1368 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1371 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1372 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1373 }
1374 // Try starting an animation.
1375 if (applyAnimationLocked(win, transit, false)) {
1376 win.mExiting = true;
1377 }
1378 }
1379 if (win.mExiting || win.isAnimating()) {
1380 // The exit animation is running... wait for it!
1381 //Log.i(TAG, "*** Running exit animation...");
1382 win.mExiting = true;
1383 win.mRemoveOnExit = true;
1384 mLayoutNeeded = true;
1385 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1386 performLayoutAndPlaceSurfacesLocked();
1387 if (win.mAppToken != null) {
1388 win.mAppToken.updateReportedVisibilityLocked();
1389 }
1390 //dump();
1391 Binder.restoreCallingIdentity(origId);
1392 return;
1393 }
1394 }
1395
1396 removeWindowInnerLocked(session, win);
1397 // Removing a visible window will effect the computed orientation
1398 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001399 if (wasVisible && computeForcedAppOrientationLocked()
1400 != mForcedAppOrientation) {
1401 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1404 Binder.restoreCallingIdentity(origId);
1405 }
Romain Guy06882f82009-06-10 13:36:04 -07001406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 private void removeWindowInnerLocked(Session session, WindowState win) {
1408 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1409 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 if (mInputMethodTarget == win) {
1414 moveInputMethodWindowsIfNeededLocked(false);
1415 }
Romain Guy06882f82009-06-10 13:36:04 -07001416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 mPolicy.removeWindowLw(win);
1418 win.removeLocked();
1419
1420 mWindowMap.remove(win.mClient.asBinder());
1421 mWindows.remove(win);
1422
1423 if (mInputMethodWindow == win) {
1424 mInputMethodWindow = null;
1425 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1426 mInputMethodDialogs.remove(win);
1427 }
Romain Guy06882f82009-06-10 13:36:04 -07001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 final WindowToken token = win.mToken;
1430 final AppWindowToken atoken = win.mAppToken;
1431 token.windows.remove(win);
1432 if (atoken != null) {
1433 atoken.allAppWindows.remove(win);
1434 }
1435 if (localLOGV) Log.v(
1436 TAG, "**** Removing window " + win + ": count="
1437 + token.windows.size());
1438 if (token.windows.size() == 0) {
1439 if (!token.explicit) {
1440 mTokenMap.remove(token.token);
1441 mTokenList.remove(token);
1442 } else if (atoken != null) {
1443 atoken.firstWindowDrawn = false;
1444 }
1445 }
1446
1447 if (atoken != null) {
1448 if (atoken.startingWindow == win) {
1449 atoken.startingWindow = null;
1450 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1451 // If this is the last window and we had requested a starting
1452 // transition window, well there is no point now.
1453 atoken.startingData = null;
1454 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1455 // If this is the last window except for a starting transition
1456 // window, we need to get rid of the starting transition.
1457 if (DEBUG_STARTING_WINDOW) {
1458 Log.v(TAG, "Schedule remove starting " + token
1459 + ": no more real windows");
1460 }
1461 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1462 mH.sendMessage(m);
1463 }
1464 }
Romain Guy06882f82009-06-10 13:36:04 -07001465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 if (!mInLayout) {
1467 assignLayersLocked();
1468 mLayoutNeeded = true;
1469 performLayoutAndPlaceSurfacesLocked();
1470 if (win.mAppToken != null) {
1471 win.mAppToken.updateReportedVisibilityLocked();
1472 }
1473 }
1474 }
1475
1476 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1477 long origId = Binder.clearCallingIdentity();
1478 try {
1479 synchronized (mWindowMap) {
1480 WindowState w = windowForClientLocked(session, client);
1481 if ((w != null) && (w.mSurface != null)) {
1482 Surface.openTransaction();
1483 try {
1484 w.mSurface.setTransparentRegionHint(region);
1485 } finally {
1486 Surface.closeTransaction();
1487 }
1488 }
1489 }
1490 } finally {
1491 Binder.restoreCallingIdentity(origId);
1492 }
1493 }
1494
1495 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001496 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 Rect visibleInsets) {
1498 long origId = Binder.clearCallingIdentity();
1499 try {
1500 synchronized (mWindowMap) {
1501 WindowState w = windowForClientLocked(session, client);
1502 if (w != null) {
1503 w.mGivenInsetsPending = false;
1504 w.mGivenContentInsets.set(contentInsets);
1505 w.mGivenVisibleInsets.set(visibleInsets);
1506 w.mTouchableInsets = touchableInsets;
1507 mLayoutNeeded = true;
1508 performLayoutAndPlaceSurfacesLocked();
1509 }
1510 }
1511 } finally {
1512 Binder.restoreCallingIdentity(origId);
1513 }
1514 }
Romain Guy06882f82009-06-10 13:36:04 -07001515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 public void getWindowDisplayFrame(Session session, IWindow client,
1517 Rect outDisplayFrame) {
1518 synchronized(mWindowMap) {
1519 WindowState win = windowForClientLocked(session, client);
1520 if (win == null) {
1521 outDisplayFrame.setEmpty();
1522 return;
1523 }
1524 outDisplayFrame.set(win.mDisplayFrame);
1525 }
1526 }
1527
1528 public int relayoutWindow(Session session, IWindow client,
1529 WindowManager.LayoutParams attrs, int requestedWidth,
1530 int requestedHeight, int viewVisibility, boolean insetsPending,
1531 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1532 Surface outSurface) {
1533 boolean displayed = false;
1534 boolean inTouchMode;
1535 Configuration newConfig = null;
1536 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 synchronized(mWindowMap) {
1539 WindowState win = windowForClientLocked(session, client);
1540 if (win == null) {
1541 return 0;
1542 }
1543 win.mRequestedWidth = requestedWidth;
1544 win.mRequestedHeight = requestedHeight;
1545
1546 if (attrs != null) {
1547 mPolicy.adjustWindowParamsLw(attrs);
1548 }
Romain Guy06882f82009-06-10 13:36:04 -07001549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 int attrChanges = 0;
1551 int flagChanges = 0;
1552 if (attrs != null) {
1553 flagChanges = win.mAttrs.flags ^= attrs.flags;
1554 attrChanges = win.mAttrs.copyFrom(attrs);
1555 }
1556
1557 if (localLOGV) Log.v(
1558 TAG, "Relayout given client " + client.asBinder()
1559 + " (" + win.mAttrs.getTitle() + ")");
1560
1561
1562 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1563 win.mAlpha = attrs.alpha;
1564 }
1565
1566 final boolean scaledWindow =
1567 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1568
1569 if (scaledWindow) {
1570 // requested{Width|Height} Surface's physical size
1571 // attrs.{width|height} Size on screen
1572 win.mHScale = (attrs.width != requestedWidth) ?
1573 (attrs.width / (float)requestedWidth) : 1.0f;
1574 win.mVScale = (attrs.height != requestedHeight) ?
1575 (attrs.height / (float)requestedHeight) : 1.0f;
1576 }
1577
1578 boolean imMayMove = (flagChanges&(
1579 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1580 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 boolean focusMayChange = win.mViewVisibility != viewVisibility
1583 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1584 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 win.mRelayoutCalled = true;
1587 final int oldVisibility = win.mViewVisibility;
1588 win.mViewVisibility = viewVisibility;
1589 if (viewVisibility == View.VISIBLE &&
1590 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1591 displayed = !win.isVisibleLw();
1592 if (win.mExiting) {
1593 win.mExiting = false;
1594 win.mAnimation = null;
1595 }
1596 if (win.mDestroying) {
1597 win.mDestroying = false;
1598 mDestroySurface.remove(win);
1599 }
1600 if (oldVisibility == View.GONE) {
1601 win.mEnterAnimationPending = true;
1602 }
1603 if (displayed && win.mSurface != null && !win.mDrawPending
1604 && !win.mCommitDrawPending && !mDisplayFrozen) {
1605 applyEnterAnimationLocked(win);
1606 }
1607 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1608 // To change the format, we need to re-build the surface.
1609 win.destroySurfaceLocked();
1610 displayed = true;
1611 }
1612 try {
1613 Surface surface = win.createSurfaceLocked();
1614 if (surface != null) {
1615 outSurface.copyFrom(surface);
1616 } else {
1617 outSurface.clear();
1618 }
1619 } catch (Exception e) {
1620 Log.w(TAG, "Exception thrown when creating surface for client "
1621 + client + " (" + win.mAttrs.getTitle() + ")",
1622 e);
1623 Binder.restoreCallingIdentity(origId);
1624 return 0;
1625 }
1626 if (displayed) {
1627 focusMayChange = true;
1628 }
1629 if (win.mAttrs.type == TYPE_INPUT_METHOD
1630 && mInputMethodWindow == null) {
1631 mInputMethodWindow = win;
1632 imMayMove = true;
1633 }
1634 } else {
1635 win.mEnterAnimationPending = false;
1636 if (win.mSurface != null) {
1637 // If we are not currently running the exit animation, we
1638 // need to see about starting one.
1639 if (!win.mExiting) {
1640 // Try starting an animation; if there isn't one, we
1641 // can destroy the surface right away.
1642 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1643 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1644 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1645 }
1646 if (win.isWinVisibleLw() &&
1647 applyAnimationLocked(win, transit, false)) {
1648 win.mExiting = true;
1649 mKeyWaiter.finishedKey(session, client, true,
1650 KeyWaiter.RETURN_NOTHING);
1651 } else if (win.isAnimating()) {
1652 // Currently in a hide animation... turn this into
1653 // an exit.
1654 win.mExiting = true;
1655 } else {
1656 if (mInputMethodWindow == win) {
1657 mInputMethodWindow = null;
1658 }
1659 win.destroySurfaceLocked();
1660 }
1661 }
1662 }
1663 outSurface.clear();
1664 }
1665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 if (focusMayChange) {
1667 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1668 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 imMayMove = false;
1670 }
1671 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1672 }
Romain Guy06882f82009-06-10 13:36:04 -07001673
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001674 // updateFocusedWindowLocked() already assigned layers so we only need to
1675 // reassign them at this point if the IM window state gets shuffled
1676 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 if (imMayMove) {
1679 if (moveInputMethodWindowsIfNeededLocked(false)) {
1680 assignLayers = true;
1681 }
1682 }
Romain Guy06882f82009-06-10 13:36:04 -07001683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 mLayoutNeeded = true;
1685 win.mGivenInsetsPending = insetsPending;
1686 if (assignLayers) {
1687 assignLayersLocked();
1688 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001689 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 performLayoutAndPlaceSurfacesLocked();
1691 if (win.mAppToken != null) {
1692 win.mAppToken.updateReportedVisibilityLocked();
1693 }
1694 outFrame.set(win.mFrame);
1695 outContentInsets.set(win.mContentInsets);
1696 outVisibleInsets.set(win.mVisibleInsets);
1697 if (localLOGV) Log.v(
1698 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001699 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 + ", requestedHeight=" + requestedHeight
1701 + ", viewVisibility=" + viewVisibility
1702 + "\nRelayout returning frame=" + outFrame
1703 + ", surface=" + outSurface);
1704
1705 if (localLOGV || DEBUG_FOCUS) Log.v(
1706 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1707
1708 inTouchMode = mInTouchMode;
1709 }
1710
1711 if (newConfig != null) {
1712 sendNewConfiguration();
1713 }
Romain Guy06882f82009-06-10 13:36:04 -07001714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1718 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1719 }
1720
1721 public void finishDrawingWindow(Session session, IWindow client) {
1722 final long origId = Binder.clearCallingIdentity();
1723 synchronized(mWindowMap) {
1724 WindowState win = windowForClientLocked(session, client);
1725 if (win != null && win.finishDrawingLocked()) {
1726 mLayoutNeeded = true;
1727 performLayoutAndPlaceSurfacesLocked();
1728 }
1729 }
1730 Binder.restoreCallingIdentity(origId);
1731 }
1732
1733 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1734 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1735 + (lp != null ? lp.packageName : null)
1736 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1737 if (lp != null && lp.windowAnimations != 0) {
1738 // If this is a system resource, don't try to load it from the
1739 // application resources. It is nice to avoid loading application
1740 // resources if we can.
1741 String packageName = lp.packageName != null ? lp.packageName : "android";
1742 int resId = lp.windowAnimations;
1743 if ((resId&0xFF000000) == 0x01000000) {
1744 packageName = "android";
1745 }
1746 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1747 + packageName);
1748 return AttributeCache.instance().get(packageName, resId,
1749 com.android.internal.R.styleable.WindowAnimation);
1750 }
1751 return null;
1752 }
Romain Guy06882f82009-06-10 13:36:04 -07001753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 private void applyEnterAnimationLocked(WindowState win) {
1755 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1756 if (win.mEnterAnimationPending) {
1757 win.mEnterAnimationPending = false;
1758 transit = WindowManagerPolicy.TRANSIT_ENTER;
1759 }
1760
1761 applyAnimationLocked(win, transit, true);
1762 }
1763
1764 private boolean applyAnimationLocked(WindowState win,
1765 int transit, boolean isEntrance) {
1766 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1767 // If we are trying to apply an animation, but already running
1768 // an animation of the same type, then just leave that one alone.
1769 return true;
1770 }
Romain Guy06882f82009-06-10 13:36:04 -07001771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 // Only apply an animation if the display isn't frozen. If it is
1773 // frozen, there is no reason to animate and it can cause strange
1774 // artifacts when we unfreeze the display if some different animation
1775 // is running.
1776 if (!mDisplayFrozen) {
1777 int anim = mPolicy.selectAnimationLw(win, transit);
1778 int attr = -1;
1779 Animation a = null;
1780 if (anim != 0) {
1781 a = AnimationUtils.loadAnimation(mContext, anim);
1782 } else {
1783 switch (transit) {
1784 case WindowManagerPolicy.TRANSIT_ENTER:
1785 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1786 break;
1787 case WindowManagerPolicy.TRANSIT_EXIT:
1788 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1789 break;
1790 case WindowManagerPolicy.TRANSIT_SHOW:
1791 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1792 break;
1793 case WindowManagerPolicy.TRANSIT_HIDE:
1794 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1795 break;
1796 }
1797 if (attr >= 0) {
1798 a = loadAnimation(win.mAttrs, attr);
1799 }
1800 }
1801 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1802 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1803 + " mAnimation=" + win.mAnimation
1804 + " isEntrance=" + isEntrance);
1805 if (a != null) {
1806 if (DEBUG_ANIM) {
1807 RuntimeException e = new RuntimeException();
1808 e.fillInStackTrace();
1809 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1810 }
1811 win.setAnimation(a);
1812 win.mAnimationIsEntrance = isEntrance;
1813 }
1814 } else {
1815 win.clearAnimation();
1816 }
1817
1818 return win.mAnimation != null;
1819 }
1820
1821 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1822 int anim = 0;
1823 Context context = mContext;
1824 if (animAttr >= 0) {
1825 AttributeCache.Entry ent = getCachedAnimations(lp);
1826 if (ent != null) {
1827 context = ent.context;
1828 anim = ent.array.getResourceId(animAttr, 0);
1829 }
1830 }
1831 if (anim != 0) {
1832 return AnimationUtils.loadAnimation(context, anim);
1833 }
1834 return null;
1835 }
Romain Guy06882f82009-06-10 13:36:04 -07001836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 private boolean applyAnimationLocked(AppWindowToken wtoken,
1838 WindowManager.LayoutParams lp, int transit, boolean enter) {
1839 // Only apply an animation if the display isn't frozen. If it is
1840 // frozen, there is no reason to animate and it can cause strange
1841 // artifacts when we unfreeze the display if some different animation
1842 // is running.
1843 if (!mDisplayFrozen) {
1844 int animAttr = 0;
1845 switch (transit) {
1846 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1847 animAttr = enter
1848 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1849 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1850 break;
1851 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1852 animAttr = enter
1853 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1854 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1855 break;
1856 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1857 animAttr = enter
1858 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1859 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1860 break;
1861 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1862 animAttr = enter
1863 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1864 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1865 break;
1866 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1867 animAttr = enter
1868 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1869 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1870 break;
1871 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1872 animAttr = enter
1873 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
1874 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1875 break;
1876 }
1877 Animation a = loadAnimation(lp, animAttr);
1878 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1879 + " anim=" + a
1880 + " animAttr=0x" + Integer.toHexString(animAttr)
1881 + " transit=" + transit);
1882 if (a != null) {
1883 if (DEBUG_ANIM) {
1884 RuntimeException e = new RuntimeException();
1885 e.fillInStackTrace();
1886 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1887 }
1888 wtoken.setAnimation(a);
1889 }
1890 } else {
1891 wtoken.clearAnimation();
1892 }
1893
1894 return wtoken.animation != null;
1895 }
1896
1897 // -------------------------------------------------------------
1898 // Application Window Tokens
1899 // -------------------------------------------------------------
1900
1901 public void validateAppTokens(List tokens) {
1902 int v = tokens.size()-1;
1903 int m = mAppTokens.size()-1;
1904 while (v >= 0 && m >= 0) {
1905 AppWindowToken wtoken = mAppTokens.get(m);
1906 if (wtoken.removed) {
1907 m--;
1908 continue;
1909 }
1910 if (tokens.get(v) != wtoken.token) {
1911 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1912 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1913 }
1914 v--;
1915 m--;
1916 }
1917 while (v >= 0) {
1918 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1919 v--;
1920 }
1921 while (m >= 0) {
1922 AppWindowToken wtoken = mAppTokens.get(m);
1923 if (!wtoken.removed) {
1924 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1925 }
1926 m--;
1927 }
1928 }
1929
1930 boolean checkCallingPermission(String permission, String func) {
1931 // Quick check: if the calling permission is me, it's all okay.
1932 if (Binder.getCallingPid() == Process.myPid()) {
1933 return true;
1934 }
Romain Guy06882f82009-06-10 13:36:04 -07001935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if (mContext.checkCallingPermission(permission)
1937 == PackageManager.PERMISSION_GRANTED) {
1938 return true;
1939 }
1940 String msg = "Permission Denial: " + func + " from pid="
1941 + Binder.getCallingPid()
1942 + ", uid=" + Binder.getCallingUid()
1943 + " requires " + permission;
1944 Log.w(TAG, msg);
1945 return false;
1946 }
Romain Guy06882f82009-06-10 13:36:04 -07001947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 AppWindowToken findAppWindowToken(IBinder token) {
1949 WindowToken wtoken = mTokenMap.get(token);
1950 if (wtoken == null) {
1951 return null;
1952 }
1953 return wtoken.appWindowToken;
1954 }
Romain Guy06882f82009-06-10 13:36:04 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 public void addWindowToken(IBinder token, int type) {
1957 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1958 "addWindowToken()")) {
1959 return;
1960 }
Romain Guy06882f82009-06-10 13:36:04 -07001961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 synchronized(mWindowMap) {
1963 WindowToken wtoken = mTokenMap.get(token);
1964 if (wtoken != null) {
1965 Log.w(TAG, "Attempted to add existing input method token: " + token);
1966 return;
1967 }
1968 wtoken = new WindowToken(token, type, true);
1969 mTokenMap.put(token, wtoken);
1970 mTokenList.add(wtoken);
1971 }
1972 }
Romain Guy06882f82009-06-10 13:36:04 -07001973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 public void removeWindowToken(IBinder token) {
1975 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1976 "removeWindowToken()")) {
1977 return;
1978 }
1979
1980 final long origId = Binder.clearCallingIdentity();
1981 synchronized(mWindowMap) {
1982 WindowToken wtoken = mTokenMap.remove(token);
1983 mTokenList.remove(wtoken);
1984 if (wtoken != null) {
1985 boolean delayed = false;
1986 if (!wtoken.hidden) {
1987 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 final int N = wtoken.windows.size();
1990 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07001991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 for (int i=0; i<N; i++) {
1993 WindowState win = wtoken.windows.get(i);
1994
1995 if (win.isAnimating()) {
1996 delayed = true;
1997 }
Romain Guy06882f82009-06-10 13:36:04 -07001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 if (win.isVisibleNow()) {
2000 applyAnimationLocked(win,
2001 WindowManagerPolicy.TRANSIT_EXIT, false);
2002 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2003 KeyWaiter.RETURN_NOTHING);
2004 changed = true;
2005 }
2006 }
2007
2008 if (changed) {
2009 mLayoutNeeded = true;
2010 performLayoutAndPlaceSurfacesLocked();
2011 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2012 }
Romain Guy06882f82009-06-10 13:36:04 -07002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 if (delayed) {
2015 mExitingTokens.add(wtoken);
2016 }
2017 }
Romain Guy06882f82009-06-10 13:36:04 -07002018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 } else {
2020 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2021 }
2022 }
2023 Binder.restoreCallingIdentity(origId);
2024 }
2025
2026 public void addAppToken(int addPos, IApplicationToken token,
2027 int groupId, int requestedOrientation, boolean fullscreen) {
2028 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2029 "addAppToken()")) {
2030 return;
2031 }
Romain Guy06882f82009-06-10 13:36:04 -07002032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 synchronized(mWindowMap) {
2034 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2035 if (wtoken != null) {
2036 Log.w(TAG, "Attempted to add existing app token: " + token);
2037 return;
2038 }
2039 wtoken = new AppWindowToken(token);
2040 wtoken.groupId = groupId;
2041 wtoken.appFullscreen = fullscreen;
2042 wtoken.requestedOrientation = requestedOrientation;
2043 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002044 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 mTokenMap.put(token.asBinder(), wtoken);
2046 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 // Application tokens start out hidden.
2049 wtoken.hidden = true;
2050 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 //dump();
2053 }
2054 }
Romain Guy06882f82009-06-10 13:36:04 -07002055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 public void setAppGroupId(IBinder token, int groupId) {
2057 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2058 "setAppStartingIcon()")) {
2059 return;
2060 }
2061
2062 synchronized(mWindowMap) {
2063 AppWindowToken wtoken = findAppWindowToken(token);
2064 if (wtoken == null) {
2065 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2066 return;
2067 }
2068 wtoken.groupId = groupId;
2069 }
2070 }
Romain Guy06882f82009-06-10 13:36:04 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 public int getOrientationFromWindowsLocked() {
2073 int pos = mWindows.size() - 1;
2074 while (pos >= 0) {
2075 WindowState wtoken = (WindowState) mWindows.get(pos);
2076 pos--;
2077 if (wtoken.mAppToken != null) {
2078 // We hit an application window. so the orientation will be determined by the
2079 // app window. No point in continuing further.
2080 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2081 }
2082 if (!wtoken.isVisibleLw()) {
2083 continue;
2084 }
2085 int req = wtoken.mAttrs.screenOrientation;
2086 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2087 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2088 continue;
2089 } else {
2090 return req;
2091 }
2092 }
2093 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2094 }
Romain Guy06882f82009-06-10 13:36:04 -07002095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 public int getOrientationFromAppTokensLocked() {
2097 int pos = mAppTokens.size() - 1;
2098 int curGroup = 0;
2099 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002100 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 while (pos >= 0) {
2104 AppWindowToken wtoken = mAppTokens.get(pos);
2105 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002106 // if we're about to tear down this window and not seek for
2107 // the behind activity, don't use it for orientation
2108 if (!findingBehind
2109 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002110 continue;
2111 }
2112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 if (!haveGroup) {
2114 // We ignore any hidden applications on the top.
2115 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2116 continue;
2117 }
2118 haveGroup = true;
2119 curGroup = wtoken.groupId;
2120 lastOrientation = wtoken.requestedOrientation;
2121 } else if (curGroup != wtoken.groupId) {
2122 // If we have hit a new application group, and the bottom
2123 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002124 // the orientation behind it, and the last app was
2125 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002127 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2128 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 return lastOrientation;
2130 }
2131 }
2132 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002133 // If this application is fullscreen, and didn't explicitly say
2134 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002136 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002137 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002138 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 return or;
2140 }
2141 // If this application has requested an explicit orientation,
2142 // then use it.
2143 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2144 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2145 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2146 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2147 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2148 return or;
2149 }
Owen Lin3413b892009-05-01 17:12:32 -07002150 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
2152 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2153 }
Romain Guy06882f82009-06-10 13:36:04 -07002154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002156 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 Configuration config;
2158 long ident = Binder.clearCallingIdentity();
2159 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002160 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162 if (config != null) {
2163 mLayoutNeeded = true;
2164 performLayoutAndPlaceSurfacesLocked();
2165 }
2166 Binder.restoreCallingIdentity(ident);
2167 return config;
2168 }
Romain Guy06882f82009-06-10 13:36:04 -07002169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 /*
2171 * The orientation is computed from non-application windows first. If none of
2172 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002173 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2175 * android.os.IBinder)
2176 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002177 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002178 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 long ident = Binder.clearCallingIdentity();
2181 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002182 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 if (req != mForcedAppOrientation) {
2185 changed = true;
2186 mForcedAppOrientation = req;
2187 //send a message to Policy indicating orientation change to take
2188 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002189 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
Romain Guy06882f82009-06-10 13:36:04 -07002191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 if (changed) {
2193 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002194 WindowManagerPolicy.USE_LAST_ROTATION,
2195 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 if (changed) {
2197 if (freezeThisOneIfNeeded != null) {
2198 AppWindowToken wtoken = findAppWindowToken(
2199 freezeThisOneIfNeeded);
2200 if (wtoken != null) {
2201 startAppFreezingScreenLocked(wtoken,
2202 ActivityInfo.CONFIG_ORIENTATION);
2203 }
2204 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002205 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
2207 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002208
2209 // No obvious action we need to take, but if our current
2210 // state mismatches the activity maanager's, update it
2211 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002212 mTempConfiguration.setToDefaults();
2213 if (computeNewConfigurationLocked(mTempConfiguration)) {
2214 if (appConfig.diff(mTempConfiguration) != 0) {
2215 Log.i(TAG, "Config changed: " + mTempConfiguration);
2216 return new Configuration(mTempConfiguration);
2217 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002218 }
2219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 } finally {
2221 Binder.restoreCallingIdentity(ident);
2222 }
Romain Guy06882f82009-06-10 13:36:04 -07002223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 return null;
2225 }
Romain Guy06882f82009-06-10 13:36:04 -07002226
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002227 int computeForcedAppOrientationLocked() {
2228 int req = getOrientationFromWindowsLocked();
2229 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2230 req = getOrientationFromAppTokensLocked();
2231 }
2232 return req;
2233 }
Romain Guy06882f82009-06-10 13:36:04 -07002234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2236 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2237 "setAppOrientation()")) {
2238 return;
2239 }
Romain Guy06882f82009-06-10 13:36:04 -07002240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 synchronized(mWindowMap) {
2242 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2243 if (wtoken == null) {
2244 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2245 return;
2246 }
Romain Guy06882f82009-06-10 13:36:04 -07002247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 wtoken.requestedOrientation = requestedOrientation;
2249 }
2250 }
Romain Guy06882f82009-06-10 13:36:04 -07002251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 public int getAppOrientation(IApplicationToken token) {
2253 synchronized(mWindowMap) {
2254 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2255 if (wtoken == null) {
2256 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2257 }
Romain Guy06882f82009-06-10 13:36:04 -07002258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 return wtoken.requestedOrientation;
2260 }
2261 }
Romain Guy06882f82009-06-10 13:36:04 -07002262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2264 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2265 "setFocusedApp()")) {
2266 return;
2267 }
2268
2269 synchronized(mWindowMap) {
2270 boolean changed = false;
2271 if (token == null) {
2272 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2273 changed = mFocusedApp != null;
2274 mFocusedApp = null;
2275 mKeyWaiter.tickle();
2276 } else {
2277 AppWindowToken newFocus = findAppWindowToken(token);
2278 if (newFocus == null) {
2279 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2280 return;
2281 }
2282 changed = mFocusedApp != newFocus;
2283 mFocusedApp = newFocus;
2284 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2285 mKeyWaiter.tickle();
2286 }
2287
2288 if (moveFocusNow && changed) {
2289 final long origId = Binder.clearCallingIdentity();
2290 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2291 Binder.restoreCallingIdentity(origId);
2292 }
2293 }
2294 }
2295
2296 public void prepareAppTransition(int transit) {
2297 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2298 "prepareAppTransition()")) {
2299 return;
2300 }
Romain Guy06882f82009-06-10 13:36:04 -07002301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 synchronized(mWindowMap) {
2303 if (DEBUG_APP_TRANSITIONS) Log.v(
2304 TAG, "Prepare app transition: transit=" + transit
2305 + " mNextAppTransition=" + mNextAppTransition);
2306 if (!mDisplayFrozen) {
2307 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2308 mNextAppTransition = transit;
2309 }
2310 mAppTransitionReady = false;
2311 mAppTransitionTimeout = false;
2312 mStartingIconInTransition = false;
2313 mSkipAppTransitionAnimation = false;
2314 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2315 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2316 5000);
2317 }
2318 }
2319 }
2320
2321 public int getPendingAppTransition() {
2322 return mNextAppTransition;
2323 }
Romain Guy06882f82009-06-10 13:36:04 -07002324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 public void executeAppTransition() {
2326 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2327 "executeAppTransition()")) {
2328 return;
2329 }
Romain Guy06882f82009-06-10 13:36:04 -07002330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 synchronized(mWindowMap) {
2332 if (DEBUG_APP_TRANSITIONS) Log.v(
2333 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2334 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2335 mAppTransitionReady = true;
2336 final long origId = Binder.clearCallingIdentity();
2337 performLayoutAndPlaceSurfacesLocked();
2338 Binder.restoreCallingIdentity(origId);
2339 }
2340 }
2341 }
2342
2343 public void setAppStartingWindow(IBinder token, String pkg,
2344 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2345 IBinder transferFrom, boolean createIfNeeded) {
2346 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2347 "setAppStartingIcon()")) {
2348 return;
2349 }
2350
2351 synchronized(mWindowMap) {
2352 if (DEBUG_STARTING_WINDOW) Log.v(
2353 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2354 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 AppWindowToken wtoken = findAppWindowToken(token);
2357 if (wtoken == null) {
2358 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2359 return;
2360 }
2361
2362 // If the display is frozen, we won't do anything until the
2363 // actual window is displayed so there is no reason to put in
2364 // the starting window.
2365 if (mDisplayFrozen) {
2366 return;
2367 }
Romain Guy06882f82009-06-10 13:36:04 -07002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 if (wtoken.startingData != null) {
2370 return;
2371 }
Romain Guy06882f82009-06-10 13:36:04 -07002372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 if (transferFrom != null) {
2374 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2375 if (ttoken != null) {
2376 WindowState startingWindow = ttoken.startingWindow;
2377 if (startingWindow != null) {
2378 if (mStartingIconInTransition) {
2379 // In this case, the starting icon has already
2380 // been displayed, so start letting windows get
2381 // shown immediately without any more transitions.
2382 mSkipAppTransitionAnimation = true;
2383 }
2384 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2385 "Moving existing starting from " + ttoken
2386 + " to " + wtoken);
2387 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 // Transfer the starting window over to the new
2390 // token.
2391 wtoken.startingData = ttoken.startingData;
2392 wtoken.startingView = ttoken.startingView;
2393 wtoken.startingWindow = startingWindow;
2394 ttoken.startingData = null;
2395 ttoken.startingView = null;
2396 ttoken.startingWindow = null;
2397 ttoken.startingMoved = true;
2398 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002399 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 startingWindow.mAppToken = wtoken;
2401 mWindows.remove(startingWindow);
2402 ttoken.windows.remove(startingWindow);
2403 ttoken.allAppWindows.remove(startingWindow);
2404 addWindowToListInOrderLocked(startingWindow, true);
2405 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 // Propagate other interesting state between the
2408 // tokens. If the old token is displayed, we should
2409 // immediately force the new one to be displayed. If
2410 // it is animating, we need to move that animation to
2411 // the new one.
2412 if (ttoken.allDrawn) {
2413 wtoken.allDrawn = true;
2414 }
2415 if (ttoken.firstWindowDrawn) {
2416 wtoken.firstWindowDrawn = true;
2417 }
2418 if (!ttoken.hidden) {
2419 wtoken.hidden = false;
2420 wtoken.hiddenRequested = false;
2421 wtoken.willBeHidden = false;
2422 }
2423 if (wtoken.clientHidden != ttoken.clientHidden) {
2424 wtoken.clientHidden = ttoken.clientHidden;
2425 wtoken.sendAppVisibilityToClients();
2426 }
2427 if (ttoken.animation != null) {
2428 wtoken.animation = ttoken.animation;
2429 wtoken.animating = ttoken.animating;
2430 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2431 ttoken.animation = null;
2432 ttoken.animLayerAdjustment = 0;
2433 wtoken.updateLayers();
2434 ttoken.updateLayers();
2435 }
Romain Guy06882f82009-06-10 13:36:04 -07002436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 mLayoutNeeded = true;
2439 performLayoutAndPlaceSurfacesLocked();
2440 Binder.restoreCallingIdentity(origId);
2441 return;
2442 } else if (ttoken.startingData != null) {
2443 // The previous app was getting ready to show a
2444 // starting window, but hasn't yet done so. Steal it!
2445 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2446 "Moving pending starting from " + ttoken
2447 + " to " + wtoken);
2448 wtoken.startingData = ttoken.startingData;
2449 ttoken.startingData = null;
2450 ttoken.startingMoved = true;
2451 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2452 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2453 // want to process the message ASAP, before any other queued
2454 // messages.
2455 mH.sendMessageAtFrontOfQueue(m);
2456 return;
2457 }
2458 }
2459 }
2460
2461 // There is no existing starting window, and the caller doesn't
2462 // want us to create one, so that's it!
2463 if (!createIfNeeded) {
2464 return;
2465 }
Romain Guy06882f82009-06-10 13:36:04 -07002466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 mStartingIconInTransition = true;
2468 wtoken.startingData = new StartingData(
2469 pkg, theme, nonLocalizedLabel,
2470 labelRes, icon);
2471 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2472 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2473 // want to process the message ASAP, before any other queued
2474 // messages.
2475 mH.sendMessageAtFrontOfQueue(m);
2476 }
2477 }
2478
2479 public void setAppWillBeHidden(IBinder token) {
2480 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2481 "setAppWillBeHidden()")) {
2482 return;
2483 }
2484
2485 AppWindowToken wtoken;
2486
2487 synchronized(mWindowMap) {
2488 wtoken = findAppWindowToken(token);
2489 if (wtoken == null) {
2490 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2491 return;
2492 }
2493 wtoken.willBeHidden = true;
2494 }
2495 }
Romain Guy06882f82009-06-10 13:36:04 -07002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2498 boolean visible, int transit, boolean performLayout) {
2499 boolean delayed = false;
2500
2501 if (wtoken.clientHidden == visible) {
2502 wtoken.clientHidden = !visible;
2503 wtoken.sendAppVisibilityToClients();
2504 }
Romain Guy06882f82009-06-10 13:36:04 -07002505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 wtoken.willBeHidden = false;
2507 if (wtoken.hidden == visible) {
2508 final int N = wtoken.allAppWindows.size();
2509 boolean changed = false;
2510 if (DEBUG_APP_TRANSITIONS) Log.v(
2511 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2512 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2517 if (wtoken.animation == sDummyAnimation) {
2518 wtoken.animation = null;
2519 }
2520 applyAnimationLocked(wtoken, lp, transit, visible);
2521 changed = true;
2522 if (wtoken.animation != null) {
2523 delayed = runningAppAnimation = true;
2524 }
2525 }
Romain Guy06882f82009-06-10 13:36:04 -07002526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 for (int i=0; i<N; i++) {
2528 WindowState win = wtoken.allAppWindows.get(i);
2529 if (win == wtoken.startingWindow) {
2530 continue;
2531 }
2532
2533 if (win.isAnimating()) {
2534 delayed = true;
2535 }
Romain Guy06882f82009-06-10 13:36:04 -07002536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2538 //win.dump(" ");
2539 if (visible) {
2540 if (!win.isVisibleNow()) {
2541 if (!runningAppAnimation) {
2542 applyAnimationLocked(win,
2543 WindowManagerPolicy.TRANSIT_ENTER, true);
2544 }
2545 changed = true;
2546 }
2547 } else if (win.isVisibleNow()) {
2548 if (!runningAppAnimation) {
2549 applyAnimationLocked(win,
2550 WindowManagerPolicy.TRANSIT_EXIT, false);
2551 }
2552 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2553 KeyWaiter.RETURN_NOTHING);
2554 changed = true;
2555 }
2556 }
2557
2558 wtoken.hidden = wtoken.hiddenRequested = !visible;
2559 if (!visible) {
2560 unsetAppFreezingScreenLocked(wtoken, true, true);
2561 } else {
2562 // If we are being set visible, and the starting window is
2563 // not yet displayed, then make sure it doesn't get displayed.
2564 WindowState swin = wtoken.startingWindow;
2565 if (swin != null && (swin.mDrawPending
2566 || swin.mCommitDrawPending)) {
2567 swin.mPolicyVisibility = false;
2568 swin.mPolicyVisibilityAfterAnim = false;
2569 }
2570 }
Romain Guy06882f82009-06-10 13:36:04 -07002571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2573 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2574 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 if (changed && performLayout) {
2577 mLayoutNeeded = true;
2578 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 performLayoutAndPlaceSurfacesLocked();
2580 }
2581 }
2582
2583 if (wtoken.animation != null) {
2584 delayed = true;
2585 }
Romain Guy06882f82009-06-10 13:36:04 -07002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 return delayed;
2588 }
2589
2590 public void setAppVisibility(IBinder token, boolean visible) {
2591 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2592 "setAppVisibility()")) {
2593 return;
2594 }
2595
2596 AppWindowToken wtoken;
2597
2598 synchronized(mWindowMap) {
2599 wtoken = findAppWindowToken(token);
2600 if (wtoken == null) {
2601 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2602 return;
2603 }
2604
2605 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2606 RuntimeException e = new RuntimeException();
2607 e.fillInStackTrace();
2608 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2609 + "): mNextAppTransition=" + mNextAppTransition
2610 + " hidden=" + wtoken.hidden
2611 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2612 }
Romain Guy06882f82009-06-10 13:36:04 -07002613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 // If we are preparing an app transition, then delay changing
2615 // the visibility of this token until we execute that transition.
2616 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2617 // Already in requested state, don't do anything more.
2618 if (wtoken.hiddenRequested != visible) {
2619 return;
2620 }
2621 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 if (DEBUG_APP_TRANSITIONS) Log.v(
2624 TAG, "Setting dummy animation on: " + wtoken);
2625 wtoken.setDummyAnimation();
2626 mOpeningApps.remove(wtoken);
2627 mClosingApps.remove(wtoken);
2628 wtoken.inPendingTransaction = true;
2629 if (visible) {
2630 mOpeningApps.add(wtoken);
2631 wtoken.allDrawn = false;
2632 wtoken.startingDisplayed = false;
2633 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 if (wtoken.clientHidden) {
2636 // In the case where we are making an app visible
2637 // but holding off for a transition, we still need
2638 // to tell the client to make its windows visible so
2639 // they get drawn. Otherwise, we will wait on
2640 // performing the transition until all windows have
2641 // been drawn, they never will be, and we are sad.
2642 wtoken.clientHidden = false;
2643 wtoken.sendAppVisibilityToClients();
2644 }
2645 } else {
2646 mClosingApps.add(wtoken);
2647 }
2648 return;
2649 }
Romain Guy06882f82009-06-10 13:36:04 -07002650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 final long origId = Binder.clearCallingIdentity();
2652 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2653 wtoken.updateReportedVisibilityLocked();
2654 Binder.restoreCallingIdentity(origId);
2655 }
2656 }
2657
2658 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2659 boolean unfreezeSurfaceNow, boolean force) {
2660 if (wtoken.freezingScreen) {
2661 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2662 + " force=" + force);
2663 final int N = wtoken.allAppWindows.size();
2664 boolean unfrozeWindows = false;
2665 for (int i=0; i<N; i++) {
2666 WindowState w = wtoken.allAppWindows.get(i);
2667 if (w.mAppFreezing) {
2668 w.mAppFreezing = false;
2669 if (w.mSurface != null && !w.mOrientationChanging) {
2670 w.mOrientationChanging = true;
2671 }
2672 unfrozeWindows = true;
2673 }
2674 }
2675 if (force || unfrozeWindows) {
2676 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2677 wtoken.freezingScreen = false;
2678 mAppsFreezingScreen--;
2679 }
2680 if (unfreezeSurfaceNow) {
2681 if (unfrozeWindows) {
2682 mLayoutNeeded = true;
2683 performLayoutAndPlaceSurfacesLocked();
2684 }
2685 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2686 stopFreezingDisplayLocked();
2687 }
2688 }
2689 }
2690 }
Romain Guy06882f82009-06-10 13:36:04 -07002691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2693 int configChanges) {
2694 if (DEBUG_ORIENTATION) {
2695 RuntimeException e = new RuntimeException();
2696 e.fillInStackTrace();
2697 Log.i(TAG, "Set freezing of " + wtoken.appToken
2698 + ": hidden=" + wtoken.hidden + " freezing="
2699 + wtoken.freezingScreen, e);
2700 }
2701 if (!wtoken.hiddenRequested) {
2702 if (!wtoken.freezingScreen) {
2703 wtoken.freezingScreen = true;
2704 mAppsFreezingScreen++;
2705 if (mAppsFreezingScreen == 1) {
2706 startFreezingDisplayLocked();
2707 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2708 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2709 5000);
2710 }
2711 }
2712 final int N = wtoken.allAppWindows.size();
2713 for (int i=0; i<N; i++) {
2714 WindowState w = wtoken.allAppWindows.get(i);
2715 w.mAppFreezing = true;
2716 }
2717 }
2718 }
Romain Guy06882f82009-06-10 13:36:04 -07002719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 public void startAppFreezingScreen(IBinder token, int configChanges) {
2721 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2722 "setAppFreezingScreen()")) {
2723 return;
2724 }
2725
2726 synchronized(mWindowMap) {
2727 if (configChanges == 0 && !mDisplayFrozen) {
2728 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2729 return;
2730 }
Romain Guy06882f82009-06-10 13:36:04 -07002731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 AppWindowToken wtoken = findAppWindowToken(token);
2733 if (wtoken == null || wtoken.appToken == null) {
2734 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2735 return;
2736 }
2737 final long origId = Binder.clearCallingIdentity();
2738 startAppFreezingScreenLocked(wtoken, configChanges);
2739 Binder.restoreCallingIdentity(origId);
2740 }
2741 }
Romain Guy06882f82009-06-10 13:36:04 -07002742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 public void stopAppFreezingScreen(IBinder token, boolean force) {
2744 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2745 "setAppFreezingScreen()")) {
2746 return;
2747 }
2748
2749 synchronized(mWindowMap) {
2750 AppWindowToken wtoken = findAppWindowToken(token);
2751 if (wtoken == null || wtoken.appToken == null) {
2752 return;
2753 }
2754 final long origId = Binder.clearCallingIdentity();
2755 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2756 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2757 unsetAppFreezingScreenLocked(wtoken, true, force);
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 removeAppToken(IBinder token) {
2763 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2764 "removeAppToken()")) {
2765 return;
2766 }
2767
2768 AppWindowToken wtoken = null;
2769 AppWindowToken startingToken = null;
2770 boolean delayed = false;
2771
2772 final long origId = Binder.clearCallingIdentity();
2773 synchronized(mWindowMap) {
2774 WindowToken basewtoken = mTokenMap.remove(token);
2775 mTokenList.remove(basewtoken);
2776 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2777 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2778 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2779 wtoken.inPendingTransaction = false;
2780 mOpeningApps.remove(wtoken);
2781 if (mClosingApps.contains(wtoken)) {
2782 delayed = true;
2783 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2784 mClosingApps.add(wtoken);
2785 delayed = true;
2786 }
2787 if (DEBUG_APP_TRANSITIONS) Log.v(
2788 TAG, "Removing app " + wtoken + " delayed=" + delayed
2789 + " animation=" + wtoken.animation
2790 + " animating=" + wtoken.animating);
2791 if (delayed) {
2792 // set the token aside because it has an active animation to be finished
2793 mExitingAppTokens.add(wtoken);
2794 }
2795 mAppTokens.remove(wtoken);
2796 wtoken.removed = true;
2797 if (wtoken.startingData != null) {
2798 startingToken = wtoken;
2799 }
2800 unsetAppFreezingScreenLocked(wtoken, true, true);
2801 if (mFocusedApp == wtoken) {
2802 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2803 mFocusedApp = null;
2804 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2805 mKeyWaiter.tickle();
2806 }
2807 } else {
2808 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2809 }
Romain Guy06882f82009-06-10 13:36:04 -07002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 if (!delayed && wtoken != null) {
2812 wtoken.updateReportedVisibilityLocked();
2813 }
2814 }
2815 Binder.restoreCallingIdentity(origId);
2816
2817 if (startingToken != null) {
2818 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2819 + startingToken + ": app token removed");
2820 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2821 mH.sendMessage(m);
2822 }
2823 }
2824
2825 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2826 final int NW = token.windows.size();
2827 for (int i=0; i<NW; i++) {
2828 WindowState win = token.windows.get(i);
2829 mWindows.remove(win);
2830 int j = win.mChildWindows.size();
2831 while (j > 0) {
2832 j--;
2833 mWindows.remove(win.mChildWindows.get(j));
2834 }
2835 }
2836 return NW > 0;
2837 }
2838
2839 void dumpAppTokensLocked() {
2840 for (int i=mAppTokens.size()-1; i>=0; i--) {
2841 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2842 }
2843 }
Romain Guy06882f82009-06-10 13:36:04 -07002844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 void dumpWindowsLocked() {
2846 for (int i=mWindows.size()-1; i>=0; i--) {
2847 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2848 }
2849 }
Romain Guy06882f82009-06-10 13:36:04 -07002850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 private int findWindowOffsetLocked(int tokenPos) {
2852 final int NW = mWindows.size();
2853
2854 if (tokenPos >= mAppTokens.size()) {
2855 int i = NW;
2856 while (i > 0) {
2857 i--;
2858 WindowState win = (WindowState)mWindows.get(i);
2859 if (win.getAppToken() != null) {
2860 return i+1;
2861 }
2862 }
2863 }
2864
2865 while (tokenPos > 0) {
2866 // Find the first app token below the new position that has
2867 // a window displayed.
2868 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2869 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2870 + tokenPos + " -- " + wtoken.token);
2871 int i = wtoken.windows.size();
2872 while (i > 0) {
2873 i--;
2874 WindowState win = wtoken.windows.get(i);
2875 int j = win.mChildWindows.size();
2876 while (j > 0) {
2877 j--;
2878 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2879 if (cwin.mSubLayer >= 0 ) {
2880 for (int pos=NW-1; pos>=0; pos--) {
2881 if (mWindows.get(pos) == cwin) {
2882 if (DEBUG_REORDER) Log.v(TAG,
2883 "Found child win @" + (pos+1));
2884 return pos+1;
2885 }
2886 }
2887 }
2888 }
2889 for (int pos=NW-1; pos>=0; pos--) {
2890 if (mWindows.get(pos) == win) {
2891 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2892 return pos+1;
2893 }
2894 }
2895 }
2896 tokenPos--;
2897 }
2898
2899 return 0;
2900 }
2901
2902 private final int reAddWindowLocked(int index, WindowState win) {
2903 final int NCW = win.mChildWindows.size();
2904 boolean added = false;
2905 for (int j=0; j<NCW; j++) {
2906 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2907 if (!added && cwin.mSubLayer >= 0) {
2908 mWindows.add(index, win);
2909 index++;
2910 added = true;
2911 }
2912 mWindows.add(index, cwin);
2913 index++;
2914 }
2915 if (!added) {
2916 mWindows.add(index, win);
2917 index++;
2918 }
2919 return index;
2920 }
Romain Guy06882f82009-06-10 13:36:04 -07002921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2923 final int NW = token.windows.size();
2924 for (int i=0; i<NW; i++) {
2925 index = reAddWindowLocked(index, token.windows.get(i));
2926 }
2927 return index;
2928 }
2929
2930 public void moveAppToken(int index, IBinder token) {
2931 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2932 "moveAppToken()")) {
2933 return;
2934 }
2935
2936 synchronized(mWindowMap) {
2937 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2938 if (DEBUG_REORDER) dumpAppTokensLocked();
2939 final AppWindowToken wtoken = findAppWindowToken(token);
2940 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2941 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2942 + token + " (" + wtoken + ")");
2943 return;
2944 }
2945 mAppTokens.add(index, wtoken);
2946 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2947 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 final long origId = Binder.clearCallingIdentity();
2950 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
2951 if (DEBUG_REORDER) dumpWindowsLocked();
2952 if (tmpRemoveAppWindowsLocked(wtoken)) {
2953 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
2954 if (DEBUG_REORDER) dumpWindowsLocked();
2955 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
2956 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
2957 if (DEBUG_REORDER) dumpWindowsLocked();
2958 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 mLayoutNeeded = true;
2960 performLayoutAndPlaceSurfacesLocked();
2961 }
2962 Binder.restoreCallingIdentity(origId);
2963 }
2964 }
2965
2966 private void removeAppTokensLocked(List<IBinder> tokens) {
2967 // XXX This should be done more efficiently!
2968 // (take advantage of the fact that both lists should be
2969 // ordered in the same way.)
2970 int N = tokens.size();
2971 for (int i=0; i<N; i++) {
2972 IBinder token = tokens.get(i);
2973 final AppWindowToken wtoken = findAppWindowToken(token);
2974 if (!mAppTokens.remove(wtoken)) {
2975 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2976 + token + " (" + wtoken + ")");
2977 i--;
2978 N--;
2979 }
2980 }
2981 }
2982
2983 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
2984 // First remove all of the windows from the list.
2985 final int N = tokens.size();
2986 int i;
2987 for (i=0; i<N; i++) {
2988 WindowToken token = mTokenMap.get(tokens.get(i));
2989 if (token != null) {
2990 tmpRemoveAppWindowsLocked(token);
2991 }
2992 }
2993
2994 // Where to start adding?
2995 int pos = findWindowOffsetLocked(tokenPos);
2996
2997 // And now add them back at the correct place.
2998 for (i=0; i<N; i++) {
2999 WindowToken token = mTokenMap.get(tokens.get(i));
3000 if (token != null) {
3001 pos = reAddAppWindowsLocked(pos, token);
3002 }
3003 }
3004
3005 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 mLayoutNeeded = true;
3007 performLayoutAndPlaceSurfacesLocked();
3008
3009 //dump();
3010 }
3011
3012 public void moveAppTokensToTop(List<IBinder> tokens) {
3013 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3014 "moveAppTokensToTop()")) {
3015 return;
3016 }
3017
3018 final long origId = Binder.clearCallingIdentity();
3019 synchronized(mWindowMap) {
3020 removeAppTokensLocked(tokens);
3021 final int N = tokens.size();
3022 for (int i=0; i<N; i++) {
3023 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3024 if (wt != null) {
3025 mAppTokens.add(wt);
3026 }
3027 }
3028 moveAppWindowsLocked(tokens, mAppTokens.size());
3029 }
3030 Binder.restoreCallingIdentity(origId);
3031 }
3032
3033 public void moveAppTokensToBottom(List<IBinder> tokens) {
3034 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3035 "moveAppTokensToBottom()")) {
3036 return;
3037 }
3038
3039 final long origId = Binder.clearCallingIdentity();
3040 synchronized(mWindowMap) {
3041 removeAppTokensLocked(tokens);
3042 final int N = tokens.size();
3043 int pos = 0;
3044 for (int i=0; i<N; i++) {
3045 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3046 if (wt != null) {
3047 mAppTokens.add(pos, wt);
3048 pos++;
3049 }
3050 }
3051 moveAppWindowsLocked(tokens, 0);
3052 }
3053 Binder.restoreCallingIdentity(origId);
3054 }
3055
3056 // -------------------------------------------------------------
3057 // Misc IWindowSession methods
3058 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 public void disableKeyguard(IBinder token, String tag) {
3061 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3062 != PackageManager.PERMISSION_GRANTED) {
3063 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3064 }
3065 mKeyguardDisabled.acquire(token, tag);
3066 }
3067
3068 public void reenableKeyguard(IBinder token) {
3069 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3070 != PackageManager.PERMISSION_GRANTED) {
3071 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3072 }
3073 synchronized (mKeyguardDisabled) {
3074 mKeyguardDisabled.release(token);
3075
3076 if (!mKeyguardDisabled.isAcquired()) {
3077 // if we are the last one to reenable the keyguard wait until
3078 // we have actaully finished reenabling until returning
3079 mWaitingUntilKeyguardReenabled = true;
3080 while (mWaitingUntilKeyguardReenabled) {
3081 try {
3082 mKeyguardDisabled.wait();
3083 } catch (InterruptedException e) {
3084 Thread.currentThread().interrupt();
3085 }
3086 }
3087 }
3088 }
3089 }
3090
3091 /**
3092 * @see android.app.KeyguardManager#exitKeyguardSecurely
3093 */
3094 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3095 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3096 != PackageManager.PERMISSION_GRANTED) {
3097 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3098 }
3099 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3100 public void onKeyguardExitResult(boolean success) {
3101 try {
3102 callback.onKeyguardExitResult(success);
3103 } catch (RemoteException e) {
3104 // Client has died, we don't care.
3105 }
3106 }
3107 });
3108 }
3109
3110 public boolean inKeyguardRestrictedInputMode() {
3111 return mPolicy.inKeyguardRestrictedKeyInputMode();
3112 }
Romain Guy06882f82009-06-10 13:36:04 -07003113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 static float fixScale(float scale) {
3115 if (scale < 0) scale = 0;
3116 else if (scale > 20) scale = 20;
3117 return Math.abs(scale);
3118 }
Romain Guy06882f82009-06-10 13:36:04 -07003119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 public void setAnimationScale(int which, float scale) {
3121 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3122 "setAnimationScale()")) {
3123 return;
3124 }
3125
3126 if (scale < 0) scale = 0;
3127 else if (scale > 20) scale = 20;
3128 scale = Math.abs(scale);
3129 switch (which) {
3130 case 0: mWindowAnimationScale = fixScale(scale); break;
3131 case 1: mTransitionAnimationScale = fixScale(scale); break;
3132 }
Romain Guy06882f82009-06-10 13:36:04 -07003133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 // Persist setting
3135 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3136 }
Romain Guy06882f82009-06-10 13:36:04 -07003137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 public void setAnimationScales(float[] scales) {
3139 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3140 "setAnimationScale()")) {
3141 return;
3142 }
3143
3144 if (scales != null) {
3145 if (scales.length >= 1) {
3146 mWindowAnimationScale = fixScale(scales[0]);
3147 }
3148 if (scales.length >= 2) {
3149 mTransitionAnimationScale = fixScale(scales[1]);
3150 }
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 float getAnimationScale(int which) {
3158 switch (which) {
3159 case 0: return mWindowAnimationScale;
3160 case 1: return mTransitionAnimationScale;
3161 }
3162 return 0;
3163 }
Romain Guy06882f82009-06-10 13:36:04 -07003164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 public float[] getAnimationScales() {
3166 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3167 }
Romain Guy06882f82009-06-10 13:36:04 -07003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 public int getSwitchState(int sw) {
3170 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3171 "getSwitchState()")) {
3172 return -1;
3173 }
3174 return KeyInputQueue.getSwitchState(sw);
3175 }
Romain Guy06882f82009-06-10 13:36:04 -07003176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 public int getSwitchStateForDevice(int devid, int sw) {
3178 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3179 "getSwitchStateForDevice()")) {
3180 return -1;
3181 }
3182 return KeyInputQueue.getSwitchState(devid, sw);
3183 }
Romain Guy06882f82009-06-10 13:36:04 -07003184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 public int getScancodeState(int sw) {
3186 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3187 "getScancodeState()")) {
3188 return -1;
3189 }
3190 return KeyInputQueue.getScancodeState(sw);
3191 }
Romain Guy06882f82009-06-10 13:36:04 -07003192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 public int getScancodeStateForDevice(int devid, int sw) {
3194 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3195 "getScancodeStateForDevice()")) {
3196 return -1;
3197 }
3198 return KeyInputQueue.getScancodeState(devid, sw);
3199 }
Romain Guy06882f82009-06-10 13:36:04 -07003200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 public int getKeycodeState(int sw) {
3202 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3203 "getKeycodeState()")) {
3204 return -1;
3205 }
3206 return KeyInputQueue.getKeycodeState(sw);
3207 }
Romain Guy06882f82009-06-10 13:36:04 -07003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 public int getKeycodeStateForDevice(int devid, int sw) {
3210 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3211 "getKeycodeStateForDevice()")) {
3212 return -1;
3213 }
3214 return KeyInputQueue.getKeycodeState(devid, sw);
3215 }
Romain Guy06882f82009-06-10 13:36:04 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3218 return KeyInputQueue.hasKeys(keycodes, keyExists);
3219 }
Romain Guy06882f82009-06-10 13:36:04 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 public void enableScreenAfterBoot() {
3222 synchronized(mWindowMap) {
3223 if (mSystemBooted) {
3224 return;
3225 }
3226 mSystemBooted = true;
3227 }
Romain Guy06882f82009-06-10 13:36:04 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 performEnableScreen();
3230 }
Romain Guy06882f82009-06-10 13:36:04 -07003231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 public void enableScreenIfNeededLocked() {
3233 if (mDisplayEnabled) {
3234 return;
3235 }
3236 if (!mSystemBooted) {
3237 return;
3238 }
3239 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3240 }
Romain Guy06882f82009-06-10 13:36:04 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 public void performEnableScreen() {
3243 synchronized(mWindowMap) {
3244 if (mDisplayEnabled) {
3245 return;
3246 }
3247 if (!mSystemBooted) {
3248 return;
3249 }
Romain Guy06882f82009-06-10 13:36:04 -07003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 // Don't enable the screen until all existing windows
3252 // have been drawn.
3253 final int N = mWindows.size();
3254 for (int i=0; i<N; i++) {
3255 WindowState w = (WindowState)mWindows.get(i);
3256 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3257 return;
3258 }
3259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 mDisplayEnabled = true;
3262 if (false) {
3263 Log.i(TAG, "ENABLING SCREEN!");
3264 StringWriter sw = new StringWriter();
3265 PrintWriter pw = new PrintWriter(sw);
3266 this.dump(null, pw, null);
3267 Log.i(TAG, sw.toString());
3268 }
3269 try {
3270 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3271 if (surfaceFlinger != null) {
3272 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3273 Parcel data = Parcel.obtain();
3274 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3275 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3276 data, null, 0);
3277 data.recycle();
3278 }
3279 } catch (RemoteException ex) {
3280 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3281 }
3282 }
Romain Guy06882f82009-06-10 13:36:04 -07003283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003287 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3288 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 }
Romain Guy06882f82009-06-10 13:36:04 -07003290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 public void setInTouchMode(boolean mode) {
3292 synchronized(mWindowMap) {
3293 mInTouchMode = mode;
3294 }
3295 }
3296
Romain Guy06882f82009-06-10 13:36:04 -07003297 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003298 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003300 "setRotation()")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 return;
3302 }
3303
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003304 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 }
Romain Guy06882f82009-06-10 13:36:04 -07003306
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003307 public void setRotationUnchecked(int rotation,
3308 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 if(DEBUG_ORIENTATION) Log.v(TAG,
3310 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 long origId = Binder.clearCallingIdentity();
3313 boolean changed;
3314 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003315 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 }
Romain Guy06882f82009-06-10 13:36:04 -07003317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 if (changed) {
3319 sendNewConfiguration();
3320 synchronized(mWindowMap) {
3321 mLayoutNeeded = true;
3322 performLayoutAndPlaceSurfacesLocked();
3323 }
3324 } else if (alwaysSendConfiguration) {
3325 //update configuration ignoring orientation change
3326 sendNewConfiguration();
3327 }
Romain Guy06882f82009-06-10 13:36:04 -07003328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 Binder.restoreCallingIdentity(origId);
3330 }
Romain Guy06882f82009-06-10 13:36:04 -07003331
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003332 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 boolean changed;
3334 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3335 rotation = mRequestedRotation;
3336 } else {
3337 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003338 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 }
3340 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003341 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 mRotation, mDisplayEnabled);
3343 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3344 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003347 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 "Rotation changed to " + rotation
3349 + " from " + mRotation
3350 + " (forceApp=" + mForcedAppOrientation
3351 + ", req=" + mRequestedRotation + ")");
3352 mRotation = rotation;
3353 mWindowsFreezingScreen = true;
3354 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3355 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3356 2000);
3357 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003358 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 mQueue.setOrientation(rotation);
3360 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003361 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 }
3363 for (int i=mWindows.size()-1; i>=0; i--) {
3364 WindowState w = (WindowState)mWindows.get(i);
3365 if (w.mSurface != null) {
3366 w.mOrientationChanging = true;
3367 }
3368 }
3369 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3370 try {
3371 mRotationWatchers.get(i).onRotationChanged(rotation);
3372 } catch (RemoteException e) {
3373 }
3374 }
3375 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 return changed;
3378 }
Romain Guy06882f82009-06-10 13:36:04 -07003379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 public int getRotation() {
3381 return mRotation;
3382 }
3383
3384 public int watchRotation(IRotationWatcher watcher) {
3385 final IBinder watcherBinder = watcher.asBinder();
3386 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3387 public void binderDied() {
3388 synchronized (mWindowMap) {
3389 for (int i=0; i<mRotationWatchers.size(); i++) {
3390 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
3391 mRotationWatchers.remove(i);
3392 i--;
3393 }
3394 }
3395 }
3396 }
3397 };
Romain Guy06882f82009-06-10 13:36:04 -07003398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 synchronized (mWindowMap) {
3400 try {
3401 watcher.asBinder().linkToDeath(dr, 0);
3402 mRotationWatchers.add(watcher);
3403 } catch (RemoteException e) {
3404 // Client died, no cleanup needed.
3405 }
Romain Guy06882f82009-06-10 13:36:04 -07003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 return mRotation;
3408 }
3409 }
3410
3411 /**
3412 * Starts the view server on the specified port.
3413 *
3414 * @param port The port to listener to.
3415 *
3416 * @return True if the server was successfully started, false otherwise.
3417 *
3418 * @see com.android.server.ViewServer
3419 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3420 */
3421 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003422 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 return false;
3424 }
3425
3426 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3427 return false;
3428 }
3429
3430 if (port < 1024) {
3431 return false;
3432 }
3433
3434 if (mViewServer != null) {
3435 if (!mViewServer.isRunning()) {
3436 try {
3437 return mViewServer.start();
3438 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003439 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 }
3441 }
3442 return false;
3443 }
3444
3445 try {
3446 mViewServer = new ViewServer(this, port);
3447 return mViewServer.start();
3448 } catch (IOException e) {
3449 Log.w(TAG, "View server did not start");
3450 }
3451 return false;
3452 }
3453
Romain Guy06882f82009-06-10 13:36:04 -07003454 private boolean isSystemSecure() {
3455 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3456 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3457 }
3458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 /**
3460 * Stops the view server if it exists.
3461 *
3462 * @return True if the server stopped, false if it wasn't started or
3463 * couldn't be stopped.
3464 *
3465 * @see com.android.server.ViewServer
3466 */
3467 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003468 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 return false;
3470 }
3471
3472 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3473 return false;
3474 }
3475
3476 if (mViewServer != null) {
3477 return mViewServer.stop();
3478 }
3479 return false;
3480 }
3481
3482 /**
3483 * Indicates whether the view server is running.
3484 *
3485 * @return True if the server is running, false otherwise.
3486 *
3487 * @see com.android.server.ViewServer
3488 */
3489 public boolean isViewServerRunning() {
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, "isViewServerRunning")) {
3495 return false;
3496 }
3497
3498 return mViewServer != null && mViewServer.isRunning();
3499 }
3500
3501 /**
3502 * Lists all availble windows in the system. The listing is written in the
3503 * specified Socket's output stream with the following syntax:
3504 * windowHashCodeInHexadecimal windowName
3505 * Each line of the ouput represents a different window.
3506 *
3507 * @param client The remote client to send the listing to.
3508 * @return False if an error occured, true otherwise.
3509 */
3510 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003511 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 return false;
3513 }
3514
3515 boolean result = true;
3516
3517 Object[] windows;
3518 synchronized (mWindowMap) {
3519 windows = new Object[mWindows.size()];
3520 //noinspection unchecked
3521 windows = mWindows.toArray(windows);
3522 }
3523
3524 BufferedWriter out = null;
3525
3526 // Any uncaught exception will crash the system process
3527 try {
3528 OutputStream clientStream = client.getOutputStream();
3529 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3530
3531 final int count = windows.length;
3532 for (int i = 0; i < count; i++) {
3533 final WindowState w = (WindowState) windows[i];
3534 out.write(Integer.toHexString(System.identityHashCode(w)));
3535 out.write(' ');
3536 out.append(w.mAttrs.getTitle());
3537 out.write('\n');
3538 }
3539
3540 out.write("DONE.\n");
3541 out.flush();
3542 } catch (Exception e) {
3543 result = false;
3544 } finally {
3545 if (out != null) {
3546 try {
3547 out.close();
3548 } catch (IOException e) {
3549 result = false;
3550 }
3551 }
3552 }
3553
3554 return result;
3555 }
3556
3557 /**
3558 * Sends a command to a target window. The result of the command, if any, will be
3559 * written in the output stream of the specified socket.
3560 *
3561 * The parameters must follow this syntax:
3562 * windowHashcode extra
3563 *
3564 * Where XX is the length in characeters of the windowTitle.
3565 *
3566 * The first parameter is the target window. The window with the specified hashcode
3567 * will be the target. If no target can be found, nothing happens. The extra parameters
3568 * will be delivered to the target window and as parameters to the command itself.
3569 *
3570 * @param client The remote client to sent the result, if any, to.
3571 * @param command The command to execute.
3572 * @param parameters The command parameters.
3573 *
3574 * @return True if the command was successfully delivered, false otherwise. This does
3575 * not indicate whether the command itself was successful.
3576 */
3577 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003578 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 return false;
3580 }
3581
3582 boolean success = true;
3583 Parcel data = null;
3584 Parcel reply = null;
3585
3586 // Any uncaught exception will crash the system process
3587 try {
3588 // Find the hashcode of the window
3589 int index = parameters.indexOf(' ');
3590 if (index == -1) {
3591 index = parameters.length();
3592 }
3593 final String code = parameters.substring(0, index);
3594 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3595
3596 // Extract the command's parameter after the window description
3597 if (index < parameters.length()) {
3598 parameters = parameters.substring(index + 1);
3599 } else {
3600 parameters = "";
3601 }
3602
3603 final WindowManagerService.WindowState window = findWindow(hashCode);
3604 if (window == null) {
3605 return false;
3606 }
3607
3608 data = Parcel.obtain();
3609 data.writeInterfaceToken("android.view.IWindow");
3610 data.writeString(command);
3611 data.writeString(parameters);
3612 data.writeInt(1);
3613 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3614
3615 reply = Parcel.obtain();
3616
3617 final IBinder binder = window.mClient.asBinder();
3618 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3619 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3620
3621 reply.readException();
3622
3623 } catch (Exception e) {
3624 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3625 success = false;
3626 } finally {
3627 if (data != null) {
3628 data.recycle();
3629 }
3630 if (reply != null) {
3631 reply.recycle();
3632 }
3633 }
3634
3635 return success;
3636 }
3637
3638 private WindowState findWindow(int hashCode) {
3639 if (hashCode == -1) {
3640 return getFocusedWindow();
3641 }
3642
3643 synchronized (mWindowMap) {
3644 final ArrayList windows = mWindows;
3645 final int count = windows.size();
3646
3647 for (int i = 0; i < count; i++) {
3648 WindowState w = (WindowState) windows.get(i);
3649 if (System.identityHashCode(w) == hashCode) {
3650 return w;
3651 }
3652 }
3653 }
3654
3655 return null;
3656 }
3657
3658 /*
3659 * Instruct the Activity Manager to fetch the current configuration and broadcast
3660 * that to config-changed listeners if appropriate.
3661 */
3662 void sendNewConfiguration() {
3663 try {
3664 mActivityManager.updateConfiguration(null);
3665 } catch (RemoteException e) {
3666 }
3667 }
Romain Guy06882f82009-06-10 13:36:04 -07003668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 public Configuration computeNewConfiguration() {
3670 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003671 return computeNewConfigurationLocked();
3672 }
3673 }
Romain Guy06882f82009-06-10 13:36:04 -07003674
Dianne Hackbornc485a602009-03-24 22:39:49 -07003675 Configuration computeNewConfigurationLocked() {
3676 Configuration config = new Configuration();
3677 if (!computeNewConfigurationLocked(config)) {
3678 return null;
3679 }
3680 Log.i(TAG, "Config changed: " + config);
3681 long now = SystemClock.uptimeMillis();
3682 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3683 if (mFreezeGcPending != 0) {
3684 if (now > (mFreezeGcPending+1000)) {
3685 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3686 mH.removeMessages(H.FORCE_GC);
3687 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 mFreezeGcPending = now;
3689 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003690 } else {
3691 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003693 return config;
3694 }
Romain Guy06882f82009-06-10 13:36:04 -07003695
Dianne Hackbornc485a602009-03-24 22:39:49 -07003696 boolean computeNewConfigurationLocked(Configuration config) {
3697 if (mDisplay == null) {
3698 return false;
3699 }
3700 mQueue.getInputConfiguration(config);
3701 final int dw = mDisplay.getWidth();
3702 final int dh = mDisplay.getHeight();
3703 int orientation = Configuration.ORIENTATION_SQUARE;
3704 if (dw < dh) {
3705 orientation = Configuration.ORIENTATION_PORTRAIT;
3706 } else if (dw > dh) {
3707 orientation = Configuration.ORIENTATION_LANDSCAPE;
3708 }
3709 config.orientation = orientation;
3710 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3711 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3712 mPolicy.adjustConfigurationLw(config);
3713 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 }
Romain Guy06882f82009-06-10 13:36:04 -07003715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 // -------------------------------------------------------------
3717 // Input Events and Focus Management
3718 // -------------------------------------------------------------
3719
3720 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003721 long curTime = SystemClock.uptimeMillis();
3722
3723 if (eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
3724 if (mLastTouchEventType == eventType &&
3725 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3726 return;
3727 }
3728 mLastUserActivityCallTime = curTime;
3729 mLastTouchEventType = eventType;
3730 }
3731
3732 if (targetWin == null
3733 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3734 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 }
3736 }
3737
3738 // tells if it's a cheek event or not -- this function is stateful
3739 private static final int EVENT_NONE = 0;
3740 private static final int EVENT_UNKNOWN = 0;
3741 private static final int EVENT_CHEEK = 0;
3742 private static final int EVENT_IGNORE_DURATION = 300; // ms
3743 private static final float CHEEK_THRESHOLD = 0.6f;
3744 private int mEventState = EVENT_NONE;
3745 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 private int eventType(MotionEvent ev) {
3748 float size = ev.getSize();
3749 switch (ev.getAction()) {
3750 case MotionEvent.ACTION_DOWN:
3751 mEventSize = size;
3752 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3753 case MotionEvent.ACTION_UP:
3754 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003755 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 case MotionEvent.ACTION_MOVE:
3757 final int N = ev.getHistorySize();
3758 if (size > mEventSize) mEventSize = size;
3759 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3760 for (int i=0; i<N; i++) {
3761 size = ev.getHistoricalSize(i);
3762 if (size > mEventSize) mEventSize = size;
3763 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3764 }
3765 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3766 return TOUCH_EVENT;
3767 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003768 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 }
3770 default:
3771 // not good
3772 return OTHER_EVENT;
3773 }
3774 }
3775
3776 /**
3777 * @return Returns true if event was dispatched, false if it was dropped for any reason
3778 */
3779 private boolean dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
3780 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3781 "dispatchPointer " + ev);
3782
3783 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
3784 ev, true, false);
Romain Guy06882f82009-06-10 13:36:04 -07003785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 if (action == MotionEvent.ACTION_UP) {
3789 // let go of our target
3790 mKeyWaiter.mMotionTarget = null;
3791 mPowerManager.logPointerUpEvent();
3792 } else if (action == MotionEvent.ACTION_DOWN) {
3793 mPowerManager.logPointerDownEvent();
3794 }
3795
3796 if (targetObj == null) {
3797 // In this case we are either dropping the event, or have received
3798 // a move or up without a down. It is common to receive move
3799 // events in such a way, since this means the user is moving the
3800 // pointer without actually pressing down. All other cases should
3801 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003802 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3804 }
3805 if (qev != null) {
3806 mQueue.recycleEvent(qev);
3807 }
3808 ev.recycle();
3809 return false;
3810 }
3811 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3812 if (qev != null) {
3813 mQueue.recycleEvent(qev);
3814 }
3815 ev.recycle();
3816 return true;
3817 }
Romain Guy06882f82009-06-10 13:36:04 -07003818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07003822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 //Log.i(TAG, "Sending " + ev + " to " + target);
3824
3825 if (uid != 0 && uid != target.mSession.mUid) {
3826 if (mContext.checkPermission(
3827 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3828 != PackageManager.PERMISSION_GRANTED) {
3829 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3830 + pid + " uid " + uid + " to window " + target
3831 + " owned by uid " + target.mSession.mUid);
3832 if (qev != null) {
3833 mQueue.recycleEvent(qev);
3834 }
3835 ev.recycle();
3836 return false;
3837 }
3838 }
Romain Guy06882f82009-06-10 13:36:04 -07003839
3840 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3842 //target wants to ignore fat touch events
3843 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3844 //explicit flag to return without processing event further
3845 boolean returnFlag = false;
3846 if((action == MotionEvent.ACTION_DOWN)) {
3847 mFatTouch = false;
3848 if(cheekPress) {
3849 mFatTouch = true;
3850 returnFlag = true;
3851 }
3852 } else {
3853 if(action == MotionEvent.ACTION_UP) {
3854 if(mFatTouch) {
3855 //earlier even was invalid doesnt matter if current up is cheekpress or not
3856 mFatTouch = false;
3857 returnFlag = true;
3858 } else if(cheekPress) {
3859 //cancel the earlier event
3860 ev.setAction(MotionEvent.ACTION_CANCEL);
3861 action = MotionEvent.ACTION_CANCEL;
3862 }
3863 } else if(action == MotionEvent.ACTION_MOVE) {
3864 if(mFatTouch) {
3865 //two cases here
3866 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003867 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 returnFlag = true;
3869 } else if(cheekPress) {
3870 //valid down followed by invalid moves
3871 //an invalid move have to cancel earlier action
3872 ev.setAction(MotionEvent.ACTION_CANCEL);
3873 action = MotionEvent.ACTION_CANCEL;
3874 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3875 //note that the subsequent invalid moves will not get here
3876 mFatTouch = true;
3877 }
3878 }
3879 } //else if action
3880 if(returnFlag) {
3881 //recycle que, ev
3882 if (qev != null) {
3883 mQueue.recycleEvent(qev);
3884 }
3885 ev.recycle();
3886 return false;
3887 }
3888 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07003889
3890 // TODO remove once we settle on a value or make it app specific
3891 if (action == MotionEvent.ACTION_DOWN) {
3892 int max_events_per_sec = 35;
3893 try {
3894 max_events_per_sec = Integer.parseInt(SystemProperties
3895 .get("windowsmgr.max_events_per_sec"));
3896 if (max_events_per_sec < 1) {
3897 max_events_per_sec = 35;
3898 }
3899 } catch (NumberFormatException e) {
3900 }
3901 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
3902 }
3903
3904 /*
3905 * Throttle events to minimize CPU usage when there's a flood of events
3906 * e.g. constant contact with the screen
3907 */
3908 if (action == MotionEvent.ACTION_MOVE) {
3909 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
3910 long now = SystemClock.uptimeMillis();
3911 if (now < nextEventTime) {
3912 try {
3913 Thread.sleep(nextEventTime - now);
3914 } catch (InterruptedException e) {
3915 }
3916 mLastTouchEventTime = nextEventTime;
3917 } else {
3918 mLastTouchEventTime = now;
3919 }
3920 }
3921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 synchronized(mWindowMap) {
3923 if (qev != null && action == MotionEvent.ACTION_MOVE) {
3924 mKeyWaiter.bindTargetWindowLocked(target,
3925 KeyWaiter.RETURN_PENDING_POINTER, qev);
3926 ev = null;
3927 } else {
3928 if (action == MotionEvent.ACTION_DOWN) {
3929 WindowState out = mKeyWaiter.mOutsideTouchTargets;
3930 if (out != null) {
3931 MotionEvent oev = MotionEvent.obtain(ev);
3932 oev.setAction(MotionEvent.ACTION_OUTSIDE);
3933 do {
3934 final Rect frame = out.mFrame;
3935 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
3936 try {
3937 out.mClient.dispatchPointer(oev, eventTime);
3938 } catch (android.os.RemoteException e) {
3939 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
3940 }
3941 oev.offsetLocation((float)frame.left, (float)frame.top);
3942 out = out.mNextOutsideTouch;
3943 } while (out != null);
3944 mKeyWaiter.mOutsideTouchTargets = null;
3945 }
3946 }
3947 final Rect frame = target.mFrame;
3948 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
3949 mKeyWaiter.bindTargetWindowLocked(target);
3950 }
3951 }
Romain Guy06882f82009-06-10 13:36:04 -07003952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 // finally offset the event to the target's coordinate system and
3954 // dispatch the event.
3955 try {
3956 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
3957 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
3958 }
3959 target.mClient.dispatchPointer(ev, eventTime);
3960 return true;
3961 } catch (android.os.RemoteException e) {
3962 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
3963 mKeyWaiter.mMotionTarget = null;
3964 try {
3965 removeWindow(target.mSession, target.mClient);
3966 } catch (java.util.NoSuchElementException ex) {
3967 // This will happen if the window has already been
3968 // removed.
3969 }
3970 }
3971 return false;
3972 }
Romain Guy06882f82009-06-10 13:36:04 -07003973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 /**
3975 * @return Returns true if event was dispatched, false if it was dropped for any reason
3976 */
3977 private boolean dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
3978 if (DEBUG_INPUT) Log.v(
3979 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07003980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
3982 ev, false, false);
3983 if (focusObj == null) {
3984 Log.w(TAG, "No focus window, dropping trackball: " + ev);
3985 if (qev != null) {
3986 mQueue.recycleEvent(qev);
3987 }
3988 ev.recycle();
3989 return false;
3990 }
3991 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3992 if (qev != null) {
3993 mQueue.recycleEvent(qev);
3994 }
3995 ev.recycle();
3996 return true;
3997 }
Romain Guy06882f82009-06-10 13:36:04 -07003998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 if (uid != 0 && uid != focus.mSession.mUid) {
4002 if (mContext.checkPermission(
4003 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4004 != PackageManager.PERMISSION_GRANTED) {
4005 Log.w(TAG, "Permission denied: injecting key event from pid "
4006 + pid + " uid " + uid + " to window " + focus
4007 + " owned by uid " + focus.mSession.mUid);
4008 if (qev != null) {
4009 mQueue.recycleEvent(qev);
4010 }
4011 ev.recycle();
4012 return false;
4013 }
4014 }
Romain Guy06882f82009-06-10 13:36:04 -07004015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 synchronized(mWindowMap) {
4019 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4020 mKeyWaiter.bindTargetWindowLocked(focus,
4021 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4022 // We don't deliver movement events to the client, we hold
4023 // them and wait for them to call back.
4024 ev = null;
4025 } else {
4026 mKeyWaiter.bindTargetWindowLocked(focus);
4027 }
4028 }
Romain Guy06882f82009-06-10 13:36:04 -07004029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 try {
4031 focus.mClient.dispatchTrackball(ev, eventTime);
4032 return true;
4033 } catch (android.os.RemoteException e) {
4034 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4035 try {
4036 removeWindow(focus.mSession, focus.mClient);
4037 } catch (java.util.NoSuchElementException ex) {
4038 // This will happen if the window has already been
4039 // removed.
4040 }
4041 }
Romain Guy06882f82009-06-10 13:36:04 -07004042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 return false;
4044 }
Romain Guy06882f82009-06-10 13:36:04 -07004045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 /**
4047 * @return Returns true if event was dispatched, false if it was dropped for any reason
4048 */
4049 private boolean dispatchKey(KeyEvent event, int pid, int uid) {
4050 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4051
4052 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
4053 null, false, false);
4054 if (focusObj == null) {
4055 Log.w(TAG, "No focus window, dropping: " + event);
4056 return false;
4057 }
4058 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4059 return true;
4060 }
Romain Guy06882f82009-06-10 13:36:04 -07004061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 if (DEBUG_INPUT) Log.v(
4065 TAG, "Dispatching to " + focus + ": " + event);
4066
4067 if (uid != 0 && uid != focus.mSession.mUid) {
4068 if (mContext.checkPermission(
4069 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4070 != PackageManager.PERMISSION_GRANTED) {
4071 Log.w(TAG, "Permission denied: injecting key event from pid "
4072 + pid + " uid " + uid + " to window " + focus
4073 + " owned by uid " + focus.mSession.mUid);
4074 return false;
4075 }
4076 }
Romain Guy06882f82009-06-10 13:36:04 -07004077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 synchronized(mWindowMap) {
4079 mKeyWaiter.bindTargetWindowLocked(focus);
4080 }
4081
4082 // NOSHIP extra state logging
4083 mKeyWaiter.recordDispatchState(event, focus);
4084 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 try {
4087 if (DEBUG_INPUT || DEBUG_FOCUS) {
4088 Log.v(TAG, "Delivering key " + event.getKeyCode()
4089 + " to " + focus);
4090 }
4091 focus.mClient.dispatchKey(event);
4092 return true;
4093 } catch (android.os.RemoteException e) {
4094 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4095 try {
4096 removeWindow(focus.mSession, focus.mClient);
4097 } catch (java.util.NoSuchElementException ex) {
4098 // This will happen if the window has already been
4099 // removed.
4100 }
4101 }
Romain Guy06882f82009-06-10 13:36:04 -07004102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 return false;
4104 }
Romain Guy06882f82009-06-10 13:36:04 -07004105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 public void pauseKeyDispatching(IBinder _token) {
4107 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4108 "pauseKeyDispatching()")) {
4109 return;
4110 }
4111
4112 synchronized (mWindowMap) {
4113 WindowToken token = mTokenMap.get(_token);
4114 if (token != null) {
4115 mKeyWaiter.pauseDispatchingLocked(token);
4116 }
4117 }
4118 }
4119
4120 public void resumeKeyDispatching(IBinder _token) {
4121 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4122 "resumeKeyDispatching()")) {
4123 return;
4124 }
4125
4126 synchronized (mWindowMap) {
4127 WindowToken token = mTokenMap.get(_token);
4128 if (token != null) {
4129 mKeyWaiter.resumeDispatchingLocked(token);
4130 }
4131 }
4132 }
4133
4134 public void setEventDispatching(boolean enabled) {
4135 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4136 "resumeKeyDispatching()")) {
4137 return;
4138 }
4139
4140 synchronized (mWindowMap) {
4141 mKeyWaiter.setEventDispatchingLocked(enabled);
4142 }
4143 }
Romain Guy06882f82009-06-10 13:36:04 -07004144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 /**
4146 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004147 *
4148 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 * {@link SystemClock#uptimeMillis()} as the timebase.)
4150 * @param sync If true, wait for the event to be completed before returning to the caller.
4151 * @return Returns true if event was dispatched, false if it was dropped for any reason
4152 */
4153 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4154 long downTime = ev.getDownTime();
4155 long eventTime = ev.getEventTime();
4156
4157 int action = ev.getAction();
4158 int code = ev.getKeyCode();
4159 int repeatCount = ev.getRepeatCount();
4160 int metaState = ev.getMetaState();
4161 int deviceId = ev.getDeviceId();
4162 int scancode = ev.getScanCode();
4163
4164 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4165 if (downTime == 0) downTime = eventTime;
4166
4167 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004168 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169
4170 boolean result = dispatchKey(newEvent, Binder.getCallingPid(), Binder.getCallingUid());
4171 if (sync) {
4172 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true);
4173 }
4174 return result;
4175 }
4176
4177 /**
4178 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004179 *
4180 * @param ev A motion event describing the pointer (touch) action. (As noted in
4181 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 * {@link SystemClock#uptimeMillis()} as the timebase.)
4183 * @param sync If true, wait for the event to be completed before returning to the caller.
4184 * @return Returns true if event was dispatched, false if it was dropped for any reason
4185 */
4186 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
4187 boolean result = dispatchPointer(null, ev, Binder.getCallingPid(), Binder.getCallingUid());
4188 if (sync) {
4189 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true);
4190 }
Romain Guy06882f82009-06-10 13:36:04 -07004191 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 }
Romain Guy06882f82009-06-10 13:36:04 -07004193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 /**
4195 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004196 *
4197 * @param ev A motion event describing the trackball action. (As noted in
4198 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 * {@link SystemClock#uptimeMillis()} as the timebase.)
4200 * @param sync If true, wait for the event to be completed before returning to the caller.
4201 * @return Returns true if event was dispatched, false if it was dropped for any reason
4202 */
4203 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
4204 boolean result = dispatchTrackball(null, ev, Binder.getCallingPid(), Binder.getCallingUid());
4205 if (sync) {
4206 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true);
4207 }
4208 return result;
4209 }
Romain Guy06882f82009-06-10 13:36:04 -07004210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 private WindowState getFocusedWindow() {
4212 synchronized (mWindowMap) {
4213 return getFocusedWindowLocked();
4214 }
4215 }
4216
4217 private WindowState getFocusedWindowLocked() {
4218 return mCurrentFocus;
4219 }
Romain Guy06882f82009-06-10 13:36:04 -07004220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 /**
4222 * This class holds the state for dispatching key events. This state
4223 * is protected by the KeyWaiter instance, NOT by the window lock. You
4224 * can be holding the main window lock while acquire the KeyWaiter lock,
4225 * but not the other way around.
4226 */
4227 final class KeyWaiter {
4228 // NOSHIP debugging
4229 public class DispatchState {
4230 private KeyEvent event;
4231 private WindowState focus;
4232 private long time;
4233 private WindowState lastWin;
4234 private IBinder lastBinder;
4235 private boolean finished;
4236 private boolean gotFirstWindow;
4237 private boolean eventDispatching;
4238 private long timeToSwitch;
4239 private boolean wasFrozen;
4240 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004241 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4244 focus = theFocus;
4245 event = theEvent;
4246 time = System.currentTimeMillis();
4247 // snapshot KeyWaiter state
4248 lastWin = mLastWin;
4249 lastBinder = mLastBinder;
4250 finished = mFinished;
4251 gotFirstWindow = mGotFirstWindow;
4252 eventDispatching = mEventDispatching;
4253 timeToSwitch = mTimeToSwitch;
4254 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004255 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 // cache the paused state at ctor time as well
4257 if (theFocus == null || theFocus.mToken == null) {
4258 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4259 focusPaused = false;
4260 } else {
4261 focusPaused = theFocus.mToken.paused;
4262 }
4263 }
Romain Guy06882f82009-06-10 13:36:04 -07004264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 public String toString() {
4266 return "{{" + event + " to " + focus + " @ " + time
4267 + " lw=" + lastWin + " lb=" + lastBinder
4268 + " fin=" + finished + " gfw=" + gotFirstWindow
4269 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004270 + " wf=" + wasFrozen + " fp=" + focusPaused
4271 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 }
4273 };
4274 private DispatchState mDispatchState = null;
4275 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4276 mDispatchState = new DispatchState(theEvent, theFocus);
4277 }
4278 // END NOSHIP
4279
4280 public static final int RETURN_NOTHING = 0;
4281 public static final int RETURN_PENDING_POINTER = 1;
4282 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004284 final Object SKIP_TARGET_TOKEN = new Object();
4285 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 private WindowState mLastWin = null;
4288 private IBinder mLastBinder = null;
4289 private boolean mFinished = true;
4290 private boolean mGotFirstWindow = false;
4291 private boolean mEventDispatching = true;
4292 private long mTimeToSwitch = 0;
4293 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 // Target of Motion events
4296 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 // Windows above the target who would like to receive an "outside"
4299 // touch event for any down events outside of them.
4300 WindowState mOutsideTouchTargets;
4301
4302 /**
4303 * Wait for the last event dispatch to complete, then find the next
4304 * target that should receive the given event and wait for that one
4305 * to be ready to receive it.
4306 */
4307 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4308 MotionEvent nextMotion, boolean isPointerEvent,
4309 boolean failIfTimeout) {
4310 long startTime = SystemClock.uptimeMillis();
4311 long keyDispatchingTimeout = 5 * 1000;
4312 long waitedFor = 0;
4313
4314 while (true) {
4315 // Figure out which window we care about. It is either the
4316 // last window we are waiting to have process the event or,
4317 // if none, then the next window we think the event should go
4318 // to. Note: we retrieve mLastWin outside of the lock, so
4319 // it may change before we lock. Thus we must check it again.
4320 WindowState targetWin = mLastWin;
4321 boolean targetIsNew = targetWin == null;
4322 if (DEBUG_INPUT) Log.v(
4323 TAG, "waitForLastKey: mFinished=" + mFinished +
4324 ", mLastWin=" + mLastWin);
4325 if (targetIsNew) {
4326 Object target = findTargetWindow(nextKey, qev, nextMotion,
4327 isPointerEvent);
4328 if (target == SKIP_TARGET_TOKEN) {
4329 // The user has pressed a special key, and we are
4330 // dropping all pending events before it.
4331 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4332 + " " + nextMotion);
4333 return null;
4334 }
4335 if (target == CONSUMED_EVENT_TOKEN) {
4336 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4337 + " " + nextMotion);
4338 return target;
4339 }
4340 targetWin = (WindowState)target;
4341 }
Romain Guy06882f82009-06-10 13:36:04 -07004342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 // Now: is it okay to send the next event to this window?
4346 synchronized (this) {
4347 // First: did we come here based on the last window not
4348 // being null, but it changed by the time we got here?
4349 // If so, try again.
4350 if (!targetIsNew && mLastWin == null) {
4351 continue;
4352 }
Romain Guy06882f82009-06-10 13:36:04 -07004353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 // We never dispatch events if not finished with the
4355 // last one, or the display is frozen.
4356 if (mFinished && !mDisplayFrozen) {
4357 // If event dispatching is disabled, then we
4358 // just consume the events.
4359 if (!mEventDispatching) {
4360 if (DEBUG_INPUT) Log.v(TAG,
4361 "Skipping event; dispatching disabled: "
4362 + nextKey + " " + nextMotion);
4363 return null;
4364 }
4365 if (targetWin != null) {
4366 // If this is a new target, and that target is not
4367 // paused or unresponsive, then all looks good to
4368 // handle the event.
4369 if (targetIsNew && !targetWin.mToken.paused) {
4370 return targetWin;
4371 }
Romain Guy06882f82009-06-10 13:36:04 -07004372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 // If we didn't find a target window, and there is no
4374 // focused app window, then just eat the events.
4375 } else if (mFocusedApp == null) {
4376 if (DEBUG_INPUT) Log.v(TAG,
4377 "Skipping event; no focused app: "
4378 + nextKey + " " + nextMotion);
4379 return null;
4380 }
4381 }
Romain Guy06882f82009-06-10 13:36:04 -07004382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 if (DEBUG_INPUT) Log.v(
4384 TAG, "Waiting for last key in " + mLastBinder
4385 + " target=" + targetWin
4386 + " mFinished=" + mFinished
4387 + " mDisplayFrozen=" + mDisplayFrozen
4388 + " targetIsNew=" + targetIsNew
4389 + " paused="
4390 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004391 + " mFocusedApp=" + mFocusedApp
4392 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 targetApp = targetWin != null
4395 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 long curTimeout = keyDispatchingTimeout;
4398 if (mTimeToSwitch != 0) {
4399 long now = SystemClock.uptimeMillis();
4400 if (mTimeToSwitch <= now) {
4401 // If an app switch key has been pressed, and we have
4402 // waited too long for the current app to finish
4403 // processing keys, then wait no more!
4404 doFinishedKeyLocked(true);
4405 continue;
4406 }
4407 long switchTimeout = mTimeToSwitch - now;
4408 if (curTimeout > switchTimeout) {
4409 curTimeout = switchTimeout;
4410 }
4411 }
Romain Guy06882f82009-06-10 13:36:04 -07004412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 try {
4414 // after that continue
4415 // processing keys, so we don't get stuck.
4416 if (DEBUG_INPUT) Log.v(
4417 TAG, "Waiting for key dispatch: " + curTimeout);
4418 wait(curTimeout);
4419 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4420 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004421 + startTime + " switchTime=" + mTimeToSwitch
4422 + " target=" + targetWin + " mLW=" + mLastWin
4423 + " mLB=" + mLastBinder + " fin=" + mFinished
4424 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 } catch (InterruptedException e) {
4426 }
4427 }
4428
4429 // If we were frozen during configuration change, restart the
4430 // timeout checks from now; otherwise look at whether we timed
4431 // out before awakening.
4432 if (mWasFrozen) {
4433 waitedFor = 0;
4434 mWasFrozen = false;
4435 } else {
4436 waitedFor = SystemClock.uptimeMillis() - startTime;
4437 }
4438
4439 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4440 IApplicationToken at = null;
4441 synchronized (this) {
4442 Log.w(TAG, "Key dispatching timed out sending to " +
4443 (targetWin != null ? targetWin.mAttrs.getTitle()
4444 : "<null>"));
4445 // NOSHIP debugging
4446 Log.w(TAG, "Dispatch state: " + mDispatchState);
4447 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4448 // END NOSHIP
4449 //dump();
4450 if (targetWin != null) {
4451 at = targetWin.getAppToken();
4452 } else if (targetApp != null) {
4453 at = targetApp.appToken;
4454 }
4455 }
4456
4457 boolean abort = true;
4458 if (at != null) {
4459 try {
4460 long timeout = at.getKeyDispatchingTimeout();
4461 if (timeout > waitedFor) {
4462 // we did not wait the proper amount of time for this application.
4463 // set the timeout to be the real timeout and wait again.
4464 keyDispatchingTimeout = timeout - waitedFor;
4465 continue;
4466 } else {
4467 abort = at.keyDispatchingTimedOut();
4468 }
4469 } catch (RemoteException ex) {
4470 }
4471 }
4472
4473 synchronized (this) {
4474 if (abort && (mLastWin == targetWin || targetWin == null)) {
4475 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004476 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 if (DEBUG_INPUT) Log.v(TAG,
4478 "Window " + mLastWin +
4479 " timed out on key input");
4480 if (mLastWin.mToken.paused) {
4481 Log.w(TAG, "Un-pausing dispatching to this window");
4482 mLastWin.mToken.paused = false;
4483 }
4484 }
4485 if (mMotionTarget == targetWin) {
4486 mMotionTarget = null;
4487 }
4488 mLastWin = null;
4489 mLastBinder = null;
4490 if (failIfTimeout || targetWin == null) {
4491 return null;
4492 }
4493 } else {
4494 Log.w(TAG, "Continuing to wait for key to be dispatched");
4495 startTime = SystemClock.uptimeMillis();
4496 }
4497 }
4498 }
4499 }
4500 }
Romain Guy06882f82009-06-10 13:36:04 -07004501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
4503 MotionEvent nextMotion, boolean isPointerEvent) {
4504 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 if (nextKey != null) {
4507 // Find the target window for a normal key event.
4508 final int keycode = nextKey.getKeyCode();
4509 final int repeatCount = nextKey.getRepeatCount();
4510 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4511 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
4512 if (!dispatch) {
4513 mPolicy.interceptKeyTi(null, keycode,
4514 nextKey.getMetaState(), down, repeatCount);
4515 Log.w(TAG, "Event timeout during app switch: dropping "
4516 + nextKey);
4517 return SKIP_TARGET_TOKEN;
4518 }
Romain Guy06882f82009-06-10 13:36:04 -07004519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 WindowState focus = null;
4523 synchronized(mWindowMap) {
4524 focus = getFocusedWindowLocked();
4525 }
Romain Guy06882f82009-06-10 13:36:04 -07004526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 if (mPolicy.interceptKeyTi(focus,
4530 keycode, nextKey.getMetaState(), down, repeatCount)) {
4531 return CONSUMED_EVENT_TOKEN;
4532 }
Romain Guy06882f82009-06-10 13:36:04 -07004533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 } else if (!isPointerEvent) {
4537 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4538 if (!dispatch) {
4539 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4540 + nextMotion);
4541 return SKIP_TARGET_TOKEN;
4542 }
Romain Guy06882f82009-06-10 13:36:04 -07004543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 WindowState focus = null;
4545 synchronized(mWindowMap) {
4546 focus = getFocusedWindowLocked();
4547 }
Romain Guy06882f82009-06-10 13:36:04 -07004548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4550 return focus;
4551 }
Romain Guy06882f82009-06-10 13:36:04 -07004552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 if (nextMotion == null) {
4554 return SKIP_TARGET_TOKEN;
4555 }
Romain Guy06882f82009-06-10 13:36:04 -07004556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4558 KeyEvent.KEYCODE_UNKNOWN);
4559 if (!dispatch) {
4560 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4561 + nextMotion);
4562 return SKIP_TARGET_TOKEN;
4563 }
Romain Guy06882f82009-06-10 13:36:04 -07004564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 // Find the target window for a pointer event.
4566 int action = nextMotion.getAction();
4567 final float xf = nextMotion.getX();
4568 final float yf = nextMotion.getY();
4569 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 final boolean screenWasOff = qev != null
4572 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 synchronized(mWindowMap) {
4577 synchronized (this) {
4578 if (action == MotionEvent.ACTION_DOWN) {
4579 if (mMotionTarget != null) {
4580 // this is weird, we got a pen down, but we thought it was
4581 // already down!
4582 // XXX: We should probably send an ACTION_UP to the current
4583 // target.
4584 Log.w(TAG, "Pointer down received while already down in: "
4585 + mMotionTarget);
4586 mMotionTarget = null;
4587 }
Romain Guy06882f82009-06-10 13:36:04 -07004588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 // ACTION_DOWN is special, because we need to lock next events to
4590 // the window we'll land onto.
4591 final int x = (int)xf;
4592 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 final ArrayList windows = mWindows;
4595 final int N = windows.size();
4596 WindowState topErrWindow = null;
4597 final Rect tmpRect = mTempRect;
4598 for (int i=N-1; i>=0; i--) {
4599 WindowState child = (WindowState)windows.get(i);
4600 //Log.i(TAG, "Checking dispatch to: " + child);
4601 final int flags = child.mAttrs.flags;
4602 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4603 if (topErrWindow == null) {
4604 topErrWindow = child;
4605 }
4606 }
4607 if (!child.isVisibleLw()) {
4608 //Log.i(TAG, "Not visible!");
4609 continue;
4610 }
4611 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4612 //Log.i(TAG, "Not touchable!");
4613 if ((flags & WindowManager.LayoutParams
4614 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4615 child.mNextOutsideTouch = mOutsideTouchTargets;
4616 mOutsideTouchTargets = child;
4617 }
4618 continue;
4619 }
4620 tmpRect.set(child.mFrame);
4621 if (child.mTouchableInsets == ViewTreeObserver
4622 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4623 // The touch is inside of the window if it is
4624 // inside the frame, AND the content part of that
4625 // frame that was given by the application.
4626 tmpRect.left += child.mGivenContentInsets.left;
4627 tmpRect.top += child.mGivenContentInsets.top;
4628 tmpRect.right -= child.mGivenContentInsets.right;
4629 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4630 } else if (child.mTouchableInsets == ViewTreeObserver
4631 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4632 // The touch is inside of the window if it is
4633 // inside the frame, AND the visible part of that
4634 // frame that was given by the application.
4635 tmpRect.left += child.mGivenVisibleInsets.left;
4636 tmpRect.top += child.mGivenVisibleInsets.top;
4637 tmpRect.right -= child.mGivenVisibleInsets.right;
4638 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4639 }
4640 final int touchFlags = flags &
4641 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4642 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4643 if (tmpRect.contains(x, y) || touchFlags == 0) {
4644 //Log.i(TAG, "Using this target!");
4645 if (!screenWasOff || (flags &
4646 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4647 mMotionTarget = child;
4648 } else {
4649 //Log.i(TAG, "Waking, skip!");
4650 mMotionTarget = null;
4651 }
4652 break;
4653 }
Romain Guy06882f82009-06-10 13:36:04 -07004654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 if ((flags & WindowManager.LayoutParams
4656 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4657 child.mNextOutsideTouch = mOutsideTouchTargets;
4658 mOutsideTouchTargets = child;
4659 //Log.i(TAG, "Adding to outside target list: " + child);
4660 }
4661 }
4662
4663 // if there's an error window but it's not accepting
4664 // focus (typically because it is not yet visible) just
4665 // wait for it -- any other focused window may in fact
4666 // be in ANR state.
4667 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4668 mMotionTarget = null;
4669 }
4670 }
Romain Guy06882f82009-06-10 13:36:04 -07004671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 target = mMotionTarget;
4673 }
4674 }
Romain Guy06882f82009-06-10 13:36:04 -07004675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 // Pointer events are a little different -- if there isn't a
4679 // target found for any event, then just drop it.
4680 return target != null ? target : SKIP_TARGET_TOKEN;
4681 }
Romain Guy06882f82009-06-10 13:36:04 -07004682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 boolean checkShouldDispatchKey(int keycode) {
4684 synchronized (this) {
4685 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4686 mTimeToSwitch = 0;
4687 return true;
4688 }
4689 if (mTimeToSwitch != 0
4690 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4691 return false;
4692 }
4693 return true;
4694 }
4695 }
Romain Guy06882f82009-06-10 13:36:04 -07004696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 void bindTargetWindowLocked(WindowState win,
4698 int pendingWhat, QueuedEvent pendingMotion) {
4699 synchronized (this) {
4700 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4701 }
4702 }
Romain Guy06882f82009-06-10 13:36:04 -07004703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 void bindTargetWindowLocked(WindowState win) {
4705 synchronized (this) {
4706 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4707 }
4708 }
4709
4710 void bindTargetWindowLockedLocked(WindowState win,
4711 int pendingWhat, QueuedEvent pendingMotion) {
4712 mLastWin = win;
4713 mLastBinder = win.mClient.asBinder();
4714 mFinished = false;
4715 if (pendingMotion != null) {
4716 final Session s = win.mSession;
4717 if (pendingWhat == RETURN_PENDING_POINTER) {
4718 releasePendingPointerLocked(s);
4719 s.mPendingPointerMove = pendingMotion;
4720 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004721 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 "bindTargetToWindow " + s.mPendingPointerMove);
4723 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4724 releasePendingTrackballLocked(s);
4725 s.mPendingTrackballMove = pendingMotion;
4726 s.mPendingTrackballWindow = win;
4727 }
4728 }
4729 }
Romain Guy06882f82009-06-10 13:36:04 -07004730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 void releasePendingPointerLocked(Session s) {
4732 if (DEBUG_INPUT) Log.v(TAG,
4733 "releasePendingPointer " + s.mPendingPointerMove);
4734 if (s.mPendingPointerMove != null) {
4735 mQueue.recycleEvent(s.mPendingPointerMove);
4736 s.mPendingPointerMove = null;
4737 }
4738 }
Romain Guy06882f82009-06-10 13:36:04 -07004739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 void releasePendingTrackballLocked(Session s) {
4741 if (s.mPendingTrackballMove != null) {
4742 mQueue.recycleEvent(s.mPendingTrackballMove);
4743 s.mPendingTrackballMove = null;
4744 }
4745 }
Romain Guy06882f82009-06-10 13:36:04 -07004746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4748 int returnWhat) {
4749 if (DEBUG_INPUT) Log.v(
4750 TAG, "finishedKey: client=" + client + ", force=" + force);
4751
4752 if (client == null) {
4753 return null;
4754 }
4755
4756 synchronized (this) {
4757 if (DEBUG_INPUT) Log.v(
4758 TAG, "finishedKey: client=" + client.asBinder()
4759 + ", force=" + force + ", last=" + mLastBinder
4760 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4761
4762 QueuedEvent qev = null;
4763 WindowState win = null;
4764 if (returnWhat == RETURN_PENDING_POINTER) {
4765 qev = session.mPendingPointerMove;
4766 win = session.mPendingPointerWindow;
4767 session.mPendingPointerMove = null;
4768 session.mPendingPointerWindow = null;
4769 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4770 qev = session.mPendingTrackballMove;
4771 win = session.mPendingTrackballWindow;
4772 session.mPendingTrackballMove = null;
4773 session.mPendingTrackballWindow = null;
4774 }
Romain Guy06882f82009-06-10 13:36:04 -07004775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 if (mLastBinder == client.asBinder()) {
4777 if (DEBUG_INPUT) Log.v(
4778 TAG, "finishedKey: last paused="
4779 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4780 if (mLastWin != null && (!mLastWin.mToken.paused || force
4781 || !mEventDispatching)) {
4782 doFinishedKeyLocked(false);
4783 } else {
4784 // Make sure to wake up anyone currently waiting to
4785 // dispatch a key, so they can re-evaluate their
4786 // current situation.
4787 mFinished = true;
4788 notifyAll();
4789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 }
Romain Guy06882f82009-06-10 13:36:04 -07004791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 if (qev != null) {
4793 MotionEvent res = (MotionEvent)qev.event;
4794 if (DEBUG_INPUT) Log.v(TAG,
4795 "Returning pending motion: " + res);
4796 mQueue.recycleEvent(qev);
4797 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4798 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4799 }
4800 return res;
4801 }
4802 return null;
4803 }
4804 }
4805
4806 void tickle() {
4807 synchronized (this) {
4808 notifyAll();
4809 }
4810 }
Romain Guy06882f82009-06-10 13:36:04 -07004811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 void handleNewWindowLocked(WindowState newWindow) {
4813 if (!newWindow.canReceiveKeys()) {
4814 return;
4815 }
4816 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004817 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 TAG, "New key dispatch window: win="
4819 + newWindow.mClient.asBinder()
4820 + ", last=" + mLastBinder
4821 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4822 + "), finished=" + mFinished + ", paused="
4823 + newWindow.mToken.paused);
4824
4825 // Displaying a window implicitly causes dispatching to
4826 // be unpaused. (This is to protect against bugs if someone
4827 // pauses dispatching but forgets to resume.)
4828 newWindow.mToken.paused = false;
4829
4830 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831
4832 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4833 if (DEBUG_INPUT) Log.v(TAG,
4834 "New SYSTEM_ERROR window; resetting state");
4835 mLastWin = null;
4836 mLastBinder = null;
4837 mMotionTarget = null;
4838 mFinished = true;
4839 } else if (mLastWin != null) {
4840 // If the new window is above the window we are
4841 // waiting on, then stop waiting and let key dispatching
4842 // start on the new guy.
4843 if (DEBUG_INPUT) Log.v(
4844 TAG, "Last win layer=" + mLastWin.mLayer
4845 + ", new win layer=" + newWindow.mLayer);
4846 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004847 // The new window is above the old; finish pending input to the last
4848 // window and start directing it to the new one.
4849 mLastWin.mToken.paused = false;
4850 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004852 // Either the new window is lower, so there is no need to wake key waiters,
4853 // or we just finished key input to the previous window, which implicitly
4854 // notified the key waiters. In both cases, we don't need to issue the
4855 // notification here.
4856 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 }
4858
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004859 // Now that we've put a new window state in place, make the event waiter
4860 // take notice and retarget its attentions.
4861 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 }
4863 }
4864
4865 void pauseDispatchingLocked(WindowToken token) {
4866 synchronized (this)
4867 {
4868 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
4869 token.paused = true;
4870
4871 /*
4872 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
4873 mPaused = true;
4874 } else {
4875 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07004876 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07004878 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07004880 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 }
4882 }
4883 */
4884 }
4885 }
4886
4887 void resumeDispatchingLocked(WindowToken token) {
4888 synchronized (this) {
4889 if (token.paused) {
4890 if (DEBUG_INPUT) Log.v(
4891 TAG, "Resuming WindowToken " + token
4892 + ", last=" + mLastBinder
4893 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4894 + "), finished=" + mFinished + ", paused="
4895 + token.paused);
4896 token.paused = false;
4897 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
4898 doFinishedKeyLocked(true);
4899 } else {
4900 notifyAll();
4901 }
4902 }
4903 }
4904 }
4905
4906 void setEventDispatchingLocked(boolean enabled) {
4907 synchronized (this) {
4908 mEventDispatching = enabled;
4909 notifyAll();
4910 }
4911 }
Romain Guy06882f82009-06-10 13:36:04 -07004912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 void appSwitchComing() {
4914 synchronized (this) {
4915 // Don't wait for more than .5 seconds for app to finish
4916 // processing the pending events.
4917 long now = SystemClock.uptimeMillis() + 500;
4918 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
4919 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
4920 mTimeToSwitch = now;
4921 }
4922 notifyAll();
4923 }
4924 }
Romain Guy06882f82009-06-10 13:36:04 -07004925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 private final void doFinishedKeyLocked(boolean doRecycle) {
4927 if (mLastWin != null) {
4928 releasePendingPointerLocked(mLastWin.mSession);
4929 releasePendingTrackballLocked(mLastWin.mSession);
4930 }
Romain Guy06882f82009-06-10 13:36:04 -07004931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 if (mLastWin == null || !mLastWin.mToken.paused
4933 || !mLastWin.isVisibleLw()) {
4934 // If the current window has been paused, we aren't -really-
4935 // finished... so let the waiters still wait.
4936 mLastWin = null;
4937 mLastBinder = null;
4938 }
4939 mFinished = true;
4940 notifyAll();
4941 }
4942 }
4943
4944 private class KeyQ extends KeyInputQueue
4945 implements KeyInputQueue.FilterCallback {
4946 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07004947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 KeyQ() {
4949 super(mContext);
4950 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
4951 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
4952 "KEEP_SCREEN_ON_FLAG");
4953 mHoldingScreen.setReferenceCounted(false);
4954 }
4955
4956 @Override
4957 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
4958 if (mPolicy.preprocessInputEventTq(event)) {
4959 return true;
4960 }
Romain Guy06882f82009-06-10 13:36:04 -07004961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 switch (event.type) {
4963 case RawInputEvent.EV_KEY: {
4964 // XXX begin hack
4965 if (DEBUG) {
4966 if (event.keycode == KeyEvent.KEYCODE_G) {
4967 if (event.value != 0) {
4968 // G down
4969 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
4970 }
4971 return false;
4972 }
4973 if (event.keycode == KeyEvent.KEYCODE_D) {
4974 if (event.value != 0) {
4975 //dump();
4976 }
4977 return false;
4978 }
4979 }
4980 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07004981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 boolean screenIsOff = !mPowerManager.screenIsOn();
4983 boolean screenIsDim = !mPowerManager.screenIsBright();
4984 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07004985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
4987 mPowerManager.goToSleep(event.when);
4988 }
4989
4990 if (screenIsOff) {
4991 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
4992 }
4993 if (screenIsDim) {
4994 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
4995 }
4996 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
4997 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07004998 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 }
Romain Guy06882f82009-06-10 13:36:04 -07005000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5002 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5003 filterQueue(this);
5004 mKeyWaiter.appSwitchComing();
5005 }
5006 return true;
5007 } else {
5008 return false;
5009 }
5010 }
Romain Guy06882f82009-06-10 13:36:04 -07005011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 case RawInputEvent.EV_REL: {
5013 boolean screenIsOff = !mPowerManager.screenIsOn();
5014 boolean screenIsDim = !mPowerManager.screenIsBright();
5015 if (screenIsOff) {
5016 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5017 device.classes, event)) {
5018 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5019 return false;
5020 }
5021 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5022 }
5023 if (screenIsDim) {
5024 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5025 }
5026 return true;
5027 }
Romain Guy06882f82009-06-10 13:36:04 -07005028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 case RawInputEvent.EV_ABS: {
5030 boolean screenIsOff = !mPowerManager.screenIsOn();
5031 boolean screenIsDim = !mPowerManager.screenIsBright();
5032 if (screenIsOff) {
5033 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5034 device.classes, event)) {
5035 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5036 return false;
5037 }
5038 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5039 }
5040 if (screenIsDim) {
5041 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5042 }
5043 return true;
5044 }
Romain Guy06882f82009-06-10 13:36:04 -07005045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005046 default:
5047 return true;
5048 }
5049 }
5050
5051 public int filterEvent(QueuedEvent ev) {
5052 switch (ev.classType) {
5053 case RawInputEvent.CLASS_KEYBOARD:
5054 KeyEvent ke = (KeyEvent)ev.event;
5055 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5056 Log.w(TAG, "Dropping movement key during app switch: "
5057 + ke.getKeyCode() + ", action=" + ke.getAction());
5058 return FILTER_REMOVE;
5059 }
5060 return FILTER_ABORT;
5061 default:
5062 return FILTER_KEEP;
5063 }
5064 }
Romain Guy06882f82009-06-10 13:36:04 -07005065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 /**
5067 * Must be called with the main window manager lock held.
5068 */
5069 void setHoldScreenLocked(boolean holding) {
5070 boolean state = mHoldingScreen.isHeld();
5071 if (holding != state) {
5072 if (holding) {
5073 mHoldingScreen.acquire();
5074 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005075 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 mHoldingScreen.release();
5077 }
5078 }
5079 }
5080 };
5081
5082 public boolean detectSafeMode() {
5083 mSafeMode = mPolicy.detectSafeMode();
5084 return mSafeMode;
5085 }
Romain Guy06882f82009-06-10 13:36:04 -07005086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 public void systemReady() {
5088 mPolicy.systemReady();
5089 }
Romain Guy06882f82009-06-10 13:36:04 -07005090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005091 private final class InputDispatcherThread extends Thread {
5092 // Time to wait when there is nothing to do: 9999 seconds.
5093 static final int LONG_WAIT=9999*1000;
5094
5095 public InputDispatcherThread() {
5096 super("InputDispatcher");
5097 }
Romain Guy06882f82009-06-10 13:36:04 -07005098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 @Override
5100 public void run() {
5101 while (true) {
5102 try {
5103 process();
5104 } catch (Exception e) {
5105 Log.e(TAG, "Exception in input dispatcher", e);
5106 }
5107 }
5108 }
Romain Guy06882f82009-06-10 13:36:04 -07005109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 private void process() {
5111 android.os.Process.setThreadPriority(
5112 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 // The last key event we saw
5115 KeyEvent lastKey = null;
5116
5117 // Last keydown time for auto-repeating keys
5118 long lastKeyTime = SystemClock.uptimeMillis();
5119 long nextKeyTime = lastKeyTime+LONG_WAIT;
5120
Romain Guy06882f82009-06-10 13:36:04 -07005121 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122 int keyRepeatCount = 0;
5123
5124 // Need to report that configuration has changed?
5125 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 while (true) {
5128 long curTime = SystemClock.uptimeMillis();
5129
5130 if (DEBUG_INPUT) Log.v(
5131 TAG, "Waiting for next key: now=" + curTime
5132 + ", repeat @ " + nextKeyTime);
5133
5134 // Retrieve next event, waiting only as long as the next
5135 // repeat timeout. If the configuration has changed, then
5136 // don't wait at all -- we'll report the change as soon as
5137 // we have processed all events.
5138 QueuedEvent ev = mQueue.getEvent(
5139 (int)((!configChanged && curTime < nextKeyTime)
5140 ? (nextKeyTime-curTime) : 0));
5141
5142 if (DEBUG_INPUT && ev != null) Log.v(
5143 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5144
5145 try {
5146 if (ev != null) {
5147 curTime = ev.when;
5148 int eventType;
5149 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5150 eventType = eventType((MotionEvent)ev.event);
5151 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5152 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5153 eventType = LocalPowerManager.BUTTON_EVENT;
5154 } else {
5155 eventType = LocalPowerManager.OTHER_EVENT;
5156 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005157 try {
Michael Chane96440f2009-05-06 10:27:36 -07005158 long now = SystemClock.uptimeMillis();
5159
5160 if ((now - mLastBatteryStatsCallTime)
5161 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5162 mLastBatteryStatsCallTime = now;
5163 mBatteryStats.noteInputEvent();
5164 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005165 } catch (RemoteException e) {
5166 // Ignore
5167 }
Michael Chane96440f2009-05-06 10:27:36 -07005168 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 switch (ev.classType) {
5170 case RawInputEvent.CLASS_KEYBOARD:
5171 KeyEvent ke = (KeyEvent)ev.event;
5172 if (ke.isDown()) {
5173 lastKey = ke;
5174 keyRepeatCount = 0;
5175 lastKeyTime = curTime;
5176 nextKeyTime = lastKeyTime
5177 + KEY_REPEAT_FIRST_DELAY;
5178 if (DEBUG_INPUT) Log.v(
5179 TAG, "Received key down: first repeat @ "
5180 + nextKeyTime);
5181 } else {
5182 lastKey = null;
5183 // Arbitrary long timeout.
5184 lastKeyTime = curTime;
5185 nextKeyTime = curTime + LONG_WAIT;
5186 if (DEBUG_INPUT) Log.v(
5187 TAG, "Received key up: ignore repeat @ "
5188 + nextKeyTime);
5189 }
5190 dispatchKey((KeyEvent)ev.event, 0, 0);
5191 mQueue.recycleEvent(ev);
5192 break;
5193 case RawInputEvent.CLASS_TOUCHSCREEN:
5194 //Log.i(TAG, "Read next event " + ev);
5195 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5196 break;
5197 case RawInputEvent.CLASS_TRACKBALL:
5198 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5199 break;
5200 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5201 configChanged = true;
5202 break;
5203 default:
5204 mQueue.recycleEvent(ev);
5205 break;
5206 }
Romain Guy06882f82009-06-10 13:36:04 -07005207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 } else if (configChanged) {
5209 configChanged = false;
5210 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 } else if (lastKey != null) {
5213 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 // Timeout occurred while key was down. If it is at or
5216 // past the key repeat time, dispatch the repeat.
5217 if (DEBUG_INPUT) Log.v(
5218 TAG, "Key timeout: repeat=" + nextKeyTime
5219 + ", now=" + curTime);
5220 if (curTime < nextKeyTime) {
5221 continue;
5222 }
Romain Guy06882f82009-06-10 13:36:04 -07005223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 lastKeyTime = nextKeyTime;
5225 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5226 keyRepeatCount++;
5227 if (DEBUG_INPUT) Log.v(
5228 TAG, "Key repeat: count=" + keyRepeatCount
5229 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005230 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 } else {
5233 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 lastKeyTime = curTime;
5236 nextKeyTime = curTime + LONG_WAIT;
5237 }
Romain Guy06882f82009-06-10 13:36:04 -07005238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 } catch (Exception e) {
5240 Log.e(TAG,
5241 "Input thread received uncaught exception: " + e, e);
5242 }
5243 }
5244 }
5245 }
5246
5247 // -------------------------------------------------------------
5248 // Client Session State
5249 // -------------------------------------------------------------
5250
5251 private final class Session extends IWindowSession.Stub
5252 implements IBinder.DeathRecipient {
5253 final IInputMethodClient mClient;
5254 final IInputContext mInputContext;
5255 final int mUid;
5256 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005257 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 SurfaceSession mSurfaceSession;
5259 int mNumWindow = 0;
5260 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 /**
5263 * Current pointer move event being dispatched to client window... must
5264 * hold key lock to access.
5265 */
5266 QueuedEvent mPendingPointerMove;
5267 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 /**
5270 * Current trackball move event being dispatched to client window... must
5271 * hold key lock to access.
5272 */
5273 QueuedEvent mPendingTrackballMove;
5274 WindowState mPendingTrackballWindow;
5275
5276 public Session(IInputMethodClient client, IInputContext inputContext) {
5277 mClient = client;
5278 mInputContext = inputContext;
5279 mUid = Binder.getCallingUid();
5280 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005281 StringBuilder sb = new StringBuilder();
5282 sb.append("Session{");
5283 sb.append(Integer.toHexString(System.identityHashCode(this)));
5284 sb.append(" uid ");
5285 sb.append(mUid);
5286 sb.append("}");
5287 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 synchronized (mWindowMap) {
5290 if (mInputMethodManager == null && mHaveInputMethods) {
5291 IBinder b = ServiceManager.getService(
5292 Context.INPUT_METHOD_SERVICE);
5293 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5294 }
5295 }
5296 long ident = Binder.clearCallingIdentity();
5297 try {
5298 // Note: it is safe to call in to the input method manager
5299 // here because we are not holding our lock.
5300 if (mInputMethodManager != null) {
5301 mInputMethodManager.addClient(client, inputContext,
5302 mUid, mPid);
5303 } else {
5304 client.setUsingInputMethod(false);
5305 }
5306 client.asBinder().linkToDeath(this, 0);
5307 } catch (RemoteException e) {
5308 // The caller has died, so we can just forget about this.
5309 try {
5310 if (mInputMethodManager != null) {
5311 mInputMethodManager.removeClient(client);
5312 }
5313 } catch (RemoteException ee) {
5314 }
5315 } finally {
5316 Binder.restoreCallingIdentity(ident);
5317 }
5318 }
Romain Guy06882f82009-06-10 13:36:04 -07005319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 @Override
5321 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5322 throws RemoteException {
5323 try {
5324 return super.onTransact(code, data, reply, flags);
5325 } catch (RuntimeException e) {
5326 // Log all 'real' exceptions thrown to the caller
5327 if (!(e instanceof SecurityException)) {
5328 Log.e(TAG, "Window Session Crash", e);
5329 }
5330 throw e;
5331 }
5332 }
5333
5334 public void binderDied() {
5335 // Note: it is safe to call in to the input method manager
5336 // here because we are not holding our lock.
5337 try {
5338 if (mInputMethodManager != null) {
5339 mInputMethodManager.removeClient(mClient);
5340 }
5341 } catch (RemoteException e) {
5342 }
5343 synchronized(mWindowMap) {
5344 mClientDead = true;
5345 killSessionLocked();
5346 }
5347 }
5348
5349 public int add(IWindow window, WindowManager.LayoutParams attrs,
5350 int viewVisibility, Rect outContentInsets) {
5351 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5352 }
Romain Guy06882f82009-06-10 13:36:04 -07005353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 public void remove(IWindow window) {
5355 removeWindow(this, window);
5356 }
Romain Guy06882f82009-06-10 13:36:04 -07005357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5359 int requestedWidth, int requestedHeight, int viewFlags,
5360 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5361 Rect outVisibleInsets, Surface outSurface) {
5362 return relayoutWindow(this, window, attrs,
5363 requestedWidth, requestedHeight, viewFlags, insetsPending,
5364 outFrame, outContentInsets, outVisibleInsets, outSurface);
5365 }
Romain Guy06882f82009-06-10 13:36:04 -07005366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 public void setTransparentRegion(IWindow window, Region region) {
5368 setTransparentRegionWindow(this, window, region);
5369 }
Romain Guy06882f82009-06-10 13:36:04 -07005370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 public void setInsets(IWindow window, int touchableInsets,
5372 Rect contentInsets, Rect visibleInsets) {
5373 setInsetsWindow(this, window, touchableInsets, contentInsets,
5374 visibleInsets);
5375 }
Romain Guy06882f82009-06-10 13:36:04 -07005376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5378 getWindowDisplayFrame(this, window, outDisplayFrame);
5379 }
Romain Guy06882f82009-06-10 13:36:04 -07005380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 public void finishDrawing(IWindow window) {
5382 if (localLOGV) Log.v(
5383 TAG, "IWindow finishDrawing called for " + window);
5384 finishDrawingWindow(this, window);
5385 }
5386
5387 public void finishKey(IWindow window) {
5388 if (localLOGV) Log.v(
5389 TAG, "IWindow finishKey called for " + window);
5390 mKeyWaiter.finishedKey(this, window, false,
5391 KeyWaiter.RETURN_NOTHING);
5392 }
5393
5394 public MotionEvent getPendingPointerMove(IWindow window) {
5395 if (localLOGV) Log.v(
5396 TAG, "IWindow getPendingMotionEvent called for " + window);
5397 return mKeyWaiter.finishedKey(this, window, false,
5398 KeyWaiter.RETURN_PENDING_POINTER);
5399 }
Romain Guy06882f82009-06-10 13:36:04 -07005400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 public MotionEvent getPendingTrackballMove(IWindow window) {
5402 if (localLOGV) Log.v(
5403 TAG, "IWindow getPendingMotionEvent called for " + window);
5404 return mKeyWaiter.finishedKey(this, window, false,
5405 KeyWaiter.RETURN_PENDING_TRACKBALL);
5406 }
5407
5408 public void setInTouchMode(boolean mode) {
5409 synchronized(mWindowMap) {
5410 mInTouchMode = mode;
5411 }
5412 }
5413
5414 public boolean getInTouchMode() {
5415 synchronized(mWindowMap) {
5416 return mInTouchMode;
5417 }
5418 }
5419
5420 public boolean performHapticFeedback(IWindow window, int effectId,
5421 boolean always) {
5422 synchronized(mWindowMap) {
5423 long ident = Binder.clearCallingIdentity();
5424 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005425 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 windowForClientLocked(this, window), effectId, always);
5427 } finally {
5428 Binder.restoreCallingIdentity(ident);
5429 }
5430 }
5431 }
Romain Guy06882f82009-06-10 13:36:04 -07005432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 void windowAddedLocked() {
5434 if (mSurfaceSession == null) {
5435 if (localLOGV) Log.v(
5436 TAG, "First window added to " + this + ", creating SurfaceSession");
5437 mSurfaceSession = new SurfaceSession();
5438 mSessions.add(this);
5439 }
5440 mNumWindow++;
5441 }
5442
5443 void windowRemovedLocked() {
5444 mNumWindow--;
5445 killSessionLocked();
5446 }
Romain Guy06882f82009-06-10 13:36:04 -07005447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 void killSessionLocked() {
5449 if (mNumWindow <= 0 && mClientDead) {
5450 mSessions.remove(this);
5451 if (mSurfaceSession != null) {
5452 if (localLOGV) Log.v(
5453 TAG, "Last window removed from " + this
5454 + ", destroying " + mSurfaceSession);
5455 try {
5456 mSurfaceSession.kill();
5457 } catch (Exception e) {
5458 Log.w(TAG, "Exception thrown when killing surface session "
5459 + mSurfaceSession + " in session " + this
5460 + ": " + e.toString());
5461 }
5462 mSurfaceSession = null;
5463 }
5464 }
5465 }
Romain Guy06882f82009-06-10 13:36:04 -07005466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005468 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5469 pw.print(" mClientDead="); pw.print(mClientDead);
5470 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5471 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5472 pw.print(prefix);
5473 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5474 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5475 }
5476 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5477 pw.print(prefix);
5478 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5479 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 }
5482
5483 @Override
5484 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005485 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 }
5487 }
5488
5489 // -------------------------------------------------------------
5490 // Client Window State
5491 // -------------------------------------------------------------
5492
5493 private final class WindowState implements WindowManagerPolicy.WindowState {
5494 final Session mSession;
5495 final IWindow mClient;
5496 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005497 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 AppWindowToken mAppToken;
5499 AppWindowToken mTargetAppToken;
5500 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5501 final DeathRecipient mDeathRecipient;
5502 final WindowState mAttachedWindow;
5503 final ArrayList mChildWindows = new ArrayList();
5504 final int mBaseLayer;
5505 final int mSubLayer;
5506 final boolean mLayoutAttached;
5507 final boolean mIsImWindow;
5508 int mViewVisibility;
5509 boolean mPolicyVisibility = true;
5510 boolean mPolicyVisibilityAfterAnim = true;
5511 boolean mAppFreezing;
5512 Surface mSurface;
5513 boolean mAttachedHidden; // is our parent window hidden?
5514 boolean mLastHidden; // was this window last hidden?
5515 int mRequestedWidth;
5516 int mRequestedHeight;
5517 int mLastRequestedWidth;
5518 int mLastRequestedHeight;
5519 int mReqXPos;
5520 int mReqYPos;
5521 int mLayer;
5522 int mAnimLayer;
5523 int mLastLayer;
5524 boolean mHaveFrame;
5525
5526 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 // Actual frame shown on-screen (may be modified by animation)
5529 final Rect mShownFrame = new Rect();
5530 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 /**
5533 * Insets that determine the actually visible area
5534 */
5535 final Rect mVisibleInsets = new Rect();
5536 final Rect mLastVisibleInsets = new Rect();
5537 boolean mVisibleInsetsChanged;
5538
5539 /**
5540 * Insets that are covered by system windows
5541 */
5542 final Rect mContentInsets = new Rect();
5543 final Rect mLastContentInsets = new Rect();
5544 boolean mContentInsetsChanged;
5545
5546 /**
5547 * Set to true if we are waiting for this window to receive its
5548 * given internal insets before laying out other windows based on it.
5549 */
5550 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 /**
5553 * These are the content insets that were given during layout for
5554 * this window, to be applied to windows behind it.
5555 */
5556 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 /**
5559 * These are the visible insets that were given during layout for
5560 * this window, to be applied to windows behind it.
5561 */
5562 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 /**
5565 * Flag indicating whether the touchable region should be adjusted by
5566 * the visible insets; if false the area outside the visible insets is
5567 * NOT touchable, so we must use those to adjust the frame during hit
5568 * tests.
5569 */
5570 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 // Current transformation being applied.
5573 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5574 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5575 float mHScale=1, mVScale=1;
5576 float mLastHScale=1, mLastVScale=1;
5577 final Matrix mTmpMatrix = new Matrix();
5578
5579 // "Real" frame that the application sees.
5580 final Rect mFrame = new Rect();
5581 final Rect mLastFrame = new Rect();
5582
5583 final Rect mContainingFrame = new Rect();
5584 final Rect mDisplayFrame = new Rect();
5585 final Rect mContentFrame = new Rect();
5586 final Rect mVisibleFrame = new Rect();
5587
5588 float mShownAlpha = 1;
5589 float mAlpha = 1;
5590 float mLastAlpha = 1;
5591
5592 // Set to true if, when the window gets displayed, it should perform
5593 // an enter animation.
5594 boolean mEnterAnimationPending;
5595
5596 // Currently running animation.
5597 boolean mAnimating;
5598 boolean mLocalAnimating;
5599 Animation mAnimation;
5600 boolean mAnimationIsEntrance;
5601 boolean mHasTransformation;
5602 boolean mHasLocalTransformation;
5603 final Transformation mTransformation = new Transformation();
5604
5605 // This is set after IWindowSession.relayout() has been called at
5606 // least once for the window. It allows us to detect the situation
5607 // where we don't yet have a surface, but should have one soon, so
5608 // we can give the window focus before waiting for the relayout.
5609 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 // This is set after the Surface has been created but before the
5612 // window has been drawn. During this time the surface is hidden.
5613 boolean mDrawPending;
5614
5615 // This is set after the window has finished drawing for the first
5616 // time but before its surface is shown. The surface will be
5617 // displayed when the next layout is run.
5618 boolean mCommitDrawPending;
5619
5620 // This is set during the time after the window's drawing has been
5621 // committed, and before its surface is actually shown. It is used
5622 // to delay showing the surface until all windows in a token are ready
5623 // to be shown.
5624 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 // Set when the window has been shown in the screen the first time.
5627 boolean mHasDrawn;
5628
5629 // Currently running an exit animation?
5630 boolean mExiting;
5631
5632 // Currently on the mDestroySurface list?
5633 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 // Completely remove from window manager after exit animation?
5636 boolean mRemoveOnExit;
5637
5638 // Set when the orientation is changing and this window has not yet
5639 // been updated for the new orientation.
5640 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 // Is this window now (or just being) removed?
5643 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 WindowState(Session s, IWindow c, WindowToken token,
5646 WindowState attachedWindow, WindowManager.LayoutParams a,
5647 int viewVisibility) {
5648 mSession = s;
5649 mClient = c;
5650 mToken = token;
5651 mAttrs.copyFrom(a);
5652 mViewVisibility = viewVisibility;
5653 DeathRecipient deathRecipient = new DeathRecipient();
5654 mAlpha = a.alpha;
5655 if (localLOGV) Log.v(
5656 TAG, "Window " + this + " client=" + c.asBinder()
5657 + " token=" + token + " (" + mAttrs.token + ")");
5658 try {
5659 c.asBinder().linkToDeath(deathRecipient, 0);
5660 } catch (RemoteException e) {
5661 mDeathRecipient = null;
5662 mAttachedWindow = null;
5663 mLayoutAttached = false;
5664 mIsImWindow = false;
5665 mBaseLayer = 0;
5666 mSubLayer = 0;
5667 return;
5668 }
5669 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5672 mAttrs.type <= LAST_SUB_WINDOW)) {
5673 // The multiplier here is to reserve space for multiple
5674 // windows in the same type layer.
5675 mBaseLayer = mPolicy.windowTypeToLayerLw(
5676 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5677 + TYPE_LAYER_OFFSET;
5678 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5679 mAttachedWindow = attachedWindow;
5680 mAttachedWindow.mChildWindows.add(this);
5681 mLayoutAttached = mAttrs.type !=
5682 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5683 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5684 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5685 } else {
5686 // The multiplier here is to reserve space for multiple
5687 // windows in the same type layer.
5688 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5689 * TYPE_LAYER_MULTIPLIER
5690 + TYPE_LAYER_OFFSET;
5691 mSubLayer = 0;
5692 mAttachedWindow = null;
5693 mLayoutAttached = false;
5694 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5695 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5696 }
5697
5698 WindowState appWin = this;
5699 while (appWin.mAttachedWindow != null) {
5700 appWin = mAttachedWindow;
5701 }
5702 WindowToken appToken = appWin.mToken;
5703 while (appToken.appWindowToken == null) {
5704 WindowToken parent = mTokenMap.get(appToken.token);
5705 if (parent == null || appToken == parent) {
5706 break;
5707 }
5708 appToken = parent;
5709 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005710 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005711 mAppToken = appToken.appWindowToken;
5712
5713 mSurface = null;
5714 mRequestedWidth = 0;
5715 mRequestedHeight = 0;
5716 mLastRequestedWidth = 0;
5717 mLastRequestedHeight = 0;
5718 mReqXPos = 0;
5719 mReqYPos = 0;
5720 mLayer = 0;
5721 mAnimLayer = 0;
5722 mLastLayer = 0;
5723 }
5724
5725 void attach() {
5726 if (localLOGV) Log.v(
5727 TAG, "Attaching " + this + " token=" + mToken
5728 + ", list=" + mToken.windows);
5729 mSession.windowAddedLocked();
5730 }
5731
5732 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5733 mHaveFrame = true;
5734
5735 final int pw = pf.right-pf.left;
5736 final int ph = pf.bottom-pf.top;
5737
5738 int w,h;
5739 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5740 w = mAttrs.width < 0 ? pw : mAttrs.width;
5741 h = mAttrs.height< 0 ? ph : mAttrs.height;
5742 } else {
5743 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5744 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5745 }
Romain Guy06882f82009-06-10 13:36:04 -07005746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 final Rect container = mContainingFrame;
5748 container.set(pf);
5749
5750 final Rect display = mDisplayFrame;
5751 display.set(df);
5752
5753 final Rect content = mContentFrame;
5754 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 final Rect visible = mVisibleFrame;
5757 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5762 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5763
5764 Gravity.apply(mAttrs.gravity, w, h, container,
5765 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5766 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5767
5768 //System.out.println("Out: " + mFrame);
5769
5770 // Now make sure the window fits in the overall display.
5771 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Romain Guy06882f82009-06-10 13:36:04 -07005772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 // Make sure the content and visible frames are inside of the
5774 // final window frame.
5775 if (content.left < frame.left) content.left = frame.left;
5776 if (content.top < frame.top) content.top = frame.top;
5777 if (content.right > frame.right) content.right = frame.right;
5778 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5779 if (visible.left < frame.left) visible.left = frame.left;
5780 if (visible.top < frame.top) visible.top = frame.top;
5781 if (visible.right > frame.right) visible.right = frame.right;
5782 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 final Rect contentInsets = mContentInsets;
5785 contentInsets.left = content.left-frame.left;
5786 contentInsets.top = content.top-frame.top;
5787 contentInsets.right = frame.right-content.right;
5788 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 final Rect visibleInsets = mVisibleInsets;
5791 visibleInsets.left = visible.left-frame.left;
5792 visibleInsets.top = visible.top-frame.top;
5793 visibleInsets.right = frame.right-visible.right;
5794 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 if (localLOGV) {
5797 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5798 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5799 Log.v(TAG, "Resolving (mRequestedWidth="
5800 + mRequestedWidth + ", mRequestedheight="
5801 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5802 + "): frame=" + mFrame.toShortString()
5803 + " ci=" + contentInsets.toShortString()
5804 + " vi=" + visibleInsets.toShortString());
5805 //}
5806 }
5807 }
Romain Guy06882f82009-06-10 13:36:04 -07005808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 public Rect getFrameLw() {
5810 return mFrame;
5811 }
5812
5813 public Rect getShownFrameLw() {
5814 return mShownFrame;
5815 }
5816
5817 public Rect getDisplayFrameLw() {
5818 return mDisplayFrame;
5819 }
5820
5821 public Rect getContentFrameLw() {
5822 return mContentFrame;
5823 }
5824
5825 public Rect getVisibleFrameLw() {
5826 return mVisibleFrame;
5827 }
5828
5829 public boolean getGivenInsetsPendingLw() {
5830 return mGivenInsetsPending;
5831 }
5832
5833 public Rect getGivenContentInsetsLw() {
5834 return mGivenContentInsets;
5835 }
Romain Guy06882f82009-06-10 13:36:04 -07005836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 public Rect getGivenVisibleInsetsLw() {
5838 return mGivenVisibleInsets;
5839 }
Romain Guy06882f82009-06-10 13:36:04 -07005840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 public WindowManager.LayoutParams getAttrs() {
5842 return mAttrs;
5843 }
5844
5845 public int getSurfaceLayer() {
5846 return mLayer;
5847 }
Romain Guy06882f82009-06-10 13:36:04 -07005848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 public IApplicationToken getAppToken() {
5850 return mAppToken != null ? mAppToken.appToken : null;
5851 }
5852
5853 public boolean hasAppShownWindows() {
5854 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
5855 }
5856
5857 public boolean hasAppStartingIcon() {
5858 return mAppToken != null ? (mAppToken.startingData != null) : false;
5859 }
5860
5861 public WindowManagerPolicy.WindowState getAppStartingWindow() {
5862 return mAppToken != null ? mAppToken.startingWindow : null;
5863 }
5864
5865 public void setAnimation(Animation anim) {
5866 if (localLOGV) Log.v(
5867 TAG, "Setting animation in " + this + ": " + anim);
5868 mAnimating = false;
5869 mLocalAnimating = false;
5870 mAnimation = anim;
5871 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
5872 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
5873 }
5874
5875 public void clearAnimation() {
5876 if (mAnimation != null) {
5877 mAnimating = true;
5878 mLocalAnimating = false;
5879 mAnimation = null;
5880 }
5881 }
Romain Guy06882f82009-06-10 13:36:04 -07005882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 Surface createSurfaceLocked() {
5884 if (mSurface == null) {
5885 mDrawPending = true;
5886 mCommitDrawPending = false;
5887 mReadyToShow = false;
5888 if (mAppToken != null) {
5889 mAppToken.allDrawn = false;
5890 }
5891
5892 int flags = 0;
5893 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
5894 flags |= Surface.HARDWARE;
5895 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
5896 flags |= Surface.GPU;
5897 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
5898 flags |= Surface.PUSH_BUFFERS;
5899 }
5900
5901 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
5902 flags |= Surface.SECURE;
5903 }
5904 if (DEBUG_VISIBILITY) Log.v(
5905 TAG, "Creating surface in session "
5906 + mSession.mSurfaceSession + " window " + this
5907 + " w=" + mFrame.width()
5908 + " h=" + mFrame.height() + " format="
5909 + mAttrs.format + " flags=" + flags);
5910
5911 int w = mFrame.width();
5912 int h = mFrame.height();
5913 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
5914 // for a scaled surface, we always want the requested
5915 // size.
5916 w = mRequestedWidth;
5917 h = mRequestedHeight;
5918 }
5919
5920 try {
5921 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07005922 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 0, w, h, mAttrs.format, flags);
5924 } catch (Surface.OutOfResourcesException e) {
5925 Log.w(TAG, "OutOfResourcesException creating surface");
5926 reclaimSomeSurfaceMemoryLocked(this, "create");
5927 return null;
5928 } catch (Exception e) {
5929 Log.e(TAG, "Exception creating surface", e);
5930 return null;
5931 }
Romain Guy06882f82009-06-10 13:36:04 -07005932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 if (localLOGV) Log.v(
5934 TAG, "Got surface: " + mSurface
5935 + ", set left=" + mFrame.left + " top=" + mFrame.top
5936 + ", animLayer=" + mAnimLayer);
5937 if (SHOW_TRANSACTIONS) {
5938 Log.i(TAG, ">>> OPEN TRANSACTION");
5939 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
5940 + mAttrs.getTitle() + ") pos=(" +
5941 mFrame.left + "," + mFrame.top + ") (" +
5942 mFrame.width() + "x" + mFrame.height() + "), layer=" +
5943 mAnimLayer + " HIDE");
5944 }
5945 Surface.openTransaction();
5946 try {
5947 try {
5948 mSurface.setPosition(mFrame.left, mFrame.top);
5949 mSurface.setLayer(mAnimLayer);
5950 mSurface.hide();
5951 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
5952 mSurface.setFlags(Surface.SURFACE_DITHER,
5953 Surface.SURFACE_DITHER);
5954 }
5955 } catch (RuntimeException e) {
5956 Log.w(TAG, "Error creating surface in " + w, e);
5957 reclaimSomeSurfaceMemoryLocked(this, "create-init");
5958 }
5959 mLastHidden = true;
5960 } finally {
5961 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
5962 Surface.closeTransaction();
5963 }
5964 if (localLOGV) Log.v(
5965 TAG, "Created surface " + this);
5966 }
5967 return mSurface;
5968 }
Romain Guy06882f82009-06-10 13:36:04 -07005969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 void destroySurfaceLocked() {
5971 // Window is no longer on-screen, so can no longer receive
5972 // key events... if we were waiting for it to finish
5973 // handling a key event, the wait is over!
5974 mKeyWaiter.finishedKey(mSession, mClient, true,
5975 KeyWaiter.RETURN_NOTHING);
5976 mKeyWaiter.releasePendingPointerLocked(mSession);
5977 mKeyWaiter.releasePendingTrackballLocked(mSession);
5978
5979 if (mAppToken != null && this == mAppToken.startingWindow) {
5980 mAppToken.startingDisplayed = false;
5981 }
Romain Guy06882f82009-06-10 13:36:04 -07005982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 if (localLOGV) Log.v(
5984 TAG, "Window " + this
5985 + " destroying surface " + mSurface + ", session " + mSession);
5986 if (mSurface != null) {
5987 try {
5988 if (SHOW_TRANSACTIONS) {
5989 RuntimeException ex = new RuntimeException();
5990 ex.fillInStackTrace();
5991 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
5992 + mAttrs.getTitle() + ")", ex);
5993 }
5994 mSurface.clear();
5995 } catch (RuntimeException e) {
5996 Log.w(TAG, "Exception thrown when destroying Window " + this
5997 + " surface " + mSurface + " session " + mSession
5998 + ": " + e.toString());
5999 }
6000 mSurface = null;
6001 mDrawPending = false;
6002 mCommitDrawPending = false;
6003 mReadyToShow = false;
6004
6005 int i = mChildWindows.size();
6006 while (i > 0) {
6007 i--;
6008 WindowState c = (WindowState)mChildWindows.get(i);
6009 c.mAttachedHidden = true;
6010 }
6011 }
6012 }
6013
6014 boolean finishDrawingLocked() {
6015 if (mDrawPending) {
6016 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6017 TAG, "finishDrawingLocked: " + mSurface);
6018 mCommitDrawPending = true;
6019 mDrawPending = false;
6020 return true;
6021 }
6022 return false;
6023 }
6024
6025 // This must be called while inside a transaction.
6026 void commitFinishDrawingLocked(long currentTime) {
6027 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6028 if (!mCommitDrawPending) {
6029 return;
6030 }
6031 mCommitDrawPending = false;
6032 mReadyToShow = true;
6033 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6034 final AppWindowToken atoken = mAppToken;
6035 if (atoken == null || atoken.allDrawn || starting) {
6036 performShowLocked();
6037 }
6038 }
6039
6040 // This must be called while inside a transaction.
6041 boolean performShowLocked() {
6042 if (DEBUG_VISIBILITY) {
6043 RuntimeException e = new RuntimeException();
6044 e.fillInStackTrace();
6045 Log.v(TAG, "performShow on " + this
6046 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6047 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6048 }
6049 if (mReadyToShow && isReadyForDisplay()) {
6050 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6051 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6052 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6053 + " during animation: policyVis=" + mPolicyVisibility
6054 + " attHidden=" + mAttachedHidden
6055 + " tok.hiddenRequested="
6056 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6057 + " tok.idden="
6058 + (mAppToken != null ? mAppToken.hidden : false)
6059 + " animating=" + mAnimating
6060 + " tok animating="
6061 + (mAppToken != null ? mAppToken.animating : false));
6062 if (!showSurfaceRobustlyLocked(this)) {
6063 return false;
6064 }
6065 mLastAlpha = -1;
6066 mHasDrawn = true;
6067 mLastHidden = false;
6068 mReadyToShow = false;
6069 enableScreenIfNeededLocked();
6070
6071 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 int i = mChildWindows.size();
6074 while (i > 0) {
6075 i--;
6076 WindowState c = (WindowState)mChildWindows.get(i);
6077 if (c.mSurface != null && c.mAttachedHidden) {
6078 c.mAttachedHidden = false;
6079 c.performShowLocked();
6080 }
6081 }
Romain Guy06882f82009-06-10 13:36:04 -07006082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 if (mAttrs.type != TYPE_APPLICATION_STARTING
6084 && mAppToken != null) {
6085 mAppToken.firstWindowDrawn = true;
6086 if (mAnimation == null && mAppToken.startingData != null) {
6087 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6088 + mToken
6089 + ": first real window is shown, no animation");
6090 mFinishedStarting.add(mAppToken);
6091 mH.sendEmptyMessage(H.FINISHED_STARTING);
6092 }
6093 mAppToken.updateReportedVisibilityLocked();
6094 }
6095 }
6096 return true;
6097 }
Romain Guy06882f82009-06-10 13:36:04 -07006098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 // This must be called while inside a transaction. Returns true if
6100 // there is more animation to run.
6101 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6102 if (!mDisplayFrozen) {
6103 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6106 mHasTransformation = true;
6107 mHasLocalTransformation = true;
6108 if (!mLocalAnimating) {
6109 if (DEBUG_ANIM) Log.v(
6110 TAG, "Starting animation in " + this +
6111 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6112 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6113 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6114 mAnimation.setStartTime(currentTime);
6115 mLocalAnimating = true;
6116 mAnimating = true;
6117 }
6118 mTransformation.clear();
6119 final boolean more = mAnimation.getTransformation(
6120 currentTime, mTransformation);
6121 if (DEBUG_ANIM) Log.v(
6122 TAG, "Stepped animation in " + this +
6123 ": more=" + more + ", xform=" + mTransformation);
6124 if (more) {
6125 // we're not done!
6126 return true;
6127 }
6128 if (DEBUG_ANIM) Log.v(
6129 TAG, "Finished animation in " + this +
6130 " @ " + currentTime);
6131 mAnimation = null;
6132 //WindowManagerService.this.dump();
6133 }
6134 mHasLocalTransformation = false;
6135 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6136 && mAppToken.hasTransformation) {
6137 // When our app token is animating, we kind-of pretend like
6138 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6139 // part of this check means that we will only do this if
6140 // our window is not currently exiting, or it is not
6141 // locally animating itself. The idea being that one that
6142 // is exiting and doing a local animation should be removed
6143 // once that animation is done.
6144 mAnimating = true;
6145 mHasTransformation = true;
6146 mTransformation.clear();
6147 return false;
6148 } else if (mHasTransformation) {
6149 // Little trick to get through the path below to act like
6150 // we have finished an animation.
6151 mAnimating = true;
6152 } else if (isAnimating()) {
6153 mAnimating = true;
6154 }
6155 } else if (mAnimation != null) {
6156 // If the display is frozen, and there is a pending animation,
6157 // clear it and make sure we run the cleanup code.
6158 mAnimating = true;
6159 mLocalAnimating = true;
6160 mAnimation = null;
6161 }
Romain Guy06882f82009-06-10 13:36:04 -07006162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 if (!mAnimating && !mLocalAnimating) {
6164 return false;
6165 }
6166
6167 if (DEBUG_ANIM) Log.v(
6168 TAG, "Animation done in " + this + ": exiting=" + mExiting
6169 + ", reportedVisible="
6170 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 mAnimating = false;
6173 mLocalAnimating = false;
6174 mAnimation = null;
6175 mAnimLayer = mLayer;
6176 if (mIsImWindow) {
6177 mAnimLayer += mInputMethodAnimLayerAdjustment;
6178 }
6179 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6180 + " anim layer: " + mAnimLayer);
6181 mHasTransformation = false;
6182 mHasLocalTransformation = false;
6183 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6184 mTransformation.clear();
6185 if (mHasDrawn
6186 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6187 && mAppToken != null
6188 && mAppToken.firstWindowDrawn
6189 && mAppToken.startingData != null) {
6190 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6191 + mToken + ": first real window done animating");
6192 mFinishedStarting.add(mAppToken);
6193 mH.sendEmptyMessage(H.FINISHED_STARTING);
6194 }
Romain Guy06882f82009-06-10 13:36:04 -07006195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 finishExit();
6197
6198 if (mAppToken != null) {
6199 mAppToken.updateReportedVisibilityLocked();
6200 }
6201
6202 return false;
6203 }
6204
6205 void finishExit() {
6206 if (DEBUG_ANIM) Log.v(
6207 TAG, "finishExit in " + this
6208 + ": exiting=" + mExiting
6209 + " remove=" + mRemoveOnExit
6210 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212 final int N = mChildWindows.size();
6213 for (int i=0; i<N; i++) {
6214 ((WindowState)mChildWindows.get(i)).finishExit();
6215 }
Romain Guy06882f82009-06-10 13:36:04 -07006216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 if (!mExiting) {
6218 return;
6219 }
Romain Guy06882f82009-06-10 13:36:04 -07006220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 if (isWindowAnimating()) {
6222 return;
6223 }
6224
6225 if (localLOGV) Log.v(
6226 TAG, "Exit animation finished in " + this
6227 + ": remove=" + mRemoveOnExit);
6228 if (mSurface != null) {
6229 mDestroySurface.add(this);
6230 mDestroying = true;
6231 if (SHOW_TRANSACTIONS) Log.i(
6232 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6233 try {
6234 mSurface.hide();
6235 } catch (RuntimeException e) {
6236 Log.w(TAG, "Error hiding surface in " + this, e);
6237 }
6238 mLastHidden = true;
6239 mKeyWaiter.releasePendingPointerLocked(mSession);
6240 }
6241 mExiting = false;
6242 if (mRemoveOnExit) {
6243 mPendingRemove.add(this);
6244 mRemoveOnExit = false;
6245 }
6246 }
Romain Guy06882f82009-06-10 13:36:04 -07006247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6249 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6250 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6251 if (dtdx < -.000001f || dtdx > .000001f) return false;
6252 if (dsdy < -.000001f || dsdy > .000001f) return false;
6253 return true;
6254 }
Romain Guy06882f82009-06-10 13:36:04 -07006255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 void computeShownFrameLocked() {
6257 final boolean selfTransformation = mHasLocalTransformation;
6258 Transformation attachedTransformation =
6259 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6260 ? mAttachedWindow.mTransformation : null;
6261 Transformation appTransformation =
6262 (mAppToken != null && mAppToken.hasTransformation)
6263 ? mAppToken.transformation : null;
6264 if (selfTransformation || attachedTransformation != null
6265 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006266 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 final Rect frame = mFrame;
6268 final float tmpFloats[] = mTmpFloats;
6269 final Matrix tmpMatrix = mTmpMatrix;
6270
6271 // Compute the desired transformation.
6272 tmpMatrix.setTranslate(frame.left, frame.top);
6273 if (selfTransformation) {
6274 tmpMatrix.preConcat(mTransformation.getMatrix());
6275 }
6276 if (attachedTransformation != null) {
6277 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6278 }
6279 if (appTransformation != null) {
6280 tmpMatrix.preConcat(appTransformation.getMatrix());
6281 }
6282
6283 // "convert" it into SurfaceFlinger's format
6284 // (a 2x2 matrix + an offset)
6285 // Here we must not transform the position of the surface
6286 // since it is already included in the transformation.
6287 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 tmpMatrix.getValues(tmpFloats);
6290 mDsDx = tmpFloats[Matrix.MSCALE_X];
6291 mDtDx = tmpFloats[Matrix.MSKEW_X];
6292 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6293 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6294 int x = (int)tmpFloats[Matrix.MTRANS_X];
6295 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6296 int w = frame.width();
6297 int h = frame.height();
6298 mShownFrame.set(x, y, x+w, y+h);
6299
6300 // Now set the alpha... but because our current hardware
6301 // can't do alpha transformation on a non-opaque surface,
6302 // turn it off if we are running an animation that is also
6303 // transforming since it is more important to have that
6304 // animation be smooth.
6305 mShownAlpha = mAlpha;
6306 if (!mLimitedAlphaCompositing
6307 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6308 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6309 && x == frame.left && y == frame.top))) {
6310 //Log.i(TAG, "Applying alpha transform");
6311 if (selfTransformation) {
6312 mShownAlpha *= mTransformation.getAlpha();
6313 }
6314 if (attachedTransformation != null) {
6315 mShownAlpha *= attachedTransformation.getAlpha();
6316 }
6317 if (appTransformation != null) {
6318 mShownAlpha *= appTransformation.getAlpha();
6319 }
6320 } else {
6321 //Log.i(TAG, "Not applying alpha transform");
6322 }
Romain Guy06882f82009-06-10 13:36:04 -07006323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 if (localLOGV) Log.v(
6325 TAG, "Continuing animation in " + this +
6326 ": " + mShownFrame +
6327 ", alpha=" + mTransformation.getAlpha());
6328 return;
6329 }
Romain Guy06882f82009-06-10 13:36:04 -07006330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 mShownFrame.set(mFrame);
6332 mShownAlpha = mAlpha;
6333 mDsDx = 1;
6334 mDtDx = 0;
6335 mDsDy = 0;
6336 mDtDy = 1;
6337 }
Romain Guy06882f82009-06-10 13:36:04 -07006338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 /**
6340 * Is this window visible? It is not visible if there is no
6341 * surface, or we are in the process of running an exit animation
6342 * that will remove the surface, or its app token has been hidden.
6343 */
6344 public boolean isVisibleLw() {
6345 final AppWindowToken atoken = mAppToken;
6346 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6347 && (atoken == null || !atoken.hiddenRequested)
6348 && !mExiting && !mDestroying;
6349 }
6350
6351 /**
6352 * Is this window visible, ignoring its app token? It is not visible
6353 * if there is no surface, or we are in the process of running an exit animation
6354 * that will remove the surface.
6355 */
6356 public boolean isWinVisibleLw() {
6357 final AppWindowToken atoken = mAppToken;
6358 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6359 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6360 && !mExiting && !mDestroying;
6361 }
6362
6363 /**
6364 * The same as isVisible(), but follows the current hidden state of
6365 * the associated app token, not the pending requested hidden state.
6366 */
6367 boolean isVisibleNow() {
6368 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006369 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 }
6371
6372 /**
6373 * Same as isVisible(), but we also count it as visible between the
6374 * call to IWindowSession.add() and the first relayout().
6375 */
6376 boolean isVisibleOrAdding() {
6377 final AppWindowToken atoken = mAppToken;
6378 return (mSurface != null
6379 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6380 && mPolicyVisibility && !mAttachedHidden
6381 && (atoken == null || !atoken.hiddenRequested)
6382 && !mExiting && !mDestroying;
6383 }
6384
6385 /**
6386 * Is this window currently on-screen? It is on-screen either if it
6387 * is visible or it is currently running an animation before no longer
6388 * being visible.
6389 */
6390 boolean isOnScreen() {
6391 final AppWindowToken atoken = mAppToken;
6392 if (atoken != null) {
6393 return mSurface != null && mPolicyVisibility && !mDestroying
6394 && ((!mAttachedHidden && !atoken.hiddenRequested)
6395 || mAnimating || atoken.animating);
6396 } else {
6397 return mSurface != null && mPolicyVisibility && !mDestroying
6398 && (!mAttachedHidden || mAnimating);
6399 }
6400 }
Romain Guy06882f82009-06-10 13:36:04 -07006401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 /**
6403 * Like isOnScreen(), but we don't return true if the window is part
6404 * of a transition that has not yet been started.
6405 */
6406 boolean isReadyForDisplay() {
6407 final AppWindowToken atoken = mAppToken;
6408 final boolean animating = atoken != null ? atoken.animating : false;
6409 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006410 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006411 || mAnimating || animating);
6412 }
6413
6414 /** Is the window or its container currently animating? */
6415 boolean isAnimating() {
6416 final WindowState attached = mAttachedWindow;
6417 final AppWindowToken atoken = mAppToken;
6418 return mAnimation != null
6419 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006420 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 (atoken.animation != null
6422 || atoken.inPendingTransaction));
6423 }
6424
6425 /** Is this window currently animating? */
6426 boolean isWindowAnimating() {
6427 return mAnimation != null;
6428 }
6429
6430 /**
6431 * Like isOnScreen, but returns false if the surface hasn't yet
6432 * been drawn.
6433 */
6434 public boolean isDisplayedLw() {
6435 final AppWindowToken atoken = mAppToken;
6436 return mSurface != null && mPolicyVisibility && !mDestroying
6437 && !mDrawPending && !mCommitDrawPending
6438 && ((!mAttachedHidden &&
6439 (atoken == null || !atoken.hiddenRequested))
6440 || mAnimating);
6441 }
6442
6443 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6444 boolean shownFrame, boolean onlyOpaque) {
6445 if (mSurface == null) {
6446 return false;
6447 }
6448 if (mAppToken != null && !mAppToken.appFullscreen) {
6449 return false;
6450 }
6451 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6452 return false;
6453 }
6454 final Rect frame = shownFrame ? mShownFrame : mFrame;
6455 if (frame.left <= 0 && frame.top <= 0
6456 && frame.right >= screenWidth
6457 && frame.bottom >= screenHeight) {
6458 return true;
6459 }
6460 return false;
6461 }
Romain Guy06882f82009-06-10 13:36:04 -07006462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 boolean isFullscreenOpaque(int screenWidth, int screenHeight) {
6464 if (mAttrs.format != PixelFormat.OPAQUE || mSurface == null
6465 || mAnimation != null || mDrawPending || mCommitDrawPending) {
6466 return false;
6467 }
6468 if (mFrame.left <= 0 && mFrame.top <= 0 &&
6469 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight) {
6470 return true;
6471 }
6472 return false;
6473 }
6474
6475 void removeLocked() {
6476 if (mAttachedWindow != null) {
6477 mAttachedWindow.mChildWindows.remove(this);
6478 }
6479 destroySurfaceLocked();
6480 mSession.windowRemovedLocked();
6481 try {
6482 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6483 } catch (RuntimeException e) {
6484 // Ignore if it has already been removed (usually because
6485 // we are doing this as part of processing a death note.)
6486 }
6487 }
6488
6489 private class DeathRecipient implements IBinder.DeathRecipient {
6490 public void binderDied() {
6491 try {
6492 synchronized(mWindowMap) {
6493 WindowState win = windowForClientLocked(mSession, mClient);
6494 Log.i(TAG, "WIN DEATH: " + win);
6495 if (win != null) {
6496 removeWindowLocked(mSession, win);
6497 }
6498 }
6499 } catch (IllegalArgumentException ex) {
6500 // This will happen if the window has already been
6501 // removed.
6502 }
6503 }
6504 }
6505
6506 /** Returns true if this window desires key events. */
6507 public final boolean canReceiveKeys() {
6508 return isVisibleOrAdding()
6509 && (mViewVisibility == View.VISIBLE)
6510 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6511 }
6512
6513 public boolean hasDrawnLw() {
6514 return mHasDrawn;
6515 }
6516
6517 public boolean showLw(boolean doAnimation) {
6518 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6519 mPolicyVisibility = true;
6520 mPolicyVisibilityAfterAnim = true;
6521 if (doAnimation) {
6522 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6523 }
6524 requestAnimationLocked(0);
6525 return true;
6526 }
6527 return false;
6528 }
6529
6530 public boolean hideLw(boolean doAnimation) {
6531 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6532 : mPolicyVisibility;
6533 if (current) {
6534 if (doAnimation) {
6535 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6536 if (mAnimation == null) {
6537 doAnimation = false;
6538 }
6539 }
6540 if (doAnimation) {
6541 mPolicyVisibilityAfterAnim = false;
6542 } else {
6543 mPolicyVisibilityAfterAnim = false;
6544 mPolicyVisibility = false;
6545 }
6546 requestAnimationLocked(0);
6547 return true;
6548 }
6549 return false;
6550 }
6551
6552 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006553 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006554
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006555 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6556 pw.print(" mClient="); pw.println(mClient.asBinder());
6557 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6558 if (mAttachedWindow != null || mLayoutAttached) {
6559 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6560 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6561 }
6562 if (mIsImWindow) {
6563 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6564 }
6565 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6566 pw.print(" mSubLayer="); pw.print(mSubLayer);
6567 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6568 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6569 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6570 pw.print("="); pw.print(mAnimLayer);
6571 pw.print(" mLastLayer="); pw.println(mLastLayer);
6572 if (mSurface != null) {
6573 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6574 }
6575 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6576 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6577 if (mAppToken != null) {
6578 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6579 }
6580 if (mTargetAppToken != null) {
6581 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6582 }
6583 pw.print(prefix); pw.print("mViewVisibility=0x");
6584 pw.print(Integer.toHexString(mViewVisibility));
6585 pw.print(" mLastHidden="); pw.print(mLastHidden);
6586 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6587 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6588 pw.print(prefix); pw.print("mPolicyVisibility=");
6589 pw.print(mPolicyVisibility);
6590 pw.print(" mPolicyVisibilityAfterAnim=");
6591 pw.print(mPolicyVisibilityAfterAnim);
6592 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6593 }
6594 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6595 pw.print(" h="); pw.print(mRequestedHeight);
6596 pw.print(" x="); pw.print(mReqXPos);
6597 pw.print(" y="); pw.println(mReqYPos);
6598 pw.print(prefix); pw.print("mGivenContentInsets=");
6599 mGivenContentInsets.printShortString(pw);
6600 pw.print(" mGivenVisibleInsets=");
6601 mGivenVisibleInsets.printShortString(pw);
6602 pw.println();
6603 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6604 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6605 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6606 }
6607 pw.print(prefix); pw.print("mShownFrame=");
6608 mShownFrame.printShortString(pw);
6609 pw.print(" last="); mLastShownFrame.printShortString(pw);
6610 pw.println();
6611 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6612 pw.print(" last="); mLastFrame.printShortString(pw);
6613 pw.println();
6614 pw.print(prefix); pw.print("mContainingFrame=");
6615 mContainingFrame.printShortString(pw);
6616 pw.print(" mDisplayFrame=");
6617 mDisplayFrame.printShortString(pw);
6618 pw.println();
6619 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6620 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6621 pw.println();
6622 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6623 pw.print(" last="); mLastContentInsets.printShortString(pw);
6624 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6625 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6626 pw.println();
6627 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6628 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6629 pw.print(" mAlpha="); pw.print(mAlpha);
6630 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6631 }
6632 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6633 || mAnimation != null) {
6634 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6635 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6636 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6637 pw.print(" mAnimation="); pw.println(mAnimation);
6638 }
6639 if (mHasTransformation || mHasLocalTransformation) {
6640 pw.print(prefix); pw.print("XForm: has=");
6641 pw.print(mHasTransformation);
6642 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6643 pw.print(" "); mTransformation.printShortString(pw);
6644 pw.println();
6645 }
6646 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6647 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6648 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6649 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6650 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6651 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6652 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6653 pw.print(" mDestroying="); pw.print(mDestroying);
6654 pw.print(" mRemoved="); pw.println(mRemoved);
6655 }
6656 if (mOrientationChanging || mAppFreezing) {
6657 pw.print(prefix); pw.print("mOrientationChanging=");
6658 pw.print(mOrientationChanging);
6659 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 }
6662
6663 @Override
6664 public String toString() {
6665 return "Window{"
6666 + Integer.toHexString(System.identityHashCode(this))
6667 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6668 }
6669 }
Romain Guy06882f82009-06-10 13:36:04 -07006670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 // -------------------------------------------------------------
6672 // Window Token State
6673 // -------------------------------------------------------------
6674
6675 class WindowToken {
6676 // The actual token.
6677 final IBinder token;
6678
6679 // The type of window this token is for, as per WindowManager.LayoutParams.
6680 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006682 // Set if this token was explicitly added by a client, so should
6683 // not be removed when all windows are removed.
6684 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006685
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006686 // For printing.
6687 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 // If this is an AppWindowToken, this is non-null.
6690 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 // All of the windows associated with this token.
6693 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6694
6695 // Is key dispatching paused for this token?
6696 boolean paused = false;
6697
6698 // Should this token's windows be hidden?
6699 boolean hidden;
6700
6701 // Temporary for finding which tokens no longer have visible windows.
6702 boolean hasVisible;
6703
6704 WindowToken(IBinder _token, int type, boolean _explicit) {
6705 token = _token;
6706 windowType = type;
6707 explicit = _explicit;
6708 }
6709
6710 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006711 pw.print(prefix); pw.print("token="); pw.println(token);
6712 pw.print(prefix); pw.print("windows="); pw.println(windows);
6713 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6714 pw.print(" hidden="); pw.print(hidden);
6715 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 }
6717
6718 @Override
6719 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006720 if (stringName == null) {
6721 StringBuilder sb = new StringBuilder();
6722 sb.append("WindowToken{");
6723 sb.append(Integer.toHexString(System.identityHashCode(this)));
6724 sb.append(" token="); sb.append(token); sb.append('}');
6725 stringName = sb.toString();
6726 }
6727 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 }
6729 };
6730
6731 class AppWindowToken extends WindowToken {
6732 // Non-null only for application tokens.
6733 final IApplicationToken appToken;
6734
6735 // All of the windows and child windows that are included in this
6736 // application token. Note this list is NOT sorted!
6737 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6738
6739 int groupId = -1;
6740 boolean appFullscreen;
6741 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006743 // These are used for determining when all windows associated with
6744 // an activity have been drawn, so they can be made visible together
6745 // at the same time.
6746 int lastTransactionSequence = mTransactionSequence-1;
6747 int numInterestingWindows;
6748 int numDrawnWindows;
6749 boolean inPendingTransaction;
6750 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 // Is this token going to be hidden in a little while? If so, it
6753 // won't be taken into account for setting the screen orientation.
6754 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 // Is this window's surface needed? This is almost like hidden, except
6757 // it will sometimes be true a little earlier: when the token has
6758 // been shown, but is still waiting for its app transition to execute
6759 // before making its windows shown.
6760 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 // Have we told the window clients to hide themselves?
6763 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 // Last visibility state we reported to the app token.
6766 boolean reportedVisible;
6767
6768 // Set to true when the token has been removed from the window mgr.
6769 boolean removed;
6770
6771 // Have we been asked to have this token keep the screen frozen?
6772 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 boolean animating;
6775 Animation animation;
6776 boolean hasTransformation;
6777 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07006778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 // Offset to the window of all layers in the token, for use by
6780 // AppWindowToken animations.
6781 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07006782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 // Information about an application starting window if displayed.
6784 StartingData startingData;
6785 WindowState startingWindow;
6786 View startingView;
6787 boolean startingDisplayed;
6788 boolean startingMoved;
6789 boolean firstWindowDrawn;
6790
6791 AppWindowToken(IApplicationToken _token) {
6792 super(_token.asBinder(),
6793 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6794 appWindowToken = this;
6795 appToken = _token;
6796 }
Romain Guy06882f82009-06-10 13:36:04 -07006797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 public void setAnimation(Animation anim) {
6799 if (localLOGV) Log.v(
6800 TAG, "Setting animation in " + this + ": " + anim);
6801 animation = anim;
6802 animating = false;
6803 anim.restrictDuration(MAX_ANIMATION_DURATION);
6804 anim.scaleCurrentDuration(mTransitionAnimationScale);
6805 int zorder = anim.getZAdjustment();
6806 int adj = 0;
6807 if (zorder == Animation.ZORDER_TOP) {
6808 adj = TYPE_LAYER_OFFSET;
6809 } else if (zorder == Animation.ZORDER_BOTTOM) {
6810 adj = -TYPE_LAYER_OFFSET;
6811 }
Romain Guy06882f82009-06-10 13:36:04 -07006812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 if (animLayerAdjustment != adj) {
6814 animLayerAdjustment = adj;
6815 updateLayers();
6816 }
6817 }
Romain Guy06882f82009-06-10 13:36:04 -07006818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006819 public void setDummyAnimation() {
6820 if (animation == null) {
6821 if (localLOGV) Log.v(
6822 TAG, "Setting dummy animation in " + this);
6823 animation = sDummyAnimation;
6824 }
6825 }
6826
6827 public void clearAnimation() {
6828 if (animation != null) {
6829 animation = null;
6830 animating = true;
6831 }
6832 }
Romain Guy06882f82009-06-10 13:36:04 -07006833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 void updateLayers() {
6835 final int N = allAppWindows.size();
6836 final int adj = animLayerAdjustment;
6837 for (int i=0; i<N; i++) {
6838 WindowState w = allAppWindows.get(i);
6839 w.mAnimLayer = w.mLayer + adj;
6840 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
6841 + w.mAnimLayer);
6842 if (w == mInputMethodTarget) {
6843 setInputMethodAnimLayerAdjustment(adj);
6844 }
6845 }
6846 }
Romain Guy06882f82009-06-10 13:36:04 -07006847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 void sendAppVisibilityToClients() {
6849 final int N = allAppWindows.size();
6850 for (int i=0; i<N; i++) {
6851 WindowState win = allAppWindows.get(i);
6852 if (win == startingWindow && clientHidden) {
6853 // Don't hide the starting window.
6854 continue;
6855 }
6856 try {
6857 if (DEBUG_VISIBILITY) Log.v(TAG,
6858 "Setting visibility of " + win + ": " + (!clientHidden));
6859 win.mClient.dispatchAppVisibility(!clientHidden);
6860 } catch (RemoteException e) {
6861 }
6862 }
6863 }
Romain Guy06882f82009-06-10 13:36:04 -07006864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 void showAllWindowsLocked() {
6866 final int NW = allAppWindows.size();
6867 for (int i=0; i<NW; i++) {
6868 WindowState w = allAppWindows.get(i);
6869 if (DEBUG_VISIBILITY) Log.v(TAG,
6870 "performing show on: " + w);
6871 w.performShowLocked();
6872 }
6873 }
Romain Guy06882f82009-06-10 13:36:04 -07006874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 // This must be called while inside a transaction.
6876 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6877 if (!mDisplayFrozen) {
6878 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 if (animation == sDummyAnimation) {
6881 // This guy is going to animate, but not yet. For now count
6882 // it is not animating for purposes of scheduling transactions;
6883 // when it is really time to animate, this will be set to
6884 // a real animation and the next call will execute normally.
6885 return false;
6886 }
Romain Guy06882f82009-06-10 13:36:04 -07006887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 if ((allDrawn || animating || startingDisplayed) && animation != null) {
6889 if (!animating) {
6890 if (DEBUG_ANIM) Log.v(
6891 TAG, "Starting animation in " + this +
6892 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
6893 + " scale=" + mTransitionAnimationScale
6894 + " allDrawn=" + allDrawn + " animating=" + animating);
6895 animation.initialize(dw, dh, dw, dh);
6896 animation.setStartTime(currentTime);
6897 animating = true;
6898 }
6899 transformation.clear();
6900 final boolean more = animation.getTransformation(
6901 currentTime, transformation);
6902 if (DEBUG_ANIM) Log.v(
6903 TAG, "Stepped animation in " + this +
6904 ": more=" + more + ", xform=" + transformation);
6905 if (more) {
6906 // we're done!
6907 hasTransformation = true;
6908 return true;
6909 }
6910 if (DEBUG_ANIM) Log.v(
6911 TAG, "Finished animation in " + this +
6912 " @ " + currentTime);
6913 animation = null;
6914 }
6915 } else if (animation != null) {
6916 // If the display is frozen, and there is a pending animation,
6917 // clear it and make sure we run the cleanup code.
6918 animating = true;
6919 animation = null;
6920 }
6921
6922 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07006923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 if (!animating) {
6925 return false;
6926 }
6927
6928 clearAnimation();
6929 animating = false;
6930 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
6931 moveInputMethodWindowsIfNeededLocked(true);
6932 }
Romain Guy06882f82009-06-10 13:36:04 -07006933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006934 if (DEBUG_ANIM) Log.v(
6935 TAG, "Animation done in " + this
6936 + ": reportedVisible=" + reportedVisible);
6937
6938 transformation.clear();
6939 if (animLayerAdjustment != 0) {
6940 animLayerAdjustment = 0;
6941 updateLayers();
6942 }
Romain Guy06882f82009-06-10 13:36:04 -07006943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 final int N = windows.size();
6945 for (int i=0; i<N; i++) {
6946 ((WindowState)windows.get(i)).finishExit();
6947 }
6948 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07006949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 return false;
6951 }
6952
6953 void updateReportedVisibilityLocked() {
6954 if (appToken == null) {
6955 return;
6956 }
Romain Guy06882f82009-06-10 13:36:04 -07006957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 int numInteresting = 0;
6959 int numVisible = 0;
6960 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07006961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
6963 final int N = allAppWindows.size();
6964 for (int i=0; i<N; i++) {
6965 WindowState win = allAppWindows.get(i);
6966 if (win == startingWindow || win.mAppFreezing) {
6967 continue;
6968 }
6969 if (DEBUG_VISIBILITY) {
6970 Log.v(TAG, "Win " + win + ": isDisplayed="
6971 + win.isDisplayedLw()
6972 + ", isAnimating=" + win.isAnimating());
6973 if (!win.isDisplayedLw()) {
6974 Log.v(TAG, "Not displayed: s=" + win.mSurface
6975 + " pv=" + win.mPolicyVisibility
6976 + " dp=" + win.mDrawPending
6977 + " cdp=" + win.mCommitDrawPending
6978 + " ah=" + win.mAttachedHidden
6979 + " th="
6980 + (win.mAppToken != null
6981 ? win.mAppToken.hiddenRequested : false)
6982 + " a=" + win.mAnimating);
6983 }
6984 }
6985 numInteresting++;
6986 if (win.isDisplayedLw()) {
6987 if (!win.isAnimating()) {
6988 numVisible++;
6989 }
6990 nowGone = false;
6991 } else if (win.isAnimating()) {
6992 nowGone = false;
6993 }
6994 }
Romain Guy06882f82009-06-10 13:36:04 -07006995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
6997 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
6998 + numInteresting + " visible=" + numVisible);
6999 if (nowVisible != reportedVisible) {
7000 if (DEBUG_VISIBILITY) Log.v(
7001 TAG, "Visibility changed in " + this
7002 + ": vis=" + nowVisible);
7003 reportedVisible = nowVisible;
7004 Message m = mH.obtainMessage(
7005 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7006 nowVisible ? 1 : 0,
7007 nowGone ? 1 : 0,
7008 this);
7009 mH.sendMessage(m);
7010 }
7011 }
Romain Guy06882f82009-06-10 13:36:04 -07007012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 void dump(PrintWriter pw, String prefix) {
7014 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007015 if (appToken != null) {
7016 pw.print(prefix); pw.println("app=true");
7017 }
7018 if (allAppWindows.size() > 0) {
7019 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7020 }
7021 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7022 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7023 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7024 pw.print(" clientHidden="); pw.print(clientHidden);
7025 pw.print(" willBeHidden="); pw.print(willBeHidden);
7026 pw.print(" reportedVisible="); pw.println(reportedVisible);
7027 if (paused || freezingScreen) {
7028 pw.print(prefix); pw.print("paused="); pw.print(paused);
7029 pw.print(" freezingScreen="); pw.println(freezingScreen);
7030 }
7031 if (numInterestingWindows != 0 || numDrawnWindows != 0
7032 || inPendingTransaction || allDrawn) {
7033 pw.print(prefix); pw.print("numInterestingWindows=");
7034 pw.print(numInterestingWindows);
7035 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7036 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7037 pw.print(" allDrawn="); pw.println(allDrawn);
7038 }
7039 if (animating || animation != null) {
7040 pw.print(prefix); pw.print("animating="); pw.print(animating);
7041 pw.print(" animation="); pw.println(animation);
7042 }
7043 if (animLayerAdjustment != 0) {
7044 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7045 }
7046 if (hasTransformation) {
7047 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7048 pw.print(" transformation="); transformation.printShortString(pw);
7049 pw.println();
7050 }
7051 if (startingData != null || removed || firstWindowDrawn) {
7052 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7053 pw.print(" removed="); pw.print(removed);
7054 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7055 }
7056 if (startingWindow != null || startingView != null
7057 || startingDisplayed || startingMoved) {
7058 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7059 pw.print(" startingView="); pw.print(startingView);
7060 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7061 pw.print(" startingMoved"); pw.println(startingMoved);
7062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007063 }
7064
7065 @Override
7066 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007067 if (stringName == null) {
7068 StringBuilder sb = new StringBuilder();
7069 sb.append("AppWindowToken{");
7070 sb.append(Integer.toHexString(System.identityHashCode(this)));
7071 sb.append(" token="); sb.append(token); sb.append('}');
7072 stringName = sb.toString();
7073 }
7074 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007075 }
7076 }
Romain Guy06882f82009-06-10 13:36:04 -07007077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 public static WindowManager.LayoutParams findAnimations(
7079 ArrayList<AppWindowToken> order,
7080 ArrayList<AppWindowToken> tokenList1,
7081 ArrayList<AppWindowToken> tokenList2) {
7082 // We need to figure out which animation to use...
7083 WindowManager.LayoutParams animParams = null;
7084 int animSrc = 0;
Romain Guy06882f82009-06-10 13:36:04 -07007085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 //Log.i(TAG, "Looking for animations...");
7087 for (int i=order.size()-1; i>=0; i--) {
7088 AppWindowToken wtoken = order.get(i);
7089 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
7090 if (tokenList1.contains(wtoken) || tokenList2.contains(wtoken)) {
7091 int j = wtoken.windows.size();
7092 while (j > 0) {
7093 j--;
7094 WindowState win = wtoken.windows.get(j);
7095 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7096 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7097 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7098 //Log.i(TAG, "Found base or application window, done!");
7099 if (wtoken.appFullscreen) {
7100 return win.mAttrs;
7101 }
7102 if (animSrc < 2) {
7103 animParams = win.mAttrs;
7104 animSrc = 2;
7105 }
7106 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7107 //Log.i(TAG, "Found normal window, we may use this...");
7108 animParams = win.mAttrs;
7109 animSrc = 1;
7110 }
7111 }
7112 }
7113 }
Romain Guy06882f82009-06-10 13:36:04 -07007114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 return animParams;
7116 }
Romain Guy06882f82009-06-10 13:36:04 -07007117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 // -------------------------------------------------------------
7119 // DummyAnimation
7120 // -------------------------------------------------------------
7121
7122 // This is an animation that does nothing: it just immediately finishes
7123 // itself every time it is called. It is used as a stub animation in cases
7124 // where we want to synchronize multiple things that may be animating.
7125 static final class DummyAnimation extends Animation {
7126 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7127 return false;
7128 }
7129 }
7130 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 // -------------------------------------------------------------
7133 // Async Handler
7134 // -------------------------------------------------------------
7135
7136 static final class StartingData {
7137 final String pkg;
7138 final int theme;
7139 final CharSequence nonLocalizedLabel;
7140 final int labelRes;
7141 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7144 int _labelRes, int _icon) {
7145 pkg = _pkg;
7146 theme = _theme;
7147 nonLocalizedLabel = _nonLocalizedLabel;
7148 labelRes = _labelRes;
7149 icon = _icon;
7150 }
7151 }
7152
7153 private final class H extends Handler {
7154 public static final int REPORT_FOCUS_CHANGE = 2;
7155 public static final int REPORT_LOSING_FOCUS = 3;
7156 public static final int ANIMATE = 4;
7157 public static final int ADD_STARTING = 5;
7158 public static final int REMOVE_STARTING = 6;
7159 public static final int FINISHED_STARTING = 7;
7160 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7162 public static final int HOLD_SCREEN_CHANGED = 12;
7163 public static final int APP_TRANSITION_TIMEOUT = 13;
7164 public static final int PERSIST_ANIMATION_SCALE = 14;
7165 public static final int FORCE_GC = 15;
7166 public static final int ENABLE_SCREEN = 16;
7167 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007168 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172 public H() {
7173 }
Romain Guy06882f82009-06-10 13:36:04 -07007174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007175 @Override
7176 public void handleMessage(Message msg) {
7177 switch (msg.what) {
7178 case REPORT_FOCUS_CHANGE: {
7179 WindowState lastFocus;
7180 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 synchronized(mWindowMap) {
7183 lastFocus = mLastFocus;
7184 newFocus = mCurrentFocus;
7185 if (lastFocus == newFocus) {
7186 // Focus is not changing, so nothing to do.
7187 return;
7188 }
7189 mLastFocus = newFocus;
7190 //Log.i(TAG, "Focus moving from " + lastFocus
7191 // + " to " + newFocus);
7192 if (newFocus != null && lastFocus != null
7193 && !newFocus.isDisplayedLw()) {
7194 //Log.i(TAG, "Delaying loss of focus...");
7195 mLosingFocus.add(lastFocus);
7196 lastFocus = null;
7197 }
7198 }
7199
7200 if (lastFocus != newFocus) {
7201 //System.out.println("Changing focus from " + lastFocus
7202 // + " to " + newFocus);
7203 if (newFocus != null) {
7204 try {
7205 //Log.i(TAG, "Gaining focus: " + newFocus);
7206 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7207 } catch (RemoteException e) {
7208 // Ignore if process has died.
7209 }
7210 }
7211
7212 if (lastFocus != null) {
7213 try {
7214 //Log.i(TAG, "Losing focus: " + lastFocus);
7215 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7216 } catch (RemoteException e) {
7217 // Ignore if process has died.
7218 }
7219 }
7220 }
7221 } break;
7222
7223 case REPORT_LOSING_FOCUS: {
7224 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 synchronized(mWindowMap) {
7227 losers = mLosingFocus;
7228 mLosingFocus = new ArrayList<WindowState>();
7229 }
7230
7231 final int N = losers.size();
7232 for (int i=0; i<N; i++) {
7233 try {
7234 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7235 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7236 } catch (RemoteException e) {
7237 // Ignore if process has died.
7238 }
7239 }
7240 } break;
7241
7242 case ANIMATE: {
7243 synchronized(mWindowMap) {
7244 mAnimationPending = false;
7245 performLayoutAndPlaceSurfacesLocked();
7246 }
7247 } break;
7248
7249 case ADD_STARTING: {
7250 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7251 final StartingData sd = wtoken.startingData;
7252
7253 if (sd == null) {
7254 // Animation has been canceled... do nothing.
7255 return;
7256 }
Romain Guy06882f82009-06-10 13:36:04 -07007257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7259 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007261 View view = null;
7262 try {
7263 view = mPolicy.addStartingWindow(
7264 wtoken.token, sd.pkg,
7265 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7266 sd.icon);
7267 } catch (Exception e) {
7268 Log.w(TAG, "Exception when adding starting window", e);
7269 }
7270
7271 if (view != null) {
7272 boolean abort = false;
7273
7274 synchronized(mWindowMap) {
7275 if (wtoken.removed || wtoken.startingData == null) {
7276 // If the window was successfully added, then
7277 // we need to remove it.
7278 if (wtoken.startingWindow != null) {
7279 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7280 "Aborted starting " + wtoken
7281 + ": removed=" + wtoken.removed
7282 + " startingData=" + wtoken.startingData);
7283 wtoken.startingWindow = null;
7284 wtoken.startingData = null;
7285 abort = true;
7286 }
7287 } else {
7288 wtoken.startingView = view;
7289 }
7290 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7291 "Added starting " + wtoken
7292 + ": startingWindow="
7293 + wtoken.startingWindow + " startingView="
7294 + wtoken.startingView);
7295 }
7296
7297 if (abort) {
7298 try {
7299 mPolicy.removeStartingWindow(wtoken.token, view);
7300 } catch (Exception e) {
7301 Log.w(TAG, "Exception when removing starting window", e);
7302 }
7303 }
7304 }
7305 } break;
7306
7307 case REMOVE_STARTING: {
7308 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7309 IBinder token = null;
7310 View view = null;
7311 synchronized (mWindowMap) {
7312 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7313 + wtoken + ": startingWindow="
7314 + wtoken.startingWindow + " startingView="
7315 + wtoken.startingView);
7316 if (wtoken.startingWindow != null) {
7317 view = wtoken.startingView;
7318 token = wtoken.token;
7319 wtoken.startingData = null;
7320 wtoken.startingView = null;
7321 wtoken.startingWindow = null;
7322 }
7323 }
7324 if (view != null) {
7325 try {
7326 mPolicy.removeStartingWindow(token, view);
7327 } catch (Exception e) {
7328 Log.w(TAG, "Exception when removing starting window", e);
7329 }
7330 }
7331 } break;
7332
7333 case FINISHED_STARTING: {
7334 IBinder token = null;
7335 View view = null;
7336 while (true) {
7337 synchronized (mWindowMap) {
7338 final int N = mFinishedStarting.size();
7339 if (N <= 0) {
7340 break;
7341 }
7342 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7343
7344 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7345 "Finished starting " + wtoken
7346 + ": startingWindow=" + wtoken.startingWindow
7347 + " startingView=" + wtoken.startingView);
7348
7349 if (wtoken.startingWindow == null) {
7350 continue;
7351 }
7352
7353 view = wtoken.startingView;
7354 token = wtoken.token;
7355 wtoken.startingData = null;
7356 wtoken.startingView = null;
7357 wtoken.startingWindow = null;
7358 }
7359
7360 try {
7361 mPolicy.removeStartingWindow(token, view);
7362 } catch (Exception e) {
7363 Log.w(TAG, "Exception when removing starting window", e);
7364 }
7365 }
7366 } break;
7367
7368 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7369 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7370
7371 boolean nowVisible = msg.arg1 != 0;
7372 boolean nowGone = msg.arg2 != 0;
7373
7374 try {
7375 if (DEBUG_VISIBILITY) Log.v(
7376 TAG, "Reporting visible in " + wtoken
7377 + " visible=" + nowVisible
7378 + " gone=" + nowGone);
7379 if (nowVisible) {
7380 wtoken.appToken.windowsVisible();
7381 } else {
7382 wtoken.appToken.windowsGone();
7383 }
7384 } catch (RemoteException ex) {
7385 }
7386 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007388 case WINDOW_FREEZE_TIMEOUT: {
7389 synchronized (mWindowMap) {
7390 Log.w(TAG, "Window freeze timeout expired.");
7391 int i = mWindows.size();
7392 while (i > 0) {
7393 i--;
7394 WindowState w = (WindowState)mWindows.get(i);
7395 if (w.mOrientationChanging) {
7396 w.mOrientationChanging = false;
7397 Log.w(TAG, "Force clearing orientation change: " + w);
7398 }
7399 }
7400 performLayoutAndPlaceSurfacesLocked();
7401 }
7402 break;
7403 }
Romain Guy06882f82009-06-10 13:36:04 -07007404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 case HOLD_SCREEN_CHANGED: {
7406 Session oldHold;
7407 Session newHold;
7408 synchronized (mWindowMap) {
7409 oldHold = mLastReportedHold;
7410 newHold = (Session)msg.obj;
7411 mLastReportedHold = newHold;
7412 }
Romain Guy06882f82009-06-10 13:36:04 -07007413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 if (oldHold != newHold) {
7415 try {
7416 if (oldHold != null) {
7417 mBatteryStats.noteStopWakelock(oldHold.mUid,
7418 "window",
7419 BatteryStats.WAKE_TYPE_WINDOW);
7420 }
7421 if (newHold != null) {
7422 mBatteryStats.noteStartWakelock(newHold.mUid,
7423 "window",
7424 BatteryStats.WAKE_TYPE_WINDOW);
7425 }
7426 } catch (RemoteException e) {
7427 }
7428 }
7429 break;
7430 }
Romain Guy06882f82009-06-10 13:36:04 -07007431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 case APP_TRANSITION_TIMEOUT: {
7433 synchronized (mWindowMap) {
7434 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7435 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7436 "*** APP TRANSITION TIMEOUT");
7437 mAppTransitionReady = true;
7438 mAppTransitionTimeout = true;
7439 performLayoutAndPlaceSurfacesLocked();
7440 }
7441 }
7442 break;
7443 }
Romain Guy06882f82009-06-10 13:36:04 -07007444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 case PERSIST_ANIMATION_SCALE: {
7446 Settings.System.putFloat(mContext.getContentResolver(),
7447 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7448 Settings.System.putFloat(mContext.getContentResolver(),
7449 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7450 break;
7451 }
Romain Guy06882f82009-06-10 13:36:04 -07007452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 case FORCE_GC: {
7454 synchronized(mWindowMap) {
7455 if (mAnimationPending) {
7456 // If we are animating, don't do the gc now but
7457 // delay a bit so we don't interrupt the animation.
7458 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7459 2000);
7460 return;
7461 }
7462 // If we are currently rotating the display, it will
7463 // schedule a new message when done.
7464 if (mDisplayFrozen) {
7465 return;
7466 }
7467 mFreezeGcPending = 0;
7468 }
7469 Runtime.getRuntime().gc();
7470 break;
7471 }
Romain Guy06882f82009-06-10 13:36:04 -07007472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473 case ENABLE_SCREEN: {
7474 performEnableScreen();
7475 break;
7476 }
Romain Guy06882f82009-06-10 13:36:04 -07007477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 case APP_FREEZE_TIMEOUT: {
7479 synchronized (mWindowMap) {
7480 Log.w(TAG, "App freeze timeout expired.");
7481 int i = mAppTokens.size();
7482 while (i > 0) {
7483 i--;
7484 AppWindowToken tok = mAppTokens.get(i);
7485 if (tok.freezingScreen) {
7486 Log.w(TAG, "Force clearing freeze: " + tok);
7487 unsetAppFreezingScreenLocked(tok, true, true);
7488 }
7489 }
7490 }
7491 break;
7492 }
Romain Guy06882f82009-06-10 13:36:04 -07007493
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007494 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
The Android Open Source Project10592532009-03-18 17:39:46 -07007495 if (updateOrientationFromAppTokens(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007496 sendNewConfiguration();
7497 }
7498 break;
7499 }
Romain Guy06882f82009-06-10 13:36:04 -07007500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 }
7502 }
7503 }
7504
7505 // -------------------------------------------------------------
7506 // IWindowManager API
7507 // -------------------------------------------------------------
7508
7509 public IWindowSession openSession(IInputMethodClient client,
7510 IInputContext inputContext) {
7511 if (client == null) throw new IllegalArgumentException("null client");
7512 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7513 return new Session(client, inputContext);
7514 }
7515
7516 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7517 synchronized (mWindowMap) {
7518 // The focus for the client is the window immediately below
7519 // where we would place the input method window.
7520 int idx = findDesiredInputMethodWindowIndexLocked(false);
7521 WindowState imFocus;
7522 if (idx > 0) {
7523 imFocus = (WindowState)mWindows.get(idx-1);
7524 if (imFocus != null) {
7525 if (imFocus.mSession.mClient != null &&
7526 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7527 return true;
7528 }
7529 }
7530 }
7531 }
7532 return false;
7533 }
Romain Guy06882f82009-06-10 13:36:04 -07007534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 // -------------------------------------------------------------
7536 // Internals
7537 // -------------------------------------------------------------
7538
7539 final WindowState windowForClientLocked(Session session, IWindow client) {
7540 return windowForClientLocked(session, client.asBinder());
7541 }
Romain Guy06882f82009-06-10 13:36:04 -07007542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007543 final WindowState windowForClientLocked(Session session, IBinder client) {
7544 WindowState win = mWindowMap.get(client);
7545 if (localLOGV) Log.v(
7546 TAG, "Looking up client " + client + ": " + win);
7547 if (win == null) {
7548 RuntimeException ex = new RuntimeException();
7549 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7550 return null;
7551 }
7552 if (session != null && win.mSession != session) {
7553 RuntimeException ex = new RuntimeException();
7554 Log.w(TAG, "Requested window " + client + " is in session " +
7555 win.mSession + ", not " + session, ex);
7556 return null;
7557 }
7558
7559 return win;
7560 }
7561
7562 private final void assignLayersLocked() {
7563 int N = mWindows.size();
7564 int curBaseLayer = 0;
7565 int curLayer = 0;
7566 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 for (i=0; i<N; i++) {
7569 WindowState w = (WindowState)mWindows.get(i);
7570 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7571 curLayer += WINDOW_LAYER_MULTIPLIER;
7572 w.mLayer = curLayer;
7573 } else {
7574 curBaseLayer = curLayer = w.mBaseLayer;
7575 w.mLayer = curLayer;
7576 }
7577 if (w.mTargetAppToken != null) {
7578 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7579 } else if (w.mAppToken != null) {
7580 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7581 } else {
7582 w.mAnimLayer = w.mLayer;
7583 }
7584 if (w.mIsImWindow) {
7585 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7586 }
7587 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7588 + w.mAnimLayer);
7589 //System.out.println(
7590 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7591 }
7592 }
7593
7594 private boolean mInLayout = false;
7595 private final void performLayoutAndPlaceSurfacesLocked() {
7596 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007597 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007598 throw new RuntimeException("Recursive call!");
7599 }
7600 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7601 return;
7602 }
7603
7604 boolean recoveringMemory = false;
7605 if (mForceRemoves != null) {
7606 recoveringMemory = true;
7607 // Wait a little it for things to settle down, and off we go.
7608 for (int i=0; i<mForceRemoves.size(); i++) {
7609 WindowState ws = mForceRemoves.get(i);
7610 Log.i(TAG, "Force removing: " + ws);
7611 removeWindowInnerLocked(ws.mSession, ws);
7612 }
7613 mForceRemoves = null;
7614 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7615 Object tmp = new Object();
7616 synchronized (tmp) {
7617 try {
7618 tmp.wait(250);
7619 } catch (InterruptedException e) {
7620 }
7621 }
7622 }
Romain Guy06882f82009-06-10 13:36:04 -07007623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 mInLayout = true;
7625 try {
7626 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007628 int i = mPendingRemove.size()-1;
7629 if (i >= 0) {
7630 while (i >= 0) {
7631 WindowState w = mPendingRemove.get(i);
7632 removeWindowInnerLocked(w.mSession, w);
7633 i--;
7634 }
7635 mPendingRemove.clear();
7636
7637 mInLayout = false;
7638 assignLayersLocked();
7639 mLayoutNeeded = true;
7640 performLayoutAndPlaceSurfacesLocked();
7641
7642 } else {
7643 mInLayout = false;
7644 if (mLayoutNeeded) {
7645 requestAnimationLocked(0);
7646 }
7647 }
7648 } catch (RuntimeException e) {
7649 mInLayout = false;
7650 Log.e(TAG, "Unhandled exception while layout out windows", e);
7651 }
7652 }
7653
7654 private final void performLayoutLockedInner() {
7655 final int dw = mDisplay.getWidth();
7656 final int dh = mDisplay.getHeight();
7657
7658 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007659 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 int i;
7661
7662 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007663
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007664 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 mPolicy.beginLayoutLw(dw, dh);
7666
7667 // First perform layout of any root windows (not attached
7668 // to another window).
7669 int topAttached = -1;
7670 for (i = N-1; i >= 0; i--) {
7671 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007672
7673 // Don't do layout of a window if it is not visible, or
7674 // soon won't be visible, to avoid wasting time and funky
7675 // changes while a window is animating away.
7676 final AppWindowToken atoken = win.mAppToken;
7677 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007679 || win.mRootToken.hidden
7680 || (atoken != null && atoken.hiddenRequested)
7681 || !win.mPolicyVisibility
7682 || win.mAttachedHidden
7683 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684
7685 // If this view is GONE, then skip it -- keep the current
7686 // frame, and let the caller know so they can ignore it
7687 // if they want. (We do the normal layout for INVISIBLE
7688 // windows, since that means "perform layout as normal,
7689 // just don't display").
7690 if (!gone || !win.mHaveFrame) {
7691 if (!win.mLayoutAttached) {
7692 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7693 } else {
7694 if (topAttached < 0) topAttached = i;
7695 }
7696 }
7697 }
Romain Guy06882f82009-06-10 13:36:04 -07007698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 // Now perform layout of attached windows, which usually
7700 // depend on the position of the window they are attached to.
7701 // XXX does not deal with windows that are attached to windows
7702 // that are themselves attached.
7703 for (i = topAttached; i >= 0; i--) {
7704 WindowState win = (WindowState) mWindows.get(i);
7705
7706 // If this view is GONE, then skip it -- keep the current
7707 // frame, and let the caller know so they can ignore it
7708 // if they want. (We do the normal layout for INVISIBLE
7709 // windows, since that means "perform layout as normal,
7710 // just don't display").
7711 if (win.mLayoutAttached) {
7712 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7713 || !win.mHaveFrame) {
7714 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7715 }
7716 }
7717 }
7718
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007719 if (!mPolicy.finishLayoutLw()) {
7720 mLayoutNeeded = false;
7721 } else if (repeats > 2) {
7722 Log.w(TAG, "Layout repeat aborted after too many iterations");
7723 mLayoutNeeded = false;
7724 } else {
7725 repeats++;
7726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 }
7728 }
Romain Guy06882f82009-06-10 13:36:04 -07007729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 private final void performLayoutAndPlaceSurfacesLockedInner(
7731 boolean recoveringMemory) {
7732 final long currentTime = SystemClock.uptimeMillis();
7733 final int dw = mDisplay.getWidth();
7734 final int dh = mDisplay.getHeight();
7735
7736 final int N = mWindows.size();
7737 int i;
7738
7739 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007742 if (mFxSession == null) {
7743 mFxSession = new SurfaceSession();
7744 }
Romain Guy06882f82009-06-10 13:36:04 -07007745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7747
7748 // Initialize state of exiting tokens.
7749 for (i=mExitingTokens.size()-1; i>=0; i--) {
7750 mExitingTokens.get(i).hasVisible = false;
7751 }
7752
7753 // Initialize state of exiting applications.
7754 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7755 mExitingAppTokens.get(i).hasVisible = false;
7756 }
7757
7758 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 boolean orientationChangeComplete = true;
7760 Session holdScreen = null;
7761 float screenBrightness = -1;
7762 boolean focusDisplayed = false;
7763 boolean animating = false;
7764
7765 Surface.openTransaction();
7766 try {
7767 boolean restart;
7768
7769 do {
7770 final int transactionSequence = ++mTransactionSequence;
7771
7772 // Update animations of all applications, including those
7773 // associated with exiting/removed apps
7774 boolean tokensAnimating = false;
7775 final int NAT = mAppTokens.size();
7776 for (i=0; i<NAT; i++) {
7777 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7778 tokensAnimating = true;
7779 }
7780 }
7781 final int NEAT = mExitingAppTokens.size();
7782 for (i=0; i<NEAT; i++) {
7783 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7784 tokensAnimating = true;
7785 }
7786 }
7787
7788 animating = tokensAnimating;
7789 restart = false;
7790
7791 boolean tokenMayBeDrawn = false;
7792
7793 mPolicy.beginAnimationLw(dw, dh);
7794
7795 for (i=N-1; i>=0; i--) {
7796 WindowState w = (WindowState)mWindows.get(i);
7797
7798 final WindowManager.LayoutParams attrs = w.mAttrs;
7799
7800 if (w.mSurface != null) {
7801 // Execute animation.
7802 w.commitFinishDrawingLocked(currentTime);
7803 if (w.stepAnimationLocked(currentTime, dw, dh)) {
7804 animating = true;
7805 //w.dump(" ");
7806 }
7807
7808 mPolicy.animatingWindowLw(w, attrs);
7809 }
7810
7811 final AppWindowToken atoken = w.mAppToken;
7812 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
7813 if (atoken.lastTransactionSequence != transactionSequence) {
7814 atoken.lastTransactionSequence = transactionSequence;
7815 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
7816 atoken.startingDisplayed = false;
7817 }
7818 if ((w.isOnScreen() || w.mAttrs.type
7819 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
7820 && !w.mExiting && !w.mDestroying) {
7821 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
7822 Log.v(TAG, "Eval win " + w + ": isDisplayed="
7823 + w.isDisplayedLw()
7824 + ", isAnimating=" + w.isAnimating());
7825 if (!w.isDisplayedLw()) {
7826 Log.v(TAG, "Not displayed: s=" + w.mSurface
7827 + " pv=" + w.mPolicyVisibility
7828 + " dp=" + w.mDrawPending
7829 + " cdp=" + w.mCommitDrawPending
7830 + " ah=" + w.mAttachedHidden
7831 + " th=" + atoken.hiddenRequested
7832 + " a=" + w.mAnimating);
7833 }
7834 }
7835 if (w != atoken.startingWindow) {
7836 if (!atoken.freezingScreen || !w.mAppFreezing) {
7837 atoken.numInterestingWindows++;
7838 if (w.isDisplayedLw()) {
7839 atoken.numDrawnWindows++;
7840 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
7841 "tokenMayBeDrawn: " + atoken
7842 + " freezingScreen=" + atoken.freezingScreen
7843 + " mAppFreezing=" + w.mAppFreezing);
7844 tokenMayBeDrawn = true;
7845 }
7846 }
7847 } else if (w.isDisplayedLw()) {
7848 atoken.startingDisplayed = true;
7849 }
7850 }
7851 } else if (w.mReadyToShow) {
7852 w.performShowLocked();
7853 }
7854 }
7855
7856 if (mPolicy.finishAnimationLw()) {
7857 restart = true;
7858 }
7859
7860 if (tokenMayBeDrawn) {
7861 // See if any windows have been drawn, so they (and others
7862 // associated with them) can now be shown.
7863 final int NT = mTokenList.size();
7864 for (i=0; i<NT; i++) {
7865 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
7866 if (wtoken == null) {
7867 continue;
7868 }
7869 if (wtoken.freezingScreen) {
7870 int numInteresting = wtoken.numInterestingWindows;
7871 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
7872 if (DEBUG_VISIBILITY) Log.v(TAG,
7873 "allDrawn: " + wtoken
7874 + " interesting=" + numInteresting
7875 + " drawn=" + wtoken.numDrawnWindows);
7876 wtoken.showAllWindowsLocked();
7877 unsetAppFreezingScreenLocked(wtoken, false, true);
7878 orientationChangeComplete = true;
7879 }
7880 } else if (!wtoken.allDrawn) {
7881 int numInteresting = wtoken.numInterestingWindows;
7882 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
7883 if (DEBUG_VISIBILITY) Log.v(TAG,
7884 "allDrawn: " + wtoken
7885 + " interesting=" + numInteresting
7886 + " drawn=" + wtoken.numDrawnWindows);
7887 wtoken.allDrawn = true;
7888 restart = true;
7889
7890 // We can now show all of the drawn windows!
7891 if (!mOpeningApps.contains(wtoken)) {
7892 wtoken.showAllWindowsLocked();
7893 }
7894 }
7895 }
7896 }
7897 }
7898
7899 // If we are ready to perform an app transition, check through
7900 // all of the app tokens to be shown and see if they are ready
7901 // to go.
7902 if (mAppTransitionReady) {
7903 int NN = mOpeningApps.size();
7904 boolean goodToGo = true;
7905 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7906 "Checking " + NN + " opening apps (frozen="
7907 + mDisplayFrozen + " timeout="
7908 + mAppTransitionTimeout + ")...");
7909 if (!mDisplayFrozen && !mAppTransitionTimeout) {
7910 // If the display isn't frozen, wait to do anything until
7911 // all of the apps are ready. Otherwise just go because
7912 // we'll unfreeze the display when everyone is ready.
7913 for (i=0; i<NN && goodToGo; i++) {
7914 AppWindowToken wtoken = mOpeningApps.get(i);
7915 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7916 "Check opening app" + wtoken + ": allDrawn="
7917 + wtoken.allDrawn + " startingDisplayed="
7918 + wtoken.startingDisplayed);
7919 if (!wtoken.allDrawn && !wtoken.startingDisplayed
7920 && !wtoken.startingMoved) {
7921 goodToGo = false;
7922 }
7923 }
7924 }
7925 if (goodToGo) {
7926 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
7927 int transit = mNextAppTransition;
7928 if (mSkipAppTransitionAnimation) {
7929 transit = WindowManagerPolicy.TRANSIT_NONE;
7930 }
7931 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
7932 mAppTransitionReady = false;
7933 mAppTransitionTimeout = false;
7934 mStartingIconInTransition = false;
7935 mSkipAppTransitionAnimation = false;
7936
7937 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
7938
7939 // We need to figure out which animation to use...
7940 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
7941 mOpeningApps, mClosingApps);
7942
7943 NN = mOpeningApps.size();
7944 for (i=0; i<NN; i++) {
7945 AppWindowToken wtoken = mOpeningApps.get(i);
7946 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7947 "Now opening app" + wtoken);
7948 wtoken.reportedVisible = false;
7949 wtoken.inPendingTransaction = false;
7950 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
7951 wtoken.updateReportedVisibilityLocked();
7952 wtoken.showAllWindowsLocked();
7953 }
7954 NN = mClosingApps.size();
7955 for (i=0; i<NN; i++) {
7956 AppWindowToken wtoken = mClosingApps.get(i);
7957 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7958 "Now closing app" + wtoken);
7959 wtoken.inPendingTransaction = false;
7960 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
7961 wtoken.updateReportedVisibilityLocked();
7962 // Force the allDrawn flag, because we want to start
7963 // this guy's animations regardless of whether it's
7964 // gotten drawn.
7965 wtoken.allDrawn = true;
7966 }
7967
7968 mOpeningApps.clear();
7969 mClosingApps.clear();
7970
7971 // This has changed the visibility of windows, so perform
7972 // a new layout to get them all up-to-date.
7973 mLayoutNeeded = true;
7974 moveInputMethodWindowsIfNeededLocked(true);
7975 performLayoutLockedInner();
7976 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
7977
7978 restart = true;
7979 }
7980 }
7981 } while (restart);
7982
7983 // THIRD LOOP: Update the surfaces of all windows.
7984
7985 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
7986
7987 boolean obscured = false;
7988 boolean blurring = false;
7989 boolean dimming = false;
7990 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07007991 boolean syswin = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007992
7993 for (i=N-1; i>=0; i--) {
7994 WindowState w = (WindowState)mWindows.get(i);
7995
7996 boolean displayed = false;
7997 final WindowManager.LayoutParams attrs = w.mAttrs;
7998 final int attrFlags = attrs.flags;
7999
8000 if (w.mSurface != null) {
8001 w.computeShownFrameLocked();
8002 if (localLOGV) Log.v(
8003 TAG, "Placing surface #" + i + " " + w.mSurface
8004 + ": new=" + w.mShownFrame + ", old="
8005 + w.mLastShownFrame);
8006
8007 boolean resize;
8008 int width, height;
8009 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8010 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8011 w.mLastRequestedHeight != w.mRequestedHeight;
8012 // for a scaled surface, we just want to use
8013 // the requested size.
8014 width = w.mRequestedWidth;
8015 height = w.mRequestedHeight;
8016 w.mLastRequestedWidth = width;
8017 w.mLastRequestedHeight = height;
8018 w.mLastShownFrame.set(w.mShownFrame);
8019 try {
8020 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8021 } catch (RuntimeException e) {
8022 Log.w(TAG, "Error positioning surface in " + w, e);
8023 if (!recoveringMemory) {
8024 reclaimSomeSurfaceMemoryLocked(w, "position");
8025 }
8026 }
8027 } else {
8028 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8029 width = w.mShownFrame.width();
8030 height = w.mShownFrame.height();
8031 w.mLastShownFrame.set(w.mShownFrame);
8032 if (resize) {
8033 if (SHOW_TRANSACTIONS) Log.i(
8034 TAG, " SURFACE " + w.mSurface + ": ("
8035 + w.mShownFrame.left + ","
8036 + w.mShownFrame.top + ") ("
8037 + w.mShownFrame.width() + "x"
8038 + w.mShownFrame.height() + ")");
8039 }
8040 }
8041
8042 if (resize) {
8043 if (width < 1) width = 1;
8044 if (height < 1) height = 1;
8045 if (w.mSurface != null) {
8046 try {
8047 w.mSurface.setSize(width, height);
8048 w.mSurface.setPosition(w.mShownFrame.left,
8049 w.mShownFrame.top);
8050 } catch (RuntimeException e) {
8051 // If something goes wrong with the surface (such
8052 // as running out of memory), don't take down the
8053 // entire system.
8054 Log.e(TAG, "Failure updating surface of " + w
8055 + "size=(" + width + "x" + height
8056 + "), pos=(" + w.mShownFrame.left
8057 + "," + w.mShownFrame.top + ")", e);
8058 if (!recoveringMemory) {
8059 reclaimSomeSurfaceMemoryLocked(w, "size");
8060 }
8061 }
8062 }
8063 }
8064 if (!w.mAppFreezing) {
8065 w.mContentInsetsChanged =
8066 !w.mLastContentInsets.equals(w.mContentInsets);
8067 w.mVisibleInsetsChanged =
8068 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008069 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008070 || w.mContentInsetsChanged
8071 || w.mVisibleInsetsChanged) {
8072 w.mLastFrame.set(w.mFrame);
8073 w.mLastContentInsets.set(w.mContentInsets);
8074 w.mLastVisibleInsets.set(w.mVisibleInsets);
8075 // If the orientation is changing, then we need to
8076 // hold off on unfreezing the display until this
8077 // window has been redrawn; to do that, we need
8078 // to go through the process of getting informed
8079 // by the application when it has finished drawing.
8080 if (w.mOrientationChanging) {
8081 if (DEBUG_ORIENTATION) Log.v(TAG,
8082 "Orientation start waiting for draw in "
8083 + w + ", surface " + w.mSurface);
8084 w.mDrawPending = true;
8085 w.mCommitDrawPending = false;
8086 w.mReadyToShow = false;
8087 if (w.mAppToken != null) {
8088 w.mAppToken.allDrawn = false;
8089 }
8090 }
Romain Guy06882f82009-06-10 13:36:04 -07008091 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 "Resizing window " + w + " to " + w.mFrame);
8093 mResizingWindows.add(w);
8094 } else if (w.mOrientationChanging) {
8095 if (!w.mDrawPending && !w.mCommitDrawPending) {
8096 if (DEBUG_ORIENTATION) Log.v(TAG,
8097 "Orientation not waiting for draw in "
8098 + w + ", surface " + w.mSurface);
8099 w.mOrientationChanging = false;
8100 }
8101 }
8102 }
8103
8104 if (w.mAttachedHidden) {
8105 if (!w.mLastHidden) {
8106 //dump();
8107 w.mLastHidden = true;
8108 if (SHOW_TRANSACTIONS) Log.i(
8109 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8110 if (w.mSurface != null) {
8111 try {
8112 w.mSurface.hide();
8113 } catch (RuntimeException e) {
8114 Log.w(TAG, "Exception hiding surface in " + w);
8115 }
8116 }
8117 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8118 }
8119 // If we are waiting for this window to handle an
8120 // orientation change, well, it is hidden, so
8121 // doesn't really matter. Note that this does
8122 // introduce a potential glitch if the window
8123 // becomes unhidden before it has drawn for the
8124 // new orientation.
8125 if (w.mOrientationChanging) {
8126 w.mOrientationChanging = false;
8127 if (DEBUG_ORIENTATION) Log.v(TAG,
8128 "Orientation change skips hidden " + w);
8129 }
8130 } else if (!w.isReadyForDisplay()) {
8131 if (!w.mLastHidden) {
8132 //dump();
8133 w.mLastHidden = true;
8134 if (SHOW_TRANSACTIONS) Log.i(
8135 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8136 if (w.mSurface != null) {
8137 try {
8138 w.mSurface.hide();
8139 } catch (RuntimeException e) {
8140 Log.w(TAG, "Exception exception hiding surface in " + w);
8141 }
8142 }
8143 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8144 }
8145 // If we are waiting for this window to handle an
8146 // orientation change, well, it is hidden, so
8147 // doesn't really matter. Note that this does
8148 // introduce a potential glitch if the window
8149 // becomes unhidden before it has drawn for the
8150 // new orientation.
8151 if (w.mOrientationChanging) {
8152 w.mOrientationChanging = false;
8153 if (DEBUG_ORIENTATION) Log.v(TAG,
8154 "Orientation change skips hidden " + w);
8155 }
8156 } else if (w.mLastLayer != w.mAnimLayer
8157 || w.mLastAlpha != w.mShownAlpha
8158 || w.mLastDsDx != w.mDsDx
8159 || w.mLastDtDx != w.mDtDx
8160 || w.mLastDsDy != w.mDsDy
8161 || w.mLastDtDy != w.mDtDy
8162 || w.mLastHScale != w.mHScale
8163 || w.mLastVScale != w.mVScale
8164 || w.mLastHidden) {
8165 displayed = true;
8166 w.mLastAlpha = w.mShownAlpha;
8167 w.mLastLayer = w.mAnimLayer;
8168 w.mLastDsDx = w.mDsDx;
8169 w.mLastDtDx = w.mDtDx;
8170 w.mLastDsDy = w.mDsDy;
8171 w.mLastDtDy = w.mDtDy;
8172 w.mLastHScale = w.mHScale;
8173 w.mLastVScale = w.mVScale;
8174 if (SHOW_TRANSACTIONS) Log.i(
8175 TAG, " SURFACE " + w.mSurface + ": alpha="
8176 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8177 if (w.mSurface != null) {
8178 try {
8179 w.mSurface.setAlpha(w.mShownAlpha);
8180 w.mSurface.setLayer(w.mAnimLayer);
8181 w.mSurface.setMatrix(
8182 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8183 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8184 } catch (RuntimeException e) {
8185 Log.w(TAG, "Error updating surface in " + w, e);
8186 if (!recoveringMemory) {
8187 reclaimSomeSurfaceMemoryLocked(w, "update");
8188 }
8189 }
8190 }
8191
8192 if (w.mLastHidden && !w.mDrawPending
8193 && !w.mCommitDrawPending
8194 && !w.mReadyToShow) {
8195 if (SHOW_TRANSACTIONS) Log.i(
8196 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8197 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8198 + " during relayout");
8199 if (showSurfaceRobustlyLocked(w)) {
8200 w.mHasDrawn = true;
8201 w.mLastHidden = false;
8202 } else {
8203 w.mOrientationChanging = false;
8204 }
8205 }
8206 if (w.mSurface != null) {
8207 w.mToken.hasVisible = true;
8208 }
8209 } else {
8210 displayed = true;
8211 }
8212
8213 if (displayed) {
8214 if (!covered) {
8215 if (attrs.width == LayoutParams.FILL_PARENT
8216 && attrs.height == LayoutParams.FILL_PARENT) {
8217 covered = true;
8218 }
8219 }
8220 if (w.mOrientationChanging) {
8221 if (w.mDrawPending || w.mCommitDrawPending) {
8222 orientationChangeComplete = false;
8223 if (DEBUG_ORIENTATION) Log.v(TAG,
8224 "Orientation continue waiting for draw in " + w);
8225 } else {
8226 w.mOrientationChanging = false;
8227 if (DEBUG_ORIENTATION) Log.v(TAG,
8228 "Orientation change complete in " + w);
8229 }
8230 }
8231 w.mToken.hasVisible = true;
8232 }
8233 } else if (w.mOrientationChanging) {
8234 if (DEBUG_ORIENTATION) Log.v(TAG,
8235 "Orientation change skips hidden " + w);
8236 w.mOrientationChanging = false;
8237 }
8238
8239 final boolean canBeSeen = w.isDisplayedLw();
8240
8241 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8242 focusDisplayed = true;
8243 }
8244
8245 // Update effect.
8246 if (!obscured) {
8247 if (w.mSurface != null) {
8248 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8249 holdScreen = w.mSession;
8250 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008251 if (!syswin && w.mAttrs.screenBrightness >= 0
8252 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 screenBrightness = w.mAttrs.screenBrightness;
8254 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008255 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8256 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8257 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8258 syswin = true;
8259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 }
8261 if (w.isFullscreenOpaque(dw, dh)) {
8262 // This window completely covers everything behind it,
8263 // so we want to leave all of them as unblurred (for
8264 // performance reasons).
8265 obscured = true;
8266 } else if (canBeSeen && !obscured &&
8267 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8268 if (localLOGV) Log.v(TAG, "Win " + w
8269 + ": blurring=" + blurring
8270 + " obscured=" + obscured
8271 + " displayed=" + displayed);
8272 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8273 if (!dimming) {
8274 //Log.i(TAG, "DIM BEHIND: " + w);
8275 dimming = true;
8276 mDimShown = true;
8277 if (mDimSurface == null) {
8278 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8279 + mDimSurface + ": CREATE");
8280 try {
Romain Guy06882f82009-06-10 13:36:04 -07008281 mDimSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 -1, 16, 16,
8283 PixelFormat.OPAQUE,
8284 Surface.FX_SURFACE_DIM);
8285 } catch (Exception e) {
8286 Log.e(TAG, "Exception creating Dim surface", e);
8287 }
8288 }
8289 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8290 + mDimSurface + ": SHOW pos=(0,0) (" +
8291 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8292 if (mDimSurface != null) {
8293 try {
8294 mDimSurface.setPosition(0, 0);
8295 mDimSurface.setSize(dw, dh);
8296 mDimSurface.show();
8297 } catch (RuntimeException e) {
8298 Log.w(TAG, "Failure showing dim surface", e);
8299 }
8300 }
8301 }
8302 mDimSurface.setLayer(w.mAnimLayer-1);
8303 final float target = w.mExiting ? 0 : attrs.dimAmount;
8304 if (mDimTargetAlpha != target) {
8305 // If the desired dim level has changed, then
8306 // start an animation to it.
8307 mLastDimAnimTime = currentTime;
8308 long duration = (w.mAnimating && w.mAnimation != null)
8309 ? w.mAnimation.computeDurationHint()
8310 : DEFAULT_DIM_DURATION;
8311 if (target > mDimTargetAlpha) {
8312 // This is happening behind the activity UI,
8313 // so we can make it run a little longer to
8314 // give a stronger impression without disrupting
8315 // the user.
8316 duration *= DIM_DURATION_MULTIPLIER;
8317 }
8318 if (duration < 1) {
8319 // Don't divide by zero
8320 duration = 1;
8321 }
8322 mDimTargetAlpha = target;
8323 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha)
8324 / duration;
8325 }
8326 }
8327 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8328 if (!blurring) {
8329 //Log.i(TAG, "BLUR BEHIND: " + w);
8330 blurring = true;
8331 mBlurShown = true;
8332 if (mBlurSurface == null) {
8333 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8334 + mBlurSurface + ": CREATE");
8335 try {
Romain Guy06882f82009-06-10 13:36:04 -07008336 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 -1, 16, 16,
8338 PixelFormat.OPAQUE,
8339 Surface.FX_SURFACE_BLUR);
8340 } catch (Exception e) {
8341 Log.e(TAG, "Exception creating Blur surface", e);
8342 }
8343 }
8344 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8345 + mBlurSurface + ": SHOW pos=(0,0) (" +
8346 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8347 if (mBlurSurface != null) {
8348 mBlurSurface.setPosition(0, 0);
8349 mBlurSurface.setSize(dw, dh);
8350 try {
8351 mBlurSurface.show();
8352 } catch (RuntimeException e) {
8353 Log.w(TAG, "Failure showing blur surface", e);
8354 }
8355 }
8356 }
8357 mBlurSurface.setLayer(w.mAnimLayer-2);
8358 }
8359 }
8360 }
8361 }
8362
8363 if (!dimming && mDimShown) {
8364 // Time to hide the dim surface... start fading.
8365 if (mDimTargetAlpha != 0) {
8366 mLastDimAnimTime = currentTime;
8367 mDimTargetAlpha = 0;
8368 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
8369 }
8370 }
8371
8372 if (mDimShown && mLastDimAnimTime != 0) {
8373 mDimCurrentAlpha += mDimDeltaPerMs
8374 * (currentTime-mLastDimAnimTime);
8375 boolean more = true;
8376 if (mDisplayFrozen) {
8377 // If the display is frozen, there is no reason to animate.
8378 more = false;
8379 } else if (mDimDeltaPerMs > 0) {
8380 if (mDimCurrentAlpha > mDimTargetAlpha) {
8381 more = false;
8382 }
8383 } else if (mDimDeltaPerMs < 0) {
8384 if (mDimCurrentAlpha < mDimTargetAlpha) {
8385 more = false;
8386 }
8387 } else {
8388 more = false;
8389 }
Romain Guy06882f82009-06-10 13:36:04 -07008390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 // Do we need to continue animating?
8392 if (more) {
8393 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8394 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
8395 mLastDimAnimTime = currentTime;
8396 mDimSurface.setAlpha(mDimCurrentAlpha);
8397 animating = true;
8398 } else {
8399 mDimCurrentAlpha = mDimTargetAlpha;
8400 mLastDimAnimTime = 0;
8401 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8402 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
8403 mDimSurface.setAlpha(mDimCurrentAlpha);
8404 if (!dimming) {
8405 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
8406 + ": HIDE");
8407 try {
8408 mDimSurface.hide();
8409 } catch (RuntimeException e) {
8410 Log.w(TAG, "Illegal argument exception hiding dim surface");
8411 }
8412 mDimShown = false;
8413 }
8414 }
8415 }
Romain Guy06882f82009-06-10 13:36:04 -07008416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 if (!blurring && mBlurShown) {
8418 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8419 + ": HIDE");
8420 try {
8421 mBlurSurface.hide();
8422 } catch (IllegalArgumentException e) {
8423 Log.w(TAG, "Illegal argument exception hiding blur surface");
8424 }
8425 mBlurShown = false;
8426 }
8427
8428 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8429 } catch (RuntimeException e) {
8430 Log.e(TAG, "Unhandled exception in Window Manager", e);
8431 }
8432
8433 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8436 "With display frozen, orientationChangeComplete="
8437 + orientationChangeComplete);
8438 if (orientationChangeComplete) {
8439 if (mWindowsFreezingScreen) {
8440 mWindowsFreezingScreen = false;
8441 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8442 }
8443 if (mAppsFreezingScreen == 0) {
8444 stopFreezingDisplayLocked();
8445 }
8446 }
Romain Guy06882f82009-06-10 13:36:04 -07008447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 i = mResizingWindows.size();
8449 if (i > 0) {
8450 do {
8451 i--;
8452 WindowState win = mResizingWindows.get(i);
8453 try {
8454 win.mClient.resized(win.mFrame.width(),
8455 win.mFrame.height(), win.mLastContentInsets,
8456 win.mLastVisibleInsets, win.mDrawPending);
8457 win.mContentInsetsChanged = false;
8458 win.mVisibleInsetsChanged = false;
8459 } catch (RemoteException e) {
8460 win.mOrientationChanging = false;
8461 }
8462 } while (i > 0);
8463 mResizingWindows.clear();
8464 }
Romain Guy06882f82009-06-10 13:36:04 -07008465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 // Destroy the surface of any windows that are no longer visible.
8467 i = mDestroySurface.size();
8468 if (i > 0) {
8469 do {
8470 i--;
8471 WindowState win = mDestroySurface.get(i);
8472 win.mDestroying = false;
8473 if (mInputMethodWindow == win) {
8474 mInputMethodWindow = null;
8475 }
8476 win.destroySurfaceLocked();
8477 } while (i > 0);
8478 mDestroySurface.clear();
8479 }
8480
8481 // Time to remove any exiting tokens?
8482 for (i=mExitingTokens.size()-1; i>=0; i--) {
8483 WindowToken token = mExitingTokens.get(i);
8484 if (!token.hasVisible) {
8485 mExitingTokens.remove(i);
8486 }
8487 }
8488
8489 // Time to remove any exiting applications?
8490 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8491 AppWindowToken token = mExitingAppTokens.get(i);
8492 if (!token.hasVisible && !mClosingApps.contains(token)) {
8493 mAppTokens.remove(token);
8494 mExitingAppTokens.remove(i);
8495 }
8496 }
8497
8498 if (focusDisplayed) {
8499 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8500 }
8501 if (animating) {
8502 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8503 }
8504 mQueue.setHoldScreenLocked(holdScreen != null);
8505 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8506 mPowerManager.setScreenBrightnessOverride(-1);
8507 } else {
8508 mPowerManager.setScreenBrightnessOverride((int)
8509 (screenBrightness * Power.BRIGHTNESS_ON));
8510 }
8511 if (holdScreen != mHoldingScreenOn) {
8512 mHoldingScreenOn = holdScreen;
8513 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8514 mH.sendMessage(m);
8515 }
8516 }
8517
8518 void requestAnimationLocked(long delay) {
8519 if (!mAnimationPending) {
8520 mAnimationPending = true;
8521 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8522 }
8523 }
Romain Guy06882f82009-06-10 13:36:04 -07008524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 /**
8526 * Have the surface flinger show a surface, robustly dealing with
8527 * error conditions. In particular, if there is not enough memory
8528 * to show the surface, then we will try to get rid of other surfaces
8529 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008530 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008531 * @return Returns true if the surface was successfully shown.
8532 */
8533 boolean showSurfaceRobustlyLocked(WindowState win) {
8534 try {
8535 if (win.mSurface != null) {
8536 win.mSurface.show();
8537 }
8538 return true;
8539 } catch (RuntimeException e) {
8540 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8541 }
Romain Guy06882f82009-06-10 13:36:04 -07008542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008545 return false;
8546 }
Romain Guy06882f82009-06-10 13:36:04 -07008547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8549 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8552 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 if (mForceRemoves == null) {
8555 mForceRemoves = new ArrayList<WindowState>();
8556 }
Romain Guy06882f82009-06-10 13:36:04 -07008557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 long callingIdentity = Binder.clearCallingIdentity();
8559 try {
8560 // There was some problem... first, do a sanity check of the
8561 // window list to make sure we haven't left any dangling surfaces
8562 // around.
8563 int N = mWindows.size();
8564 boolean leakedSurface = false;
8565 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8566 for (int i=0; i<N; i++) {
8567 WindowState ws = (WindowState)mWindows.get(i);
8568 if (ws.mSurface != null) {
8569 if (!mSessions.contains(ws.mSession)) {
8570 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8571 + ws + " surface=" + ws.mSurface
8572 + " token=" + win.mToken
8573 + " pid=" + ws.mSession.mPid
8574 + " uid=" + ws.mSession.mUid);
8575 ws.mSurface.clear();
8576 ws.mSurface = null;
8577 mForceRemoves.add(ws);
8578 i--;
8579 N--;
8580 leakedSurface = true;
8581 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8582 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8583 + ws + " surface=" + ws.mSurface
8584 + " token=" + win.mAppToken);
8585 ws.mSurface.clear();
8586 ws.mSurface = null;
8587 leakedSurface = true;
8588 }
8589 }
8590 }
Romain Guy06882f82009-06-10 13:36:04 -07008591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 boolean killedApps = false;
8593 if (!leakedSurface) {
8594 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8595 SparseIntArray pidCandidates = new SparseIntArray();
8596 for (int i=0; i<N; i++) {
8597 WindowState ws = (WindowState)mWindows.get(i);
8598 if (ws.mSurface != null) {
8599 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8600 }
8601 }
8602 if (pidCandidates.size() > 0) {
8603 int[] pids = new int[pidCandidates.size()];
8604 for (int i=0; i<pids.length; i++) {
8605 pids[i] = pidCandidates.keyAt(i);
8606 }
8607 try {
8608 if (mActivityManager.killPidsForMemory(pids)) {
8609 killedApps = true;
8610 }
8611 } catch (RemoteException e) {
8612 }
8613 }
8614 }
Romain Guy06882f82009-06-10 13:36:04 -07008615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 if (leakedSurface || killedApps) {
8617 // We managed to reclaim some memory, so get rid of the trouble
8618 // surface and ask the app to request another one.
8619 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8620 if (surface != null) {
8621 surface.clear();
8622 win.mSurface = null;
8623 }
Romain Guy06882f82009-06-10 13:36:04 -07008624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 try {
8626 win.mClient.dispatchGetNewSurface();
8627 } catch (RemoteException e) {
8628 }
8629 }
8630 } finally {
8631 Binder.restoreCallingIdentity(callingIdentity);
8632 }
8633 }
Romain Guy06882f82009-06-10 13:36:04 -07008634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 private boolean updateFocusedWindowLocked(int mode) {
8636 WindowState newFocus = computeFocusedWindowLocked();
8637 if (mCurrentFocus != newFocus) {
8638 // This check makes sure that we don't already have the focus
8639 // change message pending.
8640 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8641 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8642 if (localLOGV) Log.v(
8643 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8644 final WindowState oldFocus = mCurrentFocus;
8645 mCurrentFocus = newFocus;
8646 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 final WindowState imWindow = mInputMethodWindow;
8649 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008650 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008651 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008652 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8653 mLayoutNeeded = true;
8654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8656 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008657 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8658 // Client will do the layout, but we need to assign layers
8659 // for handleNewWindowLocked() below.
8660 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 }
8662 }
Romain Guy06882f82009-06-10 13:36:04 -07008663
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008664 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8665 mKeyWaiter.handleNewWindowLocked(newFocus);
8666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667 return true;
8668 }
8669 return false;
8670 }
8671
8672 private WindowState computeFocusedWindowLocked() {
8673 WindowState result = null;
8674 WindowState win;
8675
8676 int i = mWindows.size() - 1;
8677 int nextAppIndex = mAppTokens.size()-1;
8678 WindowToken nextApp = nextAppIndex >= 0
8679 ? mAppTokens.get(nextAppIndex) : null;
8680
8681 while (i >= 0) {
8682 win = (WindowState)mWindows.get(i);
8683
8684 if (localLOGV || DEBUG_FOCUS) Log.v(
8685 TAG, "Looking for focus: " + i
8686 + " = " + win
8687 + ", flags=" + win.mAttrs.flags
8688 + ", canReceive=" + win.canReceiveKeys());
8689
8690 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 // If this window's application has been removed, just skip it.
8693 if (thisApp != null && thisApp.removed) {
8694 i--;
8695 continue;
8696 }
Romain Guy06882f82009-06-10 13:36:04 -07008697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 // If there is a focused app, don't allow focus to go to any
8699 // windows below it. If this is an application window, step
8700 // through the app tokens until we find its app.
8701 if (thisApp != null && nextApp != null && thisApp != nextApp
8702 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8703 int origAppIndex = nextAppIndex;
8704 while (nextAppIndex > 0) {
8705 if (nextApp == mFocusedApp) {
8706 // Whoops, we are below the focused app... no focus
8707 // for you!
8708 if (localLOGV || DEBUG_FOCUS) Log.v(
8709 TAG, "Reached focused app: " + mFocusedApp);
8710 return null;
8711 }
8712 nextAppIndex--;
8713 nextApp = mAppTokens.get(nextAppIndex);
8714 if (nextApp == thisApp) {
8715 break;
8716 }
8717 }
8718 if (thisApp != nextApp) {
8719 // Uh oh, the app token doesn't exist! This shouldn't
8720 // happen, but if it does we can get totally hosed...
8721 // so restart at the original app.
8722 nextAppIndex = origAppIndex;
8723 nextApp = mAppTokens.get(nextAppIndex);
8724 }
8725 }
8726
8727 // Dispatch to this window if it is wants key events.
8728 if (win.canReceiveKeys()) {
8729 if (DEBUG_FOCUS) Log.v(
8730 TAG, "Found focus @ " + i + " = " + win);
8731 result = win;
8732 break;
8733 }
8734
8735 i--;
8736 }
8737
8738 return result;
8739 }
8740
8741 private void startFreezingDisplayLocked() {
8742 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008743 // Freezing the display also suspends key event delivery, to
8744 // keep events from going astray while the display is reconfigured.
8745 // If someone has changed orientation again while the screen is
8746 // still frozen, the events will continue to be blocked while the
8747 // successive orientation change is processed. To prevent spurious
8748 // ANRs, we reset the event dispatch timeout in this case.
8749 synchronized (mKeyWaiter) {
8750 mKeyWaiter.mWasFrozen = true;
8751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 return;
8753 }
Romain Guy06882f82009-06-10 13:36:04 -07008754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 long now = SystemClock.uptimeMillis();
8758 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8759 if (mFreezeGcPending != 0) {
8760 if (now > (mFreezeGcPending+1000)) {
8761 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8762 mH.removeMessages(H.FORCE_GC);
8763 Runtime.getRuntime().gc();
8764 mFreezeGcPending = now;
8765 }
8766 } else {
8767 mFreezeGcPending = now;
8768 }
Romain Guy06882f82009-06-10 13:36:04 -07008769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 mDisplayFrozen = true;
8771 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8772 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8773 mAppTransitionReady = true;
8774 }
Romain Guy06882f82009-06-10 13:36:04 -07008775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008776 if (PROFILE_ORIENTATION) {
8777 File file = new File("/data/system/frozen");
8778 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8779 }
8780 Surface.freezeDisplay(0);
8781 }
Romain Guy06882f82009-06-10 13:36:04 -07008782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 private void stopFreezingDisplayLocked() {
8784 if (!mDisplayFrozen) {
8785 return;
8786 }
Romain Guy06882f82009-06-10 13:36:04 -07008787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008788 mDisplayFrozen = false;
8789 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8790 if (PROFILE_ORIENTATION) {
8791 Debug.stopMethodTracing();
8792 }
8793 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008794
Chris Tate2ad63a92009-03-25 17:36:48 -07008795 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8796 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 synchronized (mKeyWaiter) {
8798 mKeyWaiter.mWasFrozen = true;
8799 mKeyWaiter.notifyAll();
8800 }
8801
8802 // A little kludge: a lot could have happened while the
8803 // display was frozen, so now that we are coming back we
8804 // do a gc so that any remote references the system
8805 // processes holds on others can be released if they are
8806 // no longer needed.
8807 mH.removeMessages(H.FORCE_GC);
8808 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8809 2000);
Romain Guy06882f82009-06-10 13:36:04 -07008810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 mScreenFrozenLock.release();
8812 }
Romain Guy06882f82009-06-10 13:36:04 -07008813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 @Override
8815 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
8816 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
8817 != PackageManager.PERMISSION_GRANTED) {
8818 pw.println("Permission Denial: can't dump WindowManager from from pid="
8819 + Binder.getCallingPid()
8820 + ", uid=" + Binder.getCallingUid());
8821 return;
8822 }
Romain Guy06882f82009-06-10 13:36:04 -07008823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008824 synchronized(mWindowMap) {
8825 pw.println("Current Window Manager state:");
8826 for (int i=mWindows.size()-1; i>=0; i--) {
8827 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008828 pw.print(" Window #"); pw.print(i); pw.print(' ');
8829 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008830 w.dump(pw, " ");
8831 }
8832 if (mInputMethodDialogs.size() > 0) {
8833 pw.println(" ");
8834 pw.println(" Input method dialogs:");
8835 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
8836 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008837 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838 }
8839 }
8840 if (mPendingRemove.size() > 0) {
8841 pw.println(" ");
8842 pw.println(" Remove pending for:");
8843 for (int i=mPendingRemove.size()-1; i>=0; i--) {
8844 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008845 pw.print(" Remove #"); pw.print(i); pw.print(' ');
8846 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008847 w.dump(pw, " ");
8848 }
8849 }
8850 if (mForceRemoves != null && mForceRemoves.size() > 0) {
8851 pw.println(" ");
8852 pw.println(" Windows force removing:");
8853 for (int i=mForceRemoves.size()-1; i>=0; i--) {
8854 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008855 pw.print(" Removing #"); pw.print(i); pw.print(' ');
8856 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 w.dump(pw, " ");
8858 }
8859 }
8860 if (mDestroySurface.size() > 0) {
8861 pw.println(" ");
8862 pw.println(" Windows waiting to destroy their surface:");
8863 for (int i=mDestroySurface.size()-1; i>=0; i--) {
8864 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008865 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
8866 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008867 w.dump(pw, " ");
8868 }
8869 }
8870 if (mLosingFocus.size() > 0) {
8871 pw.println(" ");
8872 pw.println(" Windows losing focus:");
8873 for (int i=mLosingFocus.size()-1; i>=0; i--) {
8874 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008875 pw.print(" Losing #"); pw.print(i); pw.print(' ');
8876 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008877 w.dump(pw, " ");
8878 }
8879 }
8880 if (mSessions.size() > 0) {
8881 pw.println(" ");
8882 pw.println(" All active sessions:");
8883 Iterator<Session> it = mSessions.iterator();
8884 while (it.hasNext()) {
8885 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008886 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008887 s.dump(pw, " ");
8888 }
8889 }
8890 if (mTokenMap.size() > 0) {
8891 pw.println(" ");
8892 pw.println(" All tokens:");
8893 Iterator<WindowToken> it = mTokenMap.values().iterator();
8894 while (it.hasNext()) {
8895 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008896 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008897 token.dump(pw, " ");
8898 }
8899 }
8900 if (mTokenList.size() > 0) {
8901 pw.println(" ");
8902 pw.println(" Window token list:");
8903 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008904 pw.print(" #"); pw.print(i); pw.print(": ");
8905 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 }
8907 }
8908 if (mAppTokens.size() > 0) {
8909 pw.println(" ");
8910 pw.println(" Application tokens in Z order:");
8911 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008912 pw.print(" App #"); pw.print(i); pw.print(": ");
8913 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008914 }
8915 }
8916 if (mFinishedStarting.size() > 0) {
8917 pw.println(" ");
8918 pw.println(" Finishing start of application tokens:");
8919 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
8920 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008921 pw.print(" Finished Starting #"); pw.print(i);
8922 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 token.dump(pw, " ");
8924 }
8925 }
8926 if (mExitingTokens.size() > 0) {
8927 pw.println(" ");
8928 pw.println(" Exiting tokens:");
8929 for (int i=mExitingTokens.size()-1; i>=0; i--) {
8930 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008931 pw.print(" Exiting #"); pw.print(i);
8932 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 token.dump(pw, " ");
8934 }
8935 }
8936 if (mExitingAppTokens.size() > 0) {
8937 pw.println(" ");
8938 pw.println(" Exiting application tokens:");
8939 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
8940 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008941 pw.print(" Exiting App #"); pw.print(i);
8942 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 token.dump(pw, " ");
8944 }
8945 }
8946 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008947 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
8948 pw.print(" mLastFocus="); pw.println(mLastFocus);
8949 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
8950 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
8951 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
8952 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
8953 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
8954 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
8955 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
8956 pw.print(" mBlurShown="); pw.println(mBlurShown);
8957 pw.print(" mDimShown="); pw.print(mDimShown);
8958 pw.print(" current="); pw.print(mDimCurrentAlpha);
8959 pw.print(" target="); pw.print(mDimTargetAlpha);
8960 pw.print(" delta="); pw.print(mDimDeltaPerMs);
8961 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
8962 pw.print(" mInputMethodAnimLayerAdjustment=");
8963 pw.println(mInputMethodAnimLayerAdjustment);
8964 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
8965 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
8966 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
8967 pw.print(" mRotation="); pw.print(mRotation);
8968 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
8969 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
8970 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
8971 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
8972 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
8973 pw.print(" mNextAppTransition=0x");
8974 pw.print(Integer.toHexString(mNextAppTransition));
8975 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
8976 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
8977 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
8978 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
8979 if (mOpeningApps.size() > 0) {
8980 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
8981 }
8982 if (mClosingApps.size() > 0) {
8983 pw.print(" mClosingApps="); pw.println(mClosingApps);
8984 }
8985 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
8986 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008987 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008988 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
8989 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
8990 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
8991 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
8992 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
8993 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 }
8995 }
8996
8997 public void monitor() {
8998 synchronized (mWindowMap) { }
8999 synchronized (mKeyguardDisabled) { }
9000 synchronized (mKeyWaiter) { }
9001 }
9002}