blob: 3c76cf2490896b1a3431f387f9124b25d26c02cb [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
66import android.os.Debug;
67import android.os.Handler;
68import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070069import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.LocalPowerManager;
71import android.os.Looper;
72import android.os.Message;
73import android.os.Parcel;
74import android.os.ParcelFileDescriptor;
75import android.os.Power;
76import android.os.PowerManager;
77import android.os.Process;
78import android.os.RemoteException;
79import android.os.ServiceManager;
80import android.os.SystemClock;
81import android.os.SystemProperties;
82import android.os.TokenWatcher;
83import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070084import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.EventLog;
86import android.util.Log;
87import android.util.SparseIntArray;
88import android.view.Display;
89import android.view.Gravity;
90import android.view.IApplicationToken;
91import android.view.IOnKeyguardExitResult;
92import android.view.IRotationWatcher;
93import android.view.IWindow;
94import android.view.IWindowManager;
95import android.view.IWindowSession;
96import android.view.KeyEvent;
97import android.view.MotionEvent;
98import android.view.RawInputEvent;
99import android.view.Surface;
100import android.view.SurfaceSession;
101import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700102import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.ViewTreeObserver;
104import android.view.WindowManager;
105import android.view.WindowManagerImpl;
106import android.view.WindowManagerPolicy;
107import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700108import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.animation.Animation;
110import android.view.animation.AnimationUtils;
111import android.view.animation.Transformation;
112
113import java.io.BufferedWriter;
114import java.io.File;
115import java.io.FileDescriptor;
116import java.io.IOException;
117import java.io.OutputStream;
118import java.io.OutputStreamWriter;
119import java.io.PrintWriter;
120import java.io.StringWriter;
121import java.net.Socket;
122import java.util.ArrayList;
123import java.util.HashMap;
124import java.util.HashSet;
125import java.util.Iterator;
126import java.util.List;
127
128/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700129public class WindowManagerService extends IWindowManager.Stub
130 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 static final String TAG = "WindowManager";
132 static final boolean DEBUG = false;
133 static final boolean DEBUG_FOCUS = false;
134 static final boolean DEBUG_ANIM = false;
135 static final boolean DEBUG_LAYERS = false;
136 static final boolean DEBUG_INPUT = false;
137 static final boolean DEBUG_INPUT_METHOD = false;
138 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700139 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 static final boolean DEBUG_ORIENTATION = false;
141 static final boolean DEBUG_APP_TRANSITIONS = false;
142 static final boolean DEBUG_STARTING_WINDOW = false;
143 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700144 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700146 static final boolean MEASURE_LATENCY = false;
147 static private LatencyTimer lt;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 static final boolean PROFILE_ORIENTATION = false;
150 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700151 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /** How long to wait for subsequent key repeats, in milliseconds */
156 static final int KEY_REPEAT_DELAY = 50;
157
158 /** How much to multiply the policy's type layer, to reserve room
159 * for multiple windows of the same type and Z-ordering adjustment
160 * with TYPE_LAYER_OFFSET. */
161 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
164 * or below others in the same layer. */
165 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 /** How much to increment the layer for each window, to reserve room
168 * for effect surfaces between them.
169 */
170 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 /** The maximum length we will accept for a loaded animation duration:
173 * this is 10 seconds.
174 */
175 static final int MAX_ANIMATION_DURATION = 10*1000;
176
177 /** Amount of time (in milliseconds) to animate the dim surface from one
178 * value to another, when no window animation is driving it.
179 */
180 static final int DEFAULT_DIM_DURATION = 200;
181
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700182 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
183 * compatible windows.
184 */
185 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 /** Adjustment to time to perform a dim, to make it more dramatic.
188 */
189 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700190
Dianne Hackborncfaef692009-06-15 14:24:44 -0700191 static final int INJECT_FAILED = 0;
192 static final int INJECT_SUCCEEDED = 1;
193 static final int INJECT_NO_PERMISSION = -1;
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 static final int UPDATE_FOCUS_NORMAL = 0;
196 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
197 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
198 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700199
Michael Chane96440f2009-05-06 10:27:36 -0700200 /** The minimum time between dispatching touch events. */
201 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
202
203 // Last touch event time
204 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700205
Michael Chane96440f2009-05-06 10:27:36 -0700206 // Last touch event type
207 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700208
Michael Chane96440f2009-05-06 10:27:36 -0700209 // Time to wait before calling useractivity again. This saves CPU usage
210 // when we get a flood of touch events.
211 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
212
213 // Last time we call user activity
214 long mLastUserActivityCallTime = 0;
215
Romain Guy06882f82009-06-10 13:36:04 -0700216 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700217 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700220 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 /**
223 * Condition waited on by {@link #reenableKeyguard} to know the call to
224 * the window policy has finished.
225 */
226 private boolean mWaitingUntilKeyguardReenabled = false;
227
228
229 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
230 new Handler(), "WindowManagerService.mKeyguardDisabled") {
231 public void acquired() {
232 mPolicy.enableKeyguard(false);
233 }
234 public void released() {
235 synchronized (mKeyguardDisabled) {
236 mPolicy.enableKeyguard(true);
237 mWaitingUntilKeyguardReenabled = false;
238 mKeyguardDisabled.notifyAll();
239 }
240 }
241 };
242
243 final Context mContext;
244
245 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
250
251 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * All currently active sessions with clients.
257 */
258 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Mapping from an IWindow IBinder to the server's Window object.
262 * This is also used as the lock for all of our state.
263 */
264 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
265
266 /**
267 * Mapping from a token IBinder to a WindowToken object.
268 */
269 final HashMap<IBinder, WindowToken> mTokenMap =
270 new HashMap<IBinder, WindowToken>();
271
272 /**
273 * The same tokens as mTokenMap, stored in a list for efficient iteration
274 * over them.
275 */
276 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Window tokens that are in the process of exiting, but still
280 * on screen for animations.
281 */
282 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
283
284 /**
285 * Z-ordered (bottom-most first) list of all application tokens, for
286 * controlling the ordering of windows in different applications. This
287 * contains WindowToken objects.
288 */
289 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
290
291 /**
292 * Application tokens that are in the process of exiting, but still
293 * on screen for animations.
294 */
295 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
296
297 /**
298 * List of window tokens that have finished starting their application,
299 * and now need to have the policy remove their windows.
300 */
301 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
302
303 /**
304 * Z-ordered (bottom-most first) list of all Window objects.
305 */
306 final ArrayList mWindows = new ArrayList();
307
308 /**
309 * Windows that are being resized. Used so we can tell the client about
310 * the resize after closing the transaction in which we resized the
311 * underlying surface.
312 */
313 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
314
315 /**
316 * Windows whose animations have ended and now must be removed.
317 */
318 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
319
320 /**
321 * Windows whose surface should be destroyed.
322 */
323 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
324
325 /**
326 * Windows that have lost input focus and are waiting for the new
327 * focus window to be displayed before they are told about this.
328 */
329 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
330
331 /**
332 * This is set when we have run out of memory, and will either be an empty
333 * list or contain windows that need to be force removed.
334 */
335 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700340 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 Surface mBlurSurface;
342 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 final float[] mTmpFloats = new float[9];
347
348 boolean mSafeMode;
349 boolean mDisplayEnabled = false;
350 boolean mSystemBooted = false;
351 int mRotation = 0;
352 int mRequestedRotation = 0;
353 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700354 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 ArrayList<IRotationWatcher> mRotationWatchers
356 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 boolean mLayoutNeeded = true;
359 boolean mAnimationPending = false;
360 boolean mDisplayFrozen = false;
361 boolean mWindowsFreezingScreen = false;
362 long mFreezeGcPending = 0;
363 int mAppsFreezingScreen = 0;
364
365 // This is held as long as we have the screen frozen, to give us time to
366 // perform a rotation animation when turning off shows the lock screen which
367 // changes the orientation.
368 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 // State management of app transitions. When we are preparing for a
371 // transition, mNextAppTransition will be the kind of transition to
372 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
373 // mOpeningApps and mClosingApps are the lists of tokens that will be
374 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700375 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700377 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 boolean mAppTransitionTimeout = false;
379 boolean mStartingIconInTransition = false;
380 boolean mSkipAppTransitionAnimation = false;
381 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
382 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700383 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
384 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 //flag to detect fat touch events
387 boolean mFatTouch = false;
388 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 H mH = new H();
391
392 WindowState mCurrentFocus = null;
393 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 // This just indicates the window the input method is on top of, not
396 // necessarily the window its input is going to.
397 WindowState mInputMethodTarget = null;
398 WindowState mUpcomingInputMethodTarget = null;
399 boolean mInputMethodTargetWaitingAnim;
400 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 WindowState mInputMethodWindow = null;
403 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
404
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700405 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
406
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700407 // If non-null, this is the currently visible window that is associated
408 // with the wallpaper.
409 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700410 // If non-null, we are in the middle of animating from one wallpaper target
411 // to another, and this is the lower one in Z-order.
412 WindowState mLowerWallpaperTarget = null;
413 // If non-null, we are in the middle of animating from one wallpaper target
414 // to another, and this is the higher one in Z-order.
415 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700416 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700417 float mLastWallpaperX;
418 float mLastWallpaperY;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700419 // Lock for waiting for the wallpaper.
420 final Object mWaitingOnWallpaperLock = new Object();
421 // This is set when we are waiting for a wallpaper to tell us it is done
422 // changing its scroll position.
423 WindowState mWaitingOnWallpaper;
424 // The last time we had a timeout when waiting for a wallpaper.
425 long mLastWallpaperTimeoutTime;
426 // We give a wallpaper up to 150ms to finish scrolling.
427 static final long WALLPAPER_TIMEOUT = 150;
428 // Time we wait after a timeout before trying to wait again.
429 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 AppWindowToken mFocusedApp = null;
432
433 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 float mWindowAnimationScale = 1.0f;
436 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 final KeyWaiter mKeyWaiter = new KeyWaiter();
439 final KeyQ mQueue;
440 final InputDispatcherThread mInputThread;
441
442 // Who is holding the screen on.
443 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700444
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700445 boolean mTurnOnScreen;
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
448 * Whether the UI is currently running in touch mode (not showing
449 * navigational focus because the user is directly pressing the screen).
450 */
451 boolean mInTouchMode = false;
452
453 private ViewServer mViewServer;
454
455 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700456
Dianne Hackbornc485a602009-03-24 22:39:49 -0700457 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700458 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700459
460 // The frame use to limit the size of the app running in compatibility mode.
461 Rect mCompatibleScreenFrame = new Rect();
462 // The surface used to fill the outer rim of the app running in compatibility mode.
463 Surface mBackgroundFillerSurface = null;
464 boolean mBackgroundFillerShown = false;
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public static WindowManagerService main(Context context,
467 PowerManagerService pm, boolean haveInputMethods) {
468 WMThread thr = new WMThread(context, pm, haveInputMethods);
469 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 synchronized (thr) {
472 while (thr.mService == null) {
473 try {
474 thr.wait();
475 } catch (InterruptedException e) {
476 }
477 }
478 }
Romain Guy06882f82009-06-10 13:36:04 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 return thr.mService;
481 }
Romain Guy06882f82009-06-10 13:36:04 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 static class WMThread extends Thread {
484 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 private final Context mContext;
487 private final PowerManagerService mPM;
488 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public WMThread(Context context, PowerManagerService pm,
491 boolean haveInputMethods) {
492 super("WindowManager");
493 mContext = context;
494 mPM = pm;
495 mHaveInputMethods = haveInputMethods;
496 }
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 public void run() {
499 Looper.prepare();
500 WindowManagerService s = new WindowManagerService(mContext, mPM,
501 mHaveInputMethods);
502 android.os.Process.setThreadPriority(
503 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 synchronized (this) {
506 mService = s;
507 notifyAll();
508 }
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 Looper.loop();
511 }
512 }
513
514 static class PolicyThread extends Thread {
515 private final WindowManagerPolicy mPolicy;
516 private final WindowManagerService mService;
517 private final Context mContext;
518 private final PowerManagerService mPM;
519 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public PolicyThread(WindowManagerPolicy policy,
522 WindowManagerService service, Context context,
523 PowerManagerService pm) {
524 super("WindowManagerPolicy");
525 mPolicy = policy;
526 mService = service;
527 mContext = context;
528 mPM = pm;
529 }
Romain Guy06882f82009-06-10 13:36:04 -0700530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 public void run() {
532 Looper.prepare();
533 //Looper.myLooper().setMessageLogging(new LogPrinter(
534 // Log.VERBOSE, "WindowManagerPolicy"));
535 android.os.Process.setThreadPriority(
536 android.os.Process.THREAD_PRIORITY_FOREGROUND);
537 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 synchronized (this) {
540 mRunning = true;
541 notifyAll();
542 }
Romain Guy06882f82009-06-10 13:36:04 -0700543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 Looper.loop();
545 }
546 }
547
548 private WindowManagerService(Context context, PowerManagerService pm,
549 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700550 if (MEASURE_LATENCY) {
551 lt = new LatencyTimer(100, 1000);
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 mContext = context;
555 mHaveInputMethods = haveInputMethods;
556 mLimitedAlphaCompositing = context.getResources().getBoolean(
557 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 mPowerManager = pm;
560 mPowerManager.setPolicy(mPolicy);
561 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
562 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
563 "SCREEN_FROZEN");
564 mScreenFrozenLock.setReferenceCounted(false);
565
566 mActivityManager = ActivityManagerNative.getDefault();
567 mBatteryStats = BatteryStatsService.getService();
568
569 // Get persisted window scale setting
570 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
571 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
572 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
573 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700574
Michael Chan9f028e62009-08-04 17:37:46 -0700575 int max_events_per_sec = 35;
576 try {
577 max_events_per_sec = Integer.parseInt(SystemProperties
578 .get("windowsmgr.max_events_per_sec"));
579 if (max_events_per_sec < 1) {
580 max_events_per_sec = 35;
581 }
582 } catch (NumberFormatException e) {
583 }
584 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 mQueue = new KeyQ();
587
588 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
591 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 synchronized (thr) {
594 while (!thr.mRunning) {
595 try {
596 thr.wait();
597 } catch (InterruptedException e) {
598 }
599 }
600 }
Romain Guy06882f82009-06-10 13:36:04 -0700601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 // Add ourself to the Watchdog monitors.
605 Watchdog.getInstance().addMonitor(this);
606 }
607
608 @Override
609 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
610 throws RemoteException {
611 try {
612 return super.onTransact(code, data, reply, flags);
613 } catch (RuntimeException e) {
614 // The window manager only throws security exceptions, so let's
615 // log all others.
616 if (!(e instanceof SecurityException)) {
617 Log.e(TAG, "Window Manager Crash", e);
618 }
619 throw e;
620 }
621 }
622
623 private void placeWindowAfter(Object pos, WindowState window) {
624 final int i = mWindows.indexOf(pos);
625 if (localLOGV || DEBUG_FOCUS) Log.v(
626 TAG, "Adding window " + window + " at "
627 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
628 mWindows.add(i+1, window);
629 }
630
631 private void placeWindowBefore(Object pos, WindowState window) {
632 final int i = mWindows.indexOf(pos);
633 if (localLOGV || DEBUG_FOCUS) Log.v(
634 TAG, "Adding window " + window + " at "
635 + i + " of " + mWindows.size() + " (before " + pos + ")");
636 mWindows.add(i, window);
637 }
638
639 //This method finds out the index of a window that has the same app token as
640 //win. used for z ordering the windows in mWindows
641 private int findIdxBasedOnAppTokens(WindowState win) {
642 //use a local variable to cache mWindows
643 ArrayList localmWindows = mWindows;
644 int jmax = localmWindows.size();
645 if(jmax == 0) {
646 return -1;
647 }
648 for(int j = (jmax-1); j >= 0; j--) {
649 WindowState wentry = (WindowState)localmWindows.get(j);
650 if(wentry.mAppToken == win.mAppToken) {
651 return j;
652 }
653 }
654 return -1;
655 }
Romain Guy06882f82009-06-10 13:36:04 -0700656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
658 final IWindow client = win.mClient;
659 final WindowToken token = win.mToken;
660 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 final int N = localmWindows.size();
663 final WindowState attached = win.mAttachedWindow;
664 int i;
665 if (attached == null) {
666 int tokenWindowsPos = token.windows.size();
667 if (token.appWindowToken != null) {
668 int index = tokenWindowsPos-1;
669 if (index >= 0) {
670 // If this application has existing windows, we
671 // simply place the new window on top of them... but
672 // keep the starting window on top.
673 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
674 // Base windows go behind everything else.
675 placeWindowBefore(token.windows.get(0), win);
676 tokenWindowsPos = 0;
677 } else {
678 AppWindowToken atoken = win.mAppToken;
679 if (atoken != null &&
680 token.windows.get(index) == atoken.startingWindow) {
681 placeWindowBefore(token.windows.get(index), win);
682 tokenWindowsPos--;
683 } else {
684 int newIdx = findIdxBasedOnAppTokens(win);
685 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700686 //there is a window above this one associated with the same
687 //apptoken note that the window could be a floating window
688 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 //windows associated with this token.
690 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 }
694 } else {
695 if (localLOGV) Log.v(
696 TAG, "Figuring out where to add app window "
697 + client.asBinder() + " (token=" + token + ")");
698 // Figure out where the window should go, based on the
699 // order of applications.
700 final int NA = mAppTokens.size();
701 Object pos = null;
702 for (i=NA-1; i>=0; i--) {
703 AppWindowToken t = mAppTokens.get(i);
704 if (t == token) {
705 i--;
706 break;
707 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700708
709 // We haven't reached the token yet; if this token
710 // is not going to the bottom and has windows, we can
711 // use it as an anchor for when we do reach the token.
712 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 pos = t.windows.get(0);
714 }
715 }
716 // We now know the index into the apps. If we found
717 // an app window above, that gives us the position; else
718 // we need to look some more.
719 if (pos != null) {
720 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700721 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 mTokenMap.get(((WindowState)pos).mClient.asBinder());
723 if (atoken != null) {
724 final int NC = atoken.windows.size();
725 if (NC > 0) {
726 WindowState bottom = atoken.windows.get(0);
727 if (bottom.mSubLayer < 0) {
728 pos = bottom;
729 }
730 }
731 }
732 placeWindowBefore(pos, win);
733 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700734 // Continue looking down until we find the first
735 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 while (i >= 0) {
737 AppWindowToken t = mAppTokens.get(i);
738 final int NW = t.windows.size();
739 if (NW > 0) {
740 pos = t.windows.get(NW-1);
741 break;
742 }
743 i--;
744 }
745 if (pos != null) {
746 // Move in front of any windows attached to this
747 // one.
748 WindowToken atoken =
749 mTokenMap.get(((WindowState)pos).mClient.asBinder());
750 if (atoken != null) {
751 final int NC = atoken.windows.size();
752 if (NC > 0) {
753 WindowState top = atoken.windows.get(NC-1);
754 if (top.mSubLayer >= 0) {
755 pos = top;
756 }
757 }
758 }
759 placeWindowAfter(pos, win);
760 } else {
761 // Just search for the start of this layer.
762 final int myLayer = win.mBaseLayer;
763 for (i=0; i<N; i++) {
764 WindowState w = (WindowState)localmWindows.get(i);
765 if (w.mBaseLayer > myLayer) {
766 break;
767 }
768 }
769 if (localLOGV || DEBUG_FOCUS) Log.v(
770 TAG, "Adding window " + win + " at "
771 + i + " of " + N);
772 localmWindows.add(i, win);
773 }
774 }
775 }
776 } else {
777 // Figure out where window should go, based on layer.
778 final int myLayer = win.mBaseLayer;
779 for (i=N-1; i>=0; i--) {
780 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
781 i++;
782 break;
783 }
784 }
785 if (i < 0) i = 0;
786 if (localLOGV || DEBUG_FOCUS) Log.v(
787 TAG, "Adding window " + win + " at "
788 + i + " of " + N);
789 localmWindows.add(i, win);
790 }
791 if (addToToken) {
792 token.windows.add(tokenWindowsPos, win);
793 }
794
795 } else {
796 // Figure out this window's ordering relative to the window
797 // it is attached to.
798 final int NA = token.windows.size();
799 final int sublayer = win.mSubLayer;
800 int largestSublayer = Integer.MIN_VALUE;
801 WindowState windowWithLargestSublayer = null;
802 for (i=0; i<NA; i++) {
803 WindowState w = token.windows.get(i);
804 final int wSublayer = w.mSubLayer;
805 if (wSublayer >= largestSublayer) {
806 largestSublayer = wSublayer;
807 windowWithLargestSublayer = w;
808 }
809 if (sublayer < 0) {
810 // For negative sublayers, we go below all windows
811 // in the same sublayer.
812 if (wSublayer >= sublayer) {
813 if (addToToken) {
814 token.windows.add(i, win);
815 }
816 placeWindowBefore(
817 wSublayer >= 0 ? attached : w, win);
818 break;
819 }
820 } else {
821 // For positive sublayers, we go above all windows
822 // in the same sublayer.
823 if (wSublayer > sublayer) {
824 if (addToToken) {
825 token.windows.add(i, win);
826 }
827 placeWindowBefore(w, win);
828 break;
829 }
830 }
831 }
832 if (i >= NA) {
833 if (addToToken) {
834 token.windows.add(win);
835 }
836 if (sublayer < 0) {
837 placeWindowBefore(attached, win);
838 } else {
839 placeWindowAfter(largestSublayer >= 0
840 ? windowWithLargestSublayer
841 : attached,
842 win);
843 }
844 }
845 }
Romain Guy06882f82009-06-10 13:36:04 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (win.mAppToken != null && addToToken) {
848 win.mAppToken.allAppWindows.add(win);
849 }
850 }
Romain Guy06882f82009-06-10 13:36:04 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 static boolean canBeImeTarget(WindowState w) {
853 final int fl = w.mAttrs.flags
854 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
855 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
856 return w.isVisibleOrAdding();
857 }
858 return false;
859 }
Romain Guy06882f82009-06-10 13:36:04 -0700860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
862 final ArrayList localmWindows = mWindows;
863 final int N = localmWindows.size();
864 WindowState w = null;
865 int i = N;
866 while (i > 0) {
867 i--;
868 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
871 // + Integer.toHexString(w.mAttrs.flags));
872 if (canBeImeTarget(w)) {
873 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 // Yet more tricksyness! If this window is a "starting"
876 // window, we do actually want to be on top of it, but
877 // it is not -really- where input will go. So if the caller
878 // is not actually looking to move the IME, look down below
879 // for a real window to target...
880 if (!willMove
881 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
882 && i > 0) {
883 WindowState wb = (WindowState)localmWindows.get(i-1);
884 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
885 i--;
886 w = wb;
887 }
888 }
889 break;
890 }
891 }
Romain Guy06882f82009-06-10 13:36:04 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
896 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 if (willMove && w != null) {
899 final WindowState curTarget = mInputMethodTarget;
900 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Now some fun for dealing with window animations that
903 // modify the Z order. We need to look at all windows below
904 // the current target that are in this app, finding the highest
905 // visible one in layering.
906 AppWindowToken token = curTarget.mAppToken;
907 WindowState highestTarget = null;
908 int highestPos = 0;
909 if (token.animating || token.animation != null) {
910 int pos = 0;
911 pos = localmWindows.indexOf(curTarget);
912 while (pos >= 0) {
913 WindowState win = (WindowState)localmWindows.get(pos);
914 if (win.mAppToken != token) {
915 break;
916 }
917 if (!win.mRemoved) {
918 if (highestTarget == null || win.mAnimLayer >
919 highestTarget.mAnimLayer) {
920 highestTarget = win;
921 highestPos = pos;
922 }
923 }
924 pos--;
925 }
926 }
Romain Guy06882f82009-06-10 13:36:04 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700929 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 + mNextAppTransition + " " + highestTarget
931 + " animating=" + highestTarget.isAnimating()
932 + " layer=" + highestTarget.mAnimLayer
933 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700934
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700935 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 // If we are currently setting up for an animation,
937 // hold everything until we can find out what will happen.
938 mInputMethodTargetWaitingAnim = true;
939 mInputMethodTarget = highestTarget;
940 return highestPos + 1;
941 } else if (highestTarget.isAnimating() &&
942 highestTarget.mAnimLayer > w.mAnimLayer) {
943 // If the window we are currently targeting is involved
944 // with an animation, and it is on top of the next target
945 // we will be over, then hold off on moving until
946 // that is done.
947 mInputMethodTarget = highestTarget;
948 return highestPos + 1;
949 }
950 }
951 }
952 }
Romain Guy06882f82009-06-10 13:36:04 -0700953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 //Log.i(TAG, "Placing input method @" + (i+1));
955 if (w != null) {
956 if (willMove) {
957 RuntimeException e = new RuntimeException();
958 e.fillInStackTrace();
959 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
960 + mInputMethodTarget + " to " + w, e);
961 mInputMethodTarget = w;
962 if (w.mAppToken != null) {
963 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
964 } else {
965 setInputMethodAnimLayerAdjustment(0);
966 }
967 }
968 return i+1;
969 }
970 if (willMove) {
971 RuntimeException e = new RuntimeException();
972 e.fillInStackTrace();
973 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
974 + mInputMethodTarget + " to null", e);
975 mInputMethodTarget = null;
976 setInputMethodAnimLayerAdjustment(0);
977 }
978 return -1;
979 }
Romain Guy06882f82009-06-10 13:36:04 -0700980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 void addInputMethodWindowToListLocked(WindowState win) {
982 int pos = findDesiredInputMethodWindowIndexLocked(true);
983 if (pos >= 0) {
984 win.mTargetAppToken = mInputMethodTarget.mAppToken;
985 mWindows.add(pos, win);
986 moveInputMethodDialogsLocked(pos+1);
987 return;
988 }
989 win.mTargetAppToken = null;
990 addWindowToListInOrderLocked(win, true);
991 moveInputMethodDialogsLocked(pos);
992 }
Romain Guy06882f82009-06-10 13:36:04 -0700993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 void setInputMethodAnimLayerAdjustment(int adj) {
995 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
996 mInputMethodAnimLayerAdjustment = adj;
997 WindowState imw = mInputMethodWindow;
998 if (imw != null) {
999 imw.mAnimLayer = imw.mLayer + adj;
1000 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1001 + " anim layer: " + imw.mAnimLayer);
1002 int wi = imw.mChildWindows.size();
1003 while (wi > 0) {
1004 wi--;
1005 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1006 cw.mAnimLayer = cw.mLayer + adj;
1007 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1008 + " anim layer: " + cw.mAnimLayer);
1009 }
1010 }
1011 int di = mInputMethodDialogs.size();
1012 while (di > 0) {
1013 di --;
1014 imw = mInputMethodDialogs.get(di);
1015 imw.mAnimLayer = imw.mLayer + adj;
1016 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1017 + " anim layer: " + imw.mAnimLayer);
1018 }
1019 }
Romain Guy06882f82009-06-10 13:36:04 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1022 int wpos = mWindows.indexOf(win);
1023 if (wpos >= 0) {
1024 if (wpos < interestingPos) interestingPos--;
1025 mWindows.remove(wpos);
1026 int NC = win.mChildWindows.size();
1027 while (NC > 0) {
1028 NC--;
1029 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1030 int cpos = mWindows.indexOf(cw);
1031 if (cpos >= 0) {
1032 if (cpos < interestingPos) interestingPos--;
1033 mWindows.remove(cpos);
1034 }
1035 }
1036 }
1037 return interestingPos;
1038 }
Romain Guy06882f82009-06-10 13:36:04 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 private void reAddWindowToListInOrderLocked(WindowState win) {
1041 addWindowToListInOrderLocked(win, false);
1042 // This is a hack to get all of the child windows added as well
1043 // at the right position. Child windows should be rare and
1044 // this case should be rare, so it shouldn't be that big a deal.
1045 int wpos = mWindows.indexOf(win);
1046 if (wpos >= 0) {
1047 mWindows.remove(wpos);
1048 reAddWindowLocked(wpos, win);
1049 }
1050 }
Romain Guy06882f82009-06-10 13:36:04 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 void logWindowList(String prefix) {
1053 int N = mWindows.size();
1054 while (N > 0) {
1055 N--;
1056 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1057 }
1058 }
Romain Guy06882f82009-06-10 13:36:04 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 void moveInputMethodDialogsLocked(int pos) {
1061 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 final int N = dialogs.size();
1064 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1065 for (int i=0; i<N; i++) {
1066 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1067 }
1068 if (DEBUG_INPUT_METHOD) {
1069 Log.v(TAG, "Window list w/pos=" + pos);
1070 logWindowList(" ");
1071 }
Romain Guy06882f82009-06-10 13:36:04 -07001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (pos >= 0) {
1074 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1075 if (pos < mWindows.size()) {
1076 WindowState wp = (WindowState)mWindows.get(pos);
1077 if (wp == mInputMethodWindow) {
1078 pos++;
1079 }
1080 }
1081 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1082 for (int i=0; i<N; i++) {
1083 WindowState win = dialogs.get(i);
1084 win.mTargetAppToken = targetAppToken;
1085 pos = reAddWindowLocked(pos, win);
1086 }
1087 if (DEBUG_INPUT_METHOD) {
1088 Log.v(TAG, "Final window list:");
1089 logWindowList(" ");
1090 }
1091 return;
1092 }
1093 for (int i=0; i<N; i++) {
1094 WindowState win = dialogs.get(i);
1095 win.mTargetAppToken = null;
1096 reAddWindowToListInOrderLocked(win);
1097 if (DEBUG_INPUT_METHOD) {
1098 Log.v(TAG, "No IM target, final list:");
1099 logWindowList(" ");
1100 }
1101 }
1102 }
Romain Guy06882f82009-06-10 13:36:04 -07001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1105 final WindowState imWin = mInputMethodWindow;
1106 final int DN = mInputMethodDialogs.size();
1107 if (imWin == null && DN == 0) {
1108 return false;
1109 }
Romain Guy06882f82009-06-10 13:36:04 -07001110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1112 if (imPos >= 0) {
1113 // In this case, the input method windows are to be placed
1114 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 // First check to see if the input method windows are already
1117 // located here, and contiguous.
1118 final int N = mWindows.size();
1119 WindowState firstImWin = imPos < N
1120 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 // Figure out the actual input method window that should be
1123 // at the bottom of their stack.
1124 WindowState baseImWin = imWin != null
1125 ? imWin : mInputMethodDialogs.get(0);
1126 if (baseImWin.mChildWindows.size() > 0) {
1127 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1128 if (cw.mSubLayer < 0) baseImWin = cw;
1129 }
Romain Guy06882f82009-06-10 13:36:04 -07001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (firstImWin == baseImWin) {
1132 // The windows haven't moved... but are they still contiguous?
1133 // First find the top IM window.
1134 int pos = imPos+1;
1135 while (pos < N) {
1136 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1137 break;
1138 }
1139 pos++;
1140 }
1141 pos++;
1142 // Now there should be no more input method windows above.
1143 while (pos < N) {
1144 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1145 break;
1146 }
1147 pos++;
1148 }
1149 if (pos >= N) {
1150 // All is good!
1151 return false;
1152 }
1153 }
Romain Guy06882f82009-06-10 13:36:04 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (imWin != null) {
1156 if (DEBUG_INPUT_METHOD) {
1157 Log.v(TAG, "Moving IM from " + imPos);
1158 logWindowList(" ");
1159 }
1160 imPos = tmpRemoveWindowLocked(imPos, imWin);
1161 if (DEBUG_INPUT_METHOD) {
1162 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1163 logWindowList(" ");
1164 }
1165 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1166 reAddWindowLocked(imPos, imWin);
1167 if (DEBUG_INPUT_METHOD) {
1168 Log.v(TAG, "List after moving IM to " + imPos + ":");
1169 logWindowList(" ");
1170 }
1171 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1172 } else {
1173 moveInputMethodDialogsLocked(imPos);
1174 }
Romain Guy06882f82009-06-10 13:36:04 -07001175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 } else {
1177 // In this case, the input method windows go in a fixed layer,
1178 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 if (imWin != null) {
1181 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1182 tmpRemoveWindowLocked(0, imWin);
1183 imWin.mTargetAppToken = null;
1184 reAddWindowToListInOrderLocked(imWin);
1185 if (DEBUG_INPUT_METHOD) {
1186 Log.v(TAG, "List with no IM target:");
1187 logWindowList(" ");
1188 }
1189 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1190 } else {
1191 moveInputMethodDialogsLocked(-1);;
1192 }
Romain Guy06882f82009-06-10 13:36:04 -07001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
Romain Guy06882f82009-06-10 13:36:04 -07001195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 if (needAssignLayers) {
1197 assignLayersLocked();
1198 }
Romain Guy06882f82009-06-10 13:36:04 -07001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 return true;
1201 }
Romain Guy06882f82009-06-10 13:36:04 -07001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 void adjustInputMethodDialogsLocked() {
1204 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1205 }
Romain Guy06882f82009-06-10 13:36:04 -07001206
Dianne Hackborn25994b42009-09-04 14:21:19 -07001207 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1208 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1209 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1210 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1211 ? wallpaperTarget.mAppToken.animation : null)
1212 + " upper=" + mUpperWallpaperTarget
1213 + " lower=" + mLowerWallpaperTarget);
1214 return (wallpaperTarget != null
1215 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1216 && wallpaperTarget.mAppToken.animation != null)))
1217 || mUpperWallpaperTarget != null
1218 || mLowerWallpaperTarget != null;
1219 }
1220
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001221 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1222 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1223
1224 int adjustWallpaperWindowsLocked() {
1225 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001226
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001227 final int dw = mDisplay.getWidth();
1228 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001229
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001230 // First find top-most window that has asked to be on top of the
1231 // wallpaper; all wallpapers go behind it.
1232 final ArrayList localmWindows = mWindows;
1233 int N = localmWindows.size();
1234 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001235 WindowState foundW = null;
1236 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001237 int i = N;
1238 while (i > 0) {
1239 i--;
1240 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001241 if (w.mAppToken != null) {
1242 // If this window's app token is hidden and not animating,
1243 // it is of no interest to us.
1244 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1245 if (DEBUG_WALLPAPER) Log.v(TAG,
1246 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001247 continue;
1248 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001249 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001250 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1251 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1252 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001253 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001254 && (mWallpaperTarget == w
1255 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001256 if (DEBUG_WALLPAPER) Log.v(TAG,
1257 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001258 foundW = w;
1259 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001260 if (w == mWallpaperTarget && w.mAppToken != null
1261 && w.mAppToken.animation != null) {
1262 // The current wallpaper target is animating, so we'll
1263 // look behind it for another possible target and figure
1264 // out what is going on below.
1265 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1266 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001267 continue;
1268 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001269 break;
1270 }
1271 }
1272
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001273 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001274 // If we are currently waiting for an app transition, and either
1275 // the current target or the next target are involved with it,
1276 // then hold off on doing anything with the wallpaper.
1277 // Note that we are checking here for just whether the target
1278 // is part of an app token... which is potentially overly aggressive
1279 // (the app token may not be involved in the transition), but good
1280 // enough (we'll just wait until whatever transition is pending
1281 // executes).
1282 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001283 if (DEBUG_WALLPAPER) Log.v(TAG,
1284 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001285 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 }
1287 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001288 if (DEBUG_WALLPAPER) Log.v(TAG,
1289 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001290 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001291 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001292 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001293
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001294 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001295 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001296 Log.v(TAG, "New wallpaper target: " + foundW
1297 + " oldTarget: " + mWallpaperTarget);
1298 }
1299
1300 mLowerWallpaperTarget = null;
1301 mUpperWallpaperTarget = null;
1302
1303 WindowState oldW = mWallpaperTarget;
1304 mWallpaperTarget = foundW;
1305
1306 // Now what is happening... if the current and new targets are
1307 // animating, then we are in our super special mode!
1308 if (foundW != null && foundW.mAppToken != null && oldW != null
1309 && oldW.mAppToken != null) {
1310 if (DEBUG_WALLPAPER) {
1311 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1312 + " old animation: " + oldW.mAppToken.animation);
1313 }
1314 if (foundW.mAppToken.animation != null
1315 && oldW.mAppToken.animation != null) {
1316 int oldI = localmWindows.indexOf(oldW);
1317 if (DEBUG_WALLPAPER) {
1318 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1319 }
1320 if (oldI >= 0) {
1321 if (DEBUG_WALLPAPER) {
1322 Log.v(TAG, "Animating wallpapers: old#" + oldI
1323 + "=" + oldW + "; new#" + foundI
1324 + "=" + foundW);
1325 }
1326
1327 // Set the new target correctly.
1328 if (foundW.mAppToken.hiddenRequested) {
1329 if (DEBUG_WALLPAPER) {
1330 Log.v(TAG, "Old wallpaper still the target.");
1331 }
1332 mWallpaperTarget = oldW;
1333 }
1334
1335 // Now set the upper and lower wallpaper targets
1336 // correctly, and make sure that we are positioning
1337 // the wallpaper below the lower.
1338 if (foundI > oldI) {
1339 // The new target is on top of the old one.
1340 if (DEBUG_WALLPAPER) {
1341 Log.v(TAG, "Found target above old target.");
1342 }
1343 mUpperWallpaperTarget = foundW;
1344 mLowerWallpaperTarget = oldW;
1345 foundW = oldW;
1346 foundI = oldI;
1347 } else {
1348 // The new target is below the old one.
1349 if (DEBUG_WALLPAPER) {
1350 Log.v(TAG, "Found target below old target.");
1351 }
1352 mUpperWallpaperTarget = oldW;
1353 mLowerWallpaperTarget = foundW;
1354 }
1355 }
1356 }
1357 }
1358
1359 } else {
1360 // Is it time to stop animating?
1361 if (mLowerWallpaperTarget == null
1362 || mLowerWallpaperTarget.mAppToken.animation == null
1363 || mUpperWallpaperTarget == null
1364 || mUpperWallpaperTarget.mAppToken.animation == null) {
1365 if (DEBUG_WALLPAPER) {
1366 Log.v(TAG, "No longer animating wallpaper targets!");
1367 }
1368 mLowerWallpaperTarget = null;
1369 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001370 }
1371 }
1372
1373 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001374 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001375 // The window is visible to the compositor... but is it visible
1376 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001377 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001378 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001379
1380 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001381 // its layer adjustment. Only do this if we are not transfering
1382 // between two wallpaper targets.
1383 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001384 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001385 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001386
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001387 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1388 * TYPE_LAYER_MULTIPLIER
1389 + TYPE_LAYER_OFFSET;
1390
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001391 // Now w is the window we are supposed to be behind... but we
1392 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001393 // AND any starting window associated with it, AND below the
1394 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001395 while (foundI > 0) {
1396 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001397 if (wb.mBaseLayer < maxLayer &&
1398 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001399 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001400 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001401 // This window is not related to the previous one in any
1402 // interesting way, so stop here.
1403 break;
1404 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001405 foundW = wb;
1406 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001407 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001408 } else {
1409 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001410 }
1411
1412 // Okay i is the position immediately above the wallpaper. Look at
1413 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001414 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001415
Dianne Hackborn284ac932009-08-28 10:34:25 -07001416 if (visible) {
1417 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1418 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1419 }
1420
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001421 // Start stepping backwards from here, ensuring that our wallpaper windows
1422 // are correctly placed.
1423 int curTokenIndex = mWallpaperTokens.size();
1424 while (curTokenIndex > 0) {
1425 curTokenIndex--;
1426 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001427 if (token.hidden == visible) {
1428 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1429 token.hidden = !visible;
1430 // Need to do a layout to ensure the wallpaper now has the
1431 // correct size.
1432 mLayoutNeeded = true;
1433 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001434
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001435 int curWallpaperIndex = token.windows.size();
1436 while (curWallpaperIndex > 0) {
1437 curWallpaperIndex--;
1438 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001439
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001440 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001441 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001442 }
1443
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001444 // First, make sure the client has the current visibility
1445 // state.
1446 if (wallpaper.mWallpaperVisible != visible) {
1447 wallpaper.mWallpaperVisible = visible;
1448 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001449 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001450 "Setting visibility of wallpaper " + wallpaper
1451 + ": " + visible);
1452 wallpaper.mClient.dispatchAppVisibility(visible);
1453 } catch (RemoteException e) {
1454 }
1455 }
1456
1457 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001458 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1459 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001460
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001461 // First, if this window is at the current index, then all
1462 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001463 if (wallpaper == foundW) {
1464 foundI--;
1465 foundW = foundI > 0
1466 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001467 continue;
1468 }
1469
1470 // The window didn't match... the current wallpaper window,
1471 // wherever it is, is in the wrong place, so make sure it is
1472 // not in the list.
1473 int oldIndex = localmWindows.indexOf(wallpaper);
1474 if (oldIndex >= 0) {
1475 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001476 if (oldIndex < foundI) {
1477 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001478 }
1479 }
1480
1481 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001482 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001483 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001484
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001485 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001486 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001487 }
1488 }
1489
1490 return changed;
1491 }
1492
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001493 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001494 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1495 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001496 mWallpaperAnimLayerAdjustment = adj;
1497 int curTokenIndex = mWallpaperTokens.size();
1498 while (curTokenIndex > 0) {
1499 curTokenIndex--;
1500 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1501 int curWallpaperIndex = token.windows.size();
1502 while (curWallpaperIndex > 0) {
1503 curWallpaperIndex--;
1504 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1505 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001506 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1507 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001508 }
1509 }
1510 }
1511
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001512 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1513 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001514 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001515 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001516 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001517 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001518 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001519 changed = wallpaperWin.mXOffset != offset;
1520 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001521 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1522 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001523 wallpaperWin.mXOffset = offset;
1524 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001525 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1526 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001527 rawChanged = true;
1528 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001529 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001530
Dianne Hackborn284ac932009-08-28 10:34:25 -07001531 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001532 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001533 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001534 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001535 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1536 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001537 changed = true;
1538 wallpaperWin.mYOffset = offset;
1539 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001540 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1541 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001542 rawChanged = true;
1543 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001544 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001545
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001546 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001547 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001548 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1549 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1550 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001551 if (sync) {
1552 synchronized (mWaitingOnWallpaperLock) {
1553 mWaitingOnWallpaper = wallpaperWin;
1554 }
1555 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001556 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001557 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1558 if (sync) {
1559 synchronized (mWaitingOnWallpaperLock) {
1560 if (mWaitingOnWallpaper != null) {
1561 long start = SystemClock.uptimeMillis();
1562 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1563 < start) {
1564 try {
1565 if (DEBUG_WALLPAPER) Log.v(TAG,
1566 "Waiting for offset complete...");
1567 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1568 } catch (InterruptedException e) {
1569 }
1570 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1571 if ((start+WALLPAPER_TIMEOUT)
1572 < SystemClock.uptimeMillis()) {
1573 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1574 + wallpaperWin);
1575 mLastWallpaperTimeoutTime = start;
1576 }
1577 }
1578 mWaitingOnWallpaper = null;
1579 }
1580 }
1581 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001582 } catch (RemoteException e) {
1583 }
1584 }
1585
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001586 return changed;
1587 }
1588
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001589 void wallpaperOffsetsComplete(IBinder window) {
1590 synchronized (mWaitingOnWallpaperLock) {
1591 if (mWaitingOnWallpaper != null &&
1592 mWaitingOnWallpaper.mClient.asBinder() == window) {
1593 mWaitingOnWallpaper = null;
1594 mWaitingOnWallpaperLock.notifyAll();
1595 }
1596 }
1597 }
1598
1599 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001600 final int dw = mDisplay.getWidth();
1601 final int dh = mDisplay.getHeight();
1602
1603 boolean changed = false;
1604
1605 WindowState target = mWallpaperTarget;
1606 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001607 mLastWallpaperX = target.mWallpaperX;
1608 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001609 int curTokenIndex = mWallpaperTokens.size();
1610 while (curTokenIndex > 0) {
1611 curTokenIndex--;
1612 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1613 int curWallpaperIndex = token.windows.size();
1614 while (curWallpaperIndex > 0) {
1615 curWallpaperIndex--;
1616 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001617 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001618 wallpaper.computeShownFrameLocked();
1619 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 // We only want to be synchronous with one wallpaper.
1621 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001622 }
1623 }
1624 }
1625 }
1626
1627 return changed;
1628 }
1629
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001630 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001631 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001632 final int dw = mDisplay.getWidth();
1633 final int dh = mDisplay.getHeight();
1634
1635 int curTokenIndex = mWallpaperTokens.size();
1636 while (curTokenIndex > 0) {
1637 curTokenIndex--;
1638 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001639 if (token.hidden == visible) {
1640 token.hidden = !visible;
1641 // Need to do a layout to ensure the wallpaper now has the
1642 // correct size.
1643 mLayoutNeeded = true;
1644 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001645
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001646 int curWallpaperIndex = token.windows.size();
1647 while (curWallpaperIndex > 0) {
1648 curWallpaperIndex--;
1649 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1650 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001651 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001652 }
1653
1654 if (wallpaper.mWallpaperVisible != visible) {
1655 wallpaper.mWallpaperVisible = visible;
1656 try {
1657 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001658 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001659 + ": " + visible);
1660 wallpaper.mClient.dispatchAppVisibility(visible);
1661 } catch (RemoteException e) {
1662 }
1663 }
1664 }
1665 }
1666 }
1667
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001668 void sendPointerToWallpaperLocked(WindowState srcWin,
1669 MotionEvent pointer, long eventTime) {
1670 int curTokenIndex = mWallpaperTokens.size();
1671 while (curTokenIndex > 0) {
1672 curTokenIndex--;
1673 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1674 int curWallpaperIndex = token.windows.size();
1675 while (curWallpaperIndex > 0) {
1676 curWallpaperIndex--;
1677 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1678 if ((wallpaper.mAttrs.flags &
1679 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1680 continue;
1681 }
1682 try {
1683 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1684 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1685 srcWin.mFrame.top-wallpaper.mFrame.top);
1686 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1687 } catch (RemoteException e) {
1688 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1689 }
1690 }
1691 }
1692 }
1693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 public int addWindow(Session session, IWindow client,
1695 WindowManager.LayoutParams attrs, int viewVisibility,
1696 Rect outContentInsets) {
1697 int res = mPolicy.checkAddPermission(attrs);
1698 if (res != WindowManagerImpl.ADD_OKAY) {
1699 return res;
1700 }
Romain Guy06882f82009-06-10 13:36:04 -07001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 boolean reportNewConfig = false;
1703 WindowState attachedWindow = null;
1704 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 synchronized(mWindowMap) {
1707 // Instantiating a Display requires talking with the simulator,
1708 // so don't do it until we know the system is mostly up and
1709 // running.
1710 if (mDisplay == null) {
1711 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1712 mDisplay = wm.getDefaultDisplay();
1713 mQueue.setDisplay(mDisplay);
1714 reportNewConfig = true;
1715 }
Romain Guy06882f82009-06-10 13:36:04 -07001716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 if (mWindowMap.containsKey(client.asBinder())) {
1718 Log.w(TAG, "Window " + client + " is already added");
1719 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1720 }
1721
1722 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001723 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 if (attachedWindow == null) {
1725 Log.w(TAG, "Attempted to add window with token that is not a window: "
1726 + attrs.token + ". Aborting.");
1727 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1728 }
1729 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1730 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1731 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1732 + attrs.token + ". Aborting.");
1733 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1734 }
1735 }
1736
1737 boolean addToken = false;
1738 WindowToken token = mTokenMap.get(attrs.token);
1739 if (token == null) {
1740 if (attrs.type >= FIRST_APPLICATION_WINDOW
1741 && attrs.type <= LAST_APPLICATION_WINDOW) {
1742 Log.w(TAG, "Attempted to add application window with unknown token "
1743 + attrs.token + ". Aborting.");
1744 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1745 }
1746 if (attrs.type == TYPE_INPUT_METHOD) {
1747 Log.w(TAG, "Attempted to add input method window with unknown token "
1748 + attrs.token + ". Aborting.");
1749 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1750 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001751 if (attrs.type == TYPE_WALLPAPER) {
1752 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1753 + attrs.token + ". Aborting.");
1754 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 token = new WindowToken(attrs.token, -1, false);
1757 addToken = true;
1758 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1759 && attrs.type <= LAST_APPLICATION_WINDOW) {
1760 AppWindowToken atoken = token.appWindowToken;
1761 if (atoken == null) {
1762 Log.w(TAG, "Attempted to add window with non-application token "
1763 + token + ". Aborting.");
1764 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1765 } else if (atoken.removed) {
1766 Log.w(TAG, "Attempted to add window with exiting application token "
1767 + token + ". Aborting.");
1768 return WindowManagerImpl.ADD_APP_EXITING;
1769 }
1770 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1771 // No need for this guy!
1772 if (localLOGV) Log.v(
1773 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1774 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1775 }
1776 } else if (attrs.type == TYPE_INPUT_METHOD) {
1777 if (token.windowType != TYPE_INPUT_METHOD) {
1778 Log.w(TAG, "Attempted to add input method window with bad token "
1779 + attrs.token + ". Aborting.");
1780 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1781 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001782 } else if (attrs.type == TYPE_WALLPAPER) {
1783 if (token.windowType != TYPE_WALLPAPER) {
1784 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1785 + attrs.token + ". Aborting.");
1786 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789
1790 win = new WindowState(session, client, token,
1791 attachedWindow, attrs, viewVisibility);
1792 if (win.mDeathRecipient == null) {
1793 // Client has apparently died, so there is no reason to
1794 // continue.
1795 Log.w(TAG, "Adding window client " + client.asBinder()
1796 + " that is dead, aborting.");
1797 return WindowManagerImpl.ADD_APP_EXITING;
1798 }
1799
1800 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 res = mPolicy.prepareAddWindowLw(win, attrs);
1803 if (res != WindowManagerImpl.ADD_OKAY) {
1804 return res;
1805 }
1806
1807 // From now on, no exceptions or errors allowed!
1808
1809 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 if (addToken) {
1814 mTokenMap.put(attrs.token, token);
1815 mTokenList.add(token);
1816 }
1817 win.attach();
1818 mWindowMap.put(client.asBinder(), win);
1819
1820 if (attrs.type == TYPE_APPLICATION_STARTING &&
1821 token.appWindowToken != null) {
1822 token.appWindowToken.startingWindow = win;
1823 }
1824
1825 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 if (attrs.type == TYPE_INPUT_METHOD) {
1828 mInputMethodWindow = win;
1829 addInputMethodWindowToListLocked(win);
1830 imMayMove = false;
1831 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1832 mInputMethodDialogs.add(win);
1833 addWindowToListInOrderLocked(win, true);
1834 adjustInputMethodDialogsLocked();
1835 imMayMove = false;
1836 } else {
1837 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001838 if (attrs.type == TYPE_WALLPAPER) {
1839 mLastWallpaperTimeoutTime = 0;
1840 adjustWallpaperWindowsLocked();
1841 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001842 adjustWallpaperWindowsLocked();
1843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
Romain Guy06882f82009-06-10 13:36:04 -07001845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 if (mInTouchMode) {
1851 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1852 }
1853 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1854 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1855 }
Romain Guy06882f82009-06-10 13:36:04 -07001856
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001857 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001859 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1860 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 imMayMove = false;
1862 }
1863 }
Romain Guy06882f82009-06-10 13:36:04 -07001864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001866 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
Romain Guy06882f82009-06-10 13:36:04 -07001868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 assignLayersLocked();
1870 // Don't do layout here, the window must call
1871 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 //dump();
1874
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001875 if (focusChanged) {
1876 if (mCurrentFocus != null) {
1877 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1878 }
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (localLOGV) Log.v(
1881 TAG, "New client " + client.asBinder()
1882 + ": window=" + win);
1883 }
1884
1885 // sendNewConfiguration() checks caller permissions so we must call it with
1886 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1887 // identity anyway, so it's safe to just clear & restore around this whole
1888 // block.
1889 final long origId = Binder.clearCallingIdentity();
1890 if (reportNewConfig) {
1891 sendNewConfiguration();
1892 } else {
1893 // Update Orientation after adding a window, only if the window needs to be
1894 // displayed right away
1895 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001896 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 sendNewConfiguration();
1898 }
1899 }
1900 }
1901 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 return res;
1904 }
Romain Guy06882f82009-06-10 13:36:04 -07001905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 public void removeWindow(Session session, IWindow client) {
1907 synchronized(mWindowMap) {
1908 WindowState win = windowForClientLocked(session, client);
1909 if (win == null) {
1910 return;
1911 }
1912 removeWindowLocked(session, win);
1913 }
1914 }
Romain Guy06882f82009-06-10 13:36:04 -07001915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 public void removeWindowLocked(Session session, WindowState win) {
1917
1918 if (localLOGV || DEBUG_FOCUS) Log.v(
1919 TAG, "Remove " + win + " client="
1920 + Integer.toHexString(System.identityHashCode(
1921 win.mClient.asBinder()))
1922 + ", surface=" + win.mSurface);
1923
1924 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if (DEBUG_APP_TRANSITIONS) Log.v(
1927 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1928 + " mExiting=" + win.mExiting
1929 + " isAnimating=" + win.isAnimating()
1930 + " app-animation="
1931 + (win.mAppToken != null ? win.mAppToken.animation : null)
1932 + " inPendingTransaction="
1933 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1934 + " mDisplayFrozen=" + mDisplayFrozen);
1935 // Visibility of the removed window. Will be used later to update orientation later on.
1936 boolean wasVisible = false;
1937 // First, see if we need to run an animation. If we do, we have
1938 // to hold off on removing the window until the animation is done.
1939 // If the display is frozen, just remove immediately, since the
1940 // animation wouldn't be seen.
1941 if (win.mSurface != null && !mDisplayFrozen) {
1942 // If we are not currently running the exit animation, we
1943 // need to see about starting one.
1944 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1947 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1948 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1949 }
1950 // Try starting an animation.
1951 if (applyAnimationLocked(win, transit, false)) {
1952 win.mExiting = true;
1953 }
1954 }
1955 if (win.mExiting || win.isAnimating()) {
1956 // The exit animation is running... wait for it!
1957 //Log.i(TAG, "*** Running exit animation...");
1958 win.mExiting = true;
1959 win.mRemoveOnExit = true;
1960 mLayoutNeeded = true;
1961 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1962 performLayoutAndPlaceSurfacesLocked();
1963 if (win.mAppToken != null) {
1964 win.mAppToken.updateReportedVisibilityLocked();
1965 }
1966 //dump();
1967 Binder.restoreCallingIdentity(origId);
1968 return;
1969 }
1970 }
1971
1972 removeWindowInnerLocked(session, win);
1973 // Removing a visible window will effect the computed orientation
1974 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001975 if (wasVisible && computeForcedAppOrientationLocked()
1976 != mForcedAppOrientation) {
1977 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1980 Binder.restoreCallingIdentity(origId);
1981 }
Romain Guy06882f82009-06-10 13:36:04 -07001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001984 mKeyWaiter.finishedKey(session, win.mClient, true,
1985 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1987 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 if (mInputMethodTarget == win) {
1992 moveInputMethodWindowsIfNeededLocked(false);
1993 }
Romain Guy06882f82009-06-10 13:36:04 -07001994
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001995 if (false) {
1996 RuntimeException e = new RuntimeException("here");
1997 e.fillInStackTrace();
1998 Log.w(TAG, "Removing window " + win, e);
1999 }
2000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 mPolicy.removeWindowLw(win);
2002 win.removeLocked();
2003
2004 mWindowMap.remove(win.mClient.asBinder());
2005 mWindows.remove(win);
2006
2007 if (mInputMethodWindow == win) {
2008 mInputMethodWindow = null;
2009 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2010 mInputMethodDialogs.remove(win);
2011 }
Romain Guy06882f82009-06-10 13:36:04 -07002012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 final WindowToken token = win.mToken;
2014 final AppWindowToken atoken = win.mAppToken;
2015 token.windows.remove(win);
2016 if (atoken != null) {
2017 atoken.allAppWindows.remove(win);
2018 }
2019 if (localLOGV) Log.v(
2020 TAG, "**** Removing window " + win + ": count="
2021 + token.windows.size());
2022 if (token.windows.size() == 0) {
2023 if (!token.explicit) {
2024 mTokenMap.remove(token.token);
2025 mTokenList.remove(token);
2026 } else if (atoken != null) {
2027 atoken.firstWindowDrawn = false;
2028 }
2029 }
2030
2031 if (atoken != null) {
2032 if (atoken.startingWindow == win) {
2033 atoken.startingWindow = null;
2034 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2035 // If this is the last window and we had requested a starting
2036 // transition window, well there is no point now.
2037 atoken.startingData = null;
2038 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2039 // If this is the last window except for a starting transition
2040 // window, we need to get rid of the starting transition.
2041 if (DEBUG_STARTING_WINDOW) {
2042 Log.v(TAG, "Schedule remove starting " + token
2043 + ": no more real windows");
2044 }
2045 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2046 mH.sendMessage(m);
2047 }
2048 }
Romain Guy06882f82009-06-10 13:36:04 -07002049
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002050 if (win.mAttrs.type == TYPE_WALLPAPER) {
2051 mLastWallpaperTimeoutTime = 0;
2052 adjustWallpaperWindowsLocked();
2053 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002054 adjustWallpaperWindowsLocked();
2055 }
2056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 if (!mInLayout) {
2058 assignLayersLocked();
2059 mLayoutNeeded = true;
2060 performLayoutAndPlaceSurfacesLocked();
2061 if (win.mAppToken != null) {
2062 win.mAppToken.updateReportedVisibilityLocked();
2063 }
2064 }
2065 }
2066
2067 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2068 long origId = Binder.clearCallingIdentity();
2069 try {
2070 synchronized (mWindowMap) {
2071 WindowState w = windowForClientLocked(session, client);
2072 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002073 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 Surface.openTransaction();
2075 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002076 if (SHOW_TRANSACTIONS) Log.i(
2077 TAG, " SURFACE " + w.mSurface
2078 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 w.mSurface.setTransparentRegionHint(region);
2080 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002081 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 Surface.closeTransaction();
2083 }
2084 }
2085 }
2086 } finally {
2087 Binder.restoreCallingIdentity(origId);
2088 }
2089 }
2090
2091 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002092 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 Rect visibleInsets) {
2094 long origId = Binder.clearCallingIdentity();
2095 try {
2096 synchronized (mWindowMap) {
2097 WindowState w = windowForClientLocked(session, client);
2098 if (w != null) {
2099 w.mGivenInsetsPending = false;
2100 w.mGivenContentInsets.set(contentInsets);
2101 w.mGivenVisibleInsets.set(visibleInsets);
2102 w.mTouchableInsets = touchableInsets;
2103 mLayoutNeeded = true;
2104 performLayoutAndPlaceSurfacesLocked();
2105 }
2106 }
2107 } finally {
2108 Binder.restoreCallingIdentity(origId);
2109 }
2110 }
Romain Guy06882f82009-06-10 13:36:04 -07002111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 public void getWindowDisplayFrame(Session session, IWindow client,
2113 Rect outDisplayFrame) {
2114 synchronized(mWindowMap) {
2115 WindowState win = windowForClientLocked(session, client);
2116 if (win == null) {
2117 outDisplayFrame.setEmpty();
2118 return;
2119 }
2120 outDisplayFrame.set(win.mDisplayFrame);
2121 }
2122 }
2123
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002124 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2125 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2126 window.mWallpaperX = x;
2127 window.mWallpaperY = y;
2128
2129 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002130 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002131 performLayoutAndPlaceSurfacesLocked();
2132 }
2133 }
2134 }
2135 }
2136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 public int relayoutWindow(Session session, IWindow client,
2138 WindowManager.LayoutParams attrs, int requestedWidth,
2139 int requestedHeight, int viewVisibility, boolean insetsPending,
2140 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2141 Surface outSurface) {
2142 boolean displayed = false;
2143 boolean inTouchMode;
2144 Configuration newConfig = null;
2145 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 synchronized(mWindowMap) {
2148 WindowState win = windowForClientLocked(session, client);
2149 if (win == null) {
2150 return 0;
2151 }
2152 win.mRequestedWidth = requestedWidth;
2153 win.mRequestedHeight = requestedHeight;
2154
2155 if (attrs != null) {
2156 mPolicy.adjustWindowParamsLw(attrs);
2157 }
Romain Guy06882f82009-06-10 13:36:04 -07002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 int attrChanges = 0;
2160 int flagChanges = 0;
2161 if (attrs != null) {
2162 flagChanges = win.mAttrs.flags ^= attrs.flags;
2163 attrChanges = win.mAttrs.copyFrom(attrs);
2164 }
2165
2166 if (localLOGV) Log.v(
2167 TAG, "Relayout given client " + client.asBinder()
2168 + " (" + win.mAttrs.getTitle() + ")");
2169
2170
2171 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2172 win.mAlpha = attrs.alpha;
2173 }
2174
2175 final boolean scaledWindow =
2176 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2177
2178 if (scaledWindow) {
2179 // requested{Width|Height} Surface's physical size
2180 // attrs.{width|height} Size on screen
2181 win.mHScale = (attrs.width != requestedWidth) ?
2182 (attrs.width / (float)requestedWidth) : 1.0f;
2183 win.mVScale = (attrs.height != requestedHeight) ?
2184 (attrs.height / (float)requestedHeight) : 1.0f;
2185 }
2186
2187 boolean imMayMove = (flagChanges&(
2188 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2189 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 boolean focusMayChange = win.mViewVisibility != viewVisibility
2192 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2193 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002194
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002195 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2196 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 win.mRelayoutCalled = true;
2199 final int oldVisibility = win.mViewVisibility;
2200 win.mViewVisibility = viewVisibility;
2201 if (viewVisibility == View.VISIBLE &&
2202 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2203 displayed = !win.isVisibleLw();
2204 if (win.mExiting) {
2205 win.mExiting = false;
2206 win.mAnimation = null;
2207 }
2208 if (win.mDestroying) {
2209 win.mDestroying = false;
2210 mDestroySurface.remove(win);
2211 }
2212 if (oldVisibility == View.GONE) {
2213 win.mEnterAnimationPending = true;
2214 }
2215 if (displayed && win.mSurface != null && !win.mDrawPending
2216 && !win.mCommitDrawPending && !mDisplayFrozen) {
2217 applyEnterAnimationLocked(win);
2218 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002219 if (displayed && (win.mAttrs.flags
2220 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2221 win.mTurnOnScreen = true;
2222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2224 // To change the format, we need to re-build the surface.
2225 win.destroySurfaceLocked();
2226 displayed = true;
2227 }
2228 try {
2229 Surface surface = win.createSurfaceLocked();
2230 if (surface != null) {
2231 outSurface.copyFrom(surface);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002232 if (SHOW_TRANSACTIONS) Log.i(TAG,
2233 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002235 // For some reason there isn't a surface. Clear the
2236 // caller's object so they see the same state.
2237 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
2239 } catch (Exception e) {
2240 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002241 + client + " (" + win.mAttrs.getTitle() + ")",
2242 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 Binder.restoreCallingIdentity(origId);
2244 return 0;
2245 }
2246 if (displayed) {
2247 focusMayChange = true;
2248 }
2249 if (win.mAttrs.type == TYPE_INPUT_METHOD
2250 && mInputMethodWindow == null) {
2251 mInputMethodWindow = win;
2252 imMayMove = true;
2253 }
2254 } else {
2255 win.mEnterAnimationPending = false;
2256 if (win.mSurface != null) {
2257 // If we are not currently running the exit animation, we
2258 // need to see about starting one.
2259 if (!win.mExiting) {
2260 // Try starting an animation; if there isn't one, we
2261 // can destroy the surface right away.
2262 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2263 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2264 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2265 }
2266 if (win.isWinVisibleLw() &&
2267 applyAnimationLocked(win, transit, false)) {
2268 win.mExiting = true;
2269 mKeyWaiter.finishedKey(session, client, true,
2270 KeyWaiter.RETURN_NOTHING);
2271 } else if (win.isAnimating()) {
2272 // Currently in a hide animation... turn this into
2273 // an exit.
2274 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002275 } else if (win == mWallpaperTarget) {
2276 // If the wallpaper is currently behind this
2277 // window, we need to change both of them inside
2278 // of a transaction to avoid artifacts.
2279 win.mExiting = true;
2280 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 } else {
2282 if (mInputMethodWindow == win) {
2283 mInputMethodWindow = null;
2284 }
2285 win.destroySurfaceLocked();
2286 }
2287 }
2288 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002289 // We are being called from a local process, which
2290 // means outSurface holds its current surface. Ensure the
2291 // surface object is cleared, but we don't want it actually
2292 // destroyed at this point.
2293 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
2295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 if (focusMayChange) {
2297 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2298 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 imMayMove = false;
2300 }
2301 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2302 }
Romain Guy06882f82009-06-10 13:36:04 -07002303
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002304 // updateFocusedWindowLocked() already assigned layers so we only need to
2305 // reassign them at this point if the IM window state gets shuffled
2306 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 if (imMayMove) {
2309 if (moveInputMethodWindowsIfNeededLocked(false)) {
2310 assignLayers = true;
2311 }
2312 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002313 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002314 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002315 assignLayers = true;
2316 }
2317 }
Romain Guy06882f82009-06-10 13:36:04 -07002318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 mLayoutNeeded = true;
2320 win.mGivenInsetsPending = insetsPending;
2321 if (assignLayers) {
2322 assignLayersLocked();
2323 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002324 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002326 if (displayed && win.mIsWallpaper) {
2327 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002328 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 if (win.mAppToken != null) {
2331 win.mAppToken.updateReportedVisibilityLocked();
2332 }
2333 outFrame.set(win.mFrame);
2334 outContentInsets.set(win.mContentInsets);
2335 outVisibleInsets.set(win.mVisibleInsets);
2336 if (localLOGV) Log.v(
2337 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002338 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 + ", requestedHeight=" + requestedHeight
2340 + ", viewVisibility=" + viewVisibility
2341 + "\nRelayout returning frame=" + outFrame
2342 + ", surface=" + outSurface);
2343
2344 if (localLOGV || DEBUG_FOCUS) Log.v(
2345 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2346
2347 inTouchMode = mInTouchMode;
2348 }
2349
2350 if (newConfig != null) {
2351 sendNewConfiguration();
2352 }
Romain Guy06882f82009-06-10 13:36:04 -07002353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2357 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2358 }
2359
2360 public void finishDrawingWindow(Session session, IWindow client) {
2361 final long origId = Binder.clearCallingIdentity();
2362 synchronized(mWindowMap) {
2363 WindowState win = windowForClientLocked(session, client);
2364 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002365 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2366 adjustWallpaperWindowsLocked();
2367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 mLayoutNeeded = true;
2369 performLayoutAndPlaceSurfacesLocked();
2370 }
2371 }
2372 Binder.restoreCallingIdentity(origId);
2373 }
2374
2375 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2376 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2377 + (lp != null ? lp.packageName : null)
2378 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2379 if (lp != null && lp.windowAnimations != 0) {
2380 // If this is a system resource, don't try to load it from the
2381 // application resources. It is nice to avoid loading application
2382 // resources if we can.
2383 String packageName = lp.packageName != null ? lp.packageName : "android";
2384 int resId = lp.windowAnimations;
2385 if ((resId&0xFF000000) == 0x01000000) {
2386 packageName = "android";
2387 }
2388 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2389 + packageName);
2390 return AttributeCache.instance().get(packageName, resId,
2391 com.android.internal.R.styleable.WindowAnimation);
2392 }
2393 return null;
2394 }
Romain Guy06882f82009-06-10 13:36:04 -07002395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 private void applyEnterAnimationLocked(WindowState win) {
2397 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2398 if (win.mEnterAnimationPending) {
2399 win.mEnterAnimationPending = false;
2400 transit = WindowManagerPolicy.TRANSIT_ENTER;
2401 }
2402
2403 applyAnimationLocked(win, transit, true);
2404 }
2405
2406 private boolean applyAnimationLocked(WindowState win,
2407 int transit, boolean isEntrance) {
2408 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2409 // If we are trying to apply an animation, but already running
2410 // an animation of the same type, then just leave that one alone.
2411 return true;
2412 }
Romain Guy06882f82009-06-10 13:36:04 -07002413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 // Only apply an animation if the display isn't frozen. If it is
2415 // frozen, there is no reason to animate and it can cause strange
2416 // artifacts when we unfreeze the display if some different animation
2417 // is running.
2418 if (!mDisplayFrozen) {
2419 int anim = mPolicy.selectAnimationLw(win, transit);
2420 int attr = -1;
2421 Animation a = null;
2422 if (anim != 0) {
2423 a = AnimationUtils.loadAnimation(mContext, anim);
2424 } else {
2425 switch (transit) {
2426 case WindowManagerPolicy.TRANSIT_ENTER:
2427 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2428 break;
2429 case WindowManagerPolicy.TRANSIT_EXIT:
2430 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2431 break;
2432 case WindowManagerPolicy.TRANSIT_SHOW:
2433 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2434 break;
2435 case WindowManagerPolicy.TRANSIT_HIDE:
2436 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2437 break;
2438 }
2439 if (attr >= 0) {
2440 a = loadAnimation(win.mAttrs, attr);
2441 }
2442 }
2443 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2444 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2445 + " mAnimation=" + win.mAnimation
2446 + " isEntrance=" + isEntrance);
2447 if (a != null) {
2448 if (DEBUG_ANIM) {
2449 RuntimeException e = new RuntimeException();
2450 e.fillInStackTrace();
2451 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2452 }
2453 win.setAnimation(a);
2454 win.mAnimationIsEntrance = isEntrance;
2455 }
2456 } else {
2457 win.clearAnimation();
2458 }
2459
2460 return win.mAnimation != null;
2461 }
2462
2463 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2464 int anim = 0;
2465 Context context = mContext;
2466 if (animAttr >= 0) {
2467 AttributeCache.Entry ent = getCachedAnimations(lp);
2468 if (ent != null) {
2469 context = ent.context;
2470 anim = ent.array.getResourceId(animAttr, 0);
2471 }
2472 }
2473 if (anim != 0) {
2474 return AnimationUtils.loadAnimation(context, anim);
2475 }
2476 return null;
2477 }
Romain Guy06882f82009-06-10 13:36:04 -07002478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 private boolean applyAnimationLocked(AppWindowToken wtoken,
2480 WindowManager.LayoutParams lp, int transit, boolean enter) {
2481 // Only apply an animation if the display isn't frozen. If it is
2482 // frozen, there is no reason to animate and it can cause strange
2483 // artifacts when we unfreeze the display if some different animation
2484 // is running.
2485 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002486 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002487 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002488 a = new FadeInOutAnimation(enter);
2489 if (DEBUG_ANIM) Log.v(TAG,
2490 "applying FadeInOutAnimation for a window in compatibility mode");
2491 } else {
2492 int animAttr = 0;
2493 switch (transit) {
2494 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2495 animAttr = enter
2496 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2497 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2498 break;
2499 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2500 animAttr = enter
2501 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2502 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2503 break;
2504 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2505 animAttr = enter
2506 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2507 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2508 break;
2509 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2510 animAttr = enter
2511 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2512 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2513 break;
2514 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2515 animAttr = enter
2516 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2517 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2518 break;
2519 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2520 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002521 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002522 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2523 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002524 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002525 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002526 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2527 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002528 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002529 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002530 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002531 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2532 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2533 break;
2534 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2535 animAttr = enter
2536 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2537 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2538 break;
2539 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2540 animAttr = enter
2541 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2542 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002543 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002544 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002545 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002546 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2547 + " anim=" + a
2548 + " animAttr=0x" + Integer.toHexString(animAttr)
2549 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 if (a != null) {
2552 if (DEBUG_ANIM) {
2553 RuntimeException e = new RuntimeException();
2554 e.fillInStackTrace();
2555 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2556 }
2557 wtoken.setAnimation(a);
2558 }
2559 } else {
2560 wtoken.clearAnimation();
2561 }
2562
2563 return wtoken.animation != null;
2564 }
2565
2566 // -------------------------------------------------------------
2567 // Application Window Tokens
2568 // -------------------------------------------------------------
2569
2570 public void validateAppTokens(List tokens) {
2571 int v = tokens.size()-1;
2572 int m = mAppTokens.size()-1;
2573 while (v >= 0 && m >= 0) {
2574 AppWindowToken wtoken = mAppTokens.get(m);
2575 if (wtoken.removed) {
2576 m--;
2577 continue;
2578 }
2579 if (tokens.get(v) != wtoken.token) {
2580 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2581 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2582 }
2583 v--;
2584 m--;
2585 }
2586 while (v >= 0) {
2587 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2588 v--;
2589 }
2590 while (m >= 0) {
2591 AppWindowToken wtoken = mAppTokens.get(m);
2592 if (!wtoken.removed) {
2593 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2594 }
2595 m--;
2596 }
2597 }
2598
2599 boolean checkCallingPermission(String permission, String func) {
2600 // Quick check: if the calling permission is me, it's all okay.
2601 if (Binder.getCallingPid() == Process.myPid()) {
2602 return true;
2603 }
Romain Guy06882f82009-06-10 13:36:04 -07002604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 if (mContext.checkCallingPermission(permission)
2606 == PackageManager.PERMISSION_GRANTED) {
2607 return true;
2608 }
2609 String msg = "Permission Denial: " + func + " from pid="
2610 + Binder.getCallingPid()
2611 + ", uid=" + Binder.getCallingUid()
2612 + " requires " + permission;
2613 Log.w(TAG, msg);
2614 return false;
2615 }
Romain Guy06882f82009-06-10 13:36:04 -07002616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 AppWindowToken findAppWindowToken(IBinder token) {
2618 WindowToken wtoken = mTokenMap.get(token);
2619 if (wtoken == null) {
2620 return null;
2621 }
2622 return wtoken.appWindowToken;
2623 }
Romain Guy06882f82009-06-10 13:36:04 -07002624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 public void addWindowToken(IBinder token, int type) {
2626 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2627 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002628 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 }
Romain Guy06882f82009-06-10 13:36:04 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 synchronized(mWindowMap) {
2632 WindowToken wtoken = mTokenMap.get(token);
2633 if (wtoken != null) {
2634 Log.w(TAG, "Attempted to add existing input method token: " + token);
2635 return;
2636 }
2637 wtoken = new WindowToken(token, type, true);
2638 mTokenMap.put(token, wtoken);
2639 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002640 if (type == TYPE_WALLPAPER) {
2641 mWallpaperTokens.add(wtoken);
2642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 }
2644 }
Romain Guy06882f82009-06-10 13:36:04 -07002645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 public void removeWindowToken(IBinder token) {
2647 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2648 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002649 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 }
2651
2652 final long origId = Binder.clearCallingIdentity();
2653 synchronized(mWindowMap) {
2654 WindowToken wtoken = mTokenMap.remove(token);
2655 mTokenList.remove(wtoken);
2656 if (wtoken != null) {
2657 boolean delayed = false;
2658 if (!wtoken.hidden) {
2659 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 final int N = wtoken.windows.size();
2662 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 for (int i=0; i<N; i++) {
2665 WindowState win = wtoken.windows.get(i);
2666
2667 if (win.isAnimating()) {
2668 delayed = true;
2669 }
Romain Guy06882f82009-06-10 13:36:04 -07002670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 if (win.isVisibleNow()) {
2672 applyAnimationLocked(win,
2673 WindowManagerPolicy.TRANSIT_EXIT, false);
2674 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2675 KeyWaiter.RETURN_NOTHING);
2676 changed = true;
2677 }
2678 }
2679
2680 if (changed) {
2681 mLayoutNeeded = true;
2682 performLayoutAndPlaceSurfacesLocked();
2683 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2684 }
Romain Guy06882f82009-06-10 13:36:04 -07002685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 if (delayed) {
2687 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002688 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2689 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
2691 }
Romain Guy06882f82009-06-10 13:36:04 -07002692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 } else {
2694 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2695 }
2696 }
2697 Binder.restoreCallingIdentity(origId);
2698 }
2699
2700 public void addAppToken(int addPos, IApplicationToken token,
2701 int groupId, int requestedOrientation, boolean fullscreen) {
2702 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2703 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002704 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 }
Romain Guy06882f82009-06-10 13:36:04 -07002706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 synchronized(mWindowMap) {
2708 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2709 if (wtoken != null) {
2710 Log.w(TAG, "Attempted to add existing app token: " + token);
2711 return;
2712 }
2713 wtoken = new AppWindowToken(token);
2714 wtoken.groupId = groupId;
2715 wtoken.appFullscreen = fullscreen;
2716 wtoken.requestedOrientation = requestedOrientation;
2717 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002718 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 mTokenMap.put(token.asBinder(), wtoken);
2720 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 // Application tokens start out hidden.
2723 wtoken.hidden = true;
2724 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 //dump();
2727 }
2728 }
Romain Guy06882f82009-06-10 13:36:04 -07002729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 public void setAppGroupId(IBinder token, int groupId) {
2731 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2732 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002733 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 }
2735
2736 synchronized(mWindowMap) {
2737 AppWindowToken wtoken = findAppWindowToken(token);
2738 if (wtoken == null) {
2739 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2740 return;
2741 }
2742 wtoken.groupId = groupId;
2743 }
2744 }
Romain Guy06882f82009-06-10 13:36:04 -07002745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 public int getOrientationFromWindowsLocked() {
2747 int pos = mWindows.size() - 1;
2748 while (pos >= 0) {
2749 WindowState wtoken = (WindowState) mWindows.get(pos);
2750 pos--;
2751 if (wtoken.mAppToken != null) {
2752 // We hit an application window. so the orientation will be determined by the
2753 // app window. No point in continuing further.
2754 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2755 }
2756 if (!wtoken.isVisibleLw()) {
2757 continue;
2758 }
2759 int req = wtoken.mAttrs.screenOrientation;
2760 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2761 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2762 continue;
2763 } else {
2764 return req;
2765 }
2766 }
2767 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2768 }
Romain Guy06882f82009-06-10 13:36:04 -07002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 public int getOrientationFromAppTokensLocked() {
2771 int pos = mAppTokens.size() - 1;
2772 int curGroup = 0;
2773 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002774 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002776 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 while (pos >= 0) {
2778 AppWindowToken wtoken = mAppTokens.get(pos);
2779 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002780 // if we're about to tear down this window and not seek for
2781 // the behind activity, don't use it for orientation
2782 if (!findingBehind
2783 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002784 continue;
2785 }
2786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 if (!haveGroup) {
2788 // We ignore any hidden applications on the top.
2789 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2790 continue;
2791 }
2792 haveGroup = true;
2793 curGroup = wtoken.groupId;
2794 lastOrientation = wtoken.requestedOrientation;
2795 } else if (curGroup != wtoken.groupId) {
2796 // If we have hit a new application group, and the bottom
2797 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002798 // the orientation behind it, and the last app was
2799 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002801 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2802 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 return lastOrientation;
2804 }
2805 }
2806 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002807 // If this application is fullscreen, and didn't explicitly say
2808 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002810 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002811 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002812 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 return or;
2814 }
2815 // If this application has requested an explicit orientation,
2816 // then use it.
2817 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2818 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2819 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2820 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2821 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2822 return or;
2823 }
Owen Lin3413b892009-05-01 17:12:32 -07002824 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 }
2826 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2827 }
Romain Guy06882f82009-06-10 13:36:04 -07002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002830 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002831 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2832 "updateOrientationFromAppTokens()")) {
2833 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2834 }
2835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 Configuration config;
2837 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002838 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2839 freezeThisOneIfNeeded);
2840 Binder.restoreCallingIdentity(ident);
2841 return config;
2842 }
2843
2844 Configuration updateOrientationFromAppTokensUnchecked(
2845 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2846 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002848 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002849 if (config != null) {
2850 mLayoutNeeded = true;
2851 performLayoutAndPlaceSurfacesLocked();
2852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 return config;
2855 }
Romain Guy06882f82009-06-10 13:36:04 -07002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 /*
2858 * The orientation is computed from non-application windows first. If none of
2859 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002860 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2862 * android.os.IBinder)
2863 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002864 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002865 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 long ident = Binder.clearCallingIdentity();
2868 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002869 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 if (req != mForcedAppOrientation) {
2872 changed = true;
2873 mForcedAppOrientation = req;
2874 //send a message to Policy indicating orientation change to take
2875 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002876 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 }
Romain Guy06882f82009-06-10 13:36:04 -07002878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 if (changed) {
2880 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002881 WindowManagerPolicy.USE_LAST_ROTATION,
2882 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 if (changed) {
2884 if (freezeThisOneIfNeeded != null) {
2885 AppWindowToken wtoken = findAppWindowToken(
2886 freezeThisOneIfNeeded);
2887 if (wtoken != null) {
2888 startAppFreezingScreenLocked(wtoken,
2889 ActivityInfo.CONFIG_ORIENTATION);
2890 }
2891 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002892 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 }
2894 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002895
2896 // No obvious action we need to take, but if our current
2897 // state mismatches the activity maanager's, update it
2898 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002899 mTempConfiguration.setToDefaults();
2900 if (computeNewConfigurationLocked(mTempConfiguration)) {
2901 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002902 return new Configuration(mTempConfiguration);
2903 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002904 }
2905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 } finally {
2907 Binder.restoreCallingIdentity(ident);
2908 }
Romain Guy06882f82009-06-10 13:36:04 -07002909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 return null;
2911 }
Romain Guy06882f82009-06-10 13:36:04 -07002912
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002913 int computeForcedAppOrientationLocked() {
2914 int req = getOrientationFromWindowsLocked();
2915 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2916 req = getOrientationFromAppTokensLocked();
2917 }
2918 return req;
2919 }
Romain Guy06882f82009-06-10 13:36:04 -07002920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2922 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2923 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002924 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 }
Romain Guy06882f82009-06-10 13:36:04 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 synchronized(mWindowMap) {
2928 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2929 if (wtoken == null) {
2930 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2931 return;
2932 }
Romain Guy06882f82009-06-10 13:36:04 -07002933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 wtoken.requestedOrientation = requestedOrientation;
2935 }
2936 }
Romain Guy06882f82009-06-10 13:36:04 -07002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 public int getAppOrientation(IApplicationToken token) {
2939 synchronized(mWindowMap) {
2940 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2941 if (wtoken == null) {
2942 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2943 }
Romain Guy06882f82009-06-10 13:36:04 -07002944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 return wtoken.requestedOrientation;
2946 }
2947 }
Romain Guy06882f82009-06-10 13:36:04 -07002948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2950 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2951 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002952 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954
2955 synchronized(mWindowMap) {
2956 boolean changed = false;
2957 if (token == null) {
2958 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2959 changed = mFocusedApp != null;
2960 mFocusedApp = null;
2961 mKeyWaiter.tickle();
2962 } else {
2963 AppWindowToken newFocus = findAppWindowToken(token);
2964 if (newFocus == null) {
2965 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2966 return;
2967 }
2968 changed = mFocusedApp != newFocus;
2969 mFocusedApp = newFocus;
2970 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2971 mKeyWaiter.tickle();
2972 }
2973
2974 if (moveFocusNow && changed) {
2975 final long origId = Binder.clearCallingIdentity();
2976 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2977 Binder.restoreCallingIdentity(origId);
2978 }
2979 }
2980 }
2981
2982 public void prepareAppTransition(int transit) {
2983 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2984 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002985 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
Romain Guy06882f82009-06-10 13:36:04 -07002987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 synchronized(mWindowMap) {
2989 if (DEBUG_APP_TRANSITIONS) Log.v(
2990 TAG, "Prepare app transition: transit=" + transit
2991 + " mNextAppTransition=" + mNextAppTransition);
2992 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002993 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
2994 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002996 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2997 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2998 // Opening a new task always supersedes a close for the anim.
2999 mNextAppTransition = transit;
3000 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3001 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3002 // Opening a new activity always supersedes a close for the anim.
3003 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 }
3005 mAppTransitionReady = false;
3006 mAppTransitionTimeout = false;
3007 mStartingIconInTransition = false;
3008 mSkipAppTransitionAnimation = false;
3009 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3010 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3011 5000);
3012 }
3013 }
3014 }
3015
3016 public int getPendingAppTransition() {
3017 return mNextAppTransition;
3018 }
Romain Guy06882f82009-06-10 13:36:04 -07003019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 public void executeAppTransition() {
3021 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3022 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003023 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 }
Romain Guy06882f82009-06-10 13:36:04 -07003025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 synchronized(mWindowMap) {
3027 if (DEBUG_APP_TRANSITIONS) Log.v(
3028 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003029 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 mAppTransitionReady = true;
3031 final long origId = Binder.clearCallingIdentity();
3032 performLayoutAndPlaceSurfacesLocked();
3033 Binder.restoreCallingIdentity(origId);
3034 }
3035 }
3036 }
3037
3038 public void setAppStartingWindow(IBinder token, String pkg,
3039 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3040 IBinder transferFrom, boolean createIfNeeded) {
3041 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3042 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003043 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 }
3045
3046 synchronized(mWindowMap) {
3047 if (DEBUG_STARTING_WINDOW) Log.v(
3048 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3049 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 AppWindowToken wtoken = findAppWindowToken(token);
3052 if (wtoken == null) {
3053 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3054 return;
3055 }
3056
3057 // If the display is frozen, we won't do anything until the
3058 // actual window is displayed so there is no reason to put in
3059 // the starting window.
3060 if (mDisplayFrozen) {
3061 return;
3062 }
Romain Guy06882f82009-06-10 13:36:04 -07003063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 if (wtoken.startingData != null) {
3065 return;
3066 }
Romain Guy06882f82009-06-10 13:36:04 -07003067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 if (transferFrom != null) {
3069 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3070 if (ttoken != null) {
3071 WindowState startingWindow = ttoken.startingWindow;
3072 if (startingWindow != null) {
3073 if (mStartingIconInTransition) {
3074 // In this case, the starting icon has already
3075 // been displayed, so start letting windows get
3076 // shown immediately without any more transitions.
3077 mSkipAppTransitionAnimation = true;
3078 }
3079 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3080 "Moving existing starting from " + ttoken
3081 + " to " + wtoken);
3082 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 // Transfer the starting window over to the new
3085 // token.
3086 wtoken.startingData = ttoken.startingData;
3087 wtoken.startingView = ttoken.startingView;
3088 wtoken.startingWindow = startingWindow;
3089 ttoken.startingData = null;
3090 ttoken.startingView = null;
3091 ttoken.startingWindow = null;
3092 ttoken.startingMoved = true;
3093 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003094 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 startingWindow.mAppToken = wtoken;
3096 mWindows.remove(startingWindow);
3097 ttoken.windows.remove(startingWindow);
3098 ttoken.allAppWindows.remove(startingWindow);
3099 addWindowToListInOrderLocked(startingWindow, true);
3100 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 // Propagate other interesting state between the
3103 // tokens. If the old token is displayed, we should
3104 // immediately force the new one to be displayed. If
3105 // it is animating, we need to move that animation to
3106 // the new one.
3107 if (ttoken.allDrawn) {
3108 wtoken.allDrawn = true;
3109 }
3110 if (ttoken.firstWindowDrawn) {
3111 wtoken.firstWindowDrawn = true;
3112 }
3113 if (!ttoken.hidden) {
3114 wtoken.hidden = false;
3115 wtoken.hiddenRequested = false;
3116 wtoken.willBeHidden = false;
3117 }
3118 if (wtoken.clientHidden != ttoken.clientHidden) {
3119 wtoken.clientHidden = ttoken.clientHidden;
3120 wtoken.sendAppVisibilityToClients();
3121 }
3122 if (ttoken.animation != null) {
3123 wtoken.animation = ttoken.animation;
3124 wtoken.animating = ttoken.animating;
3125 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3126 ttoken.animation = null;
3127 ttoken.animLayerAdjustment = 0;
3128 wtoken.updateLayers();
3129 ttoken.updateLayers();
3130 }
Romain Guy06882f82009-06-10 13:36:04 -07003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 mLayoutNeeded = true;
3134 performLayoutAndPlaceSurfacesLocked();
3135 Binder.restoreCallingIdentity(origId);
3136 return;
3137 } else if (ttoken.startingData != null) {
3138 // The previous app was getting ready to show a
3139 // starting window, but hasn't yet done so. Steal it!
3140 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3141 "Moving pending starting from " + ttoken
3142 + " to " + wtoken);
3143 wtoken.startingData = ttoken.startingData;
3144 ttoken.startingData = null;
3145 ttoken.startingMoved = true;
3146 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3147 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3148 // want to process the message ASAP, before any other queued
3149 // messages.
3150 mH.sendMessageAtFrontOfQueue(m);
3151 return;
3152 }
3153 }
3154 }
3155
3156 // There is no existing starting window, and the caller doesn't
3157 // want us to create one, so that's it!
3158 if (!createIfNeeded) {
3159 return;
3160 }
Romain Guy06882f82009-06-10 13:36:04 -07003161
Dianne Hackborn284ac932009-08-28 10:34:25 -07003162 // If this is a translucent or wallpaper window, then don't
3163 // show a starting window -- the current effect (a full-screen
3164 // opaque starting window that fades away to the real contents
3165 // when it is ready) does not work for this.
3166 if (theme != 0) {
3167 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3168 com.android.internal.R.styleable.Window);
3169 if (ent.array.getBoolean(
3170 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3171 return;
3172 }
3173 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003174 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3175 return;
3176 }
3177 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003178 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3179 return;
3180 }
3181 }
3182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 mStartingIconInTransition = true;
3184 wtoken.startingData = new StartingData(
3185 pkg, theme, nonLocalizedLabel,
3186 labelRes, icon);
3187 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3188 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3189 // want to process the message ASAP, before any other queued
3190 // messages.
3191 mH.sendMessageAtFrontOfQueue(m);
3192 }
3193 }
3194
3195 public void setAppWillBeHidden(IBinder token) {
3196 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3197 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003198 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200
3201 AppWindowToken wtoken;
3202
3203 synchronized(mWindowMap) {
3204 wtoken = findAppWindowToken(token);
3205 if (wtoken == null) {
3206 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3207 return;
3208 }
3209 wtoken.willBeHidden = true;
3210 }
3211 }
Romain Guy06882f82009-06-10 13:36:04 -07003212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3214 boolean visible, int transit, boolean performLayout) {
3215 boolean delayed = false;
3216
3217 if (wtoken.clientHidden == visible) {
3218 wtoken.clientHidden = !visible;
3219 wtoken.sendAppVisibilityToClients();
3220 }
Romain Guy06882f82009-06-10 13:36:04 -07003221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 wtoken.willBeHidden = false;
3223 if (wtoken.hidden == visible) {
3224 final int N = wtoken.allAppWindows.size();
3225 boolean changed = false;
3226 if (DEBUG_APP_TRANSITIONS) Log.v(
3227 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3228 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003231
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003232 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 if (wtoken.animation == sDummyAnimation) {
3234 wtoken.animation = null;
3235 }
3236 applyAnimationLocked(wtoken, lp, transit, visible);
3237 changed = true;
3238 if (wtoken.animation != null) {
3239 delayed = runningAppAnimation = true;
3240 }
3241 }
Romain Guy06882f82009-06-10 13:36:04 -07003242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 for (int i=0; i<N; i++) {
3244 WindowState win = wtoken.allAppWindows.get(i);
3245 if (win == wtoken.startingWindow) {
3246 continue;
3247 }
3248
3249 if (win.isAnimating()) {
3250 delayed = true;
3251 }
Romain Guy06882f82009-06-10 13:36:04 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3254 //win.dump(" ");
3255 if (visible) {
3256 if (!win.isVisibleNow()) {
3257 if (!runningAppAnimation) {
3258 applyAnimationLocked(win,
3259 WindowManagerPolicy.TRANSIT_ENTER, true);
3260 }
3261 changed = true;
3262 }
3263 } else if (win.isVisibleNow()) {
3264 if (!runningAppAnimation) {
3265 applyAnimationLocked(win,
3266 WindowManagerPolicy.TRANSIT_EXIT, false);
3267 }
3268 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3269 KeyWaiter.RETURN_NOTHING);
3270 changed = true;
3271 }
3272 }
3273
3274 wtoken.hidden = wtoken.hiddenRequested = !visible;
3275 if (!visible) {
3276 unsetAppFreezingScreenLocked(wtoken, true, true);
3277 } else {
3278 // If we are being set visible, and the starting window is
3279 // not yet displayed, then make sure it doesn't get displayed.
3280 WindowState swin = wtoken.startingWindow;
3281 if (swin != null && (swin.mDrawPending
3282 || swin.mCommitDrawPending)) {
3283 swin.mPolicyVisibility = false;
3284 swin.mPolicyVisibilityAfterAnim = false;
3285 }
3286 }
Romain Guy06882f82009-06-10 13:36:04 -07003287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3289 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3290 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 if (changed && performLayout) {
3293 mLayoutNeeded = true;
3294 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 performLayoutAndPlaceSurfacesLocked();
3296 }
3297 }
3298
3299 if (wtoken.animation != null) {
3300 delayed = true;
3301 }
Romain Guy06882f82009-06-10 13:36:04 -07003302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 return delayed;
3304 }
3305
3306 public void setAppVisibility(IBinder token, boolean visible) {
3307 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3308 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003309 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 }
3311
3312 AppWindowToken wtoken;
3313
3314 synchronized(mWindowMap) {
3315 wtoken = findAppWindowToken(token);
3316 if (wtoken == null) {
3317 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3318 return;
3319 }
3320
3321 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3322 RuntimeException e = new RuntimeException();
3323 e.fillInStackTrace();
3324 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3325 + "): mNextAppTransition=" + mNextAppTransition
3326 + " hidden=" + wtoken.hidden
3327 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3328 }
Romain Guy06882f82009-06-10 13:36:04 -07003329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 // If we are preparing an app transition, then delay changing
3331 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003332 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 // Already in requested state, don't do anything more.
3334 if (wtoken.hiddenRequested != visible) {
3335 return;
3336 }
3337 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 if (DEBUG_APP_TRANSITIONS) Log.v(
3340 TAG, "Setting dummy animation on: " + wtoken);
3341 wtoken.setDummyAnimation();
3342 mOpeningApps.remove(wtoken);
3343 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003344 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 wtoken.inPendingTransaction = true;
3346 if (visible) {
3347 mOpeningApps.add(wtoken);
3348 wtoken.allDrawn = false;
3349 wtoken.startingDisplayed = false;
3350 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003351 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 if (wtoken.clientHidden) {
3354 // In the case where we are making an app visible
3355 // but holding off for a transition, we still need
3356 // to tell the client to make its windows visible so
3357 // they get drawn. Otherwise, we will wait on
3358 // performing the transition until all windows have
3359 // been drawn, they never will be, and we are sad.
3360 wtoken.clientHidden = false;
3361 wtoken.sendAppVisibilityToClients();
3362 }
3363 } else {
3364 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003365 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 }
3367 return;
3368 }
Romain Guy06882f82009-06-10 13:36:04 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003371 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 wtoken.updateReportedVisibilityLocked();
3373 Binder.restoreCallingIdentity(origId);
3374 }
3375 }
3376
3377 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3378 boolean unfreezeSurfaceNow, boolean force) {
3379 if (wtoken.freezingScreen) {
3380 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3381 + " force=" + force);
3382 final int N = wtoken.allAppWindows.size();
3383 boolean unfrozeWindows = false;
3384 for (int i=0; i<N; i++) {
3385 WindowState w = wtoken.allAppWindows.get(i);
3386 if (w.mAppFreezing) {
3387 w.mAppFreezing = false;
3388 if (w.mSurface != null && !w.mOrientationChanging) {
3389 w.mOrientationChanging = true;
3390 }
3391 unfrozeWindows = true;
3392 }
3393 }
3394 if (force || unfrozeWindows) {
3395 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3396 wtoken.freezingScreen = false;
3397 mAppsFreezingScreen--;
3398 }
3399 if (unfreezeSurfaceNow) {
3400 if (unfrozeWindows) {
3401 mLayoutNeeded = true;
3402 performLayoutAndPlaceSurfacesLocked();
3403 }
3404 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3405 stopFreezingDisplayLocked();
3406 }
3407 }
3408 }
3409 }
Romain Guy06882f82009-06-10 13:36:04 -07003410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3412 int configChanges) {
3413 if (DEBUG_ORIENTATION) {
3414 RuntimeException e = new RuntimeException();
3415 e.fillInStackTrace();
3416 Log.i(TAG, "Set freezing of " + wtoken.appToken
3417 + ": hidden=" + wtoken.hidden + " freezing="
3418 + wtoken.freezingScreen, e);
3419 }
3420 if (!wtoken.hiddenRequested) {
3421 if (!wtoken.freezingScreen) {
3422 wtoken.freezingScreen = true;
3423 mAppsFreezingScreen++;
3424 if (mAppsFreezingScreen == 1) {
3425 startFreezingDisplayLocked();
3426 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3427 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3428 5000);
3429 }
3430 }
3431 final int N = wtoken.allAppWindows.size();
3432 for (int i=0; i<N; i++) {
3433 WindowState w = wtoken.allAppWindows.get(i);
3434 w.mAppFreezing = true;
3435 }
3436 }
3437 }
Romain Guy06882f82009-06-10 13:36:04 -07003438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 public void startAppFreezingScreen(IBinder token, int configChanges) {
3440 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3441 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003442 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 }
3444
3445 synchronized(mWindowMap) {
3446 if (configChanges == 0 && !mDisplayFrozen) {
3447 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3448 return;
3449 }
Romain Guy06882f82009-06-10 13:36:04 -07003450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 AppWindowToken wtoken = findAppWindowToken(token);
3452 if (wtoken == null || wtoken.appToken == null) {
3453 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3454 return;
3455 }
3456 final long origId = Binder.clearCallingIdentity();
3457 startAppFreezingScreenLocked(wtoken, configChanges);
3458 Binder.restoreCallingIdentity(origId);
3459 }
3460 }
Romain Guy06882f82009-06-10 13:36:04 -07003461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 public void stopAppFreezingScreen(IBinder token, boolean force) {
3463 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3464 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003465 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 }
3467
3468 synchronized(mWindowMap) {
3469 AppWindowToken wtoken = findAppWindowToken(token);
3470 if (wtoken == null || wtoken.appToken == null) {
3471 return;
3472 }
3473 final long origId = Binder.clearCallingIdentity();
3474 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3475 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3476 unsetAppFreezingScreenLocked(wtoken, true, force);
3477 Binder.restoreCallingIdentity(origId);
3478 }
3479 }
Romain Guy06882f82009-06-10 13:36:04 -07003480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 public void removeAppToken(IBinder token) {
3482 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3483 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003484 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 }
3486
3487 AppWindowToken wtoken = null;
3488 AppWindowToken startingToken = null;
3489 boolean delayed = false;
3490
3491 final long origId = Binder.clearCallingIdentity();
3492 synchronized(mWindowMap) {
3493 WindowToken basewtoken = mTokenMap.remove(token);
3494 mTokenList.remove(basewtoken);
3495 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3496 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003497 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 wtoken.inPendingTransaction = false;
3499 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003500 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 if (mClosingApps.contains(wtoken)) {
3502 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003503 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003505 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 delayed = true;
3507 }
3508 if (DEBUG_APP_TRANSITIONS) Log.v(
3509 TAG, "Removing app " + wtoken + " delayed=" + delayed
3510 + " animation=" + wtoken.animation
3511 + " animating=" + wtoken.animating);
3512 if (delayed) {
3513 // set the token aside because it has an active animation to be finished
3514 mExitingAppTokens.add(wtoken);
3515 }
3516 mAppTokens.remove(wtoken);
3517 wtoken.removed = true;
3518 if (wtoken.startingData != null) {
3519 startingToken = wtoken;
3520 }
3521 unsetAppFreezingScreenLocked(wtoken, true, true);
3522 if (mFocusedApp == wtoken) {
3523 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3524 mFocusedApp = null;
3525 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3526 mKeyWaiter.tickle();
3527 }
3528 } else {
3529 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3530 }
Romain Guy06882f82009-06-10 13:36:04 -07003531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 if (!delayed && wtoken != null) {
3533 wtoken.updateReportedVisibilityLocked();
3534 }
3535 }
3536 Binder.restoreCallingIdentity(origId);
3537
3538 if (startingToken != null) {
3539 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3540 + startingToken + ": app token removed");
3541 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3542 mH.sendMessage(m);
3543 }
3544 }
3545
3546 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3547 final int NW = token.windows.size();
3548 for (int i=0; i<NW; i++) {
3549 WindowState win = token.windows.get(i);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003550 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 mWindows.remove(win);
3552 int j = win.mChildWindows.size();
3553 while (j > 0) {
3554 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003555 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3556 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3557 "Tmp removing child window " + cwin);
3558 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 }
3560 }
3561 return NW > 0;
3562 }
3563
3564 void dumpAppTokensLocked() {
3565 for (int i=mAppTokens.size()-1; i>=0; i--) {
3566 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3567 }
3568 }
Romain Guy06882f82009-06-10 13:36:04 -07003569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 void dumpWindowsLocked() {
3571 for (int i=mWindows.size()-1; i>=0; i--) {
3572 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3573 }
3574 }
Romain Guy06882f82009-06-10 13:36:04 -07003575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 private int findWindowOffsetLocked(int tokenPos) {
3577 final int NW = mWindows.size();
3578
3579 if (tokenPos >= mAppTokens.size()) {
3580 int i = NW;
3581 while (i > 0) {
3582 i--;
3583 WindowState win = (WindowState)mWindows.get(i);
3584 if (win.getAppToken() != null) {
3585 return i+1;
3586 }
3587 }
3588 }
3589
3590 while (tokenPos > 0) {
3591 // Find the first app token below the new position that has
3592 // a window displayed.
3593 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3594 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3595 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003596 if (wtoken.sendingToBottom) {
3597 if (DEBUG_REORDER) Log.v(TAG,
3598 "Skipping token -- currently sending to bottom");
3599 tokenPos--;
3600 continue;
3601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 int i = wtoken.windows.size();
3603 while (i > 0) {
3604 i--;
3605 WindowState win = wtoken.windows.get(i);
3606 int j = win.mChildWindows.size();
3607 while (j > 0) {
3608 j--;
3609 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003610 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 for (int pos=NW-1; pos>=0; pos--) {
3612 if (mWindows.get(pos) == cwin) {
3613 if (DEBUG_REORDER) Log.v(TAG,
3614 "Found child win @" + (pos+1));
3615 return pos+1;
3616 }
3617 }
3618 }
3619 }
3620 for (int pos=NW-1; pos>=0; pos--) {
3621 if (mWindows.get(pos) == win) {
3622 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3623 return pos+1;
3624 }
3625 }
3626 }
3627 tokenPos--;
3628 }
3629
3630 return 0;
3631 }
3632
3633 private final int reAddWindowLocked(int index, WindowState win) {
3634 final int NCW = win.mChildWindows.size();
3635 boolean added = false;
3636 for (int j=0; j<NCW; j++) {
3637 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3638 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003639 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3640 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 mWindows.add(index, win);
3642 index++;
3643 added = true;
3644 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003645 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3646 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 mWindows.add(index, cwin);
3648 index++;
3649 }
3650 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003651 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3652 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 mWindows.add(index, win);
3654 index++;
3655 }
3656 return index;
3657 }
Romain Guy06882f82009-06-10 13:36:04 -07003658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3660 final int NW = token.windows.size();
3661 for (int i=0; i<NW; i++) {
3662 index = reAddWindowLocked(index, token.windows.get(i));
3663 }
3664 return index;
3665 }
3666
3667 public void moveAppToken(int index, IBinder token) {
3668 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3669 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003670 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 }
3672
3673 synchronized(mWindowMap) {
3674 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3675 if (DEBUG_REORDER) dumpAppTokensLocked();
3676 final AppWindowToken wtoken = findAppWindowToken(token);
3677 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3678 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3679 + token + " (" + wtoken + ")");
3680 return;
3681 }
3682 mAppTokens.add(index, wtoken);
3683 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3684 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 final long origId = Binder.clearCallingIdentity();
3687 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3688 if (DEBUG_REORDER) dumpWindowsLocked();
3689 if (tmpRemoveAppWindowsLocked(wtoken)) {
3690 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3691 if (DEBUG_REORDER) dumpWindowsLocked();
3692 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3693 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3694 if (DEBUG_REORDER) dumpWindowsLocked();
3695 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 mLayoutNeeded = true;
3697 performLayoutAndPlaceSurfacesLocked();
3698 }
3699 Binder.restoreCallingIdentity(origId);
3700 }
3701 }
3702
3703 private void removeAppTokensLocked(List<IBinder> tokens) {
3704 // XXX This should be done more efficiently!
3705 // (take advantage of the fact that both lists should be
3706 // ordered in the same way.)
3707 int N = tokens.size();
3708 for (int i=0; i<N; i++) {
3709 IBinder token = tokens.get(i);
3710 final AppWindowToken wtoken = findAppWindowToken(token);
3711 if (!mAppTokens.remove(wtoken)) {
3712 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3713 + token + " (" + wtoken + ")");
3714 i--;
3715 N--;
3716 }
3717 }
3718 }
3719
Dianne Hackborna8f60182009-09-01 19:01:50 -07003720 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3721 boolean updateFocusAndLayout) {
3722 // First remove all of the windows from the list.
3723 tmpRemoveAppWindowsLocked(wtoken);
3724
3725 // Where to start adding?
3726 int pos = findWindowOffsetLocked(tokenPos);
3727
3728 // And now add them back at the correct place.
3729 pos = reAddAppWindowsLocked(pos, wtoken);
3730
3731 if (updateFocusAndLayout) {
3732 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3733 assignLayersLocked();
3734 }
3735 mLayoutNeeded = true;
3736 performLayoutAndPlaceSurfacesLocked();
3737 }
3738 }
3739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3741 // First remove all of the windows from the list.
3742 final int N = tokens.size();
3743 int i;
3744 for (i=0; i<N; i++) {
3745 WindowToken token = mTokenMap.get(tokens.get(i));
3746 if (token != null) {
3747 tmpRemoveAppWindowsLocked(token);
3748 }
3749 }
3750
3751 // Where to start adding?
3752 int pos = findWindowOffsetLocked(tokenPos);
3753
3754 // And now add them back at the correct place.
3755 for (i=0; i<N; i++) {
3756 WindowToken token = mTokenMap.get(tokens.get(i));
3757 if (token != null) {
3758 pos = reAddAppWindowsLocked(pos, token);
3759 }
3760 }
3761
Dianne Hackborna8f60182009-09-01 19:01:50 -07003762 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3763 assignLayersLocked();
3764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 mLayoutNeeded = true;
3766 performLayoutAndPlaceSurfacesLocked();
3767
3768 //dump();
3769 }
3770
3771 public void moveAppTokensToTop(List<IBinder> tokens) {
3772 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3773 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003774 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
3776
3777 final long origId = Binder.clearCallingIdentity();
3778 synchronized(mWindowMap) {
3779 removeAppTokensLocked(tokens);
3780 final int N = tokens.size();
3781 for (int i=0; i<N; i++) {
3782 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3783 if (wt != null) {
3784 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003785 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003786 mToTopApps.remove(wt);
3787 mToBottomApps.remove(wt);
3788 mToTopApps.add(wt);
3789 wt.sendingToBottom = false;
3790 wt.sendingToTop = true;
3791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 }
3793 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003794
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003795 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003796 moveAppWindowsLocked(tokens, mAppTokens.size());
3797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 }
3799 Binder.restoreCallingIdentity(origId);
3800 }
3801
3802 public void moveAppTokensToBottom(List<IBinder> tokens) {
3803 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3804 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003805 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
3807
3808 final long origId = Binder.clearCallingIdentity();
3809 synchronized(mWindowMap) {
3810 removeAppTokensLocked(tokens);
3811 final int N = tokens.size();
3812 int pos = 0;
3813 for (int i=0; i<N; i++) {
3814 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3815 if (wt != null) {
3816 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003817 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003818 mToTopApps.remove(wt);
3819 mToBottomApps.remove(wt);
3820 mToBottomApps.add(i, wt);
3821 wt.sendingToTop = false;
3822 wt.sendingToBottom = true;
3823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 pos++;
3825 }
3826 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003827
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003828 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003829 moveAppWindowsLocked(tokens, 0);
3830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 }
3832 Binder.restoreCallingIdentity(origId);
3833 }
3834
3835 // -------------------------------------------------------------
3836 // Misc IWindowSession methods
3837 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 public void disableKeyguard(IBinder token, String tag) {
3840 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3841 != PackageManager.PERMISSION_GRANTED) {
3842 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3843 }
3844 mKeyguardDisabled.acquire(token, tag);
3845 }
3846
3847 public void reenableKeyguard(IBinder token) {
3848 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3849 != PackageManager.PERMISSION_GRANTED) {
3850 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3851 }
3852 synchronized (mKeyguardDisabled) {
3853 mKeyguardDisabled.release(token);
3854
3855 if (!mKeyguardDisabled.isAcquired()) {
3856 // if we are the last one to reenable the keyguard wait until
3857 // we have actaully finished reenabling until returning
3858 mWaitingUntilKeyguardReenabled = true;
3859 while (mWaitingUntilKeyguardReenabled) {
3860 try {
3861 mKeyguardDisabled.wait();
3862 } catch (InterruptedException e) {
3863 Thread.currentThread().interrupt();
3864 }
3865 }
3866 }
3867 }
3868 }
3869
3870 /**
3871 * @see android.app.KeyguardManager#exitKeyguardSecurely
3872 */
3873 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3874 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3875 != PackageManager.PERMISSION_GRANTED) {
3876 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3877 }
3878 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3879 public void onKeyguardExitResult(boolean success) {
3880 try {
3881 callback.onKeyguardExitResult(success);
3882 } catch (RemoteException e) {
3883 // Client has died, we don't care.
3884 }
3885 }
3886 });
3887 }
3888
3889 public boolean inKeyguardRestrictedInputMode() {
3890 return mPolicy.inKeyguardRestrictedKeyInputMode();
3891 }
Romain Guy06882f82009-06-10 13:36:04 -07003892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 static float fixScale(float scale) {
3894 if (scale < 0) scale = 0;
3895 else if (scale > 20) scale = 20;
3896 return Math.abs(scale);
3897 }
Romain Guy06882f82009-06-10 13:36:04 -07003898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 public void setAnimationScale(int which, float scale) {
3900 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3901 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003902 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 }
3904
3905 if (scale < 0) scale = 0;
3906 else if (scale > 20) scale = 20;
3907 scale = Math.abs(scale);
3908 switch (which) {
3909 case 0: mWindowAnimationScale = fixScale(scale); break;
3910 case 1: mTransitionAnimationScale = fixScale(scale); break;
3911 }
Romain Guy06882f82009-06-10 13:36:04 -07003912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 // Persist setting
3914 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3915 }
Romain Guy06882f82009-06-10 13:36:04 -07003916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 public void setAnimationScales(float[] scales) {
3918 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3919 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003920 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 }
3922
3923 if (scales != null) {
3924 if (scales.length >= 1) {
3925 mWindowAnimationScale = fixScale(scales[0]);
3926 }
3927 if (scales.length >= 2) {
3928 mTransitionAnimationScale = fixScale(scales[1]);
3929 }
3930 }
Romain Guy06882f82009-06-10 13:36:04 -07003931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 // Persist setting
3933 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3934 }
Romain Guy06882f82009-06-10 13:36:04 -07003935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 public float getAnimationScale(int which) {
3937 switch (which) {
3938 case 0: return mWindowAnimationScale;
3939 case 1: return mTransitionAnimationScale;
3940 }
3941 return 0;
3942 }
Romain Guy06882f82009-06-10 13:36:04 -07003943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 public float[] getAnimationScales() {
3945 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3946 }
Romain Guy06882f82009-06-10 13:36:04 -07003947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 public int getSwitchState(int sw) {
3949 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3950 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003951 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 }
3953 return KeyInputQueue.getSwitchState(sw);
3954 }
Romain Guy06882f82009-06-10 13:36:04 -07003955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 public int getSwitchStateForDevice(int devid, int sw) {
3957 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3958 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003959 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 }
3961 return KeyInputQueue.getSwitchState(devid, sw);
3962 }
Romain Guy06882f82009-06-10 13:36:04 -07003963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 public int getScancodeState(int sw) {
3965 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3966 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003967 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 }
3969 return KeyInputQueue.getScancodeState(sw);
3970 }
Romain Guy06882f82009-06-10 13:36:04 -07003971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 public int getScancodeStateForDevice(int devid, int sw) {
3973 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3974 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003975 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 }
3977 return KeyInputQueue.getScancodeState(devid, sw);
3978 }
Romain Guy06882f82009-06-10 13:36:04 -07003979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 public int getKeycodeState(int sw) {
3981 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3982 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003983 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 }
3985 return KeyInputQueue.getKeycodeState(sw);
3986 }
Romain Guy06882f82009-06-10 13:36:04 -07003987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 public int getKeycodeStateForDevice(int devid, int sw) {
3989 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3990 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003991 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 }
3993 return KeyInputQueue.getKeycodeState(devid, sw);
3994 }
Romain Guy06882f82009-06-10 13:36:04 -07003995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3997 return KeyInputQueue.hasKeys(keycodes, keyExists);
3998 }
Romain Guy06882f82009-06-10 13:36:04 -07003999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 public void enableScreenAfterBoot() {
4001 synchronized(mWindowMap) {
4002 if (mSystemBooted) {
4003 return;
4004 }
4005 mSystemBooted = true;
4006 }
Romain Guy06882f82009-06-10 13:36:04 -07004007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 performEnableScreen();
4009 }
Romain Guy06882f82009-06-10 13:36:04 -07004010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 public void enableScreenIfNeededLocked() {
4012 if (mDisplayEnabled) {
4013 return;
4014 }
4015 if (!mSystemBooted) {
4016 return;
4017 }
4018 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4019 }
Romain Guy06882f82009-06-10 13:36:04 -07004020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 public void performEnableScreen() {
4022 synchronized(mWindowMap) {
4023 if (mDisplayEnabled) {
4024 return;
4025 }
4026 if (!mSystemBooted) {
4027 return;
4028 }
Romain Guy06882f82009-06-10 13:36:04 -07004029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 // Don't enable the screen until all existing windows
4031 // have been drawn.
4032 final int N = mWindows.size();
4033 for (int i=0; i<N; i++) {
4034 WindowState w = (WindowState)mWindows.get(i);
4035 if (w.isVisibleLw() && !w.isDisplayedLw()) {
4036 return;
4037 }
4038 }
Romain Guy06882f82009-06-10 13:36:04 -07004039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 mDisplayEnabled = true;
4041 if (false) {
4042 Log.i(TAG, "ENABLING SCREEN!");
4043 StringWriter sw = new StringWriter();
4044 PrintWriter pw = new PrintWriter(sw);
4045 this.dump(null, pw, null);
4046 Log.i(TAG, sw.toString());
4047 }
4048 try {
4049 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4050 if (surfaceFlinger != null) {
4051 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4052 Parcel data = Parcel.obtain();
4053 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4054 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4055 data, null, 0);
4056 data.recycle();
4057 }
4058 } catch (RemoteException ex) {
4059 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4060 }
4061 }
Romain Guy06882f82009-06-10 13:36:04 -07004062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004066 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4067 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 }
Romain Guy06882f82009-06-10 13:36:04 -07004069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 public void setInTouchMode(boolean mode) {
4071 synchronized(mWindowMap) {
4072 mInTouchMode = mode;
4073 }
4074 }
4075
Romain Guy06882f82009-06-10 13:36:04 -07004076 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004077 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004079 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004080 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 }
4082
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004083 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 }
Romain Guy06882f82009-06-10 13:36:04 -07004085
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004086 public void setRotationUnchecked(int rotation,
4087 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 if(DEBUG_ORIENTATION) Log.v(TAG,
4089 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 long origId = Binder.clearCallingIdentity();
4092 boolean changed;
4093 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004094 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
Romain Guy06882f82009-06-10 13:36:04 -07004096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 if (changed) {
4098 sendNewConfiguration();
4099 synchronized(mWindowMap) {
4100 mLayoutNeeded = true;
4101 performLayoutAndPlaceSurfacesLocked();
4102 }
4103 } else if (alwaysSendConfiguration) {
4104 //update configuration ignoring orientation change
4105 sendNewConfiguration();
4106 }
Romain Guy06882f82009-06-10 13:36:04 -07004107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 Binder.restoreCallingIdentity(origId);
4109 }
Romain Guy06882f82009-06-10 13:36:04 -07004110
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004111 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 boolean changed;
4113 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4114 rotation = mRequestedRotation;
4115 } else {
4116 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004117 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 }
4119 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004120 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 mRotation, mDisplayEnabled);
4122 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4123 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004126 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 "Rotation changed to " + rotation
4128 + " from " + mRotation
4129 + " (forceApp=" + mForcedAppOrientation
4130 + ", req=" + mRequestedRotation + ")");
4131 mRotation = rotation;
4132 mWindowsFreezingScreen = true;
4133 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4134 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4135 2000);
4136 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004137 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 mQueue.setOrientation(rotation);
4139 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004140 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 }
4142 for (int i=mWindows.size()-1; i>=0; i--) {
4143 WindowState w = (WindowState)mWindows.get(i);
4144 if (w.mSurface != null) {
4145 w.mOrientationChanging = true;
4146 }
4147 }
4148 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4149 try {
4150 mRotationWatchers.get(i).onRotationChanged(rotation);
4151 } catch (RemoteException e) {
4152 }
4153 }
4154 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 return changed;
4157 }
Romain Guy06882f82009-06-10 13:36:04 -07004158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 public int getRotation() {
4160 return mRotation;
4161 }
4162
4163 public int watchRotation(IRotationWatcher watcher) {
4164 final IBinder watcherBinder = watcher.asBinder();
4165 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4166 public void binderDied() {
4167 synchronized (mWindowMap) {
4168 for (int i=0; i<mRotationWatchers.size(); i++) {
4169 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004170 IRotationWatcher removed = mRotationWatchers.remove(i);
4171 if (removed != null) {
4172 removed.asBinder().unlinkToDeath(this, 0);
4173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 i--;
4175 }
4176 }
4177 }
4178 }
4179 };
Romain Guy06882f82009-06-10 13:36:04 -07004180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 synchronized (mWindowMap) {
4182 try {
4183 watcher.asBinder().linkToDeath(dr, 0);
4184 mRotationWatchers.add(watcher);
4185 } catch (RemoteException e) {
4186 // Client died, no cleanup needed.
4187 }
Romain Guy06882f82009-06-10 13:36:04 -07004188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 return mRotation;
4190 }
4191 }
4192
4193 /**
4194 * Starts the view server on the specified port.
4195 *
4196 * @param port The port to listener to.
4197 *
4198 * @return True if the server was successfully started, false otherwise.
4199 *
4200 * @see com.android.server.ViewServer
4201 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4202 */
4203 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004204 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 return false;
4206 }
4207
4208 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4209 return false;
4210 }
4211
4212 if (port < 1024) {
4213 return false;
4214 }
4215
4216 if (mViewServer != null) {
4217 if (!mViewServer.isRunning()) {
4218 try {
4219 return mViewServer.start();
4220 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004221 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 }
4223 }
4224 return false;
4225 }
4226
4227 try {
4228 mViewServer = new ViewServer(this, port);
4229 return mViewServer.start();
4230 } catch (IOException e) {
4231 Log.w(TAG, "View server did not start");
4232 }
4233 return false;
4234 }
4235
Romain Guy06882f82009-06-10 13:36:04 -07004236 private boolean isSystemSecure() {
4237 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4238 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4239 }
4240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 /**
4242 * Stops the view server if it exists.
4243 *
4244 * @return True if the server stopped, false if it wasn't started or
4245 * couldn't be stopped.
4246 *
4247 * @see com.android.server.ViewServer
4248 */
4249 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004250 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 return false;
4252 }
4253
4254 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4255 return false;
4256 }
4257
4258 if (mViewServer != null) {
4259 return mViewServer.stop();
4260 }
4261 return false;
4262 }
4263
4264 /**
4265 * Indicates whether the view server is running.
4266 *
4267 * @return True if the server is running, false otherwise.
4268 *
4269 * @see com.android.server.ViewServer
4270 */
4271 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004272 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 return false;
4274 }
4275
4276 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4277 return false;
4278 }
4279
4280 return mViewServer != null && mViewServer.isRunning();
4281 }
4282
4283 /**
4284 * Lists all availble windows in the system. The listing is written in the
4285 * specified Socket's output stream with the following syntax:
4286 * windowHashCodeInHexadecimal windowName
4287 * Each line of the ouput represents a different window.
4288 *
4289 * @param client The remote client to send the listing to.
4290 * @return False if an error occured, true otherwise.
4291 */
4292 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004293 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 return false;
4295 }
4296
4297 boolean result = true;
4298
4299 Object[] windows;
4300 synchronized (mWindowMap) {
4301 windows = new Object[mWindows.size()];
4302 //noinspection unchecked
4303 windows = mWindows.toArray(windows);
4304 }
4305
4306 BufferedWriter out = null;
4307
4308 // Any uncaught exception will crash the system process
4309 try {
4310 OutputStream clientStream = client.getOutputStream();
4311 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4312
4313 final int count = windows.length;
4314 for (int i = 0; i < count; i++) {
4315 final WindowState w = (WindowState) windows[i];
4316 out.write(Integer.toHexString(System.identityHashCode(w)));
4317 out.write(' ');
4318 out.append(w.mAttrs.getTitle());
4319 out.write('\n');
4320 }
4321
4322 out.write("DONE.\n");
4323 out.flush();
4324 } catch (Exception e) {
4325 result = false;
4326 } finally {
4327 if (out != null) {
4328 try {
4329 out.close();
4330 } catch (IOException e) {
4331 result = false;
4332 }
4333 }
4334 }
4335
4336 return result;
4337 }
4338
4339 /**
4340 * Sends a command to a target window. The result of the command, if any, will be
4341 * written in the output stream of the specified socket.
4342 *
4343 * The parameters must follow this syntax:
4344 * windowHashcode extra
4345 *
4346 * Where XX is the length in characeters of the windowTitle.
4347 *
4348 * The first parameter is the target window. The window with the specified hashcode
4349 * will be the target. If no target can be found, nothing happens. The extra parameters
4350 * will be delivered to the target window and as parameters to the command itself.
4351 *
4352 * @param client The remote client to sent the result, if any, to.
4353 * @param command The command to execute.
4354 * @param parameters The command parameters.
4355 *
4356 * @return True if the command was successfully delivered, false otherwise. This does
4357 * not indicate whether the command itself was successful.
4358 */
4359 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004360 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 return false;
4362 }
4363
4364 boolean success = true;
4365 Parcel data = null;
4366 Parcel reply = null;
4367
4368 // Any uncaught exception will crash the system process
4369 try {
4370 // Find the hashcode of the window
4371 int index = parameters.indexOf(' ');
4372 if (index == -1) {
4373 index = parameters.length();
4374 }
4375 final String code = parameters.substring(0, index);
4376 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4377
4378 // Extract the command's parameter after the window description
4379 if (index < parameters.length()) {
4380 parameters = parameters.substring(index + 1);
4381 } else {
4382 parameters = "";
4383 }
4384
4385 final WindowManagerService.WindowState window = findWindow(hashCode);
4386 if (window == null) {
4387 return false;
4388 }
4389
4390 data = Parcel.obtain();
4391 data.writeInterfaceToken("android.view.IWindow");
4392 data.writeString(command);
4393 data.writeString(parameters);
4394 data.writeInt(1);
4395 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4396
4397 reply = Parcel.obtain();
4398
4399 final IBinder binder = window.mClient.asBinder();
4400 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4401 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4402
4403 reply.readException();
4404
4405 } catch (Exception e) {
4406 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4407 success = false;
4408 } finally {
4409 if (data != null) {
4410 data.recycle();
4411 }
4412 if (reply != null) {
4413 reply.recycle();
4414 }
4415 }
4416
4417 return success;
4418 }
4419
4420 private WindowState findWindow(int hashCode) {
4421 if (hashCode == -1) {
4422 return getFocusedWindow();
4423 }
4424
4425 synchronized (mWindowMap) {
4426 final ArrayList windows = mWindows;
4427 final int count = windows.size();
4428
4429 for (int i = 0; i < count; i++) {
4430 WindowState w = (WindowState) windows.get(i);
4431 if (System.identityHashCode(w) == hashCode) {
4432 return w;
4433 }
4434 }
4435 }
4436
4437 return null;
4438 }
4439
4440 /*
4441 * Instruct the Activity Manager to fetch the current configuration and broadcast
4442 * that to config-changed listeners if appropriate.
4443 */
4444 void sendNewConfiguration() {
4445 try {
4446 mActivityManager.updateConfiguration(null);
4447 } catch (RemoteException e) {
4448 }
4449 }
Romain Guy06882f82009-06-10 13:36:04 -07004450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 public Configuration computeNewConfiguration() {
4452 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004453 return computeNewConfigurationLocked();
4454 }
4455 }
Romain Guy06882f82009-06-10 13:36:04 -07004456
Dianne Hackbornc485a602009-03-24 22:39:49 -07004457 Configuration computeNewConfigurationLocked() {
4458 Configuration config = new Configuration();
4459 if (!computeNewConfigurationLocked(config)) {
4460 return null;
4461 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004462 return config;
4463 }
Romain Guy06882f82009-06-10 13:36:04 -07004464
Dianne Hackbornc485a602009-03-24 22:39:49 -07004465 boolean computeNewConfigurationLocked(Configuration config) {
4466 if (mDisplay == null) {
4467 return false;
4468 }
4469 mQueue.getInputConfiguration(config);
4470 final int dw = mDisplay.getWidth();
4471 final int dh = mDisplay.getHeight();
4472 int orientation = Configuration.ORIENTATION_SQUARE;
4473 if (dw < dh) {
4474 orientation = Configuration.ORIENTATION_PORTRAIT;
4475 } else if (dw > dh) {
4476 orientation = Configuration.ORIENTATION_LANDSCAPE;
4477 }
4478 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004479
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004480 DisplayMetrics dm = new DisplayMetrics();
4481 mDisplay.getMetrics(dm);
4482 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4483
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004484 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004485 // Note we only do this once because at this point we don't
4486 // expect the screen to change in this way at runtime, and want
4487 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004488 int longSize = dw;
4489 int shortSize = dh;
4490 if (longSize < shortSize) {
4491 int tmp = longSize;
4492 longSize = shortSize;
4493 shortSize = tmp;
4494 }
4495 longSize = (int)(longSize/dm.density);
4496 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004497
Dianne Hackborn723738c2009-06-25 19:48:04 -07004498 // These semi-magic numbers define our compatibility modes for
4499 // applications with different screens. Don't change unless you
4500 // make sure to test lots and lots of apps!
4501 if (longSize < 470) {
4502 // This is shorter than an HVGA normal density screen (which
4503 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004504 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4505 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004506 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004507 // Is this a large screen?
4508 if (longSize > 640 && shortSize >= 480) {
4509 // VGA or larger screens at medium density are the point
4510 // at which we consider it to be a large screen.
4511 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4512 } else {
4513 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4514
4515 // If this screen is wider than normal HVGA, or taller
4516 // than FWVGA, then for old apps we want to run in size
4517 // compatibility mode.
4518 if (shortSize > 321 || longSize > 570) {
4519 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4520 }
4521 }
4522
4523 // Is this a long screen?
4524 if (((longSize*3)/5) >= (shortSize-1)) {
4525 // Anything wider than WVGA (5:3) is considering to be long.
4526 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4527 } else {
4528 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4529 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004530 }
4531 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004532 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004533
Dianne Hackbornc485a602009-03-24 22:39:49 -07004534 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4535 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4536 mPolicy.adjustConfigurationLw(config);
4537 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 }
Romain Guy06882f82009-06-10 13:36:04 -07004539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 // -------------------------------------------------------------
4541 // Input Events and Focus Management
4542 // -------------------------------------------------------------
4543
4544 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004545 long curTime = SystemClock.uptimeMillis();
4546
Michael Chane10de972009-05-18 11:24:50 -07004547 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004548 if (mLastTouchEventType == eventType &&
4549 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4550 return;
4551 }
4552 mLastUserActivityCallTime = curTime;
4553 mLastTouchEventType = eventType;
4554 }
4555
4556 if (targetWin == null
4557 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4558 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 }
4560 }
4561
4562 // tells if it's a cheek event or not -- this function is stateful
4563 private static final int EVENT_NONE = 0;
4564 private static final int EVENT_UNKNOWN = 0;
4565 private static final int EVENT_CHEEK = 0;
4566 private static final int EVENT_IGNORE_DURATION = 300; // ms
4567 private static final float CHEEK_THRESHOLD = 0.6f;
4568 private int mEventState = EVENT_NONE;
4569 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 private int eventType(MotionEvent ev) {
4572 float size = ev.getSize();
4573 switch (ev.getAction()) {
4574 case MotionEvent.ACTION_DOWN:
4575 mEventSize = size;
4576 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4577 case MotionEvent.ACTION_UP:
4578 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004579 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 case MotionEvent.ACTION_MOVE:
4581 final int N = ev.getHistorySize();
4582 if (size > mEventSize) mEventSize = size;
4583 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4584 for (int i=0; i<N; i++) {
4585 size = ev.getHistoricalSize(i);
4586 if (size > mEventSize) mEventSize = size;
4587 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4588 }
4589 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4590 return TOUCH_EVENT;
4591 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004592 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 }
4594 default:
4595 // not good
4596 return OTHER_EVENT;
4597 }
4598 }
4599
4600 /**
4601 * @return Returns true if event was dispatched, false if it was dropped for any reason
4602 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004603 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4605 "dispatchPointer " + ev);
4606
Michael Chan53071d62009-05-13 17:29:48 -07004607 if (MEASURE_LATENCY) {
4608 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4609 }
4610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004612 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613
Michael Chan53071d62009-05-13 17:29:48 -07004614 if (MEASURE_LATENCY) {
4615 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4616 }
4617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 if (action == MotionEvent.ACTION_UP) {
4621 // let go of our target
4622 mKeyWaiter.mMotionTarget = null;
4623 mPowerManager.logPointerUpEvent();
4624 } else if (action == MotionEvent.ACTION_DOWN) {
4625 mPowerManager.logPointerDownEvent();
4626 }
4627
4628 if (targetObj == null) {
4629 // In this case we are either dropping the event, or have received
4630 // a move or up without a down. It is common to receive move
4631 // events in such a way, since this means the user is moving the
4632 // pointer without actually pressing down. All other cases should
4633 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004634 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4636 }
4637 if (qev != null) {
4638 mQueue.recycleEvent(qev);
4639 }
4640 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004641 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 }
4643 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4644 if (qev != null) {
4645 mQueue.recycleEvent(qev);
4646 }
4647 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004648 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 }
Romain Guy06882f82009-06-10 13:36:04 -07004650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004654 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655
4656 //Log.i(TAG, "Sending " + ev + " to " + target);
4657
4658 if (uid != 0 && uid != target.mSession.mUid) {
4659 if (mContext.checkPermission(
4660 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4661 != PackageManager.PERMISSION_GRANTED) {
4662 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4663 + pid + " uid " + uid + " to window " + target
4664 + " owned by uid " + target.mSession.mUid);
4665 if (qev != null) {
4666 mQueue.recycleEvent(qev);
4667 }
4668 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004669 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 }
4671 }
4672
Michael Chan53071d62009-05-13 17:29:48 -07004673 if (MEASURE_LATENCY) {
4674 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4675 }
4676
Romain Guy06882f82009-06-10 13:36:04 -07004677 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4679 //target wants to ignore fat touch events
4680 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4681 //explicit flag to return without processing event further
4682 boolean returnFlag = false;
4683 if((action == MotionEvent.ACTION_DOWN)) {
4684 mFatTouch = false;
4685 if(cheekPress) {
4686 mFatTouch = true;
4687 returnFlag = true;
4688 }
4689 } else {
4690 if(action == MotionEvent.ACTION_UP) {
4691 if(mFatTouch) {
4692 //earlier even was invalid doesnt matter if current up is cheekpress or not
4693 mFatTouch = false;
4694 returnFlag = true;
4695 } else if(cheekPress) {
4696 //cancel the earlier event
4697 ev.setAction(MotionEvent.ACTION_CANCEL);
4698 action = MotionEvent.ACTION_CANCEL;
4699 }
4700 } else if(action == MotionEvent.ACTION_MOVE) {
4701 if(mFatTouch) {
4702 //two cases here
4703 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004704 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 returnFlag = true;
4706 } else if(cheekPress) {
4707 //valid down followed by invalid moves
4708 //an invalid move have to cancel earlier action
4709 ev.setAction(MotionEvent.ACTION_CANCEL);
4710 action = MotionEvent.ACTION_CANCEL;
4711 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4712 //note that the subsequent invalid moves will not get here
4713 mFatTouch = true;
4714 }
4715 }
4716 } //else if action
4717 if(returnFlag) {
4718 //recycle que, ev
4719 if (qev != null) {
4720 mQueue.recycleEvent(qev);
4721 }
4722 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004723 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 }
4725 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004726
Michael Chan9f028e62009-08-04 17:37:46 -07004727 // Enable this for testing the "right" value
4728 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004729 int max_events_per_sec = 35;
4730 try {
4731 max_events_per_sec = Integer.parseInt(SystemProperties
4732 .get("windowsmgr.max_events_per_sec"));
4733 if (max_events_per_sec < 1) {
4734 max_events_per_sec = 35;
4735 }
4736 } catch (NumberFormatException e) {
4737 }
4738 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4739 }
4740
4741 /*
4742 * Throttle events to minimize CPU usage when there's a flood of events
4743 * e.g. constant contact with the screen
4744 */
4745 if (action == MotionEvent.ACTION_MOVE) {
4746 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4747 long now = SystemClock.uptimeMillis();
4748 if (now < nextEventTime) {
4749 try {
4750 Thread.sleep(nextEventTime - now);
4751 } catch (InterruptedException e) {
4752 }
4753 mLastTouchEventTime = nextEventTime;
4754 } else {
4755 mLastTouchEventTime = now;
4756 }
4757 }
4758
Michael Chan53071d62009-05-13 17:29:48 -07004759 if (MEASURE_LATENCY) {
4760 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4761 }
4762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004763 synchronized(mWindowMap) {
4764 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4765 mKeyWaiter.bindTargetWindowLocked(target,
4766 KeyWaiter.RETURN_PENDING_POINTER, qev);
4767 ev = null;
4768 } else {
4769 if (action == MotionEvent.ACTION_DOWN) {
4770 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4771 if (out != null) {
4772 MotionEvent oev = MotionEvent.obtain(ev);
4773 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4774 do {
4775 final Rect frame = out.mFrame;
4776 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4777 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004778 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 } catch (android.os.RemoteException e) {
4780 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4781 }
4782 oev.offsetLocation((float)frame.left, (float)frame.top);
4783 out = out.mNextOutsideTouch;
4784 } while (out != null);
4785 mKeyWaiter.mOutsideTouchTargets = null;
4786 }
4787 }
4788 final Rect frame = target.mFrame;
4789 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4790 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004791
4792 // If we are on top of the wallpaper, then the wallpaper also
4793 // gets to see this movement.
4794 if (mWallpaperTarget == target) {
4795 sendPointerToWallpaperLocked(target, ev, eventTime);
4796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 }
4798 }
Romain Guy06882f82009-06-10 13:36:04 -07004799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 // finally offset the event to the target's coordinate system and
4801 // dispatch the event.
4802 try {
4803 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4804 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4805 }
Michael Chan53071d62009-05-13 17:29:48 -07004806
4807 if (MEASURE_LATENCY) {
4808 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4809 }
4810
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004811 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004812
4813 if (MEASURE_LATENCY) {
4814 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4815 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004816 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817 } catch (android.os.RemoteException e) {
4818 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4819 mKeyWaiter.mMotionTarget = null;
4820 try {
4821 removeWindow(target.mSession, target.mClient);
4822 } catch (java.util.NoSuchElementException ex) {
4823 // This will happen if the window has already been
4824 // removed.
4825 }
4826 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004827 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 }
Romain Guy06882f82009-06-10 13:36:04 -07004829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 /**
4831 * @return Returns true if event was dispatched, false if it was dropped for any reason
4832 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004833 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 if (DEBUG_INPUT) Log.v(
4835 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004838 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 if (focusObj == null) {
4840 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4841 if (qev != null) {
4842 mQueue.recycleEvent(qev);
4843 }
4844 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004845 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 }
4847 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4848 if (qev != null) {
4849 mQueue.recycleEvent(qev);
4850 }
4851 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004852 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 }
Romain Guy06882f82009-06-10 13:36:04 -07004854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 if (uid != 0 && uid != focus.mSession.mUid) {
4858 if (mContext.checkPermission(
4859 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4860 != PackageManager.PERMISSION_GRANTED) {
4861 Log.w(TAG, "Permission denied: injecting key event from pid "
4862 + pid + " uid " + uid + " to window " + focus
4863 + " owned by uid " + focus.mSession.mUid);
4864 if (qev != null) {
4865 mQueue.recycleEvent(qev);
4866 }
4867 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004868 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 }
4870 }
Romain Guy06882f82009-06-10 13:36:04 -07004871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 synchronized(mWindowMap) {
4875 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4876 mKeyWaiter.bindTargetWindowLocked(focus,
4877 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4878 // We don't deliver movement events to the client, we hold
4879 // them and wait for them to call back.
4880 ev = null;
4881 } else {
4882 mKeyWaiter.bindTargetWindowLocked(focus);
4883 }
4884 }
Romain Guy06882f82009-06-10 13:36:04 -07004885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004887 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004888 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 } catch (android.os.RemoteException e) {
4890 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4891 try {
4892 removeWindow(focus.mSession, focus.mClient);
4893 } catch (java.util.NoSuchElementException ex) {
4894 // This will happen if the window has already been
4895 // removed.
4896 }
4897 }
Romain Guy06882f82009-06-10 13:36:04 -07004898
Dianne Hackborncfaef692009-06-15 14:24:44 -07004899 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004900 }
Romain Guy06882f82009-06-10 13:36:04 -07004901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 /**
4903 * @return Returns true if event was dispatched, false if it was dropped for any reason
4904 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004905 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4907
4908 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004909 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910 if (focusObj == null) {
4911 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004912 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 }
4914 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004915 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 }
Romain Guy06882f82009-06-10 13:36:04 -07004917
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004918 // Okay we have finished waiting for the last event to be processed.
4919 // First off, if this is a repeat event, check to see if there is
4920 // a corresponding up event in the queue. If there is, we will
4921 // just drop the repeat, because it makes no sense to repeat after
4922 // the user has released a key. (This is especially important for
4923 // long presses.)
4924 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
4925 return INJECT_SUCCEEDED;
4926 }
4927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 if (DEBUG_INPUT) Log.v(
4931 TAG, "Dispatching to " + focus + ": " + event);
4932
4933 if (uid != 0 && uid != focus.mSession.mUid) {
4934 if (mContext.checkPermission(
4935 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4936 != PackageManager.PERMISSION_GRANTED) {
4937 Log.w(TAG, "Permission denied: injecting key event from pid "
4938 + pid + " uid " + uid + " to window " + focus
4939 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004940 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 }
4942 }
Romain Guy06882f82009-06-10 13:36:04 -07004943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 synchronized(mWindowMap) {
4945 mKeyWaiter.bindTargetWindowLocked(focus);
4946 }
4947
4948 // NOSHIP extra state logging
4949 mKeyWaiter.recordDispatchState(event, focus);
4950 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 try {
4953 if (DEBUG_INPUT || DEBUG_FOCUS) {
4954 Log.v(TAG, "Delivering key " + event.getKeyCode()
4955 + " to " + focus);
4956 }
4957 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004958 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 } catch (android.os.RemoteException e) {
4960 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4961 try {
4962 removeWindow(focus.mSession, focus.mClient);
4963 } catch (java.util.NoSuchElementException ex) {
4964 // This will happen if the window has already been
4965 // removed.
4966 }
4967 }
Romain Guy06882f82009-06-10 13:36:04 -07004968
Dianne Hackborncfaef692009-06-15 14:24:44 -07004969 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 }
Romain Guy06882f82009-06-10 13:36:04 -07004971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 public void pauseKeyDispatching(IBinder _token) {
4973 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4974 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004975 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
4977
4978 synchronized (mWindowMap) {
4979 WindowToken token = mTokenMap.get(_token);
4980 if (token != null) {
4981 mKeyWaiter.pauseDispatchingLocked(token);
4982 }
4983 }
4984 }
4985
4986 public void resumeKeyDispatching(IBinder _token) {
4987 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4988 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004989 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 }
4991
4992 synchronized (mWindowMap) {
4993 WindowToken token = mTokenMap.get(_token);
4994 if (token != null) {
4995 mKeyWaiter.resumeDispatchingLocked(token);
4996 }
4997 }
4998 }
4999
5000 public void setEventDispatching(boolean enabled) {
5001 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5002 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005003 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 }
5005
5006 synchronized (mWindowMap) {
5007 mKeyWaiter.setEventDispatchingLocked(enabled);
5008 }
5009 }
Romain Guy06882f82009-06-10 13:36:04 -07005010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 /**
5012 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005013 *
5014 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 * {@link SystemClock#uptimeMillis()} as the timebase.)
5016 * @param sync If true, wait for the event to be completed before returning to the caller.
5017 * @return Returns true if event was dispatched, false if it was dropped for any reason
5018 */
5019 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5020 long downTime = ev.getDownTime();
5021 long eventTime = ev.getEventTime();
5022
5023 int action = ev.getAction();
5024 int code = ev.getKeyCode();
5025 int repeatCount = ev.getRepeatCount();
5026 int metaState = ev.getMetaState();
5027 int deviceId = ev.getDeviceId();
5028 int scancode = ev.getScanCode();
5029
5030 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5031 if (downTime == 0) downTime = eventTime;
5032
5033 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005034 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005036 final int pid = Binder.getCallingPid();
5037 final int uid = Binder.getCallingUid();
5038 final long ident = Binder.clearCallingIdentity();
5039 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005041 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005043 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005044 switch (result) {
5045 case INJECT_NO_PERMISSION:
5046 throw new SecurityException(
5047 "Injecting to another application requires INJECT_EVENT permission");
5048 case INJECT_SUCCEEDED:
5049 return true;
5050 }
5051 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 }
5053
5054 /**
5055 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005056 *
5057 * @param ev A motion event describing the pointer (touch) action. (As noted in
5058 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 * {@link SystemClock#uptimeMillis()} as the timebase.)
5060 * @param sync If true, wait for the event to be completed before returning to the caller.
5061 * @return Returns true if event was dispatched, false if it was dropped for any reason
5062 */
5063 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005064 final int pid = Binder.getCallingPid();
5065 final int uid = Binder.getCallingUid();
5066 final long ident = Binder.clearCallingIdentity();
5067 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005069 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005071 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005072 switch (result) {
5073 case INJECT_NO_PERMISSION:
5074 throw new SecurityException(
5075 "Injecting to another application requires INJECT_EVENT permission");
5076 case INJECT_SUCCEEDED:
5077 return true;
5078 }
5079 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 }
Romain Guy06882f82009-06-10 13:36:04 -07005081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 /**
5083 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005084 *
5085 * @param ev A motion event describing the trackball action. (As noted in
5086 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 * {@link SystemClock#uptimeMillis()} as the timebase.)
5088 * @param sync If true, wait for the event to be completed before returning to the caller.
5089 * @return Returns true if event was dispatched, false if it was dropped for any reason
5090 */
5091 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005092 final int pid = Binder.getCallingPid();
5093 final int uid = Binder.getCallingUid();
5094 final long ident = Binder.clearCallingIdentity();
5095 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005097 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005099 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005100 switch (result) {
5101 case INJECT_NO_PERMISSION:
5102 throw new SecurityException(
5103 "Injecting to another application requires INJECT_EVENT permission");
5104 case INJECT_SUCCEEDED:
5105 return true;
5106 }
5107 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 }
Romain Guy06882f82009-06-10 13:36:04 -07005109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 private WindowState getFocusedWindow() {
5111 synchronized (mWindowMap) {
5112 return getFocusedWindowLocked();
5113 }
5114 }
5115
5116 private WindowState getFocusedWindowLocked() {
5117 return mCurrentFocus;
5118 }
Romain Guy06882f82009-06-10 13:36:04 -07005119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 /**
5121 * This class holds the state for dispatching key events. This state
5122 * is protected by the KeyWaiter instance, NOT by the window lock. You
5123 * can be holding the main window lock while acquire the KeyWaiter lock,
5124 * but not the other way around.
5125 */
5126 final class KeyWaiter {
5127 // NOSHIP debugging
5128 public class DispatchState {
5129 private KeyEvent event;
5130 private WindowState focus;
5131 private long time;
5132 private WindowState lastWin;
5133 private IBinder lastBinder;
5134 private boolean finished;
5135 private boolean gotFirstWindow;
5136 private boolean eventDispatching;
5137 private long timeToSwitch;
5138 private boolean wasFrozen;
5139 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005140 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5143 focus = theFocus;
5144 event = theEvent;
5145 time = System.currentTimeMillis();
5146 // snapshot KeyWaiter state
5147 lastWin = mLastWin;
5148 lastBinder = mLastBinder;
5149 finished = mFinished;
5150 gotFirstWindow = mGotFirstWindow;
5151 eventDispatching = mEventDispatching;
5152 timeToSwitch = mTimeToSwitch;
5153 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005154 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 // cache the paused state at ctor time as well
5156 if (theFocus == null || theFocus.mToken == null) {
5157 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5158 focusPaused = false;
5159 } else {
5160 focusPaused = theFocus.mToken.paused;
5161 }
5162 }
Romain Guy06882f82009-06-10 13:36:04 -07005163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 public String toString() {
5165 return "{{" + event + " to " + focus + " @ " + time
5166 + " lw=" + lastWin + " lb=" + lastBinder
5167 + " fin=" + finished + " gfw=" + gotFirstWindow
5168 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005169 + " wf=" + wasFrozen + " fp=" + focusPaused
5170 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 }
5172 };
5173 private DispatchState mDispatchState = null;
5174 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5175 mDispatchState = new DispatchState(theEvent, theFocus);
5176 }
5177 // END NOSHIP
5178
5179 public static final int RETURN_NOTHING = 0;
5180 public static final int RETURN_PENDING_POINTER = 1;
5181 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 final Object SKIP_TARGET_TOKEN = new Object();
5184 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 private WindowState mLastWin = null;
5187 private IBinder mLastBinder = null;
5188 private boolean mFinished = true;
5189 private boolean mGotFirstWindow = false;
5190 private boolean mEventDispatching = true;
5191 private long mTimeToSwitch = 0;
5192 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 // Target of Motion events
5195 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 // Windows above the target who would like to receive an "outside"
5198 // touch event for any down events outside of them.
5199 WindowState mOutsideTouchTargets;
5200
5201 /**
5202 * Wait for the last event dispatch to complete, then find the next
5203 * target that should receive the given event and wait for that one
5204 * to be ready to receive it.
5205 */
5206 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5207 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005208 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 long startTime = SystemClock.uptimeMillis();
5210 long keyDispatchingTimeout = 5 * 1000;
5211 long waitedFor = 0;
5212
5213 while (true) {
5214 // Figure out which window we care about. It is either the
5215 // last window we are waiting to have process the event or,
5216 // if none, then the next window we think the event should go
5217 // to. Note: we retrieve mLastWin outside of the lock, so
5218 // it may change before we lock. Thus we must check it again.
5219 WindowState targetWin = mLastWin;
5220 boolean targetIsNew = targetWin == null;
5221 if (DEBUG_INPUT) Log.v(
5222 TAG, "waitForLastKey: mFinished=" + mFinished +
5223 ", mLastWin=" + mLastWin);
5224 if (targetIsNew) {
5225 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005226 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 if (target == SKIP_TARGET_TOKEN) {
5228 // The user has pressed a special key, and we are
5229 // dropping all pending events before it.
5230 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5231 + " " + nextMotion);
5232 return null;
5233 }
5234 if (target == CONSUMED_EVENT_TOKEN) {
5235 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5236 + " " + nextMotion);
5237 return target;
5238 }
5239 targetWin = (WindowState)target;
5240 }
Romain Guy06882f82009-06-10 13:36:04 -07005241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 // Now: is it okay to send the next event to this window?
5245 synchronized (this) {
5246 // First: did we come here based on the last window not
5247 // being null, but it changed by the time we got here?
5248 // If so, try again.
5249 if (!targetIsNew && mLastWin == null) {
5250 continue;
5251 }
Romain Guy06882f82009-06-10 13:36:04 -07005252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 // We never dispatch events if not finished with the
5254 // last one, or the display is frozen.
5255 if (mFinished && !mDisplayFrozen) {
5256 // If event dispatching is disabled, then we
5257 // just consume the events.
5258 if (!mEventDispatching) {
5259 if (DEBUG_INPUT) Log.v(TAG,
5260 "Skipping event; dispatching disabled: "
5261 + nextKey + " " + nextMotion);
5262 return null;
5263 }
5264 if (targetWin != null) {
5265 // If this is a new target, and that target is not
5266 // paused or unresponsive, then all looks good to
5267 // handle the event.
5268 if (targetIsNew && !targetWin.mToken.paused) {
5269 return targetWin;
5270 }
Romain Guy06882f82009-06-10 13:36:04 -07005271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 // If we didn't find a target window, and there is no
5273 // focused app window, then just eat the events.
5274 } else if (mFocusedApp == null) {
5275 if (DEBUG_INPUT) Log.v(TAG,
5276 "Skipping event; no focused app: "
5277 + nextKey + " " + nextMotion);
5278 return null;
5279 }
5280 }
Romain Guy06882f82009-06-10 13:36:04 -07005281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 if (DEBUG_INPUT) Log.v(
5283 TAG, "Waiting for last key in " + mLastBinder
5284 + " target=" + targetWin
5285 + " mFinished=" + mFinished
5286 + " mDisplayFrozen=" + mDisplayFrozen
5287 + " targetIsNew=" + targetIsNew
5288 + " paused="
5289 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005290 + " mFocusedApp=" + mFocusedApp
5291 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 targetApp = targetWin != null
5294 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 long curTimeout = keyDispatchingTimeout;
5297 if (mTimeToSwitch != 0) {
5298 long now = SystemClock.uptimeMillis();
5299 if (mTimeToSwitch <= now) {
5300 // If an app switch key has been pressed, and we have
5301 // waited too long for the current app to finish
5302 // processing keys, then wait no more!
5303 doFinishedKeyLocked(true);
5304 continue;
5305 }
5306 long switchTimeout = mTimeToSwitch - now;
5307 if (curTimeout > switchTimeout) {
5308 curTimeout = switchTimeout;
5309 }
5310 }
Romain Guy06882f82009-06-10 13:36:04 -07005311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 try {
5313 // after that continue
5314 // processing keys, so we don't get stuck.
5315 if (DEBUG_INPUT) Log.v(
5316 TAG, "Waiting for key dispatch: " + curTimeout);
5317 wait(curTimeout);
5318 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5319 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005320 + startTime + " switchTime=" + mTimeToSwitch
5321 + " target=" + targetWin + " mLW=" + mLastWin
5322 + " mLB=" + mLastBinder + " fin=" + mFinished
5323 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 } catch (InterruptedException e) {
5325 }
5326 }
5327
5328 // If we were frozen during configuration change, restart the
5329 // timeout checks from now; otherwise look at whether we timed
5330 // out before awakening.
5331 if (mWasFrozen) {
5332 waitedFor = 0;
5333 mWasFrozen = false;
5334 } else {
5335 waitedFor = SystemClock.uptimeMillis() - startTime;
5336 }
5337
5338 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5339 IApplicationToken at = null;
5340 synchronized (this) {
5341 Log.w(TAG, "Key dispatching timed out sending to " +
5342 (targetWin != null ? targetWin.mAttrs.getTitle()
5343 : "<null>"));
5344 // NOSHIP debugging
5345 Log.w(TAG, "Dispatch state: " + mDispatchState);
5346 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5347 // END NOSHIP
5348 //dump();
5349 if (targetWin != null) {
5350 at = targetWin.getAppToken();
5351 } else if (targetApp != null) {
5352 at = targetApp.appToken;
5353 }
5354 }
5355
5356 boolean abort = true;
5357 if (at != null) {
5358 try {
5359 long timeout = at.getKeyDispatchingTimeout();
5360 if (timeout > waitedFor) {
5361 // we did not wait the proper amount of time for this application.
5362 // set the timeout to be the real timeout and wait again.
5363 keyDispatchingTimeout = timeout - waitedFor;
5364 continue;
5365 } else {
5366 abort = at.keyDispatchingTimedOut();
5367 }
5368 } catch (RemoteException ex) {
5369 }
5370 }
5371
5372 synchronized (this) {
5373 if (abort && (mLastWin == targetWin || targetWin == null)) {
5374 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005375 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 if (DEBUG_INPUT) Log.v(TAG,
5377 "Window " + mLastWin +
5378 " timed out on key input");
5379 if (mLastWin.mToken.paused) {
5380 Log.w(TAG, "Un-pausing dispatching to this window");
5381 mLastWin.mToken.paused = false;
5382 }
5383 }
5384 if (mMotionTarget == targetWin) {
5385 mMotionTarget = null;
5386 }
5387 mLastWin = null;
5388 mLastBinder = null;
5389 if (failIfTimeout || targetWin == null) {
5390 return null;
5391 }
5392 } else {
5393 Log.w(TAG, "Continuing to wait for key to be dispatched");
5394 startTime = SystemClock.uptimeMillis();
5395 }
5396 }
5397 }
5398 }
5399 }
Romain Guy06882f82009-06-10 13:36:04 -07005400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005402 MotionEvent nextMotion, boolean isPointerEvent,
5403 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 if (nextKey != null) {
5407 // Find the target window for a normal key event.
5408 final int keycode = nextKey.getKeyCode();
5409 final int repeatCount = nextKey.getRepeatCount();
5410 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5411 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005414 if (callingUid == 0 ||
5415 mContext.checkPermission(
5416 android.Manifest.permission.INJECT_EVENTS,
5417 callingPid, callingUid)
5418 == PackageManager.PERMISSION_GRANTED) {
5419 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005420 nextKey.getMetaState(), down, repeatCount,
5421 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 Log.w(TAG, "Event timeout during app switch: dropping "
5424 + nextKey);
5425 return SKIP_TARGET_TOKEN;
5426 }
Romain Guy06882f82009-06-10 13:36:04 -07005427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 WindowState focus = null;
5431 synchronized(mWindowMap) {
5432 focus = getFocusedWindowLocked();
5433 }
Romain Guy06882f82009-06-10 13:36:04 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005436
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005437 if (callingUid == 0 ||
5438 (focus != null && callingUid == focus.mSession.mUid) ||
5439 mContext.checkPermission(
5440 android.Manifest.permission.INJECT_EVENTS,
5441 callingPid, callingUid)
5442 == PackageManager.PERMISSION_GRANTED) {
5443 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005444 keycode, nextKey.getMetaState(), down, repeatCount,
5445 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005446 return CONSUMED_EVENT_TOKEN;
5447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 }
Romain Guy06882f82009-06-10 13:36:04 -07005449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 } else if (!isPointerEvent) {
5453 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5454 if (!dispatch) {
5455 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5456 + nextMotion);
5457 return SKIP_TARGET_TOKEN;
5458 }
Romain Guy06882f82009-06-10 13:36:04 -07005459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 WindowState focus = null;
5461 synchronized(mWindowMap) {
5462 focus = getFocusedWindowLocked();
5463 }
Romain Guy06882f82009-06-10 13:36:04 -07005464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5466 return focus;
5467 }
Romain Guy06882f82009-06-10 13:36:04 -07005468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 if (nextMotion == null) {
5470 return SKIP_TARGET_TOKEN;
5471 }
Romain Guy06882f82009-06-10 13:36:04 -07005472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5474 KeyEvent.KEYCODE_UNKNOWN);
5475 if (!dispatch) {
5476 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5477 + nextMotion);
5478 return SKIP_TARGET_TOKEN;
5479 }
Romain Guy06882f82009-06-10 13:36:04 -07005480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 // Find the target window for a pointer event.
5482 int action = nextMotion.getAction();
5483 final float xf = nextMotion.getX();
5484 final float yf = nextMotion.getY();
5485 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 final boolean screenWasOff = qev != null
5488 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 synchronized(mWindowMap) {
5493 synchronized (this) {
5494 if (action == MotionEvent.ACTION_DOWN) {
5495 if (mMotionTarget != null) {
5496 // this is weird, we got a pen down, but we thought it was
5497 // already down!
5498 // XXX: We should probably send an ACTION_UP to the current
5499 // target.
5500 Log.w(TAG, "Pointer down received while already down in: "
5501 + mMotionTarget);
5502 mMotionTarget = null;
5503 }
Romain Guy06882f82009-06-10 13:36:04 -07005504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 // ACTION_DOWN is special, because we need to lock next events to
5506 // the window we'll land onto.
5507 final int x = (int)xf;
5508 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 final ArrayList windows = mWindows;
5511 final int N = windows.size();
5512 WindowState topErrWindow = null;
5513 final Rect tmpRect = mTempRect;
5514 for (int i=N-1; i>=0; i--) {
5515 WindowState child = (WindowState)windows.get(i);
5516 //Log.i(TAG, "Checking dispatch to: " + child);
5517 final int flags = child.mAttrs.flags;
5518 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5519 if (topErrWindow == null) {
5520 topErrWindow = child;
5521 }
5522 }
5523 if (!child.isVisibleLw()) {
5524 //Log.i(TAG, "Not visible!");
5525 continue;
5526 }
5527 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5528 //Log.i(TAG, "Not touchable!");
5529 if ((flags & WindowManager.LayoutParams
5530 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5531 child.mNextOutsideTouch = mOutsideTouchTargets;
5532 mOutsideTouchTargets = child;
5533 }
5534 continue;
5535 }
5536 tmpRect.set(child.mFrame);
5537 if (child.mTouchableInsets == ViewTreeObserver
5538 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5539 // The touch is inside of the window if it is
5540 // inside the frame, AND the content part of that
5541 // frame that was given by the application.
5542 tmpRect.left += child.mGivenContentInsets.left;
5543 tmpRect.top += child.mGivenContentInsets.top;
5544 tmpRect.right -= child.mGivenContentInsets.right;
5545 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5546 } else if (child.mTouchableInsets == ViewTreeObserver
5547 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5548 // The touch is inside of the window if it is
5549 // inside the frame, AND the visible part of that
5550 // frame that was given by the application.
5551 tmpRect.left += child.mGivenVisibleInsets.left;
5552 tmpRect.top += child.mGivenVisibleInsets.top;
5553 tmpRect.right -= child.mGivenVisibleInsets.right;
5554 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5555 }
5556 final int touchFlags = flags &
5557 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5558 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5559 if (tmpRect.contains(x, y) || touchFlags == 0) {
5560 //Log.i(TAG, "Using this target!");
5561 if (!screenWasOff || (flags &
5562 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5563 mMotionTarget = child;
5564 } else {
5565 //Log.i(TAG, "Waking, skip!");
5566 mMotionTarget = null;
5567 }
5568 break;
5569 }
Romain Guy06882f82009-06-10 13:36:04 -07005570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 if ((flags & WindowManager.LayoutParams
5572 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5573 child.mNextOutsideTouch = mOutsideTouchTargets;
5574 mOutsideTouchTargets = child;
5575 //Log.i(TAG, "Adding to outside target list: " + child);
5576 }
5577 }
5578
5579 // if there's an error window but it's not accepting
5580 // focus (typically because it is not yet visible) just
5581 // wait for it -- any other focused window may in fact
5582 // be in ANR state.
5583 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5584 mMotionTarget = null;
5585 }
5586 }
Romain Guy06882f82009-06-10 13:36:04 -07005587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 target = mMotionTarget;
5589 }
5590 }
Romain Guy06882f82009-06-10 13:36:04 -07005591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 // Pointer events are a little different -- if there isn't a
5595 // target found for any event, then just drop it.
5596 return target != null ? target : SKIP_TARGET_TOKEN;
5597 }
Romain Guy06882f82009-06-10 13:36:04 -07005598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 boolean checkShouldDispatchKey(int keycode) {
5600 synchronized (this) {
5601 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5602 mTimeToSwitch = 0;
5603 return true;
5604 }
5605 if (mTimeToSwitch != 0
5606 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5607 return false;
5608 }
5609 return true;
5610 }
5611 }
Romain Guy06882f82009-06-10 13:36:04 -07005612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 void bindTargetWindowLocked(WindowState win,
5614 int pendingWhat, QueuedEvent pendingMotion) {
5615 synchronized (this) {
5616 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5617 }
5618 }
Romain Guy06882f82009-06-10 13:36:04 -07005619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 void bindTargetWindowLocked(WindowState win) {
5621 synchronized (this) {
5622 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5623 }
5624 }
5625
5626 void bindTargetWindowLockedLocked(WindowState win,
5627 int pendingWhat, QueuedEvent pendingMotion) {
5628 mLastWin = win;
5629 mLastBinder = win.mClient.asBinder();
5630 mFinished = false;
5631 if (pendingMotion != null) {
5632 final Session s = win.mSession;
5633 if (pendingWhat == RETURN_PENDING_POINTER) {
5634 releasePendingPointerLocked(s);
5635 s.mPendingPointerMove = pendingMotion;
5636 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005637 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 "bindTargetToWindow " + s.mPendingPointerMove);
5639 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5640 releasePendingTrackballLocked(s);
5641 s.mPendingTrackballMove = pendingMotion;
5642 s.mPendingTrackballWindow = win;
5643 }
5644 }
5645 }
Romain Guy06882f82009-06-10 13:36:04 -07005646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 void releasePendingPointerLocked(Session s) {
5648 if (DEBUG_INPUT) Log.v(TAG,
5649 "releasePendingPointer " + s.mPendingPointerMove);
5650 if (s.mPendingPointerMove != null) {
5651 mQueue.recycleEvent(s.mPendingPointerMove);
5652 s.mPendingPointerMove = null;
5653 }
5654 }
Romain Guy06882f82009-06-10 13:36:04 -07005655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 void releasePendingTrackballLocked(Session s) {
5657 if (s.mPendingTrackballMove != null) {
5658 mQueue.recycleEvent(s.mPendingTrackballMove);
5659 s.mPendingTrackballMove = null;
5660 }
5661 }
Romain Guy06882f82009-06-10 13:36:04 -07005662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5664 int returnWhat) {
5665 if (DEBUG_INPUT) Log.v(
5666 TAG, "finishedKey: client=" + client + ", force=" + force);
5667
5668 if (client == null) {
5669 return null;
5670 }
5671
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005672 MotionEvent res = null;
5673 QueuedEvent qev = null;
5674 WindowState win = null;
5675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 synchronized (this) {
5677 if (DEBUG_INPUT) Log.v(
5678 TAG, "finishedKey: client=" + client.asBinder()
5679 + ", force=" + force + ", last=" + mLastBinder
5680 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 if (returnWhat == RETURN_PENDING_POINTER) {
5683 qev = session.mPendingPointerMove;
5684 win = session.mPendingPointerWindow;
5685 session.mPendingPointerMove = null;
5686 session.mPendingPointerWindow = null;
5687 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5688 qev = session.mPendingTrackballMove;
5689 win = session.mPendingTrackballWindow;
5690 session.mPendingTrackballMove = null;
5691 session.mPendingTrackballWindow = null;
5692 }
Romain Guy06882f82009-06-10 13:36:04 -07005693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 if (mLastBinder == client.asBinder()) {
5695 if (DEBUG_INPUT) Log.v(
5696 TAG, "finishedKey: last paused="
5697 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5698 if (mLastWin != null && (!mLastWin.mToken.paused || force
5699 || !mEventDispatching)) {
5700 doFinishedKeyLocked(false);
5701 } else {
5702 // Make sure to wake up anyone currently waiting to
5703 // dispatch a key, so they can re-evaluate their
5704 // current situation.
5705 mFinished = true;
5706 notifyAll();
5707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 }
Romain Guy06882f82009-06-10 13:36:04 -07005709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005711 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 if (DEBUG_INPUT) Log.v(TAG,
5713 "Returning pending motion: " + res);
5714 mQueue.recycleEvent(qev);
5715 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5716 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005720
5721 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5722 synchronized (mWindowMap) {
5723 if (mWallpaperTarget == win) {
5724 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5725 }
5726 }
5727 }
5728
5729 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 }
5731
5732 void tickle() {
5733 synchronized (this) {
5734 notifyAll();
5735 }
5736 }
Romain Guy06882f82009-06-10 13:36:04 -07005737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 void handleNewWindowLocked(WindowState newWindow) {
5739 if (!newWindow.canReceiveKeys()) {
5740 return;
5741 }
5742 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005743 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 TAG, "New key dispatch window: win="
5745 + newWindow.mClient.asBinder()
5746 + ", last=" + mLastBinder
5747 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5748 + "), finished=" + mFinished + ", paused="
5749 + newWindow.mToken.paused);
5750
5751 // Displaying a window implicitly causes dispatching to
5752 // be unpaused. (This is to protect against bugs if someone
5753 // pauses dispatching but forgets to resume.)
5754 newWindow.mToken.paused = false;
5755
5756 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757
5758 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5759 if (DEBUG_INPUT) Log.v(TAG,
5760 "New SYSTEM_ERROR window; resetting state");
5761 mLastWin = null;
5762 mLastBinder = null;
5763 mMotionTarget = null;
5764 mFinished = true;
5765 } else if (mLastWin != null) {
5766 // If the new window is above the window we are
5767 // waiting on, then stop waiting and let key dispatching
5768 // start on the new guy.
5769 if (DEBUG_INPUT) Log.v(
5770 TAG, "Last win layer=" + mLastWin.mLayer
5771 + ", new win layer=" + newWindow.mLayer);
5772 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005773 // The new window is above the old; finish pending input to the last
5774 // window and start directing it to the new one.
5775 mLastWin.mToken.paused = false;
5776 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005778 // Either the new window is lower, so there is no need to wake key waiters,
5779 // or we just finished key input to the previous window, which implicitly
5780 // notified the key waiters. In both cases, we don't need to issue the
5781 // notification here.
5782 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 }
5784
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005785 // Now that we've put a new window state in place, make the event waiter
5786 // take notice and retarget its attentions.
5787 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 }
5789 }
5790
5791 void pauseDispatchingLocked(WindowToken token) {
5792 synchronized (this)
5793 {
5794 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5795 token.paused = true;
5796
5797 /*
5798 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5799 mPaused = true;
5800 } else {
5801 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005802 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005804 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005806 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 }
5808 }
5809 */
5810 }
5811 }
5812
5813 void resumeDispatchingLocked(WindowToken token) {
5814 synchronized (this) {
5815 if (token.paused) {
5816 if (DEBUG_INPUT) Log.v(
5817 TAG, "Resuming WindowToken " + token
5818 + ", last=" + mLastBinder
5819 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5820 + "), finished=" + mFinished + ", paused="
5821 + token.paused);
5822 token.paused = false;
5823 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5824 doFinishedKeyLocked(true);
5825 } else {
5826 notifyAll();
5827 }
5828 }
5829 }
5830 }
5831
5832 void setEventDispatchingLocked(boolean enabled) {
5833 synchronized (this) {
5834 mEventDispatching = enabled;
5835 notifyAll();
5836 }
5837 }
Romain Guy06882f82009-06-10 13:36:04 -07005838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 void appSwitchComing() {
5840 synchronized (this) {
5841 // Don't wait for more than .5 seconds for app to finish
5842 // processing the pending events.
5843 long now = SystemClock.uptimeMillis() + 500;
5844 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5845 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5846 mTimeToSwitch = now;
5847 }
5848 notifyAll();
5849 }
5850 }
Romain Guy06882f82009-06-10 13:36:04 -07005851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 private final void doFinishedKeyLocked(boolean doRecycle) {
5853 if (mLastWin != null) {
5854 releasePendingPointerLocked(mLastWin.mSession);
5855 releasePendingTrackballLocked(mLastWin.mSession);
5856 }
Romain Guy06882f82009-06-10 13:36:04 -07005857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 if (mLastWin == null || !mLastWin.mToken.paused
5859 || !mLastWin.isVisibleLw()) {
5860 // If the current window has been paused, we aren't -really-
5861 // finished... so let the waiters still wait.
5862 mLastWin = null;
5863 mLastBinder = null;
5864 }
5865 mFinished = true;
5866 notifyAll();
5867 }
5868 }
5869
5870 private class KeyQ extends KeyInputQueue
5871 implements KeyInputQueue.FilterCallback {
5872 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005875 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5877 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5878 "KEEP_SCREEN_ON_FLAG");
5879 mHoldingScreen.setReferenceCounted(false);
5880 }
5881
5882 @Override
5883 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5884 if (mPolicy.preprocessInputEventTq(event)) {
5885 return true;
5886 }
Romain Guy06882f82009-06-10 13:36:04 -07005887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 switch (event.type) {
5889 case RawInputEvent.EV_KEY: {
5890 // XXX begin hack
5891 if (DEBUG) {
5892 if (event.keycode == KeyEvent.KEYCODE_G) {
5893 if (event.value != 0) {
5894 // G down
5895 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5896 }
5897 return false;
5898 }
5899 if (event.keycode == KeyEvent.KEYCODE_D) {
5900 if (event.value != 0) {
5901 //dump();
5902 }
5903 return false;
5904 }
5905 }
5906 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 boolean screenIsOff = !mPowerManager.screenIsOn();
5909 boolean screenIsDim = !mPowerManager.screenIsBright();
5910 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5913 mPowerManager.goToSleep(event.when);
5914 }
5915
5916 if (screenIsOff) {
5917 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5918 }
5919 if (screenIsDim) {
5920 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5921 }
5922 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5923 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005924 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 }
Romain Guy06882f82009-06-10 13:36:04 -07005926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5928 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5929 filterQueue(this);
5930 mKeyWaiter.appSwitchComing();
5931 }
5932 return true;
5933 } else {
5934 return false;
5935 }
5936 }
Romain Guy06882f82009-06-10 13:36:04 -07005937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 case RawInputEvent.EV_REL: {
5939 boolean screenIsOff = !mPowerManager.screenIsOn();
5940 boolean screenIsDim = !mPowerManager.screenIsBright();
5941 if (screenIsOff) {
5942 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5943 device.classes, event)) {
5944 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5945 return false;
5946 }
5947 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5948 }
5949 if (screenIsDim) {
5950 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5951 }
5952 return true;
5953 }
Romain Guy06882f82009-06-10 13:36:04 -07005954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 case RawInputEvent.EV_ABS: {
5956 boolean screenIsOff = !mPowerManager.screenIsOn();
5957 boolean screenIsDim = !mPowerManager.screenIsBright();
5958 if (screenIsOff) {
5959 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5960 device.classes, event)) {
5961 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5962 return false;
5963 }
5964 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5965 }
5966 if (screenIsDim) {
5967 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5968 }
5969 return true;
5970 }
Romain Guy06882f82009-06-10 13:36:04 -07005971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 default:
5973 return true;
5974 }
5975 }
5976
5977 public int filterEvent(QueuedEvent ev) {
5978 switch (ev.classType) {
5979 case RawInputEvent.CLASS_KEYBOARD:
5980 KeyEvent ke = (KeyEvent)ev.event;
5981 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5982 Log.w(TAG, "Dropping movement key during app switch: "
5983 + ke.getKeyCode() + ", action=" + ke.getAction());
5984 return FILTER_REMOVE;
5985 }
5986 return FILTER_ABORT;
5987 default:
5988 return FILTER_KEEP;
5989 }
5990 }
Romain Guy06882f82009-06-10 13:36:04 -07005991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 /**
5993 * Must be called with the main window manager lock held.
5994 */
5995 void setHoldScreenLocked(boolean holding) {
5996 boolean state = mHoldingScreen.isHeld();
5997 if (holding != state) {
5998 if (holding) {
5999 mHoldingScreen.acquire();
6000 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006001 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 mHoldingScreen.release();
6003 }
6004 }
6005 }
Michael Chan53071d62009-05-13 17:29:48 -07006006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007
6008 public boolean detectSafeMode() {
6009 mSafeMode = mPolicy.detectSafeMode();
6010 return mSafeMode;
6011 }
Romain Guy06882f82009-06-10 13:36:04 -07006012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 public void systemReady() {
6014 mPolicy.systemReady();
6015 }
Romain Guy06882f82009-06-10 13:36:04 -07006016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 private final class InputDispatcherThread extends Thread {
6018 // Time to wait when there is nothing to do: 9999 seconds.
6019 static final int LONG_WAIT=9999*1000;
6020
6021 public InputDispatcherThread() {
6022 super("InputDispatcher");
6023 }
Romain Guy06882f82009-06-10 13:36:04 -07006024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 @Override
6026 public void run() {
6027 while (true) {
6028 try {
6029 process();
6030 } catch (Exception e) {
6031 Log.e(TAG, "Exception in input dispatcher", e);
6032 }
6033 }
6034 }
Romain Guy06882f82009-06-10 13:36:04 -07006035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 private void process() {
6037 android.os.Process.setThreadPriority(
6038 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 // The last key event we saw
6041 KeyEvent lastKey = null;
6042
6043 // Last keydown time for auto-repeating keys
6044 long lastKeyTime = SystemClock.uptimeMillis();
6045 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006046 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047
Romain Guy06882f82009-06-10 13:36:04 -07006048 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 int keyRepeatCount = 0;
6050
6051 // Need to report that configuration has changed?
6052 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 while (true) {
6055 long curTime = SystemClock.uptimeMillis();
6056
6057 if (DEBUG_INPUT) Log.v(
6058 TAG, "Waiting for next key: now=" + curTime
6059 + ", repeat @ " + nextKeyTime);
6060
6061 // Retrieve next event, waiting only as long as the next
6062 // repeat timeout. If the configuration has changed, then
6063 // don't wait at all -- we'll report the change as soon as
6064 // we have processed all events.
6065 QueuedEvent ev = mQueue.getEvent(
6066 (int)((!configChanged && curTime < nextKeyTime)
6067 ? (nextKeyTime-curTime) : 0));
6068
6069 if (DEBUG_INPUT && ev != null) Log.v(
6070 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6071
Michael Chan53071d62009-05-13 17:29:48 -07006072 if (MEASURE_LATENCY) {
6073 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6074 }
6075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 try {
6077 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006078 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 int eventType;
6080 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6081 eventType = eventType((MotionEvent)ev.event);
6082 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6083 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6084 eventType = LocalPowerManager.BUTTON_EVENT;
6085 } else {
6086 eventType = LocalPowerManager.OTHER_EVENT;
6087 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006088 try {
Michael Chan53071d62009-05-13 17:29:48 -07006089 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006090 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006091 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006092 mBatteryStats.noteInputEvent();
6093 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006094 } catch (RemoteException e) {
6095 // Ignore
6096 }
Michael Chane10de972009-05-18 11:24:50 -07006097
6098 if (eventType != TOUCH_EVENT
6099 && eventType != LONG_TOUCH_EVENT
6100 && eventType != CHEEK_EVENT) {
6101 mPowerManager.userActivity(curTime, false,
6102 eventType, false);
6103 } else if (mLastTouchEventType != eventType
6104 || (curTime - mLastUserActivityCallTime)
6105 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6106 mLastUserActivityCallTime = curTime;
6107 mLastTouchEventType = eventType;
6108 mPowerManager.userActivity(curTime, false,
6109 eventType, false);
6110 }
6111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 switch (ev.classType) {
6113 case RawInputEvent.CLASS_KEYBOARD:
6114 KeyEvent ke = (KeyEvent)ev.event;
6115 if (ke.isDown()) {
6116 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006117 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 keyRepeatCount = 0;
6119 lastKeyTime = curTime;
6120 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006121 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 if (DEBUG_INPUT) Log.v(
6123 TAG, "Received key down: first repeat @ "
6124 + nextKeyTime);
6125 } else {
6126 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006127 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128 // Arbitrary long timeout.
6129 lastKeyTime = curTime;
6130 nextKeyTime = curTime + LONG_WAIT;
6131 if (DEBUG_INPUT) Log.v(
6132 TAG, "Received key up: ignore repeat @ "
6133 + nextKeyTime);
6134 }
6135 dispatchKey((KeyEvent)ev.event, 0, 0);
6136 mQueue.recycleEvent(ev);
6137 break;
6138 case RawInputEvent.CLASS_TOUCHSCREEN:
6139 //Log.i(TAG, "Read next event " + ev);
6140 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6141 break;
6142 case RawInputEvent.CLASS_TRACKBALL:
6143 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6144 break;
6145 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6146 configChanged = true;
6147 break;
6148 default:
6149 mQueue.recycleEvent(ev);
6150 break;
6151 }
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 } else if (configChanged) {
6154 configChanged = false;
6155 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 } else if (lastKey != null) {
6158 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006160 // Timeout occurred while key was down. If it is at or
6161 // past the key repeat time, dispatch the repeat.
6162 if (DEBUG_INPUT) Log.v(
6163 TAG, "Key timeout: repeat=" + nextKeyTime
6164 + ", now=" + curTime);
6165 if (curTime < nextKeyTime) {
6166 continue;
6167 }
Romain Guy06882f82009-06-10 13:36:04 -07006168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 lastKeyTime = nextKeyTime;
6170 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6171 keyRepeatCount++;
6172 if (DEBUG_INPUT) Log.v(
6173 TAG, "Key repeat: count=" + keyRepeatCount
6174 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006175 KeyEvent newEvent;
6176 if (downTime != 0 && (downTime
6177 + ViewConfiguration.getLongPressTimeout())
6178 <= curTime) {
6179 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6180 curTime, keyRepeatCount,
6181 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6182 downTime = 0;
6183 } else {
6184 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6185 curTime, keyRepeatCount);
6186 }
6187 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 } else {
6190 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 lastKeyTime = curTime;
6193 nextKeyTime = curTime + LONG_WAIT;
6194 }
Romain Guy06882f82009-06-10 13:36:04 -07006195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 } catch (Exception e) {
6197 Log.e(TAG,
6198 "Input thread received uncaught exception: " + e, e);
6199 }
6200 }
6201 }
6202 }
6203
6204 // -------------------------------------------------------------
6205 // Client Session State
6206 // -------------------------------------------------------------
6207
6208 private final class Session extends IWindowSession.Stub
6209 implements IBinder.DeathRecipient {
6210 final IInputMethodClient mClient;
6211 final IInputContext mInputContext;
6212 final int mUid;
6213 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006214 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 SurfaceSession mSurfaceSession;
6216 int mNumWindow = 0;
6217 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 /**
6220 * Current pointer move event being dispatched to client window... must
6221 * hold key lock to access.
6222 */
6223 QueuedEvent mPendingPointerMove;
6224 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 /**
6227 * Current trackball move event being dispatched to client window... must
6228 * hold key lock to access.
6229 */
6230 QueuedEvent mPendingTrackballMove;
6231 WindowState mPendingTrackballWindow;
6232
6233 public Session(IInputMethodClient client, IInputContext inputContext) {
6234 mClient = client;
6235 mInputContext = inputContext;
6236 mUid = Binder.getCallingUid();
6237 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006238 StringBuilder sb = new StringBuilder();
6239 sb.append("Session{");
6240 sb.append(Integer.toHexString(System.identityHashCode(this)));
6241 sb.append(" uid ");
6242 sb.append(mUid);
6243 sb.append("}");
6244 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006246 synchronized (mWindowMap) {
6247 if (mInputMethodManager == null && mHaveInputMethods) {
6248 IBinder b = ServiceManager.getService(
6249 Context.INPUT_METHOD_SERVICE);
6250 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6251 }
6252 }
6253 long ident = Binder.clearCallingIdentity();
6254 try {
6255 // Note: it is safe to call in to the input method manager
6256 // here because we are not holding our lock.
6257 if (mInputMethodManager != null) {
6258 mInputMethodManager.addClient(client, inputContext,
6259 mUid, mPid);
6260 } else {
6261 client.setUsingInputMethod(false);
6262 }
6263 client.asBinder().linkToDeath(this, 0);
6264 } catch (RemoteException e) {
6265 // The caller has died, so we can just forget about this.
6266 try {
6267 if (mInputMethodManager != null) {
6268 mInputMethodManager.removeClient(client);
6269 }
6270 } catch (RemoteException ee) {
6271 }
6272 } finally {
6273 Binder.restoreCallingIdentity(ident);
6274 }
6275 }
Romain Guy06882f82009-06-10 13:36:04 -07006276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006277 @Override
6278 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6279 throws RemoteException {
6280 try {
6281 return super.onTransact(code, data, reply, flags);
6282 } catch (RuntimeException e) {
6283 // Log all 'real' exceptions thrown to the caller
6284 if (!(e instanceof SecurityException)) {
6285 Log.e(TAG, "Window Session Crash", e);
6286 }
6287 throw e;
6288 }
6289 }
6290
6291 public void binderDied() {
6292 // Note: it is safe to call in to the input method manager
6293 // here because we are not holding our lock.
6294 try {
6295 if (mInputMethodManager != null) {
6296 mInputMethodManager.removeClient(mClient);
6297 }
6298 } catch (RemoteException e) {
6299 }
6300 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006301 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 mClientDead = true;
6303 killSessionLocked();
6304 }
6305 }
6306
6307 public int add(IWindow window, WindowManager.LayoutParams attrs,
6308 int viewVisibility, Rect outContentInsets) {
6309 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6310 }
Romain Guy06882f82009-06-10 13:36:04 -07006311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 public void remove(IWindow window) {
6313 removeWindow(this, window);
6314 }
Romain Guy06882f82009-06-10 13:36:04 -07006315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6317 int requestedWidth, int requestedHeight, int viewFlags,
6318 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6319 Rect outVisibleInsets, Surface outSurface) {
6320 return relayoutWindow(this, window, attrs,
6321 requestedWidth, requestedHeight, viewFlags, insetsPending,
6322 outFrame, outContentInsets, outVisibleInsets, outSurface);
6323 }
Romain Guy06882f82009-06-10 13:36:04 -07006324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 public void setTransparentRegion(IWindow window, Region region) {
6326 setTransparentRegionWindow(this, window, region);
6327 }
Romain Guy06882f82009-06-10 13:36:04 -07006328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 public void setInsets(IWindow window, int touchableInsets,
6330 Rect contentInsets, Rect visibleInsets) {
6331 setInsetsWindow(this, window, touchableInsets, contentInsets,
6332 visibleInsets);
6333 }
Romain Guy06882f82009-06-10 13:36:04 -07006334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6336 getWindowDisplayFrame(this, window, outDisplayFrame);
6337 }
Romain Guy06882f82009-06-10 13:36:04 -07006338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 public void finishDrawing(IWindow window) {
6340 if (localLOGV) Log.v(
6341 TAG, "IWindow finishDrawing called for " + window);
6342 finishDrawingWindow(this, window);
6343 }
6344
6345 public void finishKey(IWindow window) {
6346 if (localLOGV) Log.v(
6347 TAG, "IWindow finishKey called for " + window);
6348 mKeyWaiter.finishedKey(this, window, false,
6349 KeyWaiter.RETURN_NOTHING);
6350 }
6351
6352 public MotionEvent getPendingPointerMove(IWindow window) {
6353 if (localLOGV) Log.v(
6354 TAG, "IWindow getPendingMotionEvent called for " + window);
6355 return mKeyWaiter.finishedKey(this, window, false,
6356 KeyWaiter.RETURN_PENDING_POINTER);
6357 }
Romain Guy06882f82009-06-10 13:36:04 -07006358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 public MotionEvent getPendingTrackballMove(IWindow window) {
6360 if (localLOGV) Log.v(
6361 TAG, "IWindow getPendingMotionEvent called for " + window);
6362 return mKeyWaiter.finishedKey(this, window, false,
6363 KeyWaiter.RETURN_PENDING_TRACKBALL);
6364 }
6365
6366 public void setInTouchMode(boolean mode) {
6367 synchronized(mWindowMap) {
6368 mInTouchMode = mode;
6369 }
6370 }
6371
6372 public boolean getInTouchMode() {
6373 synchronized(mWindowMap) {
6374 return mInTouchMode;
6375 }
6376 }
6377
6378 public boolean performHapticFeedback(IWindow window, int effectId,
6379 boolean always) {
6380 synchronized(mWindowMap) {
6381 long ident = Binder.clearCallingIdentity();
6382 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006383 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 windowForClientLocked(this, window), effectId, always);
6385 } finally {
6386 Binder.restoreCallingIdentity(ident);
6387 }
6388 }
6389 }
Romain Guy06882f82009-06-10 13:36:04 -07006390
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006391 public void setWallpaperPosition(IBinder window, float x, float y) {
6392 synchronized(mWindowMap) {
6393 long ident = Binder.clearCallingIdentity();
6394 try {
6395 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6396 x, y);
6397 } finally {
6398 Binder.restoreCallingIdentity(ident);
6399 }
6400 }
6401 }
6402
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006403 public void wallpaperOffsetsComplete(IBinder window) {
6404 WindowManagerService.this.wallpaperOffsetsComplete(window);
6405 }
6406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 void windowAddedLocked() {
6408 if (mSurfaceSession == null) {
6409 if (localLOGV) Log.v(
6410 TAG, "First window added to " + this + ", creating SurfaceSession");
6411 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006412 if (SHOW_TRANSACTIONS) Log.i(
6413 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 mSessions.add(this);
6415 }
6416 mNumWindow++;
6417 }
6418
6419 void windowRemovedLocked() {
6420 mNumWindow--;
6421 killSessionLocked();
6422 }
Romain Guy06882f82009-06-10 13:36:04 -07006423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 void killSessionLocked() {
6425 if (mNumWindow <= 0 && mClientDead) {
6426 mSessions.remove(this);
6427 if (mSurfaceSession != null) {
6428 if (localLOGV) Log.v(
6429 TAG, "Last window removed from " + this
6430 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006431 if (SHOW_TRANSACTIONS) Log.i(
6432 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 try {
6434 mSurfaceSession.kill();
6435 } catch (Exception e) {
6436 Log.w(TAG, "Exception thrown when killing surface session "
6437 + mSurfaceSession + " in session " + this
6438 + ": " + e.toString());
6439 }
6440 mSurfaceSession = null;
6441 }
6442 }
6443 }
Romain Guy06882f82009-06-10 13:36:04 -07006444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006446 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6447 pw.print(" mClientDead="); pw.print(mClientDead);
6448 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6449 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6450 pw.print(prefix);
6451 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6452 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6453 }
6454 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6455 pw.print(prefix);
6456 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6457 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 }
6460
6461 @Override
6462 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006463 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 }
6465 }
6466
6467 // -------------------------------------------------------------
6468 // Client Window State
6469 // -------------------------------------------------------------
6470
6471 private final class WindowState implements WindowManagerPolicy.WindowState {
6472 final Session mSession;
6473 final IWindow mClient;
6474 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006475 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 AppWindowToken mAppToken;
6477 AppWindowToken mTargetAppToken;
6478 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6479 final DeathRecipient mDeathRecipient;
6480 final WindowState mAttachedWindow;
6481 final ArrayList mChildWindows = new ArrayList();
6482 final int mBaseLayer;
6483 final int mSubLayer;
6484 final boolean mLayoutAttached;
6485 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006486 final boolean mIsWallpaper;
6487 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 int mViewVisibility;
6489 boolean mPolicyVisibility = true;
6490 boolean mPolicyVisibilityAfterAnim = true;
6491 boolean mAppFreezing;
6492 Surface mSurface;
6493 boolean mAttachedHidden; // is our parent window hidden?
6494 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006495 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 int mRequestedWidth;
6497 int mRequestedHeight;
6498 int mLastRequestedWidth;
6499 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 int mLayer;
6501 int mAnimLayer;
6502 int mLastLayer;
6503 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006504 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006505 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506
6507 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 // Actual frame shown on-screen (may be modified by animation)
6510 final Rect mShownFrame = new Rect();
6511 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 /**
6514 * Insets that determine the actually visible area
6515 */
6516 final Rect mVisibleInsets = new Rect();
6517 final Rect mLastVisibleInsets = new Rect();
6518 boolean mVisibleInsetsChanged;
6519
6520 /**
6521 * Insets that are covered by system windows
6522 */
6523 final Rect mContentInsets = new Rect();
6524 final Rect mLastContentInsets = new Rect();
6525 boolean mContentInsetsChanged;
6526
6527 /**
6528 * Set to true if we are waiting for this window to receive its
6529 * given internal insets before laying out other windows based on it.
6530 */
6531 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 /**
6534 * These are the content insets that were given during layout for
6535 * this window, to be applied to windows behind it.
6536 */
6537 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 /**
6540 * These are the visible insets that were given during layout for
6541 * this window, to be applied to windows behind it.
6542 */
6543 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 /**
6546 * Flag indicating whether the touchable region should be adjusted by
6547 * the visible insets; if false the area outside the visible insets is
6548 * NOT touchable, so we must use those to adjust the frame during hit
6549 * tests.
6550 */
6551 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 // Current transformation being applied.
6554 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6555 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6556 float mHScale=1, mVScale=1;
6557 float mLastHScale=1, mLastVScale=1;
6558 final Matrix mTmpMatrix = new Matrix();
6559
6560 // "Real" frame that the application sees.
6561 final Rect mFrame = new Rect();
6562 final Rect mLastFrame = new Rect();
6563
6564 final Rect mContainingFrame = new Rect();
6565 final Rect mDisplayFrame = new Rect();
6566 final Rect mContentFrame = new Rect();
6567 final Rect mVisibleFrame = new Rect();
6568
6569 float mShownAlpha = 1;
6570 float mAlpha = 1;
6571 float mLastAlpha = 1;
6572
6573 // Set to true if, when the window gets displayed, it should perform
6574 // an enter animation.
6575 boolean mEnterAnimationPending;
6576
6577 // Currently running animation.
6578 boolean mAnimating;
6579 boolean mLocalAnimating;
6580 Animation mAnimation;
6581 boolean mAnimationIsEntrance;
6582 boolean mHasTransformation;
6583 boolean mHasLocalTransformation;
6584 final Transformation mTransformation = new Transformation();
6585
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006586 // If a window showing a wallpaper: the requested offset for the
6587 // wallpaper; if a wallpaper window: the currently applied offset.
6588 float mWallpaperX = -1;
6589 float mWallpaperY = -1;
6590
6591 // Wallpaper windows: pixels offset based on above variables.
6592 int mXOffset;
6593 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 // This is set after IWindowSession.relayout() has been called at
6596 // least once for the window. It allows us to detect the situation
6597 // where we don't yet have a surface, but should have one soon, so
6598 // we can give the window focus before waiting for the relayout.
6599 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 // This is set after the Surface has been created but before the
6602 // window has been drawn. During this time the surface is hidden.
6603 boolean mDrawPending;
6604
6605 // This is set after the window has finished drawing for the first
6606 // time but before its surface is shown. The surface will be
6607 // displayed when the next layout is run.
6608 boolean mCommitDrawPending;
6609
6610 // This is set during the time after the window's drawing has been
6611 // committed, and before its surface is actually shown. It is used
6612 // to delay showing the surface until all windows in a token are ready
6613 // to be shown.
6614 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 // Set when the window has been shown in the screen the first time.
6617 boolean mHasDrawn;
6618
6619 // Currently running an exit animation?
6620 boolean mExiting;
6621
6622 // Currently on the mDestroySurface list?
6623 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 // Completely remove from window manager after exit animation?
6626 boolean mRemoveOnExit;
6627
6628 // Set when the orientation is changing and this window has not yet
6629 // been updated for the new orientation.
6630 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 // Is this window now (or just being) removed?
6633 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 WindowState(Session s, IWindow c, WindowToken token,
6636 WindowState attachedWindow, WindowManager.LayoutParams a,
6637 int viewVisibility) {
6638 mSession = s;
6639 mClient = c;
6640 mToken = token;
6641 mAttrs.copyFrom(a);
6642 mViewVisibility = viewVisibility;
6643 DeathRecipient deathRecipient = new DeathRecipient();
6644 mAlpha = a.alpha;
6645 if (localLOGV) Log.v(
6646 TAG, "Window " + this + " client=" + c.asBinder()
6647 + " token=" + token + " (" + mAttrs.token + ")");
6648 try {
6649 c.asBinder().linkToDeath(deathRecipient, 0);
6650 } catch (RemoteException e) {
6651 mDeathRecipient = null;
6652 mAttachedWindow = null;
6653 mLayoutAttached = false;
6654 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006655 mIsWallpaper = false;
6656 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 mBaseLayer = 0;
6658 mSubLayer = 0;
6659 return;
6660 }
6661 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6664 mAttrs.type <= LAST_SUB_WINDOW)) {
6665 // The multiplier here is to reserve space for multiple
6666 // windows in the same type layer.
6667 mBaseLayer = mPolicy.windowTypeToLayerLw(
6668 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6669 + TYPE_LAYER_OFFSET;
6670 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6671 mAttachedWindow = attachedWindow;
6672 mAttachedWindow.mChildWindows.add(this);
6673 mLayoutAttached = mAttrs.type !=
6674 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6675 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6676 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006677 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6678 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 } else {
6680 // The multiplier here is to reserve space for multiple
6681 // windows in the same type layer.
6682 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6683 * TYPE_LAYER_MULTIPLIER
6684 + TYPE_LAYER_OFFSET;
6685 mSubLayer = 0;
6686 mAttachedWindow = null;
6687 mLayoutAttached = false;
6688 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6689 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006690 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6691 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 }
6693
6694 WindowState appWin = this;
6695 while (appWin.mAttachedWindow != null) {
6696 appWin = mAttachedWindow;
6697 }
6698 WindowToken appToken = appWin.mToken;
6699 while (appToken.appWindowToken == null) {
6700 WindowToken parent = mTokenMap.get(appToken.token);
6701 if (parent == null || appToken == parent) {
6702 break;
6703 }
6704 appToken = parent;
6705 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006706 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006707 mAppToken = appToken.appWindowToken;
6708
6709 mSurface = null;
6710 mRequestedWidth = 0;
6711 mRequestedHeight = 0;
6712 mLastRequestedWidth = 0;
6713 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006714 mXOffset = 0;
6715 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 mLayer = 0;
6717 mAnimLayer = 0;
6718 mLastLayer = 0;
6719 }
6720
6721 void attach() {
6722 if (localLOGV) Log.v(
6723 TAG, "Attaching " + this + " token=" + mToken
6724 + ", list=" + mToken.windows);
6725 mSession.windowAddedLocked();
6726 }
6727
6728 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6729 mHaveFrame = true;
6730
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006731 final Rect container = mContainingFrame;
6732 container.set(pf);
6733
6734 final Rect display = mDisplayFrame;
6735 display.set(df);
6736
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006737 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006738 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006739 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6740 display.intersect(mCompatibleScreenFrame);
6741 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006742 }
6743
6744 final int pw = container.right - container.left;
6745 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746
6747 int w,h;
6748 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6749 w = mAttrs.width < 0 ? pw : mAttrs.width;
6750 h = mAttrs.height< 0 ? ph : mAttrs.height;
6751 } else {
6752 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6753 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6754 }
Romain Guy06882f82009-06-10 13:36:04 -07006755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 final Rect content = mContentFrame;
6757 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 final Rect visible = mVisibleFrame;
6760 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006763 final int fw = frame.width();
6764 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6767 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6768
6769 Gravity.apply(mAttrs.gravity, w, h, container,
6770 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6771 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6772
6773 //System.out.println("Out: " + mFrame);
6774
6775 // Now make sure the window fits in the overall display.
6776 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 // Make sure the content and visible frames are inside of the
6779 // final window frame.
6780 if (content.left < frame.left) content.left = frame.left;
6781 if (content.top < frame.top) content.top = frame.top;
6782 if (content.right > frame.right) content.right = frame.right;
6783 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6784 if (visible.left < frame.left) visible.left = frame.left;
6785 if (visible.top < frame.top) visible.top = frame.top;
6786 if (visible.right > frame.right) visible.right = frame.right;
6787 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 final Rect contentInsets = mContentInsets;
6790 contentInsets.left = content.left-frame.left;
6791 contentInsets.top = content.top-frame.top;
6792 contentInsets.right = frame.right-content.right;
6793 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 final Rect visibleInsets = mVisibleInsets;
6796 visibleInsets.left = visible.left-frame.left;
6797 visibleInsets.top = visible.top-frame.top;
6798 visibleInsets.right = frame.right-visible.right;
6799 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006800
Dianne Hackborn284ac932009-08-28 10:34:25 -07006801 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6802 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006803 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006804 }
6805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 if (localLOGV) {
6807 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6808 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6809 Log.v(TAG, "Resolving (mRequestedWidth="
6810 + mRequestedWidth + ", mRequestedheight="
6811 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6812 + "): frame=" + mFrame.toShortString()
6813 + " ci=" + contentInsets.toShortString()
6814 + " vi=" + visibleInsets.toShortString());
6815 //}
6816 }
6817 }
Romain Guy06882f82009-06-10 13:36:04 -07006818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006819 public Rect getFrameLw() {
6820 return mFrame;
6821 }
6822
6823 public Rect getShownFrameLw() {
6824 return mShownFrame;
6825 }
6826
6827 public Rect getDisplayFrameLw() {
6828 return mDisplayFrame;
6829 }
6830
6831 public Rect getContentFrameLw() {
6832 return mContentFrame;
6833 }
6834
6835 public Rect getVisibleFrameLw() {
6836 return mVisibleFrame;
6837 }
6838
6839 public boolean getGivenInsetsPendingLw() {
6840 return mGivenInsetsPending;
6841 }
6842
6843 public Rect getGivenContentInsetsLw() {
6844 return mGivenContentInsets;
6845 }
Romain Guy06882f82009-06-10 13:36:04 -07006846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 public Rect getGivenVisibleInsetsLw() {
6848 return mGivenVisibleInsets;
6849 }
Romain Guy06882f82009-06-10 13:36:04 -07006850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 public WindowManager.LayoutParams getAttrs() {
6852 return mAttrs;
6853 }
6854
6855 public int getSurfaceLayer() {
6856 return mLayer;
6857 }
Romain Guy06882f82009-06-10 13:36:04 -07006858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 public IApplicationToken getAppToken() {
6860 return mAppToken != null ? mAppToken.appToken : null;
6861 }
6862
6863 public boolean hasAppShownWindows() {
6864 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6865 }
6866
6867 public boolean hasAppStartingIcon() {
6868 return mAppToken != null ? (mAppToken.startingData != null) : false;
6869 }
6870
6871 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6872 return mAppToken != null ? mAppToken.startingWindow : null;
6873 }
6874
6875 public void setAnimation(Animation anim) {
6876 if (localLOGV) Log.v(
6877 TAG, "Setting animation in " + this + ": " + anim);
6878 mAnimating = false;
6879 mLocalAnimating = false;
6880 mAnimation = anim;
6881 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6882 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6883 }
6884
6885 public void clearAnimation() {
6886 if (mAnimation != null) {
6887 mAnimating = true;
6888 mLocalAnimating = false;
6889 mAnimation = null;
6890 }
6891 }
Romain Guy06882f82009-06-10 13:36:04 -07006892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 Surface createSurfaceLocked() {
6894 if (mSurface == null) {
6895 mDrawPending = true;
6896 mCommitDrawPending = false;
6897 mReadyToShow = false;
6898 if (mAppToken != null) {
6899 mAppToken.allDrawn = false;
6900 }
6901
6902 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006903 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 flags |= Surface.PUSH_BUFFERS;
6905 }
6906
6907 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6908 flags |= Surface.SECURE;
6909 }
6910 if (DEBUG_VISIBILITY) Log.v(
6911 TAG, "Creating surface in session "
6912 + mSession.mSurfaceSession + " window " + this
6913 + " w=" + mFrame.width()
6914 + " h=" + mFrame.height() + " format="
6915 + mAttrs.format + " flags=" + flags);
6916
6917 int w = mFrame.width();
6918 int h = mFrame.height();
6919 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6920 // for a scaled surface, we always want the requested
6921 // size.
6922 w = mRequestedWidth;
6923 h = mRequestedHeight;
6924 }
6925
6926 try {
6927 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006928 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006930 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
6931 + mSurface + " IN SESSION "
6932 + mSession.mSurfaceSession
6933 + ": pid=" + mSession.mPid + " format="
6934 + mAttrs.format + " flags=0x"
6935 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 } catch (Surface.OutOfResourcesException e) {
6937 Log.w(TAG, "OutOfResourcesException creating surface");
6938 reclaimSomeSurfaceMemoryLocked(this, "create");
6939 return null;
6940 } catch (Exception e) {
6941 Log.e(TAG, "Exception creating surface", e);
6942 return null;
6943 }
Romain Guy06882f82009-06-10 13:36:04 -07006944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 if (localLOGV) Log.v(
6946 TAG, "Got surface: " + mSurface
6947 + ", set left=" + mFrame.left + " top=" + mFrame.top
6948 + ", animLayer=" + mAnimLayer);
6949 if (SHOW_TRANSACTIONS) {
6950 Log.i(TAG, ">>> OPEN TRANSACTION");
6951 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6952 + mAttrs.getTitle() + ") pos=(" +
6953 mFrame.left + "," + mFrame.top + ") (" +
6954 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6955 mAnimLayer + " HIDE");
6956 }
6957 Surface.openTransaction();
6958 try {
6959 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006960 mSurface.setPosition(mFrame.left + mXOffset,
6961 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 mSurface.setLayer(mAnimLayer);
6963 mSurface.hide();
6964 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006965 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
6966 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 mSurface.setFlags(Surface.SURFACE_DITHER,
6968 Surface.SURFACE_DITHER);
6969 }
6970 } catch (RuntimeException e) {
6971 Log.w(TAG, "Error creating surface in " + w, e);
6972 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6973 }
6974 mLastHidden = true;
6975 } finally {
6976 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6977 Surface.closeTransaction();
6978 }
6979 if (localLOGV) Log.v(
6980 TAG, "Created surface " + this);
6981 }
6982 return mSurface;
6983 }
Romain Guy06882f82009-06-10 13:36:04 -07006984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 void destroySurfaceLocked() {
6986 // Window is no longer on-screen, so can no longer receive
6987 // key events... if we were waiting for it to finish
6988 // handling a key event, the wait is over!
6989 mKeyWaiter.finishedKey(mSession, mClient, true,
6990 KeyWaiter.RETURN_NOTHING);
6991 mKeyWaiter.releasePendingPointerLocked(mSession);
6992 mKeyWaiter.releasePendingTrackballLocked(mSession);
6993
6994 if (mAppToken != null && this == mAppToken.startingWindow) {
6995 mAppToken.startingDisplayed = false;
6996 }
Romain Guy06882f82009-06-10 13:36:04 -07006997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 if (mSurface != null) {
6999 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007000 if (DEBUG_VISIBILITY) {
7001 RuntimeException e = new RuntimeException();
7002 e.fillInStackTrace();
7003 Log.w(TAG, "Window " + this + " destroying surface "
7004 + mSurface + ", session " + mSession, e);
7005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 if (SHOW_TRANSACTIONS) {
7007 RuntimeException ex = new RuntimeException();
7008 ex.fillInStackTrace();
7009 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7010 + mAttrs.getTitle() + ")", ex);
7011 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007012 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 } catch (RuntimeException e) {
7014 Log.w(TAG, "Exception thrown when destroying Window " + this
7015 + " surface " + mSurface + " session " + mSession
7016 + ": " + e.toString());
7017 }
7018 mSurface = null;
7019 mDrawPending = false;
7020 mCommitDrawPending = false;
7021 mReadyToShow = false;
7022
7023 int i = mChildWindows.size();
7024 while (i > 0) {
7025 i--;
7026 WindowState c = (WindowState)mChildWindows.get(i);
7027 c.mAttachedHidden = true;
7028 }
7029 }
7030 }
7031
7032 boolean finishDrawingLocked() {
7033 if (mDrawPending) {
7034 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7035 TAG, "finishDrawingLocked: " + mSurface);
7036 mCommitDrawPending = true;
7037 mDrawPending = false;
7038 return true;
7039 }
7040 return false;
7041 }
7042
7043 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007044 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7046 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007047 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007048 }
7049 mCommitDrawPending = false;
7050 mReadyToShow = true;
7051 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7052 final AppWindowToken atoken = mAppToken;
7053 if (atoken == null || atoken.allDrawn || starting) {
7054 performShowLocked();
7055 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007056 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057 }
7058
7059 // This must be called while inside a transaction.
7060 boolean performShowLocked() {
7061 if (DEBUG_VISIBILITY) {
7062 RuntimeException e = new RuntimeException();
7063 e.fillInStackTrace();
7064 Log.v(TAG, "performShow on " + this
7065 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7066 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7067 }
7068 if (mReadyToShow && isReadyForDisplay()) {
7069 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7070 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7071 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7072 + " during animation: policyVis=" + mPolicyVisibility
7073 + " attHidden=" + mAttachedHidden
7074 + " tok.hiddenRequested="
7075 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007076 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 + (mAppToken != null ? mAppToken.hidden : false)
7078 + " animating=" + mAnimating
7079 + " tok animating="
7080 + (mAppToken != null ? mAppToken.animating : false));
7081 if (!showSurfaceRobustlyLocked(this)) {
7082 return false;
7083 }
7084 mLastAlpha = -1;
7085 mHasDrawn = true;
7086 mLastHidden = false;
7087 mReadyToShow = false;
7088 enableScreenIfNeededLocked();
7089
7090 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007092 int i = mChildWindows.size();
7093 while (i > 0) {
7094 i--;
7095 WindowState c = (WindowState)mChildWindows.get(i);
7096 if (c.mSurface != null && c.mAttachedHidden) {
7097 c.mAttachedHidden = false;
7098 c.performShowLocked();
7099 }
7100 }
Romain Guy06882f82009-06-10 13:36:04 -07007101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 if (mAttrs.type != TYPE_APPLICATION_STARTING
7103 && mAppToken != null) {
7104 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007105
7106 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007107 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7108 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007110 // If this initial window is animating, stop it -- we
7111 // will do an animation to reveal it from behind the
7112 // starting window, so there is no need for it to also
7113 // be doing its own stuff.
7114 if (mAnimation != null) {
7115 mAnimation = null;
7116 // Make sure we clean up the animation.
7117 mAnimating = true;
7118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007119 mFinishedStarting.add(mAppToken);
7120 mH.sendEmptyMessage(H.FINISHED_STARTING);
7121 }
7122 mAppToken.updateReportedVisibilityLocked();
7123 }
7124 }
7125 return true;
7126 }
Romain Guy06882f82009-06-10 13:36:04 -07007127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 // This must be called while inside a transaction. Returns true if
7129 // there is more animation to run.
7130 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7131 if (!mDisplayFrozen) {
7132 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7135 mHasTransformation = true;
7136 mHasLocalTransformation = true;
7137 if (!mLocalAnimating) {
7138 if (DEBUG_ANIM) Log.v(
7139 TAG, "Starting animation in " + this +
7140 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7141 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7142 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7143 mAnimation.setStartTime(currentTime);
7144 mLocalAnimating = true;
7145 mAnimating = true;
7146 }
7147 mTransformation.clear();
7148 final boolean more = mAnimation.getTransformation(
7149 currentTime, mTransformation);
7150 if (DEBUG_ANIM) Log.v(
7151 TAG, "Stepped animation in " + this +
7152 ": more=" + more + ", xform=" + mTransformation);
7153 if (more) {
7154 // we're not done!
7155 return true;
7156 }
7157 if (DEBUG_ANIM) Log.v(
7158 TAG, "Finished animation in " + this +
7159 " @ " + currentTime);
7160 mAnimation = null;
7161 //WindowManagerService.this.dump();
7162 }
7163 mHasLocalTransformation = false;
7164 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007165 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 // When our app token is animating, we kind-of pretend like
7167 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7168 // part of this check means that we will only do this if
7169 // our window is not currently exiting, or it is not
7170 // locally animating itself. The idea being that one that
7171 // is exiting and doing a local animation should be removed
7172 // once that animation is done.
7173 mAnimating = true;
7174 mHasTransformation = true;
7175 mTransformation.clear();
7176 return false;
7177 } else if (mHasTransformation) {
7178 // Little trick to get through the path below to act like
7179 // we have finished an animation.
7180 mAnimating = true;
7181 } else if (isAnimating()) {
7182 mAnimating = true;
7183 }
7184 } else if (mAnimation != null) {
7185 // If the display is frozen, and there is a pending animation,
7186 // clear it and make sure we run the cleanup code.
7187 mAnimating = true;
7188 mLocalAnimating = true;
7189 mAnimation = null;
7190 }
Romain Guy06882f82009-06-10 13:36:04 -07007191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 if (!mAnimating && !mLocalAnimating) {
7193 return false;
7194 }
7195
7196 if (DEBUG_ANIM) Log.v(
7197 TAG, "Animation done in " + this + ": exiting=" + mExiting
7198 + ", reportedVisible="
7199 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 mAnimating = false;
7202 mLocalAnimating = false;
7203 mAnimation = null;
7204 mAnimLayer = mLayer;
7205 if (mIsImWindow) {
7206 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007207 } else if (mIsWallpaper) {
7208 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 }
7210 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7211 + " anim layer: " + mAnimLayer);
7212 mHasTransformation = false;
7213 mHasLocalTransformation = false;
7214 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7215 mTransformation.clear();
7216 if (mHasDrawn
7217 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7218 && mAppToken != null
7219 && mAppToken.firstWindowDrawn
7220 && mAppToken.startingData != null) {
7221 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7222 + mToken + ": first real window done animating");
7223 mFinishedStarting.add(mAppToken);
7224 mH.sendEmptyMessage(H.FINISHED_STARTING);
7225 }
Romain Guy06882f82009-06-10 13:36:04 -07007226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 finishExit();
7228
7229 if (mAppToken != null) {
7230 mAppToken.updateReportedVisibilityLocked();
7231 }
7232
7233 return false;
7234 }
7235
7236 void finishExit() {
7237 if (DEBUG_ANIM) Log.v(
7238 TAG, "finishExit in " + this
7239 + ": exiting=" + mExiting
7240 + " remove=" + mRemoveOnExit
7241 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 final int N = mChildWindows.size();
7244 for (int i=0; i<N; i++) {
7245 ((WindowState)mChildWindows.get(i)).finishExit();
7246 }
Romain Guy06882f82009-06-10 13:36:04 -07007247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 if (!mExiting) {
7249 return;
7250 }
Romain Guy06882f82009-06-10 13:36:04 -07007251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 if (isWindowAnimating()) {
7253 return;
7254 }
7255
7256 if (localLOGV) Log.v(
7257 TAG, "Exit animation finished in " + this
7258 + ": remove=" + mRemoveOnExit);
7259 if (mSurface != null) {
7260 mDestroySurface.add(this);
7261 mDestroying = true;
7262 if (SHOW_TRANSACTIONS) Log.i(
7263 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7264 try {
7265 mSurface.hide();
7266 } catch (RuntimeException e) {
7267 Log.w(TAG, "Error hiding surface in " + this, e);
7268 }
7269 mLastHidden = true;
7270 mKeyWaiter.releasePendingPointerLocked(mSession);
7271 }
7272 mExiting = false;
7273 if (mRemoveOnExit) {
7274 mPendingRemove.add(this);
7275 mRemoveOnExit = false;
7276 }
7277 }
Romain Guy06882f82009-06-10 13:36:04 -07007278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7280 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7281 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7282 if (dtdx < -.000001f || dtdx > .000001f) return false;
7283 if (dsdy < -.000001f || dsdy > .000001f) return false;
7284 return true;
7285 }
Romain Guy06882f82009-06-10 13:36:04 -07007286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 void computeShownFrameLocked() {
7288 final boolean selfTransformation = mHasLocalTransformation;
7289 Transformation attachedTransformation =
7290 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7291 ? mAttachedWindow.mTransformation : null;
7292 Transformation appTransformation =
7293 (mAppToken != null && mAppToken.hasTransformation)
7294 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007295
7296 // Wallpapers are animated based on the "real" window they
7297 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007298 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007299 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007300 if (mWallpaperTarget.mHasLocalTransformation) {
7301 attachedTransformation = mWallpaperTarget.mTransformation;
7302 }
7303 if (mWallpaperTarget.mAppToken != null &&
7304 mWallpaperTarget.mAppToken.hasTransformation) {
7305 appTransformation = mWallpaperTarget.mAppToken.transformation;
7306 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007307 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7308 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7309 }
7310 if (DEBUG_WALLPAPER && appTransformation != null) {
7311 Log.v(TAG, "WP target app xform: " + appTransformation);
7312 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007313 }
7314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 if (selfTransformation || attachedTransformation != null
7316 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007317 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007318 final Rect frame = mFrame;
7319 final float tmpFloats[] = mTmpFloats;
7320 final Matrix tmpMatrix = mTmpMatrix;
7321
7322 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007323 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007324 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007325 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007327 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007329 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 }
7331 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007332 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007333 }
7334
7335 // "convert" it into SurfaceFlinger's format
7336 // (a 2x2 matrix + an offset)
7337 // Here we must not transform the position of the surface
7338 // since it is already included in the transformation.
7339 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341 tmpMatrix.getValues(tmpFloats);
7342 mDsDx = tmpFloats[Matrix.MSCALE_X];
7343 mDtDx = tmpFloats[Matrix.MSKEW_X];
7344 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7345 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007346 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7347 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007348 int w = frame.width();
7349 int h = frame.height();
7350 mShownFrame.set(x, y, x+w, y+h);
7351
7352 // Now set the alpha... but because our current hardware
7353 // can't do alpha transformation on a non-opaque surface,
7354 // turn it off if we are running an animation that is also
7355 // transforming since it is more important to have that
7356 // animation be smooth.
7357 mShownAlpha = mAlpha;
7358 if (!mLimitedAlphaCompositing
7359 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7360 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7361 && x == frame.left && y == frame.top))) {
7362 //Log.i(TAG, "Applying alpha transform");
7363 if (selfTransformation) {
7364 mShownAlpha *= mTransformation.getAlpha();
7365 }
7366 if (attachedTransformation != null) {
7367 mShownAlpha *= attachedTransformation.getAlpha();
7368 }
7369 if (appTransformation != null) {
7370 mShownAlpha *= appTransformation.getAlpha();
7371 }
7372 } else {
7373 //Log.i(TAG, "Not applying alpha transform");
7374 }
Romain Guy06882f82009-06-10 13:36:04 -07007375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 if (localLOGV) Log.v(
7377 TAG, "Continuing animation in " + this +
7378 ": " + mShownFrame +
7379 ", alpha=" + mTransformation.getAlpha());
7380 return;
7381 }
Romain Guy06882f82009-06-10 13:36:04 -07007382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007384 if (mXOffset != 0 || mYOffset != 0) {
7385 mShownFrame.offset(mXOffset, mYOffset);
7386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 mShownAlpha = mAlpha;
7388 mDsDx = 1;
7389 mDtDx = 0;
7390 mDsDy = 0;
7391 mDtDy = 1;
7392 }
Romain Guy06882f82009-06-10 13:36:04 -07007393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007394 /**
7395 * Is this window visible? It is not visible if there is no
7396 * surface, or we are in the process of running an exit animation
7397 * that will remove the surface, or its app token has been hidden.
7398 */
7399 public boolean isVisibleLw() {
7400 final AppWindowToken atoken = mAppToken;
7401 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7402 && (atoken == null || !atoken.hiddenRequested)
7403 && !mExiting && !mDestroying;
7404 }
7405
7406 /**
7407 * Is this window visible, ignoring its app token? It is not visible
7408 * if there is no surface, or we are in the process of running an exit animation
7409 * that will remove the surface.
7410 */
7411 public boolean isWinVisibleLw() {
7412 final AppWindowToken atoken = mAppToken;
7413 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7414 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7415 && !mExiting && !mDestroying;
7416 }
7417
7418 /**
7419 * The same as isVisible(), but follows the current hidden state of
7420 * the associated app token, not the pending requested hidden state.
7421 */
7422 boolean isVisibleNow() {
7423 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007424 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 }
7426
7427 /**
7428 * Same as isVisible(), but we also count it as visible between the
7429 * call to IWindowSession.add() and the first relayout().
7430 */
7431 boolean isVisibleOrAdding() {
7432 final AppWindowToken atoken = mAppToken;
7433 return (mSurface != null
7434 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7435 && mPolicyVisibility && !mAttachedHidden
7436 && (atoken == null || !atoken.hiddenRequested)
7437 && !mExiting && !mDestroying;
7438 }
7439
7440 /**
7441 * Is this window currently on-screen? It is on-screen either if it
7442 * is visible or it is currently running an animation before no longer
7443 * being visible.
7444 */
7445 boolean isOnScreen() {
7446 final AppWindowToken atoken = mAppToken;
7447 if (atoken != null) {
7448 return mSurface != null && mPolicyVisibility && !mDestroying
7449 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007450 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451 } else {
7452 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007453 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 }
7455 }
Romain Guy06882f82009-06-10 13:36:04 -07007456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 /**
7458 * Like isOnScreen(), but we don't return true if the window is part
7459 * of a transition that has not yet been started.
7460 */
7461 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007462 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007463 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007464 return false;
7465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007467 final boolean animating = atoken != null
7468 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007470 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7471 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007472 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473 }
7474
7475 /** Is the window or its container currently animating? */
7476 boolean isAnimating() {
7477 final WindowState attached = mAttachedWindow;
7478 final AppWindowToken atoken = mAppToken;
7479 return mAnimation != null
7480 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007481 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 (atoken.animation != null
7483 || atoken.inPendingTransaction));
7484 }
7485
7486 /** Is this window currently animating? */
7487 boolean isWindowAnimating() {
7488 return mAnimation != null;
7489 }
7490
7491 /**
7492 * Like isOnScreen, but returns false if the surface hasn't yet
7493 * been drawn.
7494 */
7495 public boolean isDisplayedLw() {
7496 final AppWindowToken atoken = mAppToken;
7497 return mSurface != null && mPolicyVisibility && !mDestroying
7498 && !mDrawPending && !mCommitDrawPending
7499 && ((!mAttachedHidden &&
7500 (atoken == null || !atoken.hiddenRequested))
7501 || mAnimating);
7502 }
7503
7504 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7505 boolean shownFrame, boolean onlyOpaque) {
7506 if (mSurface == null) {
7507 return false;
7508 }
7509 if (mAppToken != null && !mAppToken.appFullscreen) {
7510 return false;
7511 }
7512 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7513 return false;
7514 }
7515 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007516
7517 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7518 return frame.left <= mCompatibleScreenFrame.left &&
7519 frame.top <= mCompatibleScreenFrame.top &&
7520 frame.right >= mCompatibleScreenFrame.right &&
7521 frame.bottom >= mCompatibleScreenFrame.bottom;
7522 } else {
7523 return frame.left <= 0 && frame.top <= 0
7524 && frame.right >= screenWidth
7525 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 }
Romain Guy06882f82009-06-10 13:36:04 -07007528
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007529 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007530 * Return true if the window is opaque and fully drawn. This indicates
7531 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007532 */
7533 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007534 return (mAttrs.format == PixelFormat.OPAQUE
7535 || mAttrs.type == TYPE_WALLPAPER)
7536 && mSurface != null && mAnimation == null
7537 && (mAppToken == null || mAppToken.animation == null)
7538 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007539 }
7540
7541 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7542 return
7543 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007544 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7545 // only if it's visible
7546 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007547 // and only if the application fills the compatible screen
7548 mFrame.left <= mCompatibleScreenFrame.left &&
7549 mFrame.top <= mCompatibleScreenFrame.top &&
7550 mFrame.right >= mCompatibleScreenFrame.right &&
7551 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007552 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007553 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007554 }
7555
7556 boolean isFullscreen(int screenWidth, int screenHeight) {
7557 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007558 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 }
7560
7561 void removeLocked() {
7562 if (mAttachedWindow != null) {
7563 mAttachedWindow.mChildWindows.remove(this);
7564 }
7565 destroySurfaceLocked();
7566 mSession.windowRemovedLocked();
7567 try {
7568 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7569 } catch (RuntimeException e) {
7570 // Ignore if it has already been removed (usually because
7571 // we are doing this as part of processing a death note.)
7572 }
7573 }
7574
7575 private class DeathRecipient implements IBinder.DeathRecipient {
7576 public void binderDied() {
7577 try {
7578 synchronized(mWindowMap) {
7579 WindowState win = windowForClientLocked(mSession, mClient);
7580 Log.i(TAG, "WIN DEATH: " + win);
7581 if (win != null) {
7582 removeWindowLocked(mSession, win);
7583 }
7584 }
7585 } catch (IllegalArgumentException ex) {
7586 // This will happen if the window has already been
7587 // removed.
7588 }
7589 }
7590 }
7591
7592 /** Returns true if this window desires key events. */
7593 public final boolean canReceiveKeys() {
7594 return isVisibleOrAdding()
7595 && (mViewVisibility == View.VISIBLE)
7596 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7597 }
7598
7599 public boolean hasDrawnLw() {
7600 return mHasDrawn;
7601 }
7602
7603 public boolean showLw(boolean doAnimation) {
7604 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7605 mPolicyVisibility = true;
7606 mPolicyVisibilityAfterAnim = true;
7607 if (doAnimation) {
7608 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7609 }
7610 requestAnimationLocked(0);
7611 return true;
7612 }
7613 return false;
7614 }
7615
7616 public boolean hideLw(boolean doAnimation) {
7617 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7618 : mPolicyVisibility;
7619 if (current) {
7620 if (doAnimation) {
7621 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7622 if (mAnimation == null) {
7623 doAnimation = false;
7624 }
7625 }
7626 if (doAnimation) {
7627 mPolicyVisibilityAfterAnim = false;
7628 } else {
7629 mPolicyVisibilityAfterAnim = false;
7630 mPolicyVisibility = false;
7631 }
7632 requestAnimationLocked(0);
7633 return true;
7634 }
7635 return false;
7636 }
7637
7638 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007639 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007640
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007641 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7642 pw.print(" mClient="); pw.println(mClient.asBinder());
7643 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7644 if (mAttachedWindow != null || mLayoutAttached) {
7645 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7646 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7647 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007648 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7649 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7650 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007651 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7652 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007653 }
7654 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7655 pw.print(" mSubLayer="); pw.print(mSubLayer);
7656 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7657 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7658 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7659 pw.print("="); pw.print(mAnimLayer);
7660 pw.print(" mLastLayer="); pw.println(mLastLayer);
7661 if (mSurface != null) {
7662 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7663 }
7664 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7665 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7666 if (mAppToken != null) {
7667 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7668 }
7669 if (mTargetAppToken != null) {
7670 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7671 }
7672 pw.print(prefix); pw.print("mViewVisibility=0x");
7673 pw.print(Integer.toHexString(mViewVisibility));
7674 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007675 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7676 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007677 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7678 pw.print(prefix); pw.print("mPolicyVisibility=");
7679 pw.print(mPolicyVisibility);
7680 pw.print(" mPolicyVisibilityAfterAnim=");
7681 pw.print(mPolicyVisibilityAfterAnim);
7682 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7683 }
7684 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007685 pw.print(" h="); pw.println(mRequestedHeight);
7686 if (mXOffset != 0 || mYOffset != 0) {
7687 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7688 pw.print(" y="); pw.println(mYOffset);
7689 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007690 pw.print(prefix); pw.print("mGivenContentInsets=");
7691 mGivenContentInsets.printShortString(pw);
7692 pw.print(" mGivenVisibleInsets=");
7693 mGivenVisibleInsets.printShortString(pw);
7694 pw.println();
7695 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7696 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7697 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7698 }
7699 pw.print(prefix); pw.print("mShownFrame=");
7700 mShownFrame.printShortString(pw);
7701 pw.print(" last="); mLastShownFrame.printShortString(pw);
7702 pw.println();
7703 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7704 pw.print(" last="); mLastFrame.printShortString(pw);
7705 pw.println();
7706 pw.print(prefix); pw.print("mContainingFrame=");
7707 mContainingFrame.printShortString(pw);
7708 pw.print(" mDisplayFrame=");
7709 mDisplayFrame.printShortString(pw);
7710 pw.println();
7711 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7712 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7713 pw.println();
7714 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7715 pw.print(" last="); mLastContentInsets.printShortString(pw);
7716 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7717 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7718 pw.println();
7719 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7720 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7721 pw.print(" mAlpha="); pw.print(mAlpha);
7722 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7723 }
7724 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7725 || mAnimation != null) {
7726 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7727 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7728 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7729 pw.print(" mAnimation="); pw.println(mAnimation);
7730 }
7731 if (mHasTransformation || mHasLocalTransformation) {
7732 pw.print(prefix); pw.print("XForm: has=");
7733 pw.print(mHasTransformation);
7734 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7735 pw.print(" "); mTransformation.printShortString(pw);
7736 pw.println();
7737 }
7738 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7739 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7740 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7741 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7742 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7743 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7744 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7745 pw.print(" mDestroying="); pw.print(mDestroying);
7746 pw.print(" mRemoved="); pw.println(mRemoved);
7747 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007748 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007749 pw.print(prefix); pw.print("mOrientationChanging=");
7750 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007751 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7752 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007753 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007754 if (mHScale != 1 || mVScale != 1) {
7755 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7756 pw.print(" mVScale="); pw.println(mVScale);
7757 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007758 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007759 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7760 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 }
7763
7764 @Override
7765 public String toString() {
7766 return "Window{"
7767 + Integer.toHexString(System.identityHashCode(this))
7768 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7769 }
7770 }
Romain Guy06882f82009-06-10 13:36:04 -07007771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 // -------------------------------------------------------------
7773 // Window Token State
7774 // -------------------------------------------------------------
7775
7776 class WindowToken {
7777 // The actual token.
7778 final IBinder token;
7779
7780 // The type of window this token is for, as per WindowManager.LayoutParams.
7781 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 // Set if this token was explicitly added by a client, so should
7784 // not be removed when all windows are removed.
7785 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007786
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007787 // For printing.
7788 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007790 // If this is an AppWindowToken, this is non-null.
7791 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 // All of the windows associated with this token.
7794 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7795
7796 // Is key dispatching paused for this token?
7797 boolean paused = false;
7798
7799 // Should this token's windows be hidden?
7800 boolean hidden;
7801
7802 // Temporary for finding which tokens no longer have visible windows.
7803 boolean hasVisible;
7804
Dianne Hackborna8f60182009-09-01 19:01:50 -07007805 // Set to true when this token is in a pending transaction where it
7806 // will be shown.
7807 boolean waitingToShow;
7808
7809 // Set to true when this token is in a pending transaction where it
7810 // will be hidden.
7811 boolean waitingToHide;
7812
7813 // Set to true when this token is in a pending transaction where its
7814 // windows will be put to the bottom of the list.
7815 boolean sendingToBottom;
7816
7817 // Set to true when this token is in a pending transaction where its
7818 // windows will be put to the top of the list.
7819 boolean sendingToTop;
7820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 WindowToken(IBinder _token, int type, boolean _explicit) {
7822 token = _token;
7823 windowType = type;
7824 explicit = _explicit;
7825 }
7826
7827 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007828 pw.print(prefix); pw.print("token="); pw.println(token);
7829 pw.print(prefix); pw.print("windows="); pw.println(windows);
7830 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7831 pw.print(" hidden="); pw.print(hidden);
7832 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007833 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7834 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7835 pw.print(" waitingToHide="); pw.print(waitingToHide);
7836 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7837 pw.print(" sendingToTop="); pw.println(sendingToTop);
7838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 }
7840
7841 @Override
7842 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007843 if (stringName == null) {
7844 StringBuilder sb = new StringBuilder();
7845 sb.append("WindowToken{");
7846 sb.append(Integer.toHexString(System.identityHashCode(this)));
7847 sb.append(" token="); sb.append(token); sb.append('}');
7848 stringName = sb.toString();
7849 }
7850 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 }
7852 };
7853
7854 class AppWindowToken extends WindowToken {
7855 // Non-null only for application tokens.
7856 final IApplicationToken appToken;
7857
7858 // All of the windows and child windows that are included in this
7859 // application token. Note this list is NOT sorted!
7860 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7861
7862 int groupId = -1;
7863 boolean appFullscreen;
7864 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 // These are used for determining when all windows associated with
7867 // an activity have been drawn, so they can be made visible together
7868 // at the same time.
7869 int lastTransactionSequence = mTransactionSequence-1;
7870 int numInterestingWindows;
7871 int numDrawnWindows;
7872 boolean inPendingTransaction;
7873 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007875 // Is this token going to be hidden in a little while? If so, it
7876 // won't be taken into account for setting the screen orientation.
7877 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 // Is this window's surface needed? This is almost like hidden, except
7880 // it will sometimes be true a little earlier: when the token has
7881 // been shown, but is still waiting for its app transition to execute
7882 // before making its windows shown.
7883 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 // Have we told the window clients to hide themselves?
7886 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 // Last visibility state we reported to the app token.
7889 boolean reportedVisible;
7890
7891 // Set to true when the token has been removed from the window mgr.
7892 boolean removed;
7893
7894 // Have we been asked to have this token keep the screen frozen?
7895 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 boolean animating;
7898 Animation animation;
7899 boolean hasTransformation;
7900 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 // Offset to the window of all layers in the token, for use by
7903 // AppWindowToken animations.
7904 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007906 // Information about an application starting window if displayed.
7907 StartingData startingData;
7908 WindowState startingWindow;
7909 View startingView;
7910 boolean startingDisplayed;
7911 boolean startingMoved;
7912 boolean firstWindowDrawn;
7913
7914 AppWindowToken(IApplicationToken _token) {
7915 super(_token.asBinder(),
7916 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7917 appWindowToken = this;
7918 appToken = _token;
7919 }
Romain Guy06882f82009-06-10 13:36:04 -07007920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 public void setAnimation(Animation anim) {
7922 if (localLOGV) Log.v(
7923 TAG, "Setting animation in " + this + ": " + anim);
7924 animation = anim;
7925 animating = false;
7926 anim.restrictDuration(MAX_ANIMATION_DURATION);
7927 anim.scaleCurrentDuration(mTransitionAnimationScale);
7928 int zorder = anim.getZAdjustment();
7929 int adj = 0;
7930 if (zorder == Animation.ZORDER_TOP) {
7931 adj = TYPE_LAYER_OFFSET;
7932 } else if (zorder == Animation.ZORDER_BOTTOM) {
7933 adj = -TYPE_LAYER_OFFSET;
7934 }
Romain Guy06882f82009-06-10 13:36:04 -07007935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936 if (animLayerAdjustment != adj) {
7937 animLayerAdjustment = adj;
7938 updateLayers();
7939 }
7940 }
Romain Guy06882f82009-06-10 13:36:04 -07007941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007942 public void setDummyAnimation() {
7943 if (animation == null) {
7944 if (localLOGV) Log.v(
7945 TAG, "Setting dummy animation in " + this);
7946 animation = sDummyAnimation;
7947 }
7948 }
7949
7950 public void clearAnimation() {
7951 if (animation != null) {
7952 animation = null;
7953 animating = true;
7954 }
7955 }
Romain Guy06882f82009-06-10 13:36:04 -07007956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 void updateLayers() {
7958 final int N = allAppWindows.size();
7959 final int adj = animLayerAdjustment;
7960 for (int i=0; i<N; i++) {
7961 WindowState w = allAppWindows.get(i);
7962 w.mAnimLayer = w.mLayer + adj;
7963 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7964 + w.mAnimLayer);
7965 if (w == mInputMethodTarget) {
7966 setInputMethodAnimLayerAdjustment(adj);
7967 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007968 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007969 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 }
7972 }
Romain Guy06882f82009-06-10 13:36:04 -07007973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 void sendAppVisibilityToClients() {
7975 final int N = allAppWindows.size();
7976 for (int i=0; i<N; i++) {
7977 WindowState win = allAppWindows.get(i);
7978 if (win == startingWindow && clientHidden) {
7979 // Don't hide the starting window.
7980 continue;
7981 }
7982 try {
7983 if (DEBUG_VISIBILITY) Log.v(TAG,
7984 "Setting visibility of " + win + ": " + (!clientHidden));
7985 win.mClient.dispatchAppVisibility(!clientHidden);
7986 } catch (RemoteException e) {
7987 }
7988 }
7989 }
Romain Guy06882f82009-06-10 13:36:04 -07007990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 void showAllWindowsLocked() {
7992 final int NW = allAppWindows.size();
7993 for (int i=0; i<NW; i++) {
7994 WindowState w = allAppWindows.get(i);
7995 if (DEBUG_VISIBILITY) Log.v(TAG,
7996 "performing show on: " + w);
7997 w.performShowLocked();
7998 }
7999 }
Romain Guy06882f82009-06-10 13:36:04 -07008000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008001 // This must be called while inside a transaction.
8002 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8003 if (!mDisplayFrozen) {
8004 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 if (animation == sDummyAnimation) {
8007 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008008 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008009 // when it is really time to animate, this will be set to
8010 // a real animation and the next call will execute normally.
8011 return false;
8012 }
Romain Guy06882f82009-06-10 13:36:04 -07008013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8015 if (!animating) {
8016 if (DEBUG_ANIM) Log.v(
8017 TAG, "Starting animation in " + this +
8018 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8019 + " scale=" + mTransitionAnimationScale
8020 + " allDrawn=" + allDrawn + " animating=" + animating);
8021 animation.initialize(dw, dh, dw, dh);
8022 animation.setStartTime(currentTime);
8023 animating = true;
8024 }
8025 transformation.clear();
8026 final boolean more = animation.getTransformation(
8027 currentTime, transformation);
8028 if (DEBUG_ANIM) Log.v(
8029 TAG, "Stepped animation in " + this +
8030 ": more=" + more + ", xform=" + transformation);
8031 if (more) {
8032 // we're done!
8033 hasTransformation = true;
8034 return true;
8035 }
8036 if (DEBUG_ANIM) Log.v(
8037 TAG, "Finished animation in " + this +
8038 " @ " + currentTime);
8039 animation = null;
8040 }
8041 } else if (animation != null) {
8042 // If the display is frozen, and there is a pending animation,
8043 // clear it and make sure we run the cleanup code.
8044 animating = true;
8045 animation = null;
8046 }
8047
8048 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 if (!animating) {
8051 return false;
8052 }
8053
8054 clearAnimation();
8055 animating = false;
8056 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8057 moveInputMethodWindowsIfNeededLocked(true);
8058 }
Romain Guy06882f82009-06-10 13:36:04 -07008059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 if (DEBUG_ANIM) Log.v(
8061 TAG, "Animation done in " + this
8062 + ": reportedVisible=" + reportedVisible);
8063
8064 transformation.clear();
8065 if (animLayerAdjustment != 0) {
8066 animLayerAdjustment = 0;
8067 updateLayers();
8068 }
Romain Guy06882f82009-06-10 13:36:04 -07008069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008070 final int N = windows.size();
8071 for (int i=0; i<N; i++) {
8072 ((WindowState)windows.get(i)).finishExit();
8073 }
8074 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008076 return false;
8077 }
8078
8079 void updateReportedVisibilityLocked() {
8080 if (appToken == null) {
8081 return;
8082 }
Romain Guy06882f82009-06-10 13:36:04 -07008083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 int numInteresting = 0;
8085 int numVisible = 0;
8086 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8089 final int N = allAppWindows.size();
8090 for (int i=0; i<N; i++) {
8091 WindowState win = allAppWindows.get(i);
8092 if (win == startingWindow || win.mAppFreezing) {
8093 continue;
8094 }
8095 if (DEBUG_VISIBILITY) {
8096 Log.v(TAG, "Win " + win + ": isDisplayed="
8097 + win.isDisplayedLw()
8098 + ", isAnimating=" + win.isAnimating());
8099 if (!win.isDisplayedLw()) {
8100 Log.v(TAG, "Not displayed: s=" + win.mSurface
8101 + " pv=" + win.mPolicyVisibility
8102 + " dp=" + win.mDrawPending
8103 + " cdp=" + win.mCommitDrawPending
8104 + " ah=" + win.mAttachedHidden
8105 + " th="
8106 + (win.mAppToken != null
8107 ? win.mAppToken.hiddenRequested : false)
8108 + " a=" + win.mAnimating);
8109 }
8110 }
8111 numInteresting++;
8112 if (win.isDisplayedLw()) {
8113 if (!win.isAnimating()) {
8114 numVisible++;
8115 }
8116 nowGone = false;
8117 } else if (win.isAnimating()) {
8118 nowGone = false;
8119 }
8120 }
Romain Guy06882f82009-06-10 13:36:04 -07008121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8123 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8124 + numInteresting + " visible=" + numVisible);
8125 if (nowVisible != reportedVisible) {
8126 if (DEBUG_VISIBILITY) Log.v(
8127 TAG, "Visibility changed in " + this
8128 + ": vis=" + nowVisible);
8129 reportedVisible = nowVisible;
8130 Message m = mH.obtainMessage(
8131 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8132 nowVisible ? 1 : 0,
8133 nowGone ? 1 : 0,
8134 this);
8135 mH.sendMessage(m);
8136 }
8137 }
Romain Guy06882f82009-06-10 13:36:04 -07008138
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008139 WindowState findMainWindow() {
8140 int j = windows.size();
8141 while (j > 0) {
8142 j--;
8143 WindowState win = windows.get(j);
8144 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8145 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8146 return win;
8147 }
8148 }
8149 return null;
8150 }
8151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 void dump(PrintWriter pw, String prefix) {
8153 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008154 if (appToken != null) {
8155 pw.print(prefix); pw.println("app=true");
8156 }
8157 if (allAppWindows.size() > 0) {
8158 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8159 }
8160 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008161 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008162 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8163 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8164 pw.print(" clientHidden="); pw.print(clientHidden);
8165 pw.print(" willBeHidden="); pw.print(willBeHidden);
8166 pw.print(" reportedVisible="); pw.println(reportedVisible);
8167 if (paused || freezingScreen) {
8168 pw.print(prefix); pw.print("paused="); pw.print(paused);
8169 pw.print(" freezingScreen="); pw.println(freezingScreen);
8170 }
8171 if (numInterestingWindows != 0 || numDrawnWindows != 0
8172 || inPendingTransaction || allDrawn) {
8173 pw.print(prefix); pw.print("numInterestingWindows=");
8174 pw.print(numInterestingWindows);
8175 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8176 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8177 pw.print(" allDrawn="); pw.println(allDrawn);
8178 }
8179 if (animating || animation != null) {
8180 pw.print(prefix); pw.print("animating="); pw.print(animating);
8181 pw.print(" animation="); pw.println(animation);
8182 }
8183 if (animLayerAdjustment != 0) {
8184 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8185 }
8186 if (hasTransformation) {
8187 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8188 pw.print(" transformation="); transformation.printShortString(pw);
8189 pw.println();
8190 }
8191 if (startingData != null || removed || firstWindowDrawn) {
8192 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8193 pw.print(" removed="); pw.print(removed);
8194 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8195 }
8196 if (startingWindow != null || startingView != null
8197 || startingDisplayed || startingMoved) {
8198 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8199 pw.print(" startingView="); pw.print(startingView);
8200 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8201 pw.print(" startingMoved"); pw.println(startingMoved);
8202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008203 }
8204
8205 @Override
8206 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008207 if (stringName == null) {
8208 StringBuilder sb = new StringBuilder();
8209 sb.append("AppWindowToken{");
8210 sb.append(Integer.toHexString(System.identityHashCode(this)));
8211 sb.append(" token="); sb.append(token); sb.append('}');
8212 stringName = sb.toString();
8213 }
8214 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 }
8216 }
Romain Guy06882f82009-06-10 13:36:04 -07008217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008218 // -------------------------------------------------------------
8219 // DummyAnimation
8220 // -------------------------------------------------------------
8221
8222 // This is an animation that does nothing: it just immediately finishes
8223 // itself every time it is called. It is used as a stub animation in cases
8224 // where we want to synchronize multiple things that may be animating.
8225 static final class DummyAnimation extends Animation {
8226 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8227 return false;
8228 }
8229 }
8230 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008232 // -------------------------------------------------------------
8233 // Async Handler
8234 // -------------------------------------------------------------
8235
8236 static final class StartingData {
8237 final String pkg;
8238 final int theme;
8239 final CharSequence nonLocalizedLabel;
8240 final int labelRes;
8241 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8244 int _labelRes, int _icon) {
8245 pkg = _pkg;
8246 theme = _theme;
8247 nonLocalizedLabel = _nonLocalizedLabel;
8248 labelRes = _labelRes;
8249 icon = _icon;
8250 }
8251 }
8252
8253 private final class H extends Handler {
8254 public static final int REPORT_FOCUS_CHANGE = 2;
8255 public static final int REPORT_LOSING_FOCUS = 3;
8256 public static final int ANIMATE = 4;
8257 public static final int ADD_STARTING = 5;
8258 public static final int REMOVE_STARTING = 6;
8259 public static final int FINISHED_STARTING = 7;
8260 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8262 public static final int HOLD_SCREEN_CHANGED = 12;
8263 public static final int APP_TRANSITION_TIMEOUT = 13;
8264 public static final int PERSIST_ANIMATION_SCALE = 14;
8265 public static final int FORCE_GC = 15;
8266 public static final int ENABLE_SCREEN = 16;
8267 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008268 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 public H() {
8273 }
Romain Guy06882f82009-06-10 13:36:04 -07008274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 @Override
8276 public void handleMessage(Message msg) {
8277 switch (msg.what) {
8278 case REPORT_FOCUS_CHANGE: {
8279 WindowState lastFocus;
8280 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 synchronized(mWindowMap) {
8283 lastFocus = mLastFocus;
8284 newFocus = mCurrentFocus;
8285 if (lastFocus == newFocus) {
8286 // Focus is not changing, so nothing to do.
8287 return;
8288 }
8289 mLastFocus = newFocus;
8290 //Log.i(TAG, "Focus moving from " + lastFocus
8291 // + " to " + newFocus);
8292 if (newFocus != null && lastFocus != null
8293 && !newFocus.isDisplayedLw()) {
8294 //Log.i(TAG, "Delaying loss of focus...");
8295 mLosingFocus.add(lastFocus);
8296 lastFocus = null;
8297 }
8298 }
8299
8300 if (lastFocus != newFocus) {
8301 //System.out.println("Changing focus from " + lastFocus
8302 // + " to " + newFocus);
8303 if (newFocus != null) {
8304 try {
8305 //Log.i(TAG, "Gaining focus: " + newFocus);
8306 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8307 } catch (RemoteException e) {
8308 // Ignore if process has died.
8309 }
8310 }
8311
8312 if (lastFocus != null) {
8313 try {
8314 //Log.i(TAG, "Losing focus: " + lastFocus);
8315 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8316 } catch (RemoteException e) {
8317 // Ignore if process has died.
8318 }
8319 }
8320 }
8321 } break;
8322
8323 case REPORT_LOSING_FOCUS: {
8324 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008326 synchronized(mWindowMap) {
8327 losers = mLosingFocus;
8328 mLosingFocus = new ArrayList<WindowState>();
8329 }
8330
8331 final int N = losers.size();
8332 for (int i=0; i<N; i++) {
8333 try {
8334 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8335 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8336 } catch (RemoteException e) {
8337 // Ignore if process has died.
8338 }
8339 }
8340 } break;
8341
8342 case ANIMATE: {
8343 synchronized(mWindowMap) {
8344 mAnimationPending = false;
8345 performLayoutAndPlaceSurfacesLocked();
8346 }
8347 } break;
8348
8349 case ADD_STARTING: {
8350 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8351 final StartingData sd = wtoken.startingData;
8352
8353 if (sd == null) {
8354 // Animation has been canceled... do nothing.
8355 return;
8356 }
Romain Guy06882f82009-06-10 13:36:04 -07008357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8359 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 View view = null;
8362 try {
8363 view = mPolicy.addStartingWindow(
8364 wtoken.token, sd.pkg,
8365 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8366 sd.icon);
8367 } catch (Exception e) {
8368 Log.w(TAG, "Exception when adding starting window", e);
8369 }
8370
8371 if (view != null) {
8372 boolean abort = false;
8373
8374 synchronized(mWindowMap) {
8375 if (wtoken.removed || wtoken.startingData == null) {
8376 // If the window was successfully added, then
8377 // we need to remove it.
8378 if (wtoken.startingWindow != null) {
8379 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8380 "Aborted starting " + wtoken
8381 + ": removed=" + wtoken.removed
8382 + " startingData=" + wtoken.startingData);
8383 wtoken.startingWindow = null;
8384 wtoken.startingData = null;
8385 abort = true;
8386 }
8387 } else {
8388 wtoken.startingView = view;
8389 }
8390 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8391 "Added starting " + wtoken
8392 + ": startingWindow="
8393 + wtoken.startingWindow + " startingView="
8394 + wtoken.startingView);
8395 }
8396
8397 if (abort) {
8398 try {
8399 mPolicy.removeStartingWindow(wtoken.token, view);
8400 } catch (Exception e) {
8401 Log.w(TAG, "Exception when removing starting window", e);
8402 }
8403 }
8404 }
8405 } break;
8406
8407 case REMOVE_STARTING: {
8408 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8409 IBinder token = null;
8410 View view = null;
8411 synchronized (mWindowMap) {
8412 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8413 + wtoken + ": startingWindow="
8414 + wtoken.startingWindow + " startingView="
8415 + wtoken.startingView);
8416 if (wtoken.startingWindow != null) {
8417 view = wtoken.startingView;
8418 token = wtoken.token;
8419 wtoken.startingData = null;
8420 wtoken.startingView = null;
8421 wtoken.startingWindow = null;
8422 }
8423 }
8424 if (view != null) {
8425 try {
8426 mPolicy.removeStartingWindow(token, view);
8427 } catch (Exception e) {
8428 Log.w(TAG, "Exception when removing starting window", e);
8429 }
8430 }
8431 } break;
8432
8433 case FINISHED_STARTING: {
8434 IBinder token = null;
8435 View view = null;
8436 while (true) {
8437 synchronized (mWindowMap) {
8438 final int N = mFinishedStarting.size();
8439 if (N <= 0) {
8440 break;
8441 }
8442 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8443
8444 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8445 "Finished starting " + wtoken
8446 + ": startingWindow=" + wtoken.startingWindow
8447 + " startingView=" + wtoken.startingView);
8448
8449 if (wtoken.startingWindow == null) {
8450 continue;
8451 }
8452
8453 view = wtoken.startingView;
8454 token = wtoken.token;
8455 wtoken.startingData = null;
8456 wtoken.startingView = null;
8457 wtoken.startingWindow = null;
8458 }
8459
8460 try {
8461 mPolicy.removeStartingWindow(token, view);
8462 } catch (Exception e) {
8463 Log.w(TAG, "Exception when removing starting window", e);
8464 }
8465 }
8466 } break;
8467
8468 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8469 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8470
8471 boolean nowVisible = msg.arg1 != 0;
8472 boolean nowGone = msg.arg2 != 0;
8473
8474 try {
8475 if (DEBUG_VISIBILITY) Log.v(
8476 TAG, "Reporting visible in " + wtoken
8477 + " visible=" + nowVisible
8478 + " gone=" + nowGone);
8479 if (nowVisible) {
8480 wtoken.appToken.windowsVisible();
8481 } else {
8482 wtoken.appToken.windowsGone();
8483 }
8484 } catch (RemoteException ex) {
8485 }
8486 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 case WINDOW_FREEZE_TIMEOUT: {
8489 synchronized (mWindowMap) {
8490 Log.w(TAG, "Window freeze timeout expired.");
8491 int i = mWindows.size();
8492 while (i > 0) {
8493 i--;
8494 WindowState w = (WindowState)mWindows.get(i);
8495 if (w.mOrientationChanging) {
8496 w.mOrientationChanging = false;
8497 Log.w(TAG, "Force clearing orientation change: " + w);
8498 }
8499 }
8500 performLayoutAndPlaceSurfacesLocked();
8501 }
8502 break;
8503 }
Romain Guy06882f82009-06-10 13:36:04 -07008504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 case HOLD_SCREEN_CHANGED: {
8506 Session oldHold;
8507 Session newHold;
8508 synchronized (mWindowMap) {
8509 oldHold = mLastReportedHold;
8510 newHold = (Session)msg.obj;
8511 mLastReportedHold = newHold;
8512 }
Romain Guy06882f82009-06-10 13:36:04 -07008513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 if (oldHold != newHold) {
8515 try {
8516 if (oldHold != null) {
8517 mBatteryStats.noteStopWakelock(oldHold.mUid,
8518 "window",
8519 BatteryStats.WAKE_TYPE_WINDOW);
8520 }
8521 if (newHold != null) {
8522 mBatteryStats.noteStartWakelock(newHold.mUid,
8523 "window",
8524 BatteryStats.WAKE_TYPE_WINDOW);
8525 }
8526 } catch (RemoteException e) {
8527 }
8528 }
8529 break;
8530 }
Romain Guy06882f82009-06-10 13:36:04 -07008531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008532 case APP_TRANSITION_TIMEOUT: {
8533 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008534 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8536 "*** APP TRANSITION TIMEOUT");
8537 mAppTransitionReady = true;
8538 mAppTransitionTimeout = true;
8539 performLayoutAndPlaceSurfacesLocked();
8540 }
8541 }
8542 break;
8543 }
Romain Guy06882f82009-06-10 13:36:04 -07008544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008545 case PERSIST_ANIMATION_SCALE: {
8546 Settings.System.putFloat(mContext.getContentResolver(),
8547 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8548 Settings.System.putFloat(mContext.getContentResolver(),
8549 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8550 break;
8551 }
Romain Guy06882f82009-06-10 13:36:04 -07008552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 case FORCE_GC: {
8554 synchronized(mWindowMap) {
8555 if (mAnimationPending) {
8556 // If we are animating, don't do the gc now but
8557 // delay a bit so we don't interrupt the animation.
8558 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8559 2000);
8560 return;
8561 }
8562 // If we are currently rotating the display, it will
8563 // schedule a new message when done.
8564 if (mDisplayFrozen) {
8565 return;
8566 }
8567 mFreezeGcPending = 0;
8568 }
8569 Runtime.getRuntime().gc();
8570 break;
8571 }
Romain Guy06882f82009-06-10 13:36:04 -07008572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 case ENABLE_SCREEN: {
8574 performEnableScreen();
8575 break;
8576 }
Romain Guy06882f82009-06-10 13:36:04 -07008577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008578 case APP_FREEZE_TIMEOUT: {
8579 synchronized (mWindowMap) {
8580 Log.w(TAG, "App freeze timeout expired.");
8581 int i = mAppTokens.size();
8582 while (i > 0) {
8583 i--;
8584 AppWindowToken tok = mAppTokens.get(i);
8585 if (tok.freezingScreen) {
8586 Log.w(TAG, "Force clearing freeze: " + tok);
8587 unsetAppFreezingScreenLocked(tok, true, true);
8588 }
8589 }
8590 }
8591 break;
8592 }
Romain Guy06882f82009-06-10 13:36:04 -07008593
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008594 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008595 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008596 sendNewConfiguration();
8597 }
8598 break;
8599 }
Romain Guy06882f82009-06-10 13:36:04 -07008600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 }
8602 }
8603 }
8604
8605 // -------------------------------------------------------------
8606 // IWindowManager API
8607 // -------------------------------------------------------------
8608
8609 public IWindowSession openSession(IInputMethodClient client,
8610 IInputContext inputContext) {
8611 if (client == null) throw new IllegalArgumentException("null client");
8612 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8613 return new Session(client, inputContext);
8614 }
8615
8616 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8617 synchronized (mWindowMap) {
8618 // The focus for the client is the window immediately below
8619 // where we would place the input method window.
8620 int idx = findDesiredInputMethodWindowIndexLocked(false);
8621 WindowState imFocus;
8622 if (idx > 0) {
8623 imFocus = (WindowState)mWindows.get(idx-1);
8624 if (imFocus != null) {
8625 if (imFocus.mSession.mClient != null &&
8626 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8627 return true;
8628 }
8629 }
8630 }
8631 }
8632 return false;
8633 }
Romain Guy06882f82009-06-10 13:36:04 -07008634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 // -------------------------------------------------------------
8636 // Internals
8637 // -------------------------------------------------------------
8638
8639 final WindowState windowForClientLocked(Session session, IWindow client) {
8640 return windowForClientLocked(session, client.asBinder());
8641 }
Romain Guy06882f82009-06-10 13:36:04 -07008642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 final WindowState windowForClientLocked(Session session, IBinder client) {
8644 WindowState win = mWindowMap.get(client);
8645 if (localLOGV) Log.v(
8646 TAG, "Looking up client " + client + ": " + win);
8647 if (win == null) {
8648 RuntimeException ex = new RuntimeException();
8649 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8650 return null;
8651 }
8652 if (session != null && win.mSession != session) {
8653 RuntimeException ex = new RuntimeException();
8654 Log.w(TAG, "Requested window " + client + " is in session " +
8655 win.mSession + ", not " + session, ex);
8656 return null;
8657 }
8658
8659 return win;
8660 }
8661
Dianne Hackborna8f60182009-09-01 19:01:50 -07008662 final void rebuildAppWindowListLocked() {
8663 int NW = mWindows.size();
8664 int i;
8665
8666 // First remove all existing app windows.
8667 i=0;
8668 while (i < NW) {
8669 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008670 WindowState win = (WindowState)mWindows.remove(i);
8671 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8672 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008673 NW--;
8674 continue;
8675 }
8676 i++;
8677 }
8678
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008679 // First add all of the exiting app tokens... these are no longer
8680 // in the main app list, but still have windows shown. We put them
8681 // in the back because now that the animation is over we no longer
8682 // will care about them.
8683 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008684 i = 0;
8685 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008686 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8687 }
8688
8689 // And add in the still active app tokens in Z order.
8690 NT = mAppTokens.size();
8691 for (int j=0; j<NT; j++) {
8692 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008693 }
8694 }
8695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 private final void assignLayersLocked() {
8697 int N = mWindows.size();
8698 int curBaseLayer = 0;
8699 int curLayer = 0;
8700 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 for (i=0; i<N; i++) {
8703 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008704 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8705 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008706 curLayer += WINDOW_LAYER_MULTIPLIER;
8707 w.mLayer = curLayer;
8708 } else {
8709 curBaseLayer = curLayer = w.mBaseLayer;
8710 w.mLayer = curLayer;
8711 }
8712 if (w.mTargetAppToken != null) {
8713 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8714 } else if (w.mAppToken != null) {
8715 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8716 } else {
8717 w.mAnimLayer = w.mLayer;
8718 }
8719 if (w.mIsImWindow) {
8720 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008721 } else if (w.mIsWallpaper) {
8722 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008723 }
8724 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8725 + w.mAnimLayer);
8726 //System.out.println(
8727 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8728 }
8729 }
8730
8731 private boolean mInLayout = false;
8732 private final void performLayoutAndPlaceSurfacesLocked() {
8733 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008734 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008735 throw new RuntimeException("Recursive call!");
8736 }
8737 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8738 return;
8739 }
8740
8741 boolean recoveringMemory = false;
8742 if (mForceRemoves != null) {
8743 recoveringMemory = true;
8744 // Wait a little it for things to settle down, and off we go.
8745 for (int i=0; i<mForceRemoves.size(); i++) {
8746 WindowState ws = mForceRemoves.get(i);
8747 Log.i(TAG, "Force removing: " + ws);
8748 removeWindowInnerLocked(ws.mSession, ws);
8749 }
8750 mForceRemoves = null;
8751 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8752 Object tmp = new Object();
8753 synchronized (tmp) {
8754 try {
8755 tmp.wait(250);
8756 } catch (InterruptedException e) {
8757 }
8758 }
8759 }
Romain Guy06882f82009-06-10 13:36:04 -07008760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008761 mInLayout = true;
8762 try {
8763 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 int i = mPendingRemove.size()-1;
8766 if (i >= 0) {
8767 while (i >= 0) {
8768 WindowState w = mPendingRemove.get(i);
8769 removeWindowInnerLocked(w.mSession, w);
8770 i--;
8771 }
8772 mPendingRemove.clear();
8773
8774 mInLayout = false;
8775 assignLayersLocked();
8776 mLayoutNeeded = true;
8777 performLayoutAndPlaceSurfacesLocked();
8778
8779 } else {
8780 mInLayout = false;
8781 if (mLayoutNeeded) {
8782 requestAnimationLocked(0);
8783 }
8784 }
8785 } catch (RuntimeException e) {
8786 mInLayout = false;
8787 Log.e(TAG, "Unhandled exception while layout out windows", e);
8788 }
8789 }
8790
8791 private final void performLayoutLockedInner() {
8792 final int dw = mDisplay.getWidth();
8793 final int dh = mDisplay.getHeight();
8794
8795 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008796 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 int i;
8798
8799 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008800
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008801 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008802 mPolicy.beginLayoutLw(dw, dh);
8803
8804 // First perform layout of any root windows (not attached
8805 // to another window).
8806 int topAttached = -1;
8807 for (i = N-1; i >= 0; i--) {
8808 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008809
8810 // Don't do layout of a window if it is not visible, or
8811 // soon won't be visible, to avoid wasting time and funky
8812 // changes while a window is animating away.
8813 final AppWindowToken atoken = win.mAppToken;
8814 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008815 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008816 || win.mRootToken.hidden
8817 || (atoken != null && atoken.hiddenRequested)
8818 || !win.mPolicyVisibility
8819 || win.mAttachedHidden
8820 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821
8822 // If this view is GONE, then skip it -- keep the current
8823 // frame, and let the caller know so they can ignore it
8824 // if they want. (We do the normal layout for INVISIBLE
8825 // windows, since that means "perform layout as normal,
8826 // just don't display").
8827 if (!gone || !win.mHaveFrame) {
8828 if (!win.mLayoutAttached) {
8829 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8830 } else {
8831 if (topAttached < 0) topAttached = i;
8832 }
8833 }
8834 }
Romain Guy06882f82009-06-10 13:36:04 -07008835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 // Now perform layout of attached windows, which usually
8837 // depend on the position of the window they are attached to.
8838 // XXX does not deal with windows that are attached to windows
8839 // that are themselves attached.
8840 for (i = topAttached; i >= 0; i--) {
8841 WindowState win = (WindowState) mWindows.get(i);
8842
8843 // If this view is GONE, then skip it -- keep the current
8844 // frame, and let the caller know so they can ignore it
8845 // if they want. (We do the normal layout for INVISIBLE
8846 // windows, since that means "perform layout as normal,
8847 // just don't display").
8848 if (win.mLayoutAttached) {
8849 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8850 || !win.mHaveFrame) {
8851 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8852 }
8853 }
8854 }
8855
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008856 if (!mPolicy.finishLayoutLw()) {
8857 mLayoutNeeded = false;
8858 } else if (repeats > 2) {
8859 Log.w(TAG, "Layout repeat aborted after too many iterations");
8860 mLayoutNeeded = false;
8861 } else {
8862 repeats++;
8863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 }
8865 }
Romain Guy06882f82009-06-10 13:36:04 -07008866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008867 private final void performLayoutAndPlaceSurfacesLockedInner(
8868 boolean recoveringMemory) {
8869 final long currentTime = SystemClock.uptimeMillis();
8870 final int dw = mDisplay.getWidth();
8871 final int dh = mDisplay.getHeight();
8872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 int i;
8874
8875 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008878 if (mFxSession == null) {
8879 mFxSession = new SurfaceSession();
8880 }
Romain Guy06882f82009-06-10 13:36:04 -07008881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008882 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8883
8884 // Initialize state of exiting tokens.
8885 for (i=mExitingTokens.size()-1; i>=0; i--) {
8886 mExitingTokens.get(i).hasVisible = false;
8887 }
8888
8889 // Initialize state of exiting applications.
8890 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8891 mExitingAppTokens.get(i).hasVisible = false;
8892 }
8893
8894 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 boolean orientationChangeComplete = true;
8896 Session holdScreen = null;
8897 float screenBrightness = -1;
8898 boolean focusDisplayed = false;
8899 boolean animating = false;
8900
8901 Surface.openTransaction();
8902 try {
8903 boolean restart;
8904
8905 do {
8906 final int transactionSequence = ++mTransactionSequence;
8907
8908 // Update animations of all applications, including those
8909 // associated with exiting/removed apps
8910 boolean tokensAnimating = false;
8911 final int NAT = mAppTokens.size();
8912 for (i=0; i<NAT; i++) {
8913 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8914 tokensAnimating = true;
8915 }
8916 }
8917 final int NEAT = mExitingAppTokens.size();
8918 for (i=0; i<NEAT; i++) {
8919 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8920 tokensAnimating = true;
8921 }
8922 }
8923
8924 animating = tokensAnimating;
8925 restart = false;
8926
8927 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008928 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008929
8930 mPolicy.beginAnimationLw(dw, dh);
8931
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008932 final int N = mWindows.size();
8933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 for (i=N-1; i>=0; i--) {
8935 WindowState w = (WindowState)mWindows.get(i);
8936
8937 final WindowManager.LayoutParams attrs = w.mAttrs;
8938
8939 if (w.mSurface != null) {
8940 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008941 if (w.commitFinishDrawingLocked(currentTime)) {
8942 if ((w.mAttrs.flags
8943 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008944 if (DEBUG_WALLPAPER) Log.v(TAG,
8945 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008946 wallpaperMayChange = true;
8947 }
8948 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008949 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008950 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8951 animating = true;
8952 //w.dump(" ");
8953 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008954 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
8955 wallpaperMayChange = true;
8956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 mPolicy.animatingWindowLw(w, attrs);
8958 }
8959
8960 final AppWindowToken atoken = w.mAppToken;
8961 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8962 if (atoken.lastTransactionSequence != transactionSequence) {
8963 atoken.lastTransactionSequence = transactionSequence;
8964 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8965 atoken.startingDisplayed = false;
8966 }
8967 if ((w.isOnScreen() || w.mAttrs.type
8968 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8969 && !w.mExiting && !w.mDestroying) {
8970 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8971 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8972 + w.isDisplayedLw()
8973 + ", isAnimating=" + w.isAnimating());
8974 if (!w.isDisplayedLw()) {
8975 Log.v(TAG, "Not displayed: s=" + w.mSurface
8976 + " pv=" + w.mPolicyVisibility
8977 + " dp=" + w.mDrawPending
8978 + " cdp=" + w.mCommitDrawPending
8979 + " ah=" + w.mAttachedHidden
8980 + " th=" + atoken.hiddenRequested
8981 + " a=" + w.mAnimating);
8982 }
8983 }
8984 if (w != atoken.startingWindow) {
8985 if (!atoken.freezingScreen || !w.mAppFreezing) {
8986 atoken.numInterestingWindows++;
8987 if (w.isDisplayedLw()) {
8988 atoken.numDrawnWindows++;
8989 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8990 "tokenMayBeDrawn: " + atoken
8991 + " freezingScreen=" + atoken.freezingScreen
8992 + " mAppFreezing=" + w.mAppFreezing);
8993 tokenMayBeDrawn = true;
8994 }
8995 }
8996 } else if (w.isDisplayedLw()) {
8997 atoken.startingDisplayed = true;
8998 }
8999 }
9000 } else if (w.mReadyToShow) {
9001 w.performShowLocked();
9002 }
9003 }
9004
9005 if (mPolicy.finishAnimationLw()) {
9006 restart = true;
9007 }
9008
9009 if (tokenMayBeDrawn) {
9010 // See if any windows have been drawn, so they (and others
9011 // associated with them) can now be shown.
9012 final int NT = mTokenList.size();
9013 for (i=0; i<NT; i++) {
9014 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9015 if (wtoken == null) {
9016 continue;
9017 }
9018 if (wtoken.freezingScreen) {
9019 int numInteresting = wtoken.numInterestingWindows;
9020 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9021 if (DEBUG_VISIBILITY) Log.v(TAG,
9022 "allDrawn: " + wtoken
9023 + " interesting=" + numInteresting
9024 + " drawn=" + wtoken.numDrawnWindows);
9025 wtoken.showAllWindowsLocked();
9026 unsetAppFreezingScreenLocked(wtoken, false, true);
9027 orientationChangeComplete = true;
9028 }
9029 } else if (!wtoken.allDrawn) {
9030 int numInteresting = wtoken.numInterestingWindows;
9031 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9032 if (DEBUG_VISIBILITY) Log.v(TAG,
9033 "allDrawn: " + wtoken
9034 + " interesting=" + numInteresting
9035 + " drawn=" + wtoken.numDrawnWindows);
9036 wtoken.allDrawn = true;
9037 restart = true;
9038
9039 // We can now show all of the drawn windows!
9040 if (!mOpeningApps.contains(wtoken)) {
9041 wtoken.showAllWindowsLocked();
9042 }
9043 }
9044 }
9045 }
9046 }
9047
9048 // If we are ready to perform an app transition, check through
9049 // all of the app tokens to be shown and see if they are ready
9050 // to go.
9051 if (mAppTransitionReady) {
9052 int NN = mOpeningApps.size();
9053 boolean goodToGo = true;
9054 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9055 "Checking " + NN + " opening apps (frozen="
9056 + mDisplayFrozen + " timeout="
9057 + mAppTransitionTimeout + ")...");
9058 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9059 // If the display isn't frozen, wait to do anything until
9060 // all of the apps are ready. Otherwise just go because
9061 // we'll unfreeze the display when everyone is ready.
9062 for (i=0; i<NN && goodToGo; i++) {
9063 AppWindowToken wtoken = mOpeningApps.get(i);
9064 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9065 "Check opening app" + wtoken + ": allDrawn="
9066 + wtoken.allDrawn + " startingDisplayed="
9067 + wtoken.startingDisplayed);
9068 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9069 && !wtoken.startingMoved) {
9070 goodToGo = false;
9071 }
9072 }
9073 }
9074 if (goodToGo) {
9075 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9076 int transit = mNextAppTransition;
9077 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009078 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009079 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009080 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009082 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 mAppTransitionTimeout = false;
9084 mStartingIconInTransition = false;
9085 mSkipAppTransitionAnimation = false;
9086
9087 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9088
Dianne Hackborna8f60182009-09-01 19:01:50 -07009089 // If there are applications waiting to come to the
9090 // top of the stack, now is the time to move their windows.
9091 // (Note that we don't do apps going to the bottom
9092 // here -- we want to keep their windows in the old
9093 // Z-order until the animation completes.)
9094 if (mToTopApps.size() > 0) {
9095 NN = mAppTokens.size();
9096 for (i=0; i<NN; i++) {
9097 AppWindowToken wtoken = mAppTokens.get(i);
9098 if (wtoken.sendingToTop) {
9099 wtoken.sendingToTop = false;
9100 moveAppWindowsLocked(wtoken, NN, false);
9101 }
9102 }
9103 mToTopApps.clear();
9104 }
9105
Dianne Hackborn25994b42009-09-04 14:21:19 -07009106 WindowState oldWallpaper = mWallpaperTarget;
9107
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009108 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009109 wallpaperMayChange = false;
9110
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009111 // The top-most window will supply the layout params,
9112 // and we will determine it below.
9113 LayoutParams animLp = null;
9114 int bestAnimLayer = -1;
9115
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009116 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009117 "New wallpaper target=" + mWallpaperTarget
9118 + ", lower target=" + mLowerWallpaperTarget
9119 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009120 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009121 // Do a first pass through the tokens for two
9122 // things:
9123 // (1) Determine if both the closing and opening
9124 // app token sets are wallpaper targets, in which
9125 // case special animations are needed
9126 // (since the wallpaper needs to stay static
9127 // behind them).
9128 // (2) Find the layout params of the top-most
9129 // application window in the tokens, which is
9130 // what will control the animation theme.
9131 final int NC = mClosingApps.size();
9132 NN = NC + mOpeningApps.size();
9133 for (i=0; i<NN; i++) {
9134 AppWindowToken wtoken;
9135 int mode;
9136 if (i < NC) {
9137 wtoken = mClosingApps.get(i);
9138 mode = 1;
9139 } else {
9140 wtoken = mOpeningApps.get(i-NC);
9141 mode = 2;
9142 }
9143 if (mLowerWallpaperTarget != null) {
9144 if (mLowerWallpaperTarget.mAppToken == wtoken
9145 || mUpperWallpaperTarget.mAppToken == wtoken) {
9146 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009147 }
9148 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009149 if (wtoken.appFullscreen) {
9150 WindowState ws = wtoken.findMainWindow();
9151 if (ws != null) {
9152 // If this is a compatibility mode
9153 // window, we will always use its anim.
9154 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9155 animLp = ws.mAttrs;
9156 bestAnimLayer = Integer.MAX_VALUE;
9157 } else if (ws.mLayer > bestAnimLayer) {
9158 animLp = ws.mAttrs;
9159 bestAnimLayer = ws.mLayer;
9160 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009161 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009162 }
9163 }
9164
Dianne Hackborn25994b42009-09-04 14:21:19 -07009165 if (foundWallpapers == 3) {
9166 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9167 "Wallpaper animation!");
9168 switch (transit) {
9169 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9170 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9171 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9172 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9173 break;
9174 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9175 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9176 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9177 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9178 break;
9179 }
9180 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9181 "New transit: " + transit);
9182 } else if (oldWallpaper != null) {
9183 // We are transitioning from an activity with
9184 // a wallpaper to one without.
9185 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9186 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9187 "New transit away from wallpaper: " + transit);
9188 } else if (mWallpaperTarget != null) {
9189 // We are transitioning from an activity without
9190 // a wallpaper to now showing the wallpaper
9191 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9192 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9193 "New transit into wallpaper: " + transit);
9194 }
9195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009196 NN = mOpeningApps.size();
9197 for (i=0; i<NN; i++) {
9198 AppWindowToken wtoken = mOpeningApps.get(i);
9199 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9200 "Now opening app" + wtoken);
9201 wtoken.reportedVisible = false;
9202 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009203 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009204 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009205 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009206 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207 wtoken.showAllWindowsLocked();
9208 }
9209 NN = mClosingApps.size();
9210 for (i=0; i<NN; i++) {
9211 AppWindowToken wtoken = mClosingApps.get(i);
9212 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9213 "Now closing app" + wtoken);
9214 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009215 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009216 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009218 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009219 // Force the allDrawn flag, because we want to start
9220 // this guy's animations regardless of whether it's
9221 // gotten drawn.
9222 wtoken.allDrawn = true;
9223 }
9224
9225 mOpeningApps.clear();
9226 mClosingApps.clear();
9227
9228 // This has changed the visibility of windows, so perform
9229 // a new layout to get them all up-to-date.
9230 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009231 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9232 assignLayersLocked();
9233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009234 performLayoutLockedInner();
9235 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9236
9237 restart = true;
9238 }
9239 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009240
Dianne Hackborna8f60182009-09-01 19:01:50 -07009241 if (!animating && mAppTransitionRunning) {
9242 // We have finished the animation of an app transition. To do
9243 // this, we have delayed a lot of operations like showing and
9244 // hiding apps, moving apps in Z-order, etc. The app token list
9245 // reflects the correct Z-order, but the window list may now
9246 // be out of sync with it. So here we will just rebuild the
9247 // entire app window list. Fun!
9248 mAppTransitionRunning = false;
9249 // Clear information about apps that were moving.
9250 mToBottomApps.clear();
9251
9252 rebuildAppWindowListLocked();
9253 restart = true;
9254 moveInputMethodWindowsIfNeededLocked(false);
9255 wallpaperMayChange = true;
9256 mLayoutNeeded = true;
9257 }
9258
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009259 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009260 if (DEBUG_WALLPAPER) Log.v(TAG,
9261 "Wallpaper may change! Adjusting");
9262 int adjResult = adjustWallpaperWindowsLocked();
9263 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9264 if (DEBUG_WALLPAPER) Log.v(TAG,
9265 "Wallpaper layer changed: assigning layers + relayout");
9266 restart = true;
9267 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009268 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009269 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9270 if (DEBUG_WALLPAPER) Log.v(TAG,
9271 "Wallpaper visibility changed: relayout");
9272 restart = true;
9273 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009274 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009275 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009276 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009277 performLayoutLockedInner();
9278 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009279 }
9280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009281 } while (restart);
9282
9283 // THIRD LOOP: Update the surfaces of all windows.
9284
9285 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9286
9287 boolean obscured = false;
9288 boolean blurring = false;
9289 boolean dimming = false;
9290 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009291 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009292 boolean backgroundFillerShown = false;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009293 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009295 final int N = mWindows.size();
9296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 for (i=N-1; i>=0; i--) {
9298 WindowState w = (WindowState)mWindows.get(i);
9299
9300 boolean displayed = false;
9301 final WindowManager.LayoutParams attrs = w.mAttrs;
9302 final int attrFlags = attrs.flags;
9303
9304 if (w.mSurface != null) {
9305 w.computeShownFrameLocked();
9306 if (localLOGV) Log.v(
9307 TAG, "Placing surface #" + i + " " + w.mSurface
9308 + ": new=" + w.mShownFrame + ", old="
9309 + w.mLastShownFrame);
9310
9311 boolean resize;
9312 int width, height;
9313 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9314 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9315 w.mLastRequestedHeight != w.mRequestedHeight;
9316 // for a scaled surface, we just want to use
9317 // the requested size.
9318 width = w.mRequestedWidth;
9319 height = w.mRequestedHeight;
9320 w.mLastRequestedWidth = width;
9321 w.mLastRequestedHeight = height;
9322 w.mLastShownFrame.set(w.mShownFrame);
9323 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009324 if (SHOW_TRANSACTIONS) Log.i(
9325 TAG, " SURFACE " + w.mSurface
9326 + ": POS " + w.mShownFrame.left
9327 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9329 } catch (RuntimeException e) {
9330 Log.w(TAG, "Error positioning surface in " + w, e);
9331 if (!recoveringMemory) {
9332 reclaimSomeSurfaceMemoryLocked(w, "position");
9333 }
9334 }
9335 } else {
9336 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9337 width = w.mShownFrame.width();
9338 height = w.mShownFrame.height();
9339 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 }
9341
9342 if (resize) {
9343 if (width < 1) width = 1;
9344 if (height < 1) height = 1;
9345 if (w.mSurface != null) {
9346 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009347 if (SHOW_TRANSACTIONS) Log.i(
9348 TAG, " SURFACE " + w.mSurface + ": POS "
9349 + w.mShownFrame.left + ","
9350 + w.mShownFrame.top + " SIZE "
9351 + w.mShownFrame.width() + "x"
9352 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009353 w.mSurface.setSize(width, height);
9354 w.mSurface.setPosition(w.mShownFrame.left,
9355 w.mShownFrame.top);
9356 } catch (RuntimeException e) {
9357 // If something goes wrong with the surface (such
9358 // as running out of memory), don't take down the
9359 // entire system.
9360 Log.e(TAG, "Failure updating surface of " + w
9361 + "size=(" + width + "x" + height
9362 + "), pos=(" + w.mShownFrame.left
9363 + "," + w.mShownFrame.top + ")", e);
9364 if (!recoveringMemory) {
9365 reclaimSomeSurfaceMemoryLocked(w, "size");
9366 }
9367 }
9368 }
9369 }
9370 if (!w.mAppFreezing) {
9371 w.mContentInsetsChanged =
9372 !w.mLastContentInsets.equals(w.mContentInsets);
9373 w.mVisibleInsetsChanged =
9374 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009375 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376 || w.mContentInsetsChanged
9377 || w.mVisibleInsetsChanged) {
9378 w.mLastFrame.set(w.mFrame);
9379 w.mLastContentInsets.set(w.mContentInsets);
9380 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009381 // If the screen is currently frozen, then keep
9382 // it frozen until this window draws at its new
9383 // orientation.
9384 if (mDisplayFrozen) {
9385 if (DEBUG_ORIENTATION) Log.v(TAG,
9386 "Resizing while display frozen: " + w);
9387 w.mOrientationChanging = true;
9388 if (mWindowsFreezingScreen) {
9389 mWindowsFreezingScreen = true;
9390 // XXX should probably keep timeout from
9391 // when we first froze the display.
9392 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9393 mH.sendMessageDelayed(mH.obtainMessage(
9394 H.WINDOW_FREEZE_TIMEOUT), 2000);
9395 }
9396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 // If the orientation is changing, then we need to
9398 // hold off on unfreezing the display until this
9399 // window has been redrawn; to do that, we need
9400 // to go through the process of getting informed
9401 // by the application when it has finished drawing.
9402 if (w.mOrientationChanging) {
9403 if (DEBUG_ORIENTATION) Log.v(TAG,
9404 "Orientation start waiting for draw in "
9405 + w + ", surface " + w.mSurface);
9406 w.mDrawPending = true;
9407 w.mCommitDrawPending = false;
9408 w.mReadyToShow = false;
9409 if (w.mAppToken != null) {
9410 w.mAppToken.allDrawn = false;
9411 }
9412 }
Romain Guy06882f82009-06-10 13:36:04 -07009413 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009414 "Resizing window " + w + " to " + w.mFrame);
9415 mResizingWindows.add(w);
9416 } else if (w.mOrientationChanging) {
9417 if (!w.mDrawPending && !w.mCommitDrawPending) {
9418 if (DEBUG_ORIENTATION) Log.v(TAG,
9419 "Orientation not waiting for draw in "
9420 + w + ", surface " + w.mSurface);
9421 w.mOrientationChanging = false;
9422 }
9423 }
9424 }
9425
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009426 if ((forceHiding
9427 && attrs.type != WindowManager.LayoutParams.TYPE_STATUS_BAR
9428 && attrs.type != WindowManager.LayoutParams.TYPE_WALLPAPER)
9429 || w.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009430 if (!w.mLastHidden) {
9431 //dump();
9432 w.mLastHidden = true;
9433 if (SHOW_TRANSACTIONS) Log.i(
9434 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9435 if (w.mSurface != null) {
9436 try {
9437 w.mSurface.hide();
9438 } catch (RuntimeException e) {
9439 Log.w(TAG, "Exception hiding surface in " + w);
9440 }
9441 }
9442 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9443 }
9444 // If we are waiting for this window to handle an
9445 // orientation change, well, it is hidden, so
9446 // doesn't really matter. Note that this does
9447 // introduce a potential glitch if the window
9448 // becomes unhidden before it has drawn for the
9449 // new orientation.
9450 if (w.mOrientationChanging) {
9451 w.mOrientationChanging = false;
9452 if (DEBUG_ORIENTATION) Log.v(TAG,
9453 "Orientation change skips hidden " + w);
9454 }
9455 } else if (!w.isReadyForDisplay()) {
9456 if (!w.mLastHidden) {
9457 //dump();
9458 w.mLastHidden = true;
9459 if (SHOW_TRANSACTIONS) Log.i(
9460 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9461 if (w.mSurface != null) {
9462 try {
9463 w.mSurface.hide();
9464 } catch (RuntimeException e) {
9465 Log.w(TAG, "Exception exception hiding surface in " + w);
9466 }
9467 }
9468 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9469 }
9470 // If we are waiting for this window to handle an
9471 // orientation change, well, it is hidden, so
9472 // doesn't really matter. Note that this does
9473 // introduce a potential glitch if the window
9474 // becomes unhidden before it has drawn for the
9475 // new orientation.
9476 if (w.mOrientationChanging) {
9477 w.mOrientationChanging = false;
9478 if (DEBUG_ORIENTATION) Log.v(TAG,
9479 "Orientation change skips hidden " + w);
9480 }
9481 } else if (w.mLastLayer != w.mAnimLayer
9482 || w.mLastAlpha != w.mShownAlpha
9483 || w.mLastDsDx != w.mDsDx
9484 || w.mLastDtDx != w.mDtDx
9485 || w.mLastDsDy != w.mDsDy
9486 || w.mLastDtDy != w.mDtDy
9487 || w.mLastHScale != w.mHScale
9488 || w.mLastVScale != w.mVScale
9489 || w.mLastHidden) {
9490 displayed = true;
9491 w.mLastAlpha = w.mShownAlpha;
9492 w.mLastLayer = w.mAnimLayer;
9493 w.mLastDsDx = w.mDsDx;
9494 w.mLastDtDx = w.mDtDx;
9495 w.mLastDsDy = w.mDsDy;
9496 w.mLastDtDy = w.mDtDy;
9497 w.mLastHScale = w.mHScale;
9498 w.mLastVScale = w.mVScale;
9499 if (SHOW_TRANSACTIONS) Log.i(
9500 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009501 + w.mShownAlpha + " layer=" + w.mAnimLayer
9502 + " matrix=[" + (w.mDsDx*w.mHScale)
9503 + "," + (w.mDtDx*w.mVScale)
9504 + "][" + (w.mDsDy*w.mHScale)
9505 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506 if (w.mSurface != null) {
9507 try {
9508 w.mSurface.setAlpha(w.mShownAlpha);
9509 w.mSurface.setLayer(w.mAnimLayer);
9510 w.mSurface.setMatrix(
9511 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9512 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9513 } catch (RuntimeException e) {
9514 Log.w(TAG, "Error updating surface in " + w, e);
9515 if (!recoveringMemory) {
9516 reclaimSomeSurfaceMemoryLocked(w, "update");
9517 }
9518 }
9519 }
9520
9521 if (w.mLastHidden && !w.mDrawPending
9522 && !w.mCommitDrawPending
9523 && !w.mReadyToShow) {
9524 if (SHOW_TRANSACTIONS) Log.i(
9525 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9526 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9527 + " during relayout");
9528 if (showSurfaceRobustlyLocked(w)) {
9529 w.mHasDrawn = true;
9530 w.mLastHidden = false;
9531 } else {
9532 w.mOrientationChanging = false;
9533 }
9534 }
9535 if (w.mSurface != null) {
9536 w.mToken.hasVisible = true;
9537 }
9538 } else {
9539 displayed = true;
9540 }
9541
9542 if (displayed) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009543 if (attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD) {
9544 forceHiding = true;
9545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009546 if (!covered) {
9547 if (attrs.width == LayoutParams.FILL_PARENT
9548 && attrs.height == LayoutParams.FILL_PARENT) {
9549 covered = true;
9550 }
9551 }
9552 if (w.mOrientationChanging) {
9553 if (w.mDrawPending || w.mCommitDrawPending) {
9554 orientationChangeComplete = false;
9555 if (DEBUG_ORIENTATION) Log.v(TAG,
9556 "Orientation continue waiting for draw in " + w);
9557 } else {
9558 w.mOrientationChanging = false;
9559 if (DEBUG_ORIENTATION) Log.v(TAG,
9560 "Orientation change complete in " + w);
9561 }
9562 }
9563 w.mToken.hasVisible = true;
9564 }
9565 } else if (w.mOrientationChanging) {
9566 if (DEBUG_ORIENTATION) Log.v(TAG,
9567 "Orientation change skips hidden " + w);
9568 w.mOrientationChanging = false;
9569 }
9570
9571 final boolean canBeSeen = w.isDisplayedLw();
9572
9573 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9574 focusDisplayed = true;
9575 }
9576
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009577 final boolean obscuredChanged = w.mObscured != obscured;
9578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009579 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009580 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009581 if (w.mSurface != null) {
9582 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9583 holdScreen = w.mSession;
9584 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009585 if (!syswin && w.mAttrs.screenBrightness >= 0
9586 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009587 screenBrightness = w.mAttrs.screenBrightness;
9588 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009589 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9590 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9591 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9592 syswin = true;
9593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009595
Dianne Hackborn25994b42009-09-04 14:21:19 -07009596 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9597 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 // This window completely covers everything behind it,
9599 // so we want to leave all of them as unblurred (for
9600 // performance reasons).
9601 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009602 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9603 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009604 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009605 obscured = true;
9606 if (mBackgroundFillerSurface == null) {
9607 try {
9608 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9609 0, dw, dh,
9610 PixelFormat.OPAQUE,
9611 Surface.FX_SURFACE_NORMAL);
9612 } catch (Exception e) {
9613 Log.e(TAG, "Exception creating filler surface", e);
9614 }
9615 }
9616 try {
9617 mBackgroundFillerSurface.setPosition(0, 0);
9618 mBackgroundFillerSurface.setSize(dw, dh);
9619 // Using the same layer as Dim because they will never be shown at the
9620 // same time.
9621 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9622 mBackgroundFillerSurface.show();
9623 } catch (RuntimeException e) {
9624 Log.e(TAG, "Exception showing filler surface");
9625 }
9626 backgroundFillerShown = true;
9627 mBackgroundFillerShown = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009628 } else if (canBeSeen && !obscured && !forceHiding &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9630 if (localLOGV) Log.v(TAG, "Win " + w
9631 + ": blurring=" + blurring
9632 + " obscured=" + obscured
9633 + " displayed=" + displayed);
9634 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9635 if (!dimming) {
9636 //Log.i(TAG, "DIM BEHIND: " + w);
9637 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009638 if (mDimAnimator == null) {
9639 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009640 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009641 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009643 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 }
9645 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9646 if (!blurring) {
9647 //Log.i(TAG, "BLUR BEHIND: " + w);
9648 blurring = true;
9649 mBlurShown = true;
9650 if (mBlurSurface == null) {
9651 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9652 + mBlurSurface + ": CREATE");
9653 try {
Romain Guy06882f82009-06-10 13:36:04 -07009654 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009655 -1, 16, 16,
9656 PixelFormat.OPAQUE,
9657 Surface.FX_SURFACE_BLUR);
9658 } catch (Exception e) {
9659 Log.e(TAG, "Exception creating Blur surface", e);
9660 }
9661 }
9662 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9663 + mBlurSurface + ": SHOW pos=(0,0) (" +
9664 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9665 if (mBlurSurface != null) {
9666 mBlurSurface.setPosition(0, 0);
9667 mBlurSurface.setSize(dw, dh);
9668 try {
9669 mBlurSurface.show();
9670 } catch (RuntimeException e) {
9671 Log.w(TAG, "Failure showing blur surface", e);
9672 }
9673 }
9674 }
9675 mBlurSurface.setLayer(w.mAnimLayer-2);
9676 }
9677 }
9678 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009679
9680 if (obscuredChanged && mWallpaperTarget == w) {
9681 // This is the wallpaper target and its obscured state
9682 // changed... make sure the current wallaper's visibility
9683 // has been updated accordingly.
9684 updateWallpaperVisibilityLocked();
9685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009687
9688 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9689 mBackgroundFillerShown = false;
9690 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9691 try {
9692 mBackgroundFillerSurface.hide();
9693 } catch (RuntimeException e) {
9694 Log.e(TAG, "Exception hiding filler surface", e);
9695 }
9696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009697
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009698 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9699 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 }
Romain Guy06882f82009-06-10 13:36:04 -07009701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 if (!blurring && mBlurShown) {
9703 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9704 + ": HIDE");
9705 try {
9706 mBlurSurface.hide();
9707 } catch (IllegalArgumentException e) {
9708 Log.w(TAG, "Illegal argument exception hiding blur surface");
9709 }
9710 mBlurShown = false;
9711 }
9712
9713 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9714 } catch (RuntimeException e) {
9715 Log.e(TAG, "Unhandled exception in Window Manager", e);
9716 }
9717
9718 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009720 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9721 "With display frozen, orientationChangeComplete="
9722 + orientationChangeComplete);
9723 if (orientationChangeComplete) {
9724 if (mWindowsFreezingScreen) {
9725 mWindowsFreezingScreen = false;
9726 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9727 }
9728 if (mAppsFreezingScreen == 0) {
9729 stopFreezingDisplayLocked();
9730 }
9731 }
Romain Guy06882f82009-06-10 13:36:04 -07009732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009733 i = mResizingWindows.size();
9734 if (i > 0) {
9735 do {
9736 i--;
9737 WindowState win = mResizingWindows.get(i);
9738 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009739 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9740 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009741 win.mClient.resized(win.mFrame.width(),
9742 win.mFrame.height(), win.mLastContentInsets,
9743 win.mLastVisibleInsets, win.mDrawPending);
9744 win.mContentInsetsChanged = false;
9745 win.mVisibleInsetsChanged = false;
9746 } catch (RemoteException e) {
9747 win.mOrientationChanging = false;
9748 }
9749 } while (i > 0);
9750 mResizingWindows.clear();
9751 }
Romain Guy06882f82009-06-10 13:36:04 -07009752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009753 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009754 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 i = mDestroySurface.size();
9756 if (i > 0) {
9757 do {
9758 i--;
9759 WindowState win = mDestroySurface.get(i);
9760 win.mDestroying = false;
9761 if (mInputMethodWindow == win) {
9762 mInputMethodWindow = null;
9763 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009764 if (win == mWallpaperTarget) {
9765 wallpaperDestroyed = true;
9766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009767 win.destroySurfaceLocked();
9768 } while (i > 0);
9769 mDestroySurface.clear();
9770 }
9771
9772 // Time to remove any exiting tokens?
9773 for (i=mExitingTokens.size()-1; i>=0; i--) {
9774 WindowToken token = mExitingTokens.get(i);
9775 if (!token.hasVisible) {
9776 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009777 if (token.windowType == TYPE_WALLPAPER) {
9778 mWallpaperTokens.remove(token);
9779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 }
9781 }
9782
9783 // Time to remove any exiting applications?
9784 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9785 AppWindowToken token = mExitingAppTokens.get(i);
9786 if (!token.hasVisible && !mClosingApps.contains(token)) {
9787 mAppTokens.remove(token);
9788 mExitingAppTokens.remove(i);
9789 }
9790 }
9791
Dianne Hackborna8f60182009-09-01 19:01:50 -07009792 boolean needRelayout = false;
9793
9794 if (!animating && mAppTransitionRunning) {
9795 // We have finished the animation of an app transition. To do
9796 // this, we have delayed a lot of operations like showing and
9797 // hiding apps, moving apps in Z-order, etc. The app token list
9798 // reflects the correct Z-order, but the window list may now
9799 // be out of sync with it. So here we will just rebuild the
9800 // entire app window list. Fun!
9801 mAppTransitionRunning = false;
9802 needRelayout = true;
9803 rebuildAppWindowListLocked();
9804 // Clear information about apps that were moving.
9805 mToBottomApps.clear();
9806 }
9807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 if (focusDisplayed) {
9809 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9810 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009811 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009812 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009813 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009814 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009815 requestAnimationLocked(0);
9816 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9818 }
9819 mQueue.setHoldScreenLocked(holdScreen != null);
9820 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9821 mPowerManager.setScreenBrightnessOverride(-1);
9822 } else {
9823 mPowerManager.setScreenBrightnessOverride((int)
9824 (screenBrightness * Power.BRIGHTNESS_ON));
9825 }
9826 if (holdScreen != mHoldingScreenOn) {
9827 mHoldingScreenOn = holdScreen;
9828 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9829 mH.sendMessage(m);
9830 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009831
9832 if (mTurnOnScreen) {
9833 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
9834 LocalPowerManager.BUTTON_EVENT, true);
9835 mTurnOnScreen = false;
9836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 }
9838
9839 void requestAnimationLocked(long delay) {
9840 if (!mAnimationPending) {
9841 mAnimationPending = true;
9842 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9843 }
9844 }
Romain Guy06882f82009-06-10 13:36:04 -07009845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 /**
9847 * Have the surface flinger show a surface, robustly dealing with
9848 * error conditions. In particular, if there is not enough memory
9849 * to show the surface, then we will try to get rid of other surfaces
9850 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 * @return Returns true if the surface was successfully shown.
9853 */
9854 boolean showSurfaceRobustlyLocked(WindowState win) {
9855 try {
9856 if (win.mSurface != null) {
9857 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009858 if (win.mTurnOnScreen) {
9859 win.mTurnOnScreen = false;
9860 mTurnOnScreen = true;
9861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 }
9863 return true;
9864 } catch (RuntimeException e) {
9865 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9866 }
Romain Guy06882f82009-06-10 13:36:04 -07009867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 return false;
9871 }
Romain Guy06882f82009-06-10 13:36:04 -07009872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9874 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9877 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 if (mForceRemoves == null) {
9880 mForceRemoves = new ArrayList<WindowState>();
9881 }
Romain Guy06882f82009-06-10 13:36:04 -07009882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 long callingIdentity = Binder.clearCallingIdentity();
9884 try {
9885 // There was some problem... first, do a sanity check of the
9886 // window list to make sure we haven't left any dangling surfaces
9887 // around.
9888 int N = mWindows.size();
9889 boolean leakedSurface = false;
9890 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9891 for (int i=0; i<N; i++) {
9892 WindowState ws = (WindowState)mWindows.get(i);
9893 if (ws.mSurface != null) {
9894 if (!mSessions.contains(ws.mSession)) {
9895 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9896 + ws + " surface=" + ws.mSurface
9897 + " token=" + win.mToken
9898 + " pid=" + ws.mSession.mPid
9899 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009900 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901 ws.mSurface = null;
9902 mForceRemoves.add(ws);
9903 i--;
9904 N--;
9905 leakedSurface = true;
9906 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9907 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9908 + ws + " surface=" + ws.mSurface
9909 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009910 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 ws.mSurface = null;
9912 leakedSurface = true;
9913 }
9914 }
9915 }
Romain Guy06882f82009-06-10 13:36:04 -07009916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917 boolean killedApps = false;
9918 if (!leakedSurface) {
9919 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9920 SparseIntArray pidCandidates = new SparseIntArray();
9921 for (int i=0; i<N; i++) {
9922 WindowState ws = (WindowState)mWindows.get(i);
9923 if (ws.mSurface != null) {
9924 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9925 }
9926 }
9927 if (pidCandidates.size() > 0) {
9928 int[] pids = new int[pidCandidates.size()];
9929 for (int i=0; i<pids.length; i++) {
9930 pids[i] = pidCandidates.keyAt(i);
9931 }
9932 try {
9933 if (mActivityManager.killPidsForMemory(pids)) {
9934 killedApps = true;
9935 }
9936 } catch (RemoteException e) {
9937 }
9938 }
9939 }
Romain Guy06882f82009-06-10 13:36:04 -07009940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009941 if (leakedSurface || killedApps) {
9942 // We managed to reclaim some memory, so get rid of the trouble
9943 // surface and ask the app to request another one.
9944 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9945 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009946 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009947 win.mSurface = null;
9948 }
Romain Guy06882f82009-06-10 13:36:04 -07009949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009950 try {
9951 win.mClient.dispatchGetNewSurface();
9952 } catch (RemoteException e) {
9953 }
9954 }
9955 } finally {
9956 Binder.restoreCallingIdentity(callingIdentity);
9957 }
9958 }
Romain Guy06882f82009-06-10 13:36:04 -07009959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 private boolean updateFocusedWindowLocked(int mode) {
9961 WindowState newFocus = computeFocusedWindowLocked();
9962 if (mCurrentFocus != newFocus) {
9963 // This check makes sure that we don't already have the focus
9964 // change message pending.
9965 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9966 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9967 if (localLOGV) Log.v(
9968 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9969 final WindowState oldFocus = mCurrentFocus;
9970 mCurrentFocus = newFocus;
9971 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009973 final WindowState imWindow = mInputMethodWindow;
9974 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009975 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009976 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009977 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9978 mLayoutNeeded = true;
9979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9981 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009982 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9983 // Client will do the layout, but we need to assign layers
9984 // for handleNewWindowLocked() below.
9985 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 }
9987 }
Romain Guy06882f82009-06-10 13:36:04 -07009988
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009989 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9990 mKeyWaiter.handleNewWindowLocked(newFocus);
9991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 return true;
9993 }
9994 return false;
9995 }
9996
9997 private WindowState computeFocusedWindowLocked() {
9998 WindowState result = null;
9999 WindowState win;
10000
10001 int i = mWindows.size() - 1;
10002 int nextAppIndex = mAppTokens.size()-1;
10003 WindowToken nextApp = nextAppIndex >= 0
10004 ? mAppTokens.get(nextAppIndex) : null;
10005
10006 while (i >= 0) {
10007 win = (WindowState)mWindows.get(i);
10008
10009 if (localLOGV || DEBUG_FOCUS) Log.v(
10010 TAG, "Looking for focus: " + i
10011 + " = " + win
10012 + ", flags=" + win.mAttrs.flags
10013 + ", canReceive=" + win.canReceiveKeys());
10014
10015 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 // If this window's application has been removed, just skip it.
10018 if (thisApp != null && thisApp.removed) {
10019 i--;
10020 continue;
10021 }
Romain Guy06882f82009-06-10 13:36:04 -070010022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010023 // If there is a focused app, don't allow focus to go to any
10024 // windows below it. If this is an application window, step
10025 // through the app tokens until we find its app.
10026 if (thisApp != null && nextApp != null && thisApp != nextApp
10027 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10028 int origAppIndex = nextAppIndex;
10029 while (nextAppIndex > 0) {
10030 if (nextApp == mFocusedApp) {
10031 // Whoops, we are below the focused app... no focus
10032 // for you!
10033 if (localLOGV || DEBUG_FOCUS) Log.v(
10034 TAG, "Reached focused app: " + mFocusedApp);
10035 return null;
10036 }
10037 nextAppIndex--;
10038 nextApp = mAppTokens.get(nextAppIndex);
10039 if (nextApp == thisApp) {
10040 break;
10041 }
10042 }
10043 if (thisApp != nextApp) {
10044 // Uh oh, the app token doesn't exist! This shouldn't
10045 // happen, but if it does we can get totally hosed...
10046 // so restart at the original app.
10047 nextAppIndex = origAppIndex;
10048 nextApp = mAppTokens.get(nextAppIndex);
10049 }
10050 }
10051
10052 // Dispatch to this window if it is wants key events.
10053 if (win.canReceiveKeys()) {
10054 if (DEBUG_FOCUS) Log.v(
10055 TAG, "Found focus @ " + i + " = " + win);
10056 result = win;
10057 break;
10058 }
10059
10060 i--;
10061 }
10062
10063 return result;
10064 }
10065
10066 private void startFreezingDisplayLocked() {
10067 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010068 // Freezing the display also suspends key event delivery, to
10069 // keep events from going astray while the display is reconfigured.
10070 // If someone has changed orientation again while the screen is
10071 // still frozen, the events will continue to be blocked while the
10072 // successive orientation change is processed. To prevent spurious
10073 // ANRs, we reset the event dispatch timeout in this case.
10074 synchronized (mKeyWaiter) {
10075 mKeyWaiter.mWasFrozen = true;
10076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010077 return;
10078 }
Romain Guy06882f82009-06-10 13:36:04 -070010079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 long now = SystemClock.uptimeMillis();
10083 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10084 if (mFreezeGcPending != 0) {
10085 if (now > (mFreezeGcPending+1000)) {
10086 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10087 mH.removeMessages(H.FORCE_GC);
10088 Runtime.getRuntime().gc();
10089 mFreezeGcPending = now;
10090 }
10091 } else {
10092 mFreezeGcPending = now;
10093 }
Romain Guy06882f82009-06-10 13:36:04 -070010094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010096 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10097 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 mAppTransitionReady = true;
10099 }
Romain Guy06882f82009-06-10 13:36:04 -070010100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 if (PROFILE_ORIENTATION) {
10102 File file = new File("/data/system/frozen");
10103 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10104 }
10105 Surface.freezeDisplay(0);
10106 }
Romain Guy06882f82009-06-10 13:36:04 -070010107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 private void stopFreezingDisplayLocked() {
10109 if (!mDisplayFrozen) {
10110 return;
10111 }
Romain Guy06882f82009-06-10 13:36:04 -070010112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010113 mDisplayFrozen = false;
10114 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10115 if (PROFILE_ORIENTATION) {
10116 Debug.stopMethodTracing();
10117 }
10118 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010119
Chris Tate2ad63a92009-03-25 17:36:48 -070010120 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10121 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 synchronized (mKeyWaiter) {
10123 mKeyWaiter.mWasFrozen = true;
10124 mKeyWaiter.notifyAll();
10125 }
10126
10127 // A little kludge: a lot could have happened while the
10128 // display was frozen, so now that we are coming back we
10129 // do a gc so that any remote references the system
10130 // processes holds on others can be released if they are
10131 // no longer needed.
10132 mH.removeMessages(H.FORCE_GC);
10133 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10134 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 mScreenFrozenLock.release();
10137 }
Romain Guy06882f82009-06-10 13:36:04 -070010138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 @Override
10140 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10141 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10142 != PackageManager.PERMISSION_GRANTED) {
10143 pw.println("Permission Denial: can't dump WindowManager from from pid="
10144 + Binder.getCallingPid()
10145 + ", uid=" + Binder.getCallingUid());
10146 return;
10147 }
Romain Guy06882f82009-06-10 13:36:04 -070010148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 synchronized(mWindowMap) {
10150 pw.println("Current Window Manager state:");
10151 for (int i=mWindows.size()-1; i>=0; i--) {
10152 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010153 pw.print(" Window #"); pw.print(i); pw.print(' ');
10154 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 w.dump(pw, " ");
10156 }
10157 if (mInputMethodDialogs.size() > 0) {
10158 pw.println(" ");
10159 pw.println(" Input method dialogs:");
10160 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10161 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010162 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 }
10164 }
10165 if (mPendingRemove.size() > 0) {
10166 pw.println(" ");
10167 pw.println(" Remove pending for:");
10168 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10169 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010170 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10171 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172 w.dump(pw, " ");
10173 }
10174 }
10175 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10176 pw.println(" ");
10177 pw.println(" Windows force removing:");
10178 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10179 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010180 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10181 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 w.dump(pw, " ");
10183 }
10184 }
10185 if (mDestroySurface.size() > 0) {
10186 pw.println(" ");
10187 pw.println(" Windows waiting to destroy their surface:");
10188 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10189 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010190 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10191 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 w.dump(pw, " ");
10193 }
10194 }
10195 if (mLosingFocus.size() > 0) {
10196 pw.println(" ");
10197 pw.println(" Windows losing focus:");
10198 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10199 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010200 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10201 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010202 w.dump(pw, " ");
10203 }
10204 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010205 if (mResizingWindows.size() > 0) {
10206 pw.println(" ");
10207 pw.println(" Windows waiting to resize:");
10208 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10209 WindowState w = mResizingWindows.get(i);
10210 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10211 pw.print(w); pw.println(":");
10212 w.dump(pw, " ");
10213 }
10214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010215 if (mSessions.size() > 0) {
10216 pw.println(" ");
10217 pw.println(" All active sessions:");
10218 Iterator<Session> it = mSessions.iterator();
10219 while (it.hasNext()) {
10220 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010221 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 s.dump(pw, " ");
10223 }
10224 }
10225 if (mTokenMap.size() > 0) {
10226 pw.println(" ");
10227 pw.println(" All tokens:");
10228 Iterator<WindowToken> it = mTokenMap.values().iterator();
10229 while (it.hasNext()) {
10230 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010231 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010232 token.dump(pw, " ");
10233 }
10234 }
10235 if (mTokenList.size() > 0) {
10236 pw.println(" ");
10237 pw.println(" Window token list:");
10238 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010239 pw.print(" #"); pw.print(i); pw.print(": ");
10240 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 }
10242 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010243 if (mWallpaperTokens.size() > 0) {
10244 pw.println(" ");
10245 pw.println(" Wallpaper tokens:");
10246 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10247 WindowToken token = mWallpaperTokens.get(i);
10248 pw.print(" Wallpaper #"); pw.print(i);
10249 pw.print(' '); pw.print(token); pw.println(':');
10250 token.dump(pw, " ");
10251 }
10252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010253 if (mAppTokens.size() > 0) {
10254 pw.println(" ");
10255 pw.println(" Application tokens in Z order:");
10256 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010257 pw.print(" App #"); pw.print(i); pw.print(": ");
10258 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 }
10260 }
10261 if (mFinishedStarting.size() > 0) {
10262 pw.println(" ");
10263 pw.println(" Finishing start of application tokens:");
10264 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10265 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010266 pw.print(" Finished Starting #"); pw.print(i);
10267 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010268 token.dump(pw, " ");
10269 }
10270 }
10271 if (mExitingTokens.size() > 0) {
10272 pw.println(" ");
10273 pw.println(" Exiting tokens:");
10274 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10275 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010276 pw.print(" Exiting #"); pw.print(i);
10277 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 token.dump(pw, " ");
10279 }
10280 }
10281 if (mExitingAppTokens.size() > 0) {
10282 pw.println(" ");
10283 pw.println(" Exiting application tokens:");
10284 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10285 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010286 pw.print(" Exiting App #"); pw.print(i);
10287 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010288 token.dump(pw, " ");
10289 }
10290 }
10291 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010292 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10293 pw.print(" mLastFocus="); pw.println(mLastFocus);
10294 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10295 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10296 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010297 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010298 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10299 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10300 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10301 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010302 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10303 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10304 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10305 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10306 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010307 if (mDimAnimator != null) {
10308 mDimAnimator.printTo(pw);
10309 } else {
10310 pw.print( " no DimAnimator ");
10311 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010312 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010313 pw.print(mInputMethodAnimLayerAdjustment);
10314 pw.print(" mWallpaperAnimLayerAdjustment=");
10315 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010316 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10317 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010318 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10319 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10320 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10321 pw.print(" mRotation="); pw.print(mRotation);
10322 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10323 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10324 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10325 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10326 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10327 pw.print(" mNextAppTransition=0x");
10328 pw.print(Integer.toHexString(mNextAppTransition));
10329 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010330 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010331 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10332 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10333 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10334 if (mOpeningApps.size() > 0) {
10335 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10336 }
10337 if (mClosingApps.size() > 0) {
10338 pw.print(" mClosingApps="); pw.println(mClosingApps);
10339 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010340 if (mToTopApps.size() > 0) {
10341 pw.print(" mToTopApps="); pw.println(mToTopApps);
10342 }
10343 if (mToBottomApps.size() > 0) {
10344 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10345 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010346 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10347 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010349 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10350 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10351 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10352 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10353 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10354 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 }
10356 }
10357
10358 public void monitor() {
10359 synchronized (mWindowMap) { }
10360 synchronized (mKeyguardDisabled) { }
10361 synchronized (mKeyWaiter) { }
10362 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010363
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010364 public void virtualKeyFeedback(KeyEvent event) {
10365 mPolicy.keyFeedbackFromInput(event);
10366 }
10367
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010368 /**
10369 * DimAnimator class that controls the dim animation. This holds the surface and
10370 * all state used for dim animation.
10371 */
10372 private static class DimAnimator {
10373 Surface mDimSurface;
10374 boolean mDimShown = false;
10375 float mDimCurrentAlpha;
10376 float mDimTargetAlpha;
10377 float mDimDeltaPerMs;
10378 long mLastDimAnimTime;
10379
10380 DimAnimator (SurfaceSession session) {
10381 if (mDimSurface == null) {
10382 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10383 + mDimSurface + ": CREATE");
10384 try {
10385 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10386 Surface.FX_SURFACE_DIM);
10387 } catch (Exception e) {
10388 Log.e(TAG, "Exception creating Dim surface", e);
10389 }
10390 }
10391 }
10392
10393 /**
10394 * Show the dim surface.
10395 */
10396 void show(int dw, int dh) {
10397 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10398 dw + "x" + dh + ")");
10399 mDimShown = true;
10400 try {
10401 mDimSurface.setPosition(0, 0);
10402 mDimSurface.setSize(dw, dh);
10403 mDimSurface.show();
10404 } catch (RuntimeException e) {
10405 Log.w(TAG, "Failure showing dim surface", e);
10406 }
10407 }
10408
10409 /**
10410 * Set's the dim surface's layer and update dim parameters that will be used in
10411 * {@link updateSurface} after all windows are examined.
10412 */
10413 void updateParameters(WindowState w, long currentTime) {
10414 mDimSurface.setLayer(w.mAnimLayer-1);
10415
10416 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010417 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10418 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010419 if (mDimTargetAlpha != target) {
10420 // If the desired dim level has changed, then
10421 // start an animation to it.
10422 mLastDimAnimTime = currentTime;
10423 long duration = (w.mAnimating && w.mAnimation != null)
10424 ? w.mAnimation.computeDurationHint()
10425 : DEFAULT_DIM_DURATION;
10426 if (target > mDimTargetAlpha) {
10427 // This is happening behind the activity UI,
10428 // so we can make it run a little longer to
10429 // give a stronger impression without disrupting
10430 // the user.
10431 duration *= DIM_DURATION_MULTIPLIER;
10432 }
10433 if (duration < 1) {
10434 // Don't divide by zero
10435 duration = 1;
10436 }
10437 mDimTargetAlpha = target;
10438 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10439 }
10440 }
10441
10442 /**
10443 * Updating the surface's alpha. Returns true if the animation continues, or returns
10444 * false when the animation is finished and the dim surface is hidden.
10445 */
10446 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10447 if (!dimming) {
10448 if (mDimTargetAlpha != 0) {
10449 mLastDimAnimTime = currentTime;
10450 mDimTargetAlpha = 0;
10451 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10452 }
10453 }
10454
10455 boolean animating = false;
10456 if (mLastDimAnimTime != 0) {
10457 mDimCurrentAlpha += mDimDeltaPerMs
10458 * (currentTime-mLastDimAnimTime);
10459 boolean more = true;
10460 if (displayFrozen) {
10461 // If the display is frozen, there is no reason to animate.
10462 more = false;
10463 } else if (mDimDeltaPerMs > 0) {
10464 if (mDimCurrentAlpha > mDimTargetAlpha) {
10465 more = false;
10466 }
10467 } else if (mDimDeltaPerMs < 0) {
10468 if (mDimCurrentAlpha < mDimTargetAlpha) {
10469 more = false;
10470 }
10471 } else {
10472 more = false;
10473 }
10474
10475 // Do we need to continue animating?
10476 if (more) {
10477 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10478 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10479 mLastDimAnimTime = currentTime;
10480 mDimSurface.setAlpha(mDimCurrentAlpha);
10481 animating = true;
10482 } else {
10483 mDimCurrentAlpha = mDimTargetAlpha;
10484 mLastDimAnimTime = 0;
10485 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10486 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10487 mDimSurface.setAlpha(mDimCurrentAlpha);
10488 if (!dimming) {
10489 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10490 + ": HIDE");
10491 try {
10492 mDimSurface.hide();
10493 } catch (RuntimeException e) {
10494 Log.w(TAG, "Illegal argument exception hiding dim surface");
10495 }
10496 mDimShown = false;
10497 }
10498 }
10499 }
10500 return animating;
10501 }
10502
10503 public void printTo(PrintWriter pw) {
10504 pw.print(" mDimShown="); pw.print(mDimShown);
10505 pw.print(" current="); pw.print(mDimCurrentAlpha);
10506 pw.print(" target="); pw.print(mDimTargetAlpha);
10507 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10508 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10509 }
10510 }
10511
10512 /**
10513 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10514 * This is used for opening/closing transition for apps in compatible mode.
10515 */
10516 private static class FadeInOutAnimation extends Animation {
10517 int mWidth;
10518 boolean mFadeIn;
10519
10520 public FadeInOutAnimation(boolean fadeIn) {
10521 setInterpolator(new AccelerateInterpolator());
10522 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10523 mFadeIn = fadeIn;
10524 }
10525
10526 @Override
10527 protected void applyTransformation(float interpolatedTime, Transformation t) {
10528 float x = interpolatedTime;
10529 if (!mFadeIn) {
10530 x = 1.0f - x; // reverse the interpolation for fade out
10531 }
10532 if (x < 0.5) {
10533 // move the window out of the screen.
10534 t.getMatrix().setTranslate(mWidth, 0);
10535 } else {
10536 t.getMatrix().setTranslate(0, 0);// show
10537 t.setAlpha((x - 0.5f) * 2);
10538 }
10539 }
10540
10541 @Override
10542 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10543 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10544 mWidth = width;
10545 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010546
10547 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010548 public int getZAdjustment() {
10549 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010550 }
10551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010552}