blob: 20b01d2d745a7ebfe7b7b81e0442bb02ff5b743e [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;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700146 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700147 static final boolean MEASURE_LATENCY = false;
148 static private LatencyTimer lt;
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 static final boolean PROFILE_ORIENTATION = false;
151 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700152 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /** How long to wait for subsequent key repeats, in milliseconds */
157 static final int KEY_REPEAT_DELAY = 50;
158
159 /** How much to multiply the policy's type layer, to reserve room
160 * for multiple windows of the same type and Z-ordering adjustment
161 * with TYPE_LAYER_OFFSET. */
162 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
165 * or below others in the same layer. */
166 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** How much to increment the layer for each window, to reserve room
169 * for effect surfaces between them.
170 */
171 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /** The maximum length we will accept for a loaded animation duration:
174 * this is 10 seconds.
175 */
176 static final int MAX_ANIMATION_DURATION = 10*1000;
177
178 /** Amount of time (in milliseconds) to animate the dim surface from one
179 * value to another, when no window animation is driving it.
180 */
181 static final int DEFAULT_DIM_DURATION = 200;
182
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700183 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
184 * compatible windows.
185 */
186 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /** Adjustment to time to perform a dim, to make it more dramatic.
189 */
190 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700191
Dianne Hackborncfaef692009-06-15 14:24:44 -0700192 static final int INJECT_FAILED = 0;
193 static final int INJECT_SUCCEEDED = 1;
194 static final int INJECT_NO_PERMISSION = -1;
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 static final int UPDATE_FOCUS_NORMAL = 0;
197 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
198 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
199 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700200
Michael Chane96440f2009-05-06 10:27:36 -0700201 /** The minimum time between dispatching touch events. */
202 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
203
204 // Last touch event time
205 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700206
Michael Chane96440f2009-05-06 10:27:36 -0700207 // Last touch event type
208 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700209
Michael Chane96440f2009-05-06 10:27:36 -0700210 // Time to wait before calling useractivity again. This saves CPU usage
211 // when we get a flood of touch events.
212 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
213
214 // Last time we call user activity
215 long mLastUserActivityCallTime = 0;
216
Romain Guy06882f82009-06-10 13:36:04 -0700217 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700218 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700221 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 /**
224 * Condition waited on by {@link #reenableKeyguard} to know the call to
225 * the window policy has finished.
226 */
227 private boolean mWaitingUntilKeyguardReenabled = false;
228
229
230 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
231 new Handler(), "WindowManagerService.mKeyguardDisabled") {
232 public void acquired() {
233 mPolicy.enableKeyguard(false);
234 }
235 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700236 mPolicy.enableKeyguard(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 synchronized (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 mWaitingUntilKeyguardReenabled = false;
239 mKeyguardDisabled.notifyAll();
240 }
241 }
242 };
243
244 final Context mContext;
245
246 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
251
252 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * All currently active sessions with clients.
258 */
259 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * Mapping from an IWindow IBinder to the server's Window object.
263 * This is also used as the lock for all of our state.
264 */
265 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
266
267 /**
268 * Mapping from a token IBinder to a WindowToken object.
269 */
270 final HashMap<IBinder, WindowToken> mTokenMap =
271 new HashMap<IBinder, WindowToken>();
272
273 /**
274 * The same tokens as mTokenMap, stored in a list for efficient iteration
275 * over them.
276 */
277 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * Window tokens that are in the process of exiting, but still
281 * on screen for animations.
282 */
283 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
284
285 /**
286 * Z-ordered (bottom-most first) list of all application tokens, for
287 * controlling the ordering of windows in different applications. This
288 * contains WindowToken objects.
289 */
290 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
291
292 /**
293 * Application tokens that are in the process of exiting, but still
294 * on screen for animations.
295 */
296 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
297
298 /**
299 * List of window tokens that have finished starting their application,
300 * and now need to have the policy remove their windows.
301 */
302 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
303
304 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700305 * This was the app token that was used to retrieve the last enter
306 * animation. It will be used for the next exit animation.
307 */
308 AppWindowToken mLastEnterAnimToken;
309
310 /**
311 * These were the layout params used to retrieve the last enter animation.
312 * They will be used for the next exit animation.
313 */
314 LayoutParams mLastEnterAnimParams;
315
316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 * Z-ordered (bottom-most first) list of all Window objects.
318 */
319 final ArrayList mWindows = new ArrayList();
320
321 /**
322 * Windows that are being resized. Used so we can tell the client about
323 * the resize after closing the transaction in which we resized the
324 * underlying surface.
325 */
326 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
327
328 /**
329 * Windows whose animations have ended and now must be removed.
330 */
331 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
332
333 /**
334 * Windows whose surface should be destroyed.
335 */
336 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
337
338 /**
339 * Windows that have lost input focus and are waiting for the new
340 * focus window to be displayed before they are told about this.
341 */
342 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
343
344 /**
345 * This is set when we have run out of memory, and will either be an empty
346 * list or contain windows that need to be force removed.
347 */
348 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700353 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 Surface mBlurSurface;
355 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 final float[] mTmpFloats = new float[9];
360
361 boolean mSafeMode;
362 boolean mDisplayEnabled = false;
363 boolean mSystemBooted = false;
364 int mRotation = 0;
365 int mRequestedRotation = 0;
366 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700367 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 ArrayList<IRotationWatcher> mRotationWatchers
369 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 boolean mLayoutNeeded = true;
372 boolean mAnimationPending = false;
373 boolean mDisplayFrozen = false;
374 boolean mWindowsFreezingScreen = false;
375 long mFreezeGcPending = 0;
376 int mAppsFreezingScreen = 0;
377
378 // This is held as long as we have the screen frozen, to give us time to
379 // perform a rotation animation when turning off shows the lock screen which
380 // changes the orientation.
381 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 // State management of app transitions. When we are preparing for a
384 // transition, mNextAppTransition will be the kind of transition to
385 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
386 // mOpeningApps and mClosingApps are the lists of tokens that will be
387 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700388 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700389 String mNextAppTransitionPackage;
390 int mNextAppTransitionEnter;
391 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700393 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 boolean mAppTransitionTimeout = false;
395 boolean mStartingIconInTransition = false;
396 boolean mSkipAppTransitionAnimation = false;
397 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
398 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700399 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
400 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 //flag to detect fat touch events
403 boolean mFatTouch = false;
404 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 H mH = new H();
407
408 WindowState mCurrentFocus = null;
409 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 // This just indicates the window the input method is on top of, not
412 // necessarily the window its input is going to.
413 WindowState mInputMethodTarget = null;
414 WindowState mUpcomingInputMethodTarget = null;
415 boolean mInputMethodTargetWaitingAnim;
416 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 WindowState mInputMethodWindow = null;
419 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
420
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700421 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
422
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700423 // If non-null, this is the currently visible window that is associated
424 // with the wallpaper.
425 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700426 // If non-null, we are in the middle of animating from one wallpaper target
427 // to another, and this is the lower one in Z-order.
428 WindowState mLowerWallpaperTarget = null;
429 // If non-null, we are in the middle of animating from one wallpaper target
430 // to another, and this is the higher one in Z-order.
431 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700432 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700433 float mLastWallpaperX;
434 float mLastWallpaperY;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700435 // Lock for waiting for the wallpaper.
436 final Object mWaitingOnWallpaperLock = new Object();
437 // This is set when we are waiting for a wallpaper to tell us it is done
438 // changing its scroll position.
439 WindowState mWaitingOnWallpaper;
440 // The last time we had a timeout when waiting for a wallpaper.
441 long mLastWallpaperTimeoutTime;
442 // We give a wallpaper up to 150ms to finish scrolling.
443 static final long WALLPAPER_TIMEOUT = 150;
444 // Time we wait after a timeout before trying to wait again.
445 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 AppWindowToken mFocusedApp = null;
448
449 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 float mWindowAnimationScale = 1.0f;
452 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 final KeyWaiter mKeyWaiter = new KeyWaiter();
455 final KeyQ mQueue;
456 final InputDispatcherThread mInputThread;
457
458 // Who is holding the screen on.
459 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700460
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700461 boolean mTurnOnScreen;
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
464 * Whether the UI is currently running in touch mode (not showing
465 * navigational focus because the user is directly pressing the screen).
466 */
467 boolean mInTouchMode = false;
468
469 private ViewServer mViewServer;
470
471 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700472
Dianne Hackbornc485a602009-03-24 22:39:49 -0700473 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700474 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700475
476 // The frame use to limit the size of the app running in compatibility mode.
477 Rect mCompatibleScreenFrame = new Rect();
478 // The surface used to fill the outer rim of the app running in compatibility mode.
479 Surface mBackgroundFillerSurface = null;
480 boolean mBackgroundFillerShown = false;
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public static WindowManagerService main(Context context,
483 PowerManagerService pm, boolean haveInputMethods) {
484 WMThread thr = new WMThread(context, pm, haveInputMethods);
485 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 synchronized (thr) {
488 while (thr.mService == null) {
489 try {
490 thr.wait();
491 } catch (InterruptedException e) {
492 }
493 }
494 }
Romain Guy06882f82009-06-10 13:36:04 -0700495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 return thr.mService;
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 static class WMThread extends Thread {
500 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 private final Context mContext;
503 private final PowerManagerService mPM;
504 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 public WMThread(Context context, PowerManagerService pm,
507 boolean haveInputMethods) {
508 super("WindowManager");
509 mContext = context;
510 mPM = pm;
511 mHaveInputMethods = haveInputMethods;
512 }
Romain Guy06882f82009-06-10 13:36:04 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 public void run() {
515 Looper.prepare();
516 WindowManagerService s = new WindowManagerService(mContext, mPM,
517 mHaveInputMethods);
518 android.os.Process.setThreadPriority(
519 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 synchronized (this) {
522 mService = s;
523 notifyAll();
524 }
Romain Guy06882f82009-06-10 13:36:04 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 Looper.loop();
527 }
528 }
529
530 static class PolicyThread extends Thread {
531 private final WindowManagerPolicy mPolicy;
532 private final WindowManagerService mService;
533 private final Context mContext;
534 private final PowerManagerService mPM;
535 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public PolicyThread(WindowManagerPolicy policy,
538 WindowManagerService service, Context context,
539 PowerManagerService pm) {
540 super("WindowManagerPolicy");
541 mPolicy = policy;
542 mService = service;
543 mContext = context;
544 mPM = pm;
545 }
Romain Guy06882f82009-06-10 13:36:04 -0700546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 public void run() {
548 Looper.prepare();
549 //Looper.myLooper().setMessageLogging(new LogPrinter(
550 // Log.VERBOSE, "WindowManagerPolicy"));
551 android.os.Process.setThreadPriority(
552 android.os.Process.THREAD_PRIORITY_FOREGROUND);
553 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 synchronized (this) {
556 mRunning = true;
557 notifyAll();
558 }
Romain Guy06882f82009-06-10 13:36:04 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 Looper.loop();
561 }
562 }
563
564 private WindowManagerService(Context context, PowerManagerService pm,
565 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700566 if (MEASURE_LATENCY) {
567 lt = new LatencyTimer(100, 1000);
568 }
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 mContext = context;
571 mHaveInputMethods = haveInputMethods;
572 mLimitedAlphaCompositing = context.getResources().getBoolean(
573 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 mPowerManager = pm;
576 mPowerManager.setPolicy(mPolicy);
577 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
578 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
579 "SCREEN_FROZEN");
580 mScreenFrozenLock.setReferenceCounted(false);
581
582 mActivityManager = ActivityManagerNative.getDefault();
583 mBatteryStats = BatteryStatsService.getService();
584
585 // Get persisted window scale setting
586 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
587 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
588 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
589 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700590
Michael Chan9f028e62009-08-04 17:37:46 -0700591 int max_events_per_sec = 35;
592 try {
593 max_events_per_sec = Integer.parseInt(SystemProperties
594 .get("windowsmgr.max_events_per_sec"));
595 if (max_events_per_sec < 1) {
596 max_events_per_sec = 35;
597 }
598 } catch (NumberFormatException e) {
599 }
600 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mQueue = new KeyQ();
603
604 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
607 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 synchronized (thr) {
610 while (!thr.mRunning) {
611 try {
612 thr.wait();
613 } catch (InterruptedException e) {
614 }
615 }
616 }
Romain Guy06882f82009-06-10 13:36:04 -0700617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 // Add ourself to the Watchdog monitors.
621 Watchdog.getInstance().addMonitor(this);
622 }
623
624 @Override
625 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
626 throws RemoteException {
627 try {
628 return super.onTransact(code, data, reply, flags);
629 } catch (RuntimeException e) {
630 // The window manager only throws security exceptions, so let's
631 // log all others.
632 if (!(e instanceof SecurityException)) {
633 Log.e(TAG, "Window Manager Crash", e);
634 }
635 throw e;
636 }
637 }
638
639 private void placeWindowAfter(Object pos, WindowState window) {
640 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700641 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 TAG, "Adding window " + window + " at "
643 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
644 mWindows.add(i+1, window);
645 }
646
647 private void placeWindowBefore(Object pos, WindowState window) {
648 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700649 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 TAG, "Adding window " + window + " at "
651 + i + " of " + mWindows.size() + " (before " + pos + ")");
652 mWindows.add(i, window);
653 }
654
655 //This method finds out the index of a window that has the same app token as
656 //win. used for z ordering the windows in mWindows
657 private int findIdxBasedOnAppTokens(WindowState win) {
658 //use a local variable to cache mWindows
659 ArrayList localmWindows = mWindows;
660 int jmax = localmWindows.size();
661 if(jmax == 0) {
662 return -1;
663 }
664 for(int j = (jmax-1); j >= 0; j--) {
665 WindowState wentry = (WindowState)localmWindows.get(j);
666 if(wentry.mAppToken == win.mAppToken) {
667 return j;
668 }
669 }
670 return -1;
671 }
Romain Guy06882f82009-06-10 13:36:04 -0700672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
674 final IWindow client = win.mClient;
675 final WindowToken token = win.mToken;
676 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 final int N = localmWindows.size();
679 final WindowState attached = win.mAttachedWindow;
680 int i;
681 if (attached == null) {
682 int tokenWindowsPos = token.windows.size();
683 if (token.appWindowToken != null) {
684 int index = tokenWindowsPos-1;
685 if (index >= 0) {
686 // If this application has existing windows, we
687 // simply place the new window on top of them... but
688 // keep the starting window on top.
689 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
690 // Base windows go behind everything else.
691 placeWindowBefore(token.windows.get(0), win);
692 tokenWindowsPos = 0;
693 } else {
694 AppWindowToken atoken = win.mAppToken;
695 if (atoken != null &&
696 token.windows.get(index) == atoken.startingWindow) {
697 placeWindowBefore(token.windows.get(index), win);
698 tokenWindowsPos--;
699 } else {
700 int newIdx = findIdxBasedOnAppTokens(win);
701 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700702 //there is a window above this one associated with the same
703 //apptoken note that the window could be a floating window
704 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700706 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
707 TAG, "Adding window " + win + " at "
708 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712 }
713 } else {
714 if (localLOGV) Log.v(
715 TAG, "Figuring out where to add app window "
716 + client.asBinder() + " (token=" + token + ")");
717 // Figure out where the window should go, based on the
718 // order of applications.
719 final int NA = mAppTokens.size();
720 Object pos = null;
721 for (i=NA-1; i>=0; i--) {
722 AppWindowToken t = mAppTokens.get(i);
723 if (t == token) {
724 i--;
725 break;
726 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700727
728 // We haven't reached the token yet; if this token
729 // is not going to the bottom and has windows, we can
730 // use it as an anchor for when we do reach the token.
731 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 pos = t.windows.get(0);
733 }
734 }
735 // We now know the index into the apps. If we found
736 // an app window above, that gives us the position; else
737 // we need to look some more.
738 if (pos != null) {
739 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700740 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 mTokenMap.get(((WindowState)pos).mClient.asBinder());
742 if (atoken != null) {
743 final int NC = atoken.windows.size();
744 if (NC > 0) {
745 WindowState bottom = atoken.windows.get(0);
746 if (bottom.mSubLayer < 0) {
747 pos = bottom;
748 }
749 }
750 }
751 placeWindowBefore(pos, win);
752 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700753 // Continue looking down until we find the first
754 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 while (i >= 0) {
756 AppWindowToken t = mAppTokens.get(i);
757 final int NW = t.windows.size();
758 if (NW > 0) {
759 pos = t.windows.get(NW-1);
760 break;
761 }
762 i--;
763 }
764 if (pos != null) {
765 // Move in front of any windows attached to this
766 // one.
767 WindowToken atoken =
768 mTokenMap.get(((WindowState)pos).mClient.asBinder());
769 if (atoken != null) {
770 final int NC = atoken.windows.size();
771 if (NC > 0) {
772 WindowState top = atoken.windows.get(NC-1);
773 if (top.mSubLayer >= 0) {
774 pos = top;
775 }
776 }
777 }
778 placeWindowAfter(pos, win);
779 } else {
780 // Just search for the start of this layer.
781 final int myLayer = win.mBaseLayer;
782 for (i=0; i<N; i++) {
783 WindowState w = (WindowState)localmWindows.get(i);
784 if (w.mBaseLayer > myLayer) {
785 break;
786 }
787 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700788 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
789 TAG, "Adding window " + win + " at "
790 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 localmWindows.add(i, win);
792 }
793 }
794 }
795 } else {
796 // Figure out where window should go, based on layer.
797 final int myLayer = win.mBaseLayer;
798 for (i=N-1; i>=0; i--) {
799 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
800 i++;
801 break;
802 }
803 }
804 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700805 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
806 TAG, "Adding window " + win + " at "
807 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 localmWindows.add(i, win);
809 }
810 if (addToToken) {
811 token.windows.add(tokenWindowsPos, win);
812 }
813
814 } else {
815 // Figure out this window's ordering relative to the window
816 // it is attached to.
817 final int NA = token.windows.size();
818 final int sublayer = win.mSubLayer;
819 int largestSublayer = Integer.MIN_VALUE;
820 WindowState windowWithLargestSublayer = null;
821 for (i=0; i<NA; i++) {
822 WindowState w = token.windows.get(i);
823 final int wSublayer = w.mSubLayer;
824 if (wSublayer >= largestSublayer) {
825 largestSublayer = wSublayer;
826 windowWithLargestSublayer = w;
827 }
828 if (sublayer < 0) {
829 // For negative sublayers, we go below all windows
830 // in the same sublayer.
831 if (wSublayer >= sublayer) {
832 if (addToToken) {
833 token.windows.add(i, win);
834 }
835 placeWindowBefore(
836 wSublayer >= 0 ? attached : w, win);
837 break;
838 }
839 } else {
840 // For positive sublayers, we go above all windows
841 // in the same sublayer.
842 if (wSublayer > sublayer) {
843 if (addToToken) {
844 token.windows.add(i, win);
845 }
846 placeWindowBefore(w, win);
847 break;
848 }
849 }
850 }
851 if (i >= NA) {
852 if (addToToken) {
853 token.windows.add(win);
854 }
855 if (sublayer < 0) {
856 placeWindowBefore(attached, win);
857 } else {
858 placeWindowAfter(largestSublayer >= 0
859 ? windowWithLargestSublayer
860 : attached,
861 win);
862 }
863 }
864 }
Romain Guy06882f82009-06-10 13:36:04 -0700865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 if (win.mAppToken != null && addToToken) {
867 win.mAppToken.allAppWindows.add(win);
868 }
869 }
Romain Guy06882f82009-06-10 13:36:04 -0700870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 static boolean canBeImeTarget(WindowState w) {
872 final int fl = w.mAttrs.flags
873 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
874 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
875 return w.isVisibleOrAdding();
876 }
877 return false;
878 }
Romain Guy06882f82009-06-10 13:36:04 -0700879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
881 final ArrayList localmWindows = mWindows;
882 final int N = localmWindows.size();
883 WindowState w = null;
884 int i = N;
885 while (i > 0) {
886 i--;
887 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
890 // + Integer.toHexString(w.mAttrs.flags));
891 if (canBeImeTarget(w)) {
892 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 // Yet more tricksyness! If this window is a "starting"
895 // window, we do actually want to be on top of it, but
896 // it is not -really- where input will go. So if the caller
897 // is not actually looking to move the IME, look down below
898 // for a real window to target...
899 if (!willMove
900 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
901 && i > 0) {
902 WindowState wb = (WindowState)localmWindows.get(i-1);
903 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
904 i--;
905 w = wb;
906 }
907 }
908 break;
909 }
910 }
Romain Guy06882f82009-06-10 13:36:04 -0700911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
915 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (willMove && w != null) {
918 final WindowState curTarget = mInputMethodTarget;
919 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 // Now some fun for dealing with window animations that
922 // modify the Z order. We need to look at all windows below
923 // the current target that are in this app, finding the highest
924 // visible one in layering.
925 AppWindowToken token = curTarget.mAppToken;
926 WindowState highestTarget = null;
927 int highestPos = 0;
928 if (token.animating || token.animation != null) {
929 int pos = 0;
930 pos = localmWindows.indexOf(curTarget);
931 while (pos >= 0) {
932 WindowState win = (WindowState)localmWindows.get(pos);
933 if (win.mAppToken != token) {
934 break;
935 }
936 if (!win.mRemoved) {
937 if (highestTarget == null || win.mAnimLayer >
938 highestTarget.mAnimLayer) {
939 highestTarget = win;
940 highestPos = pos;
941 }
942 }
943 pos--;
944 }
945 }
Romain Guy06882f82009-06-10 13:36:04 -0700946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700948 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 + mNextAppTransition + " " + highestTarget
950 + " animating=" + highestTarget.isAnimating()
951 + " layer=" + highestTarget.mAnimLayer
952 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700953
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700954 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 // If we are currently setting up for an animation,
956 // hold everything until we can find out what will happen.
957 mInputMethodTargetWaitingAnim = true;
958 mInputMethodTarget = highestTarget;
959 return highestPos + 1;
960 } else if (highestTarget.isAnimating() &&
961 highestTarget.mAnimLayer > w.mAnimLayer) {
962 // If the window we are currently targeting is involved
963 // with an animation, and it is on top of the next target
964 // we will be over, then hold off on moving until
965 // that is done.
966 mInputMethodTarget = highestTarget;
967 return highestPos + 1;
968 }
969 }
970 }
971 }
Romain Guy06882f82009-06-10 13:36:04 -0700972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 //Log.i(TAG, "Placing input method @" + (i+1));
974 if (w != null) {
975 if (willMove) {
976 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700977 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
979 + mInputMethodTarget + " to " + w, e);
980 mInputMethodTarget = w;
981 if (w.mAppToken != null) {
982 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
983 } else {
984 setInputMethodAnimLayerAdjustment(0);
985 }
986 }
987 return i+1;
988 }
989 if (willMove) {
990 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700991 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
993 + mInputMethodTarget + " to null", e);
994 mInputMethodTarget = null;
995 setInputMethodAnimLayerAdjustment(0);
996 }
997 return -1;
998 }
Romain Guy06882f82009-06-10 13:36:04 -0700999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 void addInputMethodWindowToListLocked(WindowState win) {
1001 int pos = findDesiredInputMethodWindowIndexLocked(true);
1002 if (pos >= 0) {
1003 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001004 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1005 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 mWindows.add(pos, win);
1007 moveInputMethodDialogsLocked(pos+1);
1008 return;
1009 }
1010 win.mTargetAppToken = null;
1011 addWindowToListInOrderLocked(win, true);
1012 moveInputMethodDialogsLocked(pos);
1013 }
Romain Guy06882f82009-06-10 13:36:04 -07001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 void setInputMethodAnimLayerAdjustment(int adj) {
1016 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1017 mInputMethodAnimLayerAdjustment = adj;
1018 WindowState imw = mInputMethodWindow;
1019 if (imw != null) {
1020 imw.mAnimLayer = imw.mLayer + adj;
1021 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1022 + " anim layer: " + imw.mAnimLayer);
1023 int wi = imw.mChildWindows.size();
1024 while (wi > 0) {
1025 wi--;
1026 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1027 cw.mAnimLayer = cw.mLayer + adj;
1028 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1029 + " anim layer: " + cw.mAnimLayer);
1030 }
1031 }
1032 int di = mInputMethodDialogs.size();
1033 while (di > 0) {
1034 di --;
1035 imw = mInputMethodDialogs.get(di);
1036 imw.mAnimLayer = imw.mLayer + adj;
1037 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1038 + " anim layer: " + imw.mAnimLayer);
1039 }
1040 }
Romain Guy06882f82009-06-10 13:36:04 -07001041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1043 int wpos = mWindows.indexOf(win);
1044 if (wpos >= 0) {
1045 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001046 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 mWindows.remove(wpos);
1048 int NC = win.mChildWindows.size();
1049 while (NC > 0) {
1050 NC--;
1051 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1052 int cpos = mWindows.indexOf(cw);
1053 if (cpos >= 0) {
1054 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001055 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1056 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 mWindows.remove(cpos);
1058 }
1059 }
1060 }
1061 return interestingPos;
1062 }
Romain Guy06882f82009-06-10 13:36:04 -07001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 private void reAddWindowToListInOrderLocked(WindowState win) {
1065 addWindowToListInOrderLocked(win, false);
1066 // This is a hack to get all of the child windows added as well
1067 // at the right position. Child windows should be rare and
1068 // this case should be rare, so it shouldn't be that big a deal.
1069 int wpos = mWindows.indexOf(win);
1070 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001071 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1072 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 mWindows.remove(wpos);
1074 reAddWindowLocked(wpos, win);
1075 }
1076 }
Romain Guy06882f82009-06-10 13:36:04 -07001077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 void logWindowList(String prefix) {
1079 int N = mWindows.size();
1080 while (N > 0) {
1081 N--;
1082 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1083 }
1084 }
Romain Guy06882f82009-06-10 13:36:04 -07001085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 void moveInputMethodDialogsLocked(int pos) {
1087 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 final int N = dialogs.size();
1090 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1091 for (int i=0; i<N; i++) {
1092 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1093 }
1094 if (DEBUG_INPUT_METHOD) {
1095 Log.v(TAG, "Window list w/pos=" + pos);
1096 logWindowList(" ");
1097 }
Romain Guy06882f82009-06-10 13:36:04 -07001098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 if (pos >= 0) {
1100 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1101 if (pos < mWindows.size()) {
1102 WindowState wp = (WindowState)mWindows.get(pos);
1103 if (wp == mInputMethodWindow) {
1104 pos++;
1105 }
1106 }
1107 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1108 for (int i=0; i<N; i++) {
1109 WindowState win = dialogs.get(i);
1110 win.mTargetAppToken = targetAppToken;
1111 pos = reAddWindowLocked(pos, win);
1112 }
1113 if (DEBUG_INPUT_METHOD) {
1114 Log.v(TAG, "Final window list:");
1115 logWindowList(" ");
1116 }
1117 return;
1118 }
1119 for (int i=0; i<N; i++) {
1120 WindowState win = dialogs.get(i);
1121 win.mTargetAppToken = null;
1122 reAddWindowToListInOrderLocked(win);
1123 if (DEBUG_INPUT_METHOD) {
1124 Log.v(TAG, "No IM target, final list:");
1125 logWindowList(" ");
1126 }
1127 }
1128 }
Romain Guy06882f82009-06-10 13:36:04 -07001129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1131 final WindowState imWin = mInputMethodWindow;
1132 final int DN = mInputMethodDialogs.size();
1133 if (imWin == null && DN == 0) {
1134 return false;
1135 }
Romain Guy06882f82009-06-10 13:36:04 -07001136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1138 if (imPos >= 0) {
1139 // In this case, the input method windows are to be placed
1140 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 // First check to see if the input method windows are already
1143 // located here, and contiguous.
1144 final int N = mWindows.size();
1145 WindowState firstImWin = imPos < N
1146 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 // Figure out the actual input method window that should be
1149 // at the bottom of their stack.
1150 WindowState baseImWin = imWin != null
1151 ? imWin : mInputMethodDialogs.get(0);
1152 if (baseImWin.mChildWindows.size() > 0) {
1153 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1154 if (cw.mSubLayer < 0) baseImWin = cw;
1155 }
Romain Guy06882f82009-06-10 13:36:04 -07001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 if (firstImWin == baseImWin) {
1158 // The windows haven't moved... but are they still contiguous?
1159 // First find the top IM window.
1160 int pos = imPos+1;
1161 while (pos < N) {
1162 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1163 break;
1164 }
1165 pos++;
1166 }
1167 pos++;
1168 // Now there should be no more input method windows above.
1169 while (pos < N) {
1170 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1171 break;
1172 }
1173 pos++;
1174 }
1175 if (pos >= N) {
1176 // All is good!
1177 return false;
1178 }
1179 }
Romain Guy06882f82009-06-10 13:36:04 -07001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (imWin != null) {
1182 if (DEBUG_INPUT_METHOD) {
1183 Log.v(TAG, "Moving IM from " + imPos);
1184 logWindowList(" ");
1185 }
1186 imPos = tmpRemoveWindowLocked(imPos, imWin);
1187 if (DEBUG_INPUT_METHOD) {
1188 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1189 logWindowList(" ");
1190 }
1191 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1192 reAddWindowLocked(imPos, imWin);
1193 if (DEBUG_INPUT_METHOD) {
1194 Log.v(TAG, "List after moving IM to " + imPos + ":");
1195 logWindowList(" ");
1196 }
1197 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1198 } else {
1199 moveInputMethodDialogsLocked(imPos);
1200 }
Romain Guy06882f82009-06-10 13:36:04 -07001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 } else {
1203 // In this case, the input method windows go in a fixed layer,
1204 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 if (imWin != null) {
1207 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1208 tmpRemoveWindowLocked(0, imWin);
1209 imWin.mTargetAppToken = null;
1210 reAddWindowToListInOrderLocked(imWin);
1211 if (DEBUG_INPUT_METHOD) {
1212 Log.v(TAG, "List with no IM target:");
1213 logWindowList(" ");
1214 }
1215 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1216 } else {
1217 moveInputMethodDialogsLocked(-1);;
1218 }
Romain Guy06882f82009-06-10 13:36:04 -07001219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
Romain Guy06882f82009-06-10 13:36:04 -07001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 if (needAssignLayers) {
1223 assignLayersLocked();
1224 }
Romain Guy06882f82009-06-10 13:36:04 -07001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 return true;
1227 }
Romain Guy06882f82009-06-10 13:36:04 -07001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 void adjustInputMethodDialogsLocked() {
1230 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1231 }
Romain Guy06882f82009-06-10 13:36:04 -07001232
Dianne Hackborn25994b42009-09-04 14:21:19 -07001233 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1234 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1235 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1236 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1237 ? wallpaperTarget.mAppToken.animation : null)
1238 + " upper=" + mUpperWallpaperTarget
1239 + " lower=" + mLowerWallpaperTarget);
1240 return (wallpaperTarget != null
1241 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1242 && wallpaperTarget.mAppToken.animation != null)))
1243 || mUpperWallpaperTarget != null
1244 || mLowerWallpaperTarget != null;
1245 }
1246
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001247 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1248 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1249
1250 int adjustWallpaperWindowsLocked() {
1251 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001252
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001253 final int dw = mDisplay.getWidth();
1254 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001255
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001256 // First find top-most window that has asked to be on top of the
1257 // wallpaper; all wallpapers go behind it.
1258 final ArrayList localmWindows = mWindows;
1259 int N = localmWindows.size();
1260 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001261 WindowState foundW = null;
1262 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001263 WindowState topCurW = null;
1264 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001265 int i = N;
1266 while (i > 0) {
1267 i--;
1268 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001269 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1270 if (topCurW == null) {
1271 topCurW = w;
1272 topCurI = i;
1273 }
1274 continue;
1275 }
1276 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001277 if (w.mAppToken != null) {
1278 // If this window's app token is hidden and not animating,
1279 // it is of no interest to us.
1280 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1281 if (DEBUG_WALLPAPER) Log.v(TAG,
1282 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001283 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001284 continue;
1285 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001287 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1288 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1289 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001290 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001291 && (mWallpaperTarget == w
1292 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001293 if (DEBUG_WALLPAPER) Log.v(TAG,
1294 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001295 foundW = w;
1296 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001297 if (w == mWallpaperTarget && ((w.mAppToken != null
1298 && w.mAppToken.animation != null)
1299 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001300 // The current wallpaper target is animating, so we'll
1301 // look behind it for another possible target and figure
1302 // out what is going on below.
1303 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1304 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001305 continue;
1306 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001307 break;
1308 }
1309 }
1310
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001311 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001312 // If we are currently waiting for an app transition, and either
1313 // the current target or the next target are involved with it,
1314 // then hold off on doing anything with the wallpaper.
1315 // Note that we are checking here for just whether the target
1316 // is part of an app token... which is potentially overly aggressive
1317 // (the app token may not be involved in the transition), but good
1318 // enough (we'll just wait until whatever transition is pending
1319 // executes).
1320 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001321 if (DEBUG_WALLPAPER) Log.v(TAG,
1322 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001323 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001324 }
1325 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001326 if (DEBUG_WALLPAPER) Log.v(TAG,
1327 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001328 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001329 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001330 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001331
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001332 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001333 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001334 Log.v(TAG, "New wallpaper target: " + foundW
1335 + " oldTarget: " + mWallpaperTarget);
1336 }
1337
1338 mLowerWallpaperTarget = null;
1339 mUpperWallpaperTarget = null;
1340
1341 WindowState oldW = mWallpaperTarget;
1342 mWallpaperTarget = foundW;
1343
1344 // Now what is happening... if the current and new targets are
1345 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001346 if (foundW != null && oldW != null) {
1347 boolean oldAnim = oldW.mAnimation != null
1348 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1349 boolean foundAnim = foundW.mAnimation != null
1350 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001351 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001352 Log.v(TAG, "New animation: " + foundAnim
1353 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001354 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001355 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001356 int oldI = localmWindows.indexOf(oldW);
1357 if (DEBUG_WALLPAPER) {
1358 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1359 }
1360 if (oldI >= 0) {
1361 if (DEBUG_WALLPAPER) {
1362 Log.v(TAG, "Animating wallpapers: old#" + oldI
1363 + "=" + oldW + "; new#" + foundI
1364 + "=" + foundW);
1365 }
1366
1367 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001368 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001369 if (DEBUG_WALLPAPER) {
1370 Log.v(TAG, "Old wallpaper still the target.");
1371 }
1372 mWallpaperTarget = oldW;
1373 }
1374
1375 // Now set the upper and lower wallpaper targets
1376 // correctly, and make sure that we are positioning
1377 // the wallpaper below the lower.
1378 if (foundI > oldI) {
1379 // The new target is on top of the old one.
1380 if (DEBUG_WALLPAPER) {
1381 Log.v(TAG, "Found target above old target.");
1382 }
1383 mUpperWallpaperTarget = foundW;
1384 mLowerWallpaperTarget = oldW;
1385 foundW = oldW;
1386 foundI = oldI;
1387 } else {
1388 // The new target is below the old one.
1389 if (DEBUG_WALLPAPER) {
1390 Log.v(TAG, "Found target below old target.");
1391 }
1392 mUpperWallpaperTarget = oldW;
1393 mLowerWallpaperTarget = foundW;
1394 }
1395 }
1396 }
1397 }
1398
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001399 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001400 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001401 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1402 || (mLowerWallpaperTarget.mAppToken != null
1403 && mLowerWallpaperTarget.mAppToken.animation != null);
1404 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1405 || (mUpperWallpaperTarget.mAppToken != null
1406 && mUpperWallpaperTarget.mAppToken.animation != null);
1407 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001408 if (DEBUG_WALLPAPER) {
1409 Log.v(TAG, "No longer animating wallpaper targets!");
1410 }
1411 mLowerWallpaperTarget = null;
1412 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001413 }
1414 }
1415
1416 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001417 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001418 // The window is visible to the compositor... but is it visible
1419 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001420 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001421 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001422
1423 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001424 // its layer adjustment. Only do this if we are not transfering
1425 // between two wallpaper targets.
1426 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001427 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001428 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001429
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001430 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1431 * TYPE_LAYER_MULTIPLIER
1432 + TYPE_LAYER_OFFSET;
1433
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001434 // Now w is the window we are supposed to be behind... but we
1435 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001436 // AND any starting window associated with it, AND below the
1437 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001438 while (foundI > 0) {
1439 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001440 if (wb.mBaseLayer < maxLayer &&
1441 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001442 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001443 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001444 // This window is not related to the previous one in any
1445 // interesting way, so stop here.
1446 break;
1447 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001448 foundW = wb;
1449 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001450 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001451 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001452 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001453 }
1454
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001455 if (foundW == null && topCurW != null) {
1456 // There is no wallpaper target, so it goes at the bottom.
1457 // We will assume it is the same place as last time, if known.
1458 foundW = topCurW;
1459 foundI = topCurI+1;
1460 } else {
1461 // Okay i is the position immediately above the wallpaper. Look at
1462 // what is below it for later.
1463 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1464 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001465
Dianne Hackborn284ac932009-08-28 10:34:25 -07001466 if (visible) {
1467 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1468 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1469 }
1470
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001471 // Start stepping backwards from here, ensuring that our wallpaper windows
1472 // are correctly placed.
1473 int curTokenIndex = mWallpaperTokens.size();
1474 while (curTokenIndex > 0) {
1475 curTokenIndex--;
1476 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001477 if (token.hidden == visible) {
1478 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1479 token.hidden = !visible;
1480 // Need to do a layout to ensure the wallpaper now has the
1481 // correct size.
1482 mLayoutNeeded = true;
1483 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001484
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001485 int curWallpaperIndex = token.windows.size();
1486 while (curWallpaperIndex > 0) {
1487 curWallpaperIndex--;
1488 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001489
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001490 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001491 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001492 }
1493
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001494 // First, make sure the client has the current visibility
1495 // state.
1496 if (wallpaper.mWallpaperVisible != visible) {
1497 wallpaper.mWallpaperVisible = visible;
1498 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001499 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001500 "Setting visibility of wallpaper " + wallpaper
1501 + ": " + visible);
1502 wallpaper.mClient.dispatchAppVisibility(visible);
1503 } catch (RemoteException e) {
1504 }
1505 }
1506
1507 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001508 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1509 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001510
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001511 // First, if this window is at the current index, then all
1512 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001513 if (wallpaper == foundW) {
1514 foundI--;
1515 foundW = foundI > 0
1516 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001517 continue;
1518 }
1519
1520 // The window didn't match... the current wallpaper window,
1521 // wherever it is, is in the wrong place, so make sure it is
1522 // not in the list.
1523 int oldIndex = localmWindows.indexOf(wallpaper);
1524 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001525 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1526 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001527 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001528 if (oldIndex < foundI) {
1529 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001530 }
1531 }
1532
1533 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001534 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1535 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001536 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001537
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001538 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001539 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001540 }
1541 }
1542
1543 return changed;
1544 }
1545
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001546 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001547 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1548 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001549 mWallpaperAnimLayerAdjustment = adj;
1550 int curTokenIndex = mWallpaperTokens.size();
1551 while (curTokenIndex > 0) {
1552 curTokenIndex--;
1553 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1554 int curWallpaperIndex = token.windows.size();
1555 while (curWallpaperIndex > 0) {
1556 curWallpaperIndex--;
1557 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1558 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001559 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1560 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001561 }
1562 }
1563 }
1564
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001565 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1566 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001567 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001568 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001569 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001570 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001571 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001572 changed = wallpaperWin.mXOffset != offset;
1573 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001574 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1575 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001576 wallpaperWin.mXOffset = offset;
1577 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001578 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1579 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001580 rawChanged = true;
1581 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001582 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001583
Dianne Hackborn284ac932009-08-28 10:34:25 -07001584 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001585 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001586 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001587 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001588 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1589 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001590 changed = true;
1591 wallpaperWin.mYOffset = offset;
1592 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001593 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1594 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001595 rawChanged = true;
1596 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001597 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001598
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001599 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001600 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001601 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1602 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1603 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001604 if (sync) {
1605 synchronized (mWaitingOnWallpaperLock) {
1606 mWaitingOnWallpaper = wallpaperWin;
1607 }
1608 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001609 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001610 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1611 if (sync) {
1612 synchronized (mWaitingOnWallpaperLock) {
1613 if (mWaitingOnWallpaper != null) {
1614 long start = SystemClock.uptimeMillis();
1615 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1616 < start) {
1617 try {
1618 if (DEBUG_WALLPAPER) Log.v(TAG,
1619 "Waiting for offset complete...");
1620 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1621 } catch (InterruptedException e) {
1622 }
1623 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1624 if ((start+WALLPAPER_TIMEOUT)
1625 < SystemClock.uptimeMillis()) {
1626 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1627 + wallpaperWin);
1628 mLastWallpaperTimeoutTime = start;
1629 }
1630 }
1631 mWaitingOnWallpaper = null;
1632 }
1633 }
1634 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001635 } catch (RemoteException e) {
1636 }
1637 }
1638
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001639 return changed;
1640 }
1641
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001642 void wallpaperOffsetsComplete(IBinder window) {
1643 synchronized (mWaitingOnWallpaperLock) {
1644 if (mWaitingOnWallpaper != null &&
1645 mWaitingOnWallpaper.mClient.asBinder() == window) {
1646 mWaitingOnWallpaper = null;
1647 mWaitingOnWallpaperLock.notifyAll();
1648 }
1649 }
1650 }
1651
1652 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001653 final int dw = mDisplay.getWidth();
1654 final int dh = mDisplay.getHeight();
1655
1656 boolean changed = false;
1657
1658 WindowState target = mWallpaperTarget;
1659 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001660 mLastWallpaperX = target.mWallpaperX;
1661 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001662 int curTokenIndex = mWallpaperTokens.size();
1663 while (curTokenIndex > 0) {
1664 curTokenIndex--;
1665 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1666 int curWallpaperIndex = token.windows.size();
1667 while (curWallpaperIndex > 0) {
1668 curWallpaperIndex--;
1669 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001670 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001671 wallpaper.computeShownFrameLocked();
1672 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001673 // We only want to be synchronous with one wallpaper.
1674 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001675 }
1676 }
1677 }
1678 }
1679
1680 return changed;
1681 }
1682
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001683 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001684 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001685 final int dw = mDisplay.getWidth();
1686 final int dh = mDisplay.getHeight();
1687
1688 int curTokenIndex = mWallpaperTokens.size();
1689 while (curTokenIndex > 0) {
1690 curTokenIndex--;
1691 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001692 if (token.hidden == visible) {
1693 token.hidden = !visible;
1694 // Need to do a layout to ensure the wallpaper now has the
1695 // correct size.
1696 mLayoutNeeded = true;
1697 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001698
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001699 int curWallpaperIndex = token.windows.size();
1700 while (curWallpaperIndex > 0) {
1701 curWallpaperIndex--;
1702 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1703 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001704 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001705 }
1706
1707 if (wallpaper.mWallpaperVisible != visible) {
1708 wallpaper.mWallpaperVisible = visible;
1709 try {
1710 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001711 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001712 + ": " + visible);
1713 wallpaper.mClient.dispatchAppVisibility(visible);
1714 } catch (RemoteException e) {
1715 }
1716 }
1717 }
1718 }
1719 }
1720
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001721 void sendPointerToWallpaperLocked(WindowState srcWin,
1722 MotionEvent pointer, long eventTime) {
1723 int curTokenIndex = mWallpaperTokens.size();
1724 while (curTokenIndex > 0) {
1725 curTokenIndex--;
1726 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1727 int curWallpaperIndex = token.windows.size();
1728 while (curWallpaperIndex > 0) {
1729 curWallpaperIndex--;
1730 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1731 if ((wallpaper.mAttrs.flags &
1732 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1733 continue;
1734 }
1735 try {
1736 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1737 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1738 srcWin.mFrame.top-wallpaper.mFrame.top);
1739 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1740 } catch (RemoteException e) {
1741 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1742 }
1743 }
1744 }
1745 }
1746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 public int addWindow(Session session, IWindow client,
1748 WindowManager.LayoutParams attrs, int viewVisibility,
1749 Rect outContentInsets) {
1750 int res = mPolicy.checkAddPermission(attrs);
1751 if (res != WindowManagerImpl.ADD_OKAY) {
1752 return res;
1753 }
Romain Guy06882f82009-06-10 13:36:04 -07001754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 boolean reportNewConfig = false;
1756 WindowState attachedWindow = null;
1757 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 synchronized(mWindowMap) {
1760 // Instantiating a Display requires talking with the simulator,
1761 // so don't do it until we know the system is mostly up and
1762 // running.
1763 if (mDisplay == null) {
1764 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1765 mDisplay = wm.getDefaultDisplay();
1766 mQueue.setDisplay(mDisplay);
1767 reportNewConfig = true;
1768 }
Romain Guy06882f82009-06-10 13:36:04 -07001769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 if (mWindowMap.containsKey(client.asBinder())) {
1771 Log.w(TAG, "Window " + client + " is already added");
1772 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1773 }
1774
1775 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001776 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 if (attachedWindow == null) {
1778 Log.w(TAG, "Attempted to add window with token that is not a window: "
1779 + attrs.token + ". Aborting.");
1780 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1781 }
1782 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1783 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1784 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1785 + attrs.token + ". Aborting.");
1786 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1787 }
1788 }
1789
1790 boolean addToken = false;
1791 WindowToken token = mTokenMap.get(attrs.token);
1792 if (token == null) {
1793 if (attrs.type >= FIRST_APPLICATION_WINDOW
1794 && attrs.type <= LAST_APPLICATION_WINDOW) {
1795 Log.w(TAG, "Attempted to add application window with unknown token "
1796 + attrs.token + ". Aborting.");
1797 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1798 }
1799 if (attrs.type == TYPE_INPUT_METHOD) {
1800 Log.w(TAG, "Attempted to add input method window with unknown token "
1801 + attrs.token + ". Aborting.");
1802 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1803 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001804 if (attrs.type == TYPE_WALLPAPER) {
1805 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1806 + attrs.token + ". Aborting.");
1807 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 token = new WindowToken(attrs.token, -1, false);
1810 addToken = true;
1811 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1812 && attrs.type <= LAST_APPLICATION_WINDOW) {
1813 AppWindowToken atoken = token.appWindowToken;
1814 if (atoken == null) {
1815 Log.w(TAG, "Attempted to add window with non-application token "
1816 + token + ". Aborting.");
1817 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1818 } else if (atoken.removed) {
1819 Log.w(TAG, "Attempted to add window with exiting application token "
1820 + token + ". Aborting.");
1821 return WindowManagerImpl.ADD_APP_EXITING;
1822 }
1823 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1824 // No need for this guy!
1825 if (localLOGV) Log.v(
1826 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1827 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1828 }
1829 } else if (attrs.type == TYPE_INPUT_METHOD) {
1830 if (token.windowType != TYPE_INPUT_METHOD) {
1831 Log.w(TAG, "Attempted to add input method window with bad token "
1832 + attrs.token + ". Aborting.");
1833 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1834 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001835 } else if (attrs.type == TYPE_WALLPAPER) {
1836 if (token.windowType != TYPE_WALLPAPER) {
1837 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1838 + attrs.token + ". Aborting.");
1839 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842
1843 win = new WindowState(session, client, token,
1844 attachedWindow, attrs, viewVisibility);
1845 if (win.mDeathRecipient == null) {
1846 // Client has apparently died, so there is no reason to
1847 // continue.
1848 Log.w(TAG, "Adding window client " + client.asBinder()
1849 + " that is dead, aborting.");
1850 return WindowManagerImpl.ADD_APP_EXITING;
1851 }
1852
1853 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 res = mPolicy.prepareAddWindowLw(win, attrs);
1856 if (res != WindowManagerImpl.ADD_OKAY) {
1857 return res;
1858 }
1859
1860 // From now on, no exceptions or errors allowed!
1861
1862 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 if (addToken) {
1867 mTokenMap.put(attrs.token, token);
1868 mTokenList.add(token);
1869 }
1870 win.attach();
1871 mWindowMap.put(client.asBinder(), win);
1872
1873 if (attrs.type == TYPE_APPLICATION_STARTING &&
1874 token.appWindowToken != null) {
1875 token.appWindowToken.startingWindow = win;
1876 }
1877
1878 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (attrs.type == TYPE_INPUT_METHOD) {
1881 mInputMethodWindow = win;
1882 addInputMethodWindowToListLocked(win);
1883 imMayMove = false;
1884 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1885 mInputMethodDialogs.add(win);
1886 addWindowToListInOrderLocked(win, true);
1887 adjustInputMethodDialogsLocked();
1888 imMayMove = false;
1889 } else {
1890 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001891 if (attrs.type == TYPE_WALLPAPER) {
1892 mLastWallpaperTimeoutTime = 0;
1893 adjustWallpaperWindowsLocked();
1894 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001895 adjustWallpaperWindowsLocked();
1896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 }
Romain Guy06882f82009-06-10 13:36:04 -07001898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 if (mInTouchMode) {
1904 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1905 }
1906 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1907 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1908 }
Romain Guy06882f82009-06-10 13:36:04 -07001909
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001910 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001912 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1913 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 imMayMove = false;
1915 }
1916 }
Romain Guy06882f82009-06-10 13:36:04 -07001917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001919 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
Romain Guy06882f82009-06-10 13:36:04 -07001921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 assignLayersLocked();
1923 // Don't do layout here, the window must call
1924 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 //dump();
1927
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001928 if (focusChanged) {
1929 if (mCurrentFocus != null) {
1930 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1931 }
1932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 if (localLOGV) Log.v(
1934 TAG, "New client " + client.asBinder()
1935 + ": window=" + win);
1936 }
1937
1938 // sendNewConfiguration() checks caller permissions so we must call it with
1939 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1940 // identity anyway, so it's safe to just clear & restore around this whole
1941 // block.
1942 final long origId = Binder.clearCallingIdentity();
1943 if (reportNewConfig) {
1944 sendNewConfiguration();
1945 } else {
1946 // Update Orientation after adding a window, only if the window needs to be
1947 // displayed right away
1948 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001949 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 sendNewConfiguration();
1951 }
1952 }
1953 }
1954 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 return res;
1957 }
Romain Guy06882f82009-06-10 13:36:04 -07001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 public void removeWindow(Session session, IWindow client) {
1960 synchronized(mWindowMap) {
1961 WindowState win = windowForClientLocked(session, client);
1962 if (win == null) {
1963 return;
1964 }
1965 removeWindowLocked(session, win);
1966 }
1967 }
Romain Guy06882f82009-06-10 13:36:04 -07001968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 public void removeWindowLocked(Session session, WindowState win) {
1970
1971 if (localLOGV || DEBUG_FOCUS) Log.v(
1972 TAG, "Remove " + win + " client="
1973 + Integer.toHexString(System.identityHashCode(
1974 win.mClient.asBinder()))
1975 + ", surface=" + win.mSurface);
1976
1977 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 if (DEBUG_APP_TRANSITIONS) Log.v(
1980 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1981 + " mExiting=" + win.mExiting
1982 + " isAnimating=" + win.isAnimating()
1983 + " app-animation="
1984 + (win.mAppToken != null ? win.mAppToken.animation : null)
1985 + " inPendingTransaction="
1986 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1987 + " mDisplayFrozen=" + mDisplayFrozen);
1988 // Visibility of the removed window. Will be used later to update orientation later on.
1989 boolean wasVisible = false;
1990 // First, see if we need to run an animation. If we do, we have
1991 // to hold off on removing the window until the animation is done.
1992 // If the display is frozen, just remove immediately, since the
1993 // animation wouldn't be seen.
1994 if (win.mSurface != null && !mDisplayFrozen) {
1995 // If we are not currently running the exit animation, we
1996 // need to see about starting one.
1997 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2000 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2001 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2002 }
2003 // Try starting an animation.
2004 if (applyAnimationLocked(win, transit, false)) {
2005 win.mExiting = true;
2006 }
2007 }
2008 if (win.mExiting || win.isAnimating()) {
2009 // The exit animation is running... wait for it!
2010 //Log.i(TAG, "*** Running exit animation...");
2011 win.mExiting = true;
2012 win.mRemoveOnExit = true;
2013 mLayoutNeeded = true;
2014 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2015 performLayoutAndPlaceSurfacesLocked();
2016 if (win.mAppToken != null) {
2017 win.mAppToken.updateReportedVisibilityLocked();
2018 }
2019 //dump();
2020 Binder.restoreCallingIdentity(origId);
2021 return;
2022 }
2023 }
2024
2025 removeWindowInnerLocked(session, win);
2026 // Removing a visible window will effect the computed orientation
2027 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002028 if (wasVisible && computeForcedAppOrientationLocked()
2029 != mForcedAppOrientation) {
2030 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 }
2032 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2033 Binder.restoreCallingIdentity(origId);
2034 }
Romain Guy06882f82009-06-10 13:36:04 -07002035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002037 mKeyWaiter.finishedKey(session, win.mClient, true,
2038 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2040 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 if (mInputMethodTarget == win) {
2045 moveInputMethodWindowsIfNeededLocked(false);
2046 }
Romain Guy06882f82009-06-10 13:36:04 -07002047
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002048 if (false) {
2049 RuntimeException e = new RuntimeException("here");
2050 e.fillInStackTrace();
2051 Log.w(TAG, "Removing window " + win, e);
2052 }
2053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 mPolicy.removeWindowLw(win);
2055 win.removeLocked();
2056
2057 mWindowMap.remove(win.mClient.asBinder());
2058 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002059 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060
2061 if (mInputMethodWindow == win) {
2062 mInputMethodWindow = null;
2063 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2064 mInputMethodDialogs.remove(win);
2065 }
Romain Guy06882f82009-06-10 13:36:04 -07002066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 final WindowToken token = win.mToken;
2068 final AppWindowToken atoken = win.mAppToken;
2069 token.windows.remove(win);
2070 if (atoken != null) {
2071 atoken.allAppWindows.remove(win);
2072 }
2073 if (localLOGV) Log.v(
2074 TAG, "**** Removing window " + win + ": count="
2075 + token.windows.size());
2076 if (token.windows.size() == 0) {
2077 if (!token.explicit) {
2078 mTokenMap.remove(token.token);
2079 mTokenList.remove(token);
2080 } else if (atoken != null) {
2081 atoken.firstWindowDrawn = false;
2082 }
2083 }
2084
2085 if (atoken != null) {
2086 if (atoken.startingWindow == win) {
2087 atoken.startingWindow = null;
2088 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2089 // If this is the last window and we had requested a starting
2090 // transition window, well there is no point now.
2091 atoken.startingData = null;
2092 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2093 // If this is the last window except for a starting transition
2094 // window, we need to get rid of the starting transition.
2095 if (DEBUG_STARTING_WINDOW) {
2096 Log.v(TAG, "Schedule remove starting " + token
2097 + ": no more real windows");
2098 }
2099 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2100 mH.sendMessage(m);
2101 }
2102 }
Romain Guy06882f82009-06-10 13:36:04 -07002103
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002104 if (win.mAttrs.type == TYPE_WALLPAPER) {
2105 mLastWallpaperTimeoutTime = 0;
2106 adjustWallpaperWindowsLocked();
2107 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002108 adjustWallpaperWindowsLocked();
2109 }
2110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 if (!mInLayout) {
2112 assignLayersLocked();
2113 mLayoutNeeded = true;
2114 performLayoutAndPlaceSurfacesLocked();
2115 if (win.mAppToken != null) {
2116 win.mAppToken.updateReportedVisibilityLocked();
2117 }
2118 }
2119 }
2120
2121 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2122 long origId = Binder.clearCallingIdentity();
2123 try {
2124 synchronized (mWindowMap) {
2125 WindowState w = windowForClientLocked(session, client);
2126 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002127 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 Surface.openTransaction();
2129 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002130 if (SHOW_TRANSACTIONS) Log.i(
2131 TAG, " SURFACE " + w.mSurface
2132 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 w.mSurface.setTransparentRegionHint(region);
2134 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002135 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 Surface.closeTransaction();
2137 }
2138 }
2139 }
2140 } finally {
2141 Binder.restoreCallingIdentity(origId);
2142 }
2143 }
2144
2145 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002146 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 Rect visibleInsets) {
2148 long origId = Binder.clearCallingIdentity();
2149 try {
2150 synchronized (mWindowMap) {
2151 WindowState w = windowForClientLocked(session, client);
2152 if (w != null) {
2153 w.mGivenInsetsPending = false;
2154 w.mGivenContentInsets.set(contentInsets);
2155 w.mGivenVisibleInsets.set(visibleInsets);
2156 w.mTouchableInsets = touchableInsets;
2157 mLayoutNeeded = true;
2158 performLayoutAndPlaceSurfacesLocked();
2159 }
2160 }
2161 } finally {
2162 Binder.restoreCallingIdentity(origId);
2163 }
2164 }
Romain Guy06882f82009-06-10 13:36:04 -07002165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 public void getWindowDisplayFrame(Session session, IWindow client,
2167 Rect outDisplayFrame) {
2168 synchronized(mWindowMap) {
2169 WindowState win = windowForClientLocked(session, client);
2170 if (win == null) {
2171 outDisplayFrame.setEmpty();
2172 return;
2173 }
2174 outDisplayFrame.set(win.mDisplayFrame);
2175 }
2176 }
2177
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002178 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2179 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2180 window.mWallpaperX = x;
2181 window.mWallpaperY = y;
2182
2183 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002184 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002185 performLayoutAndPlaceSurfacesLocked();
2186 }
2187 }
2188 }
2189 }
2190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 public int relayoutWindow(Session session, IWindow client,
2192 WindowManager.LayoutParams attrs, int requestedWidth,
2193 int requestedHeight, int viewVisibility, boolean insetsPending,
2194 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2195 Surface outSurface) {
2196 boolean displayed = false;
2197 boolean inTouchMode;
2198 Configuration newConfig = null;
2199 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 synchronized(mWindowMap) {
2202 WindowState win = windowForClientLocked(session, client);
2203 if (win == null) {
2204 return 0;
2205 }
2206 win.mRequestedWidth = requestedWidth;
2207 win.mRequestedHeight = requestedHeight;
2208
2209 if (attrs != null) {
2210 mPolicy.adjustWindowParamsLw(attrs);
2211 }
Romain Guy06882f82009-06-10 13:36:04 -07002212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 int attrChanges = 0;
2214 int flagChanges = 0;
2215 if (attrs != null) {
2216 flagChanges = win.mAttrs.flags ^= attrs.flags;
2217 attrChanges = win.mAttrs.copyFrom(attrs);
2218 }
2219
2220 if (localLOGV) Log.v(
2221 TAG, "Relayout given client " + client.asBinder()
2222 + " (" + win.mAttrs.getTitle() + ")");
2223
2224
2225 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2226 win.mAlpha = attrs.alpha;
2227 }
2228
2229 final boolean scaledWindow =
2230 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2231
2232 if (scaledWindow) {
2233 // requested{Width|Height} Surface's physical size
2234 // attrs.{width|height} Size on screen
2235 win.mHScale = (attrs.width != requestedWidth) ?
2236 (attrs.width / (float)requestedWidth) : 1.0f;
2237 win.mVScale = (attrs.height != requestedHeight) ?
2238 (attrs.height / (float)requestedHeight) : 1.0f;
2239 }
2240
2241 boolean imMayMove = (flagChanges&(
2242 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2243 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 boolean focusMayChange = win.mViewVisibility != viewVisibility
2246 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2247 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002248
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002249 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2250 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 win.mRelayoutCalled = true;
2253 final int oldVisibility = win.mViewVisibility;
2254 win.mViewVisibility = viewVisibility;
2255 if (viewVisibility == View.VISIBLE &&
2256 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2257 displayed = !win.isVisibleLw();
2258 if (win.mExiting) {
2259 win.mExiting = false;
2260 win.mAnimation = null;
2261 }
2262 if (win.mDestroying) {
2263 win.mDestroying = false;
2264 mDestroySurface.remove(win);
2265 }
2266 if (oldVisibility == View.GONE) {
2267 win.mEnterAnimationPending = true;
2268 }
2269 if (displayed && win.mSurface != null && !win.mDrawPending
2270 && !win.mCommitDrawPending && !mDisplayFrozen) {
2271 applyEnterAnimationLocked(win);
2272 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002273 if (displayed && (win.mAttrs.flags
2274 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2275 win.mTurnOnScreen = true;
2276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2278 // To change the format, we need to re-build the surface.
2279 win.destroySurfaceLocked();
2280 displayed = true;
2281 }
2282 try {
2283 Surface surface = win.createSurfaceLocked();
2284 if (surface != null) {
2285 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002286 win.mReportDestroySurface = false;
2287 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002288 if (SHOW_TRANSACTIONS) Log.i(TAG,
2289 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002291 // For some reason there isn't a surface. Clear the
2292 // caller's object so they see the same state.
2293 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
2295 } catch (Exception e) {
2296 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002297 + client + " (" + win.mAttrs.getTitle() + ")",
2298 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 Binder.restoreCallingIdentity(origId);
2300 return 0;
2301 }
2302 if (displayed) {
2303 focusMayChange = true;
2304 }
2305 if (win.mAttrs.type == TYPE_INPUT_METHOD
2306 && mInputMethodWindow == null) {
2307 mInputMethodWindow = win;
2308 imMayMove = true;
2309 }
2310 } else {
2311 win.mEnterAnimationPending = false;
2312 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002313 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2314 + ": mExiting=" + win.mExiting
2315 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 // If we are not currently running the exit animation, we
2317 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002318 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 // Try starting an animation; if there isn't one, we
2320 // can destroy the surface right away.
2321 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2322 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2323 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2324 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002325 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002327 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 win.mExiting = true;
2329 mKeyWaiter.finishedKey(session, client, true,
2330 KeyWaiter.RETURN_NOTHING);
2331 } else if (win.isAnimating()) {
2332 // Currently in a hide animation... turn this into
2333 // an exit.
2334 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002335 } else if (win == mWallpaperTarget) {
2336 // If the wallpaper is currently behind this
2337 // window, we need to change both of them inside
2338 // of a transaction to avoid artifacts.
2339 win.mExiting = true;
2340 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 } else {
2342 if (mInputMethodWindow == win) {
2343 mInputMethodWindow = null;
2344 }
2345 win.destroySurfaceLocked();
2346 }
2347 }
2348 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002349
2350 if (win.mSurface == null || (win.getAttrs().flags
2351 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2352 || win.mSurfacePendingDestroy) {
2353 // We are being called from a local process, which
2354 // means outSurface holds its current surface. Ensure the
2355 // surface object is cleared, but we don't want it actually
2356 // destroyed at this point.
2357 win.mSurfacePendingDestroy = false;
2358 outSurface.release();
2359 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2360 } else if (win.mSurface != null) {
2361 if (DEBUG_VISIBILITY) Log.i(TAG,
2362 "Keeping surface, will report destroy: " + win);
2363 win.mReportDestroySurface = true;
2364 outSurface.copyFrom(win.mSurface);
2365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 }
2367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 if (focusMayChange) {
2369 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2370 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 imMayMove = false;
2372 }
2373 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2374 }
Romain Guy06882f82009-06-10 13:36:04 -07002375
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002376 // updateFocusedWindowLocked() already assigned layers so we only need to
2377 // reassign them at this point if the IM window state gets shuffled
2378 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 if (imMayMove) {
2381 if (moveInputMethodWindowsIfNeededLocked(false)) {
2382 assignLayers = true;
2383 }
2384 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002385 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002386 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002387 assignLayers = true;
2388 }
2389 }
Romain Guy06882f82009-06-10 13:36:04 -07002390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 mLayoutNeeded = true;
2392 win.mGivenInsetsPending = insetsPending;
2393 if (assignLayers) {
2394 assignLayersLocked();
2395 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002396 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002398 if (displayed && win.mIsWallpaper) {
2399 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002400 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 if (win.mAppToken != null) {
2403 win.mAppToken.updateReportedVisibilityLocked();
2404 }
2405 outFrame.set(win.mFrame);
2406 outContentInsets.set(win.mContentInsets);
2407 outVisibleInsets.set(win.mVisibleInsets);
2408 if (localLOGV) Log.v(
2409 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002410 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 + ", requestedHeight=" + requestedHeight
2412 + ", viewVisibility=" + viewVisibility
2413 + "\nRelayout returning frame=" + outFrame
2414 + ", surface=" + outSurface);
2415
2416 if (localLOGV || DEBUG_FOCUS) Log.v(
2417 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2418
2419 inTouchMode = mInTouchMode;
2420 }
2421
2422 if (newConfig != null) {
2423 sendNewConfiguration();
2424 }
Romain Guy06882f82009-06-10 13:36:04 -07002425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2429 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2430 }
2431
2432 public void finishDrawingWindow(Session session, IWindow client) {
2433 final long origId = Binder.clearCallingIdentity();
2434 synchronized(mWindowMap) {
2435 WindowState win = windowForClientLocked(session, client);
2436 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002437 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2438 adjustWallpaperWindowsLocked();
2439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 mLayoutNeeded = true;
2441 performLayoutAndPlaceSurfacesLocked();
2442 }
2443 }
2444 Binder.restoreCallingIdentity(origId);
2445 }
2446
2447 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2448 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2449 + (lp != null ? lp.packageName : null)
2450 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2451 if (lp != null && lp.windowAnimations != 0) {
2452 // If this is a system resource, don't try to load it from the
2453 // application resources. It is nice to avoid loading application
2454 // resources if we can.
2455 String packageName = lp.packageName != null ? lp.packageName : "android";
2456 int resId = lp.windowAnimations;
2457 if ((resId&0xFF000000) == 0x01000000) {
2458 packageName = "android";
2459 }
2460 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2461 + packageName);
2462 return AttributeCache.instance().get(packageName, resId,
2463 com.android.internal.R.styleable.WindowAnimation);
2464 }
2465 return null;
2466 }
Romain Guy06882f82009-06-10 13:36:04 -07002467
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002468 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2469 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2470 + packageName + " resId=0x" + Integer.toHexString(resId));
2471 if (packageName != null) {
2472 if ((resId&0xFF000000) == 0x01000000) {
2473 packageName = "android";
2474 }
2475 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2476 + packageName);
2477 return AttributeCache.instance().get(packageName, resId,
2478 com.android.internal.R.styleable.WindowAnimation);
2479 }
2480 return null;
2481 }
2482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 private void applyEnterAnimationLocked(WindowState win) {
2484 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2485 if (win.mEnterAnimationPending) {
2486 win.mEnterAnimationPending = false;
2487 transit = WindowManagerPolicy.TRANSIT_ENTER;
2488 }
2489
2490 applyAnimationLocked(win, transit, true);
2491 }
2492
2493 private boolean applyAnimationLocked(WindowState win,
2494 int transit, boolean isEntrance) {
2495 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2496 // If we are trying to apply an animation, but already running
2497 // an animation of the same type, then just leave that one alone.
2498 return true;
2499 }
Romain Guy06882f82009-06-10 13:36:04 -07002500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 // Only apply an animation if the display isn't frozen. If it is
2502 // frozen, there is no reason to animate and it can cause strange
2503 // artifacts when we unfreeze the display if some different animation
2504 // is running.
2505 if (!mDisplayFrozen) {
2506 int anim = mPolicy.selectAnimationLw(win, transit);
2507 int attr = -1;
2508 Animation a = null;
2509 if (anim != 0) {
2510 a = AnimationUtils.loadAnimation(mContext, anim);
2511 } else {
2512 switch (transit) {
2513 case WindowManagerPolicy.TRANSIT_ENTER:
2514 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2515 break;
2516 case WindowManagerPolicy.TRANSIT_EXIT:
2517 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2518 break;
2519 case WindowManagerPolicy.TRANSIT_SHOW:
2520 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2521 break;
2522 case WindowManagerPolicy.TRANSIT_HIDE:
2523 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2524 break;
2525 }
2526 if (attr >= 0) {
2527 a = loadAnimation(win.mAttrs, attr);
2528 }
2529 }
2530 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2531 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2532 + " mAnimation=" + win.mAnimation
2533 + " isEntrance=" + isEntrance);
2534 if (a != null) {
2535 if (DEBUG_ANIM) {
2536 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002537 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2539 }
2540 win.setAnimation(a);
2541 win.mAnimationIsEntrance = isEntrance;
2542 }
2543 } else {
2544 win.clearAnimation();
2545 }
2546
2547 return win.mAnimation != null;
2548 }
2549
2550 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2551 int anim = 0;
2552 Context context = mContext;
2553 if (animAttr >= 0) {
2554 AttributeCache.Entry ent = getCachedAnimations(lp);
2555 if (ent != null) {
2556 context = ent.context;
2557 anim = ent.array.getResourceId(animAttr, 0);
2558 }
2559 }
2560 if (anim != 0) {
2561 return AnimationUtils.loadAnimation(context, anim);
2562 }
2563 return null;
2564 }
Romain Guy06882f82009-06-10 13:36:04 -07002565
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002566 private Animation loadAnimation(String packageName, int resId) {
2567 int anim = 0;
2568 Context context = mContext;
2569 if (resId >= 0) {
2570 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2571 if (ent != null) {
2572 context = ent.context;
2573 anim = resId;
2574 }
2575 }
2576 if (anim != 0) {
2577 return AnimationUtils.loadAnimation(context, anim);
2578 }
2579 return null;
2580 }
2581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 private boolean applyAnimationLocked(AppWindowToken wtoken,
2583 WindowManager.LayoutParams lp, int transit, boolean enter) {
2584 // Only apply an animation if the display isn't frozen. If it is
2585 // frozen, there is no reason to animate and it can cause strange
2586 // artifacts when we unfreeze the display if some different animation
2587 // is running.
2588 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002589 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002590 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002591 a = new FadeInOutAnimation(enter);
2592 if (DEBUG_ANIM) Log.v(TAG,
2593 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002594 } else if (mNextAppTransitionPackage != null) {
2595 a = loadAnimation(mNextAppTransitionPackage, enter ?
2596 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002597 } else {
2598 int animAttr = 0;
2599 switch (transit) {
2600 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2601 animAttr = enter
2602 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2603 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2604 break;
2605 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2606 animAttr = enter
2607 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2608 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2609 break;
2610 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2611 animAttr = enter
2612 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2613 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2614 break;
2615 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2616 animAttr = enter
2617 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2618 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2619 break;
2620 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2621 animAttr = enter
2622 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2623 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2624 break;
2625 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2626 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002627 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002628 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2629 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002630 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002631 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002632 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2633 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002634 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002635 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002636 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002637 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2638 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2639 break;
2640 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2641 animAttr = enter
2642 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2643 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2644 break;
2645 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2646 animAttr = enter
2647 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2648 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002649 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002650 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002651 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002652 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2653 + " anim=" + a
2654 + " animAttr=0x" + Integer.toHexString(animAttr)
2655 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 if (a != null) {
2658 if (DEBUG_ANIM) {
2659 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002660 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2662 }
2663 wtoken.setAnimation(a);
2664 }
2665 } else {
2666 wtoken.clearAnimation();
2667 }
2668
2669 return wtoken.animation != null;
2670 }
2671
2672 // -------------------------------------------------------------
2673 // Application Window Tokens
2674 // -------------------------------------------------------------
2675
2676 public void validateAppTokens(List tokens) {
2677 int v = tokens.size()-1;
2678 int m = mAppTokens.size()-1;
2679 while (v >= 0 && m >= 0) {
2680 AppWindowToken wtoken = mAppTokens.get(m);
2681 if (wtoken.removed) {
2682 m--;
2683 continue;
2684 }
2685 if (tokens.get(v) != wtoken.token) {
2686 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2687 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2688 }
2689 v--;
2690 m--;
2691 }
2692 while (v >= 0) {
2693 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2694 v--;
2695 }
2696 while (m >= 0) {
2697 AppWindowToken wtoken = mAppTokens.get(m);
2698 if (!wtoken.removed) {
2699 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2700 }
2701 m--;
2702 }
2703 }
2704
2705 boolean checkCallingPermission(String permission, String func) {
2706 // Quick check: if the calling permission is me, it's all okay.
2707 if (Binder.getCallingPid() == Process.myPid()) {
2708 return true;
2709 }
Romain Guy06882f82009-06-10 13:36:04 -07002710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 if (mContext.checkCallingPermission(permission)
2712 == PackageManager.PERMISSION_GRANTED) {
2713 return true;
2714 }
2715 String msg = "Permission Denial: " + func + " from pid="
2716 + Binder.getCallingPid()
2717 + ", uid=" + Binder.getCallingUid()
2718 + " requires " + permission;
2719 Log.w(TAG, msg);
2720 return false;
2721 }
Romain Guy06882f82009-06-10 13:36:04 -07002722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 AppWindowToken findAppWindowToken(IBinder token) {
2724 WindowToken wtoken = mTokenMap.get(token);
2725 if (wtoken == null) {
2726 return null;
2727 }
2728 return wtoken.appWindowToken;
2729 }
Romain Guy06882f82009-06-10 13:36:04 -07002730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 public void addWindowToken(IBinder token, int type) {
2732 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2733 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002734 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
Romain Guy06882f82009-06-10 13:36:04 -07002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 synchronized(mWindowMap) {
2738 WindowToken wtoken = mTokenMap.get(token);
2739 if (wtoken != null) {
2740 Log.w(TAG, "Attempted to add existing input method token: " + token);
2741 return;
2742 }
2743 wtoken = new WindowToken(token, type, true);
2744 mTokenMap.put(token, wtoken);
2745 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002746 if (type == TYPE_WALLPAPER) {
2747 mWallpaperTokens.add(wtoken);
2748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
2750 }
Romain Guy06882f82009-06-10 13:36:04 -07002751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 public void removeWindowToken(IBinder token) {
2753 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2754 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002755 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
2757
2758 final long origId = Binder.clearCallingIdentity();
2759 synchronized(mWindowMap) {
2760 WindowToken wtoken = mTokenMap.remove(token);
2761 mTokenList.remove(wtoken);
2762 if (wtoken != null) {
2763 boolean delayed = false;
2764 if (!wtoken.hidden) {
2765 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 final int N = wtoken.windows.size();
2768 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 for (int i=0; i<N; i++) {
2771 WindowState win = wtoken.windows.get(i);
2772
2773 if (win.isAnimating()) {
2774 delayed = true;
2775 }
Romain Guy06882f82009-06-10 13:36:04 -07002776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 if (win.isVisibleNow()) {
2778 applyAnimationLocked(win,
2779 WindowManagerPolicy.TRANSIT_EXIT, false);
2780 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2781 KeyWaiter.RETURN_NOTHING);
2782 changed = true;
2783 }
2784 }
2785
2786 if (changed) {
2787 mLayoutNeeded = true;
2788 performLayoutAndPlaceSurfacesLocked();
2789 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2790 }
Romain Guy06882f82009-06-10 13:36:04 -07002791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 if (delayed) {
2793 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002794 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2795 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 }
2797 }
Romain Guy06882f82009-06-10 13:36:04 -07002798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 } else {
2800 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2801 }
2802 }
2803 Binder.restoreCallingIdentity(origId);
2804 }
2805
2806 public void addAppToken(int addPos, IApplicationToken token,
2807 int groupId, int requestedOrientation, boolean fullscreen) {
2808 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2809 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002810 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Romain Guy06882f82009-06-10 13:36:04 -07002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 synchronized(mWindowMap) {
2814 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2815 if (wtoken != null) {
2816 Log.w(TAG, "Attempted to add existing app token: " + token);
2817 return;
2818 }
2819 wtoken = new AppWindowToken(token);
2820 wtoken.groupId = groupId;
2821 wtoken.appFullscreen = fullscreen;
2822 wtoken.requestedOrientation = requestedOrientation;
2823 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002824 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 mTokenMap.put(token.asBinder(), wtoken);
2826 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 // Application tokens start out hidden.
2829 wtoken.hidden = true;
2830 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 //dump();
2833 }
2834 }
Romain Guy06882f82009-06-10 13:36:04 -07002835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 public void setAppGroupId(IBinder token, int groupId) {
2837 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2838 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002839 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 }
2841
2842 synchronized(mWindowMap) {
2843 AppWindowToken wtoken = findAppWindowToken(token);
2844 if (wtoken == null) {
2845 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2846 return;
2847 }
2848 wtoken.groupId = groupId;
2849 }
2850 }
Romain Guy06882f82009-06-10 13:36:04 -07002851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 public int getOrientationFromWindowsLocked() {
2853 int pos = mWindows.size() - 1;
2854 while (pos >= 0) {
2855 WindowState wtoken = (WindowState) mWindows.get(pos);
2856 pos--;
2857 if (wtoken.mAppToken != null) {
2858 // We hit an application window. so the orientation will be determined by the
2859 // app window. No point in continuing further.
2860 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2861 }
2862 if (!wtoken.isVisibleLw()) {
2863 continue;
2864 }
2865 int req = wtoken.mAttrs.screenOrientation;
2866 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2867 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2868 continue;
2869 } else {
2870 return req;
2871 }
2872 }
2873 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2874 }
Romain Guy06882f82009-06-10 13:36:04 -07002875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 public int getOrientationFromAppTokensLocked() {
2877 int pos = mAppTokens.size() - 1;
2878 int curGroup = 0;
2879 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002880 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002882 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 while (pos >= 0) {
2884 AppWindowToken wtoken = mAppTokens.get(pos);
2885 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002886 // if we're about to tear down this window and not seek for
2887 // the behind activity, don't use it for orientation
2888 if (!findingBehind
2889 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002890 continue;
2891 }
2892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 if (!haveGroup) {
2894 // We ignore any hidden applications on the top.
2895 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2896 continue;
2897 }
2898 haveGroup = true;
2899 curGroup = wtoken.groupId;
2900 lastOrientation = wtoken.requestedOrientation;
2901 } else if (curGroup != wtoken.groupId) {
2902 // If we have hit a new application group, and the bottom
2903 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002904 // the orientation behind it, and the last app was
2905 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002907 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2908 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 return lastOrientation;
2910 }
2911 }
2912 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002913 // If this application is fullscreen, and didn't explicitly say
2914 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002916 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002917 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002918 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 return or;
2920 }
2921 // If this application has requested an explicit orientation,
2922 // then use it.
2923 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2924 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2925 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2926 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2927 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2928 return or;
2929 }
Owen Lin3413b892009-05-01 17:12:32 -07002930 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 }
2932 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2933 }
Romain Guy06882f82009-06-10 13:36:04 -07002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002936 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002937 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2938 "updateOrientationFromAppTokens()")) {
2939 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2940 }
2941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 Configuration config;
2943 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002944 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2945 freezeThisOneIfNeeded);
2946 Binder.restoreCallingIdentity(ident);
2947 return config;
2948 }
2949
2950 Configuration updateOrientationFromAppTokensUnchecked(
2951 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2952 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002954 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002955 if (config != null) {
2956 mLayoutNeeded = true;
2957 performLayoutAndPlaceSurfacesLocked();
2958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 return config;
2961 }
Romain Guy06882f82009-06-10 13:36:04 -07002962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 /*
2964 * The orientation is computed from non-application windows first. If none of
2965 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002966 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2968 * android.os.IBinder)
2969 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002970 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002971 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 long ident = Binder.clearCallingIdentity();
2974 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002975 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 if (req != mForcedAppOrientation) {
2978 changed = true;
2979 mForcedAppOrientation = req;
2980 //send a message to Policy indicating orientation change to take
2981 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002982 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 }
Romain Guy06882f82009-06-10 13:36:04 -07002984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 if (changed) {
2986 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002987 WindowManagerPolicy.USE_LAST_ROTATION,
2988 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 if (changed) {
2990 if (freezeThisOneIfNeeded != null) {
2991 AppWindowToken wtoken = findAppWindowToken(
2992 freezeThisOneIfNeeded);
2993 if (wtoken != null) {
2994 startAppFreezingScreenLocked(wtoken,
2995 ActivityInfo.CONFIG_ORIENTATION);
2996 }
2997 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002998 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 }
3000 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003001
3002 // No obvious action we need to take, but if our current
3003 // state mismatches the activity maanager's, update it
3004 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003005 mTempConfiguration.setToDefaults();
3006 if (computeNewConfigurationLocked(mTempConfiguration)) {
3007 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003008 return new Configuration(mTempConfiguration);
3009 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003010 }
3011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 } finally {
3013 Binder.restoreCallingIdentity(ident);
3014 }
Romain Guy06882f82009-06-10 13:36:04 -07003015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 return null;
3017 }
Romain Guy06882f82009-06-10 13:36:04 -07003018
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003019 int computeForcedAppOrientationLocked() {
3020 int req = getOrientationFromWindowsLocked();
3021 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3022 req = getOrientationFromAppTokensLocked();
3023 }
3024 return req;
3025 }
Romain Guy06882f82009-06-10 13:36:04 -07003026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3028 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3029 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003030 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 }
Romain Guy06882f82009-06-10 13:36:04 -07003032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 synchronized(mWindowMap) {
3034 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3035 if (wtoken == null) {
3036 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3037 return;
3038 }
Romain Guy06882f82009-06-10 13:36:04 -07003039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 wtoken.requestedOrientation = requestedOrientation;
3041 }
3042 }
Romain Guy06882f82009-06-10 13:36:04 -07003043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 public int getAppOrientation(IApplicationToken token) {
3045 synchronized(mWindowMap) {
3046 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3047 if (wtoken == null) {
3048 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3049 }
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 return wtoken.requestedOrientation;
3052 }
3053 }
Romain Guy06882f82009-06-10 13:36:04 -07003054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3056 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3057 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003058 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
3060
3061 synchronized(mWindowMap) {
3062 boolean changed = false;
3063 if (token == null) {
3064 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3065 changed = mFocusedApp != null;
3066 mFocusedApp = null;
3067 mKeyWaiter.tickle();
3068 } else {
3069 AppWindowToken newFocus = findAppWindowToken(token);
3070 if (newFocus == null) {
3071 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3072 return;
3073 }
3074 changed = mFocusedApp != newFocus;
3075 mFocusedApp = newFocus;
3076 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3077 mKeyWaiter.tickle();
3078 }
3079
3080 if (moveFocusNow && changed) {
3081 final long origId = Binder.clearCallingIdentity();
3082 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3083 Binder.restoreCallingIdentity(origId);
3084 }
3085 }
3086 }
3087
3088 public void prepareAppTransition(int transit) {
3089 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3090 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003091 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 }
Romain Guy06882f82009-06-10 13:36:04 -07003093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 synchronized(mWindowMap) {
3095 if (DEBUG_APP_TRANSITIONS) Log.v(
3096 TAG, "Prepare app transition: transit=" + transit
3097 + " mNextAppTransition=" + mNextAppTransition);
3098 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003099 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3100 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003102 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3103 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3104 // Opening a new task always supersedes a close for the anim.
3105 mNextAppTransition = transit;
3106 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3107 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3108 // Opening a new activity always supersedes a close for the anim.
3109 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 }
3111 mAppTransitionReady = false;
3112 mAppTransitionTimeout = false;
3113 mStartingIconInTransition = false;
3114 mSkipAppTransitionAnimation = false;
3115 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3116 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3117 5000);
3118 }
3119 }
3120 }
3121
3122 public int getPendingAppTransition() {
3123 return mNextAppTransition;
3124 }
Romain Guy06882f82009-06-10 13:36:04 -07003125
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003126 public void overridePendingAppTransition(String packageName,
3127 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003128 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003129 mNextAppTransitionPackage = packageName;
3130 mNextAppTransitionEnter = enterAnim;
3131 mNextAppTransitionExit = exitAnim;
3132 }
3133 }
3134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 public void executeAppTransition() {
3136 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3137 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003138 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 }
Romain Guy06882f82009-06-10 13:36:04 -07003140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003142 if (DEBUG_APP_TRANSITIONS) {
3143 RuntimeException e = new RuntimeException("here");
3144 e.fillInStackTrace();
3145 Log.w(TAG, "Execute app transition: mNextAppTransition="
3146 + mNextAppTransition, e);
3147 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003148 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 mAppTransitionReady = true;
3150 final long origId = Binder.clearCallingIdentity();
3151 performLayoutAndPlaceSurfacesLocked();
3152 Binder.restoreCallingIdentity(origId);
3153 }
3154 }
3155 }
3156
3157 public void setAppStartingWindow(IBinder token, String pkg,
3158 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3159 IBinder transferFrom, boolean createIfNeeded) {
3160 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3161 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003162 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
3164
3165 synchronized(mWindowMap) {
3166 if (DEBUG_STARTING_WINDOW) Log.v(
3167 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3168 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 AppWindowToken wtoken = findAppWindowToken(token);
3171 if (wtoken == null) {
3172 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3173 return;
3174 }
3175
3176 // If the display is frozen, we won't do anything until the
3177 // actual window is displayed so there is no reason to put in
3178 // the starting window.
3179 if (mDisplayFrozen) {
3180 return;
3181 }
Romain Guy06882f82009-06-10 13:36:04 -07003182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 if (wtoken.startingData != null) {
3184 return;
3185 }
Romain Guy06882f82009-06-10 13:36:04 -07003186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 if (transferFrom != null) {
3188 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3189 if (ttoken != null) {
3190 WindowState startingWindow = ttoken.startingWindow;
3191 if (startingWindow != null) {
3192 if (mStartingIconInTransition) {
3193 // In this case, the starting icon has already
3194 // been displayed, so start letting windows get
3195 // shown immediately without any more transitions.
3196 mSkipAppTransitionAnimation = true;
3197 }
3198 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3199 "Moving existing starting from " + ttoken
3200 + " to " + wtoken);
3201 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 // Transfer the starting window over to the new
3204 // token.
3205 wtoken.startingData = ttoken.startingData;
3206 wtoken.startingView = ttoken.startingView;
3207 wtoken.startingWindow = startingWindow;
3208 ttoken.startingData = null;
3209 ttoken.startingView = null;
3210 ttoken.startingWindow = null;
3211 ttoken.startingMoved = true;
3212 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003213 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003215 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3216 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 mWindows.remove(startingWindow);
3218 ttoken.windows.remove(startingWindow);
3219 ttoken.allAppWindows.remove(startingWindow);
3220 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 // Propagate other interesting state between the
3223 // tokens. If the old token is displayed, we should
3224 // immediately force the new one to be displayed. If
3225 // it is animating, we need to move that animation to
3226 // the new one.
3227 if (ttoken.allDrawn) {
3228 wtoken.allDrawn = true;
3229 }
3230 if (ttoken.firstWindowDrawn) {
3231 wtoken.firstWindowDrawn = true;
3232 }
3233 if (!ttoken.hidden) {
3234 wtoken.hidden = false;
3235 wtoken.hiddenRequested = false;
3236 wtoken.willBeHidden = false;
3237 }
3238 if (wtoken.clientHidden != ttoken.clientHidden) {
3239 wtoken.clientHidden = ttoken.clientHidden;
3240 wtoken.sendAppVisibilityToClients();
3241 }
3242 if (ttoken.animation != null) {
3243 wtoken.animation = ttoken.animation;
3244 wtoken.animating = ttoken.animating;
3245 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3246 ttoken.animation = null;
3247 ttoken.animLayerAdjustment = 0;
3248 wtoken.updateLayers();
3249 ttoken.updateLayers();
3250 }
Romain Guy06882f82009-06-10 13:36:04 -07003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 mLayoutNeeded = true;
3254 performLayoutAndPlaceSurfacesLocked();
3255 Binder.restoreCallingIdentity(origId);
3256 return;
3257 } else if (ttoken.startingData != null) {
3258 // The previous app was getting ready to show a
3259 // starting window, but hasn't yet done so. Steal it!
3260 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3261 "Moving pending starting from " + ttoken
3262 + " to " + wtoken);
3263 wtoken.startingData = ttoken.startingData;
3264 ttoken.startingData = null;
3265 ttoken.startingMoved = true;
3266 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3267 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3268 // want to process the message ASAP, before any other queued
3269 // messages.
3270 mH.sendMessageAtFrontOfQueue(m);
3271 return;
3272 }
3273 }
3274 }
3275
3276 // There is no existing starting window, and the caller doesn't
3277 // want us to create one, so that's it!
3278 if (!createIfNeeded) {
3279 return;
3280 }
Romain Guy06882f82009-06-10 13:36:04 -07003281
Dianne Hackborn284ac932009-08-28 10:34:25 -07003282 // If this is a translucent or wallpaper window, then don't
3283 // show a starting window -- the current effect (a full-screen
3284 // opaque starting window that fades away to the real contents
3285 // when it is ready) does not work for this.
3286 if (theme != 0) {
3287 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3288 com.android.internal.R.styleable.Window);
3289 if (ent.array.getBoolean(
3290 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3291 return;
3292 }
3293 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003294 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3295 return;
3296 }
3297 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003298 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3299 return;
3300 }
3301 }
3302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 mStartingIconInTransition = true;
3304 wtoken.startingData = new StartingData(
3305 pkg, theme, nonLocalizedLabel,
3306 labelRes, icon);
3307 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3308 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3309 // want to process the message ASAP, before any other queued
3310 // messages.
3311 mH.sendMessageAtFrontOfQueue(m);
3312 }
3313 }
3314
3315 public void setAppWillBeHidden(IBinder token) {
3316 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3317 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003318 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 }
3320
3321 AppWindowToken wtoken;
3322
3323 synchronized(mWindowMap) {
3324 wtoken = findAppWindowToken(token);
3325 if (wtoken == null) {
3326 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3327 return;
3328 }
3329 wtoken.willBeHidden = true;
3330 }
3331 }
Romain Guy06882f82009-06-10 13:36:04 -07003332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3334 boolean visible, int transit, boolean performLayout) {
3335 boolean delayed = false;
3336
3337 if (wtoken.clientHidden == visible) {
3338 wtoken.clientHidden = !visible;
3339 wtoken.sendAppVisibilityToClients();
3340 }
Romain Guy06882f82009-06-10 13:36:04 -07003341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 wtoken.willBeHidden = false;
3343 if (wtoken.hidden == visible) {
3344 final int N = wtoken.allAppWindows.size();
3345 boolean changed = false;
3346 if (DEBUG_APP_TRANSITIONS) Log.v(
3347 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3348 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003351
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003352 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 if (wtoken.animation == sDummyAnimation) {
3354 wtoken.animation = null;
3355 }
3356 applyAnimationLocked(wtoken, lp, transit, visible);
3357 changed = true;
3358 if (wtoken.animation != null) {
3359 delayed = runningAppAnimation = true;
3360 }
3361 }
Romain Guy06882f82009-06-10 13:36:04 -07003362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 for (int i=0; i<N; i++) {
3364 WindowState win = wtoken.allAppWindows.get(i);
3365 if (win == wtoken.startingWindow) {
3366 continue;
3367 }
3368
3369 if (win.isAnimating()) {
3370 delayed = true;
3371 }
Romain Guy06882f82009-06-10 13:36:04 -07003372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3374 //win.dump(" ");
3375 if (visible) {
3376 if (!win.isVisibleNow()) {
3377 if (!runningAppAnimation) {
3378 applyAnimationLocked(win,
3379 WindowManagerPolicy.TRANSIT_ENTER, true);
3380 }
3381 changed = true;
3382 }
3383 } else if (win.isVisibleNow()) {
3384 if (!runningAppAnimation) {
3385 applyAnimationLocked(win,
3386 WindowManagerPolicy.TRANSIT_EXIT, false);
3387 }
3388 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3389 KeyWaiter.RETURN_NOTHING);
3390 changed = true;
3391 }
3392 }
3393
3394 wtoken.hidden = wtoken.hiddenRequested = !visible;
3395 if (!visible) {
3396 unsetAppFreezingScreenLocked(wtoken, true, true);
3397 } else {
3398 // If we are being set visible, and the starting window is
3399 // not yet displayed, then make sure it doesn't get displayed.
3400 WindowState swin = wtoken.startingWindow;
3401 if (swin != null && (swin.mDrawPending
3402 || swin.mCommitDrawPending)) {
3403 swin.mPolicyVisibility = false;
3404 swin.mPolicyVisibilityAfterAnim = false;
3405 }
3406 }
Romain Guy06882f82009-06-10 13:36:04 -07003407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3409 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3410 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 if (changed && performLayout) {
3413 mLayoutNeeded = true;
3414 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 performLayoutAndPlaceSurfacesLocked();
3416 }
3417 }
3418
3419 if (wtoken.animation != null) {
3420 delayed = true;
3421 }
Romain Guy06882f82009-06-10 13:36:04 -07003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 return delayed;
3424 }
3425
3426 public void setAppVisibility(IBinder token, boolean visible) {
3427 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3428 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003429 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 }
3431
3432 AppWindowToken wtoken;
3433
3434 synchronized(mWindowMap) {
3435 wtoken = findAppWindowToken(token);
3436 if (wtoken == null) {
3437 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3438 return;
3439 }
3440
3441 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3442 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003443 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3445 + "): mNextAppTransition=" + mNextAppTransition
3446 + " hidden=" + wtoken.hidden
3447 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3448 }
Romain Guy06882f82009-06-10 13:36:04 -07003449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 // If we are preparing an app transition, then delay changing
3451 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003452 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 // Already in requested state, don't do anything more.
3454 if (wtoken.hiddenRequested != visible) {
3455 return;
3456 }
3457 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 if (DEBUG_APP_TRANSITIONS) Log.v(
3460 TAG, "Setting dummy animation on: " + wtoken);
3461 wtoken.setDummyAnimation();
3462 mOpeningApps.remove(wtoken);
3463 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003464 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 wtoken.inPendingTransaction = true;
3466 if (visible) {
3467 mOpeningApps.add(wtoken);
3468 wtoken.allDrawn = false;
3469 wtoken.startingDisplayed = false;
3470 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003471 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 if (wtoken.clientHidden) {
3474 // In the case where we are making an app visible
3475 // but holding off for a transition, we still need
3476 // to tell the client to make its windows visible so
3477 // they get drawn. Otherwise, we will wait on
3478 // performing the transition until all windows have
3479 // been drawn, they never will be, and we are sad.
3480 wtoken.clientHidden = false;
3481 wtoken.sendAppVisibilityToClients();
3482 }
3483 } else {
3484 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003485 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 }
3487 return;
3488 }
Romain Guy06882f82009-06-10 13:36:04 -07003489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003491 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 wtoken.updateReportedVisibilityLocked();
3493 Binder.restoreCallingIdentity(origId);
3494 }
3495 }
3496
3497 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3498 boolean unfreezeSurfaceNow, boolean force) {
3499 if (wtoken.freezingScreen) {
3500 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3501 + " force=" + force);
3502 final int N = wtoken.allAppWindows.size();
3503 boolean unfrozeWindows = false;
3504 for (int i=0; i<N; i++) {
3505 WindowState w = wtoken.allAppWindows.get(i);
3506 if (w.mAppFreezing) {
3507 w.mAppFreezing = false;
3508 if (w.mSurface != null && !w.mOrientationChanging) {
3509 w.mOrientationChanging = true;
3510 }
3511 unfrozeWindows = true;
3512 }
3513 }
3514 if (force || unfrozeWindows) {
3515 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3516 wtoken.freezingScreen = false;
3517 mAppsFreezingScreen--;
3518 }
3519 if (unfreezeSurfaceNow) {
3520 if (unfrozeWindows) {
3521 mLayoutNeeded = true;
3522 performLayoutAndPlaceSurfacesLocked();
3523 }
3524 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3525 stopFreezingDisplayLocked();
3526 }
3527 }
3528 }
3529 }
Romain Guy06882f82009-06-10 13:36:04 -07003530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3532 int configChanges) {
3533 if (DEBUG_ORIENTATION) {
3534 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003535 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 Log.i(TAG, "Set freezing of " + wtoken.appToken
3537 + ": hidden=" + wtoken.hidden + " freezing="
3538 + wtoken.freezingScreen, e);
3539 }
3540 if (!wtoken.hiddenRequested) {
3541 if (!wtoken.freezingScreen) {
3542 wtoken.freezingScreen = true;
3543 mAppsFreezingScreen++;
3544 if (mAppsFreezingScreen == 1) {
3545 startFreezingDisplayLocked();
3546 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3547 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3548 5000);
3549 }
3550 }
3551 final int N = wtoken.allAppWindows.size();
3552 for (int i=0; i<N; i++) {
3553 WindowState w = wtoken.allAppWindows.get(i);
3554 w.mAppFreezing = true;
3555 }
3556 }
3557 }
Romain Guy06882f82009-06-10 13:36:04 -07003558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 public void startAppFreezingScreen(IBinder token, int configChanges) {
3560 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3561 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003562 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 }
3564
3565 synchronized(mWindowMap) {
3566 if (configChanges == 0 && !mDisplayFrozen) {
3567 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3568 return;
3569 }
Romain Guy06882f82009-06-10 13:36:04 -07003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 AppWindowToken wtoken = findAppWindowToken(token);
3572 if (wtoken == null || wtoken.appToken == null) {
3573 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3574 return;
3575 }
3576 final long origId = Binder.clearCallingIdentity();
3577 startAppFreezingScreenLocked(wtoken, configChanges);
3578 Binder.restoreCallingIdentity(origId);
3579 }
3580 }
Romain Guy06882f82009-06-10 13:36:04 -07003581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 public void stopAppFreezingScreen(IBinder token, boolean force) {
3583 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3584 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003585 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 }
3587
3588 synchronized(mWindowMap) {
3589 AppWindowToken wtoken = findAppWindowToken(token);
3590 if (wtoken == null || wtoken.appToken == null) {
3591 return;
3592 }
3593 final long origId = Binder.clearCallingIdentity();
3594 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3595 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3596 unsetAppFreezingScreenLocked(wtoken, true, force);
3597 Binder.restoreCallingIdentity(origId);
3598 }
3599 }
Romain Guy06882f82009-06-10 13:36:04 -07003600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 public void removeAppToken(IBinder token) {
3602 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3603 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003604 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 }
3606
3607 AppWindowToken wtoken = null;
3608 AppWindowToken startingToken = null;
3609 boolean delayed = false;
3610
3611 final long origId = Binder.clearCallingIdentity();
3612 synchronized(mWindowMap) {
3613 WindowToken basewtoken = mTokenMap.remove(token);
3614 mTokenList.remove(basewtoken);
3615 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3616 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003617 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 wtoken.inPendingTransaction = false;
3619 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003620 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 if (mClosingApps.contains(wtoken)) {
3622 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003623 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003625 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 delayed = true;
3627 }
3628 if (DEBUG_APP_TRANSITIONS) Log.v(
3629 TAG, "Removing app " + wtoken + " delayed=" + delayed
3630 + " animation=" + wtoken.animation
3631 + " animating=" + wtoken.animating);
3632 if (delayed) {
3633 // set the token aside because it has an active animation to be finished
3634 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003635 } else {
3636 // Make sure there is no animation running on this token,
3637 // so any windows associated with it will be removed as
3638 // soon as their animations are complete
3639 wtoken.animation = null;
3640 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 }
3642 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003643 if (mLastEnterAnimToken == wtoken) {
3644 mLastEnterAnimToken = null;
3645 mLastEnterAnimParams = null;
3646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 wtoken.removed = true;
3648 if (wtoken.startingData != null) {
3649 startingToken = wtoken;
3650 }
3651 unsetAppFreezingScreenLocked(wtoken, true, true);
3652 if (mFocusedApp == wtoken) {
3653 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3654 mFocusedApp = null;
3655 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3656 mKeyWaiter.tickle();
3657 }
3658 } else {
3659 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3660 }
Romain Guy06882f82009-06-10 13:36:04 -07003661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 if (!delayed && wtoken != null) {
3663 wtoken.updateReportedVisibilityLocked();
3664 }
3665 }
3666 Binder.restoreCallingIdentity(origId);
3667
3668 if (startingToken != null) {
3669 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3670 + startingToken + ": app token removed");
3671 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3672 mH.sendMessage(m);
3673 }
3674 }
3675
3676 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3677 final int NW = token.windows.size();
3678 for (int i=0; i<NW; i++) {
3679 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003680 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 mWindows.remove(win);
3682 int j = win.mChildWindows.size();
3683 while (j > 0) {
3684 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003685 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3686 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3687 "Tmp removing child window " + cwin);
3688 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 }
3690 }
3691 return NW > 0;
3692 }
3693
3694 void dumpAppTokensLocked() {
3695 for (int i=mAppTokens.size()-1; i>=0; i--) {
3696 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3697 }
3698 }
Romain Guy06882f82009-06-10 13:36:04 -07003699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 void dumpWindowsLocked() {
3701 for (int i=mWindows.size()-1; i>=0; i--) {
3702 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3703 }
3704 }
Romain Guy06882f82009-06-10 13:36:04 -07003705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 private int findWindowOffsetLocked(int tokenPos) {
3707 final int NW = mWindows.size();
3708
3709 if (tokenPos >= mAppTokens.size()) {
3710 int i = NW;
3711 while (i > 0) {
3712 i--;
3713 WindowState win = (WindowState)mWindows.get(i);
3714 if (win.getAppToken() != null) {
3715 return i+1;
3716 }
3717 }
3718 }
3719
3720 while (tokenPos > 0) {
3721 // Find the first app token below the new position that has
3722 // a window displayed.
3723 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3724 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3725 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003726 if (wtoken.sendingToBottom) {
3727 if (DEBUG_REORDER) Log.v(TAG,
3728 "Skipping token -- currently sending to bottom");
3729 tokenPos--;
3730 continue;
3731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 int i = wtoken.windows.size();
3733 while (i > 0) {
3734 i--;
3735 WindowState win = wtoken.windows.get(i);
3736 int j = win.mChildWindows.size();
3737 while (j > 0) {
3738 j--;
3739 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003740 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 for (int pos=NW-1; pos>=0; pos--) {
3742 if (mWindows.get(pos) == cwin) {
3743 if (DEBUG_REORDER) Log.v(TAG,
3744 "Found child win @" + (pos+1));
3745 return pos+1;
3746 }
3747 }
3748 }
3749 }
3750 for (int pos=NW-1; pos>=0; pos--) {
3751 if (mWindows.get(pos) == win) {
3752 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3753 return pos+1;
3754 }
3755 }
3756 }
3757 tokenPos--;
3758 }
3759
3760 return 0;
3761 }
3762
3763 private final int reAddWindowLocked(int index, WindowState win) {
3764 final int NCW = win.mChildWindows.size();
3765 boolean added = false;
3766 for (int j=0; j<NCW; j++) {
3767 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3768 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003769 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3770 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 mWindows.add(index, win);
3772 index++;
3773 added = true;
3774 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003775 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3776 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 mWindows.add(index, cwin);
3778 index++;
3779 }
3780 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003781 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3782 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 mWindows.add(index, win);
3784 index++;
3785 }
3786 return index;
3787 }
Romain Guy06882f82009-06-10 13:36:04 -07003788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3790 final int NW = token.windows.size();
3791 for (int i=0; i<NW; i++) {
3792 index = reAddWindowLocked(index, token.windows.get(i));
3793 }
3794 return index;
3795 }
3796
3797 public void moveAppToken(int index, IBinder token) {
3798 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3799 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003800 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 }
3802
3803 synchronized(mWindowMap) {
3804 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3805 if (DEBUG_REORDER) dumpAppTokensLocked();
3806 final AppWindowToken wtoken = findAppWindowToken(token);
3807 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3808 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3809 + token + " (" + wtoken + ")");
3810 return;
3811 }
3812 mAppTokens.add(index, wtoken);
3813 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3814 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 final long origId = Binder.clearCallingIdentity();
3817 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3818 if (DEBUG_REORDER) dumpWindowsLocked();
3819 if (tmpRemoveAppWindowsLocked(wtoken)) {
3820 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3821 if (DEBUG_REORDER) dumpWindowsLocked();
3822 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3823 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3824 if (DEBUG_REORDER) dumpWindowsLocked();
3825 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 mLayoutNeeded = true;
3827 performLayoutAndPlaceSurfacesLocked();
3828 }
3829 Binder.restoreCallingIdentity(origId);
3830 }
3831 }
3832
3833 private void removeAppTokensLocked(List<IBinder> tokens) {
3834 // XXX This should be done more efficiently!
3835 // (take advantage of the fact that both lists should be
3836 // ordered in the same way.)
3837 int N = tokens.size();
3838 for (int i=0; i<N; i++) {
3839 IBinder token = tokens.get(i);
3840 final AppWindowToken wtoken = findAppWindowToken(token);
3841 if (!mAppTokens.remove(wtoken)) {
3842 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3843 + token + " (" + wtoken + ")");
3844 i--;
3845 N--;
3846 }
3847 }
3848 }
3849
Dianne Hackborna8f60182009-09-01 19:01:50 -07003850 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3851 boolean updateFocusAndLayout) {
3852 // First remove all of the windows from the list.
3853 tmpRemoveAppWindowsLocked(wtoken);
3854
3855 // Where to start adding?
3856 int pos = findWindowOffsetLocked(tokenPos);
3857
3858 // And now add them back at the correct place.
3859 pos = reAddAppWindowsLocked(pos, wtoken);
3860
3861 if (updateFocusAndLayout) {
3862 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3863 assignLayersLocked();
3864 }
3865 mLayoutNeeded = true;
3866 performLayoutAndPlaceSurfacesLocked();
3867 }
3868 }
3869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3871 // First remove all of the windows from the list.
3872 final int N = tokens.size();
3873 int i;
3874 for (i=0; i<N; i++) {
3875 WindowToken token = mTokenMap.get(tokens.get(i));
3876 if (token != null) {
3877 tmpRemoveAppWindowsLocked(token);
3878 }
3879 }
3880
3881 // Where to start adding?
3882 int pos = findWindowOffsetLocked(tokenPos);
3883
3884 // And now add them back at the correct place.
3885 for (i=0; i<N; i++) {
3886 WindowToken token = mTokenMap.get(tokens.get(i));
3887 if (token != null) {
3888 pos = reAddAppWindowsLocked(pos, token);
3889 }
3890 }
3891
Dianne Hackborna8f60182009-09-01 19:01:50 -07003892 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3893 assignLayersLocked();
3894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 mLayoutNeeded = true;
3896 performLayoutAndPlaceSurfacesLocked();
3897
3898 //dump();
3899 }
3900
3901 public void moveAppTokensToTop(List<IBinder> tokens) {
3902 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3903 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003904 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 }
3906
3907 final long origId = Binder.clearCallingIdentity();
3908 synchronized(mWindowMap) {
3909 removeAppTokensLocked(tokens);
3910 final int N = tokens.size();
3911 for (int i=0; i<N; i++) {
3912 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3913 if (wt != null) {
3914 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003915 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003916 mToTopApps.remove(wt);
3917 mToBottomApps.remove(wt);
3918 mToTopApps.add(wt);
3919 wt.sendingToBottom = false;
3920 wt.sendingToTop = true;
3921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
3923 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003924
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003925 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003926 moveAppWindowsLocked(tokens, mAppTokens.size());
3927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
3929 Binder.restoreCallingIdentity(origId);
3930 }
3931
3932 public void moveAppTokensToBottom(List<IBinder> tokens) {
3933 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3934 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003935 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937
3938 final long origId = Binder.clearCallingIdentity();
3939 synchronized(mWindowMap) {
3940 removeAppTokensLocked(tokens);
3941 final int N = tokens.size();
3942 int pos = 0;
3943 for (int i=0; i<N; i++) {
3944 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3945 if (wt != null) {
3946 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003947 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003948 mToTopApps.remove(wt);
3949 mToBottomApps.remove(wt);
3950 mToBottomApps.add(i, wt);
3951 wt.sendingToTop = false;
3952 wt.sendingToBottom = true;
3953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 pos++;
3955 }
3956 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003957
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003958 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003959 moveAppWindowsLocked(tokens, 0);
3960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 }
3962 Binder.restoreCallingIdentity(origId);
3963 }
3964
3965 // -------------------------------------------------------------
3966 // Misc IWindowSession methods
3967 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04003970 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 != PackageManager.PERMISSION_GRANTED) {
3972 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3973 }
Mike Lockwooddd884682009-10-11 16:57:08 -04003974 synchronized (mKeyguardDisabled) {
3975 mKeyguardDisabled.acquire(token, tag);
3976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 }
3978
3979 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04003980 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 != PackageManager.PERMISSION_GRANTED) {
3982 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3983 }
3984 synchronized (mKeyguardDisabled) {
3985 mKeyguardDisabled.release(token);
3986
3987 if (!mKeyguardDisabled.isAcquired()) {
3988 // if we are the last one to reenable the keyguard wait until
3989 // we have actaully finished reenabling until returning
3990 mWaitingUntilKeyguardReenabled = true;
3991 while (mWaitingUntilKeyguardReenabled) {
3992 try {
3993 mKeyguardDisabled.wait();
3994 } catch (InterruptedException e) {
3995 Thread.currentThread().interrupt();
3996 }
3997 }
3998 }
3999 }
4000 }
4001
4002 /**
4003 * @see android.app.KeyguardManager#exitKeyguardSecurely
4004 */
4005 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004006 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 != PackageManager.PERMISSION_GRANTED) {
4008 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4009 }
4010 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4011 public void onKeyguardExitResult(boolean success) {
4012 try {
4013 callback.onKeyguardExitResult(success);
4014 } catch (RemoteException e) {
4015 // Client has died, we don't care.
4016 }
4017 }
4018 });
4019 }
4020
4021 public boolean inKeyguardRestrictedInputMode() {
4022 return mPolicy.inKeyguardRestrictedKeyInputMode();
4023 }
Romain Guy06882f82009-06-10 13:36:04 -07004024
Dianne Hackbornffa42482009-09-23 22:20:11 -07004025 public void closeSystemDialogs(String reason) {
4026 synchronized(mWindowMap) {
4027 for (int i=mWindows.size()-1; i>=0; i--) {
4028 WindowState w = (WindowState)mWindows.get(i);
4029 if (w.mSurface != null) {
4030 try {
4031 w.mClient.closeSystemDialogs(reason);
4032 } catch (RemoteException e) {
4033 }
4034 }
4035 }
4036 }
4037 }
4038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 static float fixScale(float scale) {
4040 if (scale < 0) scale = 0;
4041 else if (scale > 20) scale = 20;
4042 return Math.abs(scale);
4043 }
Romain Guy06882f82009-06-10 13:36:04 -07004044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 public void setAnimationScale(int which, float scale) {
4046 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4047 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004048 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 }
4050
4051 if (scale < 0) scale = 0;
4052 else if (scale > 20) scale = 20;
4053 scale = Math.abs(scale);
4054 switch (which) {
4055 case 0: mWindowAnimationScale = fixScale(scale); break;
4056 case 1: mTransitionAnimationScale = fixScale(scale); break;
4057 }
Romain Guy06882f82009-06-10 13:36:04 -07004058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 // Persist setting
4060 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4061 }
Romain Guy06882f82009-06-10 13:36:04 -07004062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 public void setAnimationScales(float[] scales) {
4064 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4065 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004066 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 }
4068
4069 if (scales != null) {
4070 if (scales.length >= 1) {
4071 mWindowAnimationScale = fixScale(scales[0]);
4072 }
4073 if (scales.length >= 2) {
4074 mTransitionAnimationScale = fixScale(scales[1]);
4075 }
4076 }
Romain Guy06882f82009-06-10 13:36:04 -07004077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 // Persist setting
4079 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4080 }
Romain Guy06882f82009-06-10 13:36:04 -07004081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 public float getAnimationScale(int which) {
4083 switch (which) {
4084 case 0: return mWindowAnimationScale;
4085 case 1: return mTransitionAnimationScale;
4086 }
4087 return 0;
4088 }
Romain Guy06882f82009-06-10 13:36:04 -07004089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 public float[] getAnimationScales() {
4091 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4092 }
Romain Guy06882f82009-06-10 13:36:04 -07004093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 public int getSwitchState(int sw) {
4095 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4096 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004097 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 }
4099 return KeyInputQueue.getSwitchState(sw);
4100 }
Romain Guy06882f82009-06-10 13:36:04 -07004101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 public int getSwitchStateForDevice(int devid, int sw) {
4103 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4104 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004105 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107 return KeyInputQueue.getSwitchState(devid, sw);
4108 }
Romain Guy06882f82009-06-10 13:36:04 -07004109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 public int getScancodeState(int sw) {
4111 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4112 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004113 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004115 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 }
Romain Guy06882f82009-06-10 13:36:04 -07004117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 public int getScancodeStateForDevice(int devid, int sw) {
4119 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4120 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004121 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004123 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 }
Romain Guy06882f82009-06-10 13:36:04 -07004125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 public int getKeycodeState(int sw) {
4127 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4128 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004129 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004131 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
Romain Guy06882f82009-06-10 13:36:04 -07004133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 public int getKeycodeStateForDevice(int devid, int sw) {
4135 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4136 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004137 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004139 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 }
Romain Guy06882f82009-06-10 13:36:04 -07004141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4143 return KeyInputQueue.hasKeys(keycodes, keyExists);
4144 }
Romain Guy06882f82009-06-10 13:36:04 -07004145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 public void enableScreenAfterBoot() {
4147 synchronized(mWindowMap) {
4148 if (mSystemBooted) {
4149 return;
4150 }
4151 mSystemBooted = true;
4152 }
Romain Guy06882f82009-06-10 13:36:04 -07004153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 performEnableScreen();
4155 }
Romain Guy06882f82009-06-10 13:36:04 -07004156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 public void enableScreenIfNeededLocked() {
4158 if (mDisplayEnabled) {
4159 return;
4160 }
4161 if (!mSystemBooted) {
4162 return;
4163 }
4164 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4165 }
Romain Guy06882f82009-06-10 13:36:04 -07004166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 public void performEnableScreen() {
4168 synchronized(mWindowMap) {
4169 if (mDisplayEnabled) {
4170 return;
4171 }
4172 if (!mSystemBooted) {
4173 return;
4174 }
Romain Guy06882f82009-06-10 13:36:04 -07004175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 // Don't enable the screen until all existing windows
4177 // have been drawn.
4178 final int N = mWindows.size();
4179 for (int i=0; i<N; i++) {
4180 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004181 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 return;
4183 }
4184 }
Romain Guy06882f82009-06-10 13:36:04 -07004185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 mDisplayEnabled = true;
4187 if (false) {
4188 Log.i(TAG, "ENABLING SCREEN!");
4189 StringWriter sw = new StringWriter();
4190 PrintWriter pw = new PrintWriter(sw);
4191 this.dump(null, pw, null);
4192 Log.i(TAG, sw.toString());
4193 }
4194 try {
4195 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4196 if (surfaceFlinger != null) {
4197 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4198 Parcel data = Parcel.obtain();
4199 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4200 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4201 data, null, 0);
4202 data.recycle();
4203 }
4204 } catch (RemoteException ex) {
4205 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4206 }
4207 }
Romain Guy06882f82009-06-10 13:36:04 -07004208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004212 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4213 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 }
Romain Guy06882f82009-06-10 13:36:04 -07004215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 public void setInTouchMode(boolean mode) {
4217 synchronized(mWindowMap) {
4218 mInTouchMode = mode;
4219 }
4220 }
4221
Romain Guy06882f82009-06-10 13:36:04 -07004222 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004223 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004225 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004226 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
4228
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004229 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 }
Romain Guy06882f82009-06-10 13:36:04 -07004231
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004232 public void setRotationUnchecked(int rotation,
4233 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 if(DEBUG_ORIENTATION) Log.v(TAG,
4235 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 long origId = Binder.clearCallingIdentity();
4238 boolean changed;
4239 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004240 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 }
Romain Guy06882f82009-06-10 13:36:04 -07004242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 if (changed) {
4244 sendNewConfiguration();
4245 synchronized(mWindowMap) {
4246 mLayoutNeeded = true;
4247 performLayoutAndPlaceSurfacesLocked();
4248 }
4249 } else if (alwaysSendConfiguration) {
4250 //update configuration ignoring orientation change
4251 sendNewConfiguration();
4252 }
Romain Guy06882f82009-06-10 13:36:04 -07004253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 Binder.restoreCallingIdentity(origId);
4255 }
Romain Guy06882f82009-06-10 13:36:04 -07004256
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004257 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 boolean changed;
4259 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4260 rotation = mRequestedRotation;
4261 } else {
4262 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004263 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 }
4265 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004266 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 mRotation, mDisplayEnabled);
4268 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4269 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004272 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 "Rotation changed to " + rotation
4274 + " from " + mRotation
4275 + " (forceApp=" + mForcedAppOrientation
4276 + ", req=" + mRequestedRotation + ")");
4277 mRotation = rotation;
4278 mWindowsFreezingScreen = true;
4279 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4280 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4281 2000);
4282 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004283 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004284 mQueue.setOrientation(rotation);
4285 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004286 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 }
4288 for (int i=mWindows.size()-1; i>=0; i--) {
4289 WindowState w = (WindowState)mWindows.get(i);
4290 if (w.mSurface != null) {
4291 w.mOrientationChanging = true;
4292 }
4293 }
4294 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4295 try {
4296 mRotationWatchers.get(i).onRotationChanged(rotation);
4297 } catch (RemoteException e) {
4298 }
4299 }
4300 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 return changed;
4303 }
Romain Guy06882f82009-06-10 13:36:04 -07004304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 public int getRotation() {
4306 return mRotation;
4307 }
4308
4309 public int watchRotation(IRotationWatcher watcher) {
4310 final IBinder watcherBinder = watcher.asBinder();
4311 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4312 public void binderDied() {
4313 synchronized (mWindowMap) {
4314 for (int i=0; i<mRotationWatchers.size(); i++) {
4315 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004316 IRotationWatcher removed = mRotationWatchers.remove(i);
4317 if (removed != null) {
4318 removed.asBinder().unlinkToDeath(this, 0);
4319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 i--;
4321 }
4322 }
4323 }
4324 }
4325 };
Romain Guy06882f82009-06-10 13:36:04 -07004326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 synchronized (mWindowMap) {
4328 try {
4329 watcher.asBinder().linkToDeath(dr, 0);
4330 mRotationWatchers.add(watcher);
4331 } catch (RemoteException e) {
4332 // Client died, no cleanup needed.
4333 }
Romain Guy06882f82009-06-10 13:36:04 -07004334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 return mRotation;
4336 }
4337 }
4338
4339 /**
4340 * Starts the view server on the specified port.
4341 *
4342 * @param port The port to listener to.
4343 *
4344 * @return True if the server was successfully started, false otherwise.
4345 *
4346 * @see com.android.server.ViewServer
4347 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4348 */
4349 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004350 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 return false;
4352 }
4353
4354 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4355 return false;
4356 }
4357
4358 if (port < 1024) {
4359 return false;
4360 }
4361
4362 if (mViewServer != null) {
4363 if (!mViewServer.isRunning()) {
4364 try {
4365 return mViewServer.start();
4366 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004367 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 }
4369 }
4370 return false;
4371 }
4372
4373 try {
4374 mViewServer = new ViewServer(this, port);
4375 return mViewServer.start();
4376 } catch (IOException e) {
4377 Log.w(TAG, "View server did not start");
4378 }
4379 return false;
4380 }
4381
Romain Guy06882f82009-06-10 13:36:04 -07004382 private boolean isSystemSecure() {
4383 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4384 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4385 }
4386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 /**
4388 * Stops the view server if it exists.
4389 *
4390 * @return True if the server stopped, false if it wasn't started or
4391 * couldn't be stopped.
4392 *
4393 * @see com.android.server.ViewServer
4394 */
4395 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004396 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 return false;
4398 }
4399
4400 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4401 return false;
4402 }
4403
4404 if (mViewServer != null) {
4405 return mViewServer.stop();
4406 }
4407 return false;
4408 }
4409
4410 /**
4411 * Indicates whether the view server is running.
4412 *
4413 * @return True if the server is running, false otherwise.
4414 *
4415 * @see com.android.server.ViewServer
4416 */
4417 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004418 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 return false;
4420 }
4421
4422 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4423 return false;
4424 }
4425
4426 return mViewServer != null && mViewServer.isRunning();
4427 }
4428
4429 /**
4430 * Lists all availble windows in the system. The listing is written in the
4431 * specified Socket's output stream with the following syntax:
4432 * windowHashCodeInHexadecimal windowName
4433 * Each line of the ouput represents a different window.
4434 *
4435 * @param client The remote client to send the listing to.
4436 * @return False if an error occured, true otherwise.
4437 */
4438 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004439 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 return false;
4441 }
4442
4443 boolean result = true;
4444
4445 Object[] windows;
4446 synchronized (mWindowMap) {
4447 windows = new Object[mWindows.size()];
4448 //noinspection unchecked
4449 windows = mWindows.toArray(windows);
4450 }
4451
4452 BufferedWriter out = null;
4453
4454 // Any uncaught exception will crash the system process
4455 try {
4456 OutputStream clientStream = client.getOutputStream();
4457 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4458
4459 final int count = windows.length;
4460 for (int i = 0; i < count; i++) {
4461 final WindowState w = (WindowState) windows[i];
4462 out.write(Integer.toHexString(System.identityHashCode(w)));
4463 out.write(' ');
4464 out.append(w.mAttrs.getTitle());
4465 out.write('\n');
4466 }
4467
4468 out.write("DONE.\n");
4469 out.flush();
4470 } catch (Exception e) {
4471 result = false;
4472 } finally {
4473 if (out != null) {
4474 try {
4475 out.close();
4476 } catch (IOException e) {
4477 result = false;
4478 }
4479 }
4480 }
4481
4482 return result;
4483 }
4484
4485 /**
4486 * Sends a command to a target window. The result of the command, if any, will be
4487 * written in the output stream of the specified socket.
4488 *
4489 * The parameters must follow this syntax:
4490 * windowHashcode extra
4491 *
4492 * Where XX is the length in characeters of the windowTitle.
4493 *
4494 * The first parameter is the target window. The window with the specified hashcode
4495 * will be the target. If no target can be found, nothing happens. The extra parameters
4496 * will be delivered to the target window and as parameters to the command itself.
4497 *
4498 * @param client The remote client to sent the result, if any, to.
4499 * @param command The command to execute.
4500 * @param parameters The command parameters.
4501 *
4502 * @return True if the command was successfully delivered, false otherwise. This does
4503 * not indicate whether the command itself was successful.
4504 */
4505 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004506 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 return false;
4508 }
4509
4510 boolean success = true;
4511 Parcel data = null;
4512 Parcel reply = null;
4513
4514 // Any uncaught exception will crash the system process
4515 try {
4516 // Find the hashcode of the window
4517 int index = parameters.indexOf(' ');
4518 if (index == -1) {
4519 index = parameters.length();
4520 }
4521 final String code = parameters.substring(0, index);
4522 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4523
4524 // Extract the command's parameter after the window description
4525 if (index < parameters.length()) {
4526 parameters = parameters.substring(index + 1);
4527 } else {
4528 parameters = "";
4529 }
4530
4531 final WindowManagerService.WindowState window = findWindow(hashCode);
4532 if (window == null) {
4533 return false;
4534 }
4535
4536 data = Parcel.obtain();
4537 data.writeInterfaceToken("android.view.IWindow");
4538 data.writeString(command);
4539 data.writeString(parameters);
4540 data.writeInt(1);
4541 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4542
4543 reply = Parcel.obtain();
4544
4545 final IBinder binder = window.mClient.asBinder();
4546 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4547 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4548
4549 reply.readException();
4550
4551 } catch (Exception e) {
4552 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4553 success = false;
4554 } finally {
4555 if (data != null) {
4556 data.recycle();
4557 }
4558 if (reply != null) {
4559 reply.recycle();
4560 }
4561 }
4562
4563 return success;
4564 }
4565
4566 private WindowState findWindow(int hashCode) {
4567 if (hashCode == -1) {
4568 return getFocusedWindow();
4569 }
4570
4571 synchronized (mWindowMap) {
4572 final ArrayList windows = mWindows;
4573 final int count = windows.size();
4574
4575 for (int i = 0; i < count; i++) {
4576 WindowState w = (WindowState) windows.get(i);
4577 if (System.identityHashCode(w) == hashCode) {
4578 return w;
4579 }
4580 }
4581 }
4582
4583 return null;
4584 }
4585
4586 /*
4587 * Instruct the Activity Manager to fetch the current configuration and broadcast
4588 * that to config-changed listeners if appropriate.
4589 */
4590 void sendNewConfiguration() {
4591 try {
4592 mActivityManager.updateConfiguration(null);
4593 } catch (RemoteException e) {
4594 }
4595 }
Romain Guy06882f82009-06-10 13:36:04 -07004596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 public Configuration computeNewConfiguration() {
4598 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004599 return computeNewConfigurationLocked();
4600 }
4601 }
Romain Guy06882f82009-06-10 13:36:04 -07004602
Dianne Hackbornc485a602009-03-24 22:39:49 -07004603 Configuration computeNewConfigurationLocked() {
4604 Configuration config = new Configuration();
4605 if (!computeNewConfigurationLocked(config)) {
4606 return null;
4607 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004608 return config;
4609 }
Romain Guy06882f82009-06-10 13:36:04 -07004610
Dianne Hackbornc485a602009-03-24 22:39:49 -07004611 boolean computeNewConfigurationLocked(Configuration config) {
4612 if (mDisplay == null) {
4613 return false;
4614 }
4615 mQueue.getInputConfiguration(config);
4616 final int dw = mDisplay.getWidth();
4617 final int dh = mDisplay.getHeight();
4618 int orientation = Configuration.ORIENTATION_SQUARE;
4619 if (dw < dh) {
4620 orientation = Configuration.ORIENTATION_PORTRAIT;
4621 } else if (dw > dh) {
4622 orientation = Configuration.ORIENTATION_LANDSCAPE;
4623 }
4624 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004625
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004626 DisplayMetrics dm = new DisplayMetrics();
4627 mDisplay.getMetrics(dm);
4628 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4629
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004630 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004631 // Note we only do this once because at this point we don't
4632 // expect the screen to change in this way at runtime, and want
4633 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004634 int longSize = dw;
4635 int shortSize = dh;
4636 if (longSize < shortSize) {
4637 int tmp = longSize;
4638 longSize = shortSize;
4639 shortSize = tmp;
4640 }
4641 longSize = (int)(longSize/dm.density);
4642 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004643
Dianne Hackborn723738c2009-06-25 19:48:04 -07004644 // These semi-magic numbers define our compatibility modes for
4645 // applications with different screens. Don't change unless you
4646 // make sure to test lots and lots of apps!
4647 if (longSize < 470) {
4648 // This is shorter than an HVGA normal density screen (which
4649 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004650 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4651 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004652 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004653 // Is this a large screen?
4654 if (longSize > 640 && shortSize >= 480) {
4655 // VGA or larger screens at medium density are the point
4656 // at which we consider it to be a large screen.
4657 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4658 } else {
4659 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4660
4661 // If this screen is wider than normal HVGA, or taller
4662 // than FWVGA, then for old apps we want to run in size
4663 // compatibility mode.
4664 if (shortSize > 321 || longSize > 570) {
4665 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4666 }
4667 }
4668
4669 // Is this a long screen?
4670 if (((longSize*3)/5) >= (shortSize-1)) {
4671 // Anything wider than WVGA (5:3) is considering to be long.
4672 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4673 } else {
4674 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4675 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004676 }
4677 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004678 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004679
Dianne Hackbornc485a602009-03-24 22:39:49 -07004680 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4681 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4682 mPolicy.adjustConfigurationLw(config);
4683 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 }
Romain Guy06882f82009-06-10 13:36:04 -07004685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 // -------------------------------------------------------------
4687 // Input Events and Focus Management
4688 // -------------------------------------------------------------
4689
4690 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004691 long curTime = SystemClock.uptimeMillis();
4692
Michael Chane10de972009-05-18 11:24:50 -07004693 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004694 if (mLastTouchEventType == eventType &&
4695 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4696 return;
4697 }
4698 mLastUserActivityCallTime = curTime;
4699 mLastTouchEventType = eventType;
4700 }
4701
4702 if (targetWin == null
4703 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4704 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 }
4706 }
4707
4708 // tells if it's a cheek event or not -- this function is stateful
4709 private static final int EVENT_NONE = 0;
4710 private static final int EVENT_UNKNOWN = 0;
4711 private static final int EVENT_CHEEK = 0;
4712 private static final int EVENT_IGNORE_DURATION = 300; // ms
4713 private static final float CHEEK_THRESHOLD = 0.6f;
4714 private int mEventState = EVENT_NONE;
4715 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 private int eventType(MotionEvent ev) {
4718 float size = ev.getSize();
4719 switch (ev.getAction()) {
4720 case MotionEvent.ACTION_DOWN:
4721 mEventSize = size;
4722 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4723 case MotionEvent.ACTION_UP:
4724 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004725 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 case MotionEvent.ACTION_MOVE:
4727 final int N = ev.getHistorySize();
4728 if (size > mEventSize) mEventSize = size;
4729 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4730 for (int i=0; i<N; i++) {
4731 size = ev.getHistoricalSize(i);
4732 if (size > mEventSize) mEventSize = size;
4733 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4734 }
4735 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4736 return TOUCH_EVENT;
4737 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004738 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 }
4740 default:
4741 // not good
4742 return OTHER_EVENT;
4743 }
4744 }
4745
4746 /**
4747 * @return Returns true if event was dispatched, false if it was dropped for any reason
4748 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004749 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4751 "dispatchPointer " + ev);
4752
Michael Chan53071d62009-05-13 17:29:48 -07004753 if (MEASURE_LATENCY) {
4754 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4755 }
4756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004758 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759
Michael Chan53071d62009-05-13 17:29:48 -07004760 if (MEASURE_LATENCY) {
4761 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4762 }
4763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 if (action == MotionEvent.ACTION_UP) {
4767 // let go of our target
4768 mKeyWaiter.mMotionTarget = null;
4769 mPowerManager.logPointerUpEvent();
4770 } else if (action == MotionEvent.ACTION_DOWN) {
4771 mPowerManager.logPointerDownEvent();
4772 }
4773
4774 if (targetObj == null) {
4775 // In this case we are either dropping the event, or have received
4776 // a move or up without a down. It is common to receive move
4777 // events in such a way, since this means the user is moving the
4778 // pointer without actually pressing down. All other cases should
4779 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004780 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4782 }
4783 if (qev != null) {
4784 mQueue.recycleEvent(qev);
4785 }
4786 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004787 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 }
4789 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4790 if (qev != null) {
4791 mQueue.recycleEvent(qev);
4792 }
4793 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004794 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 }
Romain Guy06882f82009-06-10 13:36:04 -07004796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004800 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801
4802 //Log.i(TAG, "Sending " + ev + " to " + target);
4803
4804 if (uid != 0 && uid != target.mSession.mUid) {
4805 if (mContext.checkPermission(
4806 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4807 != PackageManager.PERMISSION_GRANTED) {
4808 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4809 + pid + " uid " + uid + " to window " + target
4810 + " owned by uid " + target.mSession.mUid);
4811 if (qev != null) {
4812 mQueue.recycleEvent(qev);
4813 }
4814 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004815 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 }
4817 }
4818
Michael Chan53071d62009-05-13 17:29:48 -07004819 if (MEASURE_LATENCY) {
4820 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4821 }
4822
Romain Guy06882f82009-06-10 13:36:04 -07004823 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4825 //target wants to ignore fat touch events
4826 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4827 //explicit flag to return without processing event further
4828 boolean returnFlag = false;
4829 if((action == MotionEvent.ACTION_DOWN)) {
4830 mFatTouch = false;
4831 if(cheekPress) {
4832 mFatTouch = true;
4833 returnFlag = true;
4834 }
4835 } else {
4836 if(action == MotionEvent.ACTION_UP) {
4837 if(mFatTouch) {
4838 //earlier even was invalid doesnt matter if current up is cheekpress or not
4839 mFatTouch = false;
4840 returnFlag = true;
4841 } else if(cheekPress) {
4842 //cancel the earlier event
4843 ev.setAction(MotionEvent.ACTION_CANCEL);
4844 action = MotionEvent.ACTION_CANCEL;
4845 }
4846 } else if(action == MotionEvent.ACTION_MOVE) {
4847 if(mFatTouch) {
4848 //two cases here
4849 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004850 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 returnFlag = true;
4852 } else if(cheekPress) {
4853 //valid down followed by invalid moves
4854 //an invalid move have to cancel earlier action
4855 ev.setAction(MotionEvent.ACTION_CANCEL);
4856 action = MotionEvent.ACTION_CANCEL;
4857 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4858 //note that the subsequent invalid moves will not get here
4859 mFatTouch = true;
4860 }
4861 }
4862 } //else if action
4863 if(returnFlag) {
4864 //recycle que, ev
4865 if (qev != null) {
4866 mQueue.recycleEvent(qev);
4867 }
4868 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004869 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 }
4871 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004872
Michael Chan9f028e62009-08-04 17:37:46 -07004873 // Enable this for testing the "right" value
4874 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004875 int max_events_per_sec = 35;
4876 try {
4877 max_events_per_sec = Integer.parseInt(SystemProperties
4878 .get("windowsmgr.max_events_per_sec"));
4879 if (max_events_per_sec < 1) {
4880 max_events_per_sec = 35;
4881 }
4882 } catch (NumberFormatException e) {
4883 }
4884 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4885 }
4886
4887 /*
4888 * Throttle events to minimize CPU usage when there's a flood of events
4889 * e.g. constant contact with the screen
4890 */
4891 if (action == MotionEvent.ACTION_MOVE) {
4892 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4893 long now = SystemClock.uptimeMillis();
4894 if (now < nextEventTime) {
4895 try {
4896 Thread.sleep(nextEventTime - now);
4897 } catch (InterruptedException e) {
4898 }
4899 mLastTouchEventTime = nextEventTime;
4900 } else {
4901 mLastTouchEventTime = now;
4902 }
4903 }
4904
Michael Chan53071d62009-05-13 17:29:48 -07004905 if (MEASURE_LATENCY) {
4906 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4907 }
4908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 synchronized(mWindowMap) {
4910 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4911 mKeyWaiter.bindTargetWindowLocked(target,
4912 KeyWaiter.RETURN_PENDING_POINTER, qev);
4913 ev = null;
4914 } else {
4915 if (action == MotionEvent.ACTION_DOWN) {
4916 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4917 if (out != null) {
4918 MotionEvent oev = MotionEvent.obtain(ev);
4919 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4920 do {
4921 final Rect frame = out.mFrame;
4922 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4923 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004924 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004925 } catch (android.os.RemoteException e) {
4926 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4927 }
4928 oev.offsetLocation((float)frame.left, (float)frame.top);
4929 out = out.mNextOutsideTouch;
4930 } while (out != null);
4931 mKeyWaiter.mOutsideTouchTargets = null;
4932 }
4933 }
4934 final Rect frame = target.mFrame;
4935 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4936 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004937
4938 // If we are on top of the wallpaper, then the wallpaper also
4939 // gets to see this movement.
4940 if (mWallpaperTarget == target) {
4941 sendPointerToWallpaperLocked(target, ev, eventTime);
4942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 }
4944 }
Romain Guy06882f82009-06-10 13:36:04 -07004945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004946 // finally offset the event to the target's coordinate system and
4947 // dispatch the event.
4948 try {
4949 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4950 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4951 }
Michael Chan53071d62009-05-13 17:29:48 -07004952
4953 if (MEASURE_LATENCY) {
4954 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4955 }
4956
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004957 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004958
4959 if (MEASURE_LATENCY) {
4960 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4961 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004962 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 } catch (android.os.RemoteException e) {
4964 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4965 mKeyWaiter.mMotionTarget = null;
4966 try {
4967 removeWindow(target.mSession, target.mClient);
4968 } catch (java.util.NoSuchElementException ex) {
4969 // This will happen if the window has already been
4970 // removed.
4971 }
4972 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004973 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 }
Romain Guy06882f82009-06-10 13:36:04 -07004975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 /**
4977 * @return Returns true if event was dispatched, false if it was dropped for any reason
4978 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004979 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 if (DEBUG_INPUT) Log.v(
4981 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004984 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 if (focusObj == null) {
4986 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4987 if (qev != null) {
4988 mQueue.recycleEvent(qev);
4989 }
4990 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004991 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 }
4993 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4994 if (qev != null) {
4995 mQueue.recycleEvent(qev);
4996 }
4997 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004998 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 }
Romain Guy06882f82009-06-10 13:36:04 -07005000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003 if (uid != 0 && uid != focus.mSession.mUid) {
5004 if (mContext.checkPermission(
5005 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5006 != PackageManager.PERMISSION_GRANTED) {
5007 Log.w(TAG, "Permission denied: injecting key event from pid "
5008 + pid + " uid " + uid + " to window " + focus
5009 + " owned by uid " + focus.mSession.mUid);
5010 if (qev != null) {
5011 mQueue.recycleEvent(qev);
5012 }
5013 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005014 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 }
5016 }
Romain Guy06882f82009-06-10 13:36:04 -07005017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 synchronized(mWindowMap) {
5021 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5022 mKeyWaiter.bindTargetWindowLocked(focus,
5023 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5024 // We don't deliver movement events to the client, we hold
5025 // them and wait for them to call back.
5026 ev = null;
5027 } else {
5028 mKeyWaiter.bindTargetWindowLocked(focus);
5029 }
5030 }
Romain Guy06882f82009-06-10 13:36:04 -07005031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005032 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005033 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005034 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 } catch (android.os.RemoteException e) {
5036 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5037 try {
5038 removeWindow(focus.mSession, focus.mClient);
5039 } catch (java.util.NoSuchElementException ex) {
5040 // This will happen if the window has already been
5041 // removed.
5042 }
5043 }
Romain Guy06882f82009-06-10 13:36:04 -07005044
Dianne Hackborncfaef692009-06-15 14:24:44 -07005045 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005046 }
Romain Guy06882f82009-06-10 13:36:04 -07005047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 /**
5049 * @return Returns true if event was dispatched, false if it was dropped for any reason
5050 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005051 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5053
5054 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005055 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 if (focusObj == null) {
5057 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005058 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 }
5060 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005061 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 }
Romain Guy06882f82009-06-10 13:36:04 -07005063
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005064 // Okay we have finished waiting for the last event to be processed.
5065 // First off, if this is a repeat event, check to see if there is
5066 // a corresponding up event in the queue. If there is, we will
5067 // just drop the repeat, because it makes no sense to repeat after
5068 // the user has released a key. (This is especially important for
5069 // long presses.)
5070 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5071 return INJECT_SUCCEEDED;
5072 }
5073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 if (DEBUG_INPUT) Log.v(
5077 TAG, "Dispatching to " + focus + ": " + event);
5078
5079 if (uid != 0 && uid != focus.mSession.mUid) {
5080 if (mContext.checkPermission(
5081 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5082 != PackageManager.PERMISSION_GRANTED) {
5083 Log.w(TAG, "Permission denied: injecting key event from pid "
5084 + pid + " uid " + uid + " to window " + focus
5085 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005086 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 }
5088 }
Romain Guy06882f82009-06-10 13:36:04 -07005089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 synchronized(mWindowMap) {
5091 mKeyWaiter.bindTargetWindowLocked(focus);
5092 }
5093
5094 // NOSHIP extra state logging
5095 mKeyWaiter.recordDispatchState(event, focus);
5096 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 try {
5099 if (DEBUG_INPUT || DEBUG_FOCUS) {
5100 Log.v(TAG, "Delivering key " + event.getKeyCode()
5101 + " to " + focus);
5102 }
5103 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005104 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 } catch (android.os.RemoteException e) {
5106 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5107 try {
5108 removeWindow(focus.mSession, focus.mClient);
5109 } catch (java.util.NoSuchElementException ex) {
5110 // This will happen if the window has already been
5111 // removed.
5112 }
5113 }
Romain Guy06882f82009-06-10 13:36:04 -07005114
Dianne Hackborncfaef692009-06-15 14:24:44 -07005115 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 }
Romain Guy06882f82009-06-10 13:36:04 -07005117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 public void pauseKeyDispatching(IBinder _token) {
5119 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5120 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005121 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122 }
5123
5124 synchronized (mWindowMap) {
5125 WindowToken token = mTokenMap.get(_token);
5126 if (token != null) {
5127 mKeyWaiter.pauseDispatchingLocked(token);
5128 }
5129 }
5130 }
5131
5132 public void resumeKeyDispatching(IBinder _token) {
5133 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5134 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005135 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 }
5137
5138 synchronized (mWindowMap) {
5139 WindowToken token = mTokenMap.get(_token);
5140 if (token != null) {
5141 mKeyWaiter.resumeDispatchingLocked(token);
5142 }
5143 }
5144 }
5145
5146 public void setEventDispatching(boolean enabled) {
5147 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5148 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005149 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 }
5151
5152 synchronized (mWindowMap) {
5153 mKeyWaiter.setEventDispatchingLocked(enabled);
5154 }
5155 }
Romain Guy06882f82009-06-10 13:36:04 -07005156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 /**
5158 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005159 *
5160 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005161 * {@link SystemClock#uptimeMillis()} as the timebase.)
5162 * @param sync If true, wait for the event to be completed before returning to the caller.
5163 * @return Returns true if event was dispatched, false if it was dropped for any reason
5164 */
5165 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5166 long downTime = ev.getDownTime();
5167 long eventTime = ev.getEventTime();
5168
5169 int action = ev.getAction();
5170 int code = ev.getKeyCode();
5171 int repeatCount = ev.getRepeatCount();
5172 int metaState = ev.getMetaState();
5173 int deviceId = ev.getDeviceId();
5174 int scancode = ev.getScanCode();
5175
5176 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5177 if (downTime == 0) downTime = eventTime;
5178
5179 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005180 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005182 final int pid = Binder.getCallingPid();
5183 final int uid = Binder.getCallingUid();
5184 final long ident = Binder.clearCallingIdentity();
5185 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005187 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005189 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005190 switch (result) {
5191 case INJECT_NO_PERMISSION:
5192 throw new SecurityException(
5193 "Injecting to another application requires INJECT_EVENT permission");
5194 case INJECT_SUCCEEDED:
5195 return true;
5196 }
5197 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 }
5199
5200 /**
5201 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005202 *
5203 * @param ev A motion event describing the pointer (touch) action. (As noted in
5204 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 * {@link SystemClock#uptimeMillis()} as the timebase.)
5206 * @param sync If true, wait for the event to be completed before returning to the caller.
5207 * @return Returns true if event was dispatched, false if it was dropped for any reason
5208 */
5209 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005210 final int pid = Binder.getCallingPid();
5211 final int uid = Binder.getCallingUid();
5212 final long ident = Binder.clearCallingIdentity();
5213 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005215 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005217 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005218 switch (result) {
5219 case INJECT_NO_PERMISSION:
5220 throw new SecurityException(
5221 "Injecting to another application requires INJECT_EVENT permission");
5222 case INJECT_SUCCEEDED:
5223 return true;
5224 }
5225 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 }
Romain Guy06882f82009-06-10 13:36:04 -07005227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 /**
5229 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005230 *
5231 * @param ev A motion event describing the trackball action. (As noted in
5232 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 * {@link SystemClock#uptimeMillis()} as the timebase.)
5234 * @param sync If true, wait for the event to be completed before returning to the caller.
5235 * @return Returns true if event was dispatched, false if it was dropped for any reason
5236 */
5237 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005238 final int pid = Binder.getCallingPid();
5239 final int uid = Binder.getCallingUid();
5240 final long ident = Binder.clearCallingIdentity();
5241 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005243 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005245 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005246 switch (result) {
5247 case INJECT_NO_PERMISSION:
5248 throw new SecurityException(
5249 "Injecting to another application requires INJECT_EVENT permission");
5250 case INJECT_SUCCEEDED:
5251 return true;
5252 }
5253 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 }
Romain Guy06882f82009-06-10 13:36:04 -07005255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 private WindowState getFocusedWindow() {
5257 synchronized (mWindowMap) {
5258 return getFocusedWindowLocked();
5259 }
5260 }
5261
5262 private WindowState getFocusedWindowLocked() {
5263 return mCurrentFocus;
5264 }
Romain Guy06882f82009-06-10 13:36:04 -07005265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 /**
5267 * This class holds the state for dispatching key events. This state
5268 * is protected by the KeyWaiter instance, NOT by the window lock. You
5269 * can be holding the main window lock while acquire the KeyWaiter lock,
5270 * but not the other way around.
5271 */
5272 final class KeyWaiter {
5273 // NOSHIP debugging
5274 public class DispatchState {
5275 private KeyEvent event;
5276 private WindowState focus;
5277 private long time;
5278 private WindowState lastWin;
5279 private IBinder lastBinder;
5280 private boolean finished;
5281 private boolean gotFirstWindow;
5282 private boolean eventDispatching;
5283 private long timeToSwitch;
5284 private boolean wasFrozen;
5285 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005286 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5289 focus = theFocus;
5290 event = theEvent;
5291 time = System.currentTimeMillis();
5292 // snapshot KeyWaiter state
5293 lastWin = mLastWin;
5294 lastBinder = mLastBinder;
5295 finished = mFinished;
5296 gotFirstWindow = mGotFirstWindow;
5297 eventDispatching = mEventDispatching;
5298 timeToSwitch = mTimeToSwitch;
5299 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005300 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 // cache the paused state at ctor time as well
5302 if (theFocus == null || theFocus.mToken == null) {
5303 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5304 focusPaused = false;
5305 } else {
5306 focusPaused = theFocus.mToken.paused;
5307 }
5308 }
Romain Guy06882f82009-06-10 13:36:04 -07005309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 public String toString() {
5311 return "{{" + event + " to " + focus + " @ " + time
5312 + " lw=" + lastWin + " lb=" + lastBinder
5313 + " fin=" + finished + " gfw=" + gotFirstWindow
5314 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005315 + " wf=" + wasFrozen + " fp=" + focusPaused
5316 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 }
5318 };
5319 private DispatchState mDispatchState = null;
5320 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5321 mDispatchState = new DispatchState(theEvent, theFocus);
5322 }
5323 // END NOSHIP
5324
5325 public static final int RETURN_NOTHING = 0;
5326 public static final int RETURN_PENDING_POINTER = 1;
5327 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005329 final Object SKIP_TARGET_TOKEN = new Object();
5330 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 private WindowState mLastWin = null;
5333 private IBinder mLastBinder = null;
5334 private boolean mFinished = true;
5335 private boolean mGotFirstWindow = false;
5336 private boolean mEventDispatching = true;
5337 private long mTimeToSwitch = 0;
5338 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 // Target of Motion events
5341 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 // Windows above the target who would like to receive an "outside"
5344 // touch event for any down events outside of them.
5345 WindowState mOutsideTouchTargets;
5346
5347 /**
5348 * Wait for the last event dispatch to complete, then find the next
5349 * target that should receive the given event and wait for that one
5350 * to be ready to receive it.
5351 */
5352 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5353 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005354 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 long startTime = SystemClock.uptimeMillis();
5356 long keyDispatchingTimeout = 5 * 1000;
5357 long waitedFor = 0;
5358
5359 while (true) {
5360 // Figure out which window we care about. It is either the
5361 // last window we are waiting to have process the event or,
5362 // if none, then the next window we think the event should go
5363 // to. Note: we retrieve mLastWin outside of the lock, so
5364 // it may change before we lock. Thus we must check it again.
5365 WindowState targetWin = mLastWin;
5366 boolean targetIsNew = targetWin == null;
5367 if (DEBUG_INPUT) Log.v(
5368 TAG, "waitForLastKey: mFinished=" + mFinished +
5369 ", mLastWin=" + mLastWin);
5370 if (targetIsNew) {
5371 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005372 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 if (target == SKIP_TARGET_TOKEN) {
5374 // The user has pressed a special key, and we are
5375 // dropping all pending events before it.
5376 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5377 + " " + nextMotion);
5378 return null;
5379 }
5380 if (target == CONSUMED_EVENT_TOKEN) {
5381 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5382 + " " + nextMotion);
5383 return target;
5384 }
5385 targetWin = (WindowState)target;
5386 }
Romain Guy06882f82009-06-10 13:36:04 -07005387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 // Now: is it okay to send the next event to this window?
5391 synchronized (this) {
5392 // First: did we come here based on the last window not
5393 // being null, but it changed by the time we got here?
5394 // If so, try again.
5395 if (!targetIsNew && mLastWin == null) {
5396 continue;
5397 }
Romain Guy06882f82009-06-10 13:36:04 -07005398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 // We never dispatch events if not finished with the
5400 // last one, or the display is frozen.
5401 if (mFinished && !mDisplayFrozen) {
5402 // If event dispatching is disabled, then we
5403 // just consume the events.
5404 if (!mEventDispatching) {
5405 if (DEBUG_INPUT) Log.v(TAG,
5406 "Skipping event; dispatching disabled: "
5407 + nextKey + " " + nextMotion);
5408 return null;
5409 }
5410 if (targetWin != null) {
5411 // If this is a new target, and that target is not
5412 // paused or unresponsive, then all looks good to
5413 // handle the event.
5414 if (targetIsNew && !targetWin.mToken.paused) {
5415 return targetWin;
5416 }
Romain Guy06882f82009-06-10 13:36:04 -07005417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 // If we didn't find a target window, and there is no
5419 // focused app window, then just eat the events.
5420 } else if (mFocusedApp == null) {
5421 if (DEBUG_INPUT) Log.v(TAG,
5422 "Skipping event; no focused app: "
5423 + nextKey + " " + nextMotion);
5424 return null;
5425 }
5426 }
Romain Guy06882f82009-06-10 13:36:04 -07005427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 if (DEBUG_INPUT) Log.v(
5429 TAG, "Waiting for last key in " + mLastBinder
5430 + " target=" + targetWin
5431 + " mFinished=" + mFinished
5432 + " mDisplayFrozen=" + mDisplayFrozen
5433 + " targetIsNew=" + targetIsNew
5434 + " paused="
5435 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005436 + " mFocusedApp=" + mFocusedApp
5437 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 targetApp = targetWin != null
5440 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 long curTimeout = keyDispatchingTimeout;
5443 if (mTimeToSwitch != 0) {
5444 long now = SystemClock.uptimeMillis();
5445 if (mTimeToSwitch <= now) {
5446 // If an app switch key has been pressed, and we have
5447 // waited too long for the current app to finish
5448 // processing keys, then wait no more!
5449 doFinishedKeyLocked(true);
5450 continue;
5451 }
5452 long switchTimeout = mTimeToSwitch - now;
5453 if (curTimeout > switchTimeout) {
5454 curTimeout = switchTimeout;
5455 }
5456 }
Romain Guy06882f82009-06-10 13:36:04 -07005457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 try {
5459 // after that continue
5460 // processing keys, so we don't get stuck.
5461 if (DEBUG_INPUT) Log.v(
5462 TAG, "Waiting for key dispatch: " + curTimeout);
5463 wait(curTimeout);
5464 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5465 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005466 + startTime + " switchTime=" + mTimeToSwitch
5467 + " target=" + targetWin + " mLW=" + mLastWin
5468 + " mLB=" + mLastBinder + " fin=" + mFinished
5469 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 } catch (InterruptedException e) {
5471 }
5472 }
5473
5474 // If we were frozen during configuration change, restart the
5475 // timeout checks from now; otherwise look at whether we timed
5476 // out before awakening.
5477 if (mWasFrozen) {
5478 waitedFor = 0;
5479 mWasFrozen = false;
5480 } else {
5481 waitedFor = SystemClock.uptimeMillis() - startTime;
5482 }
5483
5484 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5485 IApplicationToken at = null;
5486 synchronized (this) {
5487 Log.w(TAG, "Key dispatching timed out sending to " +
5488 (targetWin != null ? targetWin.mAttrs.getTitle()
5489 : "<null>"));
5490 // NOSHIP debugging
5491 Log.w(TAG, "Dispatch state: " + mDispatchState);
5492 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5493 // END NOSHIP
5494 //dump();
5495 if (targetWin != null) {
5496 at = targetWin.getAppToken();
5497 } else if (targetApp != null) {
5498 at = targetApp.appToken;
5499 }
5500 }
5501
5502 boolean abort = true;
5503 if (at != null) {
5504 try {
5505 long timeout = at.getKeyDispatchingTimeout();
5506 if (timeout > waitedFor) {
5507 // we did not wait the proper amount of time for this application.
5508 // set the timeout to be the real timeout and wait again.
5509 keyDispatchingTimeout = timeout - waitedFor;
5510 continue;
5511 } else {
5512 abort = at.keyDispatchingTimedOut();
5513 }
5514 } catch (RemoteException ex) {
5515 }
5516 }
5517
5518 synchronized (this) {
5519 if (abort && (mLastWin == targetWin || targetWin == null)) {
5520 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005521 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 if (DEBUG_INPUT) Log.v(TAG,
5523 "Window " + mLastWin +
5524 " timed out on key input");
5525 if (mLastWin.mToken.paused) {
5526 Log.w(TAG, "Un-pausing dispatching to this window");
5527 mLastWin.mToken.paused = false;
5528 }
5529 }
5530 if (mMotionTarget == targetWin) {
5531 mMotionTarget = null;
5532 }
5533 mLastWin = null;
5534 mLastBinder = null;
5535 if (failIfTimeout || targetWin == null) {
5536 return null;
5537 }
5538 } else {
5539 Log.w(TAG, "Continuing to wait for key to be dispatched");
5540 startTime = SystemClock.uptimeMillis();
5541 }
5542 }
5543 }
5544 }
5545 }
Romain Guy06882f82009-06-10 13:36:04 -07005546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005548 MotionEvent nextMotion, boolean isPointerEvent,
5549 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 if (nextKey != null) {
5553 // Find the target window for a normal key event.
5554 final int keycode = nextKey.getKeyCode();
5555 final int repeatCount = nextKey.getRepeatCount();
5556 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5557 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005560 if (callingUid == 0 ||
5561 mContext.checkPermission(
5562 android.Manifest.permission.INJECT_EVENTS,
5563 callingPid, callingUid)
5564 == PackageManager.PERMISSION_GRANTED) {
5565 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005566 nextKey.getMetaState(), down, repeatCount,
5567 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 Log.w(TAG, "Event timeout during app switch: dropping "
5570 + nextKey);
5571 return SKIP_TARGET_TOKEN;
5572 }
Romain Guy06882f82009-06-10 13:36:04 -07005573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005576 WindowState focus = null;
5577 synchronized(mWindowMap) {
5578 focus = getFocusedWindowLocked();
5579 }
Romain Guy06882f82009-06-10 13:36:04 -07005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005582
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005583 if (callingUid == 0 ||
5584 (focus != null && callingUid == focus.mSession.mUid) ||
5585 mContext.checkPermission(
5586 android.Manifest.permission.INJECT_EVENTS,
5587 callingPid, callingUid)
5588 == PackageManager.PERMISSION_GRANTED) {
5589 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005590 keycode, nextKey.getMetaState(), down, repeatCount,
5591 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005592 return CONSUMED_EVENT_TOKEN;
5593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 }
Romain Guy06882f82009-06-10 13:36:04 -07005595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 } else if (!isPointerEvent) {
5599 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5600 if (!dispatch) {
5601 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5602 + nextMotion);
5603 return SKIP_TARGET_TOKEN;
5604 }
Romain Guy06882f82009-06-10 13:36:04 -07005605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 WindowState focus = null;
5607 synchronized(mWindowMap) {
5608 focus = getFocusedWindowLocked();
5609 }
Romain Guy06882f82009-06-10 13:36:04 -07005610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5612 return focus;
5613 }
Romain Guy06882f82009-06-10 13:36:04 -07005614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 if (nextMotion == null) {
5616 return SKIP_TARGET_TOKEN;
5617 }
Romain Guy06882f82009-06-10 13:36:04 -07005618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5620 KeyEvent.KEYCODE_UNKNOWN);
5621 if (!dispatch) {
5622 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5623 + nextMotion);
5624 return SKIP_TARGET_TOKEN;
5625 }
Romain Guy06882f82009-06-10 13:36:04 -07005626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 // Find the target window for a pointer event.
5628 int action = nextMotion.getAction();
5629 final float xf = nextMotion.getX();
5630 final float yf = nextMotion.getY();
5631 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 final boolean screenWasOff = qev != null
5634 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 synchronized(mWindowMap) {
5639 synchronized (this) {
5640 if (action == MotionEvent.ACTION_DOWN) {
5641 if (mMotionTarget != null) {
5642 // this is weird, we got a pen down, but we thought it was
5643 // already down!
5644 // XXX: We should probably send an ACTION_UP to the current
5645 // target.
5646 Log.w(TAG, "Pointer down received while already down in: "
5647 + mMotionTarget);
5648 mMotionTarget = null;
5649 }
Romain Guy06882f82009-06-10 13:36:04 -07005650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 // ACTION_DOWN is special, because we need to lock next events to
5652 // the window we'll land onto.
5653 final int x = (int)xf;
5654 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 final ArrayList windows = mWindows;
5657 final int N = windows.size();
5658 WindowState topErrWindow = null;
5659 final Rect tmpRect = mTempRect;
5660 for (int i=N-1; i>=0; i--) {
5661 WindowState child = (WindowState)windows.get(i);
5662 //Log.i(TAG, "Checking dispatch to: " + child);
5663 final int flags = child.mAttrs.flags;
5664 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5665 if (topErrWindow == null) {
5666 topErrWindow = child;
5667 }
5668 }
5669 if (!child.isVisibleLw()) {
5670 //Log.i(TAG, "Not visible!");
5671 continue;
5672 }
5673 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5674 //Log.i(TAG, "Not touchable!");
5675 if ((flags & WindowManager.LayoutParams
5676 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5677 child.mNextOutsideTouch = mOutsideTouchTargets;
5678 mOutsideTouchTargets = child;
5679 }
5680 continue;
5681 }
5682 tmpRect.set(child.mFrame);
5683 if (child.mTouchableInsets == ViewTreeObserver
5684 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5685 // The touch is inside of the window if it is
5686 // inside the frame, AND the content part of that
5687 // frame that was given by the application.
5688 tmpRect.left += child.mGivenContentInsets.left;
5689 tmpRect.top += child.mGivenContentInsets.top;
5690 tmpRect.right -= child.mGivenContentInsets.right;
5691 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5692 } else if (child.mTouchableInsets == ViewTreeObserver
5693 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5694 // The touch is inside of the window if it is
5695 // inside the frame, AND the visible part of that
5696 // frame that was given by the application.
5697 tmpRect.left += child.mGivenVisibleInsets.left;
5698 tmpRect.top += child.mGivenVisibleInsets.top;
5699 tmpRect.right -= child.mGivenVisibleInsets.right;
5700 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5701 }
5702 final int touchFlags = flags &
5703 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5704 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5705 if (tmpRect.contains(x, y) || touchFlags == 0) {
5706 //Log.i(TAG, "Using this target!");
5707 if (!screenWasOff || (flags &
5708 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5709 mMotionTarget = child;
5710 } else {
5711 //Log.i(TAG, "Waking, skip!");
5712 mMotionTarget = null;
5713 }
5714 break;
5715 }
Romain Guy06882f82009-06-10 13:36:04 -07005716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 if ((flags & WindowManager.LayoutParams
5718 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5719 child.mNextOutsideTouch = mOutsideTouchTargets;
5720 mOutsideTouchTargets = child;
5721 //Log.i(TAG, "Adding to outside target list: " + child);
5722 }
5723 }
5724
5725 // if there's an error window but it's not accepting
5726 // focus (typically because it is not yet visible) just
5727 // wait for it -- any other focused window may in fact
5728 // be in ANR state.
5729 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5730 mMotionTarget = null;
5731 }
5732 }
Romain Guy06882f82009-06-10 13:36:04 -07005733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 target = mMotionTarget;
5735 }
5736 }
Romain Guy06882f82009-06-10 13:36:04 -07005737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 // Pointer events are a little different -- if there isn't a
5741 // target found for any event, then just drop it.
5742 return target != null ? target : SKIP_TARGET_TOKEN;
5743 }
Romain Guy06882f82009-06-10 13:36:04 -07005744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 boolean checkShouldDispatchKey(int keycode) {
5746 synchronized (this) {
5747 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5748 mTimeToSwitch = 0;
5749 return true;
5750 }
5751 if (mTimeToSwitch != 0
5752 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5753 return false;
5754 }
5755 return true;
5756 }
5757 }
Romain Guy06882f82009-06-10 13:36:04 -07005758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 void bindTargetWindowLocked(WindowState win,
5760 int pendingWhat, QueuedEvent pendingMotion) {
5761 synchronized (this) {
5762 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5763 }
5764 }
Romain Guy06882f82009-06-10 13:36:04 -07005765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 void bindTargetWindowLocked(WindowState win) {
5767 synchronized (this) {
5768 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5769 }
5770 }
5771
5772 void bindTargetWindowLockedLocked(WindowState win,
5773 int pendingWhat, QueuedEvent pendingMotion) {
5774 mLastWin = win;
5775 mLastBinder = win.mClient.asBinder();
5776 mFinished = false;
5777 if (pendingMotion != null) {
5778 final Session s = win.mSession;
5779 if (pendingWhat == RETURN_PENDING_POINTER) {
5780 releasePendingPointerLocked(s);
5781 s.mPendingPointerMove = pendingMotion;
5782 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005783 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 "bindTargetToWindow " + s.mPendingPointerMove);
5785 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5786 releasePendingTrackballLocked(s);
5787 s.mPendingTrackballMove = pendingMotion;
5788 s.mPendingTrackballWindow = win;
5789 }
5790 }
5791 }
Romain Guy06882f82009-06-10 13:36:04 -07005792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 void releasePendingPointerLocked(Session s) {
5794 if (DEBUG_INPUT) Log.v(TAG,
5795 "releasePendingPointer " + s.mPendingPointerMove);
5796 if (s.mPendingPointerMove != null) {
5797 mQueue.recycleEvent(s.mPendingPointerMove);
5798 s.mPendingPointerMove = null;
5799 }
5800 }
Romain Guy06882f82009-06-10 13:36:04 -07005801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 void releasePendingTrackballLocked(Session s) {
5803 if (s.mPendingTrackballMove != null) {
5804 mQueue.recycleEvent(s.mPendingTrackballMove);
5805 s.mPendingTrackballMove = null;
5806 }
5807 }
Romain Guy06882f82009-06-10 13:36:04 -07005808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5810 int returnWhat) {
5811 if (DEBUG_INPUT) Log.v(
5812 TAG, "finishedKey: client=" + client + ", force=" + force);
5813
5814 if (client == null) {
5815 return null;
5816 }
5817
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005818 MotionEvent res = null;
5819 QueuedEvent qev = null;
5820 WindowState win = null;
5821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 synchronized (this) {
5823 if (DEBUG_INPUT) Log.v(
5824 TAG, "finishedKey: client=" + client.asBinder()
5825 + ", force=" + force + ", last=" + mLastBinder
5826 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 if (returnWhat == RETURN_PENDING_POINTER) {
5829 qev = session.mPendingPointerMove;
5830 win = session.mPendingPointerWindow;
5831 session.mPendingPointerMove = null;
5832 session.mPendingPointerWindow = null;
5833 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5834 qev = session.mPendingTrackballMove;
5835 win = session.mPendingTrackballWindow;
5836 session.mPendingTrackballMove = null;
5837 session.mPendingTrackballWindow = null;
5838 }
Romain Guy06882f82009-06-10 13:36:04 -07005839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 if (mLastBinder == client.asBinder()) {
5841 if (DEBUG_INPUT) Log.v(
5842 TAG, "finishedKey: last paused="
5843 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5844 if (mLastWin != null && (!mLastWin.mToken.paused || force
5845 || !mEventDispatching)) {
5846 doFinishedKeyLocked(false);
5847 } else {
5848 // Make sure to wake up anyone currently waiting to
5849 // dispatch a key, so they can re-evaluate their
5850 // current situation.
5851 mFinished = true;
5852 notifyAll();
5853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 }
Romain Guy06882f82009-06-10 13:36:04 -07005855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005857 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 if (DEBUG_INPUT) Log.v(TAG,
5859 "Returning pending motion: " + res);
5860 mQueue.recycleEvent(qev);
5861 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5862 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005866
5867 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5868 synchronized (mWindowMap) {
5869 if (mWallpaperTarget == win) {
5870 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5871 }
5872 }
5873 }
5874
5875 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 }
5877
5878 void tickle() {
5879 synchronized (this) {
5880 notifyAll();
5881 }
5882 }
Romain Guy06882f82009-06-10 13:36:04 -07005883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 void handleNewWindowLocked(WindowState newWindow) {
5885 if (!newWindow.canReceiveKeys()) {
5886 return;
5887 }
5888 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005889 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 TAG, "New key dispatch window: win="
5891 + newWindow.mClient.asBinder()
5892 + ", last=" + mLastBinder
5893 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5894 + "), finished=" + mFinished + ", paused="
5895 + newWindow.mToken.paused);
5896
5897 // Displaying a window implicitly causes dispatching to
5898 // be unpaused. (This is to protect against bugs if someone
5899 // pauses dispatching but forgets to resume.)
5900 newWindow.mToken.paused = false;
5901
5902 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903
5904 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5905 if (DEBUG_INPUT) Log.v(TAG,
5906 "New SYSTEM_ERROR window; resetting state");
5907 mLastWin = null;
5908 mLastBinder = null;
5909 mMotionTarget = null;
5910 mFinished = true;
5911 } else if (mLastWin != null) {
5912 // If the new window is above the window we are
5913 // waiting on, then stop waiting and let key dispatching
5914 // start on the new guy.
5915 if (DEBUG_INPUT) Log.v(
5916 TAG, "Last win layer=" + mLastWin.mLayer
5917 + ", new win layer=" + newWindow.mLayer);
5918 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005919 // The new window is above the old; finish pending input to the last
5920 // window and start directing it to the new one.
5921 mLastWin.mToken.paused = false;
5922 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005924 // Either the new window is lower, so there is no need to wake key waiters,
5925 // or we just finished key input to the previous window, which implicitly
5926 // notified the key waiters. In both cases, we don't need to issue the
5927 // notification here.
5928 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 }
5930
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005931 // Now that we've put a new window state in place, make the event waiter
5932 // take notice and retarget its attentions.
5933 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 }
5935 }
5936
5937 void pauseDispatchingLocked(WindowToken token) {
5938 synchronized (this)
5939 {
5940 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5941 token.paused = true;
5942
5943 /*
5944 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5945 mPaused = true;
5946 } else {
5947 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005948 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005950 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005952 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 }
5954 }
5955 */
5956 }
5957 }
5958
5959 void resumeDispatchingLocked(WindowToken token) {
5960 synchronized (this) {
5961 if (token.paused) {
5962 if (DEBUG_INPUT) Log.v(
5963 TAG, "Resuming WindowToken " + token
5964 + ", last=" + mLastBinder
5965 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5966 + "), finished=" + mFinished + ", paused="
5967 + token.paused);
5968 token.paused = false;
5969 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5970 doFinishedKeyLocked(true);
5971 } else {
5972 notifyAll();
5973 }
5974 }
5975 }
5976 }
5977
5978 void setEventDispatchingLocked(boolean enabled) {
5979 synchronized (this) {
5980 mEventDispatching = enabled;
5981 notifyAll();
5982 }
5983 }
Romain Guy06882f82009-06-10 13:36:04 -07005984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 void appSwitchComing() {
5986 synchronized (this) {
5987 // Don't wait for more than .5 seconds for app to finish
5988 // processing the pending events.
5989 long now = SystemClock.uptimeMillis() + 500;
5990 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5991 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5992 mTimeToSwitch = now;
5993 }
5994 notifyAll();
5995 }
5996 }
Romain Guy06882f82009-06-10 13:36:04 -07005997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 private final void doFinishedKeyLocked(boolean doRecycle) {
5999 if (mLastWin != null) {
6000 releasePendingPointerLocked(mLastWin.mSession);
6001 releasePendingTrackballLocked(mLastWin.mSession);
6002 }
Romain Guy06882f82009-06-10 13:36:04 -07006003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006004 if (mLastWin == null || !mLastWin.mToken.paused
6005 || !mLastWin.isVisibleLw()) {
6006 // If the current window has been paused, we aren't -really-
6007 // finished... so let the waiters still wait.
6008 mLastWin = null;
6009 mLastBinder = null;
6010 }
6011 mFinished = true;
6012 notifyAll();
6013 }
6014 }
6015
6016 private class KeyQ extends KeyInputQueue
6017 implements KeyInputQueue.FilterCallback {
6018 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006020 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006021 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6023 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6024 "KEEP_SCREEN_ON_FLAG");
6025 mHoldingScreen.setReferenceCounted(false);
6026 }
6027
6028 @Override
6029 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6030 if (mPolicy.preprocessInputEventTq(event)) {
6031 return true;
6032 }
Romain Guy06882f82009-06-10 13:36:04 -07006033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 switch (event.type) {
6035 case RawInputEvent.EV_KEY: {
6036 // XXX begin hack
6037 if (DEBUG) {
6038 if (event.keycode == KeyEvent.KEYCODE_G) {
6039 if (event.value != 0) {
6040 // G down
6041 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6042 }
6043 return false;
6044 }
6045 if (event.keycode == KeyEvent.KEYCODE_D) {
6046 if (event.value != 0) {
6047 //dump();
6048 }
6049 return false;
6050 }
6051 }
6052 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 boolean screenIsOff = !mPowerManager.screenIsOn();
6055 boolean screenIsDim = !mPowerManager.screenIsBright();
6056 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6059 mPowerManager.goToSleep(event.when);
6060 }
6061
6062 if (screenIsOff) {
6063 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6064 }
6065 if (screenIsDim) {
6066 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6067 }
6068 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6069 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006070 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071 }
Romain Guy06882f82009-06-10 13:36:04 -07006072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6074 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6075 filterQueue(this);
6076 mKeyWaiter.appSwitchComing();
6077 }
6078 return true;
6079 } else {
6080 return false;
6081 }
6082 }
Romain Guy06882f82009-06-10 13:36:04 -07006083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 case RawInputEvent.EV_REL: {
6085 boolean screenIsOff = !mPowerManager.screenIsOn();
6086 boolean screenIsDim = !mPowerManager.screenIsBright();
6087 if (screenIsOff) {
6088 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6089 device.classes, event)) {
6090 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6091 return false;
6092 }
6093 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6094 }
6095 if (screenIsDim) {
6096 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6097 }
6098 return true;
6099 }
Romain Guy06882f82009-06-10 13:36:04 -07006100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 case RawInputEvent.EV_ABS: {
6102 boolean screenIsOff = !mPowerManager.screenIsOn();
6103 boolean screenIsDim = !mPowerManager.screenIsBright();
6104 if (screenIsOff) {
6105 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6106 device.classes, event)) {
6107 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6108 return false;
6109 }
6110 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6111 }
6112 if (screenIsDim) {
6113 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6114 }
6115 return true;
6116 }
Romain Guy06882f82009-06-10 13:36:04 -07006117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 default:
6119 return true;
6120 }
6121 }
6122
6123 public int filterEvent(QueuedEvent ev) {
6124 switch (ev.classType) {
6125 case RawInputEvent.CLASS_KEYBOARD:
6126 KeyEvent ke = (KeyEvent)ev.event;
6127 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6128 Log.w(TAG, "Dropping movement key during app switch: "
6129 + ke.getKeyCode() + ", action=" + ke.getAction());
6130 return FILTER_REMOVE;
6131 }
6132 return FILTER_ABORT;
6133 default:
6134 return FILTER_KEEP;
6135 }
6136 }
Romain Guy06882f82009-06-10 13:36:04 -07006137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 /**
6139 * Must be called with the main window manager lock held.
6140 */
6141 void setHoldScreenLocked(boolean holding) {
6142 boolean state = mHoldingScreen.isHeld();
6143 if (holding != state) {
6144 if (holding) {
6145 mHoldingScreen.acquire();
6146 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006147 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 mHoldingScreen.release();
6149 }
6150 }
6151 }
Michael Chan53071d62009-05-13 17:29:48 -07006152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153
6154 public boolean detectSafeMode() {
6155 mSafeMode = mPolicy.detectSafeMode();
6156 return mSafeMode;
6157 }
Romain Guy06882f82009-06-10 13:36:04 -07006158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 public void systemReady() {
6160 mPolicy.systemReady();
6161 }
Romain Guy06882f82009-06-10 13:36:04 -07006162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 private final class InputDispatcherThread extends Thread {
6164 // Time to wait when there is nothing to do: 9999 seconds.
6165 static final int LONG_WAIT=9999*1000;
6166
6167 public InputDispatcherThread() {
6168 super("InputDispatcher");
6169 }
Romain Guy06882f82009-06-10 13:36:04 -07006170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171 @Override
6172 public void run() {
6173 while (true) {
6174 try {
6175 process();
6176 } catch (Exception e) {
6177 Log.e(TAG, "Exception in input dispatcher", e);
6178 }
6179 }
6180 }
Romain Guy06882f82009-06-10 13:36:04 -07006181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 private void process() {
6183 android.os.Process.setThreadPriority(
6184 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 // The last key event we saw
6187 KeyEvent lastKey = null;
6188
6189 // Last keydown time for auto-repeating keys
6190 long lastKeyTime = SystemClock.uptimeMillis();
6191 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006192 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006193
Romain Guy06882f82009-06-10 13:36:04 -07006194 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 int keyRepeatCount = 0;
6196
6197 // Need to report that configuration has changed?
6198 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 while (true) {
6201 long curTime = SystemClock.uptimeMillis();
6202
6203 if (DEBUG_INPUT) Log.v(
6204 TAG, "Waiting for next key: now=" + curTime
6205 + ", repeat @ " + nextKeyTime);
6206
6207 // Retrieve next event, waiting only as long as the next
6208 // repeat timeout. If the configuration has changed, then
6209 // don't wait at all -- we'll report the change as soon as
6210 // we have processed all events.
6211 QueuedEvent ev = mQueue.getEvent(
6212 (int)((!configChanged && curTime < nextKeyTime)
6213 ? (nextKeyTime-curTime) : 0));
6214
6215 if (DEBUG_INPUT && ev != null) Log.v(
6216 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6217
Michael Chan53071d62009-05-13 17:29:48 -07006218 if (MEASURE_LATENCY) {
6219 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6220 }
6221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 try {
6223 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006224 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 int eventType;
6226 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6227 eventType = eventType((MotionEvent)ev.event);
6228 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6229 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6230 eventType = LocalPowerManager.BUTTON_EVENT;
6231 } else {
6232 eventType = LocalPowerManager.OTHER_EVENT;
6233 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006234 try {
Michael Chan53071d62009-05-13 17:29:48 -07006235 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006236 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006237 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006238 mBatteryStats.noteInputEvent();
6239 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006240 } catch (RemoteException e) {
6241 // Ignore
6242 }
Michael Chane10de972009-05-18 11:24:50 -07006243
6244 if (eventType != TOUCH_EVENT
6245 && eventType != LONG_TOUCH_EVENT
6246 && eventType != CHEEK_EVENT) {
6247 mPowerManager.userActivity(curTime, false,
6248 eventType, false);
6249 } else if (mLastTouchEventType != eventType
6250 || (curTime - mLastUserActivityCallTime)
6251 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6252 mLastUserActivityCallTime = curTime;
6253 mLastTouchEventType = eventType;
6254 mPowerManager.userActivity(curTime, false,
6255 eventType, false);
6256 }
6257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 switch (ev.classType) {
6259 case RawInputEvent.CLASS_KEYBOARD:
6260 KeyEvent ke = (KeyEvent)ev.event;
6261 if (ke.isDown()) {
6262 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006263 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 keyRepeatCount = 0;
6265 lastKeyTime = curTime;
6266 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006267 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 if (DEBUG_INPUT) Log.v(
6269 TAG, "Received key down: first repeat @ "
6270 + nextKeyTime);
6271 } else {
6272 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006273 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 // Arbitrary long timeout.
6275 lastKeyTime = curTime;
6276 nextKeyTime = curTime + LONG_WAIT;
6277 if (DEBUG_INPUT) Log.v(
6278 TAG, "Received key up: ignore repeat @ "
6279 + nextKeyTime);
6280 }
6281 dispatchKey((KeyEvent)ev.event, 0, 0);
6282 mQueue.recycleEvent(ev);
6283 break;
6284 case RawInputEvent.CLASS_TOUCHSCREEN:
6285 //Log.i(TAG, "Read next event " + ev);
6286 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6287 break;
6288 case RawInputEvent.CLASS_TRACKBALL:
6289 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6290 break;
6291 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6292 configChanged = true;
6293 break;
6294 default:
6295 mQueue.recycleEvent(ev);
6296 break;
6297 }
Romain Guy06882f82009-06-10 13:36:04 -07006298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 } else if (configChanged) {
6300 configChanged = false;
6301 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 } else if (lastKey != null) {
6304 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 // Timeout occurred while key was down. If it is at or
6307 // past the key repeat time, dispatch the repeat.
6308 if (DEBUG_INPUT) Log.v(
6309 TAG, "Key timeout: repeat=" + nextKeyTime
6310 + ", now=" + curTime);
6311 if (curTime < nextKeyTime) {
6312 continue;
6313 }
Romain Guy06882f82009-06-10 13:36:04 -07006314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 lastKeyTime = nextKeyTime;
6316 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6317 keyRepeatCount++;
6318 if (DEBUG_INPUT) Log.v(
6319 TAG, "Key repeat: count=" + keyRepeatCount
6320 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006321 KeyEvent newEvent;
6322 if (downTime != 0 && (downTime
6323 + ViewConfiguration.getLongPressTimeout())
6324 <= curTime) {
6325 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6326 curTime, keyRepeatCount,
6327 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6328 downTime = 0;
6329 } else {
6330 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6331 curTime, keyRepeatCount);
6332 }
6333 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 } else {
6336 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 lastKeyTime = curTime;
6339 nextKeyTime = curTime + LONG_WAIT;
6340 }
Romain Guy06882f82009-06-10 13:36:04 -07006341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 } catch (Exception e) {
6343 Log.e(TAG,
6344 "Input thread received uncaught exception: " + e, e);
6345 }
6346 }
6347 }
6348 }
6349
6350 // -------------------------------------------------------------
6351 // Client Session State
6352 // -------------------------------------------------------------
6353
6354 private final class Session extends IWindowSession.Stub
6355 implements IBinder.DeathRecipient {
6356 final IInputMethodClient mClient;
6357 final IInputContext mInputContext;
6358 final int mUid;
6359 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006360 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 SurfaceSession mSurfaceSession;
6362 int mNumWindow = 0;
6363 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 /**
6366 * Current pointer move event being dispatched to client window... must
6367 * hold key lock to access.
6368 */
6369 QueuedEvent mPendingPointerMove;
6370 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 /**
6373 * Current trackball move event being dispatched to client window... must
6374 * hold key lock to access.
6375 */
6376 QueuedEvent mPendingTrackballMove;
6377 WindowState mPendingTrackballWindow;
6378
6379 public Session(IInputMethodClient client, IInputContext inputContext) {
6380 mClient = client;
6381 mInputContext = inputContext;
6382 mUid = Binder.getCallingUid();
6383 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006384 StringBuilder sb = new StringBuilder();
6385 sb.append("Session{");
6386 sb.append(Integer.toHexString(System.identityHashCode(this)));
6387 sb.append(" uid ");
6388 sb.append(mUid);
6389 sb.append("}");
6390 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 synchronized (mWindowMap) {
6393 if (mInputMethodManager == null && mHaveInputMethods) {
6394 IBinder b = ServiceManager.getService(
6395 Context.INPUT_METHOD_SERVICE);
6396 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6397 }
6398 }
6399 long ident = Binder.clearCallingIdentity();
6400 try {
6401 // Note: it is safe to call in to the input method manager
6402 // here because we are not holding our lock.
6403 if (mInputMethodManager != null) {
6404 mInputMethodManager.addClient(client, inputContext,
6405 mUid, mPid);
6406 } else {
6407 client.setUsingInputMethod(false);
6408 }
6409 client.asBinder().linkToDeath(this, 0);
6410 } catch (RemoteException e) {
6411 // The caller has died, so we can just forget about this.
6412 try {
6413 if (mInputMethodManager != null) {
6414 mInputMethodManager.removeClient(client);
6415 }
6416 } catch (RemoteException ee) {
6417 }
6418 } finally {
6419 Binder.restoreCallingIdentity(ident);
6420 }
6421 }
Romain Guy06882f82009-06-10 13:36:04 -07006422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 @Override
6424 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6425 throws RemoteException {
6426 try {
6427 return super.onTransact(code, data, reply, flags);
6428 } catch (RuntimeException e) {
6429 // Log all 'real' exceptions thrown to the caller
6430 if (!(e instanceof SecurityException)) {
6431 Log.e(TAG, "Window Session Crash", e);
6432 }
6433 throw e;
6434 }
6435 }
6436
6437 public void binderDied() {
6438 // Note: it is safe to call in to the input method manager
6439 // here because we are not holding our lock.
6440 try {
6441 if (mInputMethodManager != null) {
6442 mInputMethodManager.removeClient(mClient);
6443 }
6444 } catch (RemoteException e) {
6445 }
6446 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006447 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 mClientDead = true;
6449 killSessionLocked();
6450 }
6451 }
6452
6453 public int add(IWindow window, WindowManager.LayoutParams attrs,
6454 int viewVisibility, Rect outContentInsets) {
6455 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6456 }
Romain Guy06882f82009-06-10 13:36:04 -07006457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 public void remove(IWindow window) {
6459 removeWindow(this, window);
6460 }
Romain Guy06882f82009-06-10 13:36:04 -07006461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6463 int requestedWidth, int requestedHeight, int viewFlags,
6464 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6465 Rect outVisibleInsets, Surface outSurface) {
6466 return relayoutWindow(this, window, attrs,
6467 requestedWidth, requestedHeight, viewFlags, insetsPending,
6468 outFrame, outContentInsets, outVisibleInsets, outSurface);
6469 }
Romain Guy06882f82009-06-10 13:36:04 -07006470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 public void setTransparentRegion(IWindow window, Region region) {
6472 setTransparentRegionWindow(this, window, region);
6473 }
Romain Guy06882f82009-06-10 13:36:04 -07006474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 public void setInsets(IWindow window, int touchableInsets,
6476 Rect contentInsets, Rect visibleInsets) {
6477 setInsetsWindow(this, window, touchableInsets, contentInsets,
6478 visibleInsets);
6479 }
Romain Guy06882f82009-06-10 13:36:04 -07006480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006481 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6482 getWindowDisplayFrame(this, window, outDisplayFrame);
6483 }
Romain Guy06882f82009-06-10 13:36:04 -07006484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 public void finishDrawing(IWindow window) {
6486 if (localLOGV) Log.v(
6487 TAG, "IWindow finishDrawing called for " + window);
6488 finishDrawingWindow(this, window);
6489 }
6490
6491 public void finishKey(IWindow window) {
6492 if (localLOGV) Log.v(
6493 TAG, "IWindow finishKey called for " + window);
6494 mKeyWaiter.finishedKey(this, window, false,
6495 KeyWaiter.RETURN_NOTHING);
6496 }
6497
6498 public MotionEvent getPendingPointerMove(IWindow window) {
6499 if (localLOGV) Log.v(
6500 TAG, "IWindow getPendingMotionEvent called for " + window);
6501 return mKeyWaiter.finishedKey(this, window, false,
6502 KeyWaiter.RETURN_PENDING_POINTER);
6503 }
Romain Guy06882f82009-06-10 13:36:04 -07006504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 public MotionEvent getPendingTrackballMove(IWindow window) {
6506 if (localLOGV) Log.v(
6507 TAG, "IWindow getPendingMotionEvent called for " + window);
6508 return mKeyWaiter.finishedKey(this, window, false,
6509 KeyWaiter.RETURN_PENDING_TRACKBALL);
6510 }
6511
6512 public void setInTouchMode(boolean mode) {
6513 synchronized(mWindowMap) {
6514 mInTouchMode = mode;
6515 }
6516 }
6517
6518 public boolean getInTouchMode() {
6519 synchronized(mWindowMap) {
6520 return mInTouchMode;
6521 }
6522 }
6523
6524 public boolean performHapticFeedback(IWindow window, int effectId,
6525 boolean always) {
6526 synchronized(mWindowMap) {
6527 long ident = Binder.clearCallingIdentity();
6528 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006529 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 windowForClientLocked(this, window), effectId, always);
6531 } finally {
6532 Binder.restoreCallingIdentity(ident);
6533 }
6534 }
6535 }
Romain Guy06882f82009-06-10 13:36:04 -07006536
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006537 public void setWallpaperPosition(IBinder window, float x, float y) {
6538 synchronized(mWindowMap) {
6539 long ident = Binder.clearCallingIdentity();
6540 try {
6541 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6542 x, y);
6543 } finally {
6544 Binder.restoreCallingIdentity(ident);
6545 }
6546 }
6547 }
6548
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006549 public void wallpaperOffsetsComplete(IBinder window) {
6550 WindowManagerService.this.wallpaperOffsetsComplete(window);
6551 }
6552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 void windowAddedLocked() {
6554 if (mSurfaceSession == null) {
6555 if (localLOGV) Log.v(
6556 TAG, "First window added to " + this + ", creating SurfaceSession");
6557 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006558 if (SHOW_TRANSACTIONS) Log.i(
6559 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 mSessions.add(this);
6561 }
6562 mNumWindow++;
6563 }
6564
6565 void windowRemovedLocked() {
6566 mNumWindow--;
6567 killSessionLocked();
6568 }
Romain Guy06882f82009-06-10 13:36:04 -07006569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006570 void killSessionLocked() {
6571 if (mNumWindow <= 0 && mClientDead) {
6572 mSessions.remove(this);
6573 if (mSurfaceSession != null) {
6574 if (localLOGV) Log.v(
6575 TAG, "Last window removed from " + this
6576 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006577 if (SHOW_TRANSACTIONS) Log.i(
6578 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 try {
6580 mSurfaceSession.kill();
6581 } catch (Exception e) {
6582 Log.w(TAG, "Exception thrown when killing surface session "
6583 + mSurfaceSession + " in session " + this
6584 + ": " + e.toString());
6585 }
6586 mSurfaceSession = null;
6587 }
6588 }
6589 }
Romain Guy06882f82009-06-10 13:36:04 -07006590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006592 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6593 pw.print(" mClientDead="); pw.print(mClientDead);
6594 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6595 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6596 pw.print(prefix);
6597 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6598 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6599 }
6600 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6601 pw.print(prefix);
6602 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6603 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006605 }
6606
6607 @Override
6608 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006609 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 }
6611 }
6612
6613 // -------------------------------------------------------------
6614 // Client Window State
6615 // -------------------------------------------------------------
6616
6617 private final class WindowState implements WindowManagerPolicy.WindowState {
6618 final Session mSession;
6619 final IWindow mClient;
6620 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006621 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 AppWindowToken mAppToken;
6623 AppWindowToken mTargetAppToken;
6624 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6625 final DeathRecipient mDeathRecipient;
6626 final WindowState mAttachedWindow;
6627 final ArrayList mChildWindows = new ArrayList();
6628 final int mBaseLayer;
6629 final int mSubLayer;
6630 final boolean mLayoutAttached;
6631 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006632 final boolean mIsWallpaper;
6633 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 int mViewVisibility;
6635 boolean mPolicyVisibility = true;
6636 boolean mPolicyVisibilityAfterAnim = true;
6637 boolean mAppFreezing;
6638 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006639 boolean mReportDestroySurface;
6640 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 boolean mAttachedHidden; // is our parent window hidden?
6642 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006643 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 int mRequestedWidth;
6645 int mRequestedHeight;
6646 int mLastRequestedWidth;
6647 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 int mLayer;
6649 int mAnimLayer;
6650 int mLastLayer;
6651 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006652 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006653 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654
6655 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 // Actual frame shown on-screen (may be modified by animation)
6658 final Rect mShownFrame = new Rect();
6659 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 /**
6662 * Insets that determine the actually visible area
6663 */
6664 final Rect mVisibleInsets = new Rect();
6665 final Rect mLastVisibleInsets = new Rect();
6666 boolean mVisibleInsetsChanged;
6667
6668 /**
6669 * Insets that are covered by system windows
6670 */
6671 final Rect mContentInsets = new Rect();
6672 final Rect mLastContentInsets = new Rect();
6673 boolean mContentInsetsChanged;
6674
6675 /**
6676 * Set to true if we are waiting for this window to receive its
6677 * given internal insets before laying out other windows based on it.
6678 */
6679 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006681 /**
6682 * These are the content insets that were given during layout for
6683 * this window, to be applied to windows behind it.
6684 */
6685 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687 /**
6688 * These are the visible insets that were given during layout for
6689 * this window, to be applied to windows behind it.
6690 */
6691 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006693 /**
6694 * Flag indicating whether the touchable region should be adjusted by
6695 * the visible insets; if false the area outside the visible insets is
6696 * NOT touchable, so we must use those to adjust the frame during hit
6697 * tests.
6698 */
6699 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006701 // Current transformation being applied.
6702 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6703 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6704 float mHScale=1, mVScale=1;
6705 float mLastHScale=1, mLastVScale=1;
6706 final Matrix mTmpMatrix = new Matrix();
6707
6708 // "Real" frame that the application sees.
6709 final Rect mFrame = new Rect();
6710 final Rect mLastFrame = new Rect();
6711
6712 final Rect mContainingFrame = new Rect();
6713 final Rect mDisplayFrame = new Rect();
6714 final Rect mContentFrame = new Rect();
6715 final Rect mVisibleFrame = new Rect();
6716
6717 float mShownAlpha = 1;
6718 float mAlpha = 1;
6719 float mLastAlpha = 1;
6720
6721 // Set to true if, when the window gets displayed, it should perform
6722 // an enter animation.
6723 boolean mEnterAnimationPending;
6724
6725 // Currently running animation.
6726 boolean mAnimating;
6727 boolean mLocalAnimating;
6728 Animation mAnimation;
6729 boolean mAnimationIsEntrance;
6730 boolean mHasTransformation;
6731 boolean mHasLocalTransformation;
6732 final Transformation mTransformation = new Transformation();
6733
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006734 // If a window showing a wallpaper: the requested offset for the
6735 // wallpaper; if a wallpaper window: the currently applied offset.
6736 float mWallpaperX = -1;
6737 float mWallpaperY = -1;
6738
6739 // Wallpaper windows: pixels offset based on above variables.
6740 int mXOffset;
6741 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006743 // This is set after IWindowSession.relayout() has been called at
6744 // least once for the window. It allows us to detect the situation
6745 // where we don't yet have a surface, but should have one soon, so
6746 // we can give the window focus before waiting for the relayout.
6747 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749 // This is set after the Surface has been created but before the
6750 // window has been drawn. During this time the surface is hidden.
6751 boolean mDrawPending;
6752
6753 // This is set after the window has finished drawing for the first
6754 // time but before its surface is shown. The surface will be
6755 // displayed when the next layout is run.
6756 boolean mCommitDrawPending;
6757
6758 // This is set during the time after the window's drawing has been
6759 // committed, and before its surface is actually shown. It is used
6760 // to delay showing the surface until all windows in a token are ready
6761 // to be shown.
6762 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 // Set when the window has been shown in the screen the first time.
6765 boolean mHasDrawn;
6766
6767 // Currently running an exit animation?
6768 boolean mExiting;
6769
6770 // Currently on the mDestroySurface list?
6771 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 // Completely remove from window manager after exit animation?
6774 boolean mRemoveOnExit;
6775
6776 // Set when the orientation is changing and this window has not yet
6777 // been updated for the new orientation.
6778 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 // Is this window now (or just being) removed?
6781 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 WindowState(Session s, IWindow c, WindowToken token,
6784 WindowState attachedWindow, WindowManager.LayoutParams a,
6785 int viewVisibility) {
6786 mSession = s;
6787 mClient = c;
6788 mToken = token;
6789 mAttrs.copyFrom(a);
6790 mViewVisibility = viewVisibility;
6791 DeathRecipient deathRecipient = new DeathRecipient();
6792 mAlpha = a.alpha;
6793 if (localLOGV) Log.v(
6794 TAG, "Window " + this + " client=" + c.asBinder()
6795 + " token=" + token + " (" + mAttrs.token + ")");
6796 try {
6797 c.asBinder().linkToDeath(deathRecipient, 0);
6798 } catch (RemoteException e) {
6799 mDeathRecipient = null;
6800 mAttachedWindow = null;
6801 mLayoutAttached = false;
6802 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006803 mIsWallpaper = false;
6804 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006805 mBaseLayer = 0;
6806 mSubLayer = 0;
6807 return;
6808 }
6809 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6812 mAttrs.type <= LAST_SUB_WINDOW)) {
6813 // The multiplier here is to reserve space for multiple
6814 // windows in the same type layer.
6815 mBaseLayer = mPolicy.windowTypeToLayerLw(
6816 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6817 + TYPE_LAYER_OFFSET;
6818 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6819 mAttachedWindow = attachedWindow;
6820 mAttachedWindow.mChildWindows.add(this);
6821 mLayoutAttached = mAttrs.type !=
6822 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6823 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6824 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006825 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6826 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 } else {
6828 // The multiplier here is to reserve space for multiple
6829 // windows in the same type layer.
6830 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6831 * TYPE_LAYER_MULTIPLIER
6832 + TYPE_LAYER_OFFSET;
6833 mSubLayer = 0;
6834 mAttachedWindow = null;
6835 mLayoutAttached = false;
6836 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6837 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006838 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6839 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006840 }
6841
6842 WindowState appWin = this;
6843 while (appWin.mAttachedWindow != null) {
6844 appWin = mAttachedWindow;
6845 }
6846 WindowToken appToken = appWin.mToken;
6847 while (appToken.appWindowToken == null) {
6848 WindowToken parent = mTokenMap.get(appToken.token);
6849 if (parent == null || appToken == parent) {
6850 break;
6851 }
6852 appToken = parent;
6853 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006854 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 mAppToken = appToken.appWindowToken;
6856
6857 mSurface = null;
6858 mRequestedWidth = 0;
6859 mRequestedHeight = 0;
6860 mLastRequestedWidth = 0;
6861 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006862 mXOffset = 0;
6863 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 mLayer = 0;
6865 mAnimLayer = 0;
6866 mLastLayer = 0;
6867 }
6868
6869 void attach() {
6870 if (localLOGV) Log.v(
6871 TAG, "Attaching " + this + " token=" + mToken
6872 + ", list=" + mToken.windows);
6873 mSession.windowAddedLocked();
6874 }
6875
6876 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6877 mHaveFrame = true;
6878
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006879 final Rect container = mContainingFrame;
6880 container.set(pf);
6881
6882 final Rect display = mDisplayFrame;
6883 display.set(df);
6884
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006885 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006886 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006887 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6888 display.intersect(mCompatibleScreenFrame);
6889 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006890 }
6891
6892 final int pw = container.right - container.left;
6893 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894
6895 int w,h;
6896 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6897 w = mAttrs.width < 0 ? pw : mAttrs.width;
6898 h = mAttrs.height< 0 ? ph : mAttrs.height;
6899 } else {
6900 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6901 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6902 }
Romain Guy06882f82009-06-10 13:36:04 -07006903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 final Rect content = mContentFrame;
6905 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 final Rect visible = mVisibleFrame;
6908 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006911 final int fw = frame.width();
6912 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6915 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6916
6917 Gravity.apply(mAttrs.gravity, w, h, container,
6918 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6919 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6920
6921 //System.out.println("Out: " + mFrame);
6922
6923 // Now make sure the window fits in the overall display.
6924 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 // Make sure the content and visible frames are inside of the
6927 // final window frame.
6928 if (content.left < frame.left) content.left = frame.left;
6929 if (content.top < frame.top) content.top = frame.top;
6930 if (content.right > frame.right) content.right = frame.right;
6931 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6932 if (visible.left < frame.left) visible.left = frame.left;
6933 if (visible.top < frame.top) visible.top = frame.top;
6934 if (visible.right > frame.right) visible.right = frame.right;
6935 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006937 final Rect contentInsets = mContentInsets;
6938 contentInsets.left = content.left-frame.left;
6939 contentInsets.top = content.top-frame.top;
6940 contentInsets.right = frame.right-content.right;
6941 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 final Rect visibleInsets = mVisibleInsets;
6944 visibleInsets.left = visible.left-frame.left;
6945 visibleInsets.top = visible.top-frame.top;
6946 visibleInsets.right = frame.right-visible.right;
6947 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006948
Dianne Hackborn284ac932009-08-28 10:34:25 -07006949 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6950 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006951 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006952 }
6953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 if (localLOGV) {
6955 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6956 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6957 Log.v(TAG, "Resolving (mRequestedWidth="
6958 + mRequestedWidth + ", mRequestedheight="
6959 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6960 + "): frame=" + mFrame.toShortString()
6961 + " ci=" + contentInsets.toShortString()
6962 + " vi=" + visibleInsets.toShortString());
6963 //}
6964 }
6965 }
Romain Guy06882f82009-06-10 13:36:04 -07006966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 public Rect getFrameLw() {
6968 return mFrame;
6969 }
6970
6971 public Rect getShownFrameLw() {
6972 return mShownFrame;
6973 }
6974
6975 public Rect getDisplayFrameLw() {
6976 return mDisplayFrame;
6977 }
6978
6979 public Rect getContentFrameLw() {
6980 return mContentFrame;
6981 }
6982
6983 public Rect getVisibleFrameLw() {
6984 return mVisibleFrame;
6985 }
6986
6987 public boolean getGivenInsetsPendingLw() {
6988 return mGivenInsetsPending;
6989 }
6990
6991 public Rect getGivenContentInsetsLw() {
6992 return mGivenContentInsets;
6993 }
Romain Guy06882f82009-06-10 13:36:04 -07006994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 public Rect getGivenVisibleInsetsLw() {
6996 return mGivenVisibleInsets;
6997 }
Romain Guy06882f82009-06-10 13:36:04 -07006998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 public WindowManager.LayoutParams getAttrs() {
7000 return mAttrs;
7001 }
7002
7003 public int getSurfaceLayer() {
7004 return mLayer;
7005 }
Romain Guy06882f82009-06-10 13:36:04 -07007006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 public IApplicationToken getAppToken() {
7008 return mAppToken != null ? mAppToken.appToken : null;
7009 }
7010
7011 public boolean hasAppShownWindows() {
7012 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7013 }
7014
7015 public boolean hasAppStartingIcon() {
7016 return mAppToken != null ? (mAppToken.startingData != null) : false;
7017 }
7018
7019 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7020 return mAppToken != null ? mAppToken.startingWindow : null;
7021 }
7022
7023 public void setAnimation(Animation anim) {
7024 if (localLOGV) Log.v(
7025 TAG, "Setting animation in " + this + ": " + anim);
7026 mAnimating = false;
7027 mLocalAnimating = false;
7028 mAnimation = anim;
7029 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7030 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7031 }
7032
7033 public void clearAnimation() {
7034 if (mAnimation != null) {
7035 mAnimating = true;
7036 mLocalAnimating = false;
7037 mAnimation = null;
7038 }
7039 }
Romain Guy06882f82009-06-10 13:36:04 -07007040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 Surface createSurfaceLocked() {
7042 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007043 mReportDestroySurface = false;
7044 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 mDrawPending = true;
7046 mCommitDrawPending = false;
7047 mReadyToShow = false;
7048 if (mAppToken != null) {
7049 mAppToken.allDrawn = false;
7050 }
7051
7052 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007053 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 flags |= Surface.PUSH_BUFFERS;
7055 }
7056
7057 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7058 flags |= Surface.SECURE;
7059 }
7060 if (DEBUG_VISIBILITY) Log.v(
7061 TAG, "Creating surface in session "
7062 + mSession.mSurfaceSession + " window " + this
7063 + " w=" + mFrame.width()
7064 + " h=" + mFrame.height() + " format="
7065 + mAttrs.format + " flags=" + flags);
7066
7067 int w = mFrame.width();
7068 int h = mFrame.height();
7069 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7070 // for a scaled surface, we always want the requested
7071 // size.
7072 w = mRequestedWidth;
7073 h = mRequestedHeight;
7074 }
7075
Romain Guy9825ec62009-10-01 00:58:09 -07007076 // Something is wrong and SurfaceFlinger will not like this,
7077 // try to revert to sane values
7078 if (w <= 0) w = 1;
7079 if (h <= 0) h = 1;
7080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 try {
7082 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007083 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007085 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7086 + mSurface + " IN SESSION "
7087 + mSession.mSurfaceSession
7088 + ": pid=" + mSession.mPid + " format="
7089 + mAttrs.format + " flags=0x"
7090 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 } catch (Surface.OutOfResourcesException e) {
7092 Log.w(TAG, "OutOfResourcesException creating surface");
7093 reclaimSomeSurfaceMemoryLocked(this, "create");
7094 return null;
7095 } catch (Exception e) {
7096 Log.e(TAG, "Exception creating surface", e);
7097 return null;
7098 }
Romain Guy06882f82009-06-10 13:36:04 -07007099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 if (localLOGV) Log.v(
7101 TAG, "Got surface: " + mSurface
7102 + ", set left=" + mFrame.left + " top=" + mFrame.top
7103 + ", animLayer=" + mAnimLayer);
7104 if (SHOW_TRANSACTIONS) {
7105 Log.i(TAG, ">>> OPEN TRANSACTION");
7106 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7107 + mAttrs.getTitle() + ") pos=(" +
7108 mFrame.left + "," + mFrame.top + ") (" +
7109 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7110 mAnimLayer + " HIDE");
7111 }
7112 Surface.openTransaction();
7113 try {
7114 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007115 mSurface.setPosition(mFrame.left + mXOffset,
7116 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 mSurface.setLayer(mAnimLayer);
7118 mSurface.hide();
7119 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007120 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7121 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 mSurface.setFlags(Surface.SURFACE_DITHER,
7123 Surface.SURFACE_DITHER);
7124 }
7125 } catch (RuntimeException e) {
7126 Log.w(TAG, "Error creating surface in " + w, e);
7127 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7128 }
7129 mLastHidden = true;
7130 } finally {
7131 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7132 Surface.closeTransaction();
7133 }
7134 if (localLOGV) Log.v(
7135 TAG, "Created surface " + this);
7136 }
7137 return mSurface;
7138 }
Romain Guy06882f82009-06-10 13:36:04 -07007139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 void destroySurfaceLocked() {
7141 // Window is no longer on-screen, so can no longer receive
7142 // key events... if we were waiting for it to finish
7143 // handling a key event, the wait is over!
7144 mKeyWaiter.finishedKey(mSession, mClient, true,
7145 KeyWaiter.RETURN_NOTHING);
7146 mKeyWaiter.releasePendingPointerLocked(mSession);
7147 mKeyWaiter.releasePendingTrackballLocked(mSession);
7148
7149 if (mAppToken != null && this == mAppToken.startingWindow) {
7150 mAppToken.startingDisplayed = false;
7151 }
Romain Guy06882f82009-06-10 13:36:04 -07007152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007154 mDrawPending = false;
7155 mCommitDrawPending = false;
7156 mReadyToShow = false;
7157
7158 int i = mChildWindows.size();
7159 while (i > 0) {
7160 i--;
7161 WindowState c = (WindowState)mChildWindows.get(i);
7162 c.mAttachedHidden = true;
7163 }
7164
7165 if (mReportDestroySurface) {
7166 mReportDestroySurface = false;
7167 mSurfacePendingDestroy = true;
7168 try {
7169 mClient.dispatchGetNewSurface();
7170 // We'll really destroy on the next time around.
7171 return;
7172 } catch (RemoteException e) {
7173 }
7174 }
7175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007177 if (DEBUG_VISIBILITY) {
7178 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007179 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007180 Log.w(TAG, "Window " + this + " destroying surface "
7181 + mSurface + ", session " + mSession, e);
7182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 if (SHOW_TRANSACTIONS) {
7184 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007185 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7187 + mAttrs.getTitle() + ")", ex);
7188 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007189 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 } catch (RuntimeException e) {
7191 Log.w(TAG, "Exception thrown when destroying Window " + this
7192 + " surface " + mSurface + " session " + mSession
7193 + ": " + e.toString());
7194 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 }
7198 }
7199
7200 boolean finishDrawingLocked() {
7201 if (mDrawPending) {
7202 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7203 TAG, "finishDrawingLocked: " + mSurface);
7204 mCommitDrawPending = true;
7205 mDrawPending = false;
7206 return true;
7207 }
7208 return false;
7209 }
7210
7211 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007212 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7214 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007215 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 }
7217 mCommitDrawPending = false;
7218 mReadyToShow = true;
7219 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7220 final AppWindowToken atoken = mAppToken;
7221 if (atoken == null || atoken.allDrawn || starting) {
7222 performShowLocked();
7223 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007224 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 }
7226
7227 // This must be called while inside a transaction.
7228 boolean performShowLocked() {
7229 if (DEBUG_VISIBILITY) {
7230 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007231 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007232 Log.v(TAG, "performShow on " + this
7233 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7234 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7235 }
7236 if (mReadyToShow && isReadyForDisplay()) {
7237 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7238 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7239 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7240 + " during animation: policyVis=" + mPolicyVisibility
7241 + " attHidden=" + mAttachedHidden
7242 + " tok.hiddenRequested="
7243 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007244 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 + (mAppToken != null ? mAppToken.hidden : false)
7246 + " animating=" + mAnimating
7247 + " tok animating="
7248 + (mAppToken != null ? mAppToken.animating : false));
7249 if (!showSurfaceRobustlyLocked(this)) {
7250 return false;
7251 }
7252 mLastAlpha = -1;
7253 mHasDrawn = true;
7254 mLastHidden = false;
7255 mReadyToShow = false;
7256 enableScreenIfNeededLocked();
7257
7258 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 int i = mChildWindows.size();
7261 while (i > 0) {
7262 i--;
7263 WindowState c = (WindowState)mChildWindows.get(i);
7264 if (c.mSurface != null && c.mAttachedHidden) {
7265 c.mAttachedHidden = false;
7266 c.performShowLocked();
7267 }
7268 }
Romain Guy06882f82009-06-10 13:36:04 -07007269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 if (mAttrs.type != TYPE_APPLICATION_STARTING
7271 && mAppToken != null) {
7272 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007273
7274 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007275 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7276 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007277 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007278 // If this initial window is animating, stop it -- we
7279 // will do an animation to reveal it from behind the
7280 // starting window, so there is no need for it to also
7281 // be doing its own stuff.
7282 if (mAnimation != null) {
7283 mAnimation = null;
7284 // Make sure we clean up the animation.
7285 mAnimating = true;
7286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 mFinishedStarting.add(mAppToken);
7288 mH.sendEmptyMessage(H.FINISHED_STARTING);
7289 }
7290 mAppToken.updateReportedVisibilityLocked();
7291 }
7292 }
7293 return true;
7294 }
Romain Guy06882f82009-06-10 13:36:04 -07007295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 // This must be called while inside a transaction. Returns true if
7297 // there is more animation to run.
7298 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7299 if (!mDisplayFrozen) {
7300 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007302 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7303 mHasTransformation = true;
7304 mHasLocalTransformation = true;
7305 if (!mLocalAnimating) {
7306 if (DEBUG_ANIM) Log.v(
7307 TAG, "Starting animation in " + this +
7308 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7309 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7310 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7311 mAnimation.setStartTime(currentTime);
7312 mLocalAnimating = true;
7313 mAnimating = true;
7314 }
7315 mTransformation.clear();
7316 final boolean more = mAnimation.getTransformation(
7317 currentTime, mTransformation);
7318 if (DEBUG_ANIM) Log.v(
7319 TAG, "Stepped animation in " + this +
7320 ": more=" + more + ", xform=" + mTransformation);
7321 if (more) {
7322 // we're not done!
7323 return true;
7324 }
7325 if (DEBUG_ANIM) Log.v(
7326 TAG, "Finished animation in " + this +
7327 " @ " + currentTime);
7328 mAnimation = null;
7329 //WindowManagerService.this.dump();
7330 }
7331 mHasLocalTransformation = false;
7332 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007333 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 // When our app token is animating, we kind-of pretend like
7335 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7336 // part of this check means that we will only do this if
7337 // our window is not currently exiting, or it is not
7338 // locally animating itself. The idea being that one that
7339 // is exiting and doing a local animation should be removed
7340 // once that animation is done.
7341 mAnimating = true;
7342 mHasTransformation = true;
7343 mTransformation.clear();
7344 return false;
7345 } else if (mHasTransformation) {
7346 // Little trick to get through the path below to act like
7347 // we have finished an animation.
7348 mAnimating = true;
7349 } else if (isAnimating()) {
7350 mAnimating = true;
7351 }
7352 } else if (mAnimation != null) {
7353 // If the display is frozen, and there is a pending animation,
7354 // clear it and make sure we run the cleanup code.
7355 mAnimating = true;
7356 mLocalAnimating = true;
7357 mAnimation = null;
7358 }
Romain Guy06882f82009-06-10 13:36:04 -07007359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 if (!mAnimating && !mLocalAnimating) {
7361 return false;
7362 }
7363
7364 if (DEBUG_ANIM) Log.v(
7365 TAG, "Animation done in " + this + ": exiting=" + mExiting
7366 + ", reportedVisible="
7367 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 mAnimating = false;
7370 mLocalAnimating = false;
7371 mAnimation = null;
7372 mAnimLayer = mLayer;
7373 if (mIsImWindow) {
7374 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007375 } else if (mIsWallpaper) {
7376 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 }
7378 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7379 + " anim layer: " + mAnimLayer);
7380 mHasTransformation = false;
7381 mHasLocalTransformation = false;
7382 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7383 mTransformation.clear();
7384 if (mHasDrawn
7385 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7386 && mAppToken != null
7387 && mAppToken.firstWindowDrawn
7388 && mAppToken.startingData != null) {
7389 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7390 + mToken + ": first real window done animating");
7391 mFinishedStarting.add(mAppToken);
7392 mH.sendEmptyMessage(H.FINISHED_STARTING);
7393 }
Romain Guy06882f82009-06-10 13:36:04 -07007394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 finishExit();
7396
7397 if (mAppToken != null) {
7398 mAppToken.updateReportedVisibilityLocked();
7399 }
7400
7401 return false;
7402 }
7403
7404 void finishExit() {
7405 if (DEBUG_ANIM) Log.v(
7406 TAG, "finishExit in " + this
7407 + ": exiting=" + mExiting
7408 + " remove=" + mRemoveOnExit
7409 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 final int N = mChildWindows.size();
7412 for (int i=0; i<N; i++) {
7413 ((WindowState)mChildWindows.get(i)).finishExit();
7414 }
Romain Guy06882f82009-06-10 13:36:04 -07007415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 if (!mExiting) {
7417 return;
7418 }
Romain Guy06882f82009-06-10 13:36:04 -07007419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 if (isWindowAnimating()) {
7421 return;
7422 }
7423
7424 if (localLOGV) Log.v(
7425 TAG, "Exit animation finished in " + this
7426 + ": remove=" + mRemoveOnExit);
7427 if (mSurface != null) {
7428 mDestroySurface.add(this);
7429 mDestroying = true;
7430 if (SHOW_TRANSACTIONS) Log.i(
7431 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7432 try {
7433 mSurface.hide();
7434 } catch (RuntimeException e) {
7435 Log.w(TAG, "Error hiding surface in " + this, e);
7436 }
7437 mLastHidden = true;
7438 mKeyWaiter.releasePendingPointerLocked(mSession);
7439 }
7440 mExiting = false;
7441 if (mRemoveOnExit) {
7442 mPendingRemove.add(this);
7443 mRemoveOnExit = false;
7444 }
7445 }
Romain Guy06882f82009-06-10 13:36:04 -07007446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7448 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7449 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7450 if (dtdx < -.000001f || dtdx > .000001f) return false;
7451 if (dsdy < -.000001f || dsdy > .000001f) return false;
7452 return true;
7453 }
Romain Guy06882f82009-06-10 13:36:04 -07007454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 void computeShownFrameLocked() {
7456 final boolean selfTransformation = mHasLocalTransformation;
7457 Transformation attachedTransformation =
7458 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7459 ? mAttachedWindow.mTransformation : null;
7460 Transformation appTransformation =
7461 (mAppToken != null && mAppToken.hasTransformation)
7462 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007463
7464 // Wallpapers are animated based on the "real" window they
7465 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007466 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007467 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007468 if (mWallpaperTarget.mHasLocalTransformation &&
7469 mWallpaperTarget.mAnimation != null &&
7470 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007471 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007472 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7473 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7474 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007475 }
7476 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007477 mWallpaperTarget.mAppToken.hasTransformation &&
7478 mWallpaperTarget.mAppToken.animation != null &&
7479 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007480 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007481 if (DEBUG_WALLPAPER && appTransformation != null) {
7482 Log.v(TAG, "WP target app xform: " + appTransformation);
7483 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007484 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007485 }
7486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487 if (selfTransformation || attachedTransformation != null
7488 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007489 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007490 final Rect frame = mFrame;
7491 final float tmpFloats[] = mTmpFloats;
7492 final Matrix tmpMatrix = mTmpMatrix;
7493
7494 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007495 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007497 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007498 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007499 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007501 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 }
7503 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007504 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 }
7506
7507 // "convert" it into SurfaceFlinger's format
7508 // (a 2x2 matrix + an offset)
7509 // Here we must not transform the position of the surface
7510 // since it is already included in the transformation.
7511 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 tmpMatrix.getValues(tmpFloats);
7514 mDsDx = tmpFloats[Matrix.MSCALE_X];
7515 mDtDx = tmpFloats[Matrix.MSKEW_X];
7516 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7517 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007518 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7519 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 int w = frame.width();
7521 int h = frame.height();
7522 mShownFrame.set(x, y, x+w, y+h);
7523
7524 // Now set the alpha... but because our current hardware
7525 // can't do alpha transformation on a non-opaque surface,
7526 // turn it off if we are running an animation that is also
7527 // transforming since it is more important to have that
7528 // animation be smooth.
7529 mShownAlpha = mAlpha;
7530 if (!mLimitedAlphaCompositing
7531 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7532 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7533 && x == frame.left && y == frame.top))) {
7534 //Log.i(TAG, "Applying alpha transform");
7535 if (selfTransformation) {
7536 mShownAlpha *= mTransformation.getAlpha();
7537 }
7538 if (attachedTransformation != null) {
7539 mShownAlpha *= attachedTransformation.getAlpha();
7540 }
7541 if (appTransformation != null) {
7542 mShownAlpha *= appTransformation.getAlpha();
7543 }
7544 } else {
7545 //Log.i(TAG, "Not applying alpha transform");
7546 }
Romain Guy06882f82009-06-10 13:36:04 -07007547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 if (localLOGV) Log.v(
7549 TAG, "Continuing animation in " + this +
7550 ": " + mShownFrame +
7551 ", alpha=" + mTransformation.getAlpha());
7552 return;
7553 }
Romain Guy06882f82009-06-10 13:36:04 -07007554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007556 if (mXOffset != 0 || mYOffset != 0) {
7557 mShownFrame.offset(mXOffset, mYOffset);
7558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 mShownAlpha = mAlpha;
7560 mDsDx = 1;
7561 mDtDx = 0;
7562 mDsDy = 0;
7563 mDtDy = 1;
7564 }
Romain Guy06882f82009-06-10 13:36:04 -07007565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 /**
7567 * Is this window visible? It is not visible if there is no
7568 * surface, or we are in the process of running an exit animation
7569 * that will remove the surface, or its app token has been hidden.
7570 */
7571 public boolean isVisibleLw() {
7572 final AppWindowToken atoken = mAppToken;
7573 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7574 && (atoken == null || !atoken.hiddenRequested)
7575 && !mExiting && !mDestroying;
7576 }
7577
7578 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007579 * Like {@link #isVisibleLw}, but also counts a window that is currently
7580 * "hidden" behind the keyguard as visible. This allows us to apply
7581 * things like window flags that impact the keyguard.
7582 * XXX I am starting to think we need to have ANOTHER visibility flag
7583 * for this "hidden behind keyguard" state rather than overloading
7584 * mPolicyVisibility. Ungh.
7585 */
7586 public boolean isVisibleOrBehindKeyguardLw() {
7587 final AppWindowToken atoken = mAppToken;
7588 return mSurface != null && !mAttachedHidden
7589 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7590 && !mExiting && !mDestroying;
7591 }
7592
7593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 * Is this window visible, ignoring its app token? It is not visible
7595 * if there is no surface, or we are in the process of running an exit animation
7596 * that will remove the surface.
7597 */
7598 public boolean isWinVisibleLw() {
7599 final AppWindowToken atoken = mAppToken;
7600 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7601 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7602 && !mExiting && !mDestroying;
7603 }
7604
7605 /**
7606 * The same as isVisible(), but follows the current hidden state of
7607 * the associated app token, not the pending requested hidden state.
7608 */
7609 boolean isVisibleNow() {
7610 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007611 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 }
7613
7614 /**
7615 * Same as isVisible(), but we also count it as visible between the
7616 * call to IWindowSession.add() and the first relayout().
7617 */
7618 boolean isVisibleOrAdding() {
7619 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007620 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7622 && mPolicyVisibility && !mAttachedHidden
7623 && (atoken == null || !atoken.hiddenRequested)
7624 && !mExiting && !mDestroying;
7625 }
7626
7627 /**
7628 * Is this window currently on-screen? It is on-screen either if it
7629 * is visible or it is currently running an animation before no longer
7630 * being visible.
7631 */
7632 boolean isOnScreen() {
7633 final AppWindowToken atoken = mAppToken;
7634 if (atoken != null) {
7635 return mSurface != null && mPolicyVisibility && !mDestroying
7636 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007637 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 } else {
7639 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007640 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 }
7642 }
Romain Guy06882f82009-06-10 13:36:04 -07007643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 /**
7645 * Like isOnScreen(), but we don't return true if the window is part
7646 * of a transition that has not yet been started.
7647 */
7648 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007649 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007650 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007651 return false;
7652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007654 final boolean animating = atoken != null
7655 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007657 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7658 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007659 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 }
7661
7662 /** Is the window or its container currently animating? */
7663 boolean isAnimating() {
7664 final WindowState attached = mAttachedWindow;
7665 final AppWindowToken atoken = mAppToken;
7666 return mAnimation != null
7667 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007668 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 (atoken.animation != null
7670 || atoken.inPendingTransaction));
7671 }
7672
7673 /** Is this window currently animating? */
7674 boolean isWindowAnimating() {
7675 return mAnimation != null;
7676 }
7677
7678 /**
7679 * Like isOnScreen, but returns false if the surface hasn't yet
7680 * been drawn.
7681 */
7682 public boolean isDisplayedLw() {
7683 final AppWindowToken atoken = mAppToken;
7684 return mSurface != null && mPolicyVisibility && !mDestroying
7685 && !mDrawPending && !mCommitDrawPending
7686 && ((!mAttachedHidden &&
7687 (atoken == null || !atoken.hiddenRequested))
7688 || mAnimating);
7689 }
7690
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007691 /**
7692 * Returns true if the window has a surface that it has drawn a
7693 * complete UI in to.
7694 */
7695 public boolean isDrawnLw() {
7696 final AppWindowToken atoken = mAppToken;
7697 return mSurface != null && !mDestroying
7698 && !mDrawPending && !mCommitDrawPending;
7699 }
7700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7702 boolean shownFrame, boolean onlyOpaque) {
7703 if (mSurface == null) {
7704 return false;
7705 }
7706 if (mAppToken != null && !mAppToken.appFullscreen) {
7707 return false;
7708 }
7709 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7710 return false;
7711 }
7712 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007713
7714 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7715 return frame.left <= mCompatibleScreenFrame.left &&
7716 frame.top <= mCompatibleScreenFrame.top &&
7717 frame.right >= mCompatibleScreenFrame.right &&
7718 frame.bottom >= mCompatibleScreenFrame.bottom;
7719 } else {
7720 return frame.left <= 0 && frame.top <= 0
7721 && frame.right >= screenWidth
7722 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 }
Romain Guy06882f82009-06-10 13:36:04 -07007725
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007726 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007727 * Return true if the window is opaque and fully drawn. This indicates
7728 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007729 */
7730 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007731 return (mAttrs.format == PixelFormat.OPAQUE
7732 || mAttrs.type == TYPE_WALLPAPER)
7733 && mSurface != null && mAnimation == null
7734 && (mAppToken == null || mAppToken.animation == null)
7735 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007736 }
7737
7738 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7739 return
7740 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007741 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7742 // only if it's visible
7743 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007744 // and only if the application fills the compatible screen
7745 mFrame.left <= mCompatibleScreenFrame.left &&
7746 mFrame.top <= mCompatibleScreenFrame.top &&
7747 mFrame.right >= mCompatibleScreenFrame.right &&
7748 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007749 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007750 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007751 }
7752
7753 boolean isFullscreen(int screenWidth, int screenHeight) {
7754 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007755 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007756 }
7757
7758 void removeLocked() {
7759 if (mAttachedWindow != null) {
7760 mAttachedWindow.mChildWindows.remove(this);
7761 }
7762 destroySurfaceLocked();
7763 mSession.windowRemovedLocked();
7764 try {
7765 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7766 } catch (RuntimeException e) {
7767 // Ignore if it has already been removed (usually because
7768 // we are doing this as part of processing a death note.)
7769 }
7770 }
7771
7772 private class DeathRecipient implements IBinder.DeathRecipient {
7773 public void binderDied() {
7774 try {
7775 synchronized(mWindowMap) {
7776 WindowState win = windowForClientLocked(mSession, mClient);
7777 Log.i(TAG, "WIN DEATH: " + win);
7778 if (win != null) {
7779 removeWindowLocked(mSession, win);
7780 }
7781 }
7782 } catch (IllegalArgumentException ex) {
7783 // This will happen if the window has already been
7784 // removed.
7785 }
7786 }
7787 }
7788
7789 /** Returns true if this window desires key events. */
7790 public final boolean canReceiveKeys() {
7791 return isVisibleOrAdding()
7792 && (mViewVisibility == View.VISIBLE)
7793 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7794 }
7795
7796 public boolean hasDrawnLw() {
7797 return mHasDrawn;
7798 }
7799
7800 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007801 return showLw(doAnimation, true);
7802 }
7803
7804 boolean showLw(boolean doAnimation, boolean requestAnim) {
7805 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7806 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007808 mPolicyVisibility = true;
7809 mPolicyVisibilityAfterAnim = true;
7810 if (doAnimation) {
7811 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7812 }
7813 if (requestAnim) {
7814 requestAnimationLocked(0);
7815 }
7816 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 }
7818
7819 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007820 return hideLw(doAnimation, true);
7821 }
7822
7823 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7825 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007826 if (!current) {
7827 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007829 if (doAnimation) {
7830 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7831 if (mAnimation == null) {
7832 doAnimation = false;
7833 }
7834 }
7835 if (doAnimation) {
7836 mPolicyVisibilityAfterAnim = false;
7837 } else {
7838 mPolicyVisibilityAfterAnim = false;
7839 mPolicyVisibility = false;
7840 }
7841 if (requestAnim) {
7842 requestAnimationLocked(0);
7843 }
7844 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 }
7846
7847 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007848 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007849
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007850 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7851 pw.print(" mClient="); pw.println(mClient.asBinder());
7852 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7853 if (mAttachedWindow != null || mLayoutAttached) {
7854 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7855 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7856 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007857 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7858 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7859 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007860 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7861 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007862 }
7863 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7864 pw.print(" mSubLayer="); pw.print(mSubLayer);
7865 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7866 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7867 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7868 pw.print("="); pw.print(mAnimLayer);
7869 pw.print(" mLastLayer="); pw.println(mLastLayer);
7870 if (mSurface != null) {
7871 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7872 }
7873 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7874 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7875 if (mAppToken != null) {
7876 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7877 }
7878 if (mTargetAppToken != null) {
7879 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7880 }
7881 pw.print(prefix); pw.print("mViewVisibility=0x");
7882 pw.print(Integer.toHexString(mViewVisibility));
7883 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007884 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7885 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007886 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7887 pw.print(prefix); pw.print("mPolicyVisibility=");
7888 pw.print(mPolicyVisibility);
7889 pw.print(" mPolicyVisibilityAfterAnim=");
7890 pw.print(mPolicyVisibilityAfterAnim);
7891 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7892 }
7893 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007894 pw.print(" h="); pw.println(mRequestedHeight);
7895 if (mXOffset != 0 || mYOffset != 0) {
7896 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7897 pw.print(" y="); pw.println(mYOffset);
7898 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007899 pw.print(prefix); pw.print("mGivenContentInsets=");
7900 mGivenContentInsets.printShortString(pw);
7901 pw.print(" mGivenVisibleInsets=");
7902 mGivenVisibleInsets.printShortString(pw);
7903 pw.println();
7904 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7905 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7906 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7907 }
7908 pw.print(prefix); pw.print("mShownFrame=");
7909 mShownFrame.printShortString(pw);
7910 pw.print(" last="); mLastShownFrame.printShortString(pw);
7911 pw.println();
7912 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7913 pw.print(" last="); mLastFrame.printShortString(pw);
7914 pw.println();
7915 pw.print(prefix); pw.print("mContainingFrame=");
7916 mContainingFrame.printShortString(pw);
7917 pw.print(" mDisplayFrame=");
7918 mDisplayFrame.printShortString(pw);
7919 pw.println();
7920 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7921 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7922 pw.println();
7923 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7924 pw.print(" last="); mLastContentInsets.printShortString(pw);
7925 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7926 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7927 pw.println();
7928 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7929 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7930 pw.print(" mAlpha="); pw.print(mAlpha);
7931 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7932 }
7933 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7934 || mAnimation != null) {
7935 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7936 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7937 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7938 pw.print(" mAnimation="); pw.println(mAnimation);
7939 }
7940 if (mHasTransformation || mHasLocalTransformation) {
7941 pw.print(prefix); pw.print("XForm: has=");
7942 pw.print(mHasTransformation);
7943 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7944 pw.print(" "); mTransformation.printShortString(pw);
7945 pw.println();
7946 }
7947 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7948 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7949 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7950 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7951 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7952 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7953 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7954 pw.print(" mDestroying="); pw.print(mDestroying);
7955 pw.print(" mRemoved="); pw.println(mRemoved);
7956 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007957 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007958 pw.print(prefix); pw.print("mOrientationChanging=");
7959 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007960 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7961 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007962 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007963 if (mHScale != 1 || mVScale != 1) {
7964 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7965 pw.print(" mVScale="); pw.println(mVScale);
7966 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007967 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007968 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7969 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 }
7972
7973 @Override
7974 public String toString() {
7975 return "Window{"
7976 + Integer.toHexString(System.identityHashCode(this))
7977 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7978 }
7979 }
Romain Guy06882f82009-06-10 13:36:04 -07007980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 // -------------------------------------------------------------
7982 // Window Token State
7983 // -------------------------------------------------------------
7984
7985 class WindowToken {
7986 // The actual token.
7987 final IBinder token;
7988
7989 // The type of window this token is for, as per WindowManager.LayoutParams.
7990 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007992 // Set if this token was explicitly added by a client, so should
7993 // not be removed when all windows are removed.
7994 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007995
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007996 // For printing.
7997 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007999 // If this is an AppWindowToken, this is non-null.
8000 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 // All of the windows associated with this token.
8003 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8004
8005 // Is key dispatching paused for this token?
8006 boolean paused = false;
8007
8008 // Should this token's windows be hidden?
8009 boolean hidden;
8010
8011 // Temporary for finding which tokens no longer have visible windows.
8012 boolean hasVisible;
8013
Dianne Hackborna8f60182009-09-01 19:01:50 -07008014 // Set to true when this token is in a pending transaction where it
8015 // will be shown.
8016 boolean waitingToShow;
8017
8018 // Set to true when this token is in a pending transaction where it
8019 // will be hidden.
8020 boolean waitingToHide;
8021
8022 // Set to true when this token is in a pending transaction where its
8023 // windows will be put to the bottom of the list.
8024 boolean sendingToBottom;
8025
8026 // Set to true when this token is in a pending transaction where its
8027 // windows will be put to the top of the list.
8028 boolean sendingToTop;
8029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 WindowToken(IBinder _token, int type, boolean _explicit) {
8031 token = _token;
8032 windowType = type;
8033 explicit = _explicit;
8034 }
8035
8036 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008037 pw.print(prefix); pw.print("token="); pw.println(token);
8038 pw.print(prefix); pw.print("windows="); pw.println(windows);
8039 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8040 pw.print(" hidden="); pw.print(hidden);
8041 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008042 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8043 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8044 pw.print(" waitingToHide="); pw.print(waitingToHide);
8045 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8046 pw.print(" sendingToTop="); pw.println(sendingToTop);
8047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 }
8049
8050 @Override
8051 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008052 if (stringName == null) {
8053 StringBuilder sb = new StringBuilder();
8054 sb.append("WindowToken{");
8055 sb.append(Integer.toHexString(System.identityHashCode(this)));
8056 sb.append(" token="); sb.append(token); sb.append('}');
8057 stringName = sb.toString();
8058 }
8059 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 }
8061 };
8062
8063 class AppWindowToken extends WindowToken {
8064 // Non-null only for application tokens.
8065 final IApplicationToken appToken;
8066
8067 // All of the windows and child windows that are included in this
8068 // application token. Note this list is NOT sorted!
8069 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8070
8071 int groupId = -1;
8072 boolean appFullscreen;
8073 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 // These are used for determining when all windows associated with
8076 // an activity have been drawn, so they can be made visible together
8077 // at the same time.
8078 int lastTransactionSequence = mTransactionSequence-1;
8079 int numInterestingWindows;
8080 int numDrawnWindows;
8081 boolean inPendingTransaction;
8082 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 // Is this token going to be hidden in a little while? If so, it
8085 // won't be taken into account for setting the screen orientation.
8086 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088 // Is this window's surface needed? This is almost like hidden, except
8089 // it will sometimes be true a little earlier: when the token has
8090 // been shown, but is still waiting for its app transition to execute
8091 // before making its windows shown.
8092 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 // Have we told the window clients to hide themselves?
8095 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 // Last visibility state we reported to the app token.
8098 boolean reportedVisible;
8099
8100 // Set to true when the token has been removed from the window mgr.
8101 boolean removed;
8102
8103 // Have we been asked to have this token keep the screen frozen?
8104 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 boolean animating;
8107 Animation animation;
8108 boolean hasTransformation;
8109 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 // Offset to the window of all layers in the token, for use by
8112 // AppWindowToken animations.
8113 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 // Information about an application starting window if displayed.
8116 StartingData startingData;
8117 WindowState startingWindow;
8118 View startingView;
8119 boolean startingDisplayed;
8120 boolean startingMoved;
8121 boolean firstWindowDrawn;
8122
8123 AppWindowToken(IApplicationToken _token) {
8124 super(_token.asBinder(),
8125 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8126 appWindowToken = this;
8127 appToken = _token;
8128 }
Romain Guy06882f82009-06-10 13:36:04 -07008129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 public void setAnimation(Animation anim) {
8131 if (localLOGV) Log.v(
8132 TAG, "Setting animation in " + this + ": " + anim);
8133 animation = anim;
8134 animating = false;
8135 anim.restrictDuration(MAX_ANIMATION_DURATION);
8136 anim.scaleCurrentDuration(mTransitionAnimationScale);
8137 int zorder = anim.getZAdjustment();
8138 int adj = 0;
8139 if (zorder == Animation.ZORDER_TOP) {
8140 adj = TYPE_LAYER_OFFSET;
8141 } else if (zorder == Animation.ZORDER_BOTTOM) {
8142 adj = -TYPE_LAYER_OFFSET;
8143 }
Romain Guy06882f82009-06-10 13:36:04 -07008144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 if (animLayerAdjustment != adj) {
8146 animLayerAdjustment = adj;
8147 updateLayers();
8148 }
8149 }
Romain Guy06882f82009-06-10 13:36:04 -07008150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 public void setDummyAnimation() {
8152 if (animation == null) {
8153 if (localLOGV) Log.v(
8154 TAG, "Setting dummy animation in " + this);
8155 animation = sDummyAnimation;
8156 }
8157 }
8158
8159 public void clearAnimation() {
8160 if (animation != null) {
8161 animation = null;
8162 animating = true;
8163 }
8164 }
Romain Guy06882f82009-06-10 13:36:04 -07008165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 void updateLayers() {
8167 final int N = allAppWindows.size();
8168 final int adj = animLayerAdjustment;
8169 for (int i=0; i<N; i++) {
8170 WindowState w = allAppWindows.get(i);
8171 w.mAnimLayer = w.mLayer + adj;
8172 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8173 + w.mAnimLayer);
8174 if (w == mInputMethodTarget) {
8175 setInputMethodAnimLayerAdjustment(adj);
8176 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008177 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008178 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 }
8181 }
Romain Guy06882f82009-06-10 13:36:04 -07008182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 void sendAppVisibilityToClients() {
8184 final int N = allAppWindows.size();
8185 for (int i=0; i<N; i++) {
8186 WindowState win = allAppWindows.get(i);
8187 if (win == startingWindow && clientHidden) {
8188 // Don't hide the starting window.
8189 continue;
8190 }
8191 try {
8192 if (DEBUG_VISIBILITY) Log.v(TAG,
8193 "Setting visibility of " + win + ": " + (!clientHidden));
8194 win.mClient.dispatchAppVisibility(!clientHidden);
8195 } catch (RemoteException e) {
8196 }
8197 }
8198 }
Romain Guy06882f82009-06-10 13:36:04 -07008199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 void showAllWindowsLocked() {
8201 final int NW = allAppWindows.size();
8202 for (int i=0; i<NW; i++) {
8203 WindowState w = allAppWindows.get(i);
8204 if (DEBUG_VISIBILITY) Log.v(TAG,
8205 "performing show on: " + w);
8206 w.performShowLocked();
8207 }
8208 }
Romain Guy06882f82009-06-10 13:36:04 -07008209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 // This must be called while inside a transaction.
8211 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8212 if (!mDisplayFrozen) {
8213 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 if (animation == sDummyAnimation) {
8216 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008217 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008218 // when it is really time to animate, this will be set to
8219 // a real animation and the next call will execute normally.
8220 return false;
8221 }
Romain Guy06882f82009-06-10 13:36:04 -07008222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8224 if (!animating) {
8225 if (DEBUG_ANIM) Log.v(
8226 TAG, "Starting animation in " + this +
8227 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8228 + " scale=" + mTransitionAnimationScale
8229 + " allDrawn=" + allDrawn + " animating=" + animating);
8230 animation.initialize(dw, dh, dw, dh);
8231 animation.setStartTime(currentTime);
8232 animating = true;
8233 }
8234 transformation.clear();
8235 final boolean more = animation.getTransformation(
8236 currentTime, transformation);
8237 if (DEBUG_ANIM) Log.v(
8238 TAG, "Stepped animation in " + this +
8239 ": more=" + more + ", xform=" + transformation);
8240 if (more) {
8241 // we're done!
8242 hasTransformation = true;
8243 return true;
8244 }
8245 if (DEBUG_ANIM) Log.v(
8246 TAG, "Finished animation in " + this +
8247 " @ " + currentTime);
8248 animation = null;
8249 }
8250 } else if (animation != null) {
8251 // If the display is frozen, and there is a pending animation,
8252 // clear it and make sure we run the cleanup code.
8253 animating = true;
8254 animation = null;
8255 }
8256
8257 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 if (!animating) {
8260 return false;
8261 }
8262
8263 clearAnimation();
8264 animating = false;
8265 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8266 moveInputMethodWindowsIfNeededLocked(true);
8267 }
Romain Guy06882f82009-06-10 13:36:04 -07008268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 if (DEBUG_ANIM) Log.v(
8270 TAG, "Animation done in " + this
8271 + ": reportedVisible=" + reportedVisible);
8272
8273 transformation.clear();
8274 if (animLayerAdjustment != 0) {
8275 animLayerAdjustment = 0;
8276 updateLayers();
8277 }
Romain Guy06882f82009-06-10 13:36:04 -07008278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 final int N = windows.size();
8280 for (int i=0; i<N; i++) {
8281 ((WindowState)windows.get(i)).finishExit();
8282 }
8283 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 return false;
8286 }
8287
8288 void updateReportedVisibilityLocked() {
8289 if (appToken == null) {
8290 return;
8291 }
Romain Guy06882f82009-06-10 13:36:04 -07008292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 int numInteresting = 0;
8294 int numVisible = 0;
8295 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8298 final int N = allAppWindows.size();
8299 for (int i=0; i<N; i++) {
8300 WindowState win = allAppWindows.get(i);
8301 if (win == startingWindow || win.mAppFreezing) {
8302 continue;
8303 }
8304 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008305 Log.v(TAG, "Win " + win + ": isDrawn="
8306 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008308 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 Log.v(TAG, "Not displayed: s=" + win.mSurface
8310 + " pv=" + win.mPolicyVisibility
8311 + " dp=" + win.mDrawPending
8312 + " cdp=" + win.mCommitDrawPending
8313 + " ah=" + win.mAttachedHidden
8314 + " th="
8315 + (win.mAppToken != null
8316 ? win.mAppToken.hiddenRequested : false)
8317 + " a=" + win.mAnimating);
8318 }
8319 }
8320 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008321 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 if (!win.isAnimating()) {
8323 numVisible++;
8324 }
8325 nowGone = false;
8326 } else if (win.isAnimating()) {
8327 nowGone = false;
8328 }
8329 }
Romain Guy06882f82009-06-10 13:36:04 -07008330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8332 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8333 + numInteresting + " visible=" + numVisible);
8334 if (nowVisible != reportedVisible) {
8335 if (DEBUG_VISIBILITY) Log.v(
8336 TAG, "Visibility changed in " + this
8337 + ": vis=" + nowVisible);
8338 reportedVisible = nowVisible;
8339 Message m = mH.obtainMessage(
8340 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8341 nowVisible ? 1 : 0,
8342 nowGone ? 1 : 0,
8343 this);
8344 mH.sendMessage(m);
8345 }
8346 }
Romain Guy06882f82009-06-10 13:36:04 -07008347
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008348 WindowState findMainWindow() {
8349 int j = windows.size();
8350 while (j > 0) {
8351 j--;
8352 WindowState win = windows.get(j);
8353 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8354 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8355 return win;
8356 }
8357 }
8358 return null;
8359 }
8360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 void dump(PrintWriter pw, String prefix) {
8362 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008363 if (appToken != null) {
8364 pw.print(prefix); pw.println("app=true");
8365 }
8366 if (allAppWindows.size() > 0) {
8367 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8368 }
8369 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008370 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008371 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8372 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8373 pw.print(" clientHidden="); pw.print(clientHidden);
8374 pw.print(" willBeHidden="); pw.print(willBeHidden);
8375 pw.print(" reportedVisible="); pw.println(reportedVisible);
8376 if (paused || freezingScreen) {
8377 pw.print(prefix); pw.print("paused="); pw.print(paused);
8378 pw.print(" freezingScreen="); pw.println(freezingScreen);
8379 }
8380 if (numInterestingWindows != 0 || numDrawnWindows != 0
8381 || inPendingTransaction || allDrawn) {
8382 pw.print(prefix); pw.print("numInterestingWindows=");
8383 pw.print(numInterestingWindows);
8384 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8385 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8386 pw.print(" allDrawn="); pw.println(allDrawn);
8387 }
8388 if (animating || animation != null) {
8389 pw.print(prefix); pw.print("animating="); pw.print(animating);
8390 pw.print(" animation="); pw.println(animation);
8391 }
8392 if (animLayerAdjustment != 0) {
8393 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8394 }
8395 if (hasTransformation) {
8396 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8397 pw.print(" transformation="); transformation.printShortString(pw);
8398 pw.println();
8399 }
8400 if (startingData != null || removed || firstWindowDrawn) {
8401 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8402 pw.print(" removed="); pw.print(removed);
8403 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8404 }
8405 if (startingWindow != null || startingView != null
8406 || startingDisplayed || startingMoved) {
8407 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8408 pw.print(" startingView="); pw.print(startingView);
8409 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8410 pw.print(" startingMoved"); pw.println(startingMoved);
8411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412 }
8413
8414 @Override
8415 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008416 if (stringName == null) {
8417 StringBuilder sb = new StringBuilder();
8418 sb.append("AppWindowToken{");
8419 sb.append(Integer.toHexString(System.identityHashCode(this)));
8420 sb.append(" token="); sb.append(token); sb.append('}');
8421 stringName = sb.toString();
8422 }
8423 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008424 }
8425 }
Romain Guy06882f82009-06-10 13:36:04 -07008426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 // -------------------------------------------------------------
8428 // DummyAnimation
8429 // -------------------------------------------------------------
8430
8431 // This is an animation that does nothing: it just immediately finishes
8432 // itself every time it is called. It is used as a stub animation in cases
8433 // where we want to synchronize multiple things that may be animating.
8434 static final class DummyAnimation extends Animation {
8435 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8436 return false;
8437 }
8438 }
8439 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441 // -------------------------------------------------------------
8442 // Async Handler
8443 // -------------------------------------------------------------
8444
8445 static final class StartingData {
8446 final String pkg;
8447 final int theme;
8448 final CharSequence nonLocalizedLabel;
8449 final int labelRes;
8450 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8453 int _labelRes, int _icon) {
8454 pkg = _pkg;
8455 theme = _theme;
8456 nonLocalizedLabel = _nonLocalizedLabel;
8457 labelRes = _labelRes;
8458 icon = _icon;
8459 }
8460 }
8461
8462 private final class H extends Handler {
8463 public static final int REPORT_FOCUS_CHANGE = 2;
8464 public static final int REPORT_LOSING_FOCUS = 3;
8465 public static final int ANIMATE = 4;
8466 public static final int ADD_STARTING = 5;
8467 public static final int REMOVE_STARTING = 6;
8468 public static final int FINISHED_STARTING = 7;
8469 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8471 public static final int HOLD_SCREEN_CHANGED = 12;
8472 public static final int APP_TRANSITION_TIMEOUT = 13;
8473 public static final int PERSIST_ANIMATION_SCALE = 14;
8474 public static final int FORCE_GC = 15;
8475 public static final int ENABLE_SCREEN = 16;
8476 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008477 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008481 public H() {
8482 }
Romain Guy06882f82009-06-10 13:36:04 -07008483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 @Override
8485 public void handleMessage(Message msg) {
8486 switch (msg.what) {
8487 case REPORT_FOCUS_CHANGE: {
8488 WindowState lastFocus;
8489 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008491 synchronized(mWindowMap) {
8492 lastFocus = mLastFocus;
8493 newFocus = mCurrentFocus;
8494 if (lastFocus == newFocus) {
8495 // Focus is not changing, so nothing to do.
8496 return;
8497 }
8498 mLastFocus = newFocus;
8499 //Log.i(TAG, "Focus moving from " + lastFocus
8500 // + " to " + newFocus);
8501 if (newFocus != null && lastFocus != null
8502 && !newFocus.isDisplayedLw()) {
8503 //Log.i(TAG, "Delaying loss of focus...");
8504 mLosingFocus.add(lastFocus);
8505 lastFocus = null;
8506 }
8507 }
8508
8509 if (lastFocus != newFocus) {
8510 //System.out.println("Changing focus from " + lastFocus
8511 // + " to " + newFocus);
8512 if (newFocus != null) {
8513 try {
8514 //Log.i(TAG, "Gaining focus: " + newFocus);
8515 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8516 } catch (RemoteException e) {
8517 // Ignore if process has died.
8518 }
8519 }
8520
8521 if (lastFocus != null) {
8522 try {
8523 //Log.i(TAG, "Losing focus: " + lastFocus);
8524 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8525 } catch (RemoteException e) {
8526 // Ignore if process has died.
8527 }
8528 }
8529 }
8530 } break;
8531
8532 case REPORT_LOSING_FOCUS: {
8533 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 synchronized(mWindowMap) {
8536 losers = mLosingFocus;
8537 mLosingFocus = new ArrayList<WindowState>();
8538 }
8539
8540 final int N = losers.size();
8541 for (int i=0; i<N; i++) {
8542 try {
8543 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8544 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8545 } catch (RemoteException e) {
8546 // Ignore if process has died.
8547 }
8548 }
8549 } break;
8550
8551 case ANIMATE: {
8552 synchronized(mWindowMap) {
8553 mAnimationPending = false;
8554 performLayoutAndPlaceSurfacesLocked();
8555 }
8556 } break;
8557
8558 case ADD_STARTING: {
8559 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8560 final StartingData sd = wtoken.startingData;
8561
8562 if (sd == null) {
8563 // Animation has been canceled... do nothing.
8564 return;
8565 }
Romain Guy06882f82009-06-10 13:36:04 -07008566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8568 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 View view = null;
8571 try {
8572 view = mPolicy.addStartingWindow(
8573 wtoken.token, sd.pkg,
8574 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8575 sd.icon);
8576 } catch (Exception e) {
8577 Log.w(TAG, "Exception when adding starting window", e);
8578 }
8579
8580 if (view != null) {
8581 boolean abort = false;
8582
8583 synchronized(mWindowMap) {
8584 if (wtoken.removed || wtoken.startingData == null) {
8585 // If the window was successfully added, then
8586 // we need to remove it.
8587 if (wtoken.startingWindow != null) {
8588 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8589 "Aborted starting " + wtoken
8590 + ": removed=" + wtoken.removed
8591 + " startingData=" + wtoken.startingData);
8592 wtoken.startingWindow = null;
8593 wtoken.startingData = null;
8594 abort = true;
8595 }
8596 } else {
8597 wtoken.startingView = view;
8598 }
8599 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8600 "Added starting " + wtoken
8601 + ": startingWindow="
8602 + wtoken.startingWindow + " startingView="
8603 + wtoken.startingView);
8604 }
8605
8606 if (abort) {
8607 try {
8608 mPolicy.removeStartingWindow(wtoken.token, view);
8609 } catch (Exception e) {
8610 Log.w(TAG, "Exception when removing starting window", e);
8611 }
8612 }
8613 }
8614 } break;
8615
8616 case REMOVE_STARTING: {
8617 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8618 IBinder token = null;
8619 View view = null;
8620 synchronized (mWindowMap) {
8621 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8622 + wtoken + ": startingWindow="
8623 + wtoken.startingWindow + " startingView="
8624 + wtoken.startingView);
8625 if (wtoken.startingWindow != null) {
8626 view = wtoken.startingView;
8627 token = wtoken.token;
8628 wtoken.startingData = null;
8629 wtoken.startingView = null;
8630 wtoken.startingWindow = null;
8631 }
8632 }
8633 if (view != null) {
8634 try {
8635 mPolicy.removeStartingWindow(token, view);
8636 } catch (Exception e) {
8637 Log.w(TAG, "Exception when removing starting window", e);
8638 }
8639 }
8640 } break;
8641
8642 case FINISHED_STARTING: {
8643 IBinder token = null;
8644 View view = null;
8645 while (true) {
8646 synchronized (mWindowMap) {
8647 final int N = mFinishedStarting.size();
8648 if (N <= 0) {
8649 break;
8650 }
8651 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8652
8653 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8654 "Finished starting " + wtoken
8655 + ": startingWindow=" + wtoken.startingWindow
8656 + " startingView=" + wtoken.startingView);
8657
8658 if (wtoken.startingWindow == null) {
8659 continue;
8660 }
8661
8662 view = wtoken.startingView;
8663 token = wtoken.token;
8664 wtoken.startingData = null;
8665 wtoken.startingView = null;
8666 wtoken.startingWindow = null;
8667 }
8668
8669 try {
8670 mPolicy.removeStartingWindow(token, view);
8671 } catch (Exception e) {
8672 Log.w(TAG, "Exception when removing starting window", e);
8673 }
8674 }
8675 } break;
8676
8677 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8678 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8679
8680 boolean nowVisible = msg.arg1 != 0;
8681 boolean nowGone = msg.arg2 != 0;
8682
8683 try {
8684 if (DEBUG_VISIBILITY) Log.v(
8685 TAG, "Reporting visible in " + wtoken
8686 + " visible=" + nowVisible
8687 + " gone=" + nowGone);
8688 if (nowVisible) {
8689 wtoken.appToken.windowsVisible();
8690 } else {
8691 wtoken.appToken.windowsGone();
8692 }
8693 } catch (RemoteException ex) {
8694 }
8695 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 case WINDOW_FREEZE_TIMEOUT: {
8698 synchronized (mWindowMap) {
8699 Log.w(TAG, "Window freeze timeout expired.");
8700 int i = mWindows.size();
8701 while (i > 0) {
8702 i--;
8703 WindowState w = (WindowState)mWindows.get(i);
8704 if (w.mOrientationChanging) {
8705 w.mOrientationChanging = false;
8706 Log.w(TAG, "Force clearing orientation change: " + w);
8707 }
8708 }
8709 performLayoutAndPlaceSurfacesLocked();
8710 }
8711 break;
8712 }
Romain Guy06882f82009-06-10 13:36:04 -07008713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 case HOLD_SCREEN_CHANGED: {
8715 Session oldHold;
8716 Session newHold;
8717 synchronized (mWindowMap) {
8718 oldHold = mLastReportedHold;
8719 newHold = (Session)msg.obj;
8720 mLastReportedHold = newHold;
8721 }
Romain Guy06882f82009-06-10 13:36:04 -07008722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008723 if (oldHold != newHold) {
8724 try {
8725 if (oldHold != null) {
8726 mBatteryStats.noteStopWakelock(oldHold.mUid,
8727 "window",
8728 BatteryStats.WAKE_TYPE_WINDOW);
8729 }
8730 if (newHold != null) {
8731 mBatteryStats.noteStartWakelock(newHold.mUid,
8732 "window",
8733 BatteryStats.WAKE_TYPE_WINDOW);
8734 }
8735 } catch (RemoteException e) {
8736 }
8737 }
8738 break;
8739 }
Romain Guy06882f82009-06-10 13:36:04 -07008740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 case APP_TRANSITION_TIMEOUT: {
8742 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008743 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8745 "*** APP TRANSITION TIMEOUT");
8746 mAppTransitionReady = true;
8747 mAppTransitionTimeout = true;
8748 performLayoutAndPlaceSurfacesLocked();
8749 }
8750 }
8751 break;
8752 }
Romain Guy06882f82009-06-10 13:36:04 -07008753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 case PERSIST_ANIMATION_SCALE: {
8755 Settings.System.putFloat(mContext.getContentResolver(),
8756 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8757 Settings.System.putFloat(mContext.getContentResolver(),
8758 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8759 break;
8760 }
Romain Guy06882f82009-06-10 13:36:04 -07008761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 case FORCE_GC: {
8763 synchronized(mWindowMap) {
8764 if (mAnimationPending) {
8765 // If we are animating, don't do the gc now but
8766 // delay a bit so we don't interrupt the animation.
8767 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8768 2000);
8769 return;
8770 }
8771 // If we are currently rotating the display, it will
8772 // schedule a new message when done.
8773 if (mDisplayFrozen) {
8774 return;
8775 }
8776 mFreezeGcPending = 0;
8777 }
8778 Runtime.getRuntime().gc();
8779 break;
8780 }
Romain Guy06882f82009-06-10 13:36:04 -07008781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 case ENABLE_SCREEN: {
8783 performEnableScreen();
8784 break;
8785 }
Romain Guy06882f82009-06-10 13:36:04 -07008786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 case APP_FREEZE_TIMEOUT: {
8788 synchronized (mWindowMap) {
8789 Log.w(TAG, "App freeze timeout expired.");
8790 int i = mAppTokens.size();
8791 while (i > 0) {
8792 i--;
8793 AppWindowToken tok = mAppTokens.get(i);
8794 if (tok.freezingScreen) {
8795 Log.w(TAG, "Force clearing freeze: " + tok);
8796 unsetAppFreezingScreenLocked(tok, true, true);
8797 }
8798 }
8799 }
8800 break;
8801 }
Romain Guy06882f82009-06-10 13:36:04 -07008802
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008803 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008804 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008805 sendNewConfiguration();
8806 }
8807 break;
8808 }
Romain Guy06882f82009-06-10 13:36:04 -07008809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 }
8811 }
8812 }
8813
8814 // -------------------------------------------------------------
8815 // IWindowManager API
8816 // -------------------------------------------------------------
8817
8818 public IWindowSession openSession(IInputMethodClient client,
8819 IInputContext inputContext) {
8820 if (client == null) throw new IllegalArgumentException("null client");
8821 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8822 return new Session(client, inputContext);
8823 }
8824
8825 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8826 synchronized (mWindowMap) {
8827 // The focus for the client is the window immediately below
8828 // where we would place the input method window.
8829 int idx = findDesiredInputMethodWindowIndexLocked(false);
8830 WindowState imFocus;
8831 if (idx > 0) {
8832 imFocus = (WindowState)mWindows.get(idx-1);
8833 if (imFocus != null) {
8834 if (imFocus.mSession.mClient != null &&
8835 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8836 return true;
8837 }
8838 }
8839 }
8840 }
8841 return false;
8842 }
Romain Guy06882f82009-06-10 13:36:04 -07008843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008844 // -------------------------------------------------------------
8845 // Internals
8846 // -------------------------------------------------------------
8847
8848 final WindowState windowForClientLocked(Session session, IWindow client) {
8849 return windowForClientLocked(session, client.asBinder());
8850 }
Romain Guy06882f82009-06-10 13:36:04 -07008851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008852 final WindowState windowForClientLocked(Session session, IBinder client) {
8853 WindowState win = mWindowMap.get(client);
8854 if (localLOGV) Log.v(
8855 TAG, "Looking up client " + client + ": " + win);
8856 if (win == null) {
8857 RuntimeException ex = new RuntimeException();
8858 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8859 return null;
8860 }
8861 if (session != null && win.mSession != session) {
8862 RuntimeException ex = new RuntimeException();
8863 Log.w(TAG, "Requested window " + client + " is in session " +
8864 win.mSession + ", not " + session, ex);
8865 return null;
8866 }
8867
8868 return win;
8869 }
8870
Dianne Hackborna8f60182009-09-01 19:01:50 -07008871 final void rebuildAppWindowListLocked() {
8872 int NW = mWindows.size();
8873 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008874 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008875 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008876
8877 // First remove all existing app windows.
8878 i=0;
8879 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008880 WindowState w = (WindowState)mWindows.get(i);
8881 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008882 WindowState win = (WindowState)mWindows.remove(i);
8883 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8884 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008885 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008886 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008887 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008888 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
8889 && lastWallpaper == i-1) {
8890 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008891 }
8892 i++;
8893 }
8894
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008895 // The wallpaper window(s) typically live at the bottom of the stack,
8896 // so skip them before adding app tokens.
8897 lastWallpaper++;
8898 i = lastWallpaper;
8899
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008900 // First add all of the exiting app tokens... these are no longer
8901 // in the main app list, but still have windows shown. We put them
8902 // in the back because now that the animation is over we no longer
8903 // will care about them.
8904 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008905 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008906 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8907 }
8908
8909 // And add in the still active app tokens in Z order.
8910 NT = mAppTokens.size();
8911 for (int j=0; j<NT; j++) {
8912 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008913 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008914
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008915 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008916 if (i != numRemoved) {
8917 Log.w(TAG, "Rebuild removed " + numRemoved
8918 + " windows but added " + i);
8919 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07008920 }
8921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008922 private final void assignLayersLocked() {
8923 int N = mWindows.size();
8924 int curBaseLayer = 0;
8925 int curLayer = 0;
8926 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928 for (i=0; i<N; i++) {
8929 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008930 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8931 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 curLayer += WINDOW_LAYER_MULTIPLIER;
8933 w.mLayer = curLayer;
8934 } else {
8935 curBaseLayer = curLayer = w.mBaseLayer;
8936 w.mLayer = curLayer;
8937 }
8938 if (w.mTargetAppToken != null) {
8939 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8940 } else if (w.mAppToken != null) {
8941 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8942 } else {
8943 w.mAnimLayer = w.mLayer;
8944 }
8945 if (w.mIsImWindow) {
8946 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008947 } else if (w.mIsWallpaper) {
8948 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008949 }
8950 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8951 + w.mAnimLayer);
8952 //System.out.println(
8953 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8954 }
8955 }
8956
8957 private boolean mInLayout = false;
8958 private final void performLayoutAndPlaceSurfacesLocked() {
8959 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008960 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 throw new RuntimeException("Recursive call!");
8962 }
8963 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8964 return;
8965 }
8966
8967 boolean recoveringMemory = false;
8968 if (mForceRemoves != null) {
8969 recoveringMemory = true;
8970 // Wait a little it for things to settle down, and off we go.
8971 for (int i=0; i<mForceRemoves.size(); i++) {
8972 WindowState ws = mForceRemoves.get(i);
8973 Log.i(TAG, "Force removing: " + ws);
8974 removeWindowInnerLocked(ws.mSession, ws);
8975 }
8976 mForceRemoves = null;
8977 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8978 Object tmp = new Object();
8979 synchronized (tmp) {
8980 try {
8981 tmp.wait(250);
8982 } catch (InterruptedException e) {
8983 }
8984 }
8985 }
Romain Guy06882f82009-06-10 13:36:04 -07008986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008987 mInLayout = true;
8988 try {
8989 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 int i = mPendingRemove.size()-1;
8992 if (i >= 0) {
8993 while (i >= 0) {
8994 WindowState w = mPendingRemove.get(i);
8995 removeWindowInnerLocked(w.mSession, w);
8996 i--;
8997 }
8998 mPendingRemove.clear();
8999
9000 mInLayout = false;
9001 assignLayersLocked();
9002 mLayoutNeeded = true;
9003 performLayoutAndPlaceSurfacesLocked();
9004
9005 } else {
9006 mInLayout = false;
9007 if (mLayoutNeeded) {
9008 requestAnimationLocked(0);
9009 }
9010 }
9011 } catch (RuntimeException e) {
9012 mInLayout = false;
9013 Log.e(TAG, "Unhandled exception while layout out windows", e);
9014 }
9015 }
9016
9017 private final void performLayoutLockedInner() {
9018 final int dw = mDisplay.getWidth();
9019 final int dh = mDisplay.getHeight();
9020
9021 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009022 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009023 int i;
9024
9025 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009026
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009027 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 mPolicy.beginLayoutLw(dw, dh);
9029
9030 // First perform layout of any root windows (not attached
9031 // to another window).
9032 int topAttached = -1;
9033 for (i = N-1; i >= 0; i--) {
9034 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009035
9036 // Don't do layout of a window if it is not visible, or
9037 // soon won't be visible, to avoid wasting time and funky
9038 // changes while a window is animating away.
9039 final AppWindowToken atoken = win.mAppToken;
9040 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009041 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009042 || win.mRootToken.hidden
9043 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009044 || win.mAttachedHidden
9045 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046
9047 // If this view is GONE, then skip it -- keep the current
9048 // frame, and let the caller know so they can ignore it
9049 // if they want. (We do the normal layout for INVISIBLE
9050 // windows, since that means "perform layout as normal,
9051 // just don't display").
9052 if (!gone || !win.mHaveFrame) {
9053 if (!win.mLayoutAttached) {
9054 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9055 } else {
9056 if (topAttached < 0) topAttached = i;
9057 }
9058 }
9059 }
Romain Guy06882f82009-06-10 13:36:04 -07009060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009061 // Now perform layout of attached windows, which usually
9062 // depend on the position of the window they are attached to.
9063 // XXX does not deal with windows that are attached to windows
9064 // that are themselves attached.
9065 for (i = topAttached; i >= 0; i--) {
9066 WindowState win = (WindowState) mWindows.get(i);
9067
9068 // If this view is GONE, then skip it -- keep the current
9069 // frame, and let the caller know so they can ignore it
9070 // if they want. (We do the normal layout for INVISIBLE
9071 // windows, since that means "perform layout as normal,
9072 // just don't display").
9073 if (win.mLayoutAttached) {
9074 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9075 || !win.mHaveFrame) {
9076 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9077 }
9078 }
9079 }
9080
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009081 int changes = mPolicy.finishLayoutLw();
9082 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9083 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9084 assignLayersLocked();
9085 }
9086 }
9087 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009088 mLayoutNeeded = false;
9089 } else if (repeats > 2) {
9090 Log.w(TAG, "Layout repeat aborted after too many iterations");
9091 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009092 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9093 Configuration newConfig = updateOrientationFromAppTokensLocked(
9094 null, null);
9095 if (newConfig != null) {
9096 mLayoutNeeded = true;
9097 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9098 }
9099 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009100 } else {
9101 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009102 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9103 Configuration newConfig = updateOrientationFromAppTokensLocked(
9104 null, null);
9105 if (newConfig != null) {
9106 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9107 }
9108 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009110 }
9111 }
Romain Guy06882f82009-06-10 13:36:04 -07009112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113 private final void performLayoutAndPlaceSurfacesLockedInner(
9114 boolean recoveringMemory) {
9115 final long currentTime = SystemClock.uptimeMillis();
9116 final int dw = mDisplay.getWidth();
9117 final int dh = mDisplay.getHeight();
9118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 int i;
9120
9121 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 if (mFxSession == null) {
9125 mFxSession = new SurfaceSession();
9126 }
Romain Guy06882f82009-06-10 13:36:04 -07009127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009128 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9129
9130 // Initialize state of exiting tokens.
9131 for (i=mExitingTokens.size()-1; i>=0; i--) {
9132 mExitingTokens.get(i).hasVisible = false;
9133 }
9134
9135 // Initialize state of exiting applications.
9136 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9137 mExitingAppTokens.get(i).hasVisible = false;
9138 }
9139
9140 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 boolean orientationChangeComplete = true;
9142 Session holdScreen = null;
9143 float screenBrightness = -1;
9144 boolean focusDisplayed = false;
9145 boolean animating = false;
9146
9147 Surface.openTransaction();
9148 try {
9149 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009150 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009151
9152 do {
9153 final int transactionSequence = ++mTransactionSequence;
9154
9155 // Update animations of all applications, including those
9156 // associated with exiting/removed apps
9157 boolean tokensAnimating = false;
9158 final int NAT = mAppTokens.size();
9159 for (i=0; i<NAT; i++) {
9160 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9161 tokensAnimating = true;
9162 }
9163 }
9164 final int NEAT = mExitingAppTokens.size();
9165 for (i=0; i<NEAT; i++) {
9166 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9167 tokensAnimating = true;
9168 }
9169 }
9170
9171 animating = tokensAnimating;
9172 restart = false;
9173
9174 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009175 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009176 boolean focusMayChange = false;
9177 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178
9179 mPolicy.beginAnimationLw(dw, dh);
9180
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009181 final int N = mWindows.size();
9182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009183 for (i=N-1; i>=0; i--) {
9184 WindowState w = (WindowState)mWindows.get(i);
9185
9186 final WindowManager.LayoutParams attrs = w.mAttrs;
9187
9188 if (w.mSurface != null) {
9189 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009190 if (w.commitFinishDrawingLocked(currentTime)) {
9191 if ((w.mAttrs.flags
9192 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009193 if (DEBUG_WALLPAPER) Log.v(TAG,
9194 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009195 wallpaperMayChange = true;
9196 }
9197 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009198
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009199 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9201 animating = true;
9202 //w.dump(" ");
9203 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009204 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9205 wallpaperMayChange = true;
9206 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009207
9208 if (mPolicy.doesForceHide(w, attrs)) {
9209 if (!wasAnimating && animating) {
9210 wallpaperForceHidingChanged = true;
9211 focusMayChange = true;
9212 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9213 forceHiding = true;
9214 }
9215 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9216 boolean changed;
9217 if (forceHiding) {
9218 changed = w.hideLw(false, false);
9219 } else {
9220 changed = w.showLw(false, false);
9221 if (changed && wallpaperForceHidingChanged
9222 && w.isReadyForDisplay()) {
9223 // Assume we will need to animate. If
9224 // we don't (because the wallpaper will
9225 // stay with the lock screen), then we will
9226 // clean up later.
9227 Animation a = mPolicy.createForceHideEnterAnimation();
9228 if (a != null) {
9229 w.setAnimation(a);
9230 }
9231 }
9232 }
9233 if (changed && (attrs.flags
9234 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9235 wallpaperMayChange = true;
9236 }
9237 }
9238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 mPolicy.animatingWindowLw(w, attrs);
9240 }
9241
9242 final AppWindowToken atoken = w.mAppToken;
9243 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9244 if (atoken.lastTransactionSequence != transactionSequence) {
9245 atoken.lastTransactionSequence = transactionSequence;
9246 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9247 atoken.startingDisplayed = false;
9248 }
9249 if ((w.isOnScreen() || w.mAttrs.type
9250 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9251 && !w.mExiting && !w.mDestroying) {
9252 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009253 Log.v(TAG, "Eval win " + w + ": isDrawn="
9254 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009256 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257 Log.v(TAG, "Not displayed: s=" + w.mSurface
9258 + " pv=" + w.mPolicyVisibility
9259 + " dp=" + w.mDrawPending
9260 + " cdp=" + w.mCommitDrawPending
9261 + " ah=" + w.mAttachedHidden
9262 + " th=" + atoken.hiddenRequested
9263 + " a=" + w.mAnimating);
9264 }
9265 }
9266 if (w != atoken.startingWindow) {
9267 if (!atoken.freezingScreen || !w.mAppFreezing) {
9268 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009269 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 atoken.numDrawnWindows++;
9271 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9272 "tokenMayBeDrawn: " + atoken
9273 + " freezingScreen=" + atoken.freezingScreen
9274 + " mAppFreezing=" + w.mAppFreezing);
9275 tokenMayBeDrawn = true;
9276 }
9277 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009278 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 atoken.startingDisplayed = true;
9280 }
9281 }
9282 } else if (w.mReadyToShow) {
9283 w.performShowLocked();
9284 }
9285 }
9286
9287 if (mPolicy.finishAnimationLw()) {
9288 restart = true;
9289 }
9290
9291 if (tokenMayBeDrawn) {
9292 // See if any windows have been drawn, so they (and others
9293 // associated with them) can now be shown.
9294 final int NT = mTokenList.size();
9295 for (i=0; i<NT; i++) {
9296 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9297 if (wtoken == null) {
9298 continue;
9299 }
9300 if (wtoken.freezingScreen) {
9301 int numInteresting = wtoken.numInterestingWindows;
9302 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9303 if (DEBUG_VISIBILITY) Log.v(TAG,
9304 "allDrawn: " + wtoken
9305 + " interesting=" + numInteresting
9306 + " drawn=" + wtoken.numDrawnWindows);
9307 wtoken.showAllWindowsLocked();
9308 unsetAppFreezingScreenLocked(wtoken, false, true);
9309 orientationChangeComplete = true;
9310 }
9311 } else if (!wtoken.allDrawn) {
9312 int numInteresting = wtoken.numInterestingWindows;
9313 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9314 if (DEBUG_VISIBILITY) Log.v(TAG,
9315 "allDrawn: " + wtoken
9316 + " interesting=" + numInteresting
9317 + " drawn=" + wtoken.numDrawnWindows);
9318 wtoken.allDrawn = true;
9319 restart = true;
9320
9321 // We can now show all of the drawn windows!
9322 if (!mOpeningApps.contains(wtoken)) {
9323 wtoken.showAllWindowsLocked();
9324 }
9325 }
9326 }
9327 }
9328 }
9329
9330 // If we are ready to perform an app transition, check through
9331 // all of the app tokens to be shown and see if they are ready
9332 // to go.
9333 if (mAppTransitionReady) {
9334 int NN = mOpeningApps.size();
9335 boolean goodToGo = true;
9336 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9337 "Checking " + NN + " opening apps (frozen="
9338 + mDisplayFrozen + " timeout="
9339 + mAppTransitionTimeout + ")...");
9340 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9341 // If the display isn't frozen, wait to do anything until
9342 // all of the apps are ready. Otherwise just go because
9343 // we'll unfreeze the display when everyone is ready.
9344 for (i=0; i<NN && goodToGo; i++) {
9345 AppWindowToken wtoken = mOpeningApps.get(i);
9346 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9347 "Check opening app" + wtoken + ": allDrawn="
9348 + wtoken.allDrawn + " startingDisplayed="
9349 + wtoken.startingDisplayed);
9350 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9351 && !wtoken.startingMoved) {
9352 goodToGo = false;
9353 }
9354 }
9355 }
9356 if (goodToGo) {
9357 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9358 int transit = mNextAppTransition;
9359 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009360 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009362 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009364 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009365 mAppTransitionTimeout = false;
9366 mStartingIconInTransition = false;
9367 mSkipAppTransitionAnimation = false;
9368
9369 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9370
Dianne Hackborna8f60182009-09-01 19:01:50 -07009371 // If there are applications waiting to come to the
9372 // top of the stack, now is the time to move their windows.
9373 // (Note that we don't do apps going to the bottom
9374 // here -- we want to keep their windows in the old
9375 // Z-order until the animation completes.)
9376 if (mToTopApps.size() > 0) {
9377 NN = mAppTokens.size();
9378 for (i=0; i<NN; i++) {
9379 AppWindowToken wtoken = mAppTokens.get(i);
9380 if (wtoken.sendingToTop) {
9381 wtoken.sendingToTop = false;
9382 moveAppWindowsLocked(wtoken, NN, false);
9383 }
9384 }
9385 mToTopApps.clear();
9386 }
9387
Dianne Hackborn25994b42009-09-04 14:21:19 -07009388 WindowState oldWallpaper = mWallpaperTarget;
9389
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009390 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009391 wallpaperMayChange = false;
9392
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009393 // The top-most window will supply the layout params,
9394 // and we will determine it below.
9395 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009396 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009397 int bestAnimLayer = -1;
9398
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009399 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009400 "New wallpaper target=" + mWallpaperTarget
9401 + ", lower target=" + mLowerWallpaperTarget
9402 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009403 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009404 // Do a first pass through the tokens for two
9405 // things:
9406 // (1) Determine if both the closing and opening
9407 // app token sets are wallpaper targets, in which
9408 // case special animations are needed
9409 // (since the wallpaper needs to stay static
9410 // behind them).
9411 // (2) Find the layout params of the top-most
9412 // application window in the tokens, which is
9413 // what will control the animation theme.
9414 final int NC = mClosingApps.size();
9415 NN = NC + mOpeningApps.size();
9416 for (i=0; i<NN; i++) {
9417 AppWindowToken wtoken;
9418 int mode;
9419 if (i < NC) {
9420 wtoken = mClosingApps.get(i);
9421 mode = 1;
9422 } else {
9423 wtoken = mOpeningApps.get(i-NC);
9424 mode = 2;
9425 }
9426 if (mLowerWallpaperTarget != null) {
9427 if (mLowerWallpaperTarget.mAppToken == wtoken
9428 || mUpperWallpaperTarget.mAppToken == wtoken) {
9429 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009430 }
9431 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009432 if (wtoken.appFullscreen) {
9433 WindowState ws = wtoken.findMainWindow();
9434 if (ws != null) {
9435 // If this is a compatibility mode
9436 // window, we will always use its anim.
9437 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9438 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009439 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009440 bestAnimLayer = Integer.MAX_VALUE;
9441 } else if (ws.mLayer > bestAnimLayer) {
9442 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009443 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009444 bestAnimLayer = ws.mLayer;
9445 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009446 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009447 }
9448 }
9449
Dianne Hackborn25994b42009-09-04 14:21:19 -07009450 if (foundWallpapers == 3) {
9451 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9452 "Wallpaper animation!");
9453 switch (transit) {
9454 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9455 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9456 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9457 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9458 break;
9459 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9460 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9461 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9462 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9463 break;
9464 }
9465 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9466 "New transit: " + transit);
9467 } else if (oldWallpaper != null) {
9468 // We are transitioning from an activity with
9469 // a wallpaper to one without.
9470 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9471 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9472 "New transit away from wallpaper: " + transit);
9473 } else if (mWallpaperTarget != null) {
9474 // We are transitioning from an activity without
9475 // a wallpaper to now showing the wallpaper
9476 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9477 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9478 "New transit into wallpaper: " + transit);
9479 }
9480
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009481 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9482 mLastEnterAnimToken = animToken;
9483 mLastEnterAnimParams = animLp;
9484 } else if (mLastEnterAnimParams != null) {
9485 animLp = mLastEnterAnimParams;
9486 mLastEnterAnimToken = null;
9487 mLastEnterAnimParams = null;
9488 }
9489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490 NN = mOpeningApps.size();
9491 for (i=0; i<NN; i++) {
9492 AppWindowToken wtoken = mOpeningApps.get(i);
9493 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9494 "Now opening app" + wtoken);
9495 wtoken.reportedVisible = false;
9496 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009497 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009498 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009500 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 wtoken.showAllWindowsLocked();
9502 }
9503 NN = mClosingApps.size();
9504 for (i=0; i<NN; i++) {
9505 AppWindowToken wtoken = mClosingApps.get(i);
9506 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9507 "Now closing app" + wtoken);
9508 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009509 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009510 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009512 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 // Force the allDrawn flag, because we want to start
9514 // this guy's animations regardless of whether it's
9515 // gotten drawn.
9516 wtoken.allDrawn = true;
9517 }
9518
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009519 mNextAppTransitionPackage = null;
9520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009521 mOpeningApps.clear();
9522 mClosingApps.clear();
9523
9524 // This has changed the visibility of windows, so perform
9525 // a new layout to get them all up-to-date.
9526 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009527 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9528 assignLayersLocked();
9529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009530 performLayoutLockedInner();
9531 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009532 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533
9534 restart = true;
9535 }
9536 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009537
Dianne Hackborna8f60182009-09-01 19:01:50 -07009538 if (!animating && mAppTransitionRunning) {
9539 // We have finished the animation of an app transition. To do
9540 // this, we have delayed a lot of operations like showing and
9541 // hiding apps, moving apps in Z-order, etc. The app token list
9542 // reflects the correct Z-order, but the window list may now
9543 // be out of sync with it. So here we will just rebuild the
9544 // entire app window list. Fun!
9545 mAppTransitionRunning = false;
9546 // Clear information about apps that were moving.
9547 mToBottomApps.clear();
9548
9549 rebuildAppWindowListLocked();
9550 restart = true;
9551 moveInputMethodWindowsIfNeededLocked(false);
9552 wallpaperMayChange = true;
9553 mLayoutNeeded = true;
9554 }
9555
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009556 int adjResult = 0;
9557
9558 if (wallpaperForceHidingChanged) {
9559 // At this point, there was a window with a wallpaper that
9560 // was force hiding other windows behind it, but now it
9561 // is going away. This may be simple -- just animate
9562 // away the wallpaper and its window -- or it may be
9563 // hard -- the wallpaper now needs to be shown behind
9564 // something that was hidden.
9565 WindowState oldWallpaper = mWallpaperTarget;
9566 adjResult = adjustWallpaperWindowsLocked();
9567 wallpaperMayChange = false;
9568 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9569 + " NEW: " + mWallpaperTarget);
9570 if (mLowerWallpaperTarget == null) {
9571 // Whoops, we don't need a special wallpaper animation.
9572 // Clear them out.
9573 forceHiding = false;
9574 for (i=N-1; i>=0; i--) {
9575 WindowState w = (WindowState)mWindows.get(i);
9576 if (w.mSurface != null) {
9577 final WindowManager.LayoutParams attrs = w.mAttrs;
9578 if (mPolicy.doesForceHide(w, attrs)) {
9579 forceHiding = true;
9580 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9581 if (!w.mAnimating) {
9582 // We set the animation above so it
9583 // is not yet running.
9584 w.clearAnimation();
9585 }
9586 }
9587 }
9588 }
9589 }
9590 }
9591
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009592 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009593 if (DEBUG_WALLPAPER) Log.v(TAG,
9594 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009595 adjResult = adjustWallpaperWindowsLocked();
9596 }
9597
9598 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9599 if (DEBUG_WALLPAPER) Log.v(TAG,
9600 "Wallpaper layer changed: assigning layers + relayout");
9601 restart = true;
9602 mLayoutNeeded = true;
9603 assignLayersLocked();
9604 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9605 if (DEBUG_WALLPAPER) Log.v(TAG,
9606 "Wallpaper visibility changed: relayout");
9607 restart = true;
9608 mLayoutNeeded = true;
9609 }
9610
9611 if (focusMayChange) {
9612 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009613 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009614 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009615 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009616 }
9617
9618 if (mLayoutNeeded) {
9619 restart = true;
9620 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009621 }
9622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 } while (restart);
9624
9625 // THIRD LOOP: Update the surfaces of all windows.
9626
9627 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9628
9629 boolean obscured = false;
9630 boolean blurring = false;
9631 boolean dimming = false;
9632 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009633 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009634 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009635
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009636 final int N = mWindows.size();
9637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009638 for (i=N-1; i>=0; i--) {
9639 WindowState w = (WindowState)mWindows.get(i);
9640
9641 boolean displayed = false;
9642 final WindowManager.LayoutParams attrs = w.mAttrs;
9643 final int attrFlags = attrs.flags;
9644
9645 if (w.mSurface != null) {
9646 w.computeShownFrameLocked();
9647 if (localLOGV) Log.v(
9648 TAG, "Placing surface #" + i + " " + w.mSurface
9649 + ": new=" + w.mShownFrame + ", old="
9650 + w.mLastShownFrame);
9651
9652 boolean resize;
9653 int width, height;
9654 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9655 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9656 w.mLastRequestedHeight != w.mRequestedHeight;
9657 // for a scaled surface, we just want to use
9658 // the requested size.
9659 width = w.mRequestedWidth;
9660 height = w.mRequestedHeight;
9661 w.mLastRequestedWidth = width;
9662 w.mLastRequestedHeight = height;
9663 w.mLastShownFrame.set(w.mShownFrame);
9664 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009665 if (SHOW_TRANSACTIONS) Log.i(
9666 TAG, " SURFACE " + w.mSurface
9667 + ": POS " + w.mShownFrame.left
9668 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9670 } catch (RuntimeException e) {
9671 Log.w(TAG, "Error positioning surface in " + w, e);
9672 if (!recoveringMemory) {
9673 reclaimSomeSurfaceMemoryLocked(w, "position");
9674 }
9675 }
9676 } else {
9677 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9678 width = w.mShownFrame.width();
9679 height = w.mShownFrame.height();
9680 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 }
9682
9683 if (resize) {
9684 if (width < 1) width = 1;
9685 if (height < 1) height = 1;
9686 if (w.mSurface != null) {
9687 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009688 if (SHOW_TRANSACTIONS) Log.i(
9689 TAG, " SURFACE " + w.mSurface + ": POS "
9690 + w.mShownFrame.left + ","
9691 + w.mShownFrame.top + " SIZE "
9692 + w.mShownFrame.width() + "x"
9693 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 w.mSurface.setSize(width, height);
9695 w.mSurface.setPosition(w.mShownFrame.left,
9696 w.mShownFrame.top);
9697 } catch (RuntimeException e) {
9698 // If something goes wrong with the surface (such
9699 // as running out of memory), don't take down the
9700 // entire system.
9701 Log.e(TAG, "Failure updating surface of " + w
9702 + "size=(" + width + "x" + height
9703 + "), pos=(" + w.mShownFrame.left
9704 + "," + w.mShownFrame.top + ")", e);
9705 if (!recoveringMemory) {
9706 reclaimSomeSurfaceMemoryLocked(w, "size");
9707 }
9708 }
9709 }
9710 }
9711 if (!w.mAppFreezing) {
9712 w.mContentInsetsChanged =
9713 !w.mLastContentInsets.equals(w.mContentInsets);
9714 w.mVisibleInsetsChanged =
9715 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009716 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 || w.mContentInsetsChanged
9718 || w.mVisibleInsetsChanged) {
9719 w.mLastFrame.set(w.mFrame);
9720 w.mLastContentInsets.set(w.mContentInsets);
9721 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009722 // If the screen is currently frozen, then keep
9723 // it frozen until this window draws at its new
9724 // orientation.
9725 if (mDisplayFrozen) {
9726 if (DEBUG_ORIENTATION) Log.v(TAG,
9727 "Resizing while display frozen: " + w);
9728 w.mOrientationChanging = true;
9729 if (mWindowsFreezingScreen) {
9730 mWindowsFreezingScreen = true;
9731 // XXX should probably keep timeout from
9732 // when we first froze the display.
9733 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9734 mH.sendMessageDelayed(mH.obtainMessage(
9735 H.WINDOW_FREEZE_TIMEOUT), 2000);
9736 }
9737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009738 // If the orientation is changing, then we need to
9739 // hold off on unfreezing the display until this
9740 // window has been redrawn; to do that, we need
9741 // to go through the process of getting informed
9742 // by the application when it has finished drawing.
9743 if (w.mOrientationChanging) {
9744 if (DEBUG_ORIENTATION) Log.v(TAG,
9745 "Orientation start waiting for draw in "
9746 + w + ", surface " + w.mSurface);
9747 w.mDrawPending = true;
9748 w.mCommitDrawPending = false;
9749 w.mReadyToShow = false;
9750 if (w.mAppToken != null) {
9751 w.mAppToken.allDrawn = false;
9752 }
9753 }
Romain Guy06882f82009-06-10 13:36:04 -07009754 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 "Resizing window " + w + " to " + w.mFrame);
9756 mResizingWindows.add(w);
9757 } else if (w.mOrientationChanging) {
9758 if (!w.mDrawPending && !w.mCommitDrawPending) {
9759 if (DEBUG_ORIENTATION) Log.v(TAG,
9760 "Orientation not waiting for draw in "
9761 + w + ", surface " + w.mSurface);
9762 w.mOrientationChanging = false;
9763 }
9764 }
9765 }
9766
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009767 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009768 if (!w.mLastHidden) {
9769 //dump();
9770 w.mLastHidden = true;
9771 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009772 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 if (w.mSurface != null) {
9774 try {
9775 w.mSurface.hide();
9776 } catch (RuntimeException e) {
9777 Log.w(TAG, "Exception hiding surface in " + w);
9778 }
9779 }
9780 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9781 }
9782 // If we are waiting for this window to handle an
9783 // orientation change, well, it is hidden, so
9784 // doesn't really matter. Note that this does
9785 // introduce a potential glitch if the window
9786 // becomes unhidden before it has drawn for the
9787 // new orientation.
9788 if (w.mOrientationChanging) {
9789 w.mOrientationChanging = false;
9790 if (DEBUG_ORIENTATION) Log.v(TAG,
9791 "Orientation change skips hidden " + w);
9792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 } else if (w.mLastLayer != w.mAnimLayer
9794 || w.mLastAlpha != w.mShownAlpha
9795 || w.mLastDsDx != w.mDsDx
9796 || w.mLastDtDx != w.mDtDx
9797 || w.mLastDsDy != w.mDsDy
9798 || w.mLastDtDy != w.mDtDy
9799 || w.mLastHScale != w.mHScale
9800 || w.mLastVScale != w.mVScale
9801 || w.mLastHidden) {
9802 displayed = true;
9803 w.mLastAlpha = w.mShownAlpha;
9804 w.mLastLayer = w.mAnimLayer;
9805 w.mLastDsDx = w.mDsDx;
9806 w.mLastDtDx = w.mDtDx;
9807 w.mLastDsDy = w.mDsDy;
9808 w.mLastDtDy = w.mDtDy;
9809 w.mLastHScale = w.mHScale;
9810 w.mLastVScale = w.mVScale;
9811 if (SHOW_TRANSACTIONS) Log.i(
9812 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009813 + w.mShownAlpha + " layer=" + w.mAnimLayer
9814 + " matrix=[" + (w.mDsDx*w.mHScale)
9815 + "," + (w.mDtDx*w.mVScale)
9816 + "][" + (w.mDsDy*w.mHScale)
9817 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 if (w.mSurface != null) {
9819 try {
9820 w.mSurface.setAlpha(w.mShownAlpha);
9821 w.mSurface.setLayer(w.mAnimLayer);
9822 w.mSurface.setMatrix(
9823 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9824 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9825 } catch (RuntimeException e) {
9826 Log.w(TAG, "Error updating surface in " + w, e);
9827 if (!recoveringMemory) {
9828 reclaimSomeSurfaceMemoryLocked(w, "update");
9829 }
9830 }
9831 }
9832
9833 if (w.mLastHidden && !w.mDrawPending
9834 && !w.mCommitDrawPending
9835 && !w.mReadyToShow) {
9836 if (SHOW_TRANSACTIONS) Log.i(
9837 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9838 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9839 + " during relayout");
9840 if (showSurfaceRobustlyLocked(w)) {
9841 w.mHasDrawn = true;
9842 w.mLastHidden = false;
9843 } else {
9844 w.mOrientationChanging = false;
9845 }
9846 }
9847 if (w.mSurface != null) {
9848 w.mToken.hasVisible = true;
9849 }
9850 } else {
9851 displayed = true;
9852 }
9853
9854 if (displayed) {
9855 if (!covered) {
9856 if (attrs.width == LayoutParams.FILL_PARENT
9857 && attrs.height == LayoutParams.FILL_PARENT) {
9858 covered = true;
9859 }
9860 }
9861 if (w.mOrientationChanging) {
9862 if (w.mDrawPending || w.mCommitDrawPending) {
9863 orientationChangeComplete = false;
9864 if (DEBUG_ORIENTATION) Log.v(TAG,
9865 "Orientation continue waiting for draw in " + w);
9866 } else {
9867 w.mOrientationChanging = false;
9868 if (DEBUG_ORIENTATION) Log.v(TAG,
9869 "Orientation change complete in " + w);
9870 }
9871 }
9872 w.mToken.hasVisible = true;
9873 }
9874 } else if (w.mOrientationChanging) {
9875 if (DEBUG_ORIENTATION) Log.v(TAG,
9876 "Orientation change skips hidden " + w);
9877 w.mOrientationChanging = false;
9878 }
9879
9880 final boolean canBeSeen = w.isDisplayedLw();
9881
9882 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9883 focusDisplayed = true;
9884 }
9885
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009886 final boolean obscuredChanged = w.mObscured != obscured;
9887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009889 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890 if (w.mSurface != null) {
9891 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9892 holdScreen = w.mSession;
9893 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009894 if (!syswin && w.mAttrs.screenBrightness >= 0
9895 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009896 screenBrightness = w.mAttrs.screenBrightness;
9897 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009898 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9899 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9900 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9901 syswin = true;
9902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009904
Dianne Hackborn25994b42009-09-04 14:21:19 -07009905 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9906 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 // This window completely covers everything behind it,
9908 // so we want to leave all of them as unblurred (for
9909 // performance reasons).
9910 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009911 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9912 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009913 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009914 obscured = true;
9915 if (mBackgroundFillerSurface == null) {
9916 try {
9917 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9918 0, dw, dh,
9919 PixelFormat.OPAQUE,
9920 Surface.FX_SURFACE_NORMAL);
9921 } catch (Exception e) {
9922 Log.e(TAG, "Exception creating filler surface", e);
9923 }
9924 }
9925 try {
9926 mBackgroundFillerSurface.setPosition(0, 0);
9927 mBackgroundFillerSurface.setSize(dw, dh);
9928 // Using the same layer as Dim because they will never be shown at the
9929 // same time.
9930 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9931 mBackgroundFillerSurface.show();
9932 } catch (RuntimeException e) {
9933 Log.e(TAG, "Exception showing filler surface");
9934 }
9935 backgroundFillerShown = true;
9936 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009937 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9939 if (localLOGV) Log.v(TAG, "Win " + w
9940 + ": blurring=" + blurring
9941 + " obscured=" + obscured
9942 + " displayed=" + displayed);
9943 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9944 if (!dimming) {
9945 //Log.i(TAG, "DIM BEHIND: " + w);
9946 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009947 if (mDimAnimator == null) {
9948 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009950 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009952 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 }
9954 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9955 if (!blurring) {
9956 //Log.i(TAG, "BLUR BEHIND: " + w);
9957 blurring = true;
9958 mBlurShown = true;
9959 if (mBlurSurface == null) {
9960 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9961 + mBlurSurface + ": CREATE");
9962 try {
Romain Guy06882f82009-06-10 13:36:04 -07009963 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 -1, 16, 16,
9965 PixelFormat.OPAQUE,
9966 Surface.FX_SURFACE_BLUR);
9967 } catch (Exception e) {
9968 Log.e(TAG, "Exception creating Blur surface", e);
9969 }
9970 }
9971 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9972 + mBlurSurface + ": SHOW pos=(0,0) (" +
9973 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9974 if (mBlurSurface != null) {
9975 mBlurSurface.setPosition(0, 0);
9976 mBlurSurface.setSize(dw, dh);
9977 try {
9978 mBlurSurface.show();
9979 } catch (RuntimeException e) {
9980 Log.w(TAG, "Failure showing blur surface", e);
9981 }
9982 }
9983 }
9984 mBlurSurface.setLayer(w.mAnimLayer-2);
9985 }
9986 }
9987 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009988
9989 if (obscuredChanged && mWallpaperTarget == w) {
9990 // This is the wallpaper target and its obscured state
9991 // changed... make sure the current wallaper's visibility
9992 // has been updated accordingly.
9993 updateWallpaperVisibilityLocked();
9994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009996
9997 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9998 mBackgroundFillerShown = false;
9999 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10000 try {
10001 mBackgroundFillerSurface.hide();
10002 } catch (RuntimeException e) {
10003 Log.e(TAG, "Exception hiding filler surface", e);
10004 }
10005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010007 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10008 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010009 }
Romain Guy06882f82009-06-10 13:36:04 -070010010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 if (!blurring && mBlurShown) {
10012 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10013 + ": HIDE");
10014 try {
10015 mBlurSurface.hide();
10016 } catch (IllegalArgumentException e) {
10017 Log.w(TAG, "Illegal argument exception hiding blur surface");
10018 }
10019 mBlurShown = false;
10020 }
10021
10022 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10023 } catch (RuntimeException e) {
10024 Log.e(TAG, "Unhandled exception in Window Manager", e);
10025 }
10026
10027 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10030 "With display frozen, orientationChangeComplete="
10031 + orientationChangeComplete);
10032 if (orientationChangeComplete) {
10033 if (mWindowsFreezingScreen) {
10034 mWindowsFreezingScreen = false;
10035 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10036 }
10037 if (mAppsFreezingScreen == 0) {
10038 stopFreezingDisplayLocked();
10039 }
10040 }
Romain Guy06882f82009-06-10 13:36:04 -070010041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 i = mResizingWindows.size();
10043 if (i > 0) {
10044 do {
10045 i--;
10046 WindowState win = mResizingWindows.get(i);
10047 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010048 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10049 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 win.mClient.resized(win.mFrame.width(),
10051 win.mFrame.height(), win.mLastContentInsets,
10052 win.mLastVisibleInsets, win.mDrawPending);
10053 win.mContentInsetsChanged = false;
10054 win.mVisibleInsetsChanged = false;
10055 } catch (RemoteException e) {
10056 win.mOrientationChanging = false;
10057 }
10058 } while (i > 0);
10059 mResizingWindows.clear();
10060 }
Romain Guy06882f82009-06-10 13:36:04 -070010061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010063 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010064 i = mDestroySurface.size();
10065 if (i > 0) {
10066 do {
10067 i--;
10068 WindowState win = mDestroySurface.get(i);
10069 win.mDestroying = false;
10070 if (mInputMethodWindow == win) {
10071 mInputMethodWindow = null;
10072 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010073 if (win == mWallpaperTarget) {
10074 wallpaperDestroyed = true;
10075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 win.destroySurfaceLocked();
10077 } while (i > 0);
10078 mDestroySurface.clear();
10079 }
10080
10081 // Time to remove any exiting tokens?
10082 for (i=mExitingTokens.size()-1; i>=0; i--) {
10083 WindowToken token = mExitingTokens.get(i);
10084 if (!token.hasVisible) {
10085 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010086 if (token.windowType == TYPE_WALLPAPER) {
10087 mWallpaperTokens.remove(token);
10088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010089 }
10090 }
10091
10092 // Time to remove any exiting applications?
10093 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10094 AppWindowToken token = mExitingAppTokens.get(i);
10095 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010096 // Make sure there is no animation running on this token,
10097 // so any windows associated with it will be removed as
10098 // soon as their animations are complete
10099 token.animation = null;
10100 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 mAppTokens.remove(token);
10102 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010103 if (mLastEnterAnimToken == token) {
10104 mLastEnterAnimToken = null;
10105 mLastEnterAnimParams = null;
10106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 }
10108 }
10109
Dianne Hackborna8f60182009-09-01 19:01:50 -070010110 boolean needRelayout = false;
10111
10112 if (!animating && mAppTransitionRunning) {
10113 // We have finished the animation of an app transition. To do
10114 // this, we have delayed a lot of operations like showing and
10115 // hiding apps, moving apps in Z-order, etc. The app token list
10116 // reflects the correct Z-order, but the window list may now
10117 // be out of sync with it. So here we will just rebuild the
10118 // entire app window list. Fun!
10119 mAppTransitionRunning = false;
10120 needRelayout = true;
10121 rebuildAppWindowListLocked();
10122 // Clear information about apps that were moving.
10123 mToBottomApps.clear();
10124 }
10125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 if (focusDisplayed) {
10127 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10128 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010129 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010130 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010131 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010132 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010133 requestAnimationLocked(0);
10134 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10136 }
10137 mQueue.setHoldScreenLocked(holdScreen != null);
10138 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10139 mPowerManager.setScreenBrightnessOverride(-1);
10140 } else {
10141 mPowerManager.setScreenBrightnessOverride((int)
10142 (screenBrightness * Power.BRIGHTNESS_ON));
10143 }
10144 if (holdScreen != mHoldingScreenOn) {
10145 mHoldingScreenOn = holdScreen;
10146 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10147 mH.sendMessage(m);
10148 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010149
10150 if (mTurnOnScreen) {
10151 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10152 LocalPowerManager.BUTTON_EVENT, true);
10153 mTurnOnScreen = false;
10154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 }
10156
10157 void requestAnimationLocked(long delay) {
10158 if (!mAnimationPending) {
10159 mAnimationPending = true;
10160 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10161 }
10162 }
Romain Guy06882f82009-06-10 13:36:04 -070010163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 /**
10165 * Have the surface flinger show a surface, robustly dealing with
10166 * error conditions. In particular, if there is not enough memory
10167 * to show the surface, then we will try to get rid of other surfaces
10168 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 * @return Returns true if the surface was successfully shown.
10171 */
10172 boolean showSurfaceRobustlyLocked(WindowState win) {
10173 try {
10174 if (win.mSurface != null) {
10175 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010176 if (win.mTurnOnScreen) {
10177 win.mTurnOnScreen = false;
10178 mTurnOnScreen = true;
10179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 }
10181 return true;
10182 } catch (RuntimeException e) {
10183 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10184 }
Romain Guy06882f82009-06-10 13:36:04 -070010185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010186 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 return false;
10189 }
Romain Guy06882f82009-06-10 13:36:04 -070010190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10192 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10195 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010197 if (mForceRemoves == null) {
10198 mForceRemoves = new ArrayList<WindowState>();
10199 }
Romain Guy06882f82009-06-10 13:36:04 -070010200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010201 long callingIdentity = Binder.clearCallingIdentity();
10202 try {
10203 // There was some problem... first, do a sanity check of the
10204 // window list to make sure we haven't left any dangling surfaces
10205 // around.
10206 int N = mWindows.size();
10207 boolean leakedSurface = false;
10208 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10209 for (int i=0; i<N; i++) {
10210 WindowState ws = (WindowState)mWindows.get(i);
10211 if (ws.mSurface != null) {
10212 if (!mSessions.contains(ws.mSession)) {
10213 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10214 + ws + " surface=" + ws.mSurface
10215 + " token=" + win.mToken
10216 + " pid=" + ws.mSession.mPid
10217 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010218 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 ws.mSurface = null;
10220 mForceRemoves.add(ws);
10221 i--;
10222 N--;
10223 leakedSurface = true;
10224 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10225 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10226 + ws + " surface=" + ws.mSurface
10227 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010228 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010229 ws.mSurface = null;
10230 leakedSurface = true;
10231 }
10232 }
10233 }
Romain Guy06882f82009-06-10 13:36:04 -070010234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 boolean killedApps = false;
10236 if (!leakedSurface) {
10237 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10238 SparseIntArray pidCandidates = new SparseIntArray();
10239 for (int i=0; i<N; i++) {
10240 WindowState ws = (WindowState)mWindows.get(i);
10241 if (ws.mSurface != null) {
10242 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10243 }
10244 }
10245 if (pidCandidates.size() > 0) {
10246 int[] pids = new int[pidCandidates.size()];
10247 for (int i=0; i<pids.length; i++) {
10248 pids[i] = pidCandidates.keyAt(i);
10249 }
10250 try {
10251 if (mActivityManager.killPidsForMemory(pids)) {
10252 killedApps = true;
10253 }
10254 } catch (RemoteException e) {
10255 }
10256 }
10257 }
Romain Guy06882f82009-06-10 13:36:04 -070010258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 if (leakedSurface || killedApps) {
10260 // We managed to reclaim some memory, so get rid of the trouble
10261 // surface and ask the app to request another one.
10262 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10263 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010264 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 win.mSurface = null;
10266 }
Romain Guy06882f82009-06-10 13:36:04 -070010267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010268 try {
10269 win.mClient.dispatchGetNewSurface();
10270 } catch (RemoteException e) {
10271 }
10272 }
10273 } finally {
10274 Binder.restoreCallingIdentity(callingIdentity);
10275 }
10276 }
Romain Guy06882f82009-06-10 13:36:04 -070010277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 private boolean updateFocusedWindowLocked(int mode) {
10279 WindowState newFocus = computeFocusedWindowLocked();
10280 if (mCurrentFocus != newFocus) {
10281 // This check makes sure that we don't already have the focus
10282 // change message pending.
10283 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10284 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10285 if (localLOGV) Log.v(
10286 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10287 final WindowState oldFocus = mCurrentFocus;
10288 mCurrentFocus = newFocus;
10289 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010291 final WindowState imWindow = mInputMethodWindow;
10292 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010293 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010295 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10296 mLayoutNeeded = true;
10297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10299 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010300 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10301 // Client will do the layout, but we need to assign layers
10302 // for handleNewWindowLocked() below.
10303 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 }
10305 }
Romain Guy06882f82009-06-10 13:36:04 -070010306
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010307 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10308 mKeyWaiter.handleNewWindowLocked(newFocus);
10309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 return true;
10311 }
10312 return false;
10313 }
10314
10315 private WindowState computeFocusedWindowLocked() {
10316 WindowState result = null;
10317 WindowState win;
10318
10319 int i = mWindows.size() - 1;
10320 int nextAppIndex = mAppTokens.size()-1;
10321 WindowToken nextApp = nextAppIndex >= 0
10322 ? mAppTokens.get(nextAppIndex) : null;
10323
10324 while (i >= 0) {
10325 win = (WindowState)mWindows.get(i);
10326
10327 if (localLOGV || DEBUG_FOCUS) Log.v(
10328 TAG, "Looking for focus: " + i
10329 + " = " + win
10330 + ", flags=" + win.mAttrs.flags
10331 + ", canReceive=" + win.canReceiveKeys());
10332
10333 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 // If this window's application has been removed, just skip it.
10336 if (thisApp != null && thisApp.removed) {
10337 i--;
10338 continue;
10339 }
Romain Guy06882f82009-06-10 13:36:04 -070010340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 // If there is a focused app, don't allow focus to go to any
10342 // windows below it. If this is an application window, step
10343 // through the app tokens until we find its app.
10344 if (thisApp != null && nextApp != null && thisApp != nextApp
10345 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10346 int origAppIndex = nextAppIndex;
10347 while (nextAppIndex > 0) {
10348 if (nextApp == mFocusedApp) {
10349 // Whoops, we are below the focused app... no focus
10350 // for you!
10351 if (localLOGV || DEBUG_FOCUS) Log.v(
10352 TAG, "Reached focused app: " + mFocusedApp);
10353 return null;
10354 }
10355 nextAppIndex--;
10356 nextApp = mAppTokens.get(nextAppIndex);
10357 if (nextApp == thisApp) {
10358 break;
10359 }
10360 }
10361 if (thisApp != nextApp) {
10362 // Uh oh, the app token doesn't exist! This shouldn't
10363 // happen, but if it does we can get totally hosed...
10364 // so restart at the original app.
10365 nextAppIndex = origAppIndex;
10366 nextApp = mAppTokens.get(nextAppIndex);
10367 }
10368 }
10369
10370 // Dispatch to this window if it is wants key events.
10371 if (win.canReceiveKeys()) {
10372 if (DEBUG_FOCUS) Log.v(
10373 TAG, "Found focus @ " + i + " = " + win);
10374 result = win;
10375 break;
10376 }
10377
10378 i--;
10379 }
10380
10381 return result;
10382 }
10383
10384 private void startFreezingDisplayLocked() {
10385 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010386 // Freezing the display also suspends key event delivery, to
10387 // keep events from going astray while the display is reconfigured.
10388 // If someone has changed orientation again while the screen is
10389 // still frozen, the events will continue to be blocked while the
10390 // successive orientation change is processed. To prevent spurious
10391 // ANRs, we reset the event dispatch timeout in this case.
10392 synchronized (mKeyWaiter) {
10393 mKeyWaiter.mWasFrozen = true;
10394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 return;
10396 }
Romain Guy06882f82009-06-10 13:36:04 -070010397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 long now = SystemClock.uptimeMillis();
10401 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10402 if (mFreezeGcPending != 0) {
10403 if (now > (mFreezeGcPending+1000)) {
10404 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10405 mH.removeMessages(H.FORCE_GC);
10406 Runtime.getRuntime().gc();
10407 mFreezeGcPending = now;
10408 }
10409 } else {
10410 mFreezeGcPending = now;
10411 }
Romain Guy06882f82009-06-10 13:36:04 -070010412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010414 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10415 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010416 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 mAppTransitionReady = true;
10418 }
Romain Guy06882f82009-06-10 13:36:04 -070010419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 if (PROFILE_ORIENTATION) {
10421 File file = new File("/data/system/frozen");
10422 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10423 }
10424 Surface.freezeDisplay(0);
10425 }
Romain Guy06882f82009-06-10 13:36:04 -070010426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010427 private void stopFreezingDisplayLocked() {
10428 if (!mDisplayFrozen) {
10429 return;
10430 }
Romain Guy06882f82009-06-10 13:36:04 -070010431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 mDisplayFrozen = false;
10433 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10434 if (PROFILE_ORIENTATION) {
10435 Debug.stopMethodTracing();
10436 }
10437 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010438
Chris Tate2ad63a92009-03-25 17:36:48 -070010439 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10440 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 synchronized (mKeyWaiter) {
10442 mKeyWaiter.mWasFrozen = true;
10443 mKeyWaiter.notifyAll();
10444 }
10445
10446 // A little kludge: a lot could have happened while the
10447 // display was frozen, so now that we are coming back we
10448 // do a gc so that any remote references the system
10449 // processes holds on others can be released if they are
10450 // no longer needed.
10451 mH.removeMessages(H.FORCE_GC);
10452 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10453 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 mScreenFrozenLock.release();
10456 }
Romain Guy06882f82009-06-10 13:36:04 -070010457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 @Override
10459 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10460 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10461 != PackageManager.PERMISSION_GRANTED) {
10462 pw.println("Permission Denial: can't dump WindowManager from from pid="
10463 + Binder.getCallingPid()
10464 + ", uid=" + Binder.getCallingUid());
10465 return;
10466 }
Romain Guy06882f82009-06-10 13:36:04 -070010467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010468 synchronized(mWindowMap) {
10469 pw.println("Current Window Manager state:");
10470 for (int i=mWindows.size()-1; i>=0; i--) {
10471 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010472 pw.print(" Window #"); pw.print(i); pw.print(' ');
10473 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 w.dump(pw, " ");
10475 }
10476 if (mInputMethodDialogs.size() > 0) {
10477 pw.println(" ");
10478 pw.println(" Input method dialogs:");
10479 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10480 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010481 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482 }
10483 }
10484 if (mPendingRemove.size() > 0) {
10485 pw.println(" ");
10486 pw.println(" Remove pending for:");
10487 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10488 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010489 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10490 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 w.dump(pw, " ");
10492 }
10493 }
10494 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10495 pw.println(" ");
10496 pw.println(" Windows force removing:");
10497 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10498 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010499 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10500 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 w.dump(pw, " ");
10502 }
10503 }
10504 if (mDestroySurface.size() > 0) {
10505 pw.println(" ");
10506 pw.println(" Windows waiting to destroy their surface:");
10507 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10508 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010509 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10510 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 w.dump(pw, " ");
10512 }
10513 }
10514 if (mLosingFocus.size() > 0) {
10515 pw.println(" ");
10516 pw.println(" Windows losing focus:");
10517 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10518 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010519 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10520 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 w.dump(pw, " ");
10522 }
10523 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010524 if (mResizingWindows.size() > 0) {
10525 pw.println(" ");
10526 pw.println(" Windows waiting to resize:");
10527 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10528 WindowState w = mResizingWindows.get(i);
10529 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10530 pw.print(w); pw.println(":");
10531 w.dump(pw, " ");
10532 }
10533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 if (mSessions.size() > 0) {
10535 pw.println(" ");
10536 pw.println(" All active sessions:");
10537 Iterator<Session> it = mSessions.iterator();
10538 while (it.hasNext()) {
10539 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010540 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 s.dump(pw, " ");
10542 }
10543 }
10544 if (mTokenMap.size() > 0) {
10545 pw.println(" ");
10546 pw.println(" All tokens:");
10547 Iterator<WindowToken> it = mTokenMap.values().iterator();
10548 while (it.hasNext()) {
10549 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010550 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 token.dump(pw, " ");
10552 }
10553 }
10554 if (mTokenList.size() > 0) {
10555 pw.println(" ");
10556 pw.println(" Window token list:");
10557 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010558 pw.print(" #"); pw.print(i); pw.print(": ");
10559 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 }
10561 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010562 if (mWallpaperTokens.size() > 0) {
10563 pw.println(" ");
10564 pw.println(" Wallpaper tokens:");
10565 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10566 WindowToken token = mWallpaperTokens.get(i);
10567 pw.print(" Wallpaper #"); pw.print(i);
10568 pw.print(' '); pw.print(token); pw.println(':');
10569 token.dump(pw, " ");
10570 }
10571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 if (mAppTokens.size() > 0) {
10573 pw.println(" ");
10574 pw.println(" Application tokens in Z order:");
10575 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010576 pw.print(" App #"); pw.print(i); pw.print(": ");
10577 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 }
10579 }
10580 if (mFinishedStarting.size() > 0) {
10581 pw.println(" ");
10582 pw.println(" Finishing start of application tokens:");
10583 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10584 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010585 pw.print(" Finished Starting #"); pw.print(i);
10586 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 token.dump(pw, " ");
10588 }
10589 }
10590 if (mExitingTokens.size() > 0) {
10591 pw.println(" ");
10592 pw.println(" Exiting tokens:");
10593 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10594 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010595 pw.print(" Exiting #"); pw.print(i);
10596 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 token.dump(pw, " ");
10598 }
10599 }
10600 if (mExitingAppTokens.size() > 0) {
10601 pw.println(" ");
10602 pw.println(" Exiting application tokens:");
10603 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10604 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010605 pw.print(" Exiting App #"); pw.print(i);
10606 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 token.dump(pw, " ");
10608 }
10609 }
10610 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010611 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10612 pw.print(" mLastFocus="); pw.println(mLastFocus);
10613 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10614 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10615 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010616 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010617 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10618 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10619 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10620 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010621 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10622 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10623 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10624 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10625 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010626 if (mDimAnimator != null) {
10627 mDimAnimator.printTo(pw);
10628 } else {
10629 pw.print( " no DimAnimator ");
10630 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010631 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010632 pw.print(mInputMethodAnimLayerAdjustment);
10633 pw.print(" mWallpaperAnimLayerAdjustment=");
10634 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010635 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10636 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010637 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10638 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10639 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10640 pw.print(" mRotation="); pw.print(mRotation);
10641 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10642 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10643 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10644 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10645 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10646 pw.print(" mNextAppTransition=0x");
10647 pw.print(Integer.toHexString(mNextAppTransition));
10648 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010649 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010650 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010651 if (mNextAppTransitionPackage != null) {
10652 pw.print(" mNextAppTransitionPackage=");
10653 pw.print(mNextAppTransitionPackage);
10654 pw.print(", mNextAppTransitionEnter=0x");
10655 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10656 pw.print(", mNextAppTransitionExit=0x");
10657 pw.print(Integer.toHexString(mNextAppTransitionExit));
10658 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010659 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10660 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010661 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10662 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10663 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10664 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010665 if (mOpeningApps.size() > 0) {
10666 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10667 }
10668 if (mClosingApps.size() > 0) {
10669 pw.print(" mClosingApps="); pw.println(mClosingApps);
10670 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010671 if (mToTopApps.size() > 0) {
10672 pw.print(" mToTopApps="); pw.println(mToTopApps);
10673 }
10674 if (mToBottomApps.size() > 0) {
10675 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10676 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010677 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10678 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010680 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10681 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10682 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10683 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10684 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10685 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 }
10687 }
10688
10689 public void monitor() {
10690 synchronized (mWindowMap) { }
10691 synchronized (mKeyguardDisabled) { }
10692 synchronized (mKeyWaiter) { }
10693 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010694
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010695 public void virtualKeyFeedback(KeyEvent event) {
10696 mPolicy.keyFeedbackFromInput(event);
10697 }
10698
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010699 /**
10700 * DimAnimator class that controls the dim animation. This holds the surface and
10701 * all state used for dim animation.
10702 */
10703 private static class DimAnimator {
10704 Surface mDimSurface;
10705 boolean mDimShown = false;
10706 float mDimCurrentAlpha;
10707 float mDimTargetAlpha;
10708 float mDimDeltaPerMs;
10709 long mLastDimAnimTime;
10710
10711 DimAnimator (SurfaceSession session) {
10712 if (mDimSurface == null) {
10713 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10714 + mDimSurface + ": CREATE");
10715 try {
10716 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10717 Surface.FX_SURFACE_DIM);
10718 } catch (Exception e) {
10719 Log.e(TAG, "Exception creating Dim surface", e);
10720 }
10721 }
10722 }
10723
10724 /**
10725 * Show the dim surface.
10726 */
10727 void show(int dw, int dh) {
10728 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10729 dw + "x" + dh + ")");
10730 mDimShown = true;
10731 try {
10732 mDimSurface.setPosition(0, 0);
10733 mDimSurface.setSize(dw, dh);
10734 mDimSurface.show();
10735 } catch (RuntimeException e) {
10736 Log.w(TAG, "Failure showing dim surface", e);
10737 }
10738 }
10739
10740 /**
10741 * Set's the dim surface's layer and update dim parameters that will be used in
10742 * {@link updateSurface} after all windows are examined.
10743 */
10744 void updateParameters(WindowState w, long currentTime) {
10745 mDimSurface.setLayer(w.mAnimLayer-1);
10746
10747 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010748 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10749 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010750 if (mDimTargetAlpha != target) {
10751 // If the desired dim level has changed, then
10752 // start an animation to it.
10753 mLastDimAnimTime = currentTime;
10754 long duration = (w.mAnimating && w.mAnimation != null)
10755 ? w.mAnimation.computeDurationHint()
10756 : DEFAULT_DIM_DURATION;
10757 if (target > mDimTargetAlpha) {
10758 // This is happening behind the activity UI,
10759 // so we can make it run a little longer to
10760 // give a stronger impression without disrupting
10761 // the user.
10762 duration *= DIM_DURATION_MULTIPLIER;
10763 }
10764 if (duration < 1) {
10765 // Don't divide by zero
10766 duration = 1;
10767 }
10768 mDimTargetAlpha = target;
10769 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10770 }
10771 }
10772
10773 /**
10774 * Updating the surface's alpha. Returns true if the animation continues, or returns
10775 * false when the animation is finished and the dim surface is hidden.
10776 */
10777 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10778 if (!dimming) {
10779 if (mDimTargetAlpha != 0) {
10780 mLastDimAnimTime = currentTime;
10781 mDimTargetAlpha = 0;
10782 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10783 }
10784 }
10785
10786 boolean animating = false;
10787 if (mLastDimAnimTime != 0) {
10788 mDimCurrentAlpha += mDimDeltaPerMs
10789 * (currentTime-mLastDimAnimTime);
10790 boolean more = true;
10791 if (displayFrozen) {
10792 // If the display is frozen, there is no reason to animate.
10793 more = false;
10794 } else if (mDimDeltaPerMs > 0) {
10795 if (mDimCurrentAlpha > mDimTargetAlpha) {
10796 more = false;
10797 }
10798 } else if (mDimDeltaPerMs < 0) {
10799 if (mDimCurrentAlpha < mDimTargetAlpha) {
10800 more = false;
10801 }
10802 } else {
10803 more = false;
10804 }
10805
10806 // Do we need to continue animating?
10807 if (more) {
10808 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10809 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10810 mLastDimAnimTime = currentTime;
10811 mDimSurface.setAlpha(mDimCurrentAlpha);
10812 animating = true;
10813 } else {
10814 mDimCurrentAlpha = mDimTargetAlpha;
10815 mLastDimAnimTime = 0;
10816 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10817 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10818 mDimSurface.setAlpha(mDimCurrentAlpha);
10819 if (!dimming) {
10820 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10821 + ": HIDE");
10822 try {
10823 mDimSurface.hide();
10824 } catch (RuntimeException e) {
10825 Log.w(TAG, "Illegal argument exception hiding dim surface");
10826 }
10827 mDimShown = false;
10828 }
10829 }
10830 }
10831 return animating;
10832 }
10833
10834 public void printTo(PrintWriter pw) {
10835 pw.print(" mDimShown="); pw.print(mDimShown);
10836 pw.print(" current="); pw.print(mDimCurrentAlpha);
10837 pw.print(" target="); pw.print(mDimTargetAlpha);
10838 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10839 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10840 }
10841 }
10842
10843 /**
10844 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10845 * This is used for opening/closing transition for apps in compatible mode.
10846 */
10847 private static class FadeInOutAnimation extends Animation {
10848 int mWidth;
10849 boolean mFadeIn;
10850
10851 public FadeInOutAnimation(boolean fadeIn) {
10852 setInterpolator(new AccelerateInterpolator());
10853 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10854 mFadeIn = fadeIn;
10855 }
10856
10857 @Override
10858 protected void applyTransformation(float interpolatedTime, Transformation t) {
10859 float x = interpolatedTime;
10860 if (!mFadeIn) {
10861 x = 1.0f - x; // reverse the interpolation for fade out
10862 }
10863 if (x < 0.5) {
10864 // move the window out of the screen.
10865 t.getMatrix().setTranslate(mWidth, 0);
10866 } else {
10867 t.getMatrix().setTranslate(0, 0);// show
10868 t.setAlpha((x - 0.5f) * 2);
10869 }
10870 }
10871
10872 @Override
10873 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10874 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10875 mWidth = width;
10876 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010877
10878 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010879 public int getZAdjustment() {
10880 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010881 }
10882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883}