blob: e743513ab924f64939402decb0581aa26a6ebe84 [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 Hackborn73e92b42009-10-15 14:29:19 -0700433 float mLastWallpaperX = -1;
434 float mLastWallpaperY = -1;
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) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001467 if (mWallpaperTarget.mWallpaperX >= 0) {
1468 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1469 }
1470 if (mWallpaperTarget.mWallpaperY >= 0) {
1471 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1472 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001473 }
1474
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001475 // Start stepping backwards from here, ensuring that our wallpaper windows
1476 // are correctly placed.
1477 int curTokenIndex = mWallpaperTokens.size();
1478 while (curTokenIndex > 0) {
1479 curTokenIndex--;
1480 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001481 if (token.hidden == visible) {
1482 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1483 token.hidden = !visible;
1484 // Need to do a layout to ensure the wallpaper now has the
1485 // correct size.
1486 mLayoutNeeded = true;
1487 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001488
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001489 int curWallpaperIndex = token.windows.size();
1490 while (curWallpaperIndex > 0) {
1491 curWallpaperIndex--;
1492 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001493
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001494 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001495 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001496 }
1497
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001498 // First, make sure the client has the current visibility
1499 // state.
1500 if (wallpaper.mWallpaperVisible != visible) {
1501 wallpaper.mWallpaperVisible = visible;
1502 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001503 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001504 "Setting visibility of wallpaper " + wallpaper
1505 + ": " + visible);
1506 wallpaper.mClient.dispatchAppVisibility(visible);
1507 } catch (RemoteException e) {
1508 }
1509 }
1510
1511 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001512 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1513 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001514
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001515 // First, if this window is at the current index, then all
1516 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001517 if (wallpaper == foundW) {
1518 foundI--;
1519 foundW = foundI > 0
1520 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001521 continue;
1522 }
1523
1524 // The window didn't match... the current wallpaper window,
1525 // wherever it is, is in the wrong place, so make sure it is
1526 // not in the list.
1527 int oldIndex = localmWindows.indexOf(wallpaper);
1528 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001529 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1530 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001531 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001532 if (oldIndex < foundI) {
1533 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001534 }
1535 }
1536
1537 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001538 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1539 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001540 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001541
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001542 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001543 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001544 }
1545 }
1546
1547 return changed;
1548 }
1549
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001550 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001551 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1552 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001553 mWallpaperAnimLayerAdjustment = adj;
1554 int curTokenIndex = mWallpaperTokens.size();
1555 while (curTokenIndex > 0) {
1556 curTokenIndex--;
1557 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1558 int curWallpaperIndex = token.windows.size();
1559 while (curWallpaperIndex > 0) {
1560 curWallpaperIndex--;
1561 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1562 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001563 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1564 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001565 }
1566 }
1567 }
1568
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001569 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1570 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001571 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001572 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001573 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
1574 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1575 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1576 changed = wallpaperWin.mXOffset != offset;
1577 if (changed) {
1578 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1579 + wallpaperWin + " x: " + offset);
1580 wallpaperWin.mXOffset = offset;
1581 }
1582 if (wallpaperWin.mWallpaperX != wpx) {
1583 wallpaperWin.mWallpaperX = wpx;
1584 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001585 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001586
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001587 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
1588 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1589 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1590 if (wallpaperWin.mYOffset != offset) {
1591 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1592 + wallpaperWin + " y: " + offset);
1593 changed = true;
1594 wallpaperWin.mYOffset = offset;
1595 }
1596 if (wallpaperWin.mWallpaperY != wpy) {
1597 wallpaperWin.mWallpaperY = wpy;
1598 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001599 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001600
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001601 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001602 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001603 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1604 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1605 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001606 if (sync) {
1607 synchronized (mWaitingOnWallpaperLock) {
1608 mWaitingOnWallpaper = wallpaperWin;
1609 }
1610 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001611 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001612 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1613 if (sync) {
1614 synchronized (mWaitingOnWallpaperLock) {
1615 if (mWaitingOnWallpaper != null) {
1616 long start = SystemClock.uptimeMillis();
1617 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1618 < start) {
1619 try {
1620 if (DEBUG_WALLPAPER) Log.v(TAG,
1621 "Waiting for offset complete...");
1622 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1623 } catch (InterruptedException e) {
1624 }
1625 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1626 if ((start+WALLPAPER_TIMEOUT)
1627 < SystemClock.uptimeMillis()) {
1628 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1629 + wallpaperWin);
1630 mLastWallpaperTimeoutTime = start;
1631 }
1632 }
1633 mWaitingOnWallpaper = null;
1634 }
1635 }
1636 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001637 } catch (RemoteException e) {
1638 }
1639 }
1640
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001641 return changed;
1642 }
1643
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001644 void wallpaperOffsetsComplete(IBinder window) {
1645 synchronized (mWaitingOnWallpaperLock) {
1646 if (mWaitingOnWallpaper != null &&
1647 mWaitingOnWallpaper.mClient.asBinder() == window) {
1648 mWaitingOnWallpaper = null;
1649 mWaitingOnWallpaperLock.notifyAll();
1650 }
1651 }
1652 }
1653
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001654 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001655 final int dw = mDisplay.getWidth();
1656 final int dh = mDisplay.getHeight();
1657
1658 boolean changed = false;
1659
1660 WindowState target = mWallpaperTarget;
1661 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001662 if (target.mWallpaperX >= 0) {
1663 mLastWallpaperX = target.mWallpaperX;
1664 } else if (changingTarget.mWallpaperX >= 0) {
1665 mLastWallpaperX = changingTarget.mWallpaperX;
1666 }
1667 if (target.mWallpaperY >= 0) {
1668 mLastWallpaperY = target.mWallpaperY;
1669 } else if (changingTarget.mWallpaperY >= 0) {
1670 mLastWallpaperY = changingTarget.mWallpaperY;
1671 }
1672 }
1673
1674 int curTokenIndex = mWallpaperTokens.size();
1675 while (curTokenIndex > 0) {
1676 curTokenIndex--;
1677 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1678 int curWallpaperIndex = token.windows.size();
1679 while (curWallpaperIndex > 0) {
1680 curWallpaperIndex--;
1681 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1682 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1683 wallpaper.computeShownFrameLocked();
1684 changed = true;
1685 // We only want to be synchronous with one wallpaper.
1686 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001687 }
1688 }
1689 }
1690
1691 return changed;
1692 }
1693
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001694 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001695 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001696 final int dw = mDisplay.getWidth();
1697 final int dh = mDisplay.getHeight();
1698
1699 int curTokenIndex = mWallpaperTokens.size();
1700 while (curTokenIndex > 0) {
1701 curTokenIndex--;
1702 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001703 if (token.hidden == visible) {
1704 token.hidden = !visible;
1705 // Need to do a layout to ensure the wallpaper now has the
1706 // correct size.
1707 mLayoutNeeded = true;
1708 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001709
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001710 int curWallpaperIndex = token.windows.size();
1711 while (curWallpaperIndex > 0) {
1712 curWallpaperIndex--;
1713 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1714 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001715 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001716 }
1717
1718 if (wallpaper.mWallpaperVisible != visible) {
1719 wallpaper.mWallpaperVisible = visible;
1720 try {
1721 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001722 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001723 + ": " + visible);
1724 wallpaper.mClient.dispatchAppVisibility(visible);
1725 } catch (RemoteException e) {
1726 }
1727 }
1728 }
1729 }
1730 }
1731
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001732 void sendPointerToWallpaperLocked(WindowState srcWin,
1733 MotionEvent pointer, long eventTime) {
1734 int curTokenIndex = mWallpaperTokens.size();
1735 while (curTokenIndex > 0) {
1736 curTokenIndex--;
1737 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1738 int curWallpaperIndex = token.windows.size();
1739 while (curWallpaperIndex > 0) {
1740 curWallpaperIndex--;
1741 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1742 if ((wallpaper.mAttrs.flags &
1743 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1744 continue;
1745 }
1746 try {
1747 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1748 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1749 srcWin.mFrame.top-wallpaper.mFrame.top);
1750 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1751 } catch (RemoteException e) {
1752 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1753 }
1754 }
1755 }
1756 }
1757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 public int addWindow(Session session, IWindow client,
1759 WindowManager.LayoutParams attrs, int viewVisibility,
1760 Rect outContentInsets) {
1761 int res = mPolicy.checkAddPermission(attrs);
1762 if (res != WindowManagerImpl.ADD_OKAY) {
1763 return res;
1764 }
Romain Guy06882f82009-06-10 13:36:04 -07001765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 boolean reportNewConfig = false;
1767 WindowState attachedWindow = null;
1768 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 synchronized(mWindowMap) {
1771 // Instantiating a Display requires talking with the simulator,
1772 // so don't do it until we know the system is mostly up and
1773 // running.
1774 if (mDisplay == null) {
1775 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1776 mDisplay = wm.getDefaultDisplay();
1777 mQueue.setDisplay(mDisplay);
1778 reportNewConfig = true;
1779 }
Romain Guy06882f82009-06-10 13:36:04 -07001780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 if (mWindowMap.containsKey(client.asBinder())) {
1782 Log.w(TAG, "Window " + client + " is already added");
1783 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1784 }
1785
1786 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001787 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 if (attachedWindow == null) {
1789 Log.w(TAG, "Attempted to add window with token that is not a window: "
1790 + attrs.token + ". Aborting.");
1791 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1792 }
1793 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1794 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1795 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1796 + attrs.token + ". Aborting.");
1797 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1798 }
1799 }
1800
1801 boolean addToken = false;
1802 WindowToken token = mTokenMap.get(attrs.token);
1803 if (token == null) {
1804 if (attrs.type >= FIRST_APPLICATION_WINDOW
1805 && attrs.type <= LAST_APPLICATION_WINDOW) {
1806 Log.w(TAG, "Attempted to add application window with unknown token "
1807 + attrs.token + ". Aborting.");
1808 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1809 }
1810 if (attrs.type == TYPE_INPUT_METHOD) {
1811 Log.w(TAG, "Attempted to add input method window with unknown token "
1812 + attrs.token + ". Aborting.");
1813 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1814 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001815 if (attrs.type == TYPE_WALLPAPER) {
1816 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1817 + attrs.token + ". Aborting.");
1818 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 token = new WindowToken(attrs.token, -1, false);
1821 addToken = true;
1822 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1823 && attrs.type <= LAST_APPLICATION_WINDOW) {
1824 AppWindowToken atoken = token.appWindowToken;
1825 if (atoken == null) {
1826 Log.w(TAG, "Attempted to add window with non-application token "
1827 + token + ". Aborting.");
1828 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1829 } else if (atoken.removed) {
1830 Log.w(TAG, "Attempted to add window with exiting application token "
1831 + token + ". Aborting.");
1832 return WindowManagerImpl.ADD_APP_EXITING;
1833 }
1834 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1835 // No need for this guy!
1836 if (localLOGV) Log.v(
1837 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1838 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1839 }
1840 } else if (attrs.type == TYPE_INPUT_METHOD) {
1841 if (token.windowType != TYPE_INPUT_METHOD) {
1842 Log.w(TAG, "Attempted to add input method window with bad token "
1843 + attrs.token + ". Aborting.");
1844 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1845 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001846 } else if (attrs.type == TYPE_WALLPAPER) {
1847 if (token.windowType != TYPE_WALLPAPER) {
1848 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1849 + attrs.token + ". Aborting.");
1850 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853
1854 win = new WindowState(session, client, token,
1855 attachedWindow, attrs, viewVisibility);
1856 if (win.mDeathRecipient == null) {
1857 // Client has apparently died, so there is no reason to
1858 // continue.
1859 Log.w(TAG, "Adding window client " + client.asBinder()
1860 + " that is dead, aborting.");
1861 return WindowManagerImpl.ADD_APP_EXITING;
1862 }
1863
1864 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 res = mPolicy.prepareAddWindowLw(win, attrs);
1867 if (res != WindowManagerImpl.ADD_OKAY) {
1868 return res;
1869 }
1870
1871 // From now on, no exceptions or errors allowed!
1872
1873 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 if (addToken) {
1878 mTokenMap.put(attrs.token, token);
1879 mTokenList.add(token);
1880 }
1881 win.attach();
1882 mWindowMap.put(client.asBinder(), win);
1883
1884 if (attrs.type == TYPE_APPLICATION_STARTING &&
1885 token.appWindowToken != null) {
1886 token.appWindowToken.startingWindow = win;
1887 }
1888
1889 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 if (attrs.type == TYPE_INPUT_METHOD) {
1892 mInputMethodWindow = win;
1893 addInputMethodWindowToListLocked(win);
1894 imMayMove = false;
1895 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1896 mInputMethodDialogs.add(win);
1897 addWindowToListInOrderLocked(win, true);
1898 adjustInputMethodDialogsLocked();
1899 imMayMove = false;
1900 } else {
1901 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001902 if (attrs.type == TYPE_WALLPAPER) {
1903 mLastWallpaperTimeoutTime = 0;
1904 adjustWallpaperWindowsLocked();
1905 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001906 adjustWallpaperWindowsLocked();
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Romain Guy06882f82009-06-10 13:36:04 -07001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 if (mInTouchMode) {
1915 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1916 }
1917 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1918 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1919 }
Romain Guy06882f82009-06-10 13:36:04 -07001920
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001921 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001923 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1924 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 imMayMove = false;
1926 }
1927 }
Romain Guy06882f82009-06-10 13:36:04 -07001928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001930 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
Romain Guy06882f82009-06-10 13:36:04 -07001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 assignLayersLocked();
1934 // Don't do layout here, the window must call
1935 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 //dump();
1938
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001939 if (focusChanged) {
1940 if (mCurrentFocus != null) {
1941 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1942 }
1943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 if (localLOGV) Log.v(
1945 TAG, "New client " + client.asBinder()
1946 + ": window=" + win);
1947 }
1948
1949 // sendNewConfiguration() checks caller permissions so we must call it with
1950 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1951 // identity anyway, so it's safe to just clear & restore around this whole
1952 // block.
1953 final long origId = Binder.clearCallingIdentity();
1954 if (reportNewConfig) {
1955 sendNewConfiguration();
1956 } else {
1957 // Update Orientation after adding a window, only if the window needs to be
1958 // displayed right away
1959 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001960 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 sendNewConfiguration();
1962 }
1963 }
1964 }
1965 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 return res;
1968 }
Romain Guy06882f82009-06-10 13:36:04 -07001969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 public void removeWindow(Session session, IWindow client) {
1971 synchronized(mWindowMap) {
1972 WindowState win = windowForClientLocked(session, client);
1973 if (win == null) {
1974 return;
1975 }
1976 removeWindowLocked(session, win);
1977 }
1978 }
Romain Guy06882f82009-06-10 13:36:04 -07001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 public void removeWindowLocked(Session session, WindowState win) {
1981
1982 if (localLOGV || DEBUG_FOCUS) Log.v(
1983 TAG, "Remove " + win + " client="
1984 + Integer.toHexString(System.identityHashCode(
1985 win.mClient.asBinder()))
1986 + ", surface=" + win.mSurface);
1987
1988 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 if (DEBUG_APP_TRANSITIONS) Log.v(
1991 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1992 + " mExiting=" + win.mExiting
1993 + " isAnimating=" + win.isAnimating()
1994 + " app-animation="
1995 + (win.mAppToken != null ? win.mAppToken.animation : null)
1996 + " inPendingTransaction="
1997 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1998 + " mDisplayFrozen=" + mDisplayFrozen);
1999 // Visibility of the removed window. Will be used later to update orientation later on.
2000 boolean wasVisible = false;
2001 // First, see if we need to run an animation. If we do, we have
2002 // to hold off on removing the window until the animation is done.
2003 // If the display is frozen, just remove immediately, since the
2004 // animation wouldn't be seen.
2005 if (win.mSurface != null && !mDisplayFrozen) {
2006 // If we are not currently running the exit animation, we
2007 // need to see about starting one.
2008 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2011 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2012 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2013 }
2014 // Try starting an animation.
2015 if (applyAnimationLocked(win, transit, false)) {
2016 win.mExiting = true;
2017 }
2018 }
2019 if (win.mExiting || win.isAnimating()) {
2020 // The exit animation is running... wait for it!
2021 //Log.i(TAG, "*** Running exit animation...");
2022 win.mExiting = true;
2023 win.mRemoveOnExit = true;
2024 mLayoutNeeded = true;
2025 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2026 performLayoutAndPlaceSurfacesLocked();
2027 if (win.mAppToken != null) {
2028 win.mAppToken.updateReportedVisibilityLocked();
2029 }
2030 //dump();
2031 Binder.restoreCallingIdentity(origId);
2032 return;
2033 }
2034 }
2035
2036 removeWindowInnerLocked(session, win);
2037 // Removing a visible window will effect the computed orientation
2038 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002039 if (wasVisible && computeForcedAppOrientationLocked()
2040 != mForcedAppOrientation) {
2041 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2044 Binder.restoreCallingIdentity(origId);
2045 }
Romain Guy06882f82009-06-10 13:36:04 -07002046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002048 mKeyWaiter.finishedKey(session, win.mClient, true,
2049 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2051 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 if (mInputMethodTarget == win) {
2056 moveInputMethodWindowsIfNeededLocked(false);
2057 }
Romain Guy06882f82009-06-10 13:36:04 -07002058
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002059 if (false) {
2060 RuntimeException e = new RuntimeException("here");
2061 e.fillInStackTrace();
2062 Log.w(TAG, "Removing window " + win, e);
2063 }
2064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 mPolicy.removeWindowLw(win);
2066 win.removeLocked();
2067
2068 mWindowMap.remove(win.mClient.asBinder());
2069 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002070 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071
2072 if (mInputMethodWindow == win) {
2073 mInputMethodWindow = null;
2074 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2075 mInputMethodDialogs.remove(win);
2076 }
Romain Guy06882f82009-06-10 13:36:04 -07002077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 final WindowToken token = win.mToken;
2079 final AppWindowToken atoken = win.mAppToken;
2080 token.windows.remove(win);
2081 if (atoken != null) {
2082 atoken.allAppWindows.remove(win);
2083 }
2084 if (localLOGV) Log.v(
2085 TAG, "**** Removing window " + win + ": count="
2086 + token.windows.size());
2087 if (token.windows.size() == 0) {
2088 if (!token.explicit) {
2089 mTokenMap.remove(token.token);
2090 mTokenList.remove(token);
2091 } else if (atoken != null) {
2092 atoken.firstWindowDrawn = false;
2093 }
2094 }
2095
2096 if (atoken != null) {
2097 if (atoken.startingWindow == win) {
2098 atoken.startingWindow = null;
2099 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2100 // If this is the last window and we had requested a starting
2101 // transition window, well there is no point now.
2102 atoken.startingData = null;
2103 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2104 // If this is the last window except for a starting transition
2105 // window, we need to get rid of the starting transition.
2106 if (DEBUG_STARTING_WINDOW) {
2107 Log.v(TAG, "Schedule remove starting " + token
2108 + ": no more real windows");
2109 }
2110 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2111 mH.sendMessage(m);
2112 }
2113 }
Romain Guy06882f82009-06-10 13:36:04 -07002114
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002115 if (win.mAttrs.type == TYPE_WALLPAPER) {
2116 mLastWallpaperTimeoutTime = 0;
2117 adjustWallpaperWindowsLocked();
2118 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002119 adjustWallpaperWindowsLocked();
2120 }
2121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 if (!mInLayout) {
2123 assignLayersLocked();
2124 mLayoutNeeded = true;
2125 performLayoutAndPlaceSurfacesLocked();
2126 if (win.mAppToken != null) {
2127 win.mAppToken.updateReportedVisibilityLocked();
2128 }
2129 }
2130 }
2131
2132 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2133 long origId = Binder.clearCallingIdentity();
2134 try {
2135 synchronized (mWindowMap) {
2136 WindowState w = windowForClientLocked(session, client);
2137 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002138 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 Surface.openTransaction();
2140 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002141 if (SHOW_TRANSACTIONS) Log.i(
2142 TAG, " SURFACE " + w.mSurface
2143 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 w.mSurface.setTransparentRegionHint(region);
2145 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002146 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 Surface.closeTransaction();
2148 }
2149 }
2150 }
2151 } finally {
2152 Binder.restoreCallingIdentity(origId);
2153 }
2154 }
2155
2156 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002157 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 Rect visibleInsets) {
2159 long origId = Binder.clearCallingIdentity();
2160 try {
2161 synchronized (mWindowMap) {
2162 WindowState w = windowForClientLocked(session, client);
2163 if (w != null) {
2164 w.mGivenInsetsPending = false;
2165 w.mGivenContentInsets.set(contentInsets);
2166 w.mGivenVisibleInsets.set(visibleInsets);
2167 w.mTouchableInsets = touchableInsets;
2168 mLayoutNeeded = true;
2169 performLayoutAndPlaceSurfacesLocked();
2170 }
2171 }
2172 } finally {
2173 Binder.restoreCallingIdentity(origId);
2174 }
2175 }
Romain Guy06882f82009-06-10 13:36:04 -07002176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 public void getWindowDisplayFrame(Session session, IWindow client,
2178 Rect outDisplayFrame) {
2179 synchronized(mWindowMap) {
2180 WindowState win = windowForClientLocked(session, client);
2181 if (win == null) {
2182 outDisplayFrame.setEmpty();
2183 return;
2184 }
2185 outDisplayFrame.set(win.mDisplayFrame);
2186 }
2187 }
2188
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002189 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2190 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2191 window.mWallpaperX = x;
2192 window.mWallpaperY = y;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002193 if (updateWallpaperOffsetLocked(window, true)) {
2194 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002195 }
2196 }
2197 }
2198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 public int relayoutWindow(Session session, IWindow client,
2200 WindowManager.LayoutParams attrs, int requestedWidth,
2201 int requestedHeight, int viewVisibility, boolean insetsPending,
2202 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2203 Surface outSurface) {
2204 boolean displayed = false;
2205 boolean inTouchMode;
2206 Configuration newConfig = null;
2207 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 synchronized(mWindowMap) {
2210 WindowState win = windowForClientLocked(session, client);
2211 if (win == null) {
2212 return 0;
2213 }
2214 win.mRequestedWidth = requestedWidth;
2215 win.mRequestedHeight = requestedHeight;
2216
2217 if (attrs != null) {
2218 mPolicy.adjustWindowParamsLw(attrs);
2219 }
Romain Guy06882f82009-06-10 13:36:04 -07002220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 int attrChanges = 0;
2222 int flagChanges = 0;
2223 if (attrs != null) {
2224 flagChanges = win.mAttrs.flags ^= attrs.flags;
2225 attrChanges = win.mAttrs.copyFrom(attrs);
2226 }
2227
2228 if (localLOGV) Log.v(
2229 TAG, "Relayout given client " + client.asBinder()
2230 + " (" + win.mAttrs.getTitle() + ")");
2231
2232
2233 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2234 win.mAlpha = attrs.alpha;
2235 }
2236
2237 final boolean scaledWindow =
2238 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2239
2240 if (scaledWindow) {
2241 // requested{Width|Height} Surface's physical size
2242 // attrs.{width|height} Size on screen
2243 win.mHScale = (attrs.width != requestedWidth) ?
2244 (attrs.width / (float)requestedWidth) : 1.0f;
2245 win.mVScale = (attrs.height != requestedHeight) ?
2246 (attrs.height / (float)requestedHeight) : 1.0f;
2247 }
2248
2249 boolean imMayMove = (flagChanges&(
2250 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2251 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 boolean focusMayChange = win.mViewVisibility != viewVisibility
2254 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2255 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002256
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002257 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2258 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 win.mRelayoutCalled = true;
2261 final int oldVisibility = win.mViewVisibility;
2262 win.mViewVisibility = viewVisibility;
2263 if (viewVisibility == View.VISIBLE &&
2264 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2265 displayed = !win.isVisibleLw();
2266 if (win.mExiting) {
2267 win.mExiting = false;
2268 win.mAnimation = null;
2269 }
2270 if (win.mDestroying) {
2271 win.mDestroying = false;
2272 mDestroySurface.remove(win);
2273 }
2274 if (oldVisibility == View.GONE) {
2275 win.mEnterAnimationPending = true;
2276 }
2277 if (displayed && win.mSurface != null && !win.mDrawPending
2278 && !win.mCommitDrawPending && !mDisplayFrozen) {
2279 applyEnterAnimationLocked(win);
2280 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002281 if (displayed && (win.mAttrs.flags
2282 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2283 win.mTurnOnScreen = true;
2284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2286 // To change the format, we need to re-build the surface.
2287 win.destroySurfaceLocked();
2288 displayed = true;
2289 }
2290 try {
2291 Surface surface = win.createSurfaceLocked();
2292 if (surface != null) {
2293 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002294 win.mReportDestroySurface = false;
2295 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002296 if (SHOW_TRANSACTIONS) Log.i(TAG,
2297 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002299 // For some reason there isn't a surface. Clear the
2300 // caller's object so they see the same state.
2301 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 }
2303 } catch (Exception e) {
2304 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002305 + client + " (" + win.mAttrs.getTitle() + ")",
2306 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 Binder.restoreCallingIdentity(origId);
2308 return 0;
2309 }
2310 if (displayed) {
2311 focusMayChange = true;
2312 }
2313 if (win.mAttrs.type == TYPE_INPUT_METHOD
2314 && mInputMethodWindow == null) {
2315 mInputMethodWindow = win;
2316 imMayMove = true;
2317 }
2318 } else {
2319 win.mEnterAnimationPending = false;
2320 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002321 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2322 + ": mExiting=" + win.mExiting
2323 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 // If we are not currently running the exit animation, we
2325 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002326 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 // Try starting an animation; if there isn't one, we
2328 // can destroy the surface right away.
2329 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2330 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2331 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2332 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002333 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002335 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 win.mExiting = true;
2337 mKeyWaiter.finishedKey(session, client, true,
2338 KeyWaiter.RETURN_NOTHING);
2339 } else if (win.isAnimating()) {
2340 // Currently in a hide animation... turn this into
2341 // an exit.
2342 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002343 } else if (win == mWallpaperTarget) {
2344 // If the wallpaper is currently behind this
2345 // window, we need to change both of them inside
2346 // of a transaction to avoid artifacts.
2347 win.mExiting = true;
2348 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 } else {
2350 if (mInputMethodWindow == win) {
2351 mInputMethodWindow = null;
2352 }
2353 win.destroySurfaceLocked();
2354 }
2355 }
2356 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002357
2358 if (win.mSurface == null || (win.getAttrs().flags
2359 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2360 || win.mSurfacePendingDestroy) {
2361 // We are being called from a local process, which
2362 // means outSurface holds its current surface. Ensure the
2363 // surface object is cleared, but we don't want it actually
2364 // destroyed at this point.
2365 win.mSurfacePendingDestroy = false;
2366 outSurface.release();
2367 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2368 } else if (win.mSurface != null) {
2369 if (DEBUG_VISIBILITY) Log.i(TAG,
2370 "Keeping surface, will report destroy: " + win);
2371 win.mReportDestroySurface = true;
2372 outSurface.copyFrom(win.mSurface);
2373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
2375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 if (focusMayChange) {
2377 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2378 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 imMayMove = false;
2380 }
2381 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2382 }
Romain Guy06882f82009-06-10 13:36:04 -07002383
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002384 // updateFocusedWindowLocked() already assigned layers so we only need to
2385 // reassign them at this point if the IM window state gets shuffled
2386 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 if (imMayMove) {
2389 if (moveInputMethodWindowsIfNeededLocked(false)) {
2390 assignLayers = true;
2391 }
2392 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002393 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002394 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002395 assignLayers = true;
2396 }
2397 }
Romain Guy06882f82009-06-10 13:36:04 -07002398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 mLayoutNeeded = true;
2400 win.mGivenInsetsPending = insetsPending;
2401 if (assignLayers) {
2402 assignLayersLocked();
2403 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002404 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002406 if (displayed && win.mIsWallpaper) {
2407 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002408 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 if (win.mAppToken != null) {
2411 win.mAppToken.updateReportedVisibilityLocked();
2412 }
2413 outFrame.set(win.mFrame);
2414 outContentInsets.set(win.mContentInsets);
2415 outVisibleInsets.set(win.mVisibleInsets);
2416 if (localLOGV) Log.v(
2417 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002418 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 + ", requestedHeight=" + requestedHeight
2420 + ", viewVisibility=" + viewVisibility
2421 + "\nRelayout returning frame=" + outFrame
2422 + ", surface=" + outSurface);
2423
2424 if (localLOGV || DEBUG_FOCUS) Log.v(
2425 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2426
2427 inTouchMode = mInTouchMode;
2428 }
2429
2430 if (newConfig != null) {
2431 sendNewConfiguration();
2432 }
Romain Guy06882f82009-06-10 13:36:04 -07002433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2437 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2438 }
2439
2440 public void finishDrawingWindow(Session session, IWindow client) {
2441 final long origId = Binder.clearCallingIdentity();
2442 synchronized(mWindowMap) {
2443 WindowState win = windowForClientLocked(session, client);
2444 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002445 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2446 adjustWallpaperWindowsLocked();
2447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 mLayoutNeeded = true;
2449 performLayoutAndPlaceSurfacesLocked();
2450 }
2451 }
2452 Binder.restoreCallingIdentity(origId);
2453 }
2454
2455 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2456 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2457 + (lp != null ? lp.packageName : null)
2458 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2459 if (lp != null && lp.windowAnimations != 0) {
2460 // If this is a system resource, don't try to load it from the
2461 // application resources. It is nice to avoid loading application
2462 // resources if we can.
2463 String packageName = lp.packageName != null ? lp.packageName : "android";
2464 int resId = lp.windowAnimations;
2465 if ((resId&0xFF000000) == 0x01000000) {
2466 packageName = "android";
2467 }
2468 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2469 + packageName);
2470 return AttributeCache.instance().get(packageName, resId,
2471 com.android.internal.R.styleable.WindowAnimation);
2472 }
2473 return null;
2474 }
Romain Guy06882f82009-06-10 13:36:04 -07002475
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002476 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2477 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2478 + packageName + " resId=0x" + Integer.toHexString(resId));
2479 if (packageName != null) {
2480 if ((resId&0xFF000000) == 0x01000000) {
2481 packageName = "android";
2482 }
2483 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2484 + packageName);
2485 return AttributeCache.instance().get(packageName, resId,
2486 com.android.internal.R.styleable.WindowAnimation);
2487 }
2488 return null;
2489 }
2490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 private void applyEnterAnimationLocked(WindowState win) {
2492 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2493 if (win.mEnterAnimationPending) {
2494 win.mEnterAnimationPending = false;
2495 transit = WindowManagerPolicy.TRANSIT_ENTER;
2496 }
2497
2498 applyAnimationLocked(win, transit, true);
2499 }
2500
2501 private boolean applyAnimationLocked(WindowState win,
2502 int transit, boolean isEntrance) {
2503 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2504 // If we are trying to apply an animation, but already running
2505 // an animation of the same type, then just leave that one alone.
2506 return true;
2507 }
Romain Guy06882f82009-06-10 13:36:04 -07002508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 // Only apply an animation if the display isn't frozen. If it is
2510 // frozen, there is no reason to animate and it can cause strange
2511 // artifacts when we unfreeze the display if some different animation
2512 // is running.
2513 if (!mDisplayFrozen) {
2514 int anim = mPolicy.selectAnimationLw(win, transit);
2515 int attr = -1;
2516 Animation a = null;
2517 if (anim != 0) {
2518 a = AnimationUtils.loadAnimation(mContext, anim);
2519 } else {
2520 switch (transit) {
2521 case WindowManagerPolicy.TRANSIT_ENTER:
2522 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2523 break;
2524 case WindowManagerPolicy.TRANSIT_EXIT:
2525 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2526 break;
2527 case WindowManagerPolicy.TRANSIT_SHOW:
2528 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2529 break;
2530 case WindowManagerPolicy.TRANSIT_HIDE:
2531 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2532 break;
2533 }
2534 if (attr >= 0) {
2535 a = loadAnimation(win.mAttrs, attr);
2536 }
2537 }
2538 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2539 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2540 + " mAnimation=" + win.mAnimation
2541 + " isEntrance=" + isEntrance);
2542 if (a != null) {
2543 if (DEBUG_ANIM) {
2544 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002545 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2547 }
2548 win.setAnimation(a);
2549 win.mAnimationIsEntrance = isEntrance;
2550 }
2551 } else {
2552 win.clearAnimation();
2553 }
2554
2555 return win.mAnimation != null;
2556 }
2557
2558 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2559 int anim = 0;
2560 Context context = mContext;
2561 if (animAttr >= 0) {
2562 AttributeCache.Entry ent = getCachedAnimations(lp);
2563 if (ent != null) {
2564 context = ent.context;
2565 anim = ent.array.getResourceId(animAttr, 0);
2566 }
2567 }
2568 if (anim != 0) {
2569 return AnimationUtils.loadAnimation(context, anim);
2570 }
2571 return null;
2572 }
Romain Guy06882f82009-06-10 13:36:04 -07002573
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002574 private Animation loadAnimation(String packageName, int resId) {
2575 int anim = 0;
2576 Context context = mContext;
2577 if (resId >= 0) {
2578 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2579 if (ent != null) {
2580 context = ent.context;
2581 anim = resId;
2582 }
2583 }
2584 if (anim != 0) {
2585 return AnimationUtils.loadAnimation(context, anim);
2586 }
2587 return null;
2588 }
2589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 private boolean applyAnimationLocked(AppWindowToken wtoken,
2591 WindowManager.LayoutParams lp, int transit, boolean enter) {
2592 // Only apply an animation if the display isn't frozen. If it is
2593 // frozen, there is no reason to animate and it can cause strange
2594 // artifacts when we unfreeze the display if some different animation
2595 // is running.
2596 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002597 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002598 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002599 a = new FadeInOutAnimation(enter);
2600 if (DEBUG_ANIM) Log.v(TAG,
2601 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002602 } else if (mNextAppTransitionPackage != null) {
2603 a = loadAnimation(mNextAppTransitionPackage, enter ?
2604 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002605 } else {
2606 int animAttr = 0;
2607 switch (transit) {
2608 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2609 animAttr = enter
2610 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2611 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2612 break;
2613 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2614 animAttr = enter
2615 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2616 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2617 break;
2618 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2619 animAttr = enter
2620 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2621 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2622 break;
2623 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2624 animAttr = enter
2625 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2626 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2627 break;
2628 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2629 animAttr = enter
2630 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2631 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2632 break;
2633 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2634 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002635 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002636 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2637 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002638 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002639 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002640 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2641 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002642 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002643 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002644 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002645 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2646 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2647 break;
2648 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2649 animAttr = enter
2650 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2651 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2652 break;
2653 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2654 animAttr = enter
2655 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2656 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002657 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002658 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002659 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002660 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2661 + " anim=" + a
2662 + " animAttr=0x" + Integer.toHexString(animAttr)
2663 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 if (a != null) {
2666 if (DEBUG_ANIM) {
2667 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002668 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2670 }
2671 wtoken.setAnimation(a);
2672 }
2673 } else {
2674 wtoken.clearAnimation();
2675 }
2676
2677 return wtoken.animation != null;
2678 }
2679
2680 // -------------------------------------------------------------
2681 // Application Window Tokens
2682 // -------------------------------------------------------------
2683
2684 public void validateAppTokens(List tokens) {
2685 int v = tokens.size()-1;
2686 int m = mAppTokens.size()-1;
2687 while (v >= 0 && m >= 0) {
2688 AppWindowToken wtoken = mAppTokens.get(m);
2689 if (wtoken.removed) {
2690 m--;
2691 continue;
2692 }
2693 if (tokens.get(v) != wtoken.token) {
2694 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2695 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2696 }
2697 v--;
2698 m--;
2699 }
2700 while (v >= 0) {
2701 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2702 v--;
2703 }
2704 while (m >= 0) {
2705 AppWindowToken wtoken = mAppTokens.get(m);
2706 if (!wtoken.removed) {
2707 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2708 }
2709 m--;
2710 }
2711 }
2712
2713 boolean checkCallingPermission(String permission, String func) {
2714 // Quick check: if the calling permission is me, it's all okay.
2715 if (Binder.getCallingPid() == Process.myPid()) {
2716 return true;
2717 }
Romain Guy06882f82009-06-10 13:36:04 -07002718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 if (mContext.checkCallingPermission(permission)
2720 == PackageManager.PERMISSION_GRANTED) {
2721 return true;
2722 }
2723 String msg = "Permission Denial: " + func + " from pid="
2724 + Binder.getCallingPid()
2725 + ", uid=" + Binder.getCallingUid()
2726 + " requires " + permission;
2727 Log.w(TAG, msg);
2728 return false;
2729 }
Romain Guy06882f82009-06-10 13:36:04 -07002730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 AppWindowToken findAppWindowToken(IBinder token) {
2732 WindowToken wtoken = mTokenMap.get(token);
2733 if (wtoken == null) {
2734 return null;
2735 }
2736 return wtoken.appWindowToken;
2737 }
Romain Guy06882f82009-06-10 13:36:04 -07002738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 public void addWindowToken(IBinder token, int type) {
2740 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2741 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002742 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 }
Romain Guy06882f82009-06-10 13:36:04 -07002744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 synchronized(mWindowMap) {
2746 WindowToken wtoken = mTokenMap.get(token);
2747 if (wtoken != null) {
2748 Log.w(TAG, "Attempted to add existing input method token: " + token);
2749 return;
2750 }
2751 wtoken = new WindowToken(token, type, true);
2752 mTokenMap.put(token, wtoken);
2753 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002754 if (type == TYPE_WALLPAPER) {
2755 mWallpaperTokens.add(wtoken);
2756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 }
2758 }
Romain Guy06882f82009-06-10 13:36:04 -07002759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 public void removeWindowToken(IBinder token) {
2761 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2762 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002763 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 }
2765
2766 final long origId = Binder.clearCallingIdentity();
2767 synchronized(mWindowMap) {
2768 WindowToken wtoken = mTokenMap.remove(token);
2769 mTokenList.remove(wtoken);
2770 if (wtoken != null) {
2771 boolean delayed = false;
2772 if (!wtoken.hidden) {
2773 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 final int N = wtoken.windows.size();
2776 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 for (int i=0; i<N; i++) {
2779 WindowState win = wtoken.windows.get(i);
2780
2781 if (win.isAnimating()) {
2782 delayed = true;
2783 }
Romain Guy06882f82009-06-10 13:36:04 -07002784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 if (win.isVisibleNow()) {
2786 applyAnimationLocked(win,
2787 WindowManagerPolicy.TRANSIT_EXIT, false);
2788 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2789 KeyWaiter.RETURN_NOTHING);
2790 changed = true;
2791 }
2792 }
2793
2794 if (changed) {
2795 mLayoutNeeded = true;
2796 performLayoutAndPlaceSurfacesLocked();
2797 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2798 }
Romain Guy06882f82009-06-10 13:36:04 -07002799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 if (delayed) {
2801 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002802 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2803 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 }
2805 }
Romain Guy06882f82009-06-10 13:36:04 -07002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 } else {
2808 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2809 }
2810 }
2811 Binder.restoreCallingIdentity(origId);
2812 }
2813
2814 public void addAppToken(int addPos, IApplicationToken token,
2815 int groupId, int requestedOrientation, boolean fullscreen) {
2816 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2817 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002818 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 }
Romain Guy06882f82009-06-10 13:36:04 -07002820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 synchronized(mWindowMap) {
2822 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2823 if (wtoken != null) {
2824 Log.w(TAG, "Attempted to add existing app token: " + token);
2825 return;
2826 }
2827 wtoken = new AppWindowToken(token);
2828 wtoken.groupId = groupId;
2829 wtoken.appFullscreen = fullscreen;
2830 wtoken.requestedOrientation = requestedOrientation;
2831 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002832 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 mTokenMap.put(token.asBinder(), wtoken);
2834 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 // Application tokens start out hidden.
2837 wtoken.hidden = true;
2838 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 //dump();
2841 }
2842 }
Romain Guy06882f82009-06-10 13:36:04 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 public void setAppGroupId(IBinder token, int groupId) {
2845 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2846 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002847 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 }
2849
2850 synchronized(mWindowMap) {
2851 AppWindowToken wtoken = findAppWindowToken(token);
2852 if (wtoken == null) {
2853 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2854 return;
2855 }
2856 wtoken.groupId = groupId;
2857 }
2858 }
Romain Guy06882f82009-06-10 13:36:04 -07002859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 public int getOrientationFromWindowsLocked() {
2861 int pos = mWindows.size() - 1;
2862 while (pos >= 0) {
2863 WindowState wtoken = (WindowState) mWindows.get(pos);
2864 pos--;
2865 if (wtoken.mAppToken != null) {
2866 // We hit an application window. so the orientation will be determined by the
2867 // app window. No point in continuing further.
2868 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2869 }
2870 if (!wtoken.isVisibleLw()) {
2871 continue;
2872 }
2873 int req = wtoken.mAttrs.screenOrientation;
2874 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2875 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2876 continue;
2877 } else {
2878 return req;
2879 }
2880 }
2881 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2882 }
Romain Guy06882f82009-06-10 13:36:04 -07002883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 public int getOrientationFromAppTokensLocked() {
2885 int pos = mAppTokens.size() - 1;
2886 int curGroup = 0;
2887 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002888 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002890 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 while (pos >= 0) {
2892 AppWindowToken wtoken = mAppTokens.get(pos);
2893 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002894 // if we're about to tear down this window and not seek for
2895 // the behind activity, don't use it for orientation
2896 if (!findingBehind
2897 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002898 continue;
2899 }
2900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 if (!haveGroup) {
2902 // We ignore any hidden applications on the top.
2903 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2904 continue;
2905 }
2906 haveGroup = true;
2907 curGroup = wtoken.groupId;
2908 lastOrientation = wtoken.requestedOrientation;
2909 } else if (curGroup != wtoken.groupId) {
2910 // If we have hit a new application group, and the bottom
2911 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002912 // the orientation behind it, and the last app was
2913 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002915 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2916 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 return lastOrientation;
2918 }
2919 }
2920 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002921 // If this application is fullscreen, and didn't explicitly say
2922 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002924 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002925 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002926 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 return or;
2928 }
2929 // If this application has requested an explicit orientation,
2930 // then use it.
2931 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2932 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2933 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2934 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2935 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2936 return or;
2937 }
Owen Lin3413b892009-05-01 17:12:32 -07002938 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 }
2940 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2941 }
Romain Guy06882f82009-06-10 13:36:04 -07002942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002944 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002945 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2946 "updateOrientationFromAppTokens()")) {
2947 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2948 }
2949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 Configuration config;
2951 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002952 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2953 freezeThisOneIfNeeded);
2954 Binder.restoreCallingIdentity(ident);
2955 return config;
2956 }
2957
2958 Configuration updateOrientationFromAppTokensUnchecked(
2959 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2960 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002962 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002963 if (config != null) {
2964 mLayoutNeeded = true;
2965 performLayoutAndPlaceSurfacesLocked();
2966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 return config;
2969 }
Romain Guy06882f82009-06-10 13:36:04 -07002970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 /*
2972 * The orientation is computed from non-application windows first. If none of
2973 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002974 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2976 * android.os.IBinder)
2977 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002978 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002979 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 long ident = Binder.clearCallingIdentity();
2982 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002983 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 if (req != mForcedAppOrientation) {
2986 changed = true;
2987 mForcedAppOrientation = req;
2988 //send a message to Policy indicating orientation change to take
2989 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002990 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 }
Romain Guy06882f82009-06-10 13:36:04 -07002992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 if (changed) {
2994 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002995 WindowManagerPolicy.USE_LAST_ROTATION,
2996 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 if (changed) {
2998 if (freezeThisOneIfNeeded != null) {
2999 AppWindowToken wtoken = findAppWindowToken(
3000 freezeThisOneIfNeeded);
3001 if (wtoken != null) {
3002 startAppFreezingScreenLocked(wtoken,
3003 ActivityInfo.CONFIG_ORIENTATION);
3004 }
3005 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003006 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 }
3008 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003009
3010 // No obvious action we need to take, but if our current
3011 // state mismatches the activity maanager's, update it
3012 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003013 mTempConfiguration.setToDefaults();
3014 if (computeNewConfigurationLocked(mTempConfiguration)) {
3015 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003016 return new Configuration(mTempConfiguration);
3017 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003018 }
3019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 } finally {
3021 Binder.restoreCallingIdentity(ident);
3022 }
Romain Guy06882f82009-06-10 13:36:04 -07003023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 return null;
3025 }
Romain Guy06882f82009-06-10 13:36:04 -07003026
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003027 int computeForcedAppOrientationLocked() {
3028 int req = getOrientationFromWindowsLocked();
3029 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3030 req = getOrientationFromAppTokensLocked();
3031 }
3032 return req;
3033 }
Romain Guy06882f82009-06-10 13:36:04 -07003034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3036 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3037 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003038 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 }
Romain Guy06882f82009-06-10 13:36:04 -07003040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 synchronized(mWindowMap) {
3042 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3043 if (wtoken == null) {
3044 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3045 return;
3046 }
Romain Guy06882f82009-06-10 13:36:04 -07003047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 wtoken.requestedOrientation = requestedOrientation;
3049 }
3050 }
Romain Guy06882f82009-06-10 13:36:04 -07003051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 public int getAppOrientation(IApplicationToken token) {
3053 synchronized(mWindowMap) {
3054 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3055 if (wtoken == null) {
3056 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3057 }
Romain Guy06882f82009-06-10 13:36:04 -07003058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 return wtoken.requestedOrientation;
3060 }
3061 }
Romain Guy06882f82009-06-10 13:36:04 -07003062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3064 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3065 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003066 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 }
3068
3069 synchronized(mWindowMap) {
3070 boolean changed = false;
3071 if (token == null) {
3072 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3073 changed = mFocusedApp != null;
3074 mFocusedApp = null;
3075 mKeyWaiter.tickle();
3076 } else {
3077 AppWindowToken newFocus = findAppWindowToken(token);
3078 if (newFocus == null) {
3079 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3080 return;
3081 }
3082 changed = mFocusedApp != newFocus;
3083 mFocusedApp = newFocus;
3084 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3085 mKeyWaiter.tickle();
3086 }
3087
3088 if (moveFocusNow && changed) {
3089 final long origId = Binder.clearCallingIdentity();
3090 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3091 Binder.restoreCallingIdentity(origId);
3092 }
3093 }
3094 }
3095
3096 public void prepareAppTransition(int transit) {
3097 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3098 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003099 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 }
Romain Guy06882f82009-06-10 13:36:04 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 synchronized(mWindowMap) {
3103 if (DEBUG_APP_TRANSITIONS) Log.v(
3104 TAG, "Prepare app transition: transit=" + transit
3105 + " mNextAppTransition=" + mNextAppTransition);
3106 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003107 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3108 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003110 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3111 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3112 // Opening a new task always supersedes a close for the anim.
3113 mNextAppTransition = transit;
3114 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3115 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3116 // Opening a new activity always supersedes a close for the anim.
3117 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 }
3119 mAppTransitionReady = false;
3120 mAppTransitionTimeout = false;
3121 mStartingIconInTransition = false;
3122 mSkipAppTransitionAnimation = false;
3123 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3124 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3125 5000);
3126 }
3127 }
3128 }
3129
3130 public int getPendingAppTransition() {
3131 return mNextAppTransition;
3132 }
Romain Guy06882f82009-06-10 13:36:04 -07003133
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003134 public void overridePendingAppTransition(String packageName,
3135 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003136 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003137 mNextAppTransitionPackage = packageName;
3138 mNextAppTransitionEnter = enterAnim;
3139 mNextAppTransitionExit = exitAnim;
3140 }
3141 }
3142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 public void executeAppTransition() {
3144 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3145 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003146 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
Romain Guy06882f82009-06-10 13:36:04 -07003148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003150 if (DEBUG_APP_TRANSITIONS) {
3151 RuntimeException e = new RuntimeException("here");
3152 e.fillInStackTrace();
3153 Log.w(TAG, "Execute app transition: mNextAppTransition="
3154 + mNextAppTransition, e);
3155 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003156 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 mAppTransitionReady = true;
3158 final long origId = Binder.clearCallingIdentity();
3159 performLayoutAndPlaceSurfacesLocked();
3160 Binder.restoreCallingIdentity(origId);
3161 }
3162 }
3163 }
3164
3165 public void setAppStartingWindow(IBinder token, String pkg,
3166 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3167 IBinder transferFrom, boolean createIfNeeded) {
3168 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3169 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003170 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 }
3172
3173 synchronized(mWindowMap) {
3174 if (DEBUG_STARTING_WINDOW) Log.v(
3175 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3176 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 AppWindowToken wtoken = findAppWindowToken(token);
3179 if (wtoken == null) {
3180 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3181 return;
3182 }
3183
3184 // If the display is frozen, we won't do anything until the
3185 // actual window is displayed so there is no reason to put in
3186 // the starting window.
3187 if (mDisplayFrozen) {
3188 return;
3189 }
Romain Guy06882f82009-06-10 13:36:04 -07003190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 if (wtoken.startingData != null) {
3192 return;
3193 }
Romain Guy06882f82009-06-10 13:36:04 -07003194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 if (transferFrom != null) {
3196 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3197 if (ttoken != null) {
3198 WindowState startingWindow = ttoken.startingWindow;
3199 if (startingWindow != null) {
3200 if (mStartingIconInTransition) {
3201 // In this case, the starting icon has already
3202 // been displayed, so start letting windows get
3203 // shown immediately without any more transitions.
3204 mSkipAppTransitionAnimation = true;
3205 }
3206 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3207 "Moving existing starting from " + ttoken
3208 + " to " + wtoken);
3209 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 // Transfer the starting window over to the new
3212 // token.
3213 wtoken.startingData = ttoken.startingData;
3214 wtoken.startingView = ttoken.startingView;
3215 wtoken.startingWindow = startingWindow;
3216 ttoken.startingData = null;
3217 ttoken.startingView = null;
3218 ttoken.startingWindow = null;
3219 ttoken.startingMoved = true;
3220 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003221 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003223 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3224 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 mWindows.remove(startingWindow);
3226 ttoken.windows.remove(startingWindow);
3227 ttoken.allAppWindows.remove(startingWindow);
3228 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 // Propagate other interesting state between the
3231 // tokens. If the old token is displayed, we should
3232 // immediately force the new one to be displayed. If
3233 // it is animating, we need to move that animation to
3234 // the new one.
3235 if (ttoken.allDrawn) {
3236 wtoken.allDrawn = true;
3237 }
3238 if (ttoken.firstWindowDrawn) {
3239 wtoken.firstWindowDrawn = true;
3240 }
3241 if (!ttoken.hidden) {
3242 wtoken.hidden = false;
3243 wtoken.hiddenRequested = false;
3244 wtoken.willBeHidden = false;
3245 }
3246 if (wtoken.clientHidden != ttoken.clientHidden) {
3247 wtoken.clientHidden = ttoken.clientHidden;
3248 wtoken.sendAppVisibilityToClients();
3249 }
3250 if (ttoken.animation != null) {
3251 wtoken.animation = ttoken.animation;
3252 wtoken.animating = ttoken.animating;
3253 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3254 ttoken.animation = null;
3255 ttoken.animLayerAdjustment = 0;
3256 wtoken.updateLayers();
3257 ttoken.updateLayers();
3258 }
Romain Guy06882f82009-06-10 13:36:04 -07003259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 mLayoutNeeded = true;
3262 performLayoutAndPlaceSurfacesLocked();
3263 Binder.restoreCallingIdentity(origId);
3264 return;
3265 } else if (ttoken.startingData != null) {
3266 // The previous app was getting ready to show a
3267 // starting window, but hasn't yet done so. Steal it!
3268 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3269 "Moving pending starting from " + ttoken
3270 + " to " + wtoken);
3271 wtoken.startingData = ttoken.startingData;
3272 ttoken.startingData = null;
3273 ttoken.startingMoved = true;
3274 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3275 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3276 // want to process the message ASAP, before any other queued
3277 // messages.
3278 mH.sendMessageAtFrontOfQueue(m);
3279 return;
3280 }
3281 }
3282 }
3283
3284 // There is no existing starting window, and the caller doesn't
3285 // want us to create one, so that's it!
3286 if (!createIfNeeded) {
3287 return;
3288 }
Romain Guy06882f82009-06-10 13:36:04 -07003289
Dianne Hackborn284ac932009-08-28 10:34:25 -07003290 // If this is a translucent or wallpaper window, then don't
3291 // show a starting window -- the current effect (a full-screen
3292 // opaque starting window that fades away to the real contents
3293 // when it is ready) does not work for this.
3294 if (theme != 0) {
3295 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3296 com.android.internal.R.styleable.Window);
3297 if (ent.array.getBoolean(
3298 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3299 return;
3300 }
3301 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003302 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3303 return;
3304 }
3305 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003306 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3307 return;
3308 }
3309 }
3310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 mStartingIconInTransition = true;
3312 wtoken.startingData = new StartingData(
3313 pkg, theme, nonLocalizedLabel,
3314 labelRes, icon);
3315 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3316 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3317 // want to process the message ASAP, before any other queued
3318 // messages.
3319 mH.sendMessageAtFrontOfQueue(m);
3320 }
3321 }
3322
3323 public void setAppWillBeHidden(IBinder token) {
3324 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3325 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003326 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 }
3328
3329 AppWindowToken wtoken;
3330
3331 synchronized(mWindowMap) {
3332 wtoken = findAppWindowToken(token);
3333 if (wtoken == null) {
3334 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3335 return;
3336 }
3337 wtoken.willBeHidden = true;
3338 }
3339 }
Romain Guy06882f82009-06-10 13:36:04 -07003340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3342 boolean visible, int transit, boolean performLayout) {
3343 boolean delayed = false;
3344
3345 if (wtoken.clientHidden == visible) {
3346 wtoken.clientHidden = !visible;
3347 wtoken.sendAppVisibilityToClients();
3348 }
Romain Guy06882f82009-06-10 13:36:04 -07003349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 wtoken.willBeHidden = false;
3351 if (wtoken.hidden == visible) {
3352 final int N = wtoken.allAppWindows.size();
3353 boolean changed = false;
3354 if (DEBUG_APP_TRANSITIONS) Log.v(
3355 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3356 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003359
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003360 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 if (wtoken.animation == sDummyAnimation) {
3362 wtoken.animation = null;
3363 }
3364 applyAnimationLocked(wtoken, lp, transit, visible);
3365 changed = true;
3366 if (wtoken.animation != null) {
3367 delayed = runningAppAnimation = true;
3368 }
3369 }
Romain Guy06882f82009-06-10 13:36:04 -07003370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 for (int i=0; i<N; i++) {
3372 WindowState win = wtoken.allAppWindows.get(i);
3373 if (win == wtoken.startingWindow) {
3374 continue;
3375 }
3376
3377 if (win.isAnimating()) {
3378 delayed = true;
3379 }
Romain Guy06882f82009-06-10 13:36:04 -07003380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3382 //win.dump(" ");
3383 if (visible) {
3384 if (!win.isVisibleNow()) {
3385 if (!runningAppAnimation) {
3386 applyAnimationLocked(win,
3387 WindowManagerPolicy.TRANSIT_ENTER, true);
3388 }
3389 changed = true;
3390 }
3391 } else if (win.isVisibleNow()) {
3392 if (!runningAppAnimation) {
3393 applyAnimationLocked(win,
3394 WindowManagerPolicy.TRANSIT_EXIT, false);
3395 }
3396 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3397 KeyWaiter.RETURN_NOTHING);
3398 changed = true;
3399 }
3400 }
3401
3402 wtoken.hidden = wtoken.hiddenRequested = !visible;
3403 if (!visible) {
3404 unsetAppFreezingScreenLocked(wtoken, true, true);
3405 } else {
3406 // If we are being set visible, and the starting window is
3407 // not yet displayed, then make sure it doesn't get displayed.
3408 WindowState swin = wtoken.startingWindow;
3409 if (swin != null && (swin.mDrawPending
3410 || swin.mCommitDrawPending)) {
3411 swin.mPolicyVisibility = false;
3412 swin.mPolicyVisibilityAfterAnim = false;
3413 }
3414 }
Romain Guy06882f82009-06-10 13:36:04 -07003415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3417 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3418 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 if (changed && performLayout) {
3421 mLayoutNeeded = true;
3422 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 performLayoutAndPlaceSurfacesLocked();
3424 }
3425 }
3426
3427 if (wtoken.animation != null) {
3428 delayed = true;
3429 }
Romain Guy06882f82009-06-10 13:36:04 -07003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 return delayed;
3432 }
3433
3434 public void setAppVisibility(IBinder token, boolean visible) {
3435 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3436 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003437 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 }
3439
3440 AppWindowToken wtoken;
3441
3442 synchronized(mWindowMap) {
3443 wtoken = findAppWindowToken(token);
3444 if (wtoken == null) {
3445 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3446 return;
3447 }
3448
3449 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3450 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003451 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3453 + "): mNextAppTransition=" + mNextAppTransition
3454 + " hidden=" + wtoken.hidden
3455 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3456 }
Romain Guy06882f82009-06-10 13:36:04 -07003457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 // If we are preparing an app transition, then delay changing
3459 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003460 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 // Already in requested state, don't do anything more.
3462 if (wtoken.hiddenRequested != visible) {
3463 return;
3464 }
3465 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 if (DEBUG_APP_TRANSITIONS) Log.v(
3468 TAG, "Setting dummy animation on: " + wtoken);
3469 wtoken.setDummyAnimation();
3470 mOpeningApps.remove(wtoken);
3471 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003472 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 wtoken.inPendingTransaction = true;
3474 if (visible) {
3475 mOpeningApps.add(wtoken);
3476 wtoken.allDrawn = false;
3477 wtoken.startingDisplayed = false;
3478 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003479 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 if (wtoken.clientHidden) {
3482 // In the case where we are making an app visible
3483 // but holding off for a transition, we still need
3484 // to tell the client to make its windows visible so
3485 // they get drawn. Otherwise, we will wait on
3486 // performing the transition until all windows have
3487 // been drawn, they never will be, and we are sad.
3488 wtoken.clientHidden = false;
3489 wtoken.sendAppVisibilityToClients();
3490 }
3491 } else {
3492 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003493 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495 return;
3496 }
Romain Guy06882f82009-06-10 13:36:04 -07003497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003499 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 wtoken.updateReportedVisibilityLocked();
3501 Binder.restoreCallingIdentity(origId);
3502 }
3503 }
3504
3505 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3506 boolean unfreezeSurfaceNow, boolean force) {
3507 if (wtoken.freezingScreen) {
3508 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3509 + " force=" + force);
3510 final int N = wtoken.allAppWindows.size();
3511 boolean unfrozeWindows = false;
3512 for (int i=0; i<N; i++) {
3513 WindowState w = wtoken.allAppWindows.get(i);
3514 if (w.mAppFreezing) {
3515 w.mAppFreezing = false;
3516 if (w.mSurface != null && !w.mOrientationChanging) {
3517 w.mOrientationChanging = true;
3518 }
3519 unfrozeWindows = true;
3520 }
3521 }
3522 if (force || unfrozeWindows) {
3523 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3524 wtoken.freezingScreen = false;
3525 mAppsFreezingScreen--;
3526 }
3527 if (unfreezeSurfaceNow) {
3528 if (unfrozeWindows) {
3529 mLayoutNeeded = true;
3530 performLayoutAndPlaceSurfacesLocked();
3531 }
3532 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3533 stopFreezingDisplayLocked();
3534 }
3535 }
3536 }
3537 }
Romain Guy06882f82009-06-10 13:36:04 -07003538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3540 int configChanges) {
3541 if (DEBUG_ORIENTATION) {
3542 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003543 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 Log.i(TAG, "Set freezing of " + wtoken.appToken
3545 + ": hidden=" + wtoken.hidden + " freezing="
3546 + wtoken.freezingScreen, e);
3547 }
3548 if (!wtoken.hiddenRequested) {
3549 if (!wtoken.freezingScreen) {
3550 wtoken.freezingScreen = true;
3551 mAppsFreezingScreen++;
3552 if (mAppsFreezingScreen == 1) {
3553 startFreezingDisplayLocked();
3554 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3555 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3556 5000);
3557 }
3558 }
3559 final int N = wtoken.allAppWindows.size();
3560 for (int i=0; i<N; i++) {
3561 WindowState w = wtoken.allAppWindows.get(i);
3562 w.mAppFreezing = true;
3563 }
3564 }
3565 }
Romain Guy06882f82009-06-10 13:36:04 -07003566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 public void startAppFreezingScreen(IBinder token, int configChanges) {
3568 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3569 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003570 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 }
3572
3573 synchronized(mWindowMap) {
3574 if (configChanges == 0 && !mDisplayFrozen) {
3575 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3576 return;
3577 }
Romain Guy06882f82009-06-10 13:36:04 -07003578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 AppWindowToken wtoken = findAppWindowToken(token);
3580 if (wtoken == null || wtoken.appToken == null) {
3581 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3582 return;
3583 }
3584 final long origId = Binder.clearCallingIdentity();
3585 startAppFreezingScreenLocked(wtoken, configChanges);
3586 Binder.restoreCallingIdentity(origId);
3587 }
3588 }
Romain Guy06882f82009-06-10 13:36:04 -07003589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 public void stopAppFreezingScreen(IBinder token, boolean force) {
3591 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3592 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003593 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 }
3595
3596 synchronized(mWindowMap) {
3597 AppWindowToken wtoken = findAppWindowToken(token);
3598 if (wtoken == null || wtoken.appToken == null) {
3599 return;
3600 }
3601 final long origId = Binder.clearCallingIdentity();
3602 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3603 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3604 unsetAppFreezingScreenLocked(wtoken, true, force);
3605 Binder.restoreCallingIdentity(origId);
3606 }
3607 }
Romain Guy06882f82009-06-10 13:36:04 -07003608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 public void removeAppToken(IBinder token) {
3610 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3611 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003612 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
3614
3615 AppWindowToken wtoken = null;
3616 AppWindowToken startingToken = null;
3617 boolean delayed = false;
3618
3619 final long origId = Binder.clearCallingIdentity();
3620 synchronized(mWindowMap) {
3621 WindowToken basewtoken = mTokenMap.remove(token);
3622 mTokenList.remove(basewtoken);
3623 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3624 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003625 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 wtoken.inPendingTransaction = false;
3627 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003628 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 if (mClosingApps.contains(wtoken)) {
3630 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003631 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003633 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 delayed = true;
3635 }
3636 if (DEBUG_APP_TRANSITIONS) Log.v(
3637 TAG, "Removing app " + wtoken + " delayed=" + delayed
3638 + " animation=" + wtoken.animation
3639 + " animating=" + wtoken.animating);
3640 if (delayed) {
3641 // set the token aside because it has an active animation to be finished
3642 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003643 } else {
3644 // Make sure there is no animation running on this token,
3645 // so any windows associated with it will be removed as
3646 // soon as their animations are complete
3647 wtoken.animation = null;
3648 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 }
3650 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003651 if (mLastEnterAnimToken == wtoken) {
3652 mLastEnterAnimToken = null;
3653 mLastEnterAnimParams = null;
3654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 wtoken.removed = true;
3656 if (wtoken.startingData != null) {
3657 startingToken = wtoken;
3658 }
3659 unsetAppFreezingScreenLocked(wtoken, true, true);
3660 if (mFocusedApp == wtoken) {
3661 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3662 mFocusedApp = null;
3663 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3664 mKeyWaiter.tickle();
3665 }
3666 } else {
3667 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3668 }
Romain Guy06882f82009-06-10 13:36:04 -07003669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 if (!delayed && wtoken != null) {
3671 wtoken.updateReportedVisibilityLocked();
3672 }
3673 }
3674 Binder.restoreCallingIdentity(origId);
3675
3676 if (startingToken != null) {
3677 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3678 + startingToken + ": app token removed");
3679 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3680 mH.sendMessage(m);
3681 }
3682 }
3683
3684 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3685 final int NW = token.windows.size();
3686 for (int i=0; i<NW; i++) {
3687 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003688 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 mWindows.remove(win);
3690 int j = win.mChildWindows.size();
3691 while (j > 0) {
3692 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003693 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3694 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3695 "Tmp removing child window " + cwin);
3696 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 }
3698 }
3699 return NW > 0;
3700 }
3701
3702 void dumpAppTokensLocked() {
3703 for (int i=mAppTokens.size()-1; i>=0; i--) {
3704 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3705 }
3706 }
Romain Guy06882f82009-06-10 13:36:04 -07003707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 void dumpWindowsLocked() {
3709 for (int i=mWindows.size()-1; i>=0; i--) {
3710 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3711 }
3712 }
Romain Guy06882f82009-06-10 13:36:04 -07003713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 private int findWindowOffsetLocked(int tokenPos) {
3715 final int NW = mWindows.size();
3716
3717 if (tokenPos >= mAppTokens.size()) {
3718 int i = NW;
3719 while (i > 0) {
3720 i--;
3721 WindowState win = (WindowState)mWindows.get(i);
3722 if (win.getAppToken() != null) {
3723 return i+1;
3724 }
3725 }
3726 }
3727
3728 while (tokenPos > 0) {
3729 // Find the first app token below the new position that has
3730 // a window displayed.
3731 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3732 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3733 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003734 if (wtoken.sendingToBottom) {
3735 if (DEBUG_REORDER) Log.v(TAG,
3736 "Skipping token -- currently sending to bottom");
3737 tokenPos--;
3738 continue;
3739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 int i = wtoken.windows.size();
3741 while (i > 0) {
3742 i--;
3743 WindowState win = wtoken.windows.get(i);
3744 int j = win.mChildWindows.size();
3745 while (j > 0) {
3746 j--;
3747 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003748 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 for (int pos=NW-1; pos>=0; pos--) {
3750 if (mWindows.get(pos) == cwin) {
3751 if (DEBUG_REORDER) Log.v(TAG,
3752 "Found child win @" + (pos+1));
3753 return pos+1;
3754 }
3755 }
3756 }
3757 }
3758 for (int pos=NW-1; pos>=0; pos--) {
3759 if (mWindows.get(pos) == win) {
3760 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3761 return pos+1;
3762 }
3763 }
3764 }
3765 tokenPos--;
3766 }
3767
3768 return 0;
3769 }
3770
3771 private final int reAddWindowLocked(int index, WindowState win) {
3772 final int NCW = win.mChildWindows.size();
3773 boolean added = false;
3774 for (int j=0; j<NCW; j++) {
3775 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3776 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003777 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3778 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 mWindows.add(index, win);
3780 index++;
3781 added = true;
3782 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003783 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3784 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 mWindows.add(index, cwin);
3786 index++;
3787 }
3788 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003789 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3790 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 mWindows.add(index, win);
3792 index++;
3793 }
3794 return index;
3795 }
Romain Guy06882f82009-06-10 13:36:04 -07003796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3798 final int NW = token.windows.size();
3799 for (int i=0; i<NW; i++) {
3800 index = reAddWindowLocked(index, token.windows.get(i));
3801 }
3802 return index;
3803 }
3804
3805 public void moveAppToken(int index, IBinder token) {
3806 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3807 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003808 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 }
3810
3811 synchronized(mWindowMap) {
3812 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3813 if (DEBUG_REORDER) dumpAppTokensLocked();
3814 final AppWindowToken wtoken = findAppWindowToken(token);
3815 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3816 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3817 + token + " (" + wtoken + ")");
3818 return;
3819 }
3820 mAppTokens.add(index, wtoken);
3821 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3822 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 final long origId = Binder.clearCallingIdentity();
3825 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3826 if (DEBUG_REORDER) dumpWindowsLocked();
3827 if (tmpRemoveAppWindowsLocked(wtoken)) {
3828 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3829 if (DEBUG_REORDER) dumpWindowsLocked();
3830 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3831 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3832 if (DEBUG_REORDER) dumpWindowsLocked();
3833 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 mLayoutNeeded = true;
3835 performLayoutAndPlaceSurfacesLocked();
3836 }
3837 Binder.restoreCallingIdentity(origId);
3838 }
3839 }
3840
3841 private void removeAppTokensLocked(List<IBinder> tokens) {
3842 // XXX This should be done more efficiently!
3843 // (take advantage of the fact that both lists should be
3844 // ordered in the same way.)
3845 int N = tokens.size();
3846 for (int i=0; i<N; i++) {
3847 IBinder token = tokens.get(i);
3848 final AppWindowToken wtoken = findAppWindowToken(token);
3849 if (!mAppTokens.remove(wtoken)) {
3850 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3851 + token + " (" + wtoken + ")");
3852 i--;
3853 N--;
3854 }
3855 }
3856 }
3857
Dianne Hackborna8f60182009-09-01 19:01:50 -07003858 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3859 boolean updateFocusAndLayout) {
3860 // First remove all of the windows from the list.
3861 tmpRemoveAppWindowsLocked(wtoken);
3862
3863 // Where to start adding?
3864 int pos = findWindowOffsetLocked(tokenPos);
3865
3866 // And now add them back at the correct place.
3867 pos = reAddAppWindowsLocked(pos, wtoken);
3868
3869 if (updateFocusAndLayout) {
3870 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3871 assignLayersLocked();
3872 }
3873 mLayoutNeeded = true;
3874 performLayoutAndPlaceSurfacesLocked();
3875 }
3876 }
3877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3879 // First remove all of the windows from the list.
3880 final int N = tokens.size();
3881 int i;
3882 for (i=0; i<N; i++) {
3883 WindowToken token = mTokenMap.get(tokens.get(i));
3884 if (token != null) {
3885 tmpRemoveAppWindowsLocked(token);
3886 }
3887 }
3888
3889 // Where to start adding?
3890 int pos = findWindowOffsetLocked(tokenPos);
3891
3892 // And now add them back at the correct place.
3893 for (i=0; i<N; i++) {
3894 WindowToken token = mTokenMap.get(tokens.get(i));
3895 if (token != null) {
3896 pos = reAddAppWindowsLocked(pos, token);
3897 }
3898 }
3899
Dianne Hackborna8f60182009-09-01 19:01:50 -07003900 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3901 assignLayersLocked();
3902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 mLayoutNeeded = true;
3904 performLayoutAndPlaceSurfacesLocked();
3905
3906 //dump();
3907 }
3908
3909 public void moveAppTokensToTop(List<IBinder> tokens) {
3910 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3911 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003912 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 }
3914
3915 final long origId = Binder.clearCallingIdentity();
3916 synchronized(mWindowMap) {
3917 removeAppTokensLocked(tokens);
3918 final int N = tokens.size();
3919 for (int i=0; i<N; i++) {
3920 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3921 if (wt != null) {
3922 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003923 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003924 mToTopApps.remove(wt);
3925 mToBottomApps.remove(wt);
3926 mToTopApps.add(wt);
3927 wt.sendingToBottom = false;
3928 wt.sendingToTop = true;
3929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 }
3931 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003932
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003933 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003934 moveAppWindowsLocked(tokens, mAppTokens.size());
3935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937 Binder.restoreCallingIdentity(origId);
3938 }
3939
3940 public void moveAppTokensToBottom(List<IBinder> tokens) {
3941 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3942 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003943 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 }
3945
3946 final long origId = Binder.clearCallingIdentity();
3947 synchronized(mWindowMap) {
3948 removeAppTokensLocked(tokens);
3949 final int N = tokens.size();
3950 int pos = 0;
3951 for (int i=0; i<N; i++) {
3952 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3953 if (wt != null) {
3954 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003955 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003956 mToTopApps.remove(wt);
3957 mToBottomApps.remove(wt);
3958 mToBottomApps.add(i, wt);
3959 wt.sendingToTop = false;
3960 wt.sendingToBottom = true;
3961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 pos++;
3963 }
3964 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003965
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003966 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003967 moveAppWindowsLocked(tokens, 0);
3968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 }
3970 Binder.restoreCallingIdentity(origId);
3971 }
3972
3973 // -------------------------------------------------------------
3974 // Misc IWindowSession methods
3975 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04003978 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 != PackageManager.PERMISSION_GRANTED) {
3980 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3981 }
Mike Lockwooddd884682009-10-11 16:57:08 -04003982 synchronized (mKeyguardDisabled) {
3983 mKeyguardDisabled.acquire(token, tag);
3984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 }
3986
3987 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04003988 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 != PackageManager.PERMISSION_GRANTED) {
3990 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3991 }
3992 synchronized (mKeyguardDisabled) {
3993 mKeyguardDisabled.release(token);
3994
3995 if (!mKeyguardDisabled.isAcquired()) {
3996 // if we are the last one to reenable the keyguard wait until
3997 // we have actaully finished reenabling until returning
3998 mWaitingUntilKeyguardReenabled = true;
3999 while (mWaitingUntilKeyguardReenabled) {
4000 try {
4001 mKeyguardDisabled.wait();
4002 } catch (InterruptedException e) {
4003 Thread.currentThread().interrupt();
4004 }
4005 }
4006 }
4007 }
4008 }
4009
4010 /**
4011 * @see android.app.KeyguardManager#exitKeyguardSecurely
4012 */
4013 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004014 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 != PackageManager.PERMISSION_GRANTED) {
4016 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4017 }
4018 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4019 public void onKeyguardExitResult(boolean success) {
4020 try {
4021 callback.onKeyguardExitResult(success);
4022 } catch (RemoteException e) {
4023 // Client has died, we don't care.
4024 }
4025 }
4026 });
4027 }
4028
4029 public boolean inKeyguardRestrictedInputMode() {
4030 return mPolicy.inKeyguardRestrictedKeyInputMode();
4031 }
Romain Guy06882f82009-06-10 13:36:04 -07004032
Dianne Hackbornffa42482009-09-23 22:20:11 -07004033 public void closeSystemDialogs(String reason) {
4034 synchronized(mWindowMap) {
4035 for (int i=mWindows.size()-1; i>=0; i--) {
4036 WindowState w = (WindowState)mWindows.get(i);
4037 if (w.mSurface != null) {
4038 try {
4039 w.mClient.closeSystemDialogs(reason);
4040 } catch (RemoteException e) {
4041 }
4042 }
4043 }
4044 }
4045 }
4046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 static float fixScale(float scale) {
4048 if (scale < 0) scale = 0;
4049 else if (scale > 20) scale = 20;
4050 return Math.abs(scale);
4051 }
Romain Guy06882f82009-06-10 13:36:04 -07004052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 public void setAnimationScale(int which, float scale) {
4054 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4055 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004056 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 }
4058
4059 if (scale < 0) scale = 0;
4060 else if (scale > 20) scale = 20;
4061 scale = Math.abs(scale);
4062 switch (which) {
4063 case 0: mWindowAnimationScale = fixScale(scale); break;
4064 case 1: mTransitionAnimationScale = fixScale(scale); break;
4065 }
Romain Guy06882f82009-06-10 13:36:04 -07004066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 // Persist setting
4068 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4069 }
Romain Guy06882f82009-06-10 13:36:04 -07004070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 public void setAnimationScales(float[] scales) {
4072 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4073 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004074 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 }
4076
4077 if (scales != null) {
4078 if (scales.length >= 1) {
4079 mWindowAnimationScale = fixScale(scales[0]);
4080 }
4081 if (scales.length >= 2) {
4082 mTransitionAnimationScale = fixScale(scales[1]);
4083 }
4084 }
Romain Guy06882f82009-06-10 13:36:04 -07004085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 // Persist setting
4087 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4088 }
Romain Guy06882f82009-06-10 13:36:04 -07004089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 public float getAnimationScale(int which) {
4091 switch (which) {
4092 case 0: return mWindowAnimationScale;
4093 case 1: return mTransitionAnimationScale;
4094 }
4095 return 0;
4096 }
Romain Guy06882f82009-06-10 13:36:04 -07004097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 public float[] getAnimationScales() {
4099 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4100 }
Romain Guy06882f82009-06-10 13:36:04 -07004101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 public int getSwitchState(int sw) {
4103 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4104 "getSwitchState()")) {
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(sw);
4108 }
Romain Guy06882f82009-06-10 13:36:04 -07004109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 public int getSwitchStateForDevice(int devid, int sw) {
4111 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4112 "getSwitchStateForDevice()")) {
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 }
4115 return KeyInputQueue.getSwitchState(devid, sw);
4116 }
Romain Guy06882f82009-06-10 13:36:04 -07004117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 public int getScancodeState(int sw) {
4119 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4120 "getScancodeState()")) {
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(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 getScancodeStateForDevice(int devid, int sw) {
4127 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4128 "getScancodeStateForDevice()")) {
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.getScancodeState(devid, 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 getKeycodeState(int sw) {
4135 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4136 "getKeycodeState()")) {
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(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 int getKeycodeStateForDevice(int devid, int sw) {
4143 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4144 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004145 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004147 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 }
Romain Guy06882f82009-06-10 13:36:04 -07004149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4151 return KeyInputQueue.hasKeys(keycodes, keyExists);
4152 }
Romain Guy06882f82009-06-10 13:36:04 -07004153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 public void enableScreenAfterBoot() {
4155 synchronized(mWindowMap) {
4156 if (mSystemBooted) {
4157 return;
4158 }
4159 mSystemBooted = true;
4160 }
Romain Guy06882f82009-06-10 13:36:04 -07004161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 performEnableScreen();
4163 }
Romain Guy06882f82009-06-10 13:36:04 -07004164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 public void enableScreenIfNeededLocked() {
4166 if (mDisplayEnabled) {
4167 return;
4168 }
4169 if (!mSystemBooted) {
4170 return;
4171 }
4172 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4173 }
Romain Guy06882f82009-06-10 13:36:04 -07004174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 public void performEnableScreen() {
4176 synchronized(mWindowMap) {
4177 if (mDisplayEnabled) {
4178 return;
4179 }
4180 if (!mSystemBooted) {
4181 return;
4182 }
Romain Guy06882f82009-06-10 13:36:04 -07004183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 // Don't enable the screen until all existing windows
4185 // have been drawn.
4186 final int N = mWindows.size();
4187 for (int i=0; i<N; i++) {
4188 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004189 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 return;
4191 }
4192 }
Romain Guy06882f82009-06-10 13:36:04 -07004193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 mDisplayEnabled = true;
4195 if (false) {
4196 Log.i(TAG, "ENABLING SCREEN!");
4197 StringWriter sw = new StringWriter();
4198 PrintWriter pw = new PrintWriter(sw);
4199 this.dump(null, pw, null);
4200 Log.i(TAG, sw.toString());
4201 }
4202 try {
4203 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4204 if (surfaceFlinger != null) {
4205 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4206 Parcel data = Parcel.obtain();
4207 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4208 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4209 data, null, 0);
4210 data.recycle();
4211 }
4212 } catch (RemoteException ex) {
4213 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4214 }
4215 }
Romain Guy06882f82009-06-10 13:36:04 -07004216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004220 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4221 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 }
Romain Guy06882f82009-06-10 13:36:04 -07004223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 public void setInTouchMode(boolean mode) {
4225 synchronized(mWindowMap) {
4226 mInTouchMode = mode;
4227 }
4228 }
4229
Romain Guy06882f82009-06-10 13:36:04 -07004230 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004231 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004233 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004234 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 }
4236
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004237 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 }
Romain Guy06882f82009-06-10 13:36:04 -07004239
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004240 public void setRotationUnchecked(int rotation,
4241 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 if(DEBUG_ORIENTATION) Log.v(TAG,
4243 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 long origId = Binder.clearCallingIdentity();
4246 boolean changed;
4247 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004248 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 }
Romain Guy06882f82009-06-10 13:36:04 -07004250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 if (changed) {
4252 sendNewConfiguration();
4253 synchronized(mWindowMap) {
4254 mLayoutNeeded = true;
4255 performLayoutAndPlaceSurfacesLocked();
4256 }
4257 } else if (alwaysSendConfiguration) {
4258 //update configuration ignoring orientation change
4259 sendNewConfiguration();
4260 }
Romain Guy06882f82009-06-10 13:36:04 -07004261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 Binder.restoreCallingIdentity(origId);
4263 }
Romain Guy06882f82009-06-10 13:36:04 -07004264
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004265 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 boolean changed;
4267 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4268 rotation = mRequestedRotation;
4269 } else {
4270 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004271 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 }
4273 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004274 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 mRotation, mDisplayEnabled);
4276 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4277 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004280 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 "Rotation changed to " + rotation
4282 + " from " + mRotation
4283 + " (forceApp=" + mForcedAppOrientation
4284 + ", req=" + mRequestedRotation + ")");
4285 mRotation = rotation;
4286 mWindowsFreezingScreen = true;
4287 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4288 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4289 2000);
4290 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004291 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 mQueue.setOrientation(rotation);
4293 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004294 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 }
4296 for (int i=mWindows.size()-1; i>=0; i--) {
4297 WindowState w = (WindowState)mWindows.get(i);
4298 if (w.mSurface != null) {
4299 w.mOrientationChanging = true;
4300 }
4301 }
4302 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4303 try {
4304 mRotationWatchers.get(i).onRotationChanged(rotation);
4305 } catch (RemoteException e) {
4306 }
4307 }
4308 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 return changed;
4311 }
Romain Guy06882f82009-06-10 13:36:04 -07004312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 public int getRotation() {
4314 return mRotation;
4315 }
4316
4317 public int watchRotation(IRotationWatcher watcher) {
4318 final IBinder watcherBinder = watcher.asBinder();
4319 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4320 public void binderDied() {
4321 synchronized (mWindowMap) {
4322 for (int i=0; i<mRotationWatchers.size(); i++) {
4323 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004324 IRotationWatcher removed = mRotationWatchers.remove(i);
4325 if (removed != null) {
4326 removed.asBinder().unlinkToDeath(this, 0);
4327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328 i--;
4329 }
4330 }
4331 }
4332 }
4333 };
Romain Guy06882f82009-06-10 13:36:04 -07004334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 synchronized (mWindowMap) {
4336 try {
4337 watcher.asBinder().linkToDeath(dr, 0);
4338 mRotationWatchers.add(watcher);
4339 } catch (RemoteException e) {
4340 // Client died, no cleanup needed.
4341 }
Romain Guy06882f82009-06-10 13:36:04 -07004342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 return mRotation;
4344 }
4345 }
4346
4347 /**
4348 * Starts the view server on the specified port.
4349 *
4350 * @param port The port to listener to.
4351 *
4352 * @return True if the server was successfully started, false otherwise.
4353 *
4354 * @see com.android.server.ViewServer
4355 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4356 */
4357 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004358 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 return false;
4360 }
4361
4362 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4363 return false;
4364 }
4365
4366 if (port < 1024) {
4367 return false;
4368 }
4369
4370 if (mViewServer != null) {
4371 if (!mViewServer.isRunning()) {
4372 try {
4373 return mViewServer.start();
4374 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004375 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
4377 }
4378 return false;
4379 }
4380
4381 try {
4382 mViewServer = new ViewServer(this, port);
4383 return mViewServer.start();
4384 } catch (IOException e) {
4385 Log.w(TAG, "View server did not start");
4386 }
4387 return false;
4388 }
4389
Romain Guy06882f82009-06-10 13:36:04 -07004390 private boolean isSystemSecure() {
4391 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4392 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4393 }
4394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 /**
4396 * Stops the view server if it exists.
4397 *
4398 * @return True if the server stopped, false if it wasn't started or
4399 * couldn't be stopped.
4400 *
4401 * @see com.android.server.ViewServer
4402 */
4403 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004404 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 return false;
4406 }
4407
4408 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4409 return false;
4410 }
4411
4412 if (mViewServer != null) {
4413 return mViewServer.stop();
4414 }
4415 return false;
4416 }
4417
4418 /**
4419 * Indicates whether the view server is running.
4420 *
4421 * @return True if the server is running, false otherwise.
4422 *
4423 * @see com.android.server.ViewServer
4424 */
4425 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004426 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 return false;
4428 }
4429
4430 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4431 return false;
4432 }
4433
4434 return mViewServer != null && mViewServer.isRunning();
4435 }
4436
4437 /**
4438 * Lists all availble windows in the system. The listing is written in the
4439 * specified Socket's output stream with the following syntax:
4440 * windowHashCodeInHexadecimal windowName
4441 * Each line of the ouput represents a different window.
4442 *
4443 * @param client The remote client to send the listing to.
4444 * @return False if an error occured, true otherwise.
4445 */
4446 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004447 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 return false;
4449 }
4450
4451 boolean result = true;
4452
4453 Object[] windows;
4454 synchronized (mWindowMap) {
4455 windows = new Object[mWindows.size()];
4456 //noinspection unchecked
4457 windows = mWindows.toArray(windows);
4458 }
4459
4460 BufferedWriter out = null;
4461
4462 // Any uncaught exception will crash the system process
4463 try {
4464 OutputStream clientStream = client.getOutputStream();
4465 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4466
4467 final int count = windows.length;
4468 for (int i = 0; i < count; i++) {
4469 final WindowState w = (WindowState) windows[i];
4470 out.write(Integer.toHexString(System.identityHashCode(w)));
4471 out.write(' ');
4472 out.append(w.mAttrs.getTitle());
4473 out.write('\n');
4474 }
4475
4476 out.write("DONE.\n");
4477 out.flush();
4478 } catch (Exception e) {
4479 result = false;
4480 } finally {
4481 if (out != null) {
4482 try {
4483 out.close();
4484 } catch (IOException e) {
4485 result = false;
4486 }
4487 }
4488 }
4489
4490 return result;
4491 }
4492
4493 /**
4494 * Sends a command to a target window. The result of the command, if any, will be
4495 * written in the output stream of the specified socket.
4496 *
4497 * The parameters must follow this syntax:
4498 * windowHashcode extra
4499 *
4500 * Where XX is the length in characeters of the windowTitle.
4501 *
4502 * The first parameter is the target window. The window with the specified hashcode
4503 * will be the target. If no target can be found, nothing happens. The extra parameters
4504 * will be delivered to the target window and as parameters to the command itself.
4505 *
4506 * @param client The remote client to sent the result, if any, to.
4507 * @param command The command to execute.
4508 * @param parameters The command parameters.
4509 *
4510 * @return True if the command was successfully delivered, false otherwise. This does
4511 * not indicate whether the command itself was successful.
4512 */
4513 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004514 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 return false;
4516 }
4517
4518 boolean success = true;
4519 Parcel data = null;
4520 Parcel reply = null;
4521
4522 // Any uncaught exception will crash the system process
4523 try {
4524 // Find the hashcode of the window
4525 int index = parameters.indexOf(' ');
4526 if (index == -1) {
4527 index = parameters.length();
4528 }
4529 final String code = parameters.substring(0, index);
4530 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4531
4532 // Extract the command's parameter after the window description
4533 if (index < parameters.length()) {
4534 parameters = parameters.substring(index + 1);
4535 } else {
4536 parameters = "";
4537 }
4538
4539 final WindowManagerService.WindowState window = findWindow(hashCode);
4540 if (window == null) {
4541 return false;
4542 }
4543
4544 data = Parcel.obtain();
4545 data.writeInterfaceToken("android.view.IWindow");
4546 data.writeString(command);
4547 data.writeString(parameters);
4548 data.writeInt(1);
4549 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4550
4551 reply = Parcel.obtain();
4552
4553 final IBinder binder = window.mClient.asBinder();
4554 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4555 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4556
4557 reply.readException();
4558
4559 } catch (Exception e) {
4560 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4561 success = false;
4562 } finally {
4563 if (data != null) {
4564 data.recycle();
4565 }
4566 if (reply != null) {
4567 reply.recycle();
4568 }
4569 }
4570
4571 return success;
4572 }
4573
4574 private WindowState findWindow(int hashCode) {
4575 if (hashCode == -1) {
4576 return getFocusedWindow();
4577 }
4578
4579 synchronized (mWindowMap) {
4580 final ArrayList windows = mWindows;
4581 final int count = windows.size();
4582
4583 for (int i = 0; i < count; i++) {
4584 WindowState w = (WindowState) windows.get(i);
4585 if (System.identityHashCode(w) == hashCode) {
4586 return w;
4587 }
4588 }
4589 }
4590
4591 return null;
4592 }
4593
4594 /*
4595 * Instruct the Activity Manager to fetch the current configuration and broadcast
4596 * that to config-changed listeners if appropriate.
4597 */
4598 void sendNewConfiguration() {
4599 try {
4600 mActivityManager.updateConfiguration(null);
4601 } catch (RemoteException e) {
4602 }
4603 }
Romain Guy06882f82009-06-10 13:36:04 -07004604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004605 public Configuration computeNewConfiguration() {
4606 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004607 return computeNewConfigurationLocked();
4608 }
4609 }
Romain Guy06882f82009-06-10 13:36:04 -07004610
Dianne Hackbornc485a602009-03-24 22:39:49 -07004611 Configuration computeNewConfigurationLocked() {
4612 Configuration config = new Configuration();
4613 if (!computeNewConfigurationLocked(config)) {
4614 return null;
4615 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004616 return config;
4617 }
Romain Guy06882f82009-06-10 13:36:04 -07004618
Dianne Hackbornc485a602009-03-24 22:39:49 -07004619 boolean computeNewConfigurationLocked(Configuration config) {
4620 if (mDisplay == null) {
4621 return false;
4622 }
4623 mQueue.getInputConfiguration(config);
4624 final int dw = mDisplay.getWidth();
4625 final int dh = mDisplay.getHeight();
4626 int orientation = Configuration.ORIENTATION_SQUARE;
4627 if (dw < dh) {
4628 orientation = Configuration.ORIENTATION_PORTRAIT;
4629 } else if (dw > dh) {
4630 orientation = Configuration.ORIENTATION_LANDSCAPE;
4631 }
4632 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004633
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004634 DisplayMetrics dm = new DisplayMetrics();
4635 mDisplay.getMetrics(dm);
4636 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4637
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004638 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004639 // Note we only do this once because at this point we don't
4640 // expect the screen to change in this way at runtime, and want
4641 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004642 int longSize = dw;
4643 int shortSize = dh;
4644 if (longSize < shortSize) {
4645 int tmp = longSize;
4646 longSize = shortSize;
4647 shortSize = tmp;
4648 }
4649 longSize = (int)(longSize/dm.density);
4650 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004651
Dianne Hackborn723738c2009-06-25 19:48:04 -07004652 // These semi-magic numbers define our compatibility modes for
4653 // applications with different screens. Don't change unless you
4654 // make sure to test lots and lots of apps!
4655 if (longSize < 470) {
4656 // This is shorter than an HVGA normal density screen (which
4657 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004658 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4659 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004660 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004661 // Is this a large screen?
4662 if (longSize > 640 && shortSize >= 480) {
4663 // VGA or larger screens at medium density are the point
4664 // at which we consider it to be a large screen.
4665 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4666 } else {
4667 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4668
4669 // If this screen is wider than normal HVGA, or taller
4670 // than FWVGA, then for old apps we want to run in size
4671 // compatibility mode.
4672 if (shortSize > 321 || longSize > 570) {
4673 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4674 }
4675 }
4676
4677 // Is this a long screen?
4678 if (((longSize*3)/5) >= (shortSize-1)) {
4679 // Anything wider than WVGA (5:3) is considering to be long.
4680 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4681 } else {
4682 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4683 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004684 }
4685 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004686 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004687
Dianne Hackbornc485a602009-03-24 22:39:49 -07004688 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4689 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4690 mPolicy.adjustConfigurationLw(config);
4691 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 }
Romain Guy06882f82009-06-10 13:36:04 -07004693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 // -------------------------------------------------------------
4695 // Input Events and Focus Management
4696 // -------------------------------------------------------------
4697
4698 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004699 long curTime = SystemClock.uptimeMillis();
4700
Michael Chane10de972009-05-18 11:24:50 -07004701 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004702 if (mLastTouchEventType == eventType &&
4703 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4704 return;
4705 }
4706 mLastUserActivityCallTime = curTime;
4707 mLastTouchEventType = eventType;
4708 }
4709
4710 if (targetWin == null
4711 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4712 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 }
4714 }
4715
4716 // tells if it's a cheek event or not -- this function is stateful
4717 private static final int EVENT_NONE = 0;
4718 private static final int EVENT_UNKNOWN = 0;
4719 private static final int EVENT_CHEEK = 0;
4720 private static final int EVENT_IGNORE_DURATION = 300; // ms
4721 private static final float CHEEK_THRESHOLD = 0.6f;
4722 private int mEventState = EVENT_NONE;
4723 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 private int eventType(MotionEvent ev) {
4726 float size = ev.getSize();
4727 switch (ev.getAction()) {
4728 case MotionEvent.ACTION_DOWN:
4729 mEventSize = size;
4730 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4731 case MotionEvent.ACTION_UP:
4732 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004733 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 case MotionEvent.ACTION_MOVE:
4735 final int N = ev.getHistorySize();
4736 if (size > mEventSize) mEventSize = size;
4737 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4738 for (int i=0; i<N; i++) {
4739 size = ev.getHistoricalSize(i);
4740 if (size > mEventSize) mEventSize = size;
4741 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4742 }
4743 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4744 return TOUCH_EVENT;
4745 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004746 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 }
4748 default:
4749 // not good
4750 return OTHER_EVENT;
4751 }
4752 }
4753
4754 /**
4755 * @return Returns true if event was dispatched, false if it was dropped for any reason
4756 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004757 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4759 "dispatchPointer " + ev);
4760
Michael Chan53071d62009-05-13 17:29:48 -07004761 if (MEASURE_LATENCY) {
4762 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4763 }
4764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004766 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004767
Michael Chan53071d62009-05-13 17:29:48 -07004768 if (MEASURE_LATENCY) {
4769 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4770 }
4771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 if (action == MotionEvent.ACTION_UP) {
4775 // let go of our target
4776 mKeyWaiter.mMotionTarget = null;
4777 mPowerManager.logPointerUpEvent();
4778 } else if (action == MotionEvent.ACTION_DOWN) {
4779 mPowerManager.logPointerDownEvent();
4780 }
4781
4782 if (targetObj == null) {
4783 // In this case we are either dropping the event, or have received
4784 // a move or up without a down. It is common to receive move
4785 // events in such a way, since this means the user is moving the
4786 // pointer without actually pressing down. All other cases should
4787 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004788 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004789 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4790 }
4791 if (qev != null) {
4792 mQueue.recycleEvent(qev);
4793 }
4794 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004795 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 }
4797 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4798 if (qev != null) {
4799 mQueue.recycleEvent(qev);
4800 }
4801 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004802 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803 }
Romain Guy06882f82009-06-10 13:36:04 -07004804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004808 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809
4810 //Log.i(TAG, "Sending " + ev + " to " + target);
4811
4812 if (uid != 0 && uid != target.mSession.mUid) {
4813 if (mContext.checkPermission(
4814 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4815 != PackageManager.PERMISSION_GRANTED) {
4816 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4817 + pid + " uid " + uid + " to window " + target
4818 + " owned by uid " + target.mSession.mUid);
4819 if (qev != null) {
4820 mQueue.recycleEvent(qev);
4821 }
4822 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004823 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 }
4825 }
4826
Michael Chan53071d62009-05-13 17:29:48 -07004827 if (MEASURE_LATENCY) {
4828 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4829 }
4830
Romain Guy06882f82009-06-10 13:36:04 -07004831 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4833 //target wants to ignore fat touch events
4834 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4835 //explicit flag to return without processing event further
4836 boolean returnFlag = false;
4837 if((action == MotionEvent.ACTION_DOWN)) {
4838 mFatTouch = false;
4839 if(cheekPress) {
4840 mFatTouch = true;
4841 returnFlag = true;
4842 }
4843 } else {
4844 if(action == MotionEvent.ACTION_UP) {
4845 if(mFatTouch) {
4846 //earlier even was invalid doesnt matter if current up is cheekpress or not
4847 mFatTouch = false;
4848 returnFlag = true;
4849 } else if(cheekPress) {
4850 //cancel the earlier event
4851 ev.setAction(MotionEvent.ACTION_CANCEL);
4852 action = MotionEvent.ACTION_CANCEL;
4853 }
4854 } else if(action == MotionEvent.ACTION_MOVE) {
4855 if(mFatTouch) {
4856 //two cases here
4857 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004858 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 returnFlag = true;
4860 } else if(cheekPress) {
4861 //valid down followed by invalid moves
4862 //an invalid move have to cancel earlier action
4863 ev.setAction(MotionEvent.ACTION_CANCEL);
4864 action = MotionEvent.ACTION_CANCEL;
4865 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4866 //note that the subsequent invalid moves will not get here
4867 mFatTouch = true;
4868 }
4869 }
4870 } //else if action
4871 if(returnFlag) {
4872 //recycle que, ev
4873 if (qev != null) {
4874 mQueue.recycleEvent(qev);
4875 }
4876 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004877 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 }
4879 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004880
Michael Chan9f028e62009-08-04 17:37:46 -07004881 // Enable this for testing the "right" value
4882 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004883 int max_events_per_sec = 35;
4884 try {
4885 max_events_per_sec = Integer.parseInt(SystemProperties
4886 .get("windowsmgr.max_events_per_sec"));
4887 if (max_events_per_sec < 1) {
4888 max_events_per_sec = 35;
4889 }
4890 } catch (NumberFormatException e) {
4891 }
4892 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4893 }
4894
4895 /*
4896 * Throttle events to minimize CPU usage when there's a flood of events
4897 * e.g. constant contact with the screen
4898 */
4899 if (action == MotionEvent.ACTION_MOVE) {
4900 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4901 long now = SystemClock.uptimeMillis();
4902 if (now < nextEventTime) {
4903 try {
4904 Thread.sleep(nextEventTime - now);
4905 } catch (InterruptedException e) {
4906 }
4907 mLastTouchEventTime = nextEventTime;
4908 } else {
4909 mLastTouchEventTime = now;
4910 }
4911 }
4912
Michael Chan53071d62009-05-13 17:29:48 -07004913 if (MEASURE_LATENCY) {
4914 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4915 }
4916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 synchronized(mWindowMap) {
4918 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4919 mKeyWaiter.bindTargetWindowLocked(target,
4920 KeyWaiter.RETURN_PENDING_POINTER, qev);
4921 ev = null;
4922 } else {
4923 if (action == MotionEvent.ACTION_DOWN) {
4924 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4925 if (out != null) {
4926 MotionEvent oev = MotionEvent.obtain(ev);
4927 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4928 do {
4929 final Rect frame = out.mFrame;
4930 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4931 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004932 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 } catch (android.os.RemoteException e) {
4934 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4935 }
4936 oev.offsetLocation((float)frame.left, (float)frame.top);
4937 out = out.mNextOutsideTouch;
4938 } while (out != null);
4939 mKeyWaiter.mOutsideTouchTargets = null;
4940 }
4941 }
4942 final Rect frame = target.mFrame;
4943 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4944 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004945
4946 // If we are on top of the wallpaper, then the wallpaper also
4947 // gets to see this movement.
4948 if (mWallpaperTarget == target) {
4949 sendPointerToWallpaperLocked(target, ev, eventTime);
4950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 }
4952 }
Romain Guy06882f82009-06-10 13:36:04 -07004953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 // finally offset the event to the target's coordinate system and
4955 // dispatch the event.
4956 try {
4957 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4958 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4959 }
Michael Chan53071d62009-05-13 17:29:48 -07004960
4961 if (MEASURE_LATENCY) {
4962 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4963 }
4964
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004965 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004966
4967 if (MEASURE_LATENCY) {
4968 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4969 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004970 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 } catch (android.os.RemoteException e) {
4972 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4973 mKeyWaiter.mMotionTarget = null;
4974 try {
4975 removeWindow(target.mSession, target.mClient);
4976 } catch (java.util.NoSuchElementException ex) {
4977 // This will happen if the window has already been
4978 // removed.
4979 }
4980 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004981 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 }
Romain Guy06882f82009-06-10 13:36:04 -07004983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 /**
4985 * @return Returns true if event was dispatched, false if it was dropped for any reason
4986 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004987 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 if (DEBUG_INPUT) Log.v(
4989 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004992 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 if (focusObj == null) {
4994 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4995 if (qev != null) {
4996 mQueue.recycleEvent(qev);
4997 }
4998 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004999 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 }
5001 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5002 if (qev != null) {
5003 mQueue.recycleEvent(qev);
5004 }
5005 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005006 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 }
Romain Guy06882f82009-06-10 13:36:04 -07005008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 if (uid != 0 && uid != focus.mSession.mUid) {
5012 if (mContext.checkPermission(
5013 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5014 != PackageManager.PERMISSION_GRANTED) {
5015 Log.w(TAG, "Permission denied: injecting key event from pid "
5016 + pid + " uid " + uid + " to window " + focus
5017 + " owned by uid " + focus.mSession.mUid);
5018 if (qev != null) {
5019 mQueue.recycleEvent(qev);
5020 }
5021 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005022 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 }
5024 }
Romain Guy06882f82009-06-10 13:36:04 -07005025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 synchronized(mWindowMap) {
5029 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5030 mKeyWaiter.bindTargetWindowLocked(focus,
5031 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5032 // We don't deliver movement events to the client, we hold
5033 // them and wait for them to call back.
5034 ev = null;
5035 } else {
5036 mKeyWaiter.bindTargetWindowLocked(focus);
5037 }
5038 }
Romain Guy06882f82009-06-10 13:36:04 -07005039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005041 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005042 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 } catch (android.os.RemoteException e) {
5044 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5045 try {
5046 removeWindow(focus.mSession, focus.mClient);
5047 } catch (java.util.NoSuchElementException ex) {
5048 // This will happen if the window has already been
5049 // removed.
5050 }
5051 }
Romain Guy06882f82009-06-10 13:36:04 -07005052
Dianne Hackborncfaef692009-06-15 14:24:44 -07005053 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 }
Romain Guy06882f82009-06-10 13:36:04 -07005055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 /**
5057 * @return Returns true if event was dispatched, false if it was dropped for any reason
5058 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005059 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5061
5062 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005063 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 if (focusObj == null) {
5065 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005066 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 }
5068 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005069 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
Romain Guy06882f82009-06-10 13:36:04 -07005071
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005072 // Okay we have finished waiting for the last event to be processed.
5073 // First off, if this is a repeat event, check to see if there is
5074 // a corresponding up event in the queue. If there is, we will
5075 // just drop the repeat, because it makes no sense to repeat after
5076 // the user has released a key. (This is especially important for
5077 // long presses.)
5078 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5079 return INJECT_SUCCEEDED;
5080 }
5081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 if (DEBUG_INPUT) Log.v(
5085 TAG, "Dispatching to " + focus + ": " + event);
5086
5087 if (uid != 0 && uid != focus.mSession.mUid) {
5088 if (mContext.checkPermission(
5089 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5090 != PackageManager.PERMISSION_GRANTED) {
5091 Log.w(TAG, "Permission denied: injecting key event from pid "
5092 + pid + " uid " + uid + " to window " + focus
5093 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005094 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 }
5096 }
Romain Guy06882f82009-06-10 13:36:04 -07005097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 synchronized(mWindowMap) {
5099 mKeyWaiter.bindTargetWindowLocked(focus);
5100 }
5101
5102 // NOSHIP extra state logging
5103 mKeyWaiter.recordDispatchState(event, focus);
5104 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 try {
5107 if (DEBUG_INPUT || DEBUG_FOCUS) {
5108 Log.v(TAG, "Delivering key " + event.getKeyCode()
5109 + " to " + focus);
5110 }
5111 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005112 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 } catch (android.os.RemoteException e) {
5114 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5115 try {
5116 removeWindow(focus.mSession, focus.mClient);
5117 } catch (java.util.NoSuchElementException ex) {
5118 // This will happen if the window has already been
5119 // removed.
5120 }
5121 }
Romain Guy06882f82009-06-10 13:36:04 -07005122
Dianne Hackborncfaef692009-06-15 14:24:44 -07005123 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 }
Romain Guy06882f82009-06-10 13:36:04 -07005125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 public void pauseKeyDispatching(IBinder _token) {
5127 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5128 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005129 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 }
5131
5132 synchronized (mWindowMap) {
5133 WindowToken token = mTokenMap.get(_token);
5134 if (token != null) {
5135 mKeyWaiter.pauseDispatchingLocked(token);
5136 }
5137 }
5138 }
5139
5140 public void resumeKeyDispatching(IBinder _token) {
5141 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5142 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005143 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 }
5145
5146 synchronized (mWindowMap) {
5147 WindowToken token = mTokenMap.get(_token);
5148 if (token != null) {
5149 mKeyWaiter.resumeDispatchingLocked(token);
5150 }
5151 }
5152 }
5153
5154 public void setEventDispatching(boolean enabled) {
5155 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5156 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005157 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005158 }
5159
5160 synchronized (mWindowMap) {
5161 mKeyWaiter.setEventDispatchingLocked(enabled);
5162 }
5163 }
Romain Guy06882f82009-06-10 13:36:04 -07005164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 /**
5166 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005167 *
5168 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * {@link SystemClock#uptimeMillis()} as the timebase.)
5170 * @param sync If true, wait for the event to be completed before returning to the caller.
5171 * @return Returns true if event was dispatched, false if it was dropped for any reason
5172 */
5173 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5174 long downTime = ev.getDownTime();
5175 long eventTime = ev.getEventTime();
5176
5177 int action = ev.getAction();
5178 int code = ev.getKeyCode();
5179 int repeatCount = ev.getRepeatCount();
5180 int metaState = ev.getMetaState();
5181 int deviceId = ev.getDeviceId();
5182 int scancode = ev.getScanCode();
5183
5184 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5185 if (downTime == 0) downTime = eventTime;
5186
5187 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005188 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005190 final int pid = Binder.getCallingPid();
5191 final int uid = Binder.getCallingUid();
5192 final long ident = Binder.clearCallingIdentity();
5193 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005195 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005197 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005198 switch (result) {
5199 case INJECT_NO_PERMISSION:
5200 throw new SecurityException(
5201 "Injecting to another application requires INJECT_EVENT permission");
5202 case INJECT_SUCCEEDED:
5203 return true;
5204 }
5205 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 }
5207
5208 /**
5209 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005210 *
5211 * @param ev A motion event describing the pointer (touch) action. (As noted in
5212 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 * {@link SystemClock#uptimeMillis()} as the timebase.)
5214 * @param sync If true, wait for the event to be completed before returning to the caller.
5215 * @return Returns true if event was dispatched, false if it was dropped for any reason
5216 */
5217 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005218 final int pid = Binder.getCallingPid();
5219 final int uid = Binder.getCallingUid();
5220 final long ident = Binder.clearCallingIdentity();
5221 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005223 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005225 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005226 switch (result) {
5227 case INJECT_NO_PERMISSION:
5228 throw new SecurityException(
5229 "Injecting to another application requires INJECT_EVENT permission");
5230 case INJECT_SUCCEEDED:
5231 return true;
5232 }
5233 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 }
Romain Guy06882f82009-06-10 13:36:04 -07005235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 /**
5237 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005238 *
5239 * @param ev A motion event describing the trackball action. (As noted in
5240 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 * {@link SystemClock#uptimeMillis()} as the timebase.)
5242 * @param sync If true, wait for the event to be completed before returning to the caller.
5243 * @return Returns true if event was dispatched, false if it was dropped for any reason
5244 */
5245 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005246 final int pid = Binder.getCallingPid();
5247 final int uid = Binder.getCallingUid();
5248 final long ident = Binder.clearCallingIdentity();
5249 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005251 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005253 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005254 switch (result) {
5255 case INJECT_NO_PERMISSION:
5256 throw new SecurityException(
5257 "Injecting to another application requires INJECT_EVENT permission");
5258 case INJECT_SUCCEEDED:
5259 return true;
5260 }
5261 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 }
Romain Guy06882f82009-06-10 13:36:04 -07005263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 private WindowState getFocusedWindow() {
5265 synchronized (mWindowMap) {
5266 return getFocusedWindowLocked();
5267 }
5268 }
5269
5270 private WindowState getFocusedWindowLocked() {
5271 return mCurrentFocus;
5272 }
Romain Guy06882f82009-06-10 13:36:04 -07005273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 /**
5275 * This class holds the state for dispatching key events. This state
5276 * is protected by the KeyWaiter instance, NOT by the window lock. You
5277 * can be holding the main window lock while acquire the KeyWaiter lock,
5278 * but not the other way around.
5279 */
5280 final class KeyWaiter {
5281 // NOSHIP debugging
5282 public class DispatchState {
5283 private KeyEvent event;
5284 private WindowState focus;
5285 private long time;
5286 private WindowState lastWin;
5287 private IBinder lastBinder;
5288 private boolean finished;
5289 private boolean gotFirstWindow;
5290 private boolean eventDispatching;
5291 private long timeToSwitch;
5292 private boolean wasFrozen;
5293 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005294 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5297 focus = theFocus;
5298 event = theEvent;
5299 time = System.currentTimeMillis();
5300 // snapshot KeyWaiter state
5301 lastWin = mLastWin;
5302 lastBinder = mLastBinder;
5303 finished = mFinished;
5304 gotFirstWindow = mGotFirstWindow;
5305 eventDispatching = mEventDispatching;
5306 timeToSwitch = mTimeToSwitch;
5307 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005308 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 // cache the paused state at ctor time as well
5310 if (theFocus == null || theFocus.mToken == null) {
5311 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5312 focusPaused = false;
5313 } else {
5314 focusPaused = theFocus.mToken.paused;
5315 }
5316 }
Romain Guy06882f82009-06-10 13:36:04 -07005317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 public String toString() {
5319 return "{{" + event + " to " + focus + " @ " + time
5320 + " lw=" + lastWin + " lb=" + lastBinder
5321 + " fin=" + finished + " gfw=" + gotFirstWindow
5322 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005323 + " wf=" + wasFrozen + " fp=" + focusPaused
5324 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 }
5326 };
5327 private DispatchState mDispatchState = null;
5328 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5329 mDispatchState = new DispatchState(theEvent, theFocus);
5330 }
5331 // END NOSHIP
5332
5333 public static final int RETURN_NOTHING = 0;
5334 public static final int RETURN_PENDING_POINTER = 1;
5335 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 final Object SKIP_TARGET_TOKEN = new Object();
5338 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 private WindowState mLastWin = null;
5341 private IBinder mLastBinder = null;
5342 private boolean mFinished = true;
5343 private boolean mGotFirstWindow = false;
5344 private boolean mEventDispatching = true;
5345 private long mTimeToSwitch = 0;
5346 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 // Target of Motion events
5349 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 // Windows above the target who would like to receive an "outside"
5352 // touch event for any down events outside of them.
5353 WindowState mOutsideTouchTargets;
5354
5355 /**
5356 * Wait for the last event dispatch to complete, then find the next
5357 * target that should receive the given event and wait for that one
5358 * to be ready to receive it.
5359 */
5360 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5361 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005362 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 long startTime = SystemClock.uptimeMillis();
5364 long keyDispatchingTimeout = 5 * 1000;
5365 long waitedFor = 0;
5366
5367 while (true) {
5368 // Figure out which window we care about. It is either the
5369 // last window we are waiting to have process the event or,
5370 // if none, then the next window we think the event should go
5371 // to. Note: we retrieve mLastWin outside of the lock, so
5372 // it may change before we lock. Thus we must check it again.
5373 WindowState targetWin = mLastWin;
5374 boolean targetIsNew = targetWin == null;
5375 if (DEBUG_INPUT) Log.v(
5376 TAG, "waitForLastKey: mFinished=" + mFinished +
5377 ", mLastWin=" + mLastWin);
5378 if (targetIsNew) {
5379 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005380 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 if (target == SKIP_TARGET_TOKEN) {
5382 // The user has pressed a special key, and we are
5383 // dropping all pending events before it.
5384 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5385 + " " + nextMotion);
5386 return null;
5387 }
5388 if (target == CONSUMED_EVENT_TOKEN) {
5389 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5390 + " " + nextMotion);
5391 return target;
5392 }
5393 targetWin = (WindowState)target;
5394 }
Romain Guy06882f82009-06-10 13:36:04 -07005395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 // Now: is it okay to send the next event to this window?
5399 synchronized (this) {
5400 // First: did we come here based on the last window not
5401 // being null, but it changed by the time we got here?
5402 // If so, try again.
5403 if (!targetIsNew && mLastWin == null) {
5404 continue;
5405 }
Romain Guy06882f82009-06-10 13:36:04 -07005406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 // We never dispatch events if not finished with the
5408 // last one, or the display is frozen.
5409 if (mFinished && !mDisplayFrozen) {
5410 // If event dispatching is disabled, then we
5411 // just consume the events.
5412 if (!mEventDispatching) {
5413 if (DEBUG_INPUT) Log.v(TAG,
5414 "Skipping event; dispatching disabled: "
5415 + nextKey + " " + nextMotion);
5416 return null;
5417 }
5418 if (targetWin != null) {
5419 // If this is a new target, and that target is not
5420 // paused or unresponsive, then all looks good to
5421 // handle the event.
5422 if (targetIsNew && !targetWin.mToken.paused) {
5423 return targetWin;
5424 }
Romain Guy06882f82009-06-10 13:36:04 -07005425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 // If we didn't find a target window, and there is no
5427 // focused app window, then just eat the events.
5428 } else if (mFocusedApp == null) {
5429 if (DEBUG_INPUT) Log.v(TAG,
5430 "Skipping event; no focused app: "
5431 + nextKey + " " + nextMotion);
5432 return null;
5433 }
5434 }
Romain Guy06882f82009-06-10 13:36:04 -07005435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 if (DEBUG_INPUT) Log.v(
5437 TAG, "Waiting for last key in " + mLastBinder
5438 + " target=" + targetWin
5439 + " mFinished=" + mFinished
5440 + " mDisplayFrozen=" + mDisplayFrozen
5441 + " targetIsNew=" + targetIsNew
5442 + " paused="
5443 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005444 + " mFocusedApp=" + mFocusedApp
5445 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 targetApp = targetWin != null
5448 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 long curTimeout = keyDispatchingTimeout;
5451 if (mTimeToSwitch != 0) {
5452 long now = SystemClock.uptimeMillis();
5453 if (mTimeToSwitch <= now) {
5454 // If an app switch key has been pressed, and we have
5455 // waited too long for the current app to finish
5456 // processing keys, then wait no more!
5457 doFinishedKeyLocked(true);
5458 continue;
5459 }
5460 long switchTimeout = mTimeToSwitch - now;
5461 if (curTimeout > switchTimeout) {
5462 curTimeout = switchTimeout;
5463 }
5464 }
Romain Guy06882f82009-06-10 13:36:04 -07005465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 try {
5467 // after that continue
5468 // processing keys, so we don't get stuck.
5469 if (DEBUG_INPUT) Log.v(
5470 TAG, "Waiting for key dispatch: " + curTimeout);
5471 wait(curTimeout);
5472 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5473 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005474 + startTime + " switchTime=" + mTimeToSwitch
5475 + " target=" + targetWin + " mLW=" + mLastWin
5476 + " mLB=" + mLastBinder + " fin=" + mFinished
5477 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 } catch (InterruptedException e) {
5479 }
5480 }
5481
5482 // If we were frozen during configuration change, restart the
5483 // timeout checks from now; otherwise look at whether we timed
5484 // out before awakening.
5485 if (mWasFrozen) {
5486 waitedFor = 0;
5487 mWasFrozen = false;
5488 } else {
5489 waitedFor = SystemClock.uptimeMillis() - startTime;
5490 }
5491
5492 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5493 IApplicationToken at = null;
5494 synchronized (this) {
5495 Log.w(TAG, "Key dispatching timed out sending to " +
5496 (targetWin != null ? targetWin.mAttrs.getTitle()
5497 : "<null>"));
5498 // NOSHIP debugging
5499 Log.w(TAG, "Dispatch state: " + mDispatchState);
5500 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5501 // END NOSHIP
5502 //dump();
5503 if (targetWin != null) {
5504 at = targetWin.getAppToken();
5505 } else if (targetApp != null) {
5506 at = targetApp.appToken;
5507 }
5508 }
5509
5510 boolean abort = true;
5511 if (at != null) {
5512 try {
5513 long timeout = at.getKeyDispatchingTimeout();
5514 if (timeout > waitedFor) {
5515 // we did not wait the proper amount of time for this application.
5516 // set the timeout to be the real timeout and wait again.
5517 keyDispatchingTimeout = timeout - waitedFor;
5518 continue;
5519 } else {
5520 abort = at.keyDispatchingTimedOut();
5521 }
5522 } catch (RemoteException ex) {
5523 }
5524 }
5525
5526 synchronized (this) {
5527 if (abort && (mLastWin == targetWin || targetWin == null)) {
5528 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005529 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 if (DEBUG_INPUT) Log.v(TAG,
5531 "Window " + mLastWin +
5532 " timed out on key input");
5533 if (mLastWin.mToken.paused) {
5534 Log.w(TAG, "Un-pausing dispatching to this window");
5535 mLastWin.mToken.paused = false;
5536 }
5537 }
5538 if (mMotionTarget == targetWin) {
5539 mMotionTarget = null;
5540 }
5541 mLastWin = null;
5542 mLastBinder = null;
5543 if (failIfTimeout || targetWin == null) {
5544 return null;
5545 }
5546 } else {
5547 Log.w(TAG, "Continuing to wait for key to be dispatched");
5548 startTime = SystemClock.uptimeMillis();
5549 }
5550 }
5551 }
5552 }
5553 }
Romain Guy06882f82009-06-10 13:36:04 -07005554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005556 MotionEvent nextMotion, boolean isPointerEvent,
5557 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 if (nextKey != null) {
5561 // Find the target window for a normal key event.
5562 final int keycode = nextKey.getKeyCode();
5563 final int repeatCount = nextKey.getRepeatCount();
5564 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5565 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005568 if (callingUid == 0 ||
5569 mContext.checkPermission(
5570 android.Manifest.permission.INJECT_EVENTS,
5571 callingPid, callingUid)
5572 == PackageManager.PERMISSION_GRANTED) {
5573 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005574 nextKey.getMetaState(), down, repeatCount,
5575 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 Log.w(TAG, "Event timeout during app switch: dropping "
5578 + nextKey);
5579 return SKIP_TARGET_TOKEN;
5580 }
Romain Guy06882f82009-06-10 13:36:04 -07005581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 WindowState focus = null;
5585 synchronized(mWindowMap) {
5586 focus = getFocusedWindowLocked();
5587 }
Romain Guy06882f82009-06-10 13:36:04 -07005588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005590
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005591 if (callingUid == 0 ||
5592 (focus != null && callingUid == focus.mSession.mUid) ||
5593 mContext.checkPermission(
5594 android.Manifest.permission.INJECT_EVENTS,
5595 callingPid, callingUid)
5596 == PackageManager.PERMISSION_GRANTED) {
5597 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005598 keycode, nextKey.getMetaState(), down, repeatCount,
5599 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005600 return CONSUMED_EVENT_TOKEN;
5601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 }
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 } else if (!isPointerEvent) {
5607 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5608 if (!dispatch) {
5609 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5610 + nextMotion);
5611 return SKIP_TARGET_TOKEN;
5612 }
Romain Guy06882f82009-06-10 13:36:04 -07005613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 WindowState focus = null;
5615 synchronized(mWindowMap) {
5616 focus = getFocusedWindowLocked();
5617 }
Romain Guy06882f82009-06-10 13:36:04 -07005618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5620 return focus;
5621 }
Romain Guy06882f82009-06-10 13:36:04 -07005622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 if (nextMotion == null) {
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 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5628 KeyEvent.KEYCODE_UNKNOWN);
5629 if (!dispatch) {
5630 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5631 + nextMotion);
5632 return SKIP_TARGET_TOKEN;
5633 }
Romain Guy06882f82009-06-10 13:36:04 -07005634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 // Find the target window for a pointer event.
5636 int action = nextMotion.getAction();
5637 final float xf = nextMotion.getX();
5638 final float yf = nextMotion.getY();
5639 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005641 final boolean screenWasOff = qev != null
5642 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 synchronized(mWindowMap) {
5647 synchronized (this) {
5648 if (action == MotionEvent.ACTION_DOWN) {
5649 if (mMotionTarget != null) {
5650 // this is weird, we got a pen down, but we thought it was
5651 // already down!
5652 // XXX: We should probably send an ACTION_UP to the current
5653 // target.
5654 Log.w(TAG, "Pointer down received while already down in: "
5655 + mMotionTarget);
5656 mMotionTarget = null;
5657 }
Romain Guy06882f82009-06-10 13:36:04 -07005658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 // ACTION_DOWN is special, because we need to lock next events to
5660 // the window we'll land onto.
5661 final int x = (int)xf;
5662 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 final ArrayList windows = mWindows;
5665 final int N = windows.size();
5666 WindowState topErrWindow = null;
5667 final Rect tmpRect = mTempRect;
5668 for (int i=N-1; i>=0; i--) {
5669 WindowState child = (WindowState)windows.get(i);
5670 //Log.i(TAG, "Checking dispatch to: " + child);
5671 final int flags = child.mAttrs.flags;
5672 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5673 if (topErrWindow == null) {
5674 topErrWindow = child;
5675 }
5676 }
5677 if (!child.isVisibleLw()) {
5678 //Log.i(TAG, "Not visible!");
5679 continue;
5680 }
5681 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5682 //Log.i(TAG, "Not touchable!");
5683 if ((flags & WindowManager.LayoutParams
5684 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5685 child.mNextOutsideTouch = mOutsideTouchTargets;
5686 mOutsideTouchTargets = child;
5687 }
5688 continue;
5689 }
5690 tmpRect.set(child.mFrame);
5691 if (child.mTouchableInsets == ViewTreeObserver
5692 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5693 // The touch is inside of the window if it is
5694 // inside the frame, AND the content part of that
5695 // frame that was given by the application.
5696 tmpRect.left += child.mGivenContentInsets.left;
5697 tmpRect.top += child.mGivenContentInsets.top;
5698 tmpRect.right -= child.mGivenContentInsets.right;
5699 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5700 } else if (child.mTouchableInsets == ViewTreeObserver
5701 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5702 // The touch is inside of the window if it is
5703 // inside the frame, AND the visible part of that
5704 // frame that was given by the application.
5705 tmpRect.left += child.mGivenVisibleInsets.left;
5706 tmpRect.top += child.mGivenVisibleInsets.top;
5707 tmpRect.right -= child.mGivenVisibleInsets.right;
5708 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5709 }
5710 final int touchFlags = flags &
5711 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5712 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5713 if (tmpRect.contains(x, y) || touchFlags == 0) {
5714 //Log.i(TAG, "Using this target!");
5715 if (!screenWasOff || (flags &
5716 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5717 mMotionTarget = child;
5718 } else {
5719 //Log.i(TAG, "Waking, skip!");
5720 mMotionTarget = null;
5721 }
5722 break;
5723 }
Romain Guy06882f82009-06-10 13:36:04 -07005724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 if ((flags & WindowManager.LayoutParams
5726 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5727 child.mNextOutsideTouch = mOutsideTouchTargets;
5728 mOutsideTouchTargets = child;
5729 //Log.i(TAG, "Adding to outside target list: " + child);
5730 }
5731 }
5732
5733 // if there's an error window but it's not accepting
5734 // focus (typically because it is not yet visible) just
5735 // wait for it -- any other focused window may in fact
5736 // be in ANR state.
5737 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5738 mMotionTarget = null;
5739 }
5740 }
Romain Guy06882f82009-06-10 13:36:04 -07005741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742 target = mMotionTarget;
5743 }
5744 }
Romain Guy06882f82009-06-10 13:36:04 -07005745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 // Pointer events are a little different -- if there isn't a
5749 // target found for any event, then just drop it.
5750 return target != null ? target : SKIP_TARGET_TOKEN;
5751 }
Romain Guy06882f82009-06-10 13:36:04 -07005752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 boolean checkShouldDispatchKey(int keycode) {
5754 synchronized (this) {
5755 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5756 mTimeToSwitch = 0;
5757 return true;
5758 }
5759 if (mTimeToSwitch != 0
5760 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5761 return false;
5762 }
5763 return true;
5764 }
5765 }
Romain Guy06882f82009-06-10 13:36:04 -07005766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 void bindTargetWindowLocked(WindowState win,
5768 int pendingWhat, QueuedEvent pendingMotion) {
5769 synchronized (this) {
5770 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5771 }
5772 }
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 void bindTargetWindowLocked(WindowState win) {
5775 synchronized (this) {
5776 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5777 }
5778 }
5779
5780 void bindTargetWindowLockedLocked(WindowState win,
5781 int pendingWhat, QueuedEvent pendingMotion) {
5782 mLastWin = win;
5783 mLastBinder = win.mClient.asBinder();
5784 mFinished = false;
5785 if (pendingMotion != null) {
5786 final Session s = win.mSession;
5787 if (pendingWhat == RETURN_PENDING_POINTER) {
5788 releasePendingPointerLocked(s);
5789 s.mPendingPointerMove = pendingMotion;
5790 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005791 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 "bindTargetToWindow " + s.mPendingPointerMove);
5793 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5794 releasePendingTrackballLocked(s);
5795 s.mPendingTrackballMove = pendingMotion;
5796 s.mPendingTrackballWindow = win;
5797 }
5798 }
5799 }
Romain Guy06882f82009-06-10 13:36:04 -07005800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 void releasePendingPointerLocked(Session s) {
5802 if (DEBUG_INPUT) Log.v(TAG,
5803 "releasePendingPointer " + s.mPendingPointerMove);
5804 if (s.mPendingPointerMove != null) {
5805 mQueue.recycleEvent(s.mPendingPointerMove);
5806 s.mPendingPointerMove = null;
5807 }
5808 }
Romain Guy06882f82009-06-10 13:36:04 -07005809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 void releasePendingTrackballLocked(Session s) {
5811 if (s.mPendingTrackballMove != null) {
5812 mQueue.recycleEvent(s.mPendingTrackballMove);
5813 s.mPendingTrackballMove = null;
5814 }
5815 }
Romain Guy06882f82009-06-10 13:36:04 -07005816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5818 int returnWhat) {
5819 if (DEBUG_INPUT) Log.v(
5820 TAG, "finishedKey: client=" + client + ", force=" + force);
5821
5822 if (client == null) {
5823 return null;
5824 }
5825
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005826 MotionEvent res = null;
5827 QueuedEvent qev = null;
5828 WindowState win = null;
5829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005830 synchronized (this) {
5831 if (DEBUG_INPUT) Log.v(
5832 TAG, "finishedKey: client=" + client.asBinder()
5833 + ", force=" + force + ", last=" + mLastBinder
5834 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 if (returnWhat == RETURN_PENDING_POINTER) {
5837 qev = session.mPendingPointerMove;
5838 win = session.mPendingPointerWindow;
5839 session.mPendingPointerMove = null;
5840 session.mPendingPointerWindow = null;
5841 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5842 qev = session.mPendingTrackballMove;
5843 win = session.mPendingTrackballWindow;
5844 session.mPendingTrackballMove = null;
5845 session.mPendingTrackballWindow = null;
5846 }
Romain Guy06882f82009-06-10 13:36:04 -07005847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 if (mLastBinder == client.asBinder()) {
5849 if (DEBUG_INPUT) Log.v(
5850 TAG, "finishedKey: last paused="
5851 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5852 if (mLastWin != null && (!mLastWin.mToken.paused || force
5853 || !mEventDispatching)) {
5854 doFinishedKeyLocked(false);
5855 } else {
5856 // Make sure to wake up anyone currently waiting to
5857 // dispatch a key, so they can re-evaluate their
5858 // current situation.
5859 mFinished = true;
5860 notifyAll();
5861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 }
Romain Guy06882f82009-06-10 13:36:04 -07005863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005865 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 if (DEBUG_INPUT) Log.v(TAG,
5867 "Returning pending motion: " + res);
5868 mQueue.recycleEvent(qev);
5869 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5870 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005874
5875 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5876 synchronized (mWindowMap) {
5877 if (mWallpaperTarget == win) {
5878 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5879 }
5880 }
5881 }
5882
5883 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 }
5885
5886 void tickle() {
5887 synchronized (this) {
5888 notifyAll();
5889 }
5890 }
Romain Guy06882f82009-06-10 13:36:04 -07005891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 void handleNewWindowLocked(WindowState newWindow) {
5893 if (!newWindow.canReceiveKeys()) {
5894 return;
5895 }
5896 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005897 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 TAG, "New key dispatch window: win="
5899 + newWindow.mClient.asBinder()
5900 + ", last=" + mLastBinder
5901 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5902 + "), finished=" + mFinished + ", paused="
5903 + newWindow.mToken.paused);
5904
5905 // Displaying a window implicitly causes dispatching to
5906 // be unpaused. (This is to protect against bugs if someone
5907 // pauses dispatching but forgets to resume.)
5908 newWindow.mToken.paused = false;
5909
5910 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911
5912 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5913 if (DEBUG_INPUT) Log.v(TAG,
5914 "New SYSTEM_ERROR window; resetting state");
5915 mLastWin = null;
5916 mLastBinder = null;
5917 mMotionTarget = null;
5918 mFinished = true;
5919 } else if (mLastWin != null) {
5920 // If the new window is above the window we are
5921 // waiting on, then stop waiting and let key dispatching
5922 // start on the new guy.
5923 if (DEBUG_INPUT) Log.v(
5924 TAG, "Last win layer=" + mLastWin.mLayer
5925 + ", new win layer=" + newWindow.mLayer);
5926 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005927 // The new window is above the old; finish pending input to the last
5928 // window and start directing it to the new one.
5929 mLastWin.mToken.paused = false;
5930 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005932 // Either the new window is lower, so there is no need to wake key waiters,
5933 // or we just finished key input to the previous window, which implicitly
5934 // notified the key waiters. In both cases, we don't need to issue the
5935 // notification here.
5936 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 }
5938
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005939 // Now that we've put a new window state in place, make the event waiter
5940 // take notice and retarget its attentions.
5941 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 }
5943 }
5944
5945 void pauseDispatchingLocked(WindowToken token) {
5946 synchronized (this)
5947 {
5948 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5949 token.paused = true;
5950
5951 /*
5952 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5953 mPaused = true;
5954 } else {
5955 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005956 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005958 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005960 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 }
5962 }
5963 */
5964 }
5965 }
5966
5967 void resumeDispatchingLocked(WindowToken token) {
5968 synchronized (this) {
5969 if (token.paused) {
5970 if (DEBUG_INPUT) Log.v(
5971 TAG, "Resuming WindowToken " + token
5972 + ", last=" + mLastBinder
5973 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5974 + "), finished=" + mFinished + ", paused="
5975 + token.paused);
5976 token.paused = false;
5977 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5978 doFinishedKeyLocked(true);
5979 } else {
5980 notifyAll();
5981 }
5982 }
5983 }
5984 }
5985
5986 void setEventDispatchingLocked(boolean enabled) {
5987 synchronized (this) {
5988 mEventDispatching = enabled;
5989 notifyAll();
5990 }
5991 }
Romain Guy06882f82009-06-10 13:36:04 -07005992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 void appSwitchComing() {
5994 synchronized (this) {
5995 // Don't wait for more than .5 seconds for app to finish
5996 // processing the pending events.
5997 long now = SystemClock.uptimeMillis() + 500;
5998 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5999 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6000 mTimeToSwitch = now;
6001 }
6002 notifyAll();
6003 }
6004 }
Romain Guy06882f82009-06-10 13:36:04 -07006005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 private final void doFinishedKeyLocked(boolean doRecycle) {
6007 if (mLastWin != null) {
6008 releasePendingPointerLocked(mLastWin.mSession);
6009 releasePendingTrackballLocked(mLastWin.mSession);
6010 }
Romain Guy06882f82009-06-10 13:36:04 -07006011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 if (mLastWin == null || !mLastWin.mToken.paused
6013 || !mLastWin.isVisibleLw()) {
6014 // If the current window has been paused, we aren't -really-
6015 // finished... so let the waiters still wait.
6016 mLastWin = null;
6017 mLastBinder = null;
6018 }
6019 mFinished = true;
6020 notifyAll();
6021 }
6022 }
6023
6024 private class KeyQ extends KeyInputQueue
6025 implements KeyInputQueue.FilterCallback {
6026 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006029 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6031 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6032 "KEEP_SCREEN_ON_FLAG");
6033 mHoldingScreen.setReferenceCounted(false);
6034 }
6035
6036 @Override
6037 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6038 if (mPolicy.preprocessInputEventTq(event)) {
6039 return true;
6040 }
Romain Guy06882f82009-06-10 13:36:04 -07006041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 switch (event.type) {
6043 case RawInputEvent.EV_KEY: {
6044 // XXX begin hack
6045 if (DEBUG) {
6046 if (event.keycode == KeyEvent.KEYCODE_G) {
6047 if (event.value != 0) {
6048 // G down
6049 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6050 }
6051 return false;
6052 }
6053 if (event.keycode == KeyEvent.KEYCODE_D) {
6054 if (event.value != 0) {
6055 //dump();
6056 }
6057 return false;
6058 }
6059 }
6060 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 boolean screenIsOff = !mPowerManager.screenIsOn();
6063 boolean screenIsDim = !mPowerManager.screenIsBright();
6064 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6067 mPowerManager.goToSleep(event.when);
6068 }
6069
6070 if (screenIsOff) {
6071 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6072 }
6073 if (screenIsDim) {
6074 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6075 }
6076 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6077 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006078 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 }
Romain Guy06882f82009-06-10 13:36:04 -07006080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6082 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6083 filterQueue(this);
6084 mKeyWaiter.appSwitchComing();
6085 }
6086 return true;
6087 } else {
6088 return false;
6089 }
6090 }
Romain Guy06882f82009-06-10 13:36:04 -07006091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 case RawInputEvent.EV_REL: {
6093 boolean screenIsOff = !mPowerManager.screenIsOn();
6094 boolean screenIsDim = !mPowerManager.screenIsBright();
6095 if (screenIsOff) {
6096 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6097 device.classes, event)) {
6098 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6099 return false;
6100 }
6101 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6102 }
6103 if (screenIsDim) {
6104 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6105 }
6106 return true;
6107 }
Romain Guy06882f82009-06-10 13:36:04 -07006108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 case RawInputEvent.EV_ABS: {
6110 boolean screenIsOff = !mPowerManager.screenIsOn();
6111 boolean screenIsDim = !mPowerManager.screenIsBright();
6112 if (screenIsOff) {
6113 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6114 device.classes, event)) {
6115 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6116 return false;
6117 }
6118 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6119 }
6120 if (screenIsDim) {
6121 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6122 }
6123 return true;
6124 }
Romain Guy06882f82009-06-10 13:36:04 -07006125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 default:
6127 return true;
6128 }
6129 }
6130
6131 public int filterEvent(QueuedEvent ev) {
6132 switch (ev.classType) {
6133 case RawInputEvent.CLASS_KEYBOARD:
6134 KeyEvent ke = (KeyEvent)ev.event;
6135 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6136 Log.w(TAG, "Dropping movement key during app switch: "
6137 + ke.getKeyCode() + ", action=" + ke.getAction());
6138 return FILTER_REMOVE;
6139 }
6140 return FILTER_ABORT;
6141 default:
6142 return FILTER_KEEP;
6143 }
6144 }
Romain Guy06882f82009-06-10 13:36:04 -07006145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 /**
6147 * Must be called with the main window manager lock held.
6148 */
6149 void setHoldScreenLocked(boolean holding) {
6150 boolean state = mHoldingScreen.isHeld();
6151 if (holding != state) {
6152 if (holding) {
6153 mHoldingScreen.acquire();
6154 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006155 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 mHoldingScreen.release();
6157 }
6158 }
6159 }
Michael Chan53071d62009-05-13 17:29:48 -07006160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006161
6162 public boolean detectSafeMode() {
6163 mSafeMode = mPolicy.detectSafeMode();
6164 return mSafeMode;
6165 }
Romain Guy06882f82009-06-10 13:36:04 -07006166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 public void systemReady() {
6168 mPolicy.systemReady();
6169 }
Romain Guy06882f82009-06-10 13:36:04 -07006170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171 private final class InputDispatcherThread extends Thread {
6172 // Time to wait when there is nothing to do: 9999 seconds.
6173 static final int LONG_WAIT=9999*1000;
6174
6175 public InputDispatcherThread() {
6176 super("InputDispatcher");
6177 }
Romain Guy06882f82009-06-10 13:36:04 -07006178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 @Override
6180 public void run() {
6181 while (true) {
6182 try {
6183 process();
6184 } catch (Exception e) {
6185 Log.e(TAG, "Exception in input dispatcher", e);
6186 }
6187 }
6188 }
Romain Guy06882f82009-06-10 13:36:04 -07006189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 private void process() {
6191 android.os.Process.setThreadPriority(
6192 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006194 // The last key event we saw
6195 KeyEvent lastKey = null;
6196
6197 // Last keydown time for auto-repeating keys
6198 long lastKeyTime = SystemClock.uptimeMillis();
6199 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006200 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201
Romain Guy06882f82009-06-10 13:36:04 -07006202 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 int keyRepeatCount = 0;
6204
6205 // Need to report that configuration has changed?
6206 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208 while (true) {
6209 long curTime = SystemClock.uptimeMillis();
6210
6211 if (DEBUG_INPUT) Log.v(
6212 TAG, "Waiting for next key: now=" + curTime
6213 + ", repeat @ " + nextKeyTime);
6214
6215 // Retrieve next event, waiting only as long as the next
6216 // repeat timeout. If the configuration has changed, then
6217 // don't wait at all -- we'll report the change as soon as
6218 // we have processed all events.
6219 QueuedEvent ev = mQueue.getEvent(
6220 (int)((!configChanged && curTime < nextKeyTime)
6221 ? (nextKeyTime-curTime) : 0));
6222
6223 if (DEBUG_INPUT && ev != null) Log.v(
6224 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6225
Michael Chan53071d62009-05-13 17:29:48 -07006226 if (MEASURE_LATENCY) {
6227 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6228 }
6229
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006230 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6231 // cancel key repeat at the request of the policy.
6232 lastKey = null;
6233 downTime = 0;
6234 lastKeyTime = curTime;
6235 nextKeyTime = curTime + LONG_WAIT;
6236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 try {
6238 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006239 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006240 int eventType;
6241 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6242 eventType = eventType((MotionEvent)ev.event);
6243 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6244 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6245 eventType = LocalPowerManager.BUTTON_EVENT;
6246 } else {
6247 eventType = LocalPowerManager.OTHER_EVENT;
6248 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006249 try {
Michael Chan53071d62009-05-13 17:29:48 -07006250 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006251 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006252 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006253 mBatteryStats.noteInputEvent();
6254 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006255 } catch (RemoteException e) {
6256 // Ignore
6257 }
Michael Chane10de972009-05-18 11:24:50 -07006258
6259 if (eventType != TOUCH_EVENT
6260 && eventType != LONG_TOUCH_EVENT
6261 && eventType != CHEEK_EVENT) {
6262 mPowerManager.userActivity(curTime, false,
6263 eventType, false);
6264 } else if (mLastTouchEventType != eventType
6265 || (curTime - mLastUserActivityCallTime)
6266 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6267 mLastUserActivityCallTime = curTime;
6268 mLastTouchEventType = eventType;
6269 mPowerManager.userActivity(curTime, false,
6270 eventType, false);
6271 }
6272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 switch (ev.classType) {
6274 case RawInputEvent.CLASS_KEYBOARD:
6275 KeyEvent ke = (KeyEvent)ev.event;
6276 if (ke.isDown()) {
6277 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006278 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 keyRepeatCount = 0;
6280 lastKeyTime = curTime;
6281 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006282 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 if (DEBUG_INPUT) Log.v(
6284 TAG, "Received key down: first repeat @ "
6285 + nextKeyTime);
6286 } else {
6287 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006288 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 // Arbitrary long timeout.
6290 lastKeyTime = curTime;
6291 nextKeyTime = curTime + LONG_WAIT;
6292 if (DEBUG_INPUT) Log.v(
6293 TAG, "Received key up: ignore repeat @ "
6294 + nextKeyTime);
6295 }
6296 dispatchKey((KeyEvent)ev.event, 0, 0);
6297 mQueue.recycleEvent(ev);
6298 break;
6299 case RawInputEvent.CLASS_TOUCHSCREEN:
6300 //Log.i(TAG, "Read next event " + ev);
6301 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6302 break;
6303 case RawInputEvent.CLASS_TRACKBALL:
6304 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6305 break;
6306 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6307 configChanged = true;
6308 break;
6309 default:
6310 mQueue.recycleEvent(ev);
6311 break;
6312 }
Romain Guy06882f82009-06-10 13:36:04 -07006313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006314 } else if (configChanged) {
6315 configChanged = false;
6316 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 } else if (lastKey != null) {
6319 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321 // Timeout occurred while key was down. If it is at or
6322 // past the key repeat time, dispatch the repeat.
6323 if (DEBUG_INPUT) Log.v(
6324 TAG, "Key timeout: repeat=" + nextKeyTime
6325 + ", now=" + curTime);
6326 if (curTime < nextKeyTime) {
6327 continue;
6328 }
Romain Guy06882f82009-06-10 13:36:04 -07006329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 lastKeyTime = nextKeyTime;
6331 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6332 keyRepeatCount++;
6333 if (DEBUG_INPUT) Log.v(
6334 TAG, "Key repeat: count=" + keyRepeatCount
6335 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006336 KeyEvent newEvent;
6337 if (downTime != 0 && (downTime
6338 + ViewConfiguration.getLongPressTimeout())
6339 <= curTime) {
6340 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6341 curTime, keyRepeatCount,
6342 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6343 downTime = 0;
6344 } else {
6345 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6346 curTime, keyRepeatCount);
6347 }
6348 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 } else {
6351 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 lastKeyTime = curTime;
6354 nextKeyTime = curTime + LONG_WAIT;
6355 }
Romain Guy06882f82009-06-10 13:36:04 -07006356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 } catch (Exception e) {
6358 Log.e(TAG,
6359 "Input thread received uncaught exception: " + e, e);
6360 }
6361 }
6362 }
6363 }
6364
6365 // -------------------------------------------------------------
6366 // Client Session State
6367 // -------------------------------------------------------------
6368
6369 private final class Session extends IWindowSession.Stub
6370 implements IBinder.DeathRecipient {
6371 final IInputMethodClient mClient;
6372 final IInputContext mInputContext;
6373 final int mUid;
6374 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006375 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 SurfaceSession mSurfaceSession;
6377 int mNumWindow = 0;
6378 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 /**
6381 * Current pointer move event being dispatched to client window... must
6382 * hold key lock to access.
6383 */
6384 QueuedEvent mPendingPointerMove;
6385 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 /**
6388 * Current trackball move event being dispatched to client window... must
6389 * hold key lock to access.
6390 */
6391 QueuedEvent mPendingTrackballMove;
6392 WindowState mPendingTrackballWindow;
6393
6394 public Session(IInputMethodClient client, IInputContext inputContext) {
6395 mClient = client;
6396 mInputContext = inputContext;
6397 mUid = Binder.getCallingUid();
6398 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006399 StringBuilder sb = new StringBuilder();
6400 sb.append("Session{");
6401 sb.append(Integer.toHexString(System.identityHashCode(this)));
6402 sb.append(" uid ");
6403 sb.append(mUid);
6404 sb.append("}");
6405 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 synchronized (mWindowMap) {
6408 if (mInputMethodManager == null && mHaveInputMethods) {
6409 IBinder b = ServiceManager.getService(
6410 Context.INPUT_METHOD_SERVICE);
6411 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6412 }
6413 }
6414 long ident = Binder.clearCallingIdentity();
6415 try {
6416 // Note: it is safe to call in to the input method manager
6417 // here because we are not holding our lock.
6418 if (mInputMethodManager != null) {
6419 mInputMethodManager.addClient(client, inputContext,
6420 mUid, mPid);
6421 } else {
6422 client.setUsingInputMethod(false);
6423 }
6424 client.asBinder().linkToDeath(this, 0);
6425 } catch (RemoteException e) {
6426 // The caller has died, so we can just forget about this.
6427 try {
6428 if (mInputMethodManager != null) {
6429 mInputMethodManager.removeClient(client);
6430 }
6431 } catch (RemoteException ee) {
6432 }
6433 } finally {
6434 Binder.restoreCallingIdentity(ident);
6435 }
6436 }
Romain Guy06882f82009-06-10 13:36:04 -07006437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 @Override
6439 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6440 throws RemoteException {
6441 try {
6442 return super.onTransact(code, data, reply, flags);
6443 } catch (RuntimeException e) {
6444 // Log all 'real' exceptions thrown to the caller
6445 if (!(e instanceof SecurityException)) {
6446 Log.e(TAG, "Window Session Crash", e);
6447 }
6448 throw e;
6449 }
6450 }
6451
6452 public void binderDied() {
6453 // Note: it is safe to call in to the input method manager
6454 // here because we are not holding our lock.
6455 try {
6456 if (mInputMethodManager != null) {
6457 mInputMethodManager.removeClient(mClient);
6458 }
6459 } catch (RemoteException e) {
6460 }
6461 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006462 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 mClientDead = true;
6464 killSessionLocked();
6465 }
6466 }
6467
6468 public int add(IWindow window, WindowManager.LayoutParams attrs,
6469 int viewVisibility, Rect outContentInsets) {
6470 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6471 }
Romain Guy06882f82009-06-10 13:36:04 -07006472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 public void remove(IWindow window) {
6474 removeWindow(this, window);
6475 }
Romain Guy06882f82009-06-10 13:36:04 -07006476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6478 int requestedWidth, int requestedHeight, int viewFlags,
6479 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6480 Rect outVisibleInsets, Surface outSurface) {
6481 return relayoutWindow(this, window, attrs,
6482 requestedWidth, requestedHeight, viewFlags, insetsPending,
6483 outFrame, outContentInsets, outVisibleInsets, outSurface);
6484 }
Romain Guy06882f82009-06-10 13:36:04 -07006485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006486 public void setTransparentRegion(IWindow window, Region region) {
6487 setTransparentRegionWindow(this, window, region);
6488 }
Romain Guy06882f82009-06-10 13:36:04 -07006489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006490 public void setInsets(IWindow window, int touchableInsets,
6491 Rect contentInsets, Rect visibleInsets) {
6492 setInsetsWindow(this, window, touchableInsets, contentInsets,
6493 visibleInsets);
6494 }
Romain Guy06882f82009-06-10 13:36:04 -07006495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6497 getWindowDisplayFrame(this, window, outDisplayFrame);
6498 }
Romain Guy06882f82009-06-10 13:36:04 -07006499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 public void finishDrawing(IWindow window) {
6501 if (localLOGV) Log.v(
6502 TAG, "IWindow finishDrawing called for " + window);
6503 finishDrawingWindow(this, window);
6504 }
6505
6506 public void finishKey(IWindow window) {
6507 if (localLOGV) Log.v(
6508 TAG, "IWindow finishKey called for " + window);
6509 mKeyWaiter.finishedKey(this, window, false,
6510 KeyWaiter.RETURN_NOTHING);
6511 }
6512
6513 public MotionEvent getPendingPointerMove(IWindow window) {
6514 if (localLOGV) Log.v(
6515 TAG, "IWindow getPendingMotionEvent called for " + window);
6516 return mKeyWaiter.finishedKey(this, window, false,
6517 KeyWaiter.RETURN_PENDING_POINTER);
6518 }
Romain Guy06882f82009-06-10 13:36:04 -07006519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520 public MotionEvent getPendingTrackballMove(IWindow window) {
6521 if (localLOGV) Log.v(
6522 TAG, "IWindow getPendingMotionEvent called for " + window);
6523 return mKeyWaiter.finishedKey(this, window, false,
6524 KeyWaiter.RETURN_PENDING_TRACKBALL);
6525 }
6526
6527 public void setInTouchMode(boolean mode) {
6528 synchronized(mWindowMap) {
6529 mInTouchMode = mode;
6530 }
6531 }
6532
6533 public boolean getInTouchMode() {
6534 synchronized(mWindowMap) {
6535 return mInTouchMode;
6536 }
6537 }
6538
6539 public boolean performHapticFeedback(IWindow window, int effectId,
6540 boolean always) {
6541 synchronized(mWindowMap) {
6542 long ident = Binder.clearCallingIdentity();
6543 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006544 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 windowForClientLocked(this, window), effectId, always);
6546 } finally {
6547 Binder.restoreCallingIdentity(ident);
6548 }
6549 }
6550 }
Romain Guy06882f82009-06-10 13:36:04 -07006551
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006552 public void setWallpaperPosition(IBinder window, float x, float y) {
6553 synchronized(mWindowMap) {
6554 long ident = Binder.clearCallingIdentity();
6555 try {
6556 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6557 x, y);
6558 } finally {
6559 Binder.restoreCallingIdentity(ident);
6560 }
6561 }
6562 }
6563
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006564 public void wallpaperOffsetsComplete(IBinder window) {
6565 WindowManagerService.this.wallpaperOffsetsComplete(window);
6566 }
6567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006568 void windowAddedLocked() {
6569 if (mSurfaceSession == null) {
6570 if (localLOGV) Log.v(
6571 TAG, "First window added to " + this + ", creating SurfaceSession");
6572 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006573 if (SHOW_TRANSACTIONS) Log.i(
6574 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 mSessions.add(this);
6576 }
6577 mNumWindow++;
6578 }
6579
6580 void windowRemovedLocked() {
6581 mNumWindow--;
6582 killSessionLocked();
6583 }
Romain Guy06882f82009-06-10 13:36:04 -07006584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 void killSessionLocked() {
6586 if (mNumWindow <= 0 && mClientDead) {
6587 mSessions.remove(this);
6588 if (mSurfaceSession != null) {
6589 if (localLOGV) Log.v(
6590 TAG, "Last window removed from " + this
6591 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006592 if (SHOW_TRANSACTIONS) Log.i(
6593 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 try {
6595 mSurfaceSession.kill();
6596 } catch (Exception e) {
6597 Log.w(TAG, "Exception thrown when killing surface session "
6598 + mSurfaceSession + " in session " + this
6599 + ": " + e.toString());
6600 }
6601 mSurfaceSession = null;
6602 }
6603 }
6604 }
Romain Guy06882f82009-06-10 13:36:04 -07006605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006607 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6608 pw.print(" mClientDead="); pw.print(mClientDead);
6609 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6610 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6611 pw.print(prefix);
6612 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6613 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6614 }
6615 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6616 pw.print(prefix);
6617 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6618 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 }
6621
6622 @Override
6623 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006624 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 }
6626 }
6627
6628 // -------------------------------------------------------------
6629 // Client Window State
6630 // -------------------------------------------------------------
6631
6632 private final class WindowState implements WindowManagerPolicy.WindowState {
6633 final Session mSession;
6634 final IWindow mClient;
6635 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006636 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 AppWindowToken mAppToken;
6638 AppWindowToken mTargetAppToken;
6639 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6640 final DeathRecipient mDeathRecipient;
6641 final WindowState mAttachedWindow;
6642 final ArrayList mChildWindows = new ArrayList();
6643 final int mBaseLayer;
6644 final int mSubLayer;
6645 final boolean mLayoutAttached;
6646 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006647 final boolean mIsWallpaper;
6648 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 int mViewVisibility;
6650 boolean mPolicyVisibility = true;
6651 boolean mPolicyVisibilityAfterAnim = true;
6652 boolean mAppFreezing;
6653 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006654 boolean mReportDestroySurface;
6655 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 boolean mAttachedHidden; // is our parent window hidden?
6657 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006658 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 int mRequestedWidth;
6660 int mRequestedHeight;
6661 int mLastRequestedWidth;
6662 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 int mLayer;
6664 int mAnimLayer;
6665 int mLastLayer;
6666 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006667 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006668 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669
6670 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 // Actual frame shown on-screen (may be modified by animation)
6673 final Rect mShownFrame = new Rect();
6674 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 /**
6677 * Insets that determine the actually visible area
6678 */
6679 final Rect mVisibleInsets = new Rect();
6680 final Rect mLastVisibleInsets = new Rect();
6681 boolean mVisibleInsetsChanged;
6682
6683 /**
6684 * Insets that are covered by system windows
6685 */
6686 final Rect mContentInsets = new Rect();
6687 final Rect mLastContentInsets = new Rect();
6688 boolean mContentInsetsChanged;
6689
6690 /**
6691 * Set to true if we are waiting for this window to receive its
6692 * given internal insets before laying out other windows based on it.
6693 */
6694 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006696 /**
6697 * These are the content insets that were given during layout for
6698 * this window, to be applied to windows behind it.
6699 */
6700 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 /**
6703 * These are the visible insets that were given during layout for
6704 * this window, to be applied to windows behind it.
6705 */
6706 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006708 /**
6709 * Flag indicating whether the touchable region should be adjusted by
6710 * the visible insets; if false the area outside the visible insets is
6711 * NOT touchable, so we must use those to adjust the frame during hit
6712 * tests.
6713 */
6714 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 // Current transformation being applied.
6717 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6718 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6719 float mHScale=1, mVScale=1;
6720 float mLastHScale=1, mLastVScale=1;
6721 final Matrix mTmpMatrix = new Matrix();
6722
6723 // "Real" frame that the application sees.
6724 final Rect mFrame = new Rect();
6725 final Rect mLastFrame = new Rect();
6726
6727 final Rect mContainingFrame = new Rect();
6728 final Rect mDisplayFrame = new Rect();
6729 final Rect mContentFrame = new Rect();
6730 final Rect mVisibleFrame = new Rect();
6731
6732 float mShownAlpha = 1;
6733 float mAlpha = 1;
6734 float mLastAlpha = 1;
6735
6736 // Set to true if, when the window gets displayed, it should perform
6737 // an enter animation.
6738 boolean mEnterAnimationPending;
6739
6740 // Currently running animation.
6741 boolean mAnimating;
6742 boolean mLocalAnimating;
6743 Animation mAnimation;
6744 boolean mAnimationIsEntrance;
6745 boolean mHasTransformation;
6746 boolean mHasLocalTransformation;
6747 final Transformation mTransformation = new Transformation();
6748
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006749 // If a window showing a wallpaper: the requested offset for the
6750 // wallpaper; if a wallpaper window: the currently applied offset.
6751 float mWallpaperX = -1;
6752 float mWallpaperY = -1;
6753
6754 // Wallpaper windows: pixels offset based on above variables.
6755 int mXOffset;
6756 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 // This is set after IWindowSession.relayout() has been called at
6759 // least once for the window. It allows us to detect the situation
6760 // where we don't yet have a surface, but should have one soon, so
6761 // we can give the window focus before waiting for the relayout.
6762 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 // This is set after the Surface has been created but before the
6765 // window has been drawn. During this time the surface is hidden.
6766 boolean mDrawPending;
6767
6768 // This is set after the window has finished drawing for the first
6769 // time but before its surface is shown. The surface will be
6770 // displayed when the next layout is run.
6771 boolean mCommitDrawPending;
6772
6773 // This is set during the time after the window's drawing has been
6774 // committed, and before its surface is actually shown. It is used
6775 // to delay showing the surface until all windows in a token are ready
6776 // to be shown.
6777 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 // Set when the window has been shown in the screen the first time.
6780 boolean mHasDrawn;
6781
6782 // Currently running an exit animation?
6783 boolean mExiting;
6784
6785 // Currently on the mDestroySurface list?
6786 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 // Completely remove from window manager after exit animation?
6789 boolean mRemoveOnExit;
6790
6791 // Set when the orientation is changing and this window has not yet
6792 // been updated for the new orientation.
6793 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 // Is this window now (or just being) removed?
6796 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 WindowState(Session s, IWindow c, WindowToken token,
6799 WindowState attachedWindow, WindowManager.LayoutParams a,
6800 int viewVisibility) {
6801 mSession = s;
6802 mClient = c;
6803 mToken = token;
6804 mAttrs.copyFrom(a);
6805 mViewVisibility = viewVisibility;
6806 DeathRecipient deathRecipient = new DeathRecipient();
6807 mAlpha = a.alpha;
6808 if (localLOGV) Log.v(
6809 TAG, "Window " + this + " client=" + c.asBinder()
6810 + " token=" + token + " (" + mAttrs.token + ")");
6811 try {
6812 c.asBinder().linkToDeath(deathRecipient, 0);
6813 } catch (RemoteException e) {
6814 mDeathRecipient = null;
6815 mAttachedWindow = null;
6816 mLayoutAttached = false;
6817 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006818 mIsWallpaper = false;
6819 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 mBaseLayer = 0;
6821 mSubLayer = 0;
6822 return;
6823 }
6824 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6827 mAttrs.type <= LAST_SUB_WINDOW)) {
6828 // The multiplier here is to reserve space for multiple
6829 // windows in the same type layer.
6830 mBaseLayer = mPolicy.windowTypeToLayerLw(
6831 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6832 + TYPE_LAYER_OFFSET;
6833 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6834 mAttachedWindow = attachedWindow;
6835 mAttachedWindow.mChildWindows.add(this);
6836 mLayoutAttached = mAttrs.type !=
6837 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6838 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6839 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006840 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6841 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 } else {
6843 // The multiplier here is to reserve space for multiple
6844 // windows in the same type layer.
6845 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6846 * TYPE_LAYER_MULTIPLIER
6847 + TYPE_LAYER_OFFSET;
6848 mSubLayer = 0;
6849 mAttachedWindow = null;
6850 mLayoutAttached = false;
6851 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6852 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006853 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6854 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 }
6856
6857 WindowState appWin = this;
6858 while (appWin.mAttachedWindow != null) {
6859 appWin = mAttachedWindow;
6860 }
6861 WindowToken appToken = appWin.mToken;
6862 while (appToken.appWindowToken == null) {
6863 WindowToken parent = mTokenMap.get(appToken.token);
6864 if (parent == null || appToken == parent) {
6865 break;
6866 }
6867 appToken = parent;
6868 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006869 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 mAppToken = appToken.appWindowToken;
6871
6872 mSurface = null;
6873 mRequestedWidth = 0;
6874 mRequestedHeight = 0;
6875 mLastRequestedWidth = 0;
6876 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006877 mXOffset = 0;
6878 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 mLayer = 0;
6880 mAnimLayer = 0;
6881 mLastLayer = 0;
6882 }
6883
6884 void attach() {
6885 if (localLOGV) Log.v(
6886 TAG, "Attaching " + this + " token=" + mToken
6887 + ", list=" + mToken.windows);
6888 mSession.windowAddedLocked();
6889 }
6890
6891 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6892 mHaveFrame = true;
6893
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006894 final Rect container = mContainingFrame;
6895 container.set(pf);
6896
6897 final Rect display = mDisplayFrame;
6898 display.set(df);
6899
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006900 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006901 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006902 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6903 display.intersect(mCompatibleScreenFrame);
6904 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006905 }
6906
6907 final int pw = container.right - container.left;
6908 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909
6910 int w,h;
6911 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6912 w = mAttrs.width < 0 ? pw : mAttrs.width;
6913 h = mAttrs.height< 0 ? ph : mAttrs.height;
6914 } else {
6915 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6916 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6917 }
Romain Guy06882f82009-06-10 13:36:04 -07006918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 final Rect content = mContentFrame;
6920 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 final Rect visible = mVisibleFrame;
6923 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006926 final int fw = frame.width();
6927 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6930 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6931
6932 Gravity.apply(mAttrs.gravity, w, h, container,
6933 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6934 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6935
6936 //System.out.println("Out: " + mFrame);
6937
6938 // Now make sure the window fits in the overall display.
6939 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 // Make sure the content and visible frames are inside of the
6942 // final window frame.
6943 if (content.left < frame.left) content.left = frame.left;
6944 if (content.top < frame.top) content.top = frame.top;
6945 if (content.right > frame.right) content.right = frame.right;
6946 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6947 if (visible.left < frame.left) visible.left = frame.left;
6948 if (visible.top < frame.top) visible.top = frame.top;
6949 if (visible.right > frame.right) visible.right = frame.right;
6950 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 final Rect contentInsets = mContentInsets;
6953 contentInsets.left = content.left-frame.left;
6954 contentInsets.top = content.top-frame.top;
6955 contentInsets.right = frame.right-content.right;
6956 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 final Rect visibleInsets = mVisibleInsets;
6959 visibleInsets.left = visible.left-frame.left;
6960 visibleInsets.top = visible.top-frame.top;
6961 visibleInsets.right = frame.right-visible.right;
6962 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006963
Dianne Hackborn284ac932009-08-28 10:34:25 -07006964 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6965 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006966 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006967 }
6968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 if (localLOGV) {
6970 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6971 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6972 Log.v(TAG, "Resolving (mRequestedWidth="
6973 + mRequestedWidth + ", mRequestedheight="
6974 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6975 + "): frame=" + mFrame.toShortString()
6976 + " ci=" + contentInsets.toShortString()
6977 + " vi=" + visibleInsets.toShortString());
6978 //}
6979 }
6980 }
Romain Guy06882f82009-06-10 13:36:04 -07006981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 public Rect getFrameLw() {
6983 return mFrame;
6984 }
6985
6986 public Rect getShownFrameLw() {
6987 return mShownFrame;
6988 }
6989
6990 public Rect getDisplayFrameLw() {
6991 return mDisplayFrame;
6992 }
6993
6994 public Rect getContentFrameLw() {
6995 return mContentFrame;
6996 }
6997
6998 public Rect getVisibleFrameLw() {
6999 return mVisibleFrame;
7000 }
7001
7002 public boolean getGivenInsetsPendingLw() {
7003 return mGivenInsetsPending;
7004 }
7005
7006 public Rect getGivenContentInsetsLw() {
7007 return mGivenContentInsets;
7008 }
Romain Guy06882f82009-06-10 13:36:04 -07007009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007010 public Rect getGivenVisibleInsetsLw() {
7011 return mGivenVisibleInsets;
7012 }
Romain Guy06882f82009-06-10 13:36:04 -07007013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 public WindowManager.LayoutParams getAttrs() {
7015 return mAttrs;
7016 }
7017
7018 public int getSurfaceLayer() {
7019 return mLayer;
7020 }
Romain Guy06882f82009-06-10 13:36:04 -07007021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 public IApplicationToken getAppToken() {
7023 return mAppToken != null ? mAppToken.appToken : null;
7024 }
7025
7026 public boolean hasAppShownWindows() {
7027 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7028 }
7029
7030 public boolean hasAppStartingIcon() {
7031 return mAppToken != null ? (mAppToken.startingData != null) : false;
7032 }
7033
7034 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7035 return mAppToken != null ? mAppToken.startingWindow : null;
7036 }
7037
7038 public void setAnimation(Animation anim) {
7039 if (localLOGV) Log.v(
7040 TAG, "Setting animation in " + this + ": " + anim);
7041 mAnimating = false;
7042 mLocalAnimating = false;
7043 mAnimation = anim;
7044 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7045 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7046 }
7047
7048 public void clearAnimation() {
7049 if (mAnimation != null) {
7050 mAnimating = true;
7051 mLocalAnimating = false;
7052 mAnimation = null;
7053 }
7054 }
Romain Guy06882f82009-06-10 13:36:04 -07007055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 Surface createSurfaceLocked() {
7057 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007058 mReportDestroySurface = false;
7059 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 mDrawPending = true;
7061 mCommitDrawPending = false;
7062 mReadyToShow = false;
7063 if (mAppToken != null) {
7064 mAppToken.allDrawn = false;
7065 }
7066
7067 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007068 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 flags |= Surface.PUSH_BUFFERS;
7070 }
7071
7072 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7073 flags |= Surface.SECURE;
7074 }
7075 if (DEBUG_VISIBILITY) Log.v(
7076 TAG, "Creating surface in session "
7077 + mSession.mSurfaceSession + " window " + this
7078 + " w=" + mFrame.width()
7079 + " h=" + mFrame.height() + " format="
7080 + mAttrs.format + " flags=" + flags);
7081
7082 int w = mFrame.width();
7083 int h = mFrame.height();
7084 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7085 // for a scaled surface, we always want the requested
7086 // size.
7087 w = mRequestedWidth;
7088 h = mRequestedHeight;
7089 }
7090
Romain Guy9825ec62009-10-01 00:58:09 -07007091 // Something is wrong and SurfaceFlinger will not like this,
7092 // try to revert to sane values
7093 if (w <= 0) w = 1;
7094 if (h <= 0) h = 1;
7095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 try {
7097 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007098 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007100 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7101 + mSurface + " IN SESSION "
7102 + mSession.mSurfaceSession
7103 + ": pid=" + mSession.mPid + " format="
7104 + mAttrs.format + " flags=0x"
7105 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 } catch (Surface.OutOfResourcesException e) {
7107 Log.w(TAG, "OutOfResourcesException creating surface");
7108 reclaimSomeSurfaceMemoryLocked(this, "create");
7109 return null;
7110 } catch (Exception e) {
7111 Log.e(TAG, "Exception creating surface", e);
7112 return null;
7113 }
Romain Guy06882f82009-06-10 13:36:04 -07007114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 if (localLOGV) Log.v(
7116 TAG, "Got surface: " + mSurface
7117 + ", set left=" + mFrame.left + " top=" + mFrame.top
7118 + ", animLayer=" + mAnimLayer);
7119 if (SHOW_TRANSACTIONS) {
7120 Log.i(TAG, ">>> OPEN TRANSACTION");
7121 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7122 + mAttrs.getTitle() + ") pos=(" +
7123 mFrame.left + "," + mFrame.top + ") (" +
7124 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7125 mAnimLayer + " HIDE");
7126 }
7127 Surface.openTransaction();
7128 try {
7129 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007130 mSurface.setPosition(mFrame.left + mXOffset,
7131 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 mSurface.setLayer(mAnimLayer);
7133 mSurface.hide();
7134 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007135 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7136 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 mSurface.setFlags(Surface.SURFACE_DITHER,
7138 Surface.SURFACE_DITHER);
7139 }
7140 } catch (RuntimeException e) {
7141 Log.w(TAG, "Error creating surface in " + w, e);
7142 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7143 }
7144 mLastHidden = true;
7145 } finally {
7146 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7147 Surface.closeTransaction();
7148 }
7149 if (localLOGV) Log.v(
7150 TAG, "Created surface " + this);
7151 }
7152 return mSurface;
7153 }
Romain Guy06882f82009-06-10 13:36:04 -07007154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 void destroySurfaceLocked() {
7156 // Window is no longer on-screen, so can no longer receive
7157 // key events... if we were waiting for it to finish
7158 // handling a key event, the wait is over!
7159 mKeyWaiter.finishedKey(mSession, mClient, true,
7160 KeyWaiter.RETURN_NOTHING);
7161 mKeyWaiter.releasePendingPointerLocked(mSession);
7162 mKeyWaiter.releasePendingTrackballLocked(mSession);
7163
7164 if (mAppToken != null && this == mAppToken.startingWindow) {
7165 mAppToken.startingDisplayed = false;
7166 }
Romain Guy06882f82009-06-10 13:36:04 -07007167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007169 mDrawPending = false;
7170 mCommitDrawPending = false;
7171 mReadyToShow = false;
7172
7173 int i = mChildWindows.size();
7174 while (i > 0) {
7175 i--;
7176 WindowState c = (WindowState)mChildWindows.get(i);
7177 c.mAttachedHidden = true;
7178 }
7179
7180 if (mReportDestroySurface) {
7181 mReportDestroySurface = false;
7182 mSurfacePendingDestroy = true;
7183 try {
7184 mClient.dispatchGetNewSurface();
7185 // We'll really destroy on the next time around.
7186 return;
7187 } catch (RemoteException e) {
7188 }
7189 }
7190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007192 if (DEBUG_VISIBILITY) {
7193 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007194 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007195 Log.w(TAG, "Window " + this + " destroying surface "
7196 + mSurface + ", session " + mSession, e);
7197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007198 if (SHOW_TRANSACTIONS) {
7199 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007200 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7202 + mAttrs.getTitle() + ")", ex);
7203 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007204 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205 } catch (RuntimeException e) {
7206 Log.w(TAG, "Exception thrown when destroying Window " + this
7207 + " surface " + mSurface + " session " + mSession
7208 + ": " + e.toString());
7209 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 }
7213 }
7214
7215 boolean finishDrawingLocked() {
7216 if (mDrawPending) {
7217 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7218 TAG, "finishDrawingLocked: " + mSurface);
7219 mCommitDrawPending = true;
7220 mDrawPending = false;
7221 return true;
7222 }
7223 return false;
7224 }
7225
7226 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007227 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7229 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007230 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 }
7232 mCommitDrawPending = false;
7233 mReadyToShow = true;
7234 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7235 final AppWindowToken atoken = mAppToken;
7236 if (atoken == null || atoken.allDrawn || starting) {
7237 performShowLocked();
7238 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007239 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 }
7241
7242 // This must be called while inside a transaction.
7243 boolean performShowLocked() {
7244 if (DEBUG_VISIBILITY) {
7245 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007246 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 Log.v(TAG, "performShow on " + this
7248 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7249 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7250 }
7251 if (mReadyToShow && isReadyForDisplay()) {
7252 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7253 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7254 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7255 + " during animation: policyVis=" + mPolicyVisibility
7256 + " attHidden=" + mAttachedHidden
7257 + " tok.hiddenRequested="
7258 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007259 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 + (mAppToken != null ? mAppToken.hidden : false)
7261 + " animating=" + mAnimating
7262 + " tok animating="
7263 + (mAppToken != null ? mAppToken.animating : false));
7264 if (!showSurfaceRobustlyLocked(this)) {
7265 return false;
7266 }
7267 mLastAlpha = -1;
7268 mHasDrawn = true;
7269 mLastHidden = false;
7270 mReadyToShow = false;
7271 enableScreenIfNeededLocked();
7272
7273 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007275 int i = mChildWindows.size();
7276 while (i > 0) {
7277 i--;
7278 WindowState c = (WindowState)mChildWindows.get(i);
7279 if (c.mSurface != null && c.mAttachedHidden) {
7280 c.mAttachedHidden = false;
7281 c.performShowLocked();
7282 }
7283 }
Romain Guy06882f82009-06-10 13:36:04 -07007284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 if (mAttrs.type != TYPE_APPLICATION_STARTING
7286 && mAppToken != null) {
7287 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007288
7289 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007290 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7291 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007293 // If this initial window is animating, stop it -- we
7294 // will do an animation to reveal it from behind the
7295 // starting window, so there is no need for it to also
7296 // be doing its own stuff.
7297 if (mAnimation != null) {
7298 mAnimation = null;
7299 // Make sure we clean up the animation.
7300 mAnimating = true;
7301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007302 mFinishedStarting.add(mAppToken);
7303 mH.sendEmptyMessage(H.FINISHED_STARTING);
7304 }
7305 mAppToken.updateReportedVisibilityLocked();
7306 }
7307 }
7308 return true;
7309 }
Romain Guy06882f82009-06-10 13:36:04 -07007310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 // This must be called while inside a transaction. Returns true if
7312 // there is more animation to run.
7313 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7314 if (!mDisplayFrozen) {
7315 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7318 mHasTransformation = true;
7319 mHasLocalTransformation = true;
7320 if (!mLocalAnimating) {
7321 if (DEBUG_ANIM) Log.v(
7322 TAG, "Starting animation in " + this +
7323 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7324 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7325 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7326 mAnimation.setStartTime(currentTime);
7327 mLocalAnimating = true;
7328 mAnimating = true;
7329 }
7330 mTransformation.clear();
7331 final boolean more = mAnimation.getTransformation(
7332 currentTime, mTransformation);
7333 if (DEBUG_ANIM) Log.v(
7334 TAG, "Stepped animation in " + this +
7335 ": more=" + more + ", xform=" + mTransformation);
7336 if (more) {
7337 // we're not done!
7338 return true;
7339 }
7340 if (DEBUG_ANIM) Log.v(
7341 TAG, "Finished animation in " + this +
7342 " @ " + currentTime);
7343 mAnimation = null;
7344 //WindowManagerService.this.dump();
7345 }
7346 mHasLocalTransformation = false;
7347 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007348 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 // When our app token is animating, we kind-of pretend like
7350 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7351 // part of this check means that we will only do this if
7352 // our window is not currently exiting, or it is not
7353 // locally animating itself. The idea being that one that
7354 // is exiting and doing a local animation should be removed
7355 // once that animation is done.
7356 mAnimating = true;
7357 mHasTransformation = true;
7358 mTransformation.clear();
7359 return false;
7360 } else if (mHasTransformation) {
7361 // Little trick to get through the path below to act like
7362 // we have finished an animation.
7363 mAnimating = true;
7364 } else if (isAnimating()) {
7365 mAnimating = true;
7366 }
7367 } else if (mAnimation != null) {
7368 // If the display is frozen, and there is a pending animation,
7369 // clear it and make sure we run the cleanup code.
7370 mAnimating = true;
7371 mLocalAnimating = true;
7372 mAnimation = null;
7373 }
Romain Guy06882f82009-06-10 13:36:04 -07007374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 if (!mAnimating && !mLocalAnimating) {
7376 return false;
7377 }
7378
7379 if (DEBUG_ANIM) Log.v(
7380 TAG, "Animation done in " + this + ": exiting=" + mExiting
7381 + ", reportedVisible="
7382 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007384 mAnimating = false;
7385 mLocalAnimating = false;
7386 mAnimation = null;
7387 mAnimLayer = mLayer;
7388 if (mIsImWindow) {
7389 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007390 } else if (mIsWallpaper) {
7391 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 }
7393 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7394 + " anim layer: " + mAnimLayer);
7395 mHasTransformation = false;
7396 mHasLocalTransformation = false;
7397 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7398 mTransformation.clear();
7399 if (mHasDrawn
7400 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7401 && mAppToken != null
7402 && mAppToken.firstWindowDrawn
7403 && mAppToken.startingData != null) {
7404 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7405 + mToken + ": first real window done animating");
7406 mFinishedStarting.add(mAppToken);
7407 mH.sendEmptyMessage(H.FINISHED_STARTING);
7408 }
Romain Guy06882f82009-06-10 13:36:04 -07007409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 finishExit();
7411
7412 if (mAppToken != null) {
7413 mAppToken.updateReportedVisibilityLocked();
7414 }
7415
7416 return false;
7417 }
7418
7419 void finishExit() {
7420 if (DEBUG_ANIM) Log.v(
7421 TAG, "finishExit in " + this
7422 + ": exiting=" + mExiting
7423 + " remove=" + mRemoveOnExit
7424 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 final int N = mChildWindows.size();
7427 for (int i=0; i<N; i++) {
7428 ((WindowState)mChildWindows.get(i)).finishExit();
7429 }
Romain Guy06882f82009-06-10 13:36:04 -07007430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 if (!mExiting) {
7432 return;
7433 }
Romain Guy06882f82009-06-10 13:36:04 -07007434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 if (isWindowAnimating()) {
7436 return;
7437 }
7438
7439 if (localLOGV) Log.v(
7440 TAG, "Exit animation finished in " + this
7441 + ": remove=" + mRemoveOnExit);
7442 if (mSurface != null) {
7443 mDestroySurface.add(this);
7444 mDestroying = true;
7445 if (SHOW_TRANSACTIONS) Log.i(
7446 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7447 try {
7448 mSurface.hide();
7449 } catch (RuntimeException e) {
7450 Log.w(TAG, "Error hiding surface in " + this, e);
7451 }
7452 mLastHidden = true;
7453 mKeyWaiter.releasePendingPointerLocked(mSession);
7454 }
7455 mExiting = false;
7456 if (mRemoveOnExit) {
7457 mPendingRemove.add(this);
7458 mRemoveOnExit = false;
7459 }
7460 }
Romain Guy06882f82009-06-10 13:36:04 -07007461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7463 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7464 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7465 if (dtdx < -.000001f || dtdx > .000001f) return false;
7466 if (dsdy < -.000001f || dsdy > .000001f) return false;
7467 return true;
7468 }
Romain Guy06882f82009-06-10 13:36:04 -07007469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007470 void computeShownFrameLocked() {
7471 final boolean selfTransformation = mHasLocalTransformation;
7472 Transformation attachedTransformation =
7473 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7474 ? mAttachedWindow.mTransformation : null;
7475 Transformation appTransformation =
7476 (mAppToken != null && mAppToken.hasTransformation)
7477 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007478
7479 // Wallpapers are animated based on the "real" window they
7480 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007481 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007482 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007483 if (mWallpaperTarget.mHasLocalTransformation &&
7484 mWallpaperTarget.mAnimation != null &&
7485 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007486 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007487 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7488 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7489 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007490 }
7491 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007492 mWallpaperTarget.mAppToken.hasTransformation &&
7493 mWallpaperTarget.mAppToken.animation != null &&
7494 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007495 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007496 if (DEBUG_WALLPAPER && appTransformation != null) {
7497 Log.v(TAG, "WP target app xform: " + appTransformation);
7498 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007499 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007500 }
7501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 if (selfTransformation || attachedTransformation != null
7503 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007504 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 final Rect frame = mFrame;
7506 final float tmpFloats[] = mTmpFloats;
7507 final Matrix tmpMatrix = mTmpMatrix;
7508
7509 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007510 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007512 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007514 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007516 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 }
7518 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007519 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 }
7521
7522 // "convert" it into SurfaceFlinger's format
7523 // (a 2x2 matrix + an offset)
7524 // Here we must not transform the position of the surface
7525 // since it is already included in the transformation.
7526 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007528 tmpMatrix.getValues(tmpFloats);
7529 mDsDx = tmpFloats[Matrix.MSCALE_X];
7530 mDtDx = tmpFloats[Matrix.MSKEW_X];
7531 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7532 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007533 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7534 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 int w = frame.width();
7536 int h = frame.height();
7537 mShownFrame.set(x, y, x+w, y+h);
7538
7539 // Now set the alpha... but because our current hardware
7540 // can't do alpha transformation on a non-opaque surface,
7541 // turn it off if we are running an animation that is also
7542 // transforming since it is more important to have that
7543 // animation be smooth.
7544 mShownAlpha = mAlpha;
7545 if (!mLimitedAlphaCompositing
7546 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7547 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7548 && x == frame.left && y == frame.top))) {
7549 //Log.i(TAG, "Applying alpha transform");
7550 if (selfTransformation) {
7551 mShownAlpha *= mTransformation.getAlpha();
7552 }
7553 if (attachedTransformation != null) {
7554 mShownAlpha *= attachedTransformation.getAlpha();
7555 }
7556 if (appTransformation != null) {
7557 mShownAlpha *= appTransformation.getAlpha();
7558 }
7559 } else {
7560 //Log.i(TAG, "Not applying alpha transform");
7561 }
Romain Guy06882f82009-06-10 13:36:04 -07007562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 if (localLOGV) Log.v(
7564 TAG, "Continuing animation in " + this +
7565 ": " + mShownFrame +
7566 ", alpha=" + mTransformation.getAlpha());
7567 return;
7568 }
Romain Guy06882f82009-06-10 13:36:04 -07007569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007571 if (mXOffset != 0 || mYOffset != 0) {
7572 mShownFrame.offset(mXOffset, mYOffset);
7573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 mShownAlpha = mAlpha;
7575 mDsDx = 1;
7576 mDtDx = 0;
7577 mDsDy = 0;
7578 mDtDy = 1;
7579 }
Romain Guy06882f82009-06-10 13:36:04 -07007580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 /**
7582 * Is this window visible? It is not visible if there is no
7583 * surface, or we are in the process of running an exit animation
7584 * that will remove the surface, or its app token has been hidden.
7585 */
7586 public boolean isVisibleLw() {
7587 final AppWindowToken atoken = mAppToken;
7588 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7589 && (atoken == null || !atoken.hiddenRequested)
7590 && !mExiting && !mDestroying;
7591 }
7592
7593 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007594 * Like {@link #isVisibleLw}, but also counts a window that is currently
7595 * "hidden" behind the keyguard as visible. This allows us to apply
7596 * things like window flags that impact the keyguard.
7597 * XXX I am starting to think we need to have ANOTHER visibility flag
7598 * for this "hidden behind keyguard" state rather than overloading
7599 * mPolicyVisibility. Ungh.
7600 */
7601 public boolean isVisibleOrBehindKeyguardLw() {
7602 final AppWindowToken atoken = mAppToken;
7603 return mSurface != null && !mAttachedHidden
7604 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7605 && !mExiting && !mDestroying;
7606 }
7607
7608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 * Is this window visible, ignoring its app token? It is not visible
7610 * if there is no surface, or we are in the process of running an exit animation
7611 * that will remove the surface.
7612 */
7613 public boolean isWinVisibleLw() {
7614 final AppWindowToken atoken = mAppToken;
7615 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7616 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7617 && !mExiting && !mDestroying;
7618 }
7619
7620 /**
7621 * The same as isVisible(), but follows the current hidden state of
7622 * the associated app token, not the pending requested hidden state.
7623 */
7624 boolean isVisibleNow() {
7625 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007626 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 }
7628
7629 /**
7630 * Same as isVisible(), but we also count it as visible between the
7631 * call to IWindowSession.add() and the first relayout().
7632 */
7633 boolean isVisibleOrAdding() {
7634 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007635 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7637 && mPolicyVisibility && !mAttachedHidden
7638 && (atoken == null || !atoken.hiddenRequested)
7639 && !mExiting && !mDestroying;
7640 }
7641
7642 /**
7643 * Is this window currently on-screen? It is on-screen either if it
7644 * is visible or it is currently running an animation before no longer
7645 * being visible.
7646 */
7647 boolean isOnScreen() {
7648 final AppWindowToken atoken = mAppToken;
7649 if (atoken != null) {
7650 return mSurface != null && mPolicyVisibility && !mDestroying
7651 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007652 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 } else {
7654 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007655 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 }
7657 }
Romain Guy06882f82009-06-10 13:36:04 -07007658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 /**
7660 * Like isOnScreen(), but we don't return true if the window is part
7661 * of a transition that has not yet been started.
7662 */
7663 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007664 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007665 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007666 return false;
7667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007669 final boolean animating = atoken != null
7670 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007671 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007672 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7673 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007674 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007675 }
7676
7677 /** Is the window or its container currently animating? */
7678 boolean isAnimating() {
7679 final WindowState attached = mAttachedWindow;
7680 final AppWindowToken atoken = mAppToken;
7681 return mAnimation != null
7682 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007683 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 (atoken.animation != null
7685 || atoken.inPendingTransaction));
7686 }
7687
7688 /** Is this window currently animating? */
7689 boolean isWindowAnimating() {
7690 return mAnimation != null;
7691 }
7692
7693 /**
7694 * Like isOnScreen, but returns false if the surface hasn't yet
7695 * been drawn.
7696 */
7697 public boolean isDisplayedLw() {
7698 final AppWindowToken atoken = mAppToken;
7699 return mSurface != null && mPolicyVisibility && !mDestroying
7700 && !mDrawPending && !mCommitDrawPending
7701 && ((!mAttachedHidden &&
7702 (atoken == null || !atoken.hiddenRequested))
7703 || mAnimating);
7704 }
7705
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007706 /**
7707 * Returns true if the window has a surface that it has drawn a
7708 * complete UI in to.
7709 */
7710 public boolean isDrawnLw() {
7711 final AppWindowToken atoken = mAppToken;
7712 return mSurface != null && !mDestroying
7713 && !mDrawPending && !mCommitDrawPending;
7714 }
7715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7717 boolean shownFrame, boolean onlyOpaque) {
7718 if (mSurface == null) {
7719 return false;
7720 }
7721 if (mAppToken != null && !mAppToken.appFullscreen) {
7722 return false;
7723 }
7724 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7725 return false;
7726 }
7727 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007728
7729 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7730 return frame.left <= mCompatibleScreenFrame.left &&
7731 frame.top <= mCompatibleScreenFrame.top &&
7732 frame.right >= mCompatibleScreenFrame.right &&
7733 frame.bottom >= mCompatibleScreenFrame.bottom;
7734 } else {
7735 return frame.left <= 0 && frame.top <= 0
7736 && frame.right >= screenWidth
7737 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739 }
Romain Guy06882f82009-06-10 13:36:04 -07007740
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007741 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007742 * Return true if the window is opaque and fully drawn. This indicates
7743 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007744 */
7745 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007746 return (mAttrs.format == PixelFormat.OPAQUE
7747 || mAttrs.type == TYPE_WALLPAPER)
7748 && mSurface != null && mAnimation == null
7749 && (mAppToken == null || mAppToken.animation == null)
7750 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007751 }
7752
7753 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7754 return
7755 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007756 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7757 // only if it's visible
7758 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007759 // and only if the application fills the compatible screen
7760 mFrame.left <= mCompatibleScreenFrame.left &&
7761 mFrame.top <= mCompatibleScreenFrame.top &&
7762 mFrame.right >= mCompatibleScreenFrame.right &&
7763 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007764 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007765 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007766 }
7767
7768 boolean isFullscreen(int screenWidth, int screenHeight) {
7769 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007770 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 }
7772
7773 void removeLocked() {
7774 if (mAttachedWindow != null) {
7775 mAttachedWindow.mChildWindows.remove(this);
7776 }
7777 destroySurfaceLocked();
7778 mSession.windowRemovedLocked();
7779 try {
7780 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7781 } catch (RuntimeException e) {
7782 // Ignore if it has already been removed (usually because
7783 // we are doing this as part of processing a death note.)
7784 }
7785 }
7786
7787 private class DeathRecipient implements IBinder.DeathRecipient {
7788 public void binderDied() {
7789 try {
7790 synchronized(mWindowMap) {
7791 WindowState win = windowForClientLocked(mSession, mClient);
7792 Log.i(TAG, "WIN DEATH: " + win);
7793 if (win != null) {
7794 removeWindowLocked(mSession, win);
7795 }
7796 }
7797 } catch (IllegalArgumentException ex) {
7798 // This will happen if the window has already been
7799 // removed.
7800 }
7801 }
7802 }
7803
7804 /** Returns true if this window desires key events. */
7805 public final boolean canReceiveKeys() {
7806 return isVisibleOrAdding()
7807 && (mViewVisibility == View.VISIBLE)
7808 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7809 }
7810
7811 public boolean hasDrawnLw() {
7812 return mHasDrawn;
7813 }
7814
7815 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007816 return showLw(doAnimation, true);
7817 }
7818
7819 boolean showLw(boolean doAnimation, boolean requestAnim) {
7820 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7821 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007823 mPolicyVisibility = true;
7824 mPolicyVisibilityAfterAnim = true;
7825 if (doAnimation) {
7826 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7827 }
7828 if (requestAnim) {
7829 requestAnimationLocked(0);
7830 }
7831 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 }
7833
7834 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007835 return hideLw(doAnimation, true);
7836 }
7837
7838 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7840 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007841 if (!current) {
7842 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007844 if (doAnimation) {
7845 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7846 if (mAnimation == null) {
7847 doAnimation = false;
7848 }
7849 }
7850 if (doAnimation) {
7851 mPolicyVisibilityAfterAnim = false;
7852 } else {
7853 mPolicyVisibilityAfterAnim = false;
7854 mPolicyVisibility = false;
7855 }
7856 if (requestAnim) {
7857 requestAnimationLocked(0);
7858 }
7859 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 }
7861
7862 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007863 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007864
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007865 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7866 pw.print(" mClient="); pw.println(mClient.asBinder());
7867 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7868 if (mAttachedWindow != null || mLayoutAttached) {
7869 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7870 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7871 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007872 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7873 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7874 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007875 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7876 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007877 }
7878 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7879 pw.print(" mSubLayer="); pw.print(mSubLayer);
7880 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7881 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7882 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7883 pw.print("="); pw.print(mAnimLayer);
7884 pw.print(" mLastLayer="); pw.println(mLastLayer);
7885 if (mSurface != null) {
7886 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7887 }
7888 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7889 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7890 if (mAppToken != null) {
7891 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7892 }
7893 if (mTargetAppToken != null) {
7894 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7895 }
7896 pw.print(prefix); pw.print("mViewVisibility=0x");
7897 pw.print(Integer.toHexString(mViewVisibility));
7898 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007899 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7900 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007901 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7902 pw.print(prefix); pw.print("mPolicyVisibility=");
7903 pw.print(mPolicyVisibility);
7904 pw.print(" mPolicyVisibilityAfterAnim=");
7905 pw.print(mPolicyVisibilityAfterAnim);
7906 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7907 }
7908 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007909 pw.print(" h="); pw.println(mRequestedHeight);
7910 if (mXOffset != 0 || mYOffset != 0) {
7911 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7912 pw.print(" y="); pw.println(mYOffset);
7913 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007914 pw.print(prefix); pw.print("mGivenContentInsets=");
7915 mGivenContentInsets.printShortString(pw);
7916 pw.print(" mGivenVisibleInsets=");
7917 mGivenVisibleInsets.printShortString(pw);
7918 pw.println();
7919 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7920 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7921 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7922 }
7923 pw.print(prefix); pw.print("mShownFrame=");
7924 mShownFrame.printShortString(pw);
7925 pw.print(" last="); mLastShownFrame.printShortString(pw);
7926 pw.println();
7927 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7928 pw.print(" last="); mLastFrame.printShortString(pw);
7929 pw.println();
7930 pw.print(prefix); pw.print("mContainingFrame=");
7931 mContainingFrame.printShortString(pw);
7932 pw.print(" mDisplayFrame=");
7933 mDisplayFrame.printShortString(pw);
7934 pw.println();
7935 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7936 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7937 pw.println();
7938 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7939 pw.print(" last="); mLastContentInsets.printShortString(pw);
7940 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7941 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7942 pw.println();
7943 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7944 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7945 pw.print(" mAlpha="); pw.print(mAlpha);
7946 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7947 }
7948 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7949 || mAnimation != null) {
7950 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7951 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7952 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7953 pw.print(" mAnimation="); pw.println(mAnimation);
7954 }
7955 if (mHasTransformation || mHasLocalTransformation) {
7956 pw.print(prefix); pw.print("XForm: has=");
7957 pw.print(mHasTransformation);
7958 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7959 pw.print(" "); mTransformation.printShortString(pw);
7960 pw.println();
7961 }
7962 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7963 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7964 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7965 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7966 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7967 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7968 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7969 pw.print(" mDestroying="); pw.print(mDestroying);
7970 pw.print(" mRemoved="); pw.println(mRemoved);
7971 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007972 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007973 pw.print(prefix); pw.print("mOrientationChanging=");
7974 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007975 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7976 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007977 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007978 if (mHScale != 1 || mVScale != 1) {
7979 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7980 pw.print(" mVScale="); pw.println(mVScale);
7981 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007982 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007983 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7984 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 }
7987
7988 @Override
7989 public String toString() {
7990 return "Window{"
7991 + Integer.toHexString(System.identityHashCode(this))
7992 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7993 }
7994 }
Romain Guy06882f82009-06-10 13:36:04 -07007995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 // -------------------------------------------------------------
7997 // Window Token State
7998 // -------------------------------------------------------------
7999
8000 class WindowToken {
8001 // The actual token.
8002 final IBinder token;
8003
8004 // The type of window this token is for, as per WindowManager.LayoutParams.
8005 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 // Set if this token was explicitly added by a client, so should
8008 // not be removed when all windows are removed.
8009 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008010
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008011 // For printing.
8012 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 // If this is an AppWindowToken, this is non-null.
8015 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 // All of the windows associated with this token.
8018 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8019
8020 // Is key dispatching paused for this token?
8021 boolean paused = false;
8022
8023 // Should this token's windows be hidden?
8024 boolean hidden;
8025
8026 // Temporary for finding which tokens no longer have visible windows.
8027 boolean hasVisible;
8028
Dianne Hackborna8f60182009-09-01 19:01:50 -07008029 // Set to true when this token is in a pending transaction where it
8030 // will be shown.
8031 boolean waitingToShow;
8032
8033 // Set to true when this token is in a pending transaction where it
8034 // will be hidden.
8035 boolean waitingToHide;
8036
8037 // Set to true when this token is in a pending transaction where its
8038 // windows will be put to the bottom of the list.
8039 boolean sendingToBottom;
8040
8041 // Set to true when this token is in a pending transaction where its
8042 // windows will be put to the top of the list.
8043 boolean sendingToTop;
8044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 WindowToken(IBinder _token, int type, boolean _explicit) {
8046 token = _token;
8047 windowType = type;
8048 explicit = _explicit;
8049 }
8050
8051 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008052 pw.print(prefix); pw.print("token="); pw.println(token);
8053 pw.print(prefix); pw.print("windows="); pw.println(windows);
8054 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8055 pw.print(" hidden="); pw.print(hidden);
8056 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008057 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8058 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8059 pw.print(" waitingToHide="); pw.print(waitingToHide);
8060 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8061 pw.print(" sendingToTop="); pw.println(sendingToTop);
8062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008063 }
8064
8065 @Override
8066 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008067 if (stringName == null) {
8068 StringBuilder sb = new StringBuilder();
8069 sb.append("WindowToken{");
8070 sb.append(Integer.toHexString(System.identityHashCode(this)));
8071 sb.append(" token="); sb.append(token); sb.append('}');
8072 stringName = sb.toString();
8073 }
8074 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 }
8076 };
8077
8078 class AppWindowToken extends WindowToken {
8079 // Non-null only for application tokens.
8080 final IApplicationToken appToken;
8081
8082 // All of the windows and child windows that are included in this
8083 // application token. Note this list is NOT sorted!
8084 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8085
8086 int groupId = -1;
8087 boolean appFullscreen;
8088 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 // These are used for determining when all windows associated with
8091 // an activity have been drawn, so they can be made visible together
8092 // at the same time.
8093 int lastTransactionSequence = mTransactionSequence-1;
8094 int numInterestingWindows;
8095 int numDrawnWindows;
8096 boolean inPendingTransaction;
8097 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 // Is this token going to be hidden in a little while? If so, it
8100 // won't be taken into account for setting the screen orientation.
8101 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 // Is this window's surface needed? This is almost like hidden, except
8104 // it will sometimes be true a little earlier: when the token has
8105 // been shown, but is still waiting for its app transition to execute
8106 // before making its windows shown.
8107 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109 // Have we told the window clients to hide themselves?
8110 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008112 // Last visibility state we reported to the app token.
8113 boolean reportedVisible;
8114
8115 // Set to true when the token has been removed from the window mgr.
8116 boolean removed;
8117
8118 // Have we been asked to have this token keep the screen frozen?
8119 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 boolean animating;
8122 Animation animation;
8123 boolean hasTransformation;
8124 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008126 // Offset to the window of all layers in the token, for use by
8127 // AppWindowToken animations.
8128 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 // Information about an application starting window if displayed.
8131 StartingData startingData;
8132 WindowState startingWindow;
8133 View startingView;
8134 boolean startingDisplayed;
8135 boolean startingMoved;
8136 boolean firstWindowDrawn;
8137
8138 AppWindowToken(IApplicationToken _token) {
8139 super(_token.asBinder(),
8140 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8141 appWindowToken = this;
8142 appToken = _token;
8143 }
Romain Guy06882f82009-06-10 13:36:04 -07008144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 public void setAnimation(Animation anim) {
8146 if (localLOGV) Log.v(
8147 TAG, "Setting animation in " + this + ": " + anim);
8148 animation = anim;
8149 animating = false;
8150 anim.restrictDuration(MAX_ANIMATION_DURATION);
8151 anim.scaleCurrentDuration(mTransitionAnimationScale);
8152 int zorder = anim.getZAdjustment();
8153 int adj = 0;
8154 if (zorder == Animation.ZORDER_TOP) {
8155 adj = TYPE_LAYER_OFFSET;
8156 } else if (zorder == Animation.ZORDER_BOTTOM) {
8157 adj = -TYPE_LAYER_OFFSET;
8158 }
Romain Guy06882f82009-06-10 13:36:04 -07008159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 if (animLayerAdjustment != adj) {
8161 animLayerAdjustment = adj;
8162 updateLayers();
8163 }
8164 }
Romain Guy06882f82009-06-10 13:36:04 -07008165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 public void setDummyAnimation() {
8167 if (animation == null) {
8168 if (localLOGV) Log.v(
8169 TAG, "Setting dummy animation in " + this);
8170 animation = sDummyAnimation;
8171 }
8172 }
8173
8174 public void clearAnimation() {
8175 if (animation != null) {
8176 animation = null;
8177 animating = true;
8178 }
8179 }
Romain Guy06882f82009-06-10 13:36:04 -07008180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181 void updateLayers() {
8182 final int N = allAppWindows.size();
8183 final int adj = animLayerAdjustment;
8184 for (int i=0; i<N; i++) {
8185 WindowState w = allAppWindows.get(i);
8186 w.mAnimLayer = w.mLayer + adj;
8187 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8188 + w.mAnimLayer);
8189 if (w == mInputMethodTarget) {
8190 setInputMethodAnimLayerAdjustment(adj);
8191 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008192 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008193 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008195 }
8196 }
Romain Guy06882f82009-06-10 13:36:04 -07008197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 void sendAppVisibilityToClients() {
8199 final int N = allAppWindows.size();
8200 for (int i=0; i<N; i++) {
8201 WindowState win = allAppWindows.get(i);
8202 if (win == startingWindow && clientHidden) {
8203 // Don't hide the starting window.
8204 continue;
8205 }
8206 try {
8207 if (DEBUG_VISIBILITY) Log.v(TAG,
8208 "Setting visibility of " + win + ": " + (!clientHidden));
8209 win.mClient.dispatchAppVisibility(!clientHidden);
8210 } catch (RemoteException e) {
8211 }
8212 }
8213 }
Romain Guy06882f82009-06-10 13:36:04 -07008214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 void showAllWindowsLocked() {
8216 final int NW = allAppWindows.size();
8217 for (int i=0; i<NW; i++) {
8218 WindowState w = allAppWindows.get(i);
8219 if (DEBUG_VISIBILITY) Log.v(TAG,
8220 "performing show on: " + w);
8221 w.performShowLocked();
8222 }
8223 }
Romain Guy06882f82009-06-10 13:36:04 -07008224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 // This must be called while inside a transaction.
8226 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8227 if (!mDisplayFrozen) {
8228 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230 if (animation == sDummyAnimation) {
8231 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008232 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 // when it is really time to animate, this will be set to
8234 // a real animation and the next call will execute normally.
8235 return false;
8236 }
Romain Guy06882f82009-06-10 13:36:04 -07008237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8239 if (!animating) {
8240 if (DEBUG_ANIM) Log.v(
8241 TAG, "Starting animation in " + this +
8242 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8243 + " scale=" + mTransitionAnimationScale
8244 + " allDrawn=" + allDrawn + " animating=" + animating);
8245 animation.initialize(dw, dh, dw, dh);
8246 animation.setStartTime(currentTime);
8247 animating = true;
8248 }
8249 transformation.clear();
8250 final boolean more = animation.getTransformation(
8251 currentTime, transformation);
8252 if (DEBUG_ANIM) Log.v(
8253 TAG, "Stepped animation in " + this +
8254 ": more=" + more + ", xform=" + transformation);
8255 if (more) {
8256 // we're done!
8257 hasTransformation = true;
8258 return true;
8259 }
8260 if (DEBUG_ANIM) Log.v(
8261 TAG, "Finished animation in " + this +
8262 " @ " + currentTime);
8263 animation = null;
8264 }
8265 } else if (animation != null) {
8266 // If the display is frozen, and there is a pending animation,
8267 // clear it and make sure we run the cleanup code.
8268 animating = true;
8269 animation = null;
8270 }
8271
8272 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 if (!animating) {
8275 return false;
8276 }
8277
8278 clearAnimation();
8279 animating = false;
8280 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8281 moveInputMethodWindowsIfNeededLocked(true);
8282 }
Romain Guy06882f82009-06-10 13:36:04 -07008283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 if (DEBUG_ANIM) Log.v(
8285 TAG, "Animation done in " + this
8286 + ": reportedVisible=" + reportedVisible);
8287
8288 transformation.clear();
8289 if (animLayerAdjustment != 0) {
8290 animLayerAdjustment = 0;
8291 updateLayers();
8292 }
Romain Guy06882f82009-06-10 13:36:04 -07008293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 final int N = windows.size();
8295 for (int i=0; i<N; i++) {
8296 ((WindowState)windows.get(i)).finishExit();
8297 }
8298 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 return false;
8301 }
8302
8303 void updateReportedVisibilityLocked() {
8304 if (appToken == null) {
8305 return;
8306 }
Romain Guy06882f82009-06-10 13:36:04 -07008307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 int numInteresting = 0;
8309 int numVisible = 0;
8310 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8313 final int N = allAppWindows.size();
8314 for (int i=0; i<N; i++) {
8315 WindowState win = allAppWindows.get(i);
8316 if (win == startingWindow || win.mAppFreezing) {
8317 continue;
8318 }
8319 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008320 Log.v(TAG, "Win " + win + ": isDrawn="
8321 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008323 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 Log.v(TAG, "Not displayed: s=" + win.mSurface
8325 + " pv=" + win.mPolicyVisibility
8326 + " dp=" + win.mDrawPending
8327 + " cdp=" + win.mCommitDrawPending
8328 + " ah=" + win.mAttachedHidden
8329 + " th="
8330 + (win.mAppToken != null
8331 ? win.mAppToken.hiddenRequested : false)
8332 + " a=" + win.mAnimating);
8333 }
8334 }
8335 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008336 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 if (!win.isAnimating()) {
8338 numVisible++;
8339 }
8340 nowGone = false;
8341 } else if (win.isAnimating()) {
8342 nowGone = false;
8343 }
8344 }
Romain Guy06882f82009-06-10 13:36:04 -07008345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8347 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8348 + numInteresting + " visible=" + numVisible);
8349 if (nowVisible != reportedVisible) {
8350 if (DEBUG_VISIBILITY) Log.v(
8351 TAG, "Visibility changed in " + this
8352 + ": vis=" + nowVisible);
8353 reportedVisible = nowVisible;
8354 Message m = mH.obtainMessage(
8355 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8356 nowVisible ? 1 : 0,
8357 nowGone ? 1 : 0,
8358 this);
8359 mH.sendMessage(m);
8360 }
8361 }
Romain Guy06882f82009-06-10 13:36:04 -07008362
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008363 WindowState findMainWindow() {
8364 int j = windows.size();
8365 while (j > 0) {
8366 j--;
8367 WindowState win = windows.get(j);
8368 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8369 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8370 return win;
8371 }
8372 }
8373 return null;
8374 }
8375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 void dump(PrintWriter pw, String prefix) {
8377 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008378 if (appToken != null) {
8379 pw.print(prefix); pw.println("app=true");
8380 }
8381 if (allAppWindows.size() > 0) {
8382 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8383 }
8384 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008385 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008386 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8387 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8388 pw.print(" clientHidden="); pw.print(clientHidden);
8389 pw.print(" willBeHidden="); pw.print(willBeHidden);
8390 pw.print(" reportedVisible="); pw.println(reportedVisible);
8391 if (paused || freezingScreen) {
8392 pw.print(prefix); pw.print("paused="); pw.print(paused);
8393 pw.print(" freezingScreen="); pw.println(freezingScreen);
8394 }
8395 if (numInterestingWindows != 0 || numDrawnWindows != 0
8396 || inPendingTransaction || allDrawn) {
8397 pw.print(prefix); pw.print("numInterestingWindows=");
8398 pw.print(numInterestingWindows);
8399 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8400 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8401 pw.print(" allDrawn="); pw.println(allDrawn);
8402 }
8403 if (animating || animation != null) {
8404 pw.print(prefix); pw.print("animating="); pw.print(animating);
8405 pw.print(" animation="); pw.println(animation);
8406 }
8407 if (animLayerAdjustment != 0) {
8408 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8409 }
8410 if (hasTransformation) {
8411 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8412 pw.print(" transformation="); transformation.printShortString(pw);
8413 pw.println();
8414 }
8415 if (startingData != null || removed || firstWindowDrawn) {
8416 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8417 pw.print(" removed="); pw.print(removed);
8418 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8419 }
8420 if (startingWindow != null || startingView != null
8421 || startingDisplayed || startingMoved) {
8422 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8423 pw.print(" startingView="); pw.print(startingView);
8424 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8425 pw.print(" startingMoved"); pw.println(startingMoved);
8426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 }
8428
8429 @Override
8430 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008431 if (stringName == null) {
8432 StringBuilder sb = new StringBuilder();
8433 sb.append("AppWindowToken{");
8434 sb.append(Integer.toHexString(System.identityHashCode(this)));
8435 sb.append(" token="); sb.append(token); sb.append('}');
8436 stringName = sb.toString();
8437 }
8438 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 }
8440 }
Romain Guy06882f82009-06-10 13:36:04 -07008441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 // -------------------------------------------------------------
8443 // DummyAnimation
8444 // -------------------------------------------------------------
8445
8446 // This is an animation that does nothing: it just immediately finishes
8447 // itself every time it is called. It is used as a stub animation in cases
8448 // where we want to synchronize multiple things that may be animating.
8449 static final class DummyAnimation extends Animation {
8450 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8451 return false;
8452 }
8453 }
8454 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008456 // -------------------------------------------------------------
8457 // Async Handler
8458 // -------------------------------------------------------------
8459
8460 static final class StartingData {
8461 final String pkg;
8462 final int theme;
8463 final CharSequence nonLocalizedLabel;
8464 final int labelRes;
8465 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008467 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8468 int _labelRes, int _icon) {
8469 pkg = _pkg;
8470 theme = _theme;
8471 nonLocalizedLabel = _nonLocalizedLabel;
8472 labelRes = _labelRes;
8473 icon = _icon;
8474 }
8475 }
8476
8477 private final class H extends Handler {
8478 public static final int REPORT_FOCUS_CHANGE = 2;
8479 public static final int REPORT_LOSING_FOCUS = 3;
8480 public static final int ANIMATE = 4;
8481 public static final int ADD_STARTING = 5;
8482 public static final int REMOVE_STARTING = 6;
8483 public static final int FINISHED_STARTING = 7;
8484 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008485 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8486 public static final int HOLD_SCREEN_CHANGED = 12;
8487 public static final int APP_TRANSITION_TIMEOUT = 13;
8488 public static final int PERSIST_ANIMATION_SCALE = 14;
8489 public static final int FORCE_GC = 15;
8490 public static final int ENABLE_SCREEN = 16;
8491 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008492 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 public H() {
8497 }
Romain Guy06882f82009-06-10 13:36:04 -07008498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 @Override
8500 public void handleMessage(Message msg) {
8501 switch (msg.what) {
8502 case REPORT_FOCUS_CHANGE: {
8503 WindowState lastFocus;
8504 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008506 synchronized(mWindowMap) {
8507 lastFocus = mLastFocus;
8508 newFocus = mCurrentFocus;
8509 if (lastFocus == newFocus) {
8510 // Focus is not changing, so nothing to do.
8511 return;
8512 }
8513 mLastFocus = newFocus;
8514 //Log.i(TAG, "Focus moving from " + lastFocus
8515 // + " to " + newFocus);
8516 if (newFocus != null && lastFocus != null
8517 && !newFocus.isDisplayedLw()) {
8518 //Log.i(TAG, "Delaying loss of focus...");
8519 mLosingFocus.add(lastFocus);
8520 lastFocus = null;
8521 }
8522 }
8523
8524 if (lastFocus != newFocus) {
8525 //System.out.println("Changing focus from " + lastFocus
8526 // + " to " + newFocus);
8527 if (newFocus != null) {
8528 try {
8529 //Log.i(TAG, "Gaining focus: " + newFocus);
8530 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8531 } catch (RemoteException e) {
8532 // Ignore if process has died.
8533 }
8534 }
8535
8536 if (lastFocus != null) {
8537 try {
8538 //Log.i(TAG, "Losing focus: " + lastFocus);
8539 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8540 } catch (RemoteException e) {
8541 // Ignore if process has died.
8542 }
8543 }
8544 }
8545 } break;
8546
8547 case REPORT_LOSING_FOCUS: {
8548 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 synchronized(mWindowMap) {
8551 losers = mLosingFocus;
8552 mLosingFocus = new ArrayList<WindowState>();
8553 }
8554
8555 final int N = losers.size();
8556 for (int i=0; i<N; i++) {
8557 try {
8558 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8559 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8560 } catch (RemoteException e) {
8561 // Ignore if process has died.
8562 }
8563 }
8564 } break;
8565
8566 case ANIMATE: {
8567 synchronized(mWindowMap) {
8568 mAnimationPending = false;
8569 performLayoutAndPlaceSurfacesLocked();
8570 }
8571 } break;
8572
8573 case ADD_STARTING: {
8574 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8575 final StartingData sd = wtoken.startingData;
8576
8577 if (sd == null) {
8578 // Animation has been canceled... do nothing.
8579 return;
8580 }
Romain Guy06882f82009-06-10 13:36:04 -07008581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8583 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 View view = null;
8586 try {
8587 view = mPolicy.addStartingWindow(
8588 wtoken.token, sd.pkg,
8589 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8590 sd.icon);
8591 } catch (Exception e) {
8592 Log.w(TAG, "Exception when adding starting window", e);
8593 }
8594
8595 if (view != null) {
8596 boolean abort = false;
8597
8598 synchronized(mWindowMap) {
8599 if (wtoken.removed || wtoken.startingData == null) {
8600 // If the window was successfully added, then
8601 // we need to remove it.
8602 if (wtoken.startingWindow != null) {
8603 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8604 "Aborted starting " + wtoken
8605 + ": removed=" + wtoken.removed
8606 + " startingData=" + wtoken.startingData);
8607 wtoken.startingWindow = null;
8608 wtoken.startingData = null;
8609 abort = true;
8610 }
8611 } else {
8612 wtoken.startingView = view;
8613 }
8614 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8615 "Added starting " + wtoken
8616 + ": startingWindow="
8617 + wtoken.startingWindow + " startingView="
8618 + wtoken.startingView);
8619 }
8620
8621 if (abort) {
8622 try {
8623 mPolicy.removeStartingWindow(wtoken.token, view);
8624 } catch (Exception e) {
8625 Log.w(TAG, "Exception when removing starting window", e);
8626 }
8627 }
8628 }
8629 } break;
8630
8631 case REMOVE_STARTING: {
8632 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8633 IBinder token = null;
8634 View view = null;
8635 synchronized (mWindowMap) {
8636 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8637 + wtoken + ": startingWindow="
8638 + wtoken.startingWindow + " startingView="
8639 + wtoken.startingView);
8640 if (wtoken.startingWindow != null) {
8641 view = wtoken.startingView;
8642 token = wtoken.token;
8643 wtoken.startingData = null;
8644 wtoken.startingView = null;
8645 wtoken.startingWindow = null;
8646 }
8647 }
8648 if (view != null) {
8649 try {
8650 mPolicy.removeStartingWindow(token, view);
8651 } catch (Exception e) {
8652 Log.w(TAG, "Exception when removing starting window", e);
8653 }
8654 }
8655 } break;
8656
8657 case FINISHED_STARTING: {
8658 IBinder token = null;
8659 View view = null;
8660 while (true) {
8661 synchronized (mWindowMap) {
8662 final int N = mFinishedStarting.size();
8663 if (N <= 0) {
8664 break;
8665 }
8666 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8667
8668 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8669 "Finished starting " + wtoken
8670 + ": startingWindow=" + wtoken.startingWindow
8671 + " startingView=" + wtoken.startingView);
8672
8673 if (wtoken.startingWindow == null) {
8674 continue;
8675 }
8676
8677 view = wtoken.startingView;
8678 token = wtoken.token;
8679 wtoken.startingData = null;
8680 wtoken.startingView = null;
8681 wtoken.startingWindow = null;
8682 }
8683
8684 try {
8685 mPolicy.removeStartingWindow(token, view);
8686 } catch (Exception e) {
8687 Log.w(TAG, "Exception when removing starting window", e);
8688 }
8689 }
8690 } break;
8691
8692 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8693 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8694
8695 boolean nowVisible = msg.arg1 != 0;
8696 boolean nowGone = msg.arg2 != 0;
8697
8698 try {
8699 if (DEBUG_VISIBILITY) Log.v(
8700 TAG, "Reporting visible in " + wtoken
8701 + " visible=" + nowVisible
8702 + " gone=" + nowGone);
8703 if (nowVisible) {
8704 wtoken.appToken.windowsVisible();
8705 } else {
8706 wtoken.appToken.windowsGone();
8707 }
8708 } catch (RemoteException ex) {
8709 }
8710 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008712 case WINDOW_FREEZE_TIMEOUT: {
8713 synchronized (mWindowMap) {
8714 Log.w(TAG, "Window freeze timeout expired.");
8715 int i = mWindows.size();
8716 while (i > 0) {
8717 i--;
8718 WindowState w = (WindowState)mWindows.get(i);
8719 if (w.mOrientationChanging) {
8720 w.mOrientationChanging = false;
8721 Log.w(TAG, "Force clearing orientation change: " + w);
8722 }
8723 }
8724 performLayoutAndPlaceSurfacesLocked();
8725 }
8726 break;
8727 }
Romain Guy06882f82009-06-10 13:36:04 -07008728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008729 case HOLD_SCREEN_CHANGED: {
8730 Session oldHold;
8731 Session newHold;
8732 synchronized (mWindowMap) {
8733 oldHold = mLastReportedHold;
8734 newHold = (Session)msg.obj;
8735 mLastReportedHold = newHold;
8736 }
Romain Guy06882f82009-06-10 13:36:04 -07008737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 if (oldHold != newHold) {
8739 try {
8740 if (oldHold != null) {
8741 mBatteryStats.noteStopWakelock(oldHold.mUid,
8742 "window",
8743 BatteryStats.WAKE_TYPE_WINDOW);
8744 }
8745 if (newHold != null) {
8746 mBatteryStats.noteStartWakelock(newHold.mUid,
8747 "window",
8748 BatteryStats.WAKE_TYPE_WINDOW);
8749 }
8750 } catch (RemoteException e) {
8751 }
8752 }
8753 break;
8754 }
Romain Guy06882f82009-06-10 13:36:04 -07008755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 case APP_TRANSITION_TIMEOUT: {
8757 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008758 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8760 "*** APP TRANSITION TIMEOUT");
8761 mAppTransitionReady = true;
8762 mAppTransitionTimeout = true;
8763 performLayoutAndPlaceSurfacesLocked();
8764 }
8765 }
8766 break;
8767 }
Romain Guy06882f82009-06-10 13:36:04 -07008768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008769 case PERSIST_ANIMATION_SCALE: {
8770 Settings.System.putFloat(mContext.getContentResolver(),
8771 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8772 Settings.System.putFloat(mContext.getContentResolver(),
8773 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8774 break;
8775 }
Romain Guy06882f82009-06-10 13:36:04 -07008776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008777 case FORCE_GC: {
8778 synchronized(mWindowMap) {
8779 if (mAnimationPending) {
8780 // If we are animating, don't do the gc now but
8781 // delay a bit so we don't interrupt the animation.
8782 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8783 2000);
8784 return;
8785 }
8786 // If we are currently rotating the display, it will
8787 // schedule a new message when done.
8788 if (mDisplayFrozen) {
8789 return;
8790 }
8791 mFreezeGcPending = 0;
8792 }
8793 Runtime.getRuntime().gc();
8794 break;
8795 }
Romain Guy06882f82009-06-10 13:36:04 -07008796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 case ENABLE_SCREEN: {
8798 performEnableScreen();
8799 break;
8800 }
Romain Guy06882f82009-06-10 13:36:04 -07008801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008802 case APP_FREEZE_TIMEOUT: {
8803 synchronized (mWindowMap) {
8804 Log.w(TAG, "App freeze timeout expired.");
8805 int i = mAppTokens.size();
8806 while (i > 0) {
8807 i--;
8808 AppWindowToken tok = mAppTokens.get(i);
8809 if (tok.freezingScreen) {
8810 Log.w(TAG, "Force clearing freeze: " + tok);
8811 unsetAppFreezingScreenLocked(tok, true, true);
8812 }
8813 }
8814 }
8815 break;
8816 }
Romain Guy06882f82009-06-10 13:36:04 -07008817
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008818 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008819 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008820 sendNewConfiguration();
8821 }
8822 break;
8823 }
Romain Guy06882f82009-06-10 13:36:04 -07008824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008825 }
8826 }
8827 }
8828
8829 // -------------------------------------------------------------
8830 // IWindowManager API
8831 // -------------------------------------------------------------
8832
8833 public IWindowSession openSession(IInputMethodClient client,
8834 IInputContext inputContext) {
8835 if (client == null) throw new IllegalArgumentException("null client");
8836 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8837 return new Session(client, inputContext);
8838 }
8839
8840 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8841 synchronized (mWindowMap) {
8842 // The focus for the client is the window immediately below
8843 // where we would place the input method window.
8844 int idx = findDesiredInputMethodWindowIndexLocked(false);
8845 WindowState imFocus;
8846 if (idx > 0) {
8847 imFocus = (WindowState)mWindows.get(idx-1);
8848 if (imFocus != null) {
8849 if (imFocus.mSession.mClient != null &&
8850 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8851 return true;
8852 }
8853 }
8854 }
8855 }
8856 return false;
8857 }
Romain Guy06882f82009-06-10 13:36:04 -07008858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008859 // -------------------------------------------------------------
8860 // Internals
8861 // -------------------------------------------------------------
8862
8863 final WindowState windowForClientLocked(Session session, IWindow client) {
8864 return windowForClientLocked(session, client.asBinder());
8865 }
Romain Guy06882f82009-06-10 13:36:04 -07008866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008867 final WindowState windowForClientLocked(Session session, IBinder client) {
8868 WindowState win = mWindowMap.get(client);
8869 if (localLOGV) Log.v(
8870 TAG, "Looking up client " + client + ": " + win);
8871 if (win == null) {
8872 RuntimeException ex = new RuntimeException();
8873 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8874 return null;
8875 }
8876 if (session != null && win.mSession != session) {
8877 RuntimeException ex = new RuntimeException();
8878 Log.w(TAG, "Requested window " + client + " is in session " +
8879 win.mSession + ", not " + session, ex);
8880 return null;
8881 }
8882
8883 return win;
8884 }
8885
Dianne Hackborna8f60182009-09-01 19:01:50 -07008886 final void rebuildAppWindowListLocked() {
8887 int NW = mWindows.size();
8888 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008889 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008890 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008891
8892 // First remove all existing app windows.
8893 i=0;
8894 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008895 WindowState w = (WindowState)mWindows.get(i);
8896 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008897 WindowState win = (WindowState)mWindows.remove(i);
8898 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8899 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008900 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008901 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008902 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008903 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
8904 && lastWallpaper == i-1) {
8905 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008906 }
8907 i++;
8908 }
8909
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008910 // The wallpaper window(s) typically live at the bottom of the stack,
8911 // so skip them before adding app tokens.
8912 lastWallpaper++;
8913 i = lastWallpaper;
8914
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008915 // First add all of the exiting app tokens... these are no longer
8916 // in the main app list, but still have windows shown. We put them
8917 // in the back because now that the animation is over we no longer
8918 // will care about them.
8919 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008920 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008921 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8922 }
8923
8924 // And add in the still active app tokens in Z order.
8925 NT = mAppTokens.size();
8926 for (int j=0; j<NT; j++) {
8927 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008928 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008929
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008930 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008931 if (i != numRemoved) {
8932 Log.w(TAG, "Rebuild removed " + numRemoved
8933 + " windows but added " + i);
8934 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07008935 }
8936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 private final void assignLayersLocked() {
8938 int N = mWindows.size();
8939 int curBaseLayer = 0;
8940 int curLayer = 0;
8941 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 for (i=0; i<N; i++) {
8944 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008945 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8946 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008947 curLayer += WINDOW_LAYER_MULTIPLIER;
8948 w.mLayer = curLayer;
8949 } else {
8950 curBaseLayer = curLayer = w.mBaseLayer;
8951 w.mLayer = curLayer;
8952 }
8953 if (w.mTargetAppToken != null) {
8954 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8955 } else if (w.mAppToken != null) {
8956 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8957 } else {
8958 w.mAnimLayer = w.mLayer;
8959 }
8960 if (w.mIsImWindow) {
8961 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008962 } else if (w.mIsWallpaper) {
8963 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 }
8965 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8966 + w.mAnimLayer);
8967 //System.out.println(
8968 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8969 }
8970 }
8971
8972 private boolean mInLayout = false;
8973 private final void performLayoutAndPlaceSurfacesLocked() {
8974 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008975 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008976 throw new RuntimeException("Recursive call!");
8977 }
8978 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8979 return;
8980 }
8981
8982 boolean recoveringMemory = false;
8983 if (mForceRemoves != null) {
8984 recoveringMemory = true;
8985 // Wait a little it for things to settle down, and off we go.
8986 for (int i=0; i<mForceRemoves.size(); i++) {
8987 WindowState ws = mForceRemoves.get(i);
8988 Log.i(TAG, "Force removing: " + ws);
8989 removeWindowInnerLocked(ws.mSession, ws);
8990 }
8991 mForceRemoves = null;
8992 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8993 Object tmp = new Object();
8994 synchronized (tmp) {
8995 try {
8996 tmp.wait(250);
8997 } catch (InterruptedException e) {
8998 }
8999 }
9000 }
Romain Guy06882f82009-06-10 13:36:04 -07009001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002 mInLayout = true;
9003 try {
9004 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 int i = mPendingRemove.size()-1;
9007 if (i >= 0) {
9008 while (i >= 0) {
9009 WindowState w = mPendingRemove.get(i);
9010 removeWindowInnerLocked(w.mSession, w);
9011 i--;
9012 }
9013 mPendingRemove.clear();
9014
9015 mInLayout = false;
9016 assignLayersLocked();
9017 mLayoutNeeded = true;
9018 performLayoutAndPlaceSurfacesLocked();
9019
9020 } else {
9021 mInLayout = false;
9022 if (mLayoutNeeded) {
9023 requestAnimationLocked(0);
9024 }
9025 }
9026 } catch (RuntimeException e) {
9027 mInLayout = false;
9028 Log.e(TAG, "Unhandled exception while layout out windows", e);
9029 }
9030 }
9031
9032 private final void performLayoutLockedInner() {
9033 final int dw = mDisplay.getWidth();
9034 final int dh = mDisplay.getHeight();
9035
9036 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009037 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038 int i;
9039
9040 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009041
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009042 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009043 mPolicy.beginLayoutLw(dw, dh);
9044
9045 // First perform layout of any root windows (not attached
9046 // to another window).
9047 int topAttached = -1;
9048 for (i = N-1; i >= 0; i--) {
9049 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009050
9051 // Don't do layout of a window if it is not visible, or
9052 // soon won't be visible, to avoid wasting time and funky
9053 // changes while a window is animating away.
9054 final AppWindowToken atoken = win.mAppToken;
9055 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009057 || win.mRootToken.hidden
9058 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009059 || win.mAttachedHidden
9060 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009061
9062 // If this view is GONE, then skip it -- keep the current
9063 // frame, and let the caller know so they can ignore it
9064 // if they want. (We do the normal layout for INVISIBLE
9065 // windows, since that means "perform layout as normal,
9066 // just don't display").
9067 if (!gone || !win.mHaveFrame) {
9068 if (!win.mLayoutAttached) {
9069 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9070 } else {
9071 if (topAttached < 0) topAttached = i;
9072 }
9073 }
9074 }
Romain Guy06882f82009-06-10 13:36:04 -07009075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009076 // Now perform layout of attached windows, which usually
9077 // depend on the position of the window they are attached to.
9078 // XXX does not deal with windows that are attached to windows
9079 // that are themselves attached.
9080 for (i = topAttached; i >= 0; i--) {
9081 WindowState win = (WindowState) mWindows.get(i);
9082
9083 // If this view is GONE, then skip it -- keep the current
9084 // frame, and let the caller know so they can ignore it
9085 // if they want. (We do the normal layout for INVISIBLE
9086 // windows, since that means "perform layout as normal,
9087 // just don't display").
9088 if (win.mLayoutAttached) {
9089 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9090 || !win.mHaveFrame) {
9091 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9092 }
9093 }
9094 }
9095
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009096 int changes = mPolicy.finishLayoutLw();
9097 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9098 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9099 assignLayersLocked();
9100 }
9101 }
9102 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009103 mLayoutNeeded = false;
9104 } else if (repeats > 2) {
9105 Log.w(TAG, "Layout repeat aborted after too many iterations");
9106 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009107 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9108 Configuration newConfig = updateOrientationFromAppTokensLocked(
9109 null, null);
9110 if (newConfig != null) {
9111 mLayoutNeeded = true;
9112 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9113 }
9114 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009115 } else {
9116 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009117 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9118 Configuration newConfig = updateOrientationFromAppTokensLocked(
9119 null, null);
9120 if (newConfig != null) {
9121 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9122 }
9123 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009125 }
9126 }
Romain Guy06882f82009-06-10 13:36:04 -07009127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009128 private final void performLayoutAndPlaceSurfacesLockedInner(
9129 boolean recoveringMemory) {
9130 final long currentTime = SystemClock.uptimeMillis();
9131 final int dw = mDisplay.getWidth();
9132 final int dh = mDisplay.getHeight();
9133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134 int i;
9135
9136 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009139 if (mFxSession == null) {
9140 mFxSession = new SurfaceSession();
9141 }
Romain Guy06882f82009-06-10 13:36:04 -07009142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009143 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9144
9145 // Initialize state of exiting tokens.
9146 for (i=mExitingTokens.size()-1; i>=0; i--) {
9147 mExitingTokens.get(i).hasVisible = false;
9148 }
9149
9150 // Initialize state of exiting applications.
9151 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9152 mExitingAppTokens.get(i).hasVisible = false;
9153 }
9154
9155 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009156 boolean orientationChangeComplete = true;
9157 Session holdScreen = null;
9158 float screenBrightness = -1;
9159 boolean focusDisplayed = false;
9160 boolean animating = false;
9161
9162 Surface.openTransaction();
9163 try {
9164 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009165 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009166
9167 do {
9168 final int transactionSequence = ++mTransactionSequence;
9169
9170 // Update animations of all applications, including those
9171 // associated with exiting/removed apps
9172 boolean tokensAnimating = false;
9173 final int NAT = mAppTokens.size();
9174 for (i=0; i<NAT; i++) {
9175 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9176 tokensAnimating = true;
9177 }
9178 }
9179 final int NEAT = mExitingAppTokens.size();
9180 for (i=0; i<NEAT; i++) {
9181 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9182 tokensAnimating = true;
9183 }
9184 }
9185
9186 animating = tokensAnimating;
9187 restart = false;
9188
9189 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009190 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009191 boolean focusMayChange = false;
9192 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193
9194 mPolicy.beginAnimationLw(dw, dh);
9195
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009196 final int N = mWindows.size();
9197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009198 for (i=N-1; i>=0; i--) {
9199 WindowState w = (WindowState)mWindows.get(i);
9200
9201 final WindowManager.LayoutParams attrs = w.mAttrs;
9202
9203 if (w.mSurface != null) {
9204 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009205 if (w.commitFinishDrawingLocked(currentTime)) {
9206 if ((w.mAttrs.flags
9207 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009208 if (DEBUG_WALLPAPER) Log.v(TAG,
9209 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009210 wallpaperMayChange = true;
9211 }
9212 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009213
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009214 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9216 animating = true;
9217 //w.dump(" ");
9218 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009219 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9220 wallpaperMayChange = true;
9221 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009222
9223 if (mPolicy.doesForceHide(w, attrs)) {
9224 if (!wasAnimating && animating) {
9225 wallpaperForceHidingChanged = true;
9226 focusMayChange = true;
9227 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9228 forceHiding = true;
9229 }
9230 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9231 boolean changed;
9232 if (forceHiding) {
9233 changed = w.hideLw(false, false);
9234 } else {
9235 changed = w.showLw(false, false);
9236 if (changed && wallpaperForceHidingChanged
9237 && w.isReadyForDisplay()) {
9238 // Assume we will need to animate. If
9239 // we don't (because the wallpaper will
9240 // stay with the lock screen), then we will
9241 // clean up later.
9242 Animation a = mPolicy.createForceHideEnterAnimation();
9243 if (a != null) {
9244 w.setAnimation(a);
9245 }
9246 }
9247 }
9248 if (changed && (attrs.flags
9249 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9250 wallpaperMayChange = true;
9251 }
9252 }
9253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009254 mPolicy.animatingWindowLw(w, attrs);
9255 }
9256
9257 final AppWindowToken atoken = w.mAppToken;
9258 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9259 if (atoken.lastTransactionSequence != transactionSequence) {
9260 atoken.lastTransactionSequence = transactionSequence;
9261 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9262 atoken.startingDisplayed = false;
9263 }
9264 if ((w.isOnScreen() || w.mAttrs.type
9265 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9266 && !w.mExiting && !w.mDestroying) {
9267 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009268 Log.v(TAG, "Eval win " + w + ": isDrawn="
9269 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009271 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009272 Log.v(TAG, "Not displayed: s=" + w.mSurface
9273 + " pv=" + w.mPolicyVisibility
9274 + " dp=" + w.mDrawPending
9275 + " cdp=" + w.mCommitDrawPending
9276 + " ah=" + w.mAttachedHidden
9277 + " th=" + atoken.hiddenRequested
9278 + " a=" + w.mAnimating);
9279 }
9280 }
9281 if (w != atoken.startingWindow) {
9282 if (!atoken.freezingScreen || !w.mAppFreezing) {
9283 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009284 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 atoken.numDrawnWindows++;
9286 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9287 "tokenMayBeDrawn: " + atoken
9288 + " freezingScreen=" + atoken.freezingScreen
9289 + " mAppFreezing=" + w.mAppFreezing);
9290 tokenMayBeDrawn = true;
9291 }
9292 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009293 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294 atoken.startingDisplayed = true;
9295 }
9296 }
9297 } else if (w.mReadyToShow) {
9298 w.performShowLocked();
9299 }
9300 }
9301
9302 if (mPolicy.finishAnimationLw()) {
9303 restart = true;
9304 }
9305
9306 if (tokenMayBeDrawn) {
9307 // See if any windows have been drawn, so they (and others
9308 // associated with them) can now be shown.
9309 final int NT = mTokenList.size();
9310 for (i=0; i<NT; i++) {
9311 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9312 if (wtoken == null) {
9313 continue;
9314 }
9315 if (wtoken.freezingScreen) {
9316 int numInteresting = wtoken.numInterestingWindows;
9317 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9318 if (DEBUG_VISIBILITY) Log.v(TAG,
9319 "allDrawn: " + wtoken
9320 + " interesting=" + numInteresting
9321 + " drawn=" + wtoken.numDrawnWindows);
9322 wtoken.showAllWindowsLocked();
9323 unsetAppFreezingScreenLocked(wtoken, false, true);
9324 orientationChangeComplete = true;
9325 }
9326 } else if (!wtoken.allDrawn) {
9327 int numInteresting = wtoken.numInterestingWindows;
9328 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9329 if (DEBUG_VISIBILITY) Log.v(TAG,
9330 "allDrawn: " + wtoken
9331 + " interesting=" + numInteresting
9332 + " drawn=" + wtoken.numDrawnWindows);
9333 wtoken.allDrawn = true;
9334 restart = true;
9335
9336 // We can now show all of the drawn windows!
9337 if (!mOpeningApps.contains(wtoken)) {
9338 wtoken.showAllWindowsLocked();
9339 }
9340 }
9341 }
9342 }
9343 }
9344
9345 // If we are ready to perform an app transition, check through
9346 // all of the app tokens to be shown and see if they are ready
9347 // to go.
9348 if (mAppTransitionReady) {
9349 int NN = mOpeningApps.size();
9350 boolean goodToGo = true;
9351 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9352 "Checking " + NN + " opening apps (frozen="
9353 + mDisplayFrozen + " timeout="
9354 + mAppTransitionTimeout + ")...");
9355 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9356 // If the display isn't frozen, wait to do anything until
9357 // all of the apps are ready. Otherwise just go because
9358 // we'll unfreeze the display when everyone is ready.
9359 for (i=0; i<NN && goodToGo; i++) {
9360 AppWindowToken wtoken = mOpeningApps.get(i);
9361 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9362 "Check opening app" + wtoken + ": allDrawn="
9363 + wtoken.allDrawn + " startingDisplayed="
9364 + wtoken.startingDisplayed);
9365 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9366 && !wtoken.startingMoved) {
9367 goodToGo = false;
9368 }
9369 }
9370 }
9371 if (goodToGo) {
9372 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9373 int transit = mNextAppTransition;
9374 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009375 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009377 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009378 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009379 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 mAppTransitionTimeout = false;
9381 mStartingIconInTransition = false;
9382 mSkipAppTransitionAnimation = false;
9383
9384 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9385
Dianne Hackborna8f60182009-09-01 19:01:50 -07009386 // If there are applications waiting to come to the
9387 // top of the stack, now is the time to move their windows.
9388 // (Note that we don't do apps going to the bottom
9389 // here -- we want to keep their windows in the old
9390 // Z-order until the animation completes.)
9391 if (mToTopApps.size() > 0) {
9392 NN = mAppTokens.size();
9393 for (i=0; i<NN; i++) {
9394 AppWindowToken wtoken = mAppTokens.get(i);
9395 if (wtoken.sendingToTop) {
9396 wtoken.sendingToTop = false;
9397 moveAppWindowsLocked(wtoken, NN, false);
9398 }
9399 }
9400 mToTopApps.clear();
9401 }
9402
Dianne Hackborn25994b42009-09-04 14:21:19 -07009403 WindowState oldWallpaper = mWallpaperTarget;
9404
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009405 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009406 wallpaperMayChange = false;
9407
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009408 // The top-most window will supply the layout params,
9409 // and we will determine it below.
9410 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009411 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009412 int bestAnimLayer = -1;
9413
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009414 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009415 "New wallpaper target=" + mWallpaperTarget
9416 + ", lower target=" + mLowerWallpaperTarget
9417 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009418 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009419 // Do a first pass through the tokens for two
9420 // things:
9421 // (1) Determine if both the closing and opening
9422 // app token sets are wallpaper targets, in which
9423 // case special animations are needed
9424 // (since the wallpaper needs to stay static
9425 // behind them).
9426 // (2) Find the layout params of the top-most
9427 // application window in the tokens, which is
9428 // what will control the animation theme.
9429 final int NC = mClosingApps.size();
9430 NN = NC + mOpeningApps.size();
9431 for (i=0; i<NN; i++) {
9432 AppWindowToken wtoken;
9433 int mode;
9434 if (i < NC) {
9435 wtoken = mClosingApps.get(i);
9436 mode = 1;
9437 } else {
9438 wtoken = mOpeningApps.get(i-NC);
9439 mode = 2;
9440 }
9441 if (mLowerWallpaperTarget != null) {
9442 if (mLowerWallpaperTarget.mAppToken == wtoken
9443 || mUpperWallpaperTarget.mAppToken == wtoken) {
9444 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009445 }
9446 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009447 if (wtoken.appFullscreen) {
9448 WindowState ws = wtoken.findMainWindow();
9449 if (ws != null) {
9450 // If this is a compatibility mode
9451 // window, we will always use its anim.
9452 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9453 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009454 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009455 bestAnimLayer = Integer.MAX_VALUE;
9456 } else if (ws.mLayer > bestAnimLayer) {
9457 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009458 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009459 bestAnimLayer = ws.mLayer;
9460 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009461 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009462 }
9463 }
9464
Dianne Hackborn25994b42009-09-04 14:21:19 -07009465 if (foundWallpapers == 3) {
9466 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9467 "Wallpaper animation!");
9468 switch (transit) {
9469 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9470 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9471 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9472 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9473 break;
9474 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9475 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9476 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9477 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9478 break;
9479 }
9480 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9481 "New transit: " + transit);
9482 } else if (oldWallpaper != null) {
9483 // We are transitioning from an activity with
9484 // a wallpaper to one without.
9485 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9486 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9487 "New transit away from wallpaper: " + transit);
9488 } else if (mWallpaperTarget != null) {
9489 // We are transitioning from an activity without
9490 // a wallpaper to now showing the wallpaper
9491 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9492 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9493 "New transit into wallpaper: " + transit);
9494 }
9495
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009496 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9497 mLastEnterAnimToken = animToken;
9498 mLastEnterAnimParams = animLp;
9499 } else if (mLastEnterAnimParams != null) {
9500 animLp = mLastEnterAnimParams;
9501 mLastEnterAnimToken = null;
9502 mLastEnterAnimParams = null;
9503 }
9504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009505 NN = mOpeningApps.size();
9506 for (i=0; i<NN; i++) {
9507 AppWindowToken wtoken = mOpeningApps.get(i);
9508 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9509 "Now opening app" + wtoken);
9510 wtoken.reportedVisible = false;
9511 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009512 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009513 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009514 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009515 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 wtoken.showAllWindowsLocked();
9517 }
9518 NN = mClosingApps.size();
9519 for (i=0; i<NN; i++) {
9520 AppWindowToken wtoken = mClosingApps.get(i);
9521 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9522 "Now closing app" + wtoken);
9523 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009524 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009525 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009526 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009527 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009528 // Force the allDrawn flag, because we want to start
9529 // this guy's animations regardless of whether it's
9530 // gotten drawn.
9531 wtoken.allDrawn = true;
9532 }
9533
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009534 mNextAppTransitionPackage = null;
9535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009536 mOpeningApps.clear();
9537 mClosingApps.clear();
9538
9539 // This has changed the visibility of windows, so perform
9540 // a new layout to get them all up-to-date.
9541 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009542 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9543 assignLayersLocked();
9544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 performLayoutLockedInner();
9546 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009547 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009548
9549 restart = true;
9550 }
9551 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009552
Dianne Hackborna8f60182009-09-01 19:01:50 -07009553 if (!animating && mAppTransitionRunning) {
9554 // We have finished the animation of an app transition. To do
9555 // this, we have delayed a lot of operations like showing and
9556 // hiding apps, moving apps in Z-order, etc. The app token list
9557 // reflects the correct Z-order, but the window list may now
9558 // be out of sync with it. So here we will just rebuild the
9559 // entire app window list. Fun!
9560 mAppTransitionRunning = false;
9561 // Clear information about apps that were moving.
9562 mToBottomApps.clear();
9563
9564 rebuildAppWindowListLocked();
9565 restart = true;
9566 moveInputMethodWindowsIfNeededLocked(false);
9567 wallpaperMayChange = true;
9568 mLayoutNeeded = true;
9569 }
9570
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009571 int adjResult = 0;
9572
9573 if (wallpaperForceHidingChanged) {
9574 // At this point, there was a window with a wallpaper that
9575 // was force hiding other windows behind it, but now it
9576 // is going away. This may be simple -- just animate
9577 // away the wallpaper and its window -- or it may be
9578 // hard -- the wallpaper now needs to be shown behind
9579 // something that was hidden.
9580 WindowState oldWallpaper = mWallpaperTarget;
9581 adjResult = adjustWallpaperWindowsLocked();
9582 wallpaperMayChange = false;
9583 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9584 + " NEW: " + mWallpaperTarget);
9585 if (mLowerWallpaperTarget == null) {
9586 // Whoops, we don't need a special wallpaper animation.
9587 // Clear them out.
9588 forceHiding = false;
9589 for (i=N-1; i>=0; i--) {
9590 WindowState w = (WindowState)mWindows.get(i);
9591 if (w.mSurface != null) {
9592 final WindowManager.LayoutParams attrs = w.mAttrs;
9593 if (mPolicy.doesForceHide(w, attrs)) {
9594 forceHiding = true;
9595 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9596 if (!w.mAnimating) {
9597 // We set the animation above so it
9598 // is not yet running.
9599 w.clearAnimation();
9600 }
9601 }
9602 }
9603 }
9604 }
9605 }
9606
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009607 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009608 if (DEBUG_WALLPAPER) Log.v(TAG,
9609 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009610 adjResult = adjustWallpaperWindowsLocked();
9611 }
9612
9613 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9614 if (DEBUG_WALLPAPER) Log.v(TAG,
9615 "Wallpaper layer changed: assigning layers + relayout");
9616 restart = true;
9617 mLayoutNeeded = true;
9618 assignLayersLocked();
9619 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9620 if (DEBUG_WALLPAPER) Log.v(TAG,
9621 "Wallpaper visibility changed: relayout");
9622 restart = true;
9623 mLayoutNeeded = true;
9624 }
9625
9626 if (focusMayChange) {
9627 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009628 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009629 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009630 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009631 }
9632
9633 if (mLayoutNeeded) {
9634 restart = true;
9635 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009636 }
9637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009638 } while (restart);
9639
9640 // THIRD LOOP: Update the surfaces of all windows.
9641
9642 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9643
9644 boolean obscured = false;
9645 boolean blurring = false;
9646 boolean dimming = false;
9647 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009648 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009649 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009651 final int N = mWindows.size();
9652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009653 for (i=N-1; i>=0; i--) {
9654 WindowState w = (WindowState)mWindows.get(i);
9655
9656 boolean displayed = false;
9657 final WindowManager.LayoutParams attrs = w.mAttrs;
9658 final int attrFlags = attrs.flags;
9659
9660 if (w.mSurface != null) {
9661 w.computeShownFrameLocked();
9662 if (localLOGV) Log.v(
9663 TAG, "Placing surface #" + i + " " + w.mSurface
9664 + ": new=" + w.mShownFrame + ", old="
9665 + w.mLastShownFrame);
9666
9667 boolean resize;
9668 int width, height;
9669 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9670 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9671 w.mLastRequestedHeight != w.mRequestedHeight;
9672 // for a scaled surface, we just want to use
9673 // the requested size.
9674 width = w.mRequestedWidth;
9675 height = w.mRequestedHeight;
9676 w.mLastRequestedWidth = width;
9677 w.mLastRequestedHeight = height;
9678 w.mLastShownFrame.set(w.mShownFrame);
9679 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009680 if (SHOW_TRANSACTIONS) Log.i(
9681 TAG, " SURFACE " + w.mSurface
9682 + ": POS " + w.mShownFrame.left
9683 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009684 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9685 } catch (RuntimeException e) {
9686 Log.w(TAG, "Error positioning surface in " + w, e);
9687 if (!recoveringMemory) {
9688 reclaimSomeSurfaceMemoryLocked(w, "position");
9689 }
9690 }
9691 } else {
9692 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9693 width = w.mShownFrame.width();
9694 height = w.mShownFrame.height();
9695 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 }
9697
9698 if (resize) {
9699 if (width < 1) width = 1;
9700 if (height < 1) height = 1;
9701 if (w.mSurface != null) {
9702 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009703 if (SHOW_TRANSACTIONS) Log.i(
9704 TAG, " SURFACE " + w.mSurface + ": POS "
9705 + w.mShownFrame.left + ","
9706 + w.mShownFrame.top + " SIZE "
9707 + w.mShownFrame.width() + "x"
9708 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709 w.mSurface.setSize(width, height);
9710 w.mSurface.setPosition(w.mShownFrame.left,
9711 w.mShownFrame.top);
9712 } catch (RuntimeException e) {
9713 // If something goes wrong with the surface (such
9714 // as running out of memory), don't take down the
9715 // entire system.
9716 Log.e(TAG, "Failure updating surface of " + w
9717 + "size=(" + width + "x" + height
9718 + "), pos=(" + w.mShownFrame.left
9719 + "," + w.mShownFrame.top + ")", e);
9720 if (!recoveringMemory) {
9721 reclaimSomeSurfaceMemoryLocked(w, "size");
9722 }
9723 }
9724 }
9725 }
9726 if (!w.mAppFreezing) {
9727 w.mContentInsetsChanged =
9728 !w.mLastContentInsets.equals(w.mContentInsets);
9729 w.mVisibleInsetsChanged =
9730 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009731 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 || w.mContentInsetsChanged
9733 || w.mVisibleInsetsChanged) {
9734 w.mLastFrame.set(w.mFrame);
9735 w.mLastContentInsets.set(w.mContentInsets);
9736 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009737 // If the screen is currently frozen, then keep
9738 // it frozen until this window draws at its new
9739 // orientation.
9740 if (mDisplayFrozen) {
9741 if (DEBUG_ORIENTATION) Log.v(TAG,
9742 "Resizing while display frozen: " + w);
9743 w.mOrientationChanging = true;
9744 if (mWindowsFreezingScreen) {
9745 mWindowsFreezingScreen = true;
9746 // XXX should probably keep timeout from
9747 // when we first froze the display.
9748 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9749 mH.sendMessageDelayed(mH.obtainMessage(
9750 H.WINDOW_FREEZE_TIMEOUT), 2000);
9751 }
9752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009753 // If the orientation is changing, then we need to
9754 // hold off on unfreezing the display until this
9755 // window has been redrawn; to do that, we need
9756 // to go through the process of getting informed
9757 // by the application when it has finished drawing.
9758 if (w.mOrientationChanging) {
9759 if (DEBUG_ORIENTATION) Log.v(TAG,
9760 "Orientation start waiting for draw in "
9761 + w + ", surface " + w.mSurface);
9762 w.mDrawPending = true;
9763 w.mCommitDrawPending = false;
9764 w.mReadyToShow = false;
9765 if (w.mAppToken != null) {
9766 w.mAppToken.allDrawn = false;
9767 }
9768 }
Romain Guy06882f82009-06-10 13:36:04 -07009769 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 "Resizing window " + w + " to " + w.mFrame);
9771 mResizingWindows.add(w);
9772 } else if (w.mOrientationChanging) {
9773 if (!w.mDrawPending && !w.mCommitDrawPending) {
9774 if (DEBUG_ORIENTATION) Log.v(TAG,
9775 "Orientation not waiting for draw in "
9776 + w + ", surface " + w.mSurface);
9777 w.mOrientationChanging = false;
9778 }
9779 }
9780 }
9781
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009782 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 if (!w.mLastHidden) {
9784 //dump();
9785 w.mLastHidden = true;
9786 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009787 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009788 if (w.mSurface != null) {
9789 try {
9790 w.mSurface.hide();
9791 } catch (RuntimeException e) {
9792 Log.w(TAG, "Exception hiding surface in " + w);
9793 }
9794 }
9795 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9796 }
9797 // If we are waiting for this window to handle an
9798 // orientation change, well, it is hidden, so
9799 // doesn't really matter. Note that this does
9800 // introduce a potential glitch if the window
9801 // becomes unhidden before it has drawn for the
9802 // new orientation.
9803 if (w.mOrientationChanging) {
9804 w.mOrientationChanging = false;
9805 if (DEBUG_ORIENTATION) Log.v(TAG,
9806 "Orientation change skips hidden " + w);
9807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 } else if (w.mLastLayer != w.mAnimLayer
9809 || w.mLastAlpha != w.mShownAlpha
9810 || w.mLastDsDx != w.mDsDx
9811 || w.mLastDtDx != w.mDtDx
9812 || w.mLastDsDy != w.mDsDy
9813 || w.mLastDtDy != w.mDtDy
9814 || w.mLastHScale != w.mHScale
9815 || w.mLastVScale != w.mVScale
9816 || w.mLastHidden) {
9817 displayed = true;
9818 w.mLastAlpha = w.mShownAlpha;
9819 w.mLastLayer = w.mAnimLayer;
9820 w.mLastDsDx = w.mDsDx;
9821 w.mLastDtDx = w.mDtDx;
9822 w.mLastDsDy = w.mDsDy;
9823 w.mLastDtDy = w.mDtDy;
9824 w.mLastHScale = w.mHScale;
9825 w.mLastVScale = w.mVScale;
9826 if (SHOW_TRANSACTIONS) Log.i(
9827 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009828 + w.mShownAlpha + " layer=" + w.mAnimLayer
9829 + " matrix=[" + (w.mDsDx*w.mHScale)
9830 + "," + (w.mDtDx*w.mVScale)
9831 + "][" + (w.mDsDy*w.mHScale)
9832 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 if (w.mSurface != null) {
9834 try {
9835 w.mSurface.setAlpha(w.mShownAlpha);
9836 w.mSurface.setLayer(w.mAnimLayer);
9837 w.mSurface.setMatrix(
9838 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9839 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9840 } catch (RuntimeException e) {
9841 Log.w(TAG, "Error updating surface in " + w, e);
9842 if (!recoveringMemory) {
9843 reclaimSomeSurfaceMemoryLocked(w, "update");
9844 }
9845 }
9846 }
9847
9848 if (w.mLastHidden && !w.mDrawPending
9849 && !w.mCommitDrawPending
9850 && !w.mReadyToShow) {
9851 if (SHOW_TRANSACTIONS) Log.i(
9852 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9853 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9854 + " during relayout");
9855 if (showSurfaceRobustlyLocked(w)) {
9856 w.mHasDrawn = true;
9857 w.mLastHidden = false;
9858 } else {
9859 w.mOrientationChanging = false;
9860 }
9861 }
9862 if (w.mSurface != null) {
9863 w.mToken.hasVisible = true;
9864 }
9865 } else {
9866 displayed = true;
9867 }
9868
9869 if (displayed) {
9870 if (!covered) {
9871 if (attrs.width == LayoutParams.FILL_PARENT
9872 && attrs.height == LayoutParams.FILL_PARENT) {
9873 covered = true;
9874 }
9875 }
9876 if (w.mOrientationChanging) {
9877 if (w.mDrawPending || w.mCommitDrawPending) {
9878 orientationChangeComplete = false;
9879 if (DEBUG_ORIENTATION) Log.v(TAG,
9880 "Orientation continue waiting for draw in " + w);
9881 } else {
9882 w.mOrientationChanging = false;
9883 if (DEBUG_ORIENTATION) Log.v(TAG,
9884 "Orientation change complete in " + w);
9885 }
9886 }
9887 w.mToken.hasVisible = true;
9888 }
9889 } else if (w.mOrientationChanging) {
9890 if (DEBUG_ORIENTATION) Log.v(TAG,
9891 "Orientation change skips hidden " + w);
9892 w.mOrientationChanging = false;
9893 }
9894
9895 final boolean canBeSeen = w.isDisplayedLw();
9896
9897 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9898 focusDisplayed = true;
9899 }
9900
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009901 final boolean obscuredChanged = w.mObscured != obscured;
9902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009904 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009905 if (w.mSurface != null) {
9906 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9907 holdScreen = w.mSession;
9908 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009909 if (!syswin && w.mAttrs.screenBrightness >= 0
9910 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 screenBrightness = w.mAttrs.screenBrightness;
9912 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009913 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9914 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9915 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9916 syswin = true;
9917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009918 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009919
Dianne Hackborn25994b42009-09-04 14:21:19 -07009920 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9921 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 // This window completely covers everything behind it,
9923 // so we want to leave all of them as unblurred (for
9924 // performance reasons).
9925 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009926 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9927 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009928 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009929 obscured = true;
9930 if (mBackgroundFillerSurface == null) {
9931 try {
9932 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9933 0, dw, dh,
9934 PixelFormat.OPAQUE,
9935 Surface.FX_SURFACE_NORMAL);
9936 } catch (Exception e) {
9937 Log.e(TAG, "Exception creating filler surface", e);
9938 }
9939 }
9940 try {
9941 mBackgroundFillerSurface.setPosition(0, 0);
9942 mBackgroundFillerSurface.setSize(dw, dh);
9943 // Using the same layer as Dim because they will never be shown at the
9944 // same time.
9945 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9946 mBackgroundFillerSurface.show();
9947 } catch (RuntimeException e) {
9948 Log.e(TAG, "Exception showing filler surface");
9949 }
9950 backgroundFillerShown = true;
9951 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009952 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9954 if (localLOGV) Log.v(TAG, "Win " + w
9955 + ": blurring=" + blurring
9956 + " obscured=" + obscured
9957 + " displayed=" + displayed);
9958 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9959 if (!dimming) {
9960 //Log.i(TAG, "DIM BEHIND: " + w);
9961 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009962 if (mDimAnimator == null) {
9963 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009965 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009966 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009967 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 }
9969 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9970 if (!blurring) {
9971 //Log.i(TAG, "BLUR BEHIND: " + w);
9972 blurring = true;
9973 mBlurShown = true;
9974 if (mBlurSurface == null) {
9975 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9976 + mBlurSurface + ": CREATE");
9977 try {
Romain Guy06882f82009-06-10 13:36:04 -07009978 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 -1, 16, 16,
9980 PixelFormat.OPAQUE,
9981 Surface.FX_SURFACE_BLUR);
9982 } catch (Exception e) {
9983 Log.e(TAG, "Exception creating Blur surface", e);
9984 }
9985 }
9986 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9987 + mBlurSurface + ": SHOW pos=(0,0) (" +
9988 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9989 if (mBlurSurface != null) {
9990 mBlurSurface.setPosition(0, 0);
9991 mBlurSurface.setSize(dw, dh);
9992 try {
9993 mBlurSurface.show();
9994 } catch (RuntimeException e) {
9995 Log.w(TAG, "Failure showing blur surface", e);
9996 }
9997 }
9998 }
9999 mBlurSurface.setLayer(w.mAnimLayer-2);
10000 }
10001 }
10002 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010003
10004 if (obscuredChanged && mWallpaperTarget == w) {
10005 // This is the wallpaper target and its obscured state
10006 // changed... make sure the current wallaper's visibility
10007 // has been updated accordingly.
10008 updateWallpaperVisibilityLocked();
10009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010011
10012 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10013 mBackgroundFillerShown = false;
10014 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10015 try {
10016 mBackgroundFillerSurface.hide();
10017 } catch (RuntimeException e) {
10018 Log.e(TAG, "Exception hiding filler surface", e);
10019 }
10020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010022 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10023 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 }
Romain Guy06882f82009-06-10 13:36:04 -070010025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 if (!blurring && mBlurShown) {
10027 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10028 + ": HIDE");
10029 try {
10030 mBlurSurface.hide();
10031 } catch (IllegalArgumentException e) {
10032 Log.w(TAG, "Illegal argument exception hiding blur surface");
10033 }
10034 mBlurShown = false;
10035 }
10036
10037 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10038 } catch (RuntimeException e) {
10039 Log.e(TAG, "Unhandled exception in Window Manager", e);
10040 }
10041
10042 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010044 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10045 "With display frozen, orientationChangeComplete="
10046 + orientationChangeComplete);
10047 if (orientationChangeComplete) {
10048 if (mWindowsFreezingScreen) {
10049 mWindowsFreezingScreen = false;
10050 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10051 }
10052 if (mAppsFreezingScreen == 0) {
10053 stopFreezingDisplayLocked();
10054 }
10055 }
Romain Guy06882f82009-06-10 13:36:04 -070010056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010057 i = mResizingWindows.size();
10058 if (i > 0) {
10059 do {
10060 i--;
10061 WindowState win = mResizingWindows.get(i);
10062 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010063 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10064 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 win.mClient.resized(win.mFrame.width(),
10066 win.mFrame.height(), win.mLastContentInsets,
10067 win.mLastVisibleInsets, win.mDrawPending);
10068 win.mContentInsetsChanged = false;
10069 win.mVisibleInsetsChanged = false;
10070 } catch (RemoteException e) {
10071 win.mOrientationChanging = false;
10072 }
10073 } while (i > 0);
10074 mResizingWindows.clear();
10075 }
Romain Guy06882f82009-06-10 13:36:04 -070010076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010077 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010078 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010079 i = mDestroySurface.size();
10080 if (i > 0) {
10081 do {
10082 i--;
10083 WindowState win = mDestroySurface.get(i);
10084 win.mDestroying = false;
10085 if (mInputMethodWindow == win) {
10086 mInputMethodWindow = null;
10087 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010088 if (win == mWallpaperTarget) {
10089 wallpaperDestroyed = true;
10090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010091 win.destroySurfaceLocked();
10092 } while (i > 0);
10093 mDestroySurface.clear();
10094 }
10095
10096 // Time to remove any exiting tokens?
10097 for (i=mExitingTokens.size()-1; i>=0; i--) {
10098 WindowToken token = mExitingTokens.get(i);
10099 if (!token.hasVisible) {
10100 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010101 if (token.windowType == TYPE_WALLPAPER) {
10102 mWallpaperTokens.remove(token);
10103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 }
10105 }
10106
10107 // Time to remove any exiting applications?
10108 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10109 AppWindowToken token = mExitingAppTokens.get(i);
10110 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010111 // Make sure there is no animation running on this token,
10112 // so any windows associated with it will be removed as
10113 // soon as their animations are complete
10114 token.animation = null;
10115 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010116 mAppTokens.remove(token);
10117 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010118 if (mLastEnterAnimToken == token) {
10119 mLastEnterAnimToken = null;
10120 mLastEnterAnimParams = null;
10121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 }
10123 }
10124
Dianne Hackborna8f60182009-09-01 19:01:50 -070010125 boolean needRelayout = false;
10126
10127 if (!animating && mAppTransitionRunning) {
10128 // We have finished the animation of an app transition. To do
10129 // this, we have delayed a lot of operations like showing and
10130 // hiding apps, moving apps in Z-order, etc. The app token list
10131 // reflects the correct Z-order, but the window list may now
10132 // be out of sync with it. So here we will just rebuild the
10133 // entire app window list. Fun!
10134 mAppTransitionRunning = false;
10135 needRelayout = true;
10136 rebuildAppWindowListLocked();
10137 // Clear information about apps that were moving.
10138 mToBottomApps.clear();
10139 }
10140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010141 if (focusDisplayed) {
10142 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10143 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010144 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010145 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010146 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010147 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010148 requestAnimationLocked(0);
10149 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10151 }
10152 mQueue.setHoldScreenLocked(holdScreen != null);
10153 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10154 mPowerManager.setScreenBrightnessOverride(-1);
10155 } else {
10156 mPowerManager.setScreenBrightnessOverride((int)
10157 (screenBrightness * Power.BRIGHTNESS_ON));
10158 }
10159 if (holdScreen != mHoldingScreenOn) {
10160 mHoldingScreenOn = holdScreen;
10161 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10162 mH.sendMessage(m);
10163 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010164
10165 if (mTurnOnScreen) {
10166 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10167 LocalPowerManager.BUTTON_EVENT, true);
10168 mTurnOnScreen = false;
10169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 }
10171
10172 void requestAnimationLocked(long delay) {
10173 if (!mAnimationPending) {
10174 mAnimationPending = true;
10175 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10176 }
10177 }
Romain Guy06882f82009-06-10 13:36:04 -070010178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010179 /**
10180 * Have the surface flinger show a surface, robustly dealing with
10181 * error conditions. In particular, if there is not enough memory
10182 * to show the surface, then we will try to get rid of other surfaces
10183 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 * @return Returns true if the surface was successfully shown.
10186 */
10187 boolean showSurfaceRobustlyLocked(WindowState win) {
10188 try {
10189 if (win.mSurface != null) {
10190 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010191 if (win.mTurnOnScreen) {
10192 win.mTurnOnScreen = false;
10193 mTurnOnScreen = true;
10194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 }
10196 return true;
10197 } catch (RuntimeException e) {
10198 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10199 }
Romain Guy06882f82009-06-10 13:36:04 -070010200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010201 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 return false;
10204 }
Romain Guy06882f82009-06-10 13:36:04 -070010205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010206 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10207 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010209 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10210 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 if (mForceRemoves == null) {
10213 mForceRemoves = new ArrayList<WindowState>();
10214 }
Romain Guy06882f82009-06-10 13:36:04 -070010215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 long callingIdentity = Binder.clearCallingIdentity();
10217 try {
10218 // There was some problem... first, do a sanity check of the
10219 // window list to make sure we haven't left any dangling surfaces
10220 // around.
10221 int N = mWindows.size();
10222 boolean leakedSurface = false;
10223 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10224 for (int i=0; i<N; i++) {
10225 WindowState ws = (WindowState)mWindows.get(i);
10226 if (ws.mSurface != null) {
10227 if (!mSessions.contains(ws.mSession)) {
10228 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10229 + ws + " surface=" + ws.mSurface
10230 + " token=" + win.mToken
10231 + " pid=" + ws.mSession.mPid
10232 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010233 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 ws.mSurface = null;
10235 mForceRemoves.add(ws);
10236 i--;
10237 N--;
10238 leakedSurface = true;
10239 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10240 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10241 + ws + " surface=" + ws.mSurface
10242 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010243 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010244 ws.mSurface = null;
10245 leakedSurface = true;
10246 }
10247 }
10248 }
Romain Guy06882f82009-06-10 13:36:04 -070010249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 boolean killedApps = false;
10251 if (!leakedSurface) {
10252 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10253 SparseIntArray pidCandidates = new SparseIntArray();
10254 for (int i=0; i<N; i++) {
10255 WindowState ws = (WindowState)mWindows.get(i);
10256 if (ws.mSurface != null) {
10257 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10258 }
10259 }
10260 if (pidCandidates.size() > 0) {
10261 int[] pids = new int[pidCandidates.size()];
10262 for (int i=0; i<pids.length; i++) {
10263 pids[i] = pidCandidates.keyAt(i);
10264 }
10265 try {
10266 if (mActivityManager.killPidsForMemory(pids)) {
10267 killedApps = true;
10268 }
10269 } catch (RemoteException e) {
10270 }
10271 }
10272 }
Romain Guy06882f82009-06-10 13:36:04 -070010273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010274 if (leakedSurface || killedApps) {
10275 // We managed to reclaim some memory, so get rid of the trouble
10276 // surface and ask the app to request another one.
10277 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10278 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010279 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 win.mSurface = null;
10281 }
Romain Guy06882f82009-06-10 13:36:04 -070010282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283 try {
10284 win.mClient.dispatchGetNewSurface();
10285 } catch (RemoteException e) {
10286 }
10287 }
10288 } finally {
10289 Binder.restoreCallingIdentity(callingIdentity);
10290 }
10291 }
Romain Guy06882f82009-06-10 13:36:04 -070010292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010293 private boolean updateFocusedWindowLocked(int mode) {
10294 WindowState newFocus = computeFocusedWindowLocked();
10295 if (mCurrentFocus != newFocus) {
10296 // This check makes sure that we don't already have the focus
10297 // change message pending.
10298 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10299 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10300 if (localLOGV) Log.v(
10301 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10302 final WindowState oldFocus = mCurrentFocus;
10303 mCurrentFocus = newFocus;
10304 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 final WindowState imWindow = mInputMethodWindow;
10307 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010308 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010310 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10311 mLayoutNeeded = true;
10312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10314 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010315 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10316 // Client will do the layout, but we need to assign layers
10317 // for handleNewWindowLocked() below.
10318 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 }
10320 }
Romain Guy06882f82009-06-10 13:36:04 -070010321
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010322 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10323 mKeyWaiter.handleNewWindowLocked(newFocus);
10324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010325 return true;
10326 }
10327 return false;
10328 }
10329
10330 private WindowState computeFocusedWindowLocked() {
10331 WindowState result = null;
10332 WindowState win;
10333
10334 int i = mWindows.size() - 1;
10335 int nextAppIndex = mAppTokens.size()-1;
10336 WindowToken nextApp = nextAppIndex >= 0
10337 ? mAppTokens.get(nextAppIndex) : null;
10338
10339 while (i >= 0) {
10340 win = (WindowState)mWindows.get(i);
10341
10342 if (localLOGV || DEBUG_FOCUS) Log.v(
10343 TAG, "Looking for focus: " + i
10344 + " = " + win
10345 + ", flags=" + win.mAttrs.flags
10346 + ", canReceive=" + win.canReceiveKeys());
10347
10348 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 // If this window's application has been removed, just skip it.
10351 if (thisApp != null && thisApp.removed) {
10352 i--;
10353 continue;
10354 }
Romain Guy06882f82009-06-10 13:36:04 -070010355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 // If there is a focused app, don't allow focus to go to any
10357 // windows below it. If this is an application window, step
10358 // through the app tokens until we find its app.
10359 if (thisApp != null && nextApp != null && thisApp != nextApp
10360 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10361 int origAppIndex = nextAppIndex;
10362 while (nextAppIndex > 0) {
10363 if (nextApp == mFocusedApp) {
10364 // Whoops, we are below the focused app... no focus
10365 // for you!
10366 if (localLOGV || DEBUG_FOCUS) Log.v(
10367 TAG, "Reached focused app: " + mFocusedApp);
10368 return null;
10369 }
10370 nextAppIndex--;
10371 nextApp = mAppTokens.get(nextAppIndex);
10372 if (nextApp == thisApp) {
10373 break;
10374 }
10375 }
10376 if (thisApp != nextApp) {
10377 // Uh oh, the app token doesn't exist! This shouldn't
10378 // happen, but if it does we can get totally hosed...
10379 // so restart at the original app.
10380 nextAppIndex = origAppIndex;
10381 nextApp = mAppTokens.get(nextAppIndex);
10382 }
10383 }
10384
10385 // Dispatch to this window if it is wants key events.
10386 if (win.canReceiveKeys()) {
10387 if (DEBUG_FOCUS) Log.v(
10388 TAG, "Found focus @ " + i + " = " + win);
10389 result = win;
10390 break;
10391 }
10392
10393 i--;
10394 }
10395
10396 return result;
10397 }
10398
10399 private void startFreezingDisplayLocked() {
10400 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010401 // Freezing the display also suspends key event delivery, to
10402 // keep events from going astray while the display is reconfigured.
10403 // If someone has changed orientation again while the screen is
10404 // still frozen, the events will continue to be blocked while the
10405 // successive orientation change is processed. To prevent spurious
10406 // ANRs, we reset the event dispatch timeout in this case.
10407 synchronized (mKeyWaiter) {
10408 mKeyWaiter.mWasFrozen = true;
10409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 return;
10411 }
Romain Guy06882f82009-06-10 13:36:04 -070010412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010415 long now = SystemClock.uptimeMillis();
10416 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10417 if (mFreezeGcPending != 0) {
10418 if (now > (mFreezeGcPending+1000)) {
10419 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10420 mH.removeMessages(H.FORCE_GC);
10421 Runtime.getRuntime().gc();
10422 mFreezeGcPending = now;
10423 }
10424 } else {
10425 mFreezeGcPending = now;
10426 }
Romain Guy06882f82009-06-10 13:36:04 -070010427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010429 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10430 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010431 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 mAppTransitionReady = true;
10433 }
Romain Guy06882f82009-06-10 13:36:04 -070010434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435 if (PROFILE_ORIENTATION) {
10436 File file = new File("/data/system/frozen");
10437 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10438 }
10439 Surface.freezeDisplay(0);
10440 }
Romain Guy06882f82009-06-10 13:36:04 -070010441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 private void stopFreezingDisplayLocked() {
10443 if (!mDisplayFrozen) {
10444 return;
10445 }
Romain Guy06882f82009-06-10 13:36:04 -070010446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 mDisplayFrozen = false;
10448 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10449 if (PROFILE_ORIENTATION) {
10450 Debug.stopMethodTracing();
10451 }
10452 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010453
Chris Tate2ad63a92009-03-25 17:36:48 -070010454 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10455 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456 synchronized (mKeyWaiter) {
10457 mKeyWaiter.mWasFrozen = true;
10458 mKeyWaiter.notifyAll();
10459 }
10460
10461 // A little kludge: a lot could have happened while the
10462 // display was frozen, so now that we are coming back we
10463 // do a gc so that any remote references the system
10464 // processes holds on others can be released if they are
10465 // no longer needed.
10466 mH.removeMessages(H.FORCE_GC);
10467 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10468 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470 mScreenFrozenLock.release();
10471 }
Romain Guy06882f82009-06-10 13:36:04 -070010472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 @Override
10474 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10475 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10476 != PackageManager.PERMISSION_GRANTED) {
10477 pw.println("Permission Denial: can't dump WindowManager from from pid="
10478 + Binder.getCallingPid()
10479 + ", uid=" + Binder.getCallingUid());
10480 return;
10481 }
Romain Guy06882f82009-06-10 13:36:04 -070010482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010483 synchronized(mWindowMap) {
10484 pw.println("Current Window Manager state:");
10485 for (int i=mWindows.size()-1; i>=0; i--) {
10486 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010487 pw.print(" Window #"); pw.print(i); pw.print(' ');
10488 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 w.dump(pw, " ");
10490 }
10491 if (mInputMethodDialogs.size() > 0) {
10492 pw.println(" ");
10493 pw.println(" Input method dialogs:");
10494 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10495 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010496 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 }
10498 }
10499 if (mPendingRemove.size() > 0) {
10500 pw.println(" ");
10501 pw.println(" Remove pending for:");
10502 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10503 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010504 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10505 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 w.dump(pw, " ");
10507 }
10508 }
10509 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10510 pw.println(" ");
10511 pw.println(" Windows force removing:");
10512 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10513 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010514 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10515 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 w.dump(pw, " ");
10517 }
10518 }
10519 if (mDestroySurface.size() > 0) {
10520 pw.println(" ");
10521 pw.println(" Windows waiting to destroy their surface:");
10522 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10523 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010524 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10525 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 w.dump(pw, " ");
10527 }
10528 }
10529 if (mLosingFocus.size() > 0) {
10530 pw.println(" ");
10531 pw.println(" Windows losing focus:");
10532 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10533 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010534 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10535 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 w.dump(pw, " ");
10537 }
10538 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010539 if (mResizingWindows.size() > 0) {
10540 pw.println(" ");
10541 pw.println(" Windows waiting to resize:");
10542 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10543 WindowState w = mResizingWindows.get(i);
10544 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10545 pw.print(w); pw.println(":");
10546 w.dump(pw, " ");
10547 }
10548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010549 if (mSessions.size() > 0) {
10550 pw.println(" ");
10551 pw.println(" All active sessions:");
10552 Iterator<Session> it = mSessions.iterator();
10553 while (it.hasNext()) {
10554 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010555 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 s.dump(pw, " ");
10557 }
10558 }
10559 if (mTokenMap.size() > 0) {
10560 pw.println(" ");
10561 pw.println(" All tokens:");
10562 Iterator<WindowToken> it = mTokenMap.values().iterator();
10563 while (it.hasNext()) {
10564 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010565 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 token.dump(pw, " ");
10567 }
10568 }
10569 if (mTokenList.size() > 0) {
10570 pw.println(" ");
10571 pw.println(" Window token list:");
10572 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010573 pw.print(" #"); pw.print(i); pw.print(": ");
10574 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 }
10576 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010577 if (mWallpaperTokens.size() > 0) {
10578 pw.println(" ");
10579 pw.println(" Wallpaper tokens:");
10580 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10581 WindowToken token = mWallpaperTokens.get(i);
10582 pw.print(" Wallpaper #"); pw.print(i);
10583 pw.print(' '); pw.print(token); pw.println(':');
10584 token.dump(pw, " ");
10585 }
10586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 if (mAppTokens.size() > 0) {
10588 pw.println(" ");
10589 pw.println(" Application tokens in Z order:");
10590 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010591 pw.print(" App #"); pw.print(i); pw.print(": ");
10592 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 }
10594 }
10595 if (mFinishedStarting.size() > 0) {
10596 pw.println(" ");
10597 pw.println(" Finishing start of application tokens:");
10598 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10599 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010600 pw.print(" Finished Starting #"); pw.print(i);
10601 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 token.dump(pw, " ");
10603 }
10604 }
10605 if (mExitingTokens.size() > 0) {
10606 pw.println(" ");
10607 pw.println(" Exiting tokens:");
10608 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10609 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010610 pw.print(" Exiting #"); pw.print(i);
10611 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 token.dump(pw, " ");
10613 }
10614 }
10615 if (mExitingAppTokens.size() > 0) {
10616 pw.println(" ");
10617 pw.println(" Exiting application tokens:");
10618 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10619 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010620 pw.print(" Exiting App #"); pw.print(i);
10621 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622 token.dump(pw, " ");
10623 }
10624 }
10625 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010626 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10627 pw.print(" mLastFocus="); pw.println(mLastFocus);
10628 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10629 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10630 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010631 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010632 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10633 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10634 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10635 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010636 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10637 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10638 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10639 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10640 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010641 if (mDimAnimator != null) {
10642 mDimAnimator.printTo(pw);
10643 } else {
10644 pw.print( " no DimAnimator ");
10645 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010646 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010647 pw.print(mInputMethodAnimLayerAdjustment);
10648 pw.print(" mWallpaperAnimLayerAdjustment=");
10649 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010650 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10651 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010652 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10653 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10654 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10655 pw.print(" mRotation="); pw.print(mRotation);
10656 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10657 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10658 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10659 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10660 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10661 pw.print(" mNextAppTransition=0x");
10662 pw.print(Integer.toHexString(mNextAppTransition));
10663 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010664 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010665 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010666 if (mNextAppTransitionPackage != null) {
10667 pw.print(" mNextAppTransitionPackage=");
10668 pw.print(mNextAppTransitionPackage);
10669 pw.print(", mNextAppTransitionEnter=0x");
10670 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10671 pw.print(", mNextAppTransitionExit=0x");
10672 pw.print(Integer.toHexString(mNextAppTransitionExit));
10673 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010674 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10675 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010676 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10677 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10678 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10679 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010680 if (mOpeningApps.size() > 0) {
10681 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10682 }
10683 if (mClosingApps.size() > 0) {
10684 pw.print(" mClosingApps="); pw.println(mClosingApps);
10685 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010686 if (mToTopApps.size() > 0) {
10687 pw.print(" mToTopApps="); pw.println(mToTopApps);
10688 }
10689 if (mToBottomApps.size() > 0) {
10690 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10691 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010692 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10693 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010694 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010695 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10696 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10697 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10698 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10699 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10700 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 }
10702 }
10703
10704 public void monitor() {
10705 synchronized (mWindowMap) { }
10706 synchronized (mKeyguardDisabled) { }
10707 synchronized (mKeyWaiter) { }
10708 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010709
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010710 public void virtualKeyFeedback(KeyEvent event) {
10711 mPolicy.keyFeedbackFromInput(event);
10712 }
10713
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010714 /**
10715 * DimAnimator class that controls the dim animation. This holds the surface and
10716 * all state used for dim animation.
10717 */
10718 private static class DimAnimator {
10719 Surface mDimSurface;
10720 boolean mDimShown = false;
10721 float mDimCurrentAlpha;
10722 float mDimTargetAlpha;
10723 float mDimDeltaPerMs;
10724 long mLastDimAnimTime;
10725
10726 DimAnimator (SurfaceSession session) {
10727 if (mDimSurface == null) {
10728 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10729 + mDimSurface + ": CREATE");
10730 try {
10731 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10732 Surface.FX_SURFACE_DIM);
10733 } catch (Exception e) {
10734 Log.e(TAG, "Exception creating Dim surface", e);
10735 }
10736 }
10737 }
10738
10739 /**
10740 * Show the dim surface.
10741 */
10742 void show(int dw, int dh) {
10743 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10744 dw + "x" + dh + ")");
10745 mDimShown = true;
10746 try {
10747 mDimSurface.setPosition(0, 0);
10748 mDimSurface.setSize(dw, dh);
10749 mDimSurface.show();
10750 } catch (RuntimeException e) {
10751 Log.w(TAG, "Failure showing dim surface", e);
10752 }
10753 }
10754
10755 /**
10756 * Set's the dim surface's layer and update dim parameters that will be used in
10757 * {@link updateSurface} after all windows are examined.
10758 */
10759 void updateParameters(WindowState w, long currentTime) {
10760 mDimSurface.setLayer(w.mAnimLayer-1);
10761
10762 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010763 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10764 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010765 if (mDimTargetAlpha != target) {
10766 // If the desired dim level has changed, then
10767 // start an animation to it.
10768 mLastDimAnimTime = currentTime;
10769 long duration = (w.mAnimating && w.mAnimation != null)
10770 ? w.mAnimation.computeDurationHint()
10771 : DEFAULT_DIM_DURATION;
10772 if (target > mDimTargetAlpha) {
10773 // This is happening behind the activity UI,
10774 // so we can make it run a little longer to
10775 // give a stronger impression without disrupting
10776 // the user.
10777 duration *= DIM_DURATION_MULTIPLIER;
10778 }
10779 if (duration < 1) {
10780 // Don't divide by zero
10781 duration = 1;
10782 }
10783 mDimTargetAlpha = target;
10784 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10785 }
10786 }
10787
10788 /**
10789 * Updating the surface's alpha. Returns true if the animation continues, or returns
10790 * false when the animation is finished and the dim surface is hidden.
10791 */
10792 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10793 if (!dimming) {
10794 if (mDimTargetAlpha != 0) {
10795 mLastDimAnimTime = currentTime;
10796 mDimTargetAlpha = 0;
10797 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10798 }
10799 }
10800
10801 boolean animating = false;
10802 if (mLastDimAnimTime != 0) {
10803 mDimCurrentAlpha += mDimDeltaPerMs
10804 * (currentTime-mLastDimAnimTime);
10805 boolean more = true;
10806 if (displayFrozen) {
10807 // If the display is frozen, there is no reason to animate.
10808 more = false;
10809 } else if (mDimDeltaPerMs > 0) {
10810 if (mDimCurrentAlpha > mDimTargetAlpha) {
10811 more = false;
10812 }
10813 } else if (mDimDeltaPerMs < 0) {
10814 if (mDimCurrentAlpha < mDimTargetAlpha) {
10815 more = false;
10816 }
10817 } else {
10818 more = false;
10819 }
10820
10821 // Do we need to continue animating?
10822 if (more) {
10823 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10824 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10825 mLastDimAnimTime = currentTime;
10826 mDimSurface.setAlpha(mDimCurrentAlpha);
10827 animating = true;
10828 } else {
10829 mDimCurrentAlpha = mDimTargetAlpha;
10830 mLastDimAnimTime = 0;
10831 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10832 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10833 mDimSurface.setAlpha(mDimCurrentAlpha);
10834 if (!dimming) {
10835 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10836 + ": HIDE");
10837 try {
10838 mDimSurface.hide();
10839 } catch (RuntimeException e) {
10840 Log.w(TAG, "Illegal argument exception hiding dim surface");
10841 }
10842 mDimShown = false;
10843 }
10844 }
10845 }
10846 return animating;
10847 }
10848
10849 public void printTo(PrintWriter pw) {
10850 pw.print(" mDimShown="); pw.print(mDimShown);
10851 pw.print(" current="); pw.print(mDimCurrentAlpha);
10852 pw.print(" target="); pw.print(mDimTargetAlpha);
10853 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10854 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10855 }
10856 }
10857
10858 /**
10859 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10860 * This is used for opening/closing transition for apps in compatible mode.
10861 */
10862 private static class FadeInOutAnimation extends Animation {
10863 int mWidth;
10864 boolean mFadeIn;
10865
10866 public FadeInOutAnimation(boolean fadeIn) {
10867 setInterpolator(new AccelerateInterpolator());
10868 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10869 mFadeIn = fadeIn;
10870 }
10871
10872 @Override
10873 protected void applyTransformation(float interpolatedTime, Transformation t) {
10874 float x = interpolatedTime;
10875 if (!mFadeIn) {
10876 x = 1.0f - x; // reverse the interpolation for fade out
10877 }
10878 if (x < 0.5) {
10879 // move the window out of the screen.
10880 t.getMatrix().setTranslate(mWidth, 0);
10881 } else {
10882 t.getMatrix().setTranslate(0, 0);// show
10883 t.setAlpha((x - 0.5f) * 2);
10884 }
10885 }
10886
10887 @Override
10888 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10889 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10890 mWidth = width;
10891 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010892
10893 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010894 public int getZAdjustment() {
10895 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010896 }
10897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010898}