blob: bbf2a24a74da09ccf4eeaadc5beaf88452a8d20a [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;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
66import android.os.Debug;
67import android.os.Handler;
68import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070069import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.LocalPowerManager;
71import android.os.Looper;
72import android.os.Message;
73import android.os.Parcel;
74import android.os.ParcelFileDescriptor;
75import android.os.Power;
76import android.os.PowerManager;
77import android.os.Process;
78import android.os.RemoteException;
79import android.os.ServiceManager;
80import android.os.SystemClock;
81import android.os.SystemProperties;
82import android.os.TokenWatcher;
83import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070084import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.EventLog;
86import android.util.Log;
87import android.util.SparseIntArray;
88import android.view.Display;
89import android.view.Gravity;
90import android.view.IApplicationToken;
91import android.view.IOnKeyguardExitResult;
92import android.view.IRotationWatcher;
93import android.view.IWindow;
94import android.view.IWindowManager;
95import android.view.IWindowSession;
96import android.view.KeyEvent;
97import android.view.MotionEvent;
98import android.view.RawInputEvent;
99import android.view.Surface;
100import android.view.SurfaceSession;
101import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700102import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.ViewTreeObserver;
104import android.view.WindowManager;
105import android.view.WindowManagerImpl;
106import android.view.WindowManagerPolicy;
107import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700108import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.animation.Animation;
110import android.view.animation.AnimationUtils;
111import android.view.animation.Transformation;
112
113import java.io.BufferedWriter;
114import java.io.File;
115import java.io.FileDescriptor;
116import java.io.IOException;
117import java.io.OutputStream;
118import java.io.OutputStreamWriter;
119import java.io.PrintWriter;
120import java.io.StringWriter;
121import java.net.Socket;
122import java.util.ArrayList;
123import java.util.HashMap;
124import java.util.HashSet;
125import java.util.Iterator;
126import java.util.List;
127
128/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700129public class WindowManagerService extends IWindowManager.Stub
130 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 static final String TAG = "WindowManager";
132 static final boolean DEBUG = false;
133 static final boolean DEBUG_FOCUS = false;
134 static final boolean DEBUG_ANIM = false;
135 static final boolean DEBUG_LAYERS = false;
136 static final boolean DEBUG_INPUT = false;
137 static final boolean DEBUG_INPUT_METHOD = false;
138 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700139 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 static final boolean DEBUG_ORIENTATION = false;
141 static final boolean DEBUG_APP_TRANSITIONS = false;
142 static final boolean DEBUG_STARTING_WINDOW = false;
143 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700144 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700146 static final boolean MEASURE_LATENCY = false;
147 static private LatencyTimer lt;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 static final boolean PROFILE_ORIENTATION = false;
150 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700151 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /** How long to wait for subsequent key repeats, in milliseconds */
156 static final int KEY_REPEAT_DELAY = 50;
157
158 /** How much to multiply the policy's type layer, to reserve room
159 * for multiple windows of the same type and Z-ordering adjustment
160 * with TYPE_LAYER_OFFSET. */
161 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
164 * or below others in the same layer. */
165 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 /** How much to increment the layer for each window, to reserve room
168 * for effect surfaces between them.
169 */
170 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 /** The maximum length we will accept for a loaded animation duration:
173 * this is 10 seconds.
174 */
175 static final int MAX_ANIMATION_DURATION = 10*1000;
176
177 /** Amount of time (in milliseconds) to animate the dim surface from one
178 * value to another, when no window animation is driving it.
179 */
180 static final int DEFAULT_DIM_DURATION = 200;
181
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700182 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
183 * compatible windows.
184 */
185 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 /** Adjustment to time to perform a dim, to make it more dramatic.
188 */
189 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700190
Dianne Hackborncfaef692009-06-15 14:24:44 -0700191 static final int INJECT_FAILED = 0;
192 static final int INJECT_SUCCEEDED = 1;
193 static final int INJECT_NO_PERMISSION = -1;
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 static final int UPDATE_FOCUS_NORMAL = 0;
196 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
197 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
198 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700199
Michael Chane96440f2009-05-06 10:27:36 -0700200 /** The minimum time between dispatching touch events. */
201 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
202
203 // Last touch event time
204 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700205
Michael Chane96440f2009-05-06 10:27:36 -0700206 // Last touch event type
207 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700208
Michael Chane96440f2009-05-06 10:27:36 -0700209 // Time to wait before calling useractivity again. This saves CPU usage
210 // when we get a flood of touch events.
211 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
212
213 // Last time we call user activity
214 long mLastUserActivityCallTime = 0;
215
Romain Guy06882f82009-06-10 13:36:04 -0700216 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700217 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700220 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 /**
223 * Condition waited on by {@link #reenableKeyguard} to know the call to
224 * the window policy has finished.
225 */
226 private boolean mWaitingUntilKeyguardReenabled = false;
227
228
229 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
230 new Handler(), "WindowManagerService.mKeyguardDisabled") {
231 public void acquired() {
232 mPolicy.enableKeyguard(false);
233 }
234 public void released() {
235 synchronized (mKeyguardDisabled) {
236 mPolicy.enableKeyguard(true);
237 mWaitingUntilKeyguardReenabled = false;
238 mKeyguardDisabled.notifyAll();
239 }
240 }
241 };
242
243 final Context mContext;
244
245 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
250
251 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * All currently active sessions with clients.
257 */
258 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Mapping from an IWindow IBinder to the server's Window object.
262 * This is also used as the lock for all of our state.
263 */
264 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
265
266 /**
267 * Mapping from a token IBinder to a WindowToken object.
268 */
269 final HashMap<IBinder, WindowToken> mTokenMap =
270 new HashMap<IBinder, WindowToken>();
271
272 /**
273 * The same tokens as mTokenMap, stored in a list for efficient iteration
274 * over them.
275 */
276 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Window tokens that are in the process of exiting, but still
280 * on screen for animations.
281 */
282 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
283
284 /**
285 * Z-ordered (bottom-most first) list of all application tokens, for
286 * controlling the ordering of windows in different applications. This
287 * contains WindowToken objects.
288 */
289 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
290
291 /**
292 * Application tokens that are in the process of exiting, but still
293 * on screen for animations.
294 */
295 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
296
297 /**
298 * List of window tokens that have finished starting their application,
299 * and now need to have the policy remove their windows.
300 */
301 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
302
303 /**
304 * Z-ordered (bottom-most first) list of all Window objects.
305 */
306 final ArrayList mWindows = new ArrayList();
307
308 /**
309 * Windows that are being resized. Used so we can tell the client about
310 * the resize after closing the transaction in which we resized the
311 * underlying surface.
312 */
313 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
314
315 /**
316 * Windows whose animations have ended and now must be removed.
317 */
318 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
319
320 /**
321 * Windows whose surface should be destroyed.
322 */
323 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
324
325 /**
326 * Windows that have lost input focus and are waiting for the new
327 * focus window to be displayed before they are told about this.
328 */
329 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
330
331 /**
332 * This is set when we have run out of memory, and will either be an empty
333 * list or contain windows that need to be force removed.
334 */
335 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700340 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 Surface mBlurSurface;
342 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 final float[] mTmpFloats = new float[9];
347
348 boolean mSafeMode;
349 boolean mDisplayEnabled = false;
350 boolean mSystemBooted = false;
351 int mRotation = 0;
352 int mRequestedRotation = 0;
353 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700354 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 ArrayList<IRotationWatcher> mRotationWatchers
356 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 boolean mLayoutNeeded = true;
359 boolean mAnimationPending = false;
360 boolean mDisplayFrozen = false;
361 boolean mWindowsFreezingScreen = false;
362 long mFreezeGcPending = 0;
363 int mAppsFreezingScreen = 0;
364
365 // This is held as long as we have the screen frozen, to give us time to
366 // perform a rotation animation when turning off shows the lock screen which
367 // changes the orientation.
368 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 // State management of app transitions. When we are preparing for a
371 // transition, mNextAppTransition will be the kind of transition to
372 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
373 // mOpeningApps and mClosingApps are the lists of tokens that will be
374 // made visible or hidden at the next transition.
375 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
376 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700377 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 boolean mAppTransitionTimeout = false;
379 boolean mStartingIconInTransition = false;
380 boolean mSkipAppTransitionAnimation = false;
381 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
382 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700383 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
384 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 //flag to detect fat touch events
387 boolean mFatTouch = false;
388 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 H mH = new H();
391
392 WindowState mCurrentFocus = null;
393 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 // This just indicates the window the input method is on top of, not
396 // necessarily the window its input is going to.
397 WindowState mInputMethodTarget = null;
398 WindowState mUpcomingInputMethodTarget = null;
399 boolean mInputMethodTargetWaitingAnim;
400 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 WindowState mInputMethodWindow = null;
403 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
404
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700405 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
406
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700407 // If non-null, this is the currently visible window that is associated
408 // with the wallpaper.
409 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700410 // If non-null, we are in the middle of animating from one wallpaper target
411 // to another, and this is the lower one in Z-order.
412 WindowState mLowerWallpaperTarget = null;
413 // If non-null, we are in the middle of animating from one wallpaper target
414 // to another, and this is the higher one in Z-order.
415 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700416 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700417 float mLastWallpaperX;
418 float mLastWallpaperY;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700419 // Lock for waiting for the wallpaper.
420 final Object mWaitingOnWallpaperLock = new Object();
421 // This is set when we are waiting for a wallpaper to tell us it is done
422 // changing its scroll position.
423 WindowState mWaitingOnWallpaper;
424 // The last time we had a timeout when waiting for a wallpaper.
425 long mLastWallpaperTimeoutTime;
426 // We give a wallpaper up to 150ms to finish scrolling.
427 static final long WALLPAPER_TIMEOUT = 150;
428 // Time we wait after a timeout before trying to wait again.
429 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 AppWindowToken mFocusedApp = null;
432
433 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 float mWindowAnimationScale = 1.0f;
436 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 final KeyWaiter mKeyWaiter = new KeyWaiter();
439 final KeyQ mQueue;
440 final InputDispatcherThread mInputThread;
441
442 // Who is holding the screen on.
443 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700444
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700445 boolean mTurnOnScreen;
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
448 * Whether the UI is currently running in touch mode (not showing
449 * navigational focus because the user is directly pressing the screen).
450 */
451 boolean mInTouchMode = false;
452
453 private ViewServer mViewServer;
454
455 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700456
Dianne Hackbornc485a602009-03-24 22:39:49 -0700457 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700458 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700459
460 // The frame use to limit the size of the app running in compatibility mode.
461 Rect mCompatibleScreenFrame = new Rect();
462 // The surface used to fill the outer rim of the app running in compatibility mode.
463 Surface mBackgroundFillerSurface = null;
464 boolean mBackgroundFillerShown = false;
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public static WindowManagerService main(Context context,
467 PowerManagerService pm, boolean haveInputMethods) {
468 WMThread thr = new WMThread(context, pm, haveInputMethods);
469 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 synchronized (thr) {
472 while (thr.mService == null) {
473 try {
474 thr.wait();
475 } catch (InterruptedException e) {
476 }
477 }
478 }
Romain Guy06882f82009-06-10 13:36:04 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 return thr.mService;
481 }
Romain Guy06882f82009-06-10 13:36:04 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 static class WMThread extends Thread {
484 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 private final Context mContext;
487 private final PowerManagerService mPM;
488 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public WMThread(Context context, PowerManagerService pm,
491 boolean haveInputMethods) {
492 super("WindowManager");
493 mContext = context;
494 mPM = pm;
495 mHaveInputMethods = haveInputMethods;
496 }
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 public void run() {
499 Looper.prepare();
500 WindowManagerService s = new WindowManagerService(mContext, mPM,
501 mHaveInputMethods);
502 android.os.Process.setThreadPriority(
503 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 synchronized (this) {
506 mService = s;
507 notifyAll();
508 }
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 Looper.loop();
511 }
512 }
513
514 static class PolicyThread extends Thread {
515 private final WindowManagerPolicy mPolicy;
516 private final WindowManagerService mService;
517 private final Context mContext;
518 private final PowerManagerService mPM;
519 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public PolicyThread(WindowManagerPolicy policy,
522 WindowManagerService service, Context context,
523 PowerManagerService pm) {
524 super("WindowManagerPolicy");
525 mPolicy = policy;
526 mService = service;
527 mContext = context;
528 mPM = pm;
529 }
Romain Guy06882f82009-06-10 13:36:04 -0700530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 public void run() {
532 Looper.prepare();
533 //Looper.myLooper().setMessageLogging(new LogPrinter(
534 // Log.VERBOSE, "WindowManagerPolicy"));
535 android.os.Process.setThreadPriority(
536 android.os.Process.THREAD_PRIORITY_FOREGROUND);
537 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 synchronized (this) {
540 mRunning = true;
541 notifyAll();
542 }
Romain Guy06882f82009-06-10 13:36:04 -0700543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 Looper.loop();
545 }
546 }
547
548 private WindowManagerService(Context context, PowerManagerService pm,
549 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700550 if (MEASURE_LATENCY) {
551 lt = new LatencyTimer(100, 1000);
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 mContext = context;
555 mHaveInputMethods = haveInputMethods;
556 mLimitedAlphaCompositing = context.getResources().getBoolean(
557 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 mPowerManager = pm;
560 mPowerManager.setPolicy(mPolicy);
561 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
562 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
563 "SCREEN_FROZEN");
564 mScreenFrozenLock.setReferenceCounted(false);
565
566 mActivityManager = ActivityManagerNative.getDefault();
567 mBatteryStats = BatteryStatsService.getService();
568
569 // Get persisted window scale setting
570 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
571 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
572 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
573 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700574
Michael Chan9f028e62009-08-04 17:37:46 -0700575 int max_events_per_sec = 35;
576 try {
577 max_events_per_sec = Integer.parseInt(SystemProperties
578 .get("windowsmgr.max_events_per_sec"));
579 if (max_events_per_sec < 1) {
580 max_events_per_sec = 35;
581 }
582 } catch (NumberFormatException e) {
583 }
584 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 mQueue = new KeyQ();
587
588 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
591 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 synchronized (thr) {
594 while (!thr.mRunning) {
595 try {
596 thr.wait();
597 } catch (InterruptedException e) {
598 }
599 }
600 }
Romain Guy06882f82009-06-10 13:36:04 -0700601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 // Add ourself to the Watchdog monitors.
605 Watchdog.getInstance().addMonitor(this);
606 }
607
608 @Override
609 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
610 throws RemoteException {
611 try {
612 return super.onTransact(code, data, reply, flags);
613 } catch (RuntimeException e) {
614 // The window manager only throws security exceptions, so let's
615 // log all others.
616 if (!(e instanceof SecurityException)) {
617 Log.e(TAG, "Window Manager Crash", e);
618 }
619 throw e;
620 }
621 }
622
623 private void placeWindowAfter(Object pos, WindowState window) {
624 final int i = mWindows.indexOf(pos);
625 if (localLOGV || DEBUG_FOCUS) Log.v(
626 TAG, "Adding window " + window + " at "
627 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
628 mWindows.add(i+1, window);
629 }
630
631 private void placeWindowBefore(Object pos, WindowState window) {
632 final int i = mWindows.indexOf(pos);
633 if (localLOGV || DEBUG_FOCUS) Log.v(
634 TAG, "Adding window " + window + " at "
635 + i + " of " + mWindows.size() + " (before " + pos + ")");
636 mWindows.add(i, window);
637 }
638
639 //This method finds out the index of a window that has the same app token as
640 //win. used for z ordering the windows in mWindows
641 private int findIdxBasedOnAppTokens(WindowState win) {
642 //use a local variable to cache mWindows
643 ArrayList localmWindows = mWindows;
644 int jmax = localmWindows.size();
645 if(jmax == 0) {
646 return -1;
647 }
648 for(int j = (jmax-1); j >= 0; j--) {
649 WindowState wentry = (WindowState)localmWindows.get(j);
650 if(wentry.mAppToken == win.mAppToken) {
651 return j;
652 }
653 }
654 return -1;
655 }
Romain Guy06882f82009-06-10 13:36:04 -0700656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
658 final IWindow client = win.mClient;
659 final WindowToken token = win.mToken;
660 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 final int N = localmWindows.size();
663 final WindowState attached = win.mAttachedWindow;
664 int i;
665 if (attached == null) {
666 int tokenWindowsPos = token.windows.size();
667 if (token.appWindowToken != null) {
668 int index = tokenWindowsPos-1;
669 if (index >= 0) {
670 // If this application has existing windows, we
671 // simply place the new window on top of them... but
672 // keep the starting window on top.
673 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
674 // Base windows go behind everything else.
675 placeWindowBefore(token.windows.get(0), win);
676 tokenWindowsPos = 0;
677 } else {
678 AppWindowToken atoken = win.mAppToken;
679 if (atoken != null &&
680 token.windows.get(index) == atoken.startingWindow) {
681 placeWindowBefore(token.windows.get(index), win);
682 tokenWindowsPos--;
683 } else {
684 int newIdx = findIdxBasedOnAppTokens(win);
685 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700686 //there is a window above this one associated with the same
687 //apptoken note that the window could be a floating window
688 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 //windows associated with this token.
690 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 }
694 } else {
695 if (localLOGV) Log.v(
696 TAG, "Figuring out where to add app window "
697 + client.asBinder() + " (token=" + token + ")");
698 // Figure out where the window should go, based on the
699 // order of applications.
700 final int NA = mAppTokens.size();
701 Object pos = null;
702 for (i=NA-1; i>=0; i--) {
703 AppWindowToken t = mAppTokens.get(i);
704 if (t == token) {
705 i--;
706 break;
707 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700708
709 // We haven't reached the token yet; if this token
710 // is not going to the bottom and has windows, we can
711 // use it as an anchor for when we do reach the token.
712 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 pos = t.windows.get(0);
714 }
715 }
716 // We now know the index into the apps. If we found
717 // an app window above, that gives us the position; else
718 // we need to look some more.
719 if (pos != null) {
720 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700721 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 mTokenMap.get(((WindowState)pos).mClient.asBinder());
723 if (atoken != null) {
724 final int NC = atoken.windows.size();
725 if (NC > 0) {
726 WindowState bottom = atoken.windows.get(0);
727 if (bottom.mSubLayer < 0) {
728 pos = bottom;
729 }
730 }
731 }
732 placeWindowBefore(pos, win);
733 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700734 // Continue looking down until we find the first
735 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 while (i >= 0) {
737 AppWindowToken t = mAppTokens.get(i);
738 final int NW = t.windows.size();
739 if (NW > 0) {
740 pos = t.windows.get(NW-1);
741 break;
742 }
743 i--;
744 }
745 if (pos != null) {
746 // Move in front of any windows attached to this
747 // one.
748 WindowToken atoken =
749 mTokenMap.get(((WindowState)pos).mClient.asBinder());
750 if (atoken != null) {
751 final int NC = atoken.windows.size();
752 if (NC > 0) {
753 WindowState top = atoken.windows.get(NC-1);
754 if (top.mSubLayer >= 0) {
755 pos = top;
756 }
757 }
758 }
759 placeWindowAfter(pos, win);
760 } else {
761 // Just search for the start of this layer.
762 final int myLayer = win.mBaseLayer;
763 for (i=0; i<N; i++) {
764 WindowState w = (WindowState)localmWindows.get(i);
765 if (w.mBaseLayer > myLayer) {
766 break;
767 }
768 }
769 if (localLOGV || DEBUG_FOCUS) Log.v(
770 TAG, "Adding window " + win + " at "
771 + i + " of " + N);
772 localmWindows.add(i, win);
773 }
774 }
775 }
776 } else {
777 // Figure out where window should go, based on layer.
778 final int myLayer = win.mBaseLayer;
779 for (i=N-1; i>=0; i--) {
780 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
781 i++;
782 break;
783 }
784 }
785 if (i < 0) i = 0;
786 if (localLOGV || DEBUG_FOCUS) Log.v(
787 TAG, "Adding window " + win + " at "
788 + i + " of " + N);
789 localmWindows.add(i, win);
790 }
791 if (addToToken) {
792 token.windows.add(tokenWindowsPos, win);
793 }
794
795 } else {
796 // Figure out this window's ordering relative to the window
797 // it is attached to.
798 final int NA = token.windows.size();
799 final int sublayer = win.mSubLayer;
800 int largestSublayer = Integer.MIN_VALUE;
801 WindowState windowWithLargestSublayer = null;
802 for (i=0; i<NA; i++) {
803 WindowState w = token.windows.get(i);
804 final int wSublayer = w.mSubLayer;
805 if (wSublayer >= largestSublayer) {
806 largestSublayer = wSublayer;
807 windowWithLargestSublayer = w;
808 }
809 if (sublayer < 0) {
810 // For negative sublayers, we go below all windows
811 // in the same sublayer.
812 if (wSublayer >= sublayer) {
813 if (addToToken) {
814 token.windows.add(i, win);
815 }
816 placeWindowBefore(
817 wSublayer >= 0 ? attached : w, win);
818 break;
819 }
820 } else {
821 // For positive sublayers, we go above all windows
822 // in the same sublayer.
823 if (wSublayer > sublayer) {
824 if (addToToken) {
825 token.windows.add(i, win);
826 }
827 placeWindowBefore(w, win);
828 break;
829 }
830 }
831 }
832 if (i >= NA) {
833 if (addToToken) {
834 token.windows.add(win);
835 }
836 if (sublayer < 0) {
837 placeWindowBefore(attached, win);
838 } else {
839 placeWindowAfter(largestSublayer >= 0
840 ? windowWithLargestSublayer
841 : attached,
842 win);
843 }
844 }
845 }
Romain Guy06882f82009-06-10 13:36:04 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (win.mAppToken != null && addToToken) {
848 win.mAppToken.allAppWindows.add(win);
849 }
850 }
Romain Guy06882f82009-06-10 13:36:04 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 static boolean canBeImeTarget(WindowState w) {
853 final int fl = w.mAttrs.flags
854 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
855 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
856 return w.isVisibleOrAdding();
857 }
858 return false;
859 }
Romain Guy06882f82009-06-10 13:36:04 -0700860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
862 final ArrayList localmWindows = mWindows;
863 final int N = localmWindows.size();
864 WindowState w = null;
865 int i = N;
866 while (i > 0) {
867 i--;
868 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
871 // + Integer.toHexString(w.mAttrs.flags));
872 if (canBeImeTarget(w)) {
873 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 // Yet more tricksyness! If this window is a "starting"
876 // window, we do actually want to be on top of it, but
877 // it is not -really- where input will go. So if the caller
878 // is not actually looking to move the IME, look down below
879 // for a real window to target...
880 if (!willMove
881 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
882 && i > 0) {
883 WindowState wb = (WindowState)localmWindows.get(i-1);
884 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
885 i--;
886 w = wb;
887 }
888 }
889 break;
890 }
891 }
Romain Guy06882f82009-06-10 13:36:04 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
896 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 if (willMove && w != null) {
899 final WindowState curTarget = mInputMethodTarget;
900 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Now some fun for dealing with window animations that
903 // modify the Z order. We need to look at all windows below
904 // the current target that are in this app, finding the highest
905 // visible one in layering.
906 AppWindowToken token = curTarget.mAppToken;
907 WindowState highestTarget = null;
908 int highestPos = 0;
909 if (token.animating || token.animation != null) {
910 int pos = 0;
911 pos = localmWindows.indexOf(curTarget);
912 while (pos >= 0) {
913 WindowState win = (WindowState)localmWindows.get(pos);
914 if (win.mAppToken != token) {
915 break;
916 }
917 if (!win.mRemoved) {
918 if (highestTarget == null || win.mAnimLayer >
919 highestTarget.mAnimLayer) {
920 highestTarget = win;
921 highestPos = pos;
922 }
923 }
924 pos--;
925 }
926 }
Romain Guy06882f82009-06-10 13:36:04 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700929 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 + mNextAppTransition + " " + highestTarget
931 + " animating=" + highestTarget.isAnimating()
932 + " layer=" + highestTarget.mAnimLayer
933 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
936 // If we are currently setting up for an animation,
937 // hold everything until we can find out what will happen.
938 mInputMethodTargetWaitingAnim = true;
939 mInputMethodTarget = highestTarget;
940 return highestPos + 1;
941 } else if (highestTarget.isAnimating() &&
942 highestTarget.mAnimLayer > w.mAnimLayer) {
943 // If the window we are currently targeting is involved
944 // with an animation, and it is on top of the next target
945 // we will be over, then hold off on moving until
946 // that is done.
947 mInputMethodTarget = highestTarget;
948 return highestPos + 1;
949 }
950 }
951 }
952 }
Romain Guy06882f82009-06-10 13:36:04 -0700953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 //Log.i(TAG, "Placing input method @" + (i+1));
955 if (w != null) {
956 if (willMove) {
957 RuntimeException e = new RuntimeException();
958 e.fillInStackTrace();
959 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
960 + mInputMethodTarget + " to " + w, e);
961 mInputMethodTarget = w;
962 if (w.mAppToken != null) {
963 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
964 } else {
965 setInputMethodAnimLayerAdjustment(0);
966 }
967 }
968 return i+1;
969 }
970 if (willMove) {
971 RuntimeException e = new RuntimeException();
972 e.fillInStackTrace();
973 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
974 + mInputMethodTarget + " to null", e);
975 mInputMethodTarget = null;
976 setInputMethodAnimLayerAdjustment(0);
977 }
978 return -1;
979 }
Romain Guy06882f82009-06-10 13:36:04 -0700980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 void addInputMethodWindowToListLocked(WindowState win) {
982 int pos = findDesiredInputMethodWindowIndexLocked(true);
983 if (pos >= 0) {
984 win.mTargetAppToken = mInputMethodTarget.mAppToken;
985 mWindows.add(pos, win);
986 moveInputMethodDialogsLocked(pos+1);
987 return;
988 }
989 win.mTargetAppToken = null;
990 addWindowToListInOrderLocked(win, true);
991 moveInputMethodDialogsLocked(pos);
992 }
Romain Guy06882f82009-06-10 13:36:04 -0700993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 void setInputMethodAnimLayerAdjustment(int adj) {
995 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
996 mInputMethodAnimLayerAdjustment = adj;
997 WindowState imw = mInputMethodWindow;
998 if (imw != null) {
999 imw.mAnimLayer = imw.mLayer + adj;
1000 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1001 + " anim layer: " + imw.mAnimLayer);
1002 int wi = imw.mChildWindows.size();
1003 while (wi > 0) {
1004 wi--;
1005 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1006 cw.mAnimLayer = cw.mLayer + adj;
1007 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1008 + " anim layer: " + cw.mAnimLayer);
1009 }
1010 }
1011 int di = mInputMethodDialogs.size();
1012 while (di > 0) {
1013 di --;
1014 imw = mInputMethodDialogs.get(di);
1015 imw.mAnimLayer = imw.mLayer + adj;
1016 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1017 + " anim layer: " + imw.mAnimLayer);
1018 }
1019 }
Romain Guy06882f82009-06-10 13:36:04 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1022 int wpos = mWindows.indexOf(win);
1023 if (wpos >= 0) {
1024 if (wpos < interestingPos) interestingPos--;
1025 mWindows.remove(wpos);
1026 int NC = win.mChildWindows.size();
1027 while (NC > 0) {
1028 NC--;
1029 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1030 int cpos = mWindows.indexOf(cw);
1031 if (cpos >= 0) {
1032 if (cpos < interestingPos) interestingPos--;
1033 mWindows.remove(cpos);
1034 }
1035 }
1036 }
1037 return interestingPos;
1038 }
Romain Guy06882f82009-06-10 13:36:04 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 private void reAddWindowToListInOrderLocked(WindowState win) {
1041 addWindowToListInOrderLocked(win, false);
1042 // This is a hack to get all of the child windows added as well
1043 // at the right position. Child windows should be rare and
1044 // this case should be rare, so it shouldn't be that big a deal.
1045 int wpos = mWindows.indexOf(win);
1046 if (wpos >= 0) {
1047 mWindows.remove(wpos);
1048 reAddWindowLocked(wpos, win);
1049 }
1050 }
Romain Guy06882f82009-06-10 13:36:04 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 void logWindowList(String prefix) {
1053 int N = mWindows.size();
1054 while (N > 0) {
1055 N--;
1056 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1057 }
1058 }
Romain Guy06882f82009-06-10 13:36:04 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 void moveInputMethodDialogsLocked(int pos) {
1061 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 final int N = dialogs.size();
1064 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1065 for (int i=0; i<N; i++) {
1066 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1067 }
1068 if (DEBUG_INPUT_METHOD) {
1069 Log.v(TAG, "Window list w/pos=" + pos);
1070 logWindowList(" ");
1071 }
Romain Guy06882f82009-06-10 13:36:04 -07001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (pos >= 0) {
1074 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1075 if (pos < mWindows.size()) {
1076 WindowState wp = (WindowState)mWindows.get(pos);
1077 if (wp == mInputMethodWindow) {
1078 pos++;
1079 }
1080 }
1081 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1082 for (int i=0; i<N; i++) {
1083 WindowState win = dialogs.get(i);
1084 win.mTargetAppToken = targetAppToken;
1085 pos = reAddWindowLocked(pos, win);
1086 }
1087 if (DEBUG_INPUT_METHOD) {
1088 Log.v(TAG, "Final window list:");
1089 logWindowList(" ");
1090 }
1091 return;
1092 }
1093 for (int i=0; i<N; i++) {
1094 WindowState win = dialogs.get(i);
1095 win.mTargetAppToken = null;
1096 reAddWindowToListInOrderLocked(win);
1097 if (DEBUG_INPUT_METHOD) {
1098 Log.v(TAG, "No IM target, final list:");
1099 logWindowList(" ");
1100 }
1101 }
1102 }
Romain Guy06882f82009-06-10 13:36:04 -07001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1105 final WindowState imWin = mInputMethodWindow;
1106 final int DN = mInputMethodDialogs.size();
1107 if (imWin == null && DN == 0) {
1108 return false;
1109 }
Romain Guy06882f82009-06-10 13:36:04 -07001110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1112 if (imPos >= 0) {
1113 // In this case, the input method windows are to be placed
1114 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 // First check to see if the input method windows are already
1117 // located here, and contiguous.
1118 final int N = mWindows.size();
1119 WindowState firstImWin = imPos < N
1120 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 // Figure out the actual input method window that should be
1123 // at the bottom of their stack.
1124 WindowState baseImWin = imWin != null
1125 ? imWin : mInputMethodDialogs.get(0);
1126 if (baseImWin.mChildWindows.size() > 0) {
1127 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1128 if (cw.mSubLayer < 0) baseImWin = cw;
1129 }
Romain Guy06882f82009-06-10 13:36:04 -07001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (firstImWin == baseImWin) {
1132 // The windows haven't moved... but are they still contiguous?
1133 // First find the top IM window.
1134 int pos = imPos+1;
1135 while (pos < N) {
1136 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1137 break;
1138 }
1139 pos++;
1140 }
1141 pos++;
1142 // Now there should be no more input method windows above.
1143 while (pos < N) {
1144 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1145 break;
1146 }
1147 pos++;
1148 }
1149 if (pos >= N) {
1150 // All is good!
1151 return false;
1152 }
1153 }
Romain Guy06882f82009-06-10 13:36:04 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (imWin != null) {
1156 if (DEBUG_INPUT_METHOD) {
1157 Log.v(TAG, "Moving IM from " + imPos);
1158 logWindowList(" ");
1159 }
1160 imPos = tmpRemoveWindowLocked(imPos, imWin);
1161 if (DEBUG_INPUT_METHOD) {
1162 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1163 logWindowList(" ");
1164 }
1165 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1166 reAddWindowLocked(imPos, imWin);
1167 if (DEBUG_INPUT_METHOD) {
1168 Log.v(TAG, "List after moving IM to " + imPos + ":");
1169 logWindowList(" ");
1170 }
1171 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1172 } else {
1173 moveInputMethodDialogsLocked(imPos);
1174 }
Romain Guy06882f82009-06-10 13:36:04 -07001175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 } else {
1177 // In this case, the input method windows go in a fixed layer,
1178 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 if (imWin != null) {
1181 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1182 tmpRemoveWindowLocked(0, imWin);
1183 imWin.mTargetAppToken = null;
1184 reAddWindowToListInOrderLocked(imWin);
1185 if (DEBUG_INPUT_METHOD) {
1186 Log.v(TAG, "List with no IM target:");
1187 logWindowList(" ");
1188 }
1189 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1190 } else {
1191 moveInputMethodDialogsLocked(-1);;
1192 }
Romain Guy06882f82009-06-10 13:36:04 -07001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
Romain Guy06882f82009-06-10 13:36:04 -07001195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 if (needAssignLayers) {
1197 assignLayersLocked();
1198 }
Romain Guy06882f82009-06-10 13:36:04 -07001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 return true;
1201 }
Romain Guy06882f82009-06-10 13:36:04 -07001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 void adjustInputMethodDialogsLocked() {
1204 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1205 }
Romain Guy06882f82009-06-10 13:36:04 -07001206
Dianne Hackborn25994b42009-09-04 14:21:19 -07001207 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1208 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1209 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1210 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1211 ? wallpaperTarget.mAppToken.animation : null)
1212 + " upper=" + mUpperWallpaperTarget
1213 + " lower=" + mLowerWallpaperTarget);
1214 return (wallpaperTarget != null
1215 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1216 && wallpaperTarget.mAppToken.animation != null)))
1217 || mUpperWallpaperTarget != null
1218 || mLowerWallpaperTarget != null;
1219 }
1220
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001221 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1222 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1223
1224 int adjustWallpaperWindowsLocked() {
1225 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001226
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001227 final int dw = mDisplay.getWidth();
1228 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001229
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001230 // First find top-most window that has asked to be on top of the
1231 // wallpaper; all wallpapers go behind it.
1232 final ArrayList localmWindows = mWindows;
1233 int N = localmWindows.size();
1234 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001235 WindowState foundW = null;
1236 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001237 int i = N;
1238 while (i > 0) {
1239 i--;
1240 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001241 if (w.mAppToken != null) {
1242 // If this window's app token is hidden and not animating,
1243 // it is of no interest to us.
1244 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1245 if (DEBUG_WALLPAPER) Log.v(TAG,
1246 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001247 continue;
1248 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001249 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001250 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1251 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1252 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001253 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001254 && (mWallpaperTarget == w
1255 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001256 if (DEBUG_WALLPAPER) Log.v(TAG,
1257 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001258 foundW = w;
1259 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001260 if (w == mWallpaperTarget && w.mAppToken != null
1261 && w.mAppToken.animation != null) {
1262 // The current wallpaper target is animating, so we'll
1263 // look behind it for another possible target and figure
1264 // out what is going on below.
1265 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1266 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001267 continue;
1268 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001269 break;
1270 }
1271 }
1272
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001273 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1274 // If we are currently waiting for an app transition, and either
1275 // the current target or the next target are involved with it,
1276 // then hold off on doing anything with the wallpaper.
1277 // Note that we are checking here for just whether the target
1278 // is part of an app token... which is potentially overly aggressive
1279 // (the app token may not be involved in the transition), but good
1280 // enough (we'll just wait until whatever transition is pending
1281 // executes).
1282 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001283 if (DEBUG_WALLPAPER) Log.v(TAG,
1284 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001285 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 }
1287 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001288 if (DEBUG_WALLPAPER) Log.v(TAG,
1289 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001290 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001291 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001292 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001293
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001294 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001295 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001296 Log.v(TAG, "New wallpaper target: " + foundW
1297 + " oldTarget: " + mWallpaperTarget);
1298 }
1299
1300 mLowerWallpaperTarget = null;
1301 mUpperWallpaperTarget = null;
1302
1303 WindowState oldW = mWallpaperTarget;
1304 mWallpaperTarget = foundW;
1305
1306 // Now what is happening... if the current and new targets are
1307 // animating, then we are in our super special mode!
1308 if (foundW != null && foundW.mAppToken != null && oldW != null
1309 && oldW.mAppToken != null) {
1310 if (DEBUG_WALLPAPER) {
1311 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1312 + " old animation: " + oldW.mAppToken.animation);
1313 }
1314 if (foundW.mAppToken.animation != null
1315 && oldW.mAppToken.animation != null) {
1316 int oldI = localmWindows.indexOf(oldW);
1317 if (DEBUG_WALLPAPER) {
1318 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1319 }
1320 if (oldI >= 0) {
1321 if (DEBUG_WALLPAPER) {
1322 Log.v(TAG, "Animating wallpapers: old#" + oldI
1323 + "=" + oldW + "; new#" + foundI
1324 + "=" + foundW);
1325 }
1326
1327 // Set the new target correctly.
1328 if (foundW.mAppToken.hiddenRequested) {
1329 if (DEBUG_WALLPAPER) {
1330 Log.v(TAG, "Old wallpaper still the target.");
1331 }
1332 mWallpaperTarget = oldW;
1333 }
1334
1335 // Now set the upper and lower wallpaper targets
1336 // correctly, and make sure that we are positioning
1337 // the wallpaper below the lower.
1338 if (foundI > oldI) {
1339 // The new target is on top of the old one.
1340 if (DEBUG_WALLPAPER) {
1341 Log.v(TAG, "Found target above old target.");
1342 }
1343 mUpperWallpaperTarget = foundW;
1344 mLowerWallpaperTarget = oldW;
1345 foundW = oldW;
1346 foundI = oldI;
1347 } else {
1348 // The new target is below the old one.
1349 if (DEBUG_WALLPAPER) {
1350 Log.v(TAG, "Found target below old target.");
1351 }
1352 mUpperWallpaperTarget = oldW;
1353 mLowerWallpaperTarget = foundW;
1354 }
1355 }
1356 }
1357 }
1358
1359 } else {
1360 // Is it time to stop animating?
1361 if (mLowerWallpaperTarget == null
1362 || mLowerWallpaperTarget.mAppToken.animation == null
1363 || mUpperWallpaperTarget == null
1364 || mUpperWallpaperTarget.mAppToken.animation == null) {
1365 if (DEBUG_WALLPAPER) {
1366 Log.v(TAG, "No longer animating wallpaper targets!");
1367 }
1368 mLowerWallpaperTarget = null;
1369 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001370 }
1371 }
1372
1373 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001374 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001375 // The window is visible to the compositor... but is it visible
1376 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001377 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001378 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001379
1380 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001381 // its layer adjustment. Only do this if we are not transfering
1382 // between two wallpaper targets.
1383 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001384 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001385 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001386
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001387 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1388 * TYPE_LAYER_MULTIPLIER
1389 + TYPE_LAYER_OFFSET;
1390
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001391 // Now w is the window we are supposed to be behind... but we
1392 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001393 // AND any starting window associated with it, AND below the
1394 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001395 while (foundI > 0) {
1396 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001397 if (wb.mBaseLayer < maxLayer &&
1398 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001399 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001400 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001401 // This window is not related to the previous one in any
1402 // interesting way, so stop here.
1403 break;
1404 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001405 foundW = wb;
1406 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001407 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001408 } else {
1409 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001410 }
1411
1412 // Okay i is the position immediately above the wallpaper. Look at
1413 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001414 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001415
Dianne Hackborn284ac932009-08-28 10:34:25 -07001416 if (visible) {
1417 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1418 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1419 }
1420
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001421 // Start stepping backwards from here, ensuring that our wallpaper windows
1422 // are correctly placed.
1423 int curTokenIndex = mWallpaperTokens.size();
1424 while (curTokenIndex > 0) {
1425 curTokenIndex--;
1426 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001427 if (token.hidden == visible) {
1428 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1429 token.hidden = !visible;
1430 // Need to do a layout to ensure the wallpaper now has the
1431 // correct size.
1432 mLayoutNeeded = true;
1433 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001434
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001435 int curWallpaperIndex = token.windows.size();
1436 while (curWallpaperIndex > 0) {
1437 curWallpaperIndex--;
1438 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001439
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001440 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001441 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001442 }
1443
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001444 // First, make sure the client has the current visibility
1445 // state.
1446 if (wallpaper.mWallpaperVisible != visible) {
1447 wallpaper.mWallpaperVisible = visible;
1448 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001449 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001450 "Setting visibility of wallpaper " + wallpaper
1451 + ": " + visible);
1452 wallpaper.mClient.dispatchAppVisibility(visible);
1453 } catch (RemoteException e) {
1454 }
1455 }
1456
1457 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001458 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1459 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001460
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001461 // First, if this window is at the current index, then all
1462 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001463 if (wallpaper == foundW) {
1464 foundI--;
1465 foundW = foundI > 0
1466 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001467 continue;
1468 }
1469
1470 // The window didn't match... the current wallpaper window,
1471 // wherever it is, is in the wrong place, so make sure it is
1472 // not in the list.
1473 int oldIndex = localmWindows.indexOf(wallpaper);
1474 if (oldIndex >= 0) {
1475 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001476 if (oldIndex < foundI) {
1477 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001478 }
1479 }
1480
1481 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001482 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001483 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001484
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001485 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001486 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001487 }
1488 }
1489
1490 return changed;
1491 }
1492
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001493 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001494 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1495 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001496 mWallpaperAnimLayerAdjustment = adj;
1497 int curTokenIndex = mWallpaperTokens.size();
1498 while (curTokenIndex > 0) {
1499 curTokenIndex--;
1500 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1501 int curWallpaperIndex = token.windows.size();
1502 while (curWallpaperIndex > 0) {
1503 curWallpaperIndex--;
1504 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1505 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001506 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1507 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001508 }
1509 }
1510 }
1511
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001512 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1513 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001514 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001515 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001516 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001517 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001518 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001519 changed = wallpaperWin.mXOffset != offset;
1520 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001521 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1522 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001523 wallpaperWin.mXOffset = offset;
1524 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001525 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1526 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001527 rawChanged = true;
1528 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001529 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001530
Dianne Hackborn284ac932009-08-28 10:34:25 -07001531 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001532 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001533 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001534 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001535 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1536 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001537 changed = true;
1538 wallpaperWin.mYOffset = offset;
1539 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001540 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1541 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001542 rawChanged = true;
1543 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001544 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001545
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001546 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001547 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001548 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1549 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1550 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001551 if (sync) {
1552 synchronized (mWaitingOnWallpaperLock) {
1553 mWaitingOnWallpaper = wallpaperWin;
1554 }
1555 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001556 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001557 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1558 if (sync) {
1559 synchronized (mWaitingOnWallpaperLock) {
1560 if (mWaitingOnWallpaper != null) {
1561 long start = SystemClock.uptimeMillis();
1562 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1563 < start) {
1564 try {
1565 if (DEBUG_WALLPAPER) Log.v(TAG,
1566 "Waiting for offset complete...");
1567 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1568 } catch (InterruptedException e) {
1569 }
1570 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1571 if ((start+WALLPAPER_TIMEOUT)
1572 < SystemClock.uptimeMillis()) {
1573 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1574 + wallpaperWin);
1575 mLastWallpaperTimeoutTime = start;
1576 }
1577 }
1578 mWaitingOnWallpaper = null;
1579 }
1580 }
1581 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001582 } catch (RemoteException e) {
1583 }
1584 }
1585
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001586 return changed;
1587 }
1588
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001589 void wallpaperOffsetsComplete(IBinder window) {
1590 synchronized (mWaitingOnWallpaperLock) {
1591 if (mWaitingOnWallpaper != null &&
1592 mWaitingOnWallpaper.mClient.asBinder() == window) {
1593 mWaitingOnWallpaper = null;
1594 mWaitingOnWallpaperLock.notifyAll();
1595 }
1596 }
1597 }
1598
1599 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001600 final int dw = mDisplay.getWidth();
1601 final int dh = mDisplay.getHeight();
1602
1603 boolean changed = false;
1604
1605 WindowState target = mWallpaperTarget;
1606 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001607 mLastWallpaperX = target.mWallpaperX;
1608 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001609 int curTokenIndex = mWallpaperTokens.size();
1610 while (curTokenIndex > 0) {
1611 curTokenIndex--;
1612 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1613 int curWallpaperIndex = token.windows.size();
1614 while (curWallpaperIndex > 0) {
1615 curWallpaperIndex--;
1616 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001617 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001618 wallpaper.computeShownFrameLocked();
1619 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 // We only want to be synchronous with one wallpaper.
1621 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001622 }
1623 }
1624 }
1625 }
1626
1627 return changed;
1628 }
1629
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001630 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001631 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001632 final int dw = mDisplay.getWidth();
1633 final int dh = mDisplay.getHeight();
1634
1635 int curTokenIndex = mWallpaperTokens.size();
1636 while (curTokenIndex > 0) {
1637 curTokenIndex--;
1638 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001639 if (token.hidden == visible) {
1640 token.hidden = !visible;
1641 // Need to do a layout to ensure the wallpaper now has the
1642 // correct size.
1643 mLayoutNeeded = true;
1644 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001645
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001646 int curWallpaperIndex = token.windows.size();
1647 while (curWallpaperIndex > 0) {
1648 curWallpaperIndex--;
1649 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1650 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001651 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001652 }
1653
1654 if (wallpaper.mWallpaperVisible != visible) {
1655 wallpaper.mWallpaperVisible = visible;
1656 try {
1657 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001658 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001659 + ": " + visible);
1660 wallpaper.mClient.dispatchAppVisibility(visible);
1661 } catch (RemoteException e) {
1662 }
1663 }
1664 }
1665 }
1666 }
1667
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001668 void sendPointerToWallpaperLocked(WindowState srcWin,
1669 MotionEvent pointer, long eventTime) {
1670 int curTokenIndex = mWallpaperTokens.size();
1671 while (curTokenIndex > 0) {
1672 curTokenIndex--;
1673 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1674 int curWallpaperIndex = token.windows.size();
1675 while (curWallpaperIndex > 0) {
1676 curWallpaperIndex--;
1677 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1678 if ((wallpaper.mAttrs.flags &
1679 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1680 continue;
1681 }
1682 try {
1683 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1684 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1685 srcWin.mFrame.top-wallpaper.mFrame.top);
1686 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1687 } catch (RemoteException e) {
1688 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1689 }
1690 }
1691 }
1692 }
1693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 public int addWindow(Session session, IWindow client,
1695 WindowManager.LayoutParams attrs, int viewVisibility,
1696 Rect outContentInsets) {
1697 int res = mPolicy.checkAddPermission(attrs);
1698 if (res != WindowManagerImpl.ADD_OKAY) {
1699 return res;
1700 }
Romain Guy06882f82009-06-10 13:36:04 -07001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 boolean reportNewConfig = false;
1703 WindowState attachedWindow = null;
1704 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 synchronized(mWindowMap) {
1707 // Instantiating a Display requires talking with the simulator,
1708 // so don't do it until we know the system is mostly up and
1709 // running.
1710 if (mDisplay == null) {
1711 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1712 mDisplay = wm.getDefaultDisplay();
1713 mQueue.setDisplay(mDisplay);
1714 reportNewConfig = true;
1715 }
Romain Guy06882f82009-06-10 13:36:04 -07001716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 if (mWindowMap.containsKey(client.asBinder())) {
1718 Log.w(TAG, "Window " + client + " is already added");
1719 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1720 }
1721
1722 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001723 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 if (attachedWindow == null) {
1725 Log.w(TAG, "Attempted to add window with token that is not a window: "
1726 + attrs.token + ". Aborting.");
1727 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1728 }
1729 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1730 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1731 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1732 + attrs.token + ". Aborting.");
1733 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1734 }
1735 }
1736
1737 boolean addToken = false;
1738 WindowToken token = mTokenMap.get(attrs.token);
1739 if (token == null) {
1740 if (attrs.type >= FIRST_APPLICATION_WINDOW
1741 && attrs.type <= LAST_APPLICATION_WINDOW) {
1742 Log.w(TAG, "Attempted to add application window with unknown token "
1743 + attrs.token + ". Aborting.");
1744 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1745 }
1746 if (attrs.type == TYPE_INPUT_METHOD) {
1747 Log.w(TAG, "Attempted to add input method window with unknown token "
1748 + attrs.token + ". Aborting.");
1749 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1750 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001751 if (attrs.type == TYPE_WALLPAPER) {
1752 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1753 + attrs.token + ". Aborting.");
1754 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 token = new WindowToken(attrs.token, -1, false);
1757 addToken = true;
1758 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1759 && attrs.type <= LAST_APPLICATION_WINDOW) {
1760 AppWindowToken atoken = token.appWindowToken;
1761 if (atoken == null) {
1762 Log.w(TAG, "Attempted to add window with non-application token "
1763 + token + ". Aborting.");
1764 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1765 } else if (atoken.removed) {
1766 Log.w(TAG, "Attempted to add window with exiting application token "
1767 + token + ". Aborting.");
1768 return WindowManagerImpl.ADD_APP_EXITING;
1769 }
1770 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1771 // No need for this guy!
1772 if (localLOGV) Log.v(
1773 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1774 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1775 }
1776 } else if (attrs.type == TYPE_INPUT_METHOD) {
1777 if (token.windowType != TYPE_INPUT_METHOD) {
1778 Log.w(TAG, "Attempted to add input method window with bad token "
1779 + attrs.token + ". Aborting.");
1780 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1781 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001782 } else if (attrs.type == TYPE_WALLPAPER) {
1783 if (token.windowType != TYPE_WALLPAPER) {
1784 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1785 + attrs.token + ". Aborting.");
1786 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789
1790 win = new WindowState(session, client, token,
1791 attachedWindow, attrs, viewVisibility);
1792 if (win.mDeathRecipient == null) {
1793 // Client has apparently died, so there is no reason to
1794 // continue.
1795 Log.w(TAG, "Adding window client " + client.asBinder()
1796 + " that is dead, aborting.");
1797 return WindowManagerImpl.ADD_APP_EXITING;
1798 }
1799
1800 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 res = mPolicy.prepareAddWindowLw(win, attrs);
1803 if (res != WindowManagerImpl.ADD_OKAY) {
1804 return res;
1805 }
1806
1807 // From now on, no exceptions or errors allowed!
1808
1809 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 if (addToken) {
1814 mTokenMap.put(attrs.token, token);
1815 mTokenList.add(token);
1816 }
1817 win.attach();
1818 mWindowMap.put(client.asBinder(), win);
1819
1820 if (attrs.type == TYPE_APPLICATION_STARTING &&
1821 token.appWindowToken != null) {
1822 token.appWindowToken.startingWindow = win;
1823 }
1824
1825 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 if (attrs.type == TYPE_INPUT_METHOD) {
1828 mInputMethodWindow = win;
1829 addInputMethodWindowToListLocked(win);
1830 imMayMove = false;
1831 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1832 mInputMethodDialogs.add(win);
1833 addWindowToListInOrderLocked(win, true);
1834 adjustInputMethodDialogsLocked();
1835 imMayMove = false;
1836 } else {
1837 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001838 if (attrs.type == TYPE_WALLPAPER) {
1839 mLastWallpaperTimeoutTime = 0;
1840 adjustWallpaperWindowsLocked();
1841 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001842 adjustWallpaperWindowsLocked();
1843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
Romain Guy06882f82009-06-10 13:36:04 -07001845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 if (mInTouchMode) {
1851 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1852 }
1853 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1854 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1855 }
Romain Guy06882f82009-06-10 13:36:04 -07001856
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001857 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001859 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1860 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 imMayMove = false;
1862 }
1863 }
Romain Guy06882f82009-06-10 13:36:04 -07001864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001866 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
Romain Guy06882f82009-06-10 13:36:04 -07001868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 assignLayersLocked();
1870 // Don't do layout here, the window must call
1871 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 //dump();
1874
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001875 if (focusChanged) {
1876 if (mCurrentFocus != null) {
1877 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1878 }
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (localLOGV) Log.v(
1881 TAG, "New client " + client.asBinder()
1882 + ": window=" + win);
1883 }
1884
1885 // sendNewConfiguration() checks caller permissions so we must call it with
1886 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1887 // identity anyway, so it's safe to just clear & restore around this whole
1888 // block.
1889 final long origId = Binder.clearCallingIdentity();
1890 if (reportNewConfig) {
1891 sendNewConfiguration();
1892 } else {
1893 // Update Orientation after adding a window, only if the window needs to be
1894 // displayed right away
1895 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001896 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 sendNewConfiguration();
1898 }
1899 }
1900 }
1901 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 return res;
1904 }
Romain Guy06882f82009-06-10 13:36:04 -07001905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 public void removeWindow(Session session, IWindow client) {
1907 synchronized(mWindowMap) {
1908 WindowState win = windowForClientLocked(session, client);
1909 if (win == null) {
1910 return;
1911 }
1912 removeWindowLocked(session, win);
1913 }
1914 }
Romain Guy06882f82009-06-10 13:36:04 -07001915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 public void removeWindowLocked(Session session, WindowState win) {
1917
1918 if (localLOGV || DEBUG_FOCUS) Log.v(
1919 TAG, "Remove " + win + " client="
1920 + Integer.toHexString(System.identityHashCode(
1921 win.mClient.asBinder()))
1922 + ", surface=" + win.mSurface);
1923
1924 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if (DEBUG_APP_TRANSITIONS) Log.v(
1927 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1928 + " mExiting=" + win.mExiting
1929 + " isAnimating=" + win.isAnimating()
1930 + " app-animation="
1931 + (win.mAppToken != null ? win.mAppToken.animation : null)
1932 + " inPendingTransaction="
1933 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1934 + " mDisplayFrozen=" + mDisplayFrozen);
1935 // Visibility of the removed window. Will be used later to update orientation later on.
1936 boolean wasVisible = false;
1937 // First, see if we need to run an animation. If we do, we have
1938 // to hold off on removing the window until the animation is done.
1939 // If the display is frozen, just remove immediately, since the
1940 // animation wouldn't be seen.
1941 if (win.mSurface != null && !mDisplayFrozen) {
1942 // If we are not currently running the exit animation, we
1943 // need to see about starting one.
1944 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1947 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1948 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1949 }
1950 // Try starting an animation.
1951 if (applyAnimationLocked(win, transit, false)) {
1952 win.mExiting = true;
1953 }
1954 }
1955 if (win.mExiting || win.isAnimating()) {
1956 // The exit animation is running... wait for it!
1957 //Log.i(TAG, "*** Running exit animation...");
1958 win.mExiting = true;
1959 win.mRemoveOnExit = true;
1960 mLayoutNeeded = true;
1961 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1962 performLayoutAndPlaceSurfacesLocked();
1963 if (win.mAppToken != null) {
1964 win.mAppToken.updateReportedVisibilityLocked();
1965 }
1966 //dump();
1967 Binder.restoreCallingIdentity(origId);
1968 return;
1969 }
1970 }
1971
1972 removeWindowInnerLocked(session, win);
1973 // Removing a visible window will effect the computed orientation
1974 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001975 if (wasVisible && computeForcedAppOrientationLocked()
1976 != mForcedAppOrientation) {
1977 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1980 Binder.restoreCallingIdentity(origId);
1981 }
Romain Guy06882f82009-06-10 13:36:04 -07001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001984 mKeyWaiter.finishedKey(session, win.mClient, true,
1985 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1987 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 if (mInputMethodTarget == win) {
1992 moveInputMethodWindowsIfNeededLocked(false);
1993 }
Romain Guy06882f82009-06-10 13:36:04 -07001994
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001995 if (false) {
1996 RuntimeException e = new RuntimeException("here");
1997 e.fillInStackTrace();
1998 Log.w(TAG, "Removing window " + win, e);
1999 }
2000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 mPolicy.removeWindowLw(win);
2002 win.removeLocked();
2003
2004 mWindowMap.remove(win.mClient.asBinder());
2005 mWindows.remove(win);
2006
2007 if (mInputMethodWindow == win) {
2008 mInputMethodWindow = null;
2009 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2010 mInputMethodDialogs.remove(win);
2011 }
Romain Guy06882f82009-06-10 13:36:04 -07002012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 final WindowToken token = win.mToken;
2014 final AppWindowToken atoken = win.mAppToken;
2015 token.windows.remove(win);
2016 if (atoken != null) {
2017 atoken.allAppWindows.remove(win);
2018 }
2019 if (localLOGV) Log.v(
2020 TAG, "**** Removing window " + win + ": count="
2021 + token.windows.size());
2022 if (token.windows.size() == 0) {
2023 if (!token.explicit) {
2024 mTokenMap.remove(token.token);
2025 mTokenList.remove(token);
2026 } else if (atoken != null) {
2027 atoken.firstWindowDrawn = false;
2028 }
2029 }
2030
2031 if (atoken != null) {
2032 if (atoken.startingWindow == win) {
2033 atoken.startingWindow = null;
2034 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2035 // If this is the last window and we had requested a starting
2036 // transition window, well there is no point now.
2037 atoken.startingData = null;
2038 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2039 // If this is the last window except for a starting transition
2040 // window, we need to get rid of the starting transition.
2041 if (DEBUG_STARTING_WINDOW) {
2042 Log.v(TAG, "Schedule remove starting " + token
2043 + ": no more real windows");
2044 }
2045 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2046 mH.sendMessage(m);
2047 }
2048 }
Romain Guy06882f82009-06-10 13:36:04 -07002049
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002050 if (win.mAttrs.type == TYPE_WALLPAPER) {
2051 mLastWallpaperTimeoutTime = 0;
2052 adjustWallpaperWindowsLocked();
2053 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002054 adjustWallpaperWindowsLocked();
2055 }
2056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 if (!mInLayout) {
2058 assignLayersLocked();
2059 mLayoutNeeded = true;
2060 performLayoutAndPlaceSurfacesLocked();
2061 if (win.mAppToken != null) {
2062 win.mAppToken.updateReportedVisibilityLocked();
2063 }
2064 }
2065 }
2066
2067 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2068 long origId = Binder.clearCallingIdentity();
2069 try {
2070 synchronized (mWindowMap) {
2071 WindowState w = windowForClientLocked(session, client);
2072 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002073 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 Surface.openTransaction();
2075 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002076 if (SHOW_TRANSACTIONS) Log.i(
2077 TAG, " SURFACE " + w.mSurface
2078 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 w.mSurface.setTransparentRegionHint(region);
2080 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002081 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 Surface.closeTransaction();
2083 }
2084 }
2085 }
2086 } finally {
2087 Binder.restoreCallingIdentity(origId);
2088 }
2089 }
2090
2091 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002092 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 Rect visibleInsets) {
2094 long origId = Binder.clearCallingIdentity();
2095 try {
2096 synchronized (mWindowMap) {
2097 WindowState w = windowForClientLocked(session, client);
2098 if (w != null) {
2099 w.mGivenInsetsPending = false;
2100 w.mGivenContentInsets.set(contentInsets);
2101 w.mGivenVisibleInsets.set(visibleInsets);
2102 w.mTouchableInsets = touchableInsets;
2103 mLayoutNeeded = true;
2104 performLayoutAndPlaceSurfacesLocked();
2105 }
2106 }
2107 } finally {
2108 Binder.restoreCallingIdentity(origId);
2109 }
2110 }
Romain Guy06882f82009-06-10 13:36:04 -07002111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 public void getWindowDisplayFrame(Session session, IWindow client,
2113 Rect outDisplayFrame) {
2114 synchronized(mWindowMap) {
2115 WindowState win = windowForClientLocked(session, client);
2116 if (win == null) {
2117 outDisplayFrame.setEmpty();
2118 return;
2119 }
2120 outDisplayFrame.set(win.mDisplayFrame);
2121 }
2122 }
2123
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002124 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2125 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2126 window.mWallpaperX = x;
2127 window.mWallpaperY = y;
2128
2129 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002130 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002131 performLayoutAndPlaceSurfacesLocked();
2132 }
2133 }
2134 }
2135 }
2136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 public int relayoutWindow(Session session, IWindow client,
2138 WindowManager.LayoutParams attrs, int requestedWidth,
2139 int requestedHeight, int viewVisibility, boolean insetsPending,
2140 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2141 Surface outSurface) {
2142 boolean displayed = false;
2143 boolean inTouchMode;
2144 Configuration newConfig = null;
2145 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 synchronized(mWindowMap) {
2148 WindowState win = windowForClientLocked(session, client);
2149 if (win == null) {
2150 return 0;
2151 }
2152 win.mRequestedWidth = requestedWidth;
2153 win.mRequestedHeight = requestedHeight;
2154
2155 if (attrs != null) {
2156 mPolicy.adjustWindowParamsLw(attrs);
2157 }
Romain Guy06882f82009-06-10 13:36:04 -07002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 int attrChanges = 0;
2160 int flagChanges = 0;
2161 if (attrs != null) {
2162 flagChanges = win.mAttrs.flags ^= attrs.flags;
2163 attrChanges = win.mAttrs.copyFrom(attrs);
2164 }
2165
2166 if (localLOGV) Log.v(
2167 TAG, "Relayout given client " + client.asBinder()
2168 + " (" + win.mAttrs.getTitle() + ")");
2169
2170
2171 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2172 win.mAlpha = attrs.alpha;
2173 }
2174
2175 final boolean scaledWindow =
2176 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2177
2178 if (scaledWindow) {
2179 // requested{Width|Height} Surface's physical size
2180 // attrs.{width|height} Size on screen
2181 win.mHScale = (attrs.width != requestedWidth) ?
2182 (attrs.width / (float)requestedWidth) : 1.0f;
2183 win.mVScale = (attrs.height != requestedHeight) ?
2184 (attrs.height / (float)requestedHeight) : 1.0f;
2185 }
2186
2187 boolean imMayMove = (flagChanges&(
2188 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2189 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 boolean focusMayChange = win.mViewVisibility != viewVisibility
2192 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2193 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002194
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002195 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2196 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 win.mRelayoutCalled = true;
2199 final int oldVisibility = win.mViewVisibility;
2200 win.mViewVisibility = viewVisibility;
2201 if (viewVisibility == View.VISIBLE &&
2202 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2203 displayed = !win.isVisibleLw();
2204 if (win.mExiting) {
2205 win.mExiting = false;
2206 win.mAnimation = null;
2207 }
2208 if (win.mDestroying) {
2209 win.mDestroying = false;
2210 mDestroySurface.remove(win);
2211 }
2212 if (oldVisibility == View.GONE) {
2213 win.mEnterAnimationPending = true;
2214 }
2215 if (displayed && win.mSurface != null && !win.mDrawPending
2216 && !win.mCommitDrawPending && !mDisplayFrozen) {
2217 applyEnterAnimationLocked(win);
2218 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002219 if (displayed && (win.mAttrs.flags
2220 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2221 win.mTurnOnScreen = true;
2222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2224 // To change the format, we need to re-build the surface.
2225 win.destroySurfaceLocked();
2226 displayed = true;
2227 }
2228 try {
2229 Surface surface = win.createSurfaceLocked();
2230 if (surface != null) {
2231 outSurface.copyFrom(surface);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002232 if (SHOW_TRANSACTIONS) Log.i(TAG,
2233 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002235 // For some reason there isn't a surface. Clear the
2236 // caller's object so they see the same state.
2237 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
2239 } catch (Exception e) {
2240 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002241 + client + " (" + win.mAttrs.getTitle() + ")",
2242 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 Binder.restoreCallingIdentity(origId);
2244 return 0;
2245 }
2246 if (displayed) {
2247 focusMayChange = true;
2248 }
2249 if (win.mAttrs.type == TYPE_INPUT_METHOD
2250 && mInputMethodWindow == null) {
2251 mInputMethodWindow = win;
2252 imMayMove = true;
2253 }
2254 } else {
2255 win.mEnterAnimationPending = false;
2256 if (win.mSurface != null) {
2257 // If we are not currently running the exit animation, we
2258 // need to see about starting one.
2259 if (!win.mExiting) {
2260 // Try starting an animation; if there isn't one, we
2261 // can destroy the surface right away.
2262 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2263 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2264 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2265 }
2266 if (win.isWinVisibleLw() &&
2267 applyAnimationLocked(win, transit, false)) {
2268 win.mExiting = true;
2269 mKeyWaiter.finishedKey(session, client, true,
2270 KeyWaiter.RETURN_NOTHING);
2271 } else if (win.isAnimating()) {
2272 // Currently in a hide animation... turn this into
2273 // an exit.
2274 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002275 } else if (win == mWallpaperTarget) {
2276 // If the wallpaper is currently behind this
2277 // window, we need to change both of them inside
2278 // of a transaction to avoid artifacts.
2279 win.mExiting = true;
2280 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 } else {
2282 if (mInputMethodWindow == win) {
2283 mInputMethodWindow = null;
2284 }
2285 win.destroySurfaceLocked();
2286 }
2287 }
2288 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002289 // We are being called from a local process, which
2290 // means outSurface holds its current surface. Ensure the
2291 // surface object is cleared, but we don't want it actually
2292 // destroyed at this point.
2293 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
2295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 if (focusMayChange) {
2297 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2298 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 imMayMove = false;
2300 }
2301 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2302 }
Romain Guy06882f82009-06-10 13:36:04 -07002303
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002304 // updateFocusedWindowLocked() already assigned layers so we only need to
2305 // reassign them at this point if the IM window state gets shuffled
2306 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 if (imMayMove) {
2309 if (moveInputMethodWindowsIfNeededLocked(false)) {
2310 assignLayers = true;
2311 }
2312 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002313 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002314 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002315 assignLayers = true;
2316 }
2317 }
Romain Guy06882f82009-06-10 13:36:04 -07002318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 mLayoutNeeded = true;
2320 win.mGivenInsetsPending = insetsPending;
2321 if (assignLayers) {
2322 assignLayersLocked();
2323 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002324 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002326 if (displayed && win.mIsWallpaper) {
2327 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002328 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 if (win.mAppToken != null) {
2331 win.mAppToken.updateReportedVisibilityLocked();
2332 }
2333 outFrame.set(win.mFrame);
2334 outContentInsets.set(win.mContentInsets);
2335 outVisibleInsets.set(win.mVisibleInsets);
2336 if (localLOGV) Log.v(
2337 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002338 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 + ", requestedHeight=" + requestedHeight
2340 + ", viewVisibility=" + viewVisibility
2341 + "\nRelayout returning frame=" + outFrame
2342 + ", surface=" + outSurface);
2343
2344 if (localLOGV || DEBUG_FOCUS) Log.v(
2345 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2346
2347 inTouchMode = mInTouchMode;
2348 }
2349
2350 if (newConfig != null) {
2351 sendNewConfiguration();
2352 }
Romain Guy06882f82009-06-10 13:36:04 -07002353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2357 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2358 }
2359
2360 public void finishDrawingWindow(Session session, IWindow client) {
2361 final long origId = Binder.clearCallingIdentity();
2362 synchronized(mWindowMap) {
2363 WindowState win = windowForClientLocked(session, client);
2364 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002365 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2366 adjustWallpaperWindowsLocked();
2367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 mLayoutNeeded = true;
2369 performLayoutAndPlaceSurfacesLocked();
2370 }
2371 }
2372 Binder.restoreCallingIdentity(origId);
2373 }
2374
2375 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2376 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2377 + (lp != null ? lp.packageName : null)
2378 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2379 if (lp != null && lp.windowAnimations != 0) {
2380 // If this is a system resource, don't try to load it from the
2381 // application resources. It is nice to avoid loading application
2382 // resources if we can.
2383 String packageName = lp.packageName != null ? lp.packageName : "android";
2384 int resId = lp.windowAnimations;
2385 if ((resId&0xFF000000) == 0x01000000) {
2386 packageName = "android";
2387 }
2388 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2389 + packageName);
2390 return AttributeCache.instance().get(packageName, resId,
2391 com.android.internal.R.styleable.WindowAnimation);
2392 }
2393 return null;
2394 }
Romain Guy06882f82009-06-10 13:36:04 -07002395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 private void applyEnterAnimationLocked(WindowState win) {
2397 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2398 if (win.mEnterAnimationPending) {
2399 win.mEnterAnimationPending = false;
2400 transit = WindowManagerPolicy.TRANSIT_ENTER;
2401 }
2402
2403 applyAnimationLocked(win, transit, true);
2404 }
2405
2406 private boolean applyAnimationLocked(WindowState win,
2407 int transit, boolean isEntrance) {
2408 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2409 // If we are trying to apply an animation, but already running
2410 // an animation of the same type, then just leave that one alone.
2411 return true;
2412 }
Romain Guy06882f82009-06-10 13:36:04 -07002413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 // Only apply an animation if the display isn't frozen. If it is
2415 // frozen, there is no reason to animate and it can cause strange
2416 // artifacts when we unfreeze the display if some different animation
2417 // is running.
2418 if (!mDisplayFrozen) {
2419 int anim = mPolicy.selectAnimationLw(win, transit);
2420 int attr = -1;
2421 Animation a = null;
2422 if (anim != 0) {
2423 a = AnimationUtils.loadAnimation(mContext, anim);
2424 } else {
2425 switch (transit) {
2426 case WindowManagerPolicy.TRANSIT_ENTER:
2427 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2428 break;
2429 case WindowManagerPolicy.TRANSIT_EXIT:
2430 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2431 break;
2432 case WindowManagerPolicy.TRANSIT_SHOW:
2433 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2434 break;
2435 case WindowManagerPolicy.TRANSIT_HIDE:
2436 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2437 break;
2438 }
2439 if (attr >= 0) {
2440 a = loadAnimation(win.mAttrs, attr);
2441 }
2442 }
2443 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2444 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2445 + " mAnimation=" + win.mAnimation
2446 + " isEntrance=" + isEntrance);
2447 if (a != null) {
2448 if (DEBUG_ANIM) {
2449 RuntimeException e = new RuntimeException();
2450 e.fillInStackTrace();
2451 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2452 }
2453 win.setAnimation(a);
2454 win.mAnimationIsEntrance = isEntrance;
2455 }
2456 } else {
2457 win.clearAnimation();
2458 }
2459
2460 return win.mAnimation != null;
2461 }
2462
2463 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2464 int anim = 0;
2465 Context context = mContext;
2466 if (animAttr >= 0) {
2467 AttributeCache.Entry ent = getCachedAnimations(lp);
2468 if (ent != null) {
2469 context = ent.context;
2470 anim = ent.array.getResourceId(animAttr, 0);
2471 }
2472 }
2473 if (anim != 0) {
2474 return AnimationUtils.loadAnimation(context, anim);
2475 }
2476 return null;
2477 }
Romain Guy06882f82009-06-10 13:36:04 -07002478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 private boolean applyAnimationLocked(AppWindowToken wtoken,
2480 WindowManager.LayoutParams lp, int transit, boolean enter) {
2481 // Only apply an animation if the display isn't frozen. If it is
2482 // frozen, there is no reason to animate and it can cause strange
2483 // artifacts when we unfreeze the display if some different animation
2484 // is running.
2485 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002486 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002487 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002488 a = new FadeInOutAnimation(enter);
2489 if (DEBUG_ANIM) Log.v(TAG,
2490 "applying FadeInOutAnimation for a window in compatibility mode");
2491 } else {
2492 int animAttr = 0;
2493 switch (transit) {
2494 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2495 animAttr = enter
2496 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2497 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2498 break;
2499 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2500 animAttr = enter
2501 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2502 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2503 break;
2504 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2505 animAttr = enter
2506 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2507 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2508 break;
2509 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2510 animAttr = enter
2511 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2512 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2513 break;
2514 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2515 animAttr = enter
2516 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2517 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2518 break;
2519 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2520 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002521 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002522 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2523 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002524 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002525 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002526 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2527 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002528 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002529 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002530 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002531 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2532 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2533 break;
2534 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2535 animAttr = enter
2536 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2537 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2538 break;
2539 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2540 animAttr = enter
2541 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2542 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002543 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002544 }
2545 a = loadAnimation(lp, animAttr);
2546 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2547 + " anim=" + a
2548 + " animAttr=0x" + Integer.toHexString(animAttr)
2549 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 if (a != null) {
2552 if (DEBUG_ANIM) {
2553 RuntimeException e = new RuntimeException();
2554 e.fillInStackTrace();
2555 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2556 }
2557 wtoken.setAnimation(a);
2558 }
2559 } else {
2560 wtoken.clearAnimation();
2561 }
2562
2563 return wtoken.animation != null;
2564 }
2565
2566 // -------------------------------------------------------------
2567 // Application Window Tokens
2568 // -------------------------------------------------------------
2569
2570 public void validateAppTokens(List tokens) {
2571 int v = tokens.size()-1;
2572 int m = mAppTokens.size()-1;
2573 while (v >= 0 && m >= 0) {
2574 AppWindowToken wtoken = mAppTokens.get(m);
2575 if (wtoken.removed) {
2576 m--;
2577 continue;
2578 }
2579 if (tokens.get(v) != wtoken.token) {
2580 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2581 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2582 }
2583 v--;
2584 m--;
2585 }
2586 while (v >= 0) {
2587 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2588 v--;
2589 }
2590 while (m >= 0) {
2591 AppWindowToken wtoken = mAppTokens.get(m);
2592 if (!wtoken.removed) {
2593 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2594 }
2595 m--;
2596 }
2597 }
2598
2599 boolean checkCallingPermission(String permission, String func) {
2600 // Quick check: if the calling permission is me, it's all okay.
2601 if (Binder.getCallingPid() == Process.myPid()) {
2602 return true;
2603 }
Romain Guy06882f82009-06-10 13:36:04 -07002604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 if (mContext.checkCallingPermission(permission)
2606 == PackageManager.PERMISSION_GRANTED) {
2607 return true;
2608 }
2609 String msg = "Permission Denial: " + func + " from pid="
2610 + Binder.getCallingPid()
2611 + ", uid=" + Binder.getCallingUid()
2612 + " requires " + permission;
2613 Log.w(TAG, msg);
2614 return false;
2615 }
Romain Guy06882f82009-06-10 13:36:04 -07002616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 AppWindowToken findAppWindowToken(IBinder token) {
2618 WindowToken wtoken = mTokenMap.get(token);
2619 if (wtoken == null) {
2620 return null;
2621 }
2622 return wtoken.appWindowToken;
2623 }
Romain Guy06882f82009-06-10 13:36:04 -07002624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 public void addWindowToken(IBinder token, int type) {
2626 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2627 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002628 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 }
Romain Guy06882f82009-06-10 13:36:04 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 synchronized(mWindowMap) {
2632 WindowToken wtoken = mTokenMap.get(token);
2633 if (wtoken != null) {
2634 Log.w(TAG, "Attempted to add existing input method token: " + token);
2635 return;
2636 }
2637 wtoken = new WindowToken(token, type, true);
2638 mTokenMap.put(token, wtoken);
2639 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002640 if (type == TYPE_WALLPAPER) {
2641 mWallpaperTokens.add(wtoken);
2642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 }
2644 }
Romain Guy06882f82009-06-10 13:36:04 -07002645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 public void removeWindowToken(IBinder token) {
2647 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2648 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002649 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 }
2651
2652 final long origId = Binder.clearCallingIdentity();
2653 synchronized(mWindowMap) {
2654 WindowToken wtoken = mTokenMap.remove(token);
2655 mTokenList.remove(wtoken);
2656 if (wtoken != null) {
2657 boolean delayed = false;
2658 if (!wtoken.hidden) {
2659 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 final int N = wtoken.windows.size();
2662 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 for (int i=0; i<N; i++) {
2665 WindowState win = wtoken.windows.get(i);
2666
2667 if (win.isAnimating()) {
2668 delayed = true;
2669 }
Romain Guy06882f82009-06-10 13:36:04 -07002670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 if (win.isVisibleNow()) {
2672 applyAnimationLocked(win,
2673 WindowManagerPolicy.TRANSIT_EXIT, false);
2674 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2675 KeyWaiter.RETURN_NOTHING);
2676 changed = true;
2677 }
2678 }
2679
2680 if (changed) {
2681 mLayoutNeeded = true;
2682 performLayoutAndPlaceSurfacesLocked();
2683 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2684 }
Romain Guy06882f82009-06-10 13:36:04 -07002685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 if (delayed) {
2687 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002688 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2689 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
2691 }
Romain Guy06882f82009-06-10 13:36:04 -07002692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 } else {
2694 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2695 }
2696 }
2697 Binder.restoreCallingIdentity(origId);
2698 }
2699
2700 public void addAppToken(int addPos, IApplicationToken token,
2701 int groupId, int requestedOrientation, boolean fullscreen) {
2702 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2703 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002704 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 }
Romain Guy06882f82009-06-10 13:36:04 -07002706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 synchronized(mWindowMap) {
2708 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2709 if (wtoken != null) {
2710 Log.w(TAG, "Attempted to add existing app token: " + token);
2711 return;
2712 }
2713 wtoken = new AppWindowToken(token);
2714 wtoken.groupId = groupId;
2715 wtoken.appFullscreen = fullscreen;
2716 wtoken.requestedOrientation = requestedOrientation;
2717 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002718 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 mTokenMap.put(token.asBinder(), wtoken);
2720 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 // Application tokens start out hidden.
2723 wtoken.hidden = true;
2724 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 //dump();
2727 }
2728 }
Romain Guy06882f82009-06-10 13:36:04 -07002729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 public void setAppGroupId(IBinder token, int groupId) {
2731 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2732 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002733 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 }
2735
2736 synchronized(mWindowMap) {
2737 AppWindowToken wtoken = findAppWindowToken(token);
2738 if (wtoken == null) {
2739 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2740 return;
2741 }
2742 wtoken.groupId = groupId;
2743 }
2744 }
Romain Guy06882f82009-06-10 13:36:04 -07002745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 public int getOrientationFromWindowsLocked() {
2747 int pos = mWindows.size() - 1;
2748 while (pos >= 0) {
2749 WindowState wtoken = (WindowState) mWindows.get(pos);
2750 pos--;
2751 if (wtoken.mAppToken != null) {
2752 // We hit an application window. so the orientation will be determined by the
2753 // app window. No point in continuing further.
2754 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2755 }
2756 if (!wtoken.isVisibleLw()) {
2757 continue;
2758 }
2759 int req = wtoken.mAttrs.screenOrientation;
2760 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2761 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2762 continue;
2763 } else {
2764 return req;
2765 }
2766 }
2767 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2768 }
Romain Guy06882f82009-06-10 13:36:04 -07002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 public int getOrientationFromAppTokensLocked() {
2771 int pos = mAppTokens.size() - 1;
2772 int curGroup = 0;
2773 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002774 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002776 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 while (pos >= 0) {
2778 AppWindowToken wtoken = mAppTokens.get(pos);
2779 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002780 // if we're about to tear down this window and not seek for
2781 // the behind activity, don't use it for orientation
2782 if (!findingBehind
2783 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002784 continue;
2785 }
2786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 if (!haveGroup) {
2788 // We ignore any hidden applications on the top.
2789 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2790 continue;
2791 }
2792 haveGroup = true;
2793 curGroup = wtoken.groupId;
2794 lastOrientation = wtoken.requestedOrientation;
2795 } else if (curGroup != wtoken.groupId) {
2796 // If we have hit a new application group, and the bottom
2797 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002798 // the orientation behind it, and the last app was
2799 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002801 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2802 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 return lastOrientation;
2804 }
2805 }
2806 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002807 // If this application is fullscreen, and didn't explicitly say
2808 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002810 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002811 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002812 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 return or;
2814 }
2815 // If this application has requested an explicit orientation,
2816 // then use it.
2817 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2818 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2819 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2820 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2821 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2822 return or;
2823 }
Owen Lin3413b892009-05-01 17:12:32 -07002824 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 }
2826 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2827 }
Romain Guy06882f82009-06-10 13:36:04 -07002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002830 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002831 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2832 "updateOrientationFromAppTokens()")) {
2833 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2834 }
2835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 Configuration config;
2837 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002838 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2839 freezeThisOneIfNeeded);
2840 Binder.restoreCallingIdentity(ident);
2841 return config;
2842 }
2843
2844 Configuration updateOrientationFromAppTokensUnchecked(
2845 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2846 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002848 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002849 if (config != null) {
2850 mLayoutNeeded = true;
2851 performLayoutAndPlaceSurfacesLocked();
2852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 return config;
2855 }
Romain Guy06882f82009-06-10 13:36:04 -07002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 /*
2858 * The orientation is computed from non-application windows first. If none of
2859 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002860 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2862 * android.os.IBinder)
2863 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002864 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002865 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 long ident = Binder.clearCallingIdentity();
2868 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002869 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 if (req != mForcedAppOrientation) {
2872 changed = true;
2873 mForcedAppOrientation = req;
2874 //send a message to Policy indicating orientation change to take
2875 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002876 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 }
Romain Guy06882f82009-06-10 13:36:04 -07002878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 if (changed) {
2880 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002881 WindowManagerPolicy.USE_LAST_ROTATION,
2882 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 if (changed) {
2884 if (freezeThisOneIfNeeded != null) {
2885 AppWindowToken wtoken = findAppWindowToken(
2886 freezeThisOneIfNeeded);
2887 if (wtoken != null) {
2888 startAppFreezingScreenLocked(wtoken,
2889 ActivityInfo.CONFIG_ORIENTATION);
2890 }
2891 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002892 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 }
2894 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002895
2896 // No obvious action we need to take, but if our current
2897 // state mismatches the activity maanager's, update it
2898 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002899 mTempConfiguration.setToDefaults();
2900 if (computeNewConfigurationLocked(mTempConfiguration)) {
2901 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002902 return new Configuration(mTempConfiguration);
2903 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002904 }
2905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 } finally {
2907 Binder.restoreCallingIdentity(ident);
2908 }
Romain Guy06882f82009-06-10 13:36:04 -07002909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 return null;
2911 }
Romain Guy06882f82009-06-10 13:36:04 -07002912
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002913 int computeForcedAppOrientationLocked() {
2914 int req = getOrientationFromWindowsLocked();
2915 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2916 req = getOrientationFromAppTokensLocked();
2917 }
2918 return req;
2919 }
Romain Guy06882f82009-06-10 13:36:04 -07002920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2922 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2923 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002924 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 }
Romain Guy06882f82009-06-10 13:36:04 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 synchronized(mWindowMap) {
2928 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2929 if (wtoken == null) {
2930 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2931 return;
2932 }
Romain Guy06882f82009-06-10 13:36:04 -07002933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 wtoken.requestedOrientation = requestedOrientation;
2935 }
2936 }
Romain Guy06882f82009-06-10 13:36:04 -07002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 public int getAppOrientation(IApplicationToken token) {
2939 synchronized(mWindowMap) {
2940 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2941 if (wtoken == null) {
2942 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2943 }
Romain Guy06882f82009-06-10 13:36:04 -07002944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 return wtoken.requestedOrientation;
2946 }
2947 }
Romain Guy06882f82009-06-10 13:36:04 -07002948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2950 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2951 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002952 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954
2955 synchronized(mWindowMap) {
2956 boolean changed = false;
2957 if (token == null) {
2958 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2959 changed = mFocusedApp != null;
2960 mFocusedApp = null;
2961 mKeyWaiter.tickle();
2962 } else {
2963 AppWindowToken newFocus = findAppWindowToken(token);
2964 if (newFocus == null) {
2965 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2966 return;
2967 }
2968 changed = mFocusedApp != newFocus;
2969 mFocusedApp = newFocus;
2970 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2971 mKeyWaiter.tickle();
2972 }
2973
2974 if (moveFocusNow && changed) {
2975 final long origId = Binder.clearCallingIdentity();
2976 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2977 Binder.restoreCallingIdentity(origId);
2978 }
2979 }
2980 }
2981
2982 public void prepareAppTransition(int transit) {
2983 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2984 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002985 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
Romain Guy06882f82009-06-10 13:36:04 -07002987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 synchronized(mWindowMap) {
2989 if (DEBUG_APP_TRANSITIONS) Log.v(
2990 TAG, "Prepare app transition: transit=" + transit
2991 + " mNextAppTransition=" + mNextAppTransition);
2992 if (!mDisplayFrozen) {
2993 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2994 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002995 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2996 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2997 // Opening a new task always supersedes a close for the anim.
2998 mNextAppTransition = transit;
2999 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3000 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3001 // Opening a new activity always supersedes a close for the anim.
3002 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 }
3004 mAppTransitionReady = false;
3005 mAppTransitionTimeout = false;
3006 mStartingIconInTransition = false;
3007 mSkipAppTransitionAnimation = false;
3008 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3009 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3010 5000);
3011 }
3012 }
3013 }
3014
3015 public int getPendingAppTransition() {
3016 return mNextAppTransition;
3017 }
Romain Guy06882f82009-06-10 13:36:04 -07003018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 public void executeAppTransition() {
3020 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3021 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003022 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 }
Romain Guy06882f82009-06-10 13:36:04 -07003024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 synchronized(mWindowMap) {
3026 if (DEBUG_APP_TRANSITIONS) Log.v(
3027 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
3028 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3029 mAppTransitionReady = true;
3030 final long origId = Binder.clearCallingIdentity();
3031 performLayoutAndPlaceSurfacesLocked();
3032 Binder.restoreCallingIdentity(origId);
3033 }
3034 }
3035 }
3036
3037 public void setAppStartingWindow(IBinder token, String pkg,
3038 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3039 IBinder transferFrom, boolean createIfNeeded) {
3040 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3041 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003042 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 }
3044
3045 synchronized(mWindowMap) {
3046 if (DEBUG_STARTING_WINDOW) Log.v(
3047 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3048 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 AppWindowToken wtoken = findAppWindowToken(token);
3051 if (wtoken == null) {
3052 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3053 return;
3054 }
3055
3056 // If the display is frozen, we won't do anything until the
3057 // actual window is displayed so there is no reason to put in
3058 // the starting window.
3059 if (mDisplayFrozen) {
3060 return;
3061 }
Romain Guy06882f82009-06-10 13:36:04 -07003062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 if (wtoken.startingData != null) {
3064 return;
3065 }
Romain Guy06882f82009-06-10 13:36:04 -07003066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 if (transferFrom != null) {
3068 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3069 if (ttoken != null) {
3070 WindowState startingWindow = ttoken.startingWindow;
3071 if (startingWindow != null) {
3072 if (mStartingIconInTransition) {
3073 // In this case, the starting icon has already
3074 // been displayed, so start letting windows get
3075 // shown immediately without any more transitions.
3076 mSkipAppTransitionAnimation = true;
3077 }
3078 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3079 "Moving existing starting from " + ttoken
3080 + " to " + wtoken);
3081 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 // Transfer the starting window over to the new
3084 // token.
3085 wtoken.startingData = ttoken.startingData;
3086 wtoken.startingView = ttoken.startingView;
3087 wtoken.startingWindow = startingWindow;
3088 ttoken.startingData = null;
3089 ttoken.startingView = null;
3090 ttoken.startingWindow = null;
3091 ttoken.startingMoved = true;
3092 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003093 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 startingWindow.mAppToken = wtoken;
3095 mWindows.remove(startingWindow);
3096 ttoken.windows.remove(startingWindow);
3097 ttoken.allAppWindows.remove(startingWindow);
3098 addWindowToListInOrderLocked(startingWindow, true);
3099 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 // Propagate other interesting state between the
3102 // tokens. If the old token is displayed, we should
3103 // immediately force the new one to be displayed. If
3104 // it is animating, we need to move that animation to
3105 // the new one.
3106 if (ttoken.allDrawn) {
3107 wtoken.allDrawn = true;
3108 }
3109 if (ttoken.firstWindowDrawn) {
3110 wtoken.firstWindowDrawn = true;
3111 }
3112 if (!ttoken.hidden) {
3113 wtoken.hidden = false;
3114 wtoken.hiddenRequested = false;
3115 wtoken.willBeHidden = false;
3116 }
3117 if (wtoken.clientHidden != ttoken.clientHidden) {
3118 wtoken.clientHidden = ttoken.clientHidden;
3119 wtoken.sendAppVisibilityToClients();
3120 }
3121 if (ttoken.animation != null) {
3122 wtoken.animation = ttoken.animation;
3123 wtoken.animating = ttoken.animating;
3124 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3125 ttoken.animation = null;
3126 ttoken.animLayerAdjustment = 0;
3127 wtoken.updateLayers();
3128 ttoken.updateLayers();
3129 }
Romain Guy06882f82009-06-10 13:36:04 -07003130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 mLayoutNeeded = true;
3133 performLayoutAndPlaceSurfacesLocked();
3134 Binder.restoreCallingIdentity(origId);
3135 return;
3136 } else if (ttoken.startingData != null) {
3137 // The previous app was getting ready to show a
3138 // starting window, but hasn't yet done so. Steal it!
3139 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3140 "Moving pending starting from " + ttoken
3141 + " to " + wtoken);
3142 wtoken.startingData = ttoken.startingData;
3143 ttoken.startingData = null;
3144 ttoken.startingMoved = true;
3145 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3146 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3147 // want to process the message ASAP, before any other queued
3148 // messages.
3149 mH.sendMessageAtFrontOfQueue(m);
3150 return;
3151 }
3152 }
3153 }
3154
3155 // There is no existing starting window, and the caller doesn't
3156 // want us to create one, so that's it!
3157 if (!createIfNeeded) {
3158 return;
3159 }
Romain Guy06882f82009-06-10 13:36:04 -07003160
Dianne Hackborn284ac932009-08-28 10:34:25 -07003161 // If this is a translucent or wallpaper window, then don't
3162 // show a starting window -- the current effect (a full-screen
3163 // opaque starting window that fades away to the real contents
3164 // when it is ready) does not work for this.
3165 if (theme != 0) {
3166 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3167 com.android.internal.R.styleable.Window);
3168 if (ent.array.getBoolean(
3169 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3170 return;
3171 }
3172 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003173 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3174 return;
3175 }
3176 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003177 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3178 return;
3179 }
3180 }
3181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 mStartingIconInTransition = true;
3183 wtoken.startingData = new StartingData(
3184 pkg, theme, nonLocalizedLabel,
3185 labelRes, icon);
3186 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3187 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3188 // want to process the message ASAP, before any other queued
3189 // messages.
3190 mH.sendMessageAtFrontOfQueue(m);
3191 }
3192 }
3193
3194 public void setAppWillBeHidden(IBinder token) {
3195 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3196 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003197 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 }
3199
3200 AppWindowToken wtoken;
3201
3202 synchronized(mWindowMap) {
3203 wtoken = findAppWindowToken(token);
3204 if (wtoken == null) {
3205 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3206 return;
3207 }
3208 wtoken.willBeHidden = true;
3209 }
3210 }
Romain Guy06882f82009-06-10 13:36:04 -07003211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3213 boolean visible, int transit, boolean performLayout) {
3214 boolean delayed = false;
3215
3216 if (wtoken.clientHidden == visible) {
3217 wtoken.clientHidden = !visible;
3218 wtoken.sendAppVisibilityToClients();
3219 }
Romain Guy06882f82009-06-10 13:36:04 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 wtoken.willBeHidden = false;
3222 if (wtoken.hidden == visible) {
3223 final int N = wtoken.allAppWindows.size();
3224 boolean changed = false;
3225 if (DEBUG_APP_TRANSITIONS) Log.v(
3226 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3227 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3232 if (wtoken.animation == sDummyAnimation) {
3233 wtoken.animation = null;
3234 }
3235 applyAnimationLocked(wtoken, lp, transit, visible);
3236 changed = true;
3237 if (wtoken.animation != null) {
3238 delayed = runningAppAnimation = true;
3239 }
3240 }
Romain Guy06882f82009-06-10 13:36:04 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 for (int i=0; i<N; i++) {
3243 WindowState win = wtoken.allAppWindows.get(i);
3244 if (win == wtoken.startingWindow) {
3245 continue;
3246 }
3247
3248 if (win.isAnimating()) {
3249 delayed = true;
3250 }
Romain Guy06882f82009-06-10 13:36:04 -07003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3253 //win.dump(" ");
3254 if (visible) {
3255 if (!win.isVisibleNow()) {
3256 if (!runningAppAnimation) {
3257 applyAnimationLocked(win,
3258 WindowManagerPolicy.TRANSIT_ENTER, true);
3259 }
3260 changed = true;
3261 }
3262 } else if (win.isVisibleNow()) {
3263 if (!runningAppAnimation) {
3264 applyAnimationLocked(win,
3265 WindowManagerPolicy.TRANSIT_EXIT, false);
3266 }
3267 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3268 KeyWaiter.RETURN_NOTHING);
3269 changed = true;
3270 }
3271 }
3272
3273 wtoken.hidden = wtoken.hiddenRequested = !visible;
3274 if (!visible) {
3275 unsetAppFreezingScreenLocked(wtoken, true, true);
3276 } else {
3277 // If we are being set visible, and the starting window is
3278 // not yet displayed, then make sure it doesn't get displayed.
3279 WindowState swin = wtoken.startingWindow;
3280 if (swin != null && (swin.mDrawPending
3281 || swin.mCommitDrawPending)) {
3282 swin.mPolicyVisibility = false;
3283 swin.mPolicyVisibilityAfterAnim = false;
3284 }
3285 }
Romain Guy06882f82009-06-10 13:36:04 -07003286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3288 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3289 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 if (changed && performLayout) {
3292 mLayoutNeeded = true;
3293 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 performLayoutAndPlaceSurfacesLocked();
3295 }
3296 }
3297
3298 if (wtoken.animation != null) {
3299 delayed = true;
3300 }
Romain Guy06882f82009-06-10 13:36:04 -07003301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 return delayed;
3303 }
3304
3305 public void setAppVisibility(IBinder token, boolean visible) {
3306 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3307 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003308 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 }
3310
3311 AppWindowToken wtoken;
3312
3313 synchronized(mWindowMap) {
3314 wtoken = findAppWindowToken(token);
3315 if (wtoken == null) {
3316 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3317 return;
3318 }
3319
3320 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3321 RuntimeException e = new RuntimeException();
3322 e.fillInStackTrace();
3323 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3324 + "): mNextAppTransition=" + mNextAppTransition
3325 + " hidden=" + wtoken.hidden
3326 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3327 }
Romain Guy06882f82009-06-10 13:36:04 -07003328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 // If we are preparing an app transition, then delay changing
3330 // the visibility of this token until we execute that transition.
3331 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3332 // Already in requested state, don't do anything more.
3333 if (wtoken.hiddenRequested != visible) {
3334 return;
3335 }
3336 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 if (DEBUG_APP_TRANSITIONS) Log.v(
3339 TAG, "Setting dummy animation on: " + wtoken);
3340 wtoken.setDummyAnimation();
3341 mOpeningApps.remove(wtoken);
3342 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003343 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 wtoken.inPendingTransaction = true;
3345 if (visible) {
3346 mOpeningApps.add(wtoken);
3347 wtoken.allDrawn = false;
3348 wtoken.startingDisplayed = false;
3349 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003350 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 if (wtoken.clientHidden) {
3353 // In the case where we are making an app visible
3354 // but holding off for a transition, we still need
3355 // to tell the client to make its windows visible so
3356 // they get drawn. Otherwise, we will wait on
3357 // performing the transition until all windows have
3358 // been drawn, they never will be, and we are sad.
3359 wtoken.clientHidden = false;
3360 wtoken.sendAppVisibilityToClients();
3361 }
3362 } else {
3363 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003364 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 }
3366 return;
3367 }
Romain Guy06882f82009-06-10 13:36:04 -07003368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 final long origId = Binder.clearCallingIdentity();
3370 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3371 wtoken.updateReportedVisibilityLocked();
3372 Binder.restoreCallingIdentity(origId);
3373 }
3374 }
3375
3376 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3377 boolean unfreezeSurfaceNow, boolean force) {
3378 if (wtoken.freezingScreen) {
3379 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3380 + " force=" + force);
3381 final int N = wtoken.allAppWindows.size();
3382 boolean unfrozeWindows = false;
3383 for (int i=0; i<N; i++) {
3384 WindowState w = wtoken.allAppWindows.get(i);
3385 if (w.mAppFreezing) {
3386 w.mAppFreezing = false;
3387 if (w.mSurface != null && !w.mOrientationChanging) {
3388 w.mOrientationChanging = true;
3389 }
3390 unfrozeWindows = true;
3391 }
3392 }
3393 if (force || unfrozeWindows) {
3394 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3395 wtoken.freezingScreen = false;
3396 mAppsFreezingScreen--;
3397 }
3398 if (unfreezeSurfaceNow) {
3399 if (unfrozeWindows) {
3400 mLayoutNeeded = true;
3401 performLayoutAndPlaceSurfacesLocked();
3402 }
3403 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3404 stopFreezingDisplayLocked();
3405 }
3406 }
3407 }
3408 }
Romain Guy06882f82009-06-10 13:36:04 -07003409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3411 int configChanges) {
3412 if (DEBUG_ORIENTATION) {
3413 RuntimeException e = new RuntimeException();
3414 e.fillInStackTrace();
3415 Log.i(TAG, "Set freezing of " + wtoken.appToken
3416 + ": hidden=" + wtoken.hidden + " freezing="
3417 + wtoken.freezingScreen, e);
3418 }
3419 if (!wtoken.hiddenRequested) {
3420 if (!wtoken.freezingScreen) {
3421 wtoken.freezingScreen = true;
3422 mAppsFreezingScreen++;
3423 if (mAppsFreezingScreen == 1) {
3424 startFreezingDisplayLocked();
3425 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3426 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3427 5000);
3428 }
3429 }
3430 final int N = wtoken.allAppWindows.size();
3431 for (int i=0; i<N; i++) {
3432 WindowState w = wtoken.allAppWindows.get(i);
3433 w.mAppFreezing = true;
3434 }
3435 }
3436 }
Romain Guy06882f82009-06-10 13:36:04 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 public void startAppFreezingScreen(IBinder token, int configChanges) {
3439 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3440 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003441 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 }
3443
3444 synchronized(mWindowMap) {
3445 if (configChanges == 0 && !mDisplayFrozen) {
3446 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3447 return;
3448 }
Romain Guy06882f82009-06-10 13:36:04 -07003449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 AppWindowToken wtoken = findAppWindowToken(token);
3451 if (wtoken == null || wtoken.appToken == null) {
3452 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3453 return;
3454 }
3455 final long origId = Binder.clearCallingIdentity();
3456 startAppFreezingScreenLocked(wtoken, configChanges);
3457 Binder.restoreCallingIdentity(origId);
3458 }
3459 }
Romain Guy06882f82009-06-10 13:36:04 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 public void stopAppFreezingScreen(IBinder token, boolean force) {
3462 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3463 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003464 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 }
3466
3467 synchronized(mWindowMap) {
3468 AppWindowToken wtoken = findAppWindowToken(token);
3469 if (wtoken == null || wtoken.appToken == null) {
3470 return;
3471 }
3472 final long origId = Binder.clearCallingIdentity();
3473 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3474 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3475 unsetAppFreezingScreenLocked(wtoken, true, force);
3476 Binder.restoreCallingIdentity(origId);
3477 }
3478 }
Romain Guy06882f82009-06-10 13:36:04 -07003479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 public void removeAppToken(IBinder token) {
3481 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3482 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003483 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 }
3485
3486 AppWindowToken wtoken = null;
3487 AppWindowToken startingToken = null;
3488 boolean delayed = false;
3489
3490 final long origId = Binder.clearCallingIdentity();
3491 synchronized(mWindowMap) {
3492 WindowToken basewtoken = mTokenMap.remove(token);
3493 mTokenList.remove(basewtoken);
3494 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3495 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3496 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3497 wtoken.inPendingTransaction = false;
3498 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003499 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 if (mClosingApps.contains(wtoken)) {
3501 delayed = true;
3502 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3503 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003504 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 delayed = true;
3506 }
3507 if (DEBUG_APP_TRANSITIONS) Log.v(
3508 TAG, "Removing app " + wtoken + " delayed=" + delayed
3509 + " animation=" + wtoken.animation
3510 + " animating=" + wtoken.animating);
3511 if (delayed) {
3512 // set the token aside because it has an active animation to be finished
3513 mExitingAppTokens.add(wtoken);
3514 }
3515 mAppTokens.remove(wtoken);
3516 wtoken.removed = true;
3517 if (wtoken.startingData != null) {
3518 startingToken = wtoken;
3519 }
3520 unsetAppFreezingScreenLocked(wtoken, true, true);
3521 if (mFocusedApp == wtoken) {
3522 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3523 mFocusedApp = null;
3524 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3525 mKeyWaiter.tickle();
3526 }
3527 } else {
3528 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3529 }
Romain Guy06882f82009-06-10 13:36:04 -07003530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 if (!delayed && wtoken != null) {
3532 wtoken.updateReportedVisibilityLocked();
3533 }
3534 }
3535 Binder.restoreCallingIdentity(origId);
3536
3537 if (startingToken != null) {
3538 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3539 + startingToken + ": app token removed");
3540 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3541 mH.sendMessage(m);
3542 }
3543 }
3544
3545 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3546 final int NW = token.windows.size();
3547 for (int i=0; i<NW; i++) {
3548 WindowState win = token.windows.get(i);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003549 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 mWindows.remove(win);
3551 int j = win.mChildWindows.size();
3552 while (j > 0) {
3553 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003554 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3555 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3556 "Tmp removing child window " + cwin);
3557 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 }
3559 }
3560 return NW > 0;
3561 }
3562
3563 void dumpAppTokensLocked() {
3564 for (int i=mAppTokens.size()-1; i>=0; i--) {
3565 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3566 }
3567 }
Romain Guy06882f82009-06-10 13:36:04 -07003568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 void dumpWindowsLocked() {
3570 for (int i=mWindows.size()-1; i>=0; i--) {
3571 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3572 }
3573 }
Romain Guy06882f82009-06-10 13:36:04 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 private int findWindowOffsetLocked(int tokenPos) {
3576 final int NW = mWindows.size();
3577
3578 if (tokenPos >= mAppTokens.size()) {
3579 int i = NW;
3580 while (i > 0) {
3581 i--;
3582 WindowState win = (WindowState)mWindows.get(i);
3583 if (win.getAppToken() != null) {
3584 return i+1;
3585 }
3586 }
3587 }
3588
3589 while (tokenPos > 0) {
3590 // Find the first app token below the new position that has
3591 // a window displayed.
3592 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3593 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3594 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003595 if (wtoken.sendingToBottom) {
3596 if (DEBUG_REORDER) Log.v(TAG,
3597 "Skipping token -- currently sending to bottom");
3598 tokenPos--;
3599 continue;
3600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 int i = wtoken.windows.size();
3602 while (i > 0) {
3603 i--;
3604 WindowState win = wtoken.windows.get(i);
3605 int j = win.mChildWindows.size();
3606 while (j > 0) {
3607 j--;
3608 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003609 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 for (int pos=NW-1; pos>=0; pos--) {
3611 if (mWindows.get(pos) == cwin) {
3612 if (DEBUG_REORDER) Log.v(TAG,
3613 "Found child win @" + (pos+1));
3614 return pos+1;
3615 }
3616 }
3617 }
3618 }
3619 for (int pos=NW-1; pos>=0; pos--) {
3620 if (mWindows.get(pos) == win) {
3621 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3622 return pos+1;
3623 }
3624 }
3625 }
3626 tokenPos--;
3627 }
3628
3629 return 0;
3630 }
3631
3632 private final int reAddWindowLocked(int index, WindowState win) {
3633 final int NCW = win.mChildWindows.size();
3634 boolean added = false;
3635 for (int j=0; j<NCW; j++) {
3636 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3637 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003638 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3639 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 mWindows.add(index, win);
3641 index++;
3642 added = true;
3643 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003644 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3645 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 mWindows.add(index, cwin);
3647 index++;
3648 }
3649 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003650 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3651 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 mWindows.add(index, win);
3653 index++;
3654 }
3655 return index;
3656 }
Romain Guy06882f82009-06-10 13:36:04 -07003657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3659 final int NW = token.windows.size();
3660 for (int i=0; i<NW; i++) {
3661 index = reAddWindowLocked(index, token.windows.get(i));
3662 }
3663 return index;
3664 }
3665
3666 public void moveAppToken(int index, IBinder token) {
3667 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3668 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003669 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 }
3671
3672 synchronized(mWindowMap) {
3673 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3674 if (DEBUG_REORDER) dumpAppTokensLocked();
3675 final AppWindowToken wtoken = findAppWindowToken(token);
3676 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3677 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3678 + token + " (" + wtoken + ")");
3679 return;
3680 }
3681 mAppTokens.add(index, wtoken);
3682 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3683 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 final long origId = Binder.clearCallingIdentity();
3686 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3687 if (DEBUG_REORDER) dumpWindowsLocked();
3688 if (tmpRemoveAppWindowsLocked(wtoken)) {
3689 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3690 if (DEBUG_REORDER) dumpWindowsLocked();
3691 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3692 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3693 if (DEBUG_REORDER) dumpWindowsLocked();
3694 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 mLayoutNeeded = true;
3696 performLayoutAndPlaceSurfacesLocked();
3697 }
3698 Binder.restoreCallingIdentity(origId);
3699 }
3700 }
3701
3702 private void removeAppTokensLocked(List<IBinder> tokens) {
3703 // XXX This should be done more efficiently!
3704 // (take advantage of the fact that both lists should be
3705 // ordered in the same way.)
3706 int N = tokens.size();
3707 for (int i=0; i<N; i++) {
3708 IBinder token = tokens.get(i);
3709 final AppWindowToken wtoken = findAppWindowToken(token);
3710 if (!mAppTokens.remove(wtoken)) {
3711 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3712 + token + " (" + wtoken + ")");
3713 i--;
3714 N--;
3715 }
3716 }
3717 }
3718
Dianne Hackborna8f60182009-09-01 19:01:50 -07003719 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3720 boolean updateFocusAndLayout) {
3721 // First remove all of the windows from the list.
3722 tmpRemoveAppWindowsLocked(wtoken);
3723
3724 // Where to start adding?
3725 int pos = findWindowOffsetLocked(tokenPos);
3726
3727 // And now add them back at the correct place.
3728 pos = reAddAppWindowsLocked(pos, wtoken);
3729
3730 if (updateFocusAndLayout) {
3731 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3732 assignLayersLocked();
3733 }
3734 mLayoutNeeded = true;
3735 performLayoutAndPlaceSurfacesLocked();
3736 }
3737 }
3738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3740 // First remove all of the windows from the list.
3741 final int N = tokens.size();
3742 int i;
3743 for (i=0; i<N; i++) {
3744 WindowToken token = mTokenMap.get(tokens.get(i));
3745 if (token != null) {
3746 tmpRemoveAppWindowsLocked(token);
3747 }
3748 }
3749
3750 // Where to start adding?
3751 int pos = findWindowOffsetLocked(tokenPos);
3752
3753 // And now add them back at the correct place.
3754 for (i=0; i<N; i++) {
3755 WindowToken token = mTokenMap.get(tokens.get(i));
3756 if (token != null) {
3757 pos = reAddAppWindowsLocked(pos, token);
3758 }
3759 }
3760
Dianne Hackborna8f60182009-09-01 19:01:50 -07003761 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3762 assignLayersLocked();
3763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 mLayoutNeeded = true;
3765 performLayoutAndPlaceSurfacesLocked();
3766
3767 //dump();
3768 }
3769
3770 public void moveAppTokensToTop(List<IBinder> tokens) {
3771 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3772 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003773 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 }
3775
3776 final long origId = Binder.clearCallingIdentity();
3777 synchronized(mWindowMap) {
3778 removeAppTokensLocked(tokens);
3779 final int N = tokens.size();
3780 for (int i=0; i<N; i++) {
3781 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3782 if (wt != null) {
3783 mAppTokens.add(wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003784 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3785 mToTopApps.remove(wt);
3786 mToBottomApps.remove(wt);
3787 mToTopApps.add(wt);
3788 wt.sendingToBottom = false;
3789 wt.sendingToTop = true;
3790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003793
3794 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3795 moveAppWindowsLocked(tokens, mAppTokens.size());
3796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 }
3798 Binder.restoreCallingIdentity(origId);
3799 }
3800
3801 public void moveAppTokensToBottom(List<IBinder> tokens) {
3802 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3803 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003804 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
3806
3807 final long origId = Binder.clearCallingIdentity();
3808 synchronized(mWindowMap) {
3809 removeAppTokensLocked(tokens);
3810 final int N = tokens.size();
3811 int pos = 0;
3812 for (int i=0; i<N; i++) {
3813 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3814 if (wt != null) {
3815 mAppTokens.add(pos, wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003816 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3817 mToTopApps.remove(wt);
3818 mToBottomApps.remove(wt);
3819 mToBottomApps.add(i, wt);
3820 wt.sendingToTop = false;
3821 wt.sendingToBottom = true;
3822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 pos++;
3824 }
3825 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003826
3827 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3828 moveAppWindowsLocked(tokens, 0);
3829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 }
3831 Binder.restoreCallingIdentity(origId);
3832 }
3833
3834 // -------------------------------------------------------------
3835 // Misc IWindowSession methods
3836 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 public void disableKeyguard(IBinder token, String tag) {
3839 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3840 != PackageManager.PERMISSION_GRANTED) {
3841 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3842 }
3843 mKeyguardDisabled.acquire(token, tag);
3844 }
3845
3846 public void reenableKeyguard(IBinder token) {
3847 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3848 != PackageManager.PERMISSION_GRANTED) {
3849 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3850 }
3851 synchronized (mKeyguardDisabled) {
3852 mKeyguardDisabled.release(token);
3853
3854 if (!mKeyguardDisabled.isAcquired()) {
3855 // if we are the last one to reenable the keyguard wait until
3856 // we have actaully finished reenabling until returning
3857 mWaitingUntilKeyguardReenabled = true;
3858 while (mWaitingUntilKeyguardReenabled) {
3859 try {
3860 mKeyguardDisabled.wait();
3861 } catch (InterruptedException e) {
3862 Thread.currentThread().interrupt();
3863 }
3864 }
3865 }
3866 }
3867 }
3868
3869 /**
3870 * @see android.app.KeyguardManager#exitKeyguardSecurely
3871 */
3872 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3873 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3874 != PackageManager.PERMISSION_GRANTED) {
3875 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3876 }
3877 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3878 public void onKeyguardExitResult(boolean success) {
3879 try {
3880 callback.onKeyguardExitResult(success);
3881 } catch (RemoteException e) {
3882 // Client has died, we don't care.
3883 }
3884 }
3885 });
3886 }
3887
3888 public boolean inKeyguardRestrictedInputMode() {
3889 return mPolicy.inKeyguardRestrictedKeyInputMode();
3890 }
Romain Guy06882f82009-06-10 13:36:04 -07003891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 static float fixScale(float scale) {
3893 if (scale < 0) scale = 0;
3894 else if (scale > 20) scale = 20;
3895 return Math.abs(scale);
3896 }
Romain Guy06882f82009-06-10 13:36:04 -07003897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 public void setAnimationScale(int which, float scale) {
3899 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3900 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003901 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903
3904 if (scale < 0) scale = 0;
3905 else if (scale > 20) scale = 20;
3906 scale = Math.abs(scale);
3907 switch (which) {
3908 case 0: mWindowAnimationScale = fixScale(scale); break;
3909 case 1: mTransitionAnimationScale = fixScale(scale); break;
3910 }
Romain Guy06882f82009-06-10 13:36:04 -07003911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 // Persist setting
3913 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3914 }
Romain Guy06882f82009-06-10 13:36:04 -07003915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 public void setAnimationScales(float[] scales) {
3917 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3918 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003919 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 }
3921
3922 if (scales != null) {
3923 if (scales.length >= 1) {
3924 mWindowAnimationScale = fixScale(scales[0]);
3925 }
3926 if (scales.length >= 2) {
3927 mTransitionAnimationScale = fixScale(scales[1]);
3928 }
3929 }
Romain Guy06882f82009-06-10 13:36:04 -07003930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 // Persist setting
3932 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3933 }
Romain Guy06882f82009-06-10 13:36:04 -07003934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 public float getAnimationScale(int which) {
3936 switch (which) {
3937 case 0: return mWindowAnimationScale;
3938 case 1: return mTransitionAnimationScale;
3939 }
3940 return 0;
3941 }
Romain Guy06882f82009-06-10 13:36:04 -07003942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 public float[] getAnimationScales() {
3944 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3945 }
Romain Guy06882f82009-06-10 13:36:04 -07003946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 public int getSwitchState(int sw) {
3948 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3949 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003950 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 }
3952 return KeyInputQueue.getSwitchState(sw);
3953 }
Romain Guy06882f82009-06-10 13:36:04 -07003954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 public int getSwitchStateForDevice(int devid, int sw) {
3956 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3957 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003958 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
3960 return KeyInputQueue.getSwitchState(devid, sw);
3961 }
Romain Guy06882f82009-06-10 13:36:04 -07003962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 public int getScancodeState(int sw) {
3964 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3965 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003966 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 }
3968 return KeyInputQueue.getScancodeState(sw);
3969 }
Romain Guy06882f82009-06-10 13:36:04 -07003970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 public int getScancodeStateForDevice(int devid, int sw) {
3972 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3973 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003974 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 }
3976 return KeyInputQueue.getScancodeState(devid, sw);
3977 }
Romain Guy06882f82009-06-10 13:36:04 -07003978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 public int getKeycodeState(int sw) {
3980 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3981 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003982 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 }
3984 return KeyInputQueue.getKeycodeState(sw);
3985 }
Romain Guy06882f82009-06-10 13:36:04 -07003986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 public int getKeycodeStateForDevice(int devid, int sw) {
3988 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3989 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003990 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 }
3992 return KeyInputQueue.getKeycodeState(devid, sw);
3993 }
Romain Guy06882f82009-06-10 13:36:04 -07003994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3996 return KeyInputQueue.hasKeys(keycodes, keyExists);
3997 }
Romain Guy06882f82009-06-10 13:36:04 -07003998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 public void enableScreenAfterBoot() {
4000 synchronized(mWindowMap) {
4001 if (mSystemBooted) {
4002 return;
4003 }
4004 mSystemBooted = true;
4005 }
Romain Guy06882f82009-06-10 13:36:04 -07004006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 performEnableScreen();
4008 }
Romain Guy06882f82009-06-10 13:36:04 -07004009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 public void enableScreenIfNeededLocked() {
4011 if (mDisplayEnabled) {
4012 return;
4013 }
4014 if (!mSystemBooted) {
4015 return;
4016 }
4017 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4018 }
Romain Guy06882f82009-06-10 13:36:04 -07004019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 public void performEnableScreen() {
4021 synchronized(mWindowMap) {
4022 if (mDisplayEnabled) {
4023 return;
4024 }
4025 if (!mSystemBooted) {
4026 return;
4027 }
Romain Guy06882f82009-06-10 13:36:04 -07004028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 // Don't enable the screen until all existing windows
4030 // have been drawn.
4031 final int N = mWindows.size();
4032 for (int i=0; i<N; i++) {
4033 WindowState w = (WindowState)mWindows.get(i);
4034 if (w.isVisibleLw() && !w.isDisplayedLw()) {
4035 return;
4036 }
4037 }
Romain Guy06882f82009-06-10 13:36:04 -07004038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 mDisplayEnabled = true;
4040 if (false) {
4041 Log.i(TAG, "ENABLING SCREEN!");
4042 StringWriter sw = new StringWriter();
4043 PrintWriter pw = new PrintWriter(sw);
4044 this.dump(null, pw, null);
4045 Log.i(TAG, sw.toString());
4046 }
4047 try {
4048 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4049 if (surfaceFlinger != null) {
4050 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4051 Parcel data = Parcel.obtain();
4052 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4053 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4054 data, null, 0);
4055 data.recycle();
4056 }
4057 } catch (RemoteException ex) {
4058 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4059 }
4060 }
Romain Guy06882f82009-06-10 13:36:04 -07004061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004065 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4066 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 }
Romain Guy06882f82009-06-10 13:36:04 -07004068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 public void setInTouchMode(boolean mode) {
4070 synchronized(mWindowMap) {
4071 mInTouchMode = mode;
4072 }
4073 }
4074
Romain Guy06882f82009-06-10 13:36:04 -07004075 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004076 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004078 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004079 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 }
4081
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004082 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 }
Romain Guy06882f82009-06-10 13:36:04 -07004084
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004085 public void setRotationUnchecked(int rotation,
4086 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 if(DEBUG_ORIENTATION) Log.v(TAG,
4088 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 long origId = Binder.clearCallingIdentity();
4091 boolean changed;
4092 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004093 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
Romain Guy06882f82009-06-10 13:36:04 -07004095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 if (changed) {
4097 sendNewConfiguration();
4098 synchronized(mWindowMap) {
4099 mLayoutNeeded = true;
4100 performLayoutAndPlaceSurfacesLocked();
4101 }
4102 } else if (alwaysSendConfiguration) {
4103 //update configuration ignoring orientation change
4104 sendNewConfiguration();
4105 }
Romain Guy06882f82009-06-10 13:36:04 -07004106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 Binder.restoreCallingIdentity(origId);
4108 }
Romain Guy06882f82009-06-10 13:36:04 -07004109
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004110 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 boolean changed;
4112 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4113 rotation = mRequestedRotation;
4114 } else {
4115 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004116 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 }
4118 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004119 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 mRotation, mDisplayEnabled);
4121 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4122 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004125 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 "Rotation changed to " + rotation
4127 + " from " + mRotation
4128 + " (forceApp=" + mForcedAppOrientation
4129 + ", req=" + mRequestedRotation + ")");
4130 mRotation = rotation;
4131 mWindowsFreezingScreen = true;
4132 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4133 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4134 2000);
4135 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004136 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 mQueue.setOrientation(rotation);
4138 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004139 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 }
4141 for (int i=mWindows.size()-1; i>=0; i--) {
4142 WindowState w = (WindowState)mWindows.get(i);
4143 if (w.mSurface != null) {
4144 w.mOrientationChanging = true;
4145 }
4146 }
4147 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4148 try {
4149 mRotationWatchers.get(i).onRotationChanged(rotation);
4150 } catch (RemoteException e) {
4151 }
4152 }
4153 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 return changed;
4156 }
Romain Guy06882f82009-06-10 13:36:04 -07004157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 public int getRotation() {
4159 return mRotation;
4160 }
4161
4162 public int watchRotation(IRotationWatcher watcher) {
4163 final IBinder watcherBinder = watcher.asBinder();
4164 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4165 public void binderDied() {
4166 synchronized (mWindowMap) {
4167 for (int i=0; i<mRotationWatchers.size(); i++) {
4168 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004169 IRotationWatcher removed = mRotationWatchers.remove(i);
4170 if (removed != null) {
4171 removed.asBinder().unlinkToDeath(this, 0);
4172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 i--;
4174 }
4175 }
4176 }
4177 }
4178 };
Romain Guy06882f82009-06-10 13:36:04 -07004179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 synchronized (mWindowMap) {
4181 try {
4182 watcher.asBinder().linkToDeath(dr, 0);
4183 mRotationWatchers.add(watcher);
4184 } catch (RemoteException e) {
4185 // Client died, no cleanup needed.
4186 }
Romain Guy06882f82009-06-10 13:36:04 -07004187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 return mRotation;
4189 }
4190 }
4191
4192 /**
4193 * Starts the view server on the specified port.
4194 *
4195 * @param port The port to listener to.
4196 *
4197 * @return True if the server was successfully started, false otherwise.
4198 *
4199 * @see com.android.server.ViewServer
4200 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4201 */
4202 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004203 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 return false;
4205 }
4206
4207 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4208 return false;
4209 }
4210
4211 if (port < 1024) {
4212 return false;
4213 }
4214
4215 if (mViewServer != null) {
4216 if (!mViewServer.isRunning()) {
4217 try {
4218 return mViewServer.start();
4219 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004220 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
4222 }
4223 return false;
4224 }
4225
4226 try {
4227 mViewServer = new ViewServer(this, port);
4228 return mViewServer.start();
4229 } catch (IOException e) {
4230 Log.w(TAG, "View server did not start");
4231 }
4232 return false;
4233 }
4234
Romain Guy06882f82009-06-10 13:36:04 -07004235 private boolean isSystemSecure() {
4236 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4237 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4238 }
4239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 /**
4241 * Stops the view server if it exists.
4242 *
4243 * @return True if the server stopped, false if it wasn't started or
4244 * couldn't be stopped.
4245 *
4246 * @see com.android.server.ViewServer
4247 */
4248 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004249 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 return false;
4251 }
4252
4253 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4254 return false;
4255 }
4256
4257 if (mViewServer != null) {
4258 return mViewServer.stop();
4259 }
4260 return false;
4261 }
4262
4263 /**
4264 * Indicates whether the view server is running.
4265 *
4266 * @return True if the server is running, false otherwise.
4267 *
4268 * @see com.android.server.ViewServer
4269 */
4270 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004271 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 return false;
4273 }
4274
4275 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4276 return false;
4277 }
4278
4279 return mViewServer != null && mViewServer.isRunning();
4280 }
4281
4282 /**
4283 * Lists all availble windows in the system. The listing is written in the
4284 * specified Socket's output stream with the following syntax:
4285 * windowHashCodeInHexadecimal windowName
4286 * Each line of the ouput represents a different window.
4287 *
4288 * @param client The remote client to send the listing to.
4289 * @return False if an error occured, true otherwise.
4290 */
4291 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004292 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 return false;
4294 }
4295
4296 boolean result = true;
4297
4298 Object[] windows;
4299 synchronized (mWindowMap) {
4300 windows = new Object[mWindows.size()];
4301 //noinspection unchecked
4302 windows = mWindows.toArray(windows);
4303 }
4304
4305 BufferedWriter out = null;
4306
4307 // Any uncaught exception will crash the system process
4308 try {
4309 OutputStream clientStream = client.getOutputStream();
4310 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4311
4312 final int count = windows.length;
4313 for (int i = 0; i < count; i++) {
4314 final WindowState w = (WindowState) windows[i];
4315 out.write(Integer.toHexString(System.identityHashCode(w)));
4316 out.write(' ');
4317 out.append(w.mAttrs.getTitle());
4318 out.write('\n');
4319 }
4320
4321 out.write("DONE.\n");
4322 out.flush();
4323 } catch (Exception e) {
4324 result = false;
4325 } finally {
4326 if (out != null) {
4327 try {
4328 out.close();
4329 } catch (IOException e) {
4330 result = false;
4331 }
4332 }
4333 }
4334
4335 return result;
4336 }
4337
4338 /**
4339 * Sends a command to a target window. The result of the command, if any, will be
4340 * written in the output stream of the specified socket.
4341 *
4342 * The parameters must follow this syntax:
4343 * windowHashcode extra
4344 *
4345 * Where XX is the length in characeters of the windowTitle.
4346 *
4347 * The first parameter is the target window. The window with the specified hashcode
4348 * will be the target. If no target can be found, nothing happens. The extra parameters
4349 * will be delivered to the target window and as parameters to the command itself.
4350 *
4351 * @param client The remote client to sent the result, if any, to.
4352 * @param command The command to execute.
4353 * @param parameters The command parameters.
4354 *
4355 * @return True if the command was successfully delivered, false otherwise. This does
4356 * not indicate whether the command itself was successful.
4357 */
4358 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004359 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 return false;
4361 }
4362
4363 boolean success = true;
4364 Parcel data = null;
4365 Parcel reply = null;
4366
4367 // Any uncaught exception will crash the system process
4368 try {
4369 // Find the hashcode of the window
4370 int index = parameters.indexOf(' ');
4371 if (index == -1) {
4372 index = parameters.length();
4373 }
4374 final String code = parameters.substring(0, index);
4375 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4376
4377 // Extract the command's parameter after the window description
4378 if (index < parameters.length()) {
4379 parameters = parameters.substring(index + 1);
4380 } else {
4381 parameters = "";
4382 }
4383
4384 final WindowManagerService.WindowState window = findWindow(hashCode);
4385 if (window == null) {
4386 return false;
4387 }
4388
4389 data = Parcel.obtain();
4390 data.writeInterfaceToken("android.view.IWindow");
4391 data.writeString(command);
4392 data.writeString(parameters);
4393 data.writeInt(1);
4394 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4395
4396 reply = Parcel.obtain();
4397
4398 final IBinder binder = window.mClient.asBinder();
4399 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4400 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4401
4402 reply.readException();
4403
4404 } catch (Exception e) {
4405 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4406 success = false;
4407 } finally {
4408 if (data != null) {
4409 data.recycle();
4410 }
4411 if (reply != null) {
4412 reply.recycle();
4413 }
4414 }
4415
4416 return success;
4417 }
4418
4419 private WindowState findWindow(int hashCode) {
4420 if (hashCode == -1) {
4421 return getFocusedWindow();
4422 }
4423
4424 synchronized (mWindowMap) {
4425 final ArrayList windows = mWindows;
4426 final int count = windows.size();
4427
4428 for (int i = 0; i < count; i++) {
4429 WindowState w = (WindowState) windows.get(i);
4430 if (System.identityHashCode(w) == hashCode) {
4431 return w;
4432 }
4433 }
4434 }
4435
4436 return null;
4437 }
4438
4439 /*
4440 * Instruct the Activity Manager to fetch the current configuration and broadcast
4441 * that to config-changed listeners if appropriate.
4442 */
4443 void sendNewConfiguration() {
4444 try {
4445 mActivityManager.updateConfiguration(null);
4446 } catch (RemoteException e) {
4447 }
4448 }
Romain Guy06882f82009-06-10 13:36:04 -07004449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 public Configuration computeNewConfiguration() {
4451 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004452 return computeNewConfigurationLocked();
4453 }
4454 }
Romain Guy06882f82009-06-10 13:36:04 -07004455
Dianne Hackbornc485a602009-03-24 22:39:49 -07004456 Configuration computeNewConfigurationLocked() {
4457 Configuration config = new Configuration();
4458 if (!computeNewConfigurationLocked(config)) {
4459 return null;
4460 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004461 return config;
4462 }
Romain Guy06882f82009-06-10 13:36:04 -07004463
Dianne Hackbornc485a602009-03-24 22:39:49 -07004464 boolean computeNewConfigurationLocked(Configuration config) {
4465 if (mDisplay == null) {
4466 return false;
4467 }
4468 mQueue.getInputConfiguration(config);
4469 final int dw = mDisplay.getWidth();
4470 final int dh = mDisplay.getHeight();
4471 int orientation = Configuration.ORIENTATION_SQUARE;
4472 if (dw < dh) {
4473 orientation = Configuration.ORIENTATION_PORTRAIT;
4474 } else if (dw > dh) {
4475 orientation = Configuration.ORIENTATION_LANDSCAPE;
4476 }
4477 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004478
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004479 DisplayMetrics dm = new DisplayMetrics();
4480 mDisplay.getMetrics(dm);
4481 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4482
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004483 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004484 // Note we only do this once because at this point we don't
4485 // expect the screen to change in this way at runtime, and want
4486 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004487 int longSize = dw;
4488 int shortSize = dh;
4489 if (longSize < shortSize) {
4490 int tmp = longSize;
4491 longSize = shortSize;
4492 shortSize = tmp;
4493 }
4494 longSize = (int)(longSize/dm.density);
4495 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004496
Dianne Hackborn723738c2009-06-25 19:48:04 -07004497 // These semi-magic numbers define our compatibility modes for
4498 // applications with different screens. Don't change unless you
4499 // make sure to test lots and lots of apps!
4500 if (longSize < 470) {
4501 // This is shorter than an HVGA normal density screen (which
4502 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004503 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4504 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004505 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004506 // Is this a large screen?
4507 if (longSize > 640 && shortSize >= 480) {
4508 // VGA or larger screens at medium density are the point
4509 // at which we consider it to be a large screen.
4510 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4511 } else {
4512 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4513
4514 // If this screen is wider than normal HVGA, or taller
4515 // than FWVGA, then for old apps we want to run in size
4516 // compatibility mode.
4517 if (shortSize > 321 || longSize > 570) {
4518 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4519 }
4520 }
4521
4522 // Is this a long screen?
4523 if (((longSize*3)/5) >= (shortSize-1)) {
4524 // Anything wider than WVGA (5:3) is considering to be long.
4525 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4526 } else {
4527 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4528 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004529 }
4530 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004531 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004532
Dianne Hackbornc485a602009-03-24 22:39:49 -07004533 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4534 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4535 mPolicy.adjustConfigurationLw(config);
4536 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 }
Romain Guy06882f82009-06-10 13:36:04 -07004538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 // -------------------------------------------------------------
4540 // Input Events and Focus Management
4541 // -------------------------------------------------------------
4542
4543 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004544 long curTime = SystemClock.uptimeMillis();
4545
Michael Chane10de972009-05-18 11:24:50 -07004546 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004547 if (mLastTouchEventType == eventType &&
4548 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4549 return;
4550 }
4551 mLastUserActivityCallTime = curTime;
4552 mLastTouchEventType = eventType;
4553 }
4554
4555 if (targetWin == null
4556 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4557 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 }
4559 }
4560
4561 // tells if it's a cheek event or not -- this function is stateful
4562 private static final int EVENT_NONE = 0;
4563 private static final int EVENT_UNKNOWN = 0;
4564 private static final int EVENT_CHEEK = 0;
4565 private static final int EVENT_IGNORE_DURATION = 300; // ms
4566 private static final float CHEEK_THRESHOLD = 0.6f;
4567 private int mEventState = EVENT_NONE;
4568 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 private int eventType(MotionEvent ev) {
4571 float size = ev.getSize();
4572 switch (ev.getAction()) {
4573 case MotionEvent.ACTION_DOWN:
4574 mEventSize = size;
4575 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4576 case MotionEvent.ACTION_UP:
4577 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004578 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 case MotionEvent.ACTION_MOVE:
4580 final int N = ev.getHistorySize();
4581 if (size > mEventSize) mEventSize = size;
4582 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4583 for (int i=0; i<N; i++) {
4584 size = ev.getHistoricalSize(i);
4585 if (size > mEventSize) mEventSize = size;
4586 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4587 }
4588 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4589 return TOUCH_EVENT;
4590 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004591 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 }
4593 default:
4594 // not good
4595 return OTHER_EVENT;
4596 }
4597 }
4598
4599 /**
4600 * @return Returns true if event was dispatched, false if it was dropped for any reason
4601 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004602 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4604 "dispatchPointer " + ev);
4605
Michael Chan53071d62009-05-13 17:29:48 -07004606 if (MEASURE_LATENCY) {
4607 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4608 }
4609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004611 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612
Michael Chan53071d62009-05-13 17:29:48 -07004613 if (MEASURE_LATENCY) {
4614 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4615 }
4616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 if (action == MotionEvent.ACTION_UP) {
4620 // let go of our target
4621 mKeyWaiter.mMotionTarget = null;
4622 mPowerManager.logPointerUpEvent();
4623 } else if (action == MotionEvent.ACTION_DOWN) {
4624 mPowerManager.logPointerDownEvent();
4625 }
4626
4627 if (targetObj == null) {
4628 // In this case we are either dropping the event, or have received
4629 // a move or up without a down. It is common to receive move
4630 // events in such a way, since this means the user is moving the
4631 // pointer without actually pressing down. All other cases should
4632 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004633 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004634 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4635 }
4636 if (qev != null) {
4637 mQueue.recycleEvent(qev);
4638 }
4639 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004640 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 }
4642 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4643 if (qev != null) {
4644 mQueue.recycleEvent(qev);
4645 }
4646 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004647 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 }
Romain Guy06882f82009-06-10 13:36:04 -07004649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004653 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654
4655 //Log.i(TAG, "Sending " + ev + " to " + target);
4656
4657 if (uid != 0 && uid != target.mSession.mUid) {
4658 if (mContext.checkPermission(
4659 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4660 != PackageManager.PERMISSION_GRANTED) {
4661 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4662 + pid + " uid " + uid + " to window " + target
4663 + " owned by uid " + target.mSession.mUid);
4664 if (qev != null) {
4665 mQueue.recycleEvent(qev);
4666 }
4667 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004668 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 }
4670 }
4671
Michael Chan53071d62009-05-13 17:29:48 -07004672 if (MEASURE_LATENCY) {
4673 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4674 }
4675
Romain Guy06882f82009-06-10 13:36:04 -07004676 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4678 //target wants to ignore fat touch events
4679 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4680 //explicit flag to return without processing event further
4681 boolean returnFlag = false;
4682 if((action == MotionEvent.ACTION_DOWN)) {
4683 mFatTouch = false;
4684 if(cheekPress) {
4685 mFatTouch = true;
4686 returnFlag = true;
4687 }
4688 } else {
4689 if(action == MotionEvent.ACTION_UP) {
4690 if(mFatTouch) {
4691 //earlier even was invalid doesnt matter if current up is cheekpress or not
4692 mFatTouch = false;
4693 returnFlag = true;
4694 } else if(cheekPress) {
4695 //cancel the earlier event
4696 ev.setAction(MotionEvent.ACTION_CANCEL);
4697 action = MotionEvent.ACTION_CANCEL;
4698 }
4699 } else if(action == MotionEvent.ACTION_MOVE) {
4700 if(mFatTouch) {
4701 //two cases here
4702 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004703 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 returnFlag = true;
4705 } else if(cheekPress) {
4706 //valid down followed by invalid moves
4707 //an invalid move have to cancel earlier action
4708 ev.setAction(MotionEvent.ACTION_CANCEL);
4709 action = MotionEvent.ACTION_CANCEL;
4710 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4711 //note that the subsequent invalid moves will not get here
4712 mFatTouch = true;
4713 }
4714 }
4715 } //else if action
4716 if(returnFlag) {
4717 //recycle que, ev
4718 if (qev != null) {
4719 mQueue.recycleEvent(qev);
4720 }
4721 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004722 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 }
4724 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004725
Michael Chan9f028e62009-08-04 17:37:46 -07004726 // Enable this for testing the "right" value
4727 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004728 int max_events_per_sec = 35;
4729 try {
4730 max_events_per_sec = Integer.parseInt(SystemProperties
4731 .get("windowsmgr.max_events_per_sec"));
4732 if (max_events_per_sec < 1) {
4733 max_events_per_sec = 35;
4734 }
4735 } catch (NumberFormatException e) {
4736 }
4737 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4738 }
4739
4740 /*
4741 * Throttle events to minimize CPU usage when there's a flood of events
4742 * e.g. constant contact with the screen
4743 */
4744 if (action == MotionEvent.ACTION_MOVE) {
4745 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4746 long now = SystemClock.uptimeMillis();
4747 if (now < nextEventTime) {
4748 try {
4749 Thread.sleep(nextEventTime - now);
4750 } catch (InterruptedException e) {
4751 }
4752 mLastTouchEventTime = nextEventTime;
4753 } else {
4754 mLastTouchEventTime = now;
4755 }
4756 }
4757
Michael Chan53071d62009-05-13 17:29:48 -07004758 if (MEASURE_LATENCY) {
4759 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4760 }
4761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 synchronized(mWindowMap) {
4763 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4764 mKeyWaiter.bindTargetWindowLocked(target,
4765 KeyWaiter.RETURN_PENDING_POINTER, qev);
4766 ev = null;
4767 } else {
4768 if (action == MotionEvent.ACTION_DOWN) {
4769 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4770 if (out != null) {
4771 MotionEvent oev = MotionEvent.obtain(ev);
4772 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4773 do {
4774 final Rect frame = out.mFrame;
4775 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4776 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004777 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 } catch (android.os.RemoteException e) {
4779 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4780 }
4781 oev.offsetLocation((float)frame.left, (float)frame.top);
4782 out = out.mNextOutsideTouch;
4783 } while (out != null);
4784 mKeyWaiter.mOutsideTouchTargets = null;
4785 }
4786 }
4787 final Rect frame = target.mFrame;
4788 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4789 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004790
4791 // If we are on top of the wallpaper, then the wallpaper also
4792 // gets to see this movement.
4793 if (mWallpaperTarget == target) {
4794 sendPointerToWallpaperLocked(target, ev, eventTime);
4795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 }
4797 }
Romain Guy06882f82009-06-10 13:36:04 -07004798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 // finally offset the event to the target's coordinate system and
4800 // dispatch the event.
4801 try {
4802 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4803 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4804 }
Michael Chan53071d62009-05-13 17:29:48 -07004805
4806 if (MEASURE_LATENCY) {
4807 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4808 }
4809
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004810 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004811
4812 if (MEASURE_LATENCY) {
4813 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4814 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004815 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 } catch (android.os.RemoteException e) {
4817 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4818 mKeyWaiter.mMotionTarget = null;
4819 try {
4820 removeWindow(target.mSession, target.mClient);
4821 } catch (java.util.NoSuchElementException ex) {
4822 // This will happen if the window has already been
4823 // removed.
4824 }
4825 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004826 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 }
Romain Guy06882f82009-06-10 13:36:04 -07004828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 /**
4830 * @return Returns true if event was dispatched, false if it was dropped for any reason
4831 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004832 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 if (DEBUG_INPUT) Log.v(
4834 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004837 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 if (focusObj == null) {
4839 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4840 if (qev != null) {
4841 mQueue.recycleEvent(qev);
4842 }
4843 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004844 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845 }
4846 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4847 if (qev != null) {
4848 mQueue.recycleEvent(qev);
4849 }
4850 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004851 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 }
Romain Guy06882f82009-06-10 13:36:04 -07004853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004856 if (uid != 0 && uid != focus.mSession.mUid) {
4857 if (mContext.checkPermission(
4858 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4859 != PackageManager.PERMISSION_GRANTED) {
4860 Log.w(TAG, "Permission denied: injecting key event from pid "
4861 + pid + " uid " + uid + " to window " + focus
4862 + " owned by uid " + focus.mSession.mUid);
4863 if (qev != null) {
4864 mQueue.recycleEvent(qev);
4865 }
4866 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004867 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 }
4869 }
Romain Guy06882f82009-06-10 13:36:04 -07004870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 synchronized(mWindowMap) {
4874 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4875 mKeyWaiter.bindTargetWindowLocked(focus,
4876 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4877 // We don't deliver movement events to the client, we hold
4878 // them and wait for them to call back.
4879 ev = null;
4880 } else {
4881 mKeyWaiter.bindTargetWindowLocked(focus);
4882 }
4883 }
Romain Guy06882f82009-06-10 13:36:04 -07004884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004886 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004887 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 } catch (android.os.RemoteException e) {
4889 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4890 try {
4891 removeWindow(focus.mSession, focus.mClient);
4892 } catch (java.util.NoSuchElementException ex) {
4893 // This will happen if the window has already been
4894 // removed.
4895 }
4896 }
Romain Guy06882f82009-06-10 13:36:04 -07004897
Dianne Hackborncfaef692009-06-15 14:24:44 -07004898 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 }
Romain Guy06882f82009-06-10 13:36:04 -07004900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 /**
4902 * @return Returns true if event was dispatched, false if it was dropped for any reason
4903 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004904 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4906
4907 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004908 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 if (focusObj == null) {
4910 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004911 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 }
4913 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004914 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 }
Romain Guy06882f82009-06-10 13:36:04 -07004916
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004917 // Okay we have finished waiting for the last event to be processed.
4918 // First off, if this is a repeat event, check to see if there is
4919 // a corresponding up event in the queue. If there is, we will
4920 // just drop the repeat, because it makes no sense to repeat after
4921 // the user has released a key. (This is especially important for
4922 // long presses.)
4923 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
4924 return INJECT_SUCCEEDED;
4925 }
4926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 if (DEBUG_INPUT) Log.v(
4930 TAG, "Dispatching to " + focus + ": " + event);
4931
4932 if (uid != 0 && uid != focus.mSession.mUid) {
4933 if (mContext.checkPermission(
4934 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4935 != PackageManager.PERMISSION_GRANTED) {
4936 Log.w(TAG, "Permission denied: injecting key event from pid "
4937 + pid + " uid " + uid + " to window " + focus
4938 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004939 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 }
4941 }
Romain Guy06882f82009-06-10 13:36:04 -07004942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 synchronized(mWindowMap) {
4944 mKeyWaiter.bindTargetWindowLocked(focus);
4945 }
4946
4947 // NOSHIP extra state logging
4948 mKeyWaiter.recordDispatchState(event, focus);
4949 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 try {
4952 if (DEBUG_INPUT || DEBUG_FOCUS) {
4953 Log.v(TAG, "Delivering key " + event.getKeyCode()
4954 + " to " + focus);
4955 }
4956 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004957 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 } catch (android.os.RemoteException e) {
4959 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4960 try {
4961 removeWindow(focus.mSession, focus.mClient);
4962 } catch (java.util.NoSuchElementException ex) {
4963 // This will happen if the window has already been
4964 // removed.
4965 }
4966 }
Romain Guy06882f82009-06-10 13:36:04 -07004967
Dianne Hackborncfaef692009-06-15 14:24:44 -07004968 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 }
Romain Guy06882f82009-06-10 13:36:04 -07004970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 public void pauseKeyDispatching(IBinder _token) {
4972 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4973 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004974 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 }
4976
4977 synchronized (mWindowMap) {
4978 WindowToken token = mTokenMap.get(_token);
4979 if (token != null) {
4980 mKeyWaiter.pauseDispatchingLocked(token);
4981 }
4982 }
4983 }
4984
4985 public void resumeKeyDispatching(IBinder _token) {
4986 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4987 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004988 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 }
4990
4991 synchronized (mWindowMap) {
4992 WindowToken token = mTokenMap.get(_token);
4993 if (token != null) {
4994 mKeyWaiter.resumeDispatchingLocked(token);
4995 }
4996 }
4997 }
4998
4999 public void setEventDispatching(boolean enabled) {
5000 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5001 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005002 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003 }
5004
5005 synchronized (mWindowMap) {
5006 mKeyWaiter.setEventDispatchingLocked(enabled);
5007 }
5008 }
Romain Guy06882f82009-06-10 13:36:04 -07005009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 /**
5011 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005012 *
5013 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 * {@link SystemClock#uptimeMillis()} as the timebase.)
5015 * @param sync If true, wait for the event to be completed before returning to the caller.
5016 * @return Returns true if event was dispatched, false if it was dropped for any reason
5017 */
5018 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5019 long downTime = ev.getDownTime();
5020 long eventTime = ev.getEventTime();
5021
5022 int action = ev.getAction();
5023 int code = ev.getKeyCode();
5024 int repeatCount = ev.getRepeatCount();
5025 int metaState = ev.getMetaState();
5026 int deviceId = ev.getDeviceId();
5027 int scancode = ev.getScanCode();
5028
5029 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5030 if (downTime == 0) downTime = eventTime;
5031
5032 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005033 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005035 final int pid = Binder.getCallingPid();
5036 final int uid = Binder.getCallingUid();
5037 final long ident = Binder.clearCallingIdentity();
5038 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005040 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005042 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005043 switch (result) {
5044 case INJECT_NO_PERMISSION:
5045 throw new SecurityException(
5046 "Injecting to another application requires INJECT_EVENT permission");
5047 case INJECT_SUCCEEDED:
5048 return true;
5049 }
5050 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 }
5052
5053 /**
5054 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005055 *
5056 * @param ev A motion event describing the pointer (touch) action. (As noted in
5057 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 * {@link SystemClock#uptimeMillis()} as the timebase.)
5059 * @param sync If true, wait for the event to be completed before returning to the caller.
5060 * @return Returns true if event was dispatched, false if it was dropped for any reason
5061 */
5062 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005063 final int pid = Binder.getCallingPid();
5064 final int uid = Binder.getCallingUid();
5065 final long ident = Binder.clearCallingIdentity();
5066 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005068 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005070 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005071 switch (result) {
5072 case INJECT_NO_PERMISSION:
5073 throw new SecurityException(
5074 "Injecting to another application requires INJECT_EVENT permission");
5075 case INJECT_SUCCEEDED:
5076 return true;
5077 }
5078 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 }
Romain Guy06882f82009-06-10 13:36:04 -07005080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 /**
5082 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005083 *
5084 * @param ev A motion event describing the trackball action. (As noted in
5085 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 * {@link SystemClock#uptimeMillis()} as the timebase.)
5087 * @param sync If true, wait for the event to be completed before returning to the caller.
5088 * @return Returns true if event was dispatched, false if it was dropped for any reason
5089 */
5090 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005091 final int pid = Binder.getCallingPid();
5092 final int uid = Binder.getCallingUid();
5093 final long ident = Binder.clearCallingIdentity();
5094 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005096 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005098 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005099 switch (result) {
5100 case INJECT_NO_PERMISSION:
5101 throw new SecurityException(
5102 "Injecting to another application requires INJECT_EVENT permission");
5103 case INJECT_SUCCEEDED:
5104 return true;
5105 }
5106 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 }
Romain Guy06882f82009-06-10 13:36:04 -07005108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 private WindowState getFocusedWindow() {
5110 synchronized (mWindowMap) {
5111 return getFocusedWindowLocked();
5112 }
5113 }
5114
5115 private WindowState getFocusedWindowLocked() {
5116 return mCurrentFocus;
5117 }
Romain Guy06882f82009-06-10 13:36:04 -07005118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 /**
5120 * This class holds the state for dispatching key events. This state
5121 * is protected by the KeyWaiter instance, NOT by the window lock. You
5122 * can be holding the main window lock while acquire the KeyWaiter lock,
5123 * but not the other way around.
5124 */
5125 final class KeyWaiter {
5126 // NOSHIP debugging
5127 public class DispatchState {
5128 private KeyEvent event;
5129 private WindowState focus;
5130 private long time;
5131 private WindowState lastWin;
5132 private IBinder lastBinder;
5133 private boolean finished;
5134 private boolean gotFirstWindow;
5135 private boolean eventDispatching;
5136 private long timeToSwitch;
5137 private boolean wasFrozen;
5138 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005139 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5142 focus = theFocus;
5143 event = theEvent;
5144 time = System.currentTimeMillis();
5145 // snapshot KeyWaiter state
5146 lastWin = mLastWin;
5147 lastBinder = mLastBinder;
5148 finished = mFinished;
5149 gotFirstWindow = mGotFirstWindow;
5150 eventDispatching = mEventDispatching;
5151 timeToSwitch = mTimeToSwitch;
5152 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005153 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 // cache the paused state at ctor time as well
5155 if (theFocus == null || theFocus.mToken == null) {
5156 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5157 focusPaused = false;
5158 } else {
5159 focusPaused = theFocus.mToken.paused;
5160 }
5161 }
Romain Guy06882f82009-06-10 13:36:04 -07005162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163 public String toString() {
5164 return "{{" + event + " to " + focus + " @ " + time
5165 + " lw=" + lastWin + " lb=" + lastBinder
5166 + " fin=" + finished + " gfw=" + gotFirstWindow
5167 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005168 + " wf=" + wasFrozen + " fp=" + focusPaused
5169 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 }
5171 };
5172 private DispatchState mDispatchState = null;
5173 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5174 mDispatchState = new DispatchState(theEvent, theFocus);
5175 }
5176 // END NOSHIP
5177
5178 public static final int RETURN_NOTHING = 0;
5179 public static final int RETURN_PENDING_POINTER = 1;
5180 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 final Object SKIP_TARGET_TOKEN = new Object();
5183 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 private WindowState mLastWin = null;
5186 private IBinder mLastBinder = null;
5187 private boolean mFinished = true;
5188 private boolean mGotFirstWindow = false;
5189 private boolean mEventDispatching = true;
5190 private long mTimeToSwitch = 0;
5191 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 // Target of Motion events
5194 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 // Windows above the target who would like to receive an "outside"
5197 // touch event for any down events outside of them.
5198 WindowState mOutsideTouchTargets;
5199
5200 /**
5201 * Wait for the last event dispatch to complete, then find the next
5202 * target that should receive the given event and wait for that one
5203 * to be ready to receive it.
5204 */
5205 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5206 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005207 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 long startTime = SystemClock.uptimeMillis();
5209 long keyDispatchingTimeout = 5 * 1000;
5210 long waitedFor = 0;
5211
5212 while (true) {
5213 // Figure out which window we care about. It is either the
5214 // last window we are waiting to have process the event or,
5215 // if none, then the next window we think the event should go
5216 // to. Note: we retrieve mLastWin outside of the lock, so
5217 // it may change before we lock. Thus we must check it again.
5218 WindowState targetWin = mLastWin;
5219 boolean targetIsNew = targetWin == null;
5220 if (DEBUG_INPUT) Log.v(
5221 TAG, "waitForLastKey: mFinished=" + mFinished +
5222 ", mLastWin=" + mLastWin);
5223 if (targetIsNew) {
5224 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005225 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 if (target == SKIP_TARGET_TOKEN) {
5227 // The user has pressed a special key, and we are
5228 // dropping all pending events before it.
5229 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5230 + " " + nextMotion);
5231 return null;
5232 }
5233 if (target == CONSUMED_EVENT_TOKEN) {
5234 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5235 + " " + nextMotion);
5236 return target;
5237 }
5238 targetWin = (WindowState)target;
5239 }
Romain Guy06882f82009-06-10 13:36:04 -07005240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 // Now: is it okay to send the next event to this window?
5244 synchronized (this) {
5245 // First: did we come here based on the last window not
5246 // being null, but it changed by the time we got here?
5247 // If so, try again.
5248 if (!targetIsNew && mLastWin == null) {
5249 continue;
5250 }
Romain Guy06882f82009-06-10 13:36:04 -07005251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 // We never dispatch events if not finished with the
5253 // last one, or the display is frozen.
5254 if (mFinished && !mDisplayFrozen) {
5255 // If event dispatching is disabled, then we
5256 // just consume the events.
5257 if (!mEventDispatching) {
5258 if (DEBUG_INPUT) Log.v(TAG,
5259 "Skipping event; dispatching disabled: "
5260 + nextKey + " " + nextMotion);
5261 return null;
5262 }
5263 if (targetWin != null) {
5264 // If this is a new target, and that target is not
5265 // paused or unresponsive, then all looks good to
5266 // handle the event.
5267 if (targetIsNew && !targetWin.mToken.paused) {
5268 return targetWin;
5269 }
Romain Guy06882f82009-06-10 13:36:04 -07005270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 // If we didn't find a target window, and there is no
5272 // focused app window, then just eat the events.
5273 } else if (mFocusedApp == null) {
5274 if (DEBUG_INPUT) Log.v(TAG,
5275 "Skipping event; no focused app: "
5276 + nextKey + " " + nextMotion);
5277 return null;
5278 }
5279 }
Romain Guy06882f82009-06-10 13:36:04 -07005280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 if (DEBUG_INPUT) Log.v(
5282 TAG, "Waiting for last key in " + mLastBinder
5283 + " target=" + targetWin
5284 + " mFinished=" + mFinished
5285 + " mDisplayFrozen=" + mDisplayFrozen
5286 + " targetIsNew=" + targetIsNew
5287 + " paused="
5288 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005289 + " mFocusedApp=" + mFocusedApp
5290 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 targetApp = targetWin != null
5293 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 long curTimeout = keyDispatchingTimeout;
5296 if (mTimeToSwitch != 0) {
5297 long now = SystemClock.uptimeMillis();
5298 if (mTimeToSwitch <= now) {
5299 // If an app switch key has been pressed, and we have
5300 // waited too long for the current app to finish
5301 // processing keys, then wait no more!
5302 doFinishedKeyLocked(true);
5303 continue;
5304 }
5305 long switchTimeout = mTimeToSwitch - now;
5306 if (curTimeout > switchTimeout) {
5307 curTimeout = switchTimeout;
5308 }
5309 }
Romain Guy06882f82009-06-10 13:36:04 -07005310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 try {
5312 // after that continue
5313 // processing keys, so we don't get stuck.
5314 if (DEBUG_INPUT) Log.v(
5315 TAG, "Waiting for key dispatch: " + curTimeout);
5316 wait(curTimeout);
5317 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5318 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005319 + startTime + " switchTime=" + mTimeToSwitch
5320 + " target=" + targetWin + " mLW=" + mLastWin
5321 + " mLB=" + mLastBinder + " fin=" + mFinished
5322 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 } catch (InterruptedException e) {
5324 }
5325 }
5326
5327 // If we were frozen during configuration change, restart the
5328 // timeout checks from now; otherwise look at whether we timed
5329 // out before awakening.
5330 if (mWasFrozen) {
5331 waitedFor = 0;
5332 mWasFrozen = false;
5333 } else {
5334 waitedFor = SystemClock.uptimeMillis() - startTime;
5335 }
5336
5337 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5338 IApplicationToken at = null;
5339 synchronized (this) {
5340 Log.w(TAG, "Key dispatching timed out sending to " +
5341 (targetWin != null ? targetWin.mAttrs.getTitle()
5342 : "<null>"));
5343 // NOSHIP debugging
5344 Log.w(TAG, "Dispatch state: " + mDispatchState);
5345 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5346 // END NOSHIP
5347 //dump();
5348 if (targetWin != null) {
5349 at = targetWin.getAppToken();
5350 } else if (targetApp != null) {
5351 at = targetApp.appToken;
5352 }
5353 }
5354
5355 boolean abort = true;
5356 if (at != null) {
5357 try {
5358 long timeout = at.getKeyDispatchingTimeout();
5359 if (timeout > waitedFor) {
5360 // we did not wait the proper amount of time for this application.
5361 // set the timeout to be the real timeout and wait again.
5362 keyDispatchingTimeout = timeout - waitedFor;
5363 continue;
5364 } else {
5365 abort = at.keyDispatchingTimedOut();
5366 }
5367 } catch (RemoteException ex) {
5368 }
5369 }
5370
5371 synchronized (this) {
5372 if (abort && (mLastWin == targetWin || targetWin == null)) {
5373 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005374 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 if (DEBUG_INPUT) Log.v(TAG,
5376 "Window " + mLastWin +
5377 " timed out on key input");
5378 if (mLastWin.mToken.paused) {
5379 Log.w(TAG, "Un-pausing dispatching to this window");
5380 mLastWin.mToken.paused = false;
5381 }
5382 }
5383 if (mMotionTarget == targetWin) {
5384 mMotionTarget = null;
5385 }
5386 mLastWin = null;
5387 mLastBinder = null;
5388 if (failIfTimeout || targetWin == null) {
5389 return null;
5390 }
5391 } else {
5392 Log.w(TAG, "Continuing to wait for key to be dispatched");
5393 startTime = SystemClock.uptimeMillis();
5394 }
5395 }
5396 }
5397 }
5398 }
Romain Guy06882f82009-06-10 13:36:04 -07005399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005401 MotionEvent nextMotion, boolean isPointerEvent,
5402 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 if (nextKey != null) {
5406 // Find the target window for a normal key event.
5407 final int keycode = nextKey.getKeyCode();
5408 final int repeatCount = nextKey.getRepeatCount();
5409 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5410 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005413 if (callingUid == 0 ||
5414 mContext.checkPermission(
5415 android.Manifest.permission.INJECT_EVENTS,
5416 callingPid, callingUid)
5417 == PackageManager.PERMISSION_GRANTED) {
5418 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005419 nextKey.getMetaState(), down, repeatCount,
5420 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 Log.w(TAG, "Event timeout during app switch: dropping "
5423 + nextKey);
5424 return SKIP_TARGET_TOKEN;
5425 }
Romain Guy06882f82009-06-10 13:36:04 -07005426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 WindowState focus = null;
5430 synchronized(mWindowMap) {
5431 focus = getFocusedWindowLocked();
5432 }
Romain Guy06882f82009-06-10 13:36:04 -07005433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005435
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005436 if (callingUid == 0 ||
5437 (focus != null && callingUid == focus.mSession.mUid) ||
5438 mContext.checkPermission(
5439 android.Manifest.permission.INJECT_EVENTS,
5440 callingPid, callingUid)
5441 == PackageManager.PERMISSION_GRANTED) {
5442 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005443 keycode, nextKey.getMetaState(), down, repeatCount,
5444 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005445 return CONSUMED_EVENT_TOKEN;
5446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 }
Romain Guy06882f82009-06-10 13:36:04 -07005448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 } else if (!isPointerEvent) {
5452 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5453 if (!dispatch) {
5454 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5455 + nextMotion);
5456 return SKIP_TARGET_TOKEN;
5457 }
Romain Guy06882f82009-06-10 13:36:04 -07005458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 WindowState focus = null;
5460 synchronized(mWindowMap) {
5461 focus = getFocusedWindowLocked();
5462 }
Romain Guy06882f82009-06-10 13:36:04 -07005463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5465 return focus;
5466 }
Romain Guy06882f82009-06-10 13:36:04 -07005467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 if (nextMotion == null) {
5469 return SKIP_TARGET_TOKEN;
5470 }
Romain Guy06882f82009-06-10 13:36:04 -07005471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5473 KeyEvent.KEYCODE_UNKNOWN);
5474 if (!dispatch) {
5475 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5476 + nextMotion);
5477 return SKIP_TARGET_TOKEN;
5478 }
Romain Guy06882f82009-06-10 13:36:04 -07005479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 // Find the target window for a pointer event.
5481 int action = nextMotion.getAction();
5482 final float xf = nextMotion.getX();
5483 final float yf = nextMotion.getY();
5484 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 final boolean screenWasOff = qev != null
5487 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 synchronized(mWindowMap) {
5492 synchronized (this) {
5493 if (action == MotionEvent.ACTION_DOWN) {
5494 if (mMotionTarget != null) {
5495 // this is weird, we got a pen down, but we thought it was
5496 // already down!
5497 // XXX: We should probably send an ACTION_UP to the current
5498 // target.
5499 Log.w(TAG, "Pointer down received while already down in: "
5500 + mMotionTarget);
5501 mMotionTarget = null;
5502 }
Romain Guy06882f82009-06-10 13:36:04 -07005503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 // ACTION_DOWN is special, because we need to lock next events to
5505 // the window we'll land onto.
5506 final int x = (int)xf;
5507 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 final ArrayList windows = mWindows;
5510 final int N = windows.size();
5511 WindowState topErrWindow = null;
5512 final Rect tmpRect = mTempRect;
5513 for (int i=N-1; i>=0; i--) {
5514 WindowState child = (WindowState)windows.get(i);
5515 //Log.i(TAG, "Checking dispatch to: " + child);
5516 final int flags = child.mAttrs.flags;
5517 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5518 if (topErrWindow == null) {
5519 topErrWindow = child;
5520 }
5521 }
5522 if (!child.isVisibleLw()) {
5523 //Log.i(TAG, "Not visible!");
5524 continue;
5525 }
5526 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5527 //Log.i(TAG, "Not touchable!");
5528 if ((flags & WindowManager.LayoutParams
5529 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5530 child.mNextOutsideTouch = mOutsideTouchTargets;
5531 mOutsideTouchTargets = child;
5532 }
5533 continue;
5534 }
5535 tmpRect.set(child.mFrame);
5536 if (child.mTouchableInsets == ViewTreeObserver
5537 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5538 // The touch is inside of the window if it is
5539 // inside the frame, AND the content part of that
5540 // frame that was given by the application.
5541 tmpRect.left += child.mGivenContentInsets.left;
5542 tmpRect.top += child.mGivenContentInsets.top;
5543 tmpRect.right -= child.mGivenContentInsets.right;
5544 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5545 } else if (child.mTouchableInsets == ViewTreeObserver
5546 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5547 // The touch is inside of the window if it is
5548 // inside the frame, AND the visible part of that
5549 // frame that was given by the application.
5550 tmpRect.left += child.mGivenVisibleInsets.left;
5551 tmpRect.top += child.mGivenVisibleInsets.top;
5552 tmpRect.right -= child.mGivenVisibleInsets.right;
5553 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5554 }
5555 final int touchFlags = flags &
5556 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5557 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5558 if (tmpRect.contains(x, y) || touchFlags == 0) {
5559 //Log.i(TAG, "Using this target!");
5560 if (!screenWasOff || (flags &
5561 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5562 mMotionTarget = child;
5563 } else {
5564 //Log.i(TAG, "Waking, skip!");
5565 mMotionTarget = null;
5566 }
5567 break;
5568 }
Romain Guy06882f82009-06-10 13:36:04 -07005569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 if ((flags & WindowManager.LayoutParams
5571 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5572 child.mNextOutsideTouch = mOutsideTouchTargets;
5573 mOutsideTouchTargets = child;
5574 //Log.i(TAG, "Adding to outside target list: " + child);
5575 }
5576 }
5577
5578 // if there's an error window but it's not accepting
5579 // focus (typically because it is not yet visible) just
5580 // wait for it -- any other focused window may in fact
5581 // be in ANR state.
5582 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5583 mMotionTarget = null;
5584 }
5585 }
Romain Guy06882f82009-06-10 13:36:04 -07005586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005587 target = mMotionTarget;
5588 }
5589 }
Romain Guy06882f82009-06-10 13:36:04 -07005590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 // Pointer events are a little different -- if there isn't a
5594 // target found for any event, then just drop it.
5595 return target != null ? target : SKIP_TARGET_TOKEN;
5596 }
Romain Guy06882f82009-06-10 13:36:04 -07005597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 boolean checkShouldDispatchKey(int keycode) {
5599 synchronized (this) {
5600 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5601 mTimeToSwitch = 0;
5602 return true;
5603 }
5604 if (mTimeToSwitch != 0
5605 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5606 return false;
5607 }
5608 return true;
5609 }
5610 }
Romain Guy06882f82009-06-10 13:36:04 -07005611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 void bindTargetWindowLocked(WindowState win,
5613 int pendingWhat, QueuedEvent pendingMotion) {
5614 synchronized (this) {
5615 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5616 }
5617 }
Romain Guy06882f82009-06-10 13:36:04 -07005618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 void bindTargetWindowLocked(WindowState win) {
5620 synchronized (this) {
5621 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5622 }
5623 }
5624
5625 void bindTargetWindowLockedLocked(WindowState win,
5626 int pendingWhat, QueuedEvent pendingMotion) {
5627 mLastWin = win;
5628 mLastBinder = win.mClient.asBinder();
5629 mFinished = false;
5630 if (pendingMotion != null) {
5631 final Session s = win.mSession;
5632 if (pendingWhat == RETURN_PENDING_POINTER) {
5633 releasePendingPointerLocked(s);
5634 s.mPendingPointerMove = pendingMotion;
5635 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005636 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 "bindTargetToWindow " + s.mPendingPointerMove);
5638 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5639 releasePendingTrackballLocked(s);
5640 s.mPendingTrackballMove = pendingMotion;
5641 s.mPendingTrackballWindow = win;
5642 }
5643 }
5644 }
Romain Guy06882f82009-06-10 13:36:04 -07005645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 void releasePendingPointerLocked(Session s) {
5647 if (DEBUG_INPUT) Log.v(TAG,
5648 "releasePendingPointer " + s.mPendingPointerMove);
5649 if (s.mPendingPointerMove != null) {
5650 mQueue.recycleEvent(s.mPendingPointerMove);
5651 s.mPendingPointerMove = null;
5652 }
5653 }
Romain Guy06882f82009-06-10 13:36:04 -07005654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 void releasePendingTrackballLocked(Session s) {
5656 if (s.mPendingTrackballMove != null) {
5657 mQueue.recycleEvent(s.mPendingTrackballMove);
5658 s.mPendingTrackballMove = null;
5659 }
5660 }
Romain Guy06882f82009-06-10 13:36:04 -07005661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5663 int returnWhat) {
5664 if (DEBUG_INPUT) Log.v(
5665 TAG, "finishedKey: client=" + client + ", force=" + force);
5666
5667 if (client == null) {
5668 return null;
5669 }
5670
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005671 MotionEvent res = null;
5672 QueuedEvent qev = null;
5673 WindowState win = null;
5674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 synchronized (this) {
5676 if (DEBUG_INPUT) Log.v(
5677 TAG, "finishedKey: client=" + client.asBinder()
5678 + ", force=" + force + ", last=" + mLastBinder
5679 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 if (returnWhat == RETURN_PENDING_POINTER) {
5682 qev = session.mPendingPointerMove;
5683 win = session.mPendingPointerWindow;
5684 session.mPendingPointerMove = null;
5685 session.mPendingPointerWindow = null;
5686 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5687 qev = session.mPendingTrackballMove;
5688 win = session.mPendingTrackballWindow;
5689 session.mPendingTrackballMove = null;
5690 session.mPendingTrackballWindow = null;
5691 }
Romain Guy06882f82009-06-10 13:36:04 -07005692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 if (mLastBinder == client.asBinder()) {
5694 if (DEBUG_INPUT) Log.v(
5695 TAG, "finishedKey: last paused="
5696 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5697 if (mLastWin != null && (!mLastWin.mToken.paused || force
5698 || !mEventDispatching)) {
5699 doFinishedKeyLocked(false);
5700 } else {
5701 // Make sure to wake up anyone currently waiting to
5702 // dispatch a key, so they can re-evaluate their
5703 // current situation.
5704 mFinished = true;
5705 notifyAll();
5706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 }
Romain Guy06882f82009-06-10 13:36:04 -07005708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005710 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005711 if (DEBUG_INPUT) Log.v(TAG,
5712 "Returning pending motion: " + res);
5713 mQueue.recycleEvent(qev);
5714 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5715 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005719
5720 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5721 synchronized (mWindowMap) {
5722 if (mWallpaperTarget == win) {
5723 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5724 }
5725 }
5726 }
5727
5728 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 }
5730
5731 void tickle() {
5732 synchronized (this) {
5733 notifyAll();
5734 }
5735 }
Romain Guy06882f82009-06-10 13:36:04 -07005736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 void handleNewWindowLocked(WindowState newWindow) {
5738 if (!newWindow.canReceiveKeys()) {
5739 return;
5740 }
5741 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005742 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 TAG, "New key dispatch window: win="
5744 + newWindow.mClient.asBinder()
5745 + ", last=" + mLastBinder
5746 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5747 + "), finished=" + mFinished + ", paused="
5748 + newWindow.mToken.paused);
5749
5750 // Displaying a window implicitly causes dispatching to
5751 // be unpaused. (This is to protect against bugs if someone
5752 // pauses dispatching but forgets to resume.)
5753 newWindow.mToken.paused = false;
5754
5755 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756
5757 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5758 if (DEBUG_INPUT) Log.v(TAG,
5759 "New SYSTEM_ERROR window; resetting state");
5760 mLastWin = null;
5761 mLastBinder = null;
5762 mMotionTarget = null;
5763 mFinished = true;
5764 } else if (mLastWin != null) {
5765 // If the new window is above the window we are
5766 // waiting on, then stop waiting and let key dispatching
5767 // start on the new guy.
5768 if (DEBUG_INPUT) Log.v(
5769 TAG, "Last win layer=" + mLastWin.mLayer
5770 + ", new win layer=" + newWindow.mLayer);
5771 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005772 // The new window is above the old; finish pending input to the last
5773 // window and start directing it to the new one.
5774 mLastWin.mToken.paused = false;
5775 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005777 // Either the new window is lower, so there is no need to wake key waiters,
5778 // or we just finished key input to the previous window, which implicitly
5779 // notified the key waiters. In both cases, we don't need to issue the
5780 // notification here.
5781 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 }
5783
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005784 // Now that we've put a new window state in place, make the event waiter
5785 // take notice and retarget its attentions.
5786 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 }
5788 }
5789
5790 void pauseDispatchingLocked(WindowToken token) {
5791 synchronized (this)
5792 {
5793 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5794 token.paused = true;
5795
5796 /*
5797 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5798 mPaused = true;
5799 } else {
5800 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005801 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005803 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005805 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 }
5807 }
5808 */
5809 }
5810 }
5811
5812 void resumeDispatchingLocked(WindowToken token) {
5813 synchronized (this) {
5814 if (token.paused) {
5815 if (DEBUG_INPUT) Log.v(
5816 TAG, "Resuming WindowToken " + token
5817 + ", last=" + mLastBinder
5818 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5819 + "), finished=" + mFinished + ", paused="
5820 + token.paused);
5821 token.paused = false;
5822 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5823 doFinishedKeyLocked(true);
5824 } else {
5825 notifyAll();
5826 }
5827 }
5828 }
5829 }
5830
5831 void setEventDispatchingLocked(boolean enabled) {
5832 synchronized (this) {
5833 mEventDispatching = enabled;
5834 notifyAll();
5835 }
5836 }
Romain Guy06882f82009-06-10 13:36:04 -07005837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 void appSwitchComing() {
5839 synchronized (this) {
5840 // Don't wait for more than .5 seconds for app to finish
5841 // processing the pending events.
5842 long now = SystemClock.uptimeMillis() + 500;
5843 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5844 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5845 mTimeToSwitch = now;
5846 }
5847 notifyAll();
5848 }
5849 }
Romain Guy06882f82009-06-10 13:36:04 -07005850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 private final void doFinishedKeyLocked(boolean doRecycle) {
5852 if (mLastWin != null) {
5853 releasePendingPointerLocked(mLastWin.mSession);
5854 releasePendingTrackballLocked(mLastWin.mSession);
5855 }
Romain Guy06882f82009-06-10 13:36:04 -07005856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 if (mLastWin == null || !mLastWin.mToken.paused
5858 || !mLastWin.isVisibleLw()) {
5859 // If the current window has been paused, we aren't -really-
5860 // finished... so let the waiters still wait.
5861 mLastWin = null;
5862 mLastBinder = null;
5863 }
5864 mFinished = true;
5865 notifyAll();
5866 }
5867 }
5868
5869 private class KeyQ extends KeyInputQueue
5870 implements KeyInputQueue.FilterCallback {
5871 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005874 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5876 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5877 "KEEP_SCREEN_ON_FLAG");
5878 mHoldingScreen.setReferenceCounted(false);
5879 }
5880
5881 @Override
5882 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5883 if (mPolicy.preprocessInputEventTq(event)) {
5884 return true;
5885 }
Romain Guy06882f82009-06-10 13:36:04 -07005886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 switch (event.type) {
5888 case RawInputEvent.EV_KEY: {
5889 // XXX begin hack
5890 if (DEBUG) {
5891 if (event.keycode == KeyEvent.KEYCODE_G) {
5892 if (event.value != 0) {
5893 // G down
5894 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5895 }
5896 return false;
5897 }
5898 if (event.keycode == KeyEvent.KEYCODE_D) {
5899 if (event.value != 0) {
5900 //dump();
5901 }
5902 return false;
5903 }
5904 }
5905 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 boolean screenIsOff = !mPowerManager.screenIsOn();
5908 boolean screenIsDim = !mPowerManager.screenIsBright();
5909 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5912 mPowerManager.goToSleep(event.when);
5913 }
5914
5915 if (screenIsOff) {
5916 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5917 }
5918 if (screenIsDim) {
5919 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5920 }
5921 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5922 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005923 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 }
Romain Guy06882f82009-06-10 13:36:04 -07005925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5927 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5928 filterQueue(this);
5929 mKeyWaiter.appSwitchComing();
5930 }
5931 return true;
5932 } else {
5933 return false;
5934 }
5935 }
Romain Guy06882f82009-06-10 13:36:04 -07005936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 case RawInputEvent.EV_REL: {
5938 boolean screenIsOff = !mPowerManager.screenIsOn();
5939 boolean screenIsDim = !mPowerManager.screenIsBright();
5940 if (screenIsOff) {
5941 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5942 device.classes, event)) {
5943 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5944 return false;
5945 }
5946 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5947 }
5948 if (screenIsDim) {
5949 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5950 }
5951 return true;
5952 }
Romain Guy06882f82009-06-10 13:36:04 -07005953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 case RawInputEvent.EV_ABS: {
5955 boolean screenIsOff = !mPowerManager.screenIsOn();
5956 boolean screenIsDim = !mPowerManager.screenIsBright();
5957 if (screenIsOff) {
5958 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5959 device.classes, event)) {
5960 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5961 return false;
5962 }
5963 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5964 }
5965 if (screenIsDim) {
5966 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5967 }
5968 return true;
5969 }
Romain Guy06882f82009-06-10 13:36:04 -07005970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 default:
5972 return true;
5973 }
5974 }
5975
5976 public int filterEvent(QueuedEvent ev) {
5977 switch (ev.classType) {
5978 case RawInputEvent.CLASS_KEYBOARD:
5979 KeyEvent ke = (KeyEvent)ev.event;
5980 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5981 Log.w(TAG, "Dropping movement key during app switch: "
5982 + ke.getKeyCode() + ", action=" + ke.getAction());
5983 return FILTER_REMOVE;
5984 }
5985 return FILTER_ABORT;
5986 default:
5987 return FILTER_KEEP;
5988 }
5989 }
Romain Guy06882f82009-06-10 13:36:04 -07005990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 /**
5992 * Must be called with the main window manager lock held.
5993 */
5994 void setHoldScreenLocked(boolean holding) {
5995 boolean state = mHoldingScreen.isHeld();
5996 if (holding != state) {
5997 if (holding) {
5998 mHoldingScreen.acquire();
5999 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006000 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 mHoldingScreen.release();
6002 }
6003 }
6004 }
Michael Chan53071d62009-05-13 17:29:48 -07006005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006
6007 public boolean detectSafeMode() {
6008 mSafeMode = mPolicy.detectSafeMode();
6009 return mSafeMode;
6010 }
Romain Guy06882f82009-06-10 13:36:04 -07006011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 public void systemReady() {
6013 mPolicy.systemReady();
6014 }
Romain Guy06882f82009-06-10 13:36:04 -07006015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 private final class InputDispatcherThread extends Thread {
6017 // Time to wait when there is nothing to do: 9999 seconds.
6018 static final int LONG_WAIT=9999*1000;
6019
6020 public InputDispatcherThread() {
6021 super("InputDispatcher");
6022 }
Romain Guy06882f82009-06-10 13:36:04 -07006023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 @Override
6025 public void run() {
6026 while (true) {
6027 try {
6028 process();
6029 } catch (Exception e) {
6030 Log.e(TAG, "Exception in input dispatcher", e);
6031 }
6032 }
6033 }
Romain Guy06882f82009-06-10 13:36:04 -07006034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006035 private void process() {
6036 android.os.Process.setThreadPriority(
6037 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 // The last key event we saw
6040 KeyEvent lastKey = null;
6041
6042 // Last keydown time for auto-repeating keys
6043 long lastKeyTime = SystemClock.uptimeMillis();
6044 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006045 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046
Romain Guy06882f82009-06-10 13:36:04 -07006047 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 int keyRepeatCount = 0;
6049
6050 // Need to report that configuration has changed?
6051 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 while (true) {
6054 long curTime = SystemClock.uptimeMillis();
6055
6056 if (DEBUG_INPUT) Log.v(
6057 TAG, "Waiting for next key: now=" + curTime
6058 + ", repeat @ " + nextKeyTime);
6059
6060 // Retrieve next event, waiting only as long as the next
6061 // repeat timeout. If the configuration has changed, then
6062 // don't wait at all -- we'll report the change as soon as
6063 // we have processed all events.
6064 QueuedEvent ev = mQueue.getEvent(
6065 (int)((!configChanged && curTime < nextKeyTime)
6066 ? (nextKeyTime-curTime) : 0));
6067
6068 if (DEBUG_INPUT && ev != null) Log.v(
6069 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6070
Michael Chan53071d62009-05-13 17:29:48 -07006071 if (MEASURE_LATENCY) {
6072 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6073 }
6074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 try {
6076 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006077 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 int eventType;
6079 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6080 eventType = eventType((MotionEvent)ev.event);
6081 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6082 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6083 eventType = LocalPowerManager.BUTTON_EVENT;
6084 } else {
6085 eventType = LocalPowerManager.OTHER_EVENT;
6086 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006087 try {
Michael Chan53071d62009-05-13 17:29:48 -07006088 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006089 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006090 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006091 mBatteryStats.noteInputEvent();
6092 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006093 } catch (RemoteException e) {
6094 // Ignore
6095 }
Michael Chane10de972009-05-18 11:24:50 -07006096
6097 if (eventType != TOUCH_EVENT
6098 && eventType != LONG_TOUCH_EVENT
6099 && eventType != CHEEK_EVENT) {
6100 mPowerManager.userActivity(curTime, false,
6101 eventType, false);
6102 } else if (mLastTouchEventType != eventType
6103 || (curTime - mLastUserActivityCallTime)
6104 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6105 mLastUserActivityCallTime = curTime;
6106 mLastTouchEventType = eventType;
6107 mPowerManager.userActivity(curTime, false,
6108 eventType, false);
6109 }
6110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 switch (ev.classType) {
6112 case RawInputEvent.CLASS_KEYBOARD:
6113 KeyEvent ke = (KeyEvent)ev.event;
6114 if (ke.isDown()) {
6115 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006116 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 keyRepeatCount = 0;
6118 lastKeyTime = curTime;
6119 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006120 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 if (DEBUG_INPUT) Log.v(
6122 TAG, "Received key down: first repeat @ "
6123 + nextKeyTime);
6124 } else {
6125 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006126 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 // Arbitrary long timeout.
6128 lastKeyTime = curTime;
6129 nextKeyTime = curTime + LONG_WAIT;
6130 if (DEBUG_INPUT) Log.v(
6131 TAG, "Received key up: ignore repeat @ "
6132 + nextKeyTime);
6133 }
6134 dispatchKey((KeyEvent)ev.event, 0, 0);
6135 mQueue.recycleEvent(ev);
6136 break;
6137 case RawInputEvent.CLASS_TOUCHSCREEN:
6138 //Log.i(TAG, "Read next event " + ev);
6139 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6140 break;
6141 case RawInputEvent.CLASS_TRACKBALL:
6142 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6143 break;
6144 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6145 configChanged = true;
6146 break;
6147 default:
6148 mQueue.recycleEvent(ev);
6149 break;
6150 }
Romain Guy06882f82009-06-10 13:36:04 -07006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 } else if (configChanged) {
6153 configChanged = false;
6154 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 } else if (lastKey != null) {
6157 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 // Timeout occurred while key was down. If it is at or
6160 // past the key repeat time, dispatch the repeat.
6161 if (DEBUG_INPUT) Log.v(
6162 TAG, "Key timeout: repeat=" + nextKeyTime
6163 + ", now=" + curTime);
6164 if (curTime < nextKeyTime) {
6165 continue;
6166 }
Romain Guy06882f82009-06-10 13:36:04 -07006167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 lastKeyTime = nextKeyTime;
6169 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6170 keyRepeatCount++;
6171 if (DEBUG_INPUT) Log.v(
6172 TAG, "Key repeat: count=" + keyRepeatCount
6173 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006174 KeyEvent newEvent;
6175 if (downTime != 0 && (downTime
6176 + ViewConfiguration.getLongPressTimeout())
6177 <= curTime) {
6178 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6179 curTime, keyRepeatCount,
6180 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6181 downTime = 0;
6182 } else {
6183 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6184 curTime, keyRepeatCount);
6185 }
6186 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 } else {
6189 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 lastKeyTime = curTime;
6192 nextKeyTime = curTime + LONG_WAIT;
6193 }
Romain Guy06882f82009-06-10 13:36:04 -07006194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 } catch (Exception e) {
6196 Log.e(TAG,
6197 "Input thread received uncaught exception: " + e, e);
6198 }
6199 }
6200 }
6201 }
6202
6203 // -------------------------------------------------------------
6204 // Client Session State
6205 // -------------------------------------------------------------
6206
6207 private final class Session extends IWindowSession.Stub
6208 implements IBinder.DeathRecipient {
6209 final IInputMethodClient mClient;
6210 final IInputContext mInputContext;
6211 final int mUid;
6212 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006213 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 SurfaceSession mSurfaceSession;
6215 int mNumWindow = 0;
6216 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 /**
6219 * Current pointer move event being dispatched to client window... must
6220 * hold key lock to access.
6221 */
6222 QueuedEvent mPendingPointerMove;
6223 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 /**
6226 * Current trackball move event being dispatched to client window... must
6227 * hold key lock to access.
6228 */
6229 QueuedEvent mPendingTrackballMove;
6230 WindowState mPendingTrackballWindow;
6231
6232 public Session(IInputMethodClient client, IInputContext inputContext) {
6233 mClient = client;
6234 mInputContext = inputContext;
6235 mUid = Binder.getCallingUid();
6236 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006237 StringBuilder sb = new StringBuilder();
6238 sb.append("Session{");
6239 sb.append(Integer.toHexString(System.identityHashCode(this)));
6240 sb.append(" uid ");
6241 sb.append(mUid);
6242 sb.append("}");
6243 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 synchronized (mWindowMap) {
6246 if (mInputMethodManager == null && mHaveInputMethods) {
6247 IBinder b = ServiceManager.getService(
6248 Context.INPUT_METHOD_SERVICE);
6249 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6250 }
6251 }
6252 long ident = Binder.clearCallingIdentity();
6253 try {
6254 // Note: it is safe to call in to the input method manager
6255 // here because we are not holding our lock.
6256 if (mInputMethodManager != null) {
6257 mInputMethodManager.addClient(client, inputContext,
6258 mUid, mPid);
6259 } else {
6260 client.setUsingInputMethod(false);
6261 }
6262 client.asBinder().linkToDeath(this, 0);
6263 } catch (RemoteException e) {
6264 // The caller has died, so we can just forget about this.
6265 try {
6266 if (mInputMethodManager != null) {
6267 mInputMethodManager.removeClient(client);
6268 }
6269 } catch (RemoteException ee) {
6270 }
6271 } finally {
6272 Binder.restoreCallingIdentity(ident);
6273 }
6274 }
Romain Guy06882f82009-06-10 13:36:04 -07006275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 @Override
6277 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6278 throws RemoteException {
6279 try {
6280 return super.onTransact(code, data, reply, flags);
6281 } catch (RuntimeException e) {
6282 // Log all 'real' exceptions thrown to the caller
6283 if (!(e instanceof SecurityException)) {
6284 Log.e(TAG, "Window Session Crash", e);
6285 }
6286 throw e;
6287 }
6288 }
6289
6290 public void binderDied() {
6291 // Note: it is safe to call in to the input method manager
6292 // here because we are not holding our lock.
6293 try {
6294 if (mInputMethodManager != null) {
6295 mInputMethodManager.removeClient(mClient);
6296 }
6297 } catch (RemoteException e) {
6298 }
6299 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006300 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 mClientDead = true;
6302 killSessionLocked();
6303 }
6304 }
6305
6306 public int add(IWindow window, WindowManager.LayoutParams attrs,
6307 int viewVisibility, Rect outContentInsets) {
6308 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6309 }
Romain Guy06882f82009-06-10 13:36:04 -07006310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 public void remove(IWindow window) {
6312 removeWindow(this, window);
6313 }
Romain Guy06882f82009-06-10 13:36:04 -07006314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6316 int requestedWidth, int requestedHeight, int viewFlags,
6317 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6318 Rect outVisibleInsets, Surface outSurface) {
6319 return relayoutWindow(this, window, attrs,
6320 requestedWidth, requestedHeight, viewFlags, insetsPending,
6321 outFrame, outContentInsets, outVisibleInsets, outSurface);
6322 }
Romain Guy06882f82009-06-10 13:36:04 -07006323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 public void setTransparentRegion(IWindow window, Region region) {
6325 setTransparentRegionWindow(this, window, region);
6326 }
Romain Guy06882f82009-06-10 13:36:04 -07006327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 public void setInsets(IWindow window, int touchableInsets,
6329 Rect contentInsets, Rect visibleInsets) {
6330 setInsetsWindow(this, window, touchableInsets, contentInsets,
6331 visibleInsets);
6332 }
Romain Guy06882f82009-06-10 13:36:04 -07006333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6335 getWindowDisplayFrame(this, window, outDisplayFrame);
6336 }
Romain Guy06882f82009-06-10 13:36:04 -07006337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 public void finishDrawing(IWindow window) {
6339 if (localLOGV) Log.v(
6340 TAG, "IWindow finishDrawing called for " + window);
6341 finishDrawingWindow(this, window);
6342 }
6343
6344 public void finishKey(IWindow window) {
6345 if (localLOGV) Log.v(
6346 TAG, "IWindow finishKey called for " + window);
6347 mKeyWaiter.finishedKey(this, window, false,
6348 KeyWaiter.RETURN_NOTHING);
6349 }
6350
6351 public MotionEvent getPendingPointerMove(IWindow window) {
6352 if (localLOGV) Log.v(
6353 TAG, "IWindow getPendingMotionEvent called for " + window);
6354 return mKeyWaiter.finishedKey(this, window, false,
6355 KeyWaiter.RETURN_PENDING_POINTER);
6356 }
Romain Guy06882f82009-06-10 13:36:04 -07006357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 public MotionEvent getPendingTrackballMove(IWindow window) {
6359 if (localLOGV) Log.v(
6360 TAG, "IWindow getPendingMotionEvent called for " + window);
6361 return mKeyWaiter.finishedKey(this, window, false,
6362 KeyWaiter.RETURN_PENDING_TRACKBALL);
6363 }
6364
6365 public void setInTouchMode(boolean mode) {
6366 synchronized(mWindowMap) {
6367 mInTouchMode = mode;
6368 }
6369 }
6370
6371 public boolean getInTouchMode() {
6372 synchronized(mWindowMap) {
6373 return mInTouchMode;
6374 }
6375 }
6376
6377 public boolean performHapticFeedback(IWindow window, int effectId,
6378 boolean always) {
6379 synchronized(mWindowMap) {
6380 long ident = Binder.clearCallingIdentity();
6381 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006382 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006383 windowForClientLocked(this, window), effectId, always);
6384 } finally {
6385 Binder.restoreCallingIdentity(ident);
6386 }
6387 }
6388 }
Romain Guy06882f82009-06-10 13:36:04 -07006389
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006390 public void setWallpaperPosition(IBinder window, float x, float y) {
6391 synchronized(mWindowMap) {
6392 long ident = Binder.clearCallingIdentity();
6393 try {
6394 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6395 x, y);
6396 } finally {
6397 Binder.restoreCallingIdentity(ident);
6398 }
6399 }
6400 }
6401
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006402 public void wallpaperOffsetsComplete(IBinder window) {
6403 WindowManagerService.this.wallpaperOffsetsComplete(window);
6404 }
6405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 void windowAddedLocked() {
6407 if (mSurfaceSession == null) {
6408 if (localLOGV) Log.v(
6409 TAG, "First window added to " + this + ", creating SurfaceSession");
6410 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006411 if (SHOW_TRANSACTIONS) Log.i(
6412 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006413 mSessions.add(this);
6414 }
6415 mNumWindow++;
6416 }
6417
6418 void windowRemovedLocked() {
6419 mNumWindow--;
6420 killSessionLocked();
6421 }
Romain Guy06882f82009-06-10 13:36:04 -07006422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 void killSessionLocked() {
6424 if (mNumWindow <= 0 && mClientDead) {
6425 mSessions.remove(this);
6426 if (mSurfaceSession != null) {
6427 if (localLOGV) Log.v(
6428 TAG, "Last window removed from " + this
6429 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006430 if (SHOW_TRANSACTIONS) Log.i(
6431 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 try {
6433 mSurfaceSession.kill();
6434 } catch (Exception e) {
6435 Log.w(TAG, "Exception thrown when killing surface session "
6436 + mSurfaceSession + " in session " + this
6437 + ": " + e.toString());
6438 }
6439 mSurfaceSession = null;
6440 }
6441 }
6442 }
Romain Guy06882f82009-06-10 13:36:04 -07006443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006445 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6446 pw.print(" mClientDead="); pw.print(mClientDead);
6447 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6448 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6449 pw.print(prefix);
6450 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6451 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6452 }
6453 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6454 pw.print(prefix);
6455 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6456 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 }
6459
6460 @Override
6461 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006462 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 }
6464 }
6465
6466 // -------------------------------------------------------------
6467 // Client Window State
6468 // -------------------------------------------------------------
6469
6470 private final class WindowState implements WindowManagerPolicy.WindowState {
6471 final Session mSession;
6472 final IWindow mClient;
6473 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006474 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 AppWindowToken mAppToken;
6476 AppWindowToken mTargetAppToken;
6477 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6478 final DeathRecipient mDeathRecipient;
6479 final WindowState mAttachedWindow;
6480 final ArrayList mChildWindows = new ArrayList();
6481 final int mBaseLayer;
6482 final int mSubLayer;
6483 final boolean mLayoutAttached;
6484 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006485 final boolean mIsWallpaper;
6486 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006487 int mViewVisibility;
6488 boolean mPolicyVisibility = true;
6489 boolean mPolicyVisibilityAfterAnim = true;
6490 boolean mAppFreezing;
6491 Surface mSurface;
6492 boolean mAttachedHidden; // is our parent window hidden?
6493 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006494 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 int mRequestedWidth;
6496 int mRequestedHeight;
6497 int mLastRequestedWidth;
6498 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 int mLayer;
6500 int mAnimLayer;
6501 int mLastLayer;
6502 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006503 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006504 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505
6506 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 // Actual frame shown on-screen (may be modified by animation)
6509 final Rect mShownFrame = new Rect();
6510 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 /**
6513 * Insets that determine the actually visible area
6514 */
6515 final Rect mVisibleInsets = new Rect();
6516 final Rect mLastVisibleInsets = new Rect();
6517 boolean mVisibleInsetsChanged;
6518
6519 /**
6520 * Insets that are covered by system windows
6521 */
6522 final Rect mContentInsets = new Rect();
6523 final Rect mLastContentInsets = new Rect();
6524 boolean mContentInsetsChanged;
6525
6526 /**
6527 * Set to true if we are waiting for this window to receive its
6528 * given internal insets before laying out other windows based on it.
6529 */
6530 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 /**
6533 * These are the content insets that were given during layout for
6534 * this window, to be applied to windows behind it.
6535 */
6536 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 /**
6539 * These are the visible insets that were given during layout for
6540 * this window, to be applied to windows behind it.
6541 */
6542 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544 /**
6545 * Flag indicating whether the touchable region should be adjusted by
6546 * the visible insets; if false the area outside the visible insets is
6547 * NOT touchable, so we must use those to adjust the frame during hit
6548 * tests.
6549 */
6550 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006552 // Current transformation being applied.
6553 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6554 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6555 float mHScale=1, mVScale=1;
6556 float mLastHScale=1, mLastVScale=1;
6557 final Matrix mTmpMatrix = new Matrix();
6558
6559 // "Real" frame that the application sees.
6560 final Rect mFrame = new Rect();
6561 final Rect mLastFrame = new Rect();
6562
6563 final Rect mContainingFrame = new Rect();
6564 final Rect mDisplayFrame = new Rect();
6565 final Rect mContentFrame = new Rect();
6566 final Rect mVisibleFrame = new Rect();
6567
6568 float mShownAlpha = 1;
6569 float mAlpha = 1;
6570 float mLastAlpha = 1;
6571
6572 // Set to true if, when the window gets displayed, it should perform
6573 // an enter animation.
6574 boolean mEnterAnimationPending;
6575
6576 // Currently running animation.
6577 boolean mAnimating;
6578 boolean mLocalAnimating;
6579 Animation mAnimation;
6580 boolean mAnimationIsEntrance;
6581 boolean mHasTransformation;
6582 boolean mHasLocalTransformation;
6583 final Transformation mTransformation = new Transformation();
6584
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006585 // If a window showing a wallpaper: the requested offset for the
6586 // wallpaper; if a wallpaper window: the currently applied offset.
6587 float mWallpaperX = -1;
6588 float mWallpaperY = -1;
6589
6590 // Wallpaper windows: pixels offset based on above variables.
6591 int mXOffset;
6592 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 // This is set after IWindowSession.relayout() has been called at
6595 // least once for the window. It allows us to detect the situation
6596 // where we don't yet have a surface, but should have one soon, so
6597 // we can give the window focus before waiting for the relayout.
6598 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006600 // This is set after the Surface has been created but before the
6601 // window has been drawn. During this time the surface is hidden.
6602 boolean mDrawPending;
6603
6604 // This is set after the window has finished drawing for the first
6605 // time but before its surface is shown. The surface will be
6606 // displayed when the next layout is run.
6607 boolean mCommitDrawPending;
6608
6609 // This is set during the time after the window's drawing has been
6610 // committed, and before its surface is actually shown. It is used
6611 // to delay showing the surface until all windows in a token are ready
6612 // to be shown.
6613 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 // Set when the window has been shown in the screen the first time.
6616 boolean mHasDrawn;
6617
6618 // Currently running an exit animation?
6619 boolean mExiting;
6620
6621 // Currently on the mDestroySurface list?
6622 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 // Completely remove from window manager after exit animation?
6625 boolean mRemoveOnExit;
6626
6627 // Set when the orientation is changing and this window has not yet
6628 // been updated for the new orientation.
6629 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 // Is this window now (or just being) removed?
6632 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 WindowState(Session s, IWindow c, WindowToken token,
6635 WindowState attachedWindow, WindowManager.LayoutParams a,
6636 int viewVisibility) {
6637 mSession = s;
6638 mClient = c;
6639 mToken = token;
6640 mAttrs.copyFrom(a);
6641 mViewVisibility = viewVisibility;
6642 DeathRecipient deathRecipient = new DeathRecipient();
6643 mAlpha = a.alpha;
6644 if (localLOGV) Log.v(
6645 TAG, "Window " + this + " client=" + c.asBinder()
6646 + " token=" + token + " (" + mAttrs.token + ")");
6647 try {
6648 c.asBinder().linkToDeath(deathRecipient, 0);
6649 } catch (RemoteException e) {
6650 mDeathRecipient = null;
6651 mAttachedWindow = null;
6652 mLayoutAttached = false;
6653 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006654 mIsWallpaper = false;
6655 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 mBaseLayer = 0;
6657 mSubLayer = 0;
6658 return;
6659 }
6660 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6663 mAttrs.type <= LAST_SUB_WINDOW)) {
6664 // The multiplier here is to reserve space for multiple
6665 // windows in the same type layer.
6666 mBaseLayer = mPolicy.windowTypeToLayerLw(
6667 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6668 + TYPE_LAYER_OFFSET;
6669 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6670 mAttachedWindow = attachedWindow;
6671 mAttachedWindow.mChildWindows.add(this);
6672 mLayoutAttached = mAttrs.type !=
6673 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6674 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6675 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006676 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6677 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 } else {
6679 // The multiplier here is to reserve space for multiple
6680 // windows in the same type layer.
6681 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6682 * TYPE_LAYER_MULTIPLIER
6683 + TYPE_LAYER_OFFSET;
6684 mSubLayer = 0;
6685 mAttachedWindow = null;
6686 mLayoutAttached = false;
6687 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6688 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006689 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6690 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006691 }
6692
6693 WindowState appWin = this;
6694 while (appWin.mAttachedWindow != null) {
6695 appWin = mAttachedWindow;
6696 }
6697 WindowToken appToken = appWin.mToken;
6698 while (appToken.appWindowToken == null) {
6699 WindowToken parent = mTokenMap.get(appToken.token);
6700 if (parent == null || appToken == parent) {
6701 break;
6702 }
6703 appToken = parent;
6704 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006705 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006706 mAppToken = appToken.appWindowToken;
6707
6708 mSurface = null;
6709 mRequestedWidth = 0;
6710 mRequestedHeight = 0;
6711 mLastRequestedWidth = 0;
6712 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006713 mXOffset = 0;
6714 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 mLayer = 0;
6716 mAnimLayer = 0;
6717 mLastLayer = 0;
6718 }
6719
6720 void attach() {
6721 if (localLOGV) Log.v(
6722 TAG, "Attaching " + this + " token=" + mToken
6723 + ", list=" + mToken.windows);
6724 mSession.windowAddedLocked();
6725 }
6726
6727 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6728 mHaveFrame = true;
6729
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006730 final Rect container = mContainingFrame;
6731 container.set(pf);
6732
6733 final Rect display = mDisplayFrame;
6734 display.set(df);
6735
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006736 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006737 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006738 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6739 display.intersect(mCompatibleScreenFrame);
6740 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006741 }
6742
6743 final int pw = container.right - container.left;
6744 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745
6746 int w,h;
6747 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6748 w = mAttrs.width < 0 ? pw : mAttrs.width;
6749 h = mAttrs.height< 0 ? ph : mAttrs.height;
6750 } else {
6751 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6752 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6753 }
Romain Guy06882f82009-06-10 13:36:04 -07006754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006755 final Rect content = mContentFrame;
6756 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 final Rect visible = mVisibleFrame;
6759 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006762 final int fw = frame.width();
6763 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6766 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6767
6768 Gravity.apply(mAttrs.gravity, w, h, container,
6769 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6770 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6771
6772 //System.out.println("Out: " + mFrame);
6773
6774 // Now make sure the window fits in the overall display.
6775 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 // Make sure the content and visible frames are inside of the
6778 // final window frame.
6779 if (content.left < frame.left) content.left = frame.left;
6780 if (content.top < frame.top) content.top = frame.top;
6781 if (content.right > frame.right) content.right = frame.right;
6782 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6783 if (visible.left < frame.left) visible.left = frame.left;
6784 if (visible.top < frame.top) visible.top = frame.top;
6785 if (visible.right > frame.right) visible.right = frame.right;
6786 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 final Rect contentInsets = mContentInsets;
6789 contentInsets.left = content.left-frame.left;
6790 contentInsets.top = content.top-frame.top;
6791 contentInsets.right = frame.right-content.right;
6792 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 final Rect visibleInsets = mVisibleInsets;
6795 visibleInsets.left = visible.left-frame.left;
6796 visibleInsets.top = visible.top-frame.top;
6797 visibleInsets.right = frame.right-visible.right;
6798 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006799
Dianne Hackborn284ac932009-08-28 10:34:25 -07006800 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6801 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006802 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006803 }
6804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006805 if (localLOGV) {
6806 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6807 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6808 Log.v(TAG, "Resolving (mRequestedWidth="
6809 + mRequestedWidth + ", mRequestedheight="
6810 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6811 + "): frame=" + mFrame.toShortString()
6812 + " ci=" + contentInsets.toShortString()
6813 + " vi=" + visibleInsets.toShortString());
6814 //}
6815 }
6816 }
Romain Guy06882f82009-06-10 13:36:04 -07006817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 public Rect getFrameLw() {
6819 return mFrame;
6820 }
6821
6822 public Rect getShownFrameLw() {
6823 return mShownFrame;
6824 }
6825
6826 public Rect getDisplayFrameLw() {
6827 return mDisplayFrame;
6828 }
6829
6830 public Rect getContentFrameLw() {
6831 return mContentFrame;
6832 }
6833
6834 public Rect getVisibleFrameLw() {
6835 return mVisibleFrame;
6836 }
6837
6838 public boolean getGivenInsetsPendingLw() {
6839 return mGivenInsetsPending;
6840 }
6841
6842 public Rect getGivenContentInsetsLw() {
6843 return mGivenContentInsets;
6844 }
Romain Guy06882f82009-06-10 13:36:04 -07006845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 public Rect getGivenVisibleInsetsLw() {
6847 return mGivenVisibleInsets;
6848 }
Romain Guy06882f82009-06-10 13:36:04 -07006849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006850 public WindowManager.LayoutParams getAttrs() {
6851 return mAttrs;
6852 }
6853
6854 public int getSurfaceLayer() {
6855 return mLayer;
6856 }
Romain Guy06882f82009-06-10 13:36:04 -07006857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 public IApplicationToken getAppToken() {
6859 return mAppToken != null ? mAppToken.appToken : null;
6860 }
6861
6862 public boolean hasAppShownWindows() {
6863 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6864 }
6865
6866 public boolean hasAppStartingIcon() {
6867 return mAppToken != null ? (mAppToken.startingData != null) : false;
6868 }
6869
6870 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6871 return mAppToken != null ? mAppToken.startingWindow : null;
6872 }
6873
6874 public void setAnimation(Animation anim) {
6875 if (localLOGV) Log.v(
6876 TAG, "Setting animation in " + this + ": " + anim);
6877 mAnimating = false;
6878 mLocalAnimating = false;
6879 mAnimation = anim;
6880 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6881 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6882 }
6883
6884 public void clearAnimation() {
6885 if (mAnimation != null) {
6886 mAnimating = true;
6887 mLocalAnimating = false;
6888 mAnimation = null;
6889 }
6890 }
Romain Guy06882f82009-06-10 13:36:04 -07006891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 Surface createSurfaceLocked() {
6893 if (mSurface == null) {
6894 mDrawPending = true;
6895 mCommitDrawPending = false;
6896 mReadyToShow = false;
6897 if (mAppToken != null) {
6898 mAppToken.allDrawn = false;
6899 }
6900
6901 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006902 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 flags |= Surface.PUSH_BUFFERS;
6904 }
6905
6906 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6907 flags |= Surface.SECURE;
6908 }
6909 if (DEBUG_VISIBILITY) Log.v(
6910 TAG, "Creating surface in session "
6911 + mSession.mSurfaceSession + " window " + this
6912 + " w=" + mFrame.width()
6913 + " h=" + mFrame.height() + " format="
6914 + mAttrs.format + " flags=" + flags);
6915
6916 int w = mFrame.width();
6917 int h = mFrame.height();
6918 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6919 // for a scaled surface, we always want the requested
6920 // size.
6921 w = mRequestedWidth;
6922 h = mRequestedHeight;
6923 }
6924
6925 try {
6926 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006927 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006929 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
6930 + mSurface + " IN SESSION "
6931 + mSession.mSurfaceSession
6932 + ": pid=" + mSession.mPid + " format="
6933 + mAttrs.format + " flags=0x"
6934 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 } catch (Surface.OutOfResourcesException e) {
6936 Log.w(TAG, "OutOfResourcesException creating surface");
6937 reclaimSomeSurfaceMemoryLocked(this, "create");
6938 return null;
6939 } catch (Exception e) {
6940 Log.e(TAG, "Exception creating surface", e);
6941 return null;
6942 }
Romain Guy06882f82009-06-10 13:36:04 -07006943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 if (localLOGV) Log.v(
6945 TAG, "Got surface: " + mSurface
6946 + ", set left=" + mFrame.left + " top=" + mFrame.top
6947 + ", animLayer=" + mAnimLayer);
6948 if (SHOW_TRANSACTIONS) {
6949 Log.i(TAG, ">>> OPEN TRANSACTION");
6950 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6951 + mAttrs.getTitle() + ") pos=(" +
6952 mFrame.left + "," + mFrame.top + ") (" +
6953 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6954 mAnimLayer + " HIDE");
6955 }
6956 Surface.openTransaction();
6957 try {
6958 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006959 mSurface.setPosition(mFrame.left + mXOffset,
6960 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 mSurface.setLayer(mAnimLayer);
6962 mSurface.hide();
6963 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006964 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
6965 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 mSurface.setFlags(Surface.SURFACE_DITHER,
6967 Surface.SURFACE_DITHER);
6968 }
6969 } catch (RuntimeException e) {
6970 Log.w(TAG, "Error creating surface in " + w, e);
6971 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6972 }
6973 mLastHidden = true;
6974 } finally {
6975 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6976 Surface.closeTransaction();
6977 }
6978 if (localLOGV) Log.v(
6979 TAG, "Created surface " + this);
6980 }
6981 return mSurface;
6982 }
Romain Guy06882f82009-06-10 13:36:04 -07006983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 void destroySurfaceLocked() {
6985 // Window is no longer on-screen, so can no longer receive
6986 // key events... if we were waiting for it to finish
6987 // handling a key event, the wait is over!
6988 mKeyWaiter.finishedKey(mSession, mClient, true,
6989 KeyWaiter.RETURN_NOTHING);
6990 mKeyWaiter.releasePendingPointerLocked(mSession);
6991 mKeyWaiter.releasePendingTrackballLocked(mSession);
6992
6993 if (mAppToken != null && this == mAppToken.startingWindow) {
6994 mAppToken.startingDisplayed = false;
6995 }
Romain Guy06882f82009-06-10 13:36:04 -07006996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 if (mSurface != null) {
6998 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006999 if (DEBUG_VISIBILITY) {
7000 RuntimeException e = new RuntimeException();
7001 e.fillInStackTrace();
7002 Log.w(TAG, "Window " + this + " destroying surface "
7003 + mSurface + ", session " + mSession, e);
7004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 if (SHOW_TRANSACTIONS) {
7006 RuntimeException ex = new RuntimeException();
7007 ex.fillInStackTrace();
7008 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7009 + mAttrs.getTitle() + ")", ex);
7010 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007011 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 } catch (RuntimeException e) {
7013 Log.w(TAG, "Exception thrown when destroying Window " + this
7014 + " surface " + mSurface + " session " + mSession
7015 + ": " + e.toString());
7016 }
7017 mSurface = null;
7018 mDrawPending = false;
7019 mCommitDrawPending = false;
7020 mReadyToShow = false;
7021
7022 int i = mChildWindows.size();
7023 while (i > 0) {
7024 i--;
7025 WindowState c = (WindowState)mChildWindows.get(i);
7026 c.mAttachedHidden = true;
7027 }
7028 }
7029 }
7030
7031 boolean finishDrawingLocked() {
7032 if (mDrawPending) {
7033 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7034 TAG, "finishDrawingLocked: " + mSurface);
7035 mCommitDrawPending = true;
7036 mDrawPending = false;
7037 return true;
7038 }
7039 return false;
7040 }
7041
7042 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007043 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7045 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007046 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 }
7048 mCommitDrawPending = false;
7049 mReadyToShow = true;
7050 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7051 final AppWindowToken atoken = mAppToken;
7052 if (atoken == null || atoken.allDrawn || starting) {
7053 performShowLocked();
7054 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007055 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 }
7057
7058 // This must be called while inside a transaction.
7059 boolean performShowLocked() {
7060 if (DEBUG_VISIBILITY) {
7061 RuntimeException e = new RuntimeException();
7062 e.fillInStackTrace();
7063 Log.v(TAG, "performShow on " + this
7064 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7065 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7066 }
7067 if (mReadyToShow && isReadyForDisplay()) {
7068 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7069 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7070 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7071 + " during animation: policyVis=" + mPolicyVisibility
7072 + " attHidden=" + mAttachedHidden
7073 + " tok.hiddenRequested="
7074 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007075 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 + (mAppToken != null ? mAppToken.hidden : false)
7077 + " animating=" + mAnimating
7078 + " tok animating="
7079 + (mAppToken != null ? mAppToken.animating : false));
7080 if (!showSurfaceRobustlyLocked(this)) {
7081 return false;
7082 }
7083 mLastAlpha = -1;
7084 mHasDrawn = true;
7085 mLastHidden = false;
7086 mReadyToShow = false;
7087 enableScreenIfNeededLocked();
7088
7089 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 int i = mChildWindows.size();
7092 while (i > 0) {
7093 i--;
7094 WindowState c = (WindowState)mChildWindows.get(i);
7095 if (c.mSurface != null && c.mAttachedHidden) {
7096 c.mAttachedHidden = false;
7097 c.performShowLocked();
7098 }
7099 }
Romain Guy06882f82009-06-10 13:36:04 -07007100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 if (mAttrs.type != TYPE_APPLICATION_STARTING
7102 && mAppToken != null) {
7103 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007104
7105 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007106 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7107 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007109 // If this initial window is animating, stop it -- we
7110 // will do an animation to reveal it from behind the
7111 // starting window, so there is no need for it to also
7112 // be doing its own stuff.
7113 if (mAnimation != null) {
7114 mAnimation = null;
7115 // Make sure we clean up the animation.
7116 mAnimating = true;
7117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 mFinishedStarting.add(mAppToken);
7119 mH.sendEmptyMessage(H.FINISHED_STARTING);
7120 }
7121 mAppToken.updateReportedVisibilityLocked();
7122 }
7123 }
7124 return true;
7125 }
Romain Guy06882f82009-06-10 13:36:04 -07007126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 // This must be called while inside a transaction. Returns true if
7128 // there is more animation to run.
7129 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7130 if (!mDisplayFrozen) {
7131 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7134 mHasTransformation = true;
7135 mHasLocalTransformation = true;
7136 if (!mLocalAnimating) {
7137 if (DEBUG_ANIM) Log.v(
7138 TAG, "Starting animation in " + this +
7139 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7140 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7141 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7142 mAnimation.setStartTime(currentTime);
7143 mLocalAnimating = true;
7144 mAnimating = true;
7145 }
7146 mTransformation.clear();
7147 final boolean more = mAnimation.getTransformation(
7148 currentTime, mTransformation);
7149 if (DEBUG_ANIM) Log.v(
7150 TAG, "Stepped animation in " + this +
7151 ": more=" + more + ", xform=" + mTransformation);
7152 if (more) {
7153 // we're not done!
7154 return true;
7155 }
7156 if (DEBUG_ANIM) Log.v(
7157 TAG, "Finished animation in " + this +
7158 " @ " + currentTime);
7159 mAnimation = null;
7160 //WindowManagerService.this.dump();
7161 }
7162 mHasLocalTransformation = false;
7163 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007164 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 // When our app token is animating, we kind-of pretend like
7166 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7167 // part of this check means that we will only do this if
7168 // our window is not currently exiting, or it is not
7169 // locally animating itself. The idea being that one that
7170 // is exiting and doing a local animation should be removed
7171 // once that animation is done.
7172 mAnimating = true;
7173 mHasTransformation = true;
7174 mTransformation.clear();
7175 return false;
7176 } else if (mHasTransformation) {
7177 // Little trick to get through the path below to act like
7178 // we have finished an animation.
7179 mAnimating = true;
7180 } else if (isAnimating()) {
7181 mAnimating = true;
7182 }
7183 } else if (mAnimation != null) {
7184 // If the display is frozen, and there is a pending animation,
7185 // clear it and make sure we run the cleanup code.
7186 mAnimating = true;
7187 mLocalAnimating = true;
7188 mAnimation = null;
7189 }
Romain Guy06882f82009-06-10 13:36:04 -07007190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 if (!mAnimating && !mLocalAnimating) {
7192 return false;
7193 }
7194
7195 if (DEBUG_ANIM) Log.v(
7196 TAG, "Animation done in " + this + ": exiting=" + mExiting
7197 + ", reportedVisible="
7198 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 mAnimating = false;
7201 mLocalAnimating = false;
7202 mAnimation = null;
7203 mAnimLayer = mLayer;
7204 if (mIsImWindow) {
7205 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007206 } else if (mIsWallpaper) {
7207 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 }
7209 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7210 + " anim layer: " + mAnimLayer);
7211 mHasTransformation = false;
7212 mHasLocalTransformation = false;
7213 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7214 mTransformation.clear();
7215 if (mHasDrawn
7216 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7217 && mAppToken != null
7218 && mAppToken.firstWindowDrawn
7219 && mAppToken.startingData != null) {
7220 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7221 + mToken + ": first real window done animating");
7222 mFinishedStarting.add(mAppToken);
7223 mH.sendEmptyMessage(H.FINISHED_STARTING);
7224 }
Romain Guy06882f82009-06-10 13:36:04 -07007225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 finishExit();
7227
7228 if (mAppToken != null) {
7229 mAppToken.updateReportedVisibilityLocked();
7230 }
7231
7232 return false;
7233 }
7234
7235 void finishExit() {
7236 if (DEBUG_ANIM) Log.v(
7237 TAG, "finishExit in " + this
7238 + ": exiting=" + mExiting
7239 + " remove=" + mRemoveOnExit
7240 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007242 final int N = mChildWindows.size();
7243 for (int i=0; i<N; i++) {
7244 ((WindowState)mChildWindows.get(i)).finishExit();
7245 }
Romain Guy06882f82009-06-10 13:36:04 -07007246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 if (!mExiting) {
7248 return;
7249 }
Romain Guy06882f82009-06-10 13:36:04 -07007250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007251 if (isWindowAnimating()) {
7252 return;
7253 }
7254
7255 if (localLOGV) Log.v(
7256 TAG, "Exit animation finished in " + this
7257 + ": remove=" + mRemoveOnExit);
7258 if (mSurface != null) {
7259 mDestroySurface.add(this);
7260 mDestroying = true;
7261 if (SHOW_TRANSACTIONS) Log.i(
7262 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7263 try {
7264 mSurface.hide();
7265 } catch (RuntimeException e) {
7266 Log.w(TAG, "Error hiding surface in " + this, e);
7267 }
7268 mLastHidden = true;
7269 mKeyWaiter.releasePendingPointerLocked(mSession);
7270 }
7271 mExiting = false;
7272 if (mRemoveOnExit) {
7273 mPendingRemove.add(this);
7274 mRemoveOnExit = false;
7275 }
7276 }
Romain Guy06882f82009-06-10 13:36:04 -07007277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7279 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7280 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7281 if (dtdx < -.000001f || dtdx > .000001f) return false;
7282 if (dsdy < -.000001f || dsdy > .000001f) return false;
7283 return true;
7284 }
Romain Guy06882f82009-06-10 13:36:04 -07007285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 void computeShownFrameLocked() {
7287 final boolean selfTransformation = mHasLocalTransformation;
7288 Transformation attachedTransformation =
7289 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7290 ? mAttachedWindow.mTransformation : null;
7291 Transformation appTransformation =
7292 (mAppToken != null && mAppToken.hasTransformation)
7293 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007294
7295 // Wallpapers are animated based on the "real" window they
7296 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007297 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007298 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007299 if (mWallpaperTarget.mHasLocalTransformation) {
7300 attachedTransformation = mWallpaperTarget.mTransformation;
7301 }
7302 if (mWallpaperTarget.mAppToken != null &&
7303 mWallpaperTarget.mAppToken.hasTransformation) {
7304 appTransformation = mWallpaperTarget.mAppToken.transformation;
7305 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007306 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7307 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7308 }
7309 if (DEBUG_WALLPAPER && appTransformation != null) {
7310 Log.v(TAG, "WP target app xform: " + appTransformation);
7311 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007312 }
7313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 if (selfTransformation || attachedTransformation != null
7315 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007316 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 final Rect frame = mFrame;
7318 final float tmpFloats[] = mTmpFloats;
7319 final Matrix tmpMatrix = mTmpMatrix;
7320
7321 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007322 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007323 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007324 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007326 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007328 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 }
7330 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007331 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 }
7333
7334 // "convert" it into SurfaceFlinger's format
7335 // (a 2x2 matrix + an offset)
7336 // Here we must not transform the position of the surface
7337 // since it is already included in the transformation.
7338 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 tmpMatrix.getValues(tmpFloats);
7341 mDsDx = tmpFloats[Matrix.MSCALE_X];
7342 mDtDx = tmpFloats[Matrix.MSKEW_X];
7343 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7344 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007345 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7346 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 int w = frame.width();
7348 int h = frame.height();
7349 mShownFrame.set(x, y, x+w, y+h);
7350
7351 // Now set the alpha... but because our current hardware
7352 // can't do alpha transformation on a non-opaque surface,
7353 // turn it off if we are running an animation that is also
7354 // transforming since it is more important to have that
7355 // animation be smooth.
7356 mShownAlpha = mAlpha;
7357 if (!mLimitedAlphaCompositing
7358 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7359 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7360 && x == frame.left && y == frame.top))) {
7361 //Log.i(TAG, "Applying alpha transform");
7362 if (selfTransformation) {
7363 mShownAlpha *= mTransformation.getAlpha();
7364 }
7365 if (attachedTransformation != null) {
7366 mShownAlpha *= attachedTransformation.getAlpha();
7367 }
7368 if (appTransformation != null) {
7369 mShownAlpha *= appTransformation.getAlpha();
7370 }
7371 } else {
7372 //Log.i(TAG, "Not applying alpha transform");
7373 }
Romain Guy06882f82009-06-10 13:36:04 -07007374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 if (localLOGV) Log.v(
7376 TAG, "Continuing animation in " + this +
7377 ": " + mShownFrame +
7378 ", alpha=" + mTransformation.getAlpha());
7379 return;
7380 }
Romain Guy06882f82009-06-10 13:36:04 -07007381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007383 if (mXOffset != 0 || mYOffset != 0) {
7384 mShownFrame.offset(mXOffset, mYOffset);
7385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 mShownAlpha = mAlpha;
7387 mDsDx = 1;
7388 mDtDx = 0;
7389 mDsDy = 0;
7390 mDtDy = 1;
7391 }
Romain Guy06882f82009-06-10 13:36:04 -07007392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 /**
7394 * Is this window visible? It is not visible if there is no
7395 * surface, or we are in the process of running an exit animation
7396 * that will remove the surface, or its app token has been hidden.
7397 */
7398 public boolean isVisibleLw() {
7399 final AppWindowToken atoken = mAppToken;
7400 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7401 && (atoken == null || !atoken.hiddenRequested)
7402 && !mExiting && !mDestroying;
7403 }
7404
7405 /**
7406 * Is this window visible, ignoring its app token? It is not visible
7407 * if there is no surface, or we are in the process of running an exit animation
7408 * that will remove the surface.
7409 */
7410 public boolean isWinVisibleLw() {
7411 final AppWindowToken atoken = mAppToken;
7412 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7413 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7414 && !mExiting && !mDestroying;
7415 }
7416
7417 /**
7418 * The same as isVisible(), but follows the current hidden state of
7419 * the associated app token, not the pending requested hidden state.
7420 */
7421 boolean isVisibleNow() {
7422 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007423 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 }
7425
7426 /**
7427 * Same as isVisible(), but we also count it as visible between the
7428 * call to IWindowSession.add() and the first relayout().
7429 */
7430 boolean isVisibleOrAdding() {
7431 final AppWindowToken atoken = mAppToken;
7432 return (mSurface != null
7433 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7434 && mPolicyVisibility && !mAttachedHidden
7435 && (atoken == null || !atoken.hiddenRequested)
7436 && !mExiting && !mDestroying;
7437 }
7438
7439 /**
7440 * Is this window currently on-screen? It is on-screen either if it
7441 * is visible or it is currently running an animation before no longer
7442 * being visible.
7443 */
7444 boolean isOnScreen() {
7445 final AppWindowToken atoken = mAppToken;
7446 if (atoken != null) {
7447 return mSurface != null && mPolicyVisibility && !mDestroying
7448 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007449 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 } else {
7451 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007452 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 }
7454 }
Romain Guy06882f82009-06-10 13:36:04 -07007455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007456 /**
7457 * Like isOnScreen(), but we don't return true if the window is part
7458 * of a transition that has not yet been started.
7459 */
7460 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007461 if (mRootToken.waitingToShow &&
7462 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7463 return false;
7464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007466 final boolean animating = atoken != null
7467 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007469 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7470 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007471 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 }
7473
7474 /** Is the window or its container currently animating? */
7475 boolean isAnimating() {
7476 final WindowState attached = mAttachedWindow;
7477 final AppWindowToken atoken = mAppToken;
7478 return mAnimation != null
7479 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007480 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 (atoken.animation != null
7482 || atoken.inPendingTransaction));
7483 }
7484
7485 /** Is this window currently animating? */
7486 boolean isWindowAnimating() {
7487 return mAnimation != null;
7488 }
7489
7490 /**
7491 * Like isOnScreen, but returns false if the surface hasn't yet
7492 * been drawn.
7493 */
7494 public boolean isDisplayedLw() {
7495 final AppWindowToken atoken = mAppToken;
7496 return mSurface != null && mPolicyVisibility && !mDestroying
7497 && !mDrawPending && !mCommitDrawPending
7498 && ((!mAttachedHidden &&
7499 (atoken == null || !atoken.hiddenRequested))
7500 || mAnimating);
7501 }
7502
7503 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7504 boolean shownFrame, boolean onlyOpaque) {
7505 if (mSurface == null) {
7506 return false;
7507 }
7508 if (mAppToken != null && !mAppToken.appFullscreen) {
7509 return false;
7510 }
7511 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7512 return false;
7513 }
7514 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007515
7516 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7517 return frame.left <= mCompatibleScreenFrame.left &&
7518 frame.top <= mCompatibleScreenFrame.top &&
7519 frame.right >= mCompatibleScreenFrame.right &&
7520 frame.bottom >= mCompatibleScreenFrame.bottom;
7521 } else {
7522 return frame.left <= 0 && frame.top <= 0
7523 && frame.right >= screenWidth
7524 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 }
Romain Guy06882f82009-06-10 13:36:04 -07007527
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007528 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007529 * Return true if the window is opaque and fully drawn. This indicates
7530 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007531 */
7532 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007533 return (mAttrs.format == PixelFormat.OPAQUE
7534 || mAttrs.type == TYPE_WALLPAPER)
7535 && mSurface != null && mAnimation == null
7536 && (mAppToken == null || mAppToken.animation == null)
7537 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007538 }
7539
7540 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7541 return
7542 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007543 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7544 // only if it's visible
7545 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007546 // and only if the application fills the compatible screen
7547 mFrame.left <= mCompatibleScreenFrame.left &&
7548 mFrame.top <= mCompatibleScreenFrame.top &&
7549 mFrame.right >= mCompatibleScreenFrame.right &&
7550 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007551 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007552 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007553 }
7554
7555 boolean isFullscreen(int screenWidth, int screenHeight) {
7556 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007557 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 }
7559
7560 void removeLocked() {
7561 if (mAttachedWindow != null) {
7562 mAttachedWindow.mChildWindows.remove(this);
7563 }
7564 destroySurfaceLocked();
7565 mSession.windowRemovedLocked();
7566 try {
7567 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7568 } catch (RuntimeException e) {
7569 // Ignore if it has already been removed (usually because
7570 // we are doing this as part of processing a death note.)
7571 }
7572 }
7573
7574 private class DeathRecipient implements IBinder.DeathRecipient {
7575 public void binderDied() {
7576 try {
7577 synchronized(mWindowMap) {
7578 WindowState win = windowForClientLocked(mSession, mClient);
7579 Log.i(TAG, "WIN DEATH: " + win);
7580 if (win != null) {
7581 removeWindowLocked(mSession, win);
7582 }
7583 }
7584 } catch (IllegalArgumentException ex) {
7585 // This will happen if the window has already been
7586 // removed.
7587 }
7588 }
7589 }
7590
7591 /** Returns true if this window desires key events. */
7592 public final boolean canReceiveKeys() {
7593 return isVisibleOrAdding()
7594 && (mViewVisibility == View.VISIBLE)
7595 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7596 }
7597
7598 public boolean hasDrawnLw() {
7599 return mHasDrawn;
7600 }
7601
7602 public boolean showLw(boolean doAnimation) {
7603 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7604 mPolicyVisibility = true;
7605 mPolicyVisibilityAfterAnim = true;
7606 if (doAnimation) {
7607 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7608 }
7609 requestAnimationLocked(0);
7610 return true;
7611 }
7612 return false;
7613 }
7614
7615 public boolean hideLw(boolean doAnimation) {
7616 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7617 : mPolicyVisibility;
7618 if (current) {
7619 if (doAnimation) {
7620 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7621 if (mAnimation == null) {
7622 doAnimation = false;
7623 }
7624 }
7625 if (doAnimation) {
7626 mPolicyVisibilityAfterAnim = false;
7627 } else {
7628 mPolicyVisibilityAfterAnim = false;
7629 mPolicyVisibility = false;
7630 }
7631 requestAnimationLocked(0);
7632 return true;
7633 }
7634 return false;
7635 }
7636
7637 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007638 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007639
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007640 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7641 pw.print(" mClient="); pw.println(mClient.asBinder());
7642 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7643 if (mAttachedWindow != null || mLayoutAttached) {
7644 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7645 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7646 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007647 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7648 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7649 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007650 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7651 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007652 }
7653 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7654 pw.print(" mSubLayer="); pw.print(mSubLayer);
7655 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7656 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7657 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7658 pw.print("="); pw.print(mAnimLayer);
7659 pw.print(" mLastLayer="); pw.println(mLastLayer);
7660 if (mSurface != null) {
7661 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7662 }
7663 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7664 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7665 if (mAppToken != null) {
7666 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7667 }
7668 if (mTargetAppToken != null) {
7669 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7670 }
7671 pw.print(prefix); pw.print("mViewVisibility=0x");
7672 pw.print(Integer.toHexString(mViewVisibility));
7673 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007674 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7675 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007676 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7677 pw.print(prefix); pw.print("mPolicyVisibility=");
7678 pw.print(mPolicyVisibility);
7679 pw.print(" mPolicyVisibilityAfterAnim=");
7680 pw.print(mPolicyVisibilityAfterAnim);
7681 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7682 }
7683 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007684 pw.print(" h="); pw.println(mRequestedHeight);
7685 if (mXOffset != 0 || mYOffset != 0) {
7686 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7687 pw.print(" y="); pw.println(mYOffset);
7688 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007689 pw.print(prefix); pw.print("mGivenContentInsets=");
7690 mGivenContentInsets.printShortString(pw);
7691 pw.print(" mGivenVisibleInsets=");
7692 mGivenVisibleInsets.printShortString(pw);
7693 pw.println();
7694 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7695 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7696 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7697 }
7698 pw.print(prefix); pw.print("mShownFrame=");
7699 mShownFrame.printShortString(pw);
7700 pw.print(" last="); mLastShownFrame.printShortString(pw);
7701 pw.println();
7702 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7703 pw.print(" last="); mLastFrame.printShortString(pw);
7704 pw.println();
7705 pw.print(prefix); pw.print("mContainingFrame=");
7706 mContainingFrame.printShortString(pw);
7707 pw.print(" mDisplayFrame=");
7708 mDisplayFrame.printShortString(pw);
7709 pw.println();
7710 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7711 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7712 pw.println();
7713 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7714 pw.print(" last="); mLastContentInsets.printShortString(pw);
7715 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7716 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7717 pw.println();
7718 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7719 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7720 pw.print(" mAlpha="); pw.print(mAlpha);
7721 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7722 }
7723 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7724 || mAnimation != null) {
7725 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7726 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7727 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7728 pw.print(" mAnimation="); pw.println(mAnimation);
7729 }
7730 if (mHasTransformation || mHasLocalTransformation) {
7731 pw.print(prefix); pw.print("XForm: has=");
7732 pw.print(mHasTransformation);
7733 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7734 pw.print(" "); mTransformation.printShortString(pw);
7735 pw.println();
7736 }
7737 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7738 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7739 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7740 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7741 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7742 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7743 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7744 pw.print(" mDestroying="); pw.print(mDestroying);
7745 pw.print(" mRemoved="); pw.println(mRemoved);
7746 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007747 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007748 pw.print(prefix); pw.print("mOrientationChanging=");
7749 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007750 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7751 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007752 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007753 if (mHScale != 1 || mVScale != 1) {
7754 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7755 pw.print(" mVScale="); pw.println(mVScale);
7756 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007757 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007758 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7759 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 }
7762
7763 @Override
7764 public String toString() {
7765 return "Window{"
7766 + Integer.toHexString(System.identityHashCode(this))
7767 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7768 }
7769 }
Romain Guy06882f82009-06-10 13:36:04 -07007770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 // -------------------------------------------------------------
7772 // Window Token State
7773 // -------------------------------------------------------------
7774
7775 class WindowToken {
7776 // The actual token.
7777 final IBinder token;
7778
7779 // The type of window this token is for, as per WindowManager.LayoutParams.
7780 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 // Set if this token was explicitly added by a client, so should
7783 // not be removed when all windows are removed.
7784 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007785
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007786 // For printing.
7787 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 // If this is an AppWindowToken, this is non-null.
7790 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 // All of the windows associated with this token.
7793 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7794
7795 // Is key dispatching paused for this token?
7796 boolean paused = false;
7797
7798 // Should this token's windows be hidden?
7799 boolean hidden;
7800
7801 // Temporary for finding which tokens no longer have visible windows.
7802 boolean hasVisible;
7803
Dianne Hackborna8f60182009-09-01 19:01:50 -07007804 // Set to true when this token is in a pending transaction where it
7805 // will be shown.
7806 boolean waitingToShow;
7807
7808 // Set to true when this token is in a pending transaction where it
7809 // will be hidden.
7810 boolean waitingToHide;
7811
7812 // Set to true when this token is in a pending transaction where its
7813 // windows will be put to the bottom of the list.
7814 boolean sendingToBottom;
7815
7816 // Set to true when this token is in a pending transaction where its
7817 // windows will be put to the top of the list.
7818 boolean sendingToTop;
7819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 WindowToken(IBinder _token, int type, boolean _explicit) {
7821 token = _token;
7822 windowType = type;
7823 explicit = _explicit;
7824 }
7825
7826 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007827 pw.print(prefix); pw.print("token="); pw.println(token);
7828 pw.print(prefix); pw.print("windows="); pw.println(windows);
7829 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7830 pw.print(" hidden="); pw.print(hidden);
7831 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007832 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7833 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7834 pw.print(" waitingToHide="); pw.print(waitingToHide);
7835 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7836 pw.print(" sendingToTop="); pw.println(sendingToTop);
7837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 }
7839
7840 @Override
7841 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007842 if (stringName == null) {
7843 StringBuilder sb = new StringBuilder();
7844 sb.append("WindowToken{");
7845 sb.append(Integer.toHexString(System.identityHashCode(this)));
7846 sb.append(" token="); sb.append(token); sb.append('}');
7847 stringName = sb.toString();
7848 }
7849 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007850 }
7851 };
7852
7853 class AppWindowToken extends WindowToken {
7854 // Non-null only for application tokens.
7855 final IApplicationToken appToken;
7856
7857 // All of the windows and child windows that are included in this
7858 // application token. Note this list is NOT sorted!
7859 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7860
7861 int groupId = -1;
7862 boolean appFullscreen;
7863 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 // These are used for determining when all windows associated with
7866 // an activity have been drawn, so they can be made visible together
7867 // at the same time.
7868 int lastTransactionSequence = mTransactionSequence-1;
7869 int numInterestingWindows;
7870 int numDrawnWindows;
7871 boolean inPendingTransaction;
7872 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 // Is this token going to be hidden in a little while? If so, it
7875 // won't be taken into account for setting the screen orientation.
7876 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 // Is this window's surface needed? This is almost like hidden, except
7879 // it will sometimes be true a little earlier: when the token has
7880 // been shown, but is still waiting for its app transition to execute
7881 // before making its windows shown.
7882 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 // Have we told the window clients to hide themselves?
7885 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 // Last visibility state we reported to the app token.
7888 boolean reportedVisible;
7889
7890 // Set to true when the token has been removed from the window mgr.
7891 boolean removed;
7892
7893 // Have we been asked to have this token keep the screen frozen?
7894 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896 boolean animating;
7897 Animation animation;
7898 boolean hasTransformation;
7899 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 // Offset to the window of all layers in the token, for use by
7902 // AppWindowToken animations.
7903 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 // Information about an application starting window if displayed.
7906 StartingData startingData;
7907 WindowState startingWindow;
7908 View startingView;
7909 boolean startingDisplayed;
7910 boolean startingMoved;
7911 boolean firstWindowDrawn;
7912
7913 AppWindowToken(IApplicationToken _token) {
7914 super(_token.asBinder(),
7915 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7916 appWindowToken = this;
7917 appToken = _token;
7918 }
Romain Guy06882f82009-06-10 13:36:04 -07007919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 public void setAnimation(Animation anim) {
7921 if (localLOGV) Log.v(
7922 TAG, "Setting animation in " + this + ": " + anim);
7923 animation = anim;
7924 animating = false;
7925 anim.restrictDuration(MAX_ANIMATION_DURATION);
7926 anim.scaleCurrentDuration(mTransitionAnimationScale);
7927 int zorder = anim.getZAdjustment();
7928 int adj = 0;
7929 if (zorder == Animation.ZORDER_TOP) {
7930 adj = TYPE_LAYER_OFFSET;
7931 } else if (zorder == Animation.ZORDER_BOTTOM) {
7932 adj = -TYPE_LAYER_OFFSET;
7933 }
Romain Guy06882f82009-06-10 13:36:04 -07007934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 if (animLayerAdjustment != adj) {
7936 animLayerAdjustment = adj;
7937 updateLayers();
7938 }
7939 }
Romain Guy06882f82009-06-10 13:36:04 -07007940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 public void setDummyAnimation() {
7942 if (animation == null) {
7943 if (localLOGV) Log.v(
7944 TAG, "Setting dummy animation in " + this);
7945 animation = sDummyAnimation;
7946 }
7947 }
7948
7949 public void clearAnimation() {
7950 if (animation != null) {
7951 animation = null;
7952 animating = true;
7953 }
7954 }
Romain Guy06882f82009-06-10 13:36:04 -07007955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 void updateLayers() {
7957 final int N = allAppWindows.size();
7958 final int adj = animLayerAdjustment;
7959 for (int i=0; i<N; i++) {
7960 WindowState w = allAppWindows.get(i);
7961 w.mAnimLayer = w.mLayer + adj;
7962 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7963 + w.mAnimLayer);
7964 if (w == mInputMethodTarget) {
7965 setInputMethodAnimLayerAdjustment(adj);
7966 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007967 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007968 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007970 }
7971 }
Romain Guy06882f82009-06-10 13:36:04 -07007972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 void sendAppVisibilityToClients() {
7974 final int N = allAppWindows.size();
7975 for (int i=0; i<N; i++) {
7976 WindowState win = allAppWindows.get(i);
7977 if (win == startingWindow && clientHidden) {
7978 // Don't hide the starting window.
7979 continue;
7980 }
7981 try {
7982 if (DEBUG_VISIBILITY) Log.v(TAG,
7983 "Setting visibility of " + win + ": " + (!clientHidden));
7984 win.mClient.dispatchAppVisibility(!clientHidden);
7985 } catch (RemoteException e) {
7986 }
7987 }
7988 }
Romain Guy06882f82009-06-10 13:36:04 -07007989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007990 void showAllWindowsLocked() {
7991 final int NW = allAppWindows.size();
7992 for (int i=0; i<NW; i++) {
7993 WindowState w = allAppWindows.get(i);
7994 if (DEBUG_VISIBILITY) Log.v(TAG,
7995 "performing show on: " + w);
7996 w.performShowLocked();
7997 }
7998 }
Romain Guy06882f82009-06-10 13:36:04 -07007999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 // This must be called while inside a transaction.
8001 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8002 if (!mDisplayFrozen) {
8003 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008005 if (animation == sDummyAnimation) {
8006 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008007 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 // when it is really time to animate, this will be set to
8009 // a real animation and the next call will execute normally.
8010 return false;
8011 }
Romain Guy06882f82009-06-10 13:36:04 -07008012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8014 if (!animating) {
8015 if (DEBUG_ANIM) Log.v(
8016 TAG, "Starting animation in " + this +
8017 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8018 + " scale=" + mTransitionAnimationScale
8019 + " allDrawn=" + allDrawn + " animating=" + animating);
8020 animation.initialize(dw, dh, dw, dh);
8021 animation.setStartTime(currentTime);
8022 animating = true;
8023 }
8024 transformation.clear();
8025 final boolean more = animation.getTransformation(
8026 currentTime, transformation);
8027 if (DEBUG_ANIM) Log.v(
8028 TAG, "Stepped animation in " + this +
8029 ": more=" + more + ", xform=" + transformation);
8030 if (more) {
8031 // we're done!
8032 hasTransformation = true;
8033 return true;
8034 }
8035 if (DEBUG_ANIM) Log.v(
8036 TAG, "Finished animation in " + this +
8037 " @ " + currentTime);
8038 animation = null;
8039 }
8040 } else if (animation != null) {
8041 // If the display is frozen, and there is a pending animation,
8042 // clear it and make sure we run the cleanup code.
8043 animating = true;
8044 animation = null;
8045 }
8046
8047 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008049 if (!animating) {
8050 return false;
8051 }
8052
8053 clearAnimation();
8054 animating = false;
8055 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8056 moveInputMethodWindowsIfNeededLocked(true);
8057 }
Romain Guy06882f82009-06-10 13:36:04 -07008058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059 if (DEBUG_ANIM) Log.v(
8060 TAG, "Animation done in " + this
8061 + ": reportedVisible=" + reportedVisible);
8062
8063 transformation.clear();
8064 if (animLayerAdjustment != 0) {
8065 animLayerAdjustment = 0;
8066 updateLayers();
8067 }
Romain Guy06882f82009-06-10 13:36:04 -07008068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 final int N = windows.size();
8070 for (int i=0; i<N; i++) {
8071 ((WindowState)windows.get(i)).finishExit();
8072 }
8073 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 return false;
8076 }
8077
8078 void updateReportedVisibilityLocked() {
8079 if (appToken == null) {
8080 return;
8081 }
Romain Guy06882f82009-06-10 13:36:04 -07008082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 int numInteresting = 0;
8084 int numVisible = 0;
8085 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8088 final int N = allAppWindows.size();
8089 for (int i=0; i<N; i++) {
8090 WindowState win = allAppWindows.get(i);
8091 if (win == startingWindow || win.mAppFreezing) {
8092 continue;
8093 }
8094 if (DEBUG_VISIBILITY) {
8095 Log.v(TAG, "Win " + win + ": isDisplayed="
8096 + win.isDisplayedLw()
8097 + ", isAnimating=" + win.isAnimating());
8098 if (!win.isDisplayedLw()) {
8099 Log.v(TAG, "Not displayed: s=" + win.mSurface
8100 + " pv=" + win.mPolicyVisibility
8101 + " dp=" + win.mDrawPending
8102 + " cdp=" + win.mCommitDrawPending
8103 + " ah=" + win.mAttachedHidden
8104 + " th="
8105 + (win.mAppToken != null
8106 ? win.mAppToken.hiddenRequested : false)
8107 + " a=" + win.mAnimating);
8108 }
8109 }
8110 numInteresting++;
8111 if (win.isDisplayedLw()) {
8112 if (!win.isAnimating()) {
8113 numVisible++;
8114 }
8115 nowGone = false;
8116 } else if (win.isAnimating()) {
8117 nowGone = false;
8118 }
8119 }
Romain Guy06882f82009-06-10 13:36:04 -07008120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8122 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8123 + numInteresting + " visible=" + numVisible);
8124 if (nowVisible != reportedVisible) {
8125 if (DEBUG_VISIBILITY) Log.v(
8126 TAG, "Visibility changed in " + this
8127 + ": vis=" + nowVisible);
8128 reportedVisible = nowVisible;
8129 Message m = mH.obtainMessage(
8130 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8131 nowVisible ? 1 : 0,
8132 nowGone ? 1 : 0,
8133 this);
8134 mH.sendMessage(m);
8135 }
8136 }
Romain Guy06882f82009-06-10 13:36:04 -07008137
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008138 WindowState findMainWindow() {
8139 int j = windows.size();
8140 while (j > 0) {
8141 j--;
8142 WindowState win = windows.get(j);
8143 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8144 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8145 return win;
8146 }
8147 }
8148 return null;
8149 }
8150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 void dump(PrintWriter pw, String prefix) {
8152 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008153 if (appToken != null) {
8154 pw.print(prefix); pw.println("app=true");
8155 }
8156 if (allAppWindows.size() > 0) {
8157 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8158 }
8159 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008160 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008161 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8162 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8163 pw.print(" clientHidden="); pw.print(clientHidden);
8164 pw.print(" willBeHidden="); pw.print(willBeHidden);
8165 pw.print(" reportedVisible="); pw.println(reportedVisible);
8166 if (paused || freezingScreen) {
8167 pw.print(prefix); pw.print("paused="); pw.print(paused);
8168 pw.print(" freezingScreen="); pw.println(freezingScreen);
8169 }
8170 if (numInterestingWindows != 0 || numDrawnWindows != 0
8171 || inPendingTransaction || allDrawn) {
8172 pw.print(prefix); pw.print("numInterestingWindows=");
8173 pw.print(numInterestingWindows);
8174 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8175 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8176 pw.print(" allDrawn="); pw.println(allDrawn);
8177 }
8178 if (animating || animation != null) {
8179 pw.print(prefix); pw.print("animating="); pw.print(animating);
8180 pw.print(" animation="); pw.println(animation);
8181 }
8182 if (animLayerAdjustment != 0) {
8183 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8184 }
8185 if (hasTransformation) {
8186 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8187 pw.print(" transformation="); transformation.printShortString(pw);
8188 pw.println();
8189 }
8190 if (startingData != null || removed || firstWindowDrawn) {
8191 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8192 pw.print(" removed="); pw.print(removed);
8193 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8194 }
8195 if (startingWindow != null || startingView != null
8196 || startingDisplayed || startingMoved) {
8197 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8198 pw.print(" startingView="); pw.print(startingView);
8199 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8200 pw.print(" startingMoved"); pw.println(startingMoved);
8201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202 }
8203
8204 @Override
8205 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008206 if (stringName == null) {
8207 StringBuilder sb = new StringBuilder();
8208 sb.append("AppWindowToken{");
8209 sb.append(Integer.toHexString(System.identityHashCode(this)));
8210 sb.append(" token="); sb.append(token); sb.append('}');
8211 stringName = sb.toString();
8212 }
8213 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 }
8215 }
Romain Guy06882f82009-06-10 13:36:04 -07008216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 // -------------------------------------------------------------
8218 // DummyAnimation
8219 // -------------------------------------------------------------
8220
8221 // This is an animation that does nothing: it just immediately finishes
8222 // itself every time it is called. It is used as a stub animation in cases
8223 // where we want to synchronize multiple things that may be animating.
8224 static final class DummyAnimation extends Animation {
8225 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8226 return false;
8227 }
8228 }
8229 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 // -------------------------------------------------------------
8232 // Async Handler
8233 // -------------------------------------------------------------
8234
8235 static final class StartingData {
8236 final String pkg;
8237 final int theme;
8238 final CharSequence nonLocalizedLabel;
8239 final int labelRes;
8240 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8243 int _labelRes, int _icon) {
8244 pkg = _pkg;
8245 theme = _theme;
8246 nonLocalizedLabel = _nonLocalizedLabel;
8247 labelRes = _labelRes;
8248 icon = _icon;
8249 }
8250 }
8251
8252 private final class H extends Handler {
8253 public static final int REPORT_FOCUS_CHANGE = 2;
8254 public static final int REPORT_LOSING_FOCUS = 3;
8255 public static final int ANIMATE = 4;
8256 public static final int ADD_STARTING = 5;
8257 public static final int REMOVE_STARTING = 6;
8258 public static final int FINISHED_STARTING = 7;
8259 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8261 public static final int HOLD_SCREEN_CHANGED = 12;
8262 public static final int APP_TRANSITION_TIMEOUT = 13;
8263 public static final int PERSIST_ANIMATION_SCALE = 14;
8264 public static final int FORCE_GC = 15;
8265 public static final int ENABLE_SCREEN = 16;
8266 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008267 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008271 public H() {
8272 }
Romain Guy06882f82009-06-10 13:36:04 -07008273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 @Override
8275 public void handleMessage(Message msg) {
8276 switch (msg.what) {
8277 case REPORT_FOCUS_CHANGE: {
8278 WindowState lastFocus;
8279 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 synchronized(mWindowMap) {
8282 lastFocus = mLastFocus;
8283 newFocus = mCurrentFocus;
8284 if (lastFocus == newFocus) {
8285 // Focus is not changing, so nothing to do.
8286 return;
8287 }
8288 mLastFocus = newFocus;
8289 //Log.i(TAG, "Focus moving from " + lastFocus
8290 // + " to " + newFocus);
8291 if (newFocus != null && lastFocus != null
8292 && !newFocus.isDisplayedLw()) {
8293 //Log.i(TAG, "Delaying loss of focus...");
8294 mLosingFocus.add(lastFocus);
8295 lastFocus = null;
8296 }
8297 }
8298
8299 if (lastFocus != newFocus) {
8300 //System.out.println("Changing focus from " + lastFocus
8301 // + " to " + newFocus);
8302 if (newFocus != null) {
8303 try {
8304 //Log.i(TAG, "Gaining focus: " + newFocus);
8305 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8306 } catch (RemoteException e) {
8307 // Ignore if process has died.
8308 }
8309 }
8310
8311 if (lastFocus != null) {
8312 try {
8313 //Log.i(TAG, "Losing focus: " + lastFocus);
8314 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8315 } catch (RemoteException e) {
8316 // Ignore if process has died.
8317 }
8318 }
8319 }
8320 } break;
8321
8322 case REPORT_LOSING_FOCUS: {
8323 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325 synchronized(mWindowMap) {
8326 losers = mLosingFocus;
8327 mLosingFocus = new ArrayList<WindowState>();
8328 }
8329
8330 final int N = losers.size();
8331 for (int i=0; i<N; i++) {
8332 try {
8333 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8334 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8335 } catch (RemoteException e) {
8336 // Ignore if process has died.
8337 }
8338 }
8339 } break;
8340
8341 case ANIMATE: {
8342 synchronized(mWindowMap) {
8343 mAnimationPending = false;
8344 performLayoutAndPlaceSurfacesLocked();
8345 }
8346 } break;
8347
8348 case ADD_STARTING: {
8349 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8350 final StartingData sd = wtoken.startingData;
8351
8352 if (sd == null) {
8353 // Animation has been canceled... do nothing.
8354 return;
8355 }
Romain Guy06882f82009-06-10 13:36:04 -07008356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8358 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 View view = null;
8361 try {
8362 view = mPolicy.addStartingWindow(
8363 wtoken.token, sd.pkg,
8364 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8365 sd.icon);
8366 } catch (Exception e) {
8367 Log.w(TAG, "Exception when adding starting window", e);
8368 }
8369
8370 if (view != null) {
8371 boolean abort = false;
8372
8373 synchronized(mWindowMap) {
8374 if (wtoken.removed || wtoken.startingData == null) {
8375 // If the window was successfully added, then
8376 // we need to remove it.
8377 if (wtoken.startingWindow != null) {
8378 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8379 "Aborted starting " + wtoken
8380 + ": removed=" + wtoken.removed
8381 + " startingData=" + wtoken.startingData);
8382 wtoken.startingWindow = null;
8383 wtoken.startingData = null;
8384 abort = true;
8385 }
8386 } else {
8387 wtoken.startingView = view;
8388 }
8389 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8390 "Added starting " + wtoken
8391 + ": startingWindow="
8392 + wtoken.startingWindow + " startingView="
8393 + wtoken.startingView);
8394 }
8395
8396 if (abort) {
8397 try {
8398 mPolicy.removeStartingWindow(wtoken.token, view);
8399 } catch (Exception e) {
8400 Log.w(TAG, "Exception when removing starting window", e);
8401 }
8402 }
8403 }
8404 } break;
8405
8406 case REMOVE_STARTING: {
8407 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8408 IBinder token = null;
8409 View view = null;
8410 synchronized (mWindowMap) {
8411 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8412 + wtoken + ": startingWindow="
8413 + wtoken.startingWindow + " startingView="
8414 + wtoken.startingView);
8415 if (wtoken.startingWindow != null) {
8416 view = wtoken.startingView;
8417 token = wtoken.token;
8418 wtoken.startingData = null;
8419 wtoken.startingView = null;
8420 wtoken.startingWindow = null;
8421 }
8422 }
8423 if (view != null) {
8424 try {
8425 mPolicy.removeStartingWindow(token, view);
8426 } catch (Exception e) {
8427 Log.w(TAG, "Exception when removing starting window", e);
8428 }
8429 }
8430 } break;
8431
8432 case FINISHED_STARTING: {
8433 IBinder token = null;
8434 View view = null;
8435 while (true) {
8436 synchronized (mWindowMap) {
8437 final int N = mFinishedStarting.size();
8438 if (N <= 0) {
8439 break;
8440 }
8441 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8442
8443 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8444 "Finished starting " + wtoken
8445 + ": startingWindow=" + wtoken.startingWindow
8446 + " startingView=" + wtoken.startingView);
8447
8448 if (wtoken.startingWindow == null) {
8449 continue;
8450 }
8451
8452 view = wtoken.startingView;
8453 token = wtoken.token;
8454 wtoken.startingData = null;
8455 wtoken.startingView = null;
8456 wtoken.startingWindow = null;
8457 }
8458
8459 try {
8460 mPolicy.removeStartingWindow(token, view);
8461 } catch (Exception e) {
8462 Log.w(TAG, "Exception when removing starting window", e);
8463 }
8464 }
8465 } break;
8466
8467 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8468 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8469
8470 boolean nowVisible = msg.arg1 != 0;
8471 boolean nowGone = msg.arg2 != 0;
8472
8473 try {
8474 if (DEBUG_VISIBILITY) Log.v(
8475 TAG, "Reporting visible in " + wtoken
8476 + " visible=" + nowVisible
8477 + " gone=" + nowGone);
8478 if (nowVisible) {
8479 wtoken.appToken.windowsVisible();
8480 } else {
8481 wtoken.appToken.windowsGone();
8482 }
8483 } catch (RemoteException ex) {
8484 }
8485 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008487 case WINDOW_FREEZE_TIMEOUT: {
8488 synchronized (mWindowMap) {
8489 Log.w(TAG, "Window freeze timeout expired.");
8490 int i = mWindows.size();
8491 while (i > 0) {
8492 i--;
8493 WindowState w = (WindowState)mWindows.get(i);
8494 if (w.mOrientationChanging) {
8495 w.mOrientationChanging = false;
8496 Log.w(TAG, "Force clearing orientation change: " + w);
8497 }
8498 }
8499 performLayoutAndPlaceSurfacesLocked();
8500 }
8501 break;
8502 }
Romain Guy06882f82009-06-10 13:36:04 -07008503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 case HOLD_SCREEN_CHANGED: {
8505 Session oldHold;
8506 Session newHold;
8507 synchronized (mWindowMap) {
8508 oldHold = mLastReportedHold;
8509 newHold = (Session)msg.obj;
8510 mLastReportedHold = newHold;
8511 }
Romain Guy06882f82009-06-10 13:36:04 -07008512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513 if (oldHold != newHold) {
8514 try {
8515 if (oldHold != null) {
8516 mBatteryStats.noteStopWakelock(oldHold.mUid,
8517 "window",
8518 BatteryStats.WAKE_TYPE_WINDOW);
8519 }
8520 if (newHold != null) {
8521 mBatteryStats.noteStartWakelock(newHold.mUid,
8522 "window",
8523 BatteryStats.WAKE_TYPE_WINDOW);
8524 }
8525 } catch (RemoteException e) {
8526 }
8527 }
8528 break;
8529 }
Romain Guy06882f82009-06-10 13:36:04 -07008530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008531 case APP_TRANSITION_TIMEOUT: {
8532 synchronized (mWindowMap) {
8533 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8534 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8535 "*** APP TRANSITION TIMEOUT");
8536 mAppTransitionReady = true;
8537 mAppTransitionTimeout = true;
8538 performLayoutAndPlaceSurfacesLocked();
8539 }
8540 }
8541 break;
8542 }
Romain Guy06882f82009-06-10 13:36:04 -07008543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008544 case PERSIST_ANIMATION_SCALE: {
8545 Settings.System.putFloat(mContext.getContentResolver(),
8546 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8547 Settings.System.putFloat(mContext.getContentResolver(),
8548 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8549 break;
8550 }
Romain Guy06882f82009-06-10 13:36:04 -07008551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008552 case FORCE_GC: {
8553 synchronized(mWindowMap) {
8554 if (mAnimationPending) {
8555 // If we are animating, don't do the gc now but
8556 // delay a bit so we don't interrupt the animation.
8557 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8558 2000);
8559 return;
8560 }
8561 // If we are currently rotating the display, it will
8562 // schedule a new message when done.
8563 if (mDisplayFrozen) {
8564 return;
8565 }
8566 mFreezeGcPending = 0;
8567 }
8568 Runtime.getRuntime().gc();
8569 break;
8570 }
Romain Guy06882f82009-06-10 13:36:04 -07008571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 case ENABLE_SCREEN: {
8573 performEnableScreen();
8574 break;
8575 }
Romain Guy06882f82009-06-10 13:36:04 -07008576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008577 case APP_FREEZE_TIMEOUT: {
8578 synchronized (mWindowMap) {
8579 Log.w(TAG, "App freeze timeout expired.");
8580 int i = mAppTokens.size();
8581 while (i > 0) {
8582 i--;
8583 AppWindowToken tok = mAppTokens.get(i);
8584 if (tok.freezingScreen) {
8585 Log.w(TAG, "Force clearing freeze: " + tok);
8586 unsetAppFreezingScreenLocked(tok, true, true);
8587 }
8588 }
8589 }
8590 break;
8591 }
Romain Guy06882f82009-06-10 13:36:04 -07008592
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008593 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008594 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008595 sendNewConfiguration();
8596 }
8597 break;
8598 }
Romain Guy06882f82009-06-10 13:36:04 -07008599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 }
8601 }
8602 }
8603
8604 // -------------------------------------------------------------
8605 // IWindowManager API
8606 // -------------------------------------------------------------
8607
8608 public IWindowSession openSession(IInputMethodClient client,
8609 IInputContext inputContext) {
8610 if (client == null) throw new IllegalArgumentException("null client");
8611 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8612 return new Session(client, inputContext);
8613 }
8614
8615 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8616 synchronized (mWindowMap) {
8617 // The focus for the client is the window immediately below
8618 // where we would place the input method window.
8619 int idx = findDesiredInputMethodWindowIndexLocked(false);
8620 WindowState imFocus;
8621 if (idx > 0) {
8622 imFocus = (WindowState)mWindows.get(idx-1);
8623 if (imFocus != null) {
8624 if (imFocus.mSession.mClient != null &&
8625 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8626 return true;
8627 }
8628 }
8629 }
8630 }
8631 return false;
8632 }
Romain Guy06882f82009-06-10 13:36:04 -07008633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008634 // -------------------------------------------------------------
8635 // Internals
8636 // -------------------------------------------------------------
8637
8638 final WindowState windowForClientLocked(Session session, IWindow client) {
8639 return windowForClientLocked(session, client.asBinder());
8640 }
Romain Guy06882f82009-06-10 13:36:04 -07008641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642 final WindowState windowForClientLocked(Session session, IBinder client) {
8643 WindowState win = mWindowMap.get(client);
8644 if (localLOGV) Log.v(
8645 TAG, "Looking up client " + client + ": " + win);
8646 if (win == null) {
8647 RuntimeException ex = new RuntimeException();
8648 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8649 return null;
8650 }
8651 if (session != null && win.mSession != session) {
8652 RuntimeException ex = new RuntimeException();
8653 Log.w(TAG, "Requested window " + client + " is in session " +
8654 win.mSession + ", not " + session, ex);
8655 return null;
8656 }
8657
8658 return win;
8659 }
8660
Dianne Hackborna8f60182009-09-01 19:01:50 -07008661 final void rebuildAppWindowListLocked() {
8662 int NW = mWindows.size();
8663 int i;
8664
8665 // First remove all existing app windows.
8666 i=0;
8667 while (i < NW) {
8668 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008669 WindowState win = (WindowState)mWindows.remove(i);
8670 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8671 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008672 NW--;
8673 continue;
8674 }
8675 i++;
8676 }
8677
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008678 // First add all of the exiting app tokens... these are no longer
8679 // in the main app list, but still have windows shown. We put them
8680 // in the back because now that the animation is over we no longer
8681 // will care about them.
8682 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008683 i = 0;
8684 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008685 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8686 }
8687
8688 // And add in the still active app tokens in Z order.
8689 NT = mAppTokens.size();
8690 for (int j=0; j<NT; j++) {
8691 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008692 }
8693 }
8694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 private final void assignLayersLocked() {
8696 int N = mWindows.size();
8697 int curBaseLayer = 0;
8698 int curLayer = 0;
8699 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 for (i=0; i<N; i++) {
8702 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008703 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8704 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 curLayer += WINDOW_LAYER_MULTIPLIER;
8706 w.mLayer = curLayer;
8707 } else {
8708 curBaseLayer = curLayer = w.mBaseLayer;
8709 w.mLayer = curLayer;
8710 }
8711 if (w.mTargetAppToken != null) {
8712 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8713 } else if (w.mAppToken != null) {
8714 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8715 } else {
8716 w.mAnimLayer = w.mLayer;
8717 }
8718 if (w.mIsImWindow) {
8719 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008720 } else if (w.mIsWallpaper) {
8721 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 }
8723 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8724 + w.mAnimLayer);
8725 //System.out.println(
8726 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8727 }
8728 }
8729
8730 private boolean mInLayout = false;
8731 private final void performLayoutAndPlaceSurfacesLocked() {
8732 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008733 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008734 throw new RuntimeException("Recursive call!");
8735 }
8736 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8737 return;
8738 }
8739
8740 boolean recoveringMemory = false;
8741 if (mForceRemoves != null) {
8742 recoveringMemory = true;
8743 // Wait a little it for things to settle down, and off we go.
8744 for (int i=0; i<mForceRemoves.size(); i++) {
8745 WindowState ws = mForceRemoves.get(i);
8746 Log.i(TAG, "Force removing: " + ws);
8747 removeWindowInnerLocked(ws.mSession, ws);
8748 }
8749 mForceRemoves = null;
8750 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8751 Object tmp = new Object();
8752 synchronized (tmp) {
8753 try {
8754 tmp.wait(250);
8755 } catch (InterruptedException e) {
8756 }
8757 }
8758 }
Romain Guy06882f82009-06-10 13:36:04 -07008759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008760 mInLayout = true;
8761 try {
8762 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 int i = mPendingRemove.size()-1;
8765 if (i >= 0) {
8766 while (i >= 0) {
8767 WindowState w = mPendingRemove.get(i);
8768 removeWindowInnerLocked(w.mSession, w);
8769 i--;
8770 }
8771 mPendingRemove.clear();
8772
8773 mInLayout = false;
8774 assignLayersLocked();
8775 mLayoutNeeded = true;
8776 performLayoutAndPlaceSurfacesLocked();
8777
8778 } else {
8779 mInLayout = false;
8780 if (mLayoutNeeded) {
8781 requestAnimationLocked(0);
8782 }
8783 }
8784 } catch (RuntimeException e) {
8785 mInLayout = false;
8786 Log.e(TAG, "Unhandled exception while layout out windows", e);
8787 }
8788 }
8789
8790 private final void performLayoutLockedInner() {
8791 final int dw = mDisplay.getWidth();
8792 final int dh = mDisplay.getHeight();
8793
8794 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008795 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008796 int i;
8797
8798 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008799
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008800 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801 mPolicy.beginLayoutLw(dw, dh);
8802
8803 // First perform layout of any root windows (not attached
8804 // to another window).
8805 int topAttached = -1;
8806 for (i = N-1; i >= 0; i--) {
8807 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008808
8809 // Don't do layout of a window if it is not visible, or
8810 // soon won't be visible, to avoid wasting time and funky
8811 // changes while a window is animating away.
8812 final AppWindowToken atoken = win.mAppToken;
8813 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008815 || win.mRootToken.hidden
8816 || (atoken != null && atoken.hiddenRequested)
8817 || !win.mPolicyVisibility
8818 || win.mAttachedHidden
8819 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820
8821 // If this view is GONE, then skip it -- keep the current
8822 // frame, and let the caller know so they can ignore it
8823 // if they want. (We do the normal layout for INVISIBLE
8824 // windows, since that means "perform layout as normal,
8825 // just don't display").
8826 if (!gone || !win.mHaveFrame) {
8827 if (!win.mLayoutAttached) {
8828 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8829 } else {
8830 if (topAttached < 0) topAttached = i;
8831 }
8832 }
8833 }
Romain Guy06882f82009-06-10 13:36:04 -07008834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 // Now perform layout of attached windows, which usually
8836 // depend on the position of the window they are attached to.
8837 // XXX does not deal with windows that are attached to windows
8838 // that are themselves attached.
8839 for (i = topAttached; i >= 0; i--) {
8840 WindowState win = (WindowState) mWindows.get(i);
8841
8842 // If this view is GONE, then skip it -- keep the current
8843 // frame, and let the caller know so they can ignore it
8844 // if they want. (We do the normal layout for INVISIBLE
8845 // windows, since that means "perform layout as normal,
8846 // just don't display").
8847 if (win.mLayoutAttached) {
8848 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8849 || !win.mHaveFrame) {
8850 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8851 }
8852 }
8853 }
8854
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008855 if (!mPolicy.finishLayoutLw()) {
8856 mLayoutNeeded = false;
8857 } else if (repeats > 2) {
8858 Log.w(TAG, "Layout repeat aborted after too many iterations");
8859 mLayoutNeeded = false;
8860 } else {
8861 repeats++;
8862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008863 }
8864 }
Romain Guy06882f82009-06-10 13:36:04 -07008865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008866 private final void performLayoutAndPlaceSurfacesLockedInner(
8867 boolean recoveringMemory) {
8868 final long currentTime = SystemClock.uptimeMillis();
8869 final int dw = mDisplay.getWidth();
8870 final int dh = mDisplay.getHeight();
8871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 int i;
8873
8874 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008875 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008877 if (mFxSession == null) {
8878 mFxSession = new SurfaceSession();
8879 }
Romain Guy06882f82009-06-10 13:36:04 -07008880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8882
8883 // Initialize state of exiting tokens.
8884 for (i=mExitingTokens.size()-1; i>=0; i--) {
8885 mExitingTokens.get(i).hasVisible = false;
8886 }
8887
8888 // Initialize state of exiting applications.
8889 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8890 mExitingAppTokens.get(i).hasVisible = false;
8891 }
8892
8893 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008894 boolean orientationChangeComplete = true;
8895 Session holdScreen = null;
8896 float screenBrightness = -1;
8897 boolean focusDisplayed = false;
8898 boolean animating = false;
8899
8900 Surface.openTransaction();
8901 try {
8902 boolean restart;
8903
8904 do {
8905 final int transactionSequence = ++mTransactionSequence;
8906
8907 // Update animations of all applications, including those
8908 // associated with exiting/removed apps
8909 boolean tokensAnimating = false;
8910 final int NAT = mAppTokens.size();
8911 for (i=0; i<NAT; i++) {
8912 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8913 tokensAnimating = true;
8914 }
8915 }
8916 final int NEAT = mExitingAppTokens.size();
8917 for (i=0; i<NEAT; i++) {
8918 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8919 tokensAnimating = true;
8920 }
8921 }
8922
8923 animating = tokensAnimating;
8924 restart = false;
8925
8926 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008927 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928
8929 mPolicy.beginAnimationLw(dw, dh);
8930
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008931 final int N = mWindows.size();
8932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 for (i=N-1; i>=0; i--) {
8934 WindowState w = (WindowState)mWindows.get(i);
8935
8936 final WindowManager.LayoutParams attrs = w.mAttrs;
8937
8938 if (w.mSurface != null) {
8939 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008940 if (w.commitFinishDrawingLocked(currentTime)) {
8941 if ((w.mAttrs.flags
8942 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008943 if (DEBUG_WALLPAPER) Log.v(TAG,
8944 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008945 wallpaperMayChange = true;
8946 }
8947 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008948 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008949 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8950 animating = true;
8951 //w.dump(" ");
8952 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008953 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
8954 wallpaperMayChange = true;
8955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008956 mPolicy.animatingWindowLw(w, attrs);
8957 }
8958
8959 final AppWindowToken atoken = w.mAppToken;
8960 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8961 if (atoken.lastTransactionSequence != transactionSequence) {
8962 atoken.lastTransactionSequence = transactionSequence;
8963 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8964 atoken.startingDisplayed = false;
8965 }
8966 if ((w.isOnScreen() || w.mAttrs.type
8967 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8968 && !w.mExiting && !w.mDestroying) {
8969 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8970 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8971 + w.isDisplayedLw()
8972 + ", isAnimating=" + w.isAnimating());
8973 if (!w.isDisplayedLw()) {
8974 Log.v(TAG, "Not displayed: s=" + w.mSurface
8975 + " pv=" + w.mPolicyVisibility
8976 + " dp=" + w.mDrawPending
8977 + " cdp=" + w.mCommitDrawPending
8978 + " ah=" + w.mAttachedHidden
8979 + " th=" + atoken.hiddenRequested
8980 + " a=" + w.mAnimating);
8981 }
8982 }
8983 if (w != atoken.startingWindow) {
8984 if (!atoken.freezingScreen || !w.mAppFreezing) {
8985 atoken.numInterestingWindows++;
8986 if (w.isDisplayedLw()) {
8987 atoken.numDrawnWindows++;
8988 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8989 "tokenMayBeDrawn: " + atoken
8990 + " freezingScreen=" + atoken.freezingScreen
8991 + " mAppFreezing=" + w.mAppFreezing);
8992 tokenMayBeDrawn = true;
8993 }
8994 }
8995 } else if (w.isDisplayedLw()) {
8996 atoken.startingDisplayed = true;
8997 }
8998 }
8999 } else if (w.mReadyToShow) {
9000 w.performShowLocked();
9001 }
9002 }
9003
9004 if (mPolicy.finishAnimationLw()) {
9005 restart = true;
9006 }
9007
9008 if (tokenMayBeDrawn) {
9009 // See if any windows have been drawn, so they (and others
9010 // associated with them) can now be shown.
9011 final int NT = mTokenList.size();
9012 for (i=0; i<NT; i++) {
9013 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9014 if (wtoken == null) {
9015 continue;
9016 }
9017 if (wtoken.freezingScreen) {
9018 int numInteresting = wtoken.numInterestingWindows;
9019 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9020 if (DEBUG_VISIBILITY) Log.v(TAG,
9021 "allDrawn: " + wtoken
9022 + " interesting=" + numInteresting
9023 + " drawn=" + wtoken.numDrawnWindows);
9024 wtoken.showAllWindowsLocked();
9025 unsetAppFreezingScreenLocked(wtoken, false, true);
9026 orientationChangeComplete = true;
9027 }
9028 } else if (!wtoken.allDrawn) {
9029 int numInteresting = wtoken.numInterestingWindows;
9030 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9031 if (DEBUG_VISIBILITY) Log.v(TAG,
9032 "allDrawn: " + wtoken
9033 + " interesting=" + numInteresting
9034 + " drawn=" + wtoken.numDrawnWindows);
9035 wtoken.allDrawn = true;
9036 restart = true;
9037
9038 // We can now show all of the drawn windows!
9039 if (!mOpeningApps.contains(wtoken)) {
9040 wtoken.showAllWindowsLocked();
9041 }
9042 }
9043 }
9044 }
9045 }
9046
9047 // If we are ready to perform an app transition, check through
9048 // all of the app tokens to be shown and see if they are ready
9049 // to go.
9050 if (mAppTransitionReady) {
9051 int NN = mOpeningApps.size();
9052 boolean goodToGo = true;
9053 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9054 "Checking " + NN + " opening apps (frozen="
9055 + mDisplayFrozen + " timeout="
9056 + mAppTransitionTimeout + ")...");
9057 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9058 // If the display isn't frozen, wait to do anything until
9059 // all of the apps are ready. Otherwise just go because
9060 // we'll unfreeze the display when everyone is ready.
9061 for (i=0; i<NN && goodToGo; i++) {
9062 AppWindowToken wtoken = mOpeningApps.get(i);
9063 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9064 "Check opening app" + wtoken + ": allDrawn="
9065 + wtoken.allDrawn + " startingDisplayed="
9066 + wtoken.startingDisplayed);
9067 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9068 && !wtoken.startingMoved) {
9069 goodToGo = false;
9070 }
9071 }
9072 }
9073 if (goodToGo) {
9074 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9075 int transit = mNextAppTransition;
9076 if (mSkipAppTransitionAnimation) {
9077 transit = WindowManagerPolicy.TRANSIT_NONE;
9078 }
9079 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9080 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009081 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 mAppTransitionTimeout = false;
9083 mStartingIconInTransition = false;
9084 mSkipAppTransitionAnimation = false;
9085
9086 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9087
Dianne Hackborna8f60182009-09-01 19:01:50 -07009088 // If there are applications waiting to come to the
9089 // top of the stack, now is the time to move their windows.
9090 // (Note that we don't do apps going to the bottom
9091 // here -- we want to keep their windows in the old
9092 // Z-order until the animation completes.)
9093 if (mToTopApps.size() > 0) {
9094 NN = mAppTokens.size();
9095 for (i=0; i<NN; i++) {
9096 AppWindowToken wtoken = mAppTokens.get(i);
9097 if (wtoken.sendingToTop) {
9098 wtoken.sendingToTop = false;
9099 moveAppWindowsLocked(wtoken, NN, false);
9100 }
9101 }
9102 mToTopApps.clear();
9103 }
9104
Dianne Hackborn25994b42009-09-04 14:21:19 -07009105 WindowState oldWallpaper = mWallpaperTarget;
9106
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009107 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009108 wallpaperMayChange = false;
9109
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009110 // The top-most window will supply the layout params,
9111 // and we will determine it below.
9112 LayoutParams animLp = null;
9113 int bestAnimLayer = -1;
9114
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009115 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009116 "New wallpaper target=" + mWallpaperTarget
9117 + ", lower target=" + mLowerWallpaperTarget
9118 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009119 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009120 // Do a first pass through the tokens for two
9121 // things:
9122 // (1) Determine if both the closing and opening
9123 // app token sets are wallpaper targets, in which
9124 // case special animations are needed
9125 // (since the wallpaper needs to stay static
9126 // behind them).
9127 // (2) Find the layout params of the top-most
9128 // application window in the tokens, which is
9129 // what will control the animation theme.
9130 final int NC = mClosingApps.size();
9131 NN = NC + mOpeningApps.size();
9132 for (i=0; i<NN; i++) {
9133 AppWindowToken wtoken;
9134 int mode;
9135 if (i < NC) {
9136 wtoken = mClosingApps.get(i);
9137 mode = 1;
9138 } else {
9139 wtoken = mOpeningApps.get(i-NC);
9140 mode = 2;
9141 }
9142 if (mLowerWallpaperTarget != null) {
9143 if (mLowerWallpaperTarget.mAppToken == wtoken
9144 || mUpperWallpaperTarget.mAppToken == wtoken) {
9145 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009146 }
9147 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009148 if (wtoken.appFullscreen) {
9149 WindowState ws = wtoken.findMainWindow();
9150 if (ws != null) {
9151 // If this is a compatibility mode
9152 // window, we will always use its anim.
9153 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9154 animLp = ws.mAttrs;
9155 bestAnimLayer = Integer.MAX_VALUE;
9156 } else if (ws.mLayer > bestAnimLayer) {
9157 animLp = ws.mAttrs;
9158 bestAnimLayer = ws.mLayer;
9159 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009160 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009161 }
9162 }
9163
Dianne Hackborn25994b42009-09-04 14:21:19 -07009164 if (foundWallpapers == 3) {
9165 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9166 "Wallpaper animation!");
9167 switch (transit) {
9168 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9169 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9170 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9171 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9172 break;
9173 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9174 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9175 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9176 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9177 break;
9178 }
9179 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9180 "New transit: " + transit);
9181 } else if (oldWallpaper != null) {
9182 // We are transitioning from an activity with
9183 // a wallpaper to one without.
9184 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9185 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9186 "New transit away from wallpaper: " + transit);
9187 } else if (mWallpaperTarget != null) {
9188 // We are transitioning from an activity without
9189 // a wallpaper to now showing the wallpaper
9190 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9191 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9192 "New transit into wallpaper: " + transit);
9193 }
9194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009195 NN = mOpeningApps.size();
9196 for (i=0; i<NN; i++) {
9197 AppWindowToken wtoken = mOpeningApps.get(i);
9198 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9199 "Now opening app" + wtoken);
9200 wtoken.reportedVisible = false;
9201 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009202 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009203 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009204 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009205 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 wtoken.showAllWindowsLocked();
9207 }
9208 NN = mClosingApps.size();
9209 for (i=0; i<NN; i++) {
9210 AppWindowToken wtoken = mClosingApps.get(i);
9211 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9212 "Now closing app" + wtoken);
9213 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009214 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009215 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009217 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 // Force the allDrawn flag, because we want to start
9219 // this guy's animations regardless of whether it's
9220 // gotten drawn.
9221 wtoken.allDrawn = true;
9222 }
9223
9224 mOpeningApps.clear();
9225 mClosingApps.clear();
9226
9227 // This has changed the visibility of windows, so perform
9228 // a new layout to get them all up-to-date.
9229 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009230 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9231 assignLayersLocked();
9232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 performLayoutLockedInner();
9234 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9235
9236 restart = true;
9237 }
9238 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009239
Dianne Hackborna8f60182009-09-01 19:01:50 -07009240 if (!animating && mAppTransitionRunning) {
9241 // We have finished the animation of an app transition. To do
9242 // this, we have delayed a lot of operations like showing and
9243 // hiding apps, moving apps in Z-order, etc. The app token list
9244 // reflects the correct Z-order, but the window list may now
9245 // be out of sync with it. So here we will just rebuild the
9246 // entire app window list. Fun!
9247 mAppTransitionRunning = false;
9248 // Clear information about apps that were moving.
9249 mToBottomApps.clear();
9250
9251 rebuildAppWindowListLocked();
9252 restart = true;
9253 moveInputMethodWindowsIfNeededLocked(false);
9254 wallpaperMayChange = true;
9255 mLayoutNeeded = true;
9256 }
9257
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009258 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009259 if (DEBUG_WALLPAPER) Log.v(TAG,
9260 "Wallpaper may change! Adjusting");
9261 int adjResult = adjustWallpaperWindowsLocked();
9262 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9263 if (DEBUG_WALLPAPER) Log.v(TAG,
9264 "Wallpaper layer changed: assigning layers + relayout");
9265 restart = true;
9266 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009267 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009268 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9269 if (DEBUG_WALLPAPER) Log.v(TAG,
9270 "Wallpaper visibility changed: relayout");
9271 restart = true;
9272 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009273 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009274 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009275 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009276 performLayoutLockedInner();
9277 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009278 }
9279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 } while (restart);
9281
9282 // THIRD LOOP: Update the surfaces of all windows.
9283
9284 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9285
9286 boolean obscured = false;
9287 boolean blurring = false;
9288 boolean dimming = false;
9289 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009290 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009291 boolean backgroundFillerShown = false;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009292 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009294 final int N = mWindows.size();
9295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 for (i=N-1; i>=0; i--) {
9297 WindowState w = (WindowState)mWindows.get(i);
9298
9299 boolean displayed = false;
9300 final WindowManager.LayoutParams attrs = w.mAttrs;
9301 final int attrFlags = attrs.flags;
9302
9303 if (w.mSurface != null) {
9304 w.computeShownFrameLocked();
9305 if (localLOGV) Log.v(
9306 TAG, "Placing surface #" + i + " " + w.mSurface
9307 + ": new=" + w.mShownFrame + ", old="
9308 + w.mLastShownFrame);
9309
9310 boolean resize;
9311 int width, height;
9312 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9313 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9314 w.mLastRequestedHeight != w.mRequestedHeight;
9315 // for a scaled surface, we just want to use
9316 // the requested size.
9317 width = w.mRequestedWidth;
9318 height = w.mRequestedHeight;
9319 w.mLastRequestedWidth = width;
9320 w.mLastRequestedHeight = height;
9321 w.mLastShownFrame.set(w.mShownFrame);
9322 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009323 if (SHOW_TRANSACTIONS) Log.i(
9324 TAG, " SURFACE " + w.mSurface
9325 + ": POS " + w.mShownFrame.left
9326 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9328 } catch (RuntimeException e) {
9329 Log.w(TAG, "Error positioning surface in " + w, e);
9330 if (!recoveringMemory) {
9331 reclaimSomeSurfaceMemoryLocked(w, "position");
9332 }
9333 }
9334 } else {
9335 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9336 width = w.mShownFrame.width();
9337 height = w.mShownFrame.height();
9338 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 }
9340
9341 if (resize) {
9342 if (width < 1) width = 1;
9343 if (height < 1) height = 1;
9344 if (w.mSurface != null) {
9345 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009346 if (SHOW_TRANSACTIONS) Log.i(
9347 TAG, " SURFACE " + w.mSurface + ": POS "
9348 + w.mShownFrame.left + ","
9349 + w.mShownFrame.top + " SIZE "
9350 + w.mShownFrame.width() + "x"
9351 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 w.mSurface.setSize(width, height);
9353 w.mSurface.setPosition(w.mShownFrame.left,
9354 w.mShownFrame.top);
9355 } catch (RuntimeException e) {
9356 // If something goes wrong with the surface (such
9357 // as running out of memory), don't take down the
9358 // entire system.
9359 Log.e(TAG, "Failure updating surface of " + w
9360 + "size=(" + width + "x" + height
9361 + "), pos=(" + w.mShownFrame.left
9362 + "," + w.mShownFrame.top + ")", e);
9363 if (!recoveringMemory) {
9364 reclaimSomeSurfaceMemoryLocked(w, "size");
9365 }
9366 }
9367 }
9368 }
9369 if (!w.mAppFreezing) {
9370 w.mContentInsetsChanged =
9371 !w.mLastContentInsets.equals(w.mContentInsets);
9372 w.mVisibleInsetsChanged =
9373 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009374 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 || w.mContentInsetsChanged
9376 || w.mVisibleInsetsChanged) {
9377 w.mLastFrame.set(w.mFrame);
9378 w.mLastContentInsets.set(w.mContentInsets);
9379 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009380 // If the screen is currently frozen, then keep
9381 // it frozen until this window draws at its new
9382 // orientation.
9383 if (mDisplayFrozen) {
9384 if (DEBUG_ORIENTATION) Log.v(TAG,
9385 "Resizing while display frozen: " + w);
9386 w.mOrientationChanging = true;
9387 if (mWindowsFreezingScreen) {
9388 mWindowsFreezingScreen = true;
9389 // XXX should probably keep timeout from
9390 // when we first froze the display.
9391 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9392 mH.sendMessageDelayed(mH.obtainMessage(
9393 H.WINDOW_FREEZE_TIMEOUT), 2000);
9394 }
9395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396 // If the orientation is changing, then we need to
9397 // hold off on unfreezing the display until this
9398 // window has been redrawn; to do that, we need
9399 // to go through the process of getting informed
9400 // by the application when it has finished drawing.
9401 if (w.mOrientationChanging) {
9402 if (DEBUG_ORIENTATION) Log.v(TAG,
9403 "Orientation start waiting for draw in "
9404 + w + ", surface " + w.mSurface);
9405 w.mDrawPending = true;
9406 w.mCommitDrawPending = false;
9407 w.mReadyToShow = false;
9408 if (w.mAppToken != null) {
9409 w.mAppToken.allDrawn = false;
9410 }
9411 }
Romain Guy06882f82009-06-10 13:36:04 -07009412 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009413 "Resizing window " + w + " to " + w.mFrame);
9414 mResizingWindows.add(w);
9415 } else if (w.mOrientationChanging) {
9416 if (!w.mDrawPending && !w.mCommitDrawPending) {
9417 if (DEBUG_ORIENTATION) Log.v(TAG,
9418 "Orientation not waiting for draw in "
9419 + w + ", surface " + w.mSurface);
9420 w.mOrientationChanging = false;
9421 }
9422 }
9423 }
9424
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009425 if ((forceHiding
9426 && attrs.type != WindowManager.LayoutParams.TYPE_STATUS_BAR
9427 && attrs.type != WindowManager.LayoutParams.TYPE_WALLPAPER)
9428 || w.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009429 if (!w.mLastHidden) {
9430 //dump();
9431 w.mLastHidden = true;
9432 if (SHOW_TRANSACTIONS) Log.i(
9433 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9434 if (w.mSurface != null) {
9435 try {
9436 w.mSurface.hide();
9437 } catch (RuntimeException e) {
9438 Log.w(TAG, "Exception hiding surface in " + w);
9439 }
9440 }
9441 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9442 }
9443 // If we are waiting for this window to handle an
9444 // orientation change, well, it is hidden, so
9445 // doesn't really matter. Note that this does
9446 // introduce a potential glitch if the window
9447 // becomes unhidden before it has drawn for the
9448 // new orientation.
9449 if (w.mOrientationChanging) {
9450 w.mOrientationChanging = false;
9451 if (DEBUG_ORIENTATION) Log.v(TAG,
9452 "Orientation change skips hidden " + w);
9453 }
9454 } else if (!w.isReadyForDisplay()) {
9455 if (!w.mLastHidden) {
9456 //dump();
9457 w.mLastHidden = true;
9458 if (SHOW_TRANSACTIONS) Log.i(
9459 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9460 if (w.mSurface != null) {
9461 try {
9462 w.mSurface.hide();
9463 } catch (RuntimeException e) {
9464 Log.w(TAG, "Exception exception hiding surface in " + w);
9465 }
9466 }
9467 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9468 }
9469 // If we are waiting for this window to handle an
9470 // orientation change, well, it is hidden, so
9471 // doesn't really matter. Note that this does
9472 // introduce a potential glitch if the window
9473 // becomes unhidden before it has drawn for the
9474 // new orientation.
9475 if (w.mOrientationChanging) {
9476 w.mOrientationChanging = false;
9477 if (DEBUG_ORIENTATION) Log.v(TAG,
9478 "Orientation change skips hidden " + w);
9479 }
9480 } else if (w.mLastLayer != w.mAnimLayer
9481 || w.mLastAlpha != w.mShownAlpha
9482 || w.mLastDsDx != w.mDsDx
9483 || w.mLastDtDx != w.mDtDx
9484 || w.mLastDsDy != w.mDsDy
9485 || w.mLastDtDy != w.mDtDy
9486 || w.mLastHScale != w.mHScale
9487 || w.mLastVScale != w.mVScale
9488 || w.mLastHidden) {
9489 displayed = true;
9490 w.mLastAlpha = w.mShownAlpha;
9491 w.mLastLayer = w.mAnimLayer;
9492 w.mLastDsDx = w.mDsDx;
9493 w.mLastDtDx = w.mDtDx;
9494 w.mLastDsDy = w.mDsDy;
9495 w.mLastDtDy = w.mDtDy;
9496 w.mLastHScale = w.mHScale;
9497 w.mLastVScale = w.mVScale;
9498 if (SHOW_TRANSACTIONS) Log.i(
9499 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009500 + w.mShownAlpha + " layer=" + w.mAnimLayer
9501 + " matrix=[" + (w.mDsDx*w.mHScale)
9502 + "," + (w.mDtDx*w.mVScale)
9503 + "][" + (w.mDsDy*w.mHScale)
9504 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009505 if (w.mSurface != null) {
9506 try {
9507 w.mSurface.setAlpha(w.mShownAlpha);
9508 w.mSurface.setLayer(w.mAnimLayer);
9509 w.mSurface.setMatrix(
9510 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9511 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9512 } catch (RuntimeException e) {
9513 Log.w(TAG, "Error updating surface in " + w, e);
9514 if (!recoveringMemory) {
9515 reclaimSomeSurfaceMemoryLocked(w, "update");
9516 }
9517 }
9518 }
9519
9520 if (w.mLastHidden && !w.mDrawPending
9521 && !w.mCommitDrawPending
9522 && !w.mReadyToShow) {
9523 if (SHOW_TRANSACTIONS) Log.i(
9524 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9525 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9526 + " during relayout");
9527 if (showSurfaceRobustlyLocked(w)) {
9528 w.mHasDrawn = true;
9529 w.mLastHidden = false;
9530 } else {
9531 w.mOrientationChanging = false;
9532 }
9533 }
9534 if (w.mSurface != null) {
9535 w.mToken.hasVisible = true;
9536 }
9537 } else {
9538 displayed = true;
9539 }
9540
9541 if (displayed) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009542 if (attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD) {
9543 forceHiding = true;
9544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 if (!covered) {
9546 if (attrs.width == LayoutParams.FILL_PARENT
9547 && attrs.height == LayoutParams.FILL_PARENT) {
9548 covered = true;
9549 }
9550 }
9551 if (w.mOrientationChanging) {
9552 if (w.mDrawPending || w.mCommitDrawPending) {
9553 orientationChangeComplete = false;
9554 if (DEBUG_ORIENTATION) Log.v(TAG,
9555 "Orientation continue waiting for draw in " + w);
9556 } else {
9557 w.mOrientationChanging = false;
9558 if (DEBUG_ORIENTATION) Log.v(TAG,
9559 "Orientation change complete in " + w);
9560 }
9561 }
9562 w.mToken.hasVisible = true;
9563 }
9564 } else if (w.mOrientationChanging) {
9565 if (DEBUG_ORIENTATION) Log.v(TAG,
9566 "Orientation change skips hidden " + w);
9567 w.mOrientationChanging = false;
9568 }
9569
9570 final boolean canBeSeen = w.isDisplayedLw();
9571
9572 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9573 focusDisplayed = true;
9574 }
9575
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009576 final boolean obscuredChanged = w.mObscured != obscured;
9577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009579 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 if (w.mSurface != null) {
9581 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9582 holdScreen = w.mSession;
9583 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009584 if (!syswin && w.mAttrs.screenBrightness >= 0
9585 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 screenBrightness = w.mAttrs.screenBrightness;
9587 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009588 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9589 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9590 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9591 syswin = true;
9592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009593 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009594
Dianne Hackborn25994b42009-09-04 14:21:19 -07009595 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9596 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 // This window completely covers everything behind it,
9598 // so we want to leave all of them as unblurred (for
9599 // performance reasons).
9600 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009601 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9602 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009603 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009604 obscured = true;
9605 if (mBackgroundFillerSurface == null) {
9606 try {
9607 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9608 0, dw, dh,
9609 PixelFormat.OPAQUE,
9610 Surface.FX_SURFACE_NORMAL);
9611 } catch (Exception e) {
9612 Log.e(TAG, "Exception creating filler surface", e);
9613 }
9614 }
9615 try {
9616 mBackgroundFillerSurface.setPosition(0, 0);
9617 mBackgroundFillerSurface.setSize(dw, dh);
9618 // Using the same layer as Dim because they will never be shown at the
9619 // same time.
9620 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9621 mBackgroundFillerSurface.show();
9622 } catch (RuntimeException e) {
9623 Log.e(TAG, "Exception showing filler surface");
9624 }
9625 backgroundFillerShown = true;
9626 mBackgroundFillerShown = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009627 } else if (canBeSeen && !obscured && !forceHiding &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009628 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9629 if (localLOGV) Log.v(TAG, "Win " + w
9630 + ": blurring=" + blurring
9631 + " obscured=" + obscured
9632 + " displayed=" + displayed);
9633 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9634 if (!dimming) {
9635 //Log.i(TAG, "DIM BEHIND: " + w);
9636 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009637 if (mDimAnimator == null) {
9638 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009640 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009642 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009643 }
9644 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9645 if (!blurring) {
9646 //Log.i(TAG, "BLUR BEHIND: " + w);
9647 blurring = true;
9648 mBlurShown = true;
9649 if (mBlurSurface == null) {
9650 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9651 + mBlurSurface + ": CREATE");
9652 try {
Romain Guy06882f82009-06-10 13:36:04 -07009653 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654 -1, 16, 16,
9655 PixelFormat.OPAQUE,
9656 Surface.FX_SURFACE_BLUR);
9657 } catch (Exception e) {
9658 Log.e(TAG, "Exception creating Blur surface", e);
9659 }
9660 }
9661 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9662 + mBlurSurface + ": SHOW pos=(0,0) (" +
9663 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9664 if (mBlurSurface != null) {
9665 mBlurSurface.setPosition(0, 0);
9666 mBlurSurface.setSize(dw, dh);
9667 try {
9668 mBlurSurface.show();
9669 } catch (RuntimeException e) {
9670 Log.w(TAG, "Failure showing blur surface", e);
9671 }
9672 }
9673 }
9674 mBlurSurface.setLayer(w.mAnimLayer-2);
9675 }
9676 }
9677 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009678
9679 if (obscuredChanged && mWallpaperTarget == w) {
9680 // This is the wallpaper target and its obscured state
9681 // changed... make sure the current wallaper's visibility
9682 // has been updated accordingly.
9683 updateWallpaperVisibilityLocked();
9684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009686
9687 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9688 mBackgroundFillerShown = false;
9689 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9690 try {
9691 mBackgroundFillerSurface.hide();
9692 } catch (RuntimeException e) {
9693 Log.e(TAG, "Exception hiding filler surface", e);
9694 }
9695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009697 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9698 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 }
Romain Guy06882f82009-06-10 13:36:04 -07009700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009701 if (!blurring && mBlurShown) {
9702 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9703 + ": HIDE");
9704 try {
9705 mBlurSurface.hide();
9706 } catch (IllegalArgumentException e) {
9707 Log.w(TAG, "Illegal argument exception hiding blur surface");
9708 }
9709 mBlurShown = false;
9710 }
9711
9712 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9713 } catch (RuntimeException e) {
9714 Log.e(TAG, "Unhandled exception in Window Manager", e);
9715 }
9716
9717 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009719 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9720 "With display frozen, orientationChangeComplete="
9721 + orientationChangeComplete);
9722 if (orientationChangeComplete) {
9723 if (mWindowsFreezingScreen) {
9724 mWindowsFreezingScreen = false;
9725 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9726 }
9727 if (mAppsFreezingScreen == 0) {
9728 stopFreezingDisplayLocked();
9729 }
9730 }
Romain Guy06882f82009-06-10 13:36:04 -07009731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 i = mResizingWindows.size();
9733 if (i > 0) {
9734 do {
9735 i--;
9736 WindowState win = mResizingWindows.get(i);
9737 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009738 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9739 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009740 win.mClient.resized(win.mFrame.width(),
9741 win.mFrame.height(), win.mLastContentInsets,
9742 win.mLastVisibleInsets, win.mDrawPending);
9743 win.mContentInsetsChanged = false;
9744 win.mVisibleInsetsChanged = false;
9745 } catch (RemoteException e) {
9746 win.mOrientationChanging = false;
9747 }
9748 } while (i > 0);
9749 mResizingWindows.clear();
9750 }
Romain Guy06882f82009-06-10 13:36:04 -07009751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009753 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009754 i = mDestroySurface.size();
9755 if (i > 0) {
9756 do {
9757 i--;
9758 WindowState win = mDestroySurface.get(i);
9759 win.mDestroying = false;
9760 if (mInputMethodWindow == win) {
9761 mInputMethodWindow = null;
9762 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009763 if (win == mWallpaperTarget) {
9764 wallpaperDestroyed = true;
9765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 win.destroySurfaceLocked();
9767 } while (i > 0);
9768 mDestroySurface.clear();
9769 }
9770
9771 // Time to remove any exiting tokens?
9772 for (i=mExitingTokens.size()-1; i>=0; i--) {
9773 WindowToken token = mExitingTokens.get(i);
9774 if (!token.hasVisible) {
9775 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009776 if (token.windowType == TYPE_WALLPAPER) {
9777 mWallpaperTokens.remove(token);
9778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 }
9780 }
9781
9782 // Time to remove any exiting applications?
9783 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9784 AppWindowToken token = mExitingAppTokens.get(i);
9785 if (!token.hasVisible && !mClosingApps.contains(token)) {
9786 mAppTokens.remove(token);
9787 mExitingAppTokens.remove(i);
9788 }
9789 }
9790
Dianne Hackborna8f60182009-09-01 19:01:50 -07009791 boolean needRelayout = false;
9792
9793 if (!animating && mAppTransitionRunning) {
9794 // We have finished the animation of an app transition. To do
9795 // this, we have delayed a lot of operations like showing and
9796 // hiding apps, moving apps in Z-order, etc. The app token list
9797 // reflects the correct Z-order, but the window list may now
9798 // be out of sync with it. So here we will just rebuild the
9799 // entire app window list. Fun!
9800 mAppTransitionRunning = false;
9801 needRelayout = true;
9802 rebuildAppWindowListLocked();
9803 // Clear information about apps that were moving.
9804 mToBottomApps.clear();
9805 }
9806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 if (focusDisplayed) {
9808 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9809 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009810 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009811 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009812 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009813 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009814 requestAnimationLocked(0);
9815 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9817 }
9818 mQueue.setHoldScreenLocked(holdScreen != null);
9819 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9820 mPowerManager.setScreenBrightnessOverride(-1);
9821 } else {
9822 mPowerManager.setScreenBrightnessOverride((int)
9823 (screenBrightness * Power.BRIGHTNESS_ON));
9824 }
9825 if (holdScreen != mHoldingScreenOn) {
9826 mHoldingScreenOn = holdScreen;
9827 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9828 mH.sendMessage(m);
9829 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009830
9831 if (mTurnOnScreen) {
9832 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
9833 LocalPowerManager.BUTTON_EVENT, true);
9834 mTurnOnScreen = false;
9835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009836 }
9837
9838 void requestAnimationLocked(long delay) {
9839 if (!mAnimationPending) {
9840 mAnimationPending = true;
9841 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9842 }
9843 }
Romain Guy06882f82009-06-10 13:36:04 -07009844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 /**
9846 * Have the surface flinger show a surface, robustly dealing with
9847 * error conditions. In particular, if there is not enough memory
9848 * to show the surface, then we will try to get rid of other surfaces
9849 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 * @return Returns true if the surface was successfully shown.
9852 */
9853 boolean showSurfaceRobustlyLocked(WindowState win) {
9854 try {
9855 if (win.mSurface != null) {
9856 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009857 if (win.mTurnOnScreen) {
9858 win.mTurnOnScreen = false;
9859 mTurnOnScreen = true;
9860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 }
9862 return true;
9863 } catch (RuntimeException e) {
9864 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9865 }
Romain Guy06882f82009-06-10 13:36:04 -07009866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 return false;
9870 }
Romain Guy06882f82009-06-10 13:36:04 -07009871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9873 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9876 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 if (mForceRemoves == null) {
9879 mForceRemoves = new ArrayList<WindowState>();
9880 }
Romain Guy06882f82009-06-10 13:36:04 -07009881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009882 long callingIdentity = Binder.clearCallingIdentity();
9883 try {
9884 // There was some problem... first, do a sanity check of the
9885 // window list to make sure we haven't left any dangling surfaces
9886 // around.
9887 int N = mWindows.size();
9888 boolean leakedSurface = false;
9889 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9890 for (int i=0; i<N; i++) {
9891 WindowState ws = (WindowState)mWindows.get(i);
9892 if (ws.mSurface != null) {
9893 if (!mSessions.contains(ws.mSession)) {
9894 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9895 + ws + " surface=" + ws.mSurface
9896 + " token=" + win.mToken
9897 + " pid=" + ws.mSession.mPid
9898 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009899 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 ws.mSurface = null;
9901 mForceRemoves.add(ws);
9902 i--;
9903 N--;
9904 leakedSurface = true;
9905 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9906 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9907 + ws + " surface=" + ws.mSurface
9908 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009909 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 ws.mSurface = null;
9911 leakedSurface = true;
9912 }
9913 }
9914 }
Romain Guy06882f82009-06-10 13:36:04 -07009915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 boolean killedApps = false;
9917 if (!leakedSurface) {
9918 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9919 SparseIntArray pidCandidates = new SparseIntArray();
9920 for (int i=0; i<N; i++) {
9921 WindowState ws = (WindowState)mWindows.get(i);
9922 if (ws.mSurface != null) {
9923 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9924 }
9925 }
9926 if (pidCandidates.size() > 0) {
9927 int[] pids = new int[pidCandidates.size()];
9928 for (int i=0; i<pids.length; i++) {
9929 pids[i] = pidCandidates.keyAt(i);
9930 }
9931 try {
9932 if (mActivityManager.killPidsForMemory(pids)) {
9933 killedApps = true;
9934 }
9935 } catch (RemoteException e) {
9936 }
9937 }
9938 }
Romain Guy06882f82009-06-10 13:36:04 -07009939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 if (leakedSurface || killedApps) {
9941 // We managed to reclaim some memory, so get rid of the trouble
9942 // surface and ask the app to request another one.
9943 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9944 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009945 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 win.mSurface = null;
9947 }
Romain Guy06882f82009-06-10 13:36:04 -07009948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 try {
9950 win.mClient.dispatchGetNewSurface();
9951 } catch (RemoteException e) {
9952 }
9953 }
9954 } finally {
9955 Binder.restoreCallingIdentity(callingIdentity);
9956 }
9957 }
Romain Guy06882f82009-06-10 13:36:04 -07009958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 private boolean updateFocusedWindowLocked(int mode) {
9960 WindowState newFocus = computeFocusedWindowLocked();
9961 if (mCurrentFocus != newFocus) {
9962 // This check makes sure that we don't already have the focus
9963 // change message pending.
9964 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9965 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9966 if (localLOGV) Log.v(
9967 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9968 final WindowState oldFocus = mCurrentFocus;
9969 mCurrentFocus = newFocus;
9970 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 final WindowState imWindow = mInputMethodWindow;
9973 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009974 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009976 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9977 mLayoutNeeded = true;
9978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9980 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009981 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9982 // Client will do the layout, but we need to assign layers
9983 // for handleNewWindowLocked() below.
9984 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 }
9986 }
Romain Guy06882f82009-06-10 13:36:04 -07009987
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009988 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9989 mKeyWaiter.handleNewWindowLocked(newFocus);
9990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009991 return true;
9992 }
9993 return false;
9994 }
9995
9996 private WindowState computeFocusedWindowLocked() {
9997 WindowState result = null;
9998 WindowState win;
9999
10000 int i = mWindows.size() - 1;
10001 int nextAppIndex = mAppTokens.size()-1;
10002 WindowToken nextApp = nextAppIndex >= 0
10003 ? mAppTokens.get(nextAppIndex) : null;
10004
10005 while (i >= 0) {
10006 win = (WindowState)mWindows.get(i);
10007
10008 if (localLOGV || DEBUG_FOCUS) Log.v(
10009 TAG, "Looking for focus: " + i
10010 + " = " + win
10011 + ", flags=" + win.mAttrs.flags
10012 + ", canReceive=" + win.canReceiveKeys());
10013
10014 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 // If this window's application has been removed, just skip it.
10017 if (thisApp != null && thisApp.removed) {
10018 i--;
10019 continue;
10020 }
Romain Guy06882f82009-06-10 13:36:04 -070010021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022 // If there is a focused app, don't allow focus to go to any
10023 // windows below it. If this is an application window, step
10024 // through the app tokens until we find its app.
10025 if (thisApp != null && nextApp != null && thisApp != nextApp
10026 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10027 int origAppIndex = nextAppIndex;
10028 while (nextAppIndex > 0) {
10029 if (nextApp == mFocusedApp) {
10030 // Whoops, we are below the focused app... no focus
10031 // for you!
10032 if (localLOGV || DEBUG_FOCUS) Log.v(
10033 TAG, "Reached focused app: " + mFocusedApp);
10034 return null;
10035 }
10036 nextAppIndex--;
10037 nextApp = mAppTokens.get(nextAppIndex);
10038 if (nextApp == thisApp) {
10039 break;
10040 }
10041 }
10042 if (thisApp != nextApp) {
10043 // Uh oh, the app token doesn't exist! This shouldn't
10044 // happen, but if it does we can get totally hosed...
10045 // so restart at the original app.
10046 nextAppIndex = origAppIndex;
10047 nextApp = mAppTokens.get(nextAppIndex);
10048 }
10049 }
10050
10051 // Dispatch to this window if it is wants key events.
10052 if (win.canReceiveKeys()) {
10053 if (DEBUG_FOCUS) Log.v(
10054 TAG, "Found focus @ " + i + " = " + win);
10055 result = win;
10056 break;
10057 }
10058
10059 i--;
10060 }
10061
10062 return result;
10063 }
10064
10065 private void startFreezingDisplayLocked() {
10066 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010067 // Freezing the display also suspends key event delivery, to
10068 // keep events from going astray while the display is reconfigured.
10069 // If someone has changed orientation again while the screen is
10070 // still frozen, the events will continue to be blocked while the
10071 // successive orientation change is processed. To prevent spurious
10072 // ANRs, we reset the event dispatch timeout in this case.
10073 synchronized (mKeyWaiter) {
10074 mKeyWaiter.mWasFrozen = true;
10075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 return;
10077 }
Romain Guy06882f82009-06-10 13:36:04 -070010078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010079 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 long now = SystemClock.uptimeMillis();
10082 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10083 if (mFreezeGcPending != 0) {
10084 if (now > (mFreezeGcPending+1000)) {
10085 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10086 mH.removeMessages(H.FORCE_GC);
10087 Runtime.getRuntime().gc();
10088 mFreezeGcPending = now;
10089 }
10090 } else {
10091 mFreezeGcPending = now;
10092 }
Romain Guy06882f82009-06-10 13:36:04 -070010093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 mDisplayFrozen = true;
10095 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
10096 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
10097 mAppTransitionReady = true;
10098 }
Romain Guy06882f82009-06-10 13:36:04 -070010099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 if (PROFILE_ORIENTATION) {
10101 File file = new File("/data/system/frozen");
10102 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10103 }
10104 Surface.freezeDisplay(0);
10105 }
Romain Guy06882f82009-06-10 13:36:04 -070010106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 private void stopFreezingDisplayLocked() {
10108 if (!mDisplayFrozen) {
10109 return;
10110 }
Romain Guy06882f82009-06-10 13:36:04 -070010111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010112 mDisplayFrozen = false;
10113 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10114 if (PROFILE_ORIENTATION) {
10115 Debug.stopMethodTracing();
10116 }
10117 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010118
Chris Tate2ad63a92009-03-25 17:36:48 -070010119 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10120 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 synchronized (mKeyWaiter) {
10122 mKeyWaiter.mWasFrozen = true;
10123 mKeyWaiter.notifyAll();
10124 }
10125
10126 // A little kludge: a lot could have happened while the
10127 // display was frozen, so now that we are coming back we
10128 // do a gc so that any remote references the system
10129 // processes holds on others can be released if they are
10130 // no longer needed.
10131 mH.removeMessages(H.FORCE_GC);
10132 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10133 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 mScreenFrozenLock.release();
10136 }
Romain Guy06882f82009-06-10 13:36:04 -070010137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 @Override
10139 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10140 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10141 != PackageManager.PERMISSION_GRANTED) {
10142 pw.println("Permission Denial: can't dump WindowManager from from pid="
10143 + Binder.getCallingPid()
10144 + ", uid=" + Binder.getCallingUid());
10145 return;
10146 }
Romain Guy06882f82009-06-10 13:36:04 -070010147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 synchronized(mWindowMap) {
10149 pw.println("Current Window Manager state:");
10150 for (int i=mWindows.size()-1; i>=0; i--) {
10151 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010152 pw.print(" Window #"); pw.print(i); pw.print(' ');
10153 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 w.dump(pw, " ");
10155 }
10156 if (mInputMethodDialogs.size() > 0) {
10157 pw.println(" ");
10158 pw.println(" Input method dialogs:");
10159 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10160 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010161 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162 }
10163 }
10164 if (mPendingRemove.size() > 0) {
10165 pw.println(" ");
10166 pw.println(" Remove pending for:");
10167 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10168 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010169 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10170 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 w.dump(pw, " ");
10172 }
10173 }
10174 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10175 pw.println(" ");
10176 pw.println(" Windows force removing:");
10177 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10178 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010179 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10180 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181 w.dump(pw, " ");
10182 }
10183 }
10184 if (mDestroySurface.size() > 0) {
10185 pw.println(" ");
10186 pw.println(" Windows waiting to destroy their surface:");
10187 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10188 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010189 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10190 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 w.dump(pw, " ");
10192 }
10193 }
10194 if (mLosingFocus.size() > 0) {
10195 pw.println(" ");
10196 pw.println(" Windows losing focus:");
10197 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10198 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010199 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10200 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010201 w.dump(pw, " ");
10202 }
10203 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010204 if (mResizingWindows.size() > 0) {
10205 pw.println(" ");
10206 pw.println(" Windows waiting to resize:");
10207 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10208 WindowState w = mResizingWindows.get(i);
10209 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10210 pw.print(w); pw.println(":");
10211 w.dump(pw, " ");
10212 }
10213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 if (mSessions.size() > 0) {
10215 pw.println(" ");
10216 pw.println(" All active sessions:");
10217 Iterator<Session> it = mSessions.iterator();
10218 while (it.hasNext()) {
10219 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010220 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 s.dump(pw, " ");
10222 }
10223 }
10224 if (mTokenMap.size() > 0) {
10225 pw.println(" ");
10226 pw.println(" All tokens:");
10227 Iterator<WindowToken> it = mTokenMap.values().iterator();
10228 while (it.hasNext()) {
10229 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010230 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 token.dump(pw, " ");
10232 }
10233 }
10234 if (mTokenList.size() > 0) {
10235 pw.println(" ");
10236 pw.println(" Window token list:");
10237 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010238 pw.print(" #"); pw.print(i); pw.print(": ");
10239 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010240 }
10241 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010242 if (mWallpaperTokens.size() > 0) {
10243 pw.println(" ");
10244 pw.println(" Wallpaper tokens:");
10245 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10246 WindowToken token = mWallpaperTokens.get(i);
10247 pw.print(" Wallpaper #"); pw.print(i);
10248 pw.print(' '); pw.print(token); pw.println(':');
10249 token.dump(pw, " ");
10250 }
10251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010252 if (mAppTokens.size() > 0) {
10253 pw.println(" ");
10254 pw.println(" Application tokens in Z order:");
10255 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010256 pw.print(" App #"); pw.print(i); pw.print(": ");
10257 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010258 }
10259 }
10260 if (mFinishedStarting.size() > 0) {
10261 pw.println(" ");
10262 pw.println(" Finishing start of application tokens:");
10263 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10264 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010265 pw.print(" Finished Starting #"); pw.print(i);
10266 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 token.dump(pw, " ");
10268 }
10269 }
10270 if (mExitingTokens.size() > 0) {
10271 pw.println(" ");
10272 pw.println(" Exiting tokens:");
10273 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10274 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010275 pw.print(" Exiting #"); pw.print(i);
10276 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010277 token.dump(pw, " ");
10278 }
10279 }
10280 if (mExitingAppTokens.size() > 0) {
10281 pw.println(" ");
10282 pw.println(" Exiting application tokens:");
10283 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10284 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010285 pw.print(" Exiting App #"); pw.print(i);
10286 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010287 token.dump(pw, " ");
10288 }
10289 }
10290 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010291 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10292 pw.print(" mLastFocus="); pw.println(mLastFocus);
10293 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10294 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10295 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010296 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010297 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10298 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10299 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10300 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010301 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10302 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10303 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10304 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10305 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010306 if (mDimAnimator != null) {
10307 mDimAnimator.printTo(pw);
10308 } else {
10309 pw.print( " no DimAnimator ");
10310 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010311 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010312 pw.print(mInputMethodAnimLayerAdjustment);
10313 pw.print(" mWallpaperAnimLayerAdjustment=");
10314 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010315 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10316 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010317 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10318 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10319 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10320 pw.print(" mRotation="); pw.print(mRotation);
10321 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10322 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10323 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10324 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10325 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10326 pw.print(" mNextAppTransition=0x");
10327 pw.print(Integer.toHexString(mNextAppTransition));
10328 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010329 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010330 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10331 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10332 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10333 if (mOpeningApps.size() > 0) {
10334 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10335 }
10336 if (mClosingApps.size() > 0) {
10337 pw.print(" mClosingApps="); pw.println(mClosingApps);
10338 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010339 if (mToTopApps.size() > 0) {
10340 pw.print(" mToTopApps="); pw.println(mToTopApps);
10341 }
10342 if (mToBottomApps.size() > 0) {
10343 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10344 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010345 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10346 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010348 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10349 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10350 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10351 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10352 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10353 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010354 }
10355 }
10356
10357 public void monitor() {
10358 synchronized (mWindowMap) { }
10359 synchronized (mKeyguardDisabled) { }
10360 synchronized (mKeyWaiter) { }
10361 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010362
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010363 public void virtualKeyFeedback(KeyEvent event) {
10364 mPolicy.keyFeedbackFromInput(event);
10365 }
10366
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010367 /**
10368 * DimAnimator class that controls the dim animation. This holds the surface and
10369 * all state used for dim animation.
10370 */
10371 private static class DimAnimator {
10372 Surface mDimSurface;
10373 boolean mDimShown = false;
10374 float mDimCurrentAlpha;
10375 float mDimTargetAlpha;
10376 float mDimDeltaPerMs;
10377 long mLastDimAnimTime;
10378
10379 DimAnimator (SurfaceSession session) {
10380 if (mDimSurface == null) {
10381 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10382 + mDimSurface + ": CREATE");
10383 try {
10384 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10385 Surface.FX_SURFACE_DIM);
10386 } catch (Exception e) {
10387 Log.e(TAG, "Exception creating Dim surface", e);
10388 }
10389 }
10390 }
10391
10392 /**
10393 * Show the dim surface.
10394 */
10395 void show(int dw, int dh) {
10396 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10397 dw + "x" + dh + ")");
10398 mDimShown = true;
10399 try {
10400 mDimSurface.setPosition(0, 0);
10401 mDimSurface.setSize(dw, dh);
10402 mDimSurface.show();
10403 } catch (RuntimeException e) {
10404 Log.w(TAG, "Failure showing dim surface", e);
10405 }
10406 }
10407
10408 /**
10409 * Set's the dim surface's layer and update dim parameters that will be used in
10410 * {@link updateSurface} after all windows are examined.
10411 */
10412 void updateParameters(WindowState w, long currentTime) {
10413 mDimSurface.setLayer(w.mAnimLayer-1);
10414
10415 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010416 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10417 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010418 if (mDimTargetAlpha != target) {
10419 // If the desired dim level has changed, then
10420 // start an animation to it.
10421 mLastDimAnimTime = currentTime;
10422 long duration = (w.mAnimating && w.mAnimation != null)
10423 ? w.mAnimation.computeDurationHint()
10424 : DEFAULT_DIM_DURATION;
10425 if (target > mDimTargetAlpha) {
10426 // This is happening behind the activity UI,
10427 // so we can make it run a little longer to
10428 // give a stronger impression without disrupting
10429 // the user.
10430 duration *= DIM_DURATION_MULTIPLIER;
10431 }
10432 if (duration < 1) {
10433 // Don't divide by zero
10434 duration = 1;
10435 }
10436 mDimTargetAlpha = target;
10437 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10438 }
10439 }
10440
10441 /**
10442 * Updating the surface's alpha. Returns true if the animation continues, or returns
10443 * false when the animation is finished and the dim surface is hidden.
10444 */
10445 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10446 if (!dimming) {
10447 if (mDimTargetAlpha != 0) {
10448 mLastDimAnimTime = currentTime;
10449 mDimTargetAlpha = 0;
10450 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10451 }
10452 }
10453
10454 boolean animating = false;
10455 if (mLastDimAnimTime != 0) {
10456 mDimCurrentAlpha += mDimDeltaPerMs
10457 * (currentTime-mLastDimAnimTime);
10458 boolean more = true;
10459 if (displayFrozen) {
10460 // If the display is frozen, there is no reason to animate.
10461 more = false;
10462 } else if (mDimDeltaPerMs > 0) {
10463 if (mDimCurrentAlpha > mDimTargetAlpha) {
10464 more = false;
10465 }
10466 } else if (mDimDeltaPerMs < 0) {
10467 if (mDimCurrentAlpha < mDimTargetAlpha) {
10468 more = false;
10469 }
10470 } else {
10471 more = false;
10472 }
10473
10474 // Do we need to continue animating?
10475 if (more) {
10476 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10477 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10478 mLastDimAnimTime = currentTime;
10479 mDimSurface.setAlpha(mDimCurrentAlpha);
10480 animating = true;
10481 } else {
10482 mDimCurrentAlpha = mDimTargetAlpha;
10483 mLastDimAnimTime = 0;
10484 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10485 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10486 mDimSurface.setAlpha(mDimCurrentAlpha);
10487 if (!dimming) {
10488 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10489 + ": HIDE");
10490 try {
10491 mDimSurface.hide();
10492 } catch (RuntimeException e) {
10493 Log.w(TAG, "Illegal argument exception hiding dim surface");
10494 }
10495 mDimShown = false;
10496 }
10497 }
10498 }
10499 return animating;
10500 }
10501
10502 public void printTo(PrintWriter pw) {
10503 pw.print(" mDimShown="); pw.print(mDimShown);
10504 pw.print(" current="); pw.print(mDimCurrentAlpha);
10505 pw.print(" target="); pw.print(mDimTargetAlpha);
10506 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10507 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10508 }
10509 }
10510
10511 /**
10512 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10513 * This is used for opening/closing transition for apps in compatible mode.
10514 */
10515 private static class FadeInOutAnimation extends Animation {
10516 int mWidth;
10517 boolean mFadeIn;
10518
10519 public FadeInOutAnimation(boolean fadeIn) {
10520 setInterpolator(new AccelerateInterpolator());
10521 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10522 mFadeIn = fadeIn;
10523 }
10524
10525 @Override
10526 protected void applyTransformation(float interpolatedTime, Transformation t) {
10527 float x = interpolatedTime;
10528 if (!mFadeIn) {
10529 x = 1.0f - x; // reverse the interpolation for fade out
10530 }
10531 if (x < 0.5) {
10532 // move the window out of the screen.
10533 t.getMatrix().setTranslate(mWidth, 0);
10534 } else {
10535 t.getMatrix().setTranslate(0, 0);// show
10536 t.setAlpha((x - 0.5f) * 2);
10537 }
10538 }
10539
10540 @Override
10541 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10542 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10543 mWidth = width;
10544 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010545
10546 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010547 public int getZAdjustment() {
10548 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010549 }
10550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551}