blob: 01a4122c49fb0402457768fc4c06b6faed1d26dc [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) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003125 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003126 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) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04003968 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 != 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) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04003976 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 != 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) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004002 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 != 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 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004111 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
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 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004119 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 }
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 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004127 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 }
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 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004135 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 }
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);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004177 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 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
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007667 /**
7668 * Returns true if the window has a surface that it has drawn a
7669 * complete UI in to.
7670 */
7671 public boolean isDrawnLw() {
7672 final AppWindowToken atoken = mAppToken;
7673 return mSurface != null && !mDestroying
7674 && !mDrawPending && !mCommitDrawPending;
7675 }
7676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7678 boolean shownFrame, boolean onlyOpaque) {
7679 if (mSurface == null) {
7680 return false;
7681 }
7682 if (mAppToken != null && !mAppToken.appFullscreen) {
7683 return false;
7684 }
7685 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7686 return false;
7687 }
7688 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007689
7690 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7691 return frame.left <= mCompatibleScreenFrame.left &&
7692 frame.top <= mCompatibleScreenFrame.top &&
7693 frame.right >= mCompatibleScreenFrame.right &&
7694 frame.bottom >= mCompatibleScreenFrame.bottom;
7695 } else {
7696 return frame.left <= 0 && frame.top <= 0
7697 && frame.right >= screenWidth
7698 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007700 }
Romain Guy06882f82009-06-10 13:36:04 -07007701
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007702 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007703 * Return true if the window is opaque and fully drawn. This indicates
7704 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007705 */
7706 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007707 return (mAttrs.format == PixelFormat.OPAQUE
7708 || mAttrs.type == TYPE_WALLPAPER)
7709 && mSurface != null && mAnimation == null
7710 && (mAppToken == null || mAppToken.animation == null)
7711 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007712 }
7713
7714 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7715 return
7716 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007717 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7718 // only if it's visible
7719 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007720 // and only if the application fills the compatible screen
7721 mFrame.left <= mCompatibleScreenFrame.left &&
7722 mFrame.top <= mCompatibleScreenFrame.top &&
7723 mFrame.right >= mCompatibleScreenFrame.right &&
7724 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007725 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007726 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007727 }
7728
7729 boolean isFullscreen(int screenWidth, int screenHeight) {
7730 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007731 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732 }
7733
7734 void removeLocked() {
7735 if (mAttachedWindow != null) {
7736 mAttachedWindow.mChildWindows.remove(this);
7737 }
7738 destroySurfaceLocked();
7739 mSession.windowRemovedLocked();
7740 try {
7741 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7742 } catch (RuntimeException e) {
7743 // Ignore if it has already been removed (usually because
7744 // we are doing this as part of processing a death note.)
7745 }
7746 }
7747
7748 private class DeathRecipient implements IBinder.DeathRecipient {
7749 public void binderDied() {
7750 try {
7751 synchronized(mWindowMap) {
7752 WindowState win = windowForClientLocked(mSession, mClient);
7753 Log.i(TAG, "WIN DEATH: " + win);
7754 if (win != null) {
7755 removeWindowLocked(mSession, win);
7756 }
7757 }
7758 } catch (IllegalArgumentException ex) {
7759 // This will happen if the window has already been
7760 // removed.
7761 }
7762 }
7763 }
7764
7765 /** Returns true if this window desires key events. */
7766 public final boolean canReceiveKeys() {
7767 return isVisibleOrAdding()
7768 && (mViewVisibility == View.VISIBLE)
7769 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7770 }
7771
7772 public boolean hasDrawnLw() {
7773 return mHasDrawn;
7774 }
7775
7776 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007777 return showLw(doAnimation, true);
7778 }
7779
7780 boolean showLw(boolean doAnimation, boolean requestAnim) {
7781 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7782 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007784 mPolicyVisibility = true;
7785 mPolicyVisibilityAfterAnim = true;
7786 if (doAnimation) {
7787 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7788 }
7789 if (requestAnim) {
7790 requestAnimationLocked(0);
7791 }
7792 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 }
7794
7795 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007796 return hideLw(doAnimation, true);
7797 }
7798
7799 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7801 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007802 if (!current) {
7803 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007805 if (doAnimation) {
7806 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7807 if (mAnimation == null) {
7808 doAnimation = false;
7809 }
7810 }
7811 if (doAnimation) {
7812 mPolicyVisibilityAfterAnim = false;
7813 } else {
7814 mPolicyVisibilityAfterAnim = false;
7815 mPolicyVisibility = false;
7816 }
7817 if (requestAnim) {
7818 requestAnimationLocked(0);
7819 }
7820 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 }
7822
7823 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007824 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007825
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007826 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7827 pw.print(" mClient="); pw.println(mClient.asBinder());
7828 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7829 if (mAttachedWindow != null || mLayoutAttached) {
7830 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7831 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7832 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007833 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7834 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7835 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007836 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7837 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007838 }
7839 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7840 pw.print(" mSubLayer="); pw.print(mSubLayer);
7841 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7842 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7843 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7844 pw.print("="); pw.print(mAnimLayer);
7845 pw.print(" mLastLayer="); pw.println(mLastLayer);
7846 if (mSurface != null) {
7847 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7848 }
7849 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7850 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7851 if (mAppToken != null) {
7852 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7853 }
7854 if (mTargetAppToken != null) {
7855 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7856 }
7857 pw.print(prefix); pw.print("mViewVisibility=0x");
7858 pw.print(Integer.toHexString(mViewVisibility));
7859 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007860 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7861 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007862 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7863 pw.print(prefix); pw.print("mPolicyVisibility=");
7864 pw.print(mPolicyVisibility);
7865 pw.print(" mPolicyVisibilityAfterAnim=");
7866 pw.print(mPolicyVisibilityAfterAnim);
7867 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7868 }
7869 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007870 pw.print(" h="); pw.println(mRequestedHeight);
7871 if (mXOffset != 0 || mYOffset != 0) {
7872 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7873 pw.print(" y="); pw.println(mYOffset);
7874 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007875 pw.print(prefix); pw.print("mGivenContentInsets=");
7876 mGivenContentInsets.printShortString(pw);
7877 pw.print(" mGivenVisibleInsets=");
7878 mGivenVisibleInsets.printShortString(pw);
7879 pw.println();
7880 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7881 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7882 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7883 }
7884 pw.print(prefix); pw.print("mShownFrame=");
7885 mShownFrame.printShortString(pw);
7886 pw.print(" last="); mLastShownFrame.printShortString(pw);
7887 pw.println();
7888 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7889 pw.print(" last="); mLastFrame.printShortString(pw);
7890 pw.println();
7891 pw.print(prefix); pw.print("mContainingFrame=");
7892 mContainingFrame.printShortString(pw);
7893 pw.print(" mDisplayFrame=");
7894 mDisplayFrame.printShortString(pw);
7895 pw.println();
7896 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7897 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7898 pw.println();
7899 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7900 pw.print(" last="); mLastContentInsets.printShortString(pw);
7901 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7902 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7903 pw.println();
7904 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7905 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7906 pw.print(" mAlpha="); pw.print(mAlpha);
7907 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7908 }
7909 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7910 || mAnimation != null) {
7911 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7912 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7913 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7914 pw.print(" mAnimation="); pw.println(mAnimation);
7915 }
7916 if (mHasTransformation || mHasLocalTransformation) {
7917 pw.print(prefix); pw.print("XForm: has=");
7918 pw.print(mHasTransformation);
7919 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7920 pw.print(" "); mTransformation.printShortString(pw);
7921 pw.println();
7922 }
7923 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7924 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7925 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7926 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7927 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7928 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7929 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7930 pw.print(" mDestroying="); pw.print(mDestroying);
7931 pw.print(" mRemoved="); pw.println(mRemoved);
7932 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007933 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007934 pw.print(prefix); pw.print("mOrientationChanging=");
7935 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007936 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7937 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007938 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007939 if (mHScale != 1 || mVScale != 1) {
7940 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7941 pw.print(" mVScale="); pw.println(mVScale);
7942 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007943 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007944 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7945 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 }
7948
7949 @Override
7950 public String toString() {
7951 return "Window{"
7952 + Integer.toHexString(System.identityHashCode(this))
7953 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7954 }
7955 }
Romain Guy06882f82009-06-10 13:36:04 -07007956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 // -------------------------------------------------------------
7958 // Window Token State
7959 // -------------------------------------------------------------
7960
7961 class WindowToken {
7962 // The actual token.
7963 final IBinder token;
7964
7965 // The type of window this token is for, as per WindowManager.LayoutParams.
7966 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 // Set if this token was explicitly added by a client, so should
7969 // not be removed when all windows are removed.
7970 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007971
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007972 // For printing.
7973 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 // If this is an AppWindowToken, this is non-null.
7976 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 // All of the windows associated with this token.
7979 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7980
7981 // Is key dispatching paused for this token?
7982 boolean paused = false;
7983
7984 // Should this token's windows be hidden?
7985 boolean hidden;
7986
7987 // Temporary for finding which tokens no longer have visible windows.
7988 boolean hasVisible;
7989
Dianne Hackborna8f60182009-09-01 19:01:50 -07007990 // Set to true when this token is in a pending transaction where it
7991 // will be shown.
7992 boolean waitingToShow;
7993
7994 // Set to true when this token is in a pending transaction where it
7995 // will be hidden.
7996 boolean waitingToHide;
7997
7998 // Set to true when this token is in a pending transaction where its
7999 // windows will be put to the bottom of the list.
8000 boolean sendingToBottom;
8001
8002 // Set to true when this token is in a pending transaction where its
8003 // windows will be put to the top of the list.
8004 boolean sendingToTop;
8005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 WindowToken(IBinder _token, int type, boolean _explicit) {
8007 token = _token;
8008 windowType = type;
8009 explicit = _explicit;
8010 }
8011
8012 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008013 pw.print(prefix); pw.print("token="); pw.println(token);
8014 pw.print(prefix); pw.print("windows="); pw.println(windows);
8015 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8016 pw.print(" hidden="); pw.print(hidden);
8017 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008018 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8019 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8020 pw.print(" waitingToHide="); pw.print(waitingToHide);
8021 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8022 pw.print(" sendingToTop="); pw.println(sendingToTop);
8023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 }
8025
8026 @Override
8027 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008028 if (stringName == null) {
8029 StringBuilder sb = new StringBuilder();
8030 sb.append("WindowToken{");
8031 sb.append(Integer.toHexString(System.identityHashCode(this)));
8032 sb.append(" token="); sb.append(token); sb.append('}');
8033 stringName = sb.toString();
8034 }
8035 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008036 }
8037 };
8038
8039 class AppWindowToken extends WindowToken {
8040 // Non-null only for application tokens.
8041 final IApplicationToken appToken;
8042
8043 // All of the windows and child windows that are included in this
8044 // application token. Note this list is NOT sorted!
8045 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8046
8047 int groupId = -1;
8048 boolean appFullscreen;
8049 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 // These are used for determining when all windows associated with
8052 // an activity have been drawn, so they can be made visible together
8053 // at the same time.
8054 int lastTransactionSequence = mTransactionSequence-1;
8055 int numInterestingWindows;
8056 int numDrawnWindows;
8057 boolean inPendingTransaction;
8058 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 // Is this token going to be hidden in a little while? If so, it
8061 // won't be taken into account for setting the screen orientation.
8062 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008064 // Is this window's surface needed? This is almost like hidden, except
8065 // it will sometimes be true a little earlier: when the token has
8066 // been shown, but is still waiting for its app transition to execute
8067 // before making its windows shown.
8068 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008070 // Have we told the window clients to hide themselves?
8071 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 // Last visibility state we reported to the app token.
8074 boolean reportedVisible;
8075
8076 // Set to true when the token has been removed from the window mgr.
8077 boolean removed;
8078
8079 // Have we been asked to have this token keep the screen frozen?
8080 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008082 boolean animating;
8083 Animation animation;
8084 boolean hasTransformation;
8085 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 // Offset to the window of all layers in the token, for use by
8088 // AppWindowToken animations.
8089 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091 // Information about an application starting window if displayed.
8092 StartingData startingData;
8093 WindowState startingWindow;
8094 View startingView;
8095 boolean startingDisplayed;
8096 boolean startingMoved;
8097 boolean firstWindowDrawn;
8098
8099 AppWindowToken(IApplicationToken _token) {
8100 super(_token.asBinder(),
8101 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8102 appWindowToken = this;
8103 appToken = _token;
8104 }
Romain Guy06882f82009-06-10 13:36:04 -07008105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 public void setAnimation(Animation anim) {
8107 if (localLOGV) Log.v(
8108 TAG, "Setting animation in " + this + ": " + anim);
8109 animation = anim;
8110 animating = false;
8111 anim.restrictDuration(MAX_ANIMATION_DURATION);
8112 anim.scaleCurrentDuration(mTransitionAnimationScale);
8113 int zorder = anim.getZAdjustment();
8114 int adj = 0;
8115 if (zorder == Animation.ZORDER_TOP) {
8116 adj = TYPE_LAYER_OFFSET;
8117 } else if (zorder == Animation.ZORDER_BOTTOM) {
8118 adj = -TYPE_LAYER_OFFSET;
8119 }
Romain Guy06882f82009-06-10 13:36:04 -07008120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 if (animLayerAdjustment != adj) {
8122 animLayerAdjustment = adj;
8123 updateLayers();
8124 }
8125 }
Romain Guy06882f82009-06-10 13:36:04 -07008126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 public void setDummyAnimation() {
8128 if (animation == null) {
8129 if (localLOGV) Log.v(
8130 TAG, "Setting dummy animation in " + this);
8131 animation = sDummyAnimation;
8132 }
8133 }
8134
8135 public void clearAnimation() {
8136 if (animation != null) {
8137 animation = null;
8138 animating = true;
8139 }
8140 }
Romain Guy06882f82009-06-10 13:36:04 -07008141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 void updateLayers() {
8143 final int N = allAppWindows.size();
8144 final int adj = animLayerAdjustment;
8145 for (int i=0; i<N; i++) {
8146 WindowState w = allAppWindows.get(i);
8147 w.mAnimLayer = w.mLayer + adj;
8148 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8149 + w.mAnimLayer);
8150 if (w == mInputMethodTarget) {
8151 setInputMethodAnimLayerAdjustment(adj);
8152 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008153 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008154 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 }
8157 }
Romain Guy06882f82009-06-10 13:36:04 -07008158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 void sendAppVisibilityToClients() {
8160 final int N = allAppWindows.size();
8161 for (int i=0; i<N; i++) {
8162 WindowState win = allAppWindows.get(i);
8163 if (win == startingWindow && clientHidden) {
8164 // Don't hide the starting window.
8165 continue;
8166 }
8167 try {
8168 if (DEBUG_VISIBILITY) Log.v(TAG,
8169 "Setting visibility of " + win + ": " + (!clientHidden));
8170 win.mClient.dispatchAppVisibility(!clientHidden);
8171 } catch (RemoteException e) {
8172 }
8173 }
8174 }
Romain Guy06882f82009-06-10 13:36:04 -07008175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 void showAllWindowsLocked() {
8177 final int NW = allAppWindows.size();
8178 for (int i=0; i<NW; i++) {
8179 WindowState w = allAppWindows.get(i);
8180 if (DEBUG_VISIBILITY) Log.v(TAG,
8181 "performing show on: " + w);
8182 w.performShowLocked();
8183 }
8184 }
Romain Guy06882f82009-06-10 13:36:04 -07008185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 // This must be called while inside a transaction.
8187 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8188 if (!mDisplayFrozen) {
8189 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008191 if (animation == sDummyAnimation) {
8192 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008193 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008194 // when it is really time to animate, this will be set to
8195 // a real animation and the next call will execute normally.
8196 return false;
8197 }
Romain Guy06882f82009-06-10 13:36:04 -07008198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8200 if (!animating) {
8201 if (DEBUG_ANIM) Log.v(
8202 TAG, "Starting animation in " + this +
8203 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8204 + " scale=" + mTransitionAnimationScale
8205 + " allDrawn=" + allDrawn + " animating=" + animating);
8206 animation.initialize(dw, dh, dw, dh);
8207 animation.setStartTime(currentTime);
8208 animating = true;
8209 }
8210 transformation.clear();
8211 final boolean more = animation.getTransformation(
8212 currentTime, transformation);
8213 if (DEBUG_ANIM) Log.v(
8214 TAG, "Stepped animation in " + this +
8215 ": more=" + more + ", xform=" + transformation);
8216 if (more) {
8217 // we're done!
8218 hasTransformation = true;
8219 return true;
8220 }
8221 if (DEBUG_ANIM) Log.v(
8222 TAG, "Finished animation in " + this +
8223 " @ " + currentTime);
8224 animation = null;
8225 }
8226 } else if (animation != null) {
8227 // If the display is frozen, and there is a pending animation,
8228 // clear it and make sure we run the cleanup code.
8229 animating = true;
8230 animation = null;
8231 }
8232
8233 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 if (!animating) {
8236 return false;
8237 }
8238
8239 clearAnimation();
8240 animating = false;
8241 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8242 moveInputMethodWindowsIfNeededLocked(true);
8243 }
Romain Guy06882f82009-06-10 13:36:04 -07008244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 if (DEBUG_ANIM) Log.v(
8246 TAG, "Animation done in " + this
8247 + ": reportedVisible=" + reportedVisible);
8248
8249 transformation.clear();
8250 if (animLayerAdjustment != 0) {
8251 animLayerAdjustment = 0;
8252 updateLayers();
8253 }
Romain Guy06882f82009-06-10 13:36:04 -07008254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 final int N = windows.size();
8256 for (int i=0; i<N; i++) {
8257 ((WindowState)windows.get(i)).finishExit();
8258 }
8259 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 return false;
8262 }
8263
8264 void updateReportedVisibilityLocked() {
8265 if (appToken == null) {
8266 return;
8267 }
Romain Guy06882f82009-06-10 13:36:04 -07008268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 int numInteresting = 0;
8270 int numVisible = 0;
8271 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8274 final int N = allAppWindows.size();
8275 for (int i=0; i<N; i++) {
8276 WindowState win = allAppWindows.get(i);
8277 if (win == startingWindow || win.mAppFreezing) {
8278 continue;
8279 }
8280 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008281 Log.v(TAG, "Win " + win + ": isDrawn="
8282 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008283 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008284 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 Log.v(TAG, "Not displayed: s=" + win.mSurface
8286 + " pv=" + win.mPolicyVisibility
8287 + " dp=" + win.mDrawPending
8288 + " cdp=" + win.mCommitDrawPending
8289 + " ah=" + win.mAttachedHidden
8290 + " th="
8291 + (win.mAppToken != null
8292 ? win.mAppToken.hiddenRequested : false)
8293 + " a=" + win.mAnimating);
8294 }
8295 }
8296 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008297 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 if (!win.isAnimating()) {
8299 numVisible++;
8300 }
8301 nowGone = false;
8302 } else if (win.isAnimating()) {
8303 nowGone = false;
8304 }
8305 }
Romain Guy06882f82009-06-10 13:36:04 -07008306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8308 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8309 + numInteresting + " visible=" + numVisible);
8310 if (nowVisible != reportedVisible) {
8311 if (DEBUG_VISIBILITY) Log.v(
8312 TAG, "Visibility changed in " + this
8313 + ": vis=" + nowVisible);
8314 reportedVisible = nowVisible;
8315 Message m = mH.obtainMessage(
8316 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8317 nowVisible ? 1 : 0,
8318 nowGone ? 1 : 0,
8319 this);
8320 mH.sendMessage(m);
8321 }
8322 }
Romain Guy06882f82009-06-10 13:36:04 -07008323
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008324 WindowState findMainWindow() {
8325 int j = windows.size();
8326 while (j > 0) {
8327 j--;
8328 WindowState win = windows.get(j);
8329 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8330 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8331 return win;
8332 }
8333 }
8334 return null;
8335 }
8336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 void dump(PrintWriter pw, String prefix) {
8338 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008339 if (appToken != null) {
8340 pw.print(prefix); pw.println("app=true");
8341 }
8342 if (allAppWindows.size() > 0) {
8343 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8344 }
8345 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008346 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008347 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8348 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8349 pw.print(" clientHidden="); pw.print(clientHidden);
8350 pw.print(" willBeHidden="); pw.print(willBeHidden);
8351 pw.print(" reportedVisible="); pw.println(reportedVisible);
8352 if (paused || freezingScreen) {
8353 pw.print(prefix); pw.print("paused="); pw.print(paused);
8354 pw.print(" freezingScreen="); pw.println(freezingScreen);
8355 }
8356 if (numInterestingWindows != 0 || numDrawnWindows != 0
8357 || inPendingTransaction || allDrawn) {
8358 pw.print(prefix); pw.print("numInterestingWindows=");
8359 pw.print(numInterestingWindows);
8360 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8361 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8362 pw.print(" allDrawn="); pw.println(allDrawn);
8363 }
8364 if (animating || animation != null) {
8365 pw.print(prefix); pw.print("animating="); pw.print(animating);
8366 pw.print(" animation="); pw.println(animation);
8367 }
8368 if (animLayerAdjustment != 0) {
8369 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8370 }
8371 if (hasTransformation) {
8372 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8373 pw.print(" transformation="); transformation.printShortString(pw);
8374 pw.println();
8375 }
8376 if (startingData != null || removed || firstWindowDrawn) {
8377 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8378 pw.print(" removed="); pw.print(removed);
8379 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8380 }
8381 if (startingWindow != null || startingView != null
8382 || startingDisplayed || startingMoved) {
8383 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8384 pw.print(" startingView="); pw.print(startingView);
8385 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8386 pw.print(" startingMoved"); pw.println(startingMoved);
8387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 }
8389
8390 @Override
8391 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008392 if (stringName == null) {
8393 StringBuilder sb = new StringBuilder();
8394 sb.append("AppWindowToken{");
8395 sb.append(Integer.toHexString(System.identityHashCode(this)));
8396 sb.append(" token="); sb.append(token); sb.append('}');
8397 stringName = sb.toString();
8398 }
8399 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008400 }
8401 }
Romain Guy06882f82009-06-10 13:36:04 -07008402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008403 // -------------------------------------------------------------
8404 // DummyAnimation
8405 // -------------------------------------------------------------
8406
8407 // This is an animation that does nothing: it just immediately finishes
8408 // itself every time it is called. It is used as a stub animation in cases
8409 // where we want to synchronize multiple things that may be animating.
8410 static final class DummyAnimation extends Animation {
8411 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8412 return false;
8413 }
8414 }
8415 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 // -------------------------------------------------------------
8418 // Async Handler
8419 // -------------------------------------------------------------
8420
8421 static final class StartingData {
8422 final String pkg;
8423 final int theme;
8424 final CharSequence nonLocalizedLabel;
8425 final int labelRes;
8426 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8429 int _labelRes, int _icon) {
8430 pkg = _pkg;
8431 theme = _theme;
8432 nonLocalizedLabel = _nonLocalizedLabel;
8433 labelRes = _labelRes;
8434 icon = _icon;
8435 }
8436 }
8437
8438 private final class H extends Handler {
8439 public static final int REPORT_FOCUS_CHANGE = 2;
8440 public static final int REPORT_LOSING_FOCUS = 3;
8441 public static final int ANIMATE = 4;
8442 public static final int ADD_STARTING = 5;
8443 public static final int REMOVE_STARTING = 6;
8444 public static final int FINISHED_STARTING = 7;
8445 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8447 public static final int HOLD_SCREEN_CHANGED = 12;
8448 public static final int APP_TRANSITION_TIMEOUT = 13;
8449 public static final int PERSIST_ANIMATION_SCALE = 14;
8450 public static final int FORCE_GC = 15;
8451 public static final int ENABLE_SCREEN = 16;
8452 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008453 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008455 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008457 public H() {
8458 }
Romain Guy06882f82009-06-10 13:36:04 -07008459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 @Override
8461 public void handleMessage(Message msg) {
8462 switch (msg.what) {
8463 case REPORT_FOCUS_CHANGE: {
8464 WindowState lastFocus;
8465 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008467 synchronized(mWindowMap) {
8468 lastFocus = mLastFocus;
8469 newFocus = mCurrentFocus;
8470 if (lastFocus == newFocus) {
8471 // Focus is not changing, so nothing to do.
8472 return;
8473 }
8474 mLastFocus = newFocus;
8475 //Log.i(TAG, "Focus moving from " + lastFocus
8476 // + " to " + newFocus);
8477 if (newFocus != null && lastFocus != null
8478 && !newFocus.isDisplayedLw()) {
8479 //Log.i(TAG, "Delaying loss of focus...");
8480 mLosingFocus.add(lastFocus);
8481 lastFocus = null;
8482 }
8483 }
8484
8485 if (lastFocus != newFocus) {
8486 //System.out.println("Changing focus from " + lastFocus
8487 // + " to " + newFocus);
8488 if (newFocus != null) {
8489 try {
8490 //Log.i(TAG, "Gaining focus: " + newFocus);
8491 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8492 } catch (RemoteException e) {
8493 // Ignore if process has died.
8494 }
8495 }
8496
8497 if (lastFocus != null) {
8498 try {
8499 //Log.i(TAG, "Losing focus: " + lastFocus);
8500 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8501 } catch (RemoteException e) {
8502 // Ignore if process has died.
8503 }
8504 }
8505 }
8506 } break;
8507
8508 case REPORT_LOSING_FOCUS: {
8509 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 synchronized(mWindowMap) {
8512 losers = mLosingFocus;
8513 mLosingFocus = new ArrayList<WindowState>();
8514 }
8515
8516 final int N = losers.size();
8517 for (int i=0; i<N; i++) {
8518 try {
8519 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8520 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8521 } catch (RemoteException e) {
8522 // Ignore if process has died.
8523 }
8524 }
8525 } break;
8526
8527 case ANIMATE: {
8528 synchronized(mWindowMap) {
8529 mAnimationPending = false;
8530 performLayoutAndPlaceSurfacesLocked();
8531 }
8532 } break;
8533
8534 case ADD_STARTING: {
8535 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8536 final StartingData sd = wtoken.startingData;
8537
8538 if (sd == null) {
8539 // Animation has been canceled... do nothing.
8540 return;
8541 }
Romain Guy06882f82009-06-10 13:36:04 -07008542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8544 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 View view = null;
8547 try {
8548 view = mPolicy.addStartingWindow(
8549 wtoken.token, sd.pkg,
8550 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8551 sd.icon);
8552 } catch (Exception e) {
8553 Log.w(TAG, "Exception when adding starting window", e);
8554 }
8555
8556 if (view != null) {
8557 boolean abort = false;
8558
8559 synchronized(mWindowMap) {
8560 if (wtoken.removed || wtoken.startingData == null) {
8561 // If the window was successfully added, then
8562 // we need to remove it.
8563 if (wtoken.startingWindow != null) {
8564 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8565 "Aborted starting " + wtoken
8566 + ": removed=" + wtoken.removed
8567 + " startingData=" + wtoken.startingData);
8568 wtoken.startingWindow = null;
8569 wtoken.startingData = null;
8570 abort = true;
8571 }
8572 } else {
8573 wtoken.startingView = view;
8574 }
8575 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8576 "Added starting " + wtoken
8577 + ": startingWindow="
8578 + wtoken.startingWindow + " startingView="
8579 + wtoken.startingView);
8580 }
8581
8582 if (abort) {
8583 try {
8584 mPolicy.removeStartingWindow(wtoken.token, view);
8585 } catch (Exception e) {
8586 Log.w(TAG, "Exception when removing starting window", e);
8587 }
8588 }
8589 }
8590 } break;
8591
8592 case REMOVE_STARTING: {
8593 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8594 IBinder token = null;
8595 View view = null;
8596 synchronized (mWindowMap) {
8597 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8598 + wtoken + ": startingWindow="
8599 + wtoken.startingWindow + " startingView="
8600 + wtoken.startingView);
8601 if (wtoken.startingWindow != null) {
8602 view = wtoken.startingView;
8603 token = wtoken.token;
8604 wtoken.startingData = null;
8605 wtoken.startingView = null;
8606 wtoken.startingWindow = null;
8607 }
8608 }
8609 if (view != null) {
8610 try {
8611 mPolicy.removeStartingWindow(token, view);
8612 } catch (Exception e) {
8613 Log.w(TAG, "Exception when removing starting window", e);
8614 }
8615 }
8616 } break;
8617
8618 case FINISHED_STARTING: {
8619 IBinder token = null;
8620 View view = null;
8621 while (true) {
8622 synchronized (mWindowMap) {
8623 final int N = mFinishedStarting.size();
8624 if (N <= 0) {
8625 break;
8626 }
8627 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8628
8629 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8630 "Finished starting " + wtoken
8631 + ": startingWindow=" + wtoken.startingWindow
8632 + " startingView=" + wtoken.startingView);
8633
8634 if (wtoken.startingWindow == null) {
8635 continue;
8636 }
8637
8638 view = wtoken.startingView;
8639 token = wtoken.token;
8640 wtoken.startingData = null;
8641 wtoken.startingView = null;
8642 wtoken.startingWindow = null;
8643 }
8644
8645 try {
8646 mPolicy.removeStartingWindow(token, view);
8647 } catch (Exception e) {
8648 Log.w(TAG, "Exception when removing starting window", e);
8649 }
8650 }
8651 } break;
8652
8653 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8654 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8655
8656 boolean nowVisible = msg.arg1 != 0;
8657 boolean nowGone = msg.arg2 != 0;
8658
8659 try {
8660 if (DEBUG_VISIBILITY) Log.v(
8661 TAG, "Reporting visible in " + wtoken
8662 + " visible=" + nowVisible
8663 + " gone=" + nowGone);
8664 if (nowVisible) {
8665 wtoken.appToken.windowsVisible();
8666 } else {
8667 wtoken.appToken.windowsGone();
8668 }
8669 } catch (RemoteException ex) {
8670 }
8671 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 case WINDOW_FREEZE_TIMEOUT: {
8674 synchronized (mWindowMap) {
8675 Log.w(TAG, "Window freeze timeout expired.");
8676 int i = mWindows.size();
8677 while (i > 0) {
8678 i--;
8679 WindowState w = (WindowState)mWindows.get(i);
8680 if (w.mOrientationChanging) {
8681 w.mOrientationChanging = false;
8682 Log.w(TAG, "Force clearing orientation change: " + w);
8683 }
8684 }
8685 performLayoutAndPlaceSurfacesLocked();
8686 }
8687 break;
8688 }
Romain Guy06882f82009-06-10 13:36:04 -07008689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008690 case HOLD_SCREEN_CHANGED: {
8691 Session oldHold;
8692 Session newHold;
8693 synchronized (mWindowMap) {
8694 oldHold = mLastReportedHold;
8695 newHold = (Session)msg.obj;
8696 mLastReportedHold = newHold;
8697 }
Romain Guy06882f82009-06-10 13:36:04 -07008698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 if (oldHold != newHold) {
8700 try {
8701 if (oldHold != null) {
8702 mBatteryStats.noteStopWakelock(oldHold.mUid,
8703 "window",
8704 BatteryStats.WAKE_TYPE_WINDOW);
8705 }
8706 if (newHold != null) {
8707 mBatteryStats.noteStartWakelock(newHold.mUid,
8708 "window",
8709 BatteryStats.WAKE_TYPE_WINDOW);
8710 }
8711 } catch (RemoteException e) {
8712 }
8713 }
8714 break;
8715 }
Romain Guy06882f82009-06-10 13:36:04 -07008716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 case APP_TRANSITION_TIMEOUT: {
8718 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008719 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8721 "*** APP TRANSITION TIMEOUT");
8722 mAppTransitionReady = true;
8723 mAppTransitionTimeout = true;
8724 performLayoutAndPlaceSurfacesLocked();
8725 }
8726 }
8727 break;
8728 }
Romain Guy06882f82009-06-10 13:36:04 -07008729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 case PERSIST_ANIMATION_SCALE: {
8731 Settings.System.putFloat(mContext.getContentResolver(),
8732 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8733 Settings.System.putFloat(mContext.getContentResolver(),
8734 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8735 break;
8736 }
Romain Guy06882f82009-06-10 13:36:04 -07008737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 case FORCE_GC: {
8739 synchronized(mWindowMap) {
8740 if (mAnimationPending) {
8741 // If we are animating, don't do the gc now but
8742 // delay a bit so we don't interrupt the animation.
8743 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8744 2000);
8745 return;
8746 }
8747 // If we are currently rotating the display, it will
8748 // schedule a new message when done.
8749 if (mDisplayFrozen) {
8750 return;
8751 }
8752 mFreezeGcPending = 0;
8753 }
8754 Runtime.getRuntime().gc();
8755 break;
8756 }
Romain Guy06882f82009-06-10 13:36:04 -07008757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 case ENABLE_SCREEN: {
8759 performEnableScreen();
8760 break;
8761 }
Romain Guy06882f82009-06-10 13:36:04 -07008762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 case APP_FREEZE_TIMEOUT: {
8764 synchronized (mWindowMap) {
8765 Log.w(TAG, "App freeze timeout expired.");
8766 int i = mAppTokens.size();
8767 while (i > 0) {
8768 i--;
8769 AppWindowToken tok = mAppTokens.get(i);
8770 if (tok.freezingScreen) {
8771 Log.w(TAG, "Force clearing freeze: " + tok);
8772 unsetAppFreezingScreenLocked(tok, true, true);
8773 }
8774 }
8775 }
8776 break;
8777 }
Romain Guy06882f82009-06-10 13:36:04 -07008778
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008779 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008780 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008781 sendNewConfiguration();
8782 }
8783 break;
8784 }
Romain Guy06882f82009-06-10 13:36:04 -07008785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 }
8787 }
8788 }
8789
8790 // -------------------------------------------------------------
8791 // IWindowManager API
8792 // -------------------------------------------------------------
8793
8794 public IWindowSession openSession(IInputMethodClient client,
8795 IInputContext inputContext) {
8796 if (client == null) throw new IllegalArgumentException("null client");
8797 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8798 return new Session(client, inputContext);
8799 }
8800
8801 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8802 synchronized (mWindowMap) {
8803 // The focus for the client is the window immediately below
8804 // where we would place the input method window.
8805 int idx = findDesiredInputMethodWindowIndexLocked(false);
8806 WindowState imFocus;
8807 if (idx > 0) {
8808 imFocus = (WindowState)mWindows.get(idx-1);
8809 if (imFocus != null) {
8810 if (imFocus.mSession.mClient != null &&
8811 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8812 return true;
8813 }
8814 }
8815 }
8816 }
8817 return false;
8818 }
Romain Guy06882f82009-06-10 13:36:04 -07008819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 // -------------------------------------------------------------
8821 // Internals
8822 // -------------------------------------------------------------
8823
8824 final WindowState windowForClientLocked(Session session, IWindow client) {
8825 return windowForClientLocked(session, client.asBinder());
8826 }
Romain Guy06882f82009-06-10 13:36:04 -07008827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828 final WindowState windowForClientLocked(Session session, IBinder client) {
8829 WindowState win = mWindowMap.get(client);
8830 if (localLOGV) Log.v(
8831 TAG, "Looking up client " + client + ": " + win);
8832 if (win == null) {
8833 RuntimeException ex = new RuntimeException();
8834 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8835 return null;
8836 }
8837 if (session != null && win.mSession != session) {
8838 RuntimeException ex = new RuntimeException();
8839 Log.w(TAG, "Requested window " + client + " is in session " +
8840 win.mSession + ", not " + session, ex);
8841 return null;
8842 }
8843
8844 return win;
8845 }
8846
Dianne Hackborna8f60182009-09-01 19:01:50 -07008847 final void rebuildAppWindowListLocked() {
8848 int NW = mWindows.size();
8849 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008850 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008851 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008852
8853 // First remove all existing app windows.
8854 i=0;
8855 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008856 WindowState w = (WindowState)mWindows.get(i);
8857 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008858 WindowState win = (WindowState)mWindows.remove(i);
8859 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8860 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008861 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008862 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008863 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008864 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
8865 && lastWallpaper == i-1) {
8866 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008867 }
8868 i++;
8869 }
8870
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008871 // The wallpaper window(s) typically live at the bottom of the stack,
8872 // so skip them before adding app tokens.
8873 lastWallpaper++;
8874 i = lastWallpaper;
8875
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008876 // First add all of the exiting app tokens... these are no longer
8877 // in the main app list, but still have windows shown. We put them
8878 // in the back because now that the animation is over we no longer
8879 // will care about them.
8880 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008881 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008882 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8883 }
8884
8885 // And add in the still active app tokens in Z order.
8886 NT = mAppTokens.size();
8887 for (int j=0; j<NT; j++) {
8888 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008889 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008890
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008891 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008892 if (i != numRemoved) {
8893 Log.w(TAG, "Rebuild removed " + numRemoved
8894 + " windows but added " + i);
8895 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07008896 }
8897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 private final void assignLayersLocked() {
8899 int N = mWindows.size();
8900 int curBaseLayer = 0;
8901 int curLayer = 0;
8902 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 for (i=0; i<N; i++) {
8905 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008906 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8907 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 curLayer += WINDOW_LAYER_MULTIPLIER;
8909 w.mLayer = curLayer;
8910 } else {
8911 curBaseLayer = curLayer = w.mBaseLayer;
8912 w.mLayer = curLayer;
8913 }
8914 if (w.mTargetAppToken != null) {
8915 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8916 } else if (w.mAppToken != null) {
8917 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8918 } else {
8919 w.mAnimLayer = w.mLayer;
8920 }
8921 if (w.mIsImWindow) {
8922 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008923 } else if (w.mIsWallpaper) {
8924 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 }
8926 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8927 + w.mAnimLayer);
8928 //System.out.println(
8929 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8930 }
8931 }
8932
8933 private boolean mInLayout = false;
8934 private final void performLayoutAndPlaceSurfacesLocked() {
8935 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008936 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 throw new RuntimeException("Recursive call!");
8938 }
8939 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8940 return;
8941 }
8942
8943 boolean recoveringMemory = false;
8944 if (mForceRemoves != null) {
8945 recoveringMemory = true;
8946 // Wait a little it for things to settle down, and off we go.
8947 for (int i=0; i<mForceRemoves.size(); i++) {
8948 WindowState ws = mForceRemoves.get(i);
8949 Log.i(TAG, "Force removing: " + ws);
8950 removeWindowInnerLocked(ws.mSession, ws);
8951 }
8952 mForceRemoves = null;
8953 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8954 Object tmp = new Object();
8955 synchronized (tmp) {
8956 try {
8957 tmp.wait(250);
8958 } catch (InterruptedException e) {
8959 }
8960 }
8961 }
Romain Guy06882f82009-06-10 13:36:04 -07008962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 mInLayout = true;
8964 try {
8965 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008967 int i = mPendingRemove.size()-1;
8968 if (i >= 0) {
8969 while (i >= 0) {
8970 WindowState w = mPendingRemove.get(i);
8971 removeWindowInnerLocked(w.mSession, w);
8972 i--;
8973 }
8974 mPendingRemove.clear();
8975
8976 mInLayout = false;
8977 assignLayersLocked();
8978 mLayoutNeeded = true;
8979 performLayoutAndPlaceSurfacesLocked();
8980
8981 } else {
8982 mInLayout = false;
8983 if (mLayoutNeeded) {
8984 requestAnimationLocked(0);
8985 }
8986 }
8987 } catch (RuntimeException e) {
8988 mInLayout = false;
8989 Log.e(TAG, "Unhandled exception while layout out windows", e);
8990 }
8991 }
8992
8993 private final void performLayoutLockedInner() {
8994 final int dw = mDisplay.getWidth();
8995 final int dh = mDisplay.getHeight();
8996
8997 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008998 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008999 int i;
9000
9001 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009002
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009003 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004 mPolicy.beginLayoutLw(dw, dh);
9005
9006 // First perform layout of any root windows (not attached
9007 // to another window).
9008 int topAttached = -1;
9009 for (i = N-1; i >= 0; i--) {
9010 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009011
9012 // Don't do layout of a window if it is not visible, or
9013 // soon won't be visible, to avoid wasting time and funky
9014 // changes while a window is animating away.
9015 final AppWindowToken atoken = win.mAppToken;
9016 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009017 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009018 || win.mRootToken.hidden
9019 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009020 || win.mAttachedHidden
9021 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022
9023 // If this view is GONE, then skip it -- keep the current
9024 // frame, and let the caller know so they can ignore it
9025 // if they want. (We do the normal layout for INVISIBLE
9026 // windows, since that means "perform layout as normal,
9027 // just don't display").
9028 if (!gone || !win.mHaveFrame) {
9029 if (!win.mLayoutAttached) {
9030 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9031 } else {
9032 if (topAttached < 0) topAttached = i;
9033 }
9034 }
9035 }
Romain Guy06882f82009-06-10 13:36:04 -07009036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009037 // Now perform layout of attached windows, which usually
9038 // depend on the position of the window they are attached to.
9039 // XXX does not deal with windows that are attached to windows
9040 // that are themselves attached.
9041 for (i = topAttached; i >= 0; i--) {
9042 WindowState win = (WindowState) mWindows.get(i);
9043
9044 // If this view is GONE, then skip it -- keep the current
9045 // frame, and let the caller know so they can ignore it
9046 // if they want. (We do the normal layout for INVISIBLE
9047 // windows, since that means "perform layout as normal,
9048 // just don't display").
9049 if (win.mLayoutAttached) {
9050 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9051 || !win.mHaveFrame) {
9052 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9053 }
9054 }
9055 }
9056
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009057 int changes = mPolicy.finishLayoutLw();
9058 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9059 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9060 assignLayersLocked();
9061 }
9062 }
9063 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009064 mLayoutNeeded = false;
9065 } else if (repeats > 2) {
9066 Log.w(TAG, "Layout repeat aborted after too many iterations");
9067 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009068 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9069 Configuration newConfig = updateOrientationFromAppTokensLocked(
9070 null, null);
9071 if (newConfig != null) {
9072 mLayoutNeeded = true;
9073 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9074 }
9075 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009076 } else {
9077 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009078 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9079 Configuration newConfig = updateOrientationFromAppTokensLocked(
9080 null, null);
9081 if (newConfig != null) {
9082 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9083 }
9084 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009086 }
9087 }
Romain Guy06882f82009-06-10 13:36:04 -07009088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009089 private final void performLayoutAndPlaceSurfacesLockedInner(
9090 boolean recoveringMemory) {
9091 final long currentTime = SystemClock.uptimeMillis();
9092 final int dw = mDisplay.getWidth();
9093 final int dh = mDisplay.getHeight();
9094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 int i;
9096
9097 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009100 if (mFxSession == null) {
9101 mFxSession = new SurfaceSession();
9102 }
Romain Guy06882f82009-06-10 13:36:04 -07009103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009104 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9105
9106 // Initialize state of exiting tokens.
9107 for (i=mExitingTokens.size()-1; i>=0; i--) {
9108 mExitingTokens.get(i).hasVisible = false;
9109 }
9110
9111 // Initialize state of exiting applications.
9112 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9113 mExitingAppTokens.get(i).hasVisible = false;
9114 }
9115
9116 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 boolean orientationChangeComplete = true;
9118 Session holdScreen = null;
9119 float screenBrightness = -1;
9120 boolean focusDisplayed = false;
9121 boolean animating = false;
9122
9123 Surface.openTransaction();
9124 try {
9125 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009126 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009127
9128 do {
9129 final int transactionSequence = ++mTransactionSequence;
9130
9131 // Update animations of all applications, including those
9132 // associated with exiting/removed apps
9133 boolean tokensAnimating = false;
9134 final int NAT = mAppTokens.size();
9135 for (i=0; i<NAT; i++) {
9136 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9137 tokensAnimating = true;
9138 }
9139 }
9140 final int NEAT = mExitingAppTokens.size();
9141 for (i=0; i<NEAT; i++) {
9142 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9143 tokensAnimating = true;
9144 }
9145 }
9146
9147 animating = tokensAnimating;
9148 restart = false;
9149
9150 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009151 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009152 boolean focusMayChange = false;
9153 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009154
9155 mPolicy.beginAnimationLw(dw, dh);
9156
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009157 final int N = mWindows.size();
9158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009159 for (i=N-1; i>=0; i--) {
9160 WindowState w = (WindowState)mWindows.get(i);
9161
9162 final WindowManager.LayoutParams attrs = w.mAttrs;
9163
9164 if (w.mSurface != null) {
9165 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009166 if (w.commitFinishDrawingLocked(currentTime)) {
9167 if ((w.mAttrs.flags
9168 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009169 if (DEBUG_WALLPAPER) Log.v(TAG,
9170 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009171 wallpaperMayChange = true;
9172 }
9173 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009174
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009175 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9177 animating = true;
9178 //w.dump(" ");
9179 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009180 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9181 wallpaperMayChange = true;
9182 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009183
9184 if (mPolicy.doesForceHide(w, attrs)) {
9185 if (!wasAnimating && animating) {
9186 wallpaperForceHidingChanged = true;
9187 focusMayChange = true;
9188 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9189 forceHiding = true;
9190 }
9191 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9192 boolean changed;
9193 if (forceHiding) {
9194 changed = w.hideLw(false, false);
9195 } else {
9196 changed = w.showLw(false, false);
9197 if (changed && wallpaperForceHidingChanged
9198 && w.isReadyForDisplay()) {
9199 // Assume we will need to animate. If
9200 // we don't (because the wallpaper will
9201 // stay with the lock screen), then we will
9202 // clean up later.
9203 Animation a = mPolicy.createForceHideEnterAnimation();
9204 if (a != null) {
9205 w.setAnimation(a);
9206 }
9207 }
9208 }
9209 if (changed && (attrs.flags
9210 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9211 wallpaperMayChange = true;
9212 }
9213 }
9214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 mPolicy.animatingWindowLw(w, attrs);
9216 }
9217
9218 final AppWindowToken atoken = w.mAppToken;
9219 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9220 if (atoken.lastTransactionSequence != transactionSequence) {
9221 atoken.lastTransactionSequence = transactionSequence;
9222 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9223 atoken.startingDisplayed = false;
9224 }
9225 if ((w.isOnScreen() || w.mAttrs.type
9226 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9227 && !w.mExiting && !w.mDestroying) {
9228 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009229 Log.v(TAG, "Eval win " + w + ": isDrawn="
9230 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009232 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 Log.v(TAG, "Not displayed: s=" + w.mSurface
9234 + " pv=" + w.mPolicyVisibility
9235 + " dp=" + w.mDrawPending
9236 + " cdp=" + w.mCommitDrawPending
9237 + " ah=" + w.mAttachedHidden
9238 + " th=" + atoken.hiddenRequested
9239 + " a=" + w.mAnimating);
9240 }
9241 }
9242 if (w != atoken.startingWindow) {
9243 if (!atoken.freezingScreen || !w.mAppFreezing) {
9244 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009245 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009246 atoken.numDrawnWindows++;
9247 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9248 "tokenMayBeDrawn: " + atoken
9249 + " freezingScreen=" + atoken.freezingScreen
9250 + " mAppFreezing=" + w.mAppFreezing);
9251 tokenMayBeDrawn = true;
9252 }
9253 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009254 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 atoken.startingDisplayed = true;
9256 }
9257 }
9258 } else if (w.mReadyToShow) {
9259 w.performShowLocked();
9260 }
9261 }
9262
9263 if (mPolicy.finishAnimationLw()) {
9264 restart = true;
9265 }
9266
9267 if (tokenMayBeDrawn) {
9268 // See if any windows have been drawn, so they (and others
9269 // associated with them) can now be shown.
9270 final int NT = mTokenList.size();
9271 for (i=0; i<NT; i++) {
9272 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9273 if (wtoken == null) {
9274 continue;
9275 }
9276 if (wtoken.freezingScreen) {
9277 int numInteresting = wtoken.numInterestingWindows;
9278 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9279 if (DEBUG_VISIBILITY) Log.v(TAG,
9280 "allDrawn: " + wtoken
9281 + " interesting=" + numInteresting
9282 + " drawn=" + wtoken.numDrawnWindows);
9283 wtoken.showAllWindowsLocked();
9284 unsetAppFreezingScreenLocked(wtoken, false, true);
9285 orientationChangeComplete = true;
9286 }
9287 } else if (!wtoken.allDrawn) {
9288 int numInteresting = wtoken.numInterestingWindows;
9289 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9290 if (DEBUG_VISIBILITY) Log.v(TAG,
9291 "allDrawn: " + wtoken
9292 + " interesting=" + numInteresting
9293 + " drawn=" + wtoken.numDrawnWindows);
9294 wtoken.allDrawn = true;
9295 restart = true;
9296
9297 // We can now show all of the drawn windows!
9298 if (!mOpeningApps.contains(wtoken)) {
9299 wtoken.showAllWindowsLocked();
9300 }
9301 }
9302 }
9303 }
9304 }
9305
9306 // If we are ready to perform an app transition, check through
9307 // all of the app tokens to be shown and see if they are ready
9308 // to go.
9309 if (mAppTransitionReady) {
9310 int NN = mOpeningApps.size();
9311 boolean goodToGo = true;
9312 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9313 "Checking " + NN + " opening apps (frozen="
9314 + mDisplayFrozen + " timeout="
9315 + mAppTransitionTimeout + ")...");
9316 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9317 // If the display isn't frozen, wait to do anything until
9318 // all of the apps are ready. Otherwise just go because
9319 // we'll unfreeze the display when everyone is ready.
9320 for (i=0; i<NN && goodToGo; i++) {
9321 AppWindowToken wtoken = mOpeningApps.get(i);
9322 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9323 "Check opening app" + wtoken + ": allDrawn="
9324 + wtoken.allDrawn + " startingDisplayed="
9325 + wtoken.startingDisplayed);
9326 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9327 && !wtoken.startingMoved) {
9328 goodToGo = false;
9329 }
9330 }
9331 }
9332 if (goodToGo) {
9333 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9334 int transit = mNextAppTransition;
9335 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009336 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009338 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009340 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009341 mAppTransitionTimeout = false;
9342 mStartingIconInTransition = false;
9343 mSkipAppTransitionAnimation = false;
9344
9345 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9346
Dianne Hackborna8f60182009-09-01 19:01:50 -07009347 // If there are applications waiting to come to the
9348 // top of the stack, now is the time to move their windows.
9349 // (Note that we don't do apps going to the bottom
9350 // here -- we want to keep their windows in the old
9351 // Z-order until the animation completes.)
9352 if (mToTopApps.size() > 0) {
9353 NN = mAppTokens.size();
9354 for (i=0; i<NN; i++) {
9355 AppWindowToken wtoken = mAppTokens.get(i);
9356 if (wtoken.sendingToTop) {
9357 wtoken.sendingToTop = false;
9358 moveAppWindowsLocked(wtoken, NN, false);
9359 }
9360 }
9361 mToTopApps.clear();
9362 }
9363
Dianne Hackborn25994b42009-09-04 14:21:19 -07009364 WindowState oldWallpaper = mWallpaperTarget;
9365
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009366 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009367 wallpaperMayChange = false;
9368
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009369 // The top-most window will supply the layout params,
9370 // and we will determine it below.
9371 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009372 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009373 int bestAnimLayer = -1;
9374
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009375 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009376 "New wallpaper target=" + mWallpaperTarget
9377 + ", lower target=" + mLowerWallpaperTarget
9378 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009379 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009380 // Do a first pass through the tokens for two
9381 // things:
9382 // (1) Determine if both the closing and opening
9383 // app token sets are wallpaper targets, in which
9384 // case special animations are needed
9385 // (since the wallpaper needs to stay static
9386 // behind them).
9387 // (2) Find the layout params of the top-most
9388 // application window in the tokens, which is
9389 // what will control the animation theme.
9390 final int NC = mClosingApps.size();
9391 NN = NC + mOpeningApps.size();
9392 for (i=0; i<NN; i++) {
9393 AppWindowToken wtoken;
9394 int mode;
9395 if (i < NC) {
9396 wtoken = mClosingApps.get(i);
9397 mode = 1;
9398 } else {
9399 wtoken = mOpeningApps.get(i-NC);
9400 mode = 2;
9401 }
9402 if (mLowerWallpaperTarget != null) {
9403 if (mLowerWallpaperTarget.mAppToken == wtoken
9404 || mUpperWallpaperTarget.mAppToken == wtoken) {
9405 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009406 }
9407 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009408 if (wtoken.appFullscreen) {
9409 WindowState ws = wtoken.findMainWindow();
9410 if (ws != null) {
9411 // If this is a compatibility mode
9412 // window, we will always use its anim.
9413 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9414 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009415 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009416 bestAnimLayer = Integer.MAX_VALUE;
9417 } else if (ws.mLayer > bestAnimLayer) {
9418 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009419 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009420 bestAnimLayer = ws.mLayer;
9421 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009422 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009423 }
9424 }
9425
Dianne Hackborn25994b42009-09-04 14:21:19 -07009426 if (foundWallpapers == 3) {
9427 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9428 "Wallpaper animation!");
9429 switch (transit) {
9430 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9431 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9432 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9433 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9434 break;
9435 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9436 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9437 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9438 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9439 break;
9440 }
9441 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9442 "New transit: " + transit);
9443 } else if (oldWallpaper != null) {
9444 // We are transitioning from an activity with
9445 // a wallpaper to one without.
9446 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9447 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9448 "New transit away from wallpaper: " + transit);
9449 } else if (mWallpaperTarget != null) {
9450 // We are transitioning from an activity without
9451 // a wallpaper to now showing the wallpaper
9452 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9453 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9454 "New transit into wallpaper: " + transit);
9455 }
9456
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009457 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9458 mLastEnterAnimToken = animToken;
9459 mLastEnterAnimParams = animLp;
9460 } else if (mLastEnterAnimParams != null) {
9461 animLp = mLastEnterAnimParams;
9462 mLastEnterAnimToken = null;
9463 mLastEnterAnimParams = null;
9464 }
9465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466 NN = mOpeningApps.size();
9467 for (i=0; i<NN; i++) {
9468 AppWindowToken wtoken = mOpeningApps.get(i);
9469 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9470 "Now opening app" + wtoken);
9471 wtoken.reportedVisible = false;
9472 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009473 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009474 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009476 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009477 wtoken.showAllWindowsLocked();
9478 }
9479 NN = mClosingApps.size();
9480 for (i=0; i<NN; i++) {
9481 AppWindowToken wtoken = mClosingApps.get(i);
9482 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9483 "Now closing app" + wtoken);
9484 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009485 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009486 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009488 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 // Force the allDrawn flag, because we want to start
9490 // this guy's animations regardless of whether it's
9491 // gotten drawn.
9492 wtoken.allDrawn = true;
9493 }
9494
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009495 mNextAppTransitionPackage = null;
9496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009497 mOpeningApps.clear();
9498 mClosingApps.clear();
9499
9500 // This has changed the visibility of windows, so perform
9501 // a new layout to get them all up-to-date.
9502 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009503 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9504 assignLayersLocked();
9505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506 performLayoutLockedInner();
9507 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009508 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509
9510 restart = true;
9511 }
9512 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009513
Dianne Hackborna8f60182009-09-01 19:01:50 -07009514 if (!animating && mAppTransitionRunning) {
9515 // We have finished the animation of an app transition. To do
9516 // this, we have delayed a lot of operations like showing and
9517 // hiding apps, moving apps in Z-order, etc. The app token list
9518 // reflects the correct Z-order, but the window list may now
9519 // be out of sync with it. So here we will just rebuild the
9520 // entire app window list. Fun!
9521 mAppTransitionRunning = false;
9522 // Clear information about apps that were moving.
9523 mToBottomApps.clear();
9524
9525 rebuildAppWindowListLocked();
9526 restart = true;
9527 moveInputMethodWindowsIfNeededLocked(false);
9528 wallpaperMayChange = true;
9529 mLayoutNeeded = true;
9530 }
9531
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009532 int adjResult = 0;
9533
9534 if (wallpaperForceHidingChanged) {
9535 // At this point, there was a window with a wallpaper that
9536 // was force hiding other windows behind it, but now it
9537 // is going away. This may be simple -- just animate
9538 // away the wallpaper and its window -- or it may be
9539 // hard -- the wallpaper now needs to be shown behind
9540 // something that was hidden.
9541 WindowState oldWallpaper = mWallpaperTarget;
9542 adjResult = adjustWallpaperWindowsLocked();
9543 wallpaperMayChange = false;
9544 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9545 + " NEW: " + mWallpaperTarget);
9546 if (mLowerWallpaperTarget == null) {
9547 // Whoops, we don't need a special wallpaper animation.
9548 // Clear them out.
9549 forceHiding = false;
9550 for (i=N-1; i>=0; i--) {
9551 WindowState w = (WindowState)mWindows.get(i);
9552 if (w.mSurface != null) {
9553 final WindowManager.LayoutParams attrs = w.mAttrs;
9554 if (mPolicy.doesForceHide(w, attrs)) {
9555 forceHiding = true;
9556 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9557 if (!w.mAnimating) {
9558 // We set the animation above so it
9559 // is not yet running.
9560 w.clearAnimation();
9561 }
9562 }
9563 }
9564 }
9565 }
9566 }
9567
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009568 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009569 if (DEBUG_WALLPAPER) Log.v(TAG,
9570 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009571 adjResult = adjustWallpaperWindowsLocked();
9572 }
9573
9574 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9575 if (DEBUG_WALLPAPER) Log.v(TAG,
9576 "Wallpaper layer changed: assigning layers + relayout");
9577 restart = true;
9578 mLayoutNeeded = true;
9579 assignLayersLocked();
9580 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9581 if (DEBUG_WALLPAPER) Log.v(TAG,
9582 "Wallpaper visibility changed: relayout");
9583 restart = true;
9584 mLayoutNeeded = true;
9585 }
9586
9587 if (focusMayChange) {
9588 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009589 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009590 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009591 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009592 }
9593
9594 if (mLayoutNeeded) {
9595 restart = true;
9596 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009597 }
9598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599 } while (restart);
9600
9601 // THIRD LOOP: Update the surfaces of all windows.
9602
9603 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9604
9605 boolean obscured = false;
9606 boolean blurring = false;
9607 boolean dimming = false;
9608 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009609 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009610 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009611
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009612 final int N = mWindows.size();
9613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009614 for (i=N-1; i>=0; i--) {
9615 WindowState w = (WindowState)mWindows.get(i);
9616
9617 boolean displayed = false;
9618 final WindowManager.LayoutParams attrs = w.mAttrs;
9619 final int attrFlags = attrs.flags;
9620
9621 if (w.mSurface != null) {
9622 w.computeShownFrameLocked();
9623 if (localLOGV) Log.v(
9624 TAG, "Placing surface #" + i + " " + w.mSurface
9625 + ": new=" + w.mShownFrame + ", old="
9626 + w.mLastShownFrame);
9627
9628 boolean resize;
9629 int width, height;
9630 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9631 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9632 w.mLastRequestedHeight != w.mRequestedHeight;
9633 // for a scaled surface, we just want to use
9634 // the requested size.
9635 width = w.mRequestedWidth;
9636 height = w.mRequestedHeight;
9637 w.mLastRequestedWidth = width;
9638 w.mLastRequestedHeight = height;
9639 w.mLastShownFrame.set(w.mShownFrame);
9640 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009641 if (SHOW_TRANSACTIONS) Log.i(
9642 TAG, " SURFACE " + w.mSurface
9643 + ": POS " + w.mShownFrame.left
9644 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9646 } catch (RuntimeException e) {
9647 Log.w(TAG, "Error positioning surface in " + w, e);
9648 if (!recoveringMemory) {
9649 reclaimSomeSurfaceMemoryLocked(w, "position");
9650 }
9651 }
9652 } else {
9653 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9654 width = w.mShownFrame.width();
9655 height = w.mShownFrame.height();
9656 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009657 }
9658
9659 if (resize) {
9660 if (width < 1) width = 1;
9661 if (height < 1) height = 1;
9662 if (w.mSurface != null) {
9663 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009664 if (SHOW_TRANSACTIONS) Log.i(
9665 TAG, " SURFACE " + w.mSurface + ": POS "
9666 + w.mShownFrame.left + ","
9667 + w.mShownFrame.top + " SIZE "
9668 + w.mShownFrame.width() + "x"
9669 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009670 w.mSurface.setSize(width, height);
9671 w.mSurface.setPosition(w.mShownFrame.left,
9672 w.mShownFrame.top);
9673 } catch (RuntimeException e) {
9674 // If something goes wrong with the surface (such
9675 // as running out of memory), don't take down the
9676 // entire system.
9677 Log.e(TAG, "Failure updating surface of " + w
9678 + "size=(" + width + "x" + height
9679 + "), pos=(" + w.mShownFrame.left
9680 + "," + w.mShownFrame.top + ")", e);
9681 if (!recoveringMemory) {
9682 reclaimSomeSurfaceMemoryLocked(w, "size");
9683 }
9684 }
9685 }
9686 }
9687 if (!w.mAppFreezing) {
9688 w.mContentInsetsChanged =
9689 !w.mLastContentInsets.equals(w.mContentInsets);
9690 w.mVisibleInsetsChanged =
9691 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009692 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 || w.mContentInsetsChanged
9694 || w.mVisibleInsetsChanged) {
9695 w.mLastFrame.set(w.mFrame);
9696 w.mLastContentInsets.set(w.mContentInsets);
9697 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009698 // If the screen is currently frozen, then keep
9699 // it frozen until this window draws at its new
9700 // orientation.
9701 if (mDisplayFrozen) {
9702 if (DEBUG_ORIENTATION) Log.v(TAG,
9703 "Resizing while display frozen: " + w);
9704 w.mOrientationChanging = true;
9705 if (mWindowsFreezingScreen) {
9706 mWindowsFreezingScreen = true;
9707 // XXX should probably keep timeout from
9708 // when we first froze the display.
9709 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9710 mH.sendMessageDelayed(mH.obtainMessage(
9711 H.WINDOW_FREEZE_TIMEOUT), 2000);
9712 }
9713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 // If the orientation is changing, then we need to
9715 // hold off on unfreezing the display until this
9716 // window has been redrawn; to do that, we need
9717 // to go through the process of getting informed
9718 // by the application when it has finished drawing.
9719 if (w.mOrientationChanging) {
9720 if (DEBUG_ORIENTATION) Log.v(TAG,
9721 "Orientation start waiting for draw in "
9722 + w + ", surface " + w.mSurface);
9723 w.mDrawPending = true;
9724 w.mCommitDrawPending = false;
9725 w.mReadyToShow = false;
9726 if (w.mAppToken != null) {
9727 w.mAppToken.allDrawn = false;
9728 }
9729 }
Romain Guy06882f82009-06-10 13:36:04 -07009730 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009731 "Resizing window " + w + " to " + w.mFrame);
9732 mResizingWindows.add(w);
9733 } else if (w.mOrientationChanging) {
9734 if (!w.mDrawPending && !w.mCommitDrawPending) {
9735 if (DEBUG_ORIENTATION) Log.v(TAG,
9736 "Orientation not waiting for draw in "
9737 + w + ", surface " + w.mSurface);
9738 w.mOrientationChanging = false;
9739 }
9740 }
9741 }
9742
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009743 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009744 if (!w.mLastHidden) {
9745 //dump();
9746 w.mLastHidden = true;
9747 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009748 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 if (w.mSurface != null) {
9750 try {
9751 w.mSurface.hide();
9752 } catch (RuntimeException e) {
9753 Log.w(TAG, "Exception hiding surface in " + w);
9754 }
9755 }
9756 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9757 }
9758 // If we are waiting for this window to handle an
9759 // orientation change, well, it is hidden, so
9760 // doesn't really matter. Note that this does
9761 // introduce a potential glitch if the window
9762 // becomes unhidden before it has drawn for the
9763 // new orientation.
9764 if (w.mOrientationChanging) {
9765 w.mOrientationChanging = false;
9766 if (DEBUG_ORIENTATION) Log.v(TAG,
9767 "Orientation change skips hidden " + w);
9768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 } else if (w.mLastLayer != w.mAnimLayer
9770 || w.mLastAlpha != w.mShownAlpha
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 || w.mLastHidden) {
9778 displayed = true;
9779 w.mLastAlpha = w.mShownAlpha;
9780 w.mLastLayer = w.mAnimLayer;
9781 w.mLastDsDx = w.mDsDx;
9782 w.mLastDtDx = w.mDtDx;
9783 w.mLastDsDy = w.mDsDy;
9784 w.mLastDtDy = w.mDtDy;
9785 w.mLastHScale = w.mHScale;
9786 w.mLastVScale = w.mVScale;
9787 if (SHOW_TRANSACTIONS) Log.i(
9788 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009789 + w.mShownAlpha + " layer=" + w.mAnimLayer
9790 + " matrix=[" + (w.mDsDx*w.mHScale)
9791 + "," + (w.mDtDx*w.mVScale)
9792 + "][" + (w.mDsDy*w.mHScale)
9793 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 if (w.mSurface != null) {
9795 try {
9796 w.mSurface.setAlpha(w.mShownAlpha);
9797 w.mSurface.setLayer(w.mAnimLayer);
9798 w.mSurface.setMatrix(
9799 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9800 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9801 } catch (RuntimeException e) {
9802 Log.w(TAG, "Error updating surface in " + w, e);
9803 if (!recoveringMemory) {
9804 reclaimSomeSurfaceMemoryLocked(w, "update");
9805 }
9806 }
9807 }
9808
9809 if (w.mLastHidden && !w.mDrawPending
9810 && !w.mCommitDrawPending
9811 && !w.mReadyToShow) {
9812 if (SHOW_TRANSACTIONS) Log.i(
9813 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9814 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9815 + " during relayout");
9816 if (showSurfaceRobustlyLocked(w)) {
9817 w.mHasDrawn = true;
9818 w.mLastHidden = false;
9819 } else {
9820 w.mOrientationChanging = false;
9821 }
9822 }
9823 if (w.mSurface != null) {
9824 w.mToken.hasVisible = true;
9825 }
9826 } else {
9827 displayed = true;
9828 }
9829
9830 if (displayed) {
9831 if (!covered) {
9832 if (attrs.width == LayoutParams.FILL_PARENT
9833 && attrs.height == LayoutParams.FILL_PARENT) {
9834 covered = true;
9835 }
9836 }
9837 if (w.mOrientationChanging) {
9838 if (w.mDrawPending || w.mCommitDrawPending) {
9839 orientationChangeComplete = false;
9840 if (DEBUG_ORIENTATION) Log.v(TAG,
9841 "Orientation continue waiting for draw in " + w);
9842 } else {
9843 w.mOrientationChanging = false;
9844 if (DEBUG_ORIENTATION) Log.v(TAG,
9845 "Orientation change complete in " + w);
9846 }
9847 }
9848 w.mToken.hasVisible = true;
9849 }
9850 } else if (w.mOrientationChanging) {
9851 if (DEBUG_ORIENTATION) Log.v(TAG,
9852 "Orientation change skips hidden " + w);
9853 w.mOrientationChanging = false;
9854 }
9855
9856 final boolean canBeSeen = w.isDisplayedLw();
9857
9858 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9859 focusDisplayed = true;
9860 }
9861
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009862 final boolean obscuredChanged = w.mObscured != obscured;
9863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009864 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009865 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 if (w.mSurface != null) {
9867 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9868 holdScreen = w.mSession;
9869 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009870 if (!syswin && w.mAttrs.screenBrightness >= 0
9871 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 screenBrightness = w.mAttrs.screenBrightness;
9873 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009874 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9875 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9876 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9877 syswin = true;
9878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009880
Dianne Hackborn25994b42009-09-04 14:21:19 -07009881 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9882 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 // This window completely covers everything behind it,
9884 // so we want to leave all of them as unblurred (for
9885 // performance reasons).
9886 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009887 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9888 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009889 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009890 obscured = true;
9891 if (mBackgroundFillerSurface == null) {
9892 try {
9893 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9894 0, dw, dh,
9895 PixelFormat.OPAQUE,
9896 Surface.FX_SURFACE_NORMAL);
9897 } catch (Exception e) {
9898 Log.e(TAG, "Exception creating filler surface", e);
9899 }
9900 }
9901 try {
9902 mBackgroundFillerSurface.setPosition(0, 0);
9903 mBackgroundFillerSurface.setSize(dw, dh);
9904 // Using the same layer as Dim because they will never be shown at the
9905 // same time.
9906 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9907 mBackgroundFillerSurface.show();
9908 } catch (RuntimeException e) {
9909 Log.e(TAG, "Exception showing filler surface");
9910 }
9911 backgroundFillerShown = true;
9912 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009913 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009914 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9915 if (localLOGV) Log.v(TAG, "Win " + w
9916 + ": blurring=" + blurring
9917 + " obscured=" + obscured
9918 + " displayed=" + displayed);
9919 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9920 if (!dimming) {
9921 //Log.i(TAG, "DIM BEHIND: " + w);
9922 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009923 if (mDimAnimator == null) {
9924 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009925 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009926 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009928 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 }
9930 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9931 if (!blurring) {
9932 //Log.i(TAG, "BLUR BEHIND: " + w);
9933 blurring = true;
9934 mBlurShown = true;
9935 if (mBlurSurface == null) {
9936 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9937 + mBlurSurface + ": CREATE");
9938 try {
Romain Guy06882f82009-06-10 13:36:04 -07009939 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 -1, 16, 16,
9941 PixelFormat.OPAQUE,
9942 Surface.FX_SURFACE_BLUR);
9943 } catch (Exception e) {
9944 Log.e(TAG, "Exception creating Blur surface", e);
9945 }
9946 }
9947 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9948 + mBlurSurface + ": SHOW pos=(0,0) (" +
9949 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9950 if (mBlurSurface != null) {
9951 mBlurSurface.setPosition(0, 0);
9952 mBlurSurface.setSize(dw, dh);
9953 try {
9954 mBlurSurface.show();
9955 } catch (RuntimeException e) {
9956 Log.w(TAG, "Failure showing blur surface", e);
9957 }
9958 }
9959 }
9960 mBlurSurface.setLayer(w.mAnimLayer-2);
9961 }
9962 }
9963 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009964
9965 if (obscuredChanged && mWallpaperTarget == w) {
9966 // This is the wallpaper target and its obscured state
9967 // changed... make sure the current wallaper's visibility
9968 // has been updated accordingly.
9969 updateWallpaperVisibilityLocked();
9970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009972
9973 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9974 mBackgroundFillerShown = false;
9975 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9976 try {
9977 mBackgroundFillerSurface.hide();
9978 } catch (RuntimeException e) {
9979 Log.e(TAG, "Exception hiding filler surface", e);
9980 }
9981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009983 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9984 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 }
Romain Guy06882f82009-06-10 13:36:04 -07009986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009987 if (!blurring && mBlurShown) {
9988 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9989 + ": HIDE");
9990 try {
9991 mBlurSurface.hide();
9992 } catch (IllegalArgumentException e) {
9993 Log.w(TAG, "Illegal argument exception hiding blur surface");
9994 }
9995 mBlurShown = false;
9996 }
9997
9998 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9999 } catch (RuntimeException e) {
10000 Log.e(TAG, "Unhandled exception in Window Manager", e);
10001 }
10002
10003 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10006 "With display frozen, orientationChangeComplete="
10007 + orientationChangeComplete);
10008 if (orientationChangeComplete) {
10009 if (mWindowsFreezingScreen) {
10010 mWindowsFreezingScreen = false;
10011 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10012 }
10013 if (mAppsFreezingScreen == 0) {
10014 stopFreezingDisplayLocked();
10015 }
10016 }
Romain Guy06882f82009-06-10 13:36:04 -070010017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010018 i = mResizingWindows.size();
10019 if (i > 0) {
10020 do {
10021 i--;
10022 WindowState win = mResizingWindows.get(i);
10023 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010024 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10025 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 win.mClient.resized(win.mFrame.width(),
10027 win.mFrame.height(), win.mLastContentInsets,
10028 win.mLastVisibleInsets, win.mDrawPending);
10029 win.mContentInsetsChanged = false;
10030 win.mVisibleInsetsChanged = false;
10031 } catch (RemoteException e) {
10032 win.mOrientationChanging = false;
10033 }
10034 } while (i > 0);
10035 mResizingWindows.clear();
10036 }
Romain Guy06882f82009-06-10 13:36:04 -070010037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010039 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 i = mDestroySurface.size();
10041 if (i > 0) {
10042 do {
10043 i--;
10044 WindowState win = mDestroySurface.get(i);
10045 win.mDestroying = false;
10046 if (mInputMethodWindow == win) {
10047 mInputMethodWindow = null;
10048 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010049 if (win == mWallpaperTarget) {
10050 wallpaperDestroyed = true;
10051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 win.destroySurfaceLocked();
10053 } while (i > 0);
10054 mDestroySurface.clear();
10055 }
10056
10057 // Time to remove any exiting tokens?
10058 for (i=mExitingTokens.size()-1; i>=0; i--) {
10059 WindowToken token = mExitingTokens.get(i);
10060 if (!token.hasVisible) {
10061 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010062 if (token.windowType == TYPE_WALLPAPER) {
10063 mWallpaperTokens.remove(token);
10064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 }
10066 }
10067
10068 // Time to remove any exiting applications?
10069 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10070 AppWindowToken token = mExitingAppTokens.get(i);
10071 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010072 // Make sure there is no animation running on this token,
10073 // so any windows associated with it will be removed as
10074 // soon as their animations are complete
10075 token.animation = null;
10076 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010077 mAppTokens.remove(token);
10078 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010079 if (mLastEnterAnimToken == token) {
10080 mLastEnterAnimToken = null;
10081 mLastEnterAnimParams = null;
10082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 }
10084 }
10085
Dianne Hackborna8f60182009-09-01 19:01:50 -070010086 boolean needRelayout = false;
10087
10088 if (!animating && mAppTransitionRunning) {
10089 // We have finished the animation of an app transition. To do
10090 // this, we have delayed a lot of operations like showing and
10091 // hiding apps, moving apps in Z-order, etc. The app token list
10092 // reflects the correct Z-order, but the window list may now
10093 // be out of sync with it. So here we will just rebuild the
10094 // entire app window list. Fun!
10095 mAppTransitionRunning = false;
10096 needRelayout = true;
10097 rebuildAppWindowListLocked();
10098 // Clear information about apps that were moving.
10099 mToBottomApps.clear();
10100 }
10101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010102 if (focusDisplayed) {
10103 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10104 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010105 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010106 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010107 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010108 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010109 requestAnimationLocked(0);
10110 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10112 }
10113 mQueue.setHoldScreenLocked(holdScreen != null);
10114 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10115 mPowerManager.setScreenBrightnessOverride(-1);
10116 } else {
10117 mPowerManager.setScreenBrightnessOverride((int)
10118 (screenBrightness * Power.BRIGHTNESS_ON));
10119 }
10120 if (holdScreen != mHoldingScreenOn) {
10121 mHoldingScreenOn = holdScreen;
10122 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10123 mH.sendMessage(m);
10124 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010125
10126 if (mTurnOnScreen) {
10127 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10128 LocalPowerManager.BUTTON_EVENT, true);
10129 mTurnOnScreen = false;
10130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010131 }
10132
10133 void requestAnimationLocked(long delay) {
10134 if (!mAnimationPending) {
10135 mAnimationPending = true;
10136 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10137 }
10138 }
Romain Guy06882f82009-06-10 13:36:04 -070010139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010140 /**
10141 * Have the surface flinger show a surface, robustly dealing with
10142 * error conditions. In particular, if there is not enough memory
10143 * to show the surface, then we will try to get rid of other surfaces
10144 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 * @return Returns true if the surface was successfully shown.
10147 */
10148 boolean showSurfaceRobustlyLocked(WindowState win) {
10149 try {
10150 if (win.mSurface != null) {
10151 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010152 if (win.mTurnOnScreen) {
10153 win.mTurnOnScreen = false;
10154 mTurnOnScreen = true;
10155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 }
10157 return true;
10158 } catch (RuntimeException e) {
10159 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10160 }
Romain Guy06882f82009-06-10 13:36:04 -070010161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 return false;
10165 }
Romain Guy06882f82009-06-10 13:36:04 -070010166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10168 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10171 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010173 if (mForceRemoves == null) {
10174 mForceRemoves = new ArrayList<WindowState>();
10175 }
Romain Guy06882f82009-06-10 13:36:04 -070010176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010177 long callingIdentity = Binder.clearCallingIdentity();
10178 try {
10179 // There was some problem... first, do a sanity check of the
10180 // window list to make sure we haven't left any dangling surfaces
10181 // around.
10182 int N = mWindows.size();
10183 boolean leakedSurface = false;
10184 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10185 for (int i=0; i<N; i++) {
10186 WindowState ws = (WindowState)mWindows.get(i);
10187 if (ws.mSurface != null) {
10188 if (!mSessions.contains(ws.mSession)) {
10189 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10190 + ws + " surface=" + ws.mSurface
10191 + " token=" + win.mToken
10192 + " pid=" + ws.mSession.mPid
10193 + " uid=" + ws.mSession.mUid);
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 mForceRemoves.add(ws);
10197 i--;
10198 N--;
10199 leakedSurface = true;
10200 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10201 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10202 + ws + " surface=" + ws.mSurface
10203 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010204 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 ws.mSurface = null;
10206 leakedSurface = true;
10207 }
10208 }
10209 }
Romain Guy06882f82009-06-10 13:36:04 -070010210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010211 boolean killedApps = false;
10212 if (!leakedSurface) {
10213 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10214 SparseIntArray pidCandidates = new SparseIntArray();
10215 for (int i=0; i<N; i++) {
10216 WindowState ws = (WindowState)mWindows.get(i);
10217 if (ws.mSurface != null) {
10218 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10219 }
10220 }
10221 if (pidCandidates.size() > 0) {
10222 int[] pids = new int[pidCandidates.size()];
10223 for (int i=0; i<pids.length; i++) {
10224 pids[i] = pidCandidates.keyAt(i);
10225 }
10226 try {
10227 if (mActivityManager.killPidsForMemory(pids)) {
10228 killedApps = true;
10229 }
10230 } catch (RemoteException e) {
10231 }
10232 }
10233 }
Romain Guy06882f82009-06-10 13:36:04 -070010234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 if (leakedSurface || killedApps) {
10236 // We managed to reclaim some memory, so get rid of the trouble
10237 // surface and ask the app to request another one.
10238 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10239 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010240 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 win.mSurface = null;
10242 }
Romain Guy06882f82009-06-10 13:36:04 -070010243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010244 try {
10245 win.mClient.dispatchGetNewSurface();
10246 } catch (RemoteException e) {
10247 }
10248 }
10249 } finally {
10250 Binder.restoreCallingIdentity(callingIdentity);
10251 }
10252 }
Romain Guy06882f82009-06-10 13:36:04 -070010253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010254 private boolean updateFocusedWindowLocked(int mode) {
10255 WindowState newFocus = computeFocusedWindowLocked();
10256 if (mCurrentFocus != newFocus) {
10257 // This check makes sure that we don't already have the focus
10258 // change message pending.
10259 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10260 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10261 if (localLOGV) Log.v(
10262 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10263 final WindowState oldFocus = mCurrentFocus;
10264 mCurrentFocus = newFocus;
10265 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 final WindowState imWindow = mInputMethodWindow;
10268 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010269 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010270 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010271 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10272 mLayoutNeeded = true;
10273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010274 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10275 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010276 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10277 // Client will do the layout, but we need to assign layers
10278 // for handleNewWindowLocked() below.
10279 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 }
10281 }
Romain Guy06882f82009-06-10 13:36:04 -070010282
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010283 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10284 mKeyWaiter.handleNewWindowLocked(newFocus);
10285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 return true;
10287 }
10288 return false;
10289 }
10290
10291 private WindowState computeFocusedWindowLocked() {
10292 WindowState result = null;
10293 WindowState win;
10294
10295 int i = mWindows.size() - 1;
10296 int nextAppIndex = mAppTokens.size()-1;
10297 WindowToken nextApp = nextAppIndex >= 0
10298 ? mAppTokens.get(nextAppIndex) : null;
10299
10300 while (i >= 0) {
10301 win = (WindowState)mWindows.get(i);
10302
10303 if (localLOGV || DEBUG_FOCUS) Log.v(
10304 TAG, "Looking for focus: " + i
10305 + " = " + win
10306 + ", flags=" + win.mAttrs.flags
10307 + ", canReceive=" + win.canReceiveKeys());
10308
10309 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 // If this window's application has been removed, just skip it.
10312 if (thisApp != null && thisApp.removed) {
10313 i--;
10314 continue;
10315 }
Romain Guy06882f82009-06-10 13:36:04 -070010316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 // If there is a focused app, don't allow focus to go to any
10318 // windows below it. If this is an application window, step
10319 // through the app tokens until we find its app.
10320 if (thisApp != null && nextApp != null && thisApp != nextApp
10321 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10322 int origAppIndex = nextAppIndex;
10323 while (nextAppIndex > 0) {
10324 if (nextApp == mFocusedApp) {
10325 // Whoops, we are below the focused app... no focus
10326 // for you!
10327 if (localLOGV || DEBUG_FOCUS) Log.v(
10328 TAG, "Reached focused app: " + mFocusedApp);
10329 return null;
10330 }
10331 nextAppIndex--;
10332 nextApp = mAppTokens.get(nextAppIndex);
10333 if (nextApp == thisApp) {
10334 break;
10335 }
10336 }
10337 if (thisApp != nextApp) {
10338 // Uh oh, the app token doesn't exist! This shouldn't
10339 // happen, but if it does we can get totally hosed...
10340 // so restart at the original app.
10341 nextAppIndex = origAppIndex;
10342 nextApp = mAppTokens.get(nextAppIndex);
10343 }
10344 }
10345
10346 // Dispatch to this window if it is wants key events.
10347 if (win.canReceiveKeys()) {
10348 if (DEBUG_FOCUS) Log.v(
10349 TAG, "Found focus @ " + i + " = " + win);
10350 result = win;
10351 break;
10352 }
10353
10354 i--;
10355 }
10356
10357 return result;
10358 }
10359
10360 private void startFreezingDisplayLocked() {
10361 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010362 // Freezing the display also suspends key event delivery, to
10363 // keep events from going astray while the display is reconfigured.
10364 // If someone has changed orientation again while the screen is
10365 // still frozen, the events will continue to be blocked while the
10366 // successive orientation change is processed. To prevent spurious
10367 // ANRs, we reset the event dispatch timeout in this case.
10368 synchronized (mKeyWaiter) {
10369 mKeyWaiter.mWasFrozen = true;
10370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010371 return;
10372 }
Romain Guy06882f82009-06-10 13:36:04 -070010373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 long now = SystemClock.uptimeMillis();
10377 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10378 if (mFreezeGcPending != 0) {
10379 if (now > (mFreezeGcPending+1000)) {
10380 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10381 mH.removeMessages(H.FORCE_GC);
10382 Runtime.getRuntime().gc();
10383 mFreezeGcPending = now;
10384 }
10385 } else {
10386 mFreezeGcPending = now;
10387 }
Romain Guy06882f82009-06-10 13:36:04 -070010388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010390 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10391 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010392 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010393 mAppTransitionReady = true;
10394 }
Romain Guy06882f82009-06-10 13:36:04 -070010395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010396 if (PROFILE_ORIENTATION) {
10397 File file = new File("/data/system/frozen");
10398 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10399 }
10400 Surface.freezeDisplay(0);
10401 }
Romain Guy06882f82009-06-10 13:36:04 -070010402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 private void stopFreezingDisplayLocked() {
10404 if (!mDisplayFrozen) {
10405 return;
10406 }
Romain Guy06882f82009-06-10 13:36:04 -070010407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 mDisplayFrozen = false;
10409 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10410 if (PROFILE_ORIENTATION) {
10411 Debug.stopMethodTracing();
10412 }
10413 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010414
Chris Tate2ad63a92009-03-25 17:36:48 -070010415 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10416 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 synchronized (mKeyWaiter) {
10418 mKeyWaiter.mWasFrozen = true;
10419 mKeyWaiter.notifyAll();
10420 }
10421
10422 // A little kludge: a lot could have happened while the
10423 // display was frozen, so now that we are coming back we
10424 // do a gc so that any remote references the system
10425 // processes holds on others can be released if they are
10426 // no longer needed.
10427 mH.removeMessages(H.FORCE_GC);
10428 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10429 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 mScreenFrozenLock.release();
10432 }
Romain Guy06882f82009-06-10 13:36:04 -070010433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 @Override
10435 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10436 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10437 != PackageManager.PERMISSION_GRANTED) {
10438 pw.println("Permission Denial: can't dump WindowManager from from pid="
10439 + Binder.getCallingPid()
10440 + ", uid=" + Binder.getCallingUid());
10441 return;
10442 }
Romain Guy06882f82009-06-10 13:36:04 -070010443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 synchronized(mWindowMap) {
10445 pw.println("Current Window Manager state:");
10446 for (int i=mWindows.size()-1; i>=0; i--) {
10447 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010448 pw.print(" Window #"); pw.print(i); pw.print(' ');
10449 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010450 w.dump(pw, " ");
10451 }
10452 if (mInputMethodDialogs.size() > 0) {
10453 pw.println(" ");
10454 pw.println(" Input method dialogs:");
10455 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10456 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010457 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 }
10459 }
10460 if (mPendingRemove.size() > 0) {
10461 pw.println(" ");
10462 pw.println(" Remove pending for:");
10463 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10464 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010465 pw.print(" Remove #"); 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 (mForceRemoves != null && mForceRemoves.size() > 0) {
10471 pw.println(" ");
10472 pw.println(" Windows force removing:");
10473 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10474 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010475 pw.print(" Removing #"); 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 (mDestroySurface.size() > 0) {
10481 pw.println(" ");
10482 pw.println(" Windows waiting to destroy their surface:");
10483 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10484 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010485 pw.print(" Destroy #"); 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 }
10490 if (mLosingFocus.size() > 0) {
10491 pw.println(" ");
10492 pw.println(" Windows losing focus:");
10493 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10494 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010495 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10496 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 w.dump(pw, " ");
10498 }
10499 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010500 if (mResizingWindows.size() > 0) {
10501 pw.println(" ");
10502 pw.println(" Windows waiting to resize:");
10503 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10504 WindowState w = mResizingWindows.get(i);
10505 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10506 pw.print(w); pw.println(":");
10507 w.dump(pw, " ");
10508 }
10509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 if (mSessions.size() > 0) {
10511 pw.println(" ");
10512 pw.println(" All active sessions:");
10513 Iterator<Session> it = mSessions.iterator();
10514 while (it.hasNext()) {
10515 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010516 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 s.dump(pw, " ");
10518 }
10519 }
10520 if (mTokenMap.size() > 0) {
10521 pw.println(" ");
10522 pw.println(" All tokens:");
10523 Iterator<WindowToken> it = mTokenMap.values().iterator();
10524 while (it.hasNext()) {
10525 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010526 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010527 token.dump(pw, " ");
10528 }
10529 }
10530 if (mTokenList.size() > 0) {
10531 pw.println(" ");
10532 pw.println(" Window token list:");
10533 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010534 pw.print(" #"); pw.print(i); pw.print(": ");
10535 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 }
10537 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010538 if (mWallpaperTokens.size() > 0) {
10539 pw.println(" ");
10540 pw.println(" Wallpaper tokens:");
10541 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10542 WindowToken token = mWallpaperTokens.get(i);
10543 pw.print(" Wallpaper #"); pw.print(i);
10544 pw.print(' '); pw.print(token); pw.println(':');
10545 token.dump(pw, " ");
10546 }
10547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 if (mAppTokens.size() > 0) {
10549 pw.println(" ");
10550 pw.println(" Application tokens in Z order:");
10551 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010552 pw.print(" App #"); pw.print(i); pw.print(": ");
10553 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 }
10555 }
10556 if (mFinishedStarting.size() > 0) {
10557 pw.println(" ");
10558 pw.println(" Finishing start of application tokens:");
10559 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10560 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010561 pw.print(" Finished Starting #"); 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 (mExitingTokens.size() > 0) {
10567 pw.println(" ");
10568 pw.println(" Exiting tokens:");
10569 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10570 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010571 pw.print(" Exiting #"); 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 if (mExitingAppTokens.size() > 0) {
10577 pw.println(" ");
10578 pw.println(" Exiting application tokens:");
10579 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10580 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010581 pw.print(" Exiting App #"); pw.print(i);
10582 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 token.dump(pw, " ");
10584 }
10585 }
10586 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010587 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10588 pw.print(" mLastFocus="); pw.println(mLastFocus);
10589 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10590 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10591 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010592 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010593 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10594 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10595 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10596 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010597 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10598 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10599 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10600 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10601 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010602 if (mDimAnimator != null) {
10603 mDimAnimator.printTo(pw);
10604 } else {
10605 pw.print( " no DimAnimator ");
10606 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010607 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010608 pw.print(mInputMethodAnimLayerAdjustment);
10609 pw.print(" mWallpaperAnimLayerAdjustment=");
10610 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010611 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10612 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010613 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10614 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10615 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10616 pw.print(" mRotation="); pw.print(mRotation);
10617 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10618 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10619 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10620 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10621 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10622 pw.print(" mNextAppTransition=0x");
10623 pw.print(Integer.toHexString(mNextAppTransition));
10624 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010625 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010626 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010627 if (mNextAppTransitionPackage != null) {
10628 pw.print(" mNextAppTransitionPackage=");
10629 pw.print(mNextAppTransitionPackage);
10630 pw.print(", mNextAppTransitionEnter=0x");
10631 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10632 pw.print(", mNextAppTransitionExit=0x");
10633 pw.print(Integer.toHexString(mNextAppTransitionExit));
10634 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010635 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10636 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010637 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10638 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10639 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10640 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010641 if (mOpeningApps.size() > 0) {
10642 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10643 }
10644 if (mClosingApps.size() > 0) {
10645 pw.print(" mClosingApps="); pw.println(mClosingApps);
10646 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010647 if (mToTopApps.size() > 0) {
10648 pw.print(" mToTopApps="); pw.println(mToTopApps);
10649 }
10650 if (mToBottomApps.size() > 0) {
10651 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10652 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010653 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10654 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010656 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10657 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10658 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10659 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10660 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10661 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 }
10663 }
10664
10665 public void monitor() {
10666 synchronized (mWindowMap) { }
10667 synchronized (mKeyguardDisabled) { }
10668 synchronized (mKeyWaiter) { }
10669 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010670
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010671 public void virtualKeyFeedback(KeyEvent event) {
10672 mPolicy.keyFeedbackFromInput(event);
10673 }
10674
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010675 /**
10676 * DimAnimator class that controls the dim animation. This holds the surface and
10677 * all state used for dim animation.
10678 */
10679 private static class DimAnimator {
10680 Surface mDimSurface;
10681 boolean mDimShown = false;
10682 float mDimCurrentAlpha;
10683 float mDimTargetAlpha;
10684 float mDimDeltaPerMs;
10685 long mLastDimAnimTime;
10686
10687 DimAnimator (SurfaceSession session) {
10688 if (mDimSurface == null) {
10689 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10690 + mDimSurface + ": CREATE");
10691 try {
10692 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10693 Surface.FX_SURFACE_DIM);
10694 } catch (Exception e) {
10695 Log.e(TAG, "Exception creating Dim surface", e);
10696 }
10697 }
10698 }
10699
10700 /**
10701 * Show the dim surface.
10702 */
10703 void show(int dw, int dh) {
10704 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10705 dw + "x" + dh + ")");
10706 mDimShown = true;
10707 try {
10708 mDimSurface.setPosition(0, 0);
10709 mDimSurface.setSize(dw, dh);
10710 mDimSurface.show();
10711 } catch (RuntimeException e) {
10712 Log.w(TAG, "Failure showing dim surface", e);
10713 }
10714 }
10715
10716 /**
10717 * Set's the dim surface's layer and update dim parameters that will be used in
10718 * {@link updateSurface} after all windows are examined.
10719 */
10720 void updateParameters(WindowState w, long currentTime) {
10721 mDimSurface.setLayer(w.mAnimLayer-1);
10722
10723 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010724 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10725 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010726 if (mDimTargetAlpha != target) {
10727 // If the desired dim level has changed, then
10728 // start an animation to it.
10729 mLastDimAnimTime = currentTime;
10730 long duration = (w.mAnimating && w.mAnimation != null)
10731 ? w.mAnimation.computeDurationHint()
10732 : DEFAULT_DIM_DURATION;
10733 if (target > mDimTargetAlpha) {
10734 // This is happening behind the activity UI,
10735 // so we can make it run a little longer to
10736 // give a stronger impression without disrupting
10737 // the user.
10738 duration *= DIM_DURATION_MULTIPLIER;
10739 }
10740 if (duration < 1) {
10741 // Don't divide by zero
10742 duration = 1;
10743 }
10744 mDimTargetAlpha = target;
10745 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10746 }
10747 }
10748
10749 /**
10750 * Updating the surface's alpha. Returns true if the animation continues, or returns
10751 * false when the animation is finished and the dim surface is hidden.
10752 */
10753 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10754 if (!dimming) {
10755 if (mDimTargetAlpha != 0) {
10756 mLastDimAnimTime = currentTime;
10757 mDimTargetAlpha = 0;
10758 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10759 }
10760 }
10761
10762 boolean animating = false;
10763 if (mLastDimAnimTime != 0) {
10764 mDimCurrentAlpha += mDimDeltaPerMs
10765 * (currentTime-mLastDimAnimTime);
10766 boolean more = true;
10767 if (displayFrozen) {
10768 // If the display is frozen, there is no reason to animate.
10769 more = false;
10770 } else if (mDimDeltaPerMs > 0) {
10771 if (mDimCurrentAlpha > mDimTargetAlpha) {
10772 more = false;
10773 }
10774 } else if (mDimDeltaPerMs < 0) {
10775 if (mDimCurrentAlpha < mDimTargetAlpha) {
10776 more = false;
10777 }
10778 } else {
10779 more = false;
10780 }
10781
10782 // Do we need to continue animating?
10783 if (more) {
10784 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10785 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10786 mLastDimAnimTime = currentTime;
10787 mDimSurface.setAlpha(mDimCurrentAlpha);
10788 animating = true;
10789 } else {
10790 mDimCurrentAlpha = mDimTargetAlpha;
10791 mLastDimAnimTime = 0;
10792 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10793 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10794 mDimSurface.setAlpha(mDimCurrentAlpha);
10795 if (!dimming) {
10796 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10797 + ": HIDE");
10798 try {
10799 mDimSurface.hide();
10800 } catch (RuntimeException e) {
10801 Log.w(TAG, "Illegal argument exception hiding dim surface");
10802 }
10803 mDimShown = false;
10804 }
10805 }
10806 }
10807 return animating;
10808 }
10809
10810 public void printTo(PrintWriter pw) {
10811 pw.print(" mDimShown="); pw.print(mDimShown);
10812 pw.print(" current="); pw.print(mDimCurrentAlpha);
10813 pw.print(" target="); pw.print(mDimTargetAlpha);
10814 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10815 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10816 }
10817 }
10818
10819 /**
10820 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10821 * This is used for opening/closing transition for apps in compatible mode.
10822 */
10823 private static class FadeInOutAnimation extends Animation {
10824 int mWidth;
10825 boolean mFadeIn;
10826
10827 public FadeInOutAnimation(boolean fadeIn) {
10828 setInterpolator(new AccelerateInterpolator());
10829 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10830 mFadeIn = fadeIn;
10831 }
10832
10833 @Override
10834 protected void applyTransformation(float interpolatedTime, Transformation t) {
10835 float x = interpolatedTime;
10836 if (!mFadeIn) {
10837 x = 1.0f - x; // reverse the interpolation for fade out
10838 }
10839 if (x < 0.5) {
10840 // move the window out of the screen.
10841 t.getMatrix().setTranslate(mWidth, 0);
10842 } else {
10843 t.getMatrix().setTranslate(0, 0);// show
10844 t.setAlpha((x - 0.5f) * 2);
10845 }
10846 }
10847
10848 @Override
10849 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10850 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10851 mWidth = width;
10852 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010853
10854 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010855 public int getZAdjustment() {
10856 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010857 }
10858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859}