blob: 78a82dd4b765ad8528cd85db2be8b54fe24e99f0 [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 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700305 * This was the app token that was used to retrieve the last enter
306 * animation. It will be used for the next exit animation.
307 */
308 AppWindowToken mLastEnterAnimToken;
309
310 /**
311 * These were the layout params used to retrieve the last enter animation.
312 * They will be used for the next exit animation.
313 */
314 LayoutParams mLastEnterAnimParams;
315
316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 * Z-ordered (bottom-most first) list of all Window objects.
318 */
319 final ArrayList mWindows = new ArrayList();
320
321 /**
322 * Windows that are being resized. Used so we can tell the client about
323 * the resize after closing the transaction in which we resized the
324 * underlying surface.
325 */
326 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
327
328 /**
329 * Windows whose animations have ended and now must be removed.
330 */
331 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
332
333 /**
334 * Windows whose surface should be destroyed.
335 */
336 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
337
338 /**
339 * Windows that have lost input focus and are waiting for the new
340 * focus window to be displayed before they are told about this.
341 */
342 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
343
344 /**
345 * This is set when we have run out of memory, and will either be an empty
346 * list or contain windows that need to be force removed.
347 */
348 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700353 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 Surface mBlurSurface;
355 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 final float[] mTmpFloats = new float[9];
360
361 boolean mSafeMode;
362 boolean mDisplayEnabled = false;
363 boolean mSystemBooted = false;
364 int mRotation = 0;
365 int mRequestedRotation = 0;
366 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700367 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 ArrayList<IRotationWatcher> mRotationWatchers
369 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 boolean mLayoutNeeded = true;
372 boolean mAnimationPending = false;
373 boolean mDisplayFrozen = false;
374 boolean mWindowsFreezingScreen = false;
375 long mFreezeGcPending = 0;
376 int mAppsFreezingScreen = 0;
377
378 // This is held as long as we have the screen frozen, to give us time to
379 // perform a rotation animation when turning off shows the lock screen which
380 // changes the orientation.
381 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 // State management of app transitions. When we are preparing for a
384 // transition, mNextAppTransition will be the kind of transition to
385 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
386 // mOpeningApps and mClosingApps are the lists of tokens that will be
387 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700388 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700389 String mNextAppTransitionPackage;
390 int mNextAppTransitionEnter;
391 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700393 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 boolean mAppTransitionTimeout = false;
395 boolean mStartingIconInTransition = false;
396 boolean mSkipAppTransitionAnimation = false;
397 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
398 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700399 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
400 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 //flag to detect fat touch events
403 boolean mFatTouch = false;
404 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 H mH = new H();
407
408 WindowState mCurrentFocus = null;
409 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 // This just indicates the window the input method is on top of, not
412 // necessarily the window its input is going to.
413 WindowState mInputMethodTarget = null;
414 WindowState mUpcomingInputMethodTarget = null;
415 boolean mInputMethodTargetWaitingAnim;
416 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 WindowState mInputMethodWindow = null;
419 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
420
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700421 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
422
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700423 // If non-null, this is the currently visible window that is associated
424 // with the wallpaper.
425 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700426 // If non-null, we are in the middle of animating from one wallpaper target
427 // to another, and this is the lower one in Z-order.
428 WindowState mLowerWallpaperTarget = null;
429 // If non-null, we are in the middle of animating from one wallpaper target
430 // to another, and this is the higher one in Z-order.
431 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700432 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700433 float mLastWallpaperX;
434 float mLastWallpaperY;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700435 // Lock for waiting for the wallpaper.
436 final Object mWaitingOnWallpaperLock = new Object();
437 // This is set when we are waiting for a wallpaper to tell us it is done
438 // changing its scroll position.
439 WindowState mWaitingOnWallpaper;
440 // The last time we had a timeout when waiting for a wallpaper.
441 long mLastWallpaperTimeoutTime;
442 // We give a wallpaper up to 150ms to finish scrolling.
443 static final long WALLPAPER_TIMEOUT = 150;
444 // Time we wait after a timeout before trying to wait again.
445 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 AppWindowToken mFocusedApp = null;
448
449 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 float mWindowAnimationScale = 1.0f;
452 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 final KeyWaiter mKeyWaiter = new KeyWaiter();
455 final KeyQ mQueue;
456 final InputDispatcherThread mInputThread;
457
458 // Who is holding the screen on.
459 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700460
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700461 boolean mTurnOnScreen;
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
464 * Whether the UI is currently running in touch mode (not showing
465 * navigational focus because the user is directly pressing the screen).
466 */
467 boolean mInTouchMode = false;
468
469 private ViewServer mViewServer;
470
471 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700472
Dianne Hackbornc485a602009-03-24 22:39:49 -0700473 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700474 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700475
476 // The frame use to limit the size of the app running in compatibility mode.
477 Rect mCompatibleScreenFrame = new Rect();
478 // The surface used to fill the outer rim of the app running in compatibility mode.
479 Surface mBackgroundFillerSurface = null;
480 boolean mBackgroundFillerShown = false;
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public static WindowManagerService main(Context context,
483 PowerManagerService pm, boolean haveInputMethods) {
484 WMThread thr = new WMThread(context, pm, haveInputMethods);
485 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 synchronized (thr) {
488 while (thr.mService == null) {
489 try {
490 thr.wait();
491 } catch (InterruptedException e) {
492 }
493 }
494 }
Romain Guy06882f82009-06-10 13:36:04 -0700495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 return thr.mService;
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 static class WMThread extends Thread {
500 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 private final Context mContext;
503 private final PowerManagerService mPM;
504 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 public WMThread(Context context, PowerManagerService pm,
507 boolean haveInputMethods) {
508 super("WindowManager");
509 mContext = context;
510 mPM = pm;
511 mHaveInputMethods = haveInputMethods;
512 }
Romain Guy06882f82009-06-10 13:36:04 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 public void run() {
515 Looper.prepare();
516 WindowManagerService s = new WindowManagerService(mContext, mPM,
517 mHaveInputMethods);
518 android.os.Process.setThreadPriority(
519 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 synchronized (this) {
522 mService = s;
523 notifyAll();
524 }
Romain Guy06882f82009-06-10 13:36:04 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 Looper.loop();
527 }
528 }
529
530 static class PolicyThread extends Thread {
531 private final WindowManagerPolicy mPolicy;
532 private final WindowManagerService mService;
533 private final Context mContext;
534 private final PowerManagerService mPM;
535 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public PolicyThread(WindowManagerPolicy policy,
538 WindowManagerService service, Context context,
539 PowerManagerService pm) {
540 super("WindowManagerPolicy");
541 mPolicy = policy;
542 mService = service;
543 mContext = context;
544 mPM = pm;
545 }
Romain Guy06882f82009-06-10 13:36:04 -0700546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 public void run() {
548 Looper.prepare();
549 //Looper.myLooper().setMessageLogging(new LogPrinter(
550 // Log.VERBOSE, "WindowManagerPolicy"));
551 android.os.Process.setThreadPriority(
552 android.os.Process.THREAD_PRIORITY_FOREGROUND);
553 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 synchronized (this) {
556 mRunning = true;
557 notifyAll();
558 }
Romain Guy06882f82009-06-10 13:36:04 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 Looper.loop();
561 }
562 }
563
564 private WindowManagerService(Context context, PowerManagerService pm,
565 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700566 if (MEASURE_LATENCY) {
567 lt = new LatencyTimer(100, 1000);
568 }
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 mContext = context;
571 mHaveInputMethods = haveInputMethods;
572 mLimitedAlphaCompositing = context.getResources().getBoolean(
573 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 mPowerManager = pm;
576 mPowerManager.setPolicy(mPolicy);
577 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
578 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
579 "SCREEN_FROZEN");
580 mScreenFrozenLock.setReferenceCounted(false);
581
582 mActivityManager = ActivityManagerNative.getDefault();
583 mBatteryStats = BatteryStatsService.getService();
584
585 // Get persisted window scale setting
586 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
587 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
588 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
589 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700590
Michael Chan9f028e62009-08-04 17:37:46 -0700591 int max_events_per_sec = 35;
592 try {
593 max_events_per_sec = Integer.parseInt(SystemProperties
594 .get("windowsmgr.max_events_per_sec"));
595 if (max_events_per_sec < 1) {
596 max_events_per_sec = 35;
597 }
598 } catch (NumberFormatException e) {
599 }
600 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mQueue = new KeyQ();
603
604 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
607 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 synchronized (thr) {
610 while (!thr.mRunning) {
611 try {
612 thr.wait();
613 } catch (InterruptedException e) {
614 }
615 }
616 }
Romain Guy06882f82009-06-10 13:36:04 -0700617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 // Add ourself to the Watchdog monitors.
621 Watchdog.getInstance().addMonitor(this);
622 }
623
624 @Override
625 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
626 throws RemoteException {
627 try {
628 return super.onTransact(code, data, reply, flags);
629 } catch (RuntimeException e) {
630 // The window manager only throws security exceptions, so let's
631 // log all others.
632 if (!(e instanceof SecurityException)) {
633 Log.e(TAG, "Window Manager Crash", e);
634 }
635 throw e;
636 }
637 }
638
639 private void placeWindowAfter(Object pos, WindowState window) {
640 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700641 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 TAG, "Adding window " + window + " at "
643 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
644 mWindows.add(i+1, window);
645 }
646
647 private void placeWindowBefore(Object pos, WindowState window) {
648 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700649 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 TAG, "Adding window " + window + " at "
651 + i + " of " + mWindows.size() + " (before " + pos + ")");
652 mWindows.add(i, window);
653 }
654
655 //This method finds out the index of a window that has the same app token as
656 //win. used for z ordering the windows in mWindows
657 private int findIdxBasedOnAppTokens(WindowState win) {
658 //use a local variable to cache mWindows
659 ArrayList localmWindows = mWindows;
660 int jmax = localmWindows.size();
661 if(jmax == 0) {
662 return -1;
663 }
664 for(int j = (jmax-1); j >= 0; j--) {
665 WindowState wentry = (WindowState)localmWindows.get(j);
666 if(wentry.mAppToken == win.mAppToken) {
667 return j;
668 }
669 }
670 return -1;
671 }
Romain Guy06882f82009-06-10 13:36:04 -0700672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
674 final IWindow client = win.mClient;
675 final WindowToken token = win.mToken;
676 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 final int N = localmWindows.size();
679 final WindowState attached = win.mAttachedWindow;
680 int i;
681 if (attached == null) {
682 int tokenWindowsPos = token.windows.size();
683 if (token.appWindowToken != null) {
684 int index = tokenWindowsPos-1;
685 if (index >= 0) {
686 // If this application has existing windows, we
687 // simply place the new window on top of them... but
688 // keep the starting window on top.
689 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
690 // Base windows go behind everything else.
691 placeWindowBefore(token.windows.get(0), win);
692 tokenWindowsPos = 0;
693 } else {
694 AppWindowToken atoken = win.mAppToken;
695 if (atoken != null &&
696 token.windows.get(index) == atoken.startingWindow) {
697 placeWindowBefore(token.windows.get(index), win);
698 tokenWindowsPos--;
699 } else {
700 int newIdx = findIdxBasedOnAppTokens(win);
701 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700702 //there is a window above this one associated with the same
703 //apptoken note that the window could be a floating window
704 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700706 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
707 TAG, "Adding window " + win + " at "
708 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712 }
713 } else {
714 if (localLOGV) Log.v(
715 TAG, "Figuring out where to add app window "
716 + client.asBinder() + " (token=" + token + ")");
717 // Figure out where the window should go, based on the
718 // order of applications.
719 final int NA = mAppTokens.size();
720 Object pos = null;
721 for (i=NA-1; i>=0; i--) {
722 AppWindowToken t = mAppTokens.get(i);
723 if (t == token) {
724 i--;
725 break;
726 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700727
728 // We haven't reached the token yet; if this token
729 // is not going to the bottom and has windows, we can
730 // use it as an anchor for when we do reach the token.
731 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 pos = t.windows.get(0);
733 }
734 }
735 // We now know the index into the apps. If we found
736 // an app window above, that gives us the position; else
737 // we need to look some more.
738 if (pos != null) {
739 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700740 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 mTokenMap.get(((WindowState)pos).mClient.asBinder());
742 if (atoken != null) {
743 final int NC = atoken.windows.size();
744 if (NC > 0) {
745 WindowState bottom = atoken.windows.get(0);
746 if (bottom.mSubLayer < 0) {
747 pos = bottom;
748 }
749 }
750 }
751 placeWindowBefore(pos, win);
752 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700753 // Continue looking down until we find the first
754 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 while (i >= 0) {
756 AppWindowToken t = mAppTokens.get(i);
757 final int NW = t.windows.size();
758 if (NW > 0) {
759 pos = t.windows.get(NW-1);
760 break;
761 }
762 i--;
763 }
764 if (pos != null) {
765 // Move in front of any windows attached to this
766 // one.
767 WindowToken atoken =
768 mTokenMap.get(((WindowState)pos).mClient.asBinder());
769 if (atoken != null) {
770 final int NC = atoken.windows.size();
771 if (NC > 0) {
772 WindowState top = atoken.windows.get(NC-1);
773 if (top.mSubLayer >= 0) {
774 pos = top;
775 }
776 }
777 }
778 placeWindowAfter(pos, win);
779 } else {
780 // Just search for the start of this layer.
781 final int myLayer = win.mBaseLayer;
782 for (i=0; i<N; i++) {
783 WindowState w = (WindowState)localmWindows.get(i);
784 if (w.mBaseLayer > myLayer) {
785 break;
786 }
787 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700788 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
789 TAG, "Adding window " + win + " at "
790 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 localmWindows.add(i, win);
792 }
793 }
794 }
795 } else {
796 // Figure out where window should go, based on layer.
797 final int myLayer = win.mBaseLayer;
798 for (i=N-1; i>=0; i--) {
799 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
800 i++;
801 break;
802 }
803 }
804 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700805 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
806 TAG, "Adding window " + win + " at "
807 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 localmWindows.add(i, win);
809 }
810 if (addToToken) {
811 token.windows.add(tokenWindowsPos, win);
812 }
813
814 } else {
815 // Figure out this window's ordering relative to the window
816 // it is attached to.
817 final int NA = token.windows.size();
818 final int sublayer = win.mSubLayer;
819 int largestSublayer = Integer.MIN_VALUE;
820 WindowState windowWithLargestSublayer = null;
821 for (i=0; i<NA; i++) {
822 WindowState w = token.windows.get(i);
823 final int wSublayer = w.mSubLayer;
824 if (wSublayer >= largestSublayer) {
825 largestSublayer = wSublayer;
826 windowWithLargestSublayer = w;
827 }
828 if (sublayer < 0) {
829 // For negative sublayers, we go below all windows
830 // in the same sublayer.
831 if (wSublayer >= sublayer) {
832 if (addToToken) {
833 token.windows.add(i, win);
834 }
835 placeWindowBefore(
836 wSublayer >= 0 ? attached : w, win);
837 break;
838 }
839 } else {
840 // For positive sublayers, we go above all windows
841 // in the same sublayer.
842 if (wSublayer > sublayer) {
843 if (addToToken) {
844 token.windows.add(i, win);
845 }
846 placeWindowBefore(w, win);
847 break;
848 }
849 }
850 }
851 if (i >= NA) {
852 if (addToToken) {
853 token.windows.add(win);
854 }
855 if (sublayer < 0) {
856 placeWindowBefore(attached, win);
857 } else {
858 placeWindowAfter(largestSublayer >= 0
859 ? windowWithLargestSublayer
860 : attached,
861 win);
862 }
863 }
864 }
Romain Guy06882f82009-06-10 13:36:04 -0700865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 if (win.mAppToken != null && addToToken) {
867 win.mAppToken.allAppWindows.add(win);
868 }
869 }
Romain Guy06882f82009-06-10 13:36:04 -0700870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 static boolean canBeImeTarget(WindowState w) {
872 final int fl = w.mAttrs.flags
873 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
874 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
875 return w.isVisibleOrAdding();
876 }
877 return false;
878 }
Romain Guy06882f82009-06-10 13:36:04 -0700879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
881 final ArrayList localmWindows = mWindows;
882 final int N = localmWindows.size();
883 WindowState w = null;
884 int i = N;
885 while (i > 0) {
886 i--;
887 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
890 // + Integer.toHexString(w.mAttrs.flags));
891 if (canBeImeTarget(w)) {
892 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 // Yet more tricksyness! If this window is a "starting"
895 // window, we do actually want to be on top of it, but
896 // it is not -really- where input will go. So if the caller
897 // is not actually looking to move the IME, look down below
898 // for a real window to target...
899 if (!willMove
900 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
901 && i > 0) {
902 WindowState wb = (WindowState)localmWindows.get(i-1);
903 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
904 i--;
905 w = wb;
906 }
907 }
908 break;
909 }
910 }
Romain Guy06882f82009-06-10 13:36:04 -0700911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
915 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (willMove && w != null) {
918 final WindowState curTarget = mInputMethodTarget;
919 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 // Now some fun for dealing with window animations that
922 // modify the Z order. We need to look at all windows below
923 // the current target that are in this app, finding the highest
924 // visible one in layering.
925 AppWindowToken token = curTarget.mAppToken;
926 WindowState highestTarget = null;
927 int highestPos = 0;
928 if (token.animating || token.animation != null) {
929 int pos = 0;
930 pos = localmWindows.indexOf(curTarget);
931 while (pos >= 0) {
932 WindowState win = (WindowState)localmWindows.get(pos);
933 if (win.mAppToken != token) {
934 break;
935 }
936 if (!win.mRemoved) {
937 if (highestTarget == null || win.mAnimLayer >
938 highestTarget.mAnimLayer) {
939 highestTarget = win;
940 highestPos = pos;
941 }
942 }
943 pos--;
944 }
945 }
Romain Guy06882f82009-06-10 13:36:04 -0700946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700948 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 + mNextAppTransition + " " + highestTarget
950 + " animating=" + highestTarget.isAnimating()
951 + " layer=" + highestTarget.mAnimLayer
952 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700953
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700954 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 // If we are currently setting up for an animation,
956 // hold everything until we can find out what will happen.
957 mInputMethodTargetWaitingAnim = true;
958 mInputMethodTarget = highestTarget;
959 return highestPos + 1;
960 } else if (highestTarget.isAnimating() &&
961 highestTarget.mAnimLayer > w.mAnimLayer) {
962 // If the window we are currently targeting is involved
963 // with an animation, and it is on top of the next target
964 // we will be over, then hold off on moving until
965 // that is done.
966 mInputMethodTarget = highestTarget;
967 return highestPos + 1;
968 }
969 }
970 }
971 }
Romain Guy06882f82009-06-10 13:36:04 -0700972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 //Log.i(TAG, "Placing input method @" + (i+1));
974 if (w != null) {
975 if (willMove) {
976 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700977 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
979 + mInputMethodTarget + " to " + w, e);
980 mInputMethodTarget = w;
981 if (w.mAppToken != null) {
982 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
983 } else {
984 setInputMethodAnimLayerAdjustment(0);
985 }
986 }
987 return i+1;
988 }
989 if (willMove) {
990 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700991 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
993 + mInputMethodTarget + " to null", e);
994 mInputMethodTarget = null;
995 setInputMethodAnimLayerAdjustment(0);
996 }
997 return -1;
998 }
Romain Guy06882f82009-06-10 13:36:04 -0700999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 void addInputMethodWindowToListLocked(WindowState win) {
1001 int pos = findDesiredInputMethodWindowIndexLocked(true);
1002 if (pos >= 0) {
1003 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001004 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1005 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 mWindows.add(pos, win);
1007 moveInputMethodDialogsLocked(pos+1);
1008 return;
1009 }
1010 win.mTargetAppToken = null;
1011 addWindowToListInOrderLocked(win, true);
1012 moveInputMethodDialogsLocked(pos);
1013 }
Romain Guy06882f82009-06-10 13:36:04 -07001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 void setInputMethodAnimLayerAdjustment(int adj) {
1016 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1017 mInputMethodAnimLayerAdjustment = adj;
1018 WindowState imw = mInputMethodWindow;
1019 if (imw != null) {
1020 imw.mAnimLayer = imw.mLayer + adj;
1021 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1022 + " anim layer: " + imw.mAnimLayer);
1023 int wi = imw.mChildWindows.size();
1024 while (wi > 0) {
1025 wi--;
1026 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1027 cw.mAnimLayer = cw.mLayer + adj;
1028 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1029 + " anim layer: " + cw.mAnimLayer);
1030 }
1031 }
1032 int di = mInputMethodDialogs.size();
1033 while (di > 0) {
1034 di --;
1035 imw = mInputMethodDialogs.get(di);
1036 imw.mAnimLayer = imw.mLayer + adj;
1037 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1038 + " anim layer: " + imw.mAnimLayer);
1039 }
1040 }
Romain Guy06882f82009-06-10 13:36:04 -07001041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1043 int wpos = mWindows.indexOf(win);
1044 if (wpos >= 0) {
1045 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001046 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 mWindows.remove(wpos);
1048 int NC = win.mChildWindows.size();
1049 while (NC > 0) {
1050 NC--;
1051 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1052 int cpos = mWindows.indexOf(cw);
1053 if (cpos >= 0) {
1054 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001055 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1056 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 mWindows.remove(cpos);
1058 }
1059 }
1060 }
1061 return interestingPos;
1062 }
Romain Guy06882f82009-06-10 13:36:04 -07001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 private void reAddWindowToListInOrderLocked(WindowState win) {
1065 addWindowToListInOrderLocked(win, false);
1066 // This is a hack to get all of the child windows added as well
1067 // at the right position. Child windows should be rare and
1068 // this case should be rare, so it shouldn't be that big a deal.
1069 int wpos = mWindows.indexOf(win);
1070 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001071 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1072 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 mWindows.remove(wpos);
1074 reAddWindowLocked(wpos, win);
1075 }
1076 }
Romain Guy06882f82009-06-10 13:36:04 -07001077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 void logWindowList(String prefix) {
1079 int N = mWindows.size();
1080 while (N > 0) {
1081 N--;
1082 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1083 }
1084 }
Romain Guy06882f82009-06-10 13:36:04 -07001085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 void moveInputMethodDialogsLocked(int pos) {
1087 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 final int N = dialogs.size();
1090 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1091 for (int i=0; i<N; i++) {
1092 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1093 }
1094 if (DEBUG_INPUT_METHOD) {
1095 Log.v(TAG, "Window list w/pos=" + pos);
1096 logWindowList(" ");
1097 }
Romain Guy06882f82009-06-10 13:36:04 -07001098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 if (pos >= 0) {
1100 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1101 if (pos < mWindows.size()) {
1102 WindowState wp = (WindowState)mWindows.get(pos);
1103 if (wp == mInputMethodWindow) {
1104 pos++;
1105 }
1106 }
1107 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1108 for (int i=0; i<N; i++) {
1109 WindowState win = dialogs.get(i);
1110 win.mTargetAppToken = targetAppToken;
1111 pos = reAddWindowLocked(pos, win);
1112 }
1113 if (DEBUG_INPUT_METHOD) {
1114 Log.v(TAG, "Final window list:");
1115 logWindowList(" ");
1116 }
1117 return;
1118 }
1119 for (int i=0; i<N; i++) {
1120 WindowState win = dialogs.get(i);
1121 win.mTargetAppToken = null;
1122 reAddWindowToListInOrderLocked(win);
1123 if (DEBUG_INPUT_METHOD) {
1124 Log.v(TAG, "No IM target, final list:");
1125 logWindowList(" ");
1126 }
1127 }
1128 }
Romain Guy06882f82009-06-10 13:36:04 -07001129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1131 final WindowState imWin = mInputMethodWindow;
1132 final int DN = mInputMethodDialogs.size();
1133 if (imWin == null && DN == 0) {
1134 return false;
1135 }
Romain Guy06882f82009-06-10 13:36:04 -07001136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1138 if (imPos >= 0) {
1139 // In this case, the input method windows are to be placed
1140 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 // First check to see if the input method windows are already
1143 // located here, and contiguous.
1144 final int N = mWindows.size();
1145 WindowState firstImWin = imPos < N
1146 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 // Figure out the actual input method window that should be
1149 // at the bottom of their stack.
1150 WindowState baseImWin = imWin != null
1151 ? imWin : mInputMethodDialogs.get(0);
1152 if (baseImWin.mChildWindows.size() > 0) {
1153 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1154 if (cw.mSubLayer < 0) baseImWin = cw;
1155 }
Romain Guy06882f82009-06-10 13:36:04 -07001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 if (firstImWin == baseImWin) {
1158 // The windows haven't moved... but are they still contiguous?
1159 // First find the top IM window.
1160 int pos = imPos+1;
1161 while (pos < N) {
1162 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1163 break;
1164 }
1165 pos++;
1166 }
1167 pos++;
1168 // Now there should be no more input method windows above.
1169 while (pos < N) {
1170 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1171 break;
1172 }
1173 pos++;
1174 }
1175 if (pos >= N) {
1176 // All is good!
1177 return false;
1178 }
1179 }
Romain Guy06882f82009-06-10 13:36:04 -07001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (imWin != null) {
1182 if (DEBUG_INPUT_METHOD) {
1183 Log.v(TAG, "Moving IM from " + imPos);
1184 logWindowList(" ");
1185 }
1186 imPos = tmpRemoveWindowLocked(imPos, imWin);
1187 if (DEBUG_INPUT_METHOD) {
1188 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1189 logWindowList(" ");
1190 }
1191 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1192 reAddWindowLocked(imPos, imWin);
1193 if (DEBUG_INPUT_METHOD) {
1194 Log.v(TAG, "List after moving IM to " + imPos + ":");
1195 logWindowList(" ");
1196 }
1197 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1198 } else {
1199 moveInputMethodDialogsLocked(imPos);
1200 }
Romain Guy06882f82009-06-10 13:36:04 -07001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 } else {
1203 // In this case, the input method windows go in a fixed layer,
1204 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 if (imWin != null) {
1207 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1208 tmpRemoveWindowLocked(0, imWin);
1209 imWin.mTargetAppToken = null;
1210 reAddWindowToListInOrderLocked(imWin);
1211 if (DEBUG_INPUT_METHOD) {
1212 Log.v(TAG, "List with no IM target:");
1213 logWindowList(" ");
1214 }
1215 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1216 } else {
1217 moveInputMethodDialogsLocked(-1);;
1218 }
Romain Guy06882f82009-06-10 13:36:04 -07001219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
Romain Guy06882f82009-06-10 13:36:04 -07001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 if (needAssignLayers) {
1223 assignLayersLocked();
1224 }
Romain Guy06882f82009-06-10 13:36:04 -07001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 return true;
1227 }
Romain Guy06882f82009-06-10 13:36:04 -07001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 void adjustInputMethodDialogsLocked() {
1230 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1231 }
Romain Guy06882f82009-06-10 13:36:04 -07001232
Dianne Hackborn25994b42009-09-04 14:21:19 -07001233 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1234 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1235 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1236 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1237 ? wallpaperTarget.mAppToken.animation : null)
1238 + " upper=" + mUpperWallpaperTarget
1239 + " lower=" + mLowerWallpaperTarget);
1240 return (wallpaperTarget != null
1241 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1242 && wallpaperTarget.mAppToken.animation != null)))
1243 || mUpperWallpaperTarget != null
1244 || mLowerWallpaperTarget != null;
1245 }
1246
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001247 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1248 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1249
1250 int adjustWallpaperWindowsLocked() {
1251 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001252
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001253 final int dw = mDisplay.getWidth();
1254 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001255
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001256 // First find top-most window that has asked to be on top of the
1257 // wallpaper; all wallpapers go behind it.
1258 final ArrayList localmWindows = mWindows;
1259 int N = localmWindows.size();
1260 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001261 WindowState foundW = null;
1262 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001263 WindowState topCurW = null;
1264 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001265 int i = N;
1266 while (i > 0) {
1267 i--;
1268 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001269 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1270 if (topCurW == null) {
1271 topCurW = w;
1272 topCurI = i;
1273 }
1274 continue;
1275 }
1276 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001277 if (w.mAppToken != null) {
1278 // If this window's app token is hidden and not animating,
1279 // it is of no interest to us.
1280 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1281 if (DEBUG_WALLPAPER) Log.v(TAG,
1282 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001283 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001284 continue;
1285 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001287 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1288 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1289 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001290 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001291 && (mWallpaperTarget == w
1292 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001293 if (DEBUG_WALLPAPER) Log.v(TAG,
1294 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001295 foundW = w;
1296 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001297 if (w == mWallpaperTarget && ((w.mAppToken != null
1298 && w.mAppToken.animation != null)
1299 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001300 // The current wallpaper target is animating, so we'll
1301 // look behind it for another possible target and figure
1302 // out what is going on below.
1303 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1304 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001305 continue;
1306 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001307 break;
1308 }
1309 }
1310
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001311 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001312 // If we are currently waiting for an app transition, and either
1313 // the current target or the next target are involved with it,
1314 // then hold off on doing anything with the wallpaper.
1315 // Note that we are checking here for just whether the target
1316 // is part of an app token... which is potentially overly aggressive
1317 // (the app token may not be involved in the transition), but good
1318 // enough (we'll just wait until whatever transition is pending
1319 // executes).
1320 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001321 if (DEBUG_WALLPAPER) Log.v(TAG,
1322 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001323 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001324 }
1325 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001326 if (DEBUG_WALLPAPER) Log.v(TAG,
1327 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001328 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001329 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001330 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001331
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001332 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001333 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001334 Log.v(TAG, "New wallpaper target: " + foundW
1335 + " oldTarget: " + mWallpaperTarget);
1336 }
1337
1338 mLowerWallpaperTarget = null;
1339 mUpperWallpaperTarget = null;
1340
1341 WindowState oldW = mWallpaperTarget;
1342 mWallpaperTarget = foundW;
1343
1344 // Now what is happening... if the current and new targets are
1345 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001346 if (foundW != null && oldW != null) {
1347 boolean oldAnim = oldW.mAnimation != null
1348 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1349 boolean foundAnim = foundW.mAnimation != null
1350 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001351 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001352 Log.v(TAG, "New animation: " + foundAnim
1353 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001354 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001355 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001356 int oldI = localmWindows.indexOf(oldW);
1357 if (DEBUG_WALLPAPER) {
1358 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1359 }
1360 if (oldI >= 0) {
1361 if (DEBUG_WALLPAPER) {
1362 Log.v(TAG, "Animating wallpapers: old#" + oldI
1363 + "=" + oldW + "; new#" + foundI
1364 + "=" + foundW);
1365 }
1366
1367 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001368 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001369 if (DEBUG_WALLPAPER) {
1370 Log.v(TAG, "Old wallpaper still the target.");
1371 }
1372 mWallpaperTarget = oldW;
1373 }
1374
1375 // Now set the upper and lower wallpaper targets
1376 // correctly, and make sure that we are positioning
1377 // the wallpaper below the lower.
1378 if (foundI > oldI) {
1379 // The new target is on top of the old one.
1380 if (DEBUG_WALLPAPER) {
1381 Log.v(TAG, "Found target above old target.");
1382 }
1383 mUpperWallpaperTarget = foundW;
1384 mLowerWallpaperTarget = oldW;
1385 foundW = oldW;
1386 foundI = oldI;
1387 } else {
1388 // The new target is below the old one.
1389 if (DEBUG_WALLPAPER) {
1390 Log.v(TAG, "Found target below old target.");
1391 }
1392 mUpperWallpaperTarget = oldW;
1393 mLowerWallpaperTarget = foundW;
1394 }
1395 }
1396 }
1397 }
1398
1399 } else {
1400 // Is it time to stop animating?
1401 if (mLowerWallpaperTarget == null
1402 || mLowerWallpaperTarget.mAppToken.animation == null
1403 || mUpperWallpaperTarget == null
1404 || mUpperWallpaperTarget.mAppToken.animation == null) {
1405 if (DEBUG_WALLPAPER) {
1406 Log.v(TAG, "No longer animating wallpaper targets!");
1407 }
1408 mLowerWallpaperTarget = null;
1409 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001410 }
1411 }
1412
1413 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001414 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001415 // The window is visible to the compositor... but is it visible
1416 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001417 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001418 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001419
1420 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001421 // its layer adjustment. Only do this if we are not transfering
1422 // between two wallpaper targets.
1423 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001424 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001425 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001426
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001427 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1428 * TYPE_LAYER_MULTIPLIER
1429 + TYPE_LAYER_OFFSET;
1430
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001431 // Now w is the window we are supposed to be behind... but we
1432 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001433 // AND any starting window associated with it, AND below the
1434 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001435 while (foundI > 0) {
1436 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001437 if (wb.mBaseLayer < maxLayer &&
1438 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001439 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001440 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001441 // This window is not related to the previous one in any
1442 // interesting way, so stop here.
1443 break;
1444 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001445 foundW = wb;
1446 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001447 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001448 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001449 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001450 }
1451
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001452 if (foundW == null && topCurW != null) {
1453 // There is no wallpaper target, so it goes at the bottom.
1454 // We will assume it is the same place as last time, if known.
1455 foundW = topCurW;
1456 foundI = topCurI+1;
1457 } else {
1458 // Okay i is the position immediately above the wallpaper. Look at
1459 // what is below it for later.
1460 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1461 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001462
Dianne Hackborn284ac932009-08-28 10:34:25 -07001463 if (visible) {
1464 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1465 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1466 }
1467
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001468 // Start stepping backwards from here, ensuring that our wallpaper windows
1469 // are correctly placed.
1470 int curTokenIndex = mWallpaperTokens.size();
1471 while (curTokenIndex > 0) {
1472 curTokenIndex--;
1473 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001474 if (token.hidden == visible) {
1475 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1476 token.hidden = !visible;
1477 // Need to do a layout to ensure the wallpaper now has the
1478 // correct size.
1479 mLayoutNeeded = true;
1480 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001481
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001482 int curWallpaperIndex = token.windows.size();
1483 while (curWallpaperIndex > 0) {
1484 curWallpaperIndex--;
1485 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001486
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001487 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001488 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001489 }
1490
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001491 // First, make sure the client has the current visibility
1492 // state.
1493 if (wallpaper.mWallpaperVisible != visible) {
1494 wallpaper.mWallpaperVisible = visible;
1495 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001496 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001497 "Setting visibility of wallpaper " + wallpaper
1498 + ": " + visible);
1499 wallpaper.mClient.dispatchAppVisibility(visible);
1500 } catch (RemoteException e) {
1501 }
1502 }
1503
1504 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001505 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1506 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001507
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001508 // First, if this window is at the current index, then all
1509 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001510 if (wallpaper == foundW) {
1511 foundI--;
1512 foundW = foundI > 0
1513 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001514 continue;
1515 }
1516
1517 // The window didn't match... the current wallpaper window,
1518 // wherever it is, is in the wrong place, so make sure it is
1519 // not in the list.
1520 int oldIndex = localmWindows.indexOf(wallpaper);
1521 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001522 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1523 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001524 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001525 if (oldIndex < foundI) {
1526 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001527 }
1528 }
1529
1530 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001531 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1532 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001533 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001534
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001535 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001536 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001537 }
1538 }
1539
1540 return changed;
1541 }
1542
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001543 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001544 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1545 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001546 mWallpaperAnimLayerAdjustment = adj;
1547 int curTokenIndex = mWallpaperTokens.size();
1548 while (curTokenIndex > 0) {
1549 curTokenIndex--;
1550 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1551 int curWallpaperIndex = token.windows.size();
1552 while (curWallpaperIndex > 0) {
1553 curWallpaperIndex--;
1554 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1555 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001556 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1557 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001558 }
1559 }
1560 }
1561
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001562 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1563 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001564 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001565 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001566 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001567 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001568 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001569 changed = wallpaperWin.mXOffset != offset;
1570 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001571 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1572 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001573 wallpaperWin.mXOffset = offset;
1574 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001575 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1576 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001577 rawChanged = true;
1578 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001579 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001580
Dianne Hackborn284ac932009-08-28 10:34:25 -07001581 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001582 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001583 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001584 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001585 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1586 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001587 changed = true;
1588 wallpaperWin.mYOffset = offset;
1589 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001590 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1591 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001592 rawChanged = true;
1593 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001594 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001595
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001596 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001597 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001598 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1599 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1600 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001601 if (sync) {
1602 synchronized (mWaitingOnWallpaperLock) {
1603 mWaitingOnWallpaper = wallpaperWin;
1604 }
1605 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001606 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001607 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1608 if (sync) {
1609 synchronized (mWaitingOnWallpaperLock) {
1610 if (mWaitingOnWallpaper != null) {
1611 long start = SystemClock.uptimeMillis();
1612 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1613 < start) {
1614 try {
1615 if (DEBUG_WALLPAPER) Log.v(TAG,
1616 "Waiting for offset complete...");
1617 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1618 } catch (InterruptedException e) {
1619 }
1620 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1621 if ((start+WALLPAPER_TIMEOUT)
1622 < SystemClock.uptimeMillis()) {
1623 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1624 + wallpaperWin);
1625 mLastWallpaperTimeoutTime = start;
1626 }
1627 }
1628 mWaitingOnWallpaper = null;
1629 }
1630 }
1631 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001632 } catch (RemoteException e) {
1633 }
1634 }
1635
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001636 return changed;
1637 }
1638
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001639 void wallpaperOffsetsComplete(IBinder window) {
1640 synchronized (mWaitingOnWallpaperLock) {
1641 if (mWaitingOnWallpaper != null &&
1642 mWaitingOnWallpaper.mClient.asBinder() == window) {
1643 mWaitingOnWallpaper = null;
1644 mWaitingOnWallpaperLock.notifyAll();
1645 }
1646 }
1647 }
1648
1649 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001650 final int dw = mDisplay.getWidth();
1651 final int dh = mDisplay.getHeight();
1652
1653 boolean changed = false;
1654
1655 WindowState target = mWallpaperTarget;
1656 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001657 mLastWallpaperX = target.mWallpaperX;
1658 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001659 int curTokenIndex = mWallpaperTokens.size();
1660 while (curTokenIndex > 0) {
1661 curTokenIndex--;
1662 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1663 int curWallpaperIndex = token.windows.size();
1664 while (curWallpaperIndex > 0) {
1665 curWallpaperIndex--;
1666 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001667 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001668 wallpaper.computeShownFrameLocked();
1669 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001670 // We only want to be synchronous with one wallpaper.
1671 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001672 }
1673 }
1674 }
1675 }
1676
1677 return changed;
1678 }
1679
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001680 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001681 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001682 final int dw = mDisplay.getWidth();
1683 final int dh = mDisplay.getHeight();
1684
1685 int curTokenIndex = mWallpaperTokens.size();
1686 while (curTokenIndex > 0) {
1687 curTokenIndex--;
1688 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001689 if (token.hidden == visible) {
1690 token.hidden = !visible;
1691 // Need to do a layout to ensure the wallpaper now has the
1692 // correct size.
1693 mLayoutNeeded = true;
1694 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001695
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001696 int curWallpaperIndex = token.windows.size();
1697 while (curWallpaperIndex > 0) {
1698 curWallpaperIndex--;
1699 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1700 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001701 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001702 }
1703
1704 if (wallpaper.mWallpaperVisible != visible) {
1705 wallpaper.mWallpaperVisible = visible;
1706 try {
1707 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001708 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001709 + ": " + visible);
1710 wallpaper.mClient.dispatchAppVisibility(visible);
1711 } catch (RemoteException e) {
1712 }
1713 }
1714 }
1715 }
1716 }
1717
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001718 void sendPointerToWallpaperLocked(WindowState srcWin,
1719 MotionEvent pointer, long eventTime) {
1720 int curTokenIndex = mWallpaperTokens.size();
1721 while (curTokenIndex > 0) {
1722 curTokenIndex--;
1723 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1724 int curWallpaperIndex = token.windows.size();
1725 while (curWallpaperIndex > 0) {
1726 curWallpaperIndex--;
1727 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1728 if ((wallpaper.mAttrs.flags &
1729 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1730 continue;
1731 }
1732 try {
1733 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1734 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1735 srcWin.mFrame.top-wallpaper.mFrame.top);
1736 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1737 } catch (RemoteException e) {
1738 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1739 }
1740 }
1741 }
1742 }
1743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 public int addWindow(Session session, IWindow client,
1745 WindowManager.LayoutParams attrs, int viewVisibility,
1746 Rect outContentInsets) {
1747 int res = mPolicy.checkAddPermission(attrs);
1748 if (res != WindowManagerImpl.ADD_OKAY) {
1749 return res;
1750 }
Romain Guy06882f82009-06-10 13:36:04 -07001751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 boolean reportNewConfig = false;
1753 WindowState attachedWindow = null;
1754 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 synchronized(mWindowMap) {
1757 // Instantiating a Display requires talking with the simulator,
1758 // so don't do it until we know the system is mostly up and
1759 // running.
1760 if (mDisplay == null) {
1761 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1762 mDisplay = wm.getDefaultDisplay();
1763 mQueue.setDisplay(mDisplay);
1764 reportNewConfig = true;
1765 }
Romain Guy06882f82009-06-10 13:36:04 -07001766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 if (mWindowMap.containsKey(client.asBinder())) {
1768 Log.w(TAG, "Window " + client + " is already added");
1769 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1770 }
1771
1772 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001773 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 if (attachedWindow == null) {
1775 Log.w(TAG, "Attempted to add window with token that is not a window: "
1776 + attrs.token + ". Aborting.");
1777 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1778 }
1779 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1780 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1781 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1782 + attrs.token + ". Aborting.");
1783 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1784 }
1785 }
1786
1787 boolean addToken = false;
1788 WindowToken token = mTokenMap.get(attrs.token);
1789 if (token == null) {
1790 if (attrs.type >= FIRST_APPLICATION_WINDOW
1791 && attrs.type <= LAST_APPLICATION_WINDOW) {
1792 Log.w(TAG, "Attempted to add application window with unknown token "
1793 + attrs.token + ". Aborting.");
1794 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1795 }
1796 if (attrs.type == TYPE_INPUT_METHOD) {
1797 Log.w(TAG, "Attempted to add input method window with unknown token "
1798 + attrs.token + ". Aborting.");
1799 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1800 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001801 if (attrs.type == TYPE_WALLPAPER) {
1802 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1803 + attrs.token + ". Aborting.");
1804 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 token = new WindowToken(attrs.token, -1, false);
1807 addToken = true;
1808 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1809 && attrs.type <= LAST_APPLICATION_WINDOW) {
1810 AppWindowToken atoken = token.appWindowToken;
1811 if (atoken == null) {
1812 Log.w(TAG, "Attempted to add window with non-application token "
1813 + token + ". Aborting.");
1814 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1815 } else if (atoken.removed) {
1816 Log.w(TAG, "Attempted to add window with exiting application token "
1817 + token + ". Aborting.");
1818 return WindowManagerImpl.ADD_APP_EXITING;
1819 }
1820 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1821 // No need for this guy!
1822 if (localLOGV) Log.v(
1823 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1824 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1825 }
1826 } else if (attrs.type == TYPE_INPUT_METHOD) {
1827 if (token.windowType != TYPE_INPUT_METHOD) {
1828 Log.w(TAG, "Attempted to add input method window with bad token "
1829 + attrs.token + ". Aborting.");
1830 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1831 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001832 } else if (attrs.type == TYPE_WALLPAPER) {
1833 if (token.windowType != TYPE_WALLPAPER) {
1834 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1835 + attrs.token + ". Aborting.");
1836 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839
1840 win = new WindowState(session, client, token,
1841 attachedWindow, attrs, viewVisibility);
1842 if (win.mDeathRecipient == null) {
1843 // Client has apparently died, so there is no reason to
1844 // continue.
1845 Log.w(TAG, "Adding window client " + client.asBinder()
1846 + " that is dead, aborting.");
1847 return WindowManagerImpl.ADD_APP_EXITING;
1848 }
1849
1850 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 res = mPolicy.prepareAddWindowLw(win, attrs);
1853 if (res != WindowManagerImpl.ADD_OKAY) {
1854 return res;
1855 }
1856
1857 // From now on, no exceptions or errors allowed!
1858
1859 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 if (addToken) {
1864 mTokenMap.put(attrs.token, token);
1865 mTokenList.add(token);
1866 }
1867 win.attach();
1868 mWindowMap.put(client.asBinder(), win);
1869
1870 if (attrs.type == TYPE_APPLICATION_STARTING &&
1871 token.appWindowToken != null) {
1872 token.appWindowToken.startingWindow = win;
1873 }
1874
1875 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 if (attrs.type == TYPE_INPUT_METHOD) {
1878 mInputMethodWindow = win;
1879 addInputMethodWindowToListLocked(win);
1880 imMayMove = false;
1881 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1882 mInputMethodDialogs.add(win);
1883 addWindowToListInOrderLocked(win, true);
1884 adjustInputMethodDialogsLocked();
1885 imMayMove = false;
1886 } else {
1887 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001888 if (attrs.type == TYPE_WALLPAPER) {
1889 mLastWallpaperTimeoutTime = 0;
1890 adjustWallpaperWindowsLocked();
1891 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001892 adjustWallpaperWindowsLocked();
1893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 }
Romain Guy06882f82009-06-10 13:36:04 -07001895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 if (mInTouchMode) {
1901 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1902 }
1903 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1904 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1905 }
Romain Guy06882f82009-06-10 13:36:04 -07001906
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001907 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001909 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1910 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 imMayMove = false;
1912 }
1913 }
Romain Guy06882f82009-06-10 13:36:04 -07001914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001916 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
Romain Guy06882f82009-06-10 13:36:04 -07001918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 assignLayersLocked();
1920 // Don't do layout here, the window must call
1921 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 //dump();
1924
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001925 if (focusChanged) {
1926 if (mCurrentFocus != null) {
1927 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1928 }
1929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 if (localLOGV) Log.v(
1931 TAG, "New client " + client.asBinder()
1932 + ": window=" + win);
1933 }
1934
1935 // sendNewConfiguration() checks caller permissions so we must call it with
1936 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1937 // identity anyway, so it's safe to just clear & restore around this whole
1938 // block.
1939 final long origId = Binder.clearCallingIdentity();
1940 if (reportNewConfig) {
1941 sendNewConfiguration();
1942 } else {
1943 // Update Orientation after adding a window, only if the window needs to be
1944 // displayed right away
1945 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001946 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 sendNewConfiguration();
1948 }
1949 }
1950 }
1951 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 return res;
1954 }
Romain Guy06882f82009-06-10 13:36:04 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 public void removeWindow(Session session, IWindow client) {
1957 synchronized(mWindowMap) {
1958 WindowState win = windowForClientLocked(session, client);
1959 if (win == null) {
1960 return;
1961 }
1962 removeWindowLocked(session, win);
1963 }
1964 }
Romain Guy06882f82009-06-10 13:36:04 -07001965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 public void removeWindowLocked(Session session, WindowState win) {
1967
1968 if (localLOGV || DEBUG_FOCUS) Log.v(
1969 TAG, "Remove " + win + " client="
1970 + Integer.toHexString(System.identityHashCode(
1971 win.mClient.asBinder()))
1972 + ", surface=" + win.mSurface);
1973
1974 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 if (DEBUG_APP_TRANSITIONS) Log.v(
1977 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1978 + " mExiting=" + win.mExiting
1979 + " isAnimating=" + win.isAnimating()
1980 + " app-animation="
1981 + (win.mAppToken != null ? win.mAppToken.animation : null)
1982 + " inPendingTransaction="
1983 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1984 + " mDisplayFrozen=" + mDisplayFrozen);
1985 // Visibility of the removed window. Will be used later to update orientation later on.
1986 boolean wasVisible = false;
1987 // First, see if we need to run an animation. If we do, we have
1988 // to hold off on removing the window until the animation is done.
1989 // If the display is frozen, just remove immediately, since the
1990 // animation wouldn't be seen.
1991 if (win.mSurface != null && !mDisplayFrozen) {
1992 // If we are not currently running the exit animation, we
1993 // need to see about starting one.
1994 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1997 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1998 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1999 }
2000 // Try starting an animation.
2001 if (applyAnimationLocked(win, transit, false)) {
2002 win.mExiting = true;
2003 }
2004 }
2005 if (win.mExiting || win.isAnimating()) {
2006 // The exit animation is running... wait for it!
2007 //Log.i(TAG, "*** Running exit animation...");
2008 win.mExiting = true;
2009 win.mRemoveOnExit = true;
2010 mLayoutNeeded = true;
2011 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2012 performLayoutAndPlaceSurfacesLocked();
2013 if (win.mAppToken != null) {
2014 win.mAppToken.updateReportedVisibilityLocked();
2015 }
2016 //dump();
2017 Binder.restoreCallingIdentity(origId);
2018 return;
2019 }
2020 }
2021
2022 removeWindowInnerLocked(session, win);
2023 // Removing a visible window will effect the computed orientation
2024 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002025 if (wasVisible && computeForcedAppOrientationLocked()
2026 != mForcedAppOrientation) {
2027 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 }
2029 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2030 Binder.restoreCallingIdentity(origId);
2031 }
Romain Guy06882f82009-06-10 13:36:04 -07002032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002034 mKeyWaiter.finishedKey(session, win.mClient, true,
2035 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2037 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 if (mInputMethodTarget == win) {
2042 moveInputMethodWindowsIfNeededLocked(false);
2043 }
Romain Guy06882f82009-06-10 13:36:04 -07002044
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002045 if (false) {
2046 RuntimeException e = new RuntimeException("here");
2047 e.fillInStackTrace();
2048 Log.w(TAG, "Removing window " + win, e);
2049 }
2050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 mPolicy.removeWindowLw(win);
2052 win.removeLocked();
2053
2054 mWindowMap.remove(win.mClient.asBinder());
2055 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002056 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
2058 if (mInputMethodWindow == win) {
2059 mInputMethodWindow = null;
2060 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2061 mInputMethodDialogs.remove(win);
2062 }
Romain Guy06882f82009-06-10 13:36:04 -07002063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 final WindowToken token = win.mToken;
2065 final AppWindowToken atoken = win.mAppToken;
2066 token.windows.remove(win);
2067 if (atoken != null) {
2068 atoken.allAppWindows.remove(win);
2069 }
2070 if (localLOGV) Log.v(
2071 TAG, "**** Removing window " + win + ": count="
2072 + token.windows.size());
2073 if (token.windows.size() == 0) {
2074 if (!token.explicit) {
2075 mTokenMap.remove(token.token);
2076 mTokenList.remove(token);
2077 } else if (atoken != null) {
2078 atoken.firstWindowDrawn = false;
2079 }
2080 }
2081
2082 if (atoken != null) {
2083 if (atoken.startingWindow == win) {
2084 atoken.startingWindow = null;
2085 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2086 // If this is the last window and we had requested a starting
2087 // transition window, well there is no point now.
2088 atoken.startingData = null;
2089 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2090 // If this is the last window except for a starting transition
2091 // window, we need to get rid of the starting transition.
2092 if (DEBUG_STARTING_WINDOW) {
2093 Log.v(TAG, "Schedule remove starting " + token
2094 + ": no more real windows");
2095 }
2096 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2097 mH.sendMessage(m);
2098 }
2099 }
Romain Guy06882f82009-06-10 13:36:04 -07002100
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002101 if (win.mAttrs.type == TYPE_WALLPAPER) {
2102 mLastWallpaperTimeoutTime = 0;
2103 adjustWallpaperWindowsLocked();
2104 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002105 adjustWallpaperWindowsLocked();
2106 }
2107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 if (!mInLayout) {
2109 assignLayersLocked();
2110 mLayoutNeeded = true;
2111 performLayoutAndPlaceSurfacesLocked();
2112 if (win.mAppToken != null) {
2113 win.mAppToken.updateReportedVisibilityLocked();
2114 }
2115 }
2116 }
2117
2118 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2119 long origId = Binder.clearCallingIdentity();
2120 try {
2121 synchronized (mWindowMap) {
2122 WindowState w = windowForClientLocked(session, client);
2123 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002124 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 Surface.openTransaction();
2126 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002127 if (SHOW_TRANSACTIONS) Log.i(
2128 TAG, " SURFACE " + w.mSurface
2129 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 w.mSurface.setTransparentRegionHint(region);
2131 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002132 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 Surface.closeTransaction();
2134 }
2135 }
2136 }
2137 } finally {
2138 Binder.restoreCallingIdentity(origId);
2139 }
2140 }
2141
2142 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002143 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 Rect visibleInsets) {
2145 long origId = Binder.clearCallingIdentity();
2146 try {
2147 synchronized (mWindowMap) {
2148 WindowState w = windowForClientLocked(session, client);
2149 if (w != null) {
2150 w.mGivenInsetsPending = false;
2151 w.mGivenContentInsets.set(contentInsets);
2152 w.mGivenVisibleInsets.set(visibleInsets);
2153 w.mTouchableInsets = touchableInsets;
2154 mLayoutNeeded = true;
2155 performLayoutAndPlaceSurfacesLocked();
2156 }
2157 }
2158 } finally {
2159 Binder.restoreCallingIdentity(origId);
2160 }
2161 }
Romain Guy06882f82009-06-10 13:36:04 -07002162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 public void getWindowDisplayFrame(Session session, IWindow client,
2164 Rect outDisplayFrame) {
2165 synchronized(mWindowMap) {
2166 WindowState win = windowForClientLocked(session, client);
2167 if (win == null) {
2168 outDisplayFrame.setEmpty();
2169 return;
2170 }
2171 outDisplayFrame.set(win.mDisplayFrame);
2172 }
2173 }
2174
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002175 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2176 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2177 window.mWallpaperX = x;
2178 window.mWallpaperY = y;
2179
2180 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002181 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002182 performLayoutAndPlaceSurfacesLocked();
2183 }
2184 }
2185 }
2186 }
2187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 public int relayoutWindow(Session session, IWindow client,
2189 WindowManager.LayoutParams attrs, int requestedWidth,
2190 int requestedHeight, int viewVisibility, boolean insetsPending,
2191 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2192 Surface outSurface) {
2193 boolean displayed = false;
2194 boolean inTouchMode;
2195 Configuration newConfig = null;
2196 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 synchronized(mWindowMap) {
2199 WindowState win = windowForClientLocked(session, client);
2200 if (win == null) {
2201 return 0;
2202 }
2203 win.mRequestedWidth = requestedWidth;
2204 win.mRequestedHeight = requestedHeight;
2205
2206 if (attrs != null) {
2207 mPolicy.adjustWindowParamsLw(attrs);
2208 }
Romain Guy06882f82009-06-10 13:36:04 -07002209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 int attrChanges = 0;
2211 int flagChanges = 0;
2212 if (attrs != null) {
2213 flagChanges = win.mAttrs.flags ^= attrs.flags;
2214 attrChanges = win.mAttrs.copyFrom(attrs);
2215 }
2216
2217 if (localLOGV) Log.v(
2218 TAG, "Relayout given client " + client.asBinder()
2219 + " (" + win.mAttrs.getTitle() + ")");
2220
2221
2222 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2223 win.mAlpha = attrs.alpha;
2224 }
2225
2226 final boolean scaledWindow =
2227 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2228
2229 if (scaledWindow) {
2230 // requested{Width|Height} Surface's physical size
2231 // attrs.{width|height} Size on screen
2232 win.mHScale = (attrs.width != requestedWidth) ?
2233 (attrs.width / (float)requestedWidth) : 1.0f;
2234 win.mVScale = (attrs.height != requestedHeight) ?
2235 (attrs.height / (float)requestedHeight) : 1.0f;
2236 }
2237
2238 boolean imMayMove = (flagChanges&(
2239 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2240 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 boolean focusMayChange = win.mViewVisibility != viewVisibility
2243 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2244 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002245
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002246 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2247 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 win.mRelayoutCalled = true;
2250 final int oldVisibility = win.mViewVisibility;
2251 win.mViewVisibility = viewVisibility;
2252 if (viewVisibility == View.VISIBLE &&
2253 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2254 displayed = !win.isVisibleLw();
2255 if (win.mExiting) {
2256 win.mExiting = false;
2257 win.mAnimation = null;
2258 }
2259 if (win.mDestroying) {
2260 win.mDestroying = false;
2261 mDestroySurface.remove(win);
2262 }
2263 if (oldVisibility == View.GONE) {
2264 win.mEnterAnimationPending = true;
2265 }
2266 if (displayed && win.mSurface != null && !win.mDrawPending
2267 && !win.mCommitDrawPending && !mDisplayFrozen) {
2268 applyEnterAnimationLocked(win);
2269 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002270 if (displayed && (win.mAttrs.flags
2271 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2272 win.mTurnOnScreen = true;
2273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2275 // To change the format, we need to re-build the surface.
2276 win.destroySurfaceLocked();
2277 displayed = true;
2278 }
2279 try {
2280 Surface surface = win.createSurfaceLocked();
2281 if (surface != null) {
2282 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002283 win.mReportDestroySurface = false;
2284 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002285 if (SHOW_TRANSACTIONS) Log.i(TAG,
2286 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002288 // For some reason there isn't a surface. Clear the
2289 // caller's object so they see the same state.
2290 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 }
2292 } catch (Exception e) {
2293 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002294 + client + " (" + win.mAttrs.getTitle() + ")",
2295 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 Binder.restoreCallingIdentity(origId);
2297 return 0;
2298 }
2299 if (displayed) {
2300 focusMayChange = true;
2301 }
2302 if (win.mAttrs.type == TYPE_INPUT_METHOD
2303 && mInputMethodWindow == null) {
2304 mInputMethodWindow = win;
2305 imMayMove = true;
2306 }
2307 } else {
2308 win.mEnterAnimationPending = false;
2309 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002310 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2311 + ": mExiting=" + win.mExiting
2312 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 // If we are not currently running the exit animation, we
2314 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002315 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 // Try starting an animation; if there isn't one, we
2317 // can destroy the surface right away.
2318 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2319 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2320 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2321 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002322 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002324 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 win.mExiting = true;
2326 mKeyWaiter.finishedKey(session, client, true,
2327 KeyWaiter.RETURN_NOTHING);
2328 } else if (win.isAnimating()) {
2329 // Currently in a hide animation... turn this into
2330 // an exit.
2331 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002332 } else if (win == mWallpaperTarget) {
2333 // If the wallpaper is currently behind this
2334 // window, we need to change both of them inside
2335 // of a transaction to avoid artifacts.
2336 win.mExiting = true;
2337 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 } else {
2339 if (mInputMethodWindow == win) {
2340 mInputMethodWindow = null;
2341 }
2342 win.destroySurfaceLocked();
2343 }
2344 }
2345 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002346
2347 if (win.mSurface == null || (win.getAttrs().flags
2348 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2349 || win.mSurfacePendingDestroy) {
2350 // We are being called from a local process, which
2351 // means outSurface holds its current surface. Ensure the
2352 // surface object is cleared, but we don't want it actually
2353 // destroyed at this point.
2354 win.mSurfacePendingDestroy = false;
2355 outSurface.release();
2356 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2357 } else if (win.mSurface != null) {
2358 if (DEBUG_VISIBILITY) Log.i(TAG,
2359 "Keeping surface, will report destroy: " + win);
2360 win.mReportDestroySurface = true;
2361 outSurface.copyFrom(win.mSurface);
2362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
2364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 if (focusMayChange) {
2366 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2367 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 imMayMove = false;
2369 }
2370 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2371 }
Romain Guy06882f82009-06-10 13:36:04 -07002372
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002373 // updateFocusedWindowLocked() already assigned layers so we only need to
2374 // reassign them at this point if the IM window state gets shuffled
2375 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 if (imMayMove) {
2378 if (moveInputMethodWindowsIfNeededLocked(false)) {
2379 assignLayers = true;
2380 }
2381 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002382 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002383 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002384 assignLayers = true;
2385 }
2386 }
Romain Guy06882f82009-06-10 13:36:04 -07002387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 mLayoutNeeded = true;
2389 win.mGivenInsetsPending = insetsPending;
2390 if (assignLayers) {
2391 assignLayersLocked();
2392 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002393 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002395 if (displayed && win.mIsWallpaper) {
2396 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002397 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 if (win.mAppToken != null) {
2400 win.mAppToken.updateReportedVisibilityLocked();
2401 }
2402 outFrame.set(win.mFrame);
2403 outContentInsets.set(win.mContentInsets);
2404 outVisibleInsets.set(win.mVisibleInsets);
2405 if (localLOGV) Log.v(
2406 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002407 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 + ", requestedHeight=" + requestedHeight
2409 + ", viewVisibility=" + viewVisibility
2410 + "\nRelayout returning frame=" + outFrame
2411 + ", surface=" + outSurface);
2412
2413 if (localLOGV || DEBUG_FOCUS) Log.v(
2414 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2415
2416 inTouchMode = mInTouchMode;
2417 }
2418
2419 if (newConfig != null) {
2420 sendNewConfiguration();
2421 }
Romain Guy06882f82009-06-10 13:36:04 -07002422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2426 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2427 }
2428
2429 public void finishDrawingWindow(Session session, IWindow client) {
2430 final long origId = Binder.clearCallingIdentity();
2431 synchronized(mWindowMap) {
2432 WindowState win = windowForClientLocked(session, client);
2433 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002434 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2435 adjustWallpaperWindowsLocked();
2436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 mLayoutNeeded = true;
2438 performLayoutAndPlaceSurfacesLocked();
2439 }
2440 }
2441 Binder.restoreCallingIdentity(origId);
2442 }
2443
2444 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2445 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2446 + (lp != null ? lp.packageName : null)
2447 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2448 if (lp != null && lp.windowAnimations != 0) {
2449 // If this is a system resource, don't try to load it from the
2450 // application resources. It is nice to avoid loading application
2451 // resources if we can.
2452 String packageName = lp.packageName != null ? lp.packageName : "android";
2453 int resId = lp.windowAnimations;
2454 if ((resId&0xFF000000) == 0x01000000) {
2455 packageName = "android";
2456 }
2457 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2458 + packageName);
2459 return AttributeCache.instance().get(packageName, resId,
2460 com.android.internal.R.styleable.WindowAnimation);
2461 }
2462 return null;
2463 }
Romain Guy06882f82009-06-10 13:36:04 -07002464
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002465 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2466 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2467 + packageName + " resId=0x" + Integer.toHexString(resId));
2468 if (packageName != null) {
2469 if ((resId&0xFF000000) == 0x01000000) {
2470 packageName = "android";
2471 }
2472 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2473 + packageName);
2474 return AttributeCache.instance().get(packageName, resId,
2475 com.android.internal.R.styleable.WindowAnimation);
2476 }
2477 return null;
2478 }
2479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 private void applyEnterAnimationLocked(WindowState win) {
2481 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2482 if (win.mEnterAnimationPending) {
2483 win.mEnterAnimationPending = false;
2484 transit = WindowManagerPolicy.TRANSIT_ENTER;
2485 }
2486
2487 applyAnimationLocked(win, transit, true);
2488 }
2489
2490 private boolean applyAnimationLocked(WindowState win,
2491 int transit, boolean isEntrance) {
2492 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2493 // If we are trying to apply an animation, but already running
2494 // an animation of the same type, then just leave that one alone.
2495 return true;
2496 }
Romain Guy06882f82009-06-10 13:36:04 -07002497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 // Only apply an animation if the display isn't frozen. If it is
2499 // frozen, there is no reason to animate and it can cause strange
2500 // artifacts when we unfreeze the display if some different animation
2501 // is running.
2502 if (!mDisplayFrozen) {
2503 int anim = mPolicy.selectAnimationLw(win, transit);
2504 int attr = -1;
2505 Animation a = null;
2506 if (anim != 0) {
2507 a = AnimationUtils.loadAnimation(mContext, anim);
2508 } else {
2509 switch (transit) {
2510 case WindowManagerPolicy.TRANSIT_ENTER:
2511 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2512 break;
2513 case WindowManagerPolicy.TRANSIT_EXIT:
2514 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2515 break;
2516 case WindowManagerPolicy.TRANSIT_SHOW:
2517 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2518 break;
2519 case WindowManagerPolicy.TRANSIT_HIDE:
2520 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2521 break;
2522 }
2523 if (attr >= 0) {
2524 a = loadAnimation(win.mAttrs, attr);
2525 }
2526 }
2527 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2528 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2529 + " mAnimation=" + win.mAnimation
2530 + " isEntrance=" + isEntrance);
2531 if (a != null) {
2532 if (DEBUG_ANIM) {
2533 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002534 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2536 }
2537 win.setAnimation(a);
2538 win.mAnimationIsEntrance = isEntrance;
2539 }
2540 } else {
2541 win.clearAnimation();
2542 }
2543
2544 return win.mAnimation != null;
2545 }
2546
2547 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2548 int anim = 0;
2549 Context context = mContext;
2550 if (animAttr >= 0) {
2551 AttributeCache.Entry ent = getCachedAnimations(lp);
2552 if (ent != null) {
2553 context = ent.context;
2554 anim = ent.array.getResourceId(animAttr, 0);
2555 }
2556 }
2557 if (anim != 0) {
2558 return AnimationUtils.loadAnimation(context, anim);
2559 }
2560 return null;
2561 }
Romain Guy06882f82009-06-10 13:36:04 -07002562
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002563 private Animation loadAnimation(String packageName, int resId) {
2564 int anim = 0;
2565 Context context = mContext;
2566 if (resId >= 0) {
2567 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2568 if (ent != null) {
2569 context = ent.context;
2570 anim = resId;
2571 }
2572 }
2573 if (anim != 0) {
2574 return AnimationUtils.loadAnimation(context, anim);
2575 }
2576 return null;
2577 }
2578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 private boolean applyAnimationLocked(AppWindowToken wtoken,
2580 WindowManager.LayoutParams lp, int transit, boolean enter) {
2581 // Only apply an animation if the display isn't frozen. If it is
2582 // frozen, there is no reason to animate and it can cause strange
2583 // artifacts when we unfreeze the display if some different animation
2584 // is running.
2585 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002586 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002587 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002588 a = new FadeInOutAnimation(enter);
2589 if (DEBUG_ANIM) Log.v(TAG,
2590 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002591 } else if (mNextAppTransitionPackage != null) {
2592 a = loadAnimation(mNextAppTransitionPackage, enter ?
2593 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002594 } else {
2595 int animAttr = 0;
2596 switch (transit) {
2597 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2598 animAttr = enter
2599 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2600 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2601 break;
2602 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2603 animAttr = enter
2604 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2605 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2606 break;
2607 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2608 animAttr = enter
2609 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2610 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2611 break;
2612 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2613 animAttr = enter
2614 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2615 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2616 break;
2617 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2618 animAttr = enter
2619 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2620 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2621 break;
2622 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2623 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002624 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002625 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2626 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002627 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002628 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002629 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2630 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002631 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002632 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002633 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002634 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2635 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2636 break;
2637 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2638 animAttr = enter
2639 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2640 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2641 break;
2642 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2643 animAttr = enter
2644 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2645 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002646 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002647 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002648 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002649 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2650 + " anim=" + a
2651 + " animAttr=0x" + Integer.toHexString(animAttr)
2652 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 if (a != null) {
2655 if (DEBUG_ANIM) {
2656 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002657 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2659 }
2660 wtoken.setAnimation(a);
2661 }
2662 } else {
2663 wtoken.clearAnimation();
2664 }
2665
2666 return wtoken.animation != null;
2667 }
2668
2669 // -------------------------------------------------------------
2670 // Application Window Tokens
2671 // -------------------------------------------------------------
2672
2673 public void validateAppTokens(List tokens) {
2674 int v = tokens.size()-1;
2675 int m = mAppTokens.size()-1;
2676 while (v >= 0 && m >= 0) {
2677 AppWindowToken wtoken = mAppTokens.get(m);
2678 if (wtoken.removed) {
2679 m--;
2680 continue;
2681 }
2682 if (tokens.get(v) != wtoken.token) {
2683 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2684 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2685 }
2686 v--;
2687 m--;
2688 }
2689 while (v >= 0) {
2690 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2691 v--;
2692 }
2693 while (m >= 0) {
2694 AppWindowToken wtoken = mAppTokens.get(m);
2695 if (!wtoken.removed) {
2696 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2697 }
2698 m--;
2699 }
2700 }
2701
2702 boolean checkCallingPermission(String permission, String func) {
2703 // Quick check: if the calling permission is me, it's all okay.
2704 if (Binder.getCallingPid() == Process.myPid()) {
2705 return true;
2706 }
Romain Guy06882f82009-06-10 13:36:04 -07002707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 if (mContext.checkCallingPermission(permission)
2709 == PackageManager.PERMISSION_GRANTED) {
2710 return true;
2711 }
2712 String msg = "Permission Denial: " + func + " from pid="
2713 + Binder.getCallingPid()
2714 + ", uid=" + Binder.getCallingUid()
2715 + " requires " + permission;
2716 Log.w(TAG, msg);
2717 return false;
2718 }
Romain Guy06882f82009-06-10 13:36:04 -07002719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 AppWindowToken findAppWindowToken(IBinder token) {
2721 WindowToken wtoken = mTokenMap.get(token);
2722 if (wtoken == null) {
2723 return null;
2724 }
2725 return wtoken.appWindowToken;
2726 }
Romain Guy06882f82009-06-10 13:36:04 -07002727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 public void addWindowToken(IBinder token, int type) {
2729 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2730 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002731 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 }
Romain Guy06882f82009-06-10 13:36:04 -07002733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 synchronized(mWindowMap) {
2735 WindowToken wtoken = mTokenMap.get(token);
2736 if (wtoken != null) {
2737 Log.w(TAG, "Attempted to add existing input method token: " + token);
2738 return;
2739 }
2740 wtoken = new WindowToken(token, type, true);
2741 mTokenMap.put(token, wtoken);
2742 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002743 if (type == TYPE_WALLPAPER) {
2744 mWallpaperTokens.add(wtoken);
2745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 }
2747 }
Romain Guy06882f82009-06-10 13:36:04 -07002748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 public void removeWindowToken(IBinder token) {
2750 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2751 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002752 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
2754
2755 final long origId = Binder.clearCallingIdentity();
2756 synchronized(mWindowMap) {
2757 WindowToken wtoken = mTokenMap.remove(token);
2758 mTokenList.remove(wtoken);
2759 if (wtoken != null) {
2760 boolean delayed = false;
2761 if (!wtoken.hidden) {
2762 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 final int N = wtoken.windows.size();
2765 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 for (int i=0; i<N; i++) {
2768 WindowState win = wtoken.windows.get(i);
2769
2770 if (win.isAnimating()) {
2771 delayed = true;
2772 }
Romain Guy06882f82009-06-10 13:36:04 -07002773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 if (win.isVisibleNow()) {
2775 applyAnimationLocked(win,
2776 WindowManagerPolicy.TRANSIT_EXIT, false);
2777 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2778 KeyWaiter.RETURN_NOTHING);
2779 changed = true;
2780 }
2781 }
2782
2783 if (changed) {
2784 mLayoutNeeded = true;
2785 performLayoutAndPlaceSurfacesLocked();
2786 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2787 }
Romain Guy06882f82009-06-10 13:36:04 -07002788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 if (delayed) {
2790 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002791 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2792 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
2794 }
Romain Guy06882f82009-06-10 13:36:04 -07002795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 } else {
2797 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2798 }
2799 }
2800 Binder.restoreCallingIdentity(origId);
2801 }
2802
2803 public void addAppToken(int addPos, IApplicationToken token,
2804 int groupId, int requestedOrientation, boolean fullscreen) {
2805 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2806 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002807 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 }
Romain Guy06882f82009-06-10 13:36:04 -07002809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 synchronized(mWindowMap) {
2811 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2812 if (wtoken != null) {
2813 Log.w(TAG, "Attempted to add existing app token: " + token);
2814 return;
2815 }
2816 wtoken = new AppWindowToken(token);
2817 wtoken.groupId = groupId;
2818 wtoken.appFullscreen = fullscreen;
2819 wtoken.requestedOrientation = requestedOrientation;
2820 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002821 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 mTokenMap.put(token.asBinder(), wtoken);
2823 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 // Application tokens start out hidden.
2826 wtoken.hidden = true;
2827 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 //dump();
2830 }
2831 }
Romain Guy06882f82009-06-10 13:36:04 -07002832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 public void setAppGroupId(IBinder token, int groupId) {
2834 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2835 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002836 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 }
2838
2839 synchronized(mWindowMap) {
2840 AppWindowToken wtoken = findAppWindowToken(token);
2841 if (wtoken == null) {
2842 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2843 return;
2844 }
2845 wtoken.groupId = groupId;
2846 }
2847 }
Romain Guy06882f82009-06-10 13:36:04 -07002848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 public int getOrientationFromWindowsLocked() {
2850 int pos = mWindows.size() - 1;
2851 while (pos >= 0) {
2852 WindowState wtoken = (WindowState) mWindows.get(pos);
2853 pos--;
2854 if (wtoken.mAppToken != null) {
2855 // We hit an application window. so the orientation will be determined by the
2856 // app window. No point in continuing further.
2857 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2858 }
2859 if (!wtoken.isVisibleLw()) {
2860 continue;
2861 }
2862 int req = wtoken.mAttrs.screenOrientation;
2863 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2864 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2865 continue;
2866 } else {
2867 return req;
2868 }
2869 }
2870 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2871 }
Romain Guy06882f82009-06-10 13:36:04 -07002872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 public int getOrientationFromAppTokensLocked() {
2874 int pos = mAppTokens.size() - 1;
2875 int curGroup = 0;
2876 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002877 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002879 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 while (pos >= 0) {
2881 AppWindowToken wtoken = mAppTokens.get(pos);
2882 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002883 // if we're about to tear down this window and not seek for
2884 // the behind activity, don't use it for orientation
2885 if (!findingBehind
2886 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002887 continue;
2888 }
2889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 if (!haveGroup) {
2891 // We ignore any hidden applications on the top.
2892 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2893 continue;
2894 }
2895 haveGroup = true;
2896 curGroup = wtoken.groupId;
2897 lastOrientation = wtoken.requestedOrientation;
2898 } else if (curGroup != wtoken.groupId) {
2899 // If we have hit a new application group, and the bottom
2900 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002901 // the orientation behind it, and the last app was
2902 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002904 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2905 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 return lastOrientation;
2907 }
2908 }
2909 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002910 // If this application is fullscreen, and didn't explicitly say
2911 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002913 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002914 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002915 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 return or;
2917 }
2918 // If this application has requested an explicit orientation,
2919 // then use it.
2920 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2921 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2922 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2923 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2924 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2925 return or;
2926 }
Owen Lin3413b892009-05-01 17:12:32 -07002927 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
2929 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2930 }
Romain Guy06882f82009-06-10 13:36:04 -07002931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002933 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002934 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2935 "updateOrientationFromAppTokens()")) {
2936 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2937 }
2938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 Configuration config;
2940 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002941 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2942 freezeThisOneIfNeeded);
2943 Binder.restoreCallingIdentity(ident);
2944 return config;
2945 }
2946
2947 Configuration updateOrientationFromAppTokensUnchecked(
2948 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2949 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002951 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002952 if (config != null) {
2953 mLayoutNeeded = true;
2954 performLayoutAndPlaceSurfacesLocked();
2955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 return config;
2958 }
Romain Guy06882f82009-06-10 13:36:04 -07002959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 /*
2961 * The orientation is computed from non-application windows first. If none of
2962 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002963 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2965 * android.os.IBinder)
2966 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002967 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002968 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 long ident = Binder.clearCallingIdentity();
2971 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002972 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 if (req != mForcedAppOrientation) {
2975 changed = true;
2976 mForcedAppOrientation = req;
2977 //send a message to Policy indicating orientation change to take
2978 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002979 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 }
Romain Guy06882f82009-06-10 13:36:04 -07002981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 if (changed) {
2983 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002984 WindowManagerPolicy.USE_LAST_ROTATION,
2985 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 if (changed) {
2987 if (freezeThisOneIfNeeded != null) {
2988 AppWindowToken wtoken = findAppWindowToken(
2989 freezeThisOneIfNeeded);
2990 if (wtoken != null) {
2991 startAppFreezingScreenLocked(wtoken,
2992 ActivityInfo.CONFIG_ORIENTATION);
2993 }
2994 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002995 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 }
2997 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002998
2999 // No obvious action we need to take, but if our current
3000 // state mismatches the activity maanager's, update it
3001 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003002 mTempConfiguration.setToDefaults();
3003 if (computeNewConfigurationLocked(mTempConfiguration)) {
3004 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003005 return new Configuration(mTempConfiguration);
3006 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003007 }
3008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 } finally {
3010 Binder.restoreCallingIdentity(ident);
3011 }
Romain Guy06882f82009-06-10 13:36:04 -07003012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 return null;
3014 }
Romain Guy06882f82009-06-10 13:36:04 -07003015
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003016 int computeForcedAppOrientationLocked() {
3017 int req = getOrientationFromWindowsLocked();
3018 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3019 req = getOrientationFromAppTokensLocked();
3020 }
3021 return req;
3022 }
Romain Guy06882f82009-06-10 13:36:04 -07003023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3025 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3026 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003027 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 }
Romain Guy06882f82009-06-10 13:36:04 -07003029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 synchronized(mWindowMap) {
3031 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3032 if (wtoken == null) {
3033 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3034 return;
3035 }
Romain Guy06882f82009-06-10 13:36:04 -07003036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 wtoken.requestedOrientation = requestedOrientation;
3038 }
3039 }
Romain Guy06882f82009-06-10 13:36:04 -07003040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 public int getAppOrientation(IApplicationToken token) {
3042 synchronized(mWindowMap) {
3043 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3044 if (wtoken == null) {
3045 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3046 }
Romain Guy06882f82009-06-10 13:36:04 -07003047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 return wtoken.requestedOrientation;
3049 }
3050 }
Romain Guy06882f82009-06-10 13:36:04 -07003051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3053 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3054 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003055 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 }
3057
3058 synchronized(mWindowMap) {
3059 boolean changed = false;
3060 if (token == null) {
3061 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3062 changed = mFocusedApp != null;
3063 mFocusedApp = null;
3064 mKeyWaiter.tickle();
3065 } else {
3066 AppWindowToken newFocus = findAppWindowToken(token);
3067 if (newFocus == null) {
3068 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3069 return;
3070 }
3071 changed = mFocusedApp != newFocus;
3072 mFocusedApp = newFocus;
3073 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3074 mKeyWaiter.tickle();
3075 }
3076
3077 if (moveFocusNow && changed) {
3078 final long origId = Binder.clearCallingIdentity();
3079 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3080 Binder.restoreCallingIdentity(origId);
3081 }
3082 }
3083 }
3084
3085 public void prepareAppTransition(int transit) {
3086 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3087 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003088 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
Romain Guy06882f82009-06-10 13:36:04 -07003090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 synchronized(mWindowMap) {
3092 if (DEBUG_APP_TRANSITIONS) Log.v(
3093 TAG, "Prepare app transition: transit=" + transit
3094 + " mNextAppTransition=" + mNextAppTransition);
3095 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003096 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3097 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003099 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3100 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3101 // Opening a new task always supersedes a close for the anim.
3102 mNextAppTransition = transit;
3103 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3104 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3105 // Opening a new activity always supersedes a close for the anim.
3106 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 }
3108 mAppTransitionReady = false;
3109 mAppTransitionTimeout = false;
3110 mStartingIconInTransition = false;
3111 mSkipAppTransitionAnimation = false;
3112 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3113 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3114 5000);
3115 }
3116 }
3117 }
3118
3119 public int getPendingAppTransition() {
3120 return mNextAppTransition;
3121 }
Romain Guy06882f82009-06-10 13:36:04 -07003122
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003123 public void overridePendingAppTransition(String packageName,
3124 int enterAnim, int exitAnim) {
3125 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET){
3126 mNextAppTransitionPackage = packageName;
3127 mNextAppTransitionEnter = enterAnim;
3128 mNextAppTransitionExit = exitAnim;
3129 }
3130 }
3131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 public void executeAppTransition() {
3133 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3134 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003135 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 }
Romain Guy06882f82009-06-10 13:36:04 -07003137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003139 if (DEBUG_APP_TRANSITIONS) {
3140 RuntimeException e = new RuntimeException("here");
3141 e.fillInStackTrace();
3142 Log.w(TAG, "Execute app transition: mNextAppTransition="
3143 + mNextAppTransition, e);
3144 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003145 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 mAppTransitionReady = true;
3147 final long origId = Binder.clearCallingIdentity();
3148 performLayoutAndPlaceSurfacesLocked();
3149 Binder.restoreCallingIdentity(origId);
3150 }
3151 }
3152 }
3153
3154 public void setAppStartingWindow(IBinder token, String pkg,
3155 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3156 IBinder transferFrom, boolean createIfNeeded) {
3157 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3158 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003159 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 }
3161
3162 synchronized(mWindowMap) {
3163 if (DEBUG_STARTING_WINDOW) Log.v(
3164 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3165 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 AppWindowToken wtoken = findAppWindowToken(token);
3168 if (wtoken == null) {
3169 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3170 return;
3171 }
3172
3173 // If the display is frozen, we won't do anything until the
3174 // actual window is displayed so there is no reason to put in
3175 // the starting window.
3176 if (mDisplayFrozen) {
3177 return;
3178 }
Romain Guy06882f82009-06-10 13:36:04 -07003179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 if (wtoken.startingData != null) {
3181 return;
3182 }
Romain Guy06882f82009-06-10 13:36:04 -07003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 if (transferFrom != null) {
3185 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3186 if (ttoken != null) {
3187 WindowState startingWindow = ttoken.startingWindow;
3188 if (startingWindow != null) {
3189 if (mStartingIconInTransition) {
3190 // In this case, the starting icon has already
3191 // been displayed, so start letting windows get
3192 // shown immediately without any more transitions.
3193 mSkipAppTransitionAnimation = true;
3194 }
3195 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3196 "Moving existing starting from " + ttoken
3197 + " to " + wtoken);
3198 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 // Transfer the starting window over to the new
3201 // token.
3202 wtoken.startingData = ttoken.startingData;
3203 wtoken.startingView = ttoken.startingView;
3204 wtoken.startingWindow = startingWindow;
3205 ttoken.startingData = null;
3206 ttoken.startingView = null;
3207 ttoken.startingWindow = null;
3208 ttoken.startingMoved = true;
3209 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003210 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003212 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3213 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 mWindows.remove(startingWindow);
3215 ttoken.windows.remove(startingWindow);
3216 ttoken.allAppWindows.remove(startingWindow);
3217 addWindowToListInOrderLocked(startingWindow, true);
3218 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 // Propagate other interesting state between the
3221 // tokens. If the old token is displayed, we should
3222 // immediately force the new one to be displayed. If
3223 // it is animating, we need to move that animation to
3224 // the new one.
3225 if (ttoken.allDrawn) {
3226 wtoken.allDrawn = true;
3227 }
3228 if (ttoken.firstWindowDrawn) {
3229 wtoken.firstWindowDrawn = true;
3230 }
3231 if (!ttoken.hidden) {
3232 wtoken.hidden = false;
3233 wtoken.hiddenRequested = false;
3234 wtoken.willBeHidden = false;
3235 }
3236 if (wtoken.clientHidden != ttoken.clientHidden) {
3237 wtoken.clientHidden = ttoken.clientHidden;
3238 wtoken.sendAppVisibilityToClients();
3239 }
3240 if (ttoken.animation != null) {
3241 wtoken.animation = ttoken.animation;
3242 wtoken.animating = ttoken.animating;
3243 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3244 ttoken.animation = null;
3245 ttoken.animLayerAdjustment = 0;
3246 wtoken.updateLayers();
3247 ttoken.updateLayers();
3248 }
Romain Guy06882f82009-06-10 13:36:04 -07003249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 mLayoutNeeded = true;
3252 performLayoutAndPlaceSurfacesLocked();
3253 Binder.restoreCallingIdentity(origId);
3254 return;
3255 } else if (ttoken.startingData != null) {
3256 // The previous app was getting ready to show a
3257 // starting window, but hasn't yet done so. Steal it!
3258 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3259 "Moving pending starting from " + ttoken
3260 + " to " + wtoken);
3261 wtoken.startingData = ttoken.startingData;
3262 ttoken.startingData = null;
3263 ttoken.startingMoved = true;
3264 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3265 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3266 // want to process the message ASAP, before any other queued
3267 // messages.
3268 mH.sendMessageAtFrontOfQueue(m);
3269 return;
3270 }
3271 }
3272 }
3273
3274 // There is no existing starting window, and the caller doesn't
3275 // want us to create one, so that's it!
3276 if (!createIfNeeded) {
3277 return;
3278 }
Romain Guy06882f82009-06-10 13:36:04 -07003279
Dianne Hackborn284ac932009-08-28 10:34:25 -07003280 // If this is a translucent or wallpaper window, then don't
3281 // show a starting window -- the current effect (a full-screen
3282 // opaque starting window that fades away to the real contents
3283 // when it is ready) does not work for this.
3284 if (theme != 0) {
3285 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3286 com.android.internal.R.styleable.Window);
3287 if (ent.array.getBoolean(
3288 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3289 return;
3290 }
3291 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003292 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3293 return;
3294 }
3295 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003296 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3297 return;
3298 }
3299 }
3300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 mStartingIconInTransition = true;
3302 wtoken.startingData = new StartingData(
3303 pkg, theme, nonLocalizedLabel,
3304 labelRes, icon);
3305 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3306 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3307 // want to process the message ASAP, before any other queued
3308 // messages.
3309 mH.sendMessageAtFrontOfQueue(m);
3310 }
3311 }
3312
3313 public void setAppWillBeHidden(IBinder token) {
3314 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3315 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003316 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 }
3318
3319 AppWindowToken wtoken;
3320
3321 synchronized(mWindowMap) {
3322 wtoken = findAppWindowToken(token);
3323 if (wtoken == null) {
3324 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3325 return;
3326 }
3327 wtoken.willBeHidden = true;
3328 }
3329 }
Romain Guy06882f82009-06-10 13:36:04 -07003330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3332 boolean visible, int transit, boolean performLayout) {
3333 boolean delayed = false;
3334
3335 if (wtoken.clientHidden == visible) {
3336 wtoken.clientHidden = !visible;
3337 wtoken.sendAppVisibilityToClients();
3338 }
Romain Guy06882f82009-06-10 13:36:04 -07003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 wtoken.willBeHidden = false;
3341 if (wtoken.hidden == visible) {
3342 final int N = wtoken.allAppWindows.size();
3343 boolean changed = false;
3344 if (DEBUG_APP_TRANSITIONS) Log.v(
3345 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3346 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003349
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003350 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 if (wtoken.animation == sDummyAnimation) {
3352 wtoken.animation = null;
3353 }
3354 applyAnimationLocked(wtoken, lp, transit, visible);
3355 changed = true;
3356 if (wtoken.animation != null) {
3357 delayed = runningAppAnimation = true;
3358 }
3359 }
Romain Guy06882f82009-06-10 13:36:04 -07003360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 for (int i=0; i<N; i++) {
3362 WindowState win = wtoken.allAppWindows.get(i);
3363 if (win == wtoken.startingWindow) {
3364 continue;
3365 }
3366
3367 if (win.isAnimating()) {
3368 delayed = true;
3369 }
Romain Guy06882f82009-06-10 13:36:04 -07003370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3372 //win.dump(" ");
3373 if (visible) {
3374 if (!win.isVisibleNow()) {
3375 if (!runningAppAnimation) {
3376 applyAnimationLocked(win,
3377 WindowManagerPolicy.TRANSIT_ENTER, true);
3378 }
3379 changed = true;
3380 }
3381 } else if (win.isVisibleNow()) {
3382 if (!runningAppAnimation) {
3383 applyAnimationLocked(win,
3384 WindowManagerPolicy.TRANSIT_EXIT, false);
3385 }
3386 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3387 KeyWaiter.RETURN_NOTHING);
3388 changed = true;
3389 }
3390 }
3391
3392 wtoken.hidden = wtoken.hiddenRequested = !visible;
3393 if (!visible) {
3394 unsetAppFreezingScreenLocked(wtoken, true, true);
3395 } else {
3396 // If we are being set visible, and the starting window is
3397 // not yet displayed, then make sure it doesn't get displayed.
3398 WindowState swin = wtoken.startingWindow;
3399 if (swin != null && (swin.mDrawPending
3400 || swin.mCommitDrawPending)) {
3401 swin.mPolicyVisibility = false;
3402 swin.mPolicyVisibilityAfterAnim = false;
3403 }
3404 }
Romain Guy06882f82009-06-10 13:36:04 -07003405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3407 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3408 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 if (changed && performLayout) {
3411 mLayoutNeeded = true;
3412 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 performLayoutAndPlaceSurfacesLocked();
3414 }
3415 }
3416
3417 if (wtoken.animation != null) {
3418 delayed = true;
3419 }
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 return delayed;
3422 }
3423
3424 public void setAppVisibility(IBinder token, boolean visible) {
3425 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3426 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003427 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 }
3429
3430 AppWindowToken wtoken;
3431
3432 synchronized(mWindowMap) {
3433 wtoken = findAppWindowToken(token);
3434 if (wtoken == null) {
3435 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3436 return;
3437 }
3438
3439 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3440 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003441 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3443 + "): mNextAppTransition=" + mNextAppTransition
3444 + " hidden=" + wtoken.hidden
3445 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3446 }
Romain Guy06882f82009-06-10 13:36:04 -07003447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 // If we are preparing an app transition, then delay changing
3449 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003450 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 // Already in requested state, don't do anything more.
3452 if (wtoken.hiddenRequested != visible) {
3453 return;
3454 }
3455 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 if (DEBUG_APP_TRANSITIONS) Log.v(
3458 TAG, "Setting dummy animation on: " + wtoken);
3459 wtoken.setDummyAnimation();
3460 mOpeningApps.remove(wtoken);
3461 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003462 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 wtoken.inPendingTransaction = true;
3464 if (visible) {
3465 mOpeningApps.add(wtoken);
3466 wtoken.allDrawn = false;
3467 wtoken.startingDisplayed = false;
3468 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003469 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 if (wtoken.clientHidden) {
3472 // In the case where we are making an app visible
3473 // but holding off for a transition, we still need
3474 // to tell the client to make its windows visible so
3475 // they get drawn. Otherwise, we will wait on
3476 // performing the transition until all windows have
3477 // been drawn, they never will be, and we are sad.
3478 wtoken.clientHidden = false;
3479 wtoken.sendAppVisibilityToClients();
3480 }
3481 } else {
3482 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003483 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 }
3485 return;
3486 }
Romain Guy06882f82009-06-10 13:36:04 -07003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003489 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 wtoken.updateReportedVisibilityLocked();
3491 Binder.restoreCallingIdentity(origId);
3492 }
3493 }
3494
3495 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3496 boolean unfreezeSurfaceNow, boolean force) {
3497 if (wtoken.freezingScreen) {
3498 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3499 + " force=" + force);
3500 final int N = wtoken.allAppWindows.size();
3501 boolean unfrozeWindows = false;
3502 for (int i=0; i<N; i++) {
3503 WindowState w = wtoken.allAppWindows.get(i);
3504 if (w.mAppFreezing) {
3505 w.mAppFreezing = false;
3506 if (w.mSurface != null && !w.mOrientationChanging) {
3507 w.mOrientationChanging = true;
3508 }
3509 unfrozeWindows = true;
3510 }
3511 }
3512 if (force || unfrozeWindows) {
3513 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3514 wtoken.freezingScreen = false;
3515 mAppsFreezingScreen--;
3516 }
3517 if (unfreezeSurfaceNow) {
3518 if (unfrozeWindows) {
3519 mLayoutNeeded = true;
3520 performLayoutAndPlaceSurfacesLocked();
3521 }
3522 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3523 stopFreezingDisplayLocked();
3524 }
3525 }
3526 }
3527 }
Romain Guy06882f82009-06-10 13:36:04 -07003528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3530 int configChanges) {
3531 if (DEBUG_ORIENTATION) {
3532 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003533 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 Log.i(TAG, "Set freezing of " + wtoken.appToken
3535 + ": hidden=" + wtoken.hidden + " freezing="
3536 + wtoken.freezingScreen, e);
3537 }
3538 if (!wtoken.hiddenRequested) {
3539 if (!wtoken.freezingScreen) {
3540 wtoken.freezingScreen = true;
3541 mAppsFreezingScreen++;
3542 if (mAppsFreezingScreen == 1) {
3543 startFreezingDisplayLocked();
3544 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3545 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3546 5000);
3547 }
3548 }
3549 final int N = wtoken.allAppWindows.size();
3550 for (int i=0; i<N; i++) {
3551 WindowState w = wtoken.allAppWindows.get(i);
3552 w.mAppFreezing = true;
3553 }
3554 }
3555 }
Romain Guy06882f82009-06-10 13:36:04 -07003556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 public void startAppFreezingScreen(IBinder token, int configChanges) {
3558 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3559 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003560 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 }
3562
3563 synchronized(mWindowMap) {
3564 if (configChanges == 0 && !mDisplayFrozen) {
3565 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3566 return;
3567 }
Romain Guy06882f82009-06-10 13:36:04 -07003568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 AppWindowToken wtoken = findAppWindowToken(token);
3570 if (wtoken == null || wtoken.appToken == null) {
3571 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3572 return;
3573 }
3574 final long origId = Binder.clearCallingIdentity();
3575 startAppFreezingScreenLocked(wtoken, configChanges);
3576 Binder.restoreCallingIdentity(origId);
3577 }
3578 }
Romain Guy06882f82009-06-10 13:36:04 -07003579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 public void stopAppFreezingScreen(IBinder token, boolean force) {
3581 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3582 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003583 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 }
3585
3586 synchronized(mWindowMap) {
3587 AppWindowToken wtoken = findAppWindowToken(token);
3588 if (wtoken == null || wtoken.appToken == null) {
3589 return;
3590 }
3591 final long origId = Binder.clearCallingIdentity();
3592 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3593 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3594 unsetAppFreezingScreenLocked(wtoken, true, force);
3595 Binder.restoreCallingIdentity(origId);
3596 }
3597 }
Romain Guy06882f82009-06-10 13:36:04 -07003598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 public void removeAppToken(IBinder token) {
3600 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3601 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003602 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 }
3604
3605 AppWindowToken wtoken = null;
3606 AppWindowToken startingToken = null;
3607 boolean delayed = false;
3608
3609 final long origId = Binder.clearCallingIdentity();
3610 synchronized(mWindowMap) {
3611 WindowToken basewtoken = mTokenMap.remove(token);
3612 mTokenList.remove(basewtoken);
3613 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3614 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003615 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 wtoken.inPendingTransaction = false;
3617 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003618 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 if (mClosingApps.contains(wtoken)) {
3620 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003621 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003623 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 delayed = true;
3625 }
3626 if (DEBUG_APP_TRANSITIONS) Log.v(
3627 TAG, "Removing app " + wtoken + " delayed=" + delayed
3628 + " animation=" + wtoken.animation
3629 + " animating=" + wtoken.animating);
3630 if (delayed) {
3631 // set the token aside because it has an active animation to be finished
3632 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003633 } else {
3634 // Make sure there is no animation running on this token,
3635 // so any windows associated with it will be removed as
3636 // soon as their animations are complete
3637 wtoken.animation = null;
3638 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
3640 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003641 if (mLastEnterAnimToken == wtoken) {
3642 mLastEnterAnimToken = null;
3643 mLastEnterAnimParams = null;
3644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 wtoken.removed = true;
3646 if (wtoken.startingData != null) {
3647 startingToken = wtoken;
3648 }
3649 unsetAppFreezingScreenLocked(wtoken, true, true);
3650 if (mFocusedApp == wtoken) {
3651 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3652 mFocusedApp = null;
3653 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3654 mKeyWaiter.tickle();
3655 }
3656 } else {
3657 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3658 }
Romain Guy06882f82009-06-10 13:36:04 -07003659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 if (!delayed && wtoken != null) {
3661 wtoken.updateReportedVisibilityLocked();
3662 }
3663 }
3664 Binder.restoreCallingIdentity(origId);
3665
3666 if (startingToken != null) {
3667 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3668 + startingToken + ": app token removed");
3669 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3670 mH.sendMessage(m);
3671 }
3672 }
3673
3674 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3675 final int NW = token.windows.size();
3676 for (int i=0; i<NW; i++) {
3677 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003678 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 mWindows.remove(win);
3680 int j = win.mChildWindows.size();
3681 while (j > 0) {
3682 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003683 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3684 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3685 "Tmp removing child window " + cwin);
3686 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 }
3688 }
3689 return NW > 0;
3690 }
3691
3692 void dumpAppTokensLocked() {
3693 for (int i=mAppTokens.size()-1; i>=0; i--) {
3694 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3695 }
3696 }
Romain Guy06882f82009-06-10 13:36:04 -07003697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 void dumpWindowsLocked() {
3699 for (int i=mWindows.size()-1; i>=0; i--) {
3700 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3701 }
3702 }
Romain Guy06882f82009-06-10 13:36:04 -07003703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 private int findWindowOffsetLocked(int tokenPos) {
3705 final int NW = mWindows.size();
3706
3707 if (tokenPos >= mAppTokens.size()) {
3708 int i = NW;
3709 while (i > 0) {
3710 i--;
3711 WindowState win = (WindowState)mWindows.get(i);
3712 if (win.getAppToken() != null) {
3713 return i+1;
3714 }
3715 }
3716 }
3717
3718 while (tokenPos > 0) {
3719 // Find the first app token below the new position that has
3720 // a window displayed.
3721 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3722 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3723 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003724 if (wtoken.sendingToBottom) {
3725 if (DEBUG_REORDER) Log.v(TAG,
3726 "Skipping token -- currently sending to bottom");
3727 tokenPos--;
3728 continue;
3729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 int i = wtoken.windows.size();
3731 while (i > 0) {
3732 i--;
3733 WindowState win = wtoken.windows.get(i);
3734 int j = win.mChildWindows.size();
3735 while (j > 0) {
3736 j--;
3737 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003738 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 for (int pos=NW-1; pos>=0; pos--) {
3740 if (mWindows.get(pos) == cwin) {
3741 if (DEBUG_REORDER) Log.v(TAG,
3742 "Found child win @" + (pos+1));
3743 return pos+1;
3744 }
3745 }
3746 }
3747 }
3748 for (int pos=NW-1; pos>=0; pos--) {
3749 if (mWindows.get(pos) == win) {
3750 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3751 return pos+1;
3752 }
3753 }
3754 }
3755 tokenPos--;
3756 }
3757
3758 return 0;
3759 }
3760
3761 private final int reAddWindowLocked(int index, WindowState win) {
3762 final int NCW = win.mChildWindows.size();
3763 boolean added = false;
3764 for (int j=0; j<NCW; j++) {
3765 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3766 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003767 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3768 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 mWindows.add(index, win);
3770 index++;
3771 added = true;
3772 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003773 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3774 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 mWindows.add(index, cwin);
3776 index++;
3777 }
3778 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003779 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3780 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 mWindows.add(index, win);
3782 index++;
3783 }
3784 return index;
3785 }
Romain Guy06882f82009-06-10 13:36:04 -07003786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3788 final int NW = token.windows.size();
3789 for (int i=0; i<NW; i++) {
3790 index = reAddWindowLocked(index, token.windows.get(i));
3791 }
3792 return index;
3793 }
3794
3795 public void moveAppToken(int index, IBinder token) {
3796 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3797 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003798 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 }
3800
3801 synchronized(mWindowMap) {
3802 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3803 if (DEBUG_REORDER) dumpAppTokensLocked();
3804 final AppWindowToken wtoken = findAppWindowToken(token);
3805 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3806 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3807 + token + " (" + wtoken + ")");
3808 return;
3809 }
3810 mAppTokens.add(index, wtoken);
3811 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3812 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 final long origId = Binder.clearCallingIdentity();
3815 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3816 if (DEBUG_REORDER) dumpWindowsLocked();
3817 if (tmpRemoveAppWindowsLocked(wtoken)) {
3818 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3819 if (DEBUG_REORDER) dumpWindowsLocked();
3820 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3821 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3822 if (DEBUG_REORDER) dumpWindowsLocked();
3823 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 mLayoutNeeded = true;
3825 performLayoutAndPlaceSurfacesLocked();
3826 }
3827 Binder.restoreCallingIdentity(origId);
3828 }
3829 }
3830
3831 private void removeAppTokensLocked(List<IBinder> tokens) {
3832 // XXX This should be done more efficiently!
3833 // (take advantage of the fact that both lists should be
3834 // ordered in the same way.)
3835 int N = tokens.size();
3836 for (int i=0; i<N; i++) {
3837 IBinder token = tokens.get(i);
3838 final AppWindowToken wtoken = findAppWindowToken(token);
3839 if (!mAppTokens.remove(wtoken)) {
3840 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3841 + token + " (" + wtoken + ")");
3842 i--;
3843 N--;
3844 }
3845 }
3846 }
3847
Dianne Hackborna8f60182009-09-01 19:01:50 -07003848 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3849 boolean updateFocusAndLayout) {
3850 // First remove all of the windows from the list.
3851 tmpRemoveAppWindowsLocked(wtoken);
3852
3853 // Where to start adding?
3854 int pos = findWindowOffsetLocked(tokenPos);
3855
3856 // And now add them back at the correct place.
3857 pos = reAddAppWindowsLocked(pos, wtoken);
3858
3859 if (updateFocusAndLayout) {
3860 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3861 assignLayersLocked();
3862 }
3863 mLayoutNeeded = true;
3864 performLayoutAndPlaceSurfacesLocked();
3865 }
3866 }
3867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3869 // First remove all of the windows from the list.
3870 final int N = tokens.size();
3871 int i;
3872 for (i=0; i<N; i++) {
3873 WindowToken token = mTokenMap.get(tokens.get(i));
3874 if (token != null) {
3875 tmpRemoveAppWindowsLocked(token);
3876 }
3877 }
3878
3879 // Where to start adding?
3880 int pos = findWindowOffsetLocked(tokenPos);
3881
3882 // And now add them back at the correct place.
3883 for (i=0; i<N; i++) {
3884 WindowToken token = mTokenMap.get(tokens.get(i));
3885 if (token != null) {
3886 pos = reAddAppWindowsLocked(pos, token);
3887 }
3888 }
3889
Dianne Hackborna8f60182009-09-01 19:01:50 -07003890 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3891 assignLayersLocked();
3892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 mLayoutNeeded = true;
3894 performLayoutAndPlaceSurfacesLocked();
3895
3896 //dump();
3897 }
3898
3899 public void moveAppTokensToTop(List<IBinder> tokens) {
3900 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3901 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003902 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 }
3904
3905 final long origId = Binder.clearCallingIdentity();
3906 synchronized(mWindowMap) {
3907 removeAppTokensLocked(tokens);
3908 final int N = tokens.size();
3909 for (int i=0; i<N; i++) {
3910 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3911 if (wt != null) {
3912 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003913 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003914 mToTopApps.remove(wt);
3915 mToBottomApps.remove(wt);
3916 mToTopApps.add(wt);
3917 wt.sendingToBottom = false;
3918 wt.sendingToTop = true;
3919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 }
3921 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003922
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003923 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003924 moveAppWindowsLocked(tokens, mAppTokens.size());
3925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 }
3927 Binder.restoreCallingIdentity(origId);
3928 }
3929
3930 public void moveAppTokensToBottom(List<IBinder> tokens) {
3931 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3932 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003933 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 }
3935
3936 final long origId = Binder.clearCallingIdentity();
3937 synchronized(mWindowMap) {
3938 removeAppTokensLocked(tokens);
3939 final int N = tokens.size();
3940 int pos = 0;
3941 for (int i=0; i<N; i++) {
3942 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3943 if (wt != null) {
3944 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003945 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003946 mToTopApps.remove(wt);
3947 mToBottomApps.remove(wt);
3948 mToBottomApps.add(i, wt);
3949 wt.sendingToTop = false;
3950 wt.sendingToBottom = true;
3951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 pos++;
3953 }
3954 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003955
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003956 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003957 moveAppWindowsLocked(tokens, 0);
3958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
3960 Binder.restoreCallingIdentity(origId);
3961 }
3962
3963 // -------------------------------------------------------------
3964 // Misc IWindowSession methods
3965 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 public void disableKeyguard(IBinder token, String tag) {
3968 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3969 != PackageManager.PERMISSION_GRANTED) {
3970 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3971 }
3972 mKeyguardDisabled.acquire(token, tag);
3973 }
3974
3975 public void reenableKeyguard(IBinder token) {
3976 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3977 != PackageManager.PERMISSION_GRANTED) {
3978 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3979 }
3980 synchronized (mKeyguardDisabled) {
3981 mKeyguardDisabled.release(token);
3982
3983 if (!mKeyguardDisabled.isAcquired()) {
3984 // if we are the last one to reenable the keyguard wait until
3985 // we have actaully finished reenabling until returning
3986 mWaitingUntilKeyguardReenabled = true;
3987 while (mWaitingUntilKeyguardReenabled) {
3988 try {
3989 mKeyguardDisabled.wait();
3990 } catch (InterruptedException e) {
3991 Thread.currentThread().interrupt();
3992 }
3993 }
3994 }
3995 }
3996 }
3997
3998 /**
3999 * @see android.app.KeyguardManager#exitKeyguardSecurely
4000 */
4001 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
4002 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
4003 != PackageManager.PERMISSION_GRANTED) {
4004 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4005 }
4006 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4007 public void onKeyguardExitResult(boolean success) {
4008 try {
4009 callback.onKeyguardExitResult(success);
4010 } catch (RemoteException e) {
4011 // Client has died, we don't care.
4012 }
4013 }
4014 });
4015 }
4016
4017 public boolean inKeyguardRestrictedInputMode() {
4018 return mPolicy.inKeyguardRestrictedKeyInputMode();
4019 }
Romain Guy06882f82009-06-10 13:36:04 -07004020
Dianne Hackbornffa42482009-09-23 22:20:11 -07004021 public void closeSystemDialogs(String reason) {
4022 synchronized(mWindowMap) {
4023 for (int i=mWindows.size()-1; i>=0; i--) {
4024 WindowState w = (WindowState)mWindows.get(i);
4025 if (w.mSurface != null) {
4026 try {
4027 w.mClient.closeSystemDialogs(reason);
4028 } catch (RemoteException e) {
4029 }
4030 }
4031 }
4032 }
4033 }
4034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 static float fixScale(float scale) {
4036 if (scale < 0) scale = 0;
4037 else if (scale > 20) scale = 20;
4038 return Math.abs(scale);
4039 }
Romain Guy06882f82009-06-10 13:36:04 -07004040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 public void setAnimationScale(int which, float scale) {
4042 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4043 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004044 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 }
4046
4047 if (scale < 0) scale = 0;
4048 else if (scale > 20) scale = 20;
4049 scale = Math.abs(scale);
4050 switch (which) {
4051 case 0: mWindowAnimationScale = fixScale(scale); break;
4052 case 1: mTransitionAnimationScale = fixScale(scale); break;
4053 }
Romain Guy06882f82009-06-10 13:36:04 -07004054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 // Persist setting
4056 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4057 }
Romain Guy06882f82009-06-10 13:36:04 -07004058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 public void setAnimationScales(float[] scales) {
4060 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4061 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004062 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064
4065 if (scales != null) {
4066 if (scales.length >= 1) {
4067 mWindowAnimationScale = fixScale(scales[0]);
4068 }
4069 if (scales.length >= 2) {
4070 mTransitionAnimationScale = fixScale(scales[1]);
4071 }
4072 }
Romain Guy06882f82009-06-10 13:36:04 -07004073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 // Persist setting
4075 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4076 }
Romain Guy06882f82009-06-10 13:36:04 -07004077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 public float getAnimationScale(int which) {
4079 switch (which) {
4080 case 0: return mWindowAnimationScale;
4081 case 1: return mTransitionAnimationScale;
4082 }
4083 return 0;
4084 }
Romain Guy06882f82009-06-10 13:36:04 -07004085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 public float[] getAnimationScales() {
4087 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4088 }
Romain Guy06882f82009-06-10 13:36:04 -07004089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 public int getSwitchState(int sw) {
4091 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4092 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004093 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
4095 return KeyInputQueue.getSwitchState(sw);
4096 }
Romain Guy06882f82009-06-10 13:36:04 -07004097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 public int getSwitchStateForDevice(int devid, int sw) {
4099 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4100 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004101 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 }
4103 return KeyInputQueue.getSwitchState(devid, sw);
4104 }
Romain Guy06882f82009-06-10 13:36:04 -07004105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 public int getScancodeState(int sw) {
4107 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4108 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004109 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 }
4111 return KeyInputQueue.getScancodeState(sw);
4112 }
Romain Guy06882f82009-06-10 13:36:04 -07004113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 public int getScancodeStateForDevice(int devid, int sw) {
4115 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4116 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004117 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 }
4119 return KeyInputQueue.getScancodeState(devid, sw);
4120 }
Romain Guy06882f82009-06-10 13:36:04 -07004121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 public int getKeycodeState(int sw) {
4123 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4124 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004125 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
4127 return KeyInputQueue.getKeycodeState(sw);
4128 }
Romain Guy06882f82009-06-10 13:36:04 -07004129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 public int getKeycodeStateForDevice(int devid, int sw) {
4131 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4132 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004133 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 }
4135 return KeyInputQueue.getKeycodeState(devid, sw);
4136 }
Romain Guy06882f82009-06-10 13:36:04 -07004137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4139 return KeyInputQueue.hasKeys(keycodes, keyExists);
4140 }
Romain Guy06882f82009-06-10 13:36:04 -07004141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 public void enableScreenAfterBoot() {
4143 synchronized(mWindowMap) {
4144 if (mSystemBooted) {
4145 return;
4146 }
4147 mSystemBooted = true;
4148 }
Romain Guy06882f82009-06-10 13:36:04 -07004149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 performEnableScreen();
4151 }
Romain Guy06882f82009-06-10 13:36:04 -07004152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 public void enableScreenIfNeededLocked() {
4154 if (mDisplayEnabled) {
4155 return;
4156 }
4157 if (!mSystemBooted) {
4158 return;
4159 }
4160 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4161 }
Romain Guy06882f82009-06-10 13:36:04 -07004162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 public void performEnableScreen() {
4164 synchronized(mWindowMap) {
4165 if (mDisplayEnabled) {
4166 return;
4167 }
4168 if (!mSystemBooted) {
4169 return;
4170 }
Romain Guy06882f82009-06-10 13:36:04 -07004171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 // Don't enable the screen until all existing windows
4173 // have been drawn.
4174 final int N = mWindows.size();
4175 for (int i=0; i<N; i++) {
4176 WindowState w = (WindowState)mWindows.get(i);
4177 if (w.isVisibleLw() && !w.isDisplayedLw()) {
4178 return;
4179 }
4180 }
Romain Guy06882f82009-06-10 13:36:04 -07004181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 mDisplayEnabled = true;
4183 if (false) {
4184 Log.i(TAG, "ENABLING SCREEN!");
4185 StringWriter sw = new StringWriter();
4186 PrintWriter pw = new PrintWriter(sw);
4187 this.dump(null, pw, null);
4188 Log.i(TAG, sw.toString());
4189 }
4190 try {
4191 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4192 if (surfaceFlinger != null) {
4193 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4194 Parcel data = Parcel.obtain();
4195 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4196 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4197 data, null, 0);
4198 data.recycle();
4199 }
4200 } catch (RemoteException ex) {
4201 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4202 }
4203 }
Romain Guy06882f82009-06-10 13:36:04 -07004204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004208 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4209 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 }
Romain Guy06882f82009-06-10 13:36:04 -07004211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 public void setInTouchMode(boolean mode) {
4213 synchronized(mWindowMap) {
4214 mInTouchMode = mode;
4215 }
4216 }
4217
Romain Guy06882f82009-06-10 13:36:04 -07004218 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004219 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004221 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004222 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
4224
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004225 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 }
Romain Guy06882f82009-06-10 13:36:04 -07004227
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004228 public void setRotationUnchecked(int rotation,
4229 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 if(DEBUG_ORIENTATION) Log.v(TAG,
4231 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 long origId = Binder.clearCallingIdentity();
4234 boolean changed;
4235 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004236 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 }
Romain Guy06882f82009-06-10 13:36:04 -07004238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 if (changed) {
4240 sendNewConfiguration();
4241 synchronized(mWindowMap) {
4242 mLayoutNeeded = true;
4243 performLayoutAndPlaceSurfacesLocked();
4244 }
4245 } else if (alwaysSendConfiguration) {
4246 //update configuration ignoring orientation change
4247 sendNewConfiguration();
4248 }
Romain Guy06882f82009-06-10 13:36:04 -07004249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 Binder.restoreCallingIdentity(origId);
4251 }
Romain Guy06882f82009-06-10 13:36:04 -07004252
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004253 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 boolean changed;
4255 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4256 rotation = mRequestedRotation;
4257 } else {
4258 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004259 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
4261 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004262 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 mRotation, mDisplayEnabled);
4264 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4265 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004268 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 "Rotation changed to " + rotation
4270 + " from " + mRotation
4271 + " (forceApp=" + mForcedAppOrientation
4272 + ", req=" + mRequestedRotation + ")");
4273 mRotation = rotation;
4274 mWindowsFreezingScreen = true;
4275 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4276 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4277 2000);
4278 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004279 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 mQueue.setOrientation(rotation);
4281 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004282 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 }
4284 for (int i=mWindows.size()-1; i>=0; i--) {
4285 WindowState w = (WindowState)mWindows.get(i);
4286 if (w.mSurface != null) {
4287 w.mOrientationChanging = true;
4288 }
4289 }
4290 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4291 try {
4292 mRotationWatchers.get(i).onRotationChanged(rotation);
4293 } catch (RemoteException e) {
4294 }
4295 }
4296 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 return changed;
4299 }
Romain Guy06882f82009-06-10 13:36:04 -07004300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 public int getRotation() {
4302 return mRotation;
4303 }
4304
4305 public int watchRotation(IRotationWatcher watcher) {
4306 final IBinder watcherBinder = watcher.asBinder();
4307 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4308 public void binderDied() {
4309 synchronized (mWindowMap) {
4310 for (int i=0; i<mRotationWatchers.size(); i++) {
4311 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004312 IRotationWatcher removed = mRotationWatchers.remove(i);
4313 if (removed != null) {
4314 removed.asBinder().unlinkToDeath(this, 0);
4315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 i--;
4317 }
4318 }
4319 }
4320 }
4321 };
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 synchronized (mWindowMap) {
4324 try {
4325 watcher.asBinder().linkToDeath(dr, 0);
4326 mRotationWatchers.add(watcher);
4327 } catch (RemoteException e) {
4328 // Client died, no cleanup needed.
4329 }
Romain Guy06882f82009-06-10 13:36:04 -07004330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 return mRotation;
4332 }
4333 }
4334
4335 /**
4336 * Starts the view server on the specified port.
4337 *
4338 * @param port The port to listener to.
4339 *
4340 * @return True if the server was successfully started, false otherwise.
4341 *
4342 * @see com.android.server.ViewServer
4343 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4344 */
4345 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004346 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 return false;
4348 }
4349
4350 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4351 return false;
4352 }
4353
4354 if (port < 1024) {
4355 return false;
4356 }
4357
4358 if (mViewServer != null) {
4359 if (!mViewServer.isRunning()) {
4360 try {
4361 return mViewServer.start();
4362 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004363 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365 }
4366 return false;
4367 }
4368
4369 try {
4370 mViewServer = new ViewServer(this, port);
4371 return mViewServer.start();
4372 } catch (IOException e) {
4373 Log.w(TAG, "View server did not start");
4374 }
4375 return false;
4376 }
4377
Romain Guy06882f82009-06-10 13:36:04 -07004378 private boolean isSystemSecure() {
4379 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4380 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4381 }
4382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 /**
4384 * Stops the view server if it exists.
4385 *
4386 * @return True if the server stopped, false if it wasn't started or
4387 * couldn't be stopped.
4388 *
4389 * @see com.android.server.ViewServer
4390 */
4391 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004392 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 return false;
4394 }
4395
4396 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4397 return false;
4398 }
4399
4400 if (mViewServer != null) {
4401 return mViewServer.stop();
4402 }
4403 return false;
4404 }
4405
4406 /**
4407 * Indicates whether the view server is running.
4408 *
4409 * @return True if the server is running, false otherwise.
4410 *
4411 * @see com.android.server.ViewServer
4412 */
4413 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004414 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 return false;
4416 }
4417
4418 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4419 return false;
4420 }
4421
4422 return mViewServer != null && mViewServer.isRunning();
4423 }
4424
4425 /**
4426 * Lists all availble windows in the system. The listing is written in the
4427 * specified Socket's output stream with the following syntax:
4428 * windowHashCodeInHexadecimal windowName
4429 * Each line of the ouput represents a different window.
4430 *
4431 * @param client The remote client to send the listing to.
4432 * @return False if an error occured, true otherwise.
4433 */
4434 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004435 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 return false;
4437 }
4438
4439 boolean result = true;
4440
4441 Object[] windows;
4442 synchronized (mWindowMap) {
4443 windows = new Object[mWindows.size()];
4444 //noinspection unchecked
4445 windows = mWindows.toArray(windows);
4446 }
4447
4448 BufferedWriter out = null;
4449
4450 // Any uncaught exception will crash the system process
4451 try {
4452 OutputStream clientStream = client.getOutputStream();
4453 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4454
4455 final int count = windows.length;
4456 for (int i = 0; i < count; i++) {
4457 final WindowState w = (WindowState) windows[i];
4458 out.write(Integer.toHexString(System.identityHashCode(w)));
4459 out.write(' ');
4460 out.append(w.mAttrs.getTitle());
4461 out.write('\n');
4462 }
4463
4464 out.write("DONE.\n");
4465 out.flush();
4466 } catch (Exception e) {
4467 result = false;
4468 } finally {
4469 if (out != null) {
4470 try {
4471 out.close();
4472 } catch (IOException e) {
4473 result = false;
4474 }
4475 }
4476 }
4477
4478 return result;
4479 }
4480
4481 /**
4482 * Sends a command to a target window. The result of the command, if any, will be
4483 * written in the output stream of the specified socket.
4484 *
4485 * The parameters must follow this syntax:
4486 * windowHashcode extra
4487 *
4488 * Where XX is the length in characeters of the windowTitle.
4489 *
4490 * The first parameter is the target window. The window with the specified hashcode
4491 * will be the target. If no target can be found, nothing happens. The extra parameters
4492 * will be delivered to the target window and as parameters to the command itself.
4493 *
4494 * @param client The remote client to sent the result, if any, to.
4495 * @param command The command to execute.
4496 * @param parameters The command parameters.
4497 *
4498 * @return True if the command was successfully delivered, false otherwise. This does
4499 * not indicate whether the command itself was successful.
4500 */
4501 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004502 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 return false;
4504 }
4505
4506 boolean success = true;
4507 Parcel data = null;
4508 Parcel reply = null;
4509
4510 // Any uncaught exception will crash the system process
4511 try {
4512 // Find the hashcode of the window
4513 int index = parameters.indexOf(' ');
4514 if (index == -1) {
4515 index = parameters.length();
4516 }
4517 final String code = parameters.substring(0, index);
4518 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4519
4520 // Extract the command's parameter after the window description
4521 if (index < parameters.length()) {
4522 parameters = parameters.substring(index + 1);
4523 } else {
4524 parameters = "";
4525 }
4526
4527 final WindowManagerService.WindowState window = findWindow(hashCode);
4528 if (window == null) {
4529 return false;
4530 }
4531
4532 data = Parcel.obtain();
4533 data.writeInterfaceToken("android.view.IWindow");
4534 data.writeString(command);
4535 data.writeString(parameters);
4536 data.writeInt(1);
4537 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4538
4539 reply = Parcel.obtain();
4540
4541 final IBinder binder = window.mClient.asBinder();
4542 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4543 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4544
4545 reply.readException();
4546
4547 } catch (Exception e) {
4548 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4549 success = false;
4550 } finally {
4551 if (data != null) {
4552 data.recycle();
4553 }
4554 if (reply != null) {
4555 reply.recycle();
4556 }
4557 }
4558
4559 return success;
4560 }
4561
4562 private WindowState findWindow(int hashCode) {
4563 if (hashCode == -1) {
4564 return getFocusedWindow();
4565 }
4566
4567 synchronized (mWindowMap) {
4568 final ArrayList windows = mWindows;
4569 final int count = windows.size();
4570
4571 for (int i = 0; i < count; i++) {
4572 WindowState w = (WindowState) windows.get(i);
4573 if (System.identityHashCode(w) == hashCode) {
4574 return w;
4575 }
4576 }
4577 }
4578
4579 return null;
4580 }
4581
4582 /*
4583 * Instruct the Activity Manager to fetch the current configuration and broadcast
4584 * that to config-changed listeners if appropriate.
4585 */
4586 void sendNewConfiguration() {
4587 try {
4588 mActivityManager.updateConfiguration(null);
4589 } catch (RemoteException e) {
4590 }
4591 }
Romain Guy06882f82009-06-10 13:36:04 -07004592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 public Configuration computeNewConfiguration() {
4594 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004595 return computeNewConfigurationLocked();
4596 }
4597 }
Romain Guy06882f82009-06-10 13:36:04 -07004598
Dianne Hackbornc485a602009-03-24 22:39:49 -07004599 Configuration computeNewConfigurationLocked() {
4600 Configuration config = new Configuration();
4601 if (!computeNewConfigurationLocked(config)) {
4602 return null;
4603 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004604 return config;
4605 }
Romain Guy06882f82009-06-10 13:36:04 -07004606
Dianne Hackbornc485a602009-03-24 22:39:49 -07004607 boolean computeNewConfigurationLocked(Configuration config) {
4608 if (mDisplay == null) {
4609 return false;
4610 }
4611 mQueue.getInputConfiguration(config);
4612 final int dw = mDisplay.getWidth();
4613 final int dh = mDisplay.getHeight();
4614 int orientation = Configuration.ORIENTATION_SQUARE;
4615 if (dw < dh) {
4616 orientation = Configuration.ORIENTATION_PORTRAIT;
4617 } else if (dw > dh) {
4618 orientation = Configuration.ORIENTATION_LANDSCAPE;
4619 }
4620 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004621
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004622 DisplayMetrics dm = new DisplayMetrics();
4623 mDisplay.getMetrics(dm);
4624 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4625
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004626 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004627 // Note we only do this once because at this point we don't
4628 // expect the screen to change in this way at runtime, and want
4629 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004630 int longSize = dw;
4631 int shortSize = dh;
4632 if (longSize < shortSize) {
4633 int tmp = longSize;
4634 longSize = shortSize;
4635 shortSize = tmp;
4636 }
4637 longSize = (int)(longSize/dm.density);
4638 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004639
Dianne Hackborn723738c2009-06-25 19:48:04 -07004640 // These semi-magic numbers define our compatibility modes for
4641 // applications with different screens. Don't change unless you
4642 // make sure to test lots and lots of apps!
4643 if (longSize < 470) {
4644 // This is shorter than an HVGA normal density screen (which
4645 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004646 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4647 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004648 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004649 // Is this a large screen?
4650 if (longSize > 640 && shortSize >= 480) {
4651 // VGA or larger screens at medium density are the point
4652 // at which we consider it to be a large screen.
4653 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4654 } else {
4655 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4656
4657 // If this screen is wider than normal HVGA, or taller
4658 // than FWVGA, then for old apps we want to run in size
4659 // compatibility mode.
4660 if (shortSize > 321 || longSize > 570) {
4661 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4662 }
4663 }
4664
4665 // Is this a long screen?
4666 if (((longSize*3)/5) >= (shortSize-1)) {
4667 // Anything wider than WVGA (5:3) is considering to be long.
4668 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4669 } else {
4670 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4671 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004672 }
4673 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004674 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004675
Dianne Hackbornc485a602009-03-24 22:39:49 -07004676 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4677 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4678 mPolicy.adjustConfigurationLw(config);
4679 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 }
Romain Guy06882f82009-06-10 13:36:04 -07004681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 // -------------------------------------------------------------
4683 // Input Events and Focus Management
4684 // -------------------------------------------------------------
4685
4686 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004687 long curTime = SystemClock.uptimeMillis();
4688
Michael Chane10de972009-05-18 11:24:50 -07004689 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004690 if (mLastTouchEventType == eventType &&
4691 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4692 return;
4693 }
4694 mLastUserActivityCallTime = curTime;
4695 mLastTouchEventType = eventType;
4696 }
4697
4698 if (targetWin == null
4699 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4700 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 }
4702 }
4703
4704 // tells if it's a cheek event or not -- this function is stateful
4705 private static final int EVENT_NONE = 0;
4706 private static final int EVENT_UNKNOWN = 0;
4707 private static final int EVENT_CHEEK = 0;
4708 private static final int EVENT_IGNORE_DURATION = 300; // ms
4709 private static final float CHEEK_THRESHOLD = 0.6f;
4710 private int mEventState = EVENT_NONE;
4711 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 private int eventType(MotionEvent ev) {
4714 float size = ev.getSize();
4715 switch (ev.getAction()) {
4716 case MotionEvent.ACTION_DOWN:
4717 mEventSize = size;
4718 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4719 case MotionEvent.ACTION_UP:
4720 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004721 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 case MotionEvent.ACTION_MOVE:
4723 final int N = ev.getHistorySize();
4724 if (size > mEventSize) mEventSize = size;
4725 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4726 for (int i=0; i<N; i++) {
4727 size = ev.getHistoricalSize(i);
4728 if (size > mEventSize) mEventSize = size;
4729 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4730 }
4731 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4732 return TOUCH_EVENT;
4733 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004734 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 }
4736 default:
4737 // not good
4738 return OTHER_EVENT;
4739 }
4740 }
4741
4742 /**
4743 * @return Returns true if event was dispatched, false if it was dropped for any reason
4744 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004745 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4747 "dispatchPointer " + ev);
4748
Michael Chan53071d62009-05-13 17:29:48 -07004749 if (MEASURE_LATENCY) {
4750 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4751 }
4752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004754 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755
Michael Chan53071d62009-05-13 17:29:48 -07004756 if (MEASURE_LATENCY) {
4757 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4758 }
4759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 if (action == MotionEvent.ACTION_UP) {
4763 // let go of our target
4764 mKeyWaiter.mMotionTarget = null;
4765 mPowerManager.logPointerUpEvent();
4766 } else if (action == MotionEvent.ACTION_DOWN) {
4767 mPowerManager.logPointerDownEvent();
4768 }
4769
4770 if (targetObj == null) {
4771 // In this case we are either dropping the event, or have received
4772 // a move or up without a down. It is common to receive move
4773 // events in such a way, since this means the user is moving the
4774 // pointer without actually pressing down. All other cases should
4775 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004776 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4778 }
4779 if (qev != null) {
4780 mQueue.recycleEvent(qev);
4781 }
4782 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004783 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 }
4785 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4786 if (qev != null) {
4787 mQueue.recycleEvent(qev);
4788 }
4789 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004790 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004791 }
Romain Guy06882f82009-06-10 13:36:04 -07004792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004796 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797
4798 //Log.i(TAG, "Sending " + ev + " to " + target);
4799
4800 if (uid != 0 && uid != target.mSession.mUid) {
4801 if (mContext.checkPermission(
4802 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4803 != PackageManager.PERMISSION_GRANTED) {
4804 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4805 + pid + " uid " + uid + " to window " + target
4806 + " owned by uid " + target.mSession.mUid);
4807 if (qev != null) {
4808 mQueue.recycleEvent(qev);
4809 }
4810 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004811 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813 }
4814
Michael Chan53071d62009-05-13 17:29:48 -07004815 if (MEASURE_LATENCY) {
4816 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4817 }
4818
Romain Guy06882f82009-06-10 13:36:04 -07004819 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4821 //target wants to ignore fat touch events
4822 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4823 //explicit flag to return without processing event further
4824 boolean returnFlag = false;
4825 if((action == MotionEvent.ACTION_DOWN)) {
4826 mFatTouch = false;
4827 if(cheekPress) {
4828 mFatTouch = true;
4829 returnFlag = true;
4830 }
4831 } else {
4832 if(action == MotionEvent.ACTION_UP) {
4833 if(mFatTouch) {
4834 //earlier even was invalid doesnt matter if current up is cheekpress or not
4835 mFatTouch = false;
4836 returnFlag = true;
4837 } else if(cheekPress) {
4838 //cancel the earlier event
4839 ev.setAction(MotionEvent.ACTION_CANCEL);
4840 action = MotionEvent.ACTION_CANCEL;
4841 }
4842 } else if(action == MotionEvent.ACTION_MOVE) {
4843 if(mFatTouch) {
4844 //two cases here
4845 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004846 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 returnFlag = true;
4848 } else if(cheekPress) {
4849 //valid down followed by invalid moves
4850 //an invalid move have to cancel earlier action
4851 ev.setAction(MotionEvent.ACTION_CANCEL);
4852 action = MotionEvent.ACTION_CANCEL;
4853 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4854 //note that the subsequent invalid moves will not get here
4855 mFatTouch = true;
4856 }
4857 }
4858 } //else if action
4859 if(returnFlag) {
4860 //recycle que, ev
4861 if (qev != null) {
4862 mQueue.recycleEvent(qev);
4863 }
4864 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004865 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 }
4867 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004868
Michael Chan9f028e62009-08-04 17:37:46 -07004869 // Enable this for testing the "right" value
4870 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004871 int max_events_per_sec = 35;
4872 try {
4873 max_events_per_sec = Integer.parseInt(SystemProperties
4874 .get("windowsmgr.max_events_per_sec"));
4875 if (max_events_per_sec < 1) {
4876 max_events_per_sec = 35;
4877 }
4878 } catch (NumberFormatException e) {
4879 }
4880 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4881 }
4882
4883 /*
4884 * Throttle events to minimize CPU usage when there's a flood of events
4885 * e.g. constant contact with the screen
4886 */
4887 if (action == MotionEvent.ACTION_MOVE) {
4888 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4889 long now = SystemClock.uptimeMillis();
4890 if (now < nextEventTime) {
4891 try {
4892 Thread.sleep(nextEventTime - now);
4893 } catch (InterruptedException e) {
4894 }
4895 mLastTouchEventTime = nextEventTime;
4896 } else {
4897 mLastTouchEventTime = now;
4898 }
4899 }
4900
Michael Chan53071d62009-05-13 17:29:48 -07004901 if (MEASURE_LATENCY) {
4902 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4903 }
4904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 synchronized(mWindowMap) {
4906 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4907 mKeyWaiter.bindTargetWindowLocked(target,
4908 KeyWaiter.RETURN_PENDING_POINTER, qev);
4909 ev = null;
4910 } else {
4911 if (action == MotionEvent.ACTION_DOWN) {
4912 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4913 if (out != null) {
4914 MotionEvent oev = MotionEvent.obtain(ev);
4915 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4916 do {
4917 final Rect frame = out.mFrame;
4918 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4919 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004920 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 } catch (android.os.RemoteException e) {
4922 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4923 }
4924 oev.offsetLocation((float)frame.left, (float)frame.top);
4925 out = out.mNextOutsideTouch;
4926 } while (out != null);
4927 mKeyWaiter.mOutsideTouchTargets = null;
4928 }
4929 }
4930 final Rect frame = target.mFrame;
4931 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4932 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004933
4934 // If we are on top of the wallpaper, then the wallpaper also
4935 // gets to see this movement.
4936 if (mWallpaperTarget == target) {
4937 sendPointerToWallpaperLocked(target, ev, eventTime);
4938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 }
4940 }
Romain Guy06882f82009-06-10 13:36:04 -07004941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 // finally offset the event to the target's coordinate system and
4943 // dispatch the event.
4944 try {
4945 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4946 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4947 }
Michael Chan53071d62009-05-13 17:29:48 -07004948
4949 if (MEASURE_LATENCY) {
4950 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4951 }
4952
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004953 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004954
4955 if (MEASURE_LATENCY) {
4956 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4957 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004958 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 } catch (android.os.RemoteException e) {
4960 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4961 mKeyWaiter.mMotionTarget = null;
4962 try {
4963 removeWindow(target.mSession, target.mClient);
4964 } catch (java.util.NoSuchElementException ex) {
4965 // This will happen if the window has already been
4966 // removed.
4967 }
4968 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004969 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 }
Romain Guy06882f82009-06-10 13:36:04 -07004971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 /**
4973 * @return Returns true if event was dispatched, false if it was dropped for any reason
4974 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004975 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 if (DEBUG_INPUT) Log.v(
4977 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004980 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 if (focusObj == null) {
4982 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4983 if (qev != null) {
4984 mQueue.recycleEvent(qev);
4985 }
4986 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004987 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 }
4989 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4990 if (qev != null) {
4991 mQueue.recycleEvent(qev);
4992 }
4993 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004994 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 }
Romain Guy06882f82009-06-10 13:36:04 -07004996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 if (uid != 0 && uid != focus.mSession.mUid) {
5000 if (mContext.checkPermission(
5001 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5002 != PackageManager.PERMISSION_GRANTED) {
5003 Log.w(TAG, "Permission denied: injecting key event from pid "
5004 + pid + " uid " + uid + " to window " + focus
5005 + " owned by uid " + focus.mSession.mUid);
5006 if (qev != null) {
5007 mQueue.recycleEvent(qev);
5008 }
5009 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005010 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 }
5012 }
Romain Guy06882f82009-06-10 13:36:04 -07005013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 synchronized(mWindowMap) {
5017 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5018 mKeyWaiter.bindTargetWindowLocked(focus,
5019 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5020 // We don't deliver movement events to the client, we hold
5021 // them and wait for them to call back.
5022 ev = null;
5023 } else {
5024 mKeyWaiter.bindTargetWindowLocked(focus);
5025 }
5026 }
Romain Guy06882f82009-06-10 13:36:04 -07005027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005029 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005030 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 } catch (android.os.RemoteException e) {
5032 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5033 try {
5034 removeWindow(focus.mSession, focus.mClient);
5035 } catch (java.util.NoSuchElementException ex) {
5036 // This will happen if the window has already been
5037 // removed.
5038 }
5039 }
Romain Guy06882f82009-06-10 13:36:04 -07005040
Dianne Hackborncfaef692009-06-15 14:24:44 -07005041 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 }
Romain Guy06882f82009-06-10 13:36:04 -07005043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 /**
5045 * @return Returns true if event was dispatched, false if it was dropped for any reason
5046 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005047 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5049
5050 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005051 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 if (focusObj == null) {
5053 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005054 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 }
5056 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005057 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 }
Romain Guy06882f82009-06-10 13:36:04 -07005059
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005060 // Okay we have finished waiting for the last event to be processed.
5061 // First off, if this is a repeat event, check to see if there is
5062 // a corresponding up event in the queue. If there is, we will
5063 // just drop the repeat, because it makes no sense to repeat after
5064 // the user has released a key. (This is especially important for
5065 // long presses.)
5066 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5067 return INJECT_SUCCEEDED;
5068 }
5069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 if (DEBUG_INPUT) Log.v(
5073 TAG, "Dispatching to " + focus + ": " + event);
5074
5075 if (uid != 0 && uid != focus.mSession.mUid) {
5076 if (mContext.checkPermission(
5077 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5078 != PackageManager.PERMISSION_GRANTED) {
5079 Log.w(TAG, "Permission denied: injecting key event from pid "
5080 + pid + " uid " + uid + " to window " + focus
5081 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005082 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 }
5084 }
Romain Guy06882f82009-06-10 13:36:04 -07005085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 synchronized(mWindowMap) {
5087 mKeyWaiter.bindTargetWindowLocked(focus);
5088 }
5089
5090 // NOSHIP extra state logging
5091 mKeyWaiter.recordDispatchState(event, focus);
5092 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 try {
5095 if (DEBUG_INPUT || DEBUG_FOCUS) {
5096 Log.v(TAG, "Delivering key " + event.getKeyCode()
5097 + " to " + focus);
5098 }
5099 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005100 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 } catch (android.os.RemoteException e) {
5102 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5103 try {
5104 removeWindow(focus.mSession, focus.mClient);
5105 } catch (java.util.NoSuchElementException ex) {
5106 // This will happen if the window has already been
5107 // removed.
5108 }
5109 }
Romain Guy06882f82009-06-10 13:36:04 -07005110
Dianne Hackborncfaef692009-06-15 14:24:44 -07005111 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 }
Romain Guy06882f82009-06-10 13:36:04 -07005113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 public void pauseKeyDispatching(IBinder _token) {
5115 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5116 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005117 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 }
5119
5120 synchronized (mWindowMap) {
5121 WindowToken token = mTokenMap.get(_token);
5122 if (token != null) {
5123 mKeyWaiter.pauseDispatchingLocked(token);
5124 }
5125 }
5126 }
5127
5128 public void resumeKeyDispatching(IBinder _token) {
5129 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5130 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005131 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 }
5133
5134 synchronized (mWindowMap) {
5135 WindowToken token = mTokenMap.get(_token);
5136 if (token != null) {
5137 mKeyWaiter.resumeDispatchingLocked(token);
5138 }
5139 }
5140 }
5141
5142 public void setEventDispatching(boolean enabled) {
5143 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5144 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005145 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 }
5147
5148 synchronized (mWindowMap) {
5149 mKeyWaiter.setEventDispatchingLocked(enabled);
5150 }
5151 }
Romain Guy06882f82009-06-10 13:36:04 -07005152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 /**
5154 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005155 *
5156 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 * {@link SystemClock#uptimeMillis()} as the timebase.)
5158 * @param sync If true, wait for the event to be completed before returning to the caller.
5159 * @return Returns true if event was dispatched, false if it was dropped for any reason
5160 */
5161 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5162 long downTime = ev.getDownTime();
5163 long eventTime = ev.getEventTime();
5164
5165 int action = ev.getAction();
5166 int code = ev.getKeyCode();
5167 int repeatCount = ev.getRepeatCount();
5168 int metaState = ev.getMetaState();
5169 int deviceId = ev.getDeviceId();
5170 int scancode = ev.getScanCode();
5171
5172 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5173 if (downTime == 0) downTime = eventTime;
5174
5175 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005176 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005178 final int pid = Binder.getCallingPid();
5179 final int uid = Binder.getCallingUid();
5180 final long ident = Binder.clearCallingIdentity();
5181 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005183 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005185 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005186 switch (result) {
5187 case INJECT_NO_PERMISSION:
5188 throw new SecurityException(
5189 "Injecting to another application requires INJECT_EVENT permission");
5190 case INJECT_SUCCEEDED:
5191 return true;
5192 }
5193 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 }
5195
5196 /**
5197 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005198 *
5199 * @param ev A motion event describing the pointer (touch) action. (As noted in
5200 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 * {@link SystemClock#uptimeMillis()} as the timebase.)
5202 * @param sync If true, wait for the event to be completed before returning to the caller.
5203 * @return Returns true if event was dispatched, false if it was dropped for any reason
5204 */
5205 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005206 final int pid = Binder.getCallingPid();
5207 final int uid = Binder.getCallingUid();
5208 final long ident = Binder.clearCallingIdentity();
5209 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005211 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005213 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005214 switch (result) {
5215 case INJECT_NO_PERMISSION:
5216 throw new SecurityException(
5217 "Injecting to another application requires INJECT_EVENT permission");
5218 case INJECT_SUCCEEDED:
5219 return true;
5220 }
5221 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 }
Romain Guy06882f82009-06-10 13:36:04 -07005223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 /**
5225 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005226 *
5227 * @param ev A motion event describing the trackball action. (As noted in
5228 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 * {@link SystemClock#uptimeMillis()} as the timebase.)
5230 * @param sync If true, wait for the event to be completed before returning to the caller.
5231 * @return Returns true if event was dispatched, false if it was dropped for any reason
5232 */
5233 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005234 final int pid = Binder.getCallingPid();
5235 final int uid = Binder.getCallingUid();
5236 final long ident = Binder.clearCallingIdentity();
5237 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005239 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005241 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005242 switch (result) {
5243 case INJECT_NO_PERMISSION:
5244 throw new SecurityException(
5245 "Injecting to another application requires INJECT_EVENT permission");
5246 case INJECT_SUCCEEDED:
5247 return true;
5248 }
5249 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 }
Romain Guy06882f82009-06-10 13:36:04 -07005251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 private WindowState getFocusedWindow() {
5253 synchronized (mWindowMap) {
5254 return getFocusedWindowLocked();
5255 }
5256 }
5257
5258 private WindowState getFocusedWindowLocked() {
5259 return mCurrentFocus;
5260 }
Romain Guy06882f82009-06-10 13:36:04 -07005261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 /**
5263 * This class holds the state for dispatching key events. This state
5264 * is protected by the KeyWaiter instance, NOT by the window lock. You
5265 * can be holding the main window lock while acquire the KeyWaiter lock,
5266 * but not the other way around.
5267 */
5268 final class KeyWaiter {
5269 // NOSHIP debugging
5270 public class DispatchState {
5271 private KeyEvent event;
5272 private WindowState focus;
5273 private long time;
5274 private WindowState lastWin;
5275 private IBinder lastBinder;
5276 private boolean finished;
5277 private boolean gotFirstWindow;
5278 private boolean eventDispatching;
5279 private long timeToSwitch;
5280 private boolean wasFrozen;
5281 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005282 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5285 focus = theFocus;
5286 event = theEvent;
5287 time = System.currentTimeMillis();
5288 // snapshot KeyWaiter state
5289 lastWin = mLastWin;
5290 lastBinder = mLastBinder;
5291 finished = mFinished;
5292 gotFirstWindow = mGotFirstWindow;
5293 eventDispatching = mEventDispatching;
5294 timeToSwitch = mTimeToSwitch;
5295 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005296 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 // cache the paused state at ctor time as well
5298 if (theFocus == null || theFocus.mToken == null) {
5299 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5300 focusPaused = false;
5301 } else {
5302 focusPaused = theFocus.mToken.paused;
5303 }
5304 }
Romain Guy06882f82009-06-10 13:36:04 -07005305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 public String toString() {
5307 return "{{" + event + " to " + focus + " @ " + time
5308 + " lw=" + lastWin + " lb=" + lastBinder
5309 + " fin=" + finished + " gfw=" + gotFirstWindow
5310 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005311 + " wf=" + wasFrozen + " fp=" + focusPaused
5312 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 }
5314 };
5315 private DispatchState mDispatchState = null;
5316 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5317 mDispatchState = new DispatchState(theEvent, theFocus);
5318 }
5319 // END NOSHIP
5320
5321 public static final int RETURN_NOTHING = 0;
5322 public static final int RETURN_PENDING_POINTER = 1;
5323 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 final Object SKIP_TARGET_TOKEN = new Object();
5326 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 private WindowState mLastWin = null;
5329 private IBinder mLastBinder = null;
5330 private boolean mFinished = true;
5331 private boolean mGotFirstWindow = false;
5332 private boolean mEventDispatching = true;
5333 private long mTimeToSwitch = 0;
5334 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 // Target of Motion events
5337 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 // Windows above the target who would like to receive an "outside"
5340 // touch event for any down events outside of them.
5341 WindowState mOutsideTouchTargets;
5342
5343 /**
5344 * Wait for the last event dispatch to complete, then find the next
5345 * target that should receive the given event and wait for that one
5346 * to be ready to receive it.
5347 */
5348 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5349 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005350 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 long startTime = SystemClock.uptimeMillis();
5352 long keyDispatchingTimeout = 5 * 1000;
5353 long waitedFor = 0;
5354
5355 while (true) {
5356 // Figure out which window we care about. It is either the
5357 // last window we are waiting to have process the event or,
5358 // if none, then the next window we think the event should go
5359 // to. Note: we retrieve mLastWin outside of the lock, so
5360 // it may change before we lock. Thus we must check it again.
5361 WindowState targetWin = mLastWin;
5362 boolean targetIsNew = targetWin == null;
5363 if (DEBUG_INPUT) Log.v(
5364 TAG, "waitForLastKey: mFinished=" + mFinished +
5365 ", mLastWin=" + mLastWin);
5366 if (targetIsNew) {
5367 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005368 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 if (target == SKIP_TARGET_TOKEN) {
5370 // The user has pressed a special key, and we are
5371 // dropping all pending events before it.
5372 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5373 + " " + nextMotion);
5374 return null;
5375 }
5376 if (target == CONSUMED_EVENT_TOKEN) {
5377 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5378 + " " + nextMotion);
5379 return target;
5380 }
5381 targetWin = (WindowState)target;
5382 }
Romain Guy06882f82009-06-10 13:36:04 -07005383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 // Now: is it okay to send the next event to this window?
5387 synchronized (this) {
5388 // First: did we come here based on the last window not
5389 // being null, but it changed by the time we got here?
5390 // If so, try again.
5391 if (!targetIsNew && mLastWin == null) {
5392 continue;
5393 }
Romain Guy06882f82009-06-10 13:36:04 -07005394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005395 // We never dispatch events if not finished with the
5396 // last one, or the display is frozen.
5397 if (mFinished && !mDisplayFrozen) {
5398 // If event dispatching is disabled, then we
5399 // just consume the events.
5400 if (!mEventDispatching) {
5401 if (DEBUG_INPUT) Log.v(TAG,
5402 "Skipping event; dispatching disabled: "
5403 + nextKey + " " + nextMotion);
5404 return null;
5405 }
5406 if (targetWin != null) {
5407 // If this is a new target, and that target is not
5408 // paused or unresponsive, then all looks good to
5409 // handle the event.
5410 if (targetIsNew && !targetWin.mToken.paused) {
5411 return targetWin;
5412 }
Romain Guy06882f82009-06-10 13:36:04 -07005413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 // If we didn't find a target window, and there is no
5415 // focused app window, then just eat the events.
5416 } else if (mFocusedApp == null) {
5417 if (DEBUG_INPUT) Log.v(TAG,
5418 "Skipping event; no focused app: "
5419 + nextKey + " " + nextMotion);
5420 return null;
5421 }
5422 }
Romain Guy06882f82009-06-10 13:36:04 -07005423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 if (DEBUG_INPUT) Log.v(
5425 TAG, "Waiting for last key in " + mLastBinder
5426 + " target=" + targetWin
5427 + " mFinished=" + mFinished
5428 + " mDisplayFrozen=" + mDisplayFrozen
5429 + " targetIsNew=" + targetIsNew
5430 + " paused="
5431 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005432 + " mFocusedApp=" + mFocusedApp
5433 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 targetApp = targetWin != null
5436 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 long curTimeout = keyDispatchingTimeout;
5439 if (mTimeToSwitch != 0) {
5440 long now = SystemClock.uptimeMillis();
5441 if (mTimeToSwitch <= now) {
5442 // If an app switch key has been pressed, and we have
5443 // waited too long for the current app to finish
5444 // processing keys, then wait no more!
5445 doFinishedKeyLocked(true);
5446 continue;
5447 }
5448 long switchTimeout = mTimeToSwitch - now;
5449 if (curTimeout > switchTimeout) {
5450 curTimeout = switchTimeout;
5451 }
5452 }
Romain Guy06882f82009-06-10 13:36:04 -07005453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 try {
5455 // after that continue
5456 // processing keys, so we don't get stuck.
5457 if (DEBUG_INPUT) Log.v(
5458 TAG, "Waiting for key dispatch: " + curTimeout);
5459 wait(curTimeout);
5460 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5461 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005462 + startTime + " switchTime=" + mTimeToSwitch
5463 + " target=" + targetWin + " mLW=" + mLastWin
5464 + " mLB=" + mLastBinder + " fin=" + mFinished
5465 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 } catch (InterruptedException e) {
5467 }
5468 }
5469
5470 // If we were frozen during configuration change, restart the
5471 // timeout checks from now; otherwise look at whether we timed
5472 // out before awakening.
5473 if (mWasFrozen) {
5474 waitedFor = 0;
5475 mWasFrozen = false;
5476 } else {
5477 waitedFor = SystemClock.uptimeMillis() - startTime;
5478 }
5479
5480 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5481 IApplicationToken at = null;
5482 synchronized (this) {
5483 Log.w(TAG, "Key dispatching timed out sending to " +
5484 (targetWin != null ? targetWin.mAttrs.getTitle()
5485 : "<null>"));
5486 // NOSHIP debugging
5487 Log.w(TAG, "Dispatch state: " + mDispatchState);
5488 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5489 // END NOSHIP
5490 //dump();
5491 if (targetWin != null) {
5492 at = targetWin.getAppToken();
5493 } else if (targetApp != null) {
5494 at = targetApp.appToken;
5495 }
5496 }
5497
5498 boolean abort = true;
5499 if (at != null) {
5500 try {
5501 long timeout = at.getKeyDispatchingTimeout();
5502 if (timeout > waitedFor) {
5503 // we did not wait the proper amount of time for this application.
5504 // set the timeout to be the real timeout and wait again.
5505 keyDispatchingTimeout = timeout - waitedFor;
5506 continue;
5507 } else {
5508 abort = at.keyDispatchingTimedOut();
5509 }
5510 } catch (RemoteException ex) {
5511 }
5512 }
5513
5514 synchronized (this) {
5515 if (abort && (mLastWin == targetWin || targetWin == null)) {
5516 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005517 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 if (DEBUG_INPUT) Log.v(TAG,
5519 "Window " + mLastWin +
5520 " timed out on key input");
5521 if (mLastWin.mToken.paused) {
5522 Log.w(TAG, "Un-pausing dispatching to this window");
5523 mLastWin.mToken.paused = false;
5524 }
5525 }
5526 if (mMotionTarget == targetWin) {
5527 mMotionTarget = null;
5528 }
5529 mLastWin = null;
5530 mLastBinder = null;
5531 if (failIfTimeout || targetWin == null) {
5532 return null;
5533 }
5534 } else {
5535 Log.w(TAG, "Continuing to wait for key to be dispatched");
5536 startTime = SystemClock.uptimeMillis();
5537 }
5538 }
5539 }
5540 }
5541 }
Romain Guy06882f82009-06-10 13:36:04 -07005542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005544 MotionEvent nextMotion, boolean isPointerEvent,
5545 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 if (nextKey != null) {
5549 // Find the target window for a normal key event.
5550 final int keycode = nextKey.getKeyCode();
5551 final int repeatCount = nextKey.getRepeatCount();
5552 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5553 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005556 if (callingUid == 0 ||
5557 mContext.checkPermission(
5558 android.Manifest.permission.INJECT_EVENTS,
5559 callingPid, callingUid)
5560 == PackageManager.PERMISSION_GRANTED) {
5561 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005562 nextKey.getMetaState(), down, repeatCount,
5563 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 Log.w(TAG, "Event timeout during app switch: dropping "
5566 + nextKey);
5567 return SKIP_TARGET_TOKEN;
5568 }
Romain Guy06882f82009-06-10 13:36:04 -07005569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 WindowState focus = null;
5573 synchronized(mWindowMap) {
5574 focus = getFocusedWindowLocked();
5575 }
Romain Guy06882f82009-06-10 13:36:04 -07005576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005578
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005579 if (callingUid == 0 ||
5580 (focus != null && callingUid == focus.mSession.mUid) ||
5581 mContext.checkPermission(
5582 android.Manifest.permission.INJECT_EVENTS,
5583 callingPid, callingUid)
5584 == PackageManager.PERMISSION_GRANTED) {
5585 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005586 keycode, nextKey.getMetaState(), down, repeatCount,
5587 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005588 return CONSUMED_EVENT_TOKEN;
5589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 }
Romain Guy06882f82009-06-10 13:36:04 -07005591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 } else if (!isPointerEvent) {
5595 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5596 if (!dispatch) {
5597 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5598 + nextMotion);
5599 return SKIP_TARGET_TOKEN;
5600 }
Romain Guy06882f82009-06-10 13:36:04 -07005601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 WindowState focus = null;
5603 synchronized(mWindowMap) {
5604 focus = getFocusedWindowLocked();
5605 }
Romain Guy06882f82009-06-10 13:36:04 -07005606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5608 return focus;
5609 }
Romain Guy06882f82009-06-10 13:36:04 -07005610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 if (nextMotion == null) {
5612 return SKIP_TARGET_TOKEN;
5613 }
Romain Guy06882f82009-06-10 13:36:04 -07005614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5616 KeyEvent.KEYCODE_UNKNOWN);
5617 if (!dispatch) {
5618 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5619 + nextMotion);
5620 return SKIP_TARGET_TOKEN;
5621 }
Romain Guy06882f82009-06-10 13:36:04 -07005622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 // Find the target window for a pointer event.
5624 int action = nextMotion.getAction();
5625 final float xf = nextMotion.getX();
5626 final float yf = nextMotion.getY();
5627 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 final boolean screenWasOff = qev != null
5630 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 synchronized(mWindowMap) {
5635 synchronized (this) {
5636 if (action == MotionEvent.ACTION_DOWN) {
5637 if (mMotionTarget != null) {
5638 // this is weird, we got a pen down, but we thought it was
5639 // already down!
5640 // XXX: We should probably send an ACTION_UP to the current
5641 // target.
5642 Log.w(TAG, "Pointer down received while already down in: "
5643 + mMotionTarget);
5644 mMotionTarget = null;
5645 }
Romain Guy06882f82009-06-10 13:36:04 -07005646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 // ACTION_DOWN is special, because we need to lock next events to
5648 // the window we'll land onto.
5649 final int x = (int)xf;
5650 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 final ArrayList windows = mWindows;
5653 final int N = windows.size();
5654 WindowState topErrWindow = null;
5655 final Rect tmpRect = mTempRect;
5656 for (int i=N-1; i>=0; i--) {
5657 WindowState child = (WindowState)windows.get(i);
5658 //Log.i(TAG, "Checking dispatch to: " + child);
5659 final int flags = child.mAttrs.flags;
5660 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5661 if (topErrWindow == null) {
5662 topErrWindow = child;
5663 }
5664 }
5665 if (!child.isVisibleLw()) {
5666 //Log.i(TAG, "Not visible!");
5667 continue;
5668 }
5669 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5670 //Log.i(TAG, "Not touchable!");
5671 if ((flags & WindowManager.LayoutParams
5672 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5673 child.mNextOutsideTouch = mOutsideTouchTargets;
5674 mOutsideTouchTargets = child;
5675 }
5676 continue;
5677 }
5678 tmpRect.set(child.mFrame);
5679 if (child.mTouchableInsets == ViewTreeObserver
5680 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5681 // The touch is inside of the window if it is
5682 // inside the frame, AND the content part of that
5683 // frame that was given by the application.
5684 tmpRect.left += child.mGivenContentInsets.left;
5685 tmpRect.top += child.mGivenContentInsets.top;
5686 tmpRect.right -= child.mGivenContentInsets.right;
5687 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5688 } else if (child.mTouchableInsets == ViewTreeObserver
5689 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5690 // The touch is inside of the window if it is
5691 // inside the frame, AND the visible part of that
5692 // frame that was given by the application.
5693 tmpRect.left += child.mGivenVisibleInsets.left;
5694 tmpRect.top += child.mGivenVisibleInsets.top;
5695 tmpRect.right -= child.mGivenVisibleInsets.right;
5696 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5697 }
5698 final int touchFlags = flags &
5699 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5700 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5701 if (tmpRect.contains(x, y) || touchFlags == 0) {
5702 //Log.i(TAG, "Using this target!");
5703 if (!screenWasOff || (flags &
5704 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5705 mMotionTarget = child;
5706 } else {
5707 //Log.i(TAG, "Waking, skip!");
5708 mMotionTarget = null;
5709 }
5710 break;
5711 }
Romain Guy06882f82009-06-10 13:36:04 -07005712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 if ((flags & WindowManager.LayoutParams
5714 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5715 child.mNextOutsideTouch = mOutsideTouchTargets;
5716 mOutsideTouchTargets = child;
5717 //Log.i(TAG, "Adding to outside target list: " + child);
5718 }
5719 }
5720
5721 // if there's an error window but it's not accepting
5722 // focus (typically because it is not yet visible) just
5723 // wait for it -- any other focused window may in fact
5724 // be in ANR state.
5725 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5726 mMotionTarget = null;
5727 }
5728 }
Romain Guy06882f82009-06-10 13:36:04 -07005729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 target = mMotionTarget;
5731 }
5732 }
Romain Guy06882f82009-06-10 13:36:04 -07005733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 // Pointer events are a little different -- if there isn't a
5737 // target found for any event, then just drop it.
5738 return target != null ? target : SKIP_TARGET_TOKEN;
5739 }
Romain Guy06882f82009-06-10 13:36:04 -07005740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 boolean checkShouldDispatchKey(int keycode) {
5742 synchronized (this) {
5743 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5744 mTimeToSwitch = 0;
5745 return true;
5746 }
5747 if (mTimeToSwitch != 0
5748 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5749 return false;
5750 }
5751 return true;
5752 }
5753 }
Romain Guy06882f82009-06-10 13:36:04 -07005754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 void bindTargetWindowLocked(WindowState win,
5756 int pendingWhat, QueuedEvent pendingMotion) {
5757 synchronized (this) {
5758 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5759 }
5760 }
Romain Guy06882f82009-06-10 13:36:04 -07005761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 void bindTargetWindowLocked(WindowState win) {
5763 synchronized (this) {
5764 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5765 }
5766 }
5767
5768 void bindTargetWindowLockedLocked(WindowState win,
5769 int pendingWhat, QueuedEvent pendingMotion) {
5770 mLastWin = win;
5771 mLastBinder = win.mClient.asBinder();
5772 mFinished = false;
5773 if (pendingMotion != null) {
5774 final Session s = win.mSession;
5775 if (pendingWhat == RETURN_PENDING_POINTER) {
5776 releasePendingPointerLocked(s);
5777 s.mPendingPointerMove = pendingMotion;
5778 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005779 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 "bindTargetToWindow " + s.mPendingPointerMove);
5781 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5782 releasePendingTrackballLocked(s);
5783 s.mPendingTrackballMove = pendingMotion;
5784 s.mPendingTrackballWindow = win;
5785 }
5786 }
5787 }
Romain Guy06882f82009-06-10 13:36:04 -07005788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 void releasePendingPointerLocked(Session s) {
5790 if (DEBUG_INPUT) Log.v(TAG,
5791 "releasePendingPointer " + s.mPendingPointerMove);
5792 if (s.mPendingPointerMove != null) {
5793 mQueue.recycleEvent(s.mPendingPointerMove);
5794 s.mPendingPointerMove = null;
5795 }
5796 }
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 void releasePendingTrackballLocked(Session s) {
5799 if (s.mPendingTrackballMove != null) {
5800 mQueue.recycleEvent(s.mPendingTrackballMove);
5801 s.mPendingTrackballMove = null;
5802 }
5803 }
Romain Guy06882f82009-06-10 13:36:04 -07005804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5806 int returnWhat) {
5807 if (DEBUG_INPUT) Log.v(
5808 TAG, "finishedKey: client=" + client + ", force=" + force);
5809
5810 if (client == null) {
5811 return null;
5812 }
5813
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005814 MotionEvent res = null;
5815 QueuedEvent qev = null;
5816 WindowState win = null;
5817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 synchronized (this) {
5819 if (DEBUG_INPUT) Log.v(
5820 TAG, "finishedKey: client=" + client.asBinder()
5821 + ", force=" + force + ", last=" + mLastBinder
5822 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005824 if (returnWhat == RETURN_PENDING_POINTER) {
5825 qev = session.mPendingPointerMove;
5826 win = session.mPendingPointerWindow;
5827 session.mPendingPointerMove = null;
5828 session.mPendingPointerWindow = null;
5829 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5830 qev = session.mPendingTrackballMove;
5831 win = session.mPendingTrackballWindow;
5832 session.mPendingTrackballMove = null;
5833 session.mPendingTrackballWindow = null;
5834 }
Romain Guy06882f82009-06-10 13:36:04 -07005835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 if (mLastBinder == client.asBinder()) {
5837 if (DEBUG_INPUT) Log.v(
5838 TAG, "finishedKey: last paused="
5839 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5840 if (mLastWin != null && (!mLastWin.mToken.paused || force
5841 || !mEventDispatching)) {
5842 doFinishedKeyLocked(false);
5843 } else {
5844 // Make sure to wake up anyone currently waiting to
5845 // dispatch a key, so they can re-evaluate their
5846 // current situation.
5847 mFinished = true;
5848 notifyAll();
5849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 }
Romain Guy06882f82009-06-10 13:36:04 -07005851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005853 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 if (DEBUG_INPUT) Log.v(TAG,
5855 "Returning pending motion: " + res);
5856 mQueue.recycleEvent(qev);
5857 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5858 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005862
5863 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5864 synchronized (mWindowMap) {
5865 if (mWallpaperTarget == win) {
5866 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5867 }
5868 }
5869 }
5870
5871 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 }
5873
5874 void tickle() {
5875 synchronized (this) {
5876 notifyAll();
5877 }
5878 }
Romain Guy06882f82009-06-10 13:36:04 -07005879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 void handleNewWindowLocked(WindowState newWindow) {
5881 if (!newWindow.canReceiveKeys()) {
5882 return;
5883 }
5884 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005885 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 TAG, "New key dispatch window: win="
5887 + newWindow.mClient.asBinder()
5888 + ", last=" + mLastBinder
5889 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5890 + "), finished=" + mFinished + ", paused="
5891 + newWindow.mToken.paused);
5892
5893 // Displaying a window implicitly causes dispatching to
5894 // be unpaused. (This is to protect against bugs if someone
5895 // pauses dispatching but forgets to resume.)
5896 newWindow.mToken.paused = false;
5897
5898 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899
5900 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5901 if (DEBUG_INPUT) Log.v(TAG,
5902 "New SYSTEM_ERROR window; resetting state");
5903 mLastWin = null;
5904 mLastBinder = null;
5905 mMotionTarget = null;
5906 mFinished = true;
5907 } else if (mLastWin != null) {
5908 // If the new window is above the window we are
5909 // waiting on, then stop waiting and let key dispatching
5910 // start on the new guy.
5911 if (DEBUG_INPUT) Log.v(
5912 TAG, "Last win layer=" + mLastWin.mLayer
5913 + ", new win layer=" + newWindow.mLayer);
5914 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005915 // The new window is above the old; finish pending input to the last
5916 // window and start directing it to the new one.
5917 mLastWin.mToken.paused = false;
5918 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005920 // Either the new window is lower, so there is no need to wake key waiters,
5921 // or we just finished key input to the previous window, which implicitly
5922 // notified the key waiters. In both cases, we don't need to issue the
5923 // notification here.
5924 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 }
5926
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005927 // Now that we've put a new window state in place, make the event waiter
5928 // take notice and retarget its attentions.
5929 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 }
5931 }
5932
5933 void pauseDispatchingLocked(WindowToken token) {
5934 synchronized (this)
5935 {
5936 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5937 token.paused = true;
5938
5939 /*
5940 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5941 mPaused = true;
5942 } else {
5943 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005944 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005945 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005946 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005948 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 }
5950 }
5951 */
5952 }
5953 }
5954
5955 void resumeDispatchingLocked(WindowToken token) {
5956 synchronized (this) {
5957 if (token.paused) {
5958 if (DEBUG_INPUT) Log.v(
5959 TAG, "Resuming WindowToken " + token
5960 + ", last=" + mLastBinder
5961 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5962 + "), finished=" + mFinished + ", paused="
5963 + token.paused);
5964 token.paused = false;
5965 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5966 doFinishedKeyLocked(true);
5967 } else {
5968 notifyAll();
5969 }
5970 }
5971 }
5972 }
5973
5974 void setEventDispatchingLocked(boolean enabled) {
5975 synchronized (this) {
5976 mEventDispatching = enabled;
5977 notifyAll();
5978 }
5979 }
Romain Guy06882f82009-06-10 13:36:04 -07005980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 void appSwitchComing() {
5982 synchronized (this) {
5983 // Don't wait for more than .5 seconds for app to finish
5984 // processing the pending events.
5985 long now = SystemClock.uptimeMillis() + 500;
5986 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5987 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5988 mTimeToSwitch = now;
5989 }
5990 notifyAll();
5991 }
5992 }
Romain Guy06882f82009-06-10 13:36:04 -07005993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 private final void doFinishedKeyLocked(boolean doRecycle) {
5995 if (mLastWin != null) {
5996 releasePendingPointerLocked(mLastWin.mSession);
5997 releasePendingTrackballLocked(mLastWin.mSession);
5998 }
Romain Guy06882f82009-06-10 13:36:04 -07005999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 if (mLastWin == null || !mLastWin.mToken.paused
6001 || !mLastWin.isVisibleLw()) {
6002 // If the current window has been paused, we aren't -really-
6003 // finished... so let the waiters still wait.
6004 mLastWin = null;
6005 mLastBinder = null;
6006 }
6007 mFinished = true;
6008 notifyAll();
6009 }
6010 }
6011
6012 private class KeyQ extends KeyInputQueue
6013 implements KeyInputQueue.FilterCallback {
6014 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006017 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6019 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6020 "KEEP_SCREEN_ON_FLAG");
6021 mHoldingScreen.setReferenceCounted(false);
6022 }
6023
6024 @Override
6025 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6026 if (mPolicy.preprocessInputEventTq(event)) {
6027 return true;
6028 }
Romain Guy06882f82009-06-10 13:36:04 -07006029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 switch (event.type) {
6031 case RawInputEvent.EV_KEY: {
6032 // XXX begin hack
6033 if (DEBUG) {
6034 if (event.keycode == KeyEvent.KEYCODE_G) {
6035 if (event.value != 0) {
6036 // G down
6037 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6038 }
6039 return false;
6040 }
6041 if (event.keycode == KeyEvent.KEYCODE_D) {
6042 if (event.value != 0) {
6043 //dump();
6044 }
6045 return false;
6046 }
6047 }
6048 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 boolean screenIsOff = !mPowerManager.screenIsOn();
6051 boolean screenIsDim = !mPowerManager.screenIsBright();
6052 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6055 mPowerManager.goToSleep(event.when);
6056 }
6057
6058 if (screenIsOff) {
6059 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6060 }
6061 if (screenIsDim) {
6062 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6063 }
6064 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6065 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006066 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 }
Romain Guy06882f82009-06-10 13:36:04 -07006068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6070 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6071 filterQueue(this);
6072 mKeyWaiter.appSwitchComing();
6073 }
6074 return true;
6075 } else {
6076 return false;
6077 }
6078 }
Romain Guy06882f82009-06-10 13:36:04 -07006079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 case RawInputEvent.EV_REL: {
6081 boolean screenIsOff = !mPowerManager.screenIsOn();
6082 boolean screenIsDim = !mPowerManager.screenIsBright();
6083 if (screenIsOff) {
6084 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6085 device.classes, event)) {
6086 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6087 return false;
6088 }
6089 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6090 }
6091 if (screenIsDim) {
6092 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6093 }
6094 return true;
6095 }
Romain Guy06882f82009-06-10 13:36:04 -07006096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 case RawInputEvent.EV_ABS: {
6098 boolean screenIsOff = !mPowerManager.screenIsOn();
6099 boolean screenIsDim = !mPowerManager.screenIsBright();
6100 if (screenIsOff) {
6101 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6102 device.classes, event)) {
6103 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6104 return false;
6105 }
6106 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6107 }
6108 if (screenIsDim) {
6109 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6110 }
6111 return true;
6112 }
Romain Guy06882f82009-06-10 13:36:04 -07006113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 default:
6115 return true;
6116 }
6117 }
6118
6119 public int filterEvent(QueuedEvent ev) {
6120 switch (ev.classType) {
6121 case RawInputEvent.CLASS_KEYBOARD:
6122 KeyEvent ke = (KeyEvent)ev.event;
6123 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6124 Log.w(TAG, "Dropping movement key during app switch: "
6125 + ke.getKeyCode() + ", action=" + ke.getAction());
6126 return FILTER_REMOVE;
6127 }
6128 return FILTER_ABORT;
6129 default:
6130 return FILTER_KEEP;
6131 }
6132 }
Romain Guy06882f82009-06-10 13:36:04 -07006133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 /**
6135 * Must be called with the main window manager lock held.
6136 */
6137 void setHoldScreenLocked(boolean holding) {
6138 boolean state = mHoldingScreen.isHeld();
6139 if (holding != state) {
6140 if (holding) {
6141 mHoldingScreen.acquire();
6142 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006143 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 mHoldingScreen.release();
6145 }
6146 }
6147 }
Michael Chan53071d62009-05-13 17:29:48 -07006148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149
6150 public boolean detectSafeMode() {
6151 mSafeMode = mPolicy.detectSafeMode();
6152 return mSafeMode;
6153 }
Romain Guy06882f82009-06-10 13:36:04 -07006154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 public void systemReady() {
6156 mPolicy.systemReady();
6157 }
Romain Guy06882f82009-06-10 13:36:04 -07006158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 private final class InputDispatcherThread extends Thread {
6160 // Time to wait when there is nothing to do: 9999 seconds.
6161 static final int LONG_WAIT=9999*1000;
6162
6163 public InputDispatcherThread() {
6164 super("InputDispatcher");
6165 }
Romain Guy06882f82009-06-10 13:36:04 -07006166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 @Override
6168 public void run() {
6169 while (true) {
6170 try {
6171 process();
6172 } catch (Exception e) {
6173 Log.e(TAG, "Exception in input dispatcher", e);
6174 }
6175 }
6176 }
Romain Guy06882f82009-06-10 13:36:04 -07006177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 private void process() {
6179 android.os.Process.setThreadPriority(
6180 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 // The last key event we saw
6183 KeyEvent lastKey = null;
6184
6185 // Last keydown time for auto-repeating keys
6186 long lastKeyTime = SystemClock.uptimeMillis();
6187 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006188 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189
Romain Guy06882f82009-06-10 13:36:04 -07006190 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 int keyRepeatCount = 0;
6192
6193 // Need to report that configuration has changed?
6194 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 while (true) {
6197 long curTime = SystemClock.uptimeMillis();
6198
6199 if (DEBUG_INPUT) Log.v(
6200 TAG, "Waiting for next key: now=" + curTime
6201 + ", repeat @ " + nextKeyTime);
6202
6203 // Retrieve next event, waiting only as long as the next
6204 // repeat timeout. If the configuration has changed, then
6205 // don't wait at all -- we'll report the change as soon as
6206 // we have processed all events.
6207 QueuedEvent ev = mQueue.getEvent(
6208 (int)((!configChanged && curTime < nextKeyTime)
6209 ? (nextKeyTime-curTime) : 0));
6210
6211 if (DEBUG_INPUT && ev != null) Log.v(
6212 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6213
Michael Chan53071d62009-05-13 17:29:48 -07006214 if (MEASURE_LATENCY) {
6215 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6216 }
6217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 try {
6219 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006220 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 int eventType;
6222 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6223 eventType = eventType((MotionEvent)ev.event);
6224 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6225 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6226 eventType = LocalPowerManager.BUTTON_EVENT;
6227 } else {
6228 eventType = LocalPowerManager.OTHER_EVENT;
6229 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006230 try {
Michael Chan53071d62009-05-13 17:29:48 -07006231 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006232 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006233 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006234 mBatteryStats.noteInputEvent();
6235 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006236 } catch (RemoteException e) {
6237 // Ignore
6238 }
Michael Chane10de972009-05-18 11:24:50 -07006239
6240 if (eventType != TOUCH_EVENT
6241 && eventType != LONG_TOUCH_EVENT
6242 && eventType != CHEEK_EVENT) {
6243 mPowerManager.userActivity(curTime, false,
6244 eventType, false);
6245 } else if (mLastTouchEventType != eventType
6246 || (curTime - mLastUserActivityCallTime)
6247 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6248 mLastUserActivityCallTime = curTime;
6249 mLastTouchEventType = eventType;
6250 mPowerManager.userActivity(curTime, false,
6251 eventType, false);
6252 }
6253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 switch (ev.classType) {
6255 case RawInputEvent.CLASS_KEYBOARD:
6256 KeyEvent ke = (KeyEvent)ev.event;
6257 if (ke.isDown()) {
6258 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006259 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 keyRepeatCount = 0;
6261 lastKeyTime = curTime;
6262 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006263 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 if (DEBUG_INPUT) Log.v(
6265 TAG, "Received key down: first repeat @ "
6266 + nextKeyTime);
6267 } else {
6268 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006269 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 // Arbitrary long timeout.
6271 lastKeyTime = curTime;
6272 nextKeyTime = curTime + LONG_WAIT;
6273 if (DEBUG_INPUT) Log.v(
6274 TAG, "Received key up: ignore repeat @ "
6275 + nextKeyTime);
6276 }
6277 dispatchKey((KeyEvent)ev.event, 0, 0);
6278 mQueue.recycleEvent(ev);
6279 break;
6280 case RawInputEvent.CLASS_TOUCHSCREEN:
6281 //Log.i(TAG, "Read next event " + ev);
6282 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6283 break;
6284 case RawInputEvent.CLASS_TRACKBALL:
6285 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6286 break;
6287 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6288 configChanged = true;
6289 break;
6290 default:
6291 mQueue.recycleEvent(ev);
6292 break;
6293 }
Romain Guy06882f82009-06-10 13:36:04 -07006294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 } else if (configChanged) {
6296 configChanged = false;
6297 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 } else if (lastKey != null) {
6300 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 // Timeout occurred while key was down. If it is at or
6303 // past the key repeat time, dispatch the repeat.
6304 if (DEBUG_INPUT) Log.v(
6305 TAG, "Key timeout: repeat=" + nextKeyTime
6306 + ", now=" + curTime);
6307 if (curTime < nextKeyTime) {
6308 continue;
6309 }
Romain Guy06882f82009-06-10 13:36:04 -07006310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 lastKeyTime = nextKeyTime;
6312 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6313 keyRepeatCount++;
6314 if (DEBUG_INPUT) Log.v(
6315 TAG, "Key repeat: count=" + keyRepeatCount
6316 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006317 KeyEvent newEvent;
6318 if (downTime != 0 && (downTime
6319 + ViewConfiguration.getLongPressTimeout())
6320 <= curTime) {
6321 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6322 curTime, keyRepeatCount,
6323 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6324 downTime = 0;
6325 } else {
6326 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6327 curTime, keyRepeatCount);
6328 }
6329 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 } else {
6332 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 lastKeyTime = curTime;
6335 nextKeyTime = curTime + LONG_WAIT;
6336 }
Romain Guy06882f82009-06-10 13:36:04 -07006337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 } catch (Exception e) {
6339 Log.e(TAG,
6340 "Input thread received uncaught exception: " + e, e);
6341 }
6342 }
6343 }
6344 }
6345
6346 // -------------------------------------------------------------
6347 // Client Session State
6348 // -------------------------------------------------------------
6349
6350 private final class Session extends IWindowSession.Stub
6351 implements IBinder.DeathRecipient {
6352 final IInputMethodClient mClient;
6353 final IInputContext mInputContext;
6354 final int mUid;
6355 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006356 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 SurfaceSession mSurfaceSession;
6358 int mNumWindow = 0;
6359 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 /**
6362 * Current pointer move event being dispatched to client window... must
6363 * hold key lock to access.
6364 */
6365 QueuedEvent mPendingPointerMove;
6366 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 /**
6369 * Current trackball move event being dispatched to client window... must
6370 * hold key lock to access.
6371 */
6372 QueuedEvent mPendingTrackballMove;
6373 WindowState mPendingTrackballWindow;
6374
6375 public Session(IInputMethodClient client, IInputContext inputContext) {
6376 mClient = client;
6377 mInputContext = inputContext;
6378 mUid = Binder.getCallingUid();
6379 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006380 StringBuilder sb = new StringBuilder();
6381 sb.append("Session{");
6382 sb.append(Integer.toHexString(System.identityHashCode(this)));
6383 sb.append(" uid ");
6384 sb.append(mUid);
6385 sb.append("}");
6386 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 synchronized (mWindowMap) {
6389 if (mInputMethodManager == null && mHaveInputMethods) {
6390 IBinder b = ServiceManager.getService(
6391 Context.INPUT_METHOD_SERVICE);
6392 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6393 }
6394 }
6395 long ident = Binder.clearCallingIdentity();
6396 try {
6397 // Note: it is safe to call in to the input method manager
6398 // here because we are not holding our lock.
6399 if (mInputMethodManager != null) {
6400 mInputMethodManager.addClient(client, inputContext,
6401 mUid, mPid);
6402 } else {
6403 client.setUsingInputMethod(false);
6404 }
6405 client.asBinder().linkToDeath(this, 0);
6406 } catch (RemoteException e) {
6407 // The caller has died, so we can just forget about this.
6408 try {
6409 if (mInputMethodManager != null) {
6410 mInputMethodManager.removeClient(client);
6411 }
6412 } catch (RemoteException ee) {
6413 }
6414 } finally {
6415 Binder.restoreCallingIdentity(ident);
6416 }
6417 }
Romain Guy06882f82009-06-10 13:36:04 -07006418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 @Override
6420 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6421 throws RemoteException {
6422 try {
6423 return super.onTransact(code, data, reply, flags);
6424 } catch (RuntimeException e) {
6425 // Log all 'real' exceptions thrown to the caller
6426 if (!(e instanceof SecurityException)) {
6427 Log.e(TAG, "Window Session Crash", e);
6428 }
6429 throw e;
6430 }
6431 }
6432
6433 public void binderDied() {
6434 // Note: it is safe to call in to the input method manager
6435 // here because we are not holding our lock.
6436 try {
6437 if (mInputMethodManager != null) {
6438 mInputMethodManager.removeClient(mClient);
6439 }
6440 } catch (RemoteException e) {
6441 }
6442 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006443 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 mClientDead = true;
6445 killSessionLocked();
6446 }
6447 }
6448
6449 public int add(IWindow window, WindowManager.LayoutParams attrs,
6450 int viewVisibility, Rect outContentInsets) {
6451 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6452 }
Romain Guy06882f82009-06-10 13:36:04 -07006453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 public void remove(IWindow window) {
6455 removeWindow(this, window);
6456 }
Romain Guy06882f82009-06-10 13:36:04 -07006457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6459 int requestedWidth, int requestedHeight, int viewFlags,
6460 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6461 Rect outVisibleInsets, Surface outSurface) {
6462 return relayoutWindow(this, window, attrs,
6463 requestedWidth, requestedHeight, viewFlags, insetsPending,
6464 outFrame, outContentInsets, outVisibleInsets, outSurface);
6465 }
Romain Guy06882f82009-06-10 13:36:04 -07006466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467 public void setTransparentRegion(IWindow window, Region region) {
6468 setTransparentRegionWindow(this, window, region);
6469 }
Romain Guy06882f82009-06-10 13:36:04 -07006470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 public void setInsets(IWindow window, int touchableInsets,
6472 Rect contentInsets, Rect visibleInsets) {
6473 setInsetsWindow(this, window, touchableInsets, contentInsets,
6474 visibleInsets);
6475 }
Romain Guy06882f82009-06-10 13:36:04 -07006476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6478 getWindowDisplayFrame(this, window, outDisplayFrame);
6479 }
Romain Guy06882f82009-06-10 13:36:04 -07006480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006481 public void finishDrawing(IWindow window) {
6482 if (localLOGV) Log.v(
6483 TAG, "IWindow finishDrawing called for " + window);
6484 finishDrawingWindow(this, window);
6485 }
6486
6487 public void finishKey(IWindow window) {
6488 if (localLOGV) Log.v(
6489 TAG, "IWindow finishKey called for " + window);
6490 mKeyWaiter.finishedKey(this, window, false,
6491 KeyWaiter.RETURN_NOTHING);
6492 }
6493
6494 public MotionEvent getPendingPointerMove(IWindow window) {
6495 if (localLOGV) Log.v(
6496 TAG, "IWindow getPendingMotionEvent called for " + window);
6497 return mKeyWaiter.finishedKey(this, window, false,
6498 KeyWaiter.RETURN_PENDING_POINTER);
6499 }
Romain Guy06882f82009-06-10 13:36:04 -07006500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 public MotionEvent getPendingTrackballMove(IWindow window) {
6502 if (localLOGV) Log.v(
6503 TAG, "IWindow getPendingMotionEvent called for " + window);
6504 return mKeyWaiter.finishedKey(this, window, false,
6505 KeyWaiter.RETURN_PENDING_TRACKBALL);
6506 }
6507
6508 public void setInTouchMode(boolean mode) {
6509 synchronized(mWindowMap) {
6510 mInTouchMode = mode;
6511 }
6512 }
6513
6514 public boolean getInTouchMode() {
6515 synchronized(mWindowMap) {
6516 return mInTouchMode;
6517 }
6518 }
6519
6520 public boolean performHapticFeedback(IWindow window, int effectId,
6521 boolean always) {
6522 synchronized(mWindowMap) {
6523 long ident = Binder.clearCallingIdentity();
6524 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006525 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 windowForClientLocked(this, window), effectId, always);
6527 } finally {
6528 Binder.restoreCallingIdentity(ident);
6529 }
6530 }
6531 }
Romain Guy06882f82009-06-10 13:36:04 -07006532
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006533 public void setWallpaperPosition(IBinder window, float x, float y) {
6534 synchronized(mWindowMap) {
6535 long ident = Binder.clearCallingIdentity();
6536 try {
6537 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6538 x, y);
6539 } finally {
6540 Binder.restoreCallingIdentity(ident);
6541 }
6542 }
6543 }
6544
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006545 public void wallpaperOffsetsComplete(IBinder window) {
6546 WindowManagerService.this.wallpaperOffsetsComplete(window);
6547 }
6548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 void windowAddedLocked() {
6550 if (mSurfaceSession == null) {
6551 if (localLOGV) Log.v(
6552 TAG, "First window added to " + this + ", creating SurfaceSession");
6553 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006554 if (SHOW_TRANSACTIONS) Log.i(
6555 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 mSessions.add(this);
6557 }
6558 mNumWindow++;
6559 }
6560
6561 void windowRemovedLocked() {
6562 mNumWindow--;
6563 killSessionLocked();
6564 }
Romain Guy06882f82009-06-10 13:36:04 -07006565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 void killSessionLocked() {
6567 if (mNumWindow <= 0 && mClientDead) {
6568 mSessions.remove(this);
6569 if (mSurfaceSession != null) {
6570 if (localLOGV) Log.v(
6571 TAG, "Last window removed from " + this
6572 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006573 if (SHOW_TRANSACTIONS) Log.i(
6574 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 try {
6576 mSurfaceSession.kill();
6577 } catch (Exception e) {
6578 Log.w(TAG, "Exception thrown when killing surface session "
6579 + mSurfaceSession + " in session " + this
6580 + ": " + e.toString());
6581 }
6582 mSurfaceSession = null;
6583 }
6584 }
6585 }
Romain Guy06882f82009-06-10 13:36:04 -07006586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006588 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6589 pw.print(" mClientDead="); pw.print(mClientDead);
6590 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6591 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6592 pw.print(prefix);
6593 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6594 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6595 }
6596 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6597 pw.print(prefix);
6598 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6599 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 }
6602
6603 @Override
6604 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006605 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 }
6607 }
6608
6609 // -------------------------------------------------------------
6610 // Client Window State
6611 // -------------------------------------------------------------
6612
6613 private final class WindowState implements WindowManagerPolicy.WindowState {
6614 final Session mSession;
6615 final IWindow mClient;
6616 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006617 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 AppWindowToken mAppToken;
6619 AppWindowToken mTargetAppToken;
6620 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6621 final DeathRecipient mDeathRecipient;
6622 final WindowState mAttachedWindow;
6623 final ArrayList mChildWindows = new ArrayList();
6624 final int mBaseLayer;
6625 final int mSubLayer;
6626 final boolean mLayoutAttached;
6627 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006628 final boolean mIsWallpaper;
6629 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 int mViewVisibility;
6631 boolean mPolicyVisibility = true;
6632 boolean mPolicyVisibilityAfterAnim = true;
6633 boolean mAppFreezing;
6634 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006635 boolean mReportDestroySurface;
6636 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 boolean mAttachedHidden; // is our parent window hidden?
6638 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006639 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 int mRequestedWidth;
6641 int mRequestedHeight;
6642 int mLastRequestedWidth;
6643 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 int mLayer;
6645 int mAnimLayer;
6646 int mLastLayer;
6647 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006648 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006649 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006650
6651 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 // Actual frame shown on-screen (may be modified by animation)
6654 final Rect mShownFrame = new Rect();
6655 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 /**
6658 * Insets that determine the actually visible area
6659 */
6660 final Rect mVisibleInsets = new Rect();
6661 final Rect mLastVisibleInsets = new Rect();
6662 boolean mVisibleInsetsChanged;
6663
6664 /**
6665 * Insets that are covered by system windows
6666 */
6667 final Rect mContentInsets = new Rect();
6668 final Rect mLastContentInsets = new Rect();
6669 boolean mContentInsetsChanged;
6670
6671 /**
6672 * Set to true if we are waiting for this window to receive its
6673 * given internal insets before laying out other windows based on it.
6674 */
6675 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 /**
6678 * These are the content insets that were given during layout for
6679 * this window, to be applied to windows behind it.
6680 */
6681 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 /**
6684 * These are the visible insets that were given during layout for
6685 * this window, to be applied to windows behind it.
6686 */
6687 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 /**
6690 * Flag indicating whether the touchable region should be adjusted by
6691 * the visible insets; if false the area outside the visible insets is
6692 * NOT touchable, so we must use those to adjust the frame during hit
6693 * tests.
6694 */
6695 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006697 // Current transformation being applied.
6698 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6699 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6700 float mHScale=1, mVScale=1;
6701 float mLastHScale=1, mLastVScale=1;
6702 final Matrix mTmpMatrix = new Matrix();
6703
6704 // "Real" frame that the application sees.
6705 final Rect mFrame = new Rect();
6706 final Rect mLastFrame = new Rect();
6707
6708 final Rect mContainingFrame = new Rect();
6709 final Rect mDisplayFrame = new Rect();
6710 final Rect mContentFrame = new Rect();
6711 final Rect mVisibleFrame = new Rect();
6712
6713 float mShownAlpha = 1;
6714 float mAlpha = 1;
6715 float mLastAlpha = 1;
6716
6717 // Set to true if, when the window gets displayed, it should perform
6718 // an enter animation.
6719 boolean mEnterAnimationPending;
6720
6721 // Currently running animation.
6722 boolean mAnimating;
6723 boolean mLocalAnimating;
6724 Animation mAnimation;
6725 boolean mAnimationIsEntrance;
6726 boolean mHasTransformation;
6727 boolean mHasLocalTransformation;
6728 final Transformation mTransformation = new Transformation();
6729
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006730 // If a window showing a wallpaper: the requested offset for the
6731 // wallpaper; if a wallpaper window: the currently applied offset.
6732 float mWallpaperX = -1;
6733 float mWallpaperY = -1;
6734
6735 // Wallpaper windows: pixels offset based on above variables.
6736 int mXOffset;
6737 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006739 // This is set after IWindowSession.relayout() has been called at
6740 // least once for the window. It allows us to detect the situation
6741 // where we don't yet have a surface, but should have one soon, so
6742 // we can give the window focus before waiting for the relayout.
6743 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 // This is set after the Surface has been created but before the
6746 // window has been drawn. During this time the surface is hidden.
6747 boolean mDrawPending;
6748
6749 // This is set after the window has finished drawing for the first
6750 // time but before its surface is shown. The surface will be
6751 // displayed when the next layout is run.
6752 boolean mCommitDrawPending;
6753
6754 // This is set during the time after the window's drawing has been
6755 // committed, and before its surface is actually shown. It is used
6756 // to delay showing the surface until all windows in a token are ready
6757 // to be shown.
6758 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 // Set when the window has been shown in the screen the first time.
6761 boolean mHasDrawn;
6762
6763 // Currently running an exit animation?
6764 boolean mExiting;
6765
6766 // Currently on the mDestroySurface list?
6767 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 // Completely remove from window manager after exit animation?
6770 boolean mRemoveOnExit;
6771
6772 // Set when the orientation is changing and this window has not yet
6773 // been updated for the new orientation.
6774 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 // Is this window now (or just being) removed?
6777 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 WindowState(Session s, IWindow c, WindowToken token,
6780 WindowState attachedWindow, WindowManager.LayoutParams a,
6781 int viewVisibility) {
6782 mSession = s;
6783 mClient = c;
6784 mToken = token;
6785 mAttrs.copyFrom(a);
6786 mViewVisibility = viewVisibility;
6787 DeathRecipient deathRecipient = new DeathRecipient();
6788 mAlpha = a.alpha;
6789 if (localLOGV) Log.v(
6790 TAG, "Window " + this + " client=" + c.asBinder()
6791 + " token=" + token + " (" + mAttrs.token + ")");
6792 try {
6793 c.asBinder().linkToDeath(deathRecipient, 0);
6794 } catch (RemoteException e) {
6795 mDeathRecipient = null;
6796 mAttachedWindow = null;
6797 mLayoutAttached = false;
6798 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006799 mIsWallpaper = false;
6800 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 mBaseLayer = 0;
6802 mSubLayer = 0;
6803 return;
6804 }
6805 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6808 mAttrs.type <= LAST_SUB_WINDOW)) {
6809 // The multiplier here is to reserve space for multiple
6810 // windows in the same type layer.
6811 mBaseLayer = mPolicy.windowTypeToLayerLw(
6812 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6813 + TYPE_LAYER_OFFSET;
6814 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6815 mAttachedWindow = attachedWindow;
6816 mAttachedWindow.mChildWindows.add(this);
6817 mLayoutAttached = mAttrs.type !=
6818 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6819 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6820 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006821 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6822 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 } else {
6824 // The multiplier here is to reserve space for multiple
6825 // windows in the same type layer.
6826 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6827 * TYPE_LAYER_MULTIPLIER
6828 + TYPE_LAYER_OFFSET;
6829 mSubLayer = 0;
6830 mAttachedWindow = null;
6831 mLayoutAttached = false;
6832 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6833 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006834 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6835 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 }
6837
6838 WindowState appWin = this;
6839 while (appWin.mAttachedWindow != null) {
6840 appWin = mAttachedWindow;
6841 }
6842 WindowToken appToken = appWin.mToken;
6843 while (appToken.appWindowToken == null) {
6844 WindowToken parent = mTokenMap.get(appToken.token);
6845 if (parent == null || appToken == parent) {
6846 break;
6847 }
6848 appToken = parent;
6849 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006850 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 mAppToken = appToken.appWindowToken;
6852
6853 mSurface = null;
6854 mRequestedWidth = 0;
6855 mRequestedHeight = 0;
6856 mLastRequestedWidth = 0;
6857 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006858 mXOffset = 0;
6859 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 mLayer = 0;
6861 mAnimLayer = 0;
6862 mLastLayer = 0;
6863 }
6864
6865 void attach() {
6866 if (localLOGV) Log.v(
6867 TAG, "Attaching " + this + " token=" + mToken
6868 + ", list=" + mToken.windows);
6869 mSession.windowAddedLocked();
6870 }
6871
6872 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6873 mHaveFrame = true;
6874
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006875 final Rect container = mContainingFrame;
6876 container.set(pf);
6877
6878 final Rect display = mDisplayFrame;
6879 display.set(df);
6880
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006881 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006882 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006883 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6884 display.intersect(mCompatibleScreenFrame);
6885 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006886 }
6887
6888 final int pw = container.right - container.left;
6889 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890
6891 int w,h;
6892 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6893 w = mAttrs.width < 0 ? pw : mAttrs.width;
6894 h = mAttrs.height< 0 ? ph : mAttrs.height;
6895 } else {
6896 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6897 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6898 }
Romain Guy06882f82009-06-10 13:36:04 -07006899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 final Rect content = mContentFrame;
6901 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 final Rect visible = mVisibleFrame;
6904 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006907 final int fw = frame.width();
6908 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6911 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6912
6913 Gravity.apply(mAttrs.gravity, w, h, container,
6914 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6915 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6916
6917 //System.out.println("Out: " + mFrame);
6918
6919 // Now make sure the window fits in the overall display.
6920 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 // Make sure the content and visible frames are inside of the
6923 // final window frame.
6924 if (content.left < frame.left) content.left = frame.left;
6925 if (content.top < frame.top) content.top = frame.top;
6926 if (content.right > frame.right) content.right = frame.right;
6927 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6928 if (visible.left < frame.left) visible.left = frame.left;
6929 if (visible.top < frame.top) visible.top = frame.top;
6930 if (visible.right > frame.right) visible.right = frame.right;
6931 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 final Rect contentInsets = mContentInsets;
6934 contentInsets.left = content.left-frame.left;
6935 contentInsets.top = content.top-frame.top;
6936 contentInsets.right = frame.right-content.right;
6937 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 final Rect visibleInsets = mVisibleInsets;
6940 visibleInsets.left = visible.left-frame.left;
6941 visibleInsets.top = visible.top-frame.top;
6942 visibleInsets.right = frame.right-visible.right;
6943 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006944
Dianne Hackborn284ac932009-08-28 10:34:25 -07006945 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6946 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006947 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006948 }
6949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 if (localLOGV) {
6951 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6952 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6953 Log.v(TAG, "Resolving (mRequestedWidth="
6954 + mRequestedWidth + ", mRequestedheight="
6955 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6956 + "): frame=" + mFrame.toShortString()
6957 + " ci=" + contentInsets.toShortString()
6958 + " vi=" + visibleInsets.toShortString());
6959 //}
6960 }
6961 }
Romain Guy06882f82009-06-10 13:36:04 -07006962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 public Rect getFrameLw() {
6964 return mFrame;
6965 }
6966
6967 public Rect getShownFrameLw() {
6968 return mShownFrame;
6969 }
6970
6971 public Rect getDisplayFrameLw() {
6972 return mDisplayFrame;
6973 }
6974
6975 public Rect getContentFrameLw() {
6976 return mContentFrame;
6977 }
6978
6979 public Rect getVisibleFrameLw() {
6980 return mVisibleFrame;
6981 }
6982
6983 public boolean getGivenInsetsPendingLw() {
6984 return mGivenInsetsPending;
6985 }
6986
6987 public Rect getGivenContentInsetsLw() {
6988 return mGivenContentInsets;
6989 }
Romain Guy06882f82009-06-10 13:36:04 -07006990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006991 public Rect getGivenVisibleInsetsLw() {
6992 return mGivenVisibleInsets;
6993 }
Romain Guy06882f82009-06-10 13:36:04 -07006994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 public WindowManager.LayoutParams getAttrs() {
6996 return mAttrs;
6997 }
6998
6999 public int getSurfaceLayer() {
7000 return mLayer;
7001 }
Romain Guy06882f82009-06-10 13:36:04 -07007002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 public IApplicationToken getAppToken() {
7004 return mAppToken != null ? mAppToken.appToken : null;
7005 }
7006
7007 public boolean hasAppShownWindows() {
7008 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7009 }
7010
7011 public boolean hasAppStartingIcon() {
7012 return mAppToken != null ? (mAppToken.startingData != null) : false;
7013 }
7014
7015 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7016 return mAppToken != null ? mAppToken.startingWindow : null;
7017 }
7018
7019 public void setAnimation(Animation anim) {
7020 if (localLOGV) Log.v(
7021 TAG, "Setting animation in " + this + ": " + anim);
7022 mAnimating = false;
7023 mLocalAnimating = false;
7024 mAnimation = anim;
7025 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7026 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7027 }
7028
7029 public void clearAnimation() {
7030 if (mAnimation != null) {
7031 mAnimating = true;
7032 mLocalAnimating = false;
7033 mAnimation = null;
7034 }
7035 }
Romain Guy06882f82009-06-10 13:36:04 -07007036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 Surface createSurfaceLocked() {
7038 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007039 mReportDestroySurface = false;
7040 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 mDrawPending = true;
7042 mCommitDrawPending = false;
7043 mReadyToShow = false;
7044 if (mAppToken != null) {
7045 mAppToken.allDrawn = false;
7046 }
7047
7048 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007049 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 flags |= Surface.PUSH_BUFFERS;
7051 }
7052
7053 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7054 flags |= Surface.SECURE;
7055 }
7056 if (DEBUG_VISIBILITY) Log.v(
7057 TAG, "Creating surface in session "
7058 + mSession.mSurfaceSession + " window " + this
7059 + " w=" + mFrame.width()
7060 + " h=" + mFrame.height() + " format="
7061 + mAttrs.format + " flags=" + flags);
7062
7063 int w = mFrame.width();
7064 int h = mFrame.height();
7065 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7066 // for a scaled surface, we always want the requested
7067 // size.
7068 w = mRequestedWidth;
7069 h = mRequestedHeight;
7070 }
7071
7072 try {
7073 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007074 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007075 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007076 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7077 + mSurface + " IN SESSION "
7078 + mSession.mSurfaceSession
7079 + ": pid=" + mSession.mPid + " format="
7080 + mAttrs.format + " flags=0x"
7081 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 } catch (Surface.OutOfResourcesException e) {
7083 Log.w(TAG, "OutOfResourcesException creating surface");
7084 reclaimSomeSurfaceMemoryLocked(this, "create");
7085 return null;
7086 } catch (Exception e) {
7087 Log.e(TAG, "Exception creating surface", e);
7088 return null;
7089 }
Romain Guy06882f82009-06-10 13:36:04 -07007090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 if (localLOGV) Log.v(
7092 TAG, "Got surface: " + mSurface
7093 + ", set left=" + mFrame.left + " top=" + mFrame.top
7094 + ", animLayer=" + mAnimLayer);
7095 if (SHOW_TRANSACTIONS) {
7096 Log.i(TAG, ">>> OPEN TRANSACTION");
7097 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7098 + mAttrs.getTitle() + ") pos=(" +
7099 mFrame.left + "," + mFrame.top + ") (" +
7100 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7101 mAnimLayer + " HIDE");
7102 }
7103 Surface.openTransaction();
7104 try {
7105 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007106 mSurface.setPosition(mFrame.left + mXOffset,
7107 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 mSurface.setLayer(mAnimLayer);
7109 mSurface.hide();
7110 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007111 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7112 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 mSurface.setFlags(Surface.SURFACE_DITHER,
7114 Surface.SURFACE_DITHER);
7115 }
7116 } catch (RuntimeException e) {
7117 Log.w(TAG, "Error creating surface in " + w, e);
7118 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7119 }
7120 mLastHidden = true;
7121 } finally {
7122 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7123 Surface.closeTransaction();
7124 }
7125 if (localLOGV) Log.v(
7126 TAG, "Created surface " + this);
7127 }
7128 return mSurface;
7129 }
Romain Guy06882f82009-06-10 13:36:04 -07007130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 void destroySurfaceLocked() {
7132 // Window is no longer on-screen, so can no longer receive
7133 // key events... if we were waiting for it to finish
7134 // handling a key event, the wait is over!
7135 mKeyWaiter.finishedKey(mSession, mClient, true,
7136 KeyWaiter.RETURN_NOTHING);
7137 mKeyWaiter.releasePendingPointerLocked(mSession);
7138 mKeyWaiter.releasePendingTrackballLocked(mSession);
7139
7140 if (mAppToken != null && this == mAppToken.startingWindow) {
7141 mAppToken.startingDisplayed = false;
7142 }
Romain Guy06882f82009-06-10 13:36:04 -07007143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007144 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007145 mDrawPending = false;
7146 mCommitDrawPending = false;
7147 mReadyToShow = false;
7148
7149 int i = mChildWindows.size();
7150 while (i > 0) {
7151 i--;
7152 WindowState c = (WindowState)mChildWindows.get(i);
7153 c.mAttachedHidden = true;
7154 }
7155
7156 if (mReportDestroySurface) {
7157 mReportDestroySurface = false;
7158 mSurfacePendingDestroy = true;
7159 try {
7160 mClient.dispatchGetNewSurface();
7161 // We'll really destroy on the next time around.
7162 return;
7163 } catch (RemoteException e) {
7164 }
7165 }
7166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007168 if (DEBUG_VISIBILITY) {
7169 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007170 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007171 Log.w(TAG, "Window " + this + " destroying surface "
7172 + mSurface + ", session " + mSession, e);
7173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 if (SHOW_TRANSACTIONS) {
7175 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007176 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7178 + mAttrs.getTitle() + ")", ex);
7179 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007180 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007181 } catch (RuntimeException e) {
7182 Log.w(TAG, "Exception thrown when destroying Window " + this
7183 + " surface " + mSurface + " session " + mSession
7184 + ": " + e.toString());
7185 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007187 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 }
7189 }
7190
7191 boolean finishDrawingLocked() {
7192 if (mDrawPending) {
7193 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7194 TAG, "finishDrawingLocked: " + mSurface);
7195 mCommitDrawPending = true;
7196 mDrawPending = false;
7197 return true;
7198 }
7199 return false;
7200 }
7201
7202 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007203 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7205 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007206 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 }
7208 mCommitDrawPending = false;
7209 mReadyToShow = true;
7210 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7211 final AppWindowToken atoken = mAppToken;
7212 if (atoken == null || atoken.allDrawn || starting) {
7213 performShowLocked();
7214 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007215 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 }
7217
7218 // This must be called while inside a transaction.
7219 boolean performShowLocked() {
7220 if (DEBUG_VISIBILITY) {
7221 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007222 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007223 Log.v(TAG, "performShow on " + this
7224 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7225 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7226 }
7227 if (mReadyToShow && isReadyForDisplay()) {
7228 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7229 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7230 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7231 + " during animation: policyVis=" + mPolicyVisibility
7232 + " attHidden=" + mAttachedHidden
7233 + " tok.hiddenRequested="
7234 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007235 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 + (mAppToken != null ? mAppToken.hidden : false)
7237 + " animating=" + mAnimating
7238 + " tok animating="
7239 + (mAppToken != null ? mAppToken.animating : false));
7240 if (!showSurfaceRobustlyLocked(this)) {
7241 return false;
7242 }
7243 mLastAlpha = -1;
7244 mHasDrawn = true;
7245 mLastHidden = false;
7246 mReadyToShow = false;
7247 enableScreenIfNeededLocked();
7248
7249 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007251 int i = mChildWindows.size();
7252 while (i > 0) {
7253 i--;
7254 WindowState c = (WindowState)mChildWindows.get(i);
7255 if (c.mSurface != null && c.mAttachedHidden) {
7256 c.mAttachedHidden = false;
7257 c.performShowLocked();
7258 }
7259 }
Romain Guy06882f82009-06-10 13:36:04 -07007260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007261 if (mAttrs.type != TYPE_APPLICATION_STARTING
7262 && mAppToken != null) {
7263 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007264
7265 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007266 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7267 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007268 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007269 // If this initial window is animating, stop it -- we
7270 // will do an animation to reveal it from behind the
7271 // starting window, so there is no need for it to also
7272 // be doing its own stuff.
7273 if (mAnimation != null) {
7274 mAnimation = null;
7275 // Make sure we clean up the animation.
7276 mAnimating = true;
7277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 mFinishedStarting.add(mAppToken);
7279 mH.sendEmptyMessage(H.FINISHED_STARTING);
7280 }
7281 mAppToken.updateReportedVisibilityLocked();
7282 }
7283 }
7284 return true;
7285 }
Romain Guy06882f82009-06-10 13:36:04 -07007286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 // This must be called while inside a transaction. Returns true if
7288 // there is more animation to run.
7289 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7290 if (!mDisplayFrozen) {
7291 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7294 mHasTransformation = true;
7295 mHasLocalTransformation = true;
7296 if (!mLocalAnimating) {
7297 if (DEBUG_ANIM) Log.v(
7298 TAG, "Starting animation in " + this +
7299 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7300 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7301 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7302 mAnimation.setStartTime(currentTime);
7303 mLocalAnimating = true;
7304 mAnimating = true;
7305 }
7306 mTransformation.clear();
7307 final boolean more = mAnimation.getTransformation(
7308 currentTime, mTransformation);
7309 if (DEBUG_ANIM) Log.v(
7310 TAG, "Stepped animation in " + this +
7311 ": more=" + more + ", xform=" + mTransformation);
7312 if (more) {
7313 // we're not done!
7314 return true;
7315 }
7316 if (DEBUG_ANIM) Log.v(
7317 TAG, "Finished animation in " + this +
7318 " @ " + currentTime);
7319 mAnimation = null;
7320 //WindowManagerService.this.dump();
7321 }
7322 mHasLocalTransformation = false;
7323 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007324 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 // When our app token is animating, we kind-of pretend like
7326 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7327 // part of this check means that we will only do this if
7328 // our window is not currently exiting, or it is not
7329 // locally animating itself. The idea being that one that
7330 // is exiting and doing a local animation should be removed
7331 // once that animation is done.
7332 mAnimating = true;
7333 mHasTransformation = true;
7334 mTransformation.clear();
7335 return false;
7336 } else if (mHasTransformation) {
7337 // Little trick to get through the path below to act like
7338 // we have finished an animation.
7339 mAnimating = true;
7340 } else if (isAnimating()) {
7341 mAnimating = true;
7342 }
7343 } else if (mAnimation != null) {
7344 // If the display is frozen, and there is a pending animation,
7345 // clear it and make sure we run the cleanup code.
7346 mAnimating = true;
7347 mLocalAnimating = true;
7348 mAnimation = null;
7349 }
Romain Guy06882f82009-06-10 13:36:04 -07007350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 if (!mAnimating && !mLocalAnimating) {
7352 return false;
7353 }
7354
7355 if (DEBUG_ANIM) Log.v(
7356 TAG, "Animation done in " + this + ": exiting=" + mExiting
7357 + ", reportedVisible="
7358 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 mAnimating = false;
7361 mLocalAnimating = false;
7362 mAnimation = null;
7363 mAnimLayer = mLayer;
7364 if (mIsImWindow) {
7365 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007366 } else if (mIsWallpaper) {
7367 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007368 }
7369 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7370 + " anim layer: " + mAnimLayer);
7371 mHasTransformation = false;
7372 mHasLocalTransformation = false;
7373 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7374 mTransformation.clear();
7375 if (mHasDrawn
7376 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7377 && mAppToken != null
7378 && mAppToken.firstWindowDrawn
7379 && mAppToken.startingData != null) {
7380 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7381 + mToken + ": first real window done animating");
7382 mFinishedStarting.add(mAppToken);
7383 mH.sendEmptyMessage(H.FINISHED_STARTING);
7384 }
Romain Guy06882f82009-06-10 13:36:04 -07007385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 finishExit();
7387
7388 if (mAppToken != null) {
7389 mAppToken.updateReportedVisibilityLocked();
7390 }
7391
7392 return false;
7393 }
7394
7395 void finishExit() {
7396 if (DEBUG_ANIM) Log.v(
7397 TAG, "finishExit in " + this
7398 + ": exiting=" + mExiting
7399 + " remove=" + mRemoveOnExit
7400 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 final int N = mChildWindows.size();
7403 for (int i=0; i<N; i++) {
7404 ((WindowState)mChildWindows.get(i)).finishExit();
7405 }
Romain Guy06882f82009-06-10 13:36:04 -07007406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007407 if (!mExiting) {
7408 return;
7409 }
Romain Guy06882f82009-06-10 13:36:04 -07007410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 if (isWindowAnimating()) {
7412 return;
7413 }
7414
7415 if (localLOGV) Log.v(
7416 TAG, "Exit animation finished in " + this
7417 + ": remove=" + mRemoveOnExit);
7418 if (mSurface != null) {
7419 mDestroySurface.add(this);
7420 mDestroying = true;
7421 if (SHOW_TRANSACTIONS) Log.i(
7422 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7423 try {
7424 mSurface.hide();
7425 } catch (RuntimeException e) {
7426 Log.w(TAG, "Error hiding surface in " + this, e);
7427 }
7428 mLastHidden = true;
7429 mKeyWaiter.releasePendingPointerLocked(mSession);
7430 }
7431 mExiting = false;
7432 if (mRemoveOnExit) {
7433 mPendingRemove.add(this);
7434 mRemoveOnExit = false;
7435 }
7436 }
Romain Guy06882f82009-06-10 13:36:04 -07007437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7439 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7440 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7441 if (dtdx < -.000001f || dtdx > .000001f) return false;
7442 if (dsdy < -.000001f || dsdy > .000001f) return false;
7443 return true;
7444 }
Romain Guy06882f82009-06-10 13:36:04 -07007445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 void computeShownFrameLocked() {
7447 final boolean selfTransformation = mHasLocalTransformation;
7448 Transformation attachedTransformation =
7449 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7450 ? mAttachedWindow.mTransformation : null;
7451 Transformation appTransformation =
7452 (mAppToken != null && mAppToken.hasTransformation)
7453 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007454
7455 // Wallpapers are animated based on the "real" window they
7456 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007457 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007458 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007459 if (mWallpaperTarget.mHasLocalTransformation &&
7460 mWallpaperTarget.mAnimation != null &&
7461 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007462 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007463 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7464 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7465 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007466 }
7467 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007468 mWallpaperTarget.mAppToken.hasTransformation &&
7469 mWallpaperTarget.mAppToken.animation != null &&
7470 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007471 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007472 if (DEBUG_WALLPAPER && appTransformation != null) {
7473 Log.v(TAG, "WP target app xform: " + appTransformation);
7474 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007475 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007476 }
7477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 if (selfTransformation || attachedTransformation != null
7479 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007480 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 final Rect frame = mFrame;
7482 final float tmpFloats[] = mTmpFloats;
7483 final Matrix tmpMatrix = mTmpMatrix;
7484
7485 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007486 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007488 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007490 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007491 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007492 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 }
7494 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007495 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 }
7497
7498 // "convert" it into SurfaceFlinger's format
7499 // (a 2x2 matrix + an offset)
7500 // Here we must not transform the position of the surface
7501 // since it is already included in the transformation.
7502 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007504 tmpMatrix.getValues(tmpFloats);
7505 mDsDx = tmpFloats[Matrix.MSCALE_X];
7506 mDtDx = tmpFloats[Matrix.MSKEW_X];
7507 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7508 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007509 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7510 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 int w = frame.width();
7512 int h = frame.height();
7513 mShownFrame.set(x, y, x+w, y+h);
7514
7515 // Now set the alpha... but because our current hardware
7516 // can't do alpha transformation on a non-opaque surface,
7517 // turn it off if we are running an animation that is also
7518 // transforming since it is more important to have that
7519 // animation be smooth.
7520 mShownAlpha = mAlpha;
7521 if (!mLimitedAlphaCompositing
7522 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7523 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7524 && x == frame.left && y == frame.top))) {
7525 //Log.i(TAG, "Applying alpha transform");
7526 if (selfTransformation) {
7527 mShownAlpha *= mTransformation.getAlpha();
7528 }
7529 if (attachedTransformation != null) {
7530 mShownAlpha *= attachedTransformation.getAlpha();
7531 }
7532 if (appTransformation != null) {
7533 mShownAlpha *= appTransformation.getAlpha();
7534 }
7535 } else {
7536 //Log.i(TAG, "Not applying alpha transform");
7537 }
Romain Guy06882f82009-06-10 13:36:04 -07007538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 if (localLOGV) Log.v(
7540 TAG, "Continuing animation in " + this +
7541 ": " + mShownFrame +
7542 ", alpha=" + mTransformation.getAlpha());
7543 return;
7544 }
Romain Guy06882f82009-06-10 13:36:04 -07007545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007546 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007547 if (mXOffset != 0 || mYOffset != 0) {
7548 mShownFrame.offset(mXOffset, mYOffset);
7549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007550 mShownAlpha = mAlpha;
7551 mDsDx = 1;
7552 mDtDx = 0;
7553 mDsDy = 0;
7554 mDtDy = 1;
7555 }
Romain Guy06882f82009-06-10 13:36:04 -07007556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007557 /**
7558 * Is this window visible? It is not visible if there is no
7559 * surface, or we are in the process of running an exit animation
7560 * that will remove the surface, or its app token has been hidden.
7561 */
7562 public boolean isVisibleLw() {
7563 final AppWindowToken atoken = mAppToken;
7564 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7565 && (atoken == null || !atoken.hiddenRequested)
7566 && !mExiting && !mDestroying;
7567 }
7568
7569 /**
7570 * Is this window visible, ignoring its app token? It is not visible
7571 * if there is no surface, or we are in the process of running an exit animation
7572 * that will remove the surface.
7573 */
7574 public boolean isWinVisibleLw() {
7575 final AppWindowToken atoken = mAppToken;
7576 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7577 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7578 && !mExiting && !mDestroying;
7579 }
7580
7581 /**
7582 * The same as isVisible(), but follows the current hidden state of
7583 * the associated app token, not the pending requested hidden state.
7584 */
7585 boolean isVisibleNow() {
7586 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007587 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 }
7589
7590 /**
7591 * Same as isVisible(), but we also count it as visible between the
7592 * call to IWindowSession.add() and the first relayout().
7593 */
7594 boolean isVisibleOrAdding() {
7595 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007596 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007597 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7598 && mPolicyVisibility && !mAttachedHidden
7599 && (atoken == null || !atoken.hiddenRequested)
7600 && !mExiting && !mDestroying;
7601 }
7602
7603 /**
7604 * Is this window currently on-screen? It is on-screen either if it
7605 * is visible or it is currently running an animation before no longer
7606 * being visible.
7607 */
7608 boolean isOnScreen() {
7609 final AppWindowToken atoken = mAppToken;
7610 if (atoken != null) {
7611 return mSurface != null && mPolicyVisibility && !mDestroying
7612 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007613 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 } else {
7615 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007616 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007617 }
7618 }
Romain Guy06882f82009-06-10 13:36:04 -07007619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007620 /**
7621 * Like isOnScreen(), but we don't return true if the window is part
7622 * of a transition that has not yet been started.
7623 */
7624 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007625 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007626 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007627 return false;
7628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007630 final boolean animating = atoken != null
7631 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007633 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7634 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007635 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 }
7637
7638 /** Is the window or its container currently animating? */
7639 boolean isAnimating() {
7640 final WindowState attached = mAttachedWindow;
7641 final AppWindowToken atoken = mAppToken;
7642 return mAnimation != null
7643 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007644 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 (atoken.animation != null
7646 || atoken.inPendingTransaction));
7647 }
7648
7649 /** Is this window currently animating? */
7650 boolean isWindowAnimating() {
7651 return mAnimation != null;
7652 }
7653
7654 /**
7655 * Like isOnScreen, but returns false if the surface hasn't yet
7656 * been drawn.
7657 */
7658 public boolean isDisplayedLw() {
7659 final AppWindowToken atoken = mAppToken;
7660 return mSurface != null && mPolicyVisibility && !mDestroying
7661 && !mDrawPending && !mCommitDrawPending
7662 && ((!mAttachedHidden &&
7663 (atoken == null || !atoken.hiddenRequested))
7664 || mAnimating);
7665 }
7666
7667 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7668 boolean shownFrame, boolean onlyOpaque) {
7669 if (mSurface == null) {
7670 return false;
7671 }
7672 if (mAppToken != null && !mAppToken.appFullscreen) {
7673 return false;
7674 }
7675 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7676 return false;
7677 }
7678 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007679
7680 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7681 return frame.left <= mCompatibleScreenFrame.left &&
7682 frame.top <= mCompatibleScreenFrame.top &&
7683 frame.right >= mCompatibleScreenFrame.right &&
7684 frame.bottom >= mCompatibleScreenFrame.bottom;
7685 } else {
7686 return frame.left <= 0 && frame.top <= 0
7687 && frame.right >= screenWidth
7688 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 }
Romain Guy06882f82009-06-10 13:36:04 -07007691
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007692 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007693 * Return true if the window is opaque and fully drawn. This indicates
7694 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007695 */
7696 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007697 return (mAttrs.format == PixelFormat.OPAQUE
7698 || mAttrs.type == TYPE_WALLPAPER)
7699 && mSurface != null && mAnimation == null
7700 && (mAppToken == null || mAppToken.animation == null)
7701 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007702 }
7703
7704 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7705 return
7706 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007707 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7708 // only if it's visible
7709 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007710 // and only if the application fills the compatible screen
7711 mFrame.left <= mCompatibleScreenFrame.left &&
7712 mFrame.top <= mCompatibleScreenFrame.top &&
7713 mFrame.right >= mCompatibleScreenFrame.right &&
7714 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007715 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007716 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007717 }
7718
7719 boolean isFullscreen(int screenWidth, int screenHeight) {
7720 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007721 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 }
7723
7724 void removeLocked() {
7725 if (mAttachedWindow != null) {
7726 mAttachedWindow.mChildWindows.remove(this);
7727 }
7728 destroySurfaceLocked();
7729 mSession.windowRemovedLocked();
7730 try {
7731 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7732 } catch (RuntimeException e) {
7733 // Ignore if it has already been removed (usually because
7734 // we are doing this as part of processing a death note.)
7735 }
7736 }
7737
7738 private class DeathRecipient implements IBinder.DeathRecipient {
7739 public void binderDied() {
7740 try {
7741 synchronized(mWindowMap) {
7742 WindowState win = windowForClientLocked(mSession, mClient);
7743 Log.i(TAG, "WIN DEATH: " + win);
7744 if (win != null) {
7745 removeWindowLocked(mSession, win);
7746 }
7747 }
7748 } catch (IllegalArgumentException ex) {
7749 // This will happen if the window has already been
7750 // removed.
7751 }
7752 }
7753 }
7754
7755 /** Returns true if this window desires key events. */
7756 public final boolean canReceiveKeys() {
7757 return isVisibleOrAdding()
7758 && (mViewVisibility == View.VISIBLE)
7759 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7760 }
7761
7762 public boolean hasDrawnLw() {
7763 return mHasDrawn;
7764 }
7765
7766 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007767 return showLw(doAnimation, true);
7768 }
7769
7770 boolean showLw(boolean doAnimation, boolean requestAnim) {
7771 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7772 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007774 mPolicyVisibility = true;
7775 mPolicyVisibilityAfterAnim = true;
7776 if (doAnimation) {
7777 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7778 }
7779 if (requestAnim) {
7780 requestAnimationLocked(0);
7781 }
7782 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 }
7784
7785 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007786 return hideLw(doAnimation, true);
7787 }
7788
7789 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007790 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7791 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007792 if (!current) {
7793 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007795 if (doAnimation) {
7796 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7797 if (mAnimation == null) {
7798 doAnimation = false;
7799 }
7800 }
7801 if (doAnimation) {
7802 mPolicyVisibilityAfterAnim = false;
7803 } else {
7804 mPolicyVisibilityAfterAnim = false;
7805 mPolicyVisibility = false;
7806 }
7807 if (requestAnim) {
7808 requestAnimationLocked(0);
7809 }
7810 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 }
7812
7813 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007814 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007815
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007816 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7817 pw.print(" mClient="); pw.println(mClient.asBinder());
7818 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7819 if (mAttachedWindow != null || mLayoutAttached) {
7820 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7821 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7822 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007823 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7824 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7825 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007826 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7827 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007828 }
7829 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7830 pw.print(" mSubLayer="); pw.print(mSubLayer);
7831 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7832 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7833 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7834 pw.print("="); pw.print(mAnimLayer);
7835 pw.print(" mLastLayer="); pw.println(mLastLayer);
7836 if (mSurface != null) {
7837 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7838 }
7839 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7840 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7841 if (mAppToken != null) {
7842 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7843 }
7844 if (mTargetAppToken != null) {
7845 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7846 }
7847 pw.print(prefix); pw.print("mViewVisibility=0x");
7848 pw.print(Integer.toHexString(mViewVisibility));
7849 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007850 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7851 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007852 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7853 pw.print(prefix); pw.print("mPolicyVisibility=");
7854 pw.print(mPolicyVisibility);
7855 pw.print(" mPolicyVisibilityAfterAnim=");
7856 pw.print(mPolicyVisibilityAfterAnim);
7857 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7858 }
7859 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007860 pw.print(" h="); pw.println(mRequestedHeight);
7861 if (mXOffset != 0 || mYOffset != 0) {
7862 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7863 pw.print(" y="); pw.println(mYOffset);
7864 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007865 pw.print(prefix); pw.print("mGivenContentInsets=");
7866 mGivenContentInsets.printShortString(pw);
7867 pw.print(" mGivenVisibleInsets=");
7868 mGivenVisibleInsets.printShortString(pw);
7869 pw.println();
7870 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7871 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7872 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7873 }
7874 pw.print(prefix); pw.print("mShownFrame=");
7875 mShownFrame.printShortString(pw);
7876 pw.print(" last="); mLastShownFrame.printShortString(pw);
7877 pw.println();
7878 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7879 pw.print(" last="); mLastFrame.printShortString(pw);
7880 pw.println();
7881 pw.print(prefix); pw.print("mContainingFrame=");
7882 mContainingFrame.printShortString(pw);
7883 pw.print(" mDisplayFrame=");
7884 mDisplayFrame.printShortString(pw);
7885 pw.println();
7886 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7887 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7888 pw.println();
7889 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7890 pw.print(" last="); mLastContentInsets.printShortString(pw);
7891 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7892 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7893 pw.println();
7894 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7895 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7896 pw.print(" mAlpha="); pw.print(mAlpha);
7897 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7898 }
7899 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7900 || mAnimation != null) {
7901 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7902 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7903 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7904 pw.print(" mAnimation="); pw.println(mAnimation);
7905 }
7906 if (mHasTransformation || mHasLocalTransformation) {
7907 pw.print(prefix); pw.print("XForm: has=");
7908 pw.print(mHasTransformation);
7909 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7910 pw.print(" "); mTransformation.printShortString(pw);
7911 pw.println();
7912 }
7913 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7914 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7915 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7916 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7917 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7918 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7919 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7920 pw.print(" mDestroying="); pw.print(mDestroying);
7921 pw.print(" mRemoved="); pw.println(mRemoved);
7922 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007923 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007924 pw.print(prefix); pw.print("mOrientationChanging=");
7925 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007926 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7927 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007928 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007929 if (mHScale != 1 || mVScale != 1) {
7930 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7931 pw.print(" mVScale="); pw.println(mVScale);
7932 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007933 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007934 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7935 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 }
7938
7939 @Override
7940 public String toString() {
7941 return "Window{"
7942 + Integer.toHexString(System.identityHashCode(this))
7943 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7944 }
7945 }
Romain Guy06882f82009-06-10 13:36:04 -07007946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 // -------------------------------------------------------------
7948 // Window Token State
7949 // -------------------------------------------------------------
7950
7951 class WindowToken {
7952 // The actual token.
7953 final IBinder token;
7954
7955 // The type of window this token is for, as per WindowManager.LayoutParams.
7956 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 // Set if this token was explicitly added by a client, so should
7959 // not be removed when all windows are removed.
7960 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007961
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007962 // For printing.
7963 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 // If this is an AppWindowToken, this is non-null.
7966 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 // All of the windows associated with this token.
7969 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7970
7971 // Is key dispatching paused for this token?
7972 boolean paused = false;
7973
7974 // Should this token's windows be hidden?
7975 boolean hidden;
7976
7977 // Temporary for finding which tokens no longer have visible windows.
7978 boolean hasVisible;
7979
Dianne Hackborna8f60182009-09-01 19:01:50 -07007980 // Set to true when this token is in a pending transaction where it
7981 // will be shown.
7982 boolean waitingToShow;
7983
7984 // Set to true when this token is in a pending transaction where it
7985 // will be hidden.
7986 boolean waitingToHide;
7987
7988 // Set to true when this token is in a pending transaction where its
7989 // windows will be put to the bottom of the list.
7990 boolean sendingToBottom;
7991
7992 // Set to true when this token is in a pending transaction where its
7993 // windows will be put to the top of the list.
7994 boolean sendingToTop;
7995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 WindowToken(IBinder _token, int type, boolean _explicit) {
7997 token = _token;
7998 windowType = type;
7999 explicit = _explicit;
8000 }
8001
8002 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008003 pw.print(prefix); pw.print("token="); pw.println(token);
8004 pw.print(prefix); pw.print("windows="); pw.println(windows);
8005 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8006 pw.print(" hidden="); pw.print(hidden);
8007 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008008 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8009 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8010 pw.print(" waitingToHide="); pw.print(waitingToHide);
8011 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8012 pw.print(" sendingToTop="); pw.println(sendingToTop);
8013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 }
8015
8016 @Override
8017 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008018 if (stringName == null) {
8019 StringBuilder sb = new StringBuilder();
8020 sb.append("WindowToken{");
8021 sb.append(Integer.toHexString(System.identityHashCode(this)));
8022 sb.append(" token="); sb.append(token); sb.append('}');
8023 stringName = sb.toString();
8024 }
8025 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026 }
8027 };
8028
8029 class AppWindowToken extends WindowToken {
8030 // Non-null only for application tokens.
8031 final IApplicationToken appToken;
8032
8033 // All of the windows and child windows that are included in this
8034 // application token. Note this list is NOT sorted!
8035 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8036
8037 int groupId = -1;
8038 boolean appFullscreen;
8039 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 // These are used for determining when all windows associated with
8042 // an activity have been drawn, so they can be made visible together
8043 // at the same time.
8044 int lastTransactionSequence = mTransactionSequence-1;
8045 int numInterestingWindows;
8046 int numDrawnWindows;
8047 boolean inPendingTransaction;
8048 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 // Is this token going to be hidden in a little while? If so, it
8051 // won't be taken into account for setting the screen orientation.
8052 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 // Is this window's surface needed? This is almost like hidden, except
8055 // it will sometimes be true a little earlier: when the token has
8056 // been shown, but is still waiting for its app transition to execute
8057 // before making its windows shown.
8058 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 // Have we told the window clients to hide themselves?
8061 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008063 // Last visibility state we reported to the app token.
8064 boolean reportedVisible;
8065
8066 // Set to true when the token has been removed from the window mgr.
8067 boolean removed;
8068
8069 // Have we been asked to have this token keep the screen frozen?
8070 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 boolean animating;
8073 Animation animation;
8074 boolean hasTransformation;
8075 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 // Offset to the window of all layers in the token, for use by
8078 // AppWindowToken animations.
8079 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 // Information about an application starting window if displayed.
8082 StartingData startingData;
8083 WindowState startingWindow;
8084 View startingView;
8085 boolean startingDisplayed;
8086 boolean startingMoved;
8087 boolean firstWindowDrawn;
8088
8089 AppWindowToken(IApplicationToken _token) {
8090 super(_token.asBinder(),
8091 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8092 appWindowToken = this;
8093 appToken = _token;
8094 }
Romain Guy06882f82009-06-10 13:36:04 -07008095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 public void setAnimation(Animation anim) {
8097 if (localLOGV) Log.v(
8098 TAG, "Setting animation in " + this + ": " + anim);
8099 animation = anim;
8100 animating = false;
8101 anim.restrictDuration(MAX_ANIMATION_DURATION);
8102 anim.scaleCurrentDuration(mTransitionAnimationScale);
8103 int zorder = anim.getZAdjustment();
8104 int adj = 0;
8105 if (zorder == Animation.ZORDER_TOP) {
8106 adj = TYPE_LAYER_OFFSET;
8107 } else if (zorder == Animation.ZORDER_BOTTOM) {
8108 adj = -TYPE_LAYER_OFFSET;
8109 }
Romain Guy06882f82009-06-10 13:36:04 -07008110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 if (animLayerAdjustment != adj) {
8112 animLayerAdjustment = adj;
8113 updateLayers();
8114 }
8115 }
Romain Guy06882f82009-06-10 13:36:04 -07008116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 public void setDummyAnimation() {
8118 if (animation == null) {
8119 if (localLOGV) Log.v(
8120 TAG, "Setting dummy animation in " + this);
8121 animation = sDummyAnimation;
8122 }
8123 }
8124
8125 public void clearAnimation() {
8126 if (animation != null) {
8127 animation = null;
8128 animating = true;
8129 }
8130 }
Romain Guy06882f82009-06-10 13:36:04 -07008131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 void updateLayers() {
8133 final int N = allAppWindows.size();
8134 final int adj = animLayerAdjustment;
8135 for (int i=0; i<N; i++) {
8136 WindowState w = allAppWindows.get(i);
8137 w.mAnimLayer = w.mLayer + adj;
8138 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8139 + w.mAnimLayer);
8140 if (w == mInputMethodTarget) {
8141 setInputMethodAnimLayerAdjustment(adj);
8142 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008143 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008144 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008146 }
8147 }
Romain Guy06882f82009-06-10 13:36:04 -07008148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 void sendAppVisibilityToClients() {
8150 final int N = allAppWindows.size();
8151 for (int i=0; i<N; i++) {
8152 WindowState win = allAppWindows.get(i);
8153 if (win == startingWindow && clientHidden) {
8154 // Don't hide the starting window.
8155 continue;
8156 }
8157 try {
8158 if (DEBUG_VISIBILITY) Log.v(TAG,
8159 "Setting visibility of " + win + ": " + (!clientHidden));
8160 win.mClient.dispatchAppVisibility(!clientHidden);
8161 } catch (RemoteException e) {
8162 }
8163 }
8164 }
Romain Guy06882f82009-06-10 13:36:04 -07008165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 void showAllWindowsLocked() {
8167 final int NW = allAppWindows.size();
8168 for (int i=0; i<NW; i++) {
8169 WindowState w = allAppWindows.get(i);
8170 if (DEBUG_VISIBILITY) Log.v(TAG,
8171 "performing show on: " + w);
8172 w.performShowLocked();
8173 }
8174 }
Romain Guy06882f82009-06-10 13:36:04 -07008175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 // This must be called while inside a transaction.
8177 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8178 if (!mDisplayFrozen) {
8179 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181 if (animation == sDummyAnimation) {
8182 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008183 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008184 // when it is really time to animate, this will be set to
8185 // a real animation and the next call will execute normally.
8186 return false;
8187 }
Romain Guy06882f82009-06-10 13:36:04 -07008188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8190 if (!animating) {
8191 if (DEBUG_ANIM) Log.v(
8192 TAG, "Starting animation in " + this +
8193 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8194 + " scale=" + mTransitionAnimationScale
8195 + " allDrawn=" + allDrawn + " animating=" + animating);
8196 animation.initialize(dw, dh, dw, dh);
8197 animation.setStartTime(currentTime);
8198 animating = true;
8199 }
8200 transformation.clear();
8201 final boolean more = animation.getTransformation(
8202 currentTime, transformation);
8203 if (DEBUG_ANIM) Log.v(
8204 TAG, "Stepped animation in " + this +
8205 ": more=" + more + ", xform=" + transformation);
8206 if (more) {
8207 // we're done!
8208 hasTransformation = true;
8209 return true;
8210 }
8211 if (DEBUG_ANIM) Log.v(
8212 TAG, "Finished animation in " + this +
8213 " @ " + currentTime);
8214 animation = null;
8215 }
8216 } else if (animation != null) {
8217 // If the display is frozen, and there is a pending animation,
8218 // clear it and make sure we run the cleanup code.
8219 animating = true;
8220 animation = null;
8221 }
8222
8223 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 if (!animating) {
8226 return false;
8227 }
8228
8229 clearAnimation();
8230 animating = false;
8231 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8232 moveInputMethodWindowsIfNeededLocked(true);
8233 }
Romain Guy06882f82009-06-10 13:36:04 -07008234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 if (DEBUG_ANIM) Log.v(
8236 TAG, "Animation done in " + this
8237 + ": reportedVisible=" + reportedVisible);
8238
8239 transformation.clear();
8240 if (animLayerAdjustment != 0) {
8241 animLayerAdjustment = 0;
8242 updateLayers();
8243 }
Romain Guy06882f82009-06-10 13:36:04 -07008244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 final int N = windows.size();
8246 for (int i=0; i<N; i++) {
8247 ((WindowState)windows.get(i)).finishExit();
8248 }
8249 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 return false;
8252 }
8253
8254 void updateReportedVisibilityLocked() {
8255 if (appToken == null) {
8256 return;
8257 }
Romain Guy06882f82009-06-10 13:36:04 -07008258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 int numInteresting = 0;
8260 int numVisible = 0;
8261 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8264 final int N = allAppWindows.size();
8265 for (int i=0; i<N; i++) {
8266 WindowState win = allAppWindows.get(i);
8267 if (win == startingWindow || win.mAppFreezing) {
8268 continue;
8269 }
8270 if (DEBUG_VISIBILITY) {
8271 Log.v(TAG, "Win " + win + ": isDisplayed="
8272 + win.isDisplayedLw()
8273 + ", isAnimating=" + win.isAnimating());
8274 if (!win.isDisplayedLw()) {
8275 Log.v(TAG, "Not displayed: s=" + win.mSurface
8276 + " pv=" + win.mPolicyVisibility
8277 + " dp=" + win.mDrawPending
8278 + " cdp=" + win.mCommitDrawPending
8279 + " ah=" + win.mAttachedHidden
8280 + " th="
8281 + (win.mAppToken != null
8282 ? win.mAppToken.hiddenRequested : false)
8283 + " a=" + win.mAnimating);
8284 }
8285 }
8286 numInteresting++;
8287 if (win.isDisplayedLw()) {
8288 if (!win.isAnimating()) {
8289 numVisible++;
8290 }
8291 nowGone = false;
8292 } else if (win.isAnimating()) {
8293 nowGone = false;
8294 }
8295 }
Romain Guy06882f82009-06-10 13:36:04 -07008296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8298 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8299 + numInteresting + " visible=" + numVisible);
8300 if (nowVisible != reportedVisible) {
8301 if (DEBUG_VISIBILITY) Log.v(
8302 TAG, "Visibility changed in " + this
8303 + ": vis=" + nowVisible);
8304 reportedVisible = nowVisible;
8305 Message m = mH.obtainMessage(
8306 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8307 nowVisible ? 1 : 0,
8308 nowGone ? 1 : 0,
8309 this);
8310 mH.sendMessage(m);
8311 }
8312 }
Romain Guy06882f82009-06-10 13:36:04 -07008313
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008314 WindowState findMainWindow() {
8315 int j = windows.size();
8316 while (j > 0) {
8317 j--;
8318 WindowState win = windows.get(j);
8319 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8320 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8321 return win;
8322 }
8323 }
8324 return null;
8325 }
8326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 void dump(PrintWriter pw, String prefix) {
8328 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008329 if (appToken != null) {
8330 pw.print(prefix); pw.println("app=true");
8331 }
8332 if (allAppWindows.size() > 0) {
8333 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8334 }
8335 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008336 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008337 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8338 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8339 pw.print(" clientHidden="); pw.print(clientHidden);
8340 pw.print(" willBeHidden="); pw.print(willBeHidden);
8341 pw.print(" reportedVisible="); pw.println(reportedVisible);
8342 if (paused || freezingScreen) {
8343 pw.print(prefix); pw.print("paused="); pw.print(paused);
8344 pw.print(" freezingScreen="); pw.println(freezingScreen);
8345 }
8346 if (numInterestingWindows != 0 || numDrawnWindows != 0
8347 || inPendingTransaction || allDrawn) {
8348 pw.print(prefix); pw.print("numInterestingWindows=");
8349 pw.print(numInterestingWindows);
8350 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8351 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8352 pw.print(" allDrawn="); pw.println(allDrawn);
8353 }
8354 if (animating || animation != null) {
8355 pw.print(prefix); pw.print("animating="); pw.print(animating);
8356 pw.print(" animation="); pw.println(animation);
8357 }
8358 if (animLayerAdjustment != 0) {
8359 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8360 }
8361 if (hasTransformation) {
8362 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8363 pw.print(" transformation="); transformation.printShortString(pw);
8364 pw.println();
8365 }
8366 if (startingData != null || removed || firstWindowDrawn) {
8367 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8368 pw.print(" removed="); pw.print(removed);
8369 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8370 }
8371 if (startingWindow != null || startingView != null
8372 || startingDisplayed || startingMoved) {
8373 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8374 pw.print(" startingView="); pw.print(startingView);
8375 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8376 pw.print(" startingMoved"); pw.println(startingMoved);
8377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 }
8379
8380 @Override
8381 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008382 if (stringName == null) {
8383 StringBuilder sb = new StringBuilder();
8384 sb.append("AppWindowToken{");
8385 sb.append(Integer.toHexString(System.identityHashCode(this)));
8386 sb.append(" token="); sb.append(token); sb.append('}');
8387 stringName = sb.toString();
8388 }
8389 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 }
8391 }
Romain Guy06882f82009-06-10 13:36:04 -07008392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008393 // -------------------------------------------------------------
8394 // DummyAnimation
8395 // -------------------------------------------------------------
8396
8397 // This is an animation that does nothing: it just immediately finishes
8398 // itself every time it is called. It is used as a stub animation in cases
8399 // where we want to synchronize multiple things that may be animating.
8400 static final class DummyAnimation extends Animation {
8401 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8402 return false;
8403 }
8404 }
8405 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407 // -------------------------------------------------------------
8408 // Async Handler
8409 // -------------------------------------------------------------
8410
8411 static final class StartingData {
8412 final String pkg;
8413 final int theme;
8414 final CharSequence nonLocalizedLabel;
8415 final int labelRes;
8416 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8419 int _labelRes, int _icon) {
8420 pkg = _pkg;
8421 theme = _theme;
8422 nonLocalizedLabel = _nonLocalizedLabel;
8423 labelRes = _labelRes;
8424 icon = _icon;
8425 }
8426 }
8427
8428 private final class H extends Handler {
8429 public static final int REPORT_FOCUS_CHANGE = 2;
8430 public static final int REPORT_LOSING_FOCUS = 3;
8431 public static final int ANIMATE = 4;
8432 public static final int ADD_STARTING = 5;
8433 public static final int REMOVE_STARTING = 6;
8434 public static final int FINISHED_STARTING = 7;
8435 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8437 public static final int HOLD_SCREEN_CHANGED = 12;
8438 public static final int APP_TRANSITION_TIMEOUT = 13;
8439 public static final int PERSIST_ANIMATION_SCALE = 14;
8440 public static final int FORCE_GC = 15;
8441 public static final int ENABLE_SCREEN = 16;
8442 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008443 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008445 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 public H() {
8448 }
Romain Guy06882f82009-06-10 13:36:04 -07008449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 @Override
8451 public void handleMessage(Message msg) {
8452 switch (msg.what) {
8453 case REPORT_FOCUS_CHANGE: {
8454 WindowState lastFocus;
8455 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008457 synchronized(mWindowMap) {
8458 lastFocus = mLastFocus;
8459 newFocus = mCurrentFocus;
8460 if (lastFocus == newFocus) {
8461 // Focus is not changing, so nothing to do.
8462 return;
8463 }
8464 mLastFocus = newFocus;
8465 //Log.i(TAG, "Focus moving from " + lastFocus
8466 // + " to " + newFocus);
8467 if (newFocus != null && lastFocus != null
8468 && !newFocus.isDisplayedLw()) {
8469 //Log.i(TAG, "Delaying loss of focus...");
8470 mLosingFocus.add(lastFocus);
8471 lastFocus = null;
8472 }
8473 }
8474
8475 if (lastFocus != newFocus) {
8476 //System.out.println("Changing focus from " + lastFocus
8477 // + " to " + newFocus);
8478 if (newFocus != null) {
8479 try {
8480 //Log.i(TAG, "Gaining focus: " + newFocus);
8481 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8482 } catch (RemoteException e) {
8483 // Ignore if process has died.
8484 }
8485 }
8486
8487 if (lastFocus != null) {
8488 try {
8489 //Log.i(TAG, "Losing focus: " + lastFocus);
8490 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8491 } catch (RemoteException e) {
8492 // Ignore if process has died.
8493 }
8494 }
8495 }
8496 } break;
8497
8498 case REPORT_LOSING_FOCUS: {
8499 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008501 synchronized(mWindowMap) {
8502 losers = mLosingFocus;
8503 mLosingFocus = new ArrayList<WindowState>();
8504 }
8505
8506 final int N = losers.size();
8507 for (int i=0; i<N; i++) {
8508 try {
8509 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8510 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8511 } catch (RemoteException e) {
8512 // Ignore if process has died.
8513 }
8514 }
8515 } break;
8516
8517 case ANIMATE: {
8518 synchronized(mWindowMap) {
8519 mAnimationPending = false;
8520 performLayoutAndPlaceSurfacesLocked();
8521 }
8522 } break;
8523
8524 case ADD_STARTING: {
8525 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8526 final StartingData sd = wtoken.startingData;
8527
8528 if (sd == null) {
8529 // Animation has been canceled... do nothing.
8530 return;
8531 }
Romain Guy06882f82009-06-10 13:36:04 -07008532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8534 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 View view = null;
8537 try {
8538 view = mPolicy.addStartingWindow(
8539 wtoken.token, sd.pkg,
8540 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8541 sd.icon);
8542 } catch (Exception e) {
8543 Log.w(TAG, "Exception when adding starting window", e);
8544 }
8545
8546 if (view != null) {
8547 boolean abort = false;
8548
8549 synchronized(mWindowMap) {
8550 if (wtoken.removed || wtoken.startingData == null) {
8551 // If the window was successfully added, then
8552 // we need to remove it.
8553 if (wtoken.startingWindow != null) {
8554 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8555 "Aborted starting " + wtoken
8556 + ": removed=" + wtoken.removed
8557 + " startingData=" + wtoken.startingData);
8558 wtoken.startingWindow = null;
8559 wtoken.startingData = null;
8560 abort = true;
8561 }
8562 } else {
8563 wtoken.startingView = view;
8564 }
8565 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8566 "Added starting " + wtoken
8567 + ": startingWindow="
8568 + wtoken.startingWindow + " startingView="
8569 + wtoken.startingView);
8570 }
8571
8572 if (abort) {
8573 try {
8574 mPolicy.removeStartingWindow(wtoken.token, view);
8575 } catch (Exception e) {
8576 Log.w(TAG, "Exception when removing starting window", e);
8577 }
8578 }
8579 }
8580 } break;
8581
8582 case REMOVE_STARTING: {
8583 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8584 IBinder token = null;
8585 View view = null;
8586 synchronized (mWindowMap) {
8587 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8588 + wtoken + ": startingWindow="
8589 + wtoken.startingWindow + " startingView="
8590 + wtoken.startingView);
8591 if (wtoken.startingWindow != null) {
8592 view = wtoken.startingView;
8593 token = wtoken.token;
8594 wtoken.startingData = null;
8595 wtoken.startingView = null;
8596 wtoken.startingWindow = null;
8597 }
8598 }
8599 if (view != null) {
8600 try {
8601 mPolicy.removeStartingWindow(token, view);
8602 } catch (Exception e) {
8603 Log.w(TAG, "Exception when removing starting window", e);
8604 }
8605 }
8606 } break;
8607
8608 case FINISHED_STARTING: {
8609 IBinder token = null;
8610 View view = null;
8611 while (true) {
8612 synchronized (mWindowMap) {
8613 final int N = mFinishedStarting.size();
8614 if (N <= 0) {
8615 break;
8616 }
8617 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8618
8619 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8620 "Finished starting " + wtoken
8621 + ": startingWindow=" + wtoken.startingWindow
8622 + " startingView=" + wtoken.startingView);
8623
8624 if (wtoken.startingWindow == null) {
8625 continue;
8626 }
8627
8628 view = wtoken.startingView;
8629 token = wtoken.token;
8630 wtoken.startingData = null;
8631 wtoken.startingView = null;
8632 wtoken.startingWindow = null;
8633 }
8634
8635 try {
8636 mPolicy.removeStartingWindow(token, view);
8637 } catch (Exception e) {
8638 Log.w(TAG, "Exception when removing starting window", e);
8639 }
8640 }
8641 } break;
8642
8643 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8644 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8645
8646 boolean nowVisible = msg.arg1 != 0;
8647 boolean nowGone = msg.arg2 != 0;
8648
8649 try {
8650 if (DEBUG_VISIBILITY) Log.v(
8651 TAG, "Reporting visible in " + wtoken
8652 + " visible=" + nowVisible
8653 + " gone=" + nowGone);
8654 if (nowVisible) {
8655 wtoken.appToken.windowsVisible();
8656 } else {
8657 wtoken.appToken.windowsGone();
8658 }
8659 } catch (RemoteException ex) {
8660 }
8661 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 case WINDOW_FREEZE_TIMEOUT: {
8664 synchronized (mWindowMap) {
8665 Log.w(TAG, "Window freeze timeout expired.");
8666 int i = mWindows.size();
8667 while (i > 0) {
8668 i--;
8669 WindowState w = (WindowState)mWindows.get(i);
8670 if (w.mOrientationChanging) {
8671 w.mOrientationChanging = false;
8672 Log.w(TAG, "Force clearing orientation change: " + w);
8673 }
8674 }
8675 performLayoutAndPlaceSurfacesLocked();
8676 }
8677 break;
8678 }
Romain Guy06882f82009-06-10 13:36:04 -07008679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008680 case HOLD_SCREEN_CHANGED: {
8681 Session oldHold;
8682 Session newHold;
8683 synchronized (mWindowMap) {
8684 oldHold = mLastReportedHold;
8685 newHold = (Session)msg.obj;
8686 mLastReportedHold = newHold;
8687 }
Romain Guy06882f82009-06-10 13:36:04 -07008688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008689 if (oldHold != newHold) {
8690 try {
8691 if (oldHold != null) {
8692 mBatteryStats.noteStopWakelock(oldHold.mUid,
8693 "window",
8694 BatteryStats.WAKE_TYPE_WINDOW);
8695 }
8696 if (newHold != null) {
8697 mBatteryStats.noteStartWakelock(newHold.mUid,
8698 "window",
8699 BatteryStats.WAKE_TYPE_WINDOW);
8700 }
8701 } catch (RemoteException e) {
8702 }
8703 }
8704 break;
8705 }
Romain Guy06882f82009-06-10 13:36:04 -07008706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 case APP_TRANSITION_TIMEOUT: {
8708 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008709 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8711 "*** APP TRANSITION TIMEOUT");
8712 mAppTransitionReady = true;
8713 mAppTransitionTimeout = true;
8714 performLayoutAndPlaceSurfacesLocked();
8715 }
8716 }
8717 break;
8718 }
Romain Guy06882f82009-06-10 13:36:04 -07008719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 case PERSIST_ANIMATION_SCALE: {
8721 Settings.System.putFloat(mContext.getContentResolver(),
8722 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8723 Settings.System.putFloat(mContext.getContentResolver(),
8724 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8725 break;
8726 }
Romain Guy06882f82009-06-10 13:36:04 -07008727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008728 case FORCE_GC: {
8729 synchronized(mWindowMap) {
8730 if (mAnimationPending) {
8731 // If we are animating, don't do the gc now but
8732 // delay a bit so we don't interrupt the animation.
8733 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8734 2000);
8735 return;
8736 }
8737 // If we are currently rotating the display, it will
8738 // schedule a new message when done.
8739 if (mDisplayFrozen) {
8740 return;
8741 }
8742 mFreezeGcPending = 0;
8743 }
8744 Runtime.getRuntime().gc();
8745 break;
8746 }
Romain Guy06882f82009-06-10 13:36:04 -07008747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 case ENABLE_SCREEN: {
8749 performEnableScreen();
8750 break;
8751 }
Romain Guy06882f82009-06-10 13:36:04 -07008752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 case APP_FREEZE_TIMEOUT: {
8754 synchronized (mWindowMap) {
8755 Log.w(TAG, "App freeze timeout expired.");
8756 int i = mAppTokens.size();
8757 while (i > 0) {
8758 i--;
8759 AppWindowToken tok = mAppTokens.get(i);
8760 if (tok.freezingScreen) {
8761 Log.w(TAG, "Force clearing freeze: " + tok);
8762 unsetAppFreezingScreenLocked(tok, true, true);
8763 }
8764 }
8765 }
8766 break;
8767 }
Romain Guy06882f82009-06-10 13:36:04 -07008768
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008769 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008770 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008771 sendNewConfiguration();
8772 }
8773 break;
8774 }
Romain Guy06882f82009-06-10 13:36:04 -07008775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008776 }
8777 }
8778 }
8779
8780 // -------------------------------------------------------------
8781 // IWindowManager API
8782 // -------------------------------------------------------------
8783
8784 public IWindowSession openSession(IInputMethodClient client,
8785 IInputContext inputContext) {
8786 if (client == null) throw new IllegalArgumentException("null client");
8787 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8788 return new Session(client, inputContext);
8789 }
8790
8791 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8792 synchronized (mWindowMap) {
8793 // The focus for the client is the window immediately below
8794 // where we would place the input method window.
8795 int idx = findDesiredInputMethodWindowIndexLocked(false);
8796 WindowState imFocus;
8797 if (idx > 0) {
8798 imFocus = (WindowState)mWindows.get(idx-1);
8799 if (imFocus != null) {
8800 if (imFocus.mSession.mClient != null &&
8801 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8802 return true;
8803 }
8804 }
8805 }
8806 }
8807 return false;
8808 }
Romain Guy06882f82009-06-10 13:36:04 -07008809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 // -------------------------------------------------------------
8811 // Internals
8812 // -------------------------------------------------------------
8813
8814 final WindowState windowForClientLocked(Session session, IWindow client) {
8815 return windowForClientLocked(session, client.asBinder());
8816 }
Romain Guy06882f82009-06-10 13:36:04 -07008817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 final WindowState windowForClientLocked(Session session, IBinder client) {
8819 WindowState win = mWindowMap.get(client);
8820 if (localLOGV) Log.v(
8821 TAG, "Looking up client " + client + ": " + win);
8822 if (win == null) {
8823 RuntimeException ex = new RuntimeException();
8824 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8825 return null;
8826 }
8827 if (session != null && win.mSession != session) {
8828 RuntimeException ex = new RuntimeException();
8829 Log.w(TAG, "Requested window " + client + " is in session " +
8830 win.mSession + ", not " + session, ex);
8831 return null;
8832 }
8833
8834 return win;
8835 }
8836
Dianne Hackborna8f60182009-09-01 19:01:50 -07008837 final void rebuildAppWindowListLocked() {
8838 int NW = mWindows.size();
8839 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008840 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008841 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008842
8843 // First remove all existing app windows.
8844 i=0;
8845 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008846 WindowState w = (WindowState)mWindows.get(i);
8847 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008848 WindowState win = (WindowState)mWindows.remove(i);
8849 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8850 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008851 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008852 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008853 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008854 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
8855 && lastWallpaper == i-1) {
8856 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008857 }
8858 i++;
8859 }
8860
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008861 // The wallpaper window(s) typically live at the bottom of the stack,
8862 // so skip them before adding app tokens.
8863 lastWallpaper++;
8864 i = lastWallpaper;
8865
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008866 // First add all of the exiting app tokens... these are no longer
8867 // in the main app list, but still have windows shown. We put them
8868 // in the back because now that the animation is over we no longer
8869 // will care about them.
8870 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008871 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008872 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8873 }
8874
8875 // And add in the still active app tokens in Z order.
8876 NT = mAppTokens.size();
8877 for (int j=0; j<NT; j++) {
8878 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008879 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008880
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008881 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008882 if (i != numRemoved) {
8883 Log.w(TAG, "Rebuild removed " + numRemoved
8884 + " windows but added " + i);
8885 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07008886 }
8887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008888 private final void assignLayersLocked() {
8889 int N = mWindows.size();
8890 int curBaseLayer = 0;
8891 int curLayer = 0;
8892 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008894 for (i=0; i<N; i++) {
8895 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008896 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8897 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 curLayer += WINDOW_LAYER_MULTIPLIER;
8899 w.mLayer = curLayer;
8900 } else {
8901 curBaseLayer = curLayer = w.mBaseLayer;
8902 w.mLayer = curLayer;
8903 }
8904 if (w.mTargetAppToken != null) {
8905 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8906 } else if (w.mAppToken != null) {
8907 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8908 } else {
8909 w.mAnimLayer = w.mLayer;
8910 }
8911 if (w.mIsImWindow) {
8912 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008913 } else if (w.mIsWallpaper) {
8914 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008915 }
8916 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8917 + w.mAnimLayer);
8918 //System.out.println(
8919 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8920 }
8921 }
8922
8923 private boolean mInLayout = false;
8924 private final void performLayoutAndPlaceSurfacesLocked() {
8925 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008926 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 throw new RuntimeException("Recursive call!");
8928 }
8929 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8930 return;
8931 }
8932
8933 boolean recoveringMemory = false;
8934 if (mForceRemoves != null) {
8935 recoveringMemory = true;
8936 // Wait a little it for things to settle down, and off we go.
8937 for (int i=0; i<mForceRemoves.size(); i++) {
8938 WindowState ws = mForceRemoves.get(i);
8939 Log.i(TAG, "Force removing: " + ws);
8940 removeWindowInnerLocked(ws.mSession, ws);
8941 }
8942 mForceRemoves = null;
8943 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8944 Object tmp = new Object();
8945 synchronized (tmp) {
8946 try {
8947 tmp.wait(250);
8948 } catch (InterruptedException e) {
8949 }
8950 }
8951 }
Romain Guy06882f82009-06-10 13:36:04 -07008952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008953 mInLayout = true;
8954 try {
8955 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 int i = mPendingRemove.size()-1;
8958 if (i >= 0) {
8959 while (i >= 0) {
8960 WindowState w = mPendingRemove.get(i);
8961 removeWindowInnerLocked(w.mSession, w);
8962 i--;
8963 }
8964 mPendingRemove.clear();
8965
8966 mInLayout = false;
8967 assignLayersLocked();
8968 mLayoutNeeded = true;
8969 performLayoutAndPlaceSurfacesLocked();
8970
8971 } else {
8972 mInLayout = false;
8973 if (mLayoutNeeded) {
8974 requestAnimationLocked(0);
8975 }
8976 }
8977 } catch (RuntimeException e) {
8978 mInLayout = false;
8979 Log.e(TAG, "Unhandled exception while layout out windows", e);
8980 }
8981 }
8982
8983 private final void performLayoutLockedInner() {
8984 final int dw = mDisplay.getWidth();
8985 final int dh = mDisplay.getHeight();
8986
8987 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008988 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 int i;
8990
8991 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008992
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008993 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 mPolicy.beginLayoutLw(dw, dh);
8995
8996 // First perform layout of any root windows (not attached
8997 // to another window).
8998 int topAttached = -1;
8999 for (i = N-1; i >= 0; i--) {
9000 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009001
9002 // Don't do layout of a window if it is not visible, or
9003 // soon won't be visible, to avoid wasting time and funky
9004 // changes while a window is animating away.
9005 final AppWindowToken atoken = win.mAppToken;
9006 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009008 || win.mRootToken.hidden
9009 || (atoken != null && atoken.hiddenRequested)
9010 || !win.mPolicyVisibility
9011 || win.mAttachedHidden
9012 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013
9014 // If this view is GONE, then skip it -- keep the current
9015 // frame, and let the caller know so they can ignore it
9016 // if they want. (We do the normal layout for INVISIBLE
9017 // windows, since that means "perform layout as normal,
9018 // just don't display").
9019 if (!gone || !win.mHaveFrame) {
9020 if (!win.mLayoutAttached) {
9021 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9022 } else {
9023 if (topAttached < 0) topAttached = i;
9024 }
9025 }
9026 }
Romain Guy06882f82009-06-10 13:36:04 -07009027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 // Now perform layout of attached windows, which usually
9029 // depend on the position of the window they are attached to.
9030 // XXX does not deal with windows that are attached to windows
9031 // that are themselves attached.
9032 for (i = topAttached; i >= 0; i--) {
9033 WindowState win = (WindowState) mWindows.get(i);
9034
9035 // If this view is GONE, then skip it -- keep the current
9036 // frame, and let the caller know so they can ignore it
9037 // if they want. (We do the normal layout for INVISIBLE
9038 // windows, since that means "perform layout as normal,
9039 // just don't display").
9040 if (win.mLayoutAttached) {
9041 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9042 || !win.mHaveFrame) {
9043 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9044 }
9045 }
9046 }
9047
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009048 int changes = mPolicy.finishLayoutLw();
9049 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9050 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9051 assignLayersLocked();
9052 }
9053 }
9054 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009055 mLayoutNeeded = false;
9056 } else if (repeats > 2) {
9057 Log.w(TAG, "Layout repeat aborted after too many iterations");
9058 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009059 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9060 Configuration newConfig = updateOrientationFromAppTokensLocked(
9061 null, null);
9062 if (newConfig != null) {
9063 mLayoutNeeded = true;
9064 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9065 }
9066 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009067 } else {
9068 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009069 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9070 Configuration newConfig = updateOrientationFromAppTokensLocked(
9071 null, null);
9072 if (newConfig != null) {
9073 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9074 }
9075 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009077 }
9078 }
Romain Guy06882f82009-06-10 13:36:04 -07009079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009080 private final void performLayoutAndPlaceSurfacesLockedInner(
9081 boolean recoveringMemory) {
9082 final long currentTime = SystemClock.uptimeMillis();
9083 final int dw = mDisplay.getWidth();
9084 final int dh = mDisplay.getHeight();
9085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009086 int i;
9087
9088 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009089 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 if (mFxSession == null) {
9092 mFxSession = new SurfaceSession();
9093 }
Romain Guy06882f82009-06-10 13:36:04 -07009094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9096
9097 // Initialize state of exiting tokens.
9098 for (i=mExitingTokens.size()-1; i>=0; i--) {
9099 mExitingTokens.get(i).hasVisible = false;
9100 }
9101
9102 // Initialize state of exiting applications.
9103 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9104 mExitingAppTokens.get(i).hasVisible = false;
9105 }
9106
9107 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 boolean orientationChangeComplete = true;
9109 Session holdScreen = null;
9110 float screenBrightness = -1;
9111 boolean focusDisplayed = false;
9112 boolean animating = false;
9113
9114 Surface.openTransaction();
9115 try {
9116 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009117 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009118
9119 do {
9120 final int transactionSequence = ++mTransactionSequence;
9121
9122 // Update animations of all applications, including those
9123 // associated with exiting/removed apps
9124 boolean tokensAnimating = false;
9125 final int NAT = mAppTokens.size();
9126 for (i=0; i<NAT; i++) {
9127 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9128 tokensAnimating = true;
9129 }
9130 }
9131 final int NEAT = mExitingAppTokens.size();
9132 for (i=0; i<NEAT; i++) {
9133 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9134 tokensAnimating = true;
9135 }
9136 }
9137
9138 animating = tokensAnimating;
9139 restart = false;
9140
9141 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009142 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009143 boolean focusMayChange = false;
9144 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145
9146 mPolicy.beginAnimationLw(dw, dh);
9147
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009148 final int N = mWindows.size();
9149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009150 for (i=N-1; i>=0; i--) {
9151 WindowState w = (WindowState)mWindows.get(i);
9152
9153 final WindowManager.LayoutParams attrs = w.mAttrs;
9154
9155 if (w.mSurface != null) {
9156 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009157 if (w.commitFinishDrawingLocked(currentTime)) {
9158 if ((w.mAttrs.flags
9159 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009160 if (DEBUG_WALLPAPER) Log.v(TAG,
9161 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009162 wallpaperMayChange = true;
9163 }
9164 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009165
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009166 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009167 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9168 animating = true;
9169 //w.dump(" ");
9170 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009171 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9172 wallpaperMayChange = true;
9173 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009174
9175 if (mPolicy.doesForceHide(w, attrs)) {
9176 if (!wasAnimating && animating) {
9177 wallpaperForceHidingChanged = true;
9178 focusMayChange = true;
9179 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9180 forceHiding = true;
9181 }
9182 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9183 boolean changed;
9184 if (forceHiding) {
9185 changed = w.hideLw(false, false);
9186 } else {
9187 changed = w.showLw(false, false);
9188 if (changed && wallpaperForceHidingChanged
9189 && w.isReadyForDisplay()) {
9190 // Assume we will need to animate. If
9191 // we don't (because the wallpaper will
9192 // stay with the lock screen), then we will
9193 // clean up later.
9194 Animation a = mPolicy.createForceHideEnterAnimation();
9195 if (a != null) {
9196 w.setAnimation(a);
9197 }
9198 }
9199 }
9200 if (changed && (attrs.flags
9201 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9202 wallpaperMayChange = true;
9203 }
9204 }
9205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 mPolicy.animatingWindowLw(w, attrs);
9207 }
9208
9209 final AppWindowToken atoken = w.mAppToken;
9210 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9211 if (atoken.lastTransactionSequence != transactionSequence) {
9212 atoken.lastTransactionSequence = transactionSequence;
9213 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9214 atoken.startingDisplayed = false;
9215 }
9216 if ((w.isOnScreen() || w.mAttrs.type
9217 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9218 && !w.mExiting && !w.mDestroying) {
9219 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
9220 Log.v(TAG, "Eval win " + w + ": isDisplayed="
9221 + w.isDisplayedLw()
9222 + ", isAnimating=" + w.isAnimating());
9223 if (!w.isDisplayedLw()) {
9224 Log.v(TAG, "Not displayed: s=" + w.mSurface
9225 + " pv=" + w.mPolicyVisibility
9226 + " dp=" + w.mDrawPending
9227 + " cdp=" + w.mCommitDrawPending
9228 + " ah=" + w.mAttachedHidden
9229 + " th=" + atoken.hiddenRequested
9230 + " a=" + w.mAnimating);
9231 }
9232 }
9233 if (w != atoken.startingWindow) {
9234 if (!atoken.freezingScreen || !w.mAppFreezing) {
9235 atoken.numInterestingWindows++;
9236 if (w.isDisplayedLw()) {
9237 atoken.numDrawnWindows++;
9238 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9239 "tokenMayBeDrawn: " + atoken
9240 + " freezingScreen=" + atoken.freezingScreen
9241 + " mAppFreezing=" + w.mAppFreezing);
9242 tokenMayBeDrawn = true;
9243 }
9244 }
9245 } else if (w.isDisplayedLw()) {
9246 atoken.startingDisplayed = true;
9247 }
9248 }
9249 } else if (w.mReadyToShow) {
9250 w.performShowLocked();
9251 }
9252 }
9253
9254 if (mPolicy.finishAnimationLw()) {
9255 restart = true;
9256 }
9257
9258 if (tokenMayBeDrawn) {
9259 // See if any windows have been drawn, so they (and others
9260 // associated with them) can now be shown.
9261 final int NT = mTokenList.size();
9262 for (i=0; i<NT; i++) {
9263 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9264 if (wtoken == null) {
9265 continue;
9266 }
9267 if (wtoken.freezingScreen) {
9268 int numInteresting = wtoken.numInterestingWindows;
9269 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9270 if (DEBUG_VISIBILITY) Log.v(TAG,
9271 "allDrawn: " + wtoken
9272 + " interesting=" + numInteresting
9273 + " drawn=" + wtoken.numDrawnWindows);
9274 wtoken.showAllWindowsLocked();
9275 unsetAppFreezingScreenLocked(wtoken, false, true);
9276 orientationChangeComplete = true;
9277 }
9278 } else if (!wtoken.allDrawn) {
9279 int numInteresting = wtoken.numInterestingWindows;
9280 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9281 if (DEBUG_VISIBILITY) Log.v(TAG,
9282 "allDrawn: " + wtoken
9283 + " interesting=" + numInteresting
9284 + " drawn=" + wtoken.numDrawnWindows);
9285 wtoken.allDrawn = true;
9286 restart = true;
9287
9288 // We can now show all of the drawn windows!
9289 if (!mOpeningApps.contains(wtoken)) {
9290 wtoken.showAllWindowsLocked();
9291 }
9292 }
9293 }
9294 }
9295 }
9296
9297 // If we are ready to perform an app transition, check through
9298 // all of the app tokens to be shown and see if they are ready
9299 // to go.
9300 if (mAppTransitionReady) {
9301 int NN = mOpeningApps.size();
9302 boolean goodToGo = true;
9303 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9304 "Checking " + NN + " opening apps (frozen="
9305 + mDisplayFrozen + " timeout="
9306 + mAppTransitionTimeout + ")...");
9307 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9308 // If the display isn't frozen, wait to do anything until
9309 // all of the apps are ready. Otherwise just go because
9310 // we'll unfreeze the display when everyone is ready.
9311 for (i=0; i<NN && goodToGo; i++) {
9312 AppWindowToken wtoken = mOpeningApps.get(i);
9313 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9314 "Check opening app" + wtoken + ": allDrawn="
9315 + wtoken.allDrawn + " startingDisplayed="
9316 + wtoken.startingDisplayed);
9317 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9318 && !wtoken.startingMoved) {
9319 goodToGo = false;
9320 }
9321 }
9322 }
9323 if (goodToGo) {
9324 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9325 int transit = mNextAppTransition;
9326 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009327 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009329 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009330 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009332 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 mAppTransitionTimeout = false;
9334 mStartingIconInTransition = false;
9335 mSkipAppTransitionAnimation = false;
9336
9337 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9338
Dianne Hackborna8f60182009-09-01 19:01:50 -07009339 // If there are applications waiting to come to the
9340 // top of the stack, now is the time to move their windows.
9341 // (Note that we don't do apps going to the bottom
9342 // here -- we want to keep their windows in the old
9343 // Z-order until the animation completes.)
9344 if (mToTopApps.size() > 0) {
9345 NN = mAppTokens.size();
9346 for (i=0; i<NN; i++) {
9347 AppWindowToken wtoken = mAppTokens.get(i);
9348 if (wtoken.sendingToTop) {
9349 wtoken.sendingToTop = false;
9350 moveAppWindowsLocked(wtoken, NN, false);
9351 }
9352 }
9353 mToTopApps.clear();
9354 }
9355
Dianne Hackborn25994b42009-09-04 14:21:19 -07009356 WindowState oldWallpaper = mWallpaperTarget;
9357
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009358 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009359 wallpaperMayChange = false;
9360
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009361 // The top-most window will supply the layout params,
9362 // and we will determine it below.
9363 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009364 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009365 int bestAnimLayer = -1;
9366
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009367 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009368 "New wallpaper target=" + mWallpaperTarget
9369 + ", lower target=" + mLowerWallpaperTarget
9370 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009371 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009372 // Do a first pass through the tokens for two
9373 // things:
9374 // (1) Determine if both the closing and opening
9375 // app token sets are wallpaper targets, in which
9376 // case special animations are needed
9377 // (since the wallpaper needs to stay static
9378 // behind them).
9379 // (2) Find the layout params of the top-most
9380 // application window in the tokens, which is
9381 // what will control the animation theme.
9382 final int NC = mClosingApps.size();
9383 NN = NC + mOpeningApps.size();
9384 for (i=0; i<NN; i++) {
9385 AppWindowToken wtoken;
9386 int mode;
9387 if (i < NC) {
9388 wtoken = mClosingApps.get(i);
9389 mode = 1;
9390 } else {
9391 wtoken = mOpeningApps.get(i-NC);
9392 mode = 2;
9393 }
9394 if (mLowerWallpaperTarget != null) {
9395 if (mLowerWallpaperTarget.mAppToken == wtoken
9396 || mUpperWallpaperTarget.mAppToken == wtoken) {
9397 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009398 }
9399 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009400 if (wtoken.appFullscreen) {
9401 WindowState ws = wtoken.findMainWindow();
9402 if (ws != null) {
9403 // If this is a compatibility mode
9404 // window, we will always use its anim.
9405 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9406 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009407 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009408 bestAnimLayer = Integer.MAX_VALUE;
9409 } else if (ws.mLayer > bestAnimLayer) {
9410 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009411 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009412 bestAnimLayer = ws.mLayer;
9413 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009414 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009415 }
9416 }
9417
Dianne Hackborn25994b42009-09-04 14:21:19 -07009418 if (foundWallpapers == 3) {
9419 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9420 "Wallpaper animation!");
9421 switch (transit) {
9422 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9423 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9424 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9425 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9426 break;
9427 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9428 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9429 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9430 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9431 break;
9432 }
9433 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9434 "New transit: " + transit);
9435 } else if (oldWallpaper != null) {
9436 // We are transitioning from an activity with
9437 // a wallpaper to one without.
9438 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9439 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9440 "New transit away from wallpaper: " + transit);
9441 } else if (mWallpaperTarget != null) {
9442 // We are transitioning from an activity without
9443 // a wallpaper to now showing the wallpaper
9444 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9445 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9446 "New transit into wallpaper: " + transit);
9447 }
9448
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009449 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9450 mLastEnterAnimToken = animToken;
9451 mLastEnterAnimParams = animLp;
9452 } else if (mLastEnterAnimParams != null) {
9453 animLp = mLastEnterAnimParams;
9454 mLastEnterAnimToken = null;
9455 mLastEnterAnimParams = null;
9456 }
9457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 NN = mOpeningApps.size();
9459 for (i=0; i<NN; i++) {
9460 AppWindowToken wtoken = mOpeningApps.get(i);
9461 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9462 "Now opening app" + wtoken);
9463 wtoken.reportedVisible = false;
9464 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009465 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009466 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009468 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009469 wtoken.showAllWindowsLocked();
9470 }
9471 NN = mClosingApps.size();
9472 for (i=0; i<NN; i++) {
9473 AppWindowToken wtoken = mClosingApps.get(i);
9474 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9475 "Now closing app" + wtoken);
9476 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009477 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009478 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009479 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009480 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 // Force the allDrawn flag, because we want to start
9482 // this guy's animations regardless of whether it's
9483 // gotten drawn.
9484 wtoken.allDrawn = true;
9485 }
9486
9487 mOpeningApps.clear();
9488 mClosingApps.clear();
9489
9490 // This has changed the visibility of windows, so perform
9491 // a new layout to get them all up-to-date.
9492 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009493 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9494 assignLayersLocked();
9495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009496 performLayoutLockedInner();
9497 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009498 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499
9500 restart = true;
9501 }
9502 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009503
Dianne Hackborna8f60182009-09-01 19:01:50 -07009504 if (!animating && mAppTransitionRunning) {
9505 // We have finished the animation of an app transition. To do
9506 // this, we have delayed a lot of operations like showing and
9507 // hiding apps, moving apps in Z-order, etc. The app token list
9508 // reflects the correct Z-order, but the window list may now
9509 // be out of sync with it. So here we will just rebuild the
9510 // entire app window list. Fun!
9511 mAppTransitionRunning = false;
9512 // Clear information about apps that were moving.
9513 mToBottomApps.clear();
9514
9515 rebuildAppWindowListLocked();
9516 restart = true;
9517 moveInputMethodWindowsIfNeededLocked(false);
9518 wallpaperMayChange = true;
9519 mLayoutNeeded = true;
9520 }
9521
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009522 int adjResult = 0;
9523
9524 if (wallpaperForceHidingChanged) {
9525 // At this point, there was a window with a wallpaper that
9526 // was force hiding other windows behind it, but now it
9527 // is going away. This may be simple -- just animate
9528 // away the wallpaper and its window -- or it may be
9529 // hard -- the wallpaper now needs to be shown behind
9530 // something that was hidden.
9531 WindowState oldWallpaper = mWallpaperTarget;
9532 adjResult = adjustWallpaperWindowsLocked();
9533 wallpaperMayChange = false;
9534 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9535 + " NEW: " + mWallpaperTarget);
9536 if (mLowerWallpaperTarget == null) {
9537 // Whoops, we don't need a special wallpaper animation.
9538 // Clear them out.
9539 forceHiding = false;
9540 for (i=N-1; i>=0; i--) {
9541 WindowState w = (WindowState)mWindows.get(i);
9542 if (w.mSurface != null) {
9543 final WindowManager.LayoutParams attrs = w.mAttrs;
9544 if (mPolicy.doesForceHide(w, attrs)) {
9545 forceHiding = true;
9546 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9547 if (!w.mAnimating) {
9548 // We set the animation above so it
9549 // is not yet running.
9550 w.clearAnimation();
9551 }
9552 }
9553 }
9554 }
9555 }
9556 }
9557
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009558 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009559 if (DEBUG_WALLPAPER) Log.v(TAG,
9560 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009561 adjResult = adjustWallpaperWindowsLocked();
9562 }
9563
9564 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9565 if (DEBUG_WALLPAPER) Log.v(TAG,
9566 "Wallpaper layer changed: assigning layers + relayout");
9567 restart = true;
9568 mLayoutNeeded = true;
9569 assignLayersLocked();
9570 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9571 if (DEBUG_WALLPAPER) Log.v(TAG,
9572 "Wallpaper visibility changed: relayout");
9573 restart = true;
9574 mLayoutNeeded = true;
9575 }
9576
9577 if (focusMayChange) {
9578 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009579 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009580 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009581 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009582 }
9583
9584 if (mLayoutNeeded) {
9585 restart = true;
9586 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009587 }
9588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 } while (restart);
9590
9591 // THIRD LOOP: Update the surfaces of all windows.
9592
9593 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9594
9595 boolean obscured = false;
9596 boolean blurring = false;
9597 boolean dimming = false;
9598 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009599 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009600 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009601
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009602 final int N = mWindows.size();
9603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009604 for (i=N-1; i>=0; i--) {
9605 WindowState w = (WindowState)mWindows.get(i);
9606
9607 boolean displayed = false;
9608 final WindowManager.LayoutParams attrs = w.mAttrs;
9609 final int attrFlags = attrs.flags;
9610
9611 if (w.mSurface != null) {
9612 w.computeShownFrameLocked();
9613 if (localLOGV) Log.v(
9614 TAG, "Placing surface #" + i + " " + w.mSurface
9615 + ": new=" + w.mShownFrame + ", old="
9616 + w.mLastShownFrame);
9617
9618 boolean resize;
9619 int width, height;
9620 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9621 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9622 w.mLastRequestedHeight != w.mRequestedHeight;
9623 // for a scaled surface, we just want to use
9624 // the requested size.
9625 width = w.mRequestedWidth;
9626 height = w.mRequestedHeight;
9627 w.mLastRequestedWidth = width;
9628 w.mLastRequestedHeight = height;
9629 w.mLastShownFrame.set(w.mShownFrame);
9630 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009631 if (SHOW_TRANSACTIONS) Log.i(
9632 TAG, " SURFACE " + w.mSurface
9633 + ": POS " + w.mShownFrame.left
9634 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009635 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9636 } catch (RuntimeException e) {
9637 Log.w(TAG, "Error positioning surface in " + w, e);
9638 if (!recoveringMemory) {
9639 reclaimSomeSurfaceMemoryLocked(w, "position");
9640 }
9641 }
9642 } else {
9643 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9644 width = w.mShownFrame.width();
9645 height = w.mShownFrame.height();
9646 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009647 }
9648
9649 if (resize) {
9650 if (width < 1) width = 1;
9651 if (height < 1) height = 1;
9652 if (w.mSurface != null) {
9653 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009654 if (SHOW_TRANSACTIONS) Log.i(
9655 TAG, " SURFACE " + w.mSurface + ": POS "
9656 + w.mShownFrame.left + ","
9657 + w.mShownFrame.top + " SIZE "
9658 + w.mShownFrame.width() + "x"
9659 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 w.mSurface.setSize(width, height);
9661 w.mSurface.setPosition(w.mShownFrame.left,
9662 w.mShownFrame.top);
9663 } catch (RuntimeException e) {
9664 // If something goes wrong with the surface (such
9665 // as running out of memory), don't take down the
9666 // entire system.
9667 Log.e(TAG, "Failure updating surface of " + w
9668 + "size=(" + width + "x" + height
9669 + "), pos=(" + w.mShownFrame.left
9670 + "," + w.mShownFrame.top + ")", e);
9671 if (!recoveringMemory) {
9672 reclaimSomeSurfaceMemoryLocked(w, "size");
9673 }
9674 }
9675 }
9676 }
9677 if (!w.mAppFreezing) {
9678 w.mContentInsetsChanged =
9679 !w.mLastContentInsets.equals(w.mContentInsets);
9680 w.mVisibleInsetsChanged =
9681 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009682 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 || w.mContentInsetsChanged
9684 || w.mVisibleInsetsChanged) {
9685 w.mLastFrame.set(w.mFrame);
9686 w.mLastContentInsets.set(w.mContentInsets);
9687 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009688 // If the screen is currently frozen, then keep
9689 // it frozen until this window draws at its new
9690 // orientation.
9691 if (mDisplayFrozen) {
9692 if (DEBUG_ORIENTATION) Log.v(TAG,
9693 "Resizing while display frozen: " + w);
9694 w.mOrientationChanging = true;
9695 if (mWindowsFreezingScreen) {
9696 mWindowsFreezingScreen = true;
9697 // XXX should probably keep timeout from
9698 // when we first froze the display.
9699 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9700 mH.sendMessageDelayed(mH.obtainMessage(
9701 H.WINDOW_FREEZE_TIMEOUT), 2000);
9702 }
9703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009704 // If the orientation is changing, then we need to
9705 // hold off on unfreezing the display until this
9706 // window has been redrawn; to do that, we need
9707 // to go through the process of getting informed
9708 // by the application when it has finished drawing.
9709 if (w.mOrientationChanging) {
9710 if (DEBUG_ORIENTATION) Log.v(TAG,
9711 "Orientation start waiting for draw in "
9712 + w + ", surface " + w.mSurface);
9713 w.mDrawPending = true;
9714 w.mCommitDrawPending = false;
9715 w.mReadyToShow = false;
9716 if (w.mAppToken != null) {
9717 w.mAppToken.allDrawn = false;
9718 }
9719 }
Romain Guy06882f82009-06-10 13:36:04 -07009720 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 "Resizing window " + w + " to " + w.mFrame);
9722 mResizingWindows.add(w);
9723 } else if (w.mOrientationChanging) {
9724 if (!w.mDrawPending && !w.mCommitDrawPending) {
9725 if (DEBUG_ORIENTATION) Log.v(TAG,
9726 "Orientation not waiting for draw in "
9727 + w + ", surface " + w.mSurface);
9728 w.mOrientationChanging = false;
9729 }
9730 }
9731 }
9732
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009733 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009734 if (!w.mLastHidden) {
9735 //dump();
9736 w.mLastHidden = true;
9737 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009738 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 if (w.mSurface != null) {
9740 try {
9741 w.mSurface.hide();
9742 } catch (RuntimeException e) {
9743 Log.w(TAG, "Exception hiding surface in " + w);
9744 }
9745 }
9746 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9747 }
9748 // If we are waiting for this window to handle an
9749 // orientation change, well, it is hidden, so
9750 // doesn't really matter. Note that this does
9751 // introduce a potential glitch if the window
9752 // becomes unhidden before it has drawn for the
9753 // new orientation.
9754 if (w.mOrientationChanging) {
9755 w.mOrientationChanging = false;
9756 if (DEBUG_ORIENTATION) Log.v(TAG,
9757 "Orientation change skips hidden " + w);
9758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009759 } else if (w.mLastLayer != w.mAnimLayer
9760 || w.mLastAlpha != w.mShownAlpha
9761 || w.mLastDsDx != w.mDsDx
9762 || w.mLastDtDx != w.mDtDx
9763 || w.mLastDsDy != w.mDsDy
9764 || w.mLastDtDy != w.mDtDy
9765 || w.mLastHScale != w.mHScale
9766 || w.mLastVScale != w.mVScale
9767 || w.mLastHidden) {
9768 displayed = true;
9769 w.mLastAlpha = w.mShownAlpha;
9770 w.mLastLayer = w.mAnimLayer;
9771 w.mLastDsDx = w.mDsDx;
9772 w.mLastDtDx = w.mDtDx;
9773 w.mLastDsDy = w.mDsDy;
9774 w.mLastDtDy = w.mDtDy;
9775 w.mLastHScale = w.mHScale;
9776 w.mLastVScale = w.mVScale;
9777 if (SHOW_TRANSACTIONS) Log.i(
9778 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009779 + w.mShownAlpha + " layer=" + w.mAnimLayer
9780 + " matrix=[" + (w.mDsDx*w.mHScale)
9781 + "," + (w.mDtDx*w.mVScale)
9782 + "][" + (w.mDsDy*w.mHScale)
9783 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 if (w.mSurface != null) {
9785 try {
9786 w.mSurface.setAlpha(w.mShownAlpha);
9787 w.mSurface.setLayer(w.mAnimLayer);
9788 w.mSurface.setMatrix(
9789 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9790 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9791 } catch (RuntimeException e) {
9792 Log.w(TAG, "Error updating surface in " + w, e);
9793 if (!recoveringMemory) {
9794 reclaimSomeSurfaceMemoryLocked(w, "update");
9795 }
9796 }
9797 }
9798
9799 if (w.mLastHidden && !w.mDrawPending
9800 && !w.mCommitDrawPending
9801 && !w.mReadyToShow) {
9802 if (SHOW_TRANSACTIONS) Log.i(
9803 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9804 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9805 + " during relayout");
9806 if (showSurfaceRobustlyLocked(w)) {
9807 w.mHasDrawn = true;
9808 w.mLastHidden = false;
9809 } else {
9810 w.mOrientationChanging = false;
9811 }
9812 }
9813 if (w.mSurface != null) {
9814 w.mToken.hasVisible = true;
9815 }
9816 } else {
9817 displayed = true;
9818 }
9819
9820 if (displayed) {
9821 if (!covered) {
9822 if (attrs.width == LayoutParams.FILL_PARENT
9823 && attrs.height == LayoutParams.FILL_PARENT) {
9824 covered = true;
9825 }
9826 }
9827 if (w.mOrientationChanging) {
9828 if (w.mDrawPending || w.mCommitDrawPending) {
9829 orientationChangeComplete = false;
9830 if (DEBUG_ORIENTATION) Log.v(TAG,
9831 "Orientation continue waiting for draw in " + w);
9832 } else {
9833 w.mOrientationChanging = false;
9834 if (DEBUG_ORIENTATION) Log.v(TAG,
9835 "Orientation change complete in " + w);
9836 }
9837 }
9838 w.mToken.hasVisible = true;
9839 }
9840 } else if (w.mOrientationChanging) {
9841 if (DEBUG_ORIENTATION) Log.v(TAG,
9842 "Orientation change skips hidden " + w);
9843 w.mOrientationChanging = false;
9844 }
9845
9846 final boolean canBeSeen = w.isDisplayedLw();
9847
9848 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9849 focusDisplayed = true;
9850 }
9851
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009852 final boolean obscuredChanged = w.mObscured != obscured;
9853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009854 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009855 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009856 if (w.mSurface != null) {
9857 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9858 holdScreen = w.mSession;
9859 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009860 if (!syswin && w.mAttrs.screenBrightness >= 0
9861 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 screenBrightness = w.mAttrs.screenBrightness;
9863 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009864 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9865 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9866 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9867 syswin = true;
9868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009870
Dianne Hackborn25994b42009-09-04 14:21:19 -07009871 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9872 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 // This window completely covers everything behind it,
9874 // so we want to leave all of them as unblurred (for
9875 // performance reasons).
9876 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009877 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9878 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009879 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009880 obscured = true;
9881 if (mBackgroundFillerSurface == null) {
9882 try {
9883 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9884 0, dw, dh,
9885 PixelFormat.OPAQUE,
9886 Surface.FX_SURFACE_NORMAL);
9887 } catch (Exception e) {
9888 Log.e(TAG, "Exception creating filler surface", e);
9889 }
9890 }
9891 try {
9892 mBackgroundFillerSurface.setPosition(0, 0);
9893 mBackgroundFillerSurface.setSize(dw, dh);
9894 // Using the same layer as Dim because they will never be shown at the
9895 // same time.
9896 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9897 mBackgroundFillerSurface.show();
9898 } catch (RuntimeException e) {
9899 Log.e(TAG, "Exception showing filler surface");
9900 }
9901 backgroundFillerShown = true;
9902 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009903 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9905 if (localLOGV) Log.v(TAG, "Win " + w
9906 + ": blurring=" + blurring
9907 + " obscured=" + obscured
9908 + " displayed=" + displayed);
9909 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9910 if (!dimming) {
9911 //Log.i(TAG, "DIM BEHIND: " + w);
9912 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009913 if (mDimAnimator == null) {
9914 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009916 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009918 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 }
9920 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9921 if (!blurring) {
9922 //Log.i(TAG, "BLUR BEHIND: " + w);
9923 blurring = true;
9924 mBlurShown = true;
9925 if (mBlurSurface == null) {
9926 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9927 + mBlurSurface + ": CREATE");
9928 try {
Romain Guy06882f82009-06-10 13:36:04 -07009929 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 -1, 16, 16,
9931 PixelFormat.OPAQUE,
9932 Surface.FX_SURFACE_BLUR);
9933 } catch (Exception e) {
9934 Log.e(TAG, "Exception creating Blur surface", e);
9935 }
9936 }
9937 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9938 + mBlurSurface + ": SHOW pos=(0,0) (" +
9939 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9940 if (mBlurSurface != null) {
9941 mBlurSurface.setPosition(0, 0);
9942 mBlurSurface.setSize(dw, dh);
9943 try {
9944 mBlurSurface.show();
9945 } catch (RuntimeException e) {
9946 Log.w(TAG, "Failure showing blur surface", e);
9947 }
9948 }
9949 }
9950 mBlurSurface.setLayer(w.mAnimLayer-2);
9951 }
9952 }
9953 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009954
9955 if (obscuredChanged && mWallpaperTarget == w) {
9956 // This is the wallpaper target and its obscured state
9957 // changed... make sure the current wallaper's visibility
9958 // has been updated accordingly.
9959 updateWallpaperVisibilityLocked();
9960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009962
9963 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9964 mBackgroundFillerShown = false;
9965 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9966 try {
9967 mBackgroundFillerSurface.hide();
9968 } catch (RuntimeException e) {
9969 Log.e(TAG, "Exception hiding filler surface", e);
9970 }
9971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009973 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9974 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 }
Romain Guy06882f82009-06-10 13:36:04 -07009976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009977 if (!blurring && mBlurShown) {
9978 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9979 + ": HIDE");
9980 try {
9981 mBlurSurface.hide();
9982 } catch (IllegalArgumentException e) {
9983 Log.w(TAG, "Illegal argument exception hiding blur surface");
9984 }
9985 mBlurShown = false;
9986 }
9987
9988 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9989 } catch (RuntimeException e) {
9990 Log.e(TAG, "Unhandled exception in Window Manager", e);
9991 }
9992
9993 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9996 "With display frozen, orientationChangeComplete="
9997 + orientationChangeComplete);
9998 if (orientationChangeComplete) {
9999 if (mWindowsFreezingScreen) {
10000 mWindowsFreezingScreen = false;
10001 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10002 }
10003 if (mAppsFreezingScreen == 0) {
10004 stopFreezingDisplayLocked();
10005 }
10006 }
Romain Guy06882f82009-06-10 13:36:04 -070010007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 i = mResizingWindows.size();
10009 if (i > 0) {
10010 do {
10011 i--;
10012 WindowState win = mResizingWindows.get(i);
10013 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010014 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10015 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 win.mClient.resized(win.mFrame.width(),
10017 win.mFrame.height(), win.mLastContentInsets,
10018 win.mLastVisibleInsets, win.mDrawPending);
10019 win.mContentInsetsChanged = false;
10020 win.mVisibleInsetsChanged = false;
10021 } catch (RemoteException e) {
10022 win.mOrientationChanging = false;
10023 }
10024 } while (i > 0);
10025 mResizingWindows.clear();
10026 }
Romain Guy06882f82009-06-10 13:36:04 -070010027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010029 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 i = mDestroySurface.size();
10031 if (i > 0) {
10032 do {
10033 i--;
10034 WindowState win = mDestroySurface.get(i);
10035 win.mDestroying = false;
10036 if (mInputMethodWindow == win) {
10037 mInputMethodWindow = null;
10038 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010039 if (win == mWallpaperTarget) {
10040 wallpaperDestroyed = true;
10041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 win.destroySurfaceLocked();
10043 } while (i > 0);
10044 mDestroySurface.clear();
10045 }
10046
10047 // Time to remove any exiting tokens?
10048 for (i=mExitingTokens.size()-1; i>=0; i--) {
10049 WindowToken token = mExitingTokens.get(i);
10050 if (!token.hasVisible) {
10051 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010052 if (token.windowType == TYPE_WALLPAPER) {
10053 mWallpaperTokens.remove(token);
10054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010055 }
10056 }
10057
10058 // Time to remove any exiting applications?
10059 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10060 AppWindowToken token = mExitingAppTokens.get(i);
10061 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010062 // Make sure there is no animation running on this token,
10063 // so any windows associated with it will be removed as
10064 // soon as their animations are complete
10065 token.animation = null;
10066 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 mAppTokens.remove(token);
10068 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010069 if (mLastEnterAnimToken == token) {
10070 mLastEnterAnimToken = null;
10071 mLastEnterAnimParams = null;
10072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 }
10074 }
10075
Dianne Hackborna8f60182009-09-01 19:01:50 -070010076 boolean needRelayout = false;
10077
10078 if (!animating && mAppTransitionRunning) {
10079 // We have finished the animation of an app transition. To do
10080 // this, we have delayed a lot of operations like showing and
10081 // hiding apps, moving apps in Z-order, etc. The app token list
10082 // reflects the correct Z-order, but the window list may now
10083 // be out of sync with it. So here we will just rebuild the
10084 // entire app window list. Fun!
10085 mAppTransitionRunning = false;
10086 needRelayout = true;
10087 rebuildAppWindowListLocked();
10088 // Clear information about apps that were moving.
10089 mToBottomApps.clear();
10090 }
10091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 if (focusDisplayed) {
10093 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10094 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010095 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010096 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010097 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010098 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010099 requestAnimationLocked(0);
10100 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10102 }
10103 mQueue.setHoldScreenLocked(holdScreen != null);
10104 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10105 mPowerManager.setScreenBrightnessOverride(-1);
10106 } else {
10107 mPowerManager.setScreenBrightnessOverride((int)
10108 (screenBrightness * Power.BRIGHTNESS_ON));
10109 }
10110 if (holdScreen != mHoldingScreenOn) {
10111 mHoldingScreenOn = holdScreen;
10112 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10113 mH.sendMessage(m);
10114 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010115
10116 if (mTurnOnScreen) {
10117 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10118 LocalPowerManager.BUTTON_EVENT, true);
10119 mTurnOnScreen = false;
10120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 }
10122
10123 void requestAnimationLocked(long delay) {
10124 if (!mAnimationPending) {
10125 mAnimationPending = true;
10126 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10127 }
10128 }
Romain Guy06882f82009-06-10 13:36:04 -070010129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 /**
10131 * Have the surface flinger show a surface, robustly dealing with
10132 * error conditions. In particular, if there is not enough memory
10133 * to show the surface, then we will try to get rid of other surfaces
10134 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 * @return Returns true if the surface was successfully shown.
10137 */
10138 boolean showSurfaceRobustlyLocked(WindowState win) {
10139 try {
10140 if (win.mSurface != null) {
10141 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010142 if (win.mTurnOnScreen) {
10143 win.mTurnOnScreen = false;
10144 mTurnOnScreen = true;
10145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 }
10147 return true;
10148 } catch (RuntimeException e) {
10149 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10150 }
Romain Guy06882f82009-06-10 13:36:04 -070010151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 return false;
10155 }
Romain Guy06882f82009-06-10 13:36:04 -070010156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010157 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10158 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010160 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10161 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 if (mForceRemoves == null) {
10164 mForceRemoves = new ArrayList<WindowState>();
10165 }
Romain Guy06882f82009-06-10 13:36:04 -070010166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 long callingIdentity = Binder.clearCallingIdentity();
10168 try {
10169 // There was some problem... first, do a sanity check of the
10170 // window list to make sure we haven't left any dangling surfaces
10171 // around.
10172 int N = mWindows.size();
10173 boolean leakedSurface = false;
10174 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10175 for (int i=0; i<N; i++) {
10176 WindowState ws = (WindowState)mWindows.get(i);
10177 if (ws.mSurface != null) {
10178 if (!mSessions.contains(ws.mSession)) {
10179 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10180 + ws + " surface=" + ws.mSurface
10181 + " token=" + win.mToken
10182 + " pid=" + ws.mSession.mPid
10183 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010184 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 ws.mSurface = null;
10186 mForceRemoves.add(ws);
10187 i--;
10188 N--;
10189 leakedSurface = true;
10190 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10191 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10192 + ws + " surface=" + ws.mSurface
10193 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010194 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 ws.mSurface = null;
10196 leakedSurface = true;
10197 }
10198 }
10199 }
Romain Guy06882f82009-06-10 13:36:04 -070010200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010201 boolean killedApps = false;
10202 if (!leakedSurface) {
10203 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10204 SparseIntArray pidCandidates = new SparseIntArray();
10205 for (int i=0; i<N; i++) {
10206 WindowState ws = (WindowState)mWindows.get(i);
10207 if (ws.mSurface != null) {
10208 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10209 }
10210 }
10211 if (pidCandidates.size() > 0) {
10212 int[] pids = new int[pidCandidates.size()];
10213 for (int i=0; i<pids.length; i++) {
10214 pids[i] = pidCandidates.keyAt(i);
10215 }
10216 try {
10217 if (mActivityManager.killPidsForMemory(pids)) {
10218 killedApps = true;
10219 }
10220 } catch (RemoteException e) {
10221 }
10222 }
10223 }
Romain Guy06882f82009-06-10 13:36:04 -070010224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 if (leakedSurface || killedApps) {
10226 // We managed to reclaim some memory, so get rid of the trouble
10227 // surface and ask the app to request another one.
10228 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10229 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010230 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 win.mSurface = null;
10232 }
Romain Guy06882f82009-06-10 13:36:04 -070010233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 try {
10235 win.mClient.dispatchGetNewSurface();
10236 } catch (RemoteException e) {
10237 }
10238 }
10239 } finally {
10240 Binder.restoreCallingIdentity(callingIdentity);
10241 }
10242 }
Romain Guy06882f82009-06-10 13:36:04 -070010243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010244 private boolean updateFocusedWindowLocked(int mode) {
10245 WindowState newFocus = computeFocusedWindowLocked();
10246 if (mCurrentFocus != newFocus) {
10247 // This check makes sure that we don't already have the focus
10248 // change message pending.
10249 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10250 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10251 if (localLOGV) Log.v(
10252 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10253 final WindowState oldFocus = mCurrentFocus;
10254 mCurrentFocus = newFocus;
10255 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010257 final WindowState imWindow = mInputMethodWindow;
10258 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010259 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010260 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010261 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10262 mLayoutNeeded = true;
10263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010264 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10265 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010266 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10267 // Client will do the layout, but we need to assign layers
10268 // for handleNewWindowLocked() below.
10269 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010270 }
10271 }
Romain Guy06882f82009-06-10 13:36:04 -070010272
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010273 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10274 mKeyWaiter.handleNewWindowLocked(newFocus);
10275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 return true;
10277 }
10278 return false;
10279 }
10280
10281 private WindowState computeFocusedWindowLocked() {
10282 WindowState result = null;
10283 WindowState win;
10284
10285 int i = mWindows.size() - 1;
10286 int nextAppIndex = mAppTokens.size()-1;
10287 WindowToken nextApp = nextAppIndex >= 0
10288 ? mAppTokens.get(nextAppIndex) : null;
10289
10290 while (i >= 0) {
10291 win = (WindowState)mWindows.get(i);
10292
10293 if (localLOGV || DEBUG_FOCUS) Log.v(
10294 TAG, "Looking for focus: " + i
10295 + " = " + win
10296 + ", flags=" + win.mAttrs.flags
10297 + ", canReceive=" + win.canReceiveKeys());
10298
10299 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 // If this window's application has been removed, just skip it.
10302 if (thisApp != null && thisApp.removed) {
10303 i--;
10304 continue;
10305 }
Romain Guy06882f82009-06-10 13:36:04 -070010306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 // If there is a focused app, don't allow focus to go to any
10308 // windows below it. If this is an application window, step
10309 // through the app tokens until we find its app.
10310 if (thisApp != null && nextApp != null && thisApp != nextApp
10311 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10312 int origAppIndex = nextAppIndex;
10313 while (nextAppIndex > 0) {
10314 if (nextApp == mFocusedApp) {
10315 // Whoops, we are below the focused app... no focus
10316 // for you!
10317 if (localLOGV || DEBUG_FOCUS) Log.v(
10318 TAG, "Reached focused app: " + mFocusedApp);
10319 return null;
10320 }
10321 nextAppIndex--;
10322 nextApp = mAppTokens.get(nextAppIndex);
10323 if (nextApp == thisApp) {
10324 break;
10325 }
10326 }
10327 if (thisApp != nextApp) {
10328 // Uh oh, the app token doesn't exist! This shouldn't
10329 // happen, but if it does we can get totally hosed...
10330 // so restart at the original app.
10331 nextAppIndex = origAppIndex;
10332 nextApp = mAppTokens.get(nextAppIndex);
10333 }
10334 }
10335
10336 // Dispatch to this window if it is wants key events.
10337 if (win.canReceiveKeys()) {
10338 if (DEBUG_FOCUS) Log.v(
10339 TAG, "Found focus @ " + i + " = " + win);
10340 result = win;
10341 break;
10342 }
10343
10344 i--;
10345 }
10346
10347 return result;
10348 }
10349
10350 private void startFreezingDisplayLocked() {
10351 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010352 // Freezing the display also suspends key event delivery, to
10353 // keep events from going astray while the display is reconfigured.
10354 // If someone has changed orientation again while the screen is
10355 // still frozen, the events will continue to be blocked while the
10356 // successive orientation change is processed. To prevent spurious
10357 // ANRs, we reset the event dispatch timeout in this case.
10358 synchronized (mKeyWaiter) {
10359 mKeyWaiter.mWasFrozen = true;
10360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 return;
10362 }
Romain Guy06882f82009-06-10 13:36:04 -070010363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010364 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 long now = SystemClock.uptimeMillis();
10367 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10368 if (mFreezeGcPending != 0) {
10369 if (now > (mFreezeGcPending+1000)) {
10370 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10371 mH.removeMessages(H.FORCE_GC);
10372 Runtime.getRuntime().gc();
10373 mFreezeGcPending = now;
10374 }
10375 } else {
10376 mFreezeGcPending = now;
10377 }
Romain Guy06882f82009-06-10 13:36:04 -070010378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010380 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10381 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010382 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 mAppTransitionReady = true;
10384 }
Romain Guy06882f82009-06-10 13:36:04 -070010385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 if (PROFILE_ORIENTATION) {
10387 File file = new File("/data/system/frozen");
10388 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10389 }
10390 Surface.freezeDisplay(0);
10391 }
Romain Guy06882f82009-06-10 13:36:04 -070010392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010393 private void stopFreezingDisplayLocked() {
10394 if (!mDisplayFrozen) {
10395 return;
10396 }
Romain Guy06882f82009-06-10 13:36:04 -070010397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 mDisplayFrozen = false;
10399 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10400 if (PROFILE_ORIENTATION) {
10401 Debug.stopMethodTracing();
10402 }
10403 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010404
Chris Tate2ad63a92009-03-25 17:36:48 -070010405 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10406 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 synchronized (mKeyWaiter) {
10408 mKeyWaiter.mWasFrozen = true;
10409 mKeyWaiter.notifyAll();
10410 }
10411
10412 // A little kludge: a lot could have happened while the
10413 // display was frozen, so now that we are coming back we
10414 // do a gc so that any remote references the system
10415 // processes holds on others can be released if they are
10416 // no longer needed.
10417 mH.removeMessages(H.FORCE_GC);
10418 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10419 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 mScreenFrozenLock.release();
10422 }
Romain Guy06882f82009-06-10 13:36:04 -070010423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 @Override
10425 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10426 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10427 != PackageManager.PERMISSION_GRANTED) {
10428 pw.println("Permission Denial: can't dump WindowManager from from pid="
10429 + Binder.getCallingPid()
10430 + ", uid=" + Binder.getCallingUid());
10431 return;
10432 }
Romain Guy06882f82009-06-10 13:36:04 -070010433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 synchronized(mWindowMap) {
10435 pw.println("Current Window Manager state:");
10436 for (int i=mWindows.size()-1; i>=0; i--) {
10437 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010438 pw.print(" Window #"); pw.print(i); pw.print(' ');
10439 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 w.dump(pw, " ");
10441 }
10442 if (mInputMethodDialogs.size() > 0) {
10443 pw.println(" ");
10444 pw.println(" Input method dialogs:");
10445 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10446 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010447 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 }
10449 }
10450 if (mPendingRemove.size() > 0) {
10451 pw.println(" ");
10452 pw.println(" Remove pending for:");
10453 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10454 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010455 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10456 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 w.dump(pw, " ");
10458 }
10459 }
10460 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10461 pw.println(" ");
10462 pw.println(" Windows force removing:");
10463 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10464 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010465 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10466 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 w.dump(pw, " ");
10468 }
10469 }
10470 if (mDestroySurface.size() > 0) {
10471 pw.println(" ");
10472 pw.println(" Windows waiting to destroy their surface:");
10473 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10474 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010475 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10476 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 w.dump(pw, " ");
10478 }
10479 }
10480 if (mLosingFocus.size() > 0) {
10481 pw.println(" ");
10482 pw.println(" Windows losing focus:");
10483 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10484 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010485 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10486 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010487 w.dump(pw, " ");
10488 }
10489 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010490 if (mResizingWindows.size() > 0) {
10491 pw.println(" ");
10492 pw.println(" Windows waiting to resize:");
10493 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10494 WindowState w = mResizingWindows.get(i);
10495 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10496 pw.print(w); pw.println(":");
10497 w.dump(pw, " ");
10498 }
10499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010500 if (mSessions.size() > 0) {
10501 pw.println(" ");
10502 pw.println(" All active sessions:");
10503 Iterator<Session> it = mSessions.iterator();
10504 while (it.hasNext()) {
10505 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010506 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 s.dump(pw, " ");
10508 }
10509 }
10510 if (mTokenMap.size() > 0) {
10511 pw.println(" ");
10512 pw.println(" All tokens:");
10513 Iterator<WindowToken> it = mTokenMap.values().iterator();
10514 while (it.hasNext()) {
10515 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010516 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 token.dump(pw, " ");
10518 }
10519 }
10520 if (mTokenList.size() > 0) {
10521 pw.println(" ");
10522 pw.println(" Window token list:");
10523 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010524 pw.print(" #"); pw.print(i); pw.print(": ");
10525 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 }
10527 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010528 if (mWallpaperTokens.size() > 0) {
10529 pw.println(" ");
10530 pw.println(" Wallpaper tokens:");
10531 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10532 WindowToken token = mWallpaperTokens.get(i);
10533 pw.print(" Wallpaper #"); pw.print(i);
10534 pw.print(' '); pw.print(token); pw.println(':');
10535 token.dump(pw, " ");
10536 }
10537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010538 if (mAppTokens.size() > 0) {
10539 pw.println(" ");
10540 pw.println(" Application tokens in Z order:");
10541 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010542 pw.print(" App #"); pw.print(i); pw.print(": ");
10543 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 }
10545 }
10546 if (mFinishedStarting.size() > 0) {
10547 pw.println(" ");
10548 pw.println(" Finishing start of application tokens:");
10549 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10550 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010551 pw.print(" Finished Starting #"); pw.print(i);
10552 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 token.dump(pw, " ");
10554 }
10555 }
10556 if (mExitingTokens.size() > 0) {
10557 pw.println(" ");
10558 pw.println(" Exiting tokens:");
10559 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10560 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010561 pw.print(" Exiting #"); pw.print(i);
10562 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 token.dump(pw, " ");
10564 }
10565 }
10566 if (mExitingAppTokens.size() > 0) {
10567 pw.println(" ");
10568 pw.println(" Exiting application tokens:");
10569 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10570 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010571 pw.print(" Exiting App #"); pw.print(i);
10572 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 token.dump(pw, " ");
10574 }
10575 }
10576 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010577 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10578 pw.print(" mLastFocus="); pw.println(mLastFocus);
10579 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10580 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10581 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010582 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010583 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10584 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10585 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10586 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010587 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10588 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10589 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10590 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10591 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010592 if (mDimAnimator != null) {
10593 mDimAnimator.printTo(pw);
10594 } else {
10595 pw.print( " no DimAnimator ");
10596 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010597 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010598 pw.print(mInputMethodAnimLayerAdjustment);
10599 pw.print(" mWallpaperAnimLayerAdjustment=");
10600 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010601 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10602 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010603 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10604 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10605 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10606 pw.print(" mRotation="); pw.print(mRotation);
10607 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10608 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10609 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10610 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10611 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10612 pw.print(" mNextAppTransition=0x");
10613 pw.print(Integer.toHexString(mNextAppTransition));
10614 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010615 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010616 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010617 if (mNextAppTransitionPackage != null) {
10618 pw.print(" mNextAppTransitionPackage=");
10619 pw.print(mNextAppTransitionPackage);
10620 pw.print(", mNextAppTransitionEnter=0x");
10621 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10622 pw.print(", mNextAppTransitionExit=0x");
10623 pw.print(Integer.toHexString(mNextAppTransitionExit));
10624 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010625 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10626 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010627 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10628 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10629 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10630 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010631 if (mOpeningApps.size() > 0) {
10632 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10633 }
10634 if (mClosingApps.size() > 0) {
10635 pw.print(" mClosingApps="); pw.println(mClosingApps);
10636 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010637 if (mToTopApps.size() > 0) {
10638 pw.print(" mToTopApps="); pw.println(mToTopApps);
10639 }
10640 if (mToBottomApps.size() > 0) {
10641 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10642 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010643 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10644 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010645 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010646 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10647 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10648 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10649 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10650 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10651 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652 }
10653 }
10654
10655 public void monitor() {
10656 synchronized (mWindowMap) { }
10657 synchronized (mKeyguardDisabled) { }
10658 synchronized (mKeyWaiter) { }
10659 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010660
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010661 public void virtualKeyFeedback(KeyEvent event) {
10662 mPolicy.keyFeedbackFromInput(event);
10663 }
10664
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010665 /**
10666 * DimAnimator class that controls the dim animation. This holds the surface and
10667 * all state used for dim animation.
10668 */
10669 private static class DimAnimator {
10670 Surface mDimSurface;
10671 boolean mDimShown = false;
10672 float mDimCurrentAlpha;
10673 float mDimTargetAlpha;
10674 float mDimDeltaPerMs;
10675 long mLastDimAnimTime;
10676
10677 DimAnimator (SurfaceSession session) {
10678 if (mDimSurface == null) {
10679 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10680 + mDimSurface + ": CREATE");
10681 try {
10682 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10683 Surface.FX_SURFACE_DIM);
10684 } catch (Exception e) {
10685 Log.e(TAG, "Exception creating Dim surface", e);
10686 }
10687 }
10688 }
10689
10690 /**
10691 * Show the dim surface.
10692 */
10693 void show(int dw, int dh) {
10694 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10695 dw + "x" + dh + ")");
10696 mDimShown = true;
10697 try {
10698 mDimSurface.setPosition(0, 0);
10699 mDimSurface.setSize(dw, dh);
10700 mDimSurface.show();
10701 } catch (RuntimeException e) {
10702 Log.w(TAG, "Failure showing dim surface", e);
10703 }
10704 }
10705
10706 /**
10707 * Set's the dim surface's layer and update dim parameters that will be used in
10708 * {@link updateSurface} after all windows are examined.
10709 */
10710 void updateParameters(WindowState w, long currentTime) {
10711 mDimSurface.setLayer(w.mAnimLayer-1);
10712
10713 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010714 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10715 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010716 if (mDimTargetAlpha != target) {
10717 // If the desired dim level has changed, then
10718 // start an animation to it.
10719 mLastDimAnimTime = currentTime;
10720 long duration = (w.mAnimating && w.mAnimation != null)
10721 ? w.mAnimation.computeDurationHint()
10722 : DEFAULT_DIM_DURATION;
10723 if (target > mDimTargetAlpha) {
10724 // This is happening behind the activity UI,
10725 // so we can make it run a little longer to
10726 // give a stronger impression without disrupting
10727 // the user.
10728 duration *= DIM_DURATION_MULTIPLIER;
10729 }
10730 if (duration < 1) {
10731 // Don't divide by zero
10732 duration = 1;
10733 }
10734 mDimTargetAlpha = target;
10735 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10736 }
10737 }
10738
10739 /**
10740 * Updating the surface's alpha. Returns true if the animation continues, or returns
10741 * false when the animation is finished and the dim surface is hidden.
10742 */
10743 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10744 if (!dimming) {
10745 if (mDimTargetAlpha != 0) {
10746 mLastDimAnimTime = currentTime;
10747 mDimTargetAlpha = 0;
10748 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10749 }
10750 }
10751
10752 boolean animating = false;
10753 if (mLastDimAnimTime != 0) {
10754 mDimCurrentAlpha += mDimDeltaPerMs
10755 * (currentTime-mLastDimAnimTime);
10756 boolean more = true;
10757 if (displayFrozen) {
10758 // If the display is frozen, there is no reason to animate.
10759 more = false;
10760 } else if (mDimDeltaPerMs > 0) {
10761 if (mDimCurrentAlpha > mDimTargetAlpha) {
10762 more = false;
10763 }
10764 } else if (mDimDeltaPerMs < 0) {
10765 if (mDimCurrentAlpha < mDimTargetAlpha) {
10766 more = false;
10767 }
10768 } else {
10769 more = false;
10770 }
10771
10772 // Do we need to continue animating?
10773 if (more) {
10774 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10775 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10776 mLastDimAnimTime = currentTime;
10777 mDimSurface.setAlpha(mDimCurrentAlpha);
10778 animating = true;
10779 } else {
10780 mDimCurrentAlpha = mDimTargetAlpha;
10781 mLastDimAnimTime = 0;
10782 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10783 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10784 mDimSurface.setAlpha(mDimCurrentAlpha);
10785 if (!dimming) {
10786 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10787 + ": HIDE");
10788 try {
10789 mDimSurface.hide();
10790 } catch (RuntimeException e) {
10791 Log.w(TAG, "Illegal argument exception hiding dim surface");
10792 }
10793 mDimShown = false;
10794 }
10795 }
10796 }
10797 return animating;
10798 }
10799
10800 public void printTo(PrintWriter pw) {
10801 pw.print(" mDimShown="); pw.print(mDimShown);
10802 pw.print(" current="); pw.print(mDimCurrentAlpha);
10803 pw.print(" target="); pw.print(mDimTargetAlpha);
10804 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10805 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10806 }
10807 }
10808
10809 /**
10810 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10811 * This is used for opening/closing transition for apps in compatible mode.
10812 */
10813 private static class FadeInOutAnimation extends Animation {
10814 int mWidth;
10815 boolean mFadeIn;
10816
10817 public FadeInOutAnimation(boolean fadeIn) {
10818 setInterpolator(new AccelerateInterpolator());
10819 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10820 mFadeIn = fadeIn;
10821 }
10822
10823 @Override
10824 protected void applyTransformation(float interpolatedTime, Transformation t) {
10825 float x = interpolatedTime;
10826 if (!mFadeIn) {
10827 x = 1.0f - x; // reverse the interpolation for fade out
10828 }
10829 if (x < 0.5) {
10830 // move the window out of the screen.
10831 t.getMatrix().setTranslate(mWidth, 0);
10832 } else {
10833 t.getMatrix().setTranslate(0, 0);// show
10834 t.setAlpha((x - 0.5f) * 2);
10835 }
10836 }
10837
10838 @Override
10839 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10840 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10841 mWidth = width;
10842 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010843
10844 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010845 public int getZAdjustment() {
10846 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010847 }
10848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849}