blob: c009ea22c74e168515c3c4368a676b957914b07b [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;
Dianne Hackborn75804932009-10-20 20:15:20 -070066import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Debug;
68import android.os.Handler;
69import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070070import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.LocalPowerManager;
72import android.os.Looper;
73import android.os.Message;
74import android.os.Parcel;
75import android.os.ParcelFileDescriptor;
76import android.os.Power;
77import android.os.PowerManager;
78import android.os.Process;
79import android.os.RemoteException;
80import android.os.ServiceManager;
81import android.os.SystemClock;
82import android.os.SystemProperties;
83import android.os.TokenWatcher;
84import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070085import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.EventLog;
87import android.util.Log;
88import android.util.SparseIntArray;
89import android.view.Display;
90import android.view.Gravity;
91import android.view.IApplicationToken;
92import android.view.IOnKeyguardExitResult;
93import android.view.IRotationWatcher;
94import android.view.IWindow;
95import android.view.IWindowManager;
96import android.view.IWindowSession;
97import android.view.KeyEvent;
98import android.view.MotionEvent;
99import android.view.RawInputEvent;
100import android.view.Surface;
101import android.view.SurfaceSession;
102import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700103import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.ViewTreeObserver;
105import android.view.WindowManager;
106import android.view.WindowManagerImpl;
107import android.view.WindowManagerPolicy;
108import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700109import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.animation.Animation;
111import android.view.animation.AnimationUtils;
112import android.view.animation.Transformation;
113
114import java.io.BufferedWriter;
115import java.io.File;
116import java.io.FileDescriptor;
117import java.io.IOException;
118import java.io.OutputStream;
119import java.io.OutputStreamWriter;
120import java.io.PrintWriter;
121import java.io.StringWriter;
122import java.net.Socket;
123import java.util.ArrayList;
124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.Iterator;
127import java.util.List;
128
129/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700130public class WindowManagerService extends IWindowManager.Stub
131 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final String TAG = "WindowManager";
133 static final boolean DEBUG = false;
134 static final boolean DEBUG_FOCUS = false;
135 static final boolean DEBUG_ANIM = false;
136 static final boolean DEBUG_LAYERS = false;
137 static final boolean DEBUG_INPUT = false;
138 static final boolean DEBUG_INPUT_METHOD = false;
139 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700140 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 static final boolean DEBUG_ORIENTATION = false;
142 static final boolean DEBUG_APP_TRANSITIONS = false;
143 static final boolean DEBUG_STARTING_WINDOW = false;
144 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700145 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700147 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700148 static final boolean MEASURE_LATENCY = false;
149 static private LatencyTimer lt;
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final boolean PROFILE_ORIENTATION = false;
152 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700153 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /** How long to wait for subsequent key repeats, in milliseconds */
158 static final int KEY_REPEAT_DELAY = 50;
159
160 /** How much to multiply the policy's type layer, to reserve room
161 * for multiple windows of the same type and Z-ordering adjustment
162 * with TYPE_LAYER_OFFSET. */
163 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
166 * or below others in the same layer. */
167 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** How much to increment the layer for each window, to reserve room
170 * for effect surfaces between them.
171 */
172 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /** The maximum length we will accept for a loaded animation duration:
175 * this is 10 seconds.
176 */
177 static final int MAX_ANIMATION_DURATION = 10*1000;
178
179 /** Amount of time (in milliseconds) to animate the dim surface from one
180 * value to another, when no window animation is driving it.
181 */
182 static final int DEFAULT_DIM_DURATION = 200;
183
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700184 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
185 * compatible windows.
186 */
187 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /** Adjustment to time to perform a dim, to make it more dramatic.
190 */
191 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700192
Dianne Hackborncfaef692009-06-15 14:24:44 -0700193 static final int INJECT_FAILED = 0;
194 static final int INJECT_SUCCEEDED = 1;
195 static final int INJECT_NO_PERMISSION = -1;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 static final int UPDATE_FOCUS_NORMAL = 0;
198 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
199 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
200 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 /** The minimum time between dispatching touch events. */
203 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
204
205 // Last touch event time
206 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700207
Michael Chane96440f2009-05-06 10:27:36 -0700208 // Last touch event type
209 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 // Time to wait before calling useractivity again. This saves CPU usage
212 // when we get a flood of touch events.
213 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
214
215 // Last time we call user activity
216 long mLastUserActivityCallTime = 0;
217
Romain Guy06882f82009-06-10 13:36:04 -0700218 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700219 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700222 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 /**
225 * Condition waited on by {@link #reenableKeyguard} to know the call to
226 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500227 * This is set to true only if mKeyguardTokenWatcher.acquired() has
228 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500230 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Mike Lockwood983ee092009-11-22 01:42:24 -0500232 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
233 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public void acquired() {
235 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500236 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
238 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700239 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500240 synchronized (mKeyguardTokenWatcher) {
241 mKeyguardDisabled = false;
242 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
244 }
245 };
246
247 final Context mContext;
248
249 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
254
255 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * All currently active sessions with clients.
261 */
262 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * Mapping from an IWindow IBinder to the server's Window object.
266 * This is also used as the lock for all of our state.
267 */
268 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
269
270 /**
271 * Mapping from a token IBinder to a WindowToken object.
272 */
273 final HashMap<IBinder, WindowToken> mTokenMap =
274 new HashMap<IBinder, WindowToken>();
275
276 /**
277 * The same tokens as mTokenMap, stored in a list for efficient iteration
278 * over them.
279 */
280 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Window tokens that are in the process of exiting, but still
284 * on screen for animations.
285 */
286 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
287
288 /**
289 * Z-ordered (bottom-most first) list of all application tokens, for
290 * controlling the ordering of windows in different applications. This
291 * contains WindowToken objects.
292 */
293 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
294
295 /**
296 * Application tokens that are in the process of exiting, but still
297 * on screen for animations.
298 */
299 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
300
301 /**
302 * List of window tokens that have finished starting their application,
303 * and now need to have the policy remove their windows.
304 */
305 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
306
307 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700308 * This was the app token that was used to retrieve the last enter
309 * animation. It will be used for the next exit animation.
310 */
311 AppWindowToken mLastEnterAnimToken;
312
313 /**
314 * These were the layout params used to retrieve the last enter animation.
315 * They will be used for the next exit animation.
316 */
317 LayoutParams mLastEnterAnimParams;
318
319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 * Z-ordered (bottom-most first) list of all Window objects.
321 */
322 final ArrayList mWindows = new ArrayList();
323
324 /**
325 * Windows that are being resized. Used so we can tell the client about
326 * the resize after closing the transaction in which we resized the
327 * underlying surface.
328 */
329 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
330
331 /**
332 * Windows whose animations have ended and now must be removed.
333 */
334 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
335
336 /**
337 * Windows whose surface should be destroyed.
338 */
339 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
340
341 /**
342 * Windows that have lost input focus and are waiting for the new
343 * focus window to be displayed before they are told about this.
344 */
345 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
346
347 /**
348 * This is set when we have run out of memory, and will either be an empty
349 * list or contain windows that need to be force removed.
350 */
351 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700356 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 Surface mBlurSurface;
358 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 final float[] mTmpFloats = new float[9];
363
364 boolean mSafeMode;
365 boolean mDisplayEnabled = false;
366 boolean mSystemBooted = false;
367 int mRotation = 0;
368 int mRequestedRotation = 0;
369 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700370 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 ArrayList<IRotationWatcher> mRotationWatchers
372 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 boolean mLayoutNeeded = true;
375 boolean mAnimationPending = false;
376 boolean mDisplayFrozen = false;
377 boolean mWindowsFreezingScreen = false;
378 long mFreezeGcPending = 0;
379 int mAppsFreezingScreen = 0;
380
381 // This is held as long as we have the screen frozen, to give us time to
382 // perform a rotation animation when turning off shows the lock screen which
383 // changes the orientation.
384 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 // State management of app transitions. When we are preparing for a
387 // transition, mNextAppTransition will be the kind of transition to
388 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
389 // mOpeningApps and mClosingApps are the lists of tokens that will be
390 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700391 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700392 String mNextAppTransitionPackage;
393 int mNextAppTransitionEnter;
394 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700396 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 boolean mAppTransitionTimeout = false;
398 boolean mStartingIconInTransition = false;
399 boolean mSkipAppTransitionAnimation = false;
400 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
401 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700402 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
403 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 //flag to detect fat touch events
406 boolean mFatTouch = false;
407 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 H mH = new H();
410
411 WindowState mCurrentFocus = null;
412 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 // This just indicates the window the input method is on top of, not
415 // necessarily the window its input is going to.
416 WindowState mInputMethodTarget = null;
417 WindowState mUpcomingInputMethodTarget = null;
418 boolean mInputMethodTargetWaitingAnim;
419 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 WindowState mInputMethodWindow = null;
422 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
423
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700424 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
425
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700426 // If non-null, this is the currently visible window that is associated
427 // with the wallpaper.
428 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700429 // If non-null, we are in the middle of animating from one wallpaper target
430 // to another, and this is the lower one in Z-order.
431 WindowState mLowerWallpaperTarget = null;
432 // If non-null, we are in the middle of animating from one wallpaper target
433 // to another, and this is the higher one in Z-order.
434 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700435 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700436 float mLastWallpaperX = -1;
437 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800438 float mLastWallpaperXStep = -1;
439 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800440 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700441 // This is set when we are waiting for a wallpaper to tell us it is done
442 // changing its scroll position.
443 WindowState mWaitingOnWallpaper;
444 // The last time we had a timeout when waiting for a wallpaper.
445 long mLastWallpaperTimeoutTime;
446 // We give a wallpaper up to 150ms to finish scrolling.
447 static final long WALLPAPER_TIMEOUT = 150;
448 // Time we wait after a timeout before trying to wait again.
449 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 AppWindowToken mFocusedApp = null;
452
453 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 float mWindowAnimationScale = 1.0f;
456 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 final KeyWaiter mKeyWaiter = new KeyWaiter();
459 final KeyQ mQueue;
460 final InputDispatcherThread mInputThread;
461
462 // Who is holding the screen on.
463 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700464
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700465 boolean mTurnOnScreen;
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
468 * Whether the UI is currently running in touch mode (not showing
469 * navigational focus because the user is directly pressing the screen).
470 */
471 boolean mInTouchMode = false;
472
473 private ViewServer mViewServer;
474
475 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700476
Dianne Hackbornc485a602009-03-24 22:39:49 -0700477 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700478 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700479
480 // The frame use to limit the size of the app running in compatibility mode.
481 Rect mCompatibleScreenFrame = new Rect();
482 // The surface used to fill the outer rim of the app running in compatibility mode.
483 Surface mBackgroundFillerSurface = null;
484 boolean mBackgroundFillerShown = false;
485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public static WindowManagerService main(Context context,
487 PowerManagerService pm, boolean haveInputMethods) {
488 WMThread thr = new WMThread(context, pm, haveInputMethods);
489 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 synchronized (thr) {
492 while (thr.mService == null) {
493 try {
494 thr.wait();
495 } catch (InterruptedException e) {
496 }
497 }
498 }
Romain Guy06882f82009-06-10 13:36:04 -0700499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 return thr.mService;
501 }
Romain Guy06882f82009-06-10 13:36:04 -0700502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 static class WMThread extends Thread {
504 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 private final Context mContext;
507 private final PowerManagerService mPM;
508 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public WMThread(Context context, PowerManagerService pm,
511 boolean haveInputMethods) {
512 super("WindowManager");
513 mContext = context;
514 mPM = pm;
515 mHaveInputMethods = haveInputMethods;
516 }
Romain Guy06882f82009-06-10 13:36:04 -0700517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 public void run() {
519 Looper.prepare();
520 WindowManagerService s = new WindowManagerService(mContext, mPM,
521 mHaveInputMethods);
522 android.os.Process.setThreadPriority(
523 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 synchronized (this) {
526 mService = s;
527 notifyAll();
528 }
Romain Guy06882f82009-06-10 13:36:04 -0700529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 Looper.loop();
531 }
532 }
533
534 static class PolicyThread extends Thread {
535 private final WindowManagerPolicy mPolicy;
536 private final WindowManagerService mService;
537 private final Context mContext;
538 private final PowerManagerService mPM;
539 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 public PolicyThread(WindowManagerPolicy policy,
542 WindowManagerService service, Context context,
543 PowerManagerService pm) {
544 super("WindowManagerPolicy");
545 mPolicy = policy;
546 mService = service;
547 mContext = context;
548 mPM = pm;
549 }
Romain Guy06882f82009-06-10 13:36:04 -0700550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 public void run() {
552 Looper.prepare();
553 //Looper.myLooper().setMessageLogging(new LogPrinter(
554 // Log.VERBOSE, "WindowManagerPolicy"));
555 android.os.Process.setThreadPriority(
556 android.os.Process.THREAD_PRIORITY_FOREGROUND);
557 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 synchronized (this) {
560 mRunning = true;
561 notifyAll();
562 }
Romain Guy06882f82009-06-10 13:36:04 -0700563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 Looper.loop();
565 }
566 }
567
568 private WindowManagerService(Context context, PowerManagerService pm,
569 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700570 if (MEASURE_LATENCY) {
571 lt = new LatencyTimer(100, 1000);
572 }
573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 mContext = context;
575 mHaveInputMethods = haveInputMethods;
576 mLimitedAlphaCompositing = context.getResources().getBoolean(
577 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 mPowerManager = pm;
580 mPowerManager.setPolicy(mPolicy);
581 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
582 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
583 "SCREEN_FROZEN");
584 mScreenFrozenLock.setReferenceCounted(false);
585
586 mActivityManager = ActivityManagerNative.getDefault();
587 mBatteryStats = BatteryStatsService.getService();
588
589 // Get persisted window scale setting
590 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
591 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
592 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
593 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700594
Michael Chan9f028e62009-08-04 17:37:46 -0700595 int max_events_per_sec = 35;
596 try {
597 max_events_per_sec = Integer.parseInt(SystemProperties
598 .get("windowsmgr.max_events_per_sec"));
599 if (max_events_per_sec < 1) {
600 max_events_per_sec = 35;
601 }
602 } catch (NumberFormatException e) {
603 }
604 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 mQueue = new KeyQ();
607
608 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
611 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 synchronized (thr) {
614 while (!thr.mRunning) {
615 try {
616 thr.wait();
617 } catch (InterruptedException e) {
618 }
619 }
620 }
Romain Guy06882f82009-06-10 13:36:04 -0700621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 // Add ourself to the Watchdog monitors.
625 Watchdog.getInstance().addMonitor(this);
626 }
627
628 @Override
629 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
630 throws RemoteException {
631 try {
632 return super.onTransact(code, data, reply, flags);
633 } catch (RuntimeException e) {
634 // The window manager only throws security exceptions, so let's
635 // log all others.
636 if (!(e instanceof SecurityException)) {
637 Log.e(TAG, "Window Manager Crash", e);
638 }
639 throw e;
640 }
641 }
642
643 private void placeWindowAfter(Object pos, WindowState window) {
644 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700645 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 TAG, "Adding window " + window + " at "
647 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
648 mWindows.add(i+1, window);
649 }
650
651 private void placeWindowBefore(Object pos, WindowState window) {
652 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700653 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 TAG, "Adding window " + window + " at "
655 + i + " of " + mWindows.size() + " (before " + pos + ")");
656 mWindows.add(i, window);
657 }
658
659 //This method finds out the index of a window that has the same app token as
660 //win. used for z ordering the windows in mWindows
661 private int findIdxBasedOnAppTokens(WindowState win) {
662 //use a local variable to cache mWindows
663 ArrayList localmWindows = mWindows;
664 int jmax = localmWindows.size();
665 if(jmax == 0) {
666 return -1;
667 }
668 for(int j = (jmax-1); j >= 0; j--) {
669 WindowState wentry = (WindowState)localmWindows.get(j);
670 if(wentry.mAppToken == win.mAppToken) {
671 return j;
672 }
673 }
674 return -1;
675 }
Romain Guy06882f82009-06-10 13:36:04 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
678 final IWindow client = win.mClient;
679 final WindowToken token = win.mToken;
680 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 final int N = localmWindows.size();
683 final WindowState attached = win.mAttachedWindow;
684 int i;
685 if (attached == null) {
686 int tokenWindowsPos = token.windows.size();
687 if (token.appWindowToken != null) {
688 int index = tokenWindowsPos-1;
689 if (index >= 0) {
690 // If this application has existing windows, we
691 // simply place the new window on top of them... but
692 // keep the starting window on top.
693 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
694 // Base windows go behind everything else.
695 placeWindowBefore(token.windows.get(0), win);
696 tokenWindowsPos = 0;
697 } else {
698 AppWindowToken atoken = win.mAppToken;
699 if (atoken != null &&
700 token.windows.get(index) == atoken.startingWindow) {
701 placeWindowBefore(token.windows.get(index), win);
702 tokenWindowsPos--;
703 } else {
704 int newIdx = findIdxBasedOnAppTokens(win);
705 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700706 //there is a window above this one associated with the same
707 //apptoken note that the window could be a floating window
708 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700710 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
711 TAG, "Adding window " + win + " at "
712 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716 }
717 } else {
718 if (localLOGV) Log.v(
719 TAG, "Figuring out where to add app window "
720 + client.asBinder() + " (token=" + token + ")");
721 // Figure out where the window should go, based on the
722 // order of applications.
723 final int NA = mAppTokens.size();
724 Object pos = null;
725 for (i=NA-1; i>=0; i--) {
726 AppWindowToken t = mAppTokens.get(i);
727 if (t == token) {
728 i--;
729 break;
730 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700731
732 // We haven't reached the token yet; if this token
733 // is not going to the bottom and has windows, we can
734 // use it as an anchor for when we do reach the token.
735 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 pos = t.windows.get(0);
737 }
738 }
739 // We now know the index into the apps. If we found
740 // an app window above, that gives us the position; else
741 // we need to look some more.
742 if (pos != null) {
743 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700744 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 mTokenMap.get(((WindowState)pos).mClient.asBinder());
746 if (atoken != null) {
747 final int NC = atoken.windows.size();
748 if (NC > 0) {
749 WindowState bottom = atoken.windows.get(0);
750 if (bottom.mSubLayer < 0) {
751 pos = bottom;
752 }
753 }
754 }
755 placeWindowBefore(pos, win);
756 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700757 // Continue looking down until we find the first
758 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 while (i >= 0) {
760 AppWindowToken t = mAppTokens.get(i);
761 final int NW = t.windows.size();
762 if (NW > 0) {
763 pos = t.windows.get(NW-1);
764 break;
765 }
766 i--;
767 }
768 if (pos != null) {
769 // Move in front of any windows attached to this
770 // one.
771 WindowToken atoken =
772 mTokenMap.get(((WindowState)pos).mClient.asBinder());
773 if (atoken != null) {
774 final int NC = atoken.windows.size();
775 if (NC > 0) {
776 WindowState top = atoken.windows.get(NC-1);
777 if (top.mSubLayer >= 0) {
778 pos = top;
779 }
780 }
781 }
782 placeWindowAfter(pos, win);
783 } else {
784 // Just search for the start of this layer.
785 final int myLayer = win.mBaseLayer;
786 for (i=0; i<N; i++) {
787 WindowState w = (WindowState)localmWindows.get(i);
788 if (w.mBaseLayer > myLayer) {
789 break;
790 }
791 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700792 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
793 TAG, "Adding window " + win + " at "
794 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 localmWindows.add(i, win);
796 }
797 }
798 }
799 } else {
800 // Figure out where window should go, based on layer.
801 final int myLayer = win.mBaseLayer;
802 for (i=N-1; i>=0; i--) {
803 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
804 i++;
805 break;
806 }
807 }
808 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700809 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
810 TAG, "Adding window " + win + " at "
811 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 localmWindows.add(i, win);
813 }
814 if (addToToken) {
815 token.windows.add(tokenWindowsPos, win);
816 }
817
818 } else {
819 // Figure out this window's ordering relative to the window
820 // it is attached to.
821 final int NA = token.windows.size();
822 final int sublayer = win.mSubLayer;
823 int largestSublayer = Integer.MIN_VALUE;
824 WindowState windowWithLargestSublayer = null;
825 for (i=0; i<NA; i++) {
826 WindowState w = token.windows.get(i);
827 final int wSublayer = w.mSubLayer;
828 if (wSublayer >= largestSublayer) {
829 largestSublayer = wSublayer;
830 windowWithLargestSublayer = w;
831 }
832 if (sublayer < 0) {
833 // For negative sublayers, we go below all windows
834 // in the same sublayer.
835 if (wSublayer >= sublayer) {
836 if (addToToken) {
837 token.windows.add(i, win);
838 }
839 placeWindowBefore(
840 wSublayer >= 0 ? attached : w, win);
841 break;
842 }
843 } else {
844 // For positive sublayers, we go above all windows
845 // in the same sublayer.
846 if (wSublayer > sublayer) {
847 if (addToToken) {
848 token.windows.add(i, win);
849 }
850 placeWindowBefore(w, win);
851 break;
852 }
853 }
854 }
855 if (i >= NA) {
856 if (addToToken) {
857 token.windows.add(win);
858 }
859 if (sublayer < 0) {
860 placeWindowBefore(attached, win);
861 } else {
862 placeWindowAfter(largestSublayer >= 0
863 ? windowWithLargestSublayer
864 : attached,
865 win);
866 }
867 }
868 }
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (win.mAppToken != null && addToToken) {
871 win.mAppToken.allAppWindows.add(win);
872 }
873 }
Romain Guy06882f82009-06-10 13:36:04 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 static boolean canBeImeTarget(WindowState w) {
876 final int fl = w.mAttrs.flags
877 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
878 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
879 return w.isVisibleOrAdding();
880 }
881 return false;
882 }
Romain Guy06882f82009-06-10 13:36:04 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
885 final ArrayList localmWindows = mWindows;
886 final int N = localmWindows.size();
887 WindowState w = null;
888 int i = N;
889 while (i > 0) {
890 i--;
891 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
894 // + Integer.toHexString(w.mAttrs.flags));
895 if (canBeImeTarget(w)) {
896 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // Yet more tricksyness! If this window is a "starting"
899 // window, we do actually want to be on top of it, but
900 // it is not -really- where input will go. So if the caller
901 // is not actually looking to move the IME, look down below
902 // for a real window to target...
903 if (!willMove
904 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
905 && i > 0) {
906 WindowState wb = (WindowState)localmWindows.get(i-1);
907 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
908 i--;
909 w = wb;
910 }
911 }
912 break;
913 }
914 }
Romain Guy06882f82009-06-10 13:36:04 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
919 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 if (willMove && w != null) {
922 final WindowState curTarget = mInputMethodTarget;
923 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 // Now some fun for dealing with window animations that
926 // modify the Z order. We need to look at all windows below
927 // the current target that are in this app, finding the highest
928 // visible one in layering.
929 AppWindowToken token = curTarget.mAppToken;
930 WindowState highestTarget = null;
931 int highestPos = 0;
932 if (token.animating || token.animation != null) {
933 int pos = 0;
934 pos = localmWindows.indexOf(curTarget);
935 while (pos >= 0) {
936 WindowState win = (WindowState)localmWindows.get(pos);
937 if (win.mAppToken != token) {
938 break;
939 }
940 if (!win.mRemoved) {
941 if (highestTarget == null || win.mAnimLayer >
942 highestTarget.mAnimLayer) {
943 highestTarget = win;
944 highestPos = pos;
945 }
946 }
947 pos--;
948 }
949 }
Romain Guy06882f82009-06-10 13:36:04 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700952 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 + mNextAppTransition + " " + highestTarget
954 + " animating=" + highestTarget.isAnimating()
955 + " layer=" + highestTarget.mAnimLayer
956 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700957
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700958 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 // If we are currently setting up for an animation,
960 // hold everything until we can find out what will happen.
961 mInputMethodTargetWaitingAnim = true;
962 mInputMethodTarget = highestTarget;
963 return highestPos + 1;
964 } else if (highestTarget.isAnimating() &&
965 highestTarget.mAnimLayer > w.mAnimLayer) {
966 // If the window we are currently targeting is involved
967 // with an animation, and it is on top of the next target
968 // we will be over, then hold off on moving until
969 // that is done.
970 mInputMethodTarget = highestTarget;
971 return highestPos + 1;
972 }
973 }
974 }
975 }
Romain Guy06882f82009-06-10 13:36:04 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 //Log.i(TAG, "Placing input method @" + (i+1));
978 if (w != null) {
979 if (willMove) {
980 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700981 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
983 + mInputMethodTarget + " to " + w, e);
984 mInputMethodTarget = w;
985 if (w.mAppToken != null) {
986 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
987 } else {
988 setInputMethodAnimLayerAdjustment(0);
989 }
990 }
991 return i+1;
992 }
993 if (willMove) {
994 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700995 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
997 + mInputMethodTarget + " to null", e);
998 mInputMethodTarget = null;
999 setInputMethodAnimLayerAdjustment(0);
1000 }
1001 return -1;
1002 }
Romain Guy06882f82009-06-10 13:36:04 -07001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 void addInputMethodWindowToListLocked(WindowState win) {
1005 int pos = findDesiredInputMethodWindowIndexLocked(true);
1006 if (pos >= 0) {
1007 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001008 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1009 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 mWindows.add(pos, win);
1011 moveInputMethodDialogsLocked(pos+1);
1012 return;
1013 }
1014 win.mTargetAppToken = null;
1015 addWindowToListInOrderLocked(win, true);
1016 moveInputMethodDialogsLocked(pos);
1017 }
Romain Guy06882f82009-06-10 13:36:04 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 void setInputMethodAnimLayerAdjustment(int adj) {
1020 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1021 mInputMethodAnimLayerAdjustment = adj;
1022 WindowState imw = mInputMethodWindow;
1023 if (imw != null) {
1024 imw.mAnimLayer = imw.mLayer + adj;
1025 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1026 + " anim layer: " + imw.mAnimLayer);
1027 int wi = imw.mChildWindows.size();
1028 while (wi > 0) {
1029 wi--;
1030 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1031 cw.mAnimLayer = cw.mLayer + adj;
1032 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1033 + " anim layer: " + cw.mAnimLayer);
1034 }
1035 }
1036 int di = mInputMethodDialogs.size();
1037 while (di > 0) {
1038 di --;
1039 imw = mInputMethodDialogs.get(di);
1040 imw.mAnimLayer = imw.mLayer + adj;
1041 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1042 + " anim layer: " + imw.mAnimLayer);
1043 }
1044 }
Romain Guy06882f82009-06-10 13:36:04 -07001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1047 int wpos = mWindows.indexOf(win);
1048 if (wpos >= 0) {
1049 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001050 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 mWindows.remove(wpos);
1052 int NC = win.mChildWindows.size();
1053 while (NC > 0) {
1054 NC--;
1055 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1056 int cpos = mWindows.indexOf(cw);
1057 if (cpos >= 0) {
1058 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001059 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1060 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 mWindows.remove(cpos);
1062 }
1063 }
1064 }
1065 return interestingPos;
1066 }
Romain Guy06882f82009-06-10 13:36:04 -07001067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 private void reAddWindowToListInOrderLocked(WindowState win) {
1069 addWindowToListInOrderLocked(win, false);
1070 // This is a hack to get all of the child windows added as well
1071 // at the right position. Child windows should be rare and
1072 // this case should be rare, so it shouldn't be that big a deal.
1073 int wpos = mWindows.indexOf(win);
1074 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001075 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1076 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 mWindows.remove(wpos);
1078 reAddWindowLocked(wpos, win);
1079 }
1080 }
Romain Guy06882f82009-06-10 13:36:04 -07001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 void logWindowList(String prefix) {
1083 int N = mWindows.size();
1084 while (N > 0) {
1085 N--;
1086 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1087 }
1088 }
Romain Guy06882f82009-06-10 13:36:04 -07001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 void moveInputMethodDialogsLocked(int pos) {
1091 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 final int N = dialogs.size();
1094 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1095 for (int i=0; i<N; i++) {
1096 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1097 }
1098 if (DEBUG_INPUT_METHOD) {
1099 Log.v(TAG, "Window list w/pos=" + pos);
1100 logWindowList(" ");
1101 }
Romain Guy06882f82009-06-10 13:36:04 -07001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (pos >= 0) {
1104 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1105 if (pos < mWindows.size()) {
1106 WindowState wp = (WindowState)mWindows.get(pos);
1107 if (wp == mInputMethodWindow) {
1108 pos++;
1109 }
1110 }
1111 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1112 for (int i=0; i<N; i++) {
1113 WindowState win = dialogs.get(i);
1114 win.mTargetAppToken = targetAppToken;
1115 pos = reAddWindowLocked(pos, win);
1116 }
1117 if (DEBUG_INPUT_METHOD) {
1118 Log.v(TAG, "Final window list:");
1119 logWindowList(" ");
1120 }
1121 return;
1122 }
1123 for (int i=0; i<N; i++) {
1124 WindowState win = dialogs.get(i);
1125 win.mTargetAppToken = null;
1126 reAddWindowToListInOrderLocked(win);
1127 if (DEBUG_INPUT_METHOD) {
1128 Log.v(TAG, "No IM target, final list:");
1129 logWindowList(" ");
1130 }
1131 }
1132 }
Romain Guy06882f82009-06-10 13:36:04 -07001133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1135 final WindowState imWin = mInputMethodWindow;
1136 final int DN = mInputMethodDialogs.size();
1137 if (imWin == null && DN == 0) {
1138 return false;
1139 }
Romain Guy06882f82009-06-10 13:36:04 -07001140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1142 if (imPos >= 0) {
1143 // In this case, the input method windows are to be placed
1144 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 // First check to see if the input method windows are already
1147 // located here, and contiguous.
1148 final int N = mWindows.size();
1149 WindowState firstImWin = imPos < N
1150 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 // Figure out the actual input method window that should be
1153 // at the bottom of their stack.
1154 WindowState baseImWin = imWin != null
1155 ? imWin : mInputMethodDialogs.get(0);
1156 if (baseImWin.mChildWindows.size() > 0) {
1157 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1158 if (cw.mSubLayer < 0) baseImWin = cw;
1159 }
Romain Guy06882f82009-06-10 13:36:04 -07001160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 if (firstImWin == baseImWin) {
1162 // The windows haven't moved... but are they still contiguous?
1163 // First find the top IM window.
1164 int pos = imPos+1;
1165 while (pos < N) {
1166 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1167 break;
1168 }
1169 pos++;
1170 }
1171 pos++;
1172 // Now there should be no more input method windows above.
1173 while (pos < N) {
1174 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1175 break;
1176 }
1177 pos++;
1178 }
1179 if (pos >= N) {
1180 // All is good!
1181 return false;
1182 }
1183 }
Romain Guy06882f82009-06-10 13:36:04 -07001184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (imWin != null) {
1186 if (DEBUG_INPUT_METHOD) {
1187 Log.v(TAG, "Moving IM from " + imPos);
1188 logWindowList(" ");
1189 }
1190 imPos = tmpRemoveWindowLocked(imPos, imWin);
1191 if (DEBUG_INPUT_METHOD) {
1192 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1193 logWindowList(" ");
1194 }
1195 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1196 reAddWindowLocked(imPos, imWin);
1197 if (DEBUG_INPUT_METHOD) {
1198 Log.v(TAG, "List after moving IM to " + imPos + ":");
1199 logWindowList(" ");
1200 }
1201 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1202 } else {
1203 moveInputMethodDialogsLocked(imPos);
1204 }
Romain Guy06882f82009-06-10 13:36:04 -07001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 } else {
1207 // In this case, the input method windows go in a fixed layer,
1208 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 if (imWin != null) {
1211 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1212 tmpRemoveWindowLocked(0, imWin);
1213 imWin.mTargetAppToken = null;
1214 reAddWindowToListInOrderLocked(imWin);
1215 if (DEBUG_INPUT_METHOD) {
1216 Log.v(TAG, "List with no IM target:");
1217 logWindowList(" ");
1218 }
1219 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1220 } else {
1221 moveInputMethodDialogsLocked(-1);;
1222 }
Romain Guy06882f82009-06-10 13:36:04 -07001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
Romain Guy06882f82009-06-10 13:36:04 -07001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 if (needAssignLayers) {
1227 assignLayersLocked();
1228 }
Romain Guy06882f82009-06-10 13:36:04 -07001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 return true;
1231 }
Romain Guy06882f82009-06-10 13:36:04 -07001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 void adjustInputMethodDialogsLocked() {
1234 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1235 }
Romain Guy06882f82009-06-10 13:36:04 -07001236
Dianne Hackborn25994b42009-09-04 14:21:19 -07001237 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1238 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1239 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1240 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1241 ? wallpaperTarget.mAppToken.animation : null)
1242 + " upper=" + mUpperWallpaperTarget
1243 + " lower=" + mLowerWallpaperTarget);
1244 return (wallpaperTarget != null
1245 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1246 && wallpaperTarget.mAppToken.animation != null)))
1247 || mUpperWallpaperTarget != null
1248 || mLowerWallpaperTarget != null;
1249 }
1250
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001251 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1252 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1253
1254 int adjustWallpaperWindowsLocked() {
1255 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001256
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001257 final int dw = mDisplay.getWidth();
1258 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001259
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001260 // First find top-most window that has asked to be on top of the
1261 // wallpaper; all wallpapers go behind it.
1262 final ArrayList localmWindows = mWindows;
1263 int N = localmWindows.size();
1264 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001265 WindowState foundW = null;
1266 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001267 WindowState topCurW = null;
1268 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001269 int i = N;
1270 while (i > 0) {
1271 i--;
1272 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001273 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1274 if (topCurW == null) {
1275 topCurW = w;
1276 topCurI = i;
1277 }
1278 continue;
1279 }
1280 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001281 if (w.mAppToken != null) {
1282 // If this window's app token is hidden and not animating,
1283 // it is of no interest to us.
1284 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1285 if (DEBUG_WALLPAPER) Log.v(TAG,
1286 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001287 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001288 continue;
1289 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001290 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001291 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1292 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1293 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001294 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001295 && (mWallpaperTarget == w
1296 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001297 if (DEBUG_WALLPAPER) Log.v(TAG,
1298 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001299 foundW = w;
1300 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001301 if (w == mWallpaperTarget && ((w.mAppToken != null
1302 && w.mAppToken.animation != null)
1303 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001304 // The current wallpaper target is animating, so we'll
1305 // look behind it for another possible target and figure
1306 // out what is going on below.
1307 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1308 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001309 continue;
1310 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001311 break;
1312 }
1313 }
1314
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001315 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001316 // If we are currently waiting for an app transition, and either
1317 // the current target or the next target are involved with it,
1318 // then hold off on doing anything with the wallpaper.
1319 // Note that we are checking here for just whether the target
1320 // is part of an app token... which is potentially overly aggressive
1321 // (the app token may not be involved in the transition), but good
1322 // enough (we'll just wait until whatever transition is pending
1323 // executes).
1324 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001325 if (DEBUG_WALLPAPER) Log.v(TAG,
1326 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001327 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001328 }
1329 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001330 if (DEBUG_WALLPAPER) Log.v(TAG,
1331 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001332 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001333 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001334 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001335
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001336 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001337 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001338 Log.v(TAG, "New wallpaper target: " + foundW
1339 + " oldTarget: " + mWallpaperTarget);
1340 }
1341
1342 mLowerWallpaperTarget = null;
1343 mUpperWallpaperTarget = null;
1344
1345 WindowState oldW = mWallpaperTarget;
1346 mWallpaperTarget = foundW;
1347
1348 // Now what is happening... if the current and new targets are
1349 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001350 if (foundW != null && oldW != null) {
1351 boolean oldAnim = oldW.mAnimation != null
1352 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1353 boolean foundAnim = foundW.mAnimation != null
1354 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001355 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001356 Log.v(TAG, "New animation: " + foundAnim
1357 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001358 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001359 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001360 int oldI = localmWindows.indexOf(oldW);
1361 if (DEBUG_WALLPAPER) {
1362 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1363 }
1364 if (oldI >= 0) {
1365 if (DEBUG_WALLPAPER) {
1366 Log.v(TAG, "Animating wallpapers: old#" + oldI
1367 + "=" + oldW + "; new#" + foundI
1368 + "=" + foundW);
1369 }
1370
1371 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001372 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001373 if (DEBUG_WALLPAPER) {
1374 Log.v(TAG, "Old wallpaper still the target.");
1375 }
1376 mWallpaperTarget = oldW;
1377 }
1378
1379 // Now set the upper and lower wallpaper targets
1380 // correctly, and make sure that we are positioning
1381 // the wallpaper below the lower.
1382 if (foundI > oldI) {
1383 // The new target is on top of the old one.
1384 if (DEBUG_WALLPAPER) {
1385 Log.v(TAG, "Found target above old target.");
1386 }
1387 mUpperWallpaperTarget = foundW;
1388 mLowerWallpaperTarget = oldW;
1389 foundW = oldW;
1390 foundI = oldI;
1391 } else {
1392 // The new target is below the old one.
1393 if (DEBUG_WALLPAPER) {
1394 Log.v(TAG, "Found target below old target.");
1395 }
1396 mUpperWallpaperTarget = oldW;
1397 mLowerWallpaperTarget = foundW;
1398 }
1399 }
1400 }
1401 }
1402
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001403 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001404 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001405 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1406 || (mLowerWallpaperTarget.mAppToken != null
1407 && mLowerWallpaperTarget.mAppToken.animation != null);
1408 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1409 || (mUpperWallpaperTarget.mAppToken != null
1410 && mUpperWallpaperTarget.mAppToken.animation != null);
1411 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001412 if (DEBUG_WALLPAPER) {
1413 Log.v(TAG, "No longer animating wallpaper targets!");
1414 }
1415 mLowerWallpaperTarget = null;
1416 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001417 }
1418 }
1419
1420 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001421 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001422 // The window is visible to the compositor... but is it visible
1423 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001424 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001425 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001426
1427 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001428 // its layer adjustment. Only do this if we are not transfering
1429 // between two wallpaper targets.
1430 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001431 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001432 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001433
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001434 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1435 * TYPE_LAYER_MULTIPLIER
1436 + TYPE_LAYER_OFFSET;
1437
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001438 // Now w is the window we are supposed to be behind... but we
1439 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001440 // AND any starting window associated with it, AND below the
1441 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001442 while (foundI > 0) {
1443 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001444 if (wb.mBaseLayer < maxLayer &&
1445 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001446 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001447 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001448 // This window is not related to the previous one in any
1449 // interesting way, so stop here.
1450 break;
1451 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001452 foundW = wb;
1453 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001454 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001455 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001456 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001457 }
1458
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001459 if (foundW == null && topCurW != null) {
1460 // There is no wallpaper target, so it goes at the bottom.
1461 // We will assume it is the same place as last time, if known.
1462 foundW = topCurW;
1463 foundI = topCurI+1;
1464 } else {
1465 // Okay i is the position immediately above the wallpaper. Look at
1466 // what is below it for later.
1467 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1468 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001469
Dianne Hackborn284ac932009-08-28 10:34:25 -07001470 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001471 if (mWallpaperTarget.mWallpaperX >= 0) {
1472 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001473 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001474 }
1475 if (mWallpaperTarget.mWallpaperY >= 0) {
1476 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001477 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001478 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001479 }
1480
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001481 // Start stepping backwards from here, ensuring that our wallpaper windows
1482 // are correctly placed.
1483 int curTokenIndex = mWallpaperTokens.size();
1484 while (curTokenIndex > 0) {
1485 curTokenIndex--;
1486 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001487 if (token.hidden == visible) {
1488 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1489 token.hidden = !visible;
1490 // Need to do a layout to ensure the wallpaper now has the
1491 // correct size.
1492 mLayoutNeeded = true;
1493 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001494
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001495 int curWallpaperIndex = token.windows.size();
1496 while (curWallpaperIndex > 0) {
1497 curWallpaperIndex--;
1498 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001499
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001500 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001501 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001502 }
1503
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001504 // First, make sure the client has the current visibility
1505 // state.
1506 if (wallpaper.mWallpaperVisible != visible) {
1507 wallpaper.mWallpaperVisible = visible;
1508 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001509 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001510 "Setting visibility of wallpaper " + wallpaper
1511 + ": " + visible);
1512 wallpaper.mClient.dispatchAppVisibility(visible);
1513 } catch (RemoteException e) {
1514 }
1515 }
1516
1517 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001518 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1519 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001520
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001521 // First, if this window is at the current index, then all
1522 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001523 if (wallpaper == foundW) {
1524 foundI--;
1525 foundW = foundI > 0
1526 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001527 continue;
1528 }
1529
1530 // The window didn't match... the current wallpaper window,
1531 // wherever it is, is in the wrong place, so make sure it is
1532 // not in the list.
1533 int oldIndex = localmWindows.indexOf(wallpaper);
1534 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001535 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1536 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001537 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001538 if (oldIndex < foundI) {
1539 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001540 }
1541 }
1542
1543 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001544 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1545 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001546 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001547
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001548 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001549 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001550 }
1551 }
1552
1553 return changed;
1554 }
1555
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001556 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001557 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1558 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001559 mWallpaperAnimLayerAdjustment = adj;
1560 int curTokenIndex = mWallpaperTokens.size();
1561 while (curTokenIndex > 0) {
1562 curTokenIndex--;
1563 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1564 int curWallpaperIndex = token.windows.size();
1565 while (curWallpaperIndex > 0) {
1566 curWallpaperIndex--;
1567 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1568 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001569 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1570 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001571 }
1572 }
1573 }
1574
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001575 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1576 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001577 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001578 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001579 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001580 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001581 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1582 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1583 changed = wallpaperWin.mXOffset != offset;
1584 if (changed) {
1585 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1586 + wallpaperWin + " x: " + offset);
1587 wallpaperWin.mXOffset = offset;
1588 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001589 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001590 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001591 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001592 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001593 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001594
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001595 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001596 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001597 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1598 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1599 if (wallpaperWin.mYOffset != offset) {
1600 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1601 + wallpaperWin + " y: " + offset);
1602 changed = true;
1603 wallpaperWin.mYOffset = offset;
1604 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001605 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001606 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001607 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001608 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001609 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001610
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001611 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001612 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001613 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1614 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1615 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001616 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001617 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001618 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001619 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001620 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1621 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001622 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001623 if (mWaitingOnWallpaper != null) {
1624 long start = SystemClock.uptimeMillis();
1625 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1626 < start) {
1627 try {
1628 if (DEBUG_WALLPAPER) Log.v(TAG,
1629 "Waiting for offset complete...");
1630 mWindowMap.wait(WALLPAPER_TIMEOUT);
1631 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001632 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001633 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1634 if ((start+WALLPAPER_TIMEOUT)
1635 < SystemClock.uptimeMillis()) {
1636 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1637 + wallpaperWin);
1638 mLastWallpaperTimeoutTime = start;
1639 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001640 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001641 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001642 }
1643 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001644 } catch (RemoteException e) {
1645 }
1646 }
1647
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001648 return changed;
1649 }
1650
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001651 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001652 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001653 if (mWaitingOnWallpaper != null &&
1654 mWaitingOnWallpaper.mClient.asBinder() == window) {
1655 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001656 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001657 }
1658 }
1659 }
1660
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001661 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001662 final int dw = mDisplay.getWidth();
1663 final int dh = mDisplay.getHeight();
1664
1665 boolean changed = false;
1666
1667 WindowState target = mWallpaperTarget;
1668 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001669 if (target.mWallpaperX >= 0) {
1670 mLastWallpaperX = target.mWallpaperX;
1671 } else if (changingTarget.mWallpaperX >= 0) {
1672 mLastWallpaperX = changingTarget.mWallpaperX;
1673 }
1674 if (target.mWallpaperY >= 0) {
1675 mLastWallpaperY = target.mWallpaperY;
1676 } else if (changingTarget.mWallpaperY >= 0) {
1677 mLastWallpaperY = changingTarget.mWallpaperY;
1678 }
1679 }
1680
1681 int curTokenIndex = mWallpaperTokens.size();
1682 while (curTokenIndex > 0) {
1683 curTokenIndex--;
1684 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1685 int curWallpaperIndex = token.windows.size();
1686 while (curWallpaperIndex > 0) {
1687 curWallpaperIndex--;
1688 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1689 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1690 wallpaper.computeShownFrameLocked();
1691 changed = true;
1692 // We only want to be synchronous with one wallpaper.
1693 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001694 }
1695 }
1696 }
1697
1698 return changed;
1699 }
1700
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001701 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001702 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001703 final int dw = mDisplay.getWidth();
1704 final int dh = mDisplay.getHeight();
1705
1706 int curTokenIndex = mWallpaperTokens.size();
1707 while (curTokenIndex > 0) {
1708 curTokenIndex--;
1709 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001710 if (token.hidden == visible) {
1711 token.hidden = !visible;
1712 // Need to do a layout to ensure the wallpaper now has the
1713 // correct size.
1714 mLayoutNeeded = true;
1715 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001716
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001717 int curWallpaperIndex = token.windows.size();
1718 while (curWallpaperIndex > 0) {
1719 curWallpaperIndex--;
1720 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1721 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001722 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001723 }
1724
1725 if (wallpaper.mWallpaperVisible != visible) {
1726 wallpaper.mWallpaperVisible = visible;
1727 try {
1728 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001729 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001730 + ": " + visible);
1731 wallpaper.mClient.dispatchAppVisibility(visible);
1732 } catch (RemoteException e) {
1733 }
1734 }
1735 }
1736 }
1737 }
1738
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001739 void sendPointerToWallpaperLocked(WindowState srcWin,
1740 MotionEvent pointer, long eventTime) {
1741 int curTokenIndex = mWallpaperTokens.size();
1742 while (curTokenIndex > 0) {
1743 curTokenIndex--;
1744 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1745 int curWallpaperIndex = token.windows.size();
1746 while (curWallpaperIndex > 0) {
1747 curWallpaperIndex--;
1748 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1749 if ((wallpaper.mAttrs.flags &
1750 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1751 continue;
1752 }
1753 try {
1754 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001755 if (srcWin != null) {
1756 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1757 srcWin.mFrame.top-wallpaper.mFrame.top);
1758 } else {
1759 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1760 }
1761 switch (pointer.getAction()) {
1762 case MotionEvent.ACTION_DOWN:
1763 mSendingPointersToWallpaper = true;
1764 break;
1765 case MotionEvent.ACTION_UP:
1766 mSendingPointersToWallpaper = false;
1767 break;
1768 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001769 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1770 } catch (RemoteException e) {
1771 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1772 }
1773 }
1774 }
1775 }
1776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 public int addWindow(Session session, IWindow client,
1778 WindowManager.LayoutParams attrs, int viewVisibility,
1779 Rect outContentInsets) {
1780 int res = mPolicy.checkAddPermission(attrs);
1781 if (res != WindowManagerImpl.ADD_OKAY) {
1782 return res;
1783 }
Romain Guy06882f82009-06-10 13:36:04 -07001784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 boolean reportNewConfig = false;
1786 WindowState attachedWindow = null;
1787 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 synchronized(mWindowMap) {
1790 // Instantiating a Display requires talking with the simulator,
1791 // so don't do it until we know the system is mostly up and
1792 // running.
1793 if (mDisplay == null) {
1794 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1795 mDisplay = wm.getDefaultDisplay();
1796 mQueue.setDisplay(mDisplay);
1797 reportNewConfig = true;
1798 }
Romain Guy06882f82009-06-10 13:36:04 -07001799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 if (mWindowMap.containsKey(client.asBinder())) {
1801 Log.w(TAG, "Window " + client + " is already added");
1802 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1803 }
1804
1805 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001806 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 if (attachedWindow == null) {
1808 Log.w(TAG, "Attempted to add window with token that is not a window: "
1809 + attrs.token + ". Aborting.");
1810 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1811 }
1812 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1813 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1814 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1815 + attrs.token + ". Aborting.");
1816 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1817 }
1818 }
1819
1820 boolean addToken = false;
1821 WindowToken token = mTokenMap.get(attrs.token);
1822 if (token == null) {
1823 if (attrs.type >= FIRST_APPLICATION_WINDOW
1824 && attrs.type <= LAST_APPLICATION_WINDOW) {
1825 Log.w(TAG, "Attempted to add application window with unknown token "
1826 + attrs.token + ". Aborting.");
1827 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1828 }
1829 if (attrs.type == TYPE_INPUT_METHOD) {
1830 Log.w(TAG, "Attempted to add input method window with unknown token "
1831 + attrs.token + ". Aborting.");
1832 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1833 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001834 if (attrs.type == TYPE_WALLPAPER) {
1835 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1836 + attrs.token + ". Aborting.");
1837 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 token = new WindowToken(attrs.token, -1, false);
1840 addToken = true;
1841 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1842 && attrs.type <= LAST_APPLICATION_WINDOW) {
1843 AppWindowToken atoken = token.appWindowToken;
1844 if (atoken == null) {
1845 Log.w(TAG, "Attempted to add window with non-application token "
1846 + token + ". Aborting.");
1847 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1848 } else if (atoken.removed) {
1849 Log.w(TAG, "Attempted to add window with exiting application token "
1850 + token + ". Aborting.");
1851 return WindowManagerImpl.ADD_APP_EXITING;
1852 }
1853 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1854 // No need for this guy!
1855 if (localLOGV) Log.v(
1856 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1857 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1858 }
1859 } else if (attrs.type == TYPE_INPUT_METHOD) {
1860 if (token.windowType != TYPE_INPUT_METHOD) {
1861 Log.w(TAG, "Attempted to add input method window with bad token "
1862 + attrs.token + ". Aborting.");
1863 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1864 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001865 } else if (attrs.type == TYPE_WALLPAPER) {
1866 if (token.windowType != TYPE_WALLPAPER) {
1867 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1868 + attrs.token + ". Aborting.");
1869 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
1872
1873 win = new WindowState(session, client, token,
1874 attachedWindow, attrs, viewVisibility);
1875 if (win.mDeathRecipient == null) {
1876 // Client has apparently died, so there is no reason to
1877 // continue.
1878 Log.w(TAG, "Adding window client " + client.asBinder()
1879 + " that is dead, aborting.");
1880 return WindowManagerImpl.ADD_APP_EXITING;
1881 }
1882
1883 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 res = mPolicy.prepareAddWindowLw(win, attrs);
1886 if (res != WindowManagerImpl.ADD_OKAY) {
1887 return res;
1888 }
1889
1890 // From now on, no exceptions or errors allowed!
1891
1892 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 if (addToken) {
1897 mTokenMap.put(attrs.token, token);
1898 mTokenList.add(token);
1899 }
1900 win.attach();
1901 mWindowMap.put(client.asBinder(), win);
1902
1903 if (attrs.type == TYPE_APPLICATION_STARTING &&
1904 token.appWindowToken != null) {
1905 token.appWindowToken.startingWindow = win;
1906 }
1907
1908 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 if (attrs.type == TYPE_INPUT_METHOD) {
1911 mInputMethodWindow = win;
1912 addInputMethodWindowToListLocked(win);
1913 imMayMove = false;
1914 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1915 mInputMethodDialogs.add(win);
1916 addWindowToListInOrderLocked(win, true);
1917 adjustInputMethodDialogsLocked();
1918 imMayMove = false;
1919 } else {
1920 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001921 if (attrs.type == TYPE_WALLPAPER) {
1922 mLastWallpaperTimeoutTime = 0;
1923 adjustWallpaperWindowsLocked();
1924 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001925 adjustWallpaperWindowsLocked();
1926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
Romain Guy06882f82009-06-10 13:36:04 -07001928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 if (mInTouchMode) {
1934 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1935 }
1936 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1937 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1938 }
Romain Guy06882f82009-06-10 13:36:04 -07001939
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001940 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001942 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1943 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 imMayMove = false;
1945 }
1946 }
Romain Guy06882f82009-06-10 13:36:04 -07001947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001949 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
Romain Guy06882f82009-06-10 13:36:04 -07001951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 assignLayersLocked();
1953 // Don't do layout here, the window must call
1954 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 //dump();
1957
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001958 if (focusChanged) {
1959 if (mCurrentFocus != null) {
1960 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1961 }
1962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 if (localLOGV) Log.v(
1964 TAG, "New client " + client.asBinder()
1965 + ": window=" + win);
1966 }
1967
1968 // sendNewConfiguration() checks caller permissions so we must call it with
1969 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1970 // identity anyway, so it's safe to just clear & restore around this whole
1971 // block.
1972 final long origId = Binder.clearCallingIdentity();
1973 if (reportNewConfig) {
1974 sendNewConfiguration();
1975 } else {
1976 // Update Orientation after adding a window, only if the window needs to be
1977 // displayed right away
1978 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001979 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 sendNewConfiguration();
1981 }
1982 }
1983 }
1984 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 return res;
1987 }
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 public void removeWindow(Session session, IWindow client) {
1990 synchronized(mWindowMap) {
1991 WindowState win = windowForClientLocked(session, client);
1992 if (win == null) {
1993 return;
1994 }
1995 removeWindowLocked(session, win);
1996 }
1997 }
Romain Guy06882f82009-06-10 13:36:04 -07001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 public void removeWindowLocked(Session session, WindowState win) {
2000
2001 if (localLOGV || DEBUG_FOCUS) Log.v(
2002 TAG, "Remove " + win + " client="
2003 + Integer.toHexString(System.identityHashCode(
2004 win.mClient.asBinder()))
2005 + ", surface=" + win.mSurface);
2006
2007 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 if (DEBUG_APP_TRANSITIONS) Log.v(
2010 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2011 + " mExiting=" + win.mExiting
2012 + " isAnimating=" + win.isAnimating()
2013 + " app-animation="
2014 + (win.mAppToken != null ? win.mAppToken.animation : null)
2015 + " inPendingTransaction="
2016 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2017 + " mDisplayFrozen=" + mDisplayFrozen);
2018 // Visibility of the removed window. Will be used later to update orientation later on.
2019 boolean wasVisible = false;
2020 // First, see if we need to run an animation. If we do, we have
2021 // to hold off on removing the window until the animation is done.
2022 // If the display is frozen, just remove immediately, since the
2023 // animation wouldn't be seen.
2024 if (win.mSurface != null && !mDisplayFrozen) {
2025 // If we are not currently running the exit animation, we
2026 // need to see about starting one.
2027 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2030 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2031 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2032 }
2033 // Try starting an animation.
2034 if (applyAnimationLocked(win, transit, false)) {
2035 win.mExiting = true;
2036 }
2037 }
2038 if (win.mExiting || win.isAnimating()) {
2039 // The exit animation is running... wait for it!
2040 //Log.i(TAG, "*** Running exit animation...");
2041 win.mExiting = true;
2042 win.mRemoveOnExit = true;
2043 mLayoutNeeded = true;
2044 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2045 performLayoutAndPlaceSurfacesLocked();
2046 if (win.mAppToken != null) {
2047 win.mAppToken.updateReportedVisibilityLocked();
2048 }
2049 //dump();
2050 Binder.restoreCallingIdentity(origId);
2051 return;
2052 }
2053 }
2054
2055 removeWindowInnerLocked(session, win);
2056 // Removing a visible window will effect the computed orientation
2057 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002058 if (wasVisible && computeForcedAppOrientationLocked()
2059 != mForcedAppOrientation) {
2060 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2063 Binder.restoreCallingIdentity(origId);
2064 }
Romain Guy06882f82009-06-10 13:36:04 -07002065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002067 mKeyWaiter.finishedKey(session, win.mClient, true,
2068 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2070 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 if (mInputMethodTarget == win) {
2075 moveInputMethodWindowsIfNeededLocked(false);
2076 }
Romain Guy06882f82009-06-10 13:36:04 -07002077
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002078 if (false) {
2079 RuntimeException e = new RuntimeException("here");
2080 e.fillInStackTrace();
2081 Log.w(TAG, "Removing window " + win, e);
2082 }
2083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 mPolicy.removeWindowLw(win);
2085 win.removeLocked();
2086
2087 mWindowMap.remove(win.mClient.asBinder());
2088 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002089 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090
2091 if (mInputMethodWindow == win) {
2092 mInputMethodWindow = null;
2093 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2094 mInputMethodDialogs.remove(win);
2095 }
Romain Guy06882f82009-06-10 13:36:04 -07002096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 final WindowToken token = win.mToken;
2098 final AppWindowToken atoken = win.mAppToken;
2099 token.windows.remove(win);
2100 if (atoken != null) {
2101 atoken.allAppWindows.remove(win);
2102 }
2103 if (localLOGV) Log.v(
2104 TAG, "**** Removing window " + win + ": count="
2105 + token.windows.size());
2106 if (token.windows.size() == 0) {
2107 if (!token.explicit) {
2108 mTokenMap.remove(token.token);
2109 mTokenList.remove(token);
2110 } else if (atoken != null) {
2111 atoken.firstWindowDrawn = false;
2112 }
2113 }
2114
2115 if (atoken != null) {
2116 if (atoken.startingWindow == win) {
2117 atoken.startingWindow = null;
2118 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2119 // If this is the last window and we had requested a starting
2120 // transition window, well there is no point now.
2121 atoken.startingData = null;
2122 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2123 // If this is the last window except for a starting transition
2124 // window, we need to get rid of the starting transition.
2125 if (DEBUG_STARTING_WINDOW) {
2126 Log.v(TAG, "Schedule remove starting " + token
2127 + ": no more real windows");
2128 }
2129 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2130 mH.sendMessage(m);
2131 }
2132 }
Romain Guy06882f82009-06-10 13:36:04 -07002133
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002134 if (win.mAttrs.type == TYPE_WALLPAPER) {
2135 mLastWallpaperTimeoutTime = 0;
2136 adjustWallpaperWindowsLocked();
2137 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002138 adjustWallpaperWindowsLocked();
2139 }
2140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 if (!mInLayout) {
2142 assignLayersLocked();
2143 mLayoutNeeded = true;
2144 performLayoutAndPlaceSurfacesLocked();
2145 if (win.mAppToken != null) {
2146 win.mAppToken.updateReportedVisibilityLocked();
2147 }
2148 }
2149 }
2150
2151 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2152 long origId = Binder.clearCallingIdentity();
2153 try {
2154 synchronized (mWindowMap) {
2155 WindowState w = windowForClientLocked(session, client);
2156 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002157 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 Surface.openTransaction();
2159 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002160 if (SHOW_TRANSACTIONS) Log.i(
2161 TAG, " SURFACE " + w.mSurface
2162 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 w.mSurface.setTransparentRegionHint(region);
2164 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002165 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 Surface.closeTransaction();
2167 }
2168 }
2169 }
2170 } finally {
2171 Binder.restoreCallingIdentity(origId);
2172 }
2173 }
2174
2175 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002176 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 Rect visibleInsets) {
2178 long origId = Binder.clearCallingIdentity();
2179 try {
2180 synchronized (mWindowMap) {
2181 WindowState w = windowForClientLocked(session, client);
2182 if (w != null) {
2183 w.mGivenInsetsPending = false;
2184 w.mGivenContentInsets.set(contentInsets);
2185 w.mGivenVisibleInsets.set(visibleInsets);
2186 w.mTouchableInsets = touchableInsets;
2187 mLayoutNeeded = true;
2188 performLayoutAndPlaceSurfacesLocked();
2189 }
2190 }
2191 } finally {
2192 Binder.restoreCallingIdentity(origId);
2193 }
2194 }
Romain Guy06882f82009-06-10 13:36:04 -07002195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 public void getWindowDisplayFrame(Session session, IWindow client,
2197 Rect outDisplayFrame) {
2198 synchronized(mWindowMap) {
2199 WindowState win = windowForClientLocked(session, client);
2200 if (win == null) {
2201 outDisplayFrame.setEmpty();
2202 return;
2203 }
2204 outDisplayFrame.set(win.mDisplayFrame);
2205 }
2206 }
2207
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002208 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2209 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002210 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2211 window.mWallpaperX = x;
2212 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002213 window.mWallpaperXStep = xStep;
2214 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002215 if (updateWallpaperOffsetLocked(window, true)) {
2216 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002217 }
2218 }
2219 }
2220
Dianne Hackborn75804932009-10-20 20:15:20 -07002221 void wallpaperCommandComplete(IBinder window, Bundle result) {
2222 synchronized (mWindowMap) {
2223 if (mWaitingOnWallpaper != null &&
2224 mWaitingOnWallpaper.mClient.asBinder() == window) {
2225 mWaitingOnWallpaper = null;
2226 mWindowMap.notifyAll();
2227 }
2228 }
2229 }
2230
2231 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2232 String action, int x, int y, int z, Bundle extras, boolean sync) {
2233 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2234 || window == mUpperWallpaperTarget) {
2235 boolean doWait = sync;
2236 int curTokenIndex = mWallpaperTokens.size();
2237 while (curTokenIndex > 0) {
2238 curTokenIndex--;
2239 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2240 int curWallpaperIndex = token.windows.size();
2241 while (curWallpaperIndex > 0) {
2242 curWallpaperIndex--;
2243 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2244 try {
2245 wallpaper.mClient.dispatchWallpaperCommand(action,
2246 x, y, z, extras, sync);
2247 // We only want to be synchronous with one wallpaper.
2248 sync = false;
2249 } catch (RemoteException e) {
2250 }
2251 }
2252 }
2253
2254 if (doWait) {
2255 // XXX Need to wait for result.
2256 }
2257 }
2258
2259 return null;
2260 }
2261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 public int relayoutWindow(Session session, IWindow client,
2263 WindowManager.LayoutParams attrs, int requestedWidth,
2264 int requestedHeight, int viewVisibility, boolean insetsPending,
2265 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2266 Surface outSurface) {
2267 boolean displayed = false;
2268 boolean inTouchMode;
2269 Configuration newConfig = null;
2270 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 synchronized(mWindowMap) {
2273 WindowState win = windowForClientLocked(session, client);
2274 if (win == null) {
2275 return 0;
2276 }
2277 win.mRequestedWidth = requestedWidth;
2278 win.mRequestedHeight = requestedHeight;
2279
2280 if (attrs != null) {
2281 mPolicy.adjustWindowParamsLw(attrs);
2282 }
Romain Guy06882f82009-06-10 13:36:04 -07002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 int attrChanges = 0;
2285 int flagChanges = 0;
2286 if (attrs != null) {
2287 flagChanges = win.mAttrs.flags ^= attrs.flags;
2288 attrChanges = win.mAttrs.copyFrom(attrs);
2289 }
2290
2291 if (localLOGV) Log.v(
2292 TAG, "Relayout given client " + client.asBinder()
2293 + " (" + win.mAttrs.getTitle() + ")");
2294
2295
2296 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2297 win.mAlpha = attrs.alpha;
2298 }
2299
2300 final boolean scaledWindow =
2301 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2302
2303 if (scaledWindow) {
2304 // requested{Width|Height} Surface's physical size
2305 // attrs.{width|height} Size on screen
2306 win.mHScale = (attrs.width != requestedWidth) ?
2307 (attrs.width / (float)requestedWidth) : 1.0f;
2308 win.mVScale = (attrs.height != requestedHeight) ?
2309 (attrs.height / (float)requestedHeight) : 1.0f;
2310 }
2311
2312 boolean imMayMove = (flagChanges&(
2313 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2314 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 boolean focusMayChange = win.mViewVisibility != viewVisibility
2317 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2318 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002319
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002320 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2321 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 win.mRelayoutCalled = true;
2324 final int oldVisibility = win.mViewVisibility;
2325 win.mViewVisibility = viewVisibility;
2326 if (viewVisibility == View.VISIBLE &&
2327 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2328 displayed = !win.isVisibleLw();
2329 if (win.mExiting) {
2330 win.mExiting = false;
2331 win.mAnimation = null;
2332 }
2333 if (win.mDestroying) {
2334 win.mDestroying = false;
2335 mDestroySurface.remove(win);
2336 }
2337 if (oldVisibility == View.GONE) {
2338 win.mEnterAnimationPending = true;
2339 }
2340 if (displayed && win.mSurface != null && !win.mDrawPending
2341 && !win.mCommitDrawPending && !mDisplayFrozen) {
2342 applyEnterAnimationLocked(win);
2343 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002344 if (displayed && (win.mAttrs.flags
2345 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2346 win.mTurnOnScreen = true;
2347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2349 // To change the format, we need to re-build the surface.
2350 win.destroySurfaceLocked();
2351 displayed = true;
2352 }
2353 try {
2354 Surface surface = win.createSurfaceLocked();
2355 if (surface != null) {
2356 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002357 win.mReportDestroySurface = false;
2358 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002359 if (SHOW_TRANSACTIONS) Log.i(TAG,
2360 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002362 // For some reason there isn't a surface. Clear the
2363 // caller's object so they see the same state.
2364 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
2366 } catch (Exception e) {
2367 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002368 + client + " (" + win.mAttrs.getTitle() + ")",
2369 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 Binder.restoreCallingIdentity(origId);
2371 return 0;
2372 }
2373 if (displayed) {
2374 focusMayChange = true;
2375 }
2376 if (win.mAttrs.type == TYPE_INPUT_METHOD
2377 && mInputMethodWindow == null) {
2378 mInputMethodWindow = win;
2379 imMayMove = true;
2380 }
2381 } else {
2382 win.mEnterAnimationPending = false;
2383 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002384 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2385 + ": mExiting=" + win.mExiting
2386 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 // If we are not currently running the exit animation, we
2388 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002389 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 // Try starting an animation; if there isn't one, we
2391 // can destroy the surface right away.
2392 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2393 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2394 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2395 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002396 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002398 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 win.mExiting = true;
2400 mKeyWaiter.finishedKey(session, client, true,
2401 KeyWaiter.RETURN_NOTHING);
2402 } else if (win.isAnimating()) {
2403 // Currently in a hide animation... turn this into
2404 // an exit.
2405 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002406 } else if (win == mWallpaperTarget) {
2407 // If the wallpaper is currently behind this
2408 // window, we need to change both of them inside
2409 // of a transaction to avoid artifacts.
2410 win.mExiting = true;
2411 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 } else {
2413 if (mInputMethodWindow == win) {
2414 mInputMethodWindow = null;
2415 }
2416 win.destroySurfaceLocked();
2417 }
2418 }
2419 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002420
2421 if (win.mSurface == null || (win.getAttrs().flags
2422 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2423 || win.mSurfacePendingDestroy) {
2424 // We are being called from a local process, which
2425 // means outSurface holds its current surface. Ensure the
2426 // surface object is cleared, but we don't want it actually
2427 // destroyed at this point.
2428 win.mSurfacePendingDestroy = false;
2429 outSurface.release();
2430 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2431 } else if (win.mSurface != null) {
2432 if (DEBUG_VISIBILITY) Log.i(TAG,
2433 "Keeping surface, will report destroy: " + win);
2434 win.mReportDestroySurface = true;
2435 outSurface.copyFrom(win.mSurface);
2436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 if (focusMayChange) {
2440 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2441 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 imMayMove = false;
2443 }
2444 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2445 }
Romain Guy06882f82009-06-10 13:36:04 -07002446
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002447 // updateFocusedWindowLocked() already assigned layers so we only need to
2448 // reassign them at this point if the IM window state gets shuffled
2449 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 if (imMayMove) {
2452 if (moveInputMethodWindowsIfNeededLocked(false)) {
2453 assignLayers = true;
2454 }
2455 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002456 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002457 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002458 assignLayers = true;
2459 }
2460 }
Romain Guy06882f82009-06-10 13:36:04 -07002461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 mLayoutNeeded = true;
2463 win.mGivenInsetsPending = insetsPending;
2464 if (assignLayers) {
2465 assignLayersLocked();
2466 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002467 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002469 if (displayed && win.mIsWallpaper) {
2470 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002471 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 if (win.mAppToken != null) {
2474 win.mAppToken.updateReportedVisibilityLocked();
2475 }
2476 outFrame.set(win.mFrame);
2477 outContentInsets.set(win.mContentInsets);
2478 outVisibleInsets.set(win.mVisibleInsets);
2479 if (localLOGV) Log.v(
2480 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002481 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 + ", requestedHeight=" + requestedHeight
2483 + ", viewVisibility=" + viewVisibility
2484 + "\nRelayout returning frame=" + outFrame
2485 + ", surface=" + outSurface);
2486
2487 if (localLOGV || DEBUG_FOCUS) Log.v(
2488 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2489
2490 inTouchMode = mInTouchMode;
2491 }
2492
2493 if (newConfig != null) {
2494 sendNewConfiguration();
2495 }
Romain Guy06882f82009-06-10 13:36:04 -07002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2500 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2501 }
2502
2503 public void finishDrawingWindow(Session session, IWindow client) {
2504 final long origId = Binder.clearCallingIdentity();
2505 synchronized(mWindowMap) {
2506 WindowState win = windowForClientLocked(session, client);
2507 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002508 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2509 adjustWallpaperWindowsLocked();
2510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 mLayoutNeeded = true;
2512 performLayoutAndPlaceSurfacesLocked();
2513 }
2514 }
2515 Binder.restoreCallingIdentity(origId);
2516 }
2517
2518 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2519 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2520 + (lp != null ? lp.packageName : null)
2521 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2522 if (lp != null && lp.windowAnimations != 0) {
2523 // If this is a system resource, don't try to load it from the
2524 // application resources. It is nice to avoid loading application
2525 // resources if we can.
2526 String packageName = lp.packageName != null ? lp.packageName : "android";
2527 int resId = lp.windowAnimations;
2528 if ((resId&0xFF000000) == 0x01000000) {
2529 packageName = "android";
2530 }
2531 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2532 + packageName);
2533 return AttributeCache.instance().get(packageName, resId,
2534 com.android.internal.R.styleable.WindowAnimation);
2535 }
2536 return null;
2537 }
Romain Guy06882f82009-06-10 13:36:04 -07002538
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002539 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2540 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2541 + packageName + " resId=0x" + Integer.toHexString(resId));
2542 if (packageName != null) {
2543 if ((resId&0xFF000000) == 0x01000000) {
2544 packageName = "android";
2545 }
2546 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2547 + packageName);
2548 return AttributeCache.instance().get(packageName, resId,
2549 com.android.internal.R.styleable.WindowAnimation);
2550 }
2551 return null;
2552 }
2553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 private void applyEnterAnimationLocked(WindowState win) {
2555 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2556 if (win.mEnterAnimationPending) {
2557 win.mEnterAnimationPending = false;
2558 transit = WindowManagerPolicy.TRANSIT_ENTER;
2559 }
2560
2561 applyAnimationLocked(win, transit, true);
2562 }
2563
2564 private boolean applyAnimationLocked(WindowState win,
2565 int transit, boolean isEntrance) {
2566 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2567 // If we are trying to apply an animation, but already running
2568 // an animation of the same type, then just leave that one alone.
2569 return true;
2570 }
Romain Guy06882f82009-06-10 13:36:04 -07002571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 // Only apply an animation if the display isn't frozen. If it is
2573 // frozen, there is no reason to animate and it can cause strange
2574 // artifacts when we unfreeze the display if some different animation
2575 // is running.
2576 if (!mDisplayFrozen) {
2577 int anim = mPolicy.selectAnimationLw(win, transit);
2578 int attr = -1;
2579 Animation a = null;
2580 if (anim != 0) {
2581 a = AnimationUtils.loadAnimation(mContext, anim);
2582 } else {
2583 switch (transit) {
2584 case WindowManagerPolicy.TRANSIT_ENTER:
2585 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2586 break;
2587 case WindowManagerPolicy.TRANSIT_EXIT:
2588 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2589 break;
2590 case WindowManagerPolicy.TRANSIT_SHOW:
2591 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2592 break;
2593 case WindowManagerPolicy.TRANSIT_HIDE:
2594 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2595 break;
2596 }
2597 if (attr >= 0) {
2598 a = loadAnimation(win.mAttrs, attr);
2599 }
2600 }
2601 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2602 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2603 + " mAnimation=" + win.mAnimation
2604 + " isEntrance=" + isEntrance);
2605 if (a != null) {
2606 if (DEBUG_ANIM) {
2607 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002608 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2610 }
2611 win.setAnimation(a);
2612 win.mAnimationIsEntrance = isEntrance;
2613 }
2614 } else {
2615 win.clearAnimation();
2616 }
2617
2618 return win.mAnimation != null;
2619 }
2620
2621 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2622 int anim = 0;
2623 Context context = mContext;
2624 if (animAttr >= 0) {
2625 AttributeCache.Entry ent = getCachedAnimations(lp);
2626 if (ent != null) {
2627 context = ent.context;
2628 anim = ent.array.getResourceId(animAttr, 0);
2629 }
2630 }
2631 if (anim != 0) {
2632 return AnimationUtils.loadAnimation(context, anim);
2633 }
2634 return null;
2635 }
Romain Guy06882f82009-06-10 13:36:04 -07002636
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002637 private Animation loadAnimation(String packageName, int resId) {
2638 int anim = 0;
2639 Context context = mContext;
2640 if (resId >= 0) {
2641 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2642 if (ent != null) {
2643 context = ent.context;
2644 anim = resId;
2645 }
2646 }
2647 if (anim != 0) {
2648 return AnimationUtils.loadAnimation(context, anim);
2649 }
2650 return null;
2651 }
2652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 private boolean applyAnimationLocked(AppWindowToken wtoken,
2654 WindowManager.LayoutParams lp, int transit, boolean enter) {
2655 // Only apply an animation if the display isn't frozen. If it is
2656 // frozen, there is no reason to animate and it can cause strange
2657 // artifacts when we unfreeze the display if some different animation
2658 // is running.
2659 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002660 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002661 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002662 a = new FadeInOutAnimation(enter);
2663 if (DEBUG_ANIM) Log.v(TAG,
2664 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002665 } else if (mNextAppTransitionPackage != null) {
2666 a = loadAnimation(mNextAppTransitionPackage, enter ?
2667 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002668 } else {
2669 int animAttr = 0;
2670 switch (transit) {
2671 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2672 animAttr = enter
2673 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2674 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2675 break;
2676 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2677 animAttr = enter
2678 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2679 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2680 break;
2681 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2682 animAttr = enter
2683 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2684 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2685 break;
2686 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2687 animAttr = enter
2688 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2689 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2690 break;
2691 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2692 animAttr = enter
2693 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2694 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2695 break;
2696 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2697 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002698 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002699 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2700 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002701 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002702 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002703 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2704 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002705 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002706 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002707 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002708 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2709 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2710 break;
2711 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2712 animAttr = enter
2713 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2714 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2715 break;
2716 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2717 animAttr = enter
2718 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2719 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002720 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002721 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002722 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002723 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2724 + " anim=" + a
2725 + " animAttr=0x" + Integer.toHexString(animAttr)
2726 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 if (a != null) {
2729 if (DEBUG_ANIM) {
2730 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002731 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2733 }
2734 wtoken.setAnimation(a);
2735 }
2736 } else {
2737 wtoken.clearAnimation();
2738 }
2739
2740 return wtoken.animation != null;
2741 }
2742
2743 // -------------------------------------------------------------
2744 // Application Window Tokens
2745 // -------------------------------------------------------------
2746
2747 public void validateAppTokens(List tokens) {
2748 int v = tokens.size()-1;
2749 int m = mAppTokens.size()-1;
2750 while (v >= 0 && m >= 0) {
2751 AppWindowToken wtoken = mAppTokens.get(m);
2752 if (wtoken.removed) {
2753 m--;
2754 continue;
2755 }
2756 if (tokens.get(v) != wtoken.token) {
2757 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2758 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2759 }
2760 v--;
2761 m--;
2762 }
2763 while (v >= 0) {
2764 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2765 v--;
2766 }
2767 while (m >= 0) {
2768 AppWindowToken wtoken = mAppTokens.get(m);
2769 if (!wtoken.removed) {
2770 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2771 }
2772 m--;
2773 }
2774 }
2775
2776 boolean checkCallingPermission(String permission, String func) {
2777 // Quick check: if the calling permission is me, it's all okay.
2778 if (Binder.getCallingPid() == Process.myPid()) {
2779 return true;
2780 }
Romain Guy06882f82009-06-10 13:36:04 -07002781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 if (mContext.checkCallingPermission(permission)
2783 == PackageManager.PERMISSION_GRANTED) {
2784 return true;
2785 }
2786 String msg = "Permission Denial: " + func + " from pid="
2787 + Binder.getCallingPid()
2788 + ", uid=" + Binder.getCallingUid()
2789 + " requires " + permission;
2790 Log.w(TAG, msg);
2791 return false;
2792 }
Romain Guy06882f82009-06-10 13:36:04 -07002793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 AppWindowToken findAppWindowToken(IBinder token) {
2795 WindowToken wtoken = mTokenMap.get(token);
2796 if (wtoken == null) {
2797 return null;
2798 }
2799 return wtoken.appWindowToken;
2800 }
Romain Guy06882f82009-06-10 13:36:04 -07002801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 public void addWindowToken(IBinder token, int type) {
2803 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2804 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002805 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 }
Romain Guy06882f82009-06-10 13:36:04 -07002807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 synchronized(mWindowMap) {
2809 WindowToken wtoken = mTokenMap.get(token);
2810 if (wtoken != null) {
2811 Log.w(TAG, "Attempted to add existing input method token: " + token);
2812 return;
2813 }
2814 wtoken = new WindowToken(token, type, true);
2815 mTokenMap.put(token, wtoken);
2816 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002817 if (type == TYPE_WALLPAPER) {
2818 mWallpaperTokens.add(wtoken);
2819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 }
2821 }
Romain Guy06882f82009-06-10 13:36:04 -07002822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 public void removeWindowToken(IBinder token) {
2824 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2825 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002826 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
2828
2829 final long origId = Binder.clearCallingIdentity();
2830 synchronized(mWindowMap) {
2831 WindowToken wtoken = mTokenMap.remove(token);
2832 mTokenList.remove(wtoken);
2833 if (wtoken != null) {
2834 boolean delayed = false;
2835 if (!wtoken.hidden) {
2836 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 final int N = wtoken.windows.size();
2839 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 for (int i=0; i<N; i++) {
2842 WindowState win = wtoken.windows.get(i);
2843
2844 if (win.isAnimating()) {
2845 delayed = true;
2846 }
Romain Guy06882f82009-06-10 13:36:04 -07002847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 if (win.isVisibleNow()) {
2849 applyAnimationLocked(win,
2850 WindowManagerPolicy.TRANSIT_EXIT, false);
2851 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2852 KeyWaiter.RETURN_NOTHING);
2853 changed = true;
2854 }
2855 }
2856
2857 if (changed) {
2858 mLayoutNeeded = true;
2859 performLayoutAndPlaceSurfacesLocked();
2860 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2861 }
Romain Guy06882f82009-06-10 13:36:04 -07002862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 if (delayed) {
2864 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002865 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2866 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 }
2868 }
Romain Guy06882f82009-06-10 13:36:04 -07002869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 } else {
2871 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2872 }
2873 }
2874 Binder.restoreCallingIdentity(origId);
2875 }
2876
2877 public void addAppToken(int addPos, IApplicationToken token,
2878 int groupId, int requestedOrientation, boolean fullscreen) {
2879 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2880 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002881 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
Romain Guy06882f82009-06-10 13:36:04 -07002883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 synchronized(mWindowMap) {
2885 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2886 if (wtoken != null) {
2887 Log.w(TAG, "Attempted to add existing app token: " + token);
2888 return;
2889 }
2890 wtoken = new AppWindowToken(token);
2891 wtoken.groupId = groupId;
2892 wtoken.appFullscreen = fullscreen;
2893 wtoken.requestedOrientation = requestedOrientation;
2894 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002895 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 mTokenMap.put(token.asBinder(), wtoken);
2897 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 // Application tokens start out hidden.
2900 wtoken.hidden = true;
2901 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 //dump();
2904 }
2905 }
Romain Guy06882f82009-06-10 13:36:04 -07002906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 public void setAppGroupId(IBinder token, int groupId) {
2908 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2909 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002910 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 }
2912
2913 synchronized(mWindowMap) {
2914 AppWindowToken wtoken = findAppWindowToken(token);
2915 if (wtoken == null) {
2916 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2917 return;
2918 }
2919 wtoken.groupId = groupId;
2920 }
2921 }
Romain Guy06882f82009-06-10 13:36:04 -07002922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 public int getOrientationFromWindowsLocked() {
2924 int pos = mWindows.size() - 1;
2925 while (pos >= 0) {
2926 WindowState wtoken = (WindowState) mWindows.get(pos);
2927 pos--;
2928 if (wtoken.mAppToken != null) {
2929 // We hit an application window. so the orientation will be determined by the
2930 // app window. No point in continuing further.
2931 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2932 }
2933 if (!wtoken.isVisibleLw()) {
2934 continue;
2935 }
2936 int req = wtoken.mAttrs.screenOrientation;
2937 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2938 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2939 continue;
2940 } else {
2941 return req;
2942 }
2943 }
2944 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2945 }
Romain Guy06882f82009-06-10 13:36:04 -07002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 public int getOrientationFromAppTokensLocked() {
2948 int pos = mAppTokens.size() - 1;
2949 int curGroup = 0;
2950 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002951 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002953 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 while (pos >= 0) {
2955 AppWindowToken wtoken = mAppTokens.get(pos);
2956 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002957 // if we're about to tear down this window and not seek for
2958 // the behind activity, don't use it for orientation
2959 if (!findingBehind
2960 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002961 continue;
2962 }
2963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 if (!haveGroup) {
2965 // We ignore any hidden applications on the top.
2966 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2967 continue;
2968 }
2969 haveGroup = true;
2970 curGroup = wtoken.groupId;
2971 lastOrientation = wtoken.requestedOrientation;
2972 } else if (curGroup != wtoken.groupId) {
2973 // If we have hit a new application group, and the bottom
2974 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002975 // the orientation behind it, and the last app was
2976 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002978 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2979 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 return lastOrientation;
2981 }
2982 }
2983 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002984 // If this application is fullscreen, and didn't explicitly say
2985 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002987 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002988 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002989 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 return or;
2991 }
2992 // If this application has requested an explicit orientation,
2993 // then use it.
2994 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2995 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2996 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2997 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2998 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2999 return or;
3000 }
Owen Lin3413b892009-05-01 17:12:32 -07003001 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 }
3003 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3004 }
Romain Guy06882f82009-06-10 13:36:04 -07003005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003007 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003008 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3009 "updateOrientationFromAppTokens()")) {
3010 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3011 }
3012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 Configuration config;
3014 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003015 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3016 freezeThisOneIfNeeded);
3017 Binder.restoreCallingIdentity(ident);
3018 return config;
3019 }
3020
3021 Configuration updateOrientationFromAppTokensUnchecked(
3022 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3023 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003025 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003026 if (config != null) {
3027 mLayoutNeeded = true;
3028 performLayoutAndPlaceSurfacesLocked();
3029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 return config;
3032 }
Romain Guy06882f82009-06-10 13:36:04 -07003033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 /*
3035 * The orientation is computed from non-application windows first. If none of
3036 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003037 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3039 * android.os.IBinder)
3040 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003041 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003042 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 long ident = Binder.clearCallingIdentity();
3045 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003046 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 if (req != mForcedAppOrientation) {
3049 changed = true;
3050 mForcedAppOrientation = req;
3051 //send a message to Policy indicating orientation change to take
3052 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003053 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 }
Romain Guy06882f82009-06-10 13:36:04 -07003055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 if (changed) {
3057 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003058 WindowManagerPolicy.USE_LAST_ROTATION,
3059 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 if (changed) {
3061 if (freezeThisOneIfNeeded != null) {
3062 AppWindowToken wtoken = findAppWindowToken(
3063 freezeThisOneIfNeeded);
3064 if (wtoken != null) {
3065 startAppFreezingScreenLocked(wtoken,
3066 ActivityInfo.CONFIG_ORIENTATION);
3067 }
3068 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003069 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 }
3071 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003072
3073 // No obvious action we need to take, but if our current
3074 // state mismatches the activity maanager's, update it
3075 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003076 mTempConfiguration.setToDefaults();
3077 if (computeNewConfigurationLocked(mTempConfiguration)) {
3078 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003079 return new Configuration(mTempConfiguration);
3080 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003081 }
3082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 } finally {
3084 Binder.restoreCallingIdentity(ident);
3085 }
Romain Guy06882f82009-06-10 13:36:04 -07003086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 return null;
3088 }
Romain Guy06882f82009-06-10 13:36:04 -07003089
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003090 int computeForcedAppOrientationLocked() {
3091 int req = getOrientationFromWindowsLocked();
3092 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3093 req = getOrientationFromAppTokensLocked();
3094 }
3095 return req;
3096 }
Romain Guy06882f82009-06-10 13:36:04 -07003097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3099 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3100 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003101 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 }
Romain Guy06882f82009-06-10 13:36:04 -07003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 synchronized(mWindowMap) {
3105 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3106 if (wtoken == null) {
3107 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3108 return;
3109 }
Romain Guy06882f82009-06-10 13:36:04 -07003110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 wtoken.requestedOrientation = requestedOrientation;
3112 }
3113 }
Romain Guy06882f82009-06-10 13:36:04 -07003114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 public int getAppOrientation(IApplicationToken token) {
3116 synchronized(mWindowMap) {
3117 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3118 if (wtoken == null) {
3119 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3120 }
Romain Guy06882f82009-06-10 13:36:04 -07003121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 return wtoken.requestedOrientation;
3123 }
3124 }
Romain Guy06882f82009-06-10 13:36:04 -07003125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3127 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3128 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003129 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 }
3131
3132 synchronized(mWindowMap) {
3133 boolean changed = false;
3134 if (token == null) {
3135 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3136 changed = mFocusedApp != null;
3137 mFocusedApp = null;
3138 mKeyWaiter.tickle();
3139 } else {
3140 AppWindowToken newFocus = findAppWindowToken(token);
3141 if (newFocus == null) {
3142 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3143 return;
3144 }
3145 changed = mFocusedApp != newFocus;
3146 mFocusedApp = newFocus;
3147 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3148 mKeyWaiter.tickle();
3149 }
3150
3151 if (moveFocusNow && changed) {
3152 final long origId = Binder.clearCallingIdentity();
3153 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3154 Binder.restoreCallingIdentity(origId);
3155 }
3156 }
3157 }
3158
3159 public void prepareAppTransition(int transit) {
3160 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3161 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003162 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
Romain Guy06882f82009-06-10 13:36:04 -07003164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 synchronized(mWindowMap) {
3166 if (DEBUG_APP_TRANSITIONS) Log.v(
3167 TAG, "Prepare app transition: transit=" + transit
3168 + " mNextAppTransition=" + mNextAppTransition);
3169 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003170 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3171 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003173 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3174 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3175 // Opening a new task always supersedes a close for the anim.
3176 mNextAppTransition = transit;
3177 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3178 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3179 // Opening a new activity always supersedes a close for the anim.
3180 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 }
3182 mAppTransitionReady = false;
3183 mAppTransitionTimeout = false;
3184 mStartingIconInTransition = false;
3185 mSkipAppTransitionAnimation = false;
3186 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3187 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3188 5000);
3189 }
3190 }
3191 }
3192
3193 public int getPendingAppTransition() {
3194 return mNextAppTransition;
3195 }
Romain Guy06882f82009-06-10 13:36:04 -07003196
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003197 public void overridePendingAppTransition(String packageName,
3198 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003199 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003200 mNextAppTransitionPackage = packageName;
3201 mNextAppTransitionEnter = enterAnim;
3202 mNextAppTransitionExit = exitAnim;
3203 }
3204 }
3205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 public void executeAppTransition() {
3207 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3208 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003209 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 }
Romain Guy06882f82009-06-10 13:36:04 -07003211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003213 if (DEBUG_APP_TRANSITIONS) {
3214 RuntimeException e = new RuntimeException("here");
3215 e.fillInStackTrace();
3216 Log.w(TAG, "Execute app transition: mNextAppTransition="
3217 + mNextAppTransition, e);
3218 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003219 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 mAppTransitionReady = true;
3221 final long origId = Binder.clearCallingIdentity();
3222 performLayoutAndPlaceSurfacesLocked();
3223 Binder.restoreCallingIdentity(origId);
3224 }
3225 }
3226 }
3227
3228 public void setAppStartingWindow(IBinder token, String pkg,
3229 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3230 IBinder transferFrom, boolean createIfNeeded) {
3231 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3232 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003233 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 }
3235
3236 synchronized(mWindowMap) {
3237 if (DEBUG_STARTING_WINDOW) Log.v(
3238 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3239 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 AppWindowToken wtoken = findAppWindowToken(token);
3242 if (wtoken == null) {
3243 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3244 return;
3245 }
3246
3247 // If the display is frozen, we won't do anything until the
3248 // actual window is displayed so there is no reason to put in
3249 // the starting window.
3250 if (mDisplayFrozen) {
3251 return;
3252 }
Romain Guy06882f82009-06-10 13:36:04 -07003253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 if (wtoken.startingData != null) {
3255 return;
3256 }
Romain Guy06882f82009-06-10 13:36:04 -07003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 if (transferFrom != null) {
3259 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3260 if (ttoken != null) {
3261 WindowState startingWindow = ttoken.startingWindow;
3262 if (startingWindow != null) {
3263 if (mStartingIconInTransition) {
3264 // In this case, the starting icon has already
3265 // been displayed, so start letting windows get
3266 // shown immediately without any more transitions.
3267 mSkipAppTransitionAnimation = true;
3268 }
3269 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3270 "Moving existing starting from " + ttoken
3271 + " to " + wtoken);
3272 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 // Transfer the starting window over to the new
3275 // token.
3276 wtoken.startingData = ttoken.startingData;
3277 wtoken.startingView = ttoken.startingView;
3278 wtoken.startingWindow = startingWindow;
3279 ttoken.startingData = null;
3280 ttoken.startingView = null;
3281 ttoken.startingWindow = null;
3282 ttoken.startingMoved = true;
3283 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003284 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003286 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3287 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 mWindows.remove(startingWindow);
3289 ttoken.windows.remove(startingWindow);
3290 ttoken.allAppWindows.remove(startingWindow);
3291 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 // Propagate other interesting state between the
3294 // tokens. If the old token is displayed, we should
3295 // immediately force the new one to be displayed. If
3296 // it is animating, we need to move that animation to
3297 // the new one.
3298 if (ttoken.allDrawn) {
3299 wtoken.allDrawn = true;
3300 }
3301 if (ttoken.firstWindowDrawn) {
3302 wtoken.firstWindowDrawn = true;
3303 }
3304 if (!ttoken.hidden) {
3305 wtoken.hidden = false;
3306 wtoken.hiddenRequested = false;
3307 wtoken.willBeHidden = false;
3308 }
3309 if (wtoken.clientHidden != ttoken.clientHidden) {
3310 wtoken.clientHidden = ttoken.clientHidden;
3311 wtoken.sendAppVisibilityToClients();
3312 }
3313 if (ttoken.animation != null) {
3314 wtoken.animation = ttoken.animation;
3315 wtoken.animating = ttoken.animating;
3316 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3317 ttoken.animation = null;
3318 ttoken.animLayerAdjustment = 0;
3319 wtoken.updateLayers();
3320 ttoken.updateLayers();
3321 }
Romain Guy06882f82009-06-10 13:36:04 -07003322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 mLayoutNeeded = true;
3325 performLayoutAndPlaceSurfacesLocked();
3326 Binder.restoreCallingIdentity(origId);
3327 return;
3328 } else if (ttoken.startingData != null) {
3329 // The previous app was getting ready to show a
3330 // starting window, but hasn't yet done so. Steal it!
3331 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3332 "Moving pending starting from " + ttoken
3333 + " to " + wtoken);
3334 wtoken.startingData = ttoken.startingData;
3335 ttoken.startingData = null;
3336 ttoken.startingMoved = true;
3337 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3338 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3339 // want to process the message ASAP, before any other queued
3340 // messages.
3341 mH.sendMessageAtFrontOfQueue(m);
3342 return;
3343 }
3344 }
3345 }
3346
3347 // There is no existing starting window, and the caller doesn't
3348 // want us to create one, so that's it!
3349 if (!createIfNeeded) {
3350 return;
3351 }
Romain Guy06882f82009-06-10 13:36:04 -07003352
Dianne Hackborn284ac932009-08-28 10:34:25 -07003353 // If this is a translucent or wallpaper window, then don't
3354 // show a starting window -- the current effect (a full-screen
3355 // opaque starting window that fades away to the real contents
3356 // when it is ready) does not work for this.
3357 if (theme != 0) {
3358 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3359 com.android.internal.R.styleable.Window);
3360 if (ent.array.getBoolean(
3361 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3362 return;
3363 }
3364 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003365 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3366 return;
3367 }
3368 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003369 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3370 return;
3371 }
3372 }
3373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 mStartingIconInTransition = true;
3375 wtoken.startingData = new StartingData(
3376 pkg, theme, nonLocalizedLabel,
3377 labelRes, icon);
3378 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3379 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3380 // want to process the message ASAP, before any other queued
3381 // messages.
3382 mH.sendMessageAtFrontOfQueue(m);
3383 }
3384 }
3385
3386 public void setAppWillBeHidden(IBinder token) {
3387 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3388 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003389 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
3391
3392 AppWindowToken wtoken;
3393
3394 synchronized(mWindowMap) {
3395 wtoken = findAppWindowToken(token);
3396 if (wtoken == null) {
3397 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3398 return;
3399 }
3400 wtoken.willBeHidden = true;
3401 }
3402 }
Romain Guy06882f82009-06-10 13:36:04 -07003403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3405 boolean visible, int transit, boolean performLayout) {
3406 boolean delayed = false;
3407
3408 if (wtoken.clientHidden == visible) {
3409 wtoken.clientHidden = !visible;
3410 wtoken.sendAppVisibilityToClients();
3411 }
Romain Guy06882f82009-06-10 13:36:04 -07003412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 wtoken.willBeHidden = false;
3414 if (wtoken.hidden == visible) {
3415 final int N = wtoken.allAppWindows.size();
3416 boolean changed = false;
3417 if (DEBUG_APP_TRANSITIONS) Log.v(
3418 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3419 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003422
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003423 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 if (wtoken.animation == sDummyAnimation) {
3425 wtoken.animation = null;
3426 }
3427 applyAnimationLocked(wtoken, lp, transit, visible);
3428 changed = true;
3429 if (wtoken.animation != null) {
3430 delayed = runningAppAnimation = true;
3431 }
3432 }
Romain Guy06882f82009-06-10 13:36:04 -07003433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 for (int i=0; i<N; i++) {
3435 WindowState win = wtoken.allAppWindows.get(i);
3436 if (win == wtoken.startingWindow) {
3437 continue;
3438 }
3439
3440 if (win.isAnimating()) {
3441 delayed = true;
3442 }
Romain Guy06882f82009-06-10 13:36:04 -07003443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3445 //win.dump(" ");
3446 if (visible) {
3447 if (!win.isVisibleNow()) {
3448 if (!runningAppAnimation) {
3449 applyAnimationLocked(win,
3450 WindowManagerPolicy.TRANSIT_ENTER, true);
3451 }
3452 changed = true;
3453 }
3454 } else if (win.isVisibleNow()) {
3455 if (!runningAppAnimation) {
3456 applyAnimationLocked(win,
3457 WindowManagerPolicy.TRANSIT_EXIT, false);
3458 }
3459 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3460 KeyWaiter.RETURN_NOTHING);
3461 changed = true;
3462 }
3463 }
3464
3465 wtoken.hidden = wtoken.hiddenRequested = !visible;
3466 if (!visible) {
3467 unsetAppFreezingScreenLocked(wtoken, true, true);
3468 } else {
3469 // If we are being set visible, and the starting window is
3470 // not yet displayed, then make sure it doesn't get displayed.
3471 WindowState swin = wtoken.startingWindow;
3472 if (swin != null && (swin.mDrawPending
3473 || swin.mCommitDrawPending)) {
3474 swin.mPolicyVisibility = false;
3475 swin.mPolicyVisibilityAfterAnim = false;
3476 }
3477 }
Romain Guy06882f82009-06-10 13:36:04 -07003478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3480 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3481 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 if (changed && performLayout) {
3484 mLayoutNeeded = true;
3485 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 performLayoutAndPlaceSurfacesLocked();
3487 }
3488 }
3489
3490 if (wtoken.animation != null) {
3491 delayed = true;
3492 }
Romain Guy06882f82009-06-10 13:36:04 -07003493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 return delayed;
3495 }
3496
3497 public void setAppVisibility(IBinder token, boolean visible) {
3498 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3499 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003500 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 }
3502
3503 AppWindowToken wtoken;
3504
3505 synchronized(mWindowMap) {
3506 wtoken = findAppWindowToken(token);
3507 if (wtoken == null) {
3508 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3509 return;
3510 }
3511
3512 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3513 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003514 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3516 + "): mNextAppTransition=" + mNextAppTransition
3517 + " hidden=" + wtoken.hidden
3518 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3519 }
Romain Guy06882f82009-06-10 13:36:04 -07003520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 // If we are preparing an app transition, then delay changing
3522 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003523 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 // Already in requested state, don't do anything more.
3525 if (wtoken.hiddenRequested != visible) {
3526 return;
3527 }
3528 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 if (DEBUG_APP_TRANSITIONS) Log.v(
3531 TAG, "Setting dummy animation on: " + wtoken);
3532 wtoken.setDummyAnimation();
3533 mOpeningApps.remove(wtoken);
3534 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003535 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 wtoken.inPendingTransaction = true;
3537 if (visible) {
3538 mOpeningApps.add(wtoken);
3539 wtoken.allDrawn = false;
3540 wtoken.startingDisplayed = false;
3541 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003542 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 if (wtoken.clientHidden) {
3545 // In the case where we are making an app visible
3546 // but holding off for a transition, we still need
3547 // to tell the client to make its windows visible so
3548 // they get drawn. Otherwise, we will wait on
3549 // performing the transition until all windows have
3550 // been drawn, they never will be, and we are sad.
3551 wtoken.clientHidden = false;
3552 wtoken.sendAppVisibilityToClients();
3553 }
3554 } else {
3555 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003556 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 }
3558 return;
3559 }
Romain Guy06882f82009-06-10 13:36:04 -07003560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003562 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 wtoken.updateReportedVisibilityLocked();
3564 Binder.restoreCallingIdentity(origId);
3565 }
3566 }
3567
3568 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3569 boolean unfreezeSurfaceNow, boolean force) {
3570 if (wtoken.freezingScreen) {
3571 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3572 + " force=" + force);
3573 final int N = wtoken.allAppWindows.size();
3574 boolean unfrozeWindows = false;
3575 for (int i=0; i<N; i++) {
3576 WindowState w = wtoken.allAppWindows.get(i);
3577 if (w.mAppFreezing) {
3578 w.mAppFreezing = false;
3579 if (w.mSurface != null && !w.mOrientationChanging) {
3580 w.mOrientationChanging = true;
3581 }
3582 unfrozeWindows = true;
3583 }
3584 }
3585 if (force || unfrozeWindows) {
3586 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3587 wtoken.freezingScreen = false;
3588 mAppsFreezingScreen--;
3589 }
3590 if (unfreezeSurfaceNow) {
3591 if (unfrozeWindows) {
3592 mLayoutNeeded = true;
3593 performLayoutAndPlaceSurfacesLocked();
3594 }
3595 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3596 stopFreezingDisplayLocked();
3597 }
3598 }
3599 }
3600 }
Romain Guy06882f82009-06-10 13:36:04 -07003601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3603 int configChanges) {
3604 if (DEBUG_ORIENTATION) {
3605 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003606 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 Log.i(TAG, "Set freezing of " + wtoken.appToken
3608 + ": hidden=" + wtoken.hidden + " freezing="
3609 + wtoken.freezingScreen, e);
3610 }
3611 if (!wtoken.hiddenRequested) {
3612 if (!wtoken.freezingScreen) {
3613 wtoken.freezingScreen = true;
3614 mAppsFreezingScreen++;
3615 if (mAppsFreezingScreen == 1) {
3616 startFreezingDisplayLocked();
3617 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3618 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3619 5000);
3620 }
3621 }
3622 final int N = wtoken.allAppWindows.size();
3623 for (int i=0; i<N; i++) {
3624 WindowState w = wtoken.allAppWindows.get(i);
3625 w.mAppFreezing = true;
3626 }
3627 }
3628 }
Romain Guy06882f82009-06-10 13:36:04 -07003629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 public void startAppFreezingScreen(IBinder token, int configChanges) {
3631 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3632 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003633 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 }
3635
3636 synchronized(mWindowMap) {
3637 if (configChanges == 0 && !mDisplayFrozen) {
3638 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3639 return;
3640 }
Romain Guy06882f82009-06-10 13:36:04 -07003641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 AppWindowToken wtoken = findAppWindowToken(token);
3643 if (wtoken == null || wtoken.appToken == null) {
3644 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3645 return;
3646 }
3647 final long origId = Binder.clearCallingIdentity();
3648 startAppFreezingScreenLocked(wtoken, configChanges);
3649 Binder.restoreCallingIdentity(origId);
3650 }
3651 }
Romain Guy06882f82009-06-10 13:36:04 -07003652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 public void stopAppFreezingScreen(IBinder token, boolean force) {
3654 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3655 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003656 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 }
3658
3659 synchronized(mWindowMap) {
3660 AppWindowToken wtoken = findAppWindowToken(token);
3661 if (wtoken == null || wtoken.appToken == null) {
3662 return;
3663 }
3664 final long origId = Binder.clearCallingIdentity();
3665 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3666 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3667 unsetAppFreezingScreenLocked(wtoken, true, force);
3668 Binder.restoreCallingIdentity(origId);
3669 }
3670 }
Romain Guy06882f82009-06-10 13:36:04 -07003671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 public void removeAppToken(IBinder token) {
3673 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3674 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003675 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 }
3677
3678 AppWindowToken wtoken = null;
3679 AppWindowToken startingToken = null;
3680 boolean delayed = false;
3681
3682 final long origId = Binder.clearCallingIdentity();
3683 synchronized(mWindowMap) {
3684 WindowToken basewtoken = mTokenMap.remove(token);
3685 mTokenList.remove(basewtoken);
3686 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3687 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003688 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 wtoken.inPendingTransaction = false;
3690 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003691 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 if (mClosingApps.contains(wtoken)) {
3693 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003694 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003696 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 delayed = true;
3698 }
3699 if (DEBUG_APP_TRANSITIONS) Log.v(
3700 TAG, "Removing app " + wtoken + " delayed=" + delayed
3701 + " animation=" + wtoken.animation
3702 + " animating=" + wtoken.animating);
3703 if (delayed) {
3704 // set the token aside because it has an active animation to be finished
3705 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003706 } else {
3707 // Make sure there is no animation running on this token,
3708 // so any windows associated with it will be removed as
3709 // soon as their animations are complete
3710 wtoken.animation = null;
3711 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 }
3713 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003714 if (mLastEnterAnimToken == wtoken) {
3715 mLastEnterAnimToken = null;
3716 mLastEnterAnimParams = null;
3717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 wtoken.removed = true;
3719 if (wtoken.startingData != null) {
3720 startingToken = wtoken;
3721 }
3722 unsetAppFreezingScreenLocked(wtoken, true, true);
3723 if (mFocusedApp == wtoken) {
3724 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3725 mFocusedApp = null;
3726 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3727 mKeyWaiter.tickle();
3728 }
3729 } else {
3730 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3731 }
Romain Guy06882f82009-06-10 13:36:04 -07003732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 if (!delayed && wtoken != null) {
3734 wtoken.updateReportedVisibilityLocked();
3735 }
3736 }
3737 Binder.restoreCallingIdentity(origId);
3738
3739 if (startingToken != null) {
3740 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3741 + startingToken + ": app token removed");
3742 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3743 mH.sendMessage(m);
3744 }
3745 }
3746
3747 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3748 final int NW = token.windows.size();
3749 for (int i=0; i<NW; i++) {
3750 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003751 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 mWindows.remove(win);
3753 int j = win.mChildWindows.size();
3754 while (j > 0) {
3755 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003756 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3757 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3758 "Tmp removing child window " + cwin);
3759 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 }
3761 }
3762 return NW > 0;
3763 }
3764
3765 void dumpAppTokensLocked() {
3766 for (int i=mAppTokens.size()-1; i>=0; i--) {
3767 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3768 }
3769 }
Romain Guy06882f82009-06-10 13:36:04 -07003770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 void dumpWindowsLocked() {
3772 for (int i=mWindows.size()-1; i>=0; i--) {
3773 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3774 }
3775 }
Romain Guy06882f82009-06-10 13:36:04 -07003776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 private int findWindowOffsetLocked(int tokenPos) {
3778 final int NW = mWindows.size();
3779
3780 if (tokenPos >= mAppTokens.size()) {
3781 int i = NW;
3782 while (i > 0) {
3783 i--;
3784 WindowState win = (WindowState)mWindows.get(i);
3785 if (win.getAppToken() != null) {
3786 return i+1;
3787 }
3788 }
3789 }
3790
3791 while (tokenPos > 0) {
3792 // Find the first app token below the new position that has
3793 // a window displayed.
3794 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3795 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3796 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003797 if (wtoken.sendingToBottom) {
3798 if (DEBUG_REORDER) Log.v(TAG,
3799 "Skipping token -- currently sending to bottom");
3800 tokenPos--;
3801 continue;
3802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 int i = wtoken.windows.size();
3804 while (i > 0) {
3805 i--;
3806 WindowState win = wtoken.windows.get(i);
3807 int j = win.mChildWindows.size();
3808 while (j > 0) {
3809 j--;
3810 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003811 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 for (int pos=NW-1; pos>=0; pos--) {
3813 if (mWindows.get(pos) == cwin) {
3814 if (DEBUG_REORDER) Log.v(TAG,
3815 "Found child win @" + (pos+1));
3816 return pos+1;
3817 }
3818 }
3819 }
3820 }
3821 for (int pos=NW-1; pos>=0; pos--) {
3822 if (mWindows.get(pos) == win) {
3823 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3824 return pos+1;
3825 }
3826 }
3827 }
3828 tokenPos--;
3829 }
3830
3831 return 0;
3832 }
3833
3834 private final int reAddWindowLocked(int index, WindowState win) {
3835 final int NCW = win.mChildWindows.size();
3836 boolean added = false;
3837 for (int j=0; j<NCW; j++) {
3838 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3839 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003840 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3841 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 mWindows.add(index, win);
3843 index++;
3844 added = true;
3845 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003846 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3847 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 mWindows.add(index, cwin);
3849 index++;
3850 }
3851 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003852 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3853 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 mWindows.add(index, win);
3855 index++;
3856 }
3857 return index;
3858 }
Romain Guy06882f82009-06-10 13:36:04 -07003859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3861 final int NW = token.windows.size();
3862 for (int i=0; i<NW; i++) {
3863 index = reAddWindowLocked(index, token.windows.get(i));
3864 }
3865 return index;
3866 }
3867
3868 public void moveAppToken(int index, IBinder token) {
3869 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3870 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003871 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 }
3873
3874 synchronized(mWindowMap) {
3875 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3876 if (DEBUG_REORDER) dumpAppTokensLocked();
3877 final AppWindowToken wtoken = findAppWindowToken(token);
3878 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3879 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3880 + token + " (" + wtoken + ")");
3881 return;
3882 }
3883 mAppTokens.add(index, wtoken);
3884 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3885 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 final long origId = Binder.clearCallingIdentity();
3888 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3889 if (DEBUG_REORDER) dumpWindowsLocked();
3890 if (tmpRemoveAppWindowsLocked(wtoken)) {
3891 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3892 if (DEBUG_REORDER) dumpWindowsLocked();
3893 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3894 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3895 if (DEBUG_REORDER) dumpWindowsLocked();
3896 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 mLayoutNeeded = true;
3898 performLayoutAndPlaceSurfacesLocked();
3899 }
3900 Binder.restoreCallingIdentity(origId);
3901 }
3902 }
3903
3904 private void removeAppTokensLocked(List<IBinder> tokens) {
3905 // XXX This should be done more efficiently!
3906 // (take advantage of the fact that both lists should be
3907 // ordered in the same way.)
3908 int N = tokens.size();
3909 for (int i=0; i<N; i++) {
3910 IBinder token = tokens.get(i);
3911 final AppWindowToken wtoken = findAppWindowToken(token);
3912 if (!mAppTokens.remove(wtoken)) {
3913 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3914 + token + " (" + wtoken + ")");
3915 i--;
3916 N--;
3917 }
3918 }
3919 }
3920
Dianne Hackborna8f60182009-09-01 19:01:50 -07003921 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3922 boolean updateFocusAndLayout) {
3923 // First remove all of the windows from the list.
3924 tmpRemoveAppWindowsLocked(wtoken);
3925
3926 // Where to start adding?
3927 int pos = findWindowOffsetLocked(tokenPos);
3928
3929 // And now add them back at the correct place.
3930 pos = reAddAppWindowsLocked(pos, wtoken);
3931
3932 if (updateFocusAndLayout) {
3933 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3934 assignLayersLocked();
3935 }
3936 mLayoutNeeded = true;
3937 performLayoutAndPlaceSurfacesLocked();
3938 }
3939 }
3940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3942 // First remove all of the windows from the list.
3943 final int N = tokens.size();
3944 int i;
3945 for (i=0; i<N; i++) {
3946 WindowToken token = mTokenMap.get(tokens.get(i));
3947 if (token != null) {
3948 tmpRemoveAppWindowsLocked(token);
3949 }
3950 }
3951
3952 // Where to start adding?
3953 int pos = findWindowOffsetLocked(tokenPos);
3954
3955 // And now add them back at the correct place.
3956 for (i=0; i<N; i++) {
3957 WindowToken token = mTokenMap.get(tokens.get(i));
3958 if (token != null) {
3959 pos = reAddAppWindowsLocked(pos, token);
3960 }
3961 }
3962
Dianne Hackborna8f60182009-09-01 19:01:50 -07003963 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3964 assignLayersLocked();
3965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 mLayoutNeeded = true;
3967 performLayoutAndPlaceSurfacesLocked();
3968
3969 //dump();
3970 }
3971
3972 public void moveAppTokensToTop(List<IBinder> tokens) {
3973 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3974 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003975 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 }
3977
3978 final long origId = Binder.clearCallingIdentity();
3979 synchronized(mWindowMap) {
3980 removeAppTokensLocked(tokens);
3981 final int N = tokens.size();
3982 for (int i=0; i<N; i++) {
3983 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3984 if (wt != null) {
3985 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003986 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003987 mToTopApps.remove(wt);
3988 mToBottomApps.remove(wt);
3989 mToTopApps.add(wt);
3990 wt.sendingToBottom = false;
3991 wt.sendingToTop = true;
3992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 }
3994 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003995
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003996 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003997 moveAppWindowsLocked(tokens, mAppTokens.size());
3998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 }
4000 Binder.restoreCallingIdentity(origId);
4001 }
4002
4003 public void moveAppTokensToBottom(List<IBinder> tokens) {
4004 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4005 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004006 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 }
4008
4009 final long origId = Binder.clearCallingIdentity();
4010 synchronized(mWindowMap) {
4011 removeAppTokensLocked(tokens);
4012 final int N = tokens.size();
4013 int pos = 0;
4014 for (int i=0; i<N; i++) {
4015 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4016 if (wt != null) {
4017 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004018 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004019 mToTopApps.remove(wt);
4020 mToBottomApps.remove(wt);
4021 mToBottomApps.add(i, wt);
4022 wt.sendingToTop = false;
4023 wt.sendingToBottom = true;
4024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 pos++;
4026 }
4027 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004028
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004029 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004030 moveAppWindowsLocked(tokens, 0);
4031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 }
4033 Binder.restoreCallingIdentity(origId);
4034 }
4035
4036 // -------------------------------------------------------------
4037 // Misc IWindowSession methods
4038 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004041 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 != PackageManager.PERMISSION_GRANTED) {
4043 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4044 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004045 synchronized (mKeyguardTokenWatcher) {
4046 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 }
4049
4050 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004051 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 != PackageManager.PERMISSION_GRANTED) {
4053 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4054 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004055 synchronized (mKeyguardTokenWatcher) {
4056 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057
Mike Lockwood983ee092009-11-22 01:42:24 -05004058 if (!mKeyguardTokenWatcher.isAcquired()) {
4059 // If we are the last one to reenable the keyguard wait until
4060 // we have actaully finished reenabling until returning.
4061 // It is possible that reenableKeyguard() can be called before
4062 // the previous disableKeyguard() is handled, in which case
4063 // neither mKeyguardTokenWatcher.acquired() or released() would
4064 // be called. In that case mKeyguardDisabled will be false here
4065 // and we have nothing to wait for.
4066 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004068 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 } catch (InterruptedException e) {
4070 Thread.currentThread().interrupt();
4071 }
4072 }
4073 }
4074 }
4075 }
4076
4077 /**
4078 * @see android.app.KeyguardManager#exitKeyguardSecurely
4079 */
4080 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004081 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 != PackageManager.PERMISSION_GRANTED) {
4083 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4084 }
4085 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4086 public void onKeyguardExitResult(boolean success) {
4087 try {
4088 callback.onKeyguardExitResult(success);
4089 } catch (RemoteException e) {
4090 // Client has died, we don't care.
4091 }
4092 }
4093 });
4094 }
4095
4096 public boolean inKeyguardRestrictedInputMode() {
4097 return mPolicy.inKeyguardRestrictedKeyInputMode();
4098 }
Romain Guy06882f82009-06-10 13:36:04 -07004099
Dianne Hackbornffa42482009-09-23 22:20:11 -07004100 public void closeSystemDialogs(String reason) {
4101 synchronized(mWindowMap) {
4102 for (int i=mWindows.size()-1; i>=0; i--) {
4103 WindowState w = (WindowState)mWindows.get(i);
4104 if (w.mSurface != null) {
4105 try {
4106 w.mClient.closeSystemDialogs(reason);
4107 } catch (RemoteException e) {
4108 }
4109 }
4110 }
4111 }
4112 }
4113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 static float fixScale(float scale) {
4115 if (scale < 0) scale = 0;
4116 else if (scale > 20) scale = 20;
4117 return Math.abs(scale);
4118 }
Romain Guy06882f82009-06-10 13:36:04 -07004119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 public void setAnimationScale(int which, float scale) {
4121 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4122 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004123 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 }
4125
4126 if (scale < 0) scale = 0;
4127 else if (scale > 20) scale = 20;
4128 scale = Math.abs(scale);
4129 switch (which) {
4130 case 0: mWindowAnimationScale = fixScale(scale); break;
4131 case 1: mTransitionAnimationScale = fixScale(scale); break;
4132 }
Romain Guy06882f82009-06-10 13:36:04 -07004133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 // Persist setting
4135 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4136 }
Romain Guy06882f82009-06-10 13:36:04 -07004137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 public void setAnimationScales(float[] scales) {
4139 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4140 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004141 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 }
4143
4144 if (scales != null) {
4145 if (scales.length >= 1) {
4146 mWindowAnimationScale = fixScale(scales[0]);
4147 }
4148 if (scales.length >= 2) {
4149 mTransitionAnimationScale = fixScale(scales[1]);
4150 }
4151 }
Romain Guy06882f82009-06-10 13:36:04 -07004152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 // Persist setting
4154 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4155 }
Romain Guy06882f82009-06-10 13:36:04 -07004156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 public float getAnimationScale(int which) {
4158 switch (which) {
4159 case 0: return mWindowAnimationScale;
4160 case 1: return mTransitionAnimationScale;
4161 }
4162 return 0;
4163 }
Romain Guy06882f82009-06-10 13:36:04 -07004164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 public float[] getAnimationScales() {
4166 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4167 }
Romain Guy06882f82009-06-10 13:36:04 -07004168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 public int getSwitchState(int sw) {
4170 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4171 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004172 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
4174 return KeyInputQueue.getSwitchState(sw);
4175 }
Romain Guy06882f82009-06-10 13:36:04 -07004176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 public int getSwitchStateForDevice(int devid, int sw) {
4178 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4179 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004180 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
4182 return KeyInputQueue.getSwitchState(devid, sw);
4183 }
Romain Guy06882f82009-06-10 13:36:04 -07004184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 public int getScancodeState(int sw) {
4186 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4187 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004188 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004190 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 }
Romain Guy06882f82009-06-10 13:36:04 -07004192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 public int getScancodeStateForDevice(int devid, int sw) {
4194 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4195 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004196 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004198 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 }
Romain Guy06882f82009-06-10 13:36:04 -07004200
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004201 public int getTrackballScancodeState(int sw) {
4202 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4203 "getTrackballScancodeState()")) {
4204 throw new SecurityException("Requires READ_INPUT_STATE permission");
4205 }
4206 return mQueue.getTrackballScancodeState(sw);
4207 }
4208
4209 public int getDPadScancodeState(int sw) {
4210 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4211 "getDPadScancodeState()")) {
4212 throw new SecurityException("Requires READ_INPUT_STATE permission");
4213 }
4214 return mQueue.getDPadScancodeState(sw);
4215 }
4216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 public int getKeycodeState(int sw) {
4218 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4219 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004220 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004222 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
Romain Guy06882f82009-06-10 13:36:04 -07004224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 public int getKeycodeStateForDevice(int devid, int sw) {
4226 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4227 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004228 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004230 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 }
Romain Guy06882f82009-06-10 13:36:04 -07004232
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004233 public int getTrackballKeycodeState(int sw) {
4234 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4235 "getTrackballKeycodeState()")) {
4236 throw new SecurityException("Requires READ_INPUT_STATE permission");
4237 }
4238 return mQueue.getTrackballKeycodeState(sw);
4239 }
4240
4241 public int getDPadKeycodeState(int sw) {
4242 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4243 "getDPadKeycodeState()")) {
4244 throw new SecurityException("Requires READ_INPUT_STATE permission");
4245 }
4246 return mQueue.getDPadKeycodeState(sw);
4247 }
4248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4250 return KeyInputQueue.hasKeys(keycodes, keyExists);
4251 }
Romain Guy06882f82009-06-10 13:36:04 -07004252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 public void enableScreenAfterBoot() {
4254 synchronized(mWindowMap) {
4255 if (mSystemBooted) {
4256 return;
4257 }
4258 mSystemBooted = true;
4259 }
Romain Guy06882f82009-06-10 13:36:04 -07004260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 performEnableScreen();
4262 }
Romain Guy06882f82009-06-10 13:36:04 -07004263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 public void enableScreenIfNeededLocked() {
4265 if (mDisplayEnabled) {
4266 return;
4267 }
4268 if (!mSystemBooted) {
4269 return;
4270 }
4271 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4272 }
Romain Guy06882f82009-06-10 13:36:04 -07004273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 public void performEnableScreen() {
4275 synchronized(mWindowMap) {
4276 if (mDisplayEnabled) {
4277 return;
4278 }
4279 if (!mSystemBooted) {
4280 return;
4281 }
Romain Guy06882f82009-06-10 13:36:04 -07004282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 // Don't enable the screen until all existing windows
4284 // have been drawn.
4285 final int N = mWindows.size();
4286 for (int i=0; i<N; i++) {
4287 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004288 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 return;
4290 }
4291 }
Romain Guy06882f82009-06-10 13:36:04 -07004292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 mDisplayEnabled = true;
4294 if (false) {
4295 Log.i(TAG, "ENABLING SCREEN!");
4296 StringWriter sw = new StringWriter();
4297 PrintWriter pw = new PrintWriter(sw);
4298 this.dump(null, pw, null);
4299 Log.i(TAG, sw.toString());
4300 }
4301 try {
4302 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4303 if (surfaceFlinger != null) {
4304 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4305 Parcel data = Parcel.obtain();
4306 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4307 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4308 data, null, 0);
4309 data.recycle();
4310 }
4311 } catch (RemoteException ex) {
4312 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4313 }
4314 }
Romain Guy06882f82009-06-10 13:36:04 -07004315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004319 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4320 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 public void setInTouchMode(boolean mode) {
4324 synchronized(mWindowMap) {
4325 mInTouchMode = mode;
4326 }
4327 }
4328
Romain Guy06882f82009-06-10 13:36:04 -07004329 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004330 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004332 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004333 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 }
4335
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004336 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 }
Romain Guy06882f82009-06-10 13:36:04 -07004338
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004339 public void setRotationUnchecked(int rotation,
4340 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 if(DEBUG_ORIENTATION) Log.v(TAG,
4342 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 long origId = Binder.clearCallingIdentity();
4345 boolean changed;
4346 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004347 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 }
Romain Guy06882f82009-06-10 13:36:04 -07004349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 if (changed) {
4351 sendNewConfiguration();
4352 synchronized(mWindowMap) {
4353 mLayoutNeeded = true;
4354 performLayoutAndPlaceSurfacesLocked();
4355 }
4356 } else if (alwaysSendConfiguration) {
4357 //update configuration ignoring orientation change
4358 sendNewConfiguration();
4359 }
Romain Guy06882f82009-06-10 13:36:04 -07004360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 Binder.restoreCallingIdentity(origId);
4362 }
Romain Guy06882f82009-06-10 13:36:04 -07004363
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004364 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 boolean changed;
4366 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4367 rotation = mRequestedRotation;
4368 } else {
4369 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004370 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 }
4372 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004373 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 mRotation, mDisplayEnabled);
4375 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4376 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004379 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 "Rotation changed to " + rotation
4381 + " from " + mRotation
4382 + " (forceApp=" + mForcedAppOrientation
4383 + ", req=" + mRequestedRotation + ")");
4384 mRotation = rotation;
4385 mWindowsFreezingScreen = true;
4386 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4387 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4388 2000);
4389 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004390 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 mQueue.setOrientation(rotation);
4392 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004393 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 }
4395 for (int i=mWindows.size()-1; i>=0; i--) {
4396 WindowState w = (WindowState)mWindows.get(i);
4397 if (w.mSurface != null) {
4398 w.mOrientationChanging = true;
4399 }
4400 }
4401 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4402 try {
4403 mRotationWatchers.get(i).onRotationChanged(rotation);
4404 } catch (RemoteException e) {
4405 }
4406 }
4407 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 return changed;
4410 }
Romain Guy06882f82009-06-10 13:36:04 -07004411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 public int getRotation() {
4413 return mRotation;
4414 }
4415
4416 public int watchRotation(IRotationWatcher watcher) {
4417 final IBinder watcherBinder = watcher.asBinder();
4418 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4419 public void binderDied() {
4420 synchronized (mWindowMap) {
4421 for (int i=0; i<mRotationWatchers.size(); i++) {
4422 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004423 IRotationWatcher removed = mRotationWatchers.remove(i);
4424 if (removed != null) {
4425 removed.asBinder().unlinkToDeath(this, 0);
4426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 i--;
4428 }
4429 }
4430 }
4431 }
4432 };
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 synchronized (mWindowMap) {
4435 try {
4436 watcher.asBinder().linkToDeath(dr, 0);
4437 mRotationWatchers.add(watcher);
4438 } catch (RemoteException e) {
4439 // Client died, no cleanup needed.
4440 }
Romain Guy06882f82009-06-10 13:36:04 -07004441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 return mRotation;
4443 }
4444 }
4445
4446 /**
4447 * Starts the view server on the specified port.
4448 *
4449 * @param port The port to listener to.
4450 *
4451 * @return True if the server was successfully started, false otherwise.
4452 *
4453 * @see com.android.server.ViewServer
4454 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4455 */
4456 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004457 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 return false;
4459 }
4460
4461 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4462 return false;
4463 }
4464
4465 if (port < 1024) {
4466 return false;
4467 }
4468
4469 if (mViewServer != null) {
4470 if (!mViewServer.isRunning()) {
4471 try {
4472 return mViewServer.start();
4473 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004474 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 }
4476 }
4477 return false;
4478 }
4479
4480 try {
4481 mViewServer = new ViewServer(this, port);
4482 return mViewServer.start();
4483 } catch (IOException e) {
4484 Log.w(TAG, "View server did not start");
4485 }
4486 return false;
4487 }
4488
Romain Guy06882f82009-06-10 13:36:04 -07004489 private boolean isSystemSecure() {
4490 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4491 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4492 }
4493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 /**
4495 * Stops the view server if it exists.
4496 *
4497 * @return True if the server stopped, false if it wasn't started or
4498 * couldn't be stopped.
4499 *
4500 * @see com.android.server.ViewServer
4501 */
4502 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004503 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 return false;
4505 }
4506
4507 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4508 return false;
4509 }
4510
4511 if (mViewServer != null) {
4512 return mViewServer.stop();
4513 }
4514 return false;
4515 }
4516
4517 /**
4518 * Indicates whether the view server is running.
4519 *
4520 * @return True if the server is running, false otherwise.
4521 *
4522 * @see com.android.server.ViewServer
4523 */
4524 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004525 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 return false;
4527 }
4528
4529 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4530 return false;
4531 }
4532
4533 return mViewServer != null && mViewServer.isRunning();
4534 }
4535
4536 /**
4537 * Lists all availble windows in the system. The listing is written in the
4538 * specified Socket's output stream with the following syntax:
4539 * windowHashCodeInHexadecimal windowName
4540 * Each line of the ouput represents a different window.
4541 *
4542 * @param client The remote client to send the listing to.
4543 * @return False if an error occured, true otherwise.
4544 */
4545 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004546 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 return false;
4548 }
4549
4550 boolean result = true;
4551
4552 Object[] windows;
4553 synchronized (mWindowMap) {
4554 windows = new Object[mWindows.size()];
4555 //noinspection unchecked
4556 windows = mWindows.toArray(windows);
4557 }
4558
4559 BufferedWriter out = null;
4560
4561 // Any uncaught exception will crash the system process
4562 try {
4563 OutputStream clientStream = client.getOutputStream();
4564 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4565
4566 final int count = windows.length;
4567 for (int i = 0; i < count; i++) {
4568 final WindowState w = (WindowState) windows[i];
4569 out.write(Integer.toHexString(System.identityHashCode(w)));
4570 out.write(' ');
4571 out.append(w.mAttrs.getTitle());
4572 out.write('\n');
4573 }
4574
4575 out.write("DONE.\n");
4576 out.flush();
4577 } catch (Exception e) {
4578 result = false;
4579 } finally {
4580 if (out != null) {
4581 try {
4582 out.close();
4583 } catch (IOException e) {
4584 result = false;
4585 }
4586 }
4587 }
4588
4589 return result;
4590 }
4591
4592 /**
4593 * Sends a command to a target window. The result of the command, if any, will be
4594 * written in the output stream of the specified socket.
4595 *
4596 * The parameters must follow this syntax:
4597 * windowHashcode extra
4598 *
4599 * Where XX is the length in characeters of the windowTitle.
4600 *
4601 * The first parameter is the target window. The window with the specified hashcode
4602 * will be the target. If no target can be found, nothing happens. The extra parameters
4603 * will be delivered to the target window and as parameters to the command itself.
4604 *
4605 * @param client The remote client to sent the result, if any, to.
4606 * @param command The command to execute.
4607 * @param parameters The command parameters.
4608 *
4609 * @return True if the command was successfully delivered, false otherwise. This does
4610 * not indicate whether the command itself was successful.
4611 */
4612 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004613 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 return false;
4615 }
4616
4617 boolean success = true;
4618 Parcel data = null;
4619 Parcel reply = null;
4620
4621 // Any uncaught exception will crash the system process
4622 try {
4623 // Find the hashcode of the window
4624 int index = parameters.indexOf(' ');
4625 if (index == -1) {
4626 index = parameters.length();
4627 }
4628 final String code = parameters.substring(0, index);
4629 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4630
4631 // Extract the command's parameter after the window description
4632 if (index < parameters.length()) {
4633 parameters = parameters.substring(index + 1);
4634 } else {
4635 parameters = "";
4636 }
4637
4638 final WindowManagerService.WindowState window = findWindow(hashCode);
4639 if (window == null) {
4640 return false;
4641 }
4642
4643 data = Parcel.obtain();
4644 data.writeInterfaceToken("android.view.IWindow");
4645 data.writeString(command);
4646 data.writeString(parameters);
4647 data.writeInt(1);
4648 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4649
4650 reply = Parcel.obtain();
4651
4652 final IBinder binder = window.mClient.asBinder();
4653 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4654 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4655
4656 reply.readException();
4657
4658 } catch (Exception e) {
4659 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4660 success = false;
4661 } finally {
4662 if (data != null) {
4663 data.recycle();
4664 }
4665 if (reply != null) {
4666 reply.recycle();
4667 }
4668 }
4669
4670 return success;
4671 }
4672
4673 private WindowState findWindow(int hashCode) {
4674 if (hashCode == -1) {
4675 return getFocusedWindow();
4676 }
4677
4678 synchronized (mWindowMap) {
4679 final ArrayList windows = mWindows;
4680 final int count = windows.size();
4681
4682 for (int i = 0; i < count; i++) {
4683 WindowState w = (WindowState) windows.get(i);
4684 if (System.identityHashCode(w) == hashCode) {
4685 return w;
4686 }
4687 }
4688 }
4689
4690 return null;
4691 }
4692
4693 /*
4694 * Instruct the Activity Manager to fetch the current configuration and broadcast
4695 * that to config-changed listeners if appropriate.
4696 */
4697 void sendNewConfiguration() {
4698 try {
4699 mActivityManager.updateConfiguration(null);
4700 } catch (RemoteException e) {
4701 }
4702 }
Romain Guy06882f82009-06-10 13:36:04 -07004703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 public Configuration computeNewConfiguration() {
4705 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004706 return computeNewConfigurationLocked();
4707 }
4708 }
Romain Guy06882f82009-06-10 13:36:04 -07004709
Dianne Hackbornc485a602009-03-24 22:39:49 -07004710 Configuration computeNewConfigurationLocked() {
4711 Configuration config = new Configuration();
4712 if (!computeNewConfigurationLocked(config)) {
4713 return null;
4714 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004715 return config;
4716 }
Romain Guy06882f82009-06-10 13:36:04 -07004717
Dianne Hackbornc485a602009-03-24 22:39:49 -07004718 boolean computeNewConfigurationLocked(Configuration config) {
4719 if (mDisplay == null) {
4720 return false;
4721 }
4722 mQueue.getInputConfiguration(config);
4723 final int dw = mDisplay.getWidth();
4724 final int dh = mDisplay.getHeight();
4725 int orientation = Configuration.ORIENTATION_SQUARE;
4726 if (dw < dh) {
4727 orientation = Configuration.ORIENTATION_PORTRAIT;
4728 } else if (dw > dh) {
4729 orientation = Configuration.ORIENTATION_LANDSCAPE;
4730 }
4731 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004732
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004733 DisplayMetrics dm = new DisplayMetrics();
4734 mDisplay.getMetrics(dm);
4735 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4736
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004737 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004738 // Note we only do this once because at this point we don't
4739 // expect the screen to change in this way at runtime, and want
4740 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004741 int longSize = dw;
4742 int shortSize = dh;
4743 if (longSize < shortSize) {
4744 int tmp = longSize;
4745 longSize = shortSize;
4746 shortSize = tmp;
4747 }
4748 longSize = (int)(longSize/dm.density);
4749 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004750
Dianne Hackborn723738c2009-06-25 19:48:04 -07004751 // These semi-magic numbers define our compatibility modes for
4752 // applications with different screens. Don't change unless you
4753 // make sure to test lots and lots of apps!
4754 if (longSize < 470) {
4755 // This is shorter than an HVGA normal density screen (which
4756 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004757 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4758 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004759 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004760 // Is this a large screen?
4761 if (longSize > 640 && shortSize >= 480) {
4762 // VGA or larger screens at medium density are the point
4763 // at which we consider it to be a large screen.
4764 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4765 } else {
4766 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4767
4768 // If this screen is wider than normal HVGA, or taller
4769 // than FWVGA, then for old apps we want to run in size
4770 // compatibility mode.
4771 if (shortSize > 321 || longSize > 570) {
4772 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4773 }
4774 }
4775
4776 // Is this a long screen?
4777 if (((longSize*3)/5) >= (shortSize-1)) {
4778 // Anything wider than WVGA (5:3) is considering to be long.
4779 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4780 } else {
4781 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4782 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004783 }
4784 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004785 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004786
Dianne Hackbornc485a602009-03-24 22:39:49 -07004787 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4788 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4789 mPolicy.adjustConfigurationLw(config);
4790 return true;
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 // -------------------------------------------------------------
4794 // Input Events and Focus Management
4795 // -------------------------------------------------------------
4796
4797 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004798 long curTime = SystemClock.uptimeMillis();
4799
Michael Chane10de972009-05-18 11:24:50 -07004800 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004801 if (mLastTouchEventType == eventType &&
4802 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4803 return;
4804 }
4805 mLastUserActivityCallTime = curTime;
4806 mLastTouchEventType = eventType;
4807 }
4808
4809 if (targetWin == null
4810 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4811 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813 }
4814
4815 // tells if it's a cheek event or not -- this function is stateful
4816 private static final int EVENT_NONE = 0;
4817 private static final int EVENT_UNKNOWN = 0;
4818 private static final int EVENT_CHEEK = 0;
4819 private static final int EVENT_IGNORE_DURATION = 300; // ms
4820 private static final float CHEEK_THRESHOLD = 0.6f;
4821 private int mEventState = EVENT_NONE;
4822 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 private int eventType(MotionEvent ev) {
4825 float size = ev.getSize();
4826 switch (ev.getAction()) {
4827 case MotionEvent.ACTION_DOWN:
4828 mEventSize = size;
4829 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4830 case MotionEvent.ACTION_UP:
4831 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004832 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 case MotionEvent.ACTION_MOVE:
4834 final int N = ev.getHistorySize();
4835 if (size > mEventSize) mEventSize = size;
4836 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4837 for (int i=0; i<N; i++) {
4838 size = ev.getHistoricalSize(i);
4839 if (size > mEventSize) mEventSize = size;
4840 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4841 }
4842 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4843 return TOUCH_EVENT;
4844 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004845 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 }
4847 default:
4848 // not good
4849 return OTHER_EVENT;
4850 }
4851 }
4852
4853 /**
4854 * @return Returns true if event was dispatched, false if it was dropped for any reason
4855 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004856 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4858 "dispatchPointer " + ev);
4859
Michael Chan53071d62009-05-13 17:29:48 -07004860 if (MEASURE_LATENCY) {
4861 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4862 }
4863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004865 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866
Michael Chan53071d62009-05-13 17:29:48 -07004867 if (MEASURE_LATENCY) {
4868 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4869 }
4870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 if (action == MotionEvent.ACTION_UP) {
4874 // let go of our target
4875 mKeyWaiter.mMotionTarget = null;
4876 mPowerManager.logPointerUpEvent();
4877 } else if (action == MotionEvent.ACTION_DOWN) {
4878 mPowerManager.logPointerDownEvent();
4879 }
4880
4881 if (targetObj == null) {
4882 // In this case we are either dropping the event, or have received
4883 // a move or up without a down. It is common to receive move
4884 // events in such a way, since this means the user is moving the
4885 // pointer without actually pressing down. All other cases should
4886 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004887 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4889 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004890 synchronized (mWindowMap) {
4891 if (mSendingPointersToWallpaper) {
4892 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4893 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4894 }
4895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 if (qev != null) {
4897 mQueue.recycleEvent(qev);
4898 }
4899 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004900 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 }
4902 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004903 synchronized (mWindowMap) {
4904 if (mSendingPointersToWallpaper) {
4905 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4906 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4907 }
4908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 if (qev != null) {
4910 mQueue.recycleEvent(qev);
4911 }
4912 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004913 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 }
Romain Guy06882f82009-06-10 13:36:04 -07004915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004919 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920
4921 //Log.i(TAG, "Sending " + ev + " to " + target);
4922
4923 if (uid != 0 && uid != target.mSession.mUid) {
4924 if (mContext.checkPermission(
4925 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4926 != PackageManager.PERMISSION_GRANTED) {
4927 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4928 + pid + " uid " + uid + " to window " + target
4929 + " owned by uid " + target.mSession.mUid);
4930 if (qev != null) {
4931 mQueue.recycleEvent(qev);
4932 }
4933 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004934 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 }
4936 }
4937
Michael Chan53071d62009-05-13 17:29:48 -07004938 if (MEASURE_LATENCY) {
4939 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4940 }
4941
Romain Guy06882f82009-06-10 13:36:04 -07004942 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4944 //target wants to ignore fat touch events
4945 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4946 //explicit flag to return without processing event further
4947 boolean returnFlag = false;
4948 if((action == MotionEvent.ACTION_DOWN)) {
4949 mFatTouch = false;
4950 if(cheekPress) {
4951 mFatTouch = true;
4952 returnFlag = true;
4953 }
4954 } else {
4955 if(action == MotionEvent.ACTION_UP) {
4956 if(mFatTouch) {
4957 //earlier even was invalid doesnt matter if current up is cheekpress or not
4958 mFatTouch = false;
4959 returnFlag = true;
4960 } else if(cheekPress) {
4961 //cancel the earlier event
4962 ev.setAction(MotionEvent.ACTION_CANCEL);
4963 action = MotionEvent.ACTION_CANCEL;
4964 }
4965 } else if(action == MotionEvent.ACTION_MOVE) {
4966 if(mFatTouch) {
4967 //two cases here
4968 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004969 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 returnFlag = true;
4971 } else if(cheekPress) {
4972 //valid down followed by invalid moves
4973 //an invalid move have to cancel earlier action
4974 ev.setAction(MotionEvent.ACTION_CANCEL);
4975 action = MotionEvent.ACTION_CANCEL;
4976 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4977 //note that the subsequent invalid moves will not get here
4978 mFatTouch = true;
4979 }
4980 }
4981 } //else if action
4982 if(returnFlag) {
4983 //recycle que, ev
4984 if (qev != null) {
4985 mQueue.recycleEvent(qev);
4986 }
4987 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004988 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 }
4990 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004991
Michael Chan9f028e62009-08-04 17:37:46 -07004992 // Enable this for testing the "right" value
4993 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004994 int max_events_per_sec = 35;
4995 try {
4996 max_events_per_sec = Integer.parseInt(SystemProperties
4997 .get("windowsmgr.max_events_per_sec"));
4998 if (max_events_per_sec < 1) {
4999 max_events_per_sec = 35;
5000 }
5001 } catch (NumberFormatException e) {
5002 }
5003 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5004 }
5005
5006 /*
5007 * Throttle events to minimize CPU usage when there's a flood of events
5008 * e.g. constant contact with the screen
5009 */
5010 if (action == MotionEvent.ACTION_MOVE) {
5011 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5012 long now = SystemClock.uptimeMillis();
5013 if (now < nextEventTime) {
5014 try {
5015 Thread.sleep(nextEventTime - now);
5016 } catch (InterruptedException e) {
5017 }
5018 mLastTouchEventTime = nextEventTime;
5019 } else {
5020 mLastTouchEventTime = now;
5021 }
5022 }
5023
Michael Chan53071d62009-05-13 17:29:48 -07005024 if (MEASURE_LATENCY) {
5025 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5026 }
5027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005029 if (!target.isVisibleLw()) {
5030 // During this motion dispatch, the target window has become
5031 // invisible.
5032 if (mSendingPointersToWallpaper) {
5033 sendPointerToWallpaperLocked(null, ev, eventTime);
5034 }
5035 if (qev != null) {
5036 mQueue.recycleEvent(qev);
5037 }
5038 ev.recycle();
5039 return INJECT_SUCCEEDED;
5040 }
5041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5043 mKeyWaiter.bindTargetWindowLocked(target,
5044 KeyWaiter.RETURN_PENDING_POINTER, qev);
5045 ev = null;
5046 } else {
5047 if (action == MotionEvent.ACTION_DOWN) {
5048 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5049 if (out != null) {
5050 MotionEvent oev = MotionEvent.obtain(ev);
5051 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5052 do {
5053 final Rect frame = out.mFrame;
5054 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5055 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005056 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 } catch (android.os.RemoteException e) {
5058 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5059 }
5060 oev.offsetLocation((float)frame.left, (float)frame.top);
5061 out = out.mNextOutsideTouch;
5062 } while (out != null);
5063 mKeyWaiter.mOutsideTouchTargets = null;
5064 }
5065 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005066
5067 // If we are on top of the wallpaper, then the wallpaper also
5068 // gets to see this movement.
Dianne Hackborn6adba242009-11-10 11:10:09 -08005069 if (mWallpaperTarget == target || mSendingPointersToWallpaper) {
5070 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005071 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005072
5073 final Rect frame = target.mFrame;
5074 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5075 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 }
5077 }
Romain Guy06882f82009-06-10 13:36:04 -07005078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 // finally offset the event to the target's coordinate system and
5080 // dispatch the event.
5081 try {
5082 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5083 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5084 }
Michael Chan53071d62009-05-13 17:29:48 -07005085
5086 if (MEASURE_LATENCY) {
5087 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5088 }
5089
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005090 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005091
5092 if (MEASURE_LATENCY) {
5093 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5094 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005095 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 } catch (android.os.RemoteException e) {
5097 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5098 mKeyWaiter.mMotionTarget = null;
5099 try {
5100 removeWindow(target.mSession, target.mClient);
5101 } catch (java.util.NoSuchElementException ex) {
5102 // This will happen if the window has already been
5103 // removed.
5104 }
5105 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005106 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 }
Romain Guy06882f82009-06-10 13:36:04 -07005108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 /**
5110 * @return Returns true if event was dispatched, false if it was dropped for any reason
5111 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005112 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 if (DEBUG_INPUT) Log.v(
5114 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005117 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 if (focusObj == null) {
5119 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5120 if (qev != null) {
5121 mQueue.recycleEvent(qev);
5122 }
5123 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005124 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 }
5126 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5127 if (qev != null) {
5128 mQueue.recycleEvent(qev);
5129 }
5130 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005131 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 }
Romain Guy06882f82009-06-10 13:36:04 -07005133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 if (uid != 0 && uid != focus.mSession.mUid) {
5137 if (mContext.checkPermission(
5138 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5139 != PackageManager.PERMISSION_GRANTED) {
5140 Log.w(TAG, "Permission denied: injecting key event from pid "
5141 + pid + " uid " + uid + " to window " + focus
5142 + " owned by uid " + focus.mSession.mUid);
5143 if (qev != null) {
5144 mQueue.recycleEvent(qev);
5145 }
5146 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005147 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 }
5149 }
Romain Guy06882f82009-06-10 13:36:04 -07005150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 synchronized(mWindowMap) {
5154 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5155 mKeyWaiter.bindTargetWindowLocked(focus,
5156 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5157 // We don't deliver movement events to the client, we hold
5158 // them and wait for them to call back.
5159 ev = null;
5160 } else {
5161 mKeyWaiter.bindTargetWindowLocked(focus);
5162 }
5163 }
Romain Guy06882f82009-06-10 13:36:04 -07005164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005166 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005167 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 } catch (android.os.RemoteException e) {
5169 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5170 try {
5171 removeWindow(focus.mSession, focus.mClient);
5172 } catch (java.util.NoSuchElementException ex) {
5173 // This will happen if the window has already been
5174 // removed.
5175 }
5176 }
Romain Guy06882f82009-06-10 13:36:04 -07005177
Dianne Hackborncfaef692009-06-15 14:24:44 -07005178 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 }
Romain Guy06882f82009-06-10 13:36:04 -07005180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 /**
5182 * @return Returns true if event was dispatched, false if it was dropped for any reason
5183 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005184 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5186
5187 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005188 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 if (focusObj == null) {
5190 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005191 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 }
5193 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005194 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 }
Romain Guy06882f82009-06-10 13:36:04 -07005196
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005197 // Okay we have finished waiting for the last event to be processed.
5198 // First off, if this is a repeat event, check to see if there is
5199 // a corresponding up event in the queue. If there is, we will
5200 // just drop the repeat, because it makes no sense to repeat after
5201 // the user has released a key. (This is especially important for
5202 // long presses.)
5203 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5204 return INJECT_SUCCEEDED;
5205 }
5206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 if (DEBUG_INPUT) Log.v(
5210 TAG, "Dispatching to " + focus + ": " + event);
5211
5212 if (uid != 0 && uid != focus.mSession.mUid) {
5213 if (mContext.checkPermission(
5214 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5215 != PackageManager.PERMISSION_GRANTED) {
5216 Log.w(TAG, "Permission denied: injecting key event from pid "
5217 + pid + " uid " + uid + " to window " + focus
5218 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005219 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 }
5221 }
Romain Guy06882f82009-06-10 13:36:04 -07005222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 synchronized(mWindowMap) {
5224 mKeyWaiter.bindTargetWindowLocked(focus);
5225 }
5226
5227 // NOSHIP extra state logging
5228 mKeyWaiter.recordDispatchState(event, focus);
5229 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 try {
5232 if (DEBUG_INPUT || DEBUG_FOCUS) {
5233 Log.v(TAG, "Delivering key " + event.getKeyCode()
5234 + " to " + focus);
5235 }
5236 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005237 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 } catch (android.os.RemoteException e) {
5239 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5240 try {
5241 removeWindow(focus.mSession, focus.mClient);
5242 } catch (java.util.NoSuchElementException ex) {
5243 // This will happen if the window has already been
5244 // removed.
5245 }
5246 }
Romain Guy06882f82009-06-10 13:36:04 -07005247
Dianne Hackborncfaef692009-06-15 14:24:44 -07005248 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 }
Romain Guy06882f82009-06-10 13:36:04 -07005250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 public void pauseKeyDispatching(IBinder _token) {
5252 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5253 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005254 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 }
5256
5257 synchronized (mWindowMap) {
5258 WindowToken token = mTokenMap.get(_token);
5259 if (token != null) {
5260 mKeyWaiter.pauseDispatchingLocked(token);
5261 }
5262 }
5263 }
5264
5265 public void resumeKeyDispatching(IBinder _token) {
5266 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5267 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005268 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 }
5270
5271 synchronized (mWindowMap) {
5272 WindowToken token = mTokenMap.get(_token);
5273 if (token != null) {
5274 mKeyWaiter.resumeDispatchingLocked(token);
5275 }
5276 }
5277 }
5278
5279 public void setEventDispatching(boolean enabled) {
5280 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5281 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005282 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 }
5284
5285 synchronized (mWindowMap) {
5286 mKeyWaiter.setEventDispatchingLocked(enabled);
5287 }
5288 }
Romain Guy06882f82009-06-10 13:36:04 -07005289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 /**
5291 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005292 *
5293 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 * {@link SystemClock#uptimeMillis()} as the timebase.)
5295 * @param sync If true, wait for the event to be completed before returning to the caller.
5296 * @return Returns true if event was dispatched, false if it was dropped for any reason
5297 */
5298 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5299 long downTime = ev.getDownTime();
5300 long eventTime = ev.getEventTime();
5301
5302 int action = ev.getAction();
5303 int code = ev.getKeyCode();
5304 int repeatCount = ev.getRepeatCount();
5305 int metaState = ev.getMetaState();
5306 int deviceId = ev.getDeviceId();
5307 int scancode = ev.getScanCode();
5308
5309 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5310 if (downTime == 0) downTime = eventTime;
5311
5312 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005313 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005315 final int pid = Binder.getCallingPid();
5316 final int uid = Binder.getCallingUid();
5317 final long ident = Binder.clearCallingIdentity();
5318 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005320 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005322 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005323 switch (result) {
5324 case INJECT_NO_PERMISSION:
5325 throw new SecurityException(
5326 "Injecting to another application requires INJECT_EVENT permission");
5327 case INJECT_SUCCEEDED:
5328 return true;
5329 }
5330 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 }
5332
5333 /**
5334 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005335 *
5336 * @param ev A motion event describing the pointer (touch) action. (As noted in
5337 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 * {@link SystemClock#uptimeMillis()} as the timebase.)
5339 * @param sync If true, wait for the event to be completed before returning to the caller.
5340 * @return Returns true if event was dispatched, false if it was dropped for any reason
5341 */
5342 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005343 final int pid = Binder.getCallingPid();
5344 final int uid = Binder.getCallingUid();
5345 final long ident = Binder.clearCallingIdentity();
5346 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005348 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005350 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005351 switch (result) {
5352 case INJECT_NO_PERMISSION:
5353 throw new SecurityException(
5354 "Injecting to another application requires INJECT_EVENT permission");
5355 case INJECT_SUCCEEDED:
5356 return true;
5357 }
5358 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 }
Romain Guy06882f82009-06-10 13:36:04 -07005360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 /**
5362 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005363 *
5364 * @param ev A motion event describing the trackball action. (As noted in
5365 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 * {@link SystemClock#uptimeMillis()} as the timebase.)
5367 * @param sync If true, wait for the event to be completed before returning to the caller.
5368 * @return Returns true if event was dispatched, false if it was dropped for any reason
5369 */
5370 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005371 final int pid = Binder.getCallingPid();
5372 final int uid = Binder.getCallingUid();
5373 final long ident = Binder.clearCallingIdentity();
5374 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005376 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005378 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005379 switch (result) {
5380 case INJECT_NO_PERMISSION:
5381 throw new SecurityException(
5382 "Injecting to another application requires INJECT_EVENT permission");
5383 case INJECT_SUCCEEDED:
5384 return true;
5385 }
5386 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 }
Romain Guy06882f82009-06-10 13:36:04 -07005388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 private WindowState getFocusedWindow() {
5390 synchronized (mWindowMap) {
5391 return getFocusedWindowLocked();
5392 }
5393 }
5394
5395 private WindowState getFocusedWindowLocked() {
5396 return mCurrentFocus;
5397 }
Romain Guy06882f82009-06-10 13:36:04 -07005398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 /**
5400 * This class holds the state for dispatching key events. This state
5401 * is protected by the KeyWaiter instance, NOT by the window lock. You
5402 * can be holding the main window lock while acquire the KeyWaiter lock,
5403 * but not the other way around.
5404 */
5405 final class KeyWaiter {
5406 // NOSHIP debugging
5407 public class DispatchState {
5408 private KeyEvent event;
5409 private WindowState focus;
5410 private long time;
5411 private WindowState lastWin;
5412 private IBinder lastBinder;
5413 private boolean finished;
5414 private boolean gotFirstWindow;
5415 private boolean eventDispatching;
5416 private long timeToSwitch;
5417 private boolean wasFrozen;
5418 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005419 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5422 focus = theFocus;
5423 event = theEvent;
5424 time = System.currentTimeMillis();
5425 // snapshot KeyWaiter state
5426 lastWin = mLastWin;
5427 lastBinder = mLastBinder;
5428 finished = mFinished;
5429 gotFirstWindow = mGotFirstWindow;
5430 eventDispatching = mEventDispatching;
5431 timeToSwitch = mTimeToSwitch;
5432 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005433 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 // cache the paused state at ctor time as well
5435 if (theFocus == null || theFocus.mToken == null) {
5436 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5437 focusPaused = false;
5438 } else {
5439 focusPaused = theFocus.mToken.paused;
5440 }
5441 }
Romain Guy06882f82009-06-10 13:36:04 -07005442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 public String toString() {
5444 return "{{" + event + " to " + focus + " @ " + time
5445 + " lw=" + lastWin + " lb=" + lastBinder
5446 + " fin=" + finished + " gfw=" + gotFirstWindow
5447 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005448 + " wf=" + wasFrozen + " fp=" + focusPaused
5449 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 }
5451 };
5452 private DispatchState mDispatchState = null;
5453 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5454 mDispatchState = new DispatchState(theEvent, theFocus);
5455 }
5456 // END NOSHIP
5457
5458 public static final int RETURN_NOTHING = 0;
5459 public static final int RETURN_PENDING_POINTER = 1;
5460 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 final Object SKIP_TARGET_TOKEN = new Object();
5463 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 private WindowState mLastWin = null;
5466 private IBinder mLastBinder = null;
5467 private boolean mFinished = true;
5468 private boolean mGotFirstWindow = false;
5469 private boolean mEventDispatching = true;
5470 private long mTimeToSwitch = 0;
5471 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 // Target of Motion events
5474 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 // Windows above the target who would like to receive an "outside"
5477 // touch event for any down events outside of them.
5478 WindowState mOutsideTouchTargets;
5479
5480 /**
5481 * Wait for the last event dispatch to complete, then find the next
5482 * target that should receive the given event and wait for that one
5483 * to be ready to receive it.
5484 */
5485 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5486 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005487 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 long startTime = SystemClock.uptimeMillis();
5489 long keyDispatchingTimeout = 5 * 1000;
5490 long waitedFor = 0;
5491
5492 while (true) {
5493 // Figure out which window we care about. It is either the
5494 // last window we are waiting to have process the event or,
5495 // if none, then the next window we think the event should go
5496 // to. Note: we retrieve mLastWin outside of the lock, so
5497 // it may change before we lock. Thus we must check it again.
5498 WindowState targetWin = mLastWin;
5499 boolean targetIsNew = targetWin == null;
5500 if (DEBUG_INPUT) Log.v(
5501 TAG, "waitForLastKey: mFinished=" + mFinished +
5502 ", mLastWin=" + mLastWin);
5503 if (targetIsNew) {
5504 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005505 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 if (target == SKIP_TARGET_TOKEN) {
5507 // The user has pressed a special key, and we are
5508 // dropping all pending events before it.
5509 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5510 + " " + nextMotion);
5511 return null;
5512 }
5513 if (target == CONSUMED_EVENT_TOKEN) {
5514 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5515 + " " + nextMotion);
5516 return target;
5517 }
5518 targetWin = (WindowState)target;
5519 }
Romain Guy06882f82009-06-10 13:36:04 -07005520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 // Now: is it okay to send the next event to this window?
5524 synchronized (this) {
5525 // First: did we come here based on the last window not
5526 // being null, but it changed by the time we got here?
5527 // If so, try again.
5528 if (!targetIsNew && mLastWin == null) {
5529 continue;
5530 }
Romain Guy06882f82009-06-10 13:36:04 -07005531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 // We never dispatch events if not finished with the
5533 // last one, or the display is frozen.
5534 if (mFinished && !mDisplayFrozen) {
5535 // If event dispatching is disabled, then we
5536 // just consume the events.
5537 if (!mEventDispatching) {
5538 if (DEBUG_INPUT) Log.v(TAG,
5539 "Skipping event; dispatching disabled: "
5540 + nextKey + " " + nextMotion);
5541 return null;
5542 }
5543 if (targetWin != null) {
5544 // If this is a new target, and that target is not
5545 // paused or unresponsive, then all looks good to
5546 // handle the event.
5547 if (targetIsNew && !targetWin.mToken.paused) {
5548 return targetWin;
5549 }
Romain Guy06882f82009-06-10 13:36:04 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 // If we didn't find a target window, and there is no
5552 // focused app window, then just eat the events.
5553 } else if (mFocusedApp == null) {
5554 if (DEBUG_INPUT) Log.v(TAG,
5555 "Skipping event; no focused app: "
5556 + nextKey + " " + nextMotion);
5557 return null;
5558 }
5559 }
Romain Guy06882f82009-06-10 13:36:04 -07005560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 if (DEBUG_INPUT) Log.v(
5562 TAG, "Waiting for last key in " + mLastBinder
5563 + " target=" + targetWin
5564 + " mFinished=" + mFinished
5565 + " mDisplayFrozen=" + mDisplayFrozen
5566 + " targetIsNew=" + targetIsNew
5567 + " paused="
5568 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005569 + " mFocusedApp=" + mFocusedApp
5570 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 targetApp = targetWin != null
5573 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 long curTimeout = keyDispatchingTimeout;
5576 if (mTimeToSwitch != 0) {
5577 long now = SystemClock.uptimeMillis();
5578 if (mTimeToSwitch <= now) {
5579 // If an app switch key has been pressed, and we have
5580 // waited too long for the current app to finish
5581 // processing keys, then wait no more!
5582 doFinishedKeyLocked(true);
5583 continue;
5584 }
5585 long switchTimeout = mTimeToSwitch - now;
5586 if (curTimeout > switchTimeout) {
5587 curTimeout = switchTimeout;
5588 }
5589 }
Romain Guy06882f82009-06-10 13:36:04 -07005590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 try {
5592 // after that continue
5593 // processing keys, so we don't get stuck.
5594 if (DEBUG_INPUT) Log.v(
5595 TAG, "Waiting for key dispatch: " + curTimeout);
5596 wait(curTimeout);
5597 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5598 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005599 + startTime + " switchTime=" + mTimeToSwitch
5600 + " target=" + targetWin + " mLW=" + mLastWin
5601 + " mLB=" + mLastBinder + " fin=" + mFinished
5602 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 } catch (InterruptedException e) {
5604 }
5605 }
5606
5607 // If we were frozen during configuration change, restart the
5608 // timeout checks from now; otherwise look at whether we timed
5609 // out before awakening.
5610 if (mWasFrozen) {
5611 waitedFor = 0;
5612 mWasFrozen = false;
5613 } else {
5614 waitedFor = SystemClock.uptimeMillis() - startTime;
5615 }
5616
5617 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5618 IApplicationToken at = null;
5619 synchronized (this) {
5620 Log.w(TAG, "Key dispatching timed out sending to " +
5621 (targetWin != null ? targetWin.mAttrs.getTitle()
5622 : "<null>"));
5623 // NOSHIP debugging
5624 Log.w(TAG, "Dispatch state: " + mDispatchState);
5625 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5626 // END NOSHIP
5627 //dump();
5628 if (targetWin != null) {
5629 at = targetWin.getAppToken();
5630 } else if (targetApp != null) {
5631 at = targetApp.appToken;
5632 }
5633 }
5634
5635 boolean abort = true;
5636 if (at != null) {
5637 try {
5638 long timeout = at.getKeyDispatchingTimeout();
5639 if (timeout > waitedFor) {
5640 // we did not wait the proper amount of time for this application.
5641 // set the timeout to be the real timeout and wait again.
5642 keyDispatchingTimeout = timeout - waitedFor;
5643 continue;
5644 } else {
5645 abort = at.keyDispatchingTimedOut();
5646 }
5647 } catch (RemoteException ex) {
5648 }
5649 }
5650
5651 synchronized (this) {
5652 if (abort && (mLastWin == targetWin || targetWin == null)) {
5653 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005654 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 if (DEBUG_INPUT) Log.v(TAG,
5656 "Window " + mLastWin +
5657 " timed out on key input");
5658 if (mLastWin.mToken.paused) {
5659 Log.w(TAG, "Un-pausing dispatching to this window");
5660 mLastWin.mToken.paused = false;
5661 }
5662 }
5663 if (mMotionTarget == targetWin) {
5664 mMotionTarget = null;
5665 }
5666 mLastWin = null;
5667 mLastBinder = null;
5668 if (failIfTimeout || targetWin == null) {
5669 return null;
5670 }
5671 } else {
5672 Log.w(TAG, "Continuing to wait for key to be dispatched");
5673 startTime = SystemClock.uptimeMillis();
5674 }
5675 }
5676 }
5677 }
5678 }
Romain Guy06882f82009-06-10 13:36:04 -07005679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005681 MotionEvent nextMotion, boolean isPointerEvent,
5682 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 if (nextKey != null) {
5686 // Find the target window for a normal key event.
5687 final int keycode = nextKey.getKeyCode();
5688 final int repeatCount = nextKey.getRepeatCount();
5689 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5690 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005693 if (callingUid == 0 ||
5694 mContext.checkPermission(
5695 android.Manifest.permission.INJECT_EVENTS,
5696 callingPid, callingUid)
5697 == PackageManager.PERMISSION_GRANTED) {
5698 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005699 nextKey.getMetaState(), down, repeatCount,
5700 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 Log.w(TAG, "Event timeout during app switch: dropping "
5703 + nextKey);
5704 return SKIP_TARGET_TOKEN;
5705 }
Romain Guy06882f82009-06-10 13:36:04 -07005706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 WindowState focus = null;
5710 synchronized(mWindowMap) {
5711 focus = getFocusedWindowLocked();
5712 }
Romain Guy06882f82009-06-10 13:36:04 -07005713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005715
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005716 if (callingUid == 0 ||
5717 (focus != null && callingUid == focus.mSession.mUid) ||
5718 mContext.checkPermission(
5719 android.Manifest.permission.INJECT_EVENTS,
5720 callingPid, callingUid)
5721 == PackageManager.PERMISSION_GRANTED) {
5722 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005723 keycode, nextKey.getMetaState(), down, repeatCount,
5724 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005725 return CONSUMED_EVENT_TOKEN;
5726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 }
Romain Guy06882f82009-06-10 13:36:04 -07005728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 } else if (!isPointerEvent) {
5732 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5733 if (!dispatch) {
5734 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5735 + nextMotion);
5736 return SKIP_TARGET_TOKEN;
5737 }
Romain Guy06882f82009-06-10 13:36:04 -07005738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 WindowState focus = null;
5740 synchronized(mWindowMap) {
5741 focus = getFocusedWindowLocked();
5742 }
Romain Guy06882f82009-06-10 13:36:04 -07005743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5745 return focus;
5746 }
Romain Guy06882f82009-06-10 13:36:04 -07005747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 if (nextMotion == null) {
5749 return SKIP_TARGET_TOKEN;
5750 }
Romain Guy06882f82009-06-10 13:36:04 -07005751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5753 KeyEvent.KEYCODE_UNKNOWN);
5754 if (!dispatch) {
5755 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5756 + nextMotion);
5757 return SKIP_TARGET_TOKEN;
5758 }
Romain Guy06882f82009-06-10 13:36:04 -07005759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 // Find the target window for a pointer event.
5761 int action = nextMotion.getAction();
5762 final float xf = nextMotion.getX();
5763 final float yf = nextMotion.getY();
5764 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 final boolean screenWasOff = qev != null
5767 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005769 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 synchronized(mWindowMap) {
5772 synchronized (this) {
5773 if (action == MotionEvent.ACTION_DOWN) {
5774 if (mMotionTarget != null) {
5775 // this is weird, we got a pen down, but we thought it was
5776 // already down!
5777 // XXX: We should probably send an ACTION_UP to the current
5778 // target.
5779 Log.w(TAG, "Pointer down received while already down in: "
5780 + mMotionTarget);
5781 mMotionTarget = null;
5782 }
Romain Guy06882f82009-06-10 13:36:04 -07005783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 // ACTION_DOWN is special, because we need to lock next events to
5785 // the window we'll land onto.
5786 final int x = (int)xf;
5787 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 final ArrayList windows = mWindows;
5790 final int N = windows.size();
5791 WindowState topErrWindow = null;
5792 final Rect tmpRect = mTempRect;
5793 for (int i=N-1; i>=0; i--) {
5794 WindowState child = (WindowState)windows.get(i);
5795 //Log.i(TAG, "Checking dispatch to: " + child);
5796 final int flags = child.mAttrs.flags;
5797 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5798 if (topErrWindow == null) {
5799 topErrWindow = child;
5800 }
5801 }
5802 if (!child.isVisibleLw()) {
5803 //Log.i(TAG, "Not visible!");
5804 continue;
5805 }
5806 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5807 //Log.i(TAG, "Not touchable!");
5808 if ((flags & WindowManager.LayoutParams
5809 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5810 child.mNextOutsideTouch = mOutsideTouchTargets;
5811 mOutsideTouchTargets = child;
5812 }
5813 continue;
5814 }
5815 tmpRect.set(child.mFrame);
5816 if (child.mTouchableInsets == ViewTreeObserver
5817 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5818 // The touch is inside of the window if it is
5819 // inside the frame, AND the content part of that
5820 // frame that was given by the application.
5821 tmpRect.left += child.mGivenContentInsets.left;
5822 tmpRect.top += child.mGivenContentInsets.top;
5823 tmpRect.right -= child.mGivenContentInsets.right;
5824 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5825 } else if (child.mTouchableInsets == ViewTreeObserver
5826 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5827 // The touch is inside of the window if it is
5828 // inside the frame, AND the visible part of that
5829 // frame that was given by the application.
5830 tmpRect.left += child.mGivenVisibleInsets.left;
5831 tmpRect.top += child.mGivenVisibleInsets.top;
5832 tmpRect.right -= child.mGivenVisibleInsets.right;
5833 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5834 }
5835 final int touchFlags = flags &
5836 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5837 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5838 if (tmpRect.contains(x, y) || touchFlags == 0) {
5839 //Log.i(TAG, "Using this target!");
5840 if (!screenWasOff || (flags &
5841 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5842 mMotionTarget = child;
5843 } else {
5844 //Log.i(TAG, "Waking, skip!");
5845 mMotionTarget = null;
5846 }
5847 break;
5848 }
Romain Guy06882f82009-06-10 13:36:04 -07005849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 if ((flags & WindowManager.LayoutParams
5851 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5852 child.mNextOutsideTouch = mOutsideTouchTargets;
5853 mOutsideTouchTargets = child;
5854 //Log.i(TAG, "Adding to outside target list: " + child);
5855 }
5856 }
5857
5858 // if there's an error window but it's not accepting
5859 // focus (typically because it is not yet visible) just
5860 // wait for it -- any other focused window may in fact
5861 // be in ANR state.
5862 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5863 mMotionTarget = null;
5864 }
5865 }
Romain Guy06882f82009-06-10 13:36:04 -07005866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 target = mMotionTarget;
5868 }
5869 }
Romain Guy06882f82009-06-10 13:36:04 -07005870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 // Pointer events are a little different -- if there isn't a
5874 // target found for any event, then just drop it.
5875 return target != null ? target : SKIP_TARGET_TOKEN;
5876 }
Romain Guy06882f82009-06-10 13:36:04 -07005877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 boolean checkShouldDispatchKey(int keycode) {
5879 synchronized (this) {
5880 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5881 mTimeToSwitch = 0;
5882 return true;
5883 }
5884 if (mTimeToSwitch != 0
5885 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5886 return false;
5887 }
5888 return true;
5889 }
5890 }
Romain Guy06882f82009-06-10 13:36:04 -07005891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 void bindTargetWindowLocked(WindowState win,
5893 int pendingWhat, QueuedEvent pendingMotion) {
5894 synchronized (this) {
5895 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5896 }
5897 }
Romain Guy06882f82009-06-10 13:36:04 -07005898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 void bindTargetWindowLocked(WindowState win) {
5900 synchronized (this) {
5901 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5902 }
5903 }
5904
5905 void bindTargetWindowLockedLocked(WindowState win,
5906 int pendingWhat, QueuedEvent pendingMotion) {
5907 mLastWin = win;
5908 mLastBinder = win.mClient.asBinder();
5909 mFinished = false;
5910 if (pendingMotion != null) {
5911 final Session s = win.mSession;
5912 if (pendingWhat == RETURN_PENDING_POINTER) {
5913 releasePendingPointerLocked(s);
5914 s.mPendingPointerMove = pendingMotion;
5915 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005916 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 "bindTargetToWindow " + s.mPendingPointerMove);
5918 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5919 releasePendingTrackballLocked(s);
5920 s.mPendingTrackballMove = pendingMotion;
5921 s.mPendingTrackballWindow = win;
5922 }
5923 }
5924 }
Romain Guy06882f82009-06-10 13:36:04 -07005925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 void releasePendingPointerLocked(Session s) {
5927 if (DEBUG_INPUT) Log.v(TAG,
5928 "releasePendingPointer " + s.mPendingPointerMove);
5929 if (s.mPendingPointerMove != null) {
5930 mQueue.recycleEvent(s.mPendingPointerMove);
5931 s.mPendingPointerMove = null;
5932 }
5933 }
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 void releasePendingTrackballLocked(Session s) {
5936 if (s.mPendingTrackballMove != null) {
5937 mQueue.recycleEvent(s.mPendingTrackballMove);
5938 s.mPendingTrackballMove = null;
5939 }
5940 }
Romain Guy06882f82009-06-10 13:36:04 -07005941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5943 int returnWhat) {
5944 if (DEBUG_INPUT) Log.v(
5945 TAG, "finishedKey: client=" + client + ", force=" + force);
5946
5947 if (client == null) {
5948 return null;
5949 }
5950
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005951 MotionEvent res = null;
5952 QueuedEvent qev = null;
5953 WindowState win = null;
5954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 synchronized (this) {
5956 if (DEBUG_INPUT) Log.v(
5957 TAG, "finishedKey: client=" + client.asBinder()
5958 + ", force=" + force + ", last=" + mLastBinder
5959 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 if (returnWhat == RETURN_PENDING_POINTER) {
5962 qev = session.mPendingPointerMove;
5963 win = session.mPendingPointerWindow;
5964 session.mPendingPointerMove = null;
5965 session.mPendingPointerWindow = null;
5966 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5967 qev = session.mPendingTrackballMove;
5968 win = session.mPendingTrackballWindow;
5969 session.mPendingTrackballMove = null;
5970 session.mPendingTrackballWindow = null;
5971 }
Romain Guy06882f82009-06-10 13:36:04 -07005972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 if (mLastBinder == client.asBinder()) {
5974 if (DEBUG_INPUT) Log.v(
5975 TAG, "finishedKey: last paused="
5976 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5977 if (mLastWin != null && (!mLastWin.mToken.paused || force
5978 || !mEventDispatching)) {
5979 doFinishedKeyLocked(false);
5980 } else {
5981 // Make sure to wake up anyone currently waiting to
5982 // dispatch a key, so they can re-evaluate their
5983 // current situation.
5984 mFinished = true;
5985 notifyAll();
5986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 }
Romain Guy06882f82009-06-10 13:36:04 -07005988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005990 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 if (DEBUG_INPUT) Log.v(TAG,
5992 "Returning pending motion: " + res);
5993 mQueue.recycleEvent(qev);
5994 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5995 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005998
5999 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6000 synchronized (mWindowMap) {
6001 if (mWallpaperTarget == win || mSendingPointersToWallpaper) {
6002 sendPointerToWallpaperLocked(win, res, res.getEventTime());
6003 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006004 }
6005 }
6006 }
6007
6008 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 }
6010
6011 void tickle() {
6012 synchronized (this) {
6013 notifyAll();
6014 }
6015 }
Romain Guy06882f82009-06-10 13:36:04 -07006016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 void handleNewWindowLocked(WindowState newWindow) {
6018 if (!newWindow.canReceiveKeys()) {
6019 return;
6020 }
6021 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006022 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 TAG, "New key dispatch window: win="
6024 + newWindow.mClient.asBinder()
6025 + ", last=" + mLastBinder
6026 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6027 + "), finished=" + mFinished + ", paused="
6028 + newWindow.mToken.paused);
6029
6030 // Displaying a window implicitly causes dispatching to
6031 // be unpaused. (This is to protect against bugs if someone
6032 // pauses dispatching but forgets to resume.)
6033 newWindow.mToken.paused = false;
6034
6035 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036
6037 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6038 if (DEBUG_INPUT) Log.v(TAG,
6039 "New SYSTEM_ERROR window; resetting state");
6040 mLastWin = null;
6041 mLastBinder = null;
6042 mMotionTarget = null;
6043 mFinished = true;
6044 } else if (mLastWin != null) {
6045 // If the new window is above the window we are
6046 // waiting on, then stop waiting and let key dispatching
6047 // start on the new guy.
6048 if (DEBUG_INPUT) Log.v(
6049 TAG, "Last win layer=" + mLastWin.mLayer
6050 + ", new win layer=" + newWindow.mLayer);
6051 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006052 // The new window is above the old; finish pending input to the last
6053 // window and start directing it to the new one.
6054 mLastWin.mToken.paused = false;
6055 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006057 // Either the new window is lower, so there is no need to wake key waiters,
6058 // or we just finished key input to the previous window, which implicitly
6059 // notified the key waiters. In both cases, we don't need to issue the
6060 // notification here.
6061 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 }
6063
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006064 // Now that we've put a new window state in place, make the event waiter
6065 // take notice and retarget its attentions.
6066 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 }
6068 }
6069
6070 void pauseDispatchingLocked(WindowToken token) {
6071 synchronized (this)
6072 {
6073 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6074 token.paused = true;
6075
6076 /*
6077 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6078 mPaused = true;
6079 } else {
6080 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006081 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006083 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006085 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 }
6087 }
6088 */
6089 }
6090 }
6091
6092 void resumeDispatchingLocked(WindowToken token) {
6093 synchronized (this) {
6094 if (token.paused) {
6095 if (DEBUG_INPUT) Log.v(
6096 TAG, "Resuming WindowToken " + token
6097 + ", last=" + mLastBinder
6098 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6099 + "), finished=" + mFinished + ", paused="
6100 + token.paused);
6101 token.paused = false;
6102 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6103 doFinishedKeyLocked(true);
6104 } else {
6105 notifyAll();
6106 }
6107 }
6108 }
6109 }
6110
6111 void setEventDispatchingLocked(boolean enabled) {
6112 synchronized (this) {
6113 mEventDispatching = enabled;
6114 notifyAll();
6115 }
6116 }
Romain Guy06882f82009-06-10 13:36:04 -07006117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 void appSwitchComing() {
6119 synchronized (this) {
6120 // Don't wait for more than .5 seconds for app to finish
6121 // processing the pending events.
6122 long now = SystemClock.uptimeMillis() + 500;
6123 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6124 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6125 mTimeToSwitch = now;
6126 }
6127 notifyAll();
6128 }
6129 }
Romain Guy06882f82009-06-10 13:36:04 -07006130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 private final void doFinishedKeyLocked(boolean doRecycle) {
6132 if (mLastWin != null) {
6133 releasePendingPointerLocked(mLastWin.mSession);
6134 releasePendingTrackballLocked(mLastWin.mSession);
6135 }
Romain Guy06882f82009-06-10 13:36:04 -07006136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006137 if (mLastWin == null || !mLastWin.mToken.paused
6138 || !mLastWin.isVisibleLw()) {
6139 // If the current window has been paused, we aren't -really-
6140 // finished... so let the waiters still wait.
6141 mLastWin = null;
6142 mLastBinder = null;
6143 }
6144 mFinished = true;
6145 notifyAll();
6146 }
6147 }
6148
6149 private class KeyQ extends KeyInputQueue
6150 implements KeyInputQueue.FilterCallback {
6151 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006154 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6156 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6157 "KEEP_SCREEN_ON_FLAG");
6158 mHoldingScreen.setReferenceCounted(false);
6159 }
6160
6161 @Override
6162 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6163 if (mPolicy.preprocessInputEventTq(event)) {
6164 return true;
6165 }
Romain Guy06882f82009-06-10 13:36:04 -07006166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 switch (event.type) {
6168 case RawInputEvent.EV_KEY: {
6169 // XXX begin hack
6170 if (DEBUG) {
6171 if (event.keycode == KeyEvent.KEYCODE_G) {
6172 if (event.value != 0) {
6173 // G down
6174 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6175 }
6176 return false;
6177 }
6178 if (event.keycode == KeyEvent.KEYCODE_D) {
6179 if (event.value != 0) {
6180 //dump();
6181 }
6182 return false;
6183 }
6184 }
6185 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006186
Charles Mendis322591c2009-10-29 11:06:59 -07006187 boolean screenIsOff = !mPowerManager.isScreenOn();
6188 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6192 mPowerManager.goToSleep(event.when);
6193 }
6194
6195 if (screenIsOff) {
6196 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6197 }
6198 if (screenIsDim) {
6199 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6200 }
6201 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6202 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006203 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204 }
Romain Guy06882f82009-06-10 13:36:04 -07006205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6207 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6208 filterQueue(this);
6209 mKeyWaiter.appSwitchComing();
6210 }
6211 return true;
6212 } else {
6213 return false;
6214 }
6215 }
Romain Guy06882f82009-06-10 13:36:04 -07006216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006218 boolean screenIsOff = !mPowerManager.isScreenOn();
6219 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 if (screenIsOff) {
6221 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6222 device.classes, event)) {
6223 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6224 return false;
6225 }
6226 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6227 }
6228 if (screenIsDim) {
6229 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6230 }
6231 return true;
6232 }
Romain Guy06882f82009-06-10 13:36:04 -07006233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006235 boolean screenIsOff = !mPowerManager.isScreenOn();
6236 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 if (screenIsOff) {
6238 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6239 device.classes, event)) {
6240 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6241 return false;
6242 }
6243 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6244 }
6245 if (screenIsDim) {
6246 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6247 }
6248 return true;
6249 }
Romain Guy06882f82009-06-10 13:36:04 -07006250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 default:
6252 return true;
6253 }
6254 }
6255
6256 public int filterEvent(QueuedEvent ev) {
6257 switch (ev.classType) {
6258 case RawInputEvent.CLASS_KEYBOARD:
6259 KeyEvent ke = (KeyEvent)ev.event;
6260 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6261 Log.w(TAG, "Dropping movement key during app switch: "
6262 + ke.getKeyCode() + ", action=" + ke.getAction());
6263 return FILTER_REMOVE;
6264 }
6265 return FILTER_ABORT;
6266 default:
6267 return FILTER_KEEP;
6268 }
6269 }
Romain Guy06882f82009-06-10 13:36:04 -07006270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 /**
6272 * Must be called with the main window manager lock held.
6273 */
6274 void setHoldScreenLocked(boolean holding) {
6275 boolean state = mHoldingScreen.isHeld();
6276 if (holding != state) {
6277 if (holding) {
6278 mHoldingScreen.acquire();
6279 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006280 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 mHoldingScreen.release();
6282 }
6283 }
6284 }
Michael Chan53071d62009-05-13 17:29:48 -07006285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286
6287 public boolean detectSafeMode() {
6288 mSafeMode = mPolicy.detectSafeMode();
6289 return mSafeMode;
6290 }
Romain Guy06882f82009-06-10 13:36:04 -07006291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 public void systemReady() {
6293 mPolicy.systemReady();
6294 }
Romain Guy06882f82009-06-10 13:36:04 -07006295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 private final class InputDispatcherThread extends Thread {
6297 // Time to wait when there is nothing to do: 9999 seconds.
6298 static final int LONG_WAIT=9999*1000;
6299
6300 public InputDispatcherThread() {
6301 super("InputDispatcher");
6302 }
Romain Guy06882f82009-06-10 13:36:04 -07006303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 @Override
6305 public void run() {
6306 while (true) {
6307 try {
6308 process();
6309 } catch (Exception e) {
6310 Log.e(TAG, "Exception in input dispatcher", e);
6311 }
6312 }
6313 }
Romain Guy06882f82009-06-10 13:36:04 -07006314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 private void process() {
6316 android.os.Process.setThreadPriority(
6317 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 // The last key event we saw
6320 KeyEvent lastKey = null;
6321
6322 // Last keydown time for auto-repeating keys
6323 long lastKeyTime = SystemClock.uptimeMillis();
6324 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006325 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326
Romain Guy06882f82009-06-10 13:36:04 -07006327 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 int keyRepeatCount = 0;
6329
6330 // Need to report that configuration has changed?
6331 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 while (true) {
6334 long curTime = SystemClock.uptimeMillis();
6335
6336 if (DEBUG_INPUT) Log.v(
6337 TAG, "Waiting for next key: now=" + curTime
6338 + ", repeat @ " + nextKeyTime);
6339
6340 // Retrieve next event, waiting only as long as the next
6341 // repeat timeout. If the configuration has changed, then
6342 // don't wait at all -- we'll report the change as soon as
6343 // we have processed all events.
6344 QueuedEvent ev = mQueue.getEvent(
6345 (int)((!configChanged && curTime < nextKeyTime)
6346 ? (nextKeyTime-curTime) : 0));
6347
6348 if (DEBUG_INPUT && ev != null) Log.v(
6349 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6350
Michael Chan53071d62009-05-13 17:29:48 -07006351 if (MEASURE_LATENCY) {
6352 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6353 }
6354
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006355 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6356 // cancel key repeat at the request of the policy.
6357 lastKey = null;
6358 downTime = 0;
6359 lastKeyTime = curTime;
6360 nextKeyTime = curTime + LONG_WAIT;
6361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 try {
6363 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006364 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 int eventType;
6366 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6367 eventType = eventType((MotionEvent)ev.event);
6368 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6369 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6370 eventType = LocalPowerManager.BUTTON_EVENT;
6371 } else {
6372 eventType = LocalPowerManager.OTHER_EVENT;
6373 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006374 try {
Michael Chan53071d62009-05-13 17:29:48 -07006375 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006376 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006377 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006378 mBatteryStats.noteInputEvent();
6379 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006380 } catch (RemoteException e) {
6381 // Ignore
6382 }
Michael Chane10de972009-05-18 11:24:50 -07006383
6384 if (eventType != TOUCH_EVENT
6385 && eventType != LONG_TOUCH_EVENT
6386 && eventType != CHEEK_EVENT) {
6387 mPowerManager.userActivity(curTime, false,
6388 eventType, false);
6389 } else if (mLastTouchEventType != eventType
6390 || (curTime - mLastUserActivityCallTime)
6391 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6392 mLastUserActivityCallTime = curTime;
6393 mLastTouchEventType = eventType;
6394 mPowerManager.userActivity(curTime, false,
6395 eventType, false);
6396 }
6397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006398 switch (ev.classType) {
6399 case RawInputEvent.CLASS_KEYBOARD:
6400 KeyEvent ke = (KeyEvent)ev.event;
6401 if (ke.isDown()) {
6402 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006403 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404 keyRepeatCount = 0;
6405 lastKeyTime = curTime;
6406 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006407 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 if (DEBUG_INPUT) Log.v(
6409 TAG, "Received key down: first repeat @ "
6410 + nextKeyTime);
6411 } else {
6412 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006413 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 // Arbitrary long timeout.
6415 lastKeyTime = curTime;
6416 nextKeyTime = curTime + LONG_WAIT;
6417 if (DEBUG_INPUT) Log.v(
6418 TAG, "Received key up: ignore repeat @ "
6419 + nextKeyTime);
6420 }
6421 dispatchKey((KeyEvent)ev.event, 0, 0);
6422 mQueue.recycleEvent(ev);
6423 break;
6424 case RawInputEvent.CLASS_TOUCHSCREEN:
6425 //Log.i(TAG, "Read next event " + ev);
6426 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6427 break;
6428 case RawInputEvent.CLASS_TRACKBALL:
6429 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6430 break;
6431 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6432 configChanged = true;
6433 break;
6434 default:
6435 mQueue.recycleEvent(ev);
6436 break;
6437 }
Romain Guy06882f82009-06-10 13:36:04 -07006438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 } else if (configChanged) {
6440 configChanged = false;
6441 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006443 } else if (lastKey != null) {
6444 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 // Timeout occurred while key was down. If it is at or
6447 // past the key repeat time, dispatch the repeat.
6448 if (DEBUG_INPUT) Log.v(
6449 TAG, "Key timeout: repeat=" + nextKeyTime
6450 + ", now=" + curTime);
6451 if (curTime < nextKeyTime) {
6452 continue;
6453 }
Romain Guy06882f82009-06-10 13:36:04 -07006454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 lastKeyTime = nextKeyTime;
6456 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6457 keyRepeatCount++;
6458 if (DEBUG_INPUT) Log.v(
6459 TAG, "Key repeat: count=" + keyRepeatCount
6460 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006461 KeyEvent newEvent;
6462 if (downTime != 0 && (downTime
6463 + ViewConfiguration.getLongPressTimeout())
6464 <= curTime) {
6465 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6466 curTime, keyRepeatCount,
6467 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6468 downTime = 0;
6469 } else {
6470 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6471 curTime, keyRepeatCount);
6472 }
6473 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 } else {
6476 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 lastKeyTime = curTime;
6479 nextKeyTime = curTime + LONG_WAIT;
6480 }
Romain Guy06882f82009-06-10 13:36:04 -07006481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 } catch (Exception e) {
6483 Log.e(TAG,
6484 "Input thread received uncaught exception: " + e, e);
6485 }
6486 }
6487 }
6488 }
6489
6490 // -------------------------------------------------------------
6491 // Client Session State
6492 // -------------------------------------------------------------
6493
6494 private final class Session extends IWindowSession.Stub
6495 implements IBinder.DeathRecipient {
6496 final IInputMethodClient mClient;
6497 final IInputContext mInputContext;
6498 final int mUid;
6499 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006500 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 SurfaceSession mSurfaceSession;
6502 int mNumWindow = 0;
6503 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 /**
6506 * Current pointer move event being dispatched to client window... must
6507 * hold key lock to access.
6508 */
6509 QueuedEvent mPendingPointerMove;
6510 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 /**
6513 * Current trackball move event being dispatched to client window... must
6514 * hold key lock to access.
6515 */
6516 QueuedEvent mPendingTrackballMove;
6517 WindowState mPendingTrackballWindow;
6518
6519 public Session(IInputMethodClient client, IInputContext inputContext) {
6520 mClient = client;
6521 mInputContext = inputContext;
6522 mUid = Binder.getCallingUid();
6523 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006524 StringBuilder sb = new StringBuilder();
6525 sb.append("Session{");
6526 sb.append(Integer.toHexString(System.identityHashCode(this)));
6527 sb.append(" uid ");
6528 sb.append(mUid);
6529 sb.append("}");
6530 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 synchronized (mWindowMap) {
6533 if (mInputMethodManager == null && mHaveInputMethods) {
6534 IBinder b = ServiceManager.getService(
6535 Context.INPUT_METHOD_SERVICE);
6536 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6537 }
6538 }
6539 long ident = Binder.clearCallingIdentity();
6540 try {
6541 // Note: it is safe to call in to the input method manager
6542 // here because we are not holding our lock.
6543 if (mInputMethodManager != null) {
6544 mInputMethodManager.addClient(client, inputContext,
6545 mUid, mPid);
6546 } else {
6547 client.setUsingInputMethod(false);
6548 }
6549 client.asBinder().linkToDeath(this, 0);
6550 } catch (RemoteException e) {
6551 // The caller has died, so we can just forget about this.
6552 try {
6553 if (mInputMethodManager != null) {
6554 mInputMethodManager.removeClient(client);
6555 }
6556 } catch (RemoteException ee) {
6557 }
6558 } finally {
6559 Binder.restoreCallingIdentity(ident);
6560 }
6561 }
Romain Guy06882f82009-06-10 13:36:04 -07006562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006563 @Override
6564 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6565 throws RemoteException {
6566 try {
6567 return super.onTransact(code, data, reply, flags);
6568 } catch (RuntimeException e) {
6569 // Log all 'real' exceptions thrown to the caller
6570 if (!(e instanceof SecurityException)) {
6571 Log.e(TAG, "Window Session Crash", e);
6572 }
6573 throw e;
6574 }
6575 }
6576
6577 public void binderDied() {
6578 // Note: it is safe to call in to the input method manager
6579 // here because we are not holding our lock.
6580 try {
6581 if (mInputMethodManager != null) {
6582 mInputMethodManager.removeClient(mClient);
6583 }
6584 } catch (RemoteException e) {
6585 }
6586 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006587 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 mClientDead = true;
6589 killSessionLocked();
6590 }
6591 }
6592
6593 public int add(IWindow window, WindowManager.LayoutParams attrs,
6594 int viewVisibility, Rect outContentInsets) {
6595 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6596 }
Romain Guy06882f82009-06-10 13:36:04 -07006597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 public void remove(IWindow window) {
6599 removeWindow(this, window);
6600 }
Romain Guy06882f82009-06-10 13:36:04 -07006601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006602 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6603 int requestedWidth, int requestedHeight, int viewFlags,
6604 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6605 Rect outVisibleInsets, Surface outSurface) {
6606 return relayoutWindow(this, window, attrs,
6607 requestedWidth, requestedHeight, viewFlags, insetsPending,
6608 outFrame, outContentInsets, outVisibleInsets, outSurface);
6609 }
Romain Guy06882f82009-06-10 13:36:04 -07006610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 public void setTransparentRegion(IWindow window, Region region) {
6612 setTransparentRegionWindow(this, window, region);
6613 }
Romain Guy06882f82009-06-10 13:36:04 -07006614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 public void setInsets(IWindow window, int touchableInsets,
6616 Rect contentInsets, Rect visibleInsets) {
6617 setInsetsWindow(this, window, touchableInsets, contentInsets,
6618 visibleInsets);
6619 }
Romain Guy06882f82009-06-10 13:36:04 -07006620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6622 getWindowDisplayFrame(this, window, outDisplayFrame);
6623 }
Romain Guy06882f82009-06-10 13:36:04 -07006624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 public void finishDrawing(IWindow window) {
6626 if (localLOGV) Log.v(
6627 TAG, "IWindow finishDrawing called for " + window);
6628 finishDrawingWindow(this, window);
6629 }
6630
6631 public void finishKey(IWindow window) {
6632 if (localLOGV) Log.v(
6633 TAG, "IWindow finishKey called for " + window);
6634 mKeyWaiter.finishedKey(this, window, false,
6635 KeyWaiter.RETURN_NOTHING);
6636 }
6637
6638 public MotionEvent getPendingPointerMove(IWindow window) {
6639 if (localLOGV) Log.v(
6640 TAG, "IWindow getPendingMotionEvent called for " + window);
6641 return mKeyWaiter.finishedKey(this, window, false,
6642 KeyWaiter.RETURN_PENDING_POINTER);
6643 }
Romain Guy06882f82009-06-10 13:36:04 -07006644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 public MotionEvent getPendingTrackballMove(IWindow window) {
6646 if (localLOGV) Log.v(
6647 TAG, "IWindow getPendingMotionEvent called for " + window);
6648 return mKeyWaiter.finishedKey(this, window, false,
6649 KeyWaiter.RETURN_PENDING_TRACKBALL);
6650 }
6651
6652 public void setInTouchMode(boolean mode) {
6653 synchronized(mWindowMap) {
6654 mInTouchMode = mode;
6655 }
6656 }
6657
6658 public boolean getInTouchMode() {
6659 synchronized(mWindowMap) {
6660 return mInTouchMode;
6661 }
6662 }
6663
6664 public boolean performHapticFeedback(IWindow window, int effectId,
6665 boolean always) {
6666 synchronized(mWindowMap) {
6667 long ident = Binder.clearCallingIdentity();
6668 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006669 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 windowForClientLocked(this, window), effectId, always);
6671 } finally {
6672 Binder.restoreCallingIdentity(ident);
6673 }
6674 }
6675 }
Romain Guy06882f82009-06-10 13:36:04 -07006676
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006677 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006678 synchronized(mWindowMap) {
6679 long ident = Binder.clearCallingIdentity();
6680 try {
6681 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006682 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006683 } finally {
6684 Binder.restoreCallingIdentity(ident);
6685 }
6686 }
6687 }
6688
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006689 public void wallpaperOffsetsComplete(IBinder window) {
6690 WindowManagerService.this.wallpaperOffsetsComplete(window);
6691 }
6692
Dianne Hackborn75804932009-10-20 20:15:20 -07006693 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6694 int z, Bundle extras, boolean sync) {
6695 synchronized(mWindowMap) {
6696 long ident = Binder.clearCallingIdentity();
6697 try {
6698 return sendWindowWallpaperCommandLocked(
6699 windowForClientLocked(this, window),
6700 action, x, y, z, extras, sync);
6701 } finally {
6702 Binder.restoreCallingIdentity(ident);
6703 }
6704 }
6705 }
6706
6707 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6708 WindowManagerService.this.wallpaperCommandComplete(window, result);
6709 }
6710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006711 void windowAddedLocked() {
6712 if (mSurfaceSession == null) {
6713 if (localLOGV) Log.v(
6714 TAG, "First window added to " + this + ", creating SurfaceSession");
6715 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006716 if (SHOW_TRANSACTIONS) Log.i(
6717 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006718 mSessions.add(this);
6719 }
6720 mNumWindow++;
6721 }
6722
6723 void windowRemovedLocked() {
6724 mNumWindow--;
6725 killSessionLocked();
6726 }
Romain Guy06882f82009-06-10 13:36:04 -07006727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 void killSessionLocked() {
6729 if (mNumWindow <= 0 && mClientDead) {
6730 mSessions.remove(this);
6731 if (mSurfaceSession != null) {
6732 if (localLOGV) Log.v(
6733 TAG, "Last window removed from " + this
6734 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006735 if (SHOW_TRANSACTIONS) Log.i(
6736 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 try {
6738 mSurfaceSession.kill();
6739 } catch (Exception e) {
6740 Log.w(TAG, "Exception thrown when killing surface session "
6741 + mSurfaceSession + " in session " + this
6742 + ": " + e.toString());
6743 }
6744 mSurfaceSession = null;
6745 }
6746 }
6747 }
Romain Guy06882f82009-06-10 13:36:04 -07006748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006750 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6751 pw.print(" mClientDead="); pw.print(mClientDead);
6752 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6753 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6754 pw.print(prefix);
6755 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6756 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6757 }
6758 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6759 pw.print(prefix);
6760 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6761 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006763 }
6764
6765 @Override
6766 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006767 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 }
6769 }
6770
6771 // -------------------------------------------------------------
6772 // Client Window State
6773 // -------------------------------------------------------------
6774
6775 private final class WindowState implements WindowManagerPolicy.WindowState {
6776 final Session mSession;
6777 final IWindow mClient;
6778 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006779 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 AppWindowToken mAppToken;
6781 AppWindowToken mTargetAppToken;
6782 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6783 final DeathRecipient mDeathRecipient;
6784 final WindowState mAttachedWindow;
6785 final ArrayList mChildWindows = new ArrayList();
6786 final int mBaseLayer;
6787 final int mSubLayer;
6788 final boolean mLayoutAttached;
6789 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006790 final boolean mIsWallpaper;
6791 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006792 int mViewVisibility;
6793 boolean mPolicyVisibility = true;
6794 boolean mPolicyVisibilityAfterAnim = true;
6795 boolean mAppFreezing;
6796 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006797 boolean mReportDestroySurface;
6798 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 boolean mAttachedHidden; // is our parent window hidden?
6800 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006801 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 int mRequestedWidth;
6803 int mRequestedHeight;
6804 int mLastRequestedWidth;
6805 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 int mLayer;
6807 int mAnimLayer;
6808 int mLastLayer;
6809 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006810 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006811 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812
6813 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 // Actual frame shown on-screen (may be modified by animation)
6816 final Rect mShownFrame = new Rect();
6817 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006819 /**
6820 * Insets that determine the actually visible area
6821 */
6822 final Rect mVisibleInsets = new Rect();
6823 final Rect mLastVisibleInsets = new Rect();
6824 boolean mVisibleInsetsChanged;
6825
6826 /**
6827 * Insets that are covered by system windows
6828 */
6829 final Rect mContentInsets = new Rect();
6830 final Rect mLastContentInsets = new Rect();
6831 boolean mContentInsetsChanged;
6832
6833 /**
6834 * Set to true if we are waiting for this window to receive its
6835 * given internal insets before laying out other windows based on it.
6836 */
6837 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 /**
6840 * These are the content insets that were given during layout for
6841 * this window, to be applied to windows behind it.
6842 */
6843 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845 /**
6846 * These are the visible insets that were given during layout for
6847 * this window, to be applied to windows behind it.
6848 */
6849 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 /**
6852 * Flag indicating whether the touchable region should be adjusted by
6853 * the visible insets; if false the area outside the visible insets is
6854 * NOT touchable, so we must use those to adjust the frame during hit
6855 * tests.
6856 */
6857 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 // Current transformation being applied.
6860 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6861 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6862 float mHScale=1, mVScale=1;
6863 float mLastHScale=1, mLastVScale=1;
6864 final Matrix mTmpMatrix = new Matrix();
6865
6866 // "Real" frame that the application sees.
6867 final Rect mFrame = new Rect();
6868 final Rect mLastFrame = new Rect();
6869
6870 final Rect mContainingFrame = new Rect();
6871 final Rect mDisplayFrame = new Rect();
6872 final Rect mContentFrame = new Rect();
6873 final Rect mVisibleFrame = new Rect();
6874
6875 float mShownAlpha = 1;
6876 float mAlpha = 1;
6877 float mLastAlpha = 1;
6878
6879 // Set to true if, when the window gets displayed, it should perform
6880 // an enter animation.
6881 boolean mEnterAnimationPending;
6882
6883 // Currently running animation.
6884 boolean mAnimating;
6885 boolean mLocalAnimating;
6886 Animation mAnimation;
6887 boolean mAnimationIsEntrance;
6888 boolean mHasTransformation;
6889 boolean mHasLocalTransformation;
6890 final Transformation mTransformation = new Transformation();
6891
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006892 // If a window showing a wallpaper: the requested offset for the
6893 // wallpaper; if a wallpaper window: the currently applied offset.
6894 float mWallpaperX = -1;
6895 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006896
6897 // If a window showing a wallpaper: what fraction of the offset
6898 // range corresponds to a full virtual screen.
6899 float mWallpaperXStep = -1;
6900 float mWallpaperYStep = -1;
6901
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006902 // Wallpaper windows: pixels offset based on above variables.
6903 int mXOffset;
6904 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 // This is set after IWindowSession.relayout() has been called at
6907 // least once for the window. It allows us to detect the situation
6908 // where we don't yet have a surface, but should have one soon, so
6909 // we can give the window focus before waiting for the relayout.
6910 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 // This is set after the Surface has been created but before the
6913 // window has been drawn. During this time the surface is hidden.
6914 boolean mDrawPending;
6915
6916 // This is set after the window has finished drawing for the first
6917 // time but before its surface is shown. The surface will be
6918 // displayed when the next layout is run.
6919 boolean mCommitDrawPending;
6920
6921 // This is set during the time after the window's drawing has been
6922 // committed, and before its surface is actually shown. It is used
6923 // to delay showing the surface until all windows in a token are ready
6924 // to be shown.
6925 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 // Set when the window has been shown in the screen the first time.
6928 boolean mHasDrawn;
6929
6930 // Currently running an exit animation?
6931 boolean mExiting;
6932
6933 // Currently on the mDestroySurface list?
6934 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 // Completely remove from window manager after exit animation?
6937 boolean mRemoveOnExit;
6938
6939 // Set when the orientation is changing and this window has not yet
6940 // been updated for the new orientation.
6941 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 // Is this window now (or just being) removed?
6944 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 WindowState(Session s, IWindow c, WindowToken token,
6947 WindowState attachedWindow, WindowManager.LayoutParams a,
6948 int viewVisibility) {
6949 mSession = s;
6950 mClient = c;
6951 mToken = token;
6952 mAttrs.copyFrom(a);
6953 mViewVisibility = viewVisibility;
6954 DeathRecipient deathRecipient = new DeathRecipient();
6955 mAlpha = a.alpha;
6956 if (localLOGV) Log.v(
6957 TAG, "Window " + this + " client=" + c.asBinder()
6958 + " token=" + token + " (" + mAttrs.token + ")");
6959 try {
6960 c.asBinder().linkToDeath(deathRecipient, 0);
6961 } catch (RemoteException e) {
6962 mDeathRecipient = null;
6963 mAttachedWindow = null;
6964 mLayoutAttached = false;
6965 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006966 mIsWallpaper = false;
6967 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 mBaseLayer = 0;
6969 mSubLayer = 0;
6970 return;
6971 }
6972 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006974 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6975 mAttrs.type <= LAST_SUB_WINDOW)) {
6976 // The multiplier here is to reserve space for multiple
6977 // windows in the same type layer.
6978 mBaseLayer = mPolicy.windowTypeToLayerLw(
6979 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6980 + TYPE_LAYER_OFFSET;
6981 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6982 mAttachedWindow = attachedWindow;
6983 mAttachedWindow.mChildWindows.add(this);
6984 mLayoutAttached = mAttrs.type !=
6985 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6986 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6987 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006988 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6989 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 } else {
6991 // The multiplier here is to reserve space for multiple
6992 // windows in the same type layer.
6993 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6994 * TYPE_LAYER_MULTIPLIER
6995 + TYPE_LAYER_OFFSET;
6996 mSubLayer = 0;
6997 mAttachedWindow = null;
6998 mLayoutAttached = false;
6999 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7000 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007001 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7002 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 }
7004
7005 WindowState appWin = this;
7006 while (appWin.mAttachedWindow != null) {
7007 appWin = mAttachedWindow;
7008 }
7009 WindowToken appToken = appWin.mToken;
7010 while (appToken.appWindowToken == null) {
7011 WindowToken parent = mTokenMap.get(appToken.token);
7012 if (parent == null || appToken == parent) {
7013 break;
7014 }
7015 appToken = parent;
7016 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007017 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 mAppToken = appToken.appWindowToken;
7019
7020 mSurface = null;
7021 mRequestedWidth = 0;
7022 mRequestedHeight = 0;
7023 mLastRequestedWidth = 0;
7024 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007025 mXOffset = 0;
7026 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 mLayer = 0;
7028 mAnimLayer = 0;
7029 mLastLayer = 0;
7030 }
7031
7032 void attach() {
7033 if (localLOGV) Log.v(
7034 TAG, "Attaching " + this + " token=" + mToken
7035 + ", list=" + mToken.windows);
7036 mSession.windowAddedLocked();
7037 }
7038
7039 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7040 mHaveFrame = true;
7041
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007042 final Rect container = mContainingFrame;
7043 container.set(pf);
7044
7045 final Rect display = mDisplayFrame;
7046 display.set(df);
7047
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007048 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007049 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007050 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7051 display.intersect(mCompatibleScreenFrame);
7052 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007053 }
7054
7055 final int pw = container.right - container.left;
7056 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057
7058 int w,h;
7059 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7060 w = mAttrs.width < 0 ? pw : mAttrs.width;
7061 h = mAttrs.height< 0 ? ph : mAttrs.height;
7062 } else {
7063 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7064 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7065 }
Romain Guy06882f82009-06-10 13:36:04 -07007066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 final Rect content = mContentFrame;
7068 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 final Rect visible = mVisibleFrame;
7071 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007074 final int fw = frame.width();
7075 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7078 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7079
7080 Gravity.apply(mAttrs.gravity, w, h, container,
7081 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7082 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7083
7084 //System.out.println("Out: " + mFrame);
7085
7086 // Now make sure the window fits in the overall display.
7087 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 // Make sure the content and visible frames are inside of the
7090 // final window frame.
7091 if (content.left < frame.left) content.left = frame.left;
7092 if (content.top < frame.top) content.top = frame.top;
7093 if (content.right > frame.right) content.right = frame.right;
7094 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7095 if (visible.left < frame.left) visible.left = frame.left;
7096 if (visible.top < frame.top) visible.top = frame.top;
7097 if (visible.right > frame.right) visible.right = frame.right;
7098 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 final Rect contentInsets = mContentInsets;
7101 contentInsets.left = content.left-frame.left;
7102 contentInsets.top = content.top-frame.top;
7103 contentInsets.right = frame.right-content.right;
7104 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 final Rect visibleInsets = mVisibleInsets;
7107 visibleInsets.left = visible.left-frame.left;
7108 visibleInsets.top = visible.top-frame.top;
7109 visibleInsets.right = frame.right-visible.right;
7110 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007111
Dianne Hackborn284ac932009-08-28 10:34:25 -07007112 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7113 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007114 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007115 }
7116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 if (localLOGV) {
7118 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7119 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7120 Log.v(TAG, "Resolving (mRequestedWidth="
7121 + mRequestedWidth + ", mRequestedheight="
7122 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7123 + "): frame=" + mFrame.toShortString()
7124 + " ci=" + contentInsets.toShortString()
7125 + " vi=" + visibleInsets.toShortString());
7126 //}
7127 }
7128 }
Romain Guy06882f82009-06-10 13:36:04 -07007129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007130 public Rect getFrameLw() {
7131 return mFrame;
7132 }
7133
7134 public Rect getShownFrameLw() {
7135 return mShownFrame;
7136 }
7137
7138 public Rect getDisplayFrameLw() {
7139 return mDisplayFrame;
7140 }
7141
7142 public Rect getContentFrameLw() {
7143 return mContentFrame;
7144 }
7145
7146 public Rect getVisibleFrameLw() {
7147 return mVisibleFrame;
7148 }
7149
7150 public boolean getGivenInsetsPendingLw() {
7151 return mGivenInsetsPending;
7152 }
7153
7154 public Rect getGivenContentInsetsLw() {
7155 return mGivenContentInsets;
7156 }
Romain Guy06882f82009-06-10 13:36:04 -07007157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007158 public Rect getGivenVisibleInsetsLw() {
7159 return mGivenVisibleInsets;
7160 }
Romain Guy06882f82009-06-10 13:36:04 -07007161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 public WindowManager.LayoutParams getAttrs() {
7163 return mAttrs;
7164 }
7165
7166 public int getSurfaceLayer() {
7167 return mLayer;
7168 }
Romain Guy06882f82009-06-10 13:36:04 -07007169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 public IApplicationToken getAppToken() {
7171 return mAppToken != null ? mAppToken.appToken : null;
7172 }
7173
7174 public boolean hasAppShownWindows() {
7175 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7176 }
7177
7178 public boolean hasAppStartingIcon() {
7179 return mAppToken != null ? (mAppToken.startingData != null) : false;
7180 }
7181
7182 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7183 return mAppToken != null ? mAppToken.startingWindow : null;
7184 }
7185
7186 public void setAnimation(Animation anim) {
7187 if (localLOGV) Log.v(
7188 TAG, "Setting animation in " + this + ": " + anim);
7189 mAnimating = false;
7190 mLocalAnimating = false;
7191 mAnimation = anim;
7192 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7193 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7194 }
7195
7196 public void clearAnimation() {
7197 if (mAnimation != null) {
7198 mAnimating = true;
7199 mLocalAnimating = false;
7200 mAnimation = null;
7201 }
7202 }
Romain Guy06882f82009-06-10 13:36:04 -07007203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 Surface createSurfaceLocked() {
7205 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007206 mReportDestroySurface = false;
7207 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 mDrawPending = true;
7209 mCommitDrawPending = false;
7210 mReadyToShow = false;
7211 if (mAppToken != null) {
7212 mAppToken.allDrawn = false;
7213 }
7214
7215 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007216 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007217 flags |= Surface.PUSH_BUFFERS;
7218 }
7219
7220 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7221 flags |= Surface.SECURE;
7222 }
7223 if (DEBUG_VISIBILITY) Log.v(
7224 TAG, "Creating surface in session "
7225 + mSession.mSurfaceSession + " window " + this
7226 + " w=" + mFrame.width()
7227 + " h=" + mFrame.height() + " format="
7228 + mAttrs.format + " flags=" + flags);
7229
7230 int w = mFrame.width();
7231 int h = mFrame.height();
7232 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7233 // for a scaled surface, we always want the requested
7234 // size.
7235 w = mRequestedWidth;
7236 h = mRequestedHeight;
7237 }
7238
Romain Guy9825ec62009-10-01 00:58:09 -07007239 // Something is wrong and SurfaceFlinger will not like this,
7240 // try to revert to sane values
7241 if (w <= 0) w = 1;
7242 if (h <= 0) h = 1;
7243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 try {
7245 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007246 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007248 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7249 + mSurface + " IN SESSION "
7250 + mSession.mSurfaceSession
7251 + ": pid=" + mSession.mPid + " format="
7252 + mAttrs.format + " flags=0x"
7253 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 } catch (Surface.OutOfResourcesException e) {
7255 Log.w(TAG, "OutOfResourcesException creating surface");
7256 reclaimSomeSurfaceMemoryLocked(this, "create");
7257 return null;
7258 } catch (Exception e) {
7259 Log.e(TAG, "Exception creating surface", e);
7260 return null;
7261 }
Romain Guy06882f82009-06-10 13:36:04 -07007262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007263 if (localLOGV) Log.v(
7264 TAG, "Got surface: " + mSurface
7265 + ", set left=" + mFrame.left + " top=" + mFrame.top
7266 + ", animLayer=" + mAnimLayer);
7267 if (SHOW_TRANSACTIONS) {
7268 Log.i(TAG, ">>> OPEN TRANSACTION");
7269 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7270 + mAttrs.getTitle() + ") pos=(" +
7271 mFrame.left + "," + mFrame.top + ") (" +
7272 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7273 mAnimLayer + " HIDE");
7274 }
7275 Surface.openTransaction();
7276 try {
7277 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007278 mSurface.setPosition(mFrame.left + mXOffset,
7279 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 mSurface.setLayer(mAnimLayer);
7281 mSurface.hide();
7282 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007283 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7284 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 mSurface.setFlags(Surface.SURFACE_DITHER,
7286 Surface.SURFACE_DITHER);
7287 }
7288 } catch (RuntimeException e) {
7289 Log.w(TAG, "Error creating surface in " + w, e);
7290 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7291 }
7292 mLastHidden = true;
7293 } finally {
7294 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7295 Surface.closeTransaction();
7296 }
7297 if (localLOGV) Log.v(
7298 TAG, "Created surface " + this);
7299 }
7300 return mSurface;
7301 }
Romain Guy06882f82009-06-10 13:36:04 -07007302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 void destroySurfaceLocked() {
7304 // Window is no longer on-screen, so can no longer receive
7305 // key events... if we were waiting for it to finish
7306 // handling a key event, the wait is over!
7307 mKeyWaiter.finishedKey(mSession, mClient, true,
7308 KeyWaiter.RETURN_NOTHING);
7309 mKeyWaiter.releasePendingPointerLocked(mSession);
7310 mKeyWaiter.releasePendingTrackballLocked(mSession);
7311
7312 if (mAppToken != null && this == mAppToken.startingWindow) {
7313 mAppToken.startingDisplayed = false;
7314 }
Romain Guy06882f82009-06-10 13:36:04 -07007315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007317 mDrawPending = false;
7318 mCommitDrawPending = false;
7319 mReadyToShow = false;
7320
7321 int i = mChildWindows.size();
7322 while (i > 0) {
7323 i--;
7324 WindowState c = (WindowState)mChildWindows.get(i);
7325 c.mAttachedHidden = true;
7326 }
7327
7328 if (mReportDestroySurface) {
7329 mReportDestroySurface = false;
7330 mSurfacePendingDestroy = true;
7331 try {
7332 mClient.dispatchGetNewSurface();
7333 // We'll really destroy on the next time around.
7334 return;
7335 } catch (RemoteException e) {
7336 }
7337 }
7338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007340 if (DEBUG_VISIBILITY) {
7341 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007342 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007343 Log.w(TAG, "Window " + this + " destroying surface "
7344 + mSurface + ", session " + mSession, e);
7345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 if (SHOW_TRANSACTIONS) {
7347 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007348 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7350 + mAttrs.getTitle() + ")", ex);
7351 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007352 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007353 } catch (RuntimeException e) {
7354 Log.w(TAG, "Exception thrown when destroying Window " + this
7355 + " surface " + mSurface + " session " + mSession
7356 + ": " + e.toString());
7357 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 }
7361 }
7362
7363 boolean finishDrawingLocked() {
7364 if (mDrawPending) {
7365 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7366 TAG, "finishDrawingLocked: " + mSurface);
7367 mCommitDrawPending = true;
7368 mDrawPending = false;
7369 return true;
7370 }
7371 return false;
7372 }
7373
7374 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007375 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7377 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007378 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 }
7380 mCommitDrawPending = false;
7381 mReadyToShow = true;
7382 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7383 final AppWindowToken atoken = mAppToken;
7384 if (atoken == null || atoken.allDrawn || starting) {
7385 performShowLocked();
7386 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007387 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007388 }
7389
7390 // This must be called while inside a transaction.
7391 boolean performShowLocked() {
7392 if (DEBUG_VISIBILITY) {
7393 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007394 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 Log.v(TAG, "performShow on " + this
7396 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7397 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7398 }
7399 if (mReadyToShow && isReadyForDisplay()) {
7400 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7401 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7402 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7403 + " during animation: policyVis=" + mPolicyVisibility
7404 + " attHidden=" + mAttachedHidden
7405 + " tok.hiddenRequested="
7406 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007407 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 + (mAppToken != null ? mAppToken.hidden : false)
7409 + " animating=" + mAnimating
7410 + " tok animating="
7411 + (mAppToken != null ? mAppToken.animating : false));
7412 if (!showSurfaceRobustlyLocked(this)) {
7413 return false;
7414 }
7415 mLastAlpha = -1;
7416 mHasDrawn = true;
7417 mLastHidden = false;
7418 mReadyToShow = false;
7419 enableScreenIfNeededLocked();
7420
7421 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 int i = mChildWindows.size();
7424 while (i > 0) {
7425 i--;
7426 WindowState c = (WindowState)mChildWindows.get(i);
7427 if (c.mSurface != null && c.mAttachedHidden) {
7428 c.mAttachedHidden = false;
7429 c.performShowLocked();
7430 }
7431 }
Romain Guy06882f82009-06-10 13:36:04 -07007432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 if (mAttrs.type != TYPE_APPLICATION_STARTING
7434 && mAppToken != null) {
7435 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007436
7437 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007438 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7439 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007441 // If this initial window is animating, stop it -- we
7442 // will do an animation to reveal it from behind the
7443 // starting window, so there is no need for it to also
7444 // be doing its own stuff.
7445 if (mAnimation != null) {
7446 mAnimation = null;
7447 // Make sure we clean up the animation.
7448 mAnimating = true;
7449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 mFinishedStarting.add(mAppToken);
7451 mH.sendEmptyMessage(H.FINISHED_STARTING);
7452 }
7453 mAppToken.updateReportedVisibilityLocked();
7454 }
7455 }
7456 return true;
7457 }
Romain Guy06882f82009-06-10 13:36:04 -07007458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007459 // This must be called while inside a transaction. Returns true if
7460 // there is more animation to run.
7461 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7462 if (!mDisplayFrozen) {
7463 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7466 mHasTransformation = true;
7467 mHasLocalTransformation = true;
7468 if (!mLocalAnimating) {
7469 if (DEBUG_ANIM) Log.v(
7470 TAG, "Starting animation in " + this +
7471 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7472 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7473 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7474 mAnimation.setStartTime(currentTime);
7475 mLocalAnimating = true;
7476 mAnimating = true;
7477 }
7478 mTransformation.clear();
7479 final boolean more = mAnimation.getTransformation(
7480 currentTime, mTransformation);
7481 if (DEBUG_ANIM) Log.v(
7482 TAG, "Stepped animation in " + this +
7483 ": more=" + more + ", xform=" + mTransformation);
7484 if (more) {
7485 // we're not done!
7486 return true;
7487 }
7488 if (DEBUG_ANIM) Log.v(
7489 TAG, "Finished animation in " + this +
7490 " @ " + currentTime);
7491 mAnimation = null;
7492 //WindowManagerService.this.dump();
7493 }
7494 mHasLocalTransformation = false;
7495 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007496 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 // When our app token is animating, we kind-of pretend like
7498 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7499 // part of this check means that we will only do this if
7500 // our window is not currently exiting, or it is not
7501 // locally animating itself. The idea being that one that
7502 // is exiting and doing a local animation should be removed
7503 // once that animation is done.
7504 mAnimating = true;
7505 mHasTransformation = true;
7506 mTransformation.clear();
7507 return false;
7508 } else if (mHasTransformation) {
7509 // Little trick to get through the path below to act like
7510 // we have finished an animation.
7511 mAnimating = true;
7512 } else if (isAnimating()) {
7513 mAnimating = true;
7514 }
7515 } else if (mAnimation != null) {
7516 // If the display is frozen, and there is a pending animation,
7517 // clear it and make sure we run the cleanup code.
7518 mAnimating = true;
7519 mLocalAnimating = true;
7520 mAnimation = null;
7521 }
Romain Guy06882f82009-06-10 13:36:04 -07007522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 if (!mAnimating && !mLocalAnimating) {
7524 return false;
7525 }
7526
7527 if (DEBUG_ANIM) Log.v(
7528 TAG, "Animation done in " + this + ": exiting=" + mExiting
7529 + ", reportedVisible="
7530 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007532 mAnimating = false;
7533 mLocalAnimating = false;
7534 mAnimation = null;
7535 mAnimLayer = mLayer;
7536 if (mIsImWindow) {
7537 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007538 } else if (mIsWallpaper) {
7539 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007540 }
7541 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7542 + " anim layer: " + mAnimLayer);
7543 mHasTransformation = false;
7544 mHasLocalTransformation = false;
7545 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7546 mTransformation.clear();
7547 if (mHasDrawn
7548 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7549 && mAppToken != null
7550 && mAppToken.firstWindowDrawn
7551 && mAppToken.startingData != null) {
7552 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7553 + mToken + ": first real window done animating");
7554 mFinishedStarting.add(mAppToken);
7555 mH.sendEmptyMessage(H.FINISHED_STARTING);
7556 }
Romain Guy06882f82009-06-10 13:36:04 -07007557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 finishExit();
7559
7560 if (mAppToken != null) {
7561 mAppToken.updateReportedVisibilityLocked();
7562 }
7563
7564 return false;
7565 }
7566
7567 void finishExit() {
7568 if (DEBUG_ANIM) Log.v(
7569 TAG, "finishExit in " + this
7570 + ": exiting=" + mExiting
7571 + " remove=" + mRemoveOnExit
7572 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 final int N = mChildWindows.size();
7575 for (int i=0; i<N; i++) {
7576 ((WindowState)mChildWindows.get(i)).finishExit();
7577 }
Romain Guy06882f82009-06-10 13:36:04 -07007578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 if (!mExiting) {
7580 return;
7581 }
Romain Guy06882f82009-06-10 13:36:04 -07007582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 if (isWindowAnimating()) {
7584 return;
7585 }
7586
7587 if (localLOGV) Log.v(
7588 TAG, "Exit animation finished in " + this
7589 + ": remove=" + mRemoveOnExit);
7590 if (mSurface != null) {
7591 mDestroySurface.add(this);
7592 mDestroying = true;
7593 if (SHOW_TRANSACTIONS) Log.i(
7594 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7595 try {
7596 mSurface.hide();
7597 } catch (RuntimeException e) {
7598 Log.w(TAG, "Error hiding surface in " + this, e);
7599 }
7600 mLastHidden = true;
7601 mKeyWaiter.releasePendingPointerLocked(mSession);
7602 }
7603 mExiting = false;
7604 if (mRemoveOnExit) {
7605 mPendingRemove.add(this);
7606 mRemoveOnExit = false;
7607 }
7608 }
Romain Guy06882f82009-06-10 13:36:04 -07007609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007610 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7611 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7612 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7613 if (dtdx < -.000001f || dtdx > .000001f) return false;
7614 if (dsdy < -.000001f || dsdy > .000001f) return false;
7615 return true;
7616 }
Romain Guy06882f82009-06-10 13:36:04 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 void computeShownFrameLocked() {
7619 final boolean selfTransformation = mHasLocalTransformation;
7620 Transformation attachedTransformation =
7621 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7622 ? mAttachedWindow.mTransformation : null;
7623 Transformation appTransformation =
7624 (mAppToken != null && mAppToken.hasTransformation)
7625 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007626
7627 // Wallpapers are animated based on the "real" window they
7628 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007629 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007630 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007631 if (mWallpaperTarget.mHasLocalTransformation &&
7632 mWallpaperTarget.mAnimation != null &&
7633 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007634 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007635 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7636 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7637 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007638 }
7639 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007640 mWallpaperTarget.mAppToken.hasTransformation &&
7641 mWallpaperTarget.mAppToken.animation != null &&
7642 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007643 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007644 if (DEBUG_WALLPAPER && appTransformation != null) {
7645 Log.v(TAG, "WP target app xform: " + appTransformation);
7646 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007647 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007648 }
7649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 if (selfTransformation || attachedTransformation != null
7651 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007652 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 final Rect frame = mFrame;
7654 final float tmpFloats[] = mTmpFloats;
7655 final Matrix tmpMatrix = mTmpMatrix;
7656
7657 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007658 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007660 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007662 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007664 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 }
7666 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007667 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 }
7669
7670 // "convert" it into SurfaceFlinger's format
7671 // (a 2x2 matrix + an offset)
7672 // Here we must not transform the position of the surface
7673 // since it is already included in the transformation.
7674 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 tmpMatrix.getValues(tmpFloats);
7677 mDsDx = tmpFloats[Matrix.MSCALE_X];
7678 mDtDx = tmpFloats[Matrix.MSKEW_X];
7679 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7680 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007681 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7682 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007683 int w = frame.width();
7684 int h = frame.height();
7685 mShownFrame.set(x, y, x+w, y+h);
7686
7687 // Now set the alpha... but because our current hardware
7688 // can't do alpha transformation on a non-opaque surface,
7689 // turn it off if we are running an animation that is also
7690 // transforming since it is more important to have that
7691 // animation be smooth.
7692 mShownAlpha = mAlpha;
7693 if (!mLimitedAlphaCompositing
7694 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7695 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7696 && x == frame.left && y == frame.top))) {
7697 //Log.i(TAG, "Applying alpha transform");
7698 if (selfTransformation) {
7699 mShownAlpha *= mTransformation.getAlpha();
7700 }
7701 if (attachedTransformation != null) {
7702 mShownAlpha *= attachedTransformation.getAlpha();
7703 }
7704 if (appTransformation != null) {
7705 mShownAlpha *= appTransformation.getAlpha();
7706 }
7707 } else {
7708 //Log.i(TAG, "Not applying alpha transform");
7709 }
Romain Guy06882f82009-06-10 13:36:04 -07007710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 if (localLOGV) Log.v(
7712 TAG, "Continuing animation in " + this +
7713 ": " + mShownFrame +
7714 ", alpha=" + mTransformation.getAlpha());
7715 return;
7716 }
Romain Guy06882f82009-06-10 13:36:04 -07007717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007719 if (mXOffset != 0 || mYOffset != 0) {
7720 mShownFrame.offset(mXOffset, mYOffset);
7721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 mShownAlpha = mAlpha;
7723 mDsDx = 1;
7724 mDtDx = 0;
7725 mDsDy = 0;
7726 mDtDy = 1;
7727 }
Romain Guy06882f82009-06-10 13:36:04 -07007728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 /**
7730 * Is this window visible? It is not visible if there is no
7731 * surface, or we are in the process of running an exit animation
7732 * that will remove the surface, or its app token has been hidden.
7733 */
7734 public boolean isVisibleLw() {
7735 final AppWindowToken atoken = mAppToken;
7736 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7737 && (atoken == null || !atoken.hiddenRequested)
7738 && !mExiting && !mDestroying;
7739 }
7740
7741 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007742 * Like {@link #isVisibleLw}, but also counts a window that is currently
7743 * "hidden" behind the keyguard as visible. This allows us to apply
7744 * things like window flags that impact the keyguard.
7745 * XXX I am starting to think we need to have ANOTHER visibility flag
7746 * for this "hidden behind keyguard" state rather than overloading
7747 * mPolicyVisibility. Ungh.
7748 */
7749 public boolean isVisibleOrBehindKeyguardLw() {
7750 final AppWindowToken atoken = mAppToken;
7751 return mSurface != null && !mAttachedHidden
7752 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7753 && !mExiting && !mDestroying;
7754 }
7755
7756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 * Is this window visible, ignoring its app token? It is not visible
7758 * if there is no surface, or we are in the process of running an exit animation
7759 * that will remove the surface.
7760 */
7761 public boolean isWinVisibleLw() {
7762 final AppWindowToken atoken = mAppToken;
7763 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7764 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7765 && !mExiting && !mDestroying;
7766 }
7767
7768 /**
7769 * The same as isVisible(), but follows the current hidden state of
7770 * the associated app token, not the pending requested hidden state.
7771 */
7772 boolean isVisibleNow() {
7773 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007774 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 }
7776
7777 /**
7778 * Same as isVisible(), but we also count it as visible between the
7779 * call to IWindowSession.add() and the first relayout().
7780 */
7781 boolean isVisibleOrAdding() {
7782 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007783 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007784 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7785 && mPolicyVisibility && !mAttachedHidden
7786 && (atoken == null || !atoken.hiddenRequested)
7787 && !mExiting && !mDestroying;
7788 }
7789
7790 /**
7791 * Is this window currently on-screen? It is on-screen either if it
7792 * is visible or it is currently running an animation before no longer
7793 * being visible.
7794 */
7795 boolean isOnScreen() {
7796 final AppWindowToken atoken = mAppToken;
7797 if (atoken != null) {
7798 return mSurface != null && mPolicyVisibility && !mDestroying
7799 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007800 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 } else {
7802 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007803 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 }
7805 }
Romain Guy06882f82009-06-10 13:36:04 -07007806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 /**
7808 * Like isOnScreen(), but we don't return true if the window is part
7809 * of a transition that has not yet been started.
7810 */
7811 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007812 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007813 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007814 return false;
7815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007816 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007817 final boolean animating = atoken != null
7818 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007820 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7821 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007822 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 }
7824
7825 /** Is the window or its container currently animating? */
7826 boolean isAnimating() {
7827 final WindowState attached = mAttachedWindow;
7828 final AppWindowToken atoken = mAppToken;
7829 return mAnimation != null
7830 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007831 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 (atoken.animation != null
7833 || atoken.inPendingTransaction));
7834 }
7835
7836 /** Is this window currently animating? */
7837 boolean isWindowAnimating() {
7838 return mAnimation != null;
7839 }
7840
7841 /**
7842 * Like isOnScreen, but returns false if the surface hasn't yet
7843 * been drawn.
7844 */
7845 public boolean isDisplayedLw() {
7846 final AppWindowToken atoken = mAppToken;
7847 return mSurface != null && mPolicyVisibility && !mDestroying
7848 && !mDrawPending && !mCommitDrawPending
7849 && ((!mAttachedHidden &&
7850 (atoken == null || !atoken.hiddenRequested))
7851 || mAnimating);
7852 }
7853
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007854 /**
7855 * Returns true if the window has a surface that it has drawn a
7856 * complete UI in to.
7857 */
7858 public boolean isDrawnLw() {
7859 final AppWindowToken atoken = mAppToken;
7860 return mSurface != null && !mDestroying
7861 && !mDrawPending && !mCommitDrawPending;
7862 }
7863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7865 boolean shownFrame, boolean onlyOpaque) {
7866 if (mSurface == null) {
7867 return false;
7868 }
7869 if (mAppToken != null && !mAppToken.appFullscreen) {
7870 return false;
7871 }
7872 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7873 return false;
7874 }
7875 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007876
7877 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7878 return frame.left <= mCompatibleScreenFrame.left &&
7879 frame.top <= mCompatibleScreenFrame.top &&
7880 frame.right >= mCompatibleScreenFrame.right &&
7881 frame.bottom >= mCompatibleScreenFrame.bottom;
7882 } else {
7883 return frame.left <= 0 && frame.top <= 0
7884 && frame.right >= screenWidth
7885 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 }
Romain Guy06882f82009-06-10 13:36:04 -07007888
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007889 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007890 * Return true if the window is opaque and fully drawn. This indicates
7891 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007892 */
7893 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007894 return (mAttrs.format == PixelFormat.OPAQUE
7895 || mAttrs.type == TYPE_WALLPAPER)
7896 && mSurface != null && mAnimation == null
7897 && (mAppToken == null || mAppToken.animation == null)
7898 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007899 }
7900
7901 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7902 return
7903 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007904 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7905 // only if it's visible
7906 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007907 // and only if the application fills the compatible screen
7908 mFrame.left <= mCompatibleScreenFrame.left &&
7909 mFrame.top <= mCompatibleScreenFrame.top &&
7910 mFrame.right >= mCompatibleScreenFrame.right &&
7911 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007912 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007913 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007914 }
7915
7916 boolean isFullscreen(int screenWidth, int screenHeight) {
7917 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007918 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007919 }
7920
7921 void removeLocked() {
7922 if (mAttachedWindow != null) {
7923 mAttachedWindow.mChildWindows.remove(this);
7924 }
7925 destroySurfaceLocked();
7926 mSession.windowRemovedLocked();
7927 try {
7928 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7929 } catch (RuntimeException e) {
7930 // Ignore if it has already been removed (usually because
7931 // we are doing this as part of processing a death note.)
7932 }
7933 }
7934
7935 private class DeathRecipient implements IBinder.DeathRecipient {
7936 public void binderDied() {
7937 try {
7938 synchronized(mWindowMap) {
7939 WindowState win = windowForClientLocked(mSession, mClient);
7940 Log.i(TAG, "WIN DEATH: " + win);
7941 if (win != null) {
7942 removeWindowLocked(mSession, win);
7943 }
7944 }
7945 } catch (IllegalArgumentException ex) {
7946 // This will happen if the window has already been
7947 // removed.
7948 }
7949 }
7950 }
7951
7952 /** Returns true if this window desires key events. */
7953 public final boolean canReceiveKeys() {
7954 return isVisibleOrAdding()
7955 && (mViewVisibility == View.VISIBLE)
7956 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7957 }
7958
7959 public boolean hasDrawnLw() {
7960 return mHasDrawn;
7961 }
7962
7963 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007964 return showLw(doAnimation, true);
7965 }
7966
7967 boolean showLw(boolean doAnimation, boolean requestAnim) {
7968 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7969 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007970 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007971 mPolicyVisibility = true;
7972 mPolicyVisibilityAfterAnim = true;
7973 if (doAnimation) {
7974 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7975 }
7976 if (requestAnim) {
7977 requestAnimationLocked(0);
7978 }
7979 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007980 }
7981
7982 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007983 return hideLw(doAnimation, true);
7984 }
7985
7986 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007987 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7988 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007989 if (!current) {
7990 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007992 if (doAnimation) {
7993 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7994 if (mAnimation == null) {
7995 doAnimation = false;
7996 }
7997 }
7998 if (doAnimation) {
7999 mPolicyVisibilityAfterAnim = false;
8000 } else {
8001 mPolicyVisibilityAfterAnim = false;
8002 mPolicyVisibility = false;
8003 }
8004 if (requestAnim) {
8005 requestAnimationLocked(0);
8006 }
8007 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 }
8009
8010 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008011 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008012
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008013 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8014 pw.print(" mClient="); pw.println(mClient.asBinder());
8015 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8016 if (mAttachedWindow != null || mLayoutAttached) {
8017 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8018 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8019 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008020 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8021 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8022 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008023 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8024 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008025 }
8026 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8027 pw.print(" mSubLayer="); pw.print(mSubLayer);
8028 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8029 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8030 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8031 pw.print("="); pw.print(mAnimLayer);
8032 pw.print(" mLastLayer="); pw.println(mLastLayer);
8033 if (mSurface != null) {
8034 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8035 }
8036 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8037 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8038 if (mAppToken != null) {
8039 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8040 }
8041 if (mTargetAppToken != null) {
8042 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8043 }
8044 pw.print(prefix); pw.print("mViewVisibility=0x");
8045 pw.print(Integer.toHexString(mViewVisibility));
8046 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008047 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8048 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008049 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8050 pw.print(prefix); pw.print("mPolicyVisibility=");
8051 pw.print(mPolicyVisibility);
8052 pw.print(" mPolicyVisibilityAfterAnim=");
8053 pw.print(mPolicyVisibilityAfterAnim);
8054 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8055 }
8056 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008057 pw.print(" h="); pw.println(mRequestedHeight);
8058 if (mXOffset != 0 || mYOffset != 0) {
8059 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8060 pw.print(" y="); pw.println(mYOffset);
8061 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008062 pw.print(prefix); pw.print("mGivenContentInsets=");
8063 mGivenContentInsets.printShortString(pw);
8064 pw.print(" mGivenVisibleInsets=");
8065 mGivenVisibleInsets.printShortString(pw);
8066 pw.println();
8067 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8068 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8069 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8070 }
8071 pw.print(prefix); pw.print("mShownFrame=");
8072 mShownFrame.printShortString(pw);
8073 pw.print(" last="); mLastShownFrame.printShortString(pw);
8074 pw.println();
8075 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8076 pw.print(" last="); mLastFrame.printShortString(pw);
8077 pw.println();
8078 pw.print(prefix); pw.print("mContainingFrame=");
8079 mContainingFrame.printShortString(pw);
8080 pw.print(" mDisplayFrame=");
8081 mDisplayFrame.printShortString(pw);
8082 pw.println();
8083 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8084 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8085 pw.println();
8086 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8087 pw.print(" last="); mLastContentInsets.printShortString(pw);
8088 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8089 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8090 pw.println();
8091 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8092 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8093 pw.print(" mAlpha="); pw.print(mAlpha);
8094 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8095 }
8096 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8097 || mAnimation != null) {
8098 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8099 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8100 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8101 pw.print(" mAnimation="); pw.println(mAnimation);
8102 }
8103 if (mHasTransformation || mHasLocalTransformation) {
8104 pw.print(prefix); pw.print("XForm: has=");
8105 pw.print(mHasTransformation);
8106 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8107 pw.print(" "); mTransformation.printShortString(pw);
8108 pw.println();
8109 }
8110 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8111 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8112 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8113 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8114 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8115 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8116 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8117 pw.print(" mDestroying="); pw.print(mDestroying);
8118 pw.print(" mRemoved="); pw.println(mRemoved);
8119 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008120 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008121 pw.print(prefix); pw.print("mOrientationChanging=");
8122 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008123 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8124 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008125 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008126 if (mHScale != 1 || mVScale != 1) {
8127 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8128 pw.print(" mVScale="); pw.println(mVScale);
8129 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008130 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008131 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8132 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8133 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008134 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8135 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8136 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008138 }
8139
8140 @Override
8141 public String toString() {
8142 return "Window{"
8143 + Integer.toHexString(System.identityHashCode(this))
8144 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8145 }
8146 }
Romain Guy06882f82009-06-10 13:36:04 -07008147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008148 // -------------------------------------------------------------
8149 // Window Token State
8150 // -------------------------------------------------------------
8151
8152 class WindowToken {
8153 // The actual token.
8154 final IBinder token;
8155
8156 // The type of window this token is for, as per WindowManager.LayoutParams.
8157 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 // Set if this token was explicitly added by a client, so should
8160 // not be removed when all windows are removed.
8161 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008162
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008163 // For printing.
8164 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 // If this is an AppWindowToken, this is non-null.
8167 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 // All of the windows associated with this token.
8170 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8171
8172 // Is key dispatching paused for this token?
8173 boolean paused = false;
8174
8175 // Should this token's windows be hidden?
8176 boolean hidden;
8177
8178 // Temporary for finding which tokens no longer have visible windows.
8179 boolean hasVisible;
8180
Dianne Hackborna8f60182009-09-01 19:01:50 -07008181 // Set to true when this token is in a pending transaction where it
8182 // will be shown.
8183 boolean waitingToShow;
8184
8185 // Set to true when this token is in a pending transaction where it
8186 // will be hidden.
8187 boolean waitingToHide;
8188
8189 // Set to true when this token is in a pending transaction where its
8190 // windows will be put to the bottom of the list.
8191 boolean sendingToBottom;
8192
8193 // Set to true when this token is in a pending transaction where its
8194 // windows will be put to the top of the list.
8195 boolean sendingToTop;
8196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 WindowToken(IBinder _token, int type, boolean _explicit) {
8198 token = _token;
8199 windowType = type;
8200 explicit = _explicit;
8201 }
8202
8203 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008204 pw.print(prefix); pw.print("token="); pw.println(token);
8205 pw.print(prefix); pw.print("windows="); pw.println(windows);
8206 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8207 pw.print(" hidden="); pw.print(hidden);
8208 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008209 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8210 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8211 pw.print(" waitingToHide="); pw.print(waitingToHide);
8212 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8213 pw.print(" sendingToTop="); pw.println(sendingToTop);
8214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 }
8216
8217 @Override
8218 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008219 if (stringName == null) {
8220 StringBuilder sb = new StringBuilder();
8221 sb.append("WindowToken{");
8222 sb.append(Integer.toHexString(System.identityHashCode(this)));
8223 sb.append(" token="); sb.append(token); sb.append('}');
8224 stringName = sb.toString();
8225 }
8226 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 }
8228 };
8229
8230 class AppWindowToken extends WindowToken {
8231 // Non-null only for application tokens.
8232 final IApplicationToken appToken;
8233
8234 // All of the windows and child windows that are included in this
8235 // application token. Note this list is NOT sorted!
8236 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8237
8238 int groupId = -1;
8239 boolean appFullscreen;
8240 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 // These are used for determining when all windows associated with
8243 // an activity have been drawn, so they can be made visible together
8244 // at the same time.
8245 int lastTransactionSequence = mTransactionSequence-1;
8246 int numInterestingWindows;
8247 int numDrawnWindows;
8248 boolean inPendingTransaction;
8249 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 // Is this token going to be hidden in a little while? If so, it
8252 // won't be taken into account for setting the screen orientation.
8253 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 // Is this window's surface needed? This is almost like hidden, except
8256 // it will sometimes be true a little earlier: when the token has
8257 // been shown, but is still waiting for its app transition to execute
8258 // before making its windows shown.
8259 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 // Have we told the window clients to hide themselves?
8262 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 // Last visibility state we reported to the app token.
8265 boolean reportedVisible;
8266
8267 // Set to true when the token has been removed from the window mgr.
8268 boolean removed;
8269
8270 // Have we been asked to have this token keep the screen frozen?
8271 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 boolean animating;
8274 Animation animation;
8275 boolean hasTransformation;
8276 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278 // Offset to the window of all layers in the token, for use by
8279 // AppWindowToken animations.
8280 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 // Information about an application starting window if displayed.
8283 StartingData startingData;
8284 WindowState startingWindow;
8285 View startingView;
8286 boolean startingDisplayed;
8287 boolean startingMoved;
8288 boolean firstWindowDrawn;
8289
8290 AppWindowToken(IApplicationToken _token) {
8291 super(_token.asBinder(),
8292 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8293 appWindowToken = this;
8294 appToken = _token;
8295 }
Romain Guy06882f82009-06-10 13:36:04 -07008296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 public void setAnimation(Animation anim) {
8298 if (localLOGV) Log.v(
8299 TAG, "Setting animation in " + this + ": " + anim);
8300 animation = anim;
8301 animating = false;
8302 anim.restrictDuration(MAX_ANIMATION_DURATION);
8303 anim.scaleCurrentDuration(mTransitionAnimationScale);
8304 int zorder = anim.getZAdjustment();
8305 int adj = 0;
8306 if (zorder == Animation.ZORDER_TOP) {
8307 adj = TYPE_LAYER_OFFSET;
8308 } else if (zorder == Animation.ZORDER_BOTTOM) {
8309 adj = -TYPE_LAYER_OFFSET;
8310 }
Romain Guy06882f82009-06-10 13:36:04 -07008311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 if (animLayerAdjustment != adj) {
8313 animLayerAdjustment = adj;
8314 updateLayers();
8315 }
8316 }
Romain Guy06882f82009-06-10 13:36:04 -07008317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 public void setDummyAnimation() {
8319 if (animation == null) {
8320 if (localLOGV) Log.v(
8321 TAG, "Setting dummy animation in " + this);
8322 animation = sDummyAnimation;
8323 }
8324 }
8325
8326 public void clearAnimation() {
8327 if (animation != null) {
8328 animation = null;
8329 animating = true;
8330 }
8331 }
Romain Guy06882f82009-06-10 13:36:04 -07008332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333 void updateLayers() {
8334 final int N = allAppWindows.size();
8335 final int adj = animLayerAdjustment;
8336 for (int i=0; i<N; i++) {
8337 WindowState w = allAppWindows.get(i);
8338 w.mAnimLayer = w.mLayer + adj;
8339 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8340 + w.mAnimLayer);
8341 if (w == mInputMethodTarget) {
8342 setInputMethodAnimLayerAdjustment(adj);
8343 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008344 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008345 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008347 }
8348 }
Romain Guy06882f82009-06-10 13:36:04 -07008349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 void sendAppVisibilityToClients() {
8351 final int N = allAppWindows.size();
8352 for (int i=0; i<N; i++) {
8353 WindowState win = allAppWindows.get(i);
8354 if (win == startingWindow && clientHidden) {
8355 // Don't hide the starting window.
8356 continue;
8357 }
8358 try {
8359 if (DEBUG_VISIBILITY) Log.v(TAG,
8360 "Setting visibility of " + win + ": " + (!clientHidden));
8361 win.mClient.dispatchAppVisibility(!clientHidden);
8362 } catch (RemoteException e) {
8363 }
8364 }
8365 }
Romain Guy06882f82009-06-10 13:36:04 -07008366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 void showAllWindowsLocked() {
8368 final int NW = allAppWindows.size();
8369 for (int i=0; i<NW; i++) {
8370 WindowState w = allAppWindows.get(i);
8371 if (DEBUG_VISIBILITY) Log.v(TAG,
8372 "performing show on: " + w);
8373 w.performShowLocked();
8374 }
8375 }
Romain Guy06882f82009-06-10 13:36:04 -07008376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008377 // This must be called while inside a transaction.
8378 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8379 if (!mDisplayFrozen) {
8380 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008382 if (animation == sDummyAnimation) {
8383 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008384 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 // when it is really time to animate, this will be set to
8386 // a real animation and the next call will execute normally.
8387 return false;
8388 }
Romain Guy06882f82009-06-10 13:36:04 -07008389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8391 if (!animating) {
8392 if (DEBUG_ANIM) Log.v(
8393 TAG, "Starting animation in " + this +
8394 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8395 + " scale=" + mTransitionAnimationScale
8396 + " allDrawn=" + allDrawn + " animating=" + animating);
8397 animation.initialize(dw, dh, dw, dh);
8398 animation.setStartTime(currentTime);
8399 animating = true;
8400 }
8401 transformation.clear();
8402 final boolean more = animation.getTransformation(
8403 currentTime, transformation);
8404 if (DEBUG_ANIM) Log.v(
8405 TAG, "Stepped animation in " + this +
8406 ": more=" + more + ", xform=" + transformation);
8407 if (more) {
8408 // we're done!
8409 hasTransformation = true;
8410 return true;
8411 }
8412 if (DEBUG_ANIM) Log.v(
8413 TAG, "Finished animation in " + this +
8414 " @ " + currentTime);
8415 animation = null;
8416 }
8417 } else if (animation != null) {
8418 // If the display is frozen, and there is a pending animation,
8419 // clear it and make sure we run the cleanup code.
8420 animating = true;
8421 animation = null;
8422 }
8423
8424 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008426 if (!animating) {
8427 return false;
8428 }
8429
8430 clearAnimation();
8431 animating = false;
8432 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8433 moveInputMethodWindowsIfNeededLocked(true);
8434 }
Romain Guy06882f82009-06-10 13:36:04 -07008435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 if (DEBUG_ANIM) Log.v(
8437 TAG, "Animation done in " + this
8438 + ": reportedVisible=" + reportedVisible);
8439
8440 transformation.clear();
8441 if (animLayerAdjustment != 0) {
8442 animLayerAdjustment = 0;
8443 updateLayers();
8444 }
Romain Guy06882f82009-06-10 13:36:04 -07008445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 final int N = windows.size();
8447 for (int i=0; i<N; i++) {
8448 ((WindowState)windows.get(i)).finishExit();
8449 }
8450 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 return false;
8453 }
8454
8455 void updateReportedVisibilityLocked() {
8456 if (appToken == null) {
8457 return;
8458 }
Romain Guy06882f82009-06-10 13:36:04 -07008459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 int numInteresting = 0;
8461 int numVisible = 0;
8462 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8465 final int N = allAppWindows.size();
8466 for (int i=0; i<N; i++) {
8467 WindowState win = allAppWindows.get(i);
8468 if (win == startingWindow || win.mAppFreezing) {
8469 continue;
8470 }
8471 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008472 Log.v(TAG, "Win " + win + ": isDrawn="
8473 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008475 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 Log.v(TAG, "Not displayed: s=" + win.mSurface
8477 + " pv=" + win.mPolicyVisibility
8478 + " dp=" + win.mDrawPending
8479 + " cdp=" + win.mCommitDrawPending
8480 + " ah=" + win.mAttachedHidden
8481 + " th="
8482 + (win.mAppToken != null
8483 ? win.mAppToken.hiddenRequested : false)
8484 + " a=" + win.mAnimating);
8485 }
8486 }
8487 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008488 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 if (!win.isAnimating()) {
8490 numVisible++;
8491 }
8492 nowGone = false;
8493 } else if (win.isAnimating()) {
8494 nowGone = false;
8495 }
8496 }
Romain Guy06882f82009-06-10 13:36:04 -07008497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8499 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8500 + numInteresting + " visible=" + numVisible);
8501 if (nowVisible != reportedVisible) {
8502 if (DEBUG_VISIBILITY) Log.v(
8503 TAG, "Visibility changed in " + this
8504 + ": vis=" + nowVisible);
8505 reportedVisible = nowVisible;
8506 Message m = mH.obtainMessage(
8507 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8508 nowVisible ? 1 : 0,
8509 nowGone ? 1 : 0,
8510 this);
8511 mH.sendMessage(m);
8512 }
8513 }
Romain Guy06882f82009-06-10 13:36:04 -07008514
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008515 WindowState findMainWindow() {
8516 int j = windows.size();
8517 while (j > 0) {
8518 j--;
8519 WindowState win = windows.get(j);
8520 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8521 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8522 return win;
8523 }
8524 }
8525 return null;
8526 }
8527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 void dump(PrintWriter pw, String prefix) {
8529 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008530 if (appToken != null) {
8531 pw.print(prefix); pw.println("app=true");
8532 }
8533 if (allAppWindows.size() > 0) {
8534 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8535 }
8536 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008537 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008538 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8539 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8540 pw.print(" clientHidden="); pw.print(clientHidden);
8541 pw.print(" willBeHidden="); pw.print(willBeHidden);
8542 pw.print(" reportedVisible="); pw.println(reportedVisible);
8543 if (paused || freezingScreen) {
8544 pw.print(prefix); pw.print("paused="); pw.print(paused);
8545 pw.print(" freezingScreen="); pw.println(freezingScreen);
8546 }
8547 if (numInterestingWindows != 0 || numDrawnWindows != 0
8548 || inPendingTransaction || allDrawn) {
8549 pw.print(prefix); pw.print("numInterestingWindows=");
8550 pw.print(numInterestingWindows);
8551 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8552 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8553 pw.print(" allDrawn="); pw.println(allDrawn);
8554 }
8555 if (animating || animation != null) {
8556 pw.print(prefix); pw.print("animating="); pw.print(animating);
8557 pw.print(" animation="); pw.println(animation);
8558 }
8559 if (animLayerAdjustment != 0) {
8560 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8561 }
8562 if (hasTransformation) {
8563 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8564 pw.print(" transformation="); transformation.printShortString(pw);
8565 pw.println();
8566 }
8567 if (startingData != null || removed || firstWindowDrawn) {
8568 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8569 pw.print(" removed="); pw.print(removed);
8570 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8571 }
8572 if (startingWindow != null || startingView != null
8573 || startingDisplayed || startingMoved) {
8574 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8575 pw.print(" startingView="); pw.print(startingView);
8576 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8577 pw.print(" startingMoved"); pw.println(startingMoved);
8578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 }
8580
8581 @Override
8582 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008583 if (stringName == null) {
8584 StringBuilder sb = new StringBuilder();
8585 sb.append("AppWindowToken{");
8586 sb.append(Integer.toHexString(System.identityHashCode(this)));
8587 sb.append(" token="); sb.append(token); sb.append('}');
8588 stringName = sb.toString();
8589 }
8590 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008591 }
8592 }
Romain Guy06882f82009-06-10 13:36:04 -07008593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 // -------------------------------------------------------------
8595 // DummyAnimation
8596 // -------------------------------------------------------------
8597
8598 // This is an animation that does nothing: it just immediately finishes
8599 // itself every time it is called. It is used as a stub animation in cases
8600 // where we want to synchronize multiple things that may be animating.
8601 static final class DummyAnimation extends Animation {
8602 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8603 return false;
8604 }
8605 }
8606 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008608 // -------------------------------------------------------------
8609 // Async Handler
8610 // -------------------------------------------------------------
8611
8612 static final class StartingData {
8613 final String pkg;
8614 final int theme;
8615 final CharSequence nonLocalizedLabel;
8616 final int labelRes;
8617 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008619 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8620 int _labelRes, int _icon) {
8621 pkg = _pkg;
8622 theme = _theme;
8623 nonLocalizedLabel = _nonLocalizedLabel;
8624 labelRes = _labelRes;
8625 icon = _icon;
8626 }
8627 }
8628
8629 private final class H extends Handler {
8630 public static final int REPORT_FOCUS_CHANGE = 2;
8631 public static final int REPORT_LOSING_FOCUS = 3;
8632 public static final int ANIMATE = 4;
8633 public static final int ADD_STARTING = 5;
8634 public static final int REMOVE_STARTING = 6;
8635 public static final int FINISHED_STARTING = 7;
8636 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008637 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8638 public static final int HOLD_SCREEN_CHANGED = 12;
8639 public static final int APP_TRANSITION_TIMEOUT = 13;
8640 public static final int PERSIST_ANIMATION_SCALE = 14;
8641 public static final int FORCE_GC = 15;
8642 public static final int ENABLE_SCREEN = 16;
8643 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008644 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008646 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 public H() {
8649 }
Romain Guy06882f82009-06-10 13:36:04 -07008650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008651 @Override
8652 public void handleMessage(Message msg) {
8653 switch (msg.what) {
8654 case REPORT_FOCUS_CHANGE: {
8655 WindowState lastFocus;
8656 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008658 synchronized(mWindowMap) {
8659 lastFocus = mLastFocus;
8660 newFocus = mCurrentFocus;
8661 if (lastFocus == newFocus) {
8662 // Focus is not changing, so nothing to do.
8663 return;
8664 }
8665 mLastFocus = newFocus;
8666 //Log.i(TAG, "Focus moving from " + lastFocus
8667 // + " to " + newFocus);
8668 if (newFocus != null && lastFocus != null
8669 && !newFocus.isDisplayedLw()) {
8670 //Log.i(TAG, "Delaying loss of focus...");
8671 mLosingFocus.add(lastFocus);
8672 lastFocus = null;
8673 }
8674 }
8675
8676 if (lastFocus != newFocus) {
8677 //System.out.println("Changing focus from " + lastFocus
8678 // + " to " + newFocus);
8679 if (newFocus != null) {
8680 try {
8681 //Log.i(TAG, "Gaining focus: " + newFocus);
8682 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8683 } catch (RemoteException e) {
8684 // Ignore if process has died.
8685 }
8686 }
8687
8688 if (lastFocus != null) {
8689 try {
8690 //Log.i(TAG, "Losing focus: " + lastFocus);
8691 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8692 } catch (RemoteException e) {
8693 // Ignore if process has died.
8694 }
8695 }
8696 }
8697 } break;
8698
8699 case REPORT_LOSING_FOCUS: {
8700 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 synchronized(mWindowMap) {
8703 losers = mLosingFocus;
8704 mLosingFocus = new ArrayList<WindowState>();
8705 }
8706
8707 final int N = losers.size();
8708 for (int i=0; i<N; i++) {
8709 try {
8710 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8711 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8712 } catch (RemoteException e) {
8713 // Ignore if process has died.
8714 }
8715 }
8716 } break;
8717
8718 case ANIMATE: {
8719 synchronized(mWindowMap) {
8720 mAnimationPending = false;
8721 performLayoutAndPlaceSurfacesLocked();
8722 }
8723 } break;
8724
8725 case ADD_STARTING: {
8726 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8727 final StartingData sd = wtoken.startingData;
8728
8729 if (sd == null) {
8730 // Animation has been canceled... do nothing.
8731 return;
8732 }
Romain Guy06882f82009-06-10 13:36:04 -07008733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008734 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8735 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008737 View view = null;
8738 try {
8739 view = mPolicy.addStartingWindow(
8740 wtoken.token, sd.pkg,
8741 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8742 sd.icon);
8743 } catch (Exception e) {
8744 Log.w(TAG, "Exception when adding starting window", e);
8745 }
8746
8747 if (view != null) {
8748 boolean abort = false;
8749
8750 synchronized(mWindowMap) {
8751 if (wtoken.removed || wtoken.startingData == null) {
8752 // If the window was successfully added, then
8753 // we need to remove it.
8754 if (wtoken.startingWindow != null) {
8755 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8756 "Aborted starting " + wtoken
8757 + ": removed=" + wtoken.removed
8758 + " startingData=" + wtoken.startingData);
8759 wtoken.startingWindow = null;
8760 wtoken.startingData = null;
8761 abort = true;
8762 }
8763 } else {
8764 wtoken.startingView = view;
8765 }
8766 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8767 "Added starting " + wtoken
8768 + ": startingWindow="
8769 + wtoken.startingWindow + " startingView="
8770 + wtoken.startingView);
8771 }
8772
8773 if (abort) {
8774 try {
8775 mPolicy.removeStartingWindow(wtoken.token, view);
8776 } catch (Exception e) {
8777 Log.w(TAG, "Exception when removing starting window", e);
8778 }
8779 }
8780 }
8781 } break;
8782
8783 case REMOVE_STARTING: {
8784 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8785 IBinder token = null;
8786 View view = null;
8787 synchronized (mWindowMap) {
8788 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8789 + wtoken + ": startingWindow="
8790 + wtoken.startingWindow + " startingView="
8791 + wtoken.startingView);
8792 if (wtoken.startingWindow != null) {
8793 view = wtoken.startingView;
8794 token = wtoken.token;
8795 wtoken.startingData = null;
8796 wtoken.startingView = null;
8797 wtoken.startingWindow = null;
8798 }
8799 }
8800 if (view != null) {
8801 try {
8802 mPolicy.removeStartingWindow(token, view);
8803 } catch (Exception e) {
8804 Log.w(TAG, "Exception when removing starting window", e);
8805 }
8806 }
8807 } break;
8808
8809 case FINISHED_STARTING: {
8810 IBinder token = null;
8811 View view = null;
8812 while (true) {
8813 synchronized (mWindowMap) {
8814 final int N = mFinishedStarting.size();
8815 if (N <= 0) {
8816 break;
8817 }
8818 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8819
8820 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8821 "Finished starting " + wtoken
8822 + ": startingWindow=" + wtoken.startingWindow
8823 + " startingView=" + wtoken.startingView);
8824
8825 if (wtoken.startingWindow == null) {
8826 continue;
8827 }
8828
8829 view = wtoken.startingView;
8830 token = wtoken.token;
8831 wtoken.startingData = null;
8832 wtoken.startingView = null;
8833 wtoken.startingWindow = null;
8834 }
8835
8836 try {
8837 mPolicy.removeStartingWindow(token, view);
8838 } catch (Exception e) {
8839 Log.w(TAG, "Exception when removing starting window", e);
8840 }
8841 }
8842 } break;
8843
8844 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8845 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8846
8847 boolean nowVisible = msg.arg1 != 0;
8848 boolean nowGone = msg.arg2 != 0;
8849
8850 try {
8851 if (DEBUG_VISIBILITY) Log.v(
8852 TAG, "Reporting visible in " + wtoken
8853 + " visible=" + nowVisible
8854 + " gone=" + nowGone);
8855 if (nowVisible) {
8856 wtoken.appToken.windowsVisible();
8857 } else {
8858 wtoken.appToken.windowsGone();
8859 }
8860 } catch (RemoteException ex) {
8861 }
8862 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 case WINDOW_FREEZE_TIMEOUT: {
8865 synchronized (mWindowMap) {
8866 Log.w(TAG, "Window freeze timeout expired.");
8867 int i = mWindows.size();
8868 while (i > 0) {
8869 i--;
8870 WindowState w = (WindowState)mWindows.get(i);
8871 if (w.mOrientationChanging) {
8872 w.mOrientationChanging = false;
8873 Log.w(TAG, "Force clearing orientation change: " + w);
8874 }
8875 }
8876 performLayoutAndPlaceSurfacesLocked();
8877 }
8878 break;
8879 }
Romain Guy06882f82009-06-10 13:36:04 -07008880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 case HOLD_SCREEN_CHANGED: {
8882 Session oldHold;
8883 Session newHold;
8884 synchronized (mWindowMap) {
8885 oldHold = mLastReportedHold;
8886 newHold = (Session)msg.obj;
8887 mLastReportedHold = newHold;
8888 }
Romain Guy06882f82009-06-10 13:36:04 -07008889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008890 if (oldHold != newHold) {
8891 try {
8892 if (oldHold != null) {
8893 mBatteryStats.noteStopWakelock(oldHold.mUid,
8894 "window",
8895 BatteryStats.WAKE_TYPE_WINDOW);
8896 }
8897 if (newHold != null) {
8898 mBatteryStats.noteStartWakelock(newHold.mUid,
8899 "window",
8900 BatteryStats.WAKE_TYPE_WINDOW);
8901 }
8902 } catch (RemoteException e) {
8903 }
8904 }
8905 break;
8906 }
Romain Guy06882f82009-06-10 13:36:04 -07008907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 case APP_TRANSITION_TIMEOUT: {
8909 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008910 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008911 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8912 "*** APP TRANSITION TIMEOUT");
8913 mAppTransitionReady = true;
8914 mAppTransitionTimeout = true;
8915 performLayoutAndPlaceSurfacesLocked();
8916 }
8917 }
8918 break;
8919 }
Romain Guy06882f82009-06-10 13:36:04 -07008920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 case PERSIST_ANIMATION_SCALE: {
8922 Settings.System.putFloat(mContext.getContentResolver(),
8923 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8924 Settings.System.putFloat(mContext.getContentResolver(),
8925 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8926 break;
8927 }
Romain Guy06882f82009-06-10 13:36:04 -07008928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008929 case FORCE_GC: {
8930 synchronized(mWindowMap) {
8931 if (mAnimationPending) {
8932 // If we are animating, don't do the gc now but
8933 // delay a bit so we don't interrupt the animation.
8934 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8935 2000);
8936 return;
8937 }
8938 // If we are currently rotating the display, it will
8939 // schedule a new message when done.
8940 if (mDisplayFrozen) {
8941 return;
8942 }
8943 mFreezeGcPending = 0;
8944 }
8945 Runtime.getRuntime().gc();
8946 break;
8947 }
Romain Guy06882f82009-06-10 13:36:04 -07008948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008949 case ENABLE_SCREEN: {
8950 performEnableScreen();
8951 break;
8952 }
Romain Guy06882f82009-06-10 13:36:04 -07008953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 case APP_FREEZE_TIMEOUT: {
8955 synchronized (mWindowMap) {
8956 Log.w(TAG, "App freeze timeout expired.");
8957 int i = mAppTokens.size();
8958 while (i > 0) {
8959 i--;
8960 AppWindowToken tok = mAppTokens.get(i);
8961 if (tok.freezingScreen) {
8962 Log.w(TAG, "Force clearing freeze: " + tok);
8963 unsetAppFreezingScreenLocked(tok, true, true);
8964 }
8965 }
8966 }
8967 break;
8968 }
Romain Guy06882f82009-06-10 13:36:04 -07008969
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008970 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008971 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008972 sendNewConfiguration();
8973 }
8974 break;
8975 }
Romain Guy06882f82009-06-10 13:36:04 -07008976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 }
8978 }
8979 }
8980
8981 // -------------------------------------------------------------
8982 // IWindowManager API
8983 // -------------------------------------------------------------
8984
8985 public IWindowSession openSession(IInputMethodClient client,
8986 IInputContext inputContext) {
8987 if (client == null) throw new IllegalArgumentException("null client");
8988 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8989 return new Session(client, inputContext);
8990 }
8991
8992 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8993 synchronized (mWindowMap) {
8994 // The focus for the client is the window immediately below
8995 // where we would place the input method window.
8996 int idx = findDesiredInputMethodWindowIndexLocked(false);
8997 WindowState imFocus;
8998 if (idx > 0) {
8999 imFocus = (WindowState)mWindows.get(idx-1);
9000 if (imFocus != null) {
9001 if (imFocus.mSession.mClient != null &&
9002 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9003 return true;
9004 }
9005 }
9006 }
9007 }
9008 return false;
9009 }
Romain Guy06882f82009-06-10 13:36:04 -07009010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011 // -------------------------------------------------------------
9012 // Internals
9013 // -------------------------------------------------------------
9014
9015 final WindowState windowForClientLocked(Session session, IWindow client) {
9016 return windowForClientLocked(session, client.asBinder());
9017 }
Romain Guy06882f82009-06-10 13:36:04 -07009018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 final WindowState windowForClientLocked(Session session, IBinder client) {
9020 WindowState win = mWindowMap.get(client);
9021 if (localLOGV) Log.v(
9022 TAG, "Looking up client " + client + ": " + win);
9023 if (win == null) {
9024 RuntimeException ex = new RuntimeException();
9025 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9026 return null;
9027 }
9028 if (session != null && win.mSession != session) {
9029 RuntimeException ex = new RuntimeException();
9030 Log.w(TAG, "Requested window " + client + " is in session " +
9031 win.mSession + ", not " + session, ex);
9032 return null;
9033 }
9034
9035 return win;
9036 }
9037
Dianne Hackborna8f60182009-09-01 19:01:50 -07009038 final void rebuildAppWindowListLocked() {
9039 int NW = mWindows.size();
9040 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009041 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009042 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009043
9044 // First remove all existing app windows.
9045 i=0;
9046 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009047 WindowState w = (WindowState)mWindows.get(i);
9048 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009049 WindowState win = (WindowState)mWindows.remove(i);
9050 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9051 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009052 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009053 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009054 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009055 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9056 && lastWallpaper == i-1) {
9057 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009058 }
9059 i++;
9060 }
9061
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009062 // The wallpaper window(s) typically live at the bottom of the stack,
9063 // so skip them before adding app tokens.
9064 lastWallpaper++;
9065 i = lastWallpaper;
9066
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009067 // First add all of the exiting app tokens... these are no longer
9068 // in the main app list, but still have windows shown. We put them
9069 // in the back because now that the animation is over we no longer
9070 // will care about them.
9071 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009072 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009073 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9074 }
9075
9076 // And add in the still active app tokens in Z order.
9077 NT = mAppTokens.size();
9078 for (int j=0; j<NT; j++) {
9079 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009080 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009081
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009082 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009083 if (i != numRemoved) {
9084 Log.w(TAG, "Rebuild removed " + numRemoved
9085 + " windows but added " + i);
9086 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009087 }
9088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009089 private final void assignLayersLocked() {
9090 int N = mWindows.size();
9091 int curBaseLayer = 0;
9092 int curLayer = 0;
9093 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 for (i=0; i<N; i++) {
9096 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009097 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9098 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 curLayer += WINDOW_LAYER_MULTIPLIER;
9100 w.mLayer = curLayer;
9101 } else {
9102 curBaseLayer = curLayer = w.mBaseLayer;
9103 w.mLayer = curLayer;
9104 }
9105 if (w.mTargetAppToken != null) {
9106 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9107 } else if (w.mAppToken != null) {
9108 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9109 } else {
9110 w.mAnimLayer = w.mLayer;
9111 }
9112 if (w.mIsImWindow) {
9113 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009114 } else if (w.mIsWallpaper) {
9115 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009116 }
9117 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9118 + w.mAnimLayer);
9119 //System.out.println(
9120 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9121 }
9122 }
9123
9124 private boolean mInLayout = false;
9125 private final void performLayoutAndPlaceSurfacesLocked() {
9126 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009127 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009128 throw new RuntimeException("Recursive call!");
9129 }
9130 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9131 return;
9132 }
9133
9134 boolean recoveringMemory = false;
9135 if (mForceRemoves != null) {
9136 recoveringMemory = true;
9137 // Wait a little it for things to settle down, and off we go.
9138 for (int i=0; i<mForceRemoves.size(); i++) {
9139 WindowState ws = mForceRemoves.get(i);
9140 Log.i(TAG, "Force removing: " + ws);
9141 removeWindowInnerLocked(ws.mSession, ws);
9142 }
9143 mForceRemoves = null;
9144 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9145 Object tmp = new Object();
9146 synchronized (tmp) {
9147 try {
9148 tmp.wait(250);
9149 } catch (InterruptedException e) {
9150 }
9151 }
9152 }
Romain Guy06882f82009-06-10 13:36:04 -07009153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009154 mInLayout = true;
9155 try {
9156 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009158 int i = mPendingRemove.size()-1;
9159 if (i >= 0) {
9160 while (i >= 0) {
9161 WindowState w = mPendingRemove.get(i);
9162 removeWindowInnerLocked(w.mSession, w);
9163 i--;
9164 }
9165 mPendingRemove.clear();
9166
9167 mInLayout = false;
9168 assignLayersLocked();
9169 mLayoutNeeded = true;
9170 performLayoutAndPlaceSurfacesLocked();
9171
9172 } else {
9173 mInLayout = false;
9174 if (mLayoutNeeded) {
9175 requestAnimationLocked(0);
9176 }
9177 }
9178 } catch (RuntimeException e) {
9179 mInLayout = false;
9180 Log.e(TAG, "Unhandled exception while layout out windows", e);
9181 }
9182 }
9183
9184 private final void performLayoutLockedInner() {
9185 final int dw = mDisplay.getWidth();
9186 final int dh = mDisplay.getHeight();
9187
9188 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009189 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009190 int i;
9191
9192 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009193
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009194 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009195 mPolicy.beginLayoutLw(dw, dh);
9196
9197 // First perform layout of any root windows (not attached
9198 // to another window).
9199 int topAttached = -1;
9200 for (i = N-1; i >= 0; i--) {
9201 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009202
9203 // Don't do layout of a window if it is not visible, or
9204 // soon won't be visible, to avoid wasting time and funky
9205 // changes while a window is animating away.
9206 final AppWindowToken atoken = win.mAppToken;
9207 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009208 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009209 || win.mRootToken.hidden
9210 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009211 || win.mAttachedHidden
9212 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009213
9214 // If this view is GONE, then skip it -- keep the current
9215 // frame, and let the caller know so they can ignore it
9216 // if they want. (We do the normal layout for INVISIBLE
9217 // windows, since that means "perform layout as normal,
9218 // just don't display").
9219 if (!gone || !win.mHaveFrame) {
9220 if (!win.mLayoutAttached) {
9221 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9222 } else {
9223 if (topAttached < 0) topAttached = i;
9224 }
9225 }
9226 }
Romain Guy06882f82009-06-10 13:36:04 -07009227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 // Now perform layout of attached windows, which usually
9229 // depend on the position of the window they are attached to.
9230 // XXX does not deal with windows that are attached to windows
9231 // that are themselves attached.
9232 for (i = topAttached; i >= 0; i--) {
9233 WindowState win = (WindowState) mWindows.get(i);
9234
9235 // If this view is GONE, then skip it -- keep the current
9236 // frame, and let the caller know so they can ignore it
9237 // if they want. (We do the normal layout for INVISIBLE
9238 // windows, since that means "perform layout as normal,
9239 // just don't display").
9240 if (win.mLayoutAttached) {
9241 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9242 || !win.mHaveFrame) {
9243 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9244 }
9245 }
9246 }
9247
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009248 int changes = mPolicy.finishLayoutLw();
9249 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9250 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9251 assignLayersLocked();
9252 }
9253 }
9254 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009255 mLayoutNeeded = false;
9256 } else if (repeats > 2) {
9257 Log.w(TAG, "Layout repeat aborted after too many iterations");
9258 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009259 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9260 Configuration newConfig = updateOrientationFromAppTokensLocked(
9261 null, null);
9262 if (newConfig != null) {
9263 mLayoutNeeded = true;
9264 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9265 }
9266 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009267 } else {
9268 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009269 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9270 Configuration newConfig = updateOrientationFromAppTokensLocked(
9271 null, null);
9272 if (newConfig != null) {
9273 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9274 }
9275 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009277 }
9278 }
Romain Guy06882f82009-06-10 13:36:04 -07009279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 private final void performLayoutAndPlaceSurfacesLockedInner(
9281 boolean recoveringMemory) {
9282 final long currentTime = SystemClock.uptimeMillis();
9283 final int dw = mDisplay.getWidth();
9284 final int dh = mDisplay.getHeight();
9285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 int i;
9287
9288 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009291 if (mFxSession == null) {
9292 mFxSession = new SurfaceSession();
9293 }
Romain Guy06882f82009-06-10 13:36:04 -07009294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9296
9297 // Initialize state of exiting tokens.
9298 for (i=mExitingTokens.size()-1; i>=0; i--) {
9299 mExitingTokens.get(i).hasVisible = false;
9300 }
9301
9302 // Initialize state of exiting applications.
9303 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9304 mExitingAppTokens.get(i).hasVisible = false;
9305 }
9306
9307 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009308 boolean orientationChangeComplete = true;
9309 Session holdScreen = null;
9310 float screenBrightness = -1;
9311 boolean focusDisplayed = false;
9312 boolean animating = false;
9313
9314 Surface.openTransaction();
9315 try {
9316 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009317 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318
9319 do {
9320 final int transactionSequence = ++mTransactionSequence;
9321
9322 // Update animations of all applications, including those
9323 // associated with exiting/removed apps
9324 boolean tokensAnimating = false;
9325 final int NAT = mAppTokens.size();
9326 for (i=0; i<NAT; i++) {
9327 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9328 tokensAnimating = true;
9329 }
9330 }
9331 final int NEAT = mExitingAppTokens.size();
9332 for (i=0; i<NEAT; i++) {
9333 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9334 tokensAnimating = true;
9335 }
9336 }
9337
9338 animating = tokensAnimating;
9339 restart = false;
9340
9341 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009342 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009343 boolean focusMayChange = false;
9344 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345
9346 mPolicy.beginAnimationLw(dw, dh);
9347
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009348 final int N = mWindows.size();
9349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009350 for (i=N-1; i>=0; i--) {
9351 WindowState w = (WindowState)mWindows.get(i);
9352
9353 final WindowManager.LayoutParams attrs = w.mAttrs;
9354
9355 if (w.mSurface != null) {
9356 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009357 if (w.commitFinishDrawingLocked(currentTime)) {
9358 if ((w.mAttrs.flags
9359 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009360 if (DEBUG_WALLPAPER) Log.v(TAG,
9361 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009362 wallpaperMayChange = true;
9363 }
9364 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009365
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009366 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009367 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9368 animating = true;
9369 //w.dump(" ");
9370 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009371 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9372 wallpaperMayChange = true;
9373 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009374
9375 if (mPolicy.doesForceHide(w, attrs)) {
9376 if (!wasAnimating && animating) {
9377 wallpaperForceHidingChanged = true;
9378 focusMayChange = true;
9379 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9380 forceHiding = true;
9381 }
9382 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9383 boolean changed;
9384 if (forceHiding) {
9385 changed = w.hideLw(false, false);
9386 } else {
9387 changed = w.showLw(false, false);
9388 if (changed && wallpaperForceHidingChanged
9389 && w.isReadyForDisplay()) {
9390 // Assume we will need to animate. If
9391 // we don't (because the wallpaper will
9392 // stay with the lock screen), then we will
9393 // clean up later.
9394 Animation a = mPolicy.createForceHideEnterAnimation();
9395 if (a != null) {
9396 w.setAnimation(a);
9397 }
9398 }
9399 }
9400 if (changed && (attrs.flags
9401 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9402 wallpaperMayChange = true;
9403 }
Christopher Tate405d0892009-10-27 20:23:28 -07009404 if (changed && !forceHiding
9405 && (mCurrentFocus == null)
9406 && (mFocusedApp != null)) {
9407 // It's possible that the last focus recalculation left no
9408 // current focused window even though the app has come to the
9409 // foreground already. In this case, we make sure to recalculate
9410 // focus when we show a window.
9411 focusMayChange = true;
9412 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009413 }
9414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 mPolicy.animatingWindowLw(w, attrs);
9416 }
9417
9418 final AppWindowToken atoken = w.mAppToken;
9419 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9420 if (atoken.lastTransactionSequence != transactionSequence) {
9421 atoken.lastTransactionSequence = transactionSequence;
9422 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9423 atoken.startingDisplayed = false;
9424 }
9425 if ((w.isOnScreen() || w.mAttrs.type
9426 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9427 && !w.mExiting && !w.mDestroying) {
9428 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009429 Log.v(TAG, "Eval win " + w + ": isDrawn="
9430 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009432 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009433 Log.v(TAG, "Not displayed: s=" + w.mSurface
9434 + " pv=" + w.mPolicyVisibility
9435 + " dp=" + w.mDrawPending
9436 + " cdp=" + w.mCommitDrawPending
9437 + " ah=" + w.mAttachedHidden
9438 + " th=" + atoken.hiddenRequested
9439 + " a=" + w.mAnimating);
9440 }
9441 }
9442 if (w != atoken.startingWindow) {
9443 if (!atoken.freezingScreen || !w.mAppFreezing) {
9444 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009445 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 atoken.numDrawnWindows++;
9447 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9448 "tokenMayBeDrawn: " + atoken
9449 + " freezingScreen=" + atoken.freezingScreen
9450 + " mAppFreezing=" + w.mAppFreezing);
9451 tokenMayBeDrawn = true;
9452 }
9453 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009454 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 atoken.startingDisplayed = true;
9456 }
9457 }
9458 } else if (w.mReadyToShow) {
9459 w.performShowLocked();
9460 }
9461 }
9462
9463 if (mPolicy.finishAnimationLw()) {
9464 restart = true;
9465 }
9466
9467 if (tokenMayBeDrawn) {
9468 // See if any windows have been drawn, so they (and others
9469 // associated with them) can now be shown.
9470 final int NT = mTokenList.size();
9471 for (i=0; i<NT; i++) {
9472 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9473 if (wtoken == null) {
9474 continue;
9475 }
9476 if (wtoken.freezingScreen) {
9477 int numInteresting = wtoken.numInterestingWindows;
9478 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9479 if (DEBUG_VISIBILITY) Log.v(TAG,
9480 "allDrawn: " + wtoken
9481 + " interesting=" + numInteresting
9482 + " drawn=" + wtoken.numDrawnWindows);
9483 wtoken.showAllWindowsLocked();
9484 unsetAppFreezingScreenLocked(wtoken, false, true);
9485 orientationChangeComplete = true;
9486 }
9487 } else if (!wtoken.allDrawn) {
9488 int numInteresting = wtoken.numInterestingWindows;
9489 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9490 if (DEBUG_VISIBILITY) Log.v(TAG,
9491 "allDrawn: " + wtoken
9492 + " interesting=" + numInteresting
9493 + " drawn=" + wtoken.numDrawnWindows);
9494 wtoken.allDrawn = true;
9495 restart = true;
9496
9497 // We can now show all of the drawn windows!
9498 if (!mOpeningApps.contains(wtoken)) {
9499 wtoken.showAllWindowsLocked();
9500 }
9501 }
9502 }
9503 }
9504 }
9505
9506 // If we are ready to perform an app transition, check through
9507 // all of the app tokens to be shown and see if they are ready
9508 // to go.
9509 if (mAppTransitionReady) {
9510 int NN = mOpeningApps.size();
9511 boolean goodToGo = true;
9512 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9513 "Checking " + NN + " opening apps (frozen="
9514 + mDisplayFrozen + " timeout="
9515 + mAppTransitionTimeout + ")...");
9516 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9517 // If the display isn't frozen, wait to do anything until
9518 // all of the apps are ready. Otherwise just go because
9519 // we'll unfreeze the display when everyone is ready.
9520 for (i=0; i<NN && goodToGo; i++) {
9521 AppWindowToken wtoken = mOpeningApps.get(i);
9522 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9523 "Check opening app" + wtoken + ": allDrawn="
9524 + wtoken.allDrawn + " startingDisplayed="
9525 + wtoken.startingDisplayed);
9526 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9527 && !wtoken.startingMoved) {
9528 goodToGo = false;
9529 }
9530 }
9531 }
9532 if (goodToGo) {
9533 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9534 int transit = mNextAppTransition;
9535 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009536 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009538 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009540 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541 mAppTransitionTimeout = false;
9542 mStartingIconInTransition = false;
9543 mSkipAppTransitionAnimation = false;
9544
9545 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9546
Dianne Hackborna8f60182009-09-01 19:01:50 -07009547 // If there are applications waiting to come to the
9548 // top of the stack, now is the time to move their windows.
9549 // (Note that we don't do apps going to the bottom
9550 // here -- we want to keep their windows in the old
9551 // Z-order until the animation completes.)
9552 if (mToTopApps.size() > 0) {
9553 NN = mAppTokens.size();
9554 for (i=0; i<NN; i++) {
9555 AppWindowToken wtoken = mAppTokens.get(i);
9556 if (wtoken.sendingToTop) {
9557 wtoken.sendingToTop = false;
9558 moveAppWindowsLocked(wtoken, NN, false);
9559 }
9560 }
9561 mToTopApps.clear();
9562 }
9563
Dianne Hackborn25994b42009-09-04 14:21:19 -07009564 WindowState oldWallpaper = mWallpaperTarget;
9565
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009566 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009567 wallpaperMayChange = false;
9568
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009569 // The top-most window will supply the layout params,
9570 // and we will determine it below.
9571 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009572 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009573 int bestAnimLayer = -1;
9574
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009575 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009576 "New wallpaper target=" + mWallpaperTarget
9577 + ", lower target=" + mLowerWallpaperTarget
9578 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009579 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009580 // Do a first pass through the tokens for two
9581 // things:
9582 // (1) Determine if both the closing and opening
9583 // app token sets are wallpaper targets, in which
9584 // case special animations are needed
9585 // (since the wallpaper needs to stay static
9586 // behind them).
9587 // (2) Find the layout params of the top-most
9588 // application window in the tokens, which is
9589 // what will control the animation theme.
9590 final int NC = mClosingApps.size();
9591 NN = NC + mOpeningApps.size();
9592 for (i=0; i<NN; i++) {
9593 AppWindowToken wtoken;
9594 int mode;
9595 if (i < NC) {
9596 wtoken = mClosingApps.get(i);
9597 mode = 1;
9598 } else {
9599 wtoken = mOpeningApps.get(i-NC);
9600 mode = 2;
9601 }
9602 if (mLowerWallpaperTarget != null) {
9603 if (mLowerWallpaperTarget.mAppToken == wtoken
9604 || mUpperWallpaperTarget.mAppToken == wtoken) {
9605 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009606 }
9607 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009608 if (wtoken.appFullscreen) {
9609 WindowState ws = wtoken.findMainWindow();
9610 if (ws != null) {
9611 // If this is a compatibility mode
9612 // window, we will always use its anim.
9613 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9614 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009615 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009616 bestAnimLayer = Integer.MAX_VALUE;
9617 } else if (ws.mLayer > bestAnimLayer) {
9618 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009619 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009620 bestAnimLayer = ws.mLayer;
9621 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009622 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009623 }
9624 }
9625
Dianne Hackborn25994b42009-09-04 14:21:19 -07009626 if (foundWallpapers == 3) {
9627 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9628 "Wallpaper animation!");
9629 switch (transit) {
9630 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9631 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9632 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9633 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9634 break;
9635 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9636 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9637 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9638 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9639 break;
9640 }
9641 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9642 "New transit: " + transit);
9643 } else if (oldWallpaper != null) {
9644 // We are transitioning from an activity with
9645 // a wallpaper to one without.
9646 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9647 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9648 "New transit away from wallpaper: " + transit);
9649 } else if (mWallpaperTarget != null) {
9650 // We are transitioning from an activity without
9651 // a wallpaper to now showing the wallpaper
9652 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9653 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9654 "New transit into wallpaper: " + transit);
9655 }
9656
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009657 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9658 mLastEnterAnimToken = animToken;
9659 mLastEnterAnimParams = animLp;
9660 } else if (mLastEnterAnimParams != null) {
9661 animLp = mLastEnterAnimParams;
9662 mLastEnterAnimToken = null;
9663 mLastEnterAnimParams = null;
9664 }
9665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009666 NN = mOpeningApps.size();
9667 for (i=0; i<NN; i++) {
9668 AppWindowToken wtoken = mOpeningApps.get(i);
9669 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9670 "Now opening app" + wtoken);
9671 wtoken.reportedVisible = false;
9672 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009673 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009674 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009675 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009676 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677 wtoken.showAllWindowsLocked();
9678 }
9679 NN = mClosingApps.size();
9680 for (i=0; i<NN; i++) {
9681 AppWindowToken wtoken = mClosingApps.get(i);
9682 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9683 "Now closing app" + wtoken);
9684 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009685 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009686 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009688 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 // Force the allDrawn flag, because we want to start
9690 // this guy's animations regardless of whether it's
9691 // gotten drawn.
9692 wtoken.allDrawn = true;
9693 }
9694
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009695 mNextAppTransitionPackage = null;
9696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009697 mOpeningApps.clear();
9698 mClosingApps.clear();
9699
9700 // This has changed the visibility of windows, so perform
9701 // a new layout to get them all up-to-date.
9702 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009703 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9704 assignLayersLocked();
9705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 performLayoutLockedInner();
9707 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009708 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709
9710 restart = true;
9711 }
9712 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009713
Dianne Hackborna8f60182009-09-01 19:01:50 -07009714 if (!animating && mAppTransitionRunning) {
9715 // We have finished the animation of an app transition. To do
9716 // this, we have delayed a lot of operations like showing and
9717 // hiding apps, moving apps in Z-order, etc. The app token list
9718 // reflects the correct Z-order, but the window list may now
9719 // be out of sync with it. So here we will just rebuild the
9720 // entire app window list. Fun!
9721 mAppTransitionRunning = false;
9722 // Clear information about apps that were moving.
9723 mToBottomApps.clear();
9724
9725 rebuildAppWindowListLocked();
9726 restart = true;
9727 moveInputMethodWindowsIfNeededLocked(false);
9728 wallpaperMayChange = true;
9729 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009730 // Since the window list has been rebuilt, focus might
9731 // have to be recomputed since the actual order of windows
9732 // might have changed again.
9733 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009734 }
9735
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009736 int adjResult = 0;
9737
9738 if (wallpaperForceHidingChanged) {
9739 // At this point, there was a window with a wallpaper that
9740 // was force hiding other windows behind it, but now it
9741 // is going away. This may be simple -- just animate
9742 // away the wallpaper and its window -- or it may be
9743 // hard -- the wallpaper now needs to be shown behind
9744 // something that was hidden.
9745 WindowState oldWallpaper = mWallpaperTarget;
9746 adjResult = adjustWallpaperWindowsLocked();
9747 wallpaperMayChange = false;
9748 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9749 + " NEW: " + mWallpaperTarget);
9750 if (mLowerWallpaperTarget == null) {
9751 // Whoops, we don't need a special wallpaper animation.
9752 // Clear them out.
9753 forceHiding = false;
9754 for (i=N-1; i>=0; i--) {
9755 WindowState w = (WindowState)mWindows.get(i);
9756 if (w.mSurface != null) {
9757 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009758 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9759 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009760 forceHiding = true;
9761 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9762 if (!w.mAnimating) {
9763 // We set the animation above so it
9764 // is not yet running.
9765 w.clearAnimation();
9766 }
9767 }
9768 }
9769 }
9770 }
9771 }
9772
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009773 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009774 if (DEBUG_WALLPAPER) Log.v(TAG,
9775 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009776 adjResult = adjustWallpaperWindowsLocked();
9777 }
9778
9779 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9780 if (DEBUG_WALLPAPER) Log.v(TAG,
9781 "Wallpaper layer changed: assigning layers + relayout");
9782 restart = true;
9783 mLayoutNeeded = true;
9784 assignLayersLocked();
9785 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9786 if (DEBUG_WALLPAPER) Log.v(TAG,
9787 "Wallpaper visibility changed: relayout");
9788 restart = true;
9789 mLayoutNeeded = true;
9790 }
9791
9792 if (focusMayChange) {
9793 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009794 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009795 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009796 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009797 }
9798
9799 if (mLayoutNeeded) {
9800 restart = true;
9801 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009802 }
9803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009804 } while (restart);
9805
9806 // THIRD LOOP: Update the surfaces of all windows.
9807
9808 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9809
9810 boolean obscured = false;
9811 boolean blurring = false;
9812 boolean dimming = false;
9813 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009814 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009815 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009817 final int N = mWindows.size();
9818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 for (i=N-1; i>=0; i--) {
9820 WindowState w = (WindowState)mWindows.get(i);
9821
9822 boolean displayed = false;
9823 final WindowManager.LayoutParams attrs = w.mAttrs;
9824 final int attrFlags = attrs.flags;
9825
9826 if (w.mSurface != null) {
9827 w.computeShownFrameLocked();
9828 if (localLOGV) Log.v(
9829 TAG, "Placing surface #" + i + " " + w.mSurface
9830 + ": new=" + w.mShownFrame + ", old="
9831 + w.mLastShownFrame);
9832
9833 boolean resize;
9834 int width, height;
9835 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9836 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9837 w.mLastRequestedHeight != w.mRequestedHeight;
9838 // for a scaled surface, we just want to use
9839 // the requested size.
9840 width = w.mRequestedWidth;
9841 height = w.mRequestedHeight;
9842 w.mLastRequestedWidth = width;
9843 w.mLastRequestedHeight = height;
9844 w.mLastShownFrame.set(w.mShownFrame);
9845 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009846 if (SHOW_TRANSACTIONS) Log.i(
9847 TAG, " SURFACE " + w.mSurface
9848 + ": POS " + w.mShownFrame.left
9849 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9851 } catch (RuntimeException e) {
9852 Log.w(TAG, "Error positioning surface in " + w, e);
9853 if (!recoveringMemory) {
9854 reclaimSomeSurfaceMemoryLocked(w, "position");
9855 }
9856 }
9857 } else {
9858 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9859 width = w.mShownFrame.width();
9860 height = w.mShownFrame.height();
9861 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 }
9863
9864 if (resize) {
9865 if (width < 1) width = 1;
9866 if (height < 1) height = 1;
9867 if (w.mSurface != null) {
9868 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009869 if (SHOW_TRANSACTIONS) Log.i(
9870 TAG, " SURFACE " + w.mSurface + ": POS "
9871 + w.mShownFrame.left + ","
9872 + w.mShownFrame.top + " SIZE "
9873 + w.mShownFrame.width() + "x"
9874 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 w.mSurface.setSize(width, height);
9876 w.mSurface.setPosition(w.mShownFrame.left,
9877 w.mShownFrame.top);
9878 } catch (RuntimeException e) {
9879 // If something goes wrong with the surface (such
9880 // as running out of memory), don't take down the
9881 // entire system.
9882 Log.e(TAG, "Failure updating surface of " + w
9883 + "size=(" + width + "x" + height
9884 + "), pos=(" + w.mShownFrame.left
9885 + "," + w.mShownFrame.top + ")", e);
9886 if (!recoveringMemory) {
9887 reclaimSomeSurfaceMemoryLocked(w, "size");
9888 }
9889 }
9890 }
9891 }
9892 if (!w.mAppFreezing) {
9893 w.mContentInsetsChanged =
9894 !w.mLastContentInsets.equals(w.mContentInsets);
9895 w.mVisibleInsetsChanged =
9896 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009897 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 || w.mContentInsetsChanged
9899 || w.mVisibleInsetsChanged) {
9900 w.mLastFrame.set(w.mFrame);
9901 w.mLastContentInsets.set(w.mContentInsets);
9902 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009903 // If the screen is currently frozen, then keep
9904 // it frozen until this window draws at its new
9905 // orientation.
9906 if (mDisplayFrozen) {
9907 if (DEBUG_ORIENTATION) Log.v(TAG,
9908 "Resizing while display frozen: " + w);
9909 w.mOrientationChanging = true;
9910 if (mWindowsFreezingScreen) {
9911 mWindowsFreezingScreen = true;
9912 // XXX should probably keep timeout from
9913 // when we first froze the display.
9914 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9915 mH.sendMessageDelayed(mH.obtainMessage(
9916 H.WINDOW_FREEZE_TIMEOUT), 2000);
9917 }
9918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 // If the orientation is changing, then we need to
9920 // hold off on unfreezing the display until this
9921 // window has been redrawn; to do that, we need
9922 // to go through the process of getting informed
9923 // by the application when it has finished drawing.
9924 if (w.mOrientationChanging) {
9925 if (DEBUG_ORIENTATION) Log.v(TAG,
9926 "Orientation start waiting for draw in "
9927 + w + ", surface " + w.mSurface);
9928 w.mDrawPending = true;
9929 w.mCommitDrawPending = false;
9930 w.mReadyToShow = false;
9931 if (w.mAppToken != null) {
9932 w.mAppToken.allDrawn = false;
9933 }
9934 }
Romain Guy06882f82009-06-10 13:36:04 -07009935 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 "Resizing window " + w + " to " + w.mFrame);
9937 mResizingWindows.add(w);
9938 } else if (w.mOrientationChanging) {
9939 if (!w.mDrawPending && !w.mCommitDrawPending) {
9940 if (DEBUG_ORIENTATION) Log.v(TAG,
9941 "Orientation not waiting for draw in "
9942 + w + ", surface " + w.mSurface);
9943 w.mOrientationChanging = false;
9944 }
9945 }
9946 }
9947
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009948 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 if (!w.mLastHidden) {
9950 //dump();
9951 w.mLastHidden = true;
9952 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009953 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 if (w.mSurface != null) {
9955 try {
9956 w.mSurface.hide();
9957 } catch (RuntimeException e) {
9958 Log.w(TAG, "Exception hiding surface in " + w);
9959 }
9960 }
9961 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9962 }
9963 // If we are waiting for this window to handle an
9964 // orientation change, well, it is hidden, so
9965 // doesn't really matter. Note that this does
9966 // introduce a potential glitch if the window
9967 // becomes unhidden before it has drawn for the
9968 // new orientation.
9969 if (w.mOrientationChanging) {
9970 w.mOrientationChanging = false;
9971 if (DEBUG_ORIENTATION) Log.v(TAG,
9972 "Orientation change skips hidden " + w);
9973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 } else if (w.mLastLayer != w.mAnimLayer
9975 || w.mLastAlpha != w.mShownAlpha
9976 || w.mLastDsDx != w.mDsDx
9977 || w.mLastDtDx != w.mDtDx
9978 || w.mLastDsDy != w.mDsDy
9979 || w.mLastDtDy != w.mDtDy
9980 || w.mLastHScale != w.mHScale
9981 || w.mLastVScale != w.mVScale
9982 || w.mLastHidden) {
9983 displayed = true;
9984 w.mLastAlpha = w.mShownAlpha;
9985 w.mLastLayer = w.mAnimLayer;
9986 w.mLastDsDx = w.mDsDx;
9987 w.mLastDtDx = w.mDtDx;
9988 w.mLastDsDy = w.mDsDy;
9989 w.mLastDtDy = w.mDtDy;
9990 w.mLastHScale = w.mHScale;
9991 w.mLastVScale = w.mVScale;
9992 if (SHOW_TRANSACTIONS) Log.i(
9993 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009994 + w.mShownAlpha + " layer=" + w.mAnimLayer
9995 + " matrix=[" + (w.mDsDx*w.mHScale)
9996 + "," + (w.mDtDx*w.mVScale)
9997 + "][" + (w.mDsDy*w.mHScale)
9998 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 if (w.mSurface != null) {
10000 try {
10001 w.mSurface.setAlpha(w.mShownAlpha);
10002 w.mSurface.setLayer(w.mAnimLayer);
10003 w.mSurface.setMatrix(
10004 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10005 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10006 } catch (RuntimeException e) {
10007 Log.w(TAG, "Error updating surface in " + w, e);
10008 if (!recoveringMemory) {
10009 reclaimSomeSurfaceMemoryLocked(w, "update");
10010 }
10011 }
10012 }
10013
10014 if (w.mLastHidden && !w.mDrawPending
10015 && !w.mCommitDrawPending
10016 && !w.mReadyToShow) {
10017 if (SHOW_TRANSACTIONS) Log.i(
10018 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10019 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10020 + " during relayout");
10021 if (showSurfaceRobustlyLocked(w)) {
10022 w.mHasDrawn = true;
10023 w.mLastHidden = false;
10024 } else {
10025 w.mOrientationChanging = false;
10026 }
10027 }
10028 if (w.mSurface != null) {
10029 w.mToken.hasVisible = true;
10030 }
10031 } else {
10032 displayed = true;
10033 }
10034
10035 if (displayed) {
10036 if (!covered) {
10037 if (attrs.width == LayoutParams.FILL_PARENT
10038 && attrs.height == LayoutParams.FILL_PARENT) {
10039 covered = true;
10040 }
10041 }
10042 if (w.mOrientationChanging) {
10043 if (w.mDrawPending || w.mCommitDrawPending) {
10044 orientationChangeComplete = false;
10045 if (DEBUG_ORIENTATION) Log.v(TAG,
10046 "Orientation continue waiting for draw in " + w);
10047 } else {
10048 w.mOrientationChanging = false;
10049 if (DEBUG_ORIENTATION) Log.v(TAG,
10050 "Orientation change complete in " + w);
10051 }
10052 }
10053 w.mToken.hasVisible = true;
10054 }
10055 } else if (w.mOrientationChanging) {
10056 if (DEBUG_ORIENTATION) Log.v(TAG,
10057 "Orientation change skips hidden " + w);
10058 w.mOrientationChanging = false;
10059 }
10060
10061 final boolean canBeSeen = w.isDisplayedLw();
10062
10063 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10064 focusDisplayed = true;
10065 }
10066
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010067 final boolean obscuredChanged = w.mObscured != obscured;
10068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010069 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010070 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010071 if (w.mSurface != null) {
10072 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10073 holdScreen = w.mSession;
10074 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010075 if (!syswin && w.mAttrs.screenBrightness >= 0
10076 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010077 screenBrightness = w.mAttrs.screenBrightness;
10078 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010079 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10080 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10081 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10082 syswin = true;
10083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010084 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010085
Dianne Hackborn25994b42009-09-04 14:21:19 -070010086 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10087 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 // This window completely covers everything behind it,
10089 // so we want to leave all of them as unblurred (for
10090 // performance reasons).
10091 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010092 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10093 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010094 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010095 obscured = true;
10096 if (mBackgroundFillerSurface == null) {
10097 try {
10098 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10099 0, dw, dh,
10100 PixelFormat.OPAQUE,
10101 Surface.FX_SURFACE_NORMAL);
10102 } catch (Exception e) {
10103 Log.e(TAG, "Exception creating filler surface", e);
10104 }
10105 }
10106 try {
10107 mBackgroundFillerSurface.setPosition(0, 0);
10108 mBackgroundFillerSurface.setSize(dw, dh);
10109 // Using the same layer as Dim because they will never be shown at the
10110 // same time.
10111 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10112 mBackgroundFillerSurface.show();
10113 } catch (RuntimeException e) {
10114 Log.e(TAG, "Exception showing filler surface");
10115 }
10116 backgroundFillerShown = true;
10117 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010118 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10120 if (localLOGV) Log.v(TAG, "Win " + w
10121 + ": blurring=" + blurring
10122 + " obscured=" + obscured
10123 + " displayed=" + displayed);
10124 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10125 if (!dimming) {
10126 //Log.i(TAG, "DIM BEHIND: " + w);
10127 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010128 if (mDimAnimator == null) {
10129 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010131 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010133 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010134 }
10135 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10136 if (!blurring) {
10137 //Log.i(TAG, "BLUR BEHIND: " + w);
10138 blurring = true;
10139 mBlurShown = true;
10140 if (mBlurSurface == null) {
10141 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10142 + mBlurSurface + ": CREATE");
10143 try {
Romain Guy06882f82009-06-10 13:36:04 -070010144 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010145 -1, 16, 16,
10146 PixelFormat.OPAQUE,
10147 Surface.FX_SURFACE_BLUR);
10148 } catch (Exception e) {
10149 Log.e(TAG, "Exception creating Blur surface", e);
10150 }
10151 }
10152 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10153 + mBlurSurface + ": SHOW pos=(0,0) (" +
10154 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10155 if (mBlurSurface != null) {
10156 mBlurSurface.setPosition(0, 0);
10157 mBlurSurface.setSize(dw, dh);
10158 try {
10159 mBlurSurface.show();
10160 } catch (RuntimeException e) {
10161 Log.w(TAG, "Failure showing blur surface", e);
10162 }
10163 }
10164 }
10165 mBlurSurface.setLayer(w.mAnimLayer-2);
10166 }
10167 }
10168 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010169
10170 if (obscuredChanged && mWallpaperTarget == w) {
10171 // This is the wallpaper target and its obscured state
10172 // changed... make sure the current wallaper's visibility
10173 // has been updated accordingly.
10174 updateWallpaperVisibilityLocked();
10175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010177
10178 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10179 mBackgroundFillerShown = false;
10180 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10181 try {
10182 mBackgroundFillerSurface.hide();
10183 } catch (RuntimeException e) {
10184 Log.e(TAG, "Exception hiding filler surface", e);
10185 }
10186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010188 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10189 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010190 }
Romain Guy06882f82009-06-10 13:36:04 -070010191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 if (!blurring && mBlurShown) {
10193 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10194 + ": HIDE");
10195 try {
10196 mBlurSurface.hide();
10197 } catch (IllegalArgumentException e) {
10198 Log.w(TAG, "Illegal argument exception hiding blur surface");
10199 }
10200 mBlurShown = false;
10201 }
10202
10203 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10204 } catch (RuntimeException e) {
10205 Log.e(TAG, "Unhandled exception in Window Manager", e);
10206 }
10207
10208 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010210 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10211 "With display frozen, orientationChangeComplete="
10212 + orientationChangeComplete);
10213 if (orientationChangeComplete) {
10214 if (mWindowsFreezingScreen) {
10215 mWindowsFreezingScreen = false;
10216 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10217 }
10218 if (mAppsFreezingScreen == 0) {
10219 stopFreezingDisplayLocked();
10220 }
10221 }
Romain Guy06882f82009-06-10 13:36:04 -070010222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010223 i = mResizingWindows.size();
10224 if (i > 0) {
10225 do {
10226 i--;
10227 WindowState win = mResizingWindows.get(i);
10228 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010229 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10230 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 win.mClient.resized(win.mFrame.width(),
10232 win.mFrame.height(), win.mLastContentInsets,
10233 win.mLastVisibleInsets, win.mDrawPending);
10234 win.mContentInsetsChanged = false;
10235 win.mVisibleInsetsChanged = false;
10236 } catch (RemoteException e) {
10237 win.mOrientationChanging = false;
10238 }
10239 } while (i > 0);
10240 mResizingWindows.clear();
10241 }
Romain Guy06882f82009-06-10 13:36:04 -070010242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010244 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010245 i = mDestroySurface.size();
10246 if (i > 0) {
10247 do {
10248 i--;
10249 WindowState win = mDestroySurface.get(i);
10250 win.mDestroying = false;
10251 if (mInputMethodWindow == win) {
10252 mInputMethodWindow = null;
10253 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010254 if (win == mWallpaperTarget) {
10255 wallpaperDestroyed = true;
10256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010257 win.destroySurfaceLocked();
10258 } while (i > 0);
10259 mDestroySurface.clear();
10260 }
10261
10262 // Time to remove any exiting tokens?
10263 for (i=mExitingTokens.size()-1; i>=0; i--) {
10264 WindowToken token = mExitingTokens.get(i);
10265 if (!token.hasVisible) {
10266 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010267 if (token.windowType == TYPE_WALLPAPER) {
10268 mWallpaperTokens.remove(token);
10269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010270 }
10271 }
10272
10273 // Time to remove any exiting applications?
10274 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10275 AppWindowToken token = mExitingAppTokens.get(i);
10276 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010277 // Make sure there is no animation running on this token,
10278 // so any windows associated with it will be removed as
10279 // soon as their animations are complete
10280 token.animation = null;
10281 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010282 mAppTokens.remove(token);
10283 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010284 if (mLastEnterAnimToken == token) {
10285 mLastEnterAnimToken = null;
10286 mLastEnterAnimParams = null;
10287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010288 }
10289 }
10290
Dianne Hackborna8f60182009-09-01 19:01:50 -070010291 boolean needRelayout = false;
10292
10293 if (!animating && mAppTransitionRunning) {
10294 // We have finished the animation of an app transition. To do
10295 // this, we have delayed a lot of operations like showing and
10296 // hiding apps, moving apps in Z-order, etc. The app token list
10297 // reflects the correct Z-order, but the window list may now
10298 // be out of sync with it. So here we will just rebuild the
10299 // entire app window list. Fun!
10300 mAppTransitionRunning = false;
10301 needRelayout = true;
10302 rebuildAppWindowListLocked();
10303 // Clear information about apps that were moving.
10304 mToBottomApps.clear();
10305 }
10306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 if (focusDisplayed) {
10308 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10309 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010310 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010311 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010312 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010313 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010314 requestAnimationLocked(0);
10315 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10317 }
10318 mQueue.setHoldScreenLocked(holdScreen != null);
10319 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10320 mPowerManager.setScreenBrightnessOverride(-1);
10321 } else {
10322 mPowerManager.setScreenBrightnessOverride((int)
10323 (screenBrightness * Power.BRIGHTNESS_ON));
10324 }
10325 if (holdScreen != mHoldingScreenOn) {
10326 mHoldingScreenOn = holdScreen;
10327 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10328 mH.sendMessage(m);
10329 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010330
10331 if (mTurnOnScreen) {
10332 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10333 LocalPowerManager.BUTTON_EVENT, true);
10334 mTurnOnScreen = false;
10335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 }
10337
10338 void requestAnimationLocked(long delay) {
10339 if (!mAnimationPending) {
10340 mAnimationPending = true;
10341 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10342 }
10343 }
Romain Guy06882f82009-06-10 13:36:04 -070010344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 /**
10346 * Have the surface flinger show a surface, robustly dealing with
10347 * error conditions. In particular, if there is not enough memory
10348 * to show the surface, then we will try to get rid of other surfaces
10349 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 * @return Returns true if the surface was successfully shown.
10352 */
10353 boolean showSurfaceRobustlyLocked(WindowState win) {
10354 try {
10355 if (win.mSurface != null) {
10356 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010357 if (win.mTurnOnScreen) {
10358 win.mTurnOnScreen = false;
10359 mTurnOnScreen = true;
10360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 }
10362 return true;
10363 } catch (RuntimeException e) {
10364 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10365 }
Romain Guy06882f82009-06-10 13:36:04 -070010366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 return false;
10370 }
Romain Guy06882f82009-06-10 13:36:04 -070010371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10373 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10376 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 if (mForceRemoves == null) {
10379 mForceRemoves = new ArrayList<WindowState>();
10380 }
Romain Guy06882f82009-06-10 13:36:04 -070010381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 long callingIdentity = Binder.clearCallingIdentity();
10383 try {
10384 // There was some problem... first, do a sanity check of the
10385 // window list to make sure we haven't left any dangling surfaces
10386 // around.
10387 int N = mWindows.size();
10388 boolean leakedSurface = false;
10389 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10390 for (int i=0; i<N; i++) {
10391 WindowState ws = (WindowState)mWindows.get(i);
10392 if (ws.mSurface != null) {
10393 if (!mSessions.contains(ws.mSession)) {
10394 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10395 + ws + " surface=" + ws.mSurface
10396 + " token=" + win.mToken
10397 + " pid=" + ws.mSession.mPid
10398 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010399 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 ws.mSurface = null;
10401 mForceRemoves.add(ws);
10402 i--;
10403 N--;
10404 leakedSurface = true;
10405 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10406 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10407 + ws + " surface=" + ws.mSurface
10408 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010409 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 ws.mSurface = null;
10411 leakedSurface = true;
10412 }
10413 }
10414 }
Romain Guy06882f82009-06-10 13:36:04 -070010415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010416 boolean killedApps = false;
10417 if (!leakedSurface) {
10418 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10419 SparseIntArray pidCandidates = new SparseIntArray();
10420 for (int i=0; i<N; i++) {
10421 WindowState ws = (WindowState)mWindows.get(i);
10422 if (ws.mSurface != null) {
10423 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10424 }
10425 }
10426 if (pidCandidates.size() > 0) {
10427 int[] pids = new int[pidCandidates.size()];
10428 for (int i=0; i<pids.length; i++) {
10429 pids[i] = pidCandidates.keyAt(i);
10430 }
10431 try {
10432 if (mActivityManager.killPidsForMemory(pids)) {
10433 killedApps = true;
10434 }
10435 } catch (RemoteException e) {
10436 }
10437 }
10438 }
Romain Guy06882f82009-06-10 13:36:04 -070010439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 if (leakedSurface || killedApps) {
10441 // We managed to reclaim some memory, so get rid of the trouble
10442 // surface and ask the app to request another one.
10443 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10444 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010445 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010446 win.mSurface = null;
10447 }
Romain Guy06882f82009-06-10 13:36:04 -070010448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 try {
10450 win.mClient.dispatchGetNewSurface();
10451 } catch (RemoteException e) {
10452 }
10453 }
10454 } finally {
10455 Binder.restoreCallingIdentity(callingIdentity);
10456 }
10457 }
Romain Guy06882f82009-06-10 13:36:04 -070010458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 private boolean updateFocusedWindowLocked(int mode) {
10460 WindowState newFocus = computeFocusedWindowLocked();
10461 if (mCurrentFocus != newFocus) {
10462 // This check makes sure that we don't already have the focus
10463 // change message pending.
10464 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10465 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10466 if (localLOGV) Log.v(
10467 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10468 final WindowState oldFocus = mCurrentFocus;
10469 mCurrentFocus = newFocus;
10470 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 final WindowState imWindow = mInputMethodWindow;
10473 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010474 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010476 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10477 mLayoutNeeded = true;
10478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10480 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010481 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10482 // Client will do the layout, but we need to assign layers
10483 // for handleNewWindowLocked() below.
10484 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 }
10486 }
Romain Guy06882f82009-06-10 13:36:04 -070010487
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010488 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10489 mKeyWaiter.handleNewWindowLocked(newFocus);
10490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 return true;
10492 }
10493 return false;
10494 }
10495
10496 private WindowState computeFocusedWindowLocked() {
10497 WindowState result = null;
10498 WindowState win;
10499
10500 int i = mWindows.size() - 1;
10501 int nextAppIndex = mAppTokens.size()-1;
10502 WindowToken nextApp = nextAppIndex >= 0
10503 ? mAppTokens.get(nextAppIndex) : null;
10504
10505 while (i >= 0) {
10506 win = (WindowState)mWindows.get(i);
10507
10508 if (localLOGV || DEBUG_FOCUS) Log.v(
10509 TAG, "Looking for focus: " + i
10510 + " = " + win
10511 + ", flags=" + win.mAttrs.flags
10512 + ", canReceive=" + win.canReceiveKeys());
10513
10514 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 // If this window's application has been removed, just skip it.
10517 if (thisApp != null && thisApp.removed) {
10518 i--;
10519 continue;
10520 }
Romain Guy06882f82009-06-10 13:36:04 -070010521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 // If there is a focused app, don't allow focus to go to any
10523 // windows below it. If this is an application window, step
10524 // through the app tokens until we find its app.
10525 if (thisApp != null && nextApp != null && thisApp != nextApp
10526 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10527 int origAppIndex = nextAppIndex;
10528 while (nextAppIndex > 0) {
10529 if (nextApp == mFocusedApp) {
10530 // Whoops, we are below the focused app... no focus
10531 // for you!
10532 if (localLOGV || DEBUG_FOCUS) Log.v(
10533 TAG, "Reached focused app: " + mFocusedApp);
10534 return null;
10535 }
10536 nextAppIndex--;
10537 nextApp = mAppTokens.get(nextAppIndex);
10538 if (nextApp == thisApp) {
10539 break;
10540 }
10541 }
10542 if (thisApp != nextApp) {
10543 // Uh oh, the app token doesn't exist! This shouldn't
10544 // happen, but if it does we can get totally hosed...
10545 // so restart at the original app.
10546 nextAppIndex = origAppIndex;
10547 nextApp = mAppTokens.get(nextAppIndex);
10548 }
10549 }
10550
10551 // Dispatch to this window if it is wants key events.
10552 if (win.canReceiveKeys()) {
10553 if (DEBUG_FOCUS) Log.v(
10554 TAG, "Found focus @ " + i + " = " + win);
10555 result = win;
10556 break;
10557 }
10558
10559 i--;
10560 }
10561
10562 return result;
10563 }
10564
10565 private void startFreezingDisplayLocked() {
10566 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010567 // Freezing the display also suspends key event delivery, to
10568 // keep events from going astray while the display is reconfigured.
10569 // If someone has changed orientation again while the screen is
10570 // still frozen, the events will continue to be blocked while the
10571 // successive orientation change is processed. To prevent spurious
10572 // ANRs, we reset the event dispatch timeout in this case.
10573 synchronized (mKeyWaiter) {
10574 mKeyWaiter.mWasFrozen = true;
10575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010576 return;
10577 }
Romain Guy06882f82009-06-10 13:36:04 -070010578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 long now = SystemClock.uptimeMillis();
10582 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10583 if (mFreezeGcPending != 0) {
10584 if (now > (mFreezeGcPending+1000)) {
10585 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10586 mH.removeMessages(H.FORCE_GC);
10587 Runtime.getRuntime().gc();
10588 mFreezeGcPending = now;
10589 }
10590 } else {
10591 mFreezeGcPending = now;
10592 }
Romain Guy06882f82009-06-10 13:36:04 -070010593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010595 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10596 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010597 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 mAppTransitionReady = true;
10599 }
Romain Guy06882f82009-06-10 13:36:04 -070010600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 if (PROFILE_ORIENTATION) {
10602 File file = new File("/data/system/frozen");
10603 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10604 }
10605 Surface.freezeDisplay(0);
10606 }
Romain Guy06882f82009-06-10 13:36:04 -070010607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 private void stopFreezingDisplayLocked() {
10609 if (!mDisplayFrozen) {
10610 return;
10611 }
Romain Guy06882f82009-06-10 13:36:04 -070010612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 mDisplayFrozen = false;
10614 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10615 if (PROFILE_ORIENTATION) {
10616 Debug.stopMethodTracing();
10617 }
10618 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010619
Chris Tate2ad63a92009-03-25 17:36:48 -070010620 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10621 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622 synchronized (mKeyWaiter) {
10623 mKeyWaiter.mWasFrozen = true;
10624 mKeyWaiter.notifyAll();
10625 }
10626
10627 // A little kludge: a lot could have happened while the
10628 // display was frozen, so now that we are coming back we
10629 // do a gc so that any remote references the system
10630 // processes holds on others can be released if they are
10631 // no longer needed.
10632 mH.removeMessages(H.FORCE_GC);
10633 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10634 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 mScreenFrozenLock.release();
10637 }
Romain Guy06882f82009-06-10 13:36:04 -070010638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 @Override
10640 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10641 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10642 != PackageManager.PERMISSION_GRANTED) {
10643 pw.println("Permission Denial: can't dump WindowManager from from pid="
10644 + Binder.getCallingPid()
10645 + ", uid=" + Binder.getCallingUid());
10646 return;
10647 }
Romain Guy06882f82009-06-10 13:36:04 -070010648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 synchronized(mWindowMap) {
10650 pw.println("Current Window Manager state:");
10651 for (int i=mWindows.size()-1; i>=0; i--) {
10652 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010653 pw.print(" Window #"); pw.print(i); pw.print(' ');
10654 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 w.dump(pw, " ");
10656 }
10657 if (mInputMethodDialogs.size() > 0) {
10658 pw.println(" ");
10659 pw.println(" Input method dialogs:");
10660 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10661 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010662 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 }
10664 }
10665 if (mPendingRemove.size() > 0) {
10666 pw.println(" ");
10667 pw.println(" Remove pending for:");
10668 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10669 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010670 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10671 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672 w.dump(pw, " ");
10673 }
10674 }
10675 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10676 pw.println(" ");
10677 pw.println(" Windows force removing:");
10678 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10679 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010680 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10681 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 w.dump(pw, " ");
10683 }
10684 }
10685 if (mDestroySurface.size() > 0) {
10686 pw.println(" ");
10687 pw.println(" Windows waiting to destroy their surface:");
10688 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10689 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010690 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10691 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010692 w.dump(pw, " ");
10693 }
10694 }
10695 if (mLosingFocus.size() > 0) {
10696 pw.println(" ");
10697 pw.println(" Windows losing focus:");
10698 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10699 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010700 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10701 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 w.dump(pw, " ");
10703 }
10704 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010705 if (mResizingWindows.size() > 0) {
10706 pw.println(" ");
10707 pw.println(" Windows waiting to resize:");
10708 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10709 WindowState w = mResizingWindows.get(i);
10710 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10711 pw.print(w); pw.println(":");
10712 w.dump(pw, " ");
10713 }
10714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 if (mSessions.size() > 0) {
10716 pw.println(" ");
10717 pw.println(" All active sessions:");
10718 Iterator<Session> it = mSessions.iterator();
10719 while (it.hasNext()) {
10720 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010721 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722 s.dump(pw, " ");
10723 }
10724 }
10725 if (mTokenMap.size() > 0) {
10726 pw.println(" ");
10727 pw.println(" All tokens:");
10728 Iterator<WindowToken> it = mTokenMap.values().iterator();
10729 while (it.hasNext()) {
10730 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010731 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 token.dump(pw, " ");
10733 }
10734 }
10735 if (mTokenList.size() > 0) {
10736 pw.println(" ");
10737 pw.println(" Window token list:");
10738 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010739 pw.print(" #"); pw.print(i); pw.print(": ");
10740 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010741 }
10742 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010743 if (mWallpaperTokens.size() > 0) {
10744 pw.println(" ");
10745 pw.println(" Wallpaper tokens:");
10746 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10747 WindowToken token = mWallpaperTokens.get(i);
10748 pw.print(" Wallpaper #"); pw.print(i);
10749 pw.print(' '); pw.print(token); pw.println(':');
10750 token.dump(pw, " ");
10751 }
10752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 if (mAppTokens.size() > 0) {
10754 pw.println(" ");
10755 pw.println(" Application tokens in Z order:");
10756 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010757 pw.print(" App #"); pw.print(i); pw.print(": ");
10758 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010759 }
10760 }
10761 if (mFinishedStarting.size() > 0) {
10762 pw.println(" ");
10763 pw.println(" Finishing start of application tokens:");
10764 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10765 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010766 pw.print(" Finished Starting #"); pw.print(i);
10767 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 token.dump(pw, " ");
10769 }
10770 }
10771 if (mExitingTokens.size() > 0) {
10772 pw.println(" ");
10773 pw.println(" Exiting tokens:");
10774 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10775 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010776 pw.print(" Exiting #"); pw.print(i);
10777 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 token.dump(pw, " ");
10779 }
10780 }
10781 if (mExitingAppTokens.size() > 0) {
10782 pw.println(" ");
10783 pw.println(" Exiting application tokens:");
10784 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10785 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010786 pw.print(" Exiting App #"); pw.print(i);
10787 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788 token.dump(pw, " ");
10789 }
10790 }
10791 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010792 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10793 pw.print(" mLastFocus="); pw.println(mLastFocus);
10794 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10795 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10796 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010797 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010798 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10799 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10800 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10801 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010802 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10803 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10804 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10805 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10806 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010807 if (mDimAnimator != null) {
10808 mDimAnimator.printTo(pw);
10809 } else {
10810 pw.print( " no DimAnimator ");
10811 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010812 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010813 pw.print(mInputMethodAnimLayerAdjustment);
10814 pw.print(" mWallpaperAnimLayerAdjustment=");
10815 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010816 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10817 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010818 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10819 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10820 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10821 pw.print(" mRotation="); pw.print(mRotation);
10822 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10823 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10824 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10825 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10826 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10827 pw.print(" mNextAppTransition=0x");
10828 pw.print(Integer.toHexString(mNextAppTransition));
10829 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010830 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010831 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010832 if (mNextAppTransitionPackage != null) {
10833 pw.print(" mNextAppTransitionPackage=");
10834 pw.print(mNextAppTransitionPackage);
10835 pw.print(", mNextAppTransitionEnter=0x");
10836 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10837 pw.print(", mNextAppTransitionExit=0x");
10838 pw.print(Integer.toHexString(mNextAppTransitionExit));
10839 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010840 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10841 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010842 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10843 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10844 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10845 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010846 if (mOpeningApps.size() > 0) {
10847 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10848 }
10849 if (mClosingApps.size() > 0) {
10850 pw.print(" mClosingApps="); pw.println(mClosingApps);
10851 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010852 if (mToTopApps.size() > 0) {
10853 pw.print(" mToTopApps="); pw.println(mToTopApps);
10854 }
10855 if (mToBottomApps.size() > 0) {
10856 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10857 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010858 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10859 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010861 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10862 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10863 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10864 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10865 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10866 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 }
10868 }
10869
10870 public void monitor() {
10871 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010872 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 synchronized (mKeyWaiter) { }
10874 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010875
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010876 public void virtualKeyFeedback(KeyEvent event) {
10877 mPolicy.keyFeedbackFromInput(event);
10878 }
10879
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010880 /**
10881 * DimAnimator class that controls the dim animation. This holds the surface and
10882 * all state used for dim animation.
10883 */
10884 private static class DimAnimator {
10885 Surface mDimSurface;
10886 boolean mDimShown = false;
10887 float mDimCurrentAlpha;
10888 float mDimTargetAlpha;
10889 float mDimDeltaPerMs;
10890 long mLastDimAnimTime;
10891
10892 DimAnimator (SurfaceSession session) {
10893 if (mDimSurface == null) {
10894 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10895 + mDimSurface + ": CREATE");
10896 try {
10897 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10898 Surface.FX_SURFACE_DIM);
10899 } catch (Exception e) {
10900 Log.e(TAG, "Exception creating Dim surface", e);
10901 }
10902 }
10903 }
10904
10905 /**
10906 * Show the dim surface.
10907 */
10908 void show(int dw, int dh) {
10909 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10910 dw + "x" + dh + ")");
10911 mDimShown = true;
10912 try {
10913 mDimSurface.setPosition(0, 0);
10914 mDimSurface.setSize(dw, dh);
10915 mDimSurface.show();
10916 } catch (RuntimeException e) {
10917 Log.w(TAG, "Failure showing dim surface", e);
10918 }
10919 }
10920
10921 /**
10922 * Set's the dim surface's layer and update dim parameters that will be used in
10923 * {@link updateSurface} after all windows are examined.
10924 */
10925 void updateParameters(WindowState w, long currentTime) {
10926 mDimSurface.setLayer(w.mAnimLayer-1);
10927
10928 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010929 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10930 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010931 if (mDimTargetAlpha != target) {
10932 // If the desired dim level has changed, then
10933 // start an animation to it.
10934 mLastDimAnimTime = currentTime;
10935 long duration = (w.mAnimating && w.mAnimation != null)
10936 ? w.mAnimation.computeDurationHint()
10937 : DEFAULT_DIM_DURATION;
10938 if (target > mDimTargetAlpha) {
10939 // This is happening behind the activity UI,
10940 // so we can make it run a little longer to
10941 // give a stronger impression without disrupting
10942 // the user.
10943 duration *= DIM_DURATION_MULTIPLIER;
10944 }
10945 if (duration < 1) {
10946 // Don't divide by zero
10947 duration = 1;
10948 }
10949 mDimTargetAlpha = target;
10950 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10951 }
10952 }
10953
10954 /**
10955 * Updating the surface's alpha. Returns true if the animation continues, or returns
10956 * false when the animation is finished and the dim surface is hidden.
10957 */
10958 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10959 if (!dimming) {
10960 if (mDimTargetAlpha != 0) {
10961 mLastDimAnimTime = currentTime;
10962 mDimTargetAlpha = 0;
10963 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10964 }
10965 }
10966
10967 boolean animating = false;
10968 if (mLastDimAnimTime != 0) {
10969 mDimCurrentAlpha += mDimDeltaPerMs
10970 * (currentTime-mLastDimAnimTime);
10971 boolean more = true;
10972 if (displayFrozen) {
10973 // If the display is frozen, there is no reason to animate.
10974 more = false;
10975 } else if (mDimDeltaPerMs > 0) {
10976 if (mDimCurrentAlpha > mDimTargetAlpha) {
10977 more = false;
10978 }
10979 } else if (mDimDeltaPerMs < 0) {
10980 if (mDimCurrentAlpha < mDimTargetAlpha) {
10981 more = false;
10982 }
10983 } else {
10984 more = false;
10985 }
10986
10987 // Do we need to continue animating?
10988 if (more) {
10989 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10990 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10991 mLastDimAnimTime = currentTime;
10992 mDimSurface.setAlpha(mDimCurrentAlpha);
10993 animating = true;
10994 } else {
10995 mDimCurrentAlpha = mDimTargetAlpha;
10996 mLastDimAnimTime = 0;
10997 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10998 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10999 mDimSurface.setAlpha(mDimCurrentAlpha);
11000 if (!dimming) {
11001 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11002 + ": HIDE");
11003 try {
11004 mDimSurface.hide();
11005 } catch (RuntimeException e) {
11006 Log.w(TAG, "Illegal argument exception hiding dim surface");
11007 }
11008 mDimShown = false;
11009 }
11010 }
11011 }
11012 return animating;
11013 }
11014
11015 public void printTo(PrintWriter pw) {
11016 pw.print(" mDimShown="); pw.print(mDimShown);
11017 pw.print(" current="); pw.print(mDimCurrentAlpha);
11018 pw.print(" target="); pw.print(mDimTargetAlpha);
11019 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11020 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11021 }
11022 }
11023
11024 /**
11025 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11026 * This is used for opening/closing transition for apps in compatible mode.
11027 */
11028 private static class FadeInOutAnimation extends Animation {
11029 int mWidth;
11030 boolean mFadeIn;
11031
11032 public FadeInOutAnimation(boolean fadeIn) {
11033 setInterpolator(new AccelerateInterpolator());
11034 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11035 mFadeIn = fadeIn;
11036 }
11037
11038 @Override
11039 protected void applyTransformation(float interpolatedTime, Transformation t) {
11040 float x = interpolatedTime;
11041 if (!mFadeIn) {
11042 x = 1.0f - x; // reverse the interpolation for fade out
11043 }
11044 if (x < 0.5) {
11045 // move the window out of the screen.
11046 t.getMatrix().setTranslate(mWidth, 0);
11047 } else {
11048 t.getMatrix().setTranslate(0, 0);// show
11049 t.setAlpha((x - 0.5f) * 2);
11050 }
11051 }
11052
11053 @Override
11054 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11055 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11056 mWidth = width;
11057 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011058
11059 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011060 public int getZAdjustment() {
11061 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011062 }
11063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064}