blob: 2c39c2a688ebee734e9abbdbe147a4c56e02fb7e [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() {
236 synchronized (mKeyguardDisabled) {
237 mPolicy.enableKeyguard(true);
238 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 /**
305 * Z-ordered (bottom-most first) list of all Window objects.
306 */
307 final ArrayList mWindows = new ArrayList();
308
309 /**
310 * Windows that are being resized. Used so we can tell the client about
311 * the resize after closing the transaction in which we resized the
312 * underlying surface.
313 */
314 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
315
316 /**
317 * Windows whose animations have ended and now must be removed.
318 */
319 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
320
321 /**
322 * Windows whose surface should be destroyed.
323 */
324 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
325
326 /**
327 * Windows that have lost input focus and are waiting for the new
328 * focus window to be displayed before they are told about this.
329 */
330 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
331
332 /**
333 * This is set when we have run out of memory, and will either be an empty
334 * list or contain windows that need to be force removed.
335 */
336 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700341 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 Surface mBlurSurface;
343 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 final float[] mTmpFloats = new float[9];
348
349 boolean mSafeMode;
350 boolean mDisplayEnabled = false;
351 boolean mSystemBooted = false;
352 int mRotation = 0;
353 int mRequestedRotation = 0;
354 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700355 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 ArrayList<IRotationWatcher> mRotationWatchers
357 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 boolean mLayoutNeeded = true;
360 boolean mAnimationPending = false;
361 boolean mDisplayFrozen = false;
362 boolean mWindowsFreezingScreen = false;
363 long mFreezeGcPending = 0;
364 int mAppsFreezingScreen = 0;
365
366 // This is held as long as we have the screen frozen, to give us time to
367 // perform a rotation animation when turning off shows the lock screen which
368 // changes the orientation.
369 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 // State management of app transitions. When we are preparing for a
372 // transition, mNextAppTransition will be the kind of transition to
373 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
374 // mOpeningApps and mClosingApps are the lists of tokens that will be
375 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700376 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700378 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 boolean mAppTransitionTimeout = false;
380 boolean mStartingIconInTransition = false;
381 boolean mSkipAppTransitionAnimation = false;
382 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
383 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700384 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
385 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 //flag to detect fat touch events
388 boolean mFatTouch = false;
389 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 H mH = new H();
392
393 WindowState mCurrentFocus = null;
394 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 // This just indicates the window the input method is on top of, not
397 // necessarily the window its input is going to.
398 WindowState mInputMethodTarget = null;
399 WindowState mUpcomingInputMethodTarget = null;
400 boolean mInputMethodTargetWaitingAnim;
401 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 WindowState mInputMethodWindow = null;
404 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
405
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700406 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
407
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700408 // If non-null, this is the currently visible window that is associated
409 // with the wallpaper.
410 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700411 // If non-null, we are in the middle of animating from one wallpaper target
412 // to another, and this is the lower one in Z-order.
413 WindowState mLowerWallpaperTarget = null;
414 // If non-null, we are in the middle of animating from one wallpaper target
415 // to another, and this is the higher one in Z-order.
416 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700417 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700418 float mLastWallpaperX;
419 float mLastWallpaperY;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700420 // Lock for waiting for the wallpaper.
421 final Object mWaitingOnWallpaperLock = new Object();
422 // This is set when we are waiting for a wallpaper to tell us it is done
423 // changing its scroll position.
424 WindowState mWaitingOnWallpaper;
425 // The last time we had a timeout when waiting for a wallpaper.
426 long mLastWallpaperTimeoutTime;
427 // We give a wallpaper up to 150ms to finish scrolling.
428 static final long WALLPAPER_TIMEOUT = 150;
429 // Time we wait after a timeout before trying to wait again.
430 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 AppWindowToken mFocusedApp = null;
433
434 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 float mWindowAnimationScale = 1.0f;
437 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 final KeyWaiter mKeyWaiter = new KeyWaiter();
440 final KeyQ mQueue;
441 final InputDispatcherThread mInputThread;
442
443 // Who is holding the screen on.
444 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700445
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700446 boolean mTurnOnScreen;
447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 /**
449 * Whether the UI is currently running in touch mode (not showing
450 * navigational focus because the user is directly pressing the screen).
451 */
452 boolean mInTouchMode = false;
453
454 private ViewServer mViewServer;
455
456 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700457
Dianne Hackbornc485a602009-03-24 22:39:49 -0700458 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700459 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700460
461 // The frame use to limit the size of the app running in compatibility mode.
462 Rect mCompatibleScreenFrame = new Rect();
463 // The surface used to fill the outer rim of the app running in compatibility mode.
464 Surface mBackgroundFillerSurface = null;
465 boolean mBackgroundFillerShown = false;
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public static WindowManagerService main(Context context,
468 PowerManagerService pm, boolean haveInputMethods) {
469 WMThread thr = new WMThread(context, pm, haveInputMethods);
470 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 synchronized (thr) {
473 while (thr.mService == null) {
474 try {
475 thr.wait();
476 } catch (InterruptedException e) {
477 }
478 }
479 }
Romain Guy06882f82009-06-10 13:36:04 -0700480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 return thr.mService;
482 }
Romain Guy06882f82009-06-10 13:36:04 -0700483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 static class WMThread extends Thread {
485 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 private final Context mContext;
488 private final PowerManagerService mPM;
489 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 public WMThread(Context context, PowerManagerService pm,
492 boolean haveInputMethods) {
493 super("WindowManager");
494 mContext = context;
495 mPM = pm;
496 mHaveInputMethods = haveInputMethods;
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public void run() {
500 Looper.prepare();
501 WindowManagerService s = new WindowManagerService(mContext, mPM,
502 mHaveInputMethods);
503 android.os.Process.setThreadPriority(
504 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 synchronized (this) {
507 mService = s;
508 notifyAll();
509 }
Romain Guy06882f82009-06-10 13:36:04 -0700510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 Looper.loop();
512 }
513 }
514
515 static class PolicyThread extends Thread {
516 private final WindowManagerPolicy mPolicy;
517 private final WindowManagerService mService;
518 private final Context mContext;
519 private final PowerManagerService mPM;
520 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public PolicyThread(WindowManagerPolicy policy,
523 WindowManagerService service, Context context,
524 PowerManagerService pm) {
525 super("WindowManagerPolicy");
526 mPolicy = policy;
527 mService = service;
528 mContext = context;
529 mPM = pm;
530 }
Romain Guy06882f82009-06-10 13:36:04 -0700531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public void run() {
533 Looper.prepare();
534 //Looper.myLooper().setMessageLogging(new LogPrinter(
535 // Log.VERBOSE, "WindowManagerPolicy"));
536 android.os.Process.setThreadPriority(
537 android.os.Process.THREAD_PRIORITY_FOREGROUND);
538 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 synchronized (this) {
541 mRunning = true;
542 notifyAll();
543 }
Romain Guy06882f82009-06-10 13:36:04 -0700544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 Looper.loop();
546 }
547 }
548
549 private WindowManagerService(Context context, PowerManagerService pm,
550 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700551 if (MEASURE_LATENCY) {
552 lt = new LatencyTimer(100, 1000);
553 }
554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 mContext = context;
556 mHaveInputMethods = haveInputMethods;
557 mLimitedAlphaCompositing = context.getResources().getBoolean(
558 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 mPowerManager = pm;
561 mPowerManager.setPolicy(mPolicy);
562 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
563 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
564 "SCREEN_FROZEN");
565 mScreenFrozenLock.setReferenceCounted(false);
566
567 mActivityManager = ActivityManagerNative.getDefault();
568 mBatteryStats = BatteryStatsService.getService();
569
570 // Get persisted window scale setting
571 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
572 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
573 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
574 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700575
Michael Chan9f028e62009-08-04 17:37:46 -0700576 int max_events_per_sec = 35;
577 try {
578 max_events_per_sec = Integer.parseInt(SystemProperties
579 .get("windowsmgr.max_events_per_sec"));
580 if (max_events_per_sec < 1) {
581 max_events_per_sec = 35;
582 }
583 } catch (NumberFormatException e) {
584 }
585 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 mQueue = new KeyQ();
588
589 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
592 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 synchronized (thr) {
595 while (!thr.mRunning) {
596 try {
597 thr.wait();
598 } catch (InterruptedException e) {
599 }
600 }
601 }
Romain Guy06882f82009-06-10 13:36:04 -0700602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 // Add ourself to the Watchdog monitors.
606 Watchdog.getInstance().addMonitor(this);
607 }
608
609 @Override
610 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
611 throws RemoteException {
612 try {
613 return super.onTransact(code, data, reply, flags);
614 } catch (RuntimeException e) {
615 // The window manager only throws security exceptions, so let's
616 // log all others.
617 if (!(e instanceof SecurityException)) {
618 Log.e(TAG, "Window Manager Crash", e);
619 }
620 throw e;
621 }
622 }
623
624 private void placeWindowAfter(Object pos, WindowState window) {
625 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700626 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 TAG, "Adding window " + window + " at "
628 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
629 mWindows.add(i+1, window);
630 }
631
632 private void placeWindowBefore(Object pos, WindowState window) {
633 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700634 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 TAG, "Adding window " + window + " at "
636 + i + " of " + mWindows.size() + " (before " + pos + ")");
637 mWindows.add(i, window);
638 }
639
640 //This method finds out the index of a window that has the same app token as
641 //win. used for z ordering the windows in mWindows
642 private int findIdxBasedOnAppTokens(WindowState win) {
643 //use a local variable to cache mWindows
644 ArrayList localmWindows = mWindows;
645 int jmax = localmWindows.size();
646 if(jmax == 0) {
647 return -1;
648 }
649 for(int j = (jmax-1); j >= 0; j--) {
650 WindowState wentry = (WindowState)localmWindows.get(j);
651 if(wentry.mAppToken == win.mAppToken) {
652 return j;
653 }
654 }
655 return -1;
656 }
Romain Guy06882f82009-06-10 13:36:04 -0700657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
659 final IWindow client = win.mClient;
660 final WindowToken token = win.mToken;
661 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 final int N = localmWindows.size();
664 final WindowState attached = win.mAttachedWindow;
665 int i;
666 if (attached == null) {
667 int tokenWindowsPos = token.windows.size();
668 if (token.appWindowToken != null) {
669 int index = tokenWindowsPos-1;
670 if (index >= 0) {
671 // If this application has existing windows, we
672 // simply place the new window on top of them... but
673 // keep the starting window on top.
674 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
675 // Base windows go behind everything else.
676 placeWindowBefore(token.windows.get(0), win);
677 tokenWindowsPos = 0;
678 } else {
679 AppWindowToken atoken = win.mAppToken;
680 if (atoken != null &&
681 token.windows.get(index) == atoken.startingWindow) {
682 placeWindowBefore(token.windows.get(index), win);
683 tokenWindowsPos--;
684 } else {
685 int newIdx = findIdxBasedOnAppTokens(win);
686 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700687 //there is a window above this one associated with the same
688 //apptoken note that the window could be a floating window
689 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700691 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
692 TAG, "Adding window " + win + " at "
693 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 }
697 }
698 } else {
699 if (localLOGV) Log.v(
700 TAG, "Figuring out where to add app window "
701 + client.asBinder() + " (token=" + token + ")");
702 // Figure out where the window should go, based on the
703 // order of applications.
704 final int NA = mAppTokens.size();
705 Object pos = null;
706 for (i=NA-1; i>=0; i--) {
707 AppWindowToken t = mAppTokens.get(i);
708 if (t == token) {
709 i--;
710 break;
711 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700712
713 // We haven't reached the token yet; if this token
714 // is not going to the bottom and has windows, we can
715 // use it as an anchor for when we do reach the token.
716 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 pos = t.windows.get(0);
718 }
719 }
720 // We now know the index into the apps. If we found
721 // an app window above, that gives us the position; else
722 // we need to look some more.
723 if (pos != null) {
724 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700725 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 mTokenMap.get(((WindowState)pos).mClient.asBinder());
727 if (atoken != null) {
728 final int NC = atoken.windows.size();
729 if (NC > 0) {
730 WindowState bottom = atoken.windows.get(0);
731 if (bottom.mSubLayer < 0) {
732 pos = bottom;
733 }
734 }
735 }
736 placeWindowBefore(pos, win);
737 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700738 // Continue looking down until we find the first
739 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 while (i >= 0) {
741 AppWindowToken t = mAppTokens.get(i);
742 final int NW = t.windows.size();
743 if (NW > 0) {
744 pos = t.windows.get(NW-1);
745 break;
746 }
747 i--;
748 }
749 if (pos != null) {
750 // Move in front of any windows attached to this
751 // one.
752 WindowToken atoken =
753 mTokenMap.get(((WindowState)pos).mClient.asBinder());
754 if (atoken != null) {
755 final int NC = atoken.windows.size();
756 if (NC > 0) {
757 WindowState top = atoken.windows.get(NC-1);
758 if (top.mSubLayer >= 0) {
759 pos = top;
760 }
761 }
762 }
763 placeWindowAfter(pos, win);
764 } else {
765 // Just search for the start of this layer.
766 final int myLayer = win.mBaseLayer;
767 for (i=0; i<N; i++) {
768 WindowState w = (WindowState)localmWindows.get(i);
769 if (w.mBaseLayer > myLayer) {
770 break;
771 }
772 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700773 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
774 TAG, "Adding window " + win + " at "
775 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 localmWindows.add(i, win);
777 }
778 }
779 }
780 } else {
781 // Figure out where window should go, based on layer.
782 final int myLayer = win.mBaseLayer;
783 for (i=N-1; i>=0; i--) {
784 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
785 i++;
786 break;
787 }
788 }
789 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700790 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
791 TAG, "Adding window " + win + " at "
792 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 localmWindows.add(i, win);
794 }
795 if (addToToken) {
796 token.windows.add(tokenWindowsPos, win);
797 }
798
799 } else {
800 // Figure out this window's ordering relative to the window
801 // it is attached to.
802 final int NA = token.windows.size();
803 final int sublayer = win.mSubLayer;
804 int largestSublayer = Integer.MIN_VALUE;
805 WindowState windowWithLargestSublayer = null;
806 for (i=0; i<NA; i++) {
807 WindowState w = token.windows.get(i);
808 final int wSublayer = w.mSubLayer;
809 if (wSublayer >= largestSublayer) {
810 largestSublayer = wSublayer;
811 windowWithLargestSublayer = w;
812 }
813 if (sublayer < 0) {
814 // For negative sublayers, we go below all windows
815 // in the same sublayer.
816 if (wSublayer >= sublayer) {
817 if (addToToken) {
818 token.windows.add(i, win);
819 }
820 placeWindowBefore(
821 wSublayer >= 0 ? attached : w, win);
822 break;
823 }
824 } else {
825 // For positive sublayers, we go above all windows
826 // in the same sublayer.
827 if (wSublayer > sublayer) {
828 if (addToToken) {
829 token.windows.add(i, win);
830 }
831 placeWindowBefore(w, win);
832 break;
833 }
834 }
835 }
836 if (i >= NA) {
837 if (addToToken) {
838 token.windows.add(win);
839 }
840 if (sublayer < 0) {
841 placeWindowBefore(attached, win);
842 } else {
843 placeWindowAfter(largestSublayer >= 0
844 ? windowWithLargestSublayer
845 : attached,
846 win);
847 }
848 }
849 }
Romain Guy06882f82009-06-10 13:36:04 -0700850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 if (win.mAppToken != null && addToToken) {
852 win.mAppToken.allAppWindows.add(win);
853 }
854 }
Romain Guy06882f82009-06-10 13:36:04 -0700855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 static boolean canBeImeTarget(WindowState w) {
857 final int fl = w.mAttrs.flags
858 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
859 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
860 return w.isVisibleOrAdding();
861 }
862 return false;
863 }
Romain Guy06882f82009-06-10 13:36:04 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
866 final ArrayList localmWindows = mWindows;
867 final int N = localmWindows.size();
868 WindowState w = null;
869 int i = N;
870 while (i > 0) {
871 i--;
872 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
875 // + Integer.toHexString(w.mAttrs.flags));
876 if (canBeImeTarget(w)) {
877 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 // Yet more tricksyness! If this window is a "starting"
880 // window, we do actually want to be on top of it, but
881 // it is not -really- where input will go. So if the caller
882 // is not actually looking to move the IME, look down below
883 // for a real window to target...
884 if (!willMove
885 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
886 && i > 0) {
887 WindowState wb = (WindowState)localmWindows.get(i-1);
888 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
889 i--;
890 w = wb;
891 }
892 }
893 break;
894 }
895 }
Romain Guy06882f82009-06-10 13:36:04 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
900 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 if (willMove && w != null) {
903 final WindowState curTarget = mInputMethodTarget;
904 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 // Now some fun for dealing with window animations that
907 // modify the Z order. We need to look at all windows below
908 // the current target that are in this app, finding the highest
909 // visible one in layering.
910 AppWindowToken token = curTarget.mAppToken;
911 WindowState highestTarget = null;
912 int highestPos = 0;
913 if (token.animating || token.animation != null) {
914 int pos = 0;
915 pos = localmWindows.indexOf(curTarget);
916 while (pos >= 0) {
917 WindowState win = (WindowState)localmWindows.get(pos);
918 if (win.mAppToken != token) {
919 break;
920 }
921 if (!win.mRemoved) {
922 if (highestTarget == null || win.mAnimLayer >
923 highestTarget.mAnimLayer) {
924 highestTarget = win;
925 highestPos = pos;
926 }
927 }
928 pos--;
929 }
930 }
Romain Guy06882f82009-06-10 13:36:04 -0700931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700933 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 + mNextAppTransition + " " + highestTarget
935 + " animating=" + highestTarget.isAnimating()
936 + " layer=" + highestTarget.mAnimLayer
937 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700938
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700939 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 // If we are currently setting up for an animation,
941 // hold everything until we can find out what will happen.
942 mInputMethodTargetWaitingAnim = true;
943 mInputMethodTarget = highestTarget;
944 return highestPos + 1;
945 } else if (highestTarget.isAnimating() &&
946 highestTarget.mAnimLayer > w.mAnimLayer) {
947 // If the window we are currently targeting is involved
948 // with an animation, and it is on top of the next target
949 // we will be over, then hold off on moving until
950 // that is done.
951 mInputMethodTarget = highestTarget;
952 return highestPos + 1;
953 }
954 }
955 }
956 }
Romain Guy06882f82009-06-10 13:36:04 -0700957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 //Log.i(TAG, "Placing input method @" + (i+1));
959 if (w != null) {
960 if (willMove) {
961 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700962 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
964 + mInputMethodTarget + " to " + w, e);
965 mInputMethodTarget = w;
966 if (w.mAppToken != null) {
967 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
968 } else {
969 setInputMethodAnimLayerAdjustment(0);
970 }
971 }
972 return i+1;
973 }
974 if (willMove) {
975 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700976 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
978 + mInputMethodTarget + " to null", e);
979 mInputMethodTarget = null;
980 setInputMethodAnimLayerAdjustment(0);
981 }
982 return -1;
983 }
Romain Guy06882f82009-06-10 13:36:04 -0700984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 void addInputMethodWindowToListLocked(WindowState win) {
986 int pos = findDesiredInputMethodWindowIndexLocked(true);
987 if (pos >= 0) {
988 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700989 if (DEBUG_WINDOW_MOVEMENT) Log.v(
990 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 mWindows.add(pos, win);
992 moveInputMethodDialogsLocked(pos+1);
993 return;
994 }
995 win.mTargetAppToken = null;
996 addWindowToListInOrderLocked(win, true);
997 moveInputMethodDialogsLocked(pos);
998 }
Romain Guy06882f82009-06-10 13:36:04 -0700999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 void setInputMethodAnimLayerAdjustment(int adj) {
1001 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1002 mInputMethodAnimLayerAdjustment = adj;
1003 WindowState imw = mInputMethodWindow;
1004 if (imw != null) {
1005 imw.mAnimLayer = imw.mLayer + adj;
1006 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1007 + " anim layer: " + imw.mAnimLayer);
1008 int wi = imw.mChildWindows.size();
1009 while (wi > 0) {
1010 wi--;
1011 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1012 cw.mAnimLayer = cw.mLayer + adj;
1013 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1014 + " anim layer: " + cw.mAnimLayer);
1015 }
1016 }
1017 int di = mInputMethodDialogs.size();
1018 while (di > 0) {
1019 di --;
1020 imw = mInputMethodDialogs.get(di);
1021 imw.mAnimLayer = imw.mLayer + adj;
1022 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1023 + " anim layer: " + imw.mAnimLayer);
1024 }
1025 }
Romain Guy06882f82009-06-10 13:36:04 -07001026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1028 int wpos = mWindows.indexOf(win);
1029 if (wpos >= 0) {
1030 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001031 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 mWindows.remove(wpos);
1033 int NC = win.mChildWindows.size();
1034 while (NC > 0) {
1035 NC--;
1036 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1037 int cpos = mWindows.indexOf(cw);
1038 if (cpos >= 0) {
1039 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001040 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1041 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 mWindows.remove(cpos);
1043 }
1044 }
1045 }
1046 return interestingPos;
1047 }
Romain Guy06882f82009-06-10 13:36:04 -07001048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 private void reAddWindowToListInOrderLocked(WindowState win) {
1050 addWindowToListInOrderLocked(win, false);
1051 // This is a hack to get all of the child windows added as well
1052 // at the right position. Child windows should be rare and
1053 // this case should be rare, so it shouldn't be that big a deal.
1054 int wpos = mWindows.indexOf(win);
1055 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001056 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1057 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 mWindows.remove(wpos);
1059 reAddWindowLocked(wpos, win);
1060 }
1061 }
Romain Guy06882f82009-06-10 13:36:04 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 void logWindowList(String prefix) {
1064 int N = mWindows.size();
1065 while (N > 0) {
1066 N--;
1067 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1068 }
1069 }
Romain Guy06882f82009-06-10 13:36:04 -07001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 void moveInputMethodDialogsLocked(int pos) {
1072 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 final int N = dialogs.size();
1075 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1076 for (int i=0; i<N; i++) {
1077 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1078 }
1079 if (DEBUG_INPUT_METHOD) {
1080 Log.v(TAG, "Window list w/pos=" + pos);
1081 logWindowList(" ");
1082 }
Romain Guy06882f82009-06-10 13:36:04 -07001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 if (pos >= 0) {
1085 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1086 if (pos < mWindows.size()) {
1087 WindowState wp = (WindowState)mWindows.get(pos);
1088 if (wp == mInputMethodWindow) {
1089 pos++;
1090 }
1091 }
1092 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1093 for (int i=0; i<N; i++) {
1094 WindowState win = dialogs.get(i);
1095 win.mTargetAppToken = targetAppToken;
1096 pos = reAddWindowLocked(pos, win);
1097 }
1098 if (DEBUG_INPUT_METHOD) {
1099 Log.v(TAG, "Final window list:");
1100 logWindowList(" ");
1101 }
1102 return;
1103 }
1104 for (int i=0; i<N; i++) {
1105 WindowState win = dialogs.get(i);
1106 win.mTargetAppToken = null;
1107 reAddWindowToListInOrderLocked(win);
1108 if (DEBUG_INPUT_METHOD) {
1109 Log.v(TAG, "No IM target, final list:");
1110 logWindowList(" ");
1111 }
1112 }
1113 }
Romain Guy06882f82009-06-10 13:36:04 -07001114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1116 final WindowState imWin = mInputMethodWindow;
1117 final int DN = mInputMethodDialogs.size();
1118 if (imWin == null && DN == 0) {
1119 return false;
1120 }
Romain Guy06882f82009-06-10 13:36:04 -07001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1123 if (imPos >= 0) {
1124 // In this case, the input method windows are to be placed
1125 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 // First check to see if the input method windows are already
1128 // located here, and contiguous.
1129 final int N = mWindows.size();
1130 WindowState firstImWin = imPos < N
1131 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 // Figure out the actual input method window that should be
1134 // at the bottom of their stack.
1135 WindowState baseImWin = imWin != null
1136 ? imWin : mInputMethodDialogs.get(0);
1137 if (baseImWin.mChildWindows.size() > 0) {
1138 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1139 if (cw.mSubLayer < 0) baseImWin = cw;
1140 }
Romain Guy06882f82009-06-10 13:36:04 -07001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 if (firstImWin == baseImWin) {
1143 // The windows haven't moved... but are they still contiguous?
1144 // First find the top IM window.
1145 int pos = imPos+1;
1146 while (pos < N) {
1147 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1148 break;
1149 }
1150 pos++;
1151 }
1152 pos++;
1153 // Now there should be no more input method windows above.
1154 while (pos < N) {
1155 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1156 break;
1157 }
1158 pos++;
1159 }
1160 if (pos >= N) {
1161 // All is good!
1162 return false;
1163 }
1164 }
Romain Guy06882f82009-06-10 13:36:04 -07001165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (imWin != null) {
1167 if (DEBUG_INPUT_METHOD) {
1168 Log.v(TAG, "Moving IM from " + imPos);
1169 logWindowList(" ");
1170 }
1171 imPos = tmpRemoveWindowLocked(imPos, imWin);
1172 if (DEBUG_INPUT_METHOD) {
1173 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1174 logWindowList(" ");
1175 }
1176 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1177 reAddWindowLocked(imPos, imWin);
1178 if (DEBUG_INPUT_METHOD) {
1179 Log.v(TAG, "List after moving IM to " + imPos + ":");
1180 logWindowList(" ");
1181 }
1182 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1183 } else {
1184 moveInputMethodDialogsLocked(imPos);
1185 }
Romain Guy06882f82009-06-10 13:36:04 -07001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 } else {
1188 // In this case, the input method windows go in a fixed layer,
1189 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 if (imWin != null) {
1192 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1193 tmpRemoveWindowLocked(0, imWin);
1194 imWin.mTargetAppToken = null;
1195 reAddWindowToListInOrderLocked(imWin);
1196 if (DEBUG_INPUT_METHOD) {
1197 Log.v(TAG, "List with no IM target:");
1198 logWindowList(" ");
1199 }
1200 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1201 } else {
1202 moveInputMethodDialogsLocked(-1);;
1203 }
Romain Guy06882f82009-06-10 13:36:04 -07001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
Romain Guy06882f82009-06-10 13:36:04 -07001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 if (needAssignLayers) {
1208 assignLayersLocked();
1209 }
Romain Guy06882f82009-06-10 13:36:04 -07001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 return true;
1212 }
Romain Guy06882f82009-06-10 13:36:04 -07001213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 void adjustInputMethodDialogsLocked() {
1215 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1216 }
Romain Guy06882f82009-06-10 13:36:04 -07001217
Dianne Hackborn25994b42009-09-04 14:21:19 -07001218 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1219 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1220 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1221 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1222 ? wallpaperTarget.mAppToken.animation : null)
1223 + " upper=" + mUpperWallpaperTarget
1224 + " lower=" + mLowerWallpaperTarget);
1225 return (wallpaperTarget != null
1226 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1227 && wallpaperTarget.mAppToken.animation != null)))
1228 || mUpperWallpaperTarget != null
1229 || mLowerWallpaperTarget != null;
1230 }
1231
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001232 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1233 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1234
1235 int adjustWallpaperWindowsLocked() {
1236 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001237
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001238 final int dw = mDisplay.getWidth();
1239 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001240
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001241 // First find top-most window that has asked to be on top of the
1242 // wallpaper; all wallpapers go behind it.
1243 final ArrayList localmWindows = mWindows;
1244 int N = localmWindows.size();
1245 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001246 WindowState foundW = null;
1247 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001248 int i = N;
1249 while (i > 0) {
1250 i--;
1251 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001252 if (w.mAppToken != null) {
1253 // If this window's app token is hidden and not animating,
1254 // it is of no interest to us.
1255 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1256 if (DEBUG_WALLPAPER) Log.v(TAG,
1257 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001258 continue;
1259 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001260 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001261 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1262 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1263 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001264 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001265 && (mWallpaperTarget == w
1266 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001267 if (DEBUG_WALLPAPER) Log.v(TAG,
1268 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001269 foundW = w;
1270 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001271 if (w == mWallpaperTarget && w.mAppToken != null
1272 && w.mAppToken.animation != null) {
1273 // The current wallpaper target is animating, so we'll
1274 // look behind it for another possible target and figure
1275 // out what is going on below.
1276 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1277 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001278 continue;
1279 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001280 break;
1281 }
1282 }
1283
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001284 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001285 // If we are currently waiting for an app transition, and either
1286 // the current target or the next target are involved with it,
1287 // then hold off on doing anything with the wallpaper.
1288 // Note that we are checking here for just whether the target
1289 // is part of an app token... which is potentially overly aggressive
1290 // (the app token may not be involved in the transition), but good
1291 // enough (we'll just wait until whatever transition is pending
1292 // executes).
1293 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001294 if (DEBUG_WALLPAPER) Log.v(TAG,
1295 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001296 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001297 }
1298 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001299 if (DEBUG_WALLPAPER) Log.v(TAG,
1300 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001301 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001302 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001303 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001304
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001305 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001306 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001307 Log.v(TAG, "New wallpaper target: " + foundW
1308 + " oldTarget: " + mWallpaperTarget);
1309 }
1310
1311 mLowerWallpaperTarget = null;
1312 mUpperWallpaperTarget = null;
1313
1314 WindowState oldW = mWallpaperTarget;
1315 mWallpaperTarget = foundW;
1316
1317 // Now what is happening... if the current and new targets are
1318 // animating, then we are in our super special mode!
1319 if (foundW != null && foundW.mAppToken != null && oldW != null
1320 && oldW.mAppToken != null) {
1321 if (DEBUG_WALLPAPER) {
1322 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1323 + " old animation: " + oldW.mAppToken.animation);
1324 }
1325 if (foundW.mAppToken.animation != null
1326 && oldW.mAppToken.animation != null) {
1327 int oldI = localmWindows.indexOf(oldW);
1328 if (DEBUG_WALLPAPER) {
1329 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1330 }
1331 if (oldI >= 0) {
1332 if (DEBUG_WALLPAPER) {
1333 Log.v(TAG, "Animating wallpapers: old#" + oldI
1334 + "=" + oldW + "; new#" + foundI
1335 + "=" + foundW);
1336 }
1337
1338 // Set the new target correctly.
1339 if (foundW.mAppToken.hiddenRequested) {
1340 if (DEBUG_WALLPAPER) {
1341 Log.v(TAG, "Old wallpaper still the target.");
1342 }
1343 mWallpaperTarget = oldW;
1344 }
1345
1346 // Now set the upper and lower wallpaper targets
1347 // correctly, and make sure that we are positioning
1348 // the wallpaper below the lower.
1349 if (foundI > oldI) {
1350 // The new target is on top of the old one.
1351 if (DEBUG_WALLPAPER) {
1352 Log.v(TAG, "Found target above old target.");
1353 }
1354 mUpperWallpaperTarget = foundW;
1355 mLowerWallpaperTarget = oldW;
1356 foundW = oldW;
1357 foundI = oldI;
1358 } else {
1359 // The new target is below the old one.
1360 if (DEBUG_WALLPAPER) {
1361 Log.v(TAG, "Found target below old target.");
1362 }
1363 mUpperWallpaperTarget = oldW;
1364 mLowerWallpaperTarget = foundW;
1365 }
1366 }
1367 }
1368 }
1369
1370 } else {
1371 // Is it time to stop animating?
1372 if (mLowerWallpaperTarget == null
1373 || mLowerWallpaperTarget.mAppToken.animation == null
1374 || mUpperWallpaperTarget == null
1375 || mUpperWallpaperTarget.mAppToken.animation == null) {
1376 if (DEBUG_WALLPAPER) {
1377 Log.v(TAG, "No longer animating wallpaper targets!");
1378 }
1379 mLowerWallpaperTarget = null;
1380 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001381 }
1382 }
1383
1384 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001385 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001386 // The window is visible to the compositor... but is it visible
1387 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001388 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001389 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001390
1391 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001392 // its layer adjustment. Only do this if we are not transfering
1393 // between two wallpaper targets.
1394 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001395 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001396 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001397
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001398 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1399 * TYPE_LAYER_MULTIPLIER
1400 + TYPE_LAYER_OFFSET;
1401
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001402 // Now w is the window we are supposed to be behind... but we
1403 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001404 // AND any starting window associated with it, AND below the
1405 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001406 while (foundI > 0) {
1407 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001408 if (wb.mBaseLayer < maxLayer &&
1409 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001410 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001411 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001412 // This window is not related to the previous one in any
1413 // interesting way, so stop here.
1414 break;
1415 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001416 foundW = wb;
1417 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001418 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001419 } else {
1420 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001421 }
1422
1423 // Okay i is the position immediately above the wallpaper. Look at
1424 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001425 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001426
Dianne Hackborn284ac932009-08-28 10:34:25 -07001427 if (visible) {
1428 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1429 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1430 }
1431
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001432 // Start stepping backwards from here, ensuring that our wallpaper windows
1433 // are correctly placed.
1434 int curTokenIndex = mWallpaperTokens.size();
1435 while (curTokenIndex > 0) {
1436 curTokenIndex--;
1437 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001438 if (token.hidden == visible) {
1439 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1440 token.hidden = !visible;
1441 // Need to do a layout to ensure the wallpaper now has the
1442 // correct size.
1443 mLayoutNeeded = true;
1444 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001445
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001446 int curWallpaperIndex = token.windows.size();
1447 while (curWallpaperIndex > 0) {
1448 curWallpaperIndex--;
1449 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001450
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001451 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001452 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001453 }
1454
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001455 // First, make sure the client has the current visibility
1456 // state.
1457 if (wallpaper.mWallpaperVisible != visible) {
1458 wallpaper.mWallpaperVisible = visible;
1459 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001460 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001461 "Setting visibility of wallpaper " + wallpaper
1462 + ": " + visible);
1463 wallpaper.mClient.dispatchAppVisibility(visible);
1464 } catch (RemoteException e) {
1465 }
1466 }
1467
1468 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001469 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1470 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001471
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001472 // First, if this window is at the current index, then all
1473 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001474 if (wallpaper == foundW) {
1475 foundI--;
1476 foundW = foundI > 0
1477 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001478 continue;
1479 }
1480
1481 // The window didn't match... the current wallpaper window,
1482 // wherever it is, is in the wrong place, so make sure it is
1483 // not in the list.
1484 int oldIndex = localmWindows.indexOf(wallpaper);
1485 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001486 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1487 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001488 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001489 if (oldIndex < foundI) {
1490 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001491 }
1492 }
1493
1494 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001495 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1496 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001497 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001498
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001499 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001500 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001501 }
1502 }
1503
1504 return changed;
1505 }
1506
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001507 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001508 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1509 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001510 mWallpaperAnimLayerAdjustment = adj;
1511 int curTokenIndex = mWallpaperTokens.size();
1512 while (curTokenIndex > 0) {
1513 curTokenIndex--;
1514 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1515 int curWallpaperIndex = token.windows.size();
1516 while (curWallpaperIndex > 0) {
1517 curWallpaperIndex--;
1518 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1519 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001520 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1521 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001522 }
1523 }
1524 }
1525
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001526 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1527 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001528 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001529 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001530 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001531 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001532 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001533 changed = wallpaperWin.mXOffset != offset;
1534 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001535 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1536 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001537 wallpaperWin.mXOffset = offset;
1538 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001539 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1540 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001541 rawChanged = true;
1542 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001543 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001544
Dianne Hackborn284ac932009-08-28 10:34:25 -07001545 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001546 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001547 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001548 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001549 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1550 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001551 changed = true;
1552 wallpaperWin.mYOffset = offset;
1553 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001554 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1555 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001556 rawChanged = true;
1557 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001558 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001559
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001560 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001561 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001562 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1563 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1564 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001565 if (sync) {
1566 synchronized (mWaitingOnWallpaperLock) {
1567 mWaitingOnWallpaper = wallpaperWin;
1568 }
1569 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001570 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001571 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1572 if (sync) {
1573 synchronized (mWaitingOnWallpaperLock) {
1574 if (mWaitingOnWallpaper != null) {
1575 long start = SystemClock.uptimeMillis();
1576 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1577 < start) {
1578 try {
1579 if (DEBUG_WALLPAPER) Log.v(TAG,
1580 "Waiting for offset complete...");
1581 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1582 } catch (InterruptedException e) {
1583 }
1584 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1585 if ((start+WALLPAPER_TIMEOUT)
1586 < SystemClock.uptimeMillis()) {
1587 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1588 + wallpaperWin);
1589 mLastWallpaperTimeoutTime = start;
1590 }
1591 }
1592 mWaitingOnWallpaper = null;
1593 }
1594 }
1595 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001596 } catch (RemoteException e) {
1597 }
1598 }
1599
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001600 return changed;
1601 }
1602
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001603 void wallpaperOffsetsComplete(IBinder window) {
1604 synchronized (mWaitingOnWallpaperLock) {
1605 if (mWaitingOnWallpaper != null &&
1606 mWaitingOnWallpaper.mClient.asBinder() == window) {
1607 mWaitingOnWallpaper = null;
1608 mWaitingOnWallpaperLock.notifyAll();
1609 }
1610 }
1611 }
1612
1613 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001614 final int dw = mDisplay.getWidth();
1615 final int dh = mDisplay.getHeight();
1616
1617 boolean changed = false;
1618
1619 WindowState target = mWallpaperTarget;
1620 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001621 mLastWallpaperX = target.mWallpaperX;
1622 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001623 int curTokenIndex = mWallpaperTokens.size();
1624 while (curTokenIndex > 0) {
1625 curTokenIndex--;
1626 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1627 int curWallpaperIndex = token.windows.size();
1628 while (curWallpaperIndex > 0) {
1629 curWallpaperIndex--;
1630 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001631 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001632 wallpaper.computeShownFrameLocked();
1633 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001634 // We only want to be synchronous with one wallpaper.
1635 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001636 }
1637 }
1638 }
1639 }
1640
1641 return changed;
1642 }
1643
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001644 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001645 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001646 final int dw = mDisplay.getWidth();
1647 final int dh = mDisplay.getHeight();
1648
1649 int curTokenIndex = mWallpaperTokens.size();
1650 while (curTokenIndex > 0) {
1651 curTokenIndex--;
1652 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001653 if (token.hidden == visible) {
1654 token.hidden = !visible;
1655 // Need to do a layout to ensure the wallpaper now has the
1656 // correct size.
1657 mLayoutNeeded = true;
1658 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001659
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001660 int curWallpaperIndex = token.windows.size();
1661 while (curWallpaperIndex > 0) {
1662 curWallpaperIndex--;
1663 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1664 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001665 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001666 }
1667
1668 if (wallpaper.mWallpaperVisible != visible) {
1669 wallpaper.mWallpaperVisible = visible;
1670 try {
1671 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001672 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001673 + ": " + visible);
1674 wallpaper.mClient.dispatchAppVisibility(visible);
1675 } catch (RemoteException e) {
1676 }
1677 }
1678 }
1679 }
1680 }
1681
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001682 void sendPointerToWallpaperLocked(WindowState srcWin,
1683 MotionEvent pointer, long eventTime) {
1684 int curTokenIndex = mWallpaperTokens.size();
1685 while (curTokenIndex > 0) {
1686 curTokenIndex--;
1687 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1688 int curWallpaperIndex = token.windows.size();
1689 while (curWallpaperIndex > 0) {
1690 curWallpaperIndex--;
1691 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1692 if ((wallpaper.mAttrs.flags &
1693 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1694 continue;
1695 }
1696 try {
1697 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1698 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1699 srcWin.mFrame.top-wallpaper.mFrame.top);
1700 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1701 } catch (RemoteException e) {
1702 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1703 }
1704 }
1705 }
1706 }
1707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 public int addWindow(Session session, IWindow client,
1709 WindowManager.LayoutParams attrs, int viewVisibility,
1710 Rect outContentInsets) {
1711 int res = mPolicy.checkAddPermission(attrs);
1712 if (res != WindowManagerImpl.ADD_OKAY) {
1713 return res;
1714 }
Romain Guy06882f82009-06-10 13:36:04 -07001715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 boolean reportNewConfig = false;
1717 WindowState attachedWindow = null;
1718 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 synchronized(mWindowMap) {
1721 // Instantiating a Display requires talking with the simulator,
1722 // so don't do it until we know the system is mostly up and
1723 // running.
1724 if (mDisplay == null) {
1725 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1726 mDisplay = wm.getDefaultDisplay();
1727 mQueue.setDisplay(mDisplay);
1728 reportNewConfig = true;
1729 }
Romain Guy06882f82009-06-10 13:36:04 -07001730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 if (mWindowMap.containsKey(client.asBinder())) {
1732 Log.w(TAG, "Window " + client + " is already added");
1733 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1734 }
1735
1736 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001737 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 if (attachedWindow == null) {
1739 Log.w(TAG, "Attempted to add window with token that is not a window: "
1740 + attrs.token + ". Aborting.");
1741 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1742 }
1743 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1744 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1745 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1746 + attrs.token + ". Aborting.");
1747 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1748 }
1749 }
1750
1751 boolean addToken = false;
1752 WindowToken token = mTokenMap.get(attrs.token);
1753 if (token == null) {
1754 if (attrs.type >= FIRST_APPLICATION_WINDOW
1755 && attrs.type <= LAST_APPLICATION_WINDOW) {
1756 Log.w(TAG, "Attempted to add application window with unknown token "
1757 + attrs.token + ". Aborting.");
1758 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1759 }
1760 if (attrs.type == TYPE_INPUT_METHOD) {
1761 Log.w(TAG, "Attempted to add input method window with unknown token "
1762 + attrs.token + ". Aborting.");
1763 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1764 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001765 if (attrs.type == TYPE_WALLPAPER) {
1766 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1767 + attrs.token + ". Aborting.");
1768 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 token = new WindowToken(attrs.token, -1, false);
1771 addToken = true;
1772 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1773 && attrs.type <= LAST_APPLICATION_WINDOW) {
1774 AppWindowToken atoken = token.appWindowToken;
1775 if (atoken == null) {
1776 Log.w(TAG, "Attempted to add window with non-application token "
1777 + token + ". Aborting.");
1778 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1779 } else if (atoken.removed) {
1780 Log.w(TAG, "Attempted to add window with exiting application token "
1781 + token + ". Aborting.");
1782 return WindowManagerImpl.ADD_APP_EXITING;
1783 }
1784 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1785 // No need for this guy!
1786 if (localLOGV) Log.v(
1787 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1788 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1789 }
1790 } else if (attrs.type == TYPE_INPUT_METHOD) {
1791 if (token.windowType != TYPE_INPUT_METHOD) {
1792 Log.w(TAG, "Attempted to add input method window with bad token "
1793 + attrs.token + ". Aborting.");
1794 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1795 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001796 } else if (attrs.type == TYPE_WALLPAPER) {
1797 if (token.windowType != TYPE_WALLPAPER) {
1798 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1799 + attrs.token + ". Aborting.");
1800 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803
1804 win = new WindowState(session, client, token,
1805 attachedWindow, attrs, viewVisibility);
1806 if (win.mDeathRecipient == null) {
1807 // Client has apparently died, so there is no reason to
1808 // continue.
1809 Log.w(TAG, "Adding window client " + client.asBinder()
1810 + " that is dead, aborting.");
1811 return WindowManagerImpl.ADD_APP_EXITING;
1812 }
1813
1814 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 res = mPolicy.prepareAddWindowLw(win, attrs);
1817 if (res != WindowManagerImpl.ADD_OKAY) {
1818 return res;
1819 }
1820
1821 // From now on, no exceptions or errors allowed!
1822
1823 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 if (addToken) {
1828 mTokenMap.put(attrs.token, token);
1829 mTokenList.add(token);
1830 }
1831 win.attach();
1832 mWindowMap.put(client.asBinder(), win);
1833
1834 if (attrs.type == TYPE_APPLICATION_STARTING &&
1835 token.appWindowToken != null) {
1836 token.appWindowToken.startingWindow = win;
1837 }
1838
1839 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 if (attrs.type == TYPE_INPUT_METHOD) {
1842 mInputMethodWindow = win;
1843 addInputMethodWindowToListLocked(win);
1844 imMayMove = false;
1845 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1846 mInputMethodDialogs.add(win);
1847 addWindowToListInOrderLocked(win, true);
1848 adjustInputMethodDialogsLocked();
1849 imMayMove = false;
1850 } else {
1851 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001852 if (attrs.type == TYPE_WALLPAPER) {
1853 mLastWallpaperTimeoutTime = 0;
1854 adjustWallpaperWindowsLocked();
1855 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001856 adjustWallpaperWindowsLocked();
1857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
Romain Guy06882f82009-06-10 13:36:04 -07001859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 if (mInTouchMode) {
1865 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1866 }
1867 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1868 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1869 }
Romain Guy06882f82009-06-10 13:36:04 -07001870
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001871 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001873 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1874 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 imMayMove = false;
1876 }
1877 }
Romain Guy06882f82009-06-10 13:36:04 -07001878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001880 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
Romain Guy06882f82009-06-10 13:36:04 -07001882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 assignLayersLocked();
1884 // Don't do layout here, the window must call
1885 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 //dump();
1888
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001889 if (focusChanged) {
1890 if (mCurrentFocus != null) {
1891 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1892 }
1893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 if (localLOGV) Log.v(
1895 TAG, "New client " + client.asBinder()
1896 + ": window=" + win);
1897 }
1898
1899 // sendNewConfiguration() checks caller permissions so we must call it with
1900 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1901 // identity anyway, so it's safe to just clear & restore around this whole
1902 // block.
1903 final long origId = Binder.clearCallingIdentity();
1904 if (reportNewConfig) {
1905 sendNewConfiguration();
1906 } else {
1907 // Update Orientation after adding a window, only if the window needs to be
1908 // displayed right away
1909 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001910 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 sendNewConfiguration();
1912 }
1913 }
1914 }
1915 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 return res;
1918 }
Romain Guy06882f82009-06-10 13:36:04 -07001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 public void removeWindow(Session session, IWindow client) {
1921 synchronized(mWindowMap) {
1922 WindowState win = windowForClientLocked(session, client);
1923 if (win == null) {
1924 return;
1925 }
1926 removeWindowLocked(session, win);
1927 }
1928 }
Romain Guy06882f82009-06-10 13:36:04 -07001929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 public void removeWindowLocked(Session session, WindowState win) {
1931
1932 if (localLOGV || DEBUG_FOCUS) Log.v(
1933 TAG, "Remove " + win + " client="
1934 + Integer.toHexString(System.identityHashCode(
1935 win.mClient.asBinder()))
1936 + ", surface=" + win.mSurface);
1937
1938 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 if (DEBUG_APP_TRANSITIONS) Log.v(
1941 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1942 + " mExiting=" + win.mExiting
1943 + " isAnimating=" + win.isAnimating()
1944 + " app-animation="
1945 + (win.mAppToken != null ? win.mAppToken.animation : null)
1946 + " inPendingTransaction="
1947 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1948 + " mDisplayFrozen=" + mDisplayFrozen);
1949 // Visibility of the removed window. Will be used later to update orientation later on.
1950 boolean wasVisible = false;
1951 // First, see if we need to run an animation. If we do, we have
1952 // to hold off on removing the window until the animation is done.
1953 // If the display is frozen, just remove immediately, since the
1954 // animation wouldn't be seen.
1955 if (win.mSurface != null && !mDisplayFrozen) {
1956 // If we are not currently running the exit animation, we
1957 // need to see about starting one.
1958 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1961 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1962 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1963 }
1964 // Try starting an animation.
1965 if (applyAnimationLocked(win, transit, false)) {
1966 win.mExiting = true;
1967 }
1968 }
1969 if (win.mExiting || win.isAnimating()) {
1970 // The exit animation is running... wait for it!
1971 //Log.i(TAG, "*** Running exit animation...");
1972 win.mExiting = true;
1973 win.mRemoveOnExit = true;
1974 mLayoutNeeded = true;
1975 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1976 performLayoutAndPlaceSurfacesLocked();
1977 if (win.mAppToken != null) {
1978 win.mAppToken.updateReportedVisibilityLocked();
1979 }
1980 //dump();
1981 Binder.restoreCallingIdentity(origId);
1982 return;
1983 }
1984 }
1985
1986 removeWindowInnerLocked(session, win);
1987 // Removing a visible window will effect the computed orientation
1988 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001989 if (wasVisible && computeForcedAppOrientationLocked()
1990 != mForcedAppOrientation) {
1991 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1994 Binder.restoreCallingIdentity(origId);
1995 }
Romain Guy06882f82009-06-10 13:36:04 -07001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001998 mKeyWaiter.finishedKey(session, win.mClient, true,
1999 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2001 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 if (mInputMethodTarget == win) {
2006 moveInputMethodWindowsIfNeededLocked(false);
2007 }
Romain Guy06882f82009-06-10 13:36:04 -07002008
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002009 if (false) {
2010 RuntimeException e = new RuntimeException("here");
2011 e.fillInStackTrace();
2012 Log.w(TAG, "Removing window " + win, e);
2013 }
2014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 mPolicy.removeWindowLw(win);
2016 win.removeLocked();
2017
2018 mWindowMap.remove(win.mClient.asBinder());
2019 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002020 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021
2022 if (mInputMethodWindow == win) {
2023 mInputMethodWindow = null;
2024 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2025 mInputMethodDialogs.remove(win);
2026 }
Romain Guy06882f82009-06-10 13:36:04 -07002027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 final WindowToken token = win.mToken;
2029 final AppWindowToken atoken = win.mAppToken;
2030 token.windows.remove(win);
2031 if (atoken != null) {
2032 atoken.allAppWindows.remove(win);
2033 }
2034 if (localLOGV) Log.v(
2035 TAG, "**** Removing window " + win + ": count="
2036 + token.windows.size());
2037 if (token.windows.size() == 0) {
2038 if (!token.explicit) {
2039 mTokenMap.remove(token.token);
2040 mTokenList.remove(token);
2041 } else if (atoken != null) {
2042 atoken.firstWindowDrawn = false;
2043 }
2044 }
2045
2046 if (atoken != null) {
2047 if (atoken.startingWindow == win) {
2048 atoken.startingWindow = null;
2049 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2050 // If this is the last window and we had requested a starting
2051 // transition window, well there is no point now.
2052 atoken.startingData = null;
2053 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2054 // If this is the last window except for a starting transition
2055 // window, we need to get rid of the starting transition.
2056 if (DEBUG_STARTING_WINDOW) {
2057 Log.v(TAG, "Schedule remove starting " + token
2058 + ": no more real windows");
2059 }
2060 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2061 mH.sendMessage(m);
2062 }
2063 }
Romain Guy06882f82009-06-10 13:36:04 -07002064
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002065 if (win.mAttrs.type == TYPE_WALLPAPER) {
2066 mLastWallpaperTimeoutTime = 0;
2067 adjustWallpaperWindowsLocked();
2068 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002069 adjustWallpaperWindowsLocked();
2070 }
2071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 if (!mInLayout) {
2073 assignLayersLocked();
2074 mLayoutNeeded = true;
2075 performLayoutAndPlaceSurfacesLocked();
2076 if (win.mAppToken != null) {
2077 win.mAppToken.updateReportedVisibilityLocked();
2078 }
2079 }
2080 }
2081
2082 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2083 long origId = Binder.clearCallingIdentity();
2084 try {
2085 synchronized (mWindowMap) {
2086 WindowState w = windowForClientLocked(session, client);
2087 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002088 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 Surface.openTransaction();
2090 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002091 if (SHOW_TRANSACTIONS) Log.i(
2092 TAG, " SURFACE " + w.mSurface
2093 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 w.mSurface.setTransparentRegionHint(region);
2095 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002096 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 Surface.closeTransaction();
2098 }
2099 }
2100 }
2101 } finally {
2102 Binder.restoreCallingIdentity(origId);
2103 }
2104 }
2105
2106 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002107 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 Rect visibleInsets) {
2109 long origId = Binder.clearCallingIdentity();
2110 try {
2111 synchronized (mWindowMap) {
2112 WindowState w = windowForClientLocked(session, client);
2113 if (w != null) {
2114 w.mGivenInsetsPending = false;
2115 w.mGivenContentInsets.set(contentInsets);
2116 w.mGivenVisibleInsets.set(visibleInsets);
2117 w.mTouchableInsets = touchableInsets;
2118 mLayoutNeeded = true;
2119 performLayoutAndPlaceSurfacesLocked();
2120 }
2121 }
2122 } finally {
2123 Binder.restoreCallingIdentity(origId);
2124 }
2125 }
Romain Guy06882f82009-06-10 13:36:04 -07002126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 public void getWindowDisplayFrame(Session session, IWindow client,
2128 Rect outDisplayFrame) {
2129 synchronized(mWindowMap) {
2130 WindowState win = windowForClientLocked(session, client);
2131 if (win == null) {
2132 outDisplayFrame.setEmpty();
2133 return;
2134 }
2135 outDisplayFrame.set(win.mDisplayFrame);
2136 }
2137 }
2138
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002139 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2140 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2141 window.mWallpaperX = x;
2142 window.mWallpaperY = y;
2143
2144 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002145 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002146 performLayoutAndPlaceSurfacesLocked();
2147 }
2148 }
2149 }
2150 }
2151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 public int relayoutWindow(Session session, IWindow client,
2153 WindowManager.LayoutParams attrs, int requestedWidth,
2154 int requestedHeight, int viewVisibility, boolean insetsPending,
2155 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2156 Surface outSurface) {
2157 boolean displayed = false;
2158 boolean inTouchMode;
2159 Configuration newConfig = null;
2160 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 synchronized(mWindowMap) {
2163 WindowState win = windowForClientLocked(session, client);
2164 if (win == null) {
2165 return 0;
2166 }
2167 win.mRequestedWidth = requestedWidth;
2168 win.mRequestedHeight = requestedHeight;
2169
2170 if (attrs != null) {
2171 mPolicy.adjustWindowParamsLw(attrs);
2172 }
Romain Guy06882f82009-06-10 13:36:04 -07002173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 int attrChanges = 0;
2175 int flagChanges = 0;
2176 if (attrs != null) {
2177 flagChanges = win.mAttrs.flags ^= attrs.flags;
2178 attrChanges = win.mAttrs.copyFrom(attrs);
2179 }
2180
2181 if (localLOGV) Log.v(
2182 TAG, "Relayout given client " + client.asBinder()
2183 + " (" + win.mAttrs.getTitle() + ")");
2184
2185
2186 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2187 win.mAlpha = attrs.alpha;
2188 }
2189
2190 final boolean scaledWindow =
2191 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2192
2193 if (scaledWindow) {
2194 // requested{Width|Height} Surface's physical size
2195 // attrs.{width|height} Size on screen
2196 win.mHScale = (attrs.width != requestedWidth) ?
2197 (attrs.width / (float)requestedWidth) : 1.0f;
2198 win.mVScale = (attrs.height != requestedHeight) ?
2199 (attrs.height / (float)requestedHeight) : 1.0f;
2200 }
2201
2202 boolean imMayMove = (flagChanges&(
2203 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2204 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 boolean focusMayChange = win.mViewVisibility != viewVisibility
2207 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2208 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002209
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002210 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2211 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 win.mRelayoutCalled = true;
2214 final int oldVisibility = win.mViewVisibility;
2215 win.mViewVisibility = viewVisibility;
2216 if (viewVisibility == View.VISIBLE &&
2217 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2218 displayed = !win.isVisibleLw();
2219 if (win.mExiting) {
2220 win.mExiting = false;
2221 win.mAnimation = null;
2222 }
2223 if (win.mDestroying) {
2224 win.mDestroying = false;
2225 mDestroySurface.remove(win);
2226 }
2227 if (oldVisibility == View.GONE) {
2228 win.mEnterAnimationPending = true;
2229 }
2230 if (displayed && win.mSurface != null && !win.mDrawPending
2231 && !win.mCommitDrawPending && !mDisplayFrozen) {
2232 applyEnterAnimationLocked(win);
2233 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002234 if (displayed && (win.mAttrs.flags
2235 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2236 win.mTurnOnScreen = true;
2237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2239 // To change the format, we need to re-build the surface.
2240 win.destroySurfaceLocked();
2241 displayed = true;
2242 }
2243 try {
2244 Surface surface = win.createSurfaceLocked();
2245 if (surface != null) {
2246 outSurface.copyFrom(surface);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002247 if (SHOW_TRANSACTIONS) Log.i(TAG,
2248 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002250 // For some reason there isn't a surface. Clear the
2251 // caller's object so they see the same state.
2252 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
2254 } catch (Exception e) {
2255 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002256 + client + " (" + win.mAttrs.getTitle() + ")",
2257 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 Binder.restoreCallingIdentity(origId);
2259 return 0;
2260 }
2261 if (displayed) {
2262 focusMayChange = true;
2263 }
2264 if (win.mAttrs.type == TYPE_INPUT_METHOD
2265 && mInputMethodWindow == null) {
2266 mInputMethodWindow = win;
2267 imMayMove = true;
2268 }
2269 } else {
2270 win.mEnterAnimationPending = false;
2271 if (win.mSurface != null) {
2272 // If we are not currently running the exit animation, we
2273 // need to see about starting one.
2274 if (!win.mExiting) {
2275 // Try starting an animation; if there isn't one, we
2276 // can destroy the surface right away.
2277 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2278 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2279 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2280 }
2281 if (win.isWinVisibleLw() &&
2282 applyAnimationLocked(win, transit, false)) {
2283 win.mExiting = true;
2284 mKeyWaiter.finishedKey(session, client, true,
2285 KeyWaiter.RETURN_NOTHING);
2286 } else if (win.isAnimating()) {
2287 // Currently in a hide animation... turn this into
2288 // an exit.
2289 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002290 } else if (win == mWallpaperTarget) {
2291 // If the wallpaper is currently behind this
2292 // window, we need to change both of them inside
2293 // of a transaction to avoid artifacts.
2294 win.mExiting = true;
2295 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 } else {
2297 if (mInputMethodWindow == win) {
2298 mInputMethodWindow = null;
2299 }
2300 win.destroySurfaceLocked();
2301 }
2302 }
2303 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002304 // We are being called from a local process, which
2305 // means outSurface holds its current surface. Ensure the
2306 // surface object is cleared, but we don't want it actually
2307 // destroyed at this point.
2308 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 }
2310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 if (focusMayChange) {
2312 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2313 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 imMayMove = false;
2315 }
2316 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2317 }
Romain Guy06882f82009-06-10 13:36:04 -07002318
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002319 // updateFocusedWindowLocked() already assigned layers so we only need to
2320 // reassign them at this point if the IM window state gets shuffled
2321 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 if (imMayMove) {
2324 if (moveInputMethodWindowsIfNeededLocked(false)) {
2325 assignLayers = true;
2326 }
2327 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002328 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002329 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002330 assignLayers = true;
2331 }
2332 }
Romain Guy06882f82009-06-10 13:36:04 -07002333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 mLayoutNeeded = true;
2335 win.mGivenInsetsPending = insetsPending;
2336 if (assignLayers) {
2337 assignLayersLocked();
2338 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002339 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002341 if (displayed && win.mIsWallpaper) {
2342 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002343 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 if (win.mAppToken != null) {
2346 win.mAppToken.updateReportedVisibilityLocked();
2347 }
2348 outFrame.set(win.mFrame);
2349 outContentInsets.set(win.mContentInsets);
2350 outVisibleInsets.set(win.mVisibleInsets);
2351 if (localLOGV) Log.v(
2352 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002353 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 + ", requestedHeight=" + requestedHeight
2355 + ", viewVisibility=" + viewVisibility
2356 + "\nRelayout returning frame=" + outFrame
2357 + ", surface=" + outSurface);
2358
2359 if (localLOGV || DEBUG_FOCUS) Log.v(
2360 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2361
2362 inTouchMode = mInTouchMode;
2363 }
2364
2365 if (newConfig != null) {
2366 sendNewConfiguration();
2367 }
Romain Guy06882f82009-06-10 13:36:04 -07002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2372 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2373 }
2374
2375 public void finishDrawingWindow(Session session, IWindow client) {
2376 final long origId = Binder.clearCallingIdentity();
2377 synchronized(mWindowMap) {
2378 WindowState win = windowForClientLocked(session, client);
2379 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002380 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2381 adjustWallpaperWindowsLocked();
2382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 mLayoutNeeded = true;
2384 performLayoutAndPlaceSurfacesLocked();
2385 }
2386 }
2387 Binder.restoreCallingIdentity(origId);
2388 }
2389
2390 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2391 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2392 + (lp != null ? lp.packageName : null)
2393 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2394 if (lp != null && lp.windowAnimations != 0) {
2395 // If this is a system resource, don't try to load it from the
2396 // application resources. It is nice to avoid loading application
2397 // resources if we can.
2398 String packageName = lp.packageName != null ? lp.packageName : "android";
2399 int resId = lp.windowAnimations;
2400 if ((resId&0xFF000000) == 0x01000000) {
2401 packageName = "android";
2402 }
2403 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2404 + packageName);
2405 return AttributeCache.instance().get(packageName, resId,
2406 com.android.internal.R.styleable.WindowAnimation);
2407 }
2408 return null;
2409 }
Romain Guy06882f82009-06-10 13:36:04 -07002410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 private void applyEnterAnimationLocked(WindowState win) {
2412 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2413 if (win.mEnterAnimationPending) {
2414 win.mEnterAnimationPending = false;
2415 transit = WindowManagerPolicy.TRANSIT_ENTER;
2416 }
2417
2418 applyAnimationLocked(win, transit, true);
2419 }
2420
2421 private boolean applyAnimationLocked(WindowState win,
2422 int transit, boolean isEntrance) {
2423 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2424 // If we are trying to apply an animation, but already running
2425 // an animation of the same type, then just leave that one alone.
2426 return true;
2427 }
Romain Guy06882f82009-06-10 13:36:04 -07002428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 // Only apply an animation if the display isn't frozen. If it is
2430 // frozen, there is no reason to animate and it can cause strange
2431 // artifacts when we unfreeze the display if some different animation
2432 // is running.
2433 if (!mDisplayFrozen) {
2434 int anim = mPolicy.selectAnimationLw(win, transit);
2435 int attr = -1;
2436 Animation a = null;
2437 if (anim != 0) {
2438 a = AnimationUtils.loadAnimation(mContext, anim);
2439 } else {
2440 switch (transit) {
2441 case WindowManagerPolicy.TRANSIT_ENTER:
2442 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2443 break;
2444 case WindowManagerPolicy.TRANSIT_EXIT:
2445 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2446 break;
2447 case WindowManagerPolicy.TRANSIT_SHOW:
2448 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2449 break;
2450 case WindowManagerPolicy.TRANSIT_HIDE:
2451 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2452 break;
2453 }
2454 if (attr >= 0) {
2455 a = loadAnimation(win.mAttrs, attr);
2456 }
2457 }
2458 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2459 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2460 + " mAnimation=" + win.mAnimation
2461 + " isEntrance=" + isEntrance);
2462 if (a != null) {
2463 if (DEBUG_ANIM) {
2464 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002465 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2467 }
2468 win.setAnimation(a);
2469 win.mAnimationIsEntrance = isEntrance;
2470 }
2471 } else {
2472 win.clearAnimation();
2473 }
2474
2475 return win.mAnimation != null;
2476 }
2477
2478 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2479 int anim = 0;
2480 Context context = mContext;
2481 if (animAttr >= 0) {
2482 AttributeCache.Entry ent = getCachedAnimations(lp);
2483 if (ent != null) {
2484 context = ent.context;
2485 anim = ent.array.getResourceId(animAttr, 0);
2486 }
2487 }
2488 if (anim != 0) {
2489 return AnimationUtils.loadAnimation(context, anim);
2490 }
2491 return null;
2492 }
Romain Guy06882f82009-06-10 13:36:04 -07002493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 private boolean applyAnimationLocked(AppWindowToken wtoken,
2495 WindowManager.LayoutParams lp, int transit, boolean enter) {
2496 // Only apply an animation if the display isn't frozen. If it is
2497 // frozen, there is no reason to animate and it can cause strange
2498 // artifacts when we unfreeze the display if some different animation
2499 // is running.
2500 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002501 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002502 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002503 a = new FadeInOutAnimation(enter);
2504 if (DEBUG_ANIM) Log.v(TAG,
2505 "applying FadeInOutAnimation for a window in compatibility mode");
2506 } else {
2507 int animAttr = 0;
2508 switch (transit) {
2509 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2510 animAttr = enter
2511 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2512 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2513 break;
2514 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2515 animAttr = enter
2516 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2517 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2518 break;
2519 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2520 animAttr = enter
2521 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2522 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2523 break;
2524 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2525 animAttr = enter
2526 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2527 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2528 break;
2529 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2530 animAttr = enter
2531 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2532 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2533 break;
2534 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2535 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002536 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002537 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2538 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002539 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002540 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002541 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2542 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002543 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002544 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002545 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002546 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2547 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2548 break;
2549 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2550 animAttr = enter
2551 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2552 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2553 break;
2554 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2555 animAttr = enter
2556 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2557 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002558 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002559 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002560 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002561 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2562 + " anim=" + a
2563 + " animAttr=0x" + Integer.toHexString(animAttr)
2564 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 if (a != null) {
2567 if (DEBUG_ANIM) {
2568 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002569 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2571 }
2572 wtoken.setAnimation(a);
2573 }
2574 } else {
2575 wtoken.clearAnimation();
2576 }
2577
2578 return wtoken.animation != null;
2579 }
2580
2581 // -------------------------------------------------------------
2582 // Application Window Tokens
2583 // -------------------------------------------------------------
2584
2585 public void validateAppTokens(List tokens) {
2586 int v = tokens.size()-1;
2587 int m = mAppTokens.size()-1;
2588 while (v >= 0 && m >= 0) {
2589 AppWindowToken wtoken = mAppTokens.get(m);
2590 if (wtoken.removed) {
2591 m--;
2592 continue;
2593 }
2594 if (tokens.get(v) != wtoken.token) {
2595 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2596 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2597 }
2598 v--;
2599 m--;
2600 }
2601 while (v >= 0) {
2602 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2603 v--;
2604 }
2605 while (m >= 0) {
2606 AppWindowToken wtoken = mAppTokens.get(m);
2607 if (!wtoken.removed) {
2608 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2609 }
2610 m--;
2611 }
2612 }
2613
2614 boolean checkCallingPermission(String permission, String func) {
2615 // Quick check: if the calling permission is me, it's all okay.
2616 if (Binder.getCallingPid() == Process.myPid()) {
2617 return true;
2618 }
Romain Guy06882f82009-06-10 13:36:04 -07002619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 if (mContext.checkCallingPermission(permission)
2621 == PackageManager.PERMISSION_GRANTED) {
2622 return true;
2623 }
2624 String msg = "Permission Denial: " + func + " from pid="
2625 + Binder.getCallingPid()
2626 + ", uid=" + Binder.getCallingUid()
2627 + " requires " + permission;
2628 Log.w(TAG, msg);
2629 return false;
2630 }
Romain Guy06882f82009-06-10 13:36:04 -07002631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 AppWindowToken findAppWindowToken(IBinder token) {
2633 WindowToken wtoken = mTokenMap.get(token);
2634 if (wtoken == null) {
2635 return null;
2636 }
2637 return wtoken.appWindowToken;
2638 }
Romain Guy06882f82009-06-10 13:36:04 -07002639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 public void addWindowToken(IBinder token, int type) {
2641 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2642 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002643 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 }
Romain Guy06882f82009-06-10 13:36:04 -07002645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 synchronized(mWindowMap) {
2647 WindowToken wtoken = mTokenMap.get(token);
2648 if (wtoken != null) {
2649 Log.w(TAG, "Attempted to add existing input method token: " + token);
2650 return;
2651 }
2652 wtoken = new WindowToken(token, type, true);
2653 mTokenMap.put(token, wtoken);
2654 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002655 if (type == TYPE_WALLPAPER) {
2656 mWallpaperTokens.add(wtoken);
2657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 }
2659 }
Romain Guy06882f82009-06-10 13:36:04 -07002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 public void removeWindowToken(IBinder token) {
2662 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2663 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002664 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
2666
2667 final long origId = Binder.clearCallingIdentity();
2668 synchronized(mWindowMap) {
2669 WindowToken wtoken = mTokenMap.remove(token);
2670 mTokenList.remove(wtoken);
2671 if (wtoken != null) {
2672 boolean delayed = false;
2673 if (!wtoken.hidden) {
2674 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 final int N = wtoken.windows.size();
2677 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 for (int i=0; i<N; i++) {
2680 WindowState win = wtoken.windows.get(i);
2681
2682 if (win.isAnimating()) {
2683 delayed = true;
2684 }
Romain Guy06882f82009-06-10 13:36:04 -07002685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 if (win.isVisibleNow()) {
2687 applyAnimationLocked(win,
2688 WindowManagerPolicy.TRANSIT_EXIT, false);
2689 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2690 KeyWaiter.RETURN_NOTHING);
2691 changed = true;
2692 }
2693 }
2694
2695 if (changed) {
2696 mLayoutNeeded = true;
2697 performLayoutAndPlaceSurfacesLocked();
2698 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2699 }
Romain Guy06882f82009-06-10 13:36:04 -07002700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 if (delayed) {
2702 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002703 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2704 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 }
2706 }
Romain Guy06882f82009-06-10 13:36:04 -07002707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 } else {
2709 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2710 }
2711 }
2712 Binder.restoreCallingIdentity(origId);
2713 }
2714
2715 public void addAppToken(int addPos, IApplicationToken token,
2716 int groupId, int requestedOrientation, boolean fullscreen) {
2717 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2718 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002719 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 }
Romain Guy06882f82009-06-10 13:36:04 -07002721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 synchronized(mWindowMap) {
2723 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2724 if (wtoken != null) {
2725 Log.w(TAG, "Attempted to add existing app token: " + token);
2726 return;
2727 }
2728 wtoken = new AppWindowToken(token);
2729 wtoken.groupId = groupId;
2730 wtoken.appFullscreen = fullscreen;
2731 wtoken.requestedOrientation = requestedOrientation;
2732 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002733 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 mTokenMap.put(token.asBinder(), wtoken);
2735 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 // Application tokens start out hidden.
2738 wtoken.hidden = true;
2739 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 //dump();
2742 }
2743 }
Romain Guy06882f82009-06-10 13:36:04 -07002744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 public void setAppGroupId(IBinder token, int groupId) {
2746 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2747 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002748 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
2750
2751 synchronized(mWindowMap) {
2752 AppWindowToken wtoken = findAppWindowToken(token);
2753 if (wtoken == null) {
2754 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2755 return;
2756 }
2757 wtoken.groupId = groupId;
2758 }
2759 }
Romain Guy06882f82009-06-10 13:36:04 -07002760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 public int getOrientationFromWindowsLocked() {
2762 int pos = mWindows.size() - 1;
2763 while (pos >= 0) {
2764 WindowState wtoken = (WindowState) mWindows.get(pos);
2765 pos--;
2766 if (wtoken.mAppToken != null) {
2767 // We hit an application window. so the orientation will be determined by the
2768 // app window. No point in continuing further.
2769 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2770 }
2771 if (!wtoken.isVisibleLw()) {
2772 continue;
2773 }
2774 int req = wtoken.mAttrs.screenOrientation;
2775 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2776 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2777 continue;
2778 } else {
2779 return req;
2780 }
2781 }
2782 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2783 }
Romain Guy06882f82009-06-10 13:36:04 -07002784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 public int getOrientationFromAppTokensLocked() {
2786 int pos = mAppTokens.size() - 1;
2787 int curGroup = 0;
2788 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002789 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002791 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 while (pos >= 0) {
2793 AppWindowToken wtoken = mAppTokens.get(pos);
2794 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002795 // if we're about to tear down this window and not seek for
2796 // the behind activity, don't use it for orientation
2797 if (!findingBehind
2798 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002799 continue;
2800 }
2801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 if (!haveGroup) {
2803 // We ignore any hidden applications on the top.
2804 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2805 continue;
2806 }
2807 haveGroup = true;
2808 curGroup = wtoken.groupId;
2809 lastOrientation = wtoken.requestedOrientation;
2810 } else if (curGroup != wtoken.groupId) {
2811 // If we have hit a new application group, and the bottom
2812 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002813 // the orientation behind it, and the last app was
2814 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002816 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2817 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 return lastOrientation;
2819 }
2820 }
2821 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002822 // If this application is fullscreen, and didn't explicitly say
2823 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002825 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002826 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002827 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 return or;
2829 }
2830 // If this application has requested an explicit orientation,
2831 // then use it.
2832 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2833 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2834 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2835 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2836 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2837 return or;
2838 }
Owen Lin3413b892009-05-01 17:12:32 -07002839 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 }
2841 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2842 }
Romain Guy06882f82009-06-10 13:36:04 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002845 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002846 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2847 "updateOrientationFromAppTokens()")) {
2848 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2849 }
2850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 Configuration config;
2852 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002853 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2854 freezeThisOneIfNeeded);
2855 Binder.restoreCallingIdentity(ident);
2856 return config;
2857 }
2858
2859 Configuration updateOrientationFromAppTokensUnchecked(
2860 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2861 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002863 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002864 if (config != null) {
2865 mLayoutNeeded = true;
2866 performLayoutAndPlaceSurfacesLocked();
2867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 return config;
2870 }
Romain Guy06882f82009-06-10 13:36:04 -07002871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 /*
2873 * The orientation is computed from non-application windows first. If none of
2874 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002875 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2877 * android.os.IBinder)
2878 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002879 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002880 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 long ident = Binder.clearCallingIdentity();
2883 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002884 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 if (req != mForcedAppOrientation) {
2887 changed = true;
2888 mForcedAppOrientation = req;
2889 //send a message to Policy indicating orientation change to take
2890 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002891 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 }
Romain Guy06882f82009-06-10 13:36:04 -07002893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 if (changed) {
2895 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002896 WindowManagerPolicy.USE_LAST_ROTATION,
2897 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 if (changed) {
2899 if (freezeThisOneIfNeeded != null) {
2900 AppWindowToken wtoken = findAppWindowToken(
2901 freezeThisOneIfNeeded);
2902 if (wtoken != null) {
2903 startAppFreezingScreenLocked(wtoken,
2904 ActivityInfo.CONFIG_ORIENTATION);
2905 }
2906 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002907 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 }
2909 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002910
2911 // No obvious action we need to take, but if our current
2912 // state mismatches the activity maanager's, update it
2913 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002914 mTempConfiguration.setToDefaults();
2915 if (computeNewConfigurationLocked(mTempConfiguration)) {
2916 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002917 return new Configuration(mTempConfiguration);
2918 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002919 }
2920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 } finally {
2922 Binder.restoreCallingIdentity(ident);
2923 }
Romain Guy06882f82009-06-10 13:36:04 -07002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 return null;
2926 }
Romain Guy06882f82009-06-10 13:36:04 -07002927
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002928 int computeForcedAppOrientationLocked() {
2929 int req = getOrientationFromWindowsLocked();
2930 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2931 req = getOrientationFromAppTokensLocked();
2932 }
2933 return req;
2934 }
Romain Guy06882f82009-06-10 13:36:04 -07002935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2937 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2938 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002939 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
Romain Guy06882f82009-06-10 13:36:04 -07002941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 synchronized(mWindowMap) {
2943 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2944 if (wtoken == null) {
2945 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2946 return;
2947 }
Romain Guy06882f82009-06-10 13:36:04 -07002948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 wtoken.requestedOrientation = requestedOrientation;
2950 }
2951 }
Romain Guy06882f82009-06-10 13:36:04 -07002952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 public int getAppOrientation(IApplicationToken token) {
2954 synchronized(mWindowMap) {
2955 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2956 if (wtoken == null) {
2957 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2958 }
Romain Guy06882f82009-06-10 13:36:04 -07002959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 return wtoken.requestedOrientation;
2961 }
2962 }
Romain Guy06882f82009-06-10 13:36:04 -07002963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2965 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2966 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002967 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 }
2969
2970 synchronized(mWindowMap) {
2971 boolean changed = false;
2972 if (token == null) {
2973 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2974 changed = mFocusedApp != null;
2975 mFocusedApp = null;
2976 mKeyWaiter.tickle();
2977 } else {
2978 AppWindowToken newFocus = findAppWindowToken(token);
2979 if (newFocus == null) {
2980 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2981 return;
2982 }
2983 changed = mFocusedApp != newFocus;
2984 mFocusedApp = newFocus;
2985 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2986 mKeyWaiter.tickle();
2987 }
2988
2989 if (moveFocusNow && changed) {
2990 final long origId = Binder.clearCallingIdentity();
2991 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2992 Binder.restoreCallingIdentity(origId);
2993 }
2994 }
2995 }
2996
2997 public void prepareAppTransition(int transit) {
2998 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2999 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003000 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 }
Romain Guy06882f82009-06-10 13:36:04 -07003002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 synchronized(mWindowMap) {
3004 if (DEBUG_APP_TRANSITIONS) Log.v(
3005 TAG, "Prepare app transition: transit=" + transit
3006 + " mNextAppTransition=" + mNextAppTransition);
3007 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003008 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3009 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003011 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3012 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3013 // Opening a new task always supersedes a close for the anim.
3014 mNextAppTransition = transit;
3015 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3016 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3017 // Opening a new activity always supersedes a close for the anim.
3018 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
3020 mAppTransitionReady = false;
3021 mAppTransitionTimeout = false;
3022 mStartingIconInTransition = false;
3023 mSkipAppTransitionAnimation = false;
3024 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3025 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3026 5000);
3027 }
3028 }
3029 }
3030
3031 public int getPendingAppTransition() {
3032 return mNextAppTransition;
3033 }
Romain Guy06882f82009-06-10 13:36:04 -07003034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 public void executeAppTransition() {
3036 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3037 "executeAppTransition()")) {
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 if (DEBUG_APP_TRANSITIONS) Log.v(
3043 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003044 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 mAppTransitionReady = true;
3046 final long origId = Binder.clearCallingIdentity();
3047 performLayoutAndPlaceSurfacesLocked();
3048 Binder.restoreCallingIdentity(origId);
3049 }
3050 }
3051 }
3052
3053 public void setAppStartingWindow(IBinder token, String pkg,
3054 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3055 IBinder transferFrom, boolean createIfNeeded) {
3056 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3057 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003058 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
3060
3061 synchronized(mWindowMap) {
3062 if (DEBUG_STARTING_WINDOW) Log.v(
3063 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3064 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 AppWindowToken wtoken = findAppWindowToken(token);
3067 if (wtoken == null) {
3068 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3069 return;
3070 }
3071
3072 // If the display is frozen, we won't do anything until the
3073 // actual window is displayed so there is no reason to put in
3074 // the starting window.
3075 if (mDisplayFrozen) {
3076 return;
3077 }
Romain Guy06882f82009-06-10 13:36:04 -07003078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 if (wtoken.startingData != null) {
3080 return;
3081 }
Romain Guy06882f82009-06-10 13:36:04 -07003082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 if (transferFrom != null) {
3084 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3085 if (ttoken != null) {
3086 WindowState startingWindow = ttoken.startingWindow;
3087 if (startingWindow != null) {
3088 if (mStartingIconInTransition) {
3089 // In this case, the starting icon has already
3090 // been displayed, so start letting windows get
3091 // shown immediately without any more transitions.
3092 mSkipAppTransitionAnimation = true;
3093 }
3094 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3095 "Moving existing starting from " + ttoken
3096 + " to " + wtoken);
3097 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 // Transfer the starting window over to the new
3100 // token.
3101 wtoken.startingData = ttoken.startingData;
3102 wtoken.startingView = ttoken.startingView;
3103 wtoken.startingWindow = startingWindow;
3104 ttoken.startingData = null;
3105 ttoken.startingView = null;
3106 ttoken.startingWindow = null;
3107 ttoken.startingMoved = true;
3108 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003109 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003111 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3112 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 mWindows.remove(startingWindow);
3114 ttoken.windows.remove(startingWindow);
3115 ttoken.allAppWindows.remove(startingWindow);
3116 addWindowToListInOrderLocked(startingWindow, true);
3117 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 // Propagate other interesting state between the
3120 // tokens. If the old token is displayed, we should
3121 // immediately force the new one to be displayed. If
3122 // it is animating, we need to move that animation to
3123 // the new one.
3124 if (ttoken.allDrawn) {
3125 wtoken.allDrawn = true;
3126 }
3127 if (ttoken.firstWindowDrawn) {
3128 wtoken.firstWindowDrawn = true;
3129 }
3130 if (!ttoken.hidden) {
3131 wtoken.hidden = false;
3132 wtoken.hiddenRequested = false;
3133 wtoken.willBeHidden = false;
3134 }
3135 if (wtoken.clientHidden != ttoken.clientHidden) {
3136 wtoken.clientHidden = ttoken.clientHidden;
3137 wtoken.sendAppVisibilityToClients();
3138 }
3139 if (ttoken.animation != null) {
3140 wtoken.animation = ttoken.animation;
3141 wtoken.animating = ttoken.animating;
3142 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3143 ttoken.animation = null;
3144 ttoken.animLayerAdjustment = 0;
3145 wtoken.updateLayers();
3146 ttoken.updateLayers();
3147 }
Romain Guy06882f82009-06-10 13:36:04 -07003148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 mLayoutNeeded = true;
3151 performLayoutAndPlaceSurfacesLocked();
3152 Binder.restoreCallingIdentity(origId);
3153 return;
3154 } else if (ttoken.startingData != null) {
3155 // The previous app was getting ready to show a
3156 // starting window, but hasn't yet done so. Steal it!
3157 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3158 "Moving pending starting from " + ttoken
3159 + " to " + wtoken);
3160 wtoken.startingData = ttoken.startingData;
3161 ttoken.startingData = null;
3162 ttoken.startingMoved = true;
3163 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3164 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3165 // want to process the message ASAP, before any other queued
3166 // messages.
3167 mH.sendMessageAtFrontOfQueue(m);
3168 return;
3169 }
3170 }
3171 }
3172
3173 // There is no existing starting window, and the caller doesn't
3174 // want us to create one, so that's it!
3175 if (!createIfNeeded) {
3176 return;
3177 }
Romain Guy06882f82009-06-10 13:36:04 -07003178
Dianne Hackborn284ac932009-08-28 10:34:25 -07003179 // If this is a translucent or wallpaper window, then don't
3180 // show a starting window -- the current effect (a full-screen
3181 // opaque starting window that fades away to the real contents
3182 // when it is ready) does not work for this.
3183 if (theme != 0) {
3184 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3185 com.android.internal.R.styleable.Window);
3186 if (ent.array.getBoolean(
3187 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3188 return;
3189 }
3190 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003191 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3192 return;
3193 }
3194 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003195 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3196 return;
3197 }
3198 }
3199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 mStartingIconInTransition = true;
3201 wtoken.startingData = new StartingData(
3202 pkg, theme, nonLocalizedLabel,
3203 labelRes, icon);
3204 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3205 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3206 // want to process the message ASAP, before any other queued
3207 // messages.
3208 mH.sendMessageAtFrontOfQueue(m);
3209 }
3210 }
3211
3212 public void setAppWillBeHidden(IBinder token) {
3213 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3214 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003215 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
3217
3218 AppWindowToken wtoken;
3219
3220 synchronized(mWindowMap) {
3221 wtoken = findAppWindowToken(token);
3222 if (wtoken == null) {
3223 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3224 return;
3225 }
3226 wtoken.willBeHidden = true;
3227 }
3228 }
Romain Guy06882f82009-06-10 13:36:04 -07003229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3231 boolean visible, int transit, boolean performLayout) {
3232 boolean delayed = false;
3233
3234 if (wtoken.clientHidden == visible) {
3235 wtoken.clientHidden = !visible;
3236 wtoken.sendAppVisibilityToClients();
3237 }
Romain Guy06882f82009-06-10 13:36:04 -07003238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 wtoken.willBeHidden = false;
3240 if (wtoken.hidden == visible) {
3241 final int N = wtoken.allAppWindows.size();
3242 boolean changed = false;
3243 if (DEBUG_APP_TRANSITIONS) Log.v(
3244 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3245 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003248
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003249 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 if (wtoken.animation == sDummyAnimation) {
3251 wtoken.animation = null;
3252 }
3253 applyAnimationLocked(wtoken, lp, transit, visible);
3254 changed = true;
3255 if (wtoken.animation != null) {
3256 delayed = runningAppAnimation = true;
3257 }
3258 }
Romain Guy06882f82009-06-10 13:36:04 -07003259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 for (int i=0; i<N; i++) {
3261 WindowState win = wtoken.allAppWindows.get(i);
3262 if (win == wtoken.startingWindow) {
3263 continue;
3264 }
3265
3266 if (win.isAnimating()) {
3267 delayed = true;
3268 }
Romain Guy06882f82009-06-10 13:36:04 -07003269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3271 //win.dump(" ");
3272 if (visible) {
3273 if (!win.isVisibleNow()) {
3274 if (!runningAppAnimation) {
3275 applyAnimationLocked(win,
3276 WindowManagerPolicy.TRANSIT_ENTER, true);
3277 }
3278 changed = true;
3279 }
3280 } else if (win.isVisibleNow()) {
3281 if (!runningAppAnimation) {
3282 applyAnimationLocked(win,
3283 WindowManagerPolicy.TRANSIT_EXIT, false);
3284 }
3285 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3286 KeyWaiter.RETURN_NOTHING);
3287 changed = true;
3288 }
3289 }
3290
3291 wtoken.hidden = wtoken.hiddenRequested = !visible;
3292 if (!visible) {
3293 unsetAppFreezingScreenLocked(wtoken, true, true);
3294 } else {
3295 // If we are being set visible, and the starting window is
3296 // not yet displayed, then make sure it doesn't get displayed.
3297 WindowState swin = wtoken.startingWindow;
3298 if (swin != null && (swin.mDrawPending
3299 || swin.mCommitDrawPending)) {
3300 swin.mPolicyVisibility = false;
3301 swin.mPolicyVisibilityAfterAnim = false;
3302 }
3303 }
Romain Guy06882f82009-06-10 13:36:04 -07003304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3306 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3307 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 if (changed && performLayout) {
3310 mLayoutNeeded = true;
3311 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 performLayoutAndPlaceSurfacesLocked();
3313 }
3314 }
3315
3316 if (wtoken.animation != null) {
3317 delayed = true;
3318 }
Romain Guy06882f82009-06-10 13:36:04 -07003319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 return delayed;
3321 }
3322
3323 public void setAppVisibility(IBinder token, boolean visible) {
3324 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3325 "setAppVisibility()")) {
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 visibility of non-existing app token: " + token);
3335 return;
3336 }
3337
3338 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3339 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003340 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3342 + "): mNextAppTransition=" + mNextAppTransition
3343 + " hidden=" + wtoken.hidden
3344 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3345 }
Romain Guy06882f82009-06-10 13:36:04 -07003346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 // If we are preparing an app transition, then delay changing
3348 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003349 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 // Already in requested state, don't do anything more.
3351 if (wtoken.hiddenRequested != visible) {
3352 return;
3353 }
3354 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 if (DEBUG_APP_TRANSITIONS) Log.v(
3357 TAG, "Setting dummy animation on: " + wtoken);
3358 wtoken.setDummyAnimation();
3359 mOpeningApps.remove(wtoken);
3360 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003361 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 wtoken.inPendingTransaction = true;
3363 if (visible) {
3364 mOpeningApps.add(wtoken);
3365 wtoken.allDrawn = false;
3366 wtoken.startingDisplayed = false;
3367 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003368 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 if (wtoken.clientHidden) {
3371 // In the case where we are making an app visible
3372 // but holding off for a transition, we still need
3373 // to tell the client to make its windows visible so
3374 // they get drawn. Otherwise, we will wait on
3375 // performing the transition until all windows have
3376 // been drawn, they never will be, and we are sad.
3377 wtoken.clientHidden = false;
3378 wtoken.sendAppVisibilityToClients();
3379 }
3380 } else {
3381 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003382 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 }
3384 return;
3385 }
Romain Guy06882f82009-06-10 13:36:04 -07003386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003388 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 wtoken.updateReportedVisibilityLocked();
3390 Binder.restoreCallingIdentity(origId);
3391 }
3392 }
3393
3394 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3395 boolean unfreezeSurfaceNow, boolean force) {
3396 if (wtoken.freezingScreen) {
3397 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3398 + " force=" + force);
3399 final int N = wtoken.allAppWindows.size();
3400 boolean unfrozeWindows = false;
3401 for (int i=0; i<N; i++) {
3402 WindowState w = wtoken.allAppWindows.get(i);
3403 if (w.mAppFreezing) {
3404 w.mAppFreezing = false;
3405 if (w.mSurface != null && !w.mOrientationChanging) {
3406 w.mOrientationChanging = true;
3407 }
3408 unfrozeWindows = true;
3409 }
3410 }
3411 if (force || unfrozeWindows) {
3412 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3413 wtoken.freezingScreen = false;
3414 mAppsFreezingScreen--;
3415 }
3416 if (unfreezeSurfaceNow) {
3417 if (unfrozeWindows) {
3418 mLayoutNeeded = true;
3419 performLayoutAndPlaceSurfacesLocked();
3420 }
3421 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3422 stopFreezingDisplayLocked();
3423 }
3424 }
3425 }
3426 }
Romain Guy06882f82009-06-10 13:36:04 -07003427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3429 int configChanges) {
3430 if (DEBUG_ORIENTATION) {
3431 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003432 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 Log.i(TAG, "Set freezing of " + wtoken.appToken
3434 + ": hidden=" + wtoken.hidden + " freezing="
3435 + wtoken.freezingScreen, e);
3436 }
3437 if (!wtoken.hiddenRequested) {
3438 if (!wtoken.freezingScreen) {
3439 wtoken.freezingScreen = true;
3440 mAppsFreezingScreen++;
3441 if (mAppsFreezingScreen == 1) {
3442 startFreezingDisplayLocked();
3443 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3444 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3445 5000);
3446 }
3447 }
3448 final int N = wtoken.allAppWindows.size();
3449 for (int i=0; i<N; i++) {
3450 WindowState w = wtoken.allAppWindows.get(i);
3451 w.mAppFreezing = true;
3452 }
3453 }
3454 }
Romain Guy06882f82009-06-10 13:36:04 -07003455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 public void startAppFreezingScreen(IBinder token, int configChanges) {
3457 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3458 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003459 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 }
3461
3462 synchronized(mWindowMap) {
3463 if (configChanges == 0 && !mDisplayFrozen) {
3464 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3465 return;
3466 }
Romain Guy06882f82009-06-10 13:36:04 -07003467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 AppWindowToken wtoken = findAppWindowToken(token);
3469 if (wtoken == null || wtoken.appToken == null) {
3470 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3471 return;
3472 }
3473 final long origId = Binder.clearCallingIdentity();
3474 startAppFreezingScreenLocked(wtoken, configChanges);
3475 Binder.restoreCallingIdentity(origId);
3476 }
3477 }
Romain Guy06882f82009-06-10 13:36:04 -07003478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 public void stopAppFreezingScreen(IBinder token, boolean force) {
3480 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3481 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003482 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 }
3484
3485 synchronized(mWindowMap) {
3486 AppWindowToken wtoken = findAppWindowToken(token);
3487 if (wtoken == null || wtoken.appToken == null) {
3488 return;
3489 }
3490 final long origId = Binder.clearCallingIdentity();
3491 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3492 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3493 unsetAppFreezingScreenLocked(wtoken, true, force);
3494 Binder.restoreCallingIdentity(origId);
3495 }
3496 }
Romain Guy06882f82009-06-10 13:36:04 -07003497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 public void removeAppToken(IBinder token) {
3499 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3500 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003501 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 }
3503
3504 AppWindowToken wtoken = null;
3505 AppWindowToken startingToken = null;
3506 boolean delayed = false;
3507
3508 final long origId = Binder.clearCallingIdentity();
3509 synchronized(mWindowMap) {
3510 WindowToken basewtoken = mTokenMap.remove(token);
3511 mTokenList.remove(basewtoken);
3512 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3513 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003514 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 wtoken.inPendingTransaction = false;
3516 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003517 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 if (mClosingApps.contains(wtoken)) {
3519 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003520 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003522 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 delayed = true;
3524 }
3525 if (DEBUG_APP_TRANSITIONS) Log.v(
3526 TAG, "Removing app " + wtoken + " delayed=" + delayed
3527 + " animation=" + wtoken.animation
3528 + " animating=" + wtoken.animating);
3529 if (delayed) {
3530 // set the token aside because it has an active animation to be finished
3531 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003532 } else {
3533 // Make sure there is no animation running on this token,
3534 // so any windows associated with it will be removed as
3535 // soon as their animations are complete
3536 wtoken.animation = null;
3537 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 }
3539 mAppTokens.remove(wtoken);
3540 wtoken.removed = true;
3541 if (wtoken.startingData != null) {
3542 startingToken = wtoken;
3543 }
3544 unsetAppFreezingScreenLocked(wtoken, true, true);
3545 if (mFocusedApp == wtoken) {
3546 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3547 mFocusedApp = null;
3548 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3549 mKeyWaiter.tickle();
3550 }
3551 } else {
3552 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3553 }
Romain Guy06882f82009-06-10 13:36:04 -07003554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 if (!delayed && wtoken != null) {
3556 wtoken.updateReportedVisibilityLocked();
3557 }
3558 }
3559 Binder.restoreCallingIdentity(origId);
3560
3561 if (startingToken != null) {
3562 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3563 + startingToken + ": app token removed");
3564 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3565 mH.sendMessage(m);
3566 }
3567 }
3568
3569 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3570 final int NW = token.windows.size();
3571 for (int i=0; i<NW; i++) {
3572 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003573 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 mWindows.remove(win);
3575 int j = win.mChildWindows.size();
3576 while (j > 0) {
3577 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003578 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3579 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3580 "Tmp removing child window " + cwin);
3581 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 }
3583 }
3584 return NW > 0;
3585 }
3586
3587 void dumpAppTokensLocked() {
3588 for (int i=mAppTokens.size()-1; i>=0; i--) {
3589 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3590 }
3591 }
Romain Guy06882f82009-06-10 13:36:04 -07003592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 void dumpWindowsLocked() {
3594 for (int i=mWindows.size()-1; i>=0; i--) {
3595 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3596 }
3597 }
Romain Guy06882f82009-06-10 13:36:04 -07003598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 private int findWindowOffsetLocked(int tokenPos) {
3600 final int NW = mWindows.size();
3601
3602 if (tokenPos >= mAppTokens.size()) {
3603 int i = NW;
3604 while (i > 0) {
3605 i--;
3606 WindowState win = (WindowState)mWindows.get(i);
3607 if (win.getAppToken() != null) {
3608 return i+1;
3609 }
3610 }
3611 }
3612
3613 while (tokenPos > 0) {
3614 // Find the first app token below the new position that has
3615 // a window displayed.
3616 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3617 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3618 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003619 if (wtoken.sendingToBottom) {
3620 if (DEBUG_REORDER) Log.v(TAG,
3621 "Skipping token -- currently sending to bottom");
3622 tokenPos--;
3623 continue;
3624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 int i = wtoken.windows.size();
3626 while (i > 0) {
3627 i--;
3628 WindowState win = wtoken.windows.get(i);
3629 int j = win.mChildWindows.size();
3630 while (j > 0) {
3631 j--;
3632 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003633 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 for (int pos=NW-1; pos>=0; pos--) {
3635 if (mWindows.get(pos) == cwin) {
3636 if (DEBUG_REORDER) Log.v(TAG,
3637 "Found child win @" + (pos+1));
3638 return pos+1;
3639 }
3640 }
3641 }
3642 }
3643 for (int pos=NW-1; pos>=0; pos--) {
3644 if (mWindows.get(pos) == win) {
3645 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3646 return pos+1;
3647 }
3648 }
3649 }
3650 tokenPos--;
3651 }
3652
3653 return 0;
3654 }
3655
3656 private final int reAddWindowLocked(int index, WindowState win) {
3657 final int NCW = win.mChildWindows.size();
3658 boolean added = false;
3659 for (int j=0; j<NCW; j++) {
3660 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3661 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003662 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3663 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 mWindows.add(index, win);
3665 index++;
3666 added = true;
3667 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003668 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3669 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 mWindows.add(index, cwin);
3671 index++;
3672 }
3673 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003674 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3675 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 mWindows.add(index, win);
3677 index++;
3678 }
3679 return index;
3680 }
Romain Guy06882f82009-06-10 13:36:04 -07003681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3683 final int NW = token.windows.size();
3684 for (int i=0; i<NW; i++) {
3685 index = reAddWindowLocked(index, token.windows.get(i));
3686 }
3687 return index;
3688 }
3689
3690 public void moveAppToken(int index, IBinder token) {
3691 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3692 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003693 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 }
3695
3696 synchronized(mWindowMap) {
3697 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3698 if (DEBUG_REORDER) dumpAppTokensLocked();
3699 final AppWindowToken wtoken = findAppWindowToken(token);
3700 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3701 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3702 + token + " (" + wtoken + ")");
3703 return;
3704 }
3705 mAppTokens.add(index, wtoken);
3706 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3707 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 final long origId = Binder.clearCallingIdentity();
3710 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3711 if (DEBUG_REORDER) dumpWindowsLocked();
3712 if (tmpRemoveAppWindowsLocked(wtoken)) {
3713 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3714 if (DEBUG_REORDER) dumpWindowsLocked();
3715 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3716 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3717 if (DEBUG_REORDER) dumpWindowsLocked();
3718 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 mLayoutNeeded = true;
3720 performLayoutAndPlaceSurfacesLocked();
3721 }
3722 Binder.restoreCallingIdentity(origId);
3723 }
3724 }
3725
3726 private void removeAppTokensLocked(List<IBinder> tokens) {
3727 // XXX This should be done more efficiently!
3728 // (take advantage of the fact that both lists should be
3729 // ordered in the same way.)
3730 int N = tokens.size();
3731 for (int i=0; i<N; i++) {
3732 IBinder token = tokens.get(i);
3733 final AppWindowToken wtoken = findAppWindowToken(token);
3734 if (!mAppTokens.remove(wtoken)) {
3735 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3736 + token + " (" + wtoken + ")");
3737 i--;
3738 N--;
3739 }
3740 }
3741 }
3742
Dianne Hackborna8f60182009-09-01 19:01:50 -07003743 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3744 boolean updateFocusAndLayout) {
3745 // First remove all of the windows from the list.
3746 tmpRemoveAppWindowsLocked(wtoken);
3747
3748 // Where to start adding?
3749 int pos = findWindowOffsetLocked(tokenPos);
3750
3751 // And now add them back at the correct place.
3752 pos = reAddAppWindowsLocked(pos, wtoken);
3753
3754 if (updateFocusAndLayout) {
3755 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3756 assignLayersLocked();
3757 }
3758 mLayoutNeeded = true;
3759 performLayoutAndPlaceSurfacesLocked();
3760 }
3761 }
3762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3764 // First remove all of the windows from the list.
3765 final int N = tokens.size();
3766 int i;
3767 for (i=0; i<N; i++) {
3768 WindowToken token = mTokenMap.get(tokens.get(i));
3769 if (token != null) {
3770 tmpRemoveAppWindowsLocked(token);
3771 }
3772 }
3773
3774 // Where to start adding?
3775 int pos = findWindowOffsetLocked(tokenPos);
3776
3777 // And now add them back at the correct place.
3778 for (i=0; i<N; i++) {
3779 WindowToken token = mTokenMap.get(tokens.get(i));
3780 if (token != null) {
3781 pos = reAddAppWindowsLocked(pos, token);
3782 }
3783 }
3784
Dianne Hackborna8f60182009-09-01 19:01:50 -07003785 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3786 assignLayersLocked();
3787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 mLayoutNeeded = true;
3789 performLayoutAndPlaceSurfacesLocked();
3790
3791 //dump();
3792 }
3793
3794 public void moveAppTokensToTop(List<IBinder> tokens) {
3795 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3796 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003797 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 }
3799
3800 final long origId = Binder.clearCallingIdentity();
3801 synchronized(mWindowMap) {
3802 removeAppTokensLocked(tokens);
3803 final int N = tokens.size();
3804 for (int i=0; i<N; i++) {
3805 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3806 if (wt != null) {
3807 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003808 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003809 mToTopApps.remove(wt);
3810 mToBottomApps.remove(wt);
3811 mToTopApps.add(wt);
3812 wt.sendingToBottom = false;
3813 wt.sendingToTop = true;
3814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 }
3816 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003817
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003818 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003819 moveAppWindowsLocked(tokens, mAppTokens.size());
3820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 }
3822 Binder.restoreCallingIdentity(origId);
3823 }
3824
3825 public void moveAppTokensToBottom(List<IBinder> tokens) {
3826 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3827 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003828 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 }
3830
3831 final long origId = Binder.clearCallingIdentity();
3832 synchronized(mWindowMap) {
3833 removeAppTokensLocked(tokens);
3834 final int N = tokens.size();
3835 int pos = 0;
3836 for (int i=0; i<N; i++) {
3837 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3838 if (wt != null) {
3839 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003840 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003841 mToTopApps.remove(wt);
3842 mToBottomApps.remove(wt);
3843 mToBottomApps.add(i, wt);
3844 wt.sendingToTop = false;
3845 wt.sendingToBottom = true;
3846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 pos++;
3848 }
3849 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003850
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003851 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003852 moveAppWindowsLocked(tokens, 0);
3853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 }
3855 Binder.restoreCallingIdentity(origId);
3856 }
3857
3858 // -------------------------------------------------------------
3859 // Misc IWindowSession methods
3860 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 public void disableKeyguard(IBinder token, String tag) {
3863 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3864 != PackageManager.PERMISSION_GRANTED) {
3865 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3866 }
3867 mKeyguardDisabled.acquire(token, tag);
3868 }
3869
3870 public void reenableKeyguard(IBinder token) {
3871 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3872 != PackageManager.PERMISSION_GRANTED) {
3873 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3874 }
3875 synchronized (mKeyguardDisabled) {
3876 mKeyguardDisabled.release(token);
3877
3878 if (!mKeyguardDisabled.isAcquired()) {
3879 // if we are the last one to reenable the keyguard wait until
3880 // we have actaully finished reenabling until returning
3881 mWaitingUntilKeyguardReenabled = true;
3882 while (mWaitingUntilKeyguardReenabled) {
3883 try {
3884 mKeyguardDisabled.wait();
3885 } catch (InterruptedException e) {
3886 Thread.currentThread().interrupt();
3887 }
3888 }
3889 }
3890 }
3891 }
3892
3893 /**
3894 * @see android.app.KeyguardManager#exitKeyguardSecurely
3895 */
3896 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3897 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3898 != PackageManager.PERMISSION_GRANTED) {
3899 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3900 }
3901 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3902 public void onKeyguardExitResult(boolean success) {
3903 try {
3904 callback.onKeyguardExitResult(success);
3905 } catch (RemoteException e) {
3906 // Client has died, we don't care.
3907 }
3908 }
3909 });
3910 }
3911
3912 public boolean inKeyguardRestrictedInputMode() {
3913 return mPolicy.inKeyguardRestrictedKeyInputMode();
3914 }
Romain Guy06882f82009-06-10 13:36:04 -07003915
Dianne Hackbornffa42482009-09-23 22:20:11 -07003916 public void closeSystemDialogs(String reason) {
3917 synchronized(mWindowMap) {
3918 for (int i=mWindows.size()-1; i>=0; i--) {
3919 WindowState w = (WindowState)mWindows.get(i);
3920 if (w.mSurface != null) {
3921 try {
3922 w.mClient.closeSystemDialogs(reason);
3923 } catch (RemoteException e) {
3924 }
3925 }
3926 }
3927 }
3928 }
3929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 static float fixScale(float scale) {
3931 if (scale < 0) scale = 0;
3932 else if (scale > 20) scale = 20;
3933 return Math.abs(scale);
3934 }
Romain Guy06882f82009-06-10 13:36:04 -07003935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 public void setAnimationScale(int which, float scale) {
3937 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3938 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003939 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 }
3941
3942 if (scale < 0) scale = 0;
3943 else if (scale > 20) scale = 20;
3944 scale = Math.abs(scale);
3945 switch (which) {
3946 case 0: mWindowAnimationScale = fixScale(scale); break;
3947 case 1: mTransitionAnimationScale = fixScale(scale); break;
3948 }
Romain Guy06882f82009-06-10 13:36:04 -07003949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 // Persist setting
3951 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3952 }
Romain Guy06882f82009-06-10 13:36:04 -07003953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 public void setAnimationScales(float[] scales) {
3955 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3956 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003957 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 }
3959
3960 if (scales != null) {
3961 if (scales.length >= 1) {
3962 mWindowAnimationScale = fixScale(scales[0]);
3963 }
3964 if (scales.length >= 2) {
3965 mTransitionAnimationScale = fixScale(scales[1]);
3966 }
3967 }
Romain Guy06882f82009-06-10 13:36:04 -07003968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 // Persist setting
3970 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3971 }
Romain Guy06882f82009-06-10 13:36:04 -07003972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 public float getAnimationScale(int which) {
3974 switch (which) {
3975 case 0: return mWindowAnimationScale;
3976 case 1: return mTransitionAnimationScale;
3977 }
3978 return 0;
3979 }
Romain Guy06882f82009-06-10 13:36:04 -07003980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 public float[] getAnimationScales() {
3982 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3983 }
Romain Guy06882f82009-06-10 13:36:04 -07003984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 public int getSwitchState(int sw) {
3986 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3987 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003988 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 }
3990 return KeyInputQueue.getSwitchState(sw);
3991 }
Romain Guy06882f82009-06-10 13:36:04 -07003992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 public int getSwitchStateForDevice(int devid, int sw) {
3994 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3995 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003996 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 }
3998 return KeyInputQueue.getSwitchState(devid, sw);
3999 }
Romain Guy06882f82009-06-10 13:36:04 -07004000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 public int getScancodeState(int sw) {
4002 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4003 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004004 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 }
4006 return KeyInputQueue.getScancodeState(sw);
4007 }
Romain Guy06882f82009-06-10 13:36:04 -07004008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 public int getScancodeStateForDevice(int devid, int sw) {
4010 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4011 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004012 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 }
4014 return KeyInputQueue.getScancodeState(devid, sw);
4015 }
Romain Guy06882f82009-06-10 13:36:04 -07004016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 public int getKeycodeState(int sw) {
4018 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4019 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004020 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 }
4022 return KeyInputQueue.getKeycodeState(sw);
4023 }
Romain Guy06882f82009-06-10 13:36:04 -07004024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 public int getKeycodeStateForDevice(int devid, int sw) {
4026 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4027 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004028 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 }
4030 return KeyInputQueue.getKeycodeState(devid, sw);
4031 }
Romain Guy06882f82009-06-10 13:36:04 -07004032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4034 return KeyInputQueue.hasKeys(keycodes, keyExists);
4035 }
Romain Guy06882f82009-06-10 13:36:04 -07004036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 public void enableScreenAfterBoot() {
4038 synchronized(mWindowMap) {
4039 if (mSystemBooted) {
4040 return;
4041 }
4042 mSystemBooted = true;
4043 }
Romain Guy06882f82009-06-10 13:36:04 -07004044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 performEnableScreen();
4046 }
Romain Guy06882f82009-06-10 13:36:04 -07004047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 public void enableScreenIfNeededLocked() {
4049 if (mDisplayEnabled) {
4050 return;
4051 }
4052 if (!mSystemBooted) {
4053 return;
4054 }
4055 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4056 }
Romain Guy06882f82009-06-10 13:36:04 -07004057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 public void performEnableScreen() {
4059 synchronized(mWindowMap) {
4060 if (mDisplayEnabled) {
4061 return;
4062 }
4063 if (!mSystemBooted) {
4064 return;
4065 }
Romain Guy06882f82009-06-10 13:36:04 -07004066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 // Don't enable the screen until all existing windows
4068 // have been drawn.
4069 final int N = mWindows.size();
4070 for (int i=0; i<N; i++) {
4071 WindowState w = (WindowState)mWindows.get(i);
4072 if (w.isVisibleLw() && !w.isDisplayedLw()) {
4073 return;
4074 }
4075 }
Romain Guy06882f82009-06-10 13:36:04 -07004076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 mDisplayEnabled = true;
4078 if (false) {
4079 Log.i(TAG, "ENABLING SCREEN!");
4080 StringWriter sw = new StringWriter();
4081 PrintWriter pw = new PrintWriter(sw);
4082 this.dump(null, pw, null);
4083 Log.i(TAG, sw.toString());
4084 }
4085 try {
4086 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4087 if (surfaceFlinger != null) {
4088 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4089 Parcel data = Parcel.obtain();
4090 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4091 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4092 data, null, 0);
4093 data.recycle();
4094 }
4095 } catch (RemoteException ex) {
4096 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4097 }
4098 }
Romain Guy06882f82009-06-10 13:36:04 -07004099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004103 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4104 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
Romain Guy06882f82009-06-10 13:36:04 -07004106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 public void setInTouchMode(boolean mode) {
4108 synchronized(mWindowMap) {
4109 mInTouchMode = mode;
4110 }
4111 }
4112
Romain Guy06882f82009-06-10 13:36:04 -07004113 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004114 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004116 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004117 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 }
4119
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004120 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 }
Romain Guy06882f82009-06-10 13:36:04 -07004122
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004123 public void setRotationUnchecked(int rotation,
4124 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 if(DEBUG_ORIENTATION) Log.v(TAG,
4126 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 long origId = Binder.clearCallingIdentity();
4129 boolean changed;
4130 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004131 changed = setRotationUncheckedLocked(rotation, animFlags);
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 if (changed) {
4135 sendNewConfiguration();
4136 synchronized(mWindowMap) {
4137 mLayoutNeeded = true;
4138 performLayoutAndPlaceSurfacesLocked();
4139 }
4140 } else if (alwaysSendConfiguration) {
4141 //update configuration ignoring orientation change
4142 sendNewConfiguration();
4143 }
Romain Guy06882f82009-06-10 13:36:04 -07004144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 Binder.restoreCallingIdentity(origId);
4146 }
Romain Guy06882f82009-06-10 13:36:04 -07004147
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004148 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 boolean changed;
4150 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4151 rotation = mRequestedRotation;
4152 } else {
4153 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004154 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 }
4156 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004157 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 mRotation, mDisplayEnabled);
4159 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4160 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004163 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 "Rotation changed to " + rotation
4165 + " from " + mRotation
4166 + " (forceApp=" + mForcedAppOrientation
4167 + ", req=" + mRequestedRotation + ")");
4168 mRotation = rotation;
4169 mWindowsFreezingScreen = true;
4170 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4171 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4172 2000);
4173 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004174 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 mQueue.setOrientation(rotation);
4176 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004177 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 }
4179 for (int i=mWindows.size()-1; i>=0; i--) {
4180 WindowState w = (WindowState)mWindows.get(i);
4181 if (w.mSurface != null) {
4182 w.mOrientationChanging = true;
4183 }
4184 }
4185 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4186 try {
4187 mRotationWatchers.get(i).onRotationChanged(rotation);
4188 } catch (RemoteException e) {
4189 }
4190 }
4191 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 return changed;
4194 }
Romain Guy06882f82009-06-10 13:36:04 -07004195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 public int getRotation() {
4197 return mRotation;
4198 }
4199
4200 public int watchRotation(IRotationWatcher watcher) {
4201 final IBinder watcherBinder = watcher.asBinder();
4202 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4203 public void binderDied() {
4204 synchronized (mWindowMap) {
4205 for (int i=0; i<mRotationWatchers.size(); i++) {
4206 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004207 IRotationWatcher removed = mRotationWatchers.remove(i);
4208 if (removed != null) {
4209 removed.asBinder().unlinkToDeath(this, 0);
4210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 i--;
4212 }
4213 }
4214 }
4215 }
4216 };
Romain Guy06882f82009-06-10 13:36:04 -07004217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 synchronized (mWindowMap) {
4219 try {
4220 watcher.asBinder().linkToDeath(dr, 0);
4221 mRotationWatchers.add(watcher);
4222 } catch (RemoteException e) {
4223 // Client died, no cleanup needed.
4224 }
Romain Guy06882f82009-06-10 13:36:04 -07004225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 return mRotation;
4227 }
4228 }
4229
4230 /**
4231 * Starts the view server on the specified port.
4232 *
4233 * @param port The port to listener to.
4234 *
4235 * @return True if the server was successfully started, false otherwise.
4236 *
4237 * @see com.android.server.ViewServer
4238 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4239 */
4240 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004241 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 return false;
4243 }
4244
4245 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4246 return false;
4247 }
4248
4249 if (port < 1024) {
4250 return false;
4251 }
4252
4253 if (mViewServer != null) {
4254 if (!mViewServer.isRunning()) {
4255 try {
4256 return mViewServer.start();
4257 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004258 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
4260 }
4261 return false;
4262 }
4263
4264 try {
4265 mViewServer = new ViewServer(this, port);
4266 return mViewServer.start();
4267 } catch (IOException e) {
4268 Log.w(TAG, "View server did not start");
4269 }
4270 return false;
4271 }
4272
Romain Guy06882f82009-06-10 13:36:04 -07004273 private boolean isSystemSecure() {
4274 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4275 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4276 }
4277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 /**
4279 * Stops the view server if it exists.
4280 *
4281 * @return True if the server stopped, false if it wasn't started or
4282 * couldn't be stopped.
4283 *
4284 * @see com.android.server.ViewServer
4285 */
4286 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004287 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 return false;
4289 }
4290
4291 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4292 return false;
4293 }
4294
4295 if (mViewServer != null) {
4296 return mViewServer.stop();
4297 }
4298 return false;
4299 }
4300
4301 /**
4302 * Indicates whether the view server is running.
4303 *
4304 * @return True if the server is running, false otherwise.
4305 *
4306 * @see com.android.server.ViewServer
4307 */
4308 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004309 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 return false;
4311 }
4312
4313 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4314 return false;
4315 }
4316
4317 return mViewServer != null && mViewServer.isRunning();
4318 }
4319
4320 /**
4321 * Lists all availble windows in the system. The listing is written in the
4322 * specified Socket's output stream with the following syntax:
4323 * windowHashCodeInHexadecimal windowName
4324 * Each line of the ouput represents a different window.
4325 *
4326 * @param client The remote client to send the listing to.
4327 * @return False if an error occured, true otherwise.
4328 */
4329 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004330 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 return false;
4332 }
4333
4334 boolean result = true;
4335
4336 Object[] windows;
4337 synchronized (mWindowMap) {
4338 windows = new Object[mWindows.size()];
4339 //noinspection unchecked
4340 windows = mWindows.toArray(windows);
4341 }
4342
4343 BufferedWriter out = null;
4344
4345 // Any uncaught exception will crash the system process
4346 try {
4347 OutputStream clientStream = client.getOutputStream();
4348 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4349
4350 final int count = windows.length;
4351 for (int i = 0; i < count; i++) {
4352 final WindowState w = (WindowState) windows[i];
4353 out.write(Integer.toHexString(System.identityHashCode(w)));
4354 out.write(' ');
4355 out.append(w.mAttrs.getTitle());
4356 out.write('\n');
4357 }
4358
4359 out.write("DONE.\n");
4360 out.flush();
4361 } catch (Exception e) {
4362 result = false;
4363 } finally {
4364 if (out != null) {
4365 try {
4366 out.close();
4367 } catch (IOException e) {
4368 result = false;
4369 }
4370 }
4371 }
4372
4373 return result;
4374 }
4375
4376 /**
4377 * Sends a command to a target window. The result of the command, if any, will be
4378 * written in the output stream of the specified socket.
4379 *
4380 * The parameters must follow this syntax:
4381 * windowHashcode extra
4382 *
4383 * Where XX is the length in characeters of the windowTitle.
4384 *
4385 * The first parameter is the target window. The window with the specified hashcode
4386 * will be the target. If no target can be found, nothing happens. The extra parameters
4387 * will be delivered to the target window and as parameters to the command itself.
4388 *
4389 * @param client The remote client to sent the result, if any, to.
4390 * @param command The command to execute.
4391 * @param parameters The command parameters.
4392 *
4393 * @return True if the command was successfully delivered, false otherwise. This does
4394 * not indicate whether the command itself was successful.
4395 */
4396 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004397 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 return false;
4399 }
4400
4401 boolean success = true;
4402 Parcel data = null;
4403 Parcel reply = null;
4404
4405 // Any uncaught exception will crash the system process
4406 try {
4407 // Find the hashcode of the window
4408 int index = parameters.indexOf(' ');
4409 if (index == -1) {
4410 index = parameters.length();
4411 }
4412 final String code = parameters.substring(0, index);
4413 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4414
4415 // Extract the command's parameter after the window description
4416 if (index < parameters.length()) {
4417 parameters = parameters.substring(index + 1);
4418 } else {
4419 parameters = "";
4420 }
4421
4422 final WindowManagerService.WindowState window = findWindow(hashCode);
4423 if (window == null) {
4424 return false;
4425 }
4426
4427 data = Parcel.obtain();
4428 data.writeInterfaceToken("android.view.IWindow");
4429 data.writeString(command);
4430 data.writeString(parameters);
4431 data.writeInt(1);
4432 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4433
4434 reply = Parcel.obtain();
4435
4436 final IBinder binder = window.mClient.asBinder();
4437 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4438 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4439
4440 reply.readException();
4441
4442 } catch (Exception e) {
4443 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4444 success = false;
4445 } finally {
4446 if (data != null) {
4447 data.recycle();
4448 }
4449 if (reply != null) {
4450 reply.recycle();
4451 }
4452 }
4453
4454 return success;
4455 }
4456
4457 private WindowState findWindow(int hashCode) {
4458 if (hashCode == -1) {
4459 return getFocusedWindow();
4460 }
4461
4462 synchronized (mWindowMap) {
4463 final ArrayList windows = mWindows;
4464 final int count = windows.size();
4465
4466 for (int i = 0; i < count; i++) {
4467 WindowState w = (WindowState) windows.get(i);
4468 if (System.identityHashCode(w) == hashCode) {
4469 return w;
4470 }
4471 }
4472 }
4473
4474 return null;
4475 }
4476
4477 /*
4478 * Instruct the Activity Manager to fetch the current configuration and broadcast
4479 * that to config-changed listeners if appropriate.
4480 */
4481 void sendNewConfiguration() {
4482 try {
4483 mActivityManager.updateConfiguration(null);
4484 } catch (RemoteException e) {
4485 }
4486 }
Romain Guy06882f82009-06-10 13:36:04 -07004487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 public Configuration computeNewConfiguration() {
4489 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004490 return computeNewConfigurationLocked();
4491 }
4492 }
Romain Guy06882f82009-06-10 13:36:04 -07004493
Dianne Hackbornc485a602009-03-24 22:39:49 -07004494 Configuration computeNewConfigurationLocked() {
4495 Configuration config = new Configuration();
4496 if (!computeNewConfigurationLocked(config)) {
4497 return null;
4498 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004499 return config;
4500 }
Romain Guy06882f82009-06-10 13:36:04 -07004501
Dianne Hackbornc485a602009-03-24 22:39:49 -07004502 boolean computeNewConfigurationLocked(Configuration config) {
4503 if (mDisplay == null) {
4504 return false;
4505 }
4506 mQueue.getInputConfiguration(config);
4507 final int dw = mDisplay.getWidth();
4508 final int dh = mDisplay.getHeight();
4509 int orientation = Configuration.ORIENTATION_SQUARE;
4510 if (dw < dh) {
4511 orientation = Configuration.ORIENTATION_PORTRAIT;
4512 } else if (dw > dh) {
4513 orientation = Configuration.ORIENTATION_LANDSCAPE;
4514 }
4515 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004516
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004517 DisplayMetrics dm = new DisplayMetrics();
4518 mDisplay.getMetrics(dm);
4519 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4520
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004521 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004522 // Note we only do this once because at this point we don't
4523 // expect the screen to change in this way at runtime, and want
4524 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004525 int longSize = dw;
4526 int shortSize = dh;
4527 if (longSize < shortSize) {
4528 int tmp = longSize;
4529 longSize = shortSize;
4530 shortSize = tmp;
4531 }
4532 longSize = (int)(longSize/dm.density);
4533 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004534
Dianne Hackborn723738c2009-06-25 19:48:04 -07004535 // These semi-magic numbers define our compatibility modes for
4536 // applications with different screens. Don't change unless you
4537 // make sure to test lots and lots of apps!
4538 if (longSize < 470) {
4539 // This is shorter than an HVGA normal density screen (which
4540 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004541 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4542 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004543 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004544 // Is this a large screen?
4545 if (longSize > 640 && shortSize >= 480) {
4546 // VGA or larger screens at medium density are the point
4547 // at which we consider it to be a large screen.
4548 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4549 } else {
4550 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4551
4552 // If this screen is wider than normal HVGA, or taller
4553 // than FWVGA, then for old apps we want to run in size
4554 // compatibility mode.
4555 if (shortSize > 321 || longSize > 570) {
4556 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4557 }
4558 }
4559
4560 // Is this a long screen?
4561 if (((longSize*3)/5) >= (shortSize-1)) {
4562 // Anything wider than WVGA (5:3) is considering to be long.
4563 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4564 } else {
4565 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4566 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004567 }
4568 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004569 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004570
Dianne Hackbornc485a602009-03-24 22:39:49 -07004571 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4572 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4573 mPolicy.adjustConfigurationLw(config);
4574 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004575 }
Romain Guy06882f82009-06-10 13:36:04 -07004576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 // -------------------------------------------------------------
4578 // Input Events and Focus Management
4579 // -------------------------------------------------------------
4580
4581 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004582 long curTime = SystemClock.uptimeMillis();
4583
Michael Chane10de972009-05-18 11:24:50 -07004584 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004585 if (mLastTouchEventType == eventType &&
4586 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4587 return;
4588 }
4589 mLastUserActivityCallTime = curTime;
4590 mLastTouchEventType = eventType;
4591 }
4592
4593 if (targetWin == null
4594 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4595 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 }
4597 }
4598
4599 // tells if it's a cheek event or not -- this function is stateful
4600 private static final int EVENT_NONE = 0;
4601 private static final int EVENT_UNKNOWN = 0;
4602 private static final int EVENT_CHEEK = 0;
4603 private static final int EVENT_IGNORE_DURATION = 300; // ms
4604 private static final float CHEEK_THRESHOLD = 0.6f;
4605 private int mEventState = EVENT_NONE;
4606 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 private int eventType(MotionEvent ev) {
4609 float size = ev.getSize();
4610 switch (ev.getAction()) {
4611 case MotionEvent.ACTION_DOWN:
4612 mEventSize = size;
4613 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4614 case MotionEvent.ACTION_UP:
4615 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004616 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 case MotionEvent.ACTION_MOVE:
4618 final int N = ev.getHistorySize();
4619 if (size > mEventSize) mEventSize = size;
4620 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4621 for (int i=0; i<N; i++) {
4622 size = ev.getHistoricalSize(i);
4623 if (size > mEventSize) mEventSize = size;
4624 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4625 }
4626 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4627 return TOUCH_EVENT;
4628 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004629 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 }
4631 default:
4632 // not good
4633 return OTHER_EVENT;
4634 }
4635 }
4636
4637 /**
4638 * @return Returns true if event was dispatched, false if it was dropped for any reason
4639 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004640 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4642 "dispatchPointer " + ev);
4643
Michael Chan53071d62009-05-13 17:29:48 -07004644 if (MEASURE_LATENCY) {
4645 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4646 }
4647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004649 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650
Michael Chan53071d62009-05-13 17:29:48 -07004651 if (MEASURE_LATENCY) {
4652 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4653 }
4654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 if (action == MotionEvent.ACTION_UP) {
4658 // let go of our target
4659 mKeyWaiter.mMotionTarget = null;
4660 mPowerManager.logPointerUpEvent();
4661 } else if (action == MotionEvent.ACTION_DOWN) {
4662 mPowerManager.logPointerDownEvent();
4663 }
4664
4665 if (targetObj == null) {
4666 // In this case we are either dropping the event, or have received
4667 // a move or up without a down. It is common to receive move
4668 // events in such a way, since this means the user is moving the
4669 // pointer without actually pressing down. All other cases should
4670 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004671 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4673 }
4674 if (qev != null) {
4675 mQueue.recycleEvent(qev);
4676 }
4677 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004678 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004679 }
4680 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4681 if (qev != null) {
4682 mQueue.recycleEvent(qev);
4683 }
4684 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004685 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 }
Romain Guy06882f82009-06-10 13:36:04 -07004687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004691 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692
4693 //Log.i(TAG, "Sending " + ev + " to " + target);
4694
4695 if (uid != 0 && uid != target.mSession.mUid) {
4696 if (mContext.checkPermission(
4697 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4698 != PackageManager.PERMISSION_GRANTED) {
4699 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4700 + pid + " uid " + uid + " to window " + target
4701 + " owned by uid " + target.mSession.mUid);
4702 if (qev != null) {
4703 mQueue.recycleEvent(qev);
4704 }
4705 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004706 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 }
4708 }
4709
Michael Chan53071d62009-05-13 17:29:48 -07004710 if (MEASURE_LATENCY) {
4711 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4712 }
4713
Romain Guy06882f82009-06-10 13:36:04 -07004714 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4716 //target wants to ignore fat touch events
4717 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4718 //explicit flag to return without processing event further
4719 boolean returnFlag = false;
4720 if((action == MotionEvent.ACTION_DOWN)) {
4721 mFatTouch = false;
4722 if(cheekPress) {
4723 mFatTouch = true;
4724 returnFlag = true;
4725 }
4726 } else {
4727 if(action == MotionEvent.ACTION_UP) {
4728 if(mFatTouch) {
4729 //earlier even was invalid doesnt matter if current up is cheekpress or not
4730 mFatTouch = false;
4731 returnFlag = true;
4732 } else if(cheekPress) {
4733 //cancel the earlier event
4734 ev.setAction(MotionEvent.ACTION_CANCEL);
4735 action = MotionEvent.ACTION_CANCEL;
4736 }
4737 } else if(action == MotionEvent.ACTION_MOVE) {
4738 if(mFatTouch) {
4739 //two cases here
4740 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004741 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 returnFlag = true;
4743 } else if(cheekPress) {
4744 //valid down followed by invalid moves
4745 //an invalid move have to cancel earlier action
4746 ev.setAction(MotionEvent.ACTION_CANCEL);
4747 action = MotionEvent.ACTION_CANCEL;
4748 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4749 //note that the subsequent invalid moves will not get here
4750 mFatTouch = true;
4751 }
4752 }
4753 } //else if action
4754 if(returnFlag) {
4755 //recycle que, ev
4756 if (qev != null) {
4757 mQueue.recycleEvent(qev);
4758 }
4759 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004760 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 }
4762 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004763
Michael Chan9f028e62009-08-04 17:37:46 -07004764 // Enable this for testing the "right" value
4765 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004766 int max_events_per_sec = 35;
4767 try {
4768 max_events_per_sec = Integer.parseInt(SystemProperties
4769 .get("windowsmgr.max_events_per_sec"));
4770 if (max_events_per_sec < 1) {
4771 max_events_per_sec = 35;
4772 }
4773 } catch (NumberFormatException e) {
4774 }
4775 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4776 }
4777
4778 /*
4779 * Throttle events to minimize CPU usage when there's a flood of events
4780 * e.g. constant contact with the screen
4781 */
4782 if (action == MotionEvent.ACTION_MOVE) {
4783 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4784 long now = SystemClock.uptimeMillis();
4785 if (now < nextEventTime) {
4786 try {
4787 Thread.sleep(nextEventTime - now);
4788 } catch (InterruptedException e) {
4789 }
4790 mLastTouchEventTime = nextEventTime;
4791 } else {
4792 mLastTouchEventTime = now;
4793 }
4794 }
4795
Michael Chan53071d62009-05-13 17:29:48 -07004796 if (MEASURE_LATENCY) {
4797 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4798 }
4799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 synchronized(mWindowMap) {
4801 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4802 mKeyWaiter.bindTargetWindowLocked(target,
4803 KeyWaiter.RETURN_PENDING_POINTER, qev);
4804 ev = null;
4805 } else {
4806 if (action == MotionEvent.ACTION_DOWN) {
4807 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4808 if (out != null) {
4809 MotionEvent oev = MotionEvent.obtain(ev);
4810 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4811 do {
4812 final Rect frame = out.mFrame;
4813 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4814 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004815 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 } catch (android.os.RemoteException e) {
4817 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4818 }
4819 oev.offsetLocation((float)frame.left, (float)frame.top);
4820 out = out.mNextOutsideTouch;
4821 } while (out != null);
4822 mKeyWaiter.mOutsideTouchTargets = null;
4823 }
4824 }
4825 final Rect frame = target.mFrame;
4826 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4827 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004828
4829 // If we are on top of the wallpaper, then the wallpaper also
4830 // gets to see this movement.
4831 if (mWallpaperTarget == target) {
4832 sendPointerToWallpaperLocked(target, ev, eventTime);
4833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 }
4835 }
Romain Guy06882f82009-06-10 13:36:04 -07004836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 // finally offset the event to the target's coordinate system and
4838 // dispatch the event.
4839 try {
4840 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4841 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4842 }
Michael Chan53071d62009-05-13 17:29:48 -07004843
4844 if (MEASURE_LATENCY) {
4845 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4846 }
4847
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004848 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004849
4850 if (MEASURE_LATENCY) {
4851 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4852 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004853 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 } catch (android.os.RemoteException e) {
4855 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4856 mKeyWaiter.mMotionTarget = null;
4857 try {
4858 removeWindow(target.mSession, target.mClient);
4859 } catch (java.util.NoSuchElementException ex) {
4860 // This will happen if the window has already been
4861 // removed.
4862 }
4863 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004864 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 }
Romain Guy06882f82009-06-10 13:36:04 -07004866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 /**
4868 * @return Returns true if event was dispatched, false if it was dropped for any reason
4869 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004870 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 if (DEBUG_INPUT) Log.v(
4872 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004875 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 if (focusObj == null) {
4877 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4878 if (qev != null) {
4879 mQueue.recycleEvent(qev);
4880 }
4881 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004882 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 }
4884 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4885 if (qev != null) {
4886 mQueue.recycleEvent(qev);
4887 }
4888 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004889 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 }
Romain Guy06882f82009-06-10 13:36:04 -07004891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 if (uid != 0 && uid != focus.mSession.mUid) {
4895 if (mContext.checkPermission(
4896 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4897 != PackageManager.PERMISSION_GRANTED) {
4898 Log.w(TAG, "Permission denied: injecting key event from pid "
4899 + pid + " uid " + uid + " to window " + focus
4900 + " owned by uid " + focus.mSession.mUid);
4901 if (qev != null) {
4902 mQueue.recycleEvent(qev);
4903 }
4904 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004905 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 }
4907 }
Romain Guy06882f82009-06-10 13:36:04 -07004908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 synchronized(mWindowMap) {
4912 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4913 mKeyWaiter.bindTargetWindowLocked(focus,
4914 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4915 // We don't deliver movement events to the client, we hold
4916 // them and wait for them to call back.
4917 ev = null;
4918 } else {
4919 mKeyWaiter.bindTargetWindowLocked(focus);
4920 }
4921 }
Romain Guy06882f82009-06-10 13:36:04 -07004922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004924 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004925 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 } catch (android.os.RemoteException e) {
4927 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4928 try {
4929 removeWindow(focus.mSession, focus.mClient);
4930 } catch (java.util.NoSuchElementException ex) {
4931 // This will happen if the window has already been
4932 // removed.
4933 }
4934 }
Romain Guy06882f82009-06-10 13:36:04 -07004935
Dianne Hackborncfaef692009-06-15 14:24:44 -07004936 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 }
Romain Guy06882f82009-06-10 13:36:04 -07004938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 /**
4940 * @return Returns true if event was dispatched, false if it was dropped for any reason
4941 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004942 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4944
4945 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004946 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 if (focusObj == null) {
4948 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004949 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004950 }
4951 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004952 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 }
Romain Guy06882f82009-06-10 13:36:04 -07004954
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004955 // Okay we have finished waiting for the last event to be processed.
4956 // First off, if this is a repeat event, check to see if there is
4957 // a corresponding up event in the queue. If there is, we will
4958 // just drop the repeat, because it makes no sense to repeat after
4959 // the user has released a key. (This is especially important for
4960 // long presses.)
4961 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
4962 return INJECT_SUCCEEDED;
4963 }
4964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 if (DEBUG_INPUT) Log.v(
4968 TAG, "Dispatching to " + focus + ": " + event);
4969
4970 if (uid != 0 && uid != focus.mSession.mUid) {
4971 if (mContext.checkPermission(
4972 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4973 != PackageManager.PERMISSION_GRANTED) {
4974 Log.w(TAG, "Permission denied: injecting key event from pid "
4975 + pid + " uid " + uid + " to window " + focus
4976 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004977 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 }
4979 }
Romain Guy06882f82009-06-10 13:36:04 -07004980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 synchronized(mWindowMap) {
4982 mKeyWaiter.bindTargetWindowLocked(focus);
4983 }
4984
4985 // NOSHIP extra state logging
4986 mKeyWaiter.recordDispatchState(event, focus);
4987 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 try {
4990 if (DEBUG_INPUT || DEBUG_FOCUS) {
4991 Log.v(TAG, "Delivering key " + event.getKeyCode()
4992 + " to " + focus);
4993 }
4994 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004995 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996 } catch (android.os.RemoteException e) {
4997 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4998 try {
4999 removeWindow(focus.mSession, focus.mClient);
5000 } catch (java.util.NoSuchElementException ex) {
5001 // This will happen if the window has already been
5002 // removed.
5003 }
5004 }
Romain Guy06882f82009-06-10 13:36:04 -07005005
Dianne Hackborncfaef692009-06-15 14:24:44 -07005006 return INJECT_FAILED;
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 public void pauseKeyDispatching(IBinder _token) {
5010 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5011 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005012 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 }
5014
5015 synchronized (mWindowMap) {
5016 WindowToken token = mTokenMap.get(_token);
5017 if (token != null) {
5018 mKeyWaiter.pauseDispatchingLocked(token);
5019 }
5020 }
5021 }
5022
5023 public void resumeKeyDispatching(IBinder _token) {
5024 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5025 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005026 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 }
5028
5029 synchronized (mWindowMap) {
5030 WindowToken token = mTokenMap.get(_token);
5031 if (token != null) {
5032 mKeyWaiter.resumeDispatchingLocked(token);
5033 }
5034 }
5035 }
5036
5037 public void setEventDispatching(boolean enabled) {
5038 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5039 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005040 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 }
5042
5043 synchronized (mWindowMap) {
5044 mKeyWaiter.setEventDispatchingLocked(enabled);
5045 }
5046 }
Romain Guy06882f82009-06-10 13:36:04 -07005047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 /**
5049 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005050 *
5051 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 * {@link SystemClock#uptimeMillis()} as the timebase.)
5053 * @param sync If true, wait for the event to be completed before returning to the caller.
5054 * @return Returns true if event was dispatched, false if it was dropped for any reason
5055 */
5056 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5057 long downTime = ev.getDownTime();
5058 long eventTime = ev.getEventTime();
5059
5060 int action = ev.getAction();
5061 int code = ev.getKeyCode();
5062 int repeatCount = ev.getRepeatCount();
5063 int metaState = ev.getMetaState();
5064 int deviceId = ev.getDeviceId();
5065 int scancode = ev.getScanCode();
5066
5067 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5068 if (downTime == 0) downTime = eventTime;
5069
5070 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005071 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005073 final int pid = Binder.getCallingPid();
5074 final int uid = Binder.getCallingUid();
5075 final long ident = Binder.clearCallingIdentity();
5076 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005078 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005080 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005081 switch (result) {
5082 case INJECT_NO_PERMISSION:
5083 throw new SecurityException(
5084 "Injecting to another application requires INJECT_EVENT permission");
5085 case INJECT_SUCCEEDED:
5086 return true;
5087 }
5088 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 }
5090
5091 /**
5092 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005093 *
5094 * @param ev A motion event describing the pointer (touch) action. (As noted in
5095 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 * {@link SystemClock#uptimeMillis()} as the timebase.)
5097 * @param sync If true, wait for the event to be completed before returning to the caller.
5098 * @return Returns true if event was dispatched, false if it was dropped for any reason
5099 */
5100 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005101 final int pid = Binder.getCallingPid();
5102 final int uid = Binder.getCallingUid();
5103 final long ident = Binder.clearCallingIdentity();
5104 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005106 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005108 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005109 switch (result) {
5110 case INJECT_NO_PERMISSION:
5111 throw new SecurityException(
5112 "Injecting to another application requires INJECT_EVENT permission");
5113 case INJECT_SUCCEEDED:
5114 return true;
5115 }
5116 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 }
Romain Guy06882f82009-06-10 13:36:04 -07005118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 /**
5120 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005121 *
5122 * @param ev A motion event describing the trackball action. (As noted in
5123 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 * {@link SystemClock#uptimeMillis()} as the timebase.)
5125 * @param sync If true, wait for the event to be completed before returning to the caller.
5126 * @return Returns true if event was dispatched, false if it was dropped for any reason
5127 */
5128 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005129 final int pid = Binder.getCallingPid();
5130 final int uid = Binder.getCallingUid();
5131 final long ident = Binder.clearCallingIdentity();
5132 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005134 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005136 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005137 switch (result) {
5138 case INJECT_NO_PERMISSION:
5139 throw new SecurityException(
5140 "Injecting to another application requires INJECT_EVENT permission");
5141 case INJECT_SUCCEEDED:
5142 return true;
5143 }
5144 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 }
Romain Guy06882f82009-06-10 13:36:04 -07005146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 private WindowState getFocusedWindow() {
5148 synchronized (mWindowMap) {
5149 return getFocusedWindowLocked();
5150 }
5151 }
5152
5153 private WindowState getFocusedWindowLocked() {
5154 return mCurrentFocus;
5155 }
Romain Guy06882f82009-06-10 13:36:04 -07005156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 /**
5158 * This class holds the state for dispatching key events. This state
5159 * is protected by the KeyWaiter instance, NOT by the window lock. You
5160 * can be holding the main window lock while acquire the KeyWaiter lock,
5161 * but not the other way around.
5162 */
5163 final class KeyWaiter {
5164 // NOSHIP debugging
5165 public class DispatchState {
5166 private KeyEvent event;
5167 private WindowState focus;
5168 private long time;
5169 private WindowState lastWin;
5170 private IBinder lastBinder;
5171 private boolean finished;
5172 private boolean gotFirstWindow;
5173 private boolean eventDispatching;
5174 private long timeToSwitch;
5175 private boolean wasFrozen;
5176 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005177 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5180 focus = theFocus;
5181 event = theEvent;
5182 time = System.currentTimeMillis();
5183 // snapshot KeyWaiter state
5184 lastWin = mLastWin;
5185 lastBinder = mLastBinder;
5186 finished = mFinished;
5187 gotFirstWindow = mGotFirstWindow;
5188 eventDispatching = mEventDispatching;
5189 timeToSwitch = mTimeToSwitch;
5190 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005191 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 // cache the paused state at ctor time as well
5193 if (theFocus == null || theFocus.mToken == null) {
5194 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5195 focusPaused = false;
5196 } else {
5197 focusPaused = theFocus.mToken.paused;
5198 }
5199 }
Romain Guy06882f82009-06-10 13:36:04 -07005200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 public String toString() {
5202 return "{{" + event + " to " + focus + " @ " + time
5203 + " lw=" + lastWin + " lb=" + lastBinder
5204 + " fin=" + finished + " gfw=" + gotFirstWindow
5205 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005206 + " wf=" + wasFrozen + " fp=" + focusPaused
5207 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 }
5209 };
5210 private DispatchState mDispatchState = null;
5211 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5212 mDispatchState = new DispatchState(theEvent, theFocus);
5213 }
5214 // END NOSHIP
5215
5216 public static final int RETURN_NOTHING = 0;
5217 public static final int RETURN_PENDING_POINTER = 1;
5218 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 final Object SKIP_TARGET_TOKEN = new Object();
5221 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 private WindowState mLastWin = null;
5224 private IBinder mLastBinder = null;
5225 private boolean mFinished = true;
5226 private boolean mGotFirstWindow = false;
5227 private boolean mEventDispatching = true;
5228 private long mTimeToSwitch = 0;
5229 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 // Target of Motion events
5232 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 // Windows above the target who would like to receive an "outside"
5235 // touch event for any down events outside of them.
5236 WindowState mOutsideTouchTargets;
5237
5238 /**
5239 * Wait for the last event dispatch to complete, then find the next
5240 * target that should receive the given event and wait for that one
5241 * to be ready to receive it.
5242 */
5243 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5244 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005245 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 long startTime = SystemClock.uptimeMillis();
5247 long keyDispatchingTimeout = 5 * 1000;
5248 long waitedFor = 0;
5249
5250 while (true) {
5251 // Figure out which window we care about. It is either the
5252 // last window we are waiting to have process the event or,
5253 // if none, then the next window we think the event should go
5254 // to. Note: we retrieve mLastWin outside of the lock, so
5255 // it may change before we lock. Thus we must check it again.
5256 WindowState targetWin = mLastWin;
5257 boolean targetIsNew = targetWin == null;
5258 if (DEBUG_INPUT) Log.v(
5259 TAG, "waitForLastKey: mFinished=" + mFinished +
5260 ", mLastWin=" + mLastWin);
5261 if (targetIsNew) {
5262 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005263 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 if (target == SKIP_TARGET_TOKEN) {
5265 // The user has pressed a special key, and we are
5266 // dropping all pending events before it.
5267 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5268 + " " + nextMotion);
5269 return null;
5270 }
5271 if (target == CONSUMED_EVENT_TOKEN) {
5272 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5273 + " " + nextMotion);
5274 return target;
5275 }
5276 targetWin = (WindowState)target;
5277 }
Romain Guy06882f82009-06-10 13:36:04 -07005278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 // Now: is it okay to send the next event to this window?
5282 synchronized (this) {
5283 // First: did we come here based on the last window not
5284 // being null, but it changed by the time we got here?
5285 // If so, try again.
5286 if (!targetIsNew && mLastWin == null) {
5287 continue;
5288 }
Romain Guy06882f82009-06-10 13:36:04 -07005289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 // We never dispatch events if not finished with the
5291 // last one, or the display is frozen.
5292 if (mFinished && !mDisplayFrozen) {
5293 // If event dispatching is disabled, then we
5294 // just consume the events.
5295 if (!mEventDispatching) {
5296 if (DEBUG_INPUT) Log.v(TAG,
5297 "Skipping event; dispatching disabled: "
5298 + nextKey + " " + nextMotion);
5299 return null;
5300 }
5301 if (targetWin != null) {
5302 // If this is a new target, and that target is not
5303 // paused or unresponsive, then all looks good to
5304 // handle the event.
5305 if (targetIsNew && !targetWin.mToken.paused) {
5306 return targetWin;
5307 }
Romain Guy06882f82009-06-10 13:36:04 -07005308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 // If we didn't find a target window, and there is no
5310 // focused app window, then just eat the events.
5311 } else if (mFocusedApp == null) {
5312 if (DEBUG_INPUT) Log.v(TAG,
5313 "Skipping event; no focused app: "
5314 + nextKey + " " + nextMotion);
5315 return null;
5316 }
5317 }
Romain Guy06882f82009-06-10 13:36:04 -07005318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 if (DEBUG_INPUT) Log.v(
5320 TAG, "Waiting for last key in " + mLastBinder
5321 + " target=" + targetWin
5322 + " mFinished=" + mFinished
5323 + " mDisplayFrozen=" + mDisplayFrozen
5324 + " targetIsNew=" + targetIsNew
5325 + " paused="
5326 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005327 + " mFocusedApp=" + mFocusedApp
5328 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 targetApp = targetWin != null
5331 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 long curTimeout = keyDispatchingTimeout;
5334 if (mTimeToSwitch != 0) {
5335 long now = SystemClock.uptimeMillis();
5336 if (mTimeToSwitch <= now) {
5337 // If an app switch key has been pressed, and we have
5338 // waited too long for the current app to finish
5339 // processing keys, then wait no more!
5340 doFinishedKeyLocked(true);
5341 continue;
5342 }
5343 long switchTimeout = mTimeToSwitch - now;
5344 if (curTimeout > switchTimeout) {
5345 curTimeout = switchTimeout;
5346 }
5347 }
Romain Guy06882f82009-06-10 13:36:04 -07005348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 try {
5350 // after that continue
5351 // processing keys, so we don't get stuck.
5352 if (DEBUG_INPUT) Log.v(
5353 TAG, "Waiting for key dispatch: " + curTimeout);
5354 wait(curTimeout);
5355 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5356 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005357 + startTime + " switchTime=" + mTimeToSwitch
5358 + " target=" + targetWin + " mLW=" + mLastWin
5359 + " mLB=" + mLastBinder + " fin=" + mFinished
5360 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 } catch (InterruptedException e) {
5362 }
5363 }
5364
5365 // If we were frozen during configuration change, restart the
5366 // timeout checks from now; otherwise look at whether we timed
5367 // out before awakening.
5368 if (mWasFrozen) {
5369 waitedFor = 0;
5370 mWasFrozen = false;
5371 } else {
5372 waitedFor = SystemClock.uptimeMillis() - startTime;
5373 }
5374
5375 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5376 IApplicationToken at = null;
5377 synchronized (this) {
5378 Log.w(TAG, "Key dispatching timed out sending to " +
5379 (targetWin != null ? targetWin.mAttrs.getTitle()
5380 : "<null>"));
5381 // NOSHIP debugging
5382 Log.w(TAG, "Dispatch state: " + mDispatchState);
5383 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5384 // END NOSHIP
5385 //dump();
5386 if (targetWin != null) {
5387 at = targetWin.getAppToken();
5388 } else if (targetApp != null) {
5389 at = targetApp.appToken;
5390 }
5391 }
5392
5393 boolean abort = true;
5394 if (at != null) {
5395 try {
5396 long timeout = at.getKeyDispatchingTimeout();
5397 if (timeout > waitedFor) {
5398 // we did not wait the proper amount of time for this application.
5399 // set the timeout to be the real timeout and wait again.
5400 keyDispatchingTimeout = timeout - waitedFor;
5401 continue;
5402 } else {
5403 abort = at.keyDispatchingTimedOut();
5404 }
5405 } catch (RemoteException ex) {
5406 }
5407 }
5408
5409 synchronized (this) {
5410 if (abort && (mLastWin == targetWin || targetWin == null)) {
5411 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005412 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 if (DEBUG_INPUT) Log.v(TAG,
5414 "Window " + mLastWin +
5415 " timed out on key input");
5416 if (mLastWin.mToken.paused) {
5417 Log.w(TAG, "Un-pausing dispatching to this window");
5418 mLastWin.mToken.paused = false;
5419 }
5420 }
5421 if (mMotionTarget == targetWin) {
5422 mMotionTarget = null;
5423 }
5424 mLastWin = null;
5425 mLastBinder = null;
5426 if (failIfTimeout || targetWin == null) {
5427 return null;
5428 }
5429 } else {
5430 Log.w(TAG, "Continuing to wait for key to be dispatched");
5431 startTime = SystemClock.uptimeMillis();
5432 }
5433 }
5434 }
5435 }
5436 }
Romain Guy06882f82009-06-10 13:36:04 -07005437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005439 MotionEvent nextMotion, boolean isPointerEvent,
5440 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 if (nextKey != null) {
5444 // Find the target window for a normal key event.
5445 final int keycode = nextKey.getKeyCode();
5446 final int repeatCount = nextKey.getRepeatCount();
5447 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5448 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005451 if (callingUid == 0 ||
5452 mContext.checkPermission(
5453 android.Manifest.permission.INJECT_EVENTS,
5454 callingPid, callingUid)
5455 == PackageManager.PERMISSION_GRANTED) {
5456 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005457 nextKey.getMetaState(), down, repeatCount,
5458 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 Log.w(TAG, "Event timeout during app switch: dropping "
5461 + nextKey);
5462 return SKIP_TARGET_TOKEN;
5463 }
Romain Guy06882f82009-06-10 13:36:04 -07005464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 WindowState focus = null;
5468 synchronized(mWindowMap) {
5469 focus = getFocusedWindowLocked();
5470 }
Romain Guy06882f82009-06-10 13:36:04 -07005471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005473
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005474 if (callingUid == 0 ||
5475 (focus != null && callingUid == focus.mSession.mUid) ||
5476 mContext.checkPermission(
5477 android.Manifest.permission.INJECT_EVENTS,
5478 callingPid, callingUid)
5479 == PackageManager.PERMISSION_GRANTED) {
5480 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005481 keycode, nextKey.getMetaState(), down, repeatCount,
5482 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005483 return CONSUMED_EVENT_TOKEN;
5484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 }
Romain Guy06882f82009-06-10 13:36:04 -07005486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 } else if (!isPointerEvent) {
5490 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5491 if (!dispatch) {
5492 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5493 + nextMotion);
5494 return SKIP_TARGET_TOKEN;
5495 }
Romain Guy06882f82009-06-10 13:36:04 -07005496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 WindowState focus = null;
5498 synchronized(mWindowMap) {
5499 focus = getFocusedWindowLocked();
5500 }
Romain Guy06882f82009-06-10 13:36:04 -07005501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5503 return focus;
5504 }
Romain Guy06882f82009-06-10 13:36:04 -07005505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 if (nextMotion == null) {
5507 return SKIP_TARGET_TOKEN;
5508 }
Romain Guy06882f82009-06-10 13:36:04 -07005509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5511 KeyEvent.KEYCODE_UNKNOWN);
5512 if (!dispatch) {
5513 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5514 + nextMotion);
5515 return SKIP_TARGET_TOKEN;
5516 }
Romain Guy06882f82009-06-10 13:36:04 -07005517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 // Find the target window for a pointer event.
5519 int action = nextMotion.getAction();
5520 final float xf = nextMotion.getX();
5521 final float yf = nextMotion.getY();
5522 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 final boolean screenWasOff = qev != null
5525 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 synchronized(mWindowMap) {
5530 synchronized (this) {
5531 if (action == MotionEvent.ACTION_DOWN) {
5532 if (mMotionTarget != null) {
5533 // this is weird, we got a pen down, but we thought it was
5534 // already down!
5535 // XXX: We should probably send an ACTION_UP to the current
5536 // target.
5537 Log.w(TAG, "Pointer down received while already down in: "
5538 + mMotionTarget);
5539 mMotionTarget = null;
5540 }
Romain Guy06882f82009-06-10 13:36:04 -07005541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 // ACTION_DOWN is special, because we need to lock next events to
5543 // the window we'll land onto.
5544 final int x = (int)xf;
5545 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 final ArrayList windows = mWindows;
5548 final int N = windows.size();
5549 WindowState topErrWindow = null;
5550 final Rect tmpRect = mTempRect;
5551 for (int i=N-1; i>=0; i--) {
5552 WindowState child = (WindowState)windows.get(i);
5553 //Log.i(TAG, "Checking dispatch to: " + child);
5554 final int flags = child.mAttrs.flags;
5555 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5556 if (topErrWindow == null) {
5557 topErrWindow = child;
5558 }
5559 }
5560 if (!child.isVisibleLw()) {
5561 //Log.i(TAG, "Not visible!");
5562 continue;
5563 }
5564 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5565 //Log.i(TAG, "Not touchable!");
5566 if ((flags & WindowManager.LayoutParams
5567 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5568 child.mNextOutsideTouch = mOutsideTouchTargets;
5569 mOutsideTouchTargets = child;
5570 }
5571 continue;
5572 }
5573 tmpRect.set(child.mFrame);
5574 if (child.mTouchableInsets == ViewTreeObserver
5575 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5576 // The touch is inside of the window if it is
5577 // inside the frame, AND the content part of that
5578 // frame that was given by the application.
5579 tmpRect.left += child.mGivenContentInsets.left;
5580 tmpRect.top += child.mGivenContentInsets.top;
5581 tmpRect.right -= child.mGivenContentInsets.right;
5582 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5583 } else if (child.mTouchableInsets == ViewTreeObserver
5584 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5585 // The touch is inside of the window if it is
5586 // inside the frame, AND the visible part of that
5587 // frame that was given by the application.
5588 tmpRect.left += child.mGivenVisibleInsets.left;
5589 tmpRect.top += child.mGivenVisibleInsets.top;
5590 tmpRect.right -= child.mGivenVisibleInsets.right;
5591 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5592 }
5593 final int touchFlags = flags &
5594 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5595 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5596 if (tmpRect.contains(x, y) || touchFlags == 0) {
5597 //Log.i(TAG, "Using this target!");
5598 if (!screenWasOff || (flags &
5599 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5600 mMotionTarget = child;
5601 } else {
5602 //Log.i(TAG, "Waking, skip!");
5603 mMotionTarget = null;
5604 }
5605 break;
5606 }
Romain Guy06882f82009-06-10 13:36:04 -07005607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 if ((flags & WindowManager.LayoutParams
5609 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5610 child.mNextOutsideTouch = mOutsideTouchTargets;
5611 mOutsideTouchTargets = child;
5612 //Log.i(TAG, "Adding to outside target list: " + child);
5613 }
5614 }
5615
5616 // if there's an error window but it's not accepting
5617 // focus (typically because it is not yet visible) just
5618 // wait for it -- any other focused window may in fact
5619 // be in ANR state.
5620 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5621 mMotionTarget = null;
5622 }
5623 }
Romain Guy06882f82009-06-10 13:36:04 -07005624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 target = mMotionTarget;
5626 }
5627 }
Romain Guy06882f82009-06-10 13:36:04 -07005628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 // Pointer events are a little different -- if there isn't a
5632 // target found for any event, then just drop it.
5633 return target != null ? target : SKIP_TARGET_TOKEN;
5634 }
Romain Guy06882f82009-06-10 13:36:04 -07005635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 boolean checkShouldDispatchKey(int keycode) {
5637 synchronized (this) {
5638 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5639 mTimeToSwitch = 0;
5640 return true;
5641 }
5642 if (mTimeToSwitch != 0
5643 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5644 return false;
5645 }
5646 return true;
5647 }
5648 }
Romain Guy06882f82009-06-10 13:36:04 -07005649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 void bindTargetWindowLocked(WindowState win,
5651 int pendingWhat, QueuedEvent pendingMotion) {
5652 synchronized (this) {
5653 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5654 }
5655 }
Romain Guy06882f82009-06-10 13:36:04 -07005656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 void bindTargetWindowLocked(WindowState win) {
5658 synchronized (this) {
5659 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5660 }
5661 }
5662
5663 void bindTargetWindowLockedLocked(WindowState win,
5664 int pendingWhat, QueuedEvent pendingMotion) {
5665 mLastWin = win;
5666 mLastBinder = win.mClient.asBinder();
5667 mFinished = false;
5668 if (pendingMotion != null) {
5669 final Session s = win.mSession;
5670 if (pendingWhat == RETURN_PENDING_POINTER) {
5671 releasePendingPointerLocked(s);
5672 s.mPendingPointerMove = pendingMotion;
5673 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005674 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 "bindTargetToWindow " + s.mPendingPointerMove);
5676 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5677 releasePendingTrackballLocked(s);
5678 s.mPendingTrackballMove = pendingMotion;
5679 s.mPendingTrackballWindow = win;
5680 }
5681 }
5682 }
Romain Guy06882f82009-06-10 13:36:04 -07005683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 void releasePendingPointerLocked(Session s) {
5685 if (DEBUG_INPUT) Log.v(TAG,
5686 "releasePendingPointer " + s.mPendingPointerMove);
5687 if (s.mPendingPointerMove != null) {
5688 mQueue.recycleEvent(s.mPendingPointerMove);
5689 s.mPendingPointerMove = null;
5690 }
5691 }
Romain Guy06882f82009-06-10 13:36:04 -07005692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 void releasePendingTrackballLocked(Session s) {
5694 if (s.mPendingTrackballMove != null) {
5695 mQueue.recycleEvent(s.mPendingTrackballMove);
5696 s.mPendingTrackballMove = null;
5697 }
5698 }
Romain Guy06882f82009-06-10 13:36:04 -07005699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5701 int returnWhat) {
5702 if (DEBUG_INPUT) Log.v(
5703 TAG, "finishedKey: client=" + client + ", force=" + force);
5704
5705 if (client == null) {
5706 return null;
5707 }
5708
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005709 MotionEvent res = null;
5710 QueuedEvent qev = null;
5711 WindowState win = null;
5712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 synchronized (this) {
5714 if (DEBUG_INPUT) Log.v(
5715 TAG, "finishedKey: client=" + client.asBinder()
5716 + ", force=" + force + ", last=" + mLastBinder
5717 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 if (returnWhat == RETURN_PENDING_POINTER) {
5720 qev = session.mPendingPointerMove;
5721 win = session.mPendingPointerWindow;
5722 session.mPendingPointerMove = null;
5723 session.mPendingPointerWindow = null;
5724 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5725 qev = session.mPendingTrackballMove;
5726 win = session.mPendingTrackballWindow;
5727 session.mPendingTrackballMove = null;
5728 session.mPendingTrackballWindow = null;
5729 }
Romain Guy06882f82009-06-10 13:36:04 -07005730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 if (mLastBinder == client.asBinder()) {
5732 if (DEBUG_INPUT) Log.v(
5733 TAG, "finishedKey: last paused="
5734 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5735 if (mLastWin != null && (!mLastWin.mToken.paused || force
5736 || !mEventDispatching)) {
5737 doFinishedKeyLocked(false);
5738 } else {
5739 // Make sure to wake up anyone currently waiting to
5740 // dispatch a key, so they can re-evaluate their
5741 // current situation.
5742 mFinished = true;
5743 notifyAll();
5744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 }
Romain Guy06882f82009-06-10 13:36:04 -07005746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005748 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 if (DEBUG_INPUT) Log.v(TAG,
5750 "Returning pending motion: " + res);
5751 mQueue.recycleEvent(qev);
5752 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5753 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005757
5758 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5759 synchronized (mWindowMap) {
5760 if (mWallpaperTarget == win) {
5761 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5762 }
5763 }
5764 }
5765
5766 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 }
5768
5769 void tickle() {
5770 synchronized (this) {
5771 notifyAll();
5772 }
5773 }
Romain Guy06882f82009-06-10 13:36:04 -07005774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 void handleNewWindowLocked(WindowState newWindow) {
5776 if (!newWindow.canReceiveKeys()) {
5777 return;
5778 }
5779 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005780 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 TAG, "New key dispatch window: win="
5782 + newWindow.mClient.asBinder()
5783 + ", last=" + mLastBinder
5784 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5785 + "), finished=" + mFinished + ", paused="
5786 + newWindow.mToken.paused);
5787
5788 // Displaying a window implicitly causes dispatching to
5789 // be unpaused. (This is to protect against bugs if someone
5790 // pauses dispatching but forgets to resume.)
5791 newWindow.mToken.paused = false;
5792
5793 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794
5795 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5796 if (DEBUG_INPUT) Log.v(TAG,
5797 "New SYSTEM_ERROR window; resetting state");
5798 mLastWin = null;
5799 mLastBinder = null;
5800 mMotionTarget = null;
5801 mFinished = true;
5802 } else if (mLastWin != null) {
5803 // If the new window is above the window we are
5804 // waiting on, then stop waiting and let key dispatching
5805 // start on the new guy.
5806 if (DEBUG_INPUT) Log.v(
5807 TAG, "Last win layer=" + mLastWin.mLayer
5808 + ", new win layer=" + newWindow.mLayer);
5809 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005810 // The new window is above the old; finish pending input to the last
5811 // window and start directing it to the new one.
5812 mLastWin.mToken.paused = false;
5813 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005815 // Either the new window is lower, so there is no need to wake key waiters,
5816 // or we just finished key input to the previous window, which implicitly
5817 // notified the key waiters. In both cases, we don't need to issue the
5818 // notification here.
5819 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 }
5821
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005822 // Now that we've put a new window state in place, make the event waiter
5823 // take notice and retarget its attentions.
5824 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 }
5826 }
5827
5828 void pauseDispatchingLocked(WindowToken token) {
5829 synchronized (this)
5830 {
5831 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5832 token.paused = true;
5833
5834 /*
5835 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5836 mPaused = true;
5837 } else {
5838 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005839 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005841 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005843 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 }
5845 }
5846 */
5847 }
5848 }
5849
5850 void resumeDispatchingLocked(WindowToken token) {
5851 synchronized (this) {
5852 if (token.paused) {
5853 if (DEBUG_INPUT) Log.v(
5854 TAG, "Resuming WindowToken " + token
5855 + ", last=" + mLastBinder
5856 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5857 + "), finished=" + mFinished + ", paused="
5858 + token.paused);
5859 token.paused = false;
5860 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5861 doFinishedKeyLocked(true);
5862 } else {
5863 notifyAll();
5864 }
5865 }
5866 }
5867 }
5868
5869 void setEventDispatchingLocked(boolean enabled) {
5870 synchronized (this) {
5871 mEventDispatching = enabled;
5872 notifyAll();
5873 }
5874 }
Romain Guy06882f82009-06-10 13:36:04 -07005875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 void appSwitchComing() {
5877 synchronized (this) {
5878 // Don't wait for more than .5 seconds for app to finish
5879 // processing the pending events.
5880 long now = SystemClock.uptimeMillis() + 500;
5881 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5882 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5883 mTimeToSwitch = now;
5884 }
5885 notifyAll();
5886 }
5887 }
Romain Guy06882f82009-06-10 13:36:04 -07005888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 private final void doFinishedKeyLocked(boolean doRecycle) {
5890 if (mLastWin != null) {
5891 releasePendingPointerLocked(mLastWin.mSession);
5892 releasePendingTrackballLocked(mLastWin.mSession);
5893 }
Romain Guy06882f82009-06-10 13:36:04 -07005894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 if (mLastWin == null || !mLastWin.mToken.paused
5896 || !mLastWin.isVisibleLw()) {
5897 // If the current window has been paused, we aren't -really-
5898 // finished... so let the waiters still wait.
5899 mLastWin = null;
5900 mLastBinder = null;
5901 }
5902 mFinished = true;
5903 notifyAll();
5904 }
5905 }
5906
5907 private class KeyQ extends KeyInputQueue
5908 implements KeyInputQueue.FilterCallback {
5909 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005912 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5914 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5915 "KEEP_SCREEN_ON_FLAG");
5916 mHoldingScreen.setReferenceCounted(false);
5917 }
5918
5919 @Override
5920 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5921 if (mPolicy.preprocessInputEventTq(event)) {
5922 return true;
5923 }
Romain Guy06882f82009-06-10 13:36:04 -07005924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 switch (event.type) {
5926 case RawInputEvent.EV_KEY: {
5927 // XXX begin hack
5928 if (DEBUG) {
5929 if (event.keycode == KeyEvent.KEYCODE_G) {
5930 if (event.value != 0) {
5931 // G down
5932 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5933 }
5934 return false;
5935 }
5936 if (event.keycode == KeyEvent.KEYCODE_D) {
5937 if (event.value != 0) {
5938 //dump();
5939 }
5940 return false;
5941 }
5942 }
5943 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005945 boolean screenIsOff = !mPowerManager.screenIsOn();
5946 boolean screenIsDim = !mPowerManager.screenIsBright();
5947 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5950 mPowerManager.goToSleep(event.when);
5951 }
5952
5953 if (screenIsOff) {
5954 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5955 }
5956 if (screenIsDim) {
5957 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5958 }
5959 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5960 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005961 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 }
Romain Guy06882f82009-06-10 13:36:04 -07005963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5965 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5966 filterQueue(this);
5967 mKeyWaiter.appSwitchComing();
5968 }
5969 return true;
5970 } else {
5971 return false;
5972 }
5973 }
Romain Guy06882f82009-06-10 13:36:04 -07005974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 case RawInputEvent.EV_REL: {
5976 boolean screenIsOff = !mPowerManager.screenIsOn();
5977 boolean screenIsDim = !mPowerManager.screenIsBright();
5978 if (screenIsOff) {
5979 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5980 device.classes, event)) {
5981 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5982 return false;
5983 }
5984 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5985 }
5986 if (screenIsDim) {
5987 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5988 }
5989 return true;
5990 }
Romain Guy06882f82009-06-10 13:36:04 -07005991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 case RawInputEvent.EV_ABS: {
5993 boolean screenIsOff = !mPowerManager.screenIsOn();
5994 boolean screenIsDim = !mPowerManager.screenIsBright();
5995 if (screenIsOff) {
5996 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5997 device.classes, event)) {
5998 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5999 return false;
6000 }
6001 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6002 }
6003 if (screenIsDim) {
6004 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6005 }
6006 return true;
6007 }
Romain Guy06882f82009-06-10 13:36:04 -07006008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 default:
6010 return true;
6011 }
6012 }
6013
6014 public int filterEvent(QueuedEvent ev) {
6015 switch (ev.classType) {
6016 case RawInputEvent.CLASS_KEYBOARD:
6017 KeyEvent ke = (KeyEvent)ev.event;
6018 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6019 Log.w(TAG, "Dropping movement key during app switch: "
6020 + ke.getKeyCode() + ", action=" + ke.getAction());
6021 return FILTER_REMOVE;
6022 }
6023 return FILTER_ABORT;
6024 default:
6025 return FILTER_KEEP;
6026 }
6027 }
Romain Guy06882f82009-06-10 13:36:04 -07006028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 /**
6030 * Must be called with the main window manager lock held.
6031 */
6032 void setHoldScreenLocked(boolean holding) {
6033 boolean state = mHoldingScreen.isHeld();
6034 if (holding != state) {
6035 if (holding) {
6036 mHoldingScreen.acquire();
6037 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006038 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 mHoldingScreen.release();
6040 }
6041 }
6042 }
Michael Chan53071d62009-05-13 17:29:48 -07006043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006044
6045 public boolean detectSafeMode() {
6046 mSafeMode = mPolicy.detectSafeMode();
6047 return mSafeMode;
6048 }
Romain Guy06882f82009-06-10 13:36:04 -07006049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 public void systemReady() {
6051 mPolicy.systemReady();
6052 }
Romain Guy06882f82009-06-10 13:36:04 -07006053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 private final class InputDispatcherThread extends Thread {
6055 // Time to wait when there is nothing to do: 9999 seconds.
6056 static final int LONG_WAIT=9999*1000;
6057
6058 public InputDispatcherThread() {
6059 super("InputDispatcher");
6060 }
Romain Guy06882f82009-06-10 13:36:04 -07006061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 @Override
6063 public void run() {
6064 while (true) {
6065 try {
6066 process();
6067 } catch (Exception e) {
6068 Log.e(TAG, "Exception in input dispatcher", e);
6069 }
6070 }
6071 }
Romain Guy06882f82009-06-10 13:36:04 -07006072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 private void process() {
6074 android.os.Process.setThreadPriority(
6075 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 // The last key event we saw
6078 KeyEvent lastKey = null;
6079
6080 // Last keydown time for auto-repeating keys
6081 long lastKeyTime = SystemClock.uptimeMillis();
6082 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006083 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084
Romain Guy06882f82009-06-10 13:36:04 -07006085 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 int keyRepeatCount = 0;
6087
6088 // Need to report that configuration has changed?
6089 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 while (true) {
6092 long curTime = SystemClock.uptimeMillis();
6093
6094 if (DEBUG_INPUT) Log.v(
6095 TAG, "Waiting for next key: now=" + curTime
6096 + ", repeat @ " + nextKeyTime);
6097
6098 // Retrieve next event, waiting only as long as the next
6099 // repeat timeout. If the configuration has changed, then
6100 // don't wait at all -- we'll report the change as soon as
6101 // we have processed all events.
6102 QueuedEvent ev = mQueue.getEvent(
6103 (int)((!configChanged && curTime < nextKeyTime)
6104 ? (nextKeyTime-curTime) : 0));
6105
6106 if (DEBUG_INPUT && ev != null) Log.v(
6107 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6108
Michael Chan53071d62009-05-13 17:29:48 -07006109 if (MEASURE_LATENCY) {
6110 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6111 }
6112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 try {
6114 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006115 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 int eventType;
6117 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6118 eventType = eventType((MotionEvent)ev.event);
6119 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6120 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6121 eventType = LocalPowerManager.BUTTON_EVENT;
6122 } else {
6123 eventType = LocalPowerManager.OTHER_EVENT;
6124 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006125 try {
Michael Chan53071d62009-05-13 17:29:48 -07006126 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006127 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006128 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006129 mBatteryStats.noteInputEvent();
6130 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006131 } catch (RemoteException e) {
6132 // Ignore
6133 }
Michael Chane10de972009-05-18 11:24:50 -07006134
6135 if (eventType != TOUCH_EVENT
6136 && eventType != LONG_TOUCH_EVENT
6137 && eventType != CHEEK_EVENT) {
6138 mPowerManager.userActivity(curTime, false,
6139 eventType, false);
6140 } else if (mLastTouchEventType != eventType
6141 || (curTime - mLastUserActivityCallTime)
6142 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6143 mLastUserActivityCallTime = curTime;
6144 mLastTouchEventType = eventType;
6145 mPowerManager.userActivity(curTime, false,
6146 eventType, false);
6147 }
6148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 switch (ev.classType) {
6150 case RawInputEvent.CLASS_KEYBOARD:
6151 KeyEvent ke = (KeyEvent)ev.event;
6152 if (ke.isDown()) {
6153 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006154 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 keyRepeatCount = 0;
6156 lastKeyTime = curTime;
6157 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006158 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 if (DEBUG_INPUT) Log.v(
6160 TAG, "Received key down: first repeat @ "
6161 + nextKeyTime);
6162 } else {
6163 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006164 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 // Arbitrary long timeout.
6166 lastKeyTime = curTime;
6167 nextKeyTime = curTime + LONG_WAIT;
6168 if (DEBUG_INPUT) Log.v(
6169 TAG, "Received key up: ignore repeat @ "
6170 + nextKeyTime);
6171 }
6172 dispatchKey((KeyEvent)ev.event, 0, 0);
6173 mQueue.recycleEvent(ev);
6174 break;
6175 case RawInputEvent.CLASS_TOUCHSCREEN:
6176 //Log.i(TAG, "Read next event " + ev);
6177 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6178 break;
6179 case RawInputEvent.CLASS_TRACKBALL:
6180 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6181 break;
6182 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6183 configChanged = true;
6184 break;
6185 default:
6186 mQueue.recycleEvent(ev);
6187 break;
6188 }
Romain Guy06882f82009-06-10 13:36:04 -07006189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 } else if (configChanged) {
6191 configChanged = false;
6192 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006194 } else if (lastKey != null) {
6195 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 // Timeout occurred while key was down. If it is at or
6198 // past the key repeat time, dispatch the repeat.
6199 if (DEBUG_INPUT) Log.v(
6200 TAG, "Key timeout: repeat=" + nextKeyTime
6201 + ", now=" + curTime);
6202 if (curTime < nextKeyTime) {
6203 continue;
6204 }
Romain Guy06882f82009-06-10 13:36:04 -07006205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 lastKeyTime = nextKeyTime;
6207 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6208 keyRepeatCount++;
6209 if (DEBUG_INPUT) Log.v(
6210 TAG, "Key repeat: count=" + keyRepeatCount
6211 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006212 KeyEvent newEvent;
6213 if (downTime != 0 && (downTime
6214 + ViewConfiguration.getLongPressTimeout())
6215 <= curTime) {
6216 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6217 curTime, keyRepeatCount,
6218 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6219 downTime = 0;
6220 } else {
6221 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6222 curTime, keyRepeatCount);
6223 }
6224 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 } else {
6227 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 lastKeyTime = curTime;
6230 nextKeyTime = curTime + LONG_WAIT;
6231 }
Romain Guy06882f82009-06-10 13:36:04 -07006232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 } catch (Exception e) {
6234 Log.e(TAG,
6235 "Input thread received uncaught exception: " + e, e);
6236 }
6237 }
6238 }
6239 }
6240
6241 // -------------------------------------------------------------
6242 // Client Session State
6243 // -------------------------------------------------------------
6244
6245 private final class Session extends IWindowSession.Stub
6246 implements IBinder.DeathRecipient {
6247 final IInputMethodClient mClient;
6248 final IInputContext mInputContext;
6249 final int mUid;
6250 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006251 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 SurfaceSession mSurfaceSession;
6253 int mNumWindow = 0;
6254 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 /**
6257 * Current pointer move event being dispatched to client window... must
6258 * hold key lock to access.
6259 */
6260 QueuedEvent mPendingPointerMove;
6261 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 /**
6264 * Current trackball move event being dispatched to client window... must
6265 * hold key lock to access.
6266 */
6267 QueuedEvent mPendingTrackballMove;
6268 WindowState mPendingTrackballWindow;
6269
6270 public Session(IInputMethodClient client, IInputContext inputContext) {
6271 mClient = client;
6272 mInputContext = inputContext;
6273 mUid = Binder.getCallingUid();
6274 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006275 StringBuilder sb = new StringBuilder();
6276 sb.append("Session{");
6277 sb.append(Integer.toHexString(System.identityHashCode(this)));
6278 sb.append(" uid ");
6279 sb.append(mUid);
6280 sb.append("}");
6281 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 synchronized (mWindowMap) {
6284 if (mInputMethodManager == null && mHaveInputMethods) {
6285 IBinder b = ServiceManager.getService(
6286 Context.INPUT_METHOD_SERVICE);
6287 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6288 }
6289 }
6290 long ident = Binder.clearCallingIdentity();
6291 try {
6292 // Note: it is safe to call in to the input method manager
6293 // here because we are not holding our lock.
6294 if (mInputMethodManager != null) {
6295 mInputMethodManager.addClient(client, inputContext,
6296 mUid, mPid);
6297 } else {
6298 client.setUsingInputMethod(false);
6299 }
6300 client.asBinder().linkToDeath(this, 0);
6301 } catch (RemoteException e) {
6302 // The caller has died, so we can just forget about this.
6303 try {
6304 if (mInputMethodManager != null) {
6305 mInputMethodManager.removeClient(client);
6306 }
6307 } catch (RemoteException ee) {
6308 }
6309 } finally {
6310 Binder.restoreCallingIdentity(ident);
6311 }
6312 }
Romain Guy06882f82009-06-10 13:36:04 -07006313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006314 @Override
6315 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6316 throws RemoteException {
6317 try {
6318 return super.onTransact(code, data, reply, flags);
6319 } catch (RuntimeException e) {
6320 // Log all 'real' exceptions thrown to the caller
6321 if (!(e instanceof SecurityException)) {
6322 Log.e(TAG, "Window Session Crash", e);
6323 }
6324 throw e;
6325 }
6326 }
6327
6328 public void binderDied() {
6329 // Note: it is safe to call in to the input method manager
6330 // here because we are not holding our lock.
6331 try {
6332 if (mInputMethodManager != null) {
6333 mInputMethodManager.removeClient(mClient);
6334 }
6335 } catch (RemoteException e) {
6336 }
6337 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006338 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 mClientDead = true;
6340 killSessionLocked();
6341 }
6342 }
6343
6344 public int add(IWindow window, WindowManager.LayoutParams attrs,
6345 int viewVisibility, Rect outContentInsets) {
6346 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6347 }
Romain Guy06882f82009-06-10 13:36:04 -07006348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 public void remove(IWindow window) {
6350 removeWindow(this, window);
6351 }
Romain Guy06882f82009-06-10 13:36:04 -07006352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6354 int requestedWidth, int requestedHeight, int viewFlags,
6355 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6356 Rect outVisibleInsets, Surface outSurface) {
6357 return relayoutWindow(this, window, attrs,
6358 requestedWidth, requestedHeight, viewFlags, insetsPending,
6359 outFrame, outContentInsets, outVisibleInsets, outSurface);
6360 }
Romain Guy06882f82009-06-10 13:36:04 -07006361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 public void setTransparentRegion(IWindow window, Region region) {
6363 setTransparentRegionWindow(this, window, region);
6364 }
Romain Guy06882f82009-06-10 13:36:04 -07006365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 public void setInsets(IWindow window, int touchableInsets,
6367 Rect contentInsets, Rect visibleInsets) {
6368 setInsetsWindow(this, window, touchableInsets, contentInsets,
6369 visibleInsets);
6370 }
Romain Guy06882f82009-06-10 13:36:04 -07006371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6373 getWindowDisplayFrame(this, window, outDisplayFrame);
6374 }
Romain Guy06882f82009-06-10 13:36:04 -07006375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 public void finishDrawing(IWindow window) {
6377 if (localLOGV) Log.v(
6378 TAG, "IWindow finishDrawing called for " + window);
6379 finishDrawingWindow(this, window);
6380 }
6381
6382 public void finishKey(IWindow window) {
6383 if (localLOGV) Log.v(
6384 TAG, "IWindow finishKey called for " + window);
6385 mKeyWaiter.finishedKey(this, window, false,
6386 KeyWaiter.RETURN_NOTHING);
6387 }
6388
6389 public MotionEvent getPendingPointerMove(IWindow window) {
6390 if (localLOGV) Log.v(
6391 TAG, "IWindow getPendingMotionEvent called for " + window);
6392 return mKeyWaiter.finishedKey(this, window, false,
6393 KeyWaiter.RETURN_PENDING_POINTER);
6394 }
Romain Guy06882f82009-06-10 13:36:04 -07006395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 public MotionEvent getPendingTrackballMove(IWindow window) {
6397 if (localLOGV) Log.v(
6398 TAG, "IWindow getPendingMotionEvent called for " + window);
6399 return mKeyWaiter.finishedKey(this, window, false,
6400 KeyWaiter.RETURN_PENDING_TRACKBALL);
6401 }
6402
6403 public void setInTouchMode(boolean mode) {
6404 synchronized(mWindowMap) {
6405 mInTouchMode = mode;
6406 }
6407 }
6408
6409 public boolean getInTouchMode() {
6410 synchronized(mWindowMap) {
6411 return mInTouchMode;
6412 }
6413 }
6414
6415 public boolean performHapticFeedback(IWindow window, int effectId,
6416 boolean always) {
6417 synchronized(mWindowMap) {
6418 long ident = Binder.clearCallingIdentity();
6419 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006420 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 windowForClientLocked(this, window), effectId, always);
6422 } finally {
6423 Binder.restoreCallingIdentity(ident);
6424 }
6425 }
6426 }
Romain Guy06882f82009-06-10 13:36:04 -07006427
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006428 public void setWallpaperPosition(IBinder window, float x, float y) {
6429 synchronized(mWindowMap) {
6430 long ident = Binder.clearCallingIdentity();
6431 try {
6432 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6433 x, y);
6434 } finally {
6435 Binder.restoreCallingIdentity(ident);
6436 }
6437 }
6438 }
6439
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006440 public void wallpaperOffsetsComplete(IBinder window) {
6441 WindowManagerService.this.wallpaperOffsetsComplete(window);
6442 }
6443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 void windowAddedLocked() {
6445 if (mSurfaceSession == null) {
6446 if (localLOGV) Log.v(
6447 TAG, "First window added to " + this + ", creating SurfaceSession");
6448 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006449 if (SHOW_TRANSACTIONS) Log.i(
6450 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 mSessions.add(this);
6452 }
6453 mNumWindow++;
6454 }
6455
6456 void windowRemovedLocked() {
6457 mNumWindow--;
6458 killSessionLocked();
6459 }
Romain Guy06882f82009-06-10 13:36:04 -07006460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 void killSessionLocked() {
6462 if (mNumWindow <= 0 && mClientDead) {
6463 mSessions.remove(this);
6464 if (mSurfaceSession != null) {
6465 if (localLOGV) Log.v(
6466 TAG, "Last window removed from " + this
6467 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006468 if (SHOW_TRANSACTIONS) Log.i(
6469 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 try {
6471 mSurfaceSession.kill();
6472 } catch (Exception e) {
6473 Log.w(TAG, "Exception thrown when killing surface session "
6474 + mSurfaceSession + " in session " + this
6475 + ": " + e.toString());
6476 }
6477 mSurfaceSession = null;
6478 }
6479 }
6480 }
Romain Guy06882f82009-06-10 13:36:04 -07006481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006483 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6484 pw.print(" mClientDead="); pw.print(mClientDead);
6485 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6486 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6487 pw.print(prefix);
6488 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6489 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6490 }
6491 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6492 pw.print(prefix);
6493 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6494 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 }
6497
6498 @Override
6499 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006500 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 }
6502 }
6503
6504 // -------------------------------------------------------------
6505 // Client Window State
6506 // -------------------------------------------------------------
6507
6508 private final class WindowState implements WindowManagerPolicy.WindowState {
6509 final Session mSession;
6510 final IWindow mClient;
6511 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006512 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 AppWindowToken mAppToken;
6514 AppWindowToken mTargetAppToken;
6515 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6516 final DeathRecipient mDeathRecipient;
6517 final WindowState mAttachedWindow;
6518 final ArrayList mChildWindows = new ArrayList();
6519 final int mBaseLayer;
6520 final int mSubLayer;
6521 final boolean mLayoutAttached;
6522 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006523 final boolean mIsWallpaper;
6524 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 int mViewVisibility;
6526 boolean mPolicyVisibility = true;
6527 boolean mPolicyVisibilityAfterAnim = true;
6528 boolean mAppFreezing;
6529 Surface mSurface;
6530 boolean mAttachedHidden; // is our parent window hidden?
6531 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006532 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 int mRequestedWidth;
6534 int mRequestedHeight;
6535 int mLastRequestedWidth;
6536 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537 int mLayer;
6538 int mAnimLayer;
6539 int mLastLayer;
6540 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006541 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006542 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543
6544 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 // Actual frame shown on-screen (may be modified by animation)
6547 final Rect mShownFrame = new Rect();
6548 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 /**
6551 * Insets that determine the actually visible area
6552 */
6553 final Rect mVisibleInsets = new Rect();
6554 final Rect mLastVisibleInsets = new Rect();
6555 boolean mVisibleInsetsChanged;
6556
6557 /**
6558 * Insets that are covered by system windows
6559 */
6560 final Rect mContentInsets = new Rect();
6561 final Rect mLastContentInsets = new Rect();
6562 boolean mContentInsetsChanged;
6563
6564 /**
6565 * Set to true if we are waiting for this window to receive its
6566 * given internal insets before laying out other windows based on it.
6567 */
6568 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006570 /**
6571 * These are the content insets that were given during layout for
6572 * this window, to be applied to windows behind it.
6573 */
6574 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 /**
6577 * These are the visible insets that were given during layout for
6578 * this window, to be applied to windows behind it.
6579 */
6580 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 /**
6583 * Flag indicating whether the touchable region should be adjusted by
6584 * the visible insets; if false the area outside the visible insets is
6585 * NOT touchable, so we must use those to adjust the frame during hit
6586 * tests.
6587 */
6588 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 // Current transformation being applied.
6591 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6592 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6593 float mHScale=1, mVScale=1;
6594 float mLastHScale=1, mLastVScale=1;
6595 final Matrix mTmpMatrix = new Matrix();
6596
6597 // "Real" frame that the application sees.
6598 final Rect mFrame = new Rect();
6599 final Rect mLastFrame = new Rect();
6600
6601 final Rect mContainingFrame = new Rect();
6602 final Rect mDisplayFrame = new Rect();
6603 final Rect mContentFrame = new Rect();
6604 final Rect mVisibleFrame = new Rect();
6605
6606 float mShownAlpha = 1;
6607 float mAlpha = 1;
6608 float mLastAlpha = 1;
6609
6610 // Set to true if, when the window gets displayed, it should perform
6611 // an enter animation.
6612 boolean mEnterAnimationPending;
6613
6614 // Currently running animation.
6615 boolean mAnimating;
6616 boolean mLocalAnimating;
6617 Animation mAnimation;
6618 boolean mAnimationIsEntrance;
6619 boolean mHasTransformation;
6620 boolean mHasLocalTransformation;
6621 final Transformation mTransformation = new Transformation();
6622
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006623 // If a window showing a wallpaper: the requested offset for the
6624 // wallpaper; if a wallpaper window: the currently applied offset.
6625 float mWallpaperX = -1;
6626 float mWallpaperY = -1;
6627
6628 // Wallpaper windows: pixels offset based on above variables.
6629 int mXOffset;
6630 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 // This is set after IWindowSession.relayout() has been called at
6633 // least once for the window. It allows us to detect the situation
6634 // where we don't yet have a surface, but should have one soon, so
6635 // we can give the window focus before waiting for the relayout.
6636 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 // This is set after the Surface has been created but before the
6639 // window has been drawn. During this time the surface is hidden.
6640 boolean mDrawPending;
6641
6642 // This is set after the window has finished drawing for the first
6643 // time but before its surface is shown. The surface will be
6644 // displayed when the next layout is run.
6645 boolean mCommitDrawPending;
6646
6647 // This is set during the time after the window's drawing has been
6648 // committed, and before its surface is actually shown. It is used
6649 // to delay showing the surface until all windows in a token are ready
6650 // to be shown.
6651 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 // Set when the window has been shown in the screen the first time.
6654 boolean mHasDrawn;
6655
6656 // Currently running an exit animation?
6657 boolean mExiting;
6658
6659 // Currently on the mDestroySurface list?
6660 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 // Completely remove from window manager after exit animation?
6663 boolean mRemoveOnExit;
6664
6665 // Set when the orientation is changing and this window has not yet
6666 // been updated for the new orientation.
6667 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 // Is this window now (or just being) removed?
6670 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 WindowState(Session s, IWindow c, WindowToken token,
6673 WindowState attachedWindow, WindowManager.LayoutParams a,
6674 int viewVisibility) {
6675 mSession = s;
6676 mClient = c;
6677 mToken = token;
6678 mAttrs.copyFrom(a);
6679 mViewVisibility = viewVisibility;
6680 DeathRecipient deathRecipient = new DeathRecipient();
6681 mAlpha = a.alpha;
6682 if (localLOGV) Log.v(
6683 TAG, "Window " + this + " client=" + c.asBinder()
6684 + " token=" + token + " (" + mAttrs.token + ")");
6685 try {
6686 c.asBinder().linkToDeath(deathRecipient, 0);
6687 } catch (RemoteException e) {
6688 mDeathRecipient = null;
6689 mAttachedWindow = null;
6690 mLayoutAttached = false;
6691 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006692 mIsWallpaper = false;
6693 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 mBaseLayer = 0;
6695 mSubLayer = 0;
6696 return;
6697 }
6698 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006700 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6701 mAttrs.type <= LAST_SUB_WINDOW)) {
6702 // The multiplier here is to reserve space for multiple
6703 // windows in the same type layer.
6704 mBaseLayer = mPolicy.windowTypeToLayerLw(
6705 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6706 + TYPE_LAYER_OFFSET;
6707 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6708 mAttachedWindow = attachedWindow;
6709 mAttachedWindow.mChildWindows.add(this);
6710 mLayoutAttached = mAttrs.type !=
6711 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6712 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6713 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006714 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6715 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 } else {
6717 // The multiplier here is to reserve space for multiple
6718 // windows in the same type layer.
6719 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6720 * TYPE_LAYER_MULTIPLIER
6721 + TYPE_LAYER_OFFSET;
6722 mSubLayer = 0;
6723 mAttachedWindow = null;
6724 mLayoutAttached = false;
6725 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6726 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006727 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6728 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 }
6730
6731 WindowState appWin = this;
6732 while (appWin.mAttachedWindow != null) {
6733 appWin = mAttachedWindow;
6734 }
6735 WindowToken appToken = appWin.mToken;
6736 while (appToken.appWindowToken == null) {
6737 WindowToken parent = mTokenMap.get(appToken.token);
6738 if (parent == null || appToken == parent) {
6739 break;
6740 }
6741 appToken = parent;
6742 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006743 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006744 mAppToken = appToken.appWindowToken;
6745
6746 mSurface = null;
6747 mRequestedWidth = 0;
6748 mRequestedHeight = 0;
6749 mLastRequestedWidth = 0;
6750 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006751 mXOffset = 0;
6752 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 mLayer = 0;
6754 mAnimLayer = 0;
6755 mLastLayer = 0;
6756 }
6757
6758 void attach() {
6759 if (localLOGV) Log.v(
6760 TAG, "Attaching " + this + " token=" + mToken
6761 + ", list=" + mToken.windows);
6762 mSession.windowAddedLocked();
6763 }
6764
6765 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6766 mHaveFrame = true;
6767
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006768 final Rect container = mContainingFrame;
6769 container.set(pf);
6770
6771 final Rect display = mDisplayFrame;
6772 display.set(df);
6773
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006774 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006775 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006776 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6777 display.intersect(mCompatibleScreenFrame);
6778 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006779 }
6780
6781 final int pw = container.right - container.left;
6782 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783
6784 int w,h;
6785 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6786 w = mAttrs.width < 0 ? pw : mAttrs.width;
6787 h = mAttrs.height< 0 ? ph : mAttrs.height;
6788 } else {
6789 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6790 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6791 }
Romain Guy06882f82009-06-10 13:36:04 -07006792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 final Rect content = mContentFrame;
6794 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 final Rect visible = mVisibleFrame;
6797 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006800 final int fw = frame.width();
6801 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6804 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6805
6806 Gravity.apply(mAttrs.gravity, w, h, container,
6807 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6808 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6809
6810 //System.out.println("Out: " + mFrame);
6811
6812 // Now make sure the window fits in the overall display.
6813 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 // Make sure the content and visible frames are inside of the
6816 // final window frame.
6817 if (content.left < frame.left) content.left = frame.left;
6818 if (content.top < frame.top) content.top = frame.top;
6819 if (content.right > frame.right) content.right = frame.right;
6820 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6821 if (visible.left < frame.left) visible.left = frame.left;
6822 if (visible.top < frame.top) visible.top = frame.top;
6823 if (visible.right > frame.right) visible.right = frame.right;
6824 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 final Rect contentInsets = mContentInsets;
6827 contentInsets.left = content.left-frame.left;
6828 contentInsets.top = content.top-frame.top;
6829 contentInsets.right = frame.right-content.right;
6830 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 final Rect visibleInsets = mVisibleInsets;
6833 visibleInsets.left = visible.left-frame.left;
6834 visibleInsets.top = visible.top-frame.top;
6835 visibleInsets.right = frame.right-visible.right;
6836 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006837
Dianne Hackborn284ac932009-08-28 10:34:25 -07006838 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6839 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006840 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006841 }
6842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843 if (localLOGV) {
6844 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6845 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6846 Log.v(TAG, "Resolving (mRequestedWidth="
6847 + mRequestedWidth + ", mRequestedheight="
6848 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6849 + "): frame=" + mFrame.toShortString()
6850 + " ci=" + contentInsets.toShortString()
6851 + " vi=" + visibleInsets.toShortString());
6852 //}
6853 }
6854 }
Romain Guy06882f82009-06-10 13:36:04 -07006855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006856 public Rect getFrameLw() {
6857 return mFrame;
6858 }
6859
6860 public Rect getShownFrameLw() {
6861 return mShownFrame;
6862 }
6863
6864 public Rect getDisplayFrameLw() {
6865 return mDisplayFrame;
6866 }
6867
6868 public Rect getContentFrameLw() {
6869 return mContentFrame;
6870 }
6871
6872 public Rect getVisibleFrameLw() {
6873 return mVisibleFrame;
6874 }
6875
6876 public boolean getGivenInsetsPendingLw() {
6877 return mGivenInsetsPending;
6878 }
6879
6880 public Rect getGivenContentInsetsLw() {
6881 return mGivenContentInsets;
6882 }
Romain Guy06882f82009-06-10 13:36:04 -07006883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006884 public Rect getGivenVisibleInsetsLw() {
6885 return mGivenVisibleInsets;
6886 }
Romain Guy06882f82009-06-10 13:36:04 -07006887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 public WindowManager.LayoutParams getAttrs() {
6889 return mAttrs;
6890 }
6891
6892 public int getSurfaceLayer() {
6893 return mLayer;
6894 }
Romain Guy06882f82009-06-10 13:36:04 -07006895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 public IApplicationToken getAppToken() {
6897 return mAppToken != null ? mAppToken.appToken : null;
6898 }
6899
6900 public boolean hasAppShownWindows() {
6901 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6902 }
6903
6904 public boolean hasAppStartingIcon() {
6905 return mAppToken != null ? (mAppToken.startingData != null) : false;
6906 }
6907
6908 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6909 return mAppToken != null ? mAppToken.startingWindow : null;
6910 }
6911
6912 public void setAnimation(Animation anim) {
6913 if (localLOGV) Log.v(
6914 TAG, "Setting animation in " + this + ": " + anim);
6915 mAnimating = false;
6916 mLocalAnimating = false;
6917 mAnimation = anim;
6918 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6919 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6920 }
6921
6922 public void clearAnimation() {
6923 if (mAnimation != null) {
6924 mAnimating = true;
6925 mLocalAnimating = false;
6926 mAnimation = null;
6927 }
6928 }
Romain Guy06882f82009-06-10 13:36:04 -07006929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 Surface createSurfaceLocked() {
6931 if (mSurface == null) {
6932 mDrawPending = true;
6933 mCommitDrawPending = false;
6934 mReadyToShow = false;
6935 if (mAppToken != null) {
6936 mAppToken.allDrawn = false;
6937 }
6938
6939 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006940 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 flags |= Surface.PUSH_BUFFERS;
6942 }
6943
6944 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6945 flags |= Surface.SECURE;
6946 }
6947 if (DEBUG_VISIBILITY) Log.v(
6948 TAG, "Creating surface in session "
6949 + mSession.mSurfaceSession + " window " + this
6950 + " w=" + mFrame.width()
6951 + " h=" + mFrame.height() + " format="
6952 + mAttrs.format + " flags=" + flags);
6953
6954 int w = mFrame.width();
6955 int h = mFrame.height();
6956 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6957 // for a scaled surface, we always want the requested
6958 // size.
6959 w = mRequestedWidth;
6960 h = mRequestedHeight;
6961 }
6962
6963 try {
6964 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006965 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006967 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
6968 + mSurface + " IN SESSION "
6969 + mSession.mSurfaceSession
6970 + ": pid=" + mSession.mPid + " format="
6971 + mAttrs.format + " flags=0x"
6972 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 } catch (Surface.OutOfResourcesException e) {
6974 Log.w(TAG, "OutOfResourcesException creating surface");
6975 reclaimSomeSurfaceMemoryLocked(this, "create");
6976 return null;
6977 } catch (Exception e) {
6978 Log.e(TAG, "Exception creating surface", e);
6979 return null;
6980 }
Romain Guy06882f82009-06-10 13:36:04 -07006981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 if (localLOGV) Log.v(
6983 TAG, "Got surface: " + mSurface
6984 + ", set left=" + mFrame.left + " top=" + mFrame.top
6985 + ", animLayer=" + mAnimLayer);
6986 if (SHOW_TRANSACTIONS) {
6987 Log.i(TAG, ">>> OPEN TRANSACTION");
6988 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6989 + mAttrs.getTitle() + ") pos=(" +
6990 mFrame.left + "," + mFrame.top + ") (" +
6991 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6992 mAnimLayer + " HIDE");
6993 }
6994 Surface.openTransaction();
6995 try {
6996 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006997 mSurface.setPosition(mFrame.left + mXOffset,
6998 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 mSurface.setLayer(mAnimLayer);
7000 mSurface.hide();
7001 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007002 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7003 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 mSurface.setFlags(Surface.SURFACE_DITHER,
7005 Surface.SURFACE_DITHER);
7006 }
7007 } catch (RuntimeException e) {
7008 Log.w(TAG, "Error creating surface in " + w, e);
7009 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7010 }
7011 mLastHidden = true;
7012 } finally {
7013 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7014 Surface.closeTransaction();
7015 }
7016 if (localLOGV) Log.v(
7017 TAG, "Created surface " + this);
7018 }
7019 return mSurface;
7020 }
Romain Guy06882f82009-06-10 13:36:04 -07007021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 void destroySurfaceLocked() {
7023 // Window is no longer on-screen, so can no longer receive
7024 // key events... if we were waiting for it to finish
7025 // handling a key event, the wait is over!
7026 mKeyWaiter.finishedKey(mSession, mClient, true,
7027 KeyWaiter.RETURN_NOTHING);
7028 mKeyWaiter.releasePendingPointerLocked(mSession);
7029 mKeyWaiter.releasePendingTrackballLocked(mSession);
7030
7031 if (mAppToken != null && this == mAppToken.startingWindow) {
7032 mAppToken.startingDisplayed = false;
7033 }
Romain Guy06882f82009-06-10 13:36:04 -07007034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 if (mSurface != null) {
7036 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007037 if (DEBUG_VISIBILITY) {
7038 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007039 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007040 Log.w(TAG, "Window " + this + " destroying surface "
7041 + mSurface + ", session " + mSession, e);
7042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 if (SHOW_TRANSACTIONS) {
7044 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007045 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7047 + mAttrs.getTitle() + ")", ex);
7048 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007049 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 } catch (RuntimeException e) {
7051 Log.w(TAG, "Exception thrown when destroying Window " + this
7052 + " surface " + mSurface + " session " + mSession
7053 + ": " + e.toString());
7054 }
7055 mSurface = null;
7056 mDrawPending = false;
7057 mCommitDrawPending = false;
7058 mReadyToShow = false;
7059
7060 int i = mChildWindows.size();
7061 while (i > 0) {
7062 i--;
7063 WindowState c = (WindowState)mChildWindows.get(i);
7064 c.mAttachedHidden = true;
7065 }
7066 }
7067 }
7068
7069 boolean finishDrawingLocked() {
7070 if (mDrawPending) {
7071 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7072 TAG, "finishDrawingLocked: " + mSurface);
7073 mCommitDrawPending = true;
7074 mDrawPending = false;
7075 return true;
7076 }
7077 return false;
7078 }
7079
7080 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007081 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7083 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007084 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 }
7086 mCommitDrawPending = false;
7087 mReadyToShow = true;
7088 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7089 final AppWindowToken atoken = mAppToken;
7090 if (atoken == null || atoken.allDrawn || starting) {
7091 performShowLocked();
7092 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007093 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007094 }
7095
7096 // This must be called while inside a transaction.
7097 boolean performShowLocked() {
7098 if (DEBUG_VISIBILITY) {
7099 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007100 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 Log.v(TAG, "performShow on " + this
7102 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7103 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7104 }
7105 if (mReadyToShow && isReadyForDisplay()) {
7106 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7107 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7108 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7109 + " during animation: policyVis=" + mPolicyVisibility
7110 + " attHidden=" + mAttachedHidden
7111 + " tok.hiddenRequested="
7112 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007113 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 + (mAppToken != null ? mAppToken.hidden : false)
7115 + " animating=" + mAnimating
7116 + " tok animating="
7117 + (mAppToken != null ? mAppToken.animating : false));
7118 if (!showSurfaceRobustlyLocked(this)) {
7119 return false;
7120 }
7121 mLastAlpha = -1;
7122 mHasDrawn = true;
7123 mLastHidden = false;
7124 mReadyToShow = false;
7125 enableScreenIfNeededLocked();
7126
7127 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 int i = mChildWindows.size();
7130 while (i > 0) {
7131 i--;
7132 WindowState c = (WindowState)mChildWindows.get(i);
7133 if (c.mSurface != null && c.mAttachedHidden) {
7134 c.mAttachedHidden = false;
7135 c.performShowLocked();
7136 }
7137 }
Romain Guy06882f82009-06-10 13:36:04 -07007138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 if (mAttrs.type != TYPE_APPLICATION_STARTING
7140 && mAppToken != null) {
7141 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007142
7143 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007144 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7145 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007147 // If this initial window is animating, stop it -- we
7148 // will do an animation to reveal it from behind the
7149 // starting window, so there is no need for it to also
7150 // be doing its own stuff.
7151 if (mAnimation != null) {
7152 mAnimation = null;
7153 // Make sure we clean up the animation.
7154 mAnimating = true;
7155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 mFinishedStarting.add(mAppToken);
7157 mH.sendEmptyMessage(H.FINISHED_STARTING);
7158 }
7159 mAppToken.updateReportedVisibilityLocked();
7160 }
7161 }
7162 return true;
7163 }
Romain Guy06882f82009-06-10 13:36:04 -07007164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 // This must be called while inside a transaction. Returns true if
7166 // there is more animation to run.
7167 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7168 if (!mDisplayFrozen) {
7169 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7172 mHasTransformation = true;
7173 mHasLocalTransformation = true;
7174 if (!mLocalAnimating) {
7175 if (DEBUG_ANIM) Log.v(
7176 TAG, "Starting animation in " + this +
7177 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7178 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7179 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7180 mAnimation.setStartTime(currentTime);
7181 mLocalAnimating = true;
7182 mAnimating = true;
7183 }
7184 mTransformation.clear();
7185 final boolean more = mAnimation.getTransformation(
7186 currentTime, mTransformation);
7187 if (DEBUG_ANIM) Log.v(
7188 TAG, "Stepped animation in " + this +
7189 ": more=" + more + ", xform=" + mTransformation);
7190 if (more) {
7191 // we're not done!
7192 return true;
7193 }
7194 if (DEBUG_ANIM) Log.v(
7195 TAG, "Finished animation in " + this +
7196 " @ " + currentTime);
7197 mAnimation = null;
7198 //WindowManagerService.this.dump();
7199 }
7200 mHasLocalTransformation = false;
7201 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007202 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007203 // When our app token is animating, we kind-of pretend like
7204 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7205 // part of this check means that we will only do this if
7206 // our window is not currently exiting, or it is not
7207 // locally animating itself. The idea being that one that
7208 // is exiting and doing a local animation should be removed
7209 // once that animation is done.
7210 mAnimating = true;
7211 mHasTransformation = true;
7212 mTransformation.clear();
7213 return false;
7214 } else if (mHasTransformation) {
7215 // Little trick to get through the path below to act like
7216 // we have finished an animation.
7217 mAnimating = true;
7218 } else if (isAnimating()) {
7219 mAnimating = true;
7220 }
7221 } else if (mAnimation != null) {
7222 // If the display is frozen, and there is a pending animation,
7223 // clear it and make sure we run the cleanup code.
7224 mAnimating = true;
7225 mLocalAnimating = true;
7226 mAnimation = null;
7227 }
Romain Guy06882f82009-06-10 13:36:04 -07007228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007229 if (!mAnimating && !mLocalAnimating) {
7230 return false;
7231 }
7232
7233 if (DEBUG_ANIM) Log.v(
7234 TAG, "Animation done in " + this + ": exiting=" + mExiting
7235 + ", reportedVisible="
7236 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007238 mAnimating = false;
7239 mLocalAnimating = false;
7240 mAnimation = null;
7241 mAnimLayer = mLayer;
7242 if (mIsImWindow) {
7243 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007244 } else if (mIsWallpaper) {
7245 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007246 }
7247 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7248 + " anim layer: " + mAnimLayer);
7249 mHasTransformation = false;
7250 mHasLocalTransformation = false;
7251 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7252 mTransformation.clear();
7253 if (mHasDrawn
7254 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7255 && mAppToken != null
7256 && mAppToken.firstWindowDrawn
7257 && mAppToken.startingData != null) {
7258 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7259 + mToken + ": first real window done animating");
7260 mFinishedStarting.add(mAppToken);
7261 mH.sendEmptyMessage(H.FINISHED_STARTING);
7262 }
Romain Guy06882f82009-06-10 13:36:04 -07007263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 finishExit();
7265
7266 if (mAppToken != null) {
7267 mAppToken.updateReportedVisibilityLocked();
7268 }
7269
7270 return false;
7271 }
7272
7273 void finishExit() {
7274 if (DEBUG_ANIM) Log.v(
7275 TAG, "finishExit in " + this
7276 + ": exiting=" + mExiting
7277 + " remove=" + mRemoveOnExit
7278 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 final int N = mChildWindows.size();
7281 for (int i=0; i<N; i++) {
7282 ((WindowState)mChildWindows.get(i)).finishExit();
7283 }
Romain Guy06882f82009-06-10 13:36:04 -07007284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 if (!mExiting) {
7286 return;
7287 }
Romain Guy06882f82009-06-10 13:36:04 -07007288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007289 if (isWindowAnimating()) {
7290 return;
7291 }
7292
7293 if (localLOGV) Log.v(
7294 TAG, "Exit animation finished in " + this
7295 + ": remove=" + mRemoveOnExit);
7296 if (mSurface != null) {
7297 mDestroySurface.add(this);
7298 mDestroying = true;
7299 if (SHOW_TRANSACTIONS) Log.i(
7300 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7301 try {
7302 mSurface.hide();
7303 } catch (RuntimeException e) {
7304 Log.w(TAG, "Error hiding surface in " + this, e);
7305 }
7306 mLastHidden = true;
7307 mKeyWaiter.releasePendingPointerLocked(mSession);
7308 }
7309 mExiting = false;
7310 if (mRemoveOnExit) {
7311 mPendingRemove.add(this);
7312 mRemoveOnExit = false;
7313 }
7314 }
Romain Guy06882f82009-06-10 13:36:04 -07007315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7317 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7318 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7319 if (dtdx < -.000001f || dtdx > .000001f) return false;
7320 if (dsdy < -.000001f || dsdy > .000001f) return false;
7321 return true;
7322 }
Romain Guy06882f82009-06-10 13:36:04 -07007323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007324 void computeShownFrameLocked() {
7325 final boolean selfTransformation = mHasLocalTransformation;
7326 Transformation attachedTransformation =
7327 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7328 ? mAttachedWindow.mTransformation : null;
7329 Transformation appTransformation =
7330 (mAppToken != null && mAppToken.hasTransformation)
7331 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007332
7333 // Wallpapers are animated based on the "real" window they
7334 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007335 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007336 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007337 if (mWallpaperTarget.mHasLocalTransformation &&
7338 mWallpaperTarget.mAnimation != null &&
7339 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007340 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007341 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7342 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7343 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007344 }
7345 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007346 mWallpaperTarget.mAppToken.hasTransformation &&
7347 mWallpaperTarget.mAppToken.animation != null &&
7348 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007349 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007350 if (DEBUG_WALLPAPER && appTransformation != null) {
7351 Log.v(TAG, "WP target app xform: " + appTransformation);
7352 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007353 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007354 }
7355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007356 if (selfTransformation || attachedTransformation != null
7357 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007358 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 final Rect frame = mFrame;
7360 final float tmpFloats[] = mTmpFloats;
7361 final Matrix tmpMatrix = mTmpMatrix;
7362
7363 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007364 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007366 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007368 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007370 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 }
7372 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007373 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 }
7375
7376 // "convert" it into SurfaceFlinger's format
7377 // (a 2x2 matrix + an offset)
7378 // Here we must not transform the position of the surface
7379 // since it is already included in the transformation.
7380 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 tmpMatrix.getValues(tmpFloats);
7383 mDsDx = tmpFloats[Matrix.MSCALE_X];
7384 mDtDx = tmpFloats[Matrix.MSKEW_X];
7385 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7386 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007387 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7388 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 int w = frame.width();
7390 int h = frame.height();
7391 mShownFrame.set(x, y, x+w, y+h);
7392
7393 // Now set the alpha... but because our current hardware
7394 // can't do alpha transformation on a non-opaque surface,
7395 // turn it off if we are running an animation that is also
7396 // transforming since it is more important to have that
7397 // animation be smooth.
7398 mShownAlpha = mAlpha;
7399 if (!mLimitedAlphaCompositing
7400 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7401 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7402 && x == frame.left && y == frame.top))) {
7403 //Log.i(TAG, "Applying alpha transform");
7404 if (selfTransformation) {
7405 mShownAlpha *= mTransformation.getAlpha();
7406 }
7407 if (attachedTransformation != null) {
7408 mShownAlpha *= attachedTransformation.getAlpha();
7409 }
7410 if (appTransformation != null) {
7411 mShownAlpha *= appTransformation.getAlpha();
7412 }
7413 } else {
7414 //Log.i(TAG, "Not applying alpha transform");
7415 }
Romain Guy06882f82009-06-10 13:36:04 -07007416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007417 if (localLOGV) Log.v(
7418 TAG, "Continuing animation in " + this +
7419 ": " + mShownFrame +
7420 ", alpha=" + mTransformation.getAlpha());
7421 return;
7422 }
Romain Guy06882f82009-06-10 13:36:04 -07007423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007425 if (mXOffset != 0 || mYOffset != 0) {
7426 mShownFrame.offset(mXOffset, mYOffset);
7427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 mShownAlpha = mAlpha;
7429 mDsDx = 1;
7430 mDtDx = 0;
7431 mDsDy = 0;
7432 mDtDy = 1;
7433 }
Romain Guy06882f82009-06-10 13:36:04 -07007434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 /**
7436 * Is this window visible? It is not visible if there is no
7437 * surface, or we are in the process of running an exit animation
7438 * that will remove the surface, or its app token has been hidden.
7439 */
7440 public boolean isVisibleLw() {
7441 final AppWindowToken atoken = mAppToken;
7442 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7443 && (atoken == null || !atoken.hiddenRequested)
7444 && !mExiting && !mDestroying;
7445 }
7446
7447 /**
7448 * Is this window visible, ignoring its app token? It is not visible
7449 * if there is no surface, or we are in the process of running an exit animation
7450 * that will remove the surface.
7451 */
7452 public boolean isWinVisibleLw() {
7453 final AppWindowToken atoken = mAppToken;
7454 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7455 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7456 && !mExiting && !mDestroying;
7457 }
7458
7459 /**
7460 * The same as isVisible(), but follows the current hidden state of
7461 * the associated app token, not the pending requested hidden state.
7462 */
7463 boolean isVisibleNow() {
7464 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007465 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 }
7467
7468 /**
7469 * Same as isVisible(), but we also count it as visible between the
7470 * call to IWindowSession.add() and the first relayout().
7471 */
7472 boolean isVisibleOrAdding() {
7473 final AppWindowToken atoken = mAppToken;
7474 return (mSurface != null
7475 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7476 && mPolicyVisibility && !mAttachedHidden
7477 && (atoken == null || !atoken.hiddenRequested)
7478 && !mExiting && !mDestroying;
7479 }
7480
7481 /**
7482 * Is this window currently on-screen? It is on-screen either if it
7483 * is visible or it is currently running an animation before no longer
7484 * being visible.
7485 */
7486 boolean isOnScreen() {
7487 final AppWindowToken atoken = mAppToken;
7488 if (atoken != null) {
7489 return mSurface != null && mPolicyVisibility && !mDestroying
7490 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007491 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 } else {
7493 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007494 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 }
7496 }
Romain Guy06882f82009-06-10 13:36:04 -07007497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007498 /**
7499 * Like isOnScreen(), but we don't return true if the window is part
7500 * of a transition that has not yet been started.
7501 */
7502 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007503 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007504 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007505 return false;
7506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007508 final boolean animating = atoken != null
7509 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007511 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7512 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007513 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 }
7515
7516 /** Is the window or its container currently animating? */
7517 boolean isAnimating() {
7518 final WindowState attached = mAttachedWindow;
7519 final AppWindowToken atoken = mAppToken;
7520 return mAnimation != null
7521 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007522 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 (atoken.animation != null
7524 || atoken.inPendingTransaction));
7525 }
7526
7527 /** Is this window currently animating? */
7528 boolean isWindowAnimating() {
7529 return mAnimation != null;
7530 }
7531
7532 /**
7533 * Like isOnScreen, but returns false if the surface hasn't yet
7534 * been drawn.
7535 */
7536 public boolean isDisplayedLw() {
7537 final AppWindowToken atoken = mAppToken;
7538 return mSurface != null && mPolicyVisibility && !mDestroying
7539 && !mDrawPending && !mCommitDrawPending
7540 && ((!mAttachedHidden &&
7541 (atoken == null || !atoken.hiddenRequested))
7542 || mAnimating);
7543 }
7544
7545 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7546 boolean shownFrame, boolean onlyOpaque) {
7547 if (mSurface == null) {
7548 return false;
7549 }
7550 if (mAppToken != null && !mAppToken.appFullscreen) {
7551 return false;
7552 }
7553 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7554 return false;
7555 }
7556 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007557
7558 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7559 return frame.left <= mCompatibleScreenFrame.left &&
7560 frame.top <= mCompatibleScreenFrame.top &&
7561 frame.right >= mCompatibleScreenFrame.right &&
7562 frame.bottom >= mCompatibleScreenFrame.bottom;
7563 } else {
7564 return frame.left <= 0 && frame.top <= 0
7565 && frame.right >= screenWidth
7566 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 }
Romain Guy06882f82009-06-10 13:36:04 -07007569
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007570 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007571 * Return true if the window is opaque and fully drawn. This indicates
7572 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007573 */
7574 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007575 return (mAttrs.format == PixelFormat.OPAQUE
7576 || mAttrs.type == TYPE_WALLPAPER)
7577 && mSurface != null && mAnimation == null
7578 && (mAppToken == null || mAppToken.animation == null)
7579 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007580 }
7581
7582 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7583 return
7584 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007585 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7586 // only if it's visible
7587 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007588 // and only if the application fills the compatible screen
7589 mFrame.left <= mCompatibleScreenFrame.left &&
7590 mFrame.top <= mCompatibleScreenFrame.top &&
7591 mFrame.right >= mCompatibleScreenFrame.right &&
7592 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007593 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007594 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007595 }
7596
7597 boolean isFullscreen(int screenWidth, int screenHeight) {
7598 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007599 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 }
7601
7602 void removeLocked() {
7603 if (mAttachedWindow != null) {
7604 mAttachedWindow.mChildWindows.remove(this);
7605 }
7606 destroySurfaceLocked();
7607 mSession.windowRemovedLocked();
7608 try {
7609 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7610 } catch (RuntimeException e) {
7611 // Ignore if it has already been removed (usually because
7612 // we are doing this as part of processing a death note.)
7613 }
7614 }
7615
7616 private class DeathRecipient implements IBinder.DeathRecipient {
7617 public void binderDied() {
7618 try {
7619 synchronized(mWindowMap) {
7620 WindowState win = windowForClientLocked(mSession, mClient);
7621 Log.i(TAG, "WIN DEATH: " + win);
7622 if (win != null) {
7623 removeWindowLocked(mSession, win);
7624 }
7625 }
7626 } catch (IllegalArgumentException ex) {
7627 // This will happen if the window has already been
7628 // removed.
7629 }
7630 }
7631 }
7632
7633 /** Returns true if this window desires key events. */
7634 public final boolean canReceiveKeys() {
7635 return isVisibleOrAdding()
7636 && (mViewVisibility == View.VISIBLE)
7637 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7638 }
7639
7640 public boolean hasDrawnLw() {
7641 return mHasDrawn;
7642 }
7643
7644 public boolean showLw(boolean doAnimation) {
7645 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7646 mPolicyVisibility = true;
7647 mPolicyVisibilityAfterAnim = true;
7648 if (doAnimation) {
7649 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7650 }
7651 requestAnimationLocked(0);
7652 return true;
7653 }
7654 return false;
7655 }
7656
7657 public boolean hideLw(boolean doAnimation) {
7658 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7659 : mPolicyVisibility;
7660 if (current) {
7661 if (doAnimation) {
7662 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7663 if (mAnimation == null) {
7664 doAnimation = false;
7665 }
7666 }
7667 if (doAnimation) {
7668 mPolicyVisibilityAfterAnim = false;
7669 } else {
7670 mPolicyVisibilityAfterAnim = false;
7671 mPolicyVisibility = false;
7672 }
7673 requestAnimationLocked(0);
7674 return true;
7675 }
7676 return false;
7677 }
7678
7679 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007680 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007681
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007682 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7683 pw.print(" mClient="); pw.println(mClient.asBinder());
7684 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7685 if (mAttachedWindow != null || mLayoutAttached) {
7686 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7687 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7688 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007689 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7690 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7691 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007692 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7693 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007694 }
7695 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7696 pw.print(" mSubLayer="); pw.print(mSubLayer);
7697 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7698 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7699 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7700 pw.print("="); pw.print(mAnimLayer);
7701 pw.print(" mLastLayer="); pw.println(mLastLayer);
7702 if (mSurface != null) {
7703 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7704 }
7705 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7706 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7707 if (mAppToken != null) {
7708 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7709 }
7710 if (mTargetAppToken != null) {
7711 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7712 }
7713 pw.print(prefix); pw.print("mViewVisibility=0x");
7714 pw.print(Integer.toHexString(mViewVisibility));
7715 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007716 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7717 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007718 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7719 pw.print(prefix); pw.print("mPolicyVisibility=");
7720 pw.print(mPolicyVisibility);
7721 pw.print(" mPolicyVisibilityAfterAnim=");
7722 pw.print(mPolicyVisibilityAfterAnim);
7723 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7724 }
7725 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007726 pw.print(" h="); pw.println(mRequestedHeight);
7727 if (mXOffset != 0 || mYOffset != 0) {
7728 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7729 pw.print(" y="); pw.println(mYOffset);
7730 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007731 pw.print(prefix); pw.print("mGivenContentInsets=");
7732 mGivenContentInsets.printShortString(pw);
7733 pw.print(" mGivenVisibleInsets=");
7734 mGivenVisibleInsets.printShortString(pw);
7735 pw.println();
7736 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7737 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7738 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7739 }
7740 pw.print(prefix); pw.print("mShownFrame=");
7741 mShownFrame.printShortString(pw);
7742 pw.print(" last="); mLastShownFrame.printShortString(pw);
7743 pw.println();
7744 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7745 pw.print(" last="); mLastFrame.printShortString(pw);
7746 pw.println();
7747 pw.print(prefix); pw.print("mContainingFrame=");
7748 mContainingFrame.printShortString(pw);
7749 pw.print(" mDisplayFrame=");
7750 mDisplayFrame.printShortString(pw);
7751 pw.println();
7752 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7753 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7754 pw.println();
7755 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7756 pw.print(" last="); mLastContentInsets.printShortString(pw);
7757 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7758 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7759 pw.println();
7760 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7761 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7762 pw.print(" mAlpha="); pw.print(mAlpha);
7763 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7764 }
7765 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7766 || mAnimation != null) {
7767 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7768 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7769 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7770 pw.print(" mAnimation="); pw.println(mAnimation);
7771 }
7772 if (mHasTransformation || mHasLocalTransformation) {
7773 pw.print(prefix); pw.print("XForm: has=");
7774 pw.print(mHasTransformation);
7775 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7776 pw.print(" "); mTransformation.printShortString(pw);
7777 pw.println();
7778 }
7779 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7780 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7781 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7782 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7783 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7784 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7785 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7786 pw.print(" mDestroying="); pw.print(mDestroying);
7787 pw.print(" mRemoved="); pw.println(mRemoved);
7788 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007789 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007790 pw.print(prefix); pw.print("mOrientationChanging=");
7791 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007792 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7793 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007794 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007795 if (mHScale != 1 || mVScale != 1) {
7796 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7797 pw.print(" mVScale="); pw.println(mVScale);
7798 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007799 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007800 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7801 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 }
7804
7805 @Override
7806 public String toString() {
7807 return "Window{"
7808 + Integer.toHexString(System.identityHashCode(this))
7809 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7810 }
7811 }
Romain Guy06882f82009-06-10 13:36:04 -07007812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 // -------------------------------------------------------------
7814 // Window Token State
7815 // -------------------------------------------------------------
7816
7817 class WindowToken {
7818 // The actual token.
7819 final IBinder token;
7820
7821 // The type of window this token is for, as per WindowManager.LayoutParams.
7822 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 // Set if this token was explicitly added by a client, so should
7825 // not be removed when all windows are removed.
7826 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007827
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007828 // For printing.
7829 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 // If this is an AppWindowToken, this is non-null.
7832 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 // All of the windows associated with this token.
7835 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7836
7837 // Is key dispatching paused for this token?
7838 boolean paused = false;
7839
7840 // Should this token's windows be hidden?
7841 boolean hidden;
7842
7843 // Temporary for finding which tokens no longer have visible windows.
7844 boolean hasVisible;
7845
Dianne Hackborna8f60182009-09-01 19:01:50 -07007846 // Set to true when this token is in a pending transaction where it
7847 // will be shown.
7848 boolean waitingToShow;
7849
7850 // Set to true when this token is in a pending transaction where it
7851 // will be hidden.
7852 boolean waitingToHide;
7853
7854 // Set to true when this token is in a pending transaction where its
7855 // windows will be put to the bottom of the list.
7856 boolean sendingToBottom;
7857
7858 // Set to true when this token is in a pending transaction where its
7859 // windows will be put to the top of the list.
7860 boolean sendingToTop;
7861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 WindowToken(IBinder _token, int type, boolean _explicit) {
7863 token = _token;
7864 windowType = type;
7865 explicit = _explicit;
7866 }
7867
7868 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007869 pw.print(prefix); pw.print("token="); pw.println(token);
7870 pw.print(prefix); pw.print("windows="); pw.println(windows);
7871 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7872 pw.print(" hidden="); pw.print(hidden);
7873 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007874 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7875 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7876 pw.print(" waitingToHide="); pw.print(waitingToHide);
7877 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7878 pw.print(" sendingToTop="); pw.println(sendingToTop);
7879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 }
7881
7882 @Override
7883 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007884 if (stringName == null) {
7885 StringBuilder sb = new StringBuilder();
7886 sb.append("WindowToken{");
7887 sb.append(Integer.toHexString(System.identityHashCode(this)));
7888 sb.append(" token="); sb.append(token); sb.append('}');
7889 stringName = sb.toString();
7890 }
7891 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892 }
7893 };
7894
7895 class AppWindowToken extends WindowToken {
7896 // Non-null only for application tokens.
7897 final IApplicationToken appToken;
7898
7899 // All of the windows and child windows that are included in this
7900 // application token. Note this list is NOT sorted!
7901 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7902
7903 int groupId = -1;
7904 boolean appFullscreen;
7905 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 // These are used for determining when all windows associated with
7908 // an activity have been drawn, so they can be made visible together
7909 // at the same time.
7910 int lastTransactionSequence = mTransactionSequence-1;
7911 int numInterestingWindows;
7912 int numDrawnWindows;
7913 boolean inPendingTransaction;
7914 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 // Is this token going to be hidden in a little while? If so, it
7917 // won't be taken into account for setting the screen orientation.
7918 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 // Is this window's surface needed? This is almost like hidden, except
7921 // it will sometimes be true a little earlier: when the token has
7922 // been shown, but is still waiting for its app transition to execute
7923 // before making its windows shown.
7924 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007926 // Have we told the window clients to hide themselves?
7927 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 // Last visibility state we reported to the app token.
7930 boolean reportedVisible;
7931
7932 // Set to true when the token has been removed from the window mgr.
7933 boolean removed;
7934
7935 // Have we been asked to have this token keep the screen frozen?
7936 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 boolean animating;
7939 Animation animation;
7940 boolean hasTransformation;
7941 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 // Offset to the window of all layers in the token, for use by
7944 // AppWindowToken animations.
7945 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 // Information about an application starting window if displayed.
7948 StartingData startingData;
7949 WindowState startingWindow;
7950 View startingView;
7951 boolean startingDisplayed;
7952 boolean startingMoved;
7953 boolean firstWindowDrawn;
7954
7955 AppWindowToken(IApplicationToken _token) {
7956 super(_token.asBinder(),
7957 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7958 appWindowToken = this;
7959 appToken = _token;
7960 }
Romain Guy06882f82009-06-10 13:36:04 -07007961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007962 public void setAnimation(Animation anim) {
7963 if (localLOGV) Log.v(
7964 TAG, "Setting animation in " + this + ": " + anim);
7965 animation = anim;
7966 animating = false;
7967 anim.restrictDuration(MAX_ANIMATION_DURATION);
7968 anim.scaleCurrentDuration(mTransitionAnimationScale);
7969 int zorder = anim.getZAdjustment();
7970 int adj = 0;
7971 if (zorder == Animation.ZORDER_TOP) {
7972 adj = TYPE_LAYER_OFFSET;
7973 } else if (zorder == Animation.ZORDER_BOTTOM) {
7974 adj = -TYPE_LAYER_OFFSET;
7975 }
Romain Guy06882f82009-06-10 13:36:04 -07007976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 if (animLayerAdjustment != adj) {
7978 animLayerAdjustment = adj;
7979 updateLayers();
7980 }
7981 }
Romain Guy06882f82009-06-10 13:36:04 -07007982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 public void setDummyAnimation() {
7984 if (animation == null) {
7985 if (localLOGV) Log.v(
7986 TAG, "Setting dummy animation in " + this);
7987 animation = sDummyAnimation;
7988 }
7989 }
7990
7991 public void clearAnimation() {
7992 if (animation != null) {
7993 animation = null;
7994 animating = true;
7995 }
7996 }
Romain Guy06882f82009-06-10 13:36:04 -07007997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 void updateLayers() {
7999 final int N = allAppWindows.size();
8000 final int adj = animLayerAdjustment;
8001 for (int i=0; i<N; i++) {
8002 WindowState w = allAppWindows.get(i);
8003 w.mAnimLayer = w.mLayer + adj;
8004 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8005 + w.mAnimLayer);
8006 if (w == mInputMethodTarget) {
8007 setInputMethodAnimLayerAdjustment(adj);
8008 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008009 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008010 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 }
8013 }
Romain Guy06882f82009-06-10 13:36:04 -07008014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 void sendAppVisibilityToClients() {
8016 final int N = allAppWindows.size();
8017 for (int i=0; i<N; i++) {
8018 WindowState win = allAppWindows.get(i);
8019 if (win == startingWindow && clientHidden) {
8020 // Don't hide the starting window.
8021 continue;
8022 }
8023 try {
8024 if (DEBUG_VISIBILITY) Log.v(TAG,
8025 "Setting visibility of " + win + ": " + (!clientHidden));
8026 win.mClient.dispatchAppVisibility(!clientHidden);
8027 } catch (RemoteException e) {
8028 }
8029 }
8030 }
Romain Guy06882f82009-06-10 13:36:04 -07008031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 void showAllWindowsLocked() {
8033 final int NW = allAppWindows.size();
8034 for (int i=0; i<NW; i++) {
8035 WindowState w = allAppWindows.get(i);
8036 if (DEBUG_VISIBILITY) Log.v(TAG,
8037 "performing show on: " + w);
8038 w.performShowLocked();
8039 }
8040 }
Romain Guy06882f82009-06-10 13:36:04 -07008041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 // This must be called while inside a transaction.
8043 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8044 if (!mDisplayFrozen) {
8045 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 if (animation == sDummyAnimation) {
8048 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008049 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 // when it is really time to animate, this will be set to
8051 // a real animation and the next call will execute normally.
8052 return false;
8053 }
Romain Guy06882f82009-06-10 13:36:04 -07008054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8056 if (!animating) {
8057 if (DEBUG_ANIM) Log.v(
8058 TAG, "Starting animation in " + this +
8059 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8060 + " scale=" + mTransitionAnimationScale
8061 + " allDrawn=" + allDrawn + " animating=" + animating);
8062 animation.initialize(dw, dh, dw, dh);
8063 animation.setStartTime(currentTime);
8064 animating = true;
8065 }
8066 transformation.clear();
8067 final boolean more = animation.getTransformation(
8068 currentTime, transformation);
8069 if (DEBUG_ANIM) Log.v(
8070 TAG, "Stepped animation in " + this +
8071 ": more=" + more + ", xform=" + transformation);
8072 if (more) {
8073 // we're done!
8074 hasTransformation = true;
8075 return true;
8076 }
8077 if (DEBUG_ANIM) Log.v(
8078 TAG, "Finished animation in " + this +
8079 " @ " + currentTime);
8080 animation = null;
8081 }
8082 } else if (animation != null) {
8083 // If the display is frozen, and there is a pending animation,
8084 // clear it and make sure we run the cleanup code.
8085 animating = true;
8086 animation = null;
8087 }
8088
8089 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091 if (!animating) {
8092 return false;
8093 }
8094
8095 clearAnimation();
8096 animating = false;
8097 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8098 moveInputMethodWindowsIfNeededLocked(true);
8099 }
Romain Guy06882f82009-06-10 13:36:04 -07008100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008101 if (DEBUG_ANIM) Log.v(
8102 TAG, "Animation done in " + this
8103 + ": reportedVisible=" + reportedVisible);
8104
8105 transformation.clear();
8106 if (animLayerAdjustment != 0) {
8107 animLayerAdjustment = 0;
8108 updateLayers();
8109 }
Romain Guy06882f82009-06-10 13:36:04 -07008110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 final int N = windows.size();
8112 for (int i=0; i<N; i++) {
8113 ((WindowState)windows.get(i)).finishExit();
8114 }
8115 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 return false;
8118 }
8119
8120 void updateReportedVisibilityLocked() {
8121 if (appToken == null) {
8122 return;
8123 }
Romain Guy06882f82009-06-10 13:36:04 -07008124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008125 int numInteresting = 0;
8126 int numVisible = 0;
8127 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8130 final int N = allAppWindows.size();
8131 for (int i=0; i<N; i++) {
8132 WindowState win = allAppWindows.get(i);
8133 if (win == startingWindow || win.mAppFreezing) {
8134 continue;
8135 }
8136 if (DEBUG_VISIBILITY) {
8137 Log.v(TAG, "Win " + win + ": isDisplayed="
8138 + win.isDisplayedLw()
8139 + ", isAnimating=" + win.isAnimating());
8140 if (!win.isDisplayedLw()) {
8141 Log.v(TAG, "Not displayed: s=" + win.mSurface
8142 + " pv=" + win.mPolicyVisibility
8143 + " dp=" + win.mDrawPending
8144 + " cdp=" + win.mCommitDrawPending
8145 + " ah=" + win.mAttachedHidden
8146 + " th="
8147 + (win.mAppToken != null
8148 ? win.mAppToken.hiddenRequested : false)
8149 + " a=" + win.mAnimating);
8150 }
8151 }
8152 numInteresting++;
8153 if (win.isDisplayedLw()) {
8154 if (!win.isAnimating()) {
8155 numVisible++;
8156 }
8157 nowGone = false;
8158 } else if (win.isAnimating()) {
8159 nowGone = false;
8160 }
8161 }
Romain Guy06882f82009-06-10 13:36:04 -07008162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8164 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8165 + numInteresting + " visible=" + numVisible);
8166 if (nowVisible != reportedVisible) {
8167 if (DEBUG_VISIBILITY) Log.v(
8168 TAG, "Visibility changed in " + this
8169 + ": vis=" + nowVisible);
8170 reportedVisible = nowVisible;
8171 Message m = mH.obtainMessage(
8172 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8173 nowVisible ? 1 : 0,
8174 nowGone ? 1 : 0,
8175 this);
8176 mH.sendMessage(m);
8177 }
8178 }
Romain Guy06882f82009-06-10 13:36:04 -07008179
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008180 WindowState findMainWindow() {
8181 int j = windows.size();
8182 while (j > 0) {
8183 j--;
8184 WindowState win = windows.get(j);
8185 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8186 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8187 return win;
8188 }
8189 }
8190 return null;
8191 }
8192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 void dump(PrintWriter pw, String prefix) {
8194 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008195 if (appToken != null) {
8196 pw.print(prefix); pw.println("app=true");
8197 }
8198 if (allAppWindows.size() > 0) {
8199 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8200 }
8201 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008202 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008203 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8204 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8205 pw.print(" clientHidden="); pw.print(clientHidden);
8206 pw.print(" willBeHidden="); pw.print(willBeHidden);
8207 pw.print(" reportedVisible="); pw.println(reportedVisible);
8208 if (paused || freezingScreen) {
8209 pw.print(prefix); pw.print("paused="); pw.print(paused);
8210 pw.print(" freezingScreen="); pw.println(freezingScreen);
8211 }
8212 if (numInterestingWindows != 0 || numDrawnWindows != 0
8213 || inPendingTransaction || allDrawn) {
8214 pw.print(prefix); pw.print("numInterestingWindows=");
8215 pw.print(numInterestingWindows);
8216 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8217 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8218 pw.print(" allDrawn="); pw.println(allDrawn);
8219 }
8220 if (animating || animation != null) {
8221 pw.print(prefix); pw.print("animating="); pw.print(animating);
8222 pw.print(" animation="); pw.println(animation);
8223 }
8224 if (animLayerAdjustment != 0) {
8225 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8226 }
8227 if (hasTransformation) {
8228 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8229 pw.print(" transformation="); transformation.printShortString(pw);
8230 pw.println();
8231 }
8232 if (startingData != null || removed || firstWindowDrawn) {
8233 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8234 pw.print(" removed="); pw.print(removed);
8235 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8236 }
8237 if (startingWindow != null || startingView != null
8238 || startingDisplayed || startingMoved) {
8239 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8240 pw.print(" startingView="); pw.print(startingView);
8241 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8242 pw.print(" startingMoved"); pw.println(startingMoved);
8243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008244 }
8245
8246 @Override
8247 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008248 if (stringName == null) {
8249 StringBuilder sb = new StringBuilder();
8250 sb.append("AppWindowToken{");
8251 sb.append(Integer.toHexString(System.identityHashCode(this)));
8252 sb.append(" token="); sb.append(token); sb.append('}');
8253 stringName = sb.toString();
8254 }
8255 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 }
8257 }
Romain Guy06882f82009-06-10 13:36:04 -07008258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 // -------------------------------------------------------------
8260 // DummyAnimation
8261 // -------------------------------------------------------------
8262
8263 // This is an animation that does nothing: it just immediately finishes
8264 // itself every time it is called. It is used as a stub animation in cases
8265 // where we want to synchronize multiple things that may be animating.
8266 static final class DummyAnimation extends Animation {
8267 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8268 return false;
8269 }
8270 }
8271 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 // -------------------------------------------------------------
8274 // Async Handler
8275 // -------------------------------------------------------------
8276
8277 static final class StartingData {
8278 final String pkg;
8279 final int theme;
8280 final CharSequence nonLocalizedLabel;
8281 final int labelRes;
8282 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8285 int _labelRes, int _icon) {
8286 pkg = _pkg;
8287 theme = _theme;
8288 nonLocalizedLabel = _nonLocalizedLabel;
8289 labelRes = _labelRes;
8290 icon = _icon;
8291 }
8292 }
8293
8294 private final class H extends Handler {
8295 public static final int REPORT_FOCUS_CHANGE = 2;
8296 public static final int REPORT_LOSING_FOCUS = 3;
8297 public static final int ANIMATE = 4;
8298 public static final int ADD_STARTING = 5;
8299 public static final int REMOVE_STARTING = 6;
8300 public static final int FINISHED_STARTING = 7;
8301 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8303 public static final int HOLD_SCREEN_CHANGED = 12;
8304 public static final int APP_TRANSITION_TIMEOUT = 13;
8305 public static final int PERSIST_ANIMATION_SCALE = 14;
8306 public static final int FORCE_GC = 15;
8307 public static final int ENABLE_SCREEN = 16;
8308 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008309 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008313 public H() {
8314 }
Romain Guy06882f82009-06-10 13:36:04 -07008315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008316 @Override
8317 public void handleMessage(Message msg) {
8318 switch (msg.what) {
8319 case REPORT_FOCUS_CHANGE: {
8320 WindowState lastFocus;
8321 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 synchronized(mWindowMap) {
8324 lastFocus = mLastFocus;
8325 newFocus = mCurrentFocus;
8326 if (lastFocus == newFocus) {
8327 // Focus is not changing, so nothing to do.
8328 return;
8329 }
8330 mLastFocus = newFocus;
8331 //Log.i(TAG, "Focus moving from " + lastFocus
8332 // + " to " + newFocus);
8333 if (newFocus != null && lastFocus != null
8334 && !newFocus.isDisplayedLw()) {
8335 //Log.i(TAG, "Delaying loss of focus...");
8336 mLosingFocus.add(lastFocus);
8337 lastFocus = null;
8338 }
8339 }
8340
8341 if (lastFocus != newFocus) {
8342 //System.out.println("Changing focus from " + lastFocus
8343 // + " to " + newFocus);
8344 if (newFocus != null) {
8345 try {
8346 //Log.i(TAG, "Gaining focus: " + newFocus);
8347 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8348 } catch (RemoteException e) {
8349 // Ignore if process has died.
8350 }
8351 }
8352
8353 if (lastFocus != null) {
8354 try {
8355 //Log.i(TAG, "Losing focus: " + lastFocus);
8356 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8357 } catch (RemoteException e) {
8358 // Ignore if process has died.
8359 }
8360 }
8361 }
8362 } break;
8363
8364 case REPORT_LOSING_FOCUS: {
8365 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 synchronized(mWindowMap) {
8368 losers = mLosingFocus;
8369 mLosingFocus = new ArrayList<WindowState>();
8370 }
8371
8372 final int N = losers.size();
8373 for (int i=0; i<N; i++) {
8374 try {
8375 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8376 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8377 } catch (RemoteException e) {
8378 // Ignore if process has died.
8379 }
8380 }
8381 } break;
8382
8383 case ANIMATE: {
8384 synchronized(mWindowMap) {
8385 mAnimationPending = false;
8386 performLayoutAndPlaceSurfacesLocked();
8387 }
8388 } break;
8389
8390 case ADD_STARTING: {
8391 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8392 final StartingData sd = wtoken.startingData;
8393
8394 if (sd == null) {
8395 // Animation has been canceled... do nothing.
8396 return;
8397 }
Romain Guy06882f82009-06-10 13:36:04 -07008398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8400 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402 View view = null;
8403 try {
8404 view = mPolicy.addStartingWindow(
8405 wtoken.token, sd.pkg,
8406 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8407 sd.icon);
8408 } catch (Exception e) {
8409 Log.w(TAG, "Exception when adding starting window", e);
8410 }
8411
8412 if (view != null) {
8413 boolean abort = false;
8414
8415 synchronized(mWindowMap) {
8416 if (wtoken.removed || wtoken.startingData == null) {
8417 // If the window was successfully added, then
8418 // we need to remove it.
8419 if (wtoken.startingWindow != null) {
8420 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8421 "Aborted starting " + wtoken
8422 + ": removed=" + wtoken.removed
8423 + " startingData=" + wtoken.startingData);
8424 wtoken.startingWindow = null;
8425 wtoken.startingData = null;
8426 abort = true;
8427 }
8428 } else {
8429 wtoken.startingView = view;
8430 }
8431 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8432 "Added starting " + wtoken
8433 + ": startingWindow="
8434 + wtoken.startingWindow + " startingView="
8435 + wtoken.startingView);
8436 }
8437
8438 if (abort) {
8439 try {
8440 mPolicy.removeStartingWindow(wtoken.token, view);
8441 } catch (Exception e) {
8442 Log.w(TAG, "Exception when removing starting window", e);
8443 }
8444 }
8445 }
8446 } break;
8447
8448 case REMOVE_STARTING: {
8449 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8450 IBinder token = null;
8451 View view = null;
8452 synchronized (mWindowMap) {
8453 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8454 + wtoken + ": startingWindow="
8455 + wtoken.startingWindow + " startingView="
8456 + wtoken.startingView);
8457 if (wtoken.startingWindow != null) {
8458 view = wtoken.startingView;
8459 token = wtoken.token;
8460 wtoken.startingData = null;
8461 wtoken.startingView = null;
8462 wtoken.startingWindow = null;
8463 }
8464 }
8465 if (view != null) {
8466 try {
8467 mPolicy.removeStartingWindow(token, view);
8468 } catch (Exception e) {
8469 Log.w(TAG, "Exception when removing starting window", e);
8470 }
8471 }
8472 } break;
8473
8474 case FINISHED_STARTING: {
8475 IBinder token = null;
8476 View view = null;
8477 while (true) {
8478 synchronized (mWindowMap) {
8479 final int N = mFinishedStarting.size();
8480 if (N <= 0) {
8481 break;
8482 }
8483 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8484
8485 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8486 "Finished starting " + wtoken
8487 + ": startingWindow=" + wtoken.startingWindow
8488 + " startingView=" + wtoken.startingView);
8489
8490 if (wtoken.startingWindow == null) {
8491 continue;
8492 }
8493
8494 view = wtoken.startingView;
8495 token = wtoken.token;
8496 wtoken.startingData = null;
8497 wtoken.startingView = null;
8498 wtoken.startingWindow = null;
8499 }
8500
8501 try {
8502 mPolicy.removeStartingWindow(token, view);
8503 } catch (Exception e) {
8504 Log.w(TAG, "Exception when removing starting window", e);
8505 }
8506 }
8507 } break;
8508
8509 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8510 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8511
8512 boolean nowVisible = msg.arg1 != 0;
8513 boolean nowGone = msg.arg2 != 0;
8514
8515 try {
8516 if (DEBUG_VISIBILITY) Log.v(
8517 TAG, "Reporting visible in " + wtoken
8518 + " visible=" + nowVisible
8519 + " gone=" + nowGone);
8520 if (nowVisible) {
8521 wtoken.appToken.windowsVisible();
8522 } else {
8523 wtoken.appToken.windowsGone();
8524 }
8525 } catch (RemoteException ex) {
8526 }
8527 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 case WINDOW_FREEZE_TIMEOUT: {
8530 synchronized (mWindowMap) {
8531 Log.w(TAG, "Window freeze timeout expired.");
8532 int i = mWindows.size();
8533 while (i > 0) {
8534 i--;
8535 WindowState w = (WindowState)mWindows.get(i);
8536 if (w.mOrientationChanging) {
8537 w.mOrientationChanging = false;
8538 Log.w(TAG, "Force clearing orientation change: " + w);
8539 }
8540 }
8541 performLayoutAndPlaceSurfacesLocked();
8542 }
8543 break;
8544 }
Romain Guy06882f82009-06-10 13:36:04 -07008545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 case HOLD_SCREEN_CHANGED: {
8547 Session oldHold;
8548 Session newHold;
8549 synchronized (mWindowMap) {
8550 oldHold = mLastReportedHold;
8551 newHold = (Session)msg.obj;
8552 mLastReportedHold = newHold;
8553 }
Romain Guy06882f82009-06-10 13:36:04 -07008554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 if (oldHold != newHold) {
8556 try {
8557 if (oldHold != null) {
8558 mBatteryStats.noteStopWakelock(oldHold.mUid,
8559 "window",
8560 BatteryStats.WAKE_TYPE_WINDOW);
8561 }
8562 if (newHold != null) {
8563 mBatteryStats.noteStartWakelock(newHold.mUid,
8564 "window",
8565 BatteryStats.WAKE_TYPE_WINDOW);
8566 }
8567 } catch (RemoteException e) {
8568 }
8569 }
8570 break;
8571 }
Romain Guy06882f82009-06-10 13:36:04 -07008572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 case APP_TRANSITION_TIMEOUT: {
8574 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008575 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008576 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8577 "*** APP TRANSITION TIMEOUT");
8578 mAppTransitionReady = true;
8579 mAppTransitionTimeout = true;
8580 performLayoutAndPlaceSurfacesLocked();
8581 }
8582 }
8583 break;
8584 }
Romain Guy06882f82009-06-10 13:36:04 -07008585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 case PERSIST_ANIMATION_SCALE: {
8587 Settings.System.putFloat(mContext.getContentResolver(),
8588 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8589 Settings.System.putFloat(mContext.getContentResolver(),
8590 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8591 break;
8592 }
Romain Guy06882f82009-06-10 13:36:04 -07008593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 case FORCE_GC: {
8595 synchronized(mWindowMap) {
8596 if (mAnimationPending) {
8597 // If we are animating, don't do the gc now but
8598 // delay a bit so we don't interrupt the animation.
8599 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8600 2000);
8601 return;
8602 }
8603 // If we are currently rotating the display, it will
8604 // schedule a new message when done.
8605 if (mDisplayFrozen) {
8606 return;
8607 }
8608 mFreezeGcPending = 0;
8609 }
8610 Runtime.getRuntime().gc();
8611 break;
8612 }
Romain Guy06882f82009-06-10 13:36:04 -07008613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 case ENABLE_SCREEN: {
8615 performEnableScreen();
8616 break;
8617 }
Romain Guy06882f82009-06-10 13:36:04 -07008618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008619 case APP_FREEZE_TIMEOUT: {
8620 synchronized (mWindowMap) {
8621 Log.w(TAG, "App freeze timeout expired.");
8622 int i = mAppTokens.size();
8623 while (i > 0) {
8624 i--;
8625 AppWindowToken tok = mAppTokens.get(i);
8626 if (tok.freezingScreen) {
8627 Log.w(TAG, "Force clearing freeze: " + tok);
8628 unsetAppFreezingScreenLocked(tok, true, true);
8629 }
8630 }
8631 }
8632 break;
8633 }
Romain Guy06882f82009-06-10 13:36:04 -07008634
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008635 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008636 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008637 sendNewConfiguration();
8638 }
8639 break;
8640 }
Romain Guy06882f82009-06-10 13:36:04 -07008641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642 }
8643 }
8644 }
8645
8646 // -------------------------------------------------------------
8647 // IWindowManager API
8648 // -------------------------------------------------------------
8649
8650 public IWindowSession openSession(IInputMethodClient client,
8651 IInputContext inputContext) {
8652 if (client == null) throw new IllegalArgumentException("null client");
8653 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8654 return new Session(client, inputContext);
8655 }
8656
8657 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8658 synchronized (mWindowMap) {
8659 // The focus for the client is the window immediately below
8660 // where we would place the input method window.
8661 int idx = findDesiredInputMethodWindowIndexLocked(false);
8662 WindowState imFocus;
8663 if (idx > 0) {
8664 imFocus = (WindowState)mWindows.get(idx-1);
8665 if (imFocus != null) {
8666 if (imFocus.mSession.mClient != null &&
8667 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8668 return true;
8669 }
8670 }
8671 }
8672 }
8673 return false;
8674 }
Romain Guy06882f82009-06-10 13:36:04 -07008675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 // -------------------------------------------------------------
8677 // Internals
8678 // -------------------------------------------------------------
8679
8680 final WindowState windowForClientLocked(Session session, IWindow client) {
8681 return windowForClientLocked(session, client.asBinder());
8682 }
Romain Guy06882f82009-06-10 13:36:04 -07008683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 final WindowState windowForClientLocked(Session session, IBinder client) {
8685 WindowState win = mWindowMap.get(client);
8686 if (localLOGV) Log.v(
8687 TAG, "Looking up client " + client + ": " + win);
8688 if (win == null) {
8689 RuntimeException ex = new RuntimeException();
8690 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8691 return null;
8692 }
8693 if (session != null && win.mSession != session) {
8694 RuntimeException ex = new RuntimeException();
8695 Log.w(TAG, "Requested window " + client + " is in session " +
8696 win.mSession + ", not " + session, ex);
8697 return null;
8698 }
8699
8700 return win;
8701 }
8702
Dianne Hackborna8f60182009-09-01 19:01:50 -07008703 final void rebuildAppWindowListLocked() {
8704 int NW = mWindows.size();
8705 int i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008706 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008707
8708 // First remove all existing app windows.
8709 i=0;
8710 while (i < NW) {
8711 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008712 WindowState win = (WindowState)mWindows.remove(i);
8713 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8714 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008715 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008716 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008717 continue;
8718 }
8719 i++;
8720 }
8721
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008722 // First add all of the exiting app tokens... these are no longer
8723 // in the main app list, but still have windows shown. We put them
8724 // in the back because now that the animation is over we no longer
8725 // will care about them.
8726 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008727 i = 0;
8728 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008729 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8730 }
8731
8732 // And add in the still active app tokens in Z order.
8733 NT = mAppTokens.size();
8734 for (int j=0; j<NT; j++) {
8735 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008736 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008737
8738 if (i != numRemoved) {
8739 Log.w(TAG, "Rebuild removed " + numRemoved
8740 + " windows but added " + i);
8741 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07008742 }
8743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 private final void assignLayersLocked() {
8745 int N = mWindows.size();
8746 int curBaseLayer = 0;
8747 int curLayer = 0;
8748 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 for (i=0; i<N; i++) {
8751 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008752 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8753 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 curLayer += WINDOW_LAYER_MULTIPLIER;
8755 w.mLayer = curLayer;
8756 } else {
8757 curBaseLayer = curLayer = w.mBaseLayer;
8758 w.mLayer = curLayer;
8759 }
8760 if (w.mTargetAppToken != null) {
8761 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8762 } else if (w.mAppToken != null) {
8763 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8764 } else {
8765 w.mAnimLayer = w.mLayer;
8766 }
8767 if (w.mIsImWindow) {
8768 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008769 } else if (w.mIsWallpaper) {
8770 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 }
8772 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8773 + w.mAnimLayer);
8774 //System.out.println(
8775 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8776 }
8777 }
8778
8779 private boolean mInLayout = false;
8780 private final void performLayoutAndPlaceSurfacesLocked() {
8781 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008782 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 throw new RuntimeException("Recursive call!");
8784 }
8785 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8786 return;
8787 }
8788
8789 boolean recoveringMemory = false;
8790 if (mForceRemoves != null) {
8791 recoveringMemory = true;
8792 // Wait a little it for things to settle down, and off we go.
8793 for (int i=0; i<mForceRemoves.size(); i++) {
8794 WindowState ws = mForceRemoves.get(i);
8795 Log.i(TAG, "Force removing: " + ws);
8796 removeWindowInnerLocked(ws.mSession, ws);
8797 }
8798 mForceRemoves = null;
8799 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8800 Object tmp = new Object();
8801 synchronized (tmp) {
8802 try {
8803 tmp.wait(250);
8804 } catch (InterruptedException e) {
8805 }
8806 }
8807 }
Romain Guy06882f82009-06-10 13:36:04 -07008808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 mInLayout = true;
8810 try {
8811 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 int i = mPendingRemove.size()-1;
8814 if (i >= 0) {
8815 while (i >= 0) {
8816 WindowState w = mPendingRemove.get(i);
8817 removeWindowInnerLocked(w.mSession, w);
8818 i--;
8819 }
8820 mPendingRemove.clear();
8821
8822 mInLayout = false;
8823 assignLayersLocked();
8824 mLayoutNeeded = true;
8825 performLayoutAndPlaceSurfacesLocked();
8826
8827 } else {
8828 mInLayout = false;
8829 if (mLayoutNeeded) {
8830 requestAnimationLocked(0);
8831 }
8832 }
8833 } catch (RuntimeException e) {
8834 mInLayout = false;
8835 Log.e(TAG, "Unhandled exception while layout out windows", e);
8836 }
8837 }
8838
8839 private final void performLayoutLockedInner() {
8840 final int dw = mDisplay.getWidth();
8841 final int dh = mDisplay.getHeight();
8842
8843 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008844 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008845 int i;
8846
8847 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008848
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008849 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 mPolicy.beginLayoutLw(dw, dh);
8851
8852 // First perform layout of any root windows (not attached
8853 // to another window).
8854 int topAttached = -1;
8855 for (i = N-1; i >= 0; i--) {
8856 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008857
8858 // Don't do layout of a window if it is not visible, or
8859 // soon won't be visible, to avoid wasting time and funky
8860 // changes while a window is animating away.
8861 final AppWindowToken atoken = win.mAppToken;
8862 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008863 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008864 || win.mRootToken.hidden
8865 || (atoken != null && atoken.hiddenRequested)
8866 || !win.mPolicyVisibility
8867 || win.mAttachedHidden
8868 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008869
8870 // If this view is GONE, then skip it -- keep the current
8871 // frame, and let the caller know so they can ignore it
8872 // if they want. (We do the normal layout for INVISIBLE
8873 // windows, since that means "perform layout as normal,
8874 // just don't display").
8875 if (!gone || !win.mHaveFrame) {
8876 if (!win.mLayoutAttached) {
8877 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8878 } else {
8879 if (topAttached < 0) topAttached = i;
8880 }
8881 }
8882 }
Romain Guy06882f82009-06-10 13:36:04 -07008883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 // Now perform layout of attached windows, which usually
8885 // depend on the position of the window they are attached to.
8886 // XXX does not deal with windows that are attached to windows
8887 // that are themselves attached.
8888 for (i = topAttached; i >= 0; i--) {
8889 WindowState win = (WindowState) mWindows.get(i);
8890
8891 // If this view is GONE, then skip it -- keep the current
8892 // frame, and let the caller know so they can ignore it
8893 // if they want. (We do the normal layout for INVISIBLE
8894 // windows, since that means "perform layout as normal,
8895 // just don't display").
8896 if (win.mLayoutAttached) {
8897 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8898 || !win.mHaveFrame) {
8899 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8900 }
8901 }
8902 }
8903
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008904 int changes = mPolicy.finishLayoutLw();
8905 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
8906 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
8907 assignLayersLocked();
8908 }
8909 }
8910 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008911 mLayoutNeeded = false;
8912 } else if (repeats > 2) {
8913 Log.w(TAG, "Layout repeat aborted after too many iterations");
8914 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008915 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
8916 Configuration newConfig = updateOrientationFromAppTokensLocked(
8917 null, null);
8918 if (newConfig != null) {
8919 mLayoutNeeded = true;
8920 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
8921 }
8922 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008923 } else {
8924 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008925 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
8926 Configuration newConfig = updateOrientationFromAppTokensLocked(
8927 null, null);
8928 if (newConfig != null) {
8929 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
8930 }
8931 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 }
8934 }
Romain Guy06882f82009-06-10 13:36:04 -07008935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 private final void performLayoutAndPlaceSurfacesLockedInner(
8937 boolean recoveringMemory) {
8938 final long currentTime = SystemClock.uptimeMillis();
8939 final int dw = mDisplay.getWidth();
8940 final int dh = mDisplay.getHeight();
8941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942 int i;
8943
8944 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008945 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008947 if (mFxSession == null) {
8948 mFxSession = new SurfaceSession();
8949 }
Romain Guy06882f82009-06-10 13:36:04 -07008950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008951 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8952
8953 // Initialize state of exiting tokens.
8954 for (i=mExitingTokens.size()-1; i>=0; i--) {
8955 mExitingTokens.get(i).hasVisible = false;
8956 }
8957
8958 // Initialize state of exiting applications.
8959 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8960 mExitingAppTokens.get(i).hasVisible = false;
8961 }
8962
8963 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 boolean orientationChangeComplete = true;
8965 Session holdScreen = null;
8966 float screenBrightness = -1;
8967 boolean focusDisplayed = false;
8968 boolean animating = false;
8969
8970 Surface.openTransaction();
8971 try {
8972 boolean restart;
8973
8974 do {
8975 final int transactionSequence = ++mTransactionSequence;
8976
8977 // Update animations of all applications, including those
8978 // associated with exiting/removed apps
8979 boolean tokensAnimating = false;
8980 final int NAT = mAppTokens.size();
8981 for (i=0; i<NAT; i++) {
8982 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8983 tokensAnimating = true;
8984 }
8985 }
8986 final int NEAT = mExitingAppTokens.size();
8987 for (i=0; i<NEAT; i++) {
8988 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8989 tokensAnimating = true;
8990 }
8991 }
8992
8993 animating = tokensAnimating;
8994 restart = false;
8995
8996 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008997 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998
8999 mPolicy.beginAnimationLw(dw, dh);
9000
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009001 final int N = mWindows.size();
9002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009003 for (i=N-1; i>=0; i--) {
9004 WindowState w = (WindowState)mWindows.get(i);
9005
9006 final WindowManager.LayoutParams attrs = w.mAttrs;
9007
9008 if (w.mSurface != null) {
9009 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009010 if (w.commitFinishDrawingLocked(currentTime)) {
9011 if ((w.mAttrs.flags
9012 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009013 if (DEBUG_WALLPAPER) Log.v(TAG,
9014 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009015 wallpaperMayChange = true;
9016 }
9017 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009018 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9020 animating = true;
9021 //w.dump(" ");
9022 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009023 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9024 wallpaperMayChange = true;
9025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 mPolicy.animatingWindowLw(w, attrs);
9027 }
9028
9029 final AppWindowToken atoken = w.mAppToken;
9030 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9031 if (atoken.lastTransactionSequence != transactionSequence) {
9032 atoken.lastTransactionSequence = transactionSequence;
9033 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9034 atoken.startingDisplayed = false;
9035 }
9036 if ((w.isOnScreen() || w.mAttrs.type
9037 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9038 && !w.mExiting && !w.mDestroying) {
9039 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
9040 Log.v(TAG, "Eval win " + w + ": isDisplayed="
9041 + w.isDisplayedLw()
9042 + ", isAnimating=" + w.isAnimating());
9043 if (!w.isDisplayedLw()) {
9044 Log.v(TAG, "Not displayed: s=" + w.mSurface
9045 + " pv=" + w.mPolicyVisibility
9046 + " dp=" + w.mDrawPending
9047 + " cdp=" + w.mCommitDrawPending
9048 + " ah=" + w.mAttachedHidden
9049 + " th=" + atoken.hiddenRequested
9050 + " a=" + w.mAnimating);
9051 }
9052 }
9053 if (w != atoken.startingWindow) {
9054 if (!atoken.freezingScreen || !w.mAppFreezing) {
9055 atoken.numInterestingWindows++;
9056 if (w.isDisplayedLw()) {
9057 atoken.numDrawnWindows++;
9058 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9059 "tokenMayBeDrawn: " + atoken
9060 + " freezingScreen=" + atoken.freezingScreen
9061 + " mAppFreezing=" + w.mAppFreezing);
9062 tokenMayBeDrawn = true;
9063 }
9064 }
9065 } else if (w.isDisplayedLw()) {
9066 atoken.startingDisplayed = true;
9067 }
9068 }
9069 } else if (w.mReadyToShow) {
9070 w.performShowLocked();
9071 }
9072 }
9073
9074 if (mPolicy.finishAnimationLw()) {
9075 restart = true;
9076 }
9077
9078 if (tokenMayBeDrawn) {
9079 // See if any windows have been drawn, so they (and others
9080 // associated with them) can now be shown.
9081 final int NT = mTokenList.size();
9082 for (i=0; i<NT; i++) {
9083 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9084 if (wtoken == null) {
9085 continue;
9086 }
9087 if (wtoken.freezingScreen) {
9088 int numInteresting = wtoken.numInterestingWindows;
9089 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9090 if (DEBUG_VISIBILITY) Log.v(TAG,
9091 "allDrawn: " + wtoken
9092 + " interesting=" + numInteresting
9093 + " drawn=" + wtoken.numDrawnWindows);
9094 wtoken.showAllWindowsLocked();
9095 unsetAppFreezingScreenLocked(wtoken, false, true);
9096 orientationChangeComplete = true;
9097 }
9098 } else if (!wtoken.allDrawn) {
9099 int numInteresting = wtoken.numInterestingWindows;
9100 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9101 if (DEBUG_VISIBILITY) Log.v(TAG,
9102 "allDrawn: " + wtoken
9103 + " interesting=" + numInteresting
9104 + " drawn=" + wtoken.numDrawnWindows);
9105 wtoken.allDrawn = true;
9106 restart = true;
9107
9108 // We can now show all of the drawn windows!
9109 if (!mOpeningApps.contains(wtoken)) {
9110 wtoken.showAllWindowsLocked();
9111 }
9112 }
9113 }
9114 }
9115 }
9116
9117 // If we are ready to perform an app transition, check through
9118 // all of the app tokens to be shown and see if they are ready
9119 // to go.
9120 if (mAppTransitionReady) {
9121 int NN = mOpeningApps.size();
9122 boolean goodToGo = true;
9123 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9124 "Checking " + NN + " opening apps (frozen="
9125 + mDisplayFrozen + " timeout="
9126 + mAppTransitionTimeout + ")...");
9127 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9128 // If the display isn't frozen, wait to do anything until
9129 // all of the apps are ready. Otherwise just go because
9130 // we'll unfreeze the display when everyone is ready.
9131 for (i=0; i<NN && goodToGo; i++) {
9132 AppWindowToken wtoken = mOpeningApps.get(i);
9133 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9134 "Check opening app" + wtoken + ": allDrawn="
9135 + wtoken.allDrawn + " startingDisplayed="
9136 + wtoken.startingDisplayed);
9137 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9138 && !wtoken.startingMoved) {
9139 goodToGo = false;
9140 }
9141 }
9142 }
9143 if (goodToGo) {
9144 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9145 int transit = mNextAppTransition;
9146 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009147 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009148 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009149 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009150 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009151 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009152 mAppTransitionTimeout = false;
9153 mStartingIconInTransition = false;
9154 mSkipAppTransitionAnimation = false;
9155
9156 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9157
Dianne Hackborna8f60182009-09-01 19:01:50 -07009158 // If there are applications waiting to come to the
9159 // top of the stack, now is the time to move their windows.
9160 // (Note that we don't do apps going to the bottom
9161 // here -- we want to keep their windows in the old
9162 // Z-order until the animation completes.)
9163 if (mToTopApps.size() > 0) {
9164 NN = mAppTokens.size();
9165 for (i=0; i<NN; i++) {
9166 AppWindowToken wtoken = mAppTokens.get(i);
9167 if (wtoken.sendingToTop) {
9168 wtoken.sendingToTop = false;
9169 moveAppWindowsLocked(wtoken, NN, false);
9170 }
9171 }
9172 mToTopApps.clear();
9173 }
9174
Dianne Hackborn25994b42009-09-04 14:21:19 -07009175 WindowState oldWallpaper = mWallpaperTarget;
9176
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009177 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009178 wallpaperMayChange = false;
9179
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009180 // The top-most window will supply the layout params,
9181 // and we will determine it below.
9182 LayoutParams animLp = null;
9183 int bestAnimLayer = -1;
9184
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009185 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009186 "New wallpaper target=" + mWallpaperTarget
9187 + ", lower target=" + mLowerWallpaperTarget
9188 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009189 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009190 // Do a first pass through the tokens for two
9191 // things:
9192 // (1) Determine if both the closing and opening
9193 // app token sets are wallpaper targets, in which
9194 // case special animations are needed
9195 // (since the wallpaper needs to stay static
9196 // behind them).
9197 // (2) Find the layout params of the top-most
9198 // application window in the tokens, which is
9199 // what will control the animation theme.
9200 final int NC = mClosingApps.size();
9201 NN = NC + mOpeningApps.size();
9202 for (i=0; i<NN; i++) {
9203 AppWindowToken wtoken;
9204 int mode;
9205 if (i < NC) {
9206 wtoken = mClosingApps.get(i);
9207 mode = 1;
9208 } else {
9209 wtoken = mOpeningApps.get(i-NC);
9210 mode = 2;
9211 }
9212 if (mLowerWallpaperTarget != null) {
9213 if (mLowerWallpaperTarget.mAppToken == wtoken
9214 || mUpperWallpaperTarget.mAppToken == wtoken) {
9215 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009216 }
9217 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009218 if (wtoken.appFullscreen) {
9219 WindowState ws = wtoken.findMainWindow();
9220 if (ws != null) {
9221 // If this is a compatibility mode
9222 // window, we will always use its anim.
9223 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9224 animLp = ws.mAttrs;
9225 bestAnimLayer = Integer.MAX_VALUE;
9226 } else if (ws.mLayer > bestAnimLayer) {
9227 animLp = ws.mAttrs;
9228 bestAnimLayer = ws.mLayer;
9229 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009230 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009231 }
9232 }
9233
Dianne Hackborn25994b42009-09-04 14:21:19 -07009234 if (foundWallpapers == 3) {
9235 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9236 "Wallpaper animation!");
9237 switch (transit) {
9238 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9239 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9240 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9241 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9242 break;
9243 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9244 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9245 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9246 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9247 break;
9248 }
9249 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9250 "New transit: " + transit);
9251 } else if (oldWallpaper != null) {
9252 // We are transitioning from an activity with
9253 // a wallpaper to one without.
9254 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9255 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9256 "New transit away from wallpaper: " + transit);
9257 } else if (mWallpaperTarget != null) {
9258 // We are transitioning from an activity without
9259 // a wallpaper to now showing the wallpaper
9260 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9261 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9262 "New transit into wallpaper: " + transit);
9263 }
9264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 NN = mOpeningApps.size();
9266 for (i=0; i<NN; i++) {
9267 AppWindowToken wtoken = mOpeningApps.get(i);
9268 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9269 "Now opening app" + wtoken);
9270 wtoken.reportedVisible = false;
9271 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009272 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009273 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009274 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009275 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009276 wtoken.showAllWindowsLocked();
9277 }
9278 NN = mClosingApps.size();
9279 for (i=0; i<NN; i++) {
9280 AppWindowToken wtoken = mClosingApps.get(i);
9281 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9282 "Now closing app" + wtoken);
9283 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009284 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009285 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009287 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 // Force the allDrawn flag, because we want to start
9289 // this guy's animations regardless of whether it's
9290 // gotten drawn.
9291 wtoken.allDrawn = true;
9292 }
9293
9294 mOpeningApps.clear();
9295 mClosingApps.clear();
9296
9297 // This has changed the visibility of windows, so perform
9298 // a new layout to get them all up-to-date.
9299 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009300 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9301 assignLayersLocked();
9302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009303 performLayoutLockedInner();
9304 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9305
9306 restart = true;
9307 }
9308 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009309
Dianne Hackborna8f60182009-09-01 19:01:50 -07009310 if (!animating && mAppTransitionRunning) {
9311 // We have finished the animation of an app transition. To do
9312 // this, we have delayed a lot of operations like showing and
9313 // hiding apps, moving apps in Z-order, etc. The app token list
9314 // reflects the correct Z-order, but the window list may now
9315 // be out of sync with it. So here we will just rebuild the
9316 // entire app window list. Fun!
9317 mAppTransitionRunning = false;
9318 // Clear information about apps that were moving.
9319 mToBottomApps.clear();
9320
9321 rebuildAppWindowListLocked();
9322 restart = true;
9323 moveInputMethodWindowsIfNeededLocked(false);
9324 wallpaperMayChange = true;
9325 mLayoutNeeded = true;
9326 }
9327
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009328 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009329 if (DEBUG_WALLPAPER) Log.v(TAG,
9330 "Wallpaper may change! Adjusting");
9331 int adjResult = adjustWallpaperWindowsLocked();
9332 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9333 if (DEBUG_WALLPAPER) Log.v(TAG,
9334 "Wallpaper layer changed: assigning layers + relayout");
9335 restart = true;
9336 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009337 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009338 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9339 if (DEBUG_WALLPAPER) Log.v(TAG,
9340 "Wallpaper visibility changed: relayout");
9341 restart = true;
9342 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009343 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009344 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009345 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009346 performLayoutLockedInner();
9347 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009348 }
9349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009350 } while (restart);
9351
9352 // THIRD LOOP: Update the surfaces of all windows.
9353
9354 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9355
9356 boolean obscured = false;
9357 boolean blurring = false;
9358 boolean dimming = false;
9359 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009360 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009361 boolean backgroundFillerShown = false;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009362 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009364 final int N = mWindows.size();
9365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 for (i=N-1; i>=0; i--) {
9367 WindowState w = (WindowState)mWindows.get(i);
9368
9369 boolean displayed = false;
9370 final WindowManager.LayoutParams attrs = w.mAttrs;
9371 final int attrFlags = attrs.flags;
9372
9373 if (w.mSurface != null) {
9374 w.computeShownFrameLocked();
9375 if (localLOGV) Log.v(
9376 TAG, "Placing surface #" + i + " " + w.mSurface
9377 + ": new=" + w.mShownFrame + ", old="
9378 + w.mLastShownFrame);
9379
9380 boolean resize;
9381 int width, height;
9382 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9383 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9384 w.mLastRequestedHeight != w.mRequestedHeight;
9385 // for a scaled surface, we just want to use
9386 // the requested size.
9387 width = w.mRequestedWidth;
9388 height = w.mRequestedHeight;
9389 w.mLastRequestedWidth = width;
9390 w.mLastRequestedHeight = height;
9391 w.mLastShownFrame.set(w.mShownFrame);
9392 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009393 if (SHOW_TRANSACTIONS) Log.i(
9394 TAG, " SURFACE " + w.mSurface
9395 + ": POS " + w.mShownFrame.left
9396 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9398 } catch (RuntimeException e) {
9399 Log.w(TAG, "Error positioning surface in " + w, e);
9400 if (!recoveringMemory) {
9401 reclaimSomeSurfaceMemoryLocked(w, "position");
9402 }
9403 }
9404 } else {
9405 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9406 width = w.mShownFrame.width();
9407 height = w.mShownFrame.height();
9408 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009409 }
9410
9411 if (resize) {
9412 if (width < 1) width = 1;
9413 if (height < 1) height = 1;
9414 if (w.mSurface != null) {
9415 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009416 if (SHOW_TRANSACTIONS) Log.i(
9417 TAG, " SURFACE " + w.mSurface + ": POS "
9418 + w.mShownFrame.left + ","
9419 + w.mShownFrame.top + " SIZE "
9420 + w.mShownFrame.width() + "x"
9421 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 w.mSurface.setSize(width, height);
9423 w.mSurface.setPosition(w.mShownFrame.left,
9424 w.mShownFrame.top);
9425 } catch (RuntimeException e) {
9426 // If something goes wrong with the surface (such
9427 // as running out of memory), don't take down the
9428 // entire system.
9429 Log.e(TAG, "Failure updating surface of " + w
9430 + "size=(" + width + "x" + height
9431 + "), pos=(" + w.mShownFrame.left
9432 + "," + w.mShownFrame.top + ")", e);
9433 if (!recoveringMemory) {
9434 reclaimSomeSurfaceMemoryLocked(w, "size");
9435 }
9436 }
9437 }
9438 }
9439 if (!w.mAppFreezing) {
9440 w.mContentInsetsChanged =
9441 !w.mLastContentInsets.equals(w.mContentInsets);
9442 w.mVisibleInsetsChanged =
9443 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009444 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 || w.mContentInsetsChanged
9446 || w.mVisibleInsetsChanged) {
9447 w.mLastFrame.set(w.mFrame);
9448 w.mLastContentInsets.set(w.mContentInsets);
9449 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009450 // If the screen is currently frozen, then keep
9451 // it frozen until this window draws at its new
9452 // orientation.
9453 if (mDisplayFrozen) {
9454 if (DEBUG_ORIENTATION) Log.v(TAG,
9455 "Resizing while display frozen: " + w);
9456 w.mOrientationChanging = true;
9457 if (mWindowsFreezingScreen) {
9458 mWindowsFreezingScreen = true;
9459 // XXX should probably keep timeout from
9460 // when we first froze the display.
9461 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9462 mH.sendMessageDelayed(mH.obtainMessage(
9463 H.WINDOW_FREEZE_TIMEOUT), 2000);
9464 }
9465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466 // If the orientation is changing, then we need to
9467 // hold off on unfreezing the display until this
9468 // window has been redrawn; to do that, we need
9469 // to go through the process of getting informed
9470 // by the application when it has finished drawing.
9471 if (w.mOrientationChanging) {
9472 if (DEBUG_ORIENTATION) Log.v(TAG,
9473 "Orientation start waiting for draw in "
9474 + w + ", surface " + w.mSurface);
9475 w.mDrawPending = true;
9476 w.mCommitDrawPending = false;
9477 w.mReadyToShow = false;
9478 if (w.mAppToken != null) {
9479 w.mAppToken.allDrawn = false;
9480 }
9481 }
Romain Guy06882f82009-06-10 13:36:04 -07009482 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 "Resizing window " + w + " to " + w.mFrame);
9484 mResizingWindows.add(w);
9485 } else if (w.mOrientationChanging) {
9486 if (!w.mDrawPending && !w.mCommitDrawPending) {
9487 if (DEBUG_ORIENTATION) Log.v(TAG,
9488 "Orientation not waiting for draw in "
9489 + w + ", surface " + w.mSurface);
9490 w.mOrientationChanging = false;
9491 }
9492 }
9493 }
9494
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009495 if ((forceHiding
9496 && attrs.type != WindowManager.LayoutParams.TYPE_STATUS_BAR
9497 && attrs.type != WindowManager.LayoutParams.TYPE_WALLPAPER)
9498 || w.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 if (!w.mLastHidden) {
9500 //dump();
9501 w.mLastHidden = true;
9502 if (SHOW_TRANSACTIONS) Log.i(
9503 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9504 if (w.mSurface != null) {
9505 try {
9506 w.mSurface.hide();
9507 } catch (RuntimeException e) {
9508 Log.w(TAG, "Exception hiding surface in " + w);
9509 }
9510 }
9511 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9512 }
9513 // If we are waiting for this window to handle an
9514 // orientation change, well, it is hidden, so
9515 // doesn't really matter. Note that this does
9516 // introduce a potential glitch if the window
9517 // becomes unhidden before it has drawn for the
9518 // new orientation.
9519 if (w.mOrientationChanging) {
9520 w.mOrientationChanging = false;
9521 if (DEBUG_ORIENTATION) Log.v(TAG,
9522 "Orientation change skips hidden " + w);
9523 }
9524 } else if (!w.isReadyForDisplay()) {
9525 if (!w.mLastHidden) {
9526 //dump();
9527 w.mLastHidden = true;
9528 if (SHOW_TRANSACTIONS) Log.i(
9529 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9530 if (w.mSurface != null) {
9531 try {
9532 w.mSurface.hide();
9533 } catch (RuntimeException e) {
9534 Log.w(TAG, "Exception exception hiding surface in " + w);
9535 }
9536 }
9537 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9538 }
9539 // If we are waiting for this window to handle an
9540 // orientation change, well, it is hidden, so
9541 // doesn't really matter. Note that this does
9542 // introduce a potential glitch if the window
9543 // becomes unhidden before it has drawn for the
9544 // new orientation.
9545 if (w.mOrientationChanging) {
9546 w.mOrientationChanging = false;
9547 if (DEBUG_ORIENTATION) Log.v(TAG,
9548 "Orientation change skips hidden " + w);
9549 }
9550 } else if (w.mLastLayer != w.mAnimLayer
9551 || w.mLastAlpha != w.mShownAlpha
9552 || w.mLastDsDx != w.mDsDx
9553 || w.mLastDtDx != w.mDtDx
9554 || w.mLastDsDy != w.mDsDy
9555 || w.mLastDtDy != w.mDtDy
9556 || w.mLastHScale != w.mHScale
9557 || w.mLastVScale != w.mVScale
9558 || w.mLastHidden) {
9559 displayed = true;
9560 w.mLastAlpha = w.mShownAlpha;
9561 w.mLastLayer = w.mAnimLayer;
9562 w.mLastDsDx = w.mDsDx;
9563 w.mLastDtDx = w.mDtDx;
9564 w.mLastDsDy = w.mDsDy;
9565 w.mLastDtDy = w.mDtDy;
9566 w.mLastHScale = w.mHScale;
9567 w.mLastVScale = w.mVScale;
9568 if (SHOW_TRANSACTIONS) Log.i(
9569 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009570 + w.mShownAlpha + " layer=" + w.mAnimLayer
9571 + " matrix=[" + (w.mDsDx*w.mHScale)
9572 + "," + (w.mDtDx*w.mVScale)
9573 + "][" + (w.mDsDy*w.mHScale)
9574 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009575 if (w.mSurface != null) {
9576 try {
9577 w.mSurface.setAlpha(w.mShownAlpha);
9578 w.mSurface.setLayer(w.mAnimLayer);
9579 w.mSurface.setMatrix(
9580 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9581 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9582 } catch (RuntimeException e) {
9583 Log.w(TAG, "Error updating surface in " + w, e);
9584 if (!recoveringMemory) {
9585 reclaimSomeSurfaceMemoryLocked(w, "update");
9586 }
9587 }
9588 }
9589
9590 if (w.mLastHidden && !w.mDrawPending
9591 && !w.mCommitDrawPending
9592 && !w.mReadyToShow) {
9593 if (SHOW_TRANSACTIONS) Log.i(
9594 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9595 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9596 + " during relayout");
9597 if (showSurfaceRobustlyLocked(w)) {
9598 w.mHasDrawn = true;
9599 w.mLastHidden = false;
9600 } else {
9601 w.mOrientationChanging = false;
9602 }
9603 }
9604 if (w.mSurface != null) {
9605 w.mToken.hasVisible = true;
9606 }
9607 } else {
9608 displayed = true;
9609 }
9610
9611 if (displayed) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009612 if (attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD) {
9613 forceHiding = true;
9614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 if (!covered) {
9616 if (attrs.width == LayoutParams.FILL_PARENT
9617 && attrs.height == LayoutParams.FILL_PARENT) {
9618 covered = true;
9619 }
9620 }
9621 if (w.mOrientationChanging) {
9622 if (w.mDrawPending || w.mCommitDrawPending) {
9623 orientationChangeComplete = false;
9624 if (DEBUG_ORIENTATION) Log.v(TAG,
9625 "Orientation continue waiting for draw in " + w);
9626 } else {
9627 w.mOrientationChanging = false;
9628 if (DEBUG_ORIENTATION) Log.v(TAG,
9629 "Orientation change complete in " + w);
9630 }
9631 }
9632 w.mToken.hasVisible = true;
9633 }
9634 } else if (w.mOrientationChanging) {
9635 if (DEBUG_ORIENTATION) Log.v(TAG,
9636 "Orientation change skips hidden " + w);
9637 w.mOrientationChanging = false;
9638 }
9639
9640 final boolean canBeSeen = w.isDisplayedLw();
9641
9642 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9643 focusDisplayed = true;
9644 }
9645
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009646 final boolean obscuredChanged = w.mObscured != obscured;
9647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009649 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650 if (w.mSurface != null) {
9651 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9652 holdScreen = w.mSession;
9653 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009654 if (!syswin && w.mAttrs.screenBrightness >= 0
9655 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009656 screenBrightness = w.mAttrs.screenBrightness;
9657 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009658 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9659 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9660 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9661 syswin = true;
9662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009663 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009664
Dianne Hackborn25994b42009-09-04 14:21:19 -07009665 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9666 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009667 // This window completely covers everything behind it,
9668 // so we want to leave all of them as unblurred (for
9669 // performance reasons).
9670 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009671 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9672 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009673 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009674 obscured = true;
9675 if (mBackgroundFillerSurface == null) {
9676 try {
9677 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9678 0, dw, dh,
9679 PixelFormat.OPAQUE,
9680 Surface.FX_SURFACE_NORMAL);
9681 } catch (Exception e) {
9682 Log.e(TAG, "Exception creating filler surface", e);
9683 }
9684 }
9685 try {
9686 mBackgroundFillerSurface.setPosition(0, 0);
9687 mBackgroundFillerSurface.setSize(dw, dh);
9688 // Using the same layer as Dim because they will never be shown at the
9689 // same time.
9690 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9691 mBackgroundFillerSurface.show();
9692 } catch (RuntimeException e) {
9693 Log.e(TAG, "Exception showing filler surface");
9694 }
9695 backgroundFillerShown = true;
9696 mBackgroundFillerShown = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009697 } else if (canBeSeen && !obscured && !forceHiding &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9699 if (localLOGV) Log.v(TAG, "Win " + w
9700 + ": blurring=" + blurring
9701 + " obscured=" + obscured
9702 + " displayed=" + displayed);
9703 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9704 if (!dimming) {
9705 //Log.i(TAG, "DIM BEHIND: " + w);
9706 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009707 if (mDimAnimator == null) {
9708 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009710 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009712 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009713 }
9714 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9715 if (!blurring) {
9716 //Log.i(TAG, "BLUR BEHIND: " + w);
9717 blurring = true;
9718 mBlurShown = true;
9719 if (mBlurSurface == null) {
9720 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9721 + mBlurSurface + ": CREATE");
9722 try {
Romain Guy06882f82009-06-10 13:36:04 -07009723 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009724 -1, 16, 16,
9725 PixelFormat.OPAQUE,
9726 Surface.FX_SURFACE_BLUR);
9727 } catch (Exception e) {
9728 Log.e(TAG, "Exception creating Blur surface", e);
9729 }
9730 }
9731 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9732 + mBlurSurface + ": SHOW pos=(0,0) (" +
9733 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9734 if (mBlurSurface != null) {
9735 mBlurSurface.setPosition(0, 0);
9736 mBlurSurface.setSize(dw, dh);
9737 try {
9738 mBlurSurface.show();
9739 } catch (RuntimeException e) {
9740 Log.w(TAG, "Failure showing blur surface", e);
9741 }
9742 }
9743 }
9744 mBlurSurface.setLayer(w.mAnimLayer-2);
9745 }
9746 }
9747 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009748
9749 if (obscuredChanged && mWallpaperTarget == w) {
9750 // This is the wallpaper target and its obscured state
9751 // changed... make sure the current wallaper's visibility
9752 // has been updated accordingly.
9753 updateWallpaperVisibilityLocked();
9754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009756
9757 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9758 mBackgroundFillerShown = false;
9759 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9760 try {
9761 mBackgroundFillerSurface.hide();
9762 } catch (RuntimeException e) {
9763 Log.e(TAG, "Exception hiding filler surface", e);
9764 }
9765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009767 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9768 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 }
Romain Guy06882f82009-06-10 13:36:04 -07009770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009771 if (!blurring && mBlurShown) {
9772 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9773 + ": HIDE");
9774 try {
9775 mBlurSurface.hide();
9776 } catch (IllegalArgumentException e) {
9777 Log.w(TAG, "Illegal argument exception hiding blur surface");
9778 }
9779 mBlurShown = false;
9780 }
9781
9782 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9783 } catch (RuntimeException e) {
9784 Log.e(TAG, "Unhandled exception in Window Manager", e);
9785 }
9786
9787 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009789 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9790 "With display frozen, orientationChangeComplete="
9791 + orientationChangeComplete);
9792 if (orientationChangeComplete) {
9793 if (mWindowsFreezingScreen) {
9794 mWindowsFreezingScreen = false;
9795 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9796 }
9797 if (mAppsFreezingScreen == 0) {
9798 stopFreezingDisplayLocked();
9799 }
9800 }
Romain Guy06882f82009-06-10 13:36:04 -07009801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009802 i = mResizingWindows.size();
9803 if (i > 0) {
9804 do {
9805 i--;
9806 WindowState win = mResizingWindows.get(i);
9807 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009808 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9809 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 win.mClient.resized(win.mFrame.width(),
9811 win.mFrame.height(), win.mLastContentInsets,
9812 win.mLastVisibleInsets, win.mDrawPending);
9813 win.mContentInsetsChanged = false;
9814 win.mVisibleInsetsChanged = false;
9815 } catch (RemoteException e) {
9816 win.mOrientationChanging = false;
9817 }
9818 } while (i > 0);
9819 mResizingWindows.clear();
9820 }
Romain Guy06882f82009-06-10 13:36:04 -07009821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009823 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009824 i = mDestroySurface.size();
9825 if (i > 0) {
9826 do {
9827 i--;
9828 WindowState win = mDestroySurface.get(i);
9829 win.mDestroying = false;
9830 if (mInputMethodWindow == win) {
9831 mInputMethodWindow = null;
9832 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009833 if (win == mWallpaperTarget) {
9834 wallpaperDestroyed = true;
9835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009836 win.destroySurfaceLocked();
9837 } while (i > 0);
9838 mDestroySurface.clear();
9839 }
9840
9841 // Time to remove any exiting tokens?
9842 for (i=mExitingTokens.size()-1; i>=0; i--) {
9843 WindowToken token = mExitingTokens.get(i);
9844 if (!token.hasVisible) {
9845 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009846 if (token.windowType == TYPE_WALLPAPER) {
9847 mWallpaperTokens.remove(token);
9848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009849 }
9850 }
9851
9852 // Time to remove any exiting applications?
9853 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9854 AppWindowToken token = mExitingAppTokens.get(i);
9855 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009856 // Make sure there is no animation running on this token,
9857 // so any windows associated with it will be removed as
9858 // soon as their animations are complete
9859 token.animation = null;
9860 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 mAppTokens.remove(token);
9862 mExitingAppTokens.remove(i);
9863 }
9864 }
9865
Dianne Hackborna8f60182009-09-01 19:01:50 -07009866 boolean needRelayout = false;
9867
9868 if (!animating && mAppTransitionRunning) {
9869 // We have finished the animation of an app transition. To do
9870 // this, we have delayed a lot of operations like showing and
9871 // hiding apps, moving apps in Z-order, etc. The app token list
9872 // reflects the correct Z-order, but the window list may now
9873 // be out of sync with it. So here we will just rebuild the
9874 // entire app window list. Fun!
9875 mAppTransitionRunning = false;
9876 needRelayout = true;
9877 rebuildAppWindowListLocked();
9878 // Clear information about apps that were moving.
9879 mToBottomApps.clear();
9880 }
9881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009882 if (focusDisplayed) {
9883 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9884 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009885 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009886 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009887 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009888 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009889 requestAnimationLocked(0);
9890 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9892 }
9893 mQueue.setHoldScreenLocked(holdScreen != null);
9894 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9895 mPowerManager.setScreenBrightnessOverride(-1);
9896 } else {
9897 mPowerManager.setScreenBrightnessOverride((int)
9898 (screenBrightness * Power.BRIGHTNESS_ON));
9899 }
9900 if (holdScreen != mHoldingScreenOn) {
9901 mHoldingScreenOn = holdScreen;
9902 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9903 mH.sendMessage(m);
9904 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009905
9906 if (mTurnOnScreen) {
9907 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
9908 LocalPowerManager.BUTTON_EVENT, true);
9909 mTurnOnScreen = false;
9910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 }
9912
9913 void requestAnimationLocked(long delay) {
9914 if (!mAnimationPending) {
9915 mAnimationPending = true;
9916 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9917 }
9918 }
Romain Guy06882f82009-06-10 13:36:04 -07009919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920 /**
9921 * Have the surface flinger show a surface, robustly dealing with
9922 * error conditions. In particular, if there is not enough memory
9923 * to show the surface, then we will try to get rid of other surfaces
9924 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009925 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 * @return Returns true if the surface was successfully shown.
9927 */
9928 boolean showSurfaceRobustlyLocked(WindowState win) {
9929 try {
9930 if (win.mSurface != null) {
9931 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009932 if (win.mTurnOnScreen) {
9933 win.mTurnOnScreen = false;
9934 mTurnOnScreen = true;
9935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 }
9937 return true;
9938 } catch (RuntimeException e) {
9939 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9940 }
Romain Guy06882f82009-06-10 13:36:04 -07009941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 return false;
9945 }
Romain Guy06882f82009-06-10 13:36:04 -07009946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009947 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9948 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009950 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9951 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 if (mForceRemoves == null) {
9954 mForceRemoves = new ArrayList<WindowState>();
9955 }
Romain Guy06882f82009-06-10 13:36:04 -07009956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 long callingIdentity = Binder.clearCallingIdentity();
9958 try {
9959 // There was some problem... first, do a sanity check of the
9960 // window list to make sure we haven't left any dangling surfaces
9961 // around.
9962 int N = mWindows.size();
9963 boolean leakedSurface = false;
9964 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9965 for (int i=0; i<N; i++) {
9966 WindowState ws = (WindowState)mWindows.get(i);
9967 if (ws.mSurface != null) {
9968 if (!mSessions.contains(ws.mSession)) {
9969 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9970 + ws + " surface=" + ws.mSurface
9971 + " token=" + win.mToken
9972 + " pid=" + ws.mSession.mPid
9973 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009974 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 ws.mSurface = null;
9976 mForceRemoves.add(ws);
9977 i--;
9978 N--;
9979 leakedSurface = true;
9980 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9981 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9982 + ws + " surface=" + ws.mSurface
9983 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009984 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 ws.mSurface = null;
9986 leakedSurface = true;
9987 }
9988 }
9989 }
Romain Guy06882f82009-06-10 13:36:04 -07009990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009991 boolean killedApps = false;
9992 if (!leakedSurface) {
9993 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9994 SparseIntArray pidCandidates = new SparseIntArray();
9995 for (int i=0; i<N; i++) {
9996 WindowState ws = (WindowState)mWindows.get(i);
9997 if (ws.mSurface != null) {
9998 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9999 }
10000 }
10001 if (pidCandidates.size() > 0) {
10002 int[] pids = new int[pidCandidates.size()];
10003 for (int i=0; i<pids.length; i++) {
10004 pids[i] = pidCandidates.keyAt(i);
10005 }
10006 try {
10007 if (mActivityManager.killPidsForMemory(pids)) {
10008 killedApps = true;
10009 }
10010 } catch (RemoteException e) {
10011 }
10012 }
10013 }
Romain Guy06882f82009-06-10 13:36:04 -070010014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 if (leakedSurface || killedApps) {
10016 // We managed to reclaim some memory, so get rid of the trouble
10017 // surface and ask the app to request another one.
10018 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10019 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010020 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 win.mSurface = null;
10022 }
Romain Guy06882f82009-06-10 13:36:04 -070010023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 try {
10025 win.mClient.dispatchGetNewSurface();
10026 } catch (RemoteException e) {
10027 }
10028 }
10029 } finally {
10030 Binder.restoreCallingIdentity(callingIdentity);
10031 }
10032 }
Romain Guy06882f82009-06-10 13:36:04 -070010033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034 private boolean updateFocusedWindowLocked(int mode) {
10035 WindowState newFocus = computeFocusedWindowLocked();
10036 if (mCurrentFocus != newFocus) {
10037 // This check makes sure that we don't already have the focus
10038 // change message pending.
10039 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10040 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10041 if (localLOGV) Log.v(
10042 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10043 final WindowState oldFocus = mCurrentFocus;
10044 mCurrentFocus = newFocus;
10045 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010047 final WindowState imWindow = mInputMethodWindow;
10048 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010049 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010051 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10052 mLayoutNeeded = true;
10053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10055 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010056 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10057 // Client will do the layout, but we need to assign layers
10058 // for handleNewWindowLocked() below.
10059 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 }
10061 }
Romain Guy06882f82009-06-10 13:36:04 -070010062
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010063 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10064 mKeyWaiter.handleNewWindowLocked(newFocus);
10065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 return true;
10067 }
10068 return false;
10069 }
10070
10071 private WindowState computeFocusedWindowLocked() {
10072 WindowState result = null;
10073 WindowState win;
10074
10075 int i = mWindows.size() - 1;
10076 int nextAppIndex = mAppTokens.size()-1;
10077 WindowToken nextApp = nextAppIndex >= 0
10078 ? mAppTokens.get(nextAppIndex) : null;
10079
10080 while (i >= 0) {
10081 win = (WindowState)mWindows.get(i);
10082
10083 if (localLOGV || DEBUG_FOCUS) Log.v(
10084 TAG, "Looking for focus: " + i
10085 + " = " + win
10086 + ", flags=" + win.mAttrs.flags
10087 + ", canReceive=" + win.canReceiveKeys());
10088
10089 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010091 // If this window's application has been removed, just skip it.
10092 if (thisApp != null && thisApp.removed) {
10093 i--;
10094 continue;
10095 }
Romain Guy06882f82009-06-10 13:36:04 -070010096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010097 // If there is a focused app, don't allow focus to go to any
10098 // windows below it. If this is an application window, step
10099 // through the app tokens until we find its app.
10100 if (thisApp != null && nextApp != null && thisApp != nextApp
10101 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10102 int origAppIndex = nextAppIndex;
10103 while (nextAppIndex > 0) {
10104 if (nextApp == mFocusedApp) {
10105 // Whoops, we are below the focused app... no focus
10106 // for you!
10107 if (localLOGV || DEBUG_FOCUS) Log.v(
10108 TAG, "Reached focused app: " + mFocusedApp);
10109 return null;
10110 }
10111 nextAppIndex--;
10112 nextApp = mAppTokens.get(nextAppIndex);
10113 if (nextApp == thisApp) {
10114 break;
10115 }
10116 }
10117 if (thisApp != nextApp) {
10118 // Uh oh, the app token doesn't exist! This shouldn't
10119 // happen, but if it does we can get totally hosed...
10120 // so restart at the original app.
10121 nextAppIndex = origAppIndex;
10122 nextApp = mAppTokens.get(nextAppIndex);
10123 }
10124 }
10125
10126 // Dispatch to this window if it is wants key events.
10127 if (win.canReceiveKeys()) {
10128 if (DEBUG_FOCUS) Log.v(
10129 TAG, "Found focus @ " + i + " = " + win);
10130 result = win;
10131 break;
10132 }
10133
10134 i--;
10135 }
10136
10137 return result;
10138 }
10139
10140 private void startFreezingDisplayLocked() {
10141 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010142 // Freezing the display also suspends key event delivery, to
10143 // keep events from going astray while the display is reconfigured.
10144 // If someone has changed orientation again while the screen is
10145 // still frozen, the events will continue to be blocked while the
10146 // successive orientation change is processed. To prevent spurious
10147 // ANRs, we reset the event dispatch timeout in this case.
10148 synchronized (mKeyWaiter) {
10149 mKeyWaiter.mWasFrozen = true;
10150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010151 return;
10152 }
Romain Guy06882f82009-06-10 13:36:04 -070010153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 long now = SystemClock.uptimeMillis();
10157 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10158 if (mFreezeGcPending != 0) {
10159 if (now > (mFreezeGcPending+1000)) {
10160 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10161 mH.removeMessages(H.FORCE_GC);
10162 Runtime.getRuntime().gc();
10163 mFreezeGcPending = now;
10164 }
10165 } else {
10166 mFreezeGcPending = now;
10167 }
Romain Guy06882f82009-06-10 13:36:04 -070010168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010169 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010170 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10171 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172 mAppTransitionReady = true;
10173 }
Romain Guy06882f82009-06-10 13:36:04 -070010174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010175 if (PROFILE_ORIENTATION) {
10176 File file = new File("/data/system/frozen");
10177 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10178 }
10179 Surface.freezeDisplay(0);
10180 }
Romain Guy06882f82009-06-10 13:36:04 -070010181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 private void stopFreezingDisplayLocked() {
10183 if (!mDisplayFrozen) {
10184 return;
10185 }
Romain Guy06882f82009-06-10 13:36:04 -070010186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 mDisplayFrozen = false;
10188 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10189 if (PROFILE_ORIENTATION) {
10190 Debug.stopMethodTracing();
10191 }
10192 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010193
Chris Tate2ad63a92009-03-25 17:36:48 -070010194 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10195 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010196 synchronized (mKeyWaiter) {
10197 mKeyWaiter.mWasFrozen = true;
10198 mKeyWaiter.notifyAll();
10199 }
10200
10201 // A little kludge: a lot could have happened while the
10202 // display was frozen, so now that we are coming back we
10203 // do a gc so that any remote references the system
10204 // processes holds on others can be released if they are
10205 // no longer needed.
10206 mH.removeMessages(H.FORCE_GC);
10207 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10208 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010210 mScreenFrozenLock.release();
10211 }
Romain Guy06882f82009-06-10 13:36:04 -070010212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010213 @Override
10214 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10215 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10216 != PackageManager.PERMISSION_GRANTED) {
10217 pw.println("Permission Denial: can't dump WindowManager from from pid="
10218 + Binder.getCallingPid()
10219 + ", uid=" + Binder.getCallingUid());
10220 return;
10221 }
Romain Guy06882f82009-06-10 13:36:04 -070010222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010223 synchronized(mWindowMap) {
10224 pw.println("Current Window Manager state:");
10225 for (int i=mWindows.size()-1; i>=0; i--) {
10226 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010227 pw.print(" Window #"); pw.print(i); pw.print(' ');
10228 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010229 w.dump(pw, " ");
10230 }
10231 if (mInputMethodDialogs.size() > 0) {
10232 pw.println(" ");
10233 pw.println(" Input method dialogs:");
10234 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10235 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010236 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 }
10238 }
10239 if (mPendingRemove.size() > 0) {
10240 pw.println(" ");
10241 pw.println(" Remove pending for:");
10242 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10243 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010244 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10245 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010246 w.dump(pw, " ");
10247 }
10248 }
10249 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10250 pw.println(" ");
10251 pw.println(" Windows force removing:");
10252 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10253 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010254 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10255 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010256 w.dump(pw, " ");
10257 }
10258 }
10259 if (mDestroySurface.size() > 0) {
10260 pw.println(" ");
10261 pw.println(" Windows waiting to destroy their surface:");
10262 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10263 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010264 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10265 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010266 w.dump(pw, " ");
10267 }
10268 }
10269 if (mLosingFocus.size() > 0) {
10270 pw.println(" ");
10271 pw.println(" Windows losing focus:");
10272 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10273 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010274 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10275 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 w.dump(pw, " ");
10277 }
10278 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010279 if (mResizingWindows.size() > 0) {
10280 pw.println(" ");
10281 pw.println(" Windows waiting to resize:");
10282 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10283 WindowState w = mResizingWindows.get(i);
10284 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10285 pw.print(w); pw.println(":");
10286 w.dump(pw, " ");
10287 }
10288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010289 if (mSessions.size() > 0) {
10290 pw.println(" ");
10291 pw.println(" All active sessions:");
10292 Iterator<Session> it = mSessions.iterator();
10293 while (it.hasNext()) {
10294 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010295 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 s.dump(pw, " ");
10297 }
10298 }
10299 if (mTokenMap.size() > 0) {
10300 pw.println(" ");
10301 pw.println(" All tokens:");
10302 Iterator<WindowToken> it = mTokenMap.values().iterator();
10303 while (it.hasNext()) {
10304 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010305 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 token.dump(pw, " ");
10307 }
10308 }
10309 if (mTokenList.size() > 0) {
10310 pw.println(" ");
10311 pw.println(" Window token list:");
10312 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010313 pw.print(" #"); pw.print(i); pw.print(": ");
10314 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010315 }
10316 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010317 if (mWallpaperTokens.size() > 0) {
10318 pw.println(" ");
10319 pw.println(" Wallpaper tokens:");
10320 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10321 WindowToken token = mWallpaperTokens.get(i);
10322 pw.print(" Wallpaper #"); pw.print(i);
10323 pw.print(' '); pw.print(token); pw.println(':');
10324 token.dump(pw, " ");
10325 }
10326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 if (mAppTokens.size() > 0) {
10328 pw.println(" ");
10329 pw.println(" Application tokens in Z order:");
10330 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010331 pw.print(" App #"); pw.print(i); pw.print(": ");
10332 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 }
10334 }
10335 if (mFinishedStarting.size() > 0) {
10336 pw.println(" ");
10337 pw.println(" Finishing start of application tokens:");
10338 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10339 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010340 pw.print(" Finished Starting #"); pw.print(i);
10341 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 token.dump(pw, " ");
10343 }
10344 }
10345 if (mExitingTokens.size() > 0) {
10346 pw.println(" ");
10347 pw.println(" Exiting tokens:");
10348 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10349 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010350 pw.print(" Exiting #"); pw.print(i);
10351 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 token.dump(pw, " ");
10353 }
10354 }
10355 if (mExitingAppTokens.size() > 0) {
10356 pw.println(" ");
10357 pw.println(" Exiting application tokens:");
10358 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10359 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010360 pw.print(" Exiting App #"); pw.print(i);
10361 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 token.dump(pw, " ");
10363 }
10364 }
10365 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010366 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10367 pw.print(" mLastFocus="); pw.println(mLastFocus);
10368 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10369 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10370 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010371 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010372 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10373 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10374 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10375 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010376 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10377 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10378 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10379 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10380 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010381 if (mDimAnimator != null) {
10382 mDimAnimator.printTo(pw);
10383 } else {
10384 pw.print( " no DimAnimator ");
10385 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010386 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010387 pw.print(mInputMethodAnimLayerAdjustment);
10388 pw.print(" mWallpaperAnimLayerAdjustment=");
10389 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010390 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10391 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010392 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10393 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10394 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10395 pw.print(" mRotation="); pw.print(mRotation);
10396 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10397 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10398 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10399 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10400 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10401 pw.print(" mNextAppTransition=0x");
10402 pw.print(Integer.toHexString(mNextAppTransition));
10403 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010404 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010405 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10406 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10407 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10408 if (mOpeningApps.size() > 0) {
10409 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10410 }
10411 if (mClosingApps.size() > 0) {
10412 pw.print(" mClosingApps="); pw.println(mClosingApps);
10413 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010414 if (mToTopApps.size() > 0) {
10415 pw.print(" mToTopApps="); pw.println(mToTopApps);
10416 }
10417 if (mToBottomApps.size() > 0) {
10418 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10419 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010420 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10421 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010423 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10424 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10425 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10426 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10427 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10428 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 }
10430 }
10431
10432 public void monitor() {
10433 synchronized (mWindowMap) { }
10434 synchronized (mKeyguardDisabled) { }
10435 synchronized (mKeyWaiter) { }
10436 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010437
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010438 public void virtualKeyFeedback(KeyEvent event) {
10439 mPolicy.keyFeedbackFromInput(event);
10440 }
10441
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010442 /**
10443 * DimAnimator class that controls the dim animation. This holds the surface and
10444 * all state used for dim animation.
10445 */
10446 private static class DimAnimator {
10447 Surface mDimSurface;
10448 boolean mDimShown = false;
10449 float mDimCurrentAlpha;
10450 float mDimTargetAlpha;
10451 float mDimDeltaPerMs;
10452 long mLastDimAnimTime;
10453
10454 DimAnimator (SurfaceSession session) {
10455 if (mDimSurface == null) {
10456 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10457 + mDimSurface + ": CREATE");
10458 try {
10459 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10460 Surface.FX_SURFACE_DIM);
10461 } catch (Exception e) {
10462 Log.e(TAG, "Exception creating Dim surface", e);
10463 }
10464 }
10465 }
10466
10467 /**
10468 * Show the dim surface.
10469 */
10470 void show(int dw, int dh) {
10471 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10472 dw + "x" + dh + ")");
10473 mDimShown = true;
10474 try {
10475 mDimSurface.setPosition(0, 0);
10476 mDimSurface.setSize(dw, dh);
10477 mDimSurface.show();
10478 } catch (RuntimeException e) {
10479 Log.w(TAG, "Failure showing dim surface", e);
10480 }
10481 }
10482
10483 /**
10484 * Set's the dim surface's layer and update dim parameters that will be used in
10485 * {@link updateSurface} after all windows are examined.
10486 */
10487 void updateParameters(WindowState w, long currentTime) {
10488 mDimSurface.setLayer(w.mAnimLayer-1);
10489
10490 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010491 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10492 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010493 if (mDimTargetAlpha != target) {
10494 // If the desired dim level has changed, then
10495 // start an animation to it.
10496 mLastDimAnimTime = currentTime;
10497 long duration = (w.mAnimating && w.mAnimation != null)
10498 ? w.mAnimation.computeDurationHint()
10499 : DEFAULT_DIM_DURATION;
10500 if (target > mDimTargetAlpha) {
10501 // This is happening behind the activity UI,
10502 // so we can make it run a little longer to
10503 // give a stronger impression without disrupting
10504 // the user.
10505 duration *= DIM_DURATION_MULTIPLIER;
10506 }
10507 if (duration < 1) {
10508 // Don't divide by zero
10509 duration = 1;
10510 }
10511 mDimTargetAlpha = target;
10512 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10513 }
10514 }
10515
10516 /**
10517 * Updating the surface's alpha. Returns true if the animation continues, or returns
10518 * false when the animation is finished and the dim surface is hidden.
10519 */
10520 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10521 if (!dimming) {
10522 if (mDimTargetAlpha != 0) {
10523 mLastDimAnimTime = currentTime;
10524 mDimTargetAlpha = 0;
10525 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10526 }
10527 }
10528
10529 boolean animating = false;
10530 if (mLastDimAnimTime != 0) {
10531 mDimCurrentAlpha += mDimDeltaPerMs
10532 * (currentTime-mLastDimAnimTime);
10533 boolean more = true;
10534 if (displayFrozen) {
10535 // If the display is frozen, there is no reason to animate.
10536 more = false;
10537 } else if (mDimDeltaPerMs > 0) {
10538 if (mDimCurrentAlpha > mDimTargetAlpha) {
10539 more = false;
10540 }
10541 } else if (mDimDeltaPerMs < 0) {
10542 if (mDimCurrentAlpha < mDimTargetAlpha) {
10543 more = false;
10544 }
10545 } else {
10546 more = false;
10547 }
10548
10549 // Do we need to continue animating?
10550 if (more) {
10551 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10552 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10553 mLastDimAnimTime = currentTime;
10554 mDimSurface.setAlpha(mDimCurrentAlpha);
10555 animating = true;
10556 } else {
10557 mDimCurrentAlpha = mDimTargetAlpha;
10558 mLastDimAnimTime = 0;
10559 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10560 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10561 mDimSurface.setAlpha(mDimCurrentAlpha);
10562 if (!dimming) {
10563 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10564 + ": HIDE");
10565 try {
10566 mDimSurface.hide();
10567 } catch (RuntimeException e) {
10568 Log.w(TAG, "Illegal argument exception hiding dim surface");
10569 }
10570 mDimShown = false;
10571 }
10572 }
10573 }
10574 return animating;
10575 }
10576
10577 public void printTo(PrintWriter pw) {
10578 pw.print(" mDimShown="); pw.print(mDimShown);
10579 pw.print(" current="); pw.print(mDimCurrentAlpha);
10580 pw.print(" target="); pw.print(mDimTargetAlpha);
10581 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10582 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10583 }
10584 }
10585
10586 /**
10587 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10588 * This is used for opening/closing transition for apps in compatible mode.
10589 */
10590 private static class FadeInOutAnimation extends Animation {
10591 int mWidth;
10592 boolean mFadeIn;
10593
10594 public FadeInOutAnimation(boolean fadeIn) {
10595 setInterpolator(new AccelerateInterpolator());
10596 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10597 mFadeIn = fadeIn;
10598 }
10599
10600 @Override
10601 protected void applyTransformation(float interpolatedTime, Transformation t) {
10602 float x = interpolatedTime;
10603 if (!mFadeIn) {
10604 x = 1.0f - x; // reverse the interpolation for fade out
10605 }
10606 if (x < 0.5) {
10607 // move the window out of the screen.
10608 t.getMatrix().setTranslate(mWidth, 0);
10609 } else {
10610 t.getMatrix().setTranslate(0, 0);// show
10611 t.setAlpha((x - 0.5f) * 2);
10612 }
10613 }
10614
10615 @Override
10616 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10617 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10618 mWidth = width;
10619 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010620
10621 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010622 public int getZAdjustment() {
10623 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010624 }
10625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626}