blob: d53f00242f18957678a834d4547ffc25134a8df7 [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.
375 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
376 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
936 // 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 Hackbornf8fbdb62009-08-19 12:39:43 -07001273 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1274 // 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 Hackborn4c62fc02009-08-08 20:40:27 -07001387 // Now w is the window we are supposed to be behind... but we
1388 // need to be sure to also be behind any of its attached windows,
1389 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001390 while (foundI > 0) {
1391 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1392 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001393 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001394 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001395 // This window is not related to the previous one in any
1396 // interesting way, so stop here.
1397 break;
1398 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001399 foundW = wb;
1400 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001401 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001402 } else {
1403 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001404 }
1405
1406 // Okay i is the position immediately above the wallpaper. Look at
1407 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001408 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001409
Dianne Hackborn284ac932009-08-28 10:34:25 -07001410 if (visible) {
1411 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1412 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1413 }
1414
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001415 // Start stepping backwards from here, ensuring that our wallpaper windows
1416 // are correctly placed.
1417 int curTokenIndex = mWallpaperTokens.size();
1418 while (curTokenIndex > 0) {
1419 curTokenIndex--;
1420 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001421 if (token.hidden == visible) {
1422 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1423 token.hidden = !visible;
1424 // Need to do a layout to ensure the wallpaper now has the
1425 // correct size.
1426 mLayoutNeeded = true;
1427 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001428
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001429 int curWallpaperIndex = token.windows.size();
1430 while (curWallpaperIndex > 0) {
1431 curWallpaperIndex--;
1432 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001433
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001434 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001435 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001436 }
1437
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001438 // First, make sure the client has the current visibility
1439 // state.
1440 if (wallpaper.mWallpaperVisible != visible) {
1441 wallpaper.mWallpaperVisible = visible;
1442 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001443 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001444 "Setting visibility of wallpaper " + wallpaper
1445 + ": " + visible);
1446 wallpaper.mClient.dispatchAppVisibility(visible);
1447 } catch (RemoteException e) {
1448 }
1449 }
1450
1451 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001452 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1453 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001454
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001455 // First, if this window is at the current index, then all
1456 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001457 if (wallpaper == foundW) {
1458 foundI--;
1459 foundW = foundI > 0
1460 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001461 continue;
1462 }
1463
1464 // The window didn't match... the current wallpaper window,
1465 // wherever it is, is in the wrong place, so make sure it is
1466 // not in the list.
1467 int oldIndex = localmWindows.indexOf(wallpaper);
1468 if (oldIndex >= 0) {
1469 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001470 if (oldIndex < foundI) {
1471 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001472 }
1473 }
1474
1475 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001476 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001477 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001478
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001479 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001480 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001481 }
1482 }
1483
1484 return changed;
1485 }
1486
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001487 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001488 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1489 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001490 mWallpaperAnimLayerAdjustment = adj;
1491 int curTokenIndex = mWallpaperTokens.size();
1492 while (curTokenIndex > 0) {
1493 curTokenIndex--;
1494 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1495 int curWallpaperIndex = token.windows.size();
1496 while (curWallpaperIndex > 0) {
1497 curWallpaperIndex--;
1498 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1499 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001500 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1501 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001502 }
1503 }
1504 }
1505
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001506 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1507 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001508 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001509 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001510 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001511 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001512 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001513 changed = wallpaperWin.mXOffset != offset;
1514 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001515 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1516 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001517 wallpaperWin.mXOffset = offset;
1518 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001519 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1520 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001521 rawChanged = true;
1522 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001523 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001524
Dianne Hackborn284ac932009-08-28 10:34:25 -07001525 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001526 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001527 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001528 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001529 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1530 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001531 changed = true;
1532 wallpaperWin.mYOffset = offset;
1533 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001534 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1535 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001536 rawChanged = true;
1537 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001538 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001539
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001540 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001541 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001542 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1543 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1544 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001545 if (sync) {
1546 synchronized (mWaitingOnWallpaperLock) {
1547 mWaitingOnWallpaper = wallpaperWin;
1548 }
1549 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001550 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001551 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1552 if (sync) {
1553 synchronized (mWaitingOnWallpaperLock) {
1554 if (mWaitingOnWallpaper != null) {
1555 long start = SystemClock.uptimeMillis();
1556 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1557 < start) {
1558 try {
1559 if (DEBUG_WALLPAPER) Log.v(TAG,
1560 "Waiting for offset complete...");
1561 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1562 } catch (InterruptedException e) {
1563 }
1564 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1565 if ((start+WALLPAPER_TIMEOUT)
1566 < SystemClock.uptimeMillis()) {
1567 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1568 + wallpaperWin);
1569 mLastWallpaperTimeoutTime = start;
1570 }
1571 }
1572 mWaitingOnWallpaper = null;
1573 }
1574 }
1575 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001576 } catch (RemoteException e) {
1577 }
1578 }
1579
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001580 return changed;
1581 }
1582
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001583 void wallpaperOffsetsComplete(IBinder window) {
1584 synchronized (mWaitingOnWallpaperLock) {
1585 if (mWaitingOnWallpaper != null &&
1586 mWaitingOnWallpaper.mClient.asBinder() == window) {
1587 mWaitingOnWallpaper = null;
1588 mWaitingOnWallpaperLock.notifyAll();
1589 }
1590 }
1591 }
1592
1593 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001594 final int dw = mDisplay.getWidth();
1595 final int dh = mDisplay.getHeight();
1596
1597 boolean changed = false;
1598
1599 WindowState target = mWallpaperTarget;
1600 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001601 mLastWallpaperX = target.mWallpaperX;
1602 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001603 int curTokenIndex = mWallpaperTokens.size();
1604 while (curTokenIndex > 0) {
1605 curTokenIndex--;
1606 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1607 int curWallpaperIndex = token.windows.size();
1608 while (curWallpaperIndex > 0) {
1609 curWallpaperIndex--;
1610 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001611 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001612 wallpaper.computeShownFrameLocked();
1613 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001614 // We only want to be synchronous with one wallpaper.
1615 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001616 }
1617 }
1618 }
1619 }
1620
1621 return changed;
1622 }
1623
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001624 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001625 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001626 final int dw = mDisplay.getWidth();
1627 final int dh = mDisplay.getHeight();
1628
1629 int curTokenIndex = mWallpaperTokens.size();
1630 while (curTokenIndex > 0) {
1631 curTokenIndex--;
1632 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001633 if (token.hidden == visible) {
1634 token.hidden = !visible;
1635 // Need to do a layout to ensure the wallpaper now has the
1636 // correct size.
1637 mLayoutNeeded = true;
1638 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001639
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001640 int curWallpaperIndex = token.windows.size();
1641 while (curWallpaperIndex > 0) {
1642 curWallpaperIndex--;
1643 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1644 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001645 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001646 }
1647
1648 if (wallpaper.mWallpaperVisible != visible) {
1649 wallpaper.mWallpaperVisible = visible;
1650 try {
1651 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001652 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001653 + ": " + visible);
1654 wallpaper.mClient.dispatchAppVisibility(visible);
1655 } catch (RemoteException e) {
1656 }
1657 }
1658 }
1659 }
1660 }
1661
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001662 void sendPointerToWallpaperLocked(WindowState srcWin,
1663 MotionEvent pointer, long eventTime) {
1664 int curTokenIndex = mWallpaperTokens.size();
1665 while (curTokenIndex > 0) {
1666 curTokenIndex--;
1667 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1668 int curWallpaperIndex = token.windows.size();
1669 while (curWallpaperIndex > 0) {
1670 curWallpaperIndex--;
1671 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1672 if ((wallpaper.mAttrs.flags &
1673 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1674 continue;
1675 }
1676 try {
1677 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1678 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1679 srcWin.mFrame.top-wallpaper.mFrame.top);
1680 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1681 } catch (RemoteException e) {
1682 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1683 }
1684 }
1685 }
1686 }
1687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 public int addWindow(Session session, IWindow client,
1689 WindowManager.LayoutParams attrs, int viewVisibility,
1690 Rect outContentInsets) {
1691 int res = mPolicy.checkAddPermission(attrs);
1692 if (res != WindowManagerImpl.ADD_OKAY) {
1693 return res;
1694 }
Romain Guy06882f82009-06-10 13:36:04 -07001695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 boolean reportNewConfig = false;
1697 WindowState attachedWindow = null;
1698 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 synchronized(mWindowMap) {
1701 // Instantiating a Display requires talking with the simulator,
1702 // so don't do it until we know the system is mostly up and
1703 // running.
1704 if (mDisplay == null) {
1705 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1706 mDisplay = wm.getDefaultDisplay();
1707 mQueue.setDisplay(mDisplay);
1708 reportNewConfig = true;
1709 }
Romain Guy06882f82009-06-10 13:36:04 -07001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 if (mWindowMap.containsKey(client.asBinder())) {
1712 Log.w(TAG, "Window " + client + " is already added");
1713 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1714 }
1715
1716 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001717 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 if (attachedWindow == null) {
1719 Log.w(TAG, "Attempted to add window with token that is not a window: "
1720 + attrs.token + ". Aborting.");
1721 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1722 }
1723 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1724 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1725 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1726 + attrs.token + ". Aborting.");
1727 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1728 }
1729 }
1730
1731 boolean addToken = false;
1732 WindowToken token = mTokenMap.get(attrs.token);
1733 if (token == null) {
1734 if (attrs.type >= FIRST_APPLICATION_WINDOW
1735 && attrs.type <= LAST_APPLICATION_WINDOW) {
1736 Log.w(TAG, "Attempted to add application window with unknown token "
1737 + attrs.token + ". Aborting.");
1738 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1739 }
1740 if (attrs.type == TYPE_INPUT_METHOD) {
1741 Log.w(TAG, "Attempted to add input method window with unknown token "
1742 + attrs.token + ". Aborting.");
1743 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1744 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001745 if (attrs.type == TYPE_WALLPAPER) {
1746 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1747 + attrs.token + ". Aborting.");
1748 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 token = new WindowToken(attrs.token, -1, false);
1751 addToken = true;
1752 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1753 && attrs.type <= LAST_APPLICATION_WINDOW) {
1754 AppWindowToken atoken = token.appWindowToken;
1755 if (atoken == null) {
1756 Log.w(TAG, "Attempted to add window with non-application token "
1757 + token + ". Aborting.");
1758 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1759 } else if (atoken.removed) {
1760 Log.w(TAG, "Attempted to add window with exiting application token "
1761 + token + ". Aborting.");
1762 return WindowManagerImpl.ADD_APP_EXITING;
1763 }
1764 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1765 // No need for this guy!
1766 if (localLOGV) Log.v(
1767 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1768 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1769 }
1770 } else if (attrs.type == TYPE_INPUT_METHOD) {
1771 if (token.windowType != TYPE_INPUT_METHOD) {
1772 Log.w(TAG, "Attempted to add input method window with bad token "
1773 + attrs.token + ". Aborting.");
1774 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1775 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001776 } else if (attrs.type == TYPE_WALLPAPER) {
1777 if (token.windowType != TYPE_WALLPAPER) {
1778 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1779 + attrs.token + ". Aborting.");
1780 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783
1784 win = new WindowState(session, client, token,
1785 attachedWindow, attrs, viewVisibility);
1786 if (win.mDeathRecipient == null) {
1787 // Client has apparently died, so there is no reason to
1788 // continue.
1789 Log.w(TAG, "Adding window client " + client.asBinder()
1790 + " that is dead, aborting.");
1791 return WindowManagerImpl.ADD_APP_EXITING;
1792 }
1793
1794 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 res = mPolicy.prepareAddWindowLw(win, attrs);
1797 if (res != WindowManagerImpl.ADD_OKAY) {
1798 return res;
1799 }
1800
1801 // From now on, no exceptions or errors allowed!
1802
1803 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 if (addToken) {
1808 mTokenMap.put(attrs.token, token);
1809 mTokenList.add(token);
1810 }
1811 win.attach();
1812 mWindowMap.put(client.asBinder(), win);
1813
1814 if (attrs.type == TYPE_APPLICATION_STARTING &&
1815 token.appWindowToken != null) {
1816 token.appWindowToken.startingWindow = win;
1817 }
1818
1819 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 if (attrs.type == TYPE_INPUT_METHOD) {
1822 mInputMethodWindow = win;
1823 addInputMethodWindowToListLocked(win);
1824 imMayMove = false;
1825 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1826 mInputMethodDialogs.add(win);
1827 addWindowToListInOrderLocked(win, true);
1828 adjustInputMethodDialogsLocked();
1829 imMayMove = false;
1830 } else {
1831 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001832 if (attrs.type == TYPE_WALLPAPER) {
1833 mLastWallpaperTimeoutTime = 0;
1834 adjustWallpaperWindowsLocked();
1835 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001836 adjustWallpaperWindowsLocked();
1837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
Romain Guy06882f82009-06-10 13:36:04 -07001839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 if (mInTouchMode) {
1845 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1846 }
1847 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1848 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1849 }
Romain Guy06882f82009-06-10 13:36:04 -07001850
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001851 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001853 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1854 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 imMayMove = false;
1856 }
1857 }
Romain Guy06882f82009-06-10 13:36:04 -07001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001860 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
Romain Guy06882f82009-06-10 13:36:04 -07001862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 assignLayersLocked();
1864 // Don't do layout here, the window must call
1865 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 //dump();
1868
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001869 if (focusChanged) {
1870 if (mCurrentFocus != null) {
1871 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1872 }
1873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 if (localLOGV) Log.v(
1875 TAG, "New client " + client.asBinder()
1876 + ": window=" + win);
1877 }
1878
1879 // sendNewConfiguration() checks caller permissions so we must call it with
1880 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1881 // identity anyway, so it's safe to just clear & restore around this whole
1882 // block.
1883 final long origId = Binder.clearCallingIdentity();
1884 if (reportNewConfig) {
1885 sendNewConfiguration();
1886 } else {
1887 // Update Orientation after adding a window, only if the window needs to be
1888 // displayed right away
1889 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001890 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 sendNewConfiguration();
1892 }
1893 }
1894 }
1895 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 return res;
1898 }
Romain Guy06882f82009-06-10 13:36:04 -07001899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 public void removeWindow(Session session, IWindow client) {
1901 synchronized(mWindowMap) {
1902 WindowState win = windowForClientLocked(session, client);
1903 if (win == null) {
1904 return;
1905 }
1906 removeWindowLocked(session, win);
1907 }
1908 }
Romain Guy06882f82009-06-10 13:36:04 -07001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 public void removeWindowLocked(Session session, WindowState win) {
1911
1912 if (localLOGV || DEBUG_FOCUS) Log.v(
1913 TAG, "Remove " + win + " client="
1914 + Integer.toHexString(System.identityHashCode(
1915 win.mClient.asBinder()))
1916 + ", surface=" + win.mSurface);
1917
1918 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 if (DEBUG_APP_TRANSITIONS) Log.v(
1921 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1922 + " mExiting=" + win.mExiting
1923 + " isAnimating=" + win.isAnimating()
1924 + " app-animation="
1925 + (win.mAppToken != null ? win.mAppToken.animation : null)
1926 + " inPendingTransaction="
1927 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1928 + " mDisplayFrozen=" + mDisplayFrozen);
1929 // Visibility of the removed window. Will be used later to update orientation later on.
1930 boolean wasVisible = false;
1931 // First, see if we need to run an animation. If we do, we have
1932 // to hold off on removing the window until the animation is done.
1933 // If the display is frozen, just remove immediately, since the
1934 // animation wouldn't be seen.
1935 if (win.mSurface != null && !mDisplayFrozen) {
1936 // If we are not currently running the exit animation, we
1937 // need to see about starting one.
1938 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1941 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1942 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1943 }
1944 // Try starting an animation.
1945 if (applyAnimationLocked(win, transit, false)) {
1946 win.mExiting = true;
1947 }
1948 }
1949 if (win.mExiting || win.isAnimating()) {
1950 // The exit animation is running... wait for it!
1951 //Log.i(TAG, "*** Running exit animation...");
1952 win.mExiting = true;
1953 win.mRemoveOnExit = true;
1954 mLayoutNeeded = true;
1955 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1956 performLayoutAndPlaceSurfacesLocked();
1957 if (win.mAppToken != null) {
1958 win.mAppToken.updateReportedVisibilityLocked();
1959 }
1960 //dump();
1961 Binder.restoreCallingIdentity(origId);
1962 return;
1963 }
1964 }
1965
1966 removeWindowInnerLocked(session, win);
1967 // Removing a visible window will effect the computed orientation
1968 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001969 if (wasVisible && computeForcedAppOrientationLocked()
1970 != mForcedAppOrientation) {
1971 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
1973 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1974 Binder.restoreCallingIdentity(origId);
1975 }
Romain Guy06882f82009-06-10 13:36:04 -07001976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001978 mKeyWaiter.finishedKey(session, win.mClient, true,
1979 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1981 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 if (mInputMethodTarget == win) {
1986 moveInputMethodWindowsIfNeededLocked(false);
1987 }
Romain Guy06882f82009-06-10 13:36:04 -07001988
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001989 if (false) {
1990 RuntimeException e = new RuntimeException("here");
1991 e.fillInStackTrace();
1992 Log.w(TAG, "Removing window " + win, e);
1993 }
1994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 mPolicy.removeWindowLw(win);
1996 win.removeLocked();
1997
1998 mWindowMap.remove(win.mClient.asBinder());
1999 mWindows.remove(win);
2000
2001 if (mInputMethodWindow == win) {
2002 mInputMethodWindow = null;
2003 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2004 mInputMethodDialogs.remove(win);
2005 }
Romain Guy06882f82009-06-10 13:36:04 -07002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 final WindowToken token = win.mToken;
2008 final AppWindowToken atoken = win.mAppToken;
2009 token.windows.remove(win);
2010 if (atoken != null) {
2011 atoken.allAppWindows.remove(win);
2012 }
2013 if (localLOGV) Log.v(
2014 TAG, "**** Removing window " + win + ": count="
2015 + token.windows.size());
2016 if (token.windows.size() == 0) {
2017 if (!token.explicit) {
2018 mTokenMap.remove(token.token);
2019 mTokenList.remove(token);
2020 } else if (atoken != null) {
2021 atoken.firstWindowDrawn = false;
2022 }
2023 }
2024
2025 if (atoken != null) {
2026 if (atoken.startingWindow == win) {
2027 atoken.startingWindow = null;
2028 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2029 // If this is the last window and we had requested a starting
2030 // transition window, well there is no point now.
2031 atoken.startingData = null;
2032 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2033 // If this is the last window except for a starting transition
2034 // window, we need to get rid of the starting transition.
2035 if (DEBUG_STARTING_WINDOW) {
2036 Log.v(TAG, "Schedule remove starting " + token
2037 + ": no more real windows");
2038 }
2039 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2040 mH.sendMessage(m);
2041 }
2042 }
Romain Guy06882f82009-06-10 13:36:04 -07002043
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002044 if (win.mAttrs.type == TYPE_WALLPAPER) {
2045 mLastWallpaperTimeoutTime = 0;
2046 adjustWallpaperWindowsLocked();
2047 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002048 adjustWallpaperWindowsLocked();
2049 }
2050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 if (!mInLayout) {
2052 assignLayersLocked();
2053 mLayoutNeeded = true;
2054 performLayoutAndPlaceSurfacesLocked();
2055 if (win.mAppToken != null) {
2056 win.mAppToken.updateReportedVisibilityLocked();
2057 }
2058 }
2059 }
2060
2061 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2062 long origId = Binder.clearCallingIdentity();
2063 try {
2064 synchronized (mWindowMap) {
2065 WindowState w = windowForClientLocked(session, client);
2066 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002067 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 Surface.openTransaction();
2069 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002070 if (SHOW_TRANSACTIONS) Log.i(
2071 TAG, " SURFACE " + w.mSurface
2072 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 w.mSurface.setTransparentRegionHint(region);
2074 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002075 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 Surface.closeTransaction();
2077 }
2078 }
2079 }
2080 } finally {
2081 Binder.restoreCallingIdentity(origId);
2082 }
2083 }
2084
2085 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002086 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 Rect visibleInsets) {
2088 long origId = Binder.clearCallingIdentity();
2089 try {
2090 synchronized (mWindowMap) {
2091 WindowState w = windowForClientLocked(session, client);
2092 if (w != null) {
2093 w.mGivenInsetsPending = false;
2094 w.mGivenContentInsets.set(contentInsets);
2095 w.mGivenVisibleInsets.set(visibleInsets);
2096 w.mTouchableInsets = touchableInsets;
2097 mLayoutNeeded = true;
2098 performLayoutAndPlaceSurfacesLocked();
2099 }
2100 }
2101 } finally {
2102 Binder.restoreCallingIdentity(origId);
2103 }
2104 }
Romain Guy06882f82009-06-10 13:36:04 -07002105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 public void getWindowDisplayFrame(Session session, IWindow client,
2107 Rect outDisplayFrame) {
2108 synchronized(mWindowMap) {
2109 WindowState win = windowForClientLocked(session, client);
2110 if (win == null) {
2111 outDisplayFrame.setEmpty();
2112 return;
2113 }
2114 outDisplayFrame.set(win.mDisplayFrame);
2115 }
2116 }
2117
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002118 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2119 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2120 window.mWallpaperX = x;
2121 window.mWallpaperY = y;
2122
2123 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002124 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002125 performLayoutAndPlaceSurfacesLocked();
2126 }
2127 }
2128 }
2129 }
2130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 public int relayoutWindow(Session session, IWindow client,
2132 WindowManager.LayoutParams attrs, int requestedWidth,
2133 int requestedHeight, int viewVisibility, boolean insetsPending,
2134 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2135 Surface outSurface) {
2136 boolean displayed = false;
2137 boolean inTouchMode;
2138 Configuration newConfig = null;
2139 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 synchronized(mWindowMap) {
2142 WindowState win = windowForClientLocked(session, client);
2143 if (win == null) {
2144 return 0;
2145 }
2146 win.mRequestedWidth = requestedWidth;
2147 win.mRequestedHeight = requestedHeight;
2148
2149 if (attrs != null) {
2150 mPolicy.adjustWindowParamsLw(attrs);
2151 }
Romain Guy06882f82009-06-10 13:36:04 -07002152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 int attrChanges = 0;
2154 int flagChanges = 0;
2155 if (attrs != null) {
2156 flagChanges = win.mAttrs.flags ^= attrs.flags;
2157 attrChanges = win.mAttrs.copyFrom(attrs);
2158 }
2159
2160 if (localLOGV) Log.v(
2161 TAG, "Relayout given client " + client.asBinder()
2162 + " (" + win.mAttrs.getTitle() + ")");
2163
2164
2165 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2166 win.mAlpha = attrs.alpha;
2167 }
2168
2169 final boolean scaledWindow =
2170 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2171
2172 if (scaledWindow) {
2173 // requested{Width|Height} Surface's physical size
2174 // attrs.{width|height} Size on screen
2175 win.mHScale = (attrs.width != requestedWidth) ?
2176 (attrs.width / (float)requestedWidth) : 1.0f;
2177 win.mVScale = (attrs.height != requestedHeight) ?
2178 (attrs.height / (float)requestedHeight) : 1.0f;
2179 }
2180
2181 boolean imMayMove = (flagChanges&(
2182 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2183 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 boolean focusMayChange = win.mViewVisibility != viewVisibility
2186 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2187 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002188
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002189 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2190 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 win.mRelayoutCalled = true;
2193 final int oldVisibility = win.mViewVisibility;
2194 win.mViewVisibility = viewVisibility;
2195 if (viewVisibility == View.VISIBLE &&
2196 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2197 displayed = !win.isVisibleLw();
2198 if (win.mExiting) {
2199 win.mExiting = false;
2200 win.mAnimation = null;
2201 }
2202 if (win.mDestroying) {
2203 win.mDestroying = false;
2204 mDestroySurface.remove(win);
2205 }
2206 if (oldVisibility == View.GONE) {
2207 win.mEnterAnimationPending = true;
2208 }
2209 if (displayed && win.mSurface != null && !win.mDrawPending
2210 && !win.mCommitDrawPending && !mDisplayFrozen) {
2211 applyEnterAnimationLocked(win);
2212 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002213 if (displayed && (win.mAttrs.flags
2214 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2215 win.mTurnOnScreen = true;
2216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2218 // To change the format, we need to re-build the surface.
2219 win.destroySurfaceLocked();
2220 displayed = true;
2221 }
2222 try {
2223 Surface surface = win.createSurfaceLocked();
2224 if (surface != null) {
2225 outSurface.copyFrom(surface);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002226 if (SHOW_TRANSACTIONS) Log.i(TAG,
2227 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002229 // For some reason there isn't a surface. Clear the
2230 // caller's object so they see the same state.
2231 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
2233 } catch (Exception e) {
2234 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002235 + client + " (" + win.mAttrs.getTitle() + ")",
2236 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 Binder.restoreCallingIdentity(origId);
2238 return 0;
2239 }
2240 if (displayed) {
2241 focusMayChange = true;
2242 }
2243 if (win.mAttrs.type == TYPE_INPUT_METHOD
2244 && mInputMethodWindow == null) {
2245 mInputMethodWindow = win;
2246 imMayMove = true;
2247 }
2248 } else {
2249 win.mEnterAnimationPending = false;
2250 if (win.mSurface != null) {
2251 // If we are not currently running the exit animation, we
2252 // need to see about starting one.
2253 if (!win.mExiting) {
2254 // Try starting an animation; if there isn't one, we
2255 // can destroy the surface right away.
2256 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2257 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2258 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2259 }
2260 if (win.isWinVisibleLw() &&
2261 applyAnimationLocked(win, transit, false)) {
2262 win.mExiting = true;
2263 mKeyWaiter.finishedKey(session, client, true,
2264 KeyWaiter.RETURN_NOTHING);
2265 } else if (win.isAnimating()) {
2266 // Currently in a hide animation... turn this into
2267 // an exit.
2268 win.mExiting = true;
2269 } else {
2270 if (mInputMethodWindow == win) {
2271 mInputMethodWindow = null;
2272 }
2273 win.destroySurfaceLocked();
2274 }
2275 }
2276 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002277 // We are being called from a local process, which
2278 // means outSurface holds its current surface. Ensure the
2279 // surface object is cleared, but we don't want it actually
2280 // destroyed at this point.
2281 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
2283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 if (focusMayChange) {
2285 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2286 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 imMayMove = false;
2288 }
2289 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2290 }
Romain Guy06882f82009-06-10 13:36:04 -07002291
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002292 // updateFocusedWindowLocked() already assigned layers so we only need to
2293 // reassign them at this point if the IM window state gets shuffled
2294 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 if (imMayMove) {
2297 if (moveInputMethodWindowsIfNeededLocked(false)) {
2298 assignLayers = true;
2299 }
2300 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002301 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002302 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002303 assignLayers = true;
2304 }
2305 }
Romain Guy06882f82009-06-10 13:36:04 -07002306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 mLayoutNeeded = true;
2308 win.mGivenInsetsPending = insetsPending;
2309 if (assignLayers) {
2310 assignLayersLocked();
2311 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002312 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002314 if (displayed && win.mIsWallpaper) {
2315 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002316 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 if (win.mAppToken != null) {
2319 win.mAppToken.updateReportedVisibilityLocked();
2320 }
2321 outFrame.set(win.mFrame);
2322 outContentInsets.set(win.mContentInsets);
2323 outVisibleInsets.set(win.mVisibleInsets);
2324 if (localLOGV) Log.v(
2325 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002326 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 + ", requestedHeight=" + requestedHeight
2328 + ", viewVisibility=" + viewVisibility
2329 + "\nRelayout returning frame=" + outFrame
2330 + ", surface=" + outSurface);
2331
2332 if (localLOGV || DEBUG_FOCUS) Log.v(
2333 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2334
2335 inTouchMode = mInTouchMode;
2336 }
2337
2338 if (newConfig != null) {
2339 sendNewConfiguration();
2340 }
Romain Guy06882f82009-06-10 13:36:04 -07002341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2345 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2346 }
2347
2348 public void finishDrawingWindow(Session session, IWindow client) {
2349 final long origId = Binder.clearCallingIdentity();
2350 synchronized(mWindowMap) {
2351 WindowState win = windowForClientLocked(session, client);
2352 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002353 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2354 adjustWallpaperWindowsLocked();
2355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 mLayoutNeeded = true;
2357 performLayoutAndPlaceSurfacesLocked();
2358 }
2359 }
2360 Binder.restoreCallingIdentity(origId);
2361 }
2362
2363 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2364 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2365 + (lp != null ? lp.packageName : null)
2366 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2367 if (lp != null && lp.windowAnimations != 0) {
2368 // If this is a system resource, don't try to load it from the
2369 // application resources. It is nice to avoid loading application
2370 // resources if we can.
2371 String packageName = lp.packageName != null ? lp.packageName : "android";
2372 int resId = lp.windowAnimations;
2373 if ((resId&0xFF000000) == 0x01000000) {
2374 packageName = "android";
2375 }
2376 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2377 + packageName);
2378 return AttributeCache.instance().get(packageName, resId,
2379 com.android.internal.R.styleable.WindowAnimation);
2380 }
2381 return null;
2382 }
Romain Guy06882f82009-06-10 13:36:04 -07002383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 private void applyEnterAnimationLocked(WindowState win) {
2385 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2386 if (win.mEnterAnimationPending) {
2387 win.mEnterAnimationPending = false;
2388 transit = WindowManagerPolicy.TRANSIT_ENTER;
2389 }
2390
2391 applyAnimationLocked(win, transit, true);
2392 }
2393
2394 private boolean applyAnimationLocked(WindowState win,
2395 int transit, boolean isEntrance) {
2396 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2397 // If we are trying to apply an animation, but already running
2398 // an animation of the same type, then just leave that one alone.
2399 return true;
2400 }
Romain Guy06882f82009-06-10 13:36:04 -07002401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 // Only apply an animation if the display isn't frozen. If it is
2403 // frozen, there is no reason to animate and it can cause strange
2404 // artifacts when we unfreeze the display if some different animation
2405 // is running.
2406 if (!mDisplayFrozen) {
2407 int anim = mPolicy.selectAnimationLw(win, transit);
2408 int attr = -1;
2409 Animation a = null;
2410 if (anim != 0) {
2411 a = AnimationUtils.loadAnimation(mContext, anim);
2412 } else {
2413 switch (transit) {
2414 case WindowManagerPolicy.TRANSIT_ENTER:
2415 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2416 break;
2417 case WindowManagerPolicy.TRANSIT_EXIT:
2418 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2419 break;
2420 case WindowManagerPolicy.TRANSIT_SHOW:
2421 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2422 break;
2423 case WindowManagerPolicy.TRANSIT_HIDE:
2424 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2425 break;
2426 }
2427 if (attr >= 0) {
2428 a = loadAnimation(win.mAttrs, attr);
2429 }
2430 }
2431 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2432 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2433 + " mAnimation=" + win.mAnimation
2434 + " isEntrance=" + isEntrance);
2435 if (a != null) {
2436 if (DEBUG_ANIM) {
2437 RuntimeException e = new RuntimeException();
2438 e.fillInStackTrace();
2439 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2440 }
2441 win.setAnimation(a);
2442 win.mAnimationIsEntrance = isEntrance;
2443 }
2444 } else {
2445 win.clearAnimation();
2446 }
2447
2448 return win.mAnimation != null;
2449 }
2450
2451 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2452 int anim = 0;
2453 Context context = mContext;
2454 if (animAttr >= 0) {
2455 AttributeCache.Entry ent = getCachedAnimations(lp);
2456 if (ent != null) {
2457 context = ent.context;
2458 anim = ent.array.getResourceId(animAttr, 0);
2459 }
2460 }
2461 if (anim != 0) {
2462 return AnimationUtils.loadAnimation(context, anim);
2463 }
2464 return null;
2465 }
Romain Guy06882f82009-06-10 13:36:04 -07002466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 private boolean applyAnimationLocked(AppWindowToken wtoken,
2468 WindowManager.LayoutParams lp, int transit, boolean enter) {
2469 // Only apply an animation if the display isn't frozen. If it is
2470 // frozen, there is no reason to animate and it can cause strange
2471 // artifacts when we unfreeze the display if some different animation
2472 // is running.
2473 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002474 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002475 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002476 a = new FadeInOutAnimation(enter);
2477 if (DEBUG_ANIM) Log.v(TAG,
2478 "applying FadeInOutAnimation for a window in compatibility mode");
2479 } else {
2480 int animAttr = 0;
2481 switch (transit) {
2482 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2483 animAttr = enter
2484 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2485 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2486 break;
2487 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2488 animAttr = enter
2489 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2490 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2491 break;
2492 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2493 animAttr = enter
2494 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2495 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2496 break;
2497 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2498 animAttr = enter
2499 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2500 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2501 break;
2502 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2503 animAttr = enter
2504 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2505 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2506 break;
2507 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2508 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002509 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002510 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2511 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002512 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002513 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002514 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2515 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002516 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002517 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002518 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002519 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2520 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2521 break;
2522 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2523 animAttr = enter
2524 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2525 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2526 break;
2527 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2528 animAttr = enter
2529 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2530 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002531 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002532 }
2533 a = loadAnimation(lp, animAttr);
2534 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2535 + " anim=" + a
2536 + " animAttr=0x" + Integer.toHexString(animAttr)
2537 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 if (a != null) {
2540 if (DEBUG_ANIM) {
2541 RuntimeException e = new RuntimeException();
2542 e.fillInStackTrace();
2543 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2544 }
2545 wtoken.setAnimation(a);
2546 }
2547 } else {
2548 wtoken.clearAnimation();
2549 }
2550
2551 return wtoken.animation != null;
2552 }
2553
2554 // -------------------------------------------------------------
2555 // Application Window Tokens
2556 // -------------------------------------------------------------
2557
2558 public void validateAppTokens(List tokens) {
2559 int v = tokens.size()-1;
2560 int m = mAppTokens.size()-1;
2561 while (v >= 0 && m >= 0) {
2562 AppWindowToken wtoken = mAppTokens.get(m);
2563 if (wtoken.removed) {
2564 m--;
2565 continue;
2566 }
2567 if (tokens.get(v) != wtoken.token) {
2568 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2569 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2570 }
2571 v--;
2572 m--;
2573 }
2574 while (v >= 0) {
2575 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2576 v--;
2577 }
2578 while (m >= 0) {
2579 AppWindowToken wtoken = mAppTokens.get(m);
2580 if (!wtoken.removed) {
2581 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2582 }
2583 m--;
2584 }
2585 }
2586
2587 boolean checkCallingPermission(String permission, String func) {
2588 // Quick check: if the calling permission is me, it's all okay.
2589 if (Binder.getCallingPid() == Process.myPid()) {
2590 return true;
2591 }
Romain Guy06882f82009-06-10 13:36:04 -07002592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 if (mContext.checkCallingPermission(permission)
2594 == PackageManager.PERMISSION_GRANTED) {
2595 return true;
2596 }
2597 String msg = "Permission Denial: " + func + " from pid="
2598 + Binder.getCallingPid()
2599 + ", uid=" + Binder.getCallingUid()
2600 + " requires " + permission;
2601 Log.w(TAG, msg);
2602 return false;
2603 }
Romain Guy06882f82009-06-10 13:36:04 -07002604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 AppWindowToken findAppWindowToken(IBinder token) {
2606 WindowToken wtoken = mTokenMap.get(token);
2607 if (wtoken == null) {
2608 return null;
2609 }
2610 return wtoken.appWindowToken;
2611 }
Romain Guy06882f82009-06-10 13:36:04 -07002612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 public void addWindowToken(IBinder token, int type) {
2614 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2615 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002616 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 }
Romain Guy06882f82009-06-10 13:36:04 -07002618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 synchronized(mWindowMap) {
2620 WindowToken wtoken = mTokenMap.get(token);
2621 if (wtoken != null) {
2622 Log.w(TAG, "Attempted to add existing input method token: " + token);
2623 return;
2624 }
2625 wtoken = new WindowToken(token, type, true);
2626 mTokenMap.put(token, wtoken);
2627 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002628 if (type == TYPE_WALLPAPER) {
2629 mWallpaperTokens.add(wtoken);
2630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 }
2632 }
Romain Guy06882f82009-06-10 13:36:04 -07002633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 public void removeWindowToken(IBinder token) {
2635 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2636 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002637 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 }
2639
2640 final long origId = Binder.clearCallingIdentity();
2641 synchronized(mWindowMap) {
2642 WindowToken wtoken = mTokenMap.remove(token);
2643 mTokenList.remove(wtoken);
2644 if (wtoken != null) {
2645 boolean delayed = false;
2646 if (!wtoken.hidden) {
2647 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 final int N = wtoken.windows.size();
2650 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 for (int i=0; i<N; i++) {
2653 WindowState win = wtoken.windows.get(i);
2654
2655 if (win.isAnimating()) {
2656 delayed = true;
2657 }
Romain Guy06882f82009-06-10 13:36:04 -07002658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 if (win.isVisibleNow()) {
2660 applyAnimationLocked(win,
2661 WindowManagerPolicy.TRANSIT_EXIT, false);
2662 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2663 KeyWaiter.RETURN_NOTHING);
2664 changed = true;
2665 }
2666 }
2667
2668 if (changed) {
2669 mLayoutNeeded = true;
2670 performLayoutAndPlaceSurfacesLocked();
2671 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2672 }
Romain Guy06882f82009-06-10 13:36:04 -07002673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 if (delayed) {
2675 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002676 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2677 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 }
2679 }
Romain Guy06882f82009-06-10 13:36:04 -07002680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 } else {
2682 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2683 }
2684 }
2685 Binder.restoreCallingIdentity(origId);
2686 }
2687
2688 public void addAppToken(int addPos, IApplicationToken token,
2689 int groupId, int requestedOrientation, boolean fullscreen) {
2690 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2691 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002692 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 }
Romain Guy06882f82009-06-10 13:36:04 -07002694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 synchronized(mWindowMap) {
2696 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2697 if (wtoken != null) {
2698 Log.w(TAG, "Attempted to add existing app token: " + token);
2699 return;
2700 }
2701 wtoken = new AppWindowToken(token);
2702 wtoken.groupId = groupId;
2703 wtoken.appFullscreen = fullscreen;
2704 wtoken.requestedOrientation = requestedOrientation;
2705 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002706 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 mTokenMap.put(token.asBinder(), wtoken);
2708 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 // Application tokens start out hidden.
2711 wtoken.hidden = true;
2712 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 //dump();
2715 }
2716 }
Romain Guy06882f82009-06-10 13:36:04 -07002717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 public void setAppGroupId(IBinder token, int groupId) {
2719 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2720 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002721 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 }
2723
2724 synchronized(mWindowMap) {
2725 AppWindowToken wtoken = findAppWindowToken(token);
2726 if (wtoken == null) {
2727 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2728 return;
2729 }
2730 wtoken.groupId = groupId;
2731 }
2732 }
Romain Guy06882f82009-06-10 13:36:04 -07002733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 public int getOrientationFromWindowsLocked() {
2735 int pos = mWindows.size() - 1;
2736 while (pos >= 0) {
2737 WindowState wtoken = (WindowState) mWindows.get(pos);
2738 pos--;
2739 if (wtoken.mAppToken != null) {
2740 // We hit an application window. so the orientation will be determined by the
2741 // app window. No point in continuing further.
2742 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2743 }
2744 if (!wtoken.isVisibleLw()) {
2745 continue;
2746 }
2747 int req = wtoken.mAttrs.screenOrientation;
2748 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2749 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2750 continue;
2751 } else {
2752 return req;
2753 }
2754 }
2755 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2756 }
Romain Guy06882f82009-06-10 13:36:04 -07002757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 public int getOrientationFromAppTokensLocked() {
2759 int pos = mAppTokens.size() - 1;
2760 int curGroup = 0;
2761 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002762 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002764 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 while (pos >= 0) {
2766 AppWindowToken wtoken = mAppTokens.get(pos);
2767 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002768 // if we're about to tear down this window and not seek for
2769 // the behind activity, don't use it for orientation
2770 if (!findingBehind
2771 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002772 continue;
2773 }
2774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 if (!haveGroup) {
2776 // We ignore any hidden applications on the top.
2777 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2778 continue;
2779 }
2780 haveGroup = true;
2781 curGroup = wtoken.groupId;
2782 lastOrientation = wtoken.requestedOrientation;
2783 } else if (curGroup != wtoken.groupId) {
2784 // If we have hit a new application group, and the bottom
2785 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002786 // the orientation behind it, and the last app was
2787 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002789 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2790 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 return lastOrientation;
2792 }
2793 }
2794 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002795 // If this application is fullscreen, and didn't explicitly say
2796 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002798 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002799 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002800 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 return or;
2802 }
2803 // If this application has requested an explicit orientation,
2804 // then use it.
2805 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2806 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2807 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2808 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2809 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2810 return or;
2811 }
Owen Lin3413b892009-05-01 17:12:32 -07002812 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 }
2814 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2815 }
Romain Guy06882f82009-06-10 13:36:04 -07002816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002818 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002819 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2820 "updateOrientationFromAppTokens()")) {
2821 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2822 }
2823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 Configuration config;
2825 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002826 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2827 freezeThisOneIfNeeded);
2828 Binder.restoreCallingIdentity(ident);
2829 return config;
2830 }
2831
2832 Configuration updateOrientationFromAppTokensUnchecked(
2833 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2834 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002836 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002837 if (config != null) {
2838 mLayoutNeeded = true;
2839 performLayoutAndPlaceSurfacesLocked();
2840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 return config;
2843 }
Romain Guy06882f82009-06-10 13:36:04 -07002844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 /*
2846 * The orientation is computed from non-application windows first. If none of
2847 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002848 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2850 * android.os.IBinder)
2851 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002852 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002853 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 long ident = Binder.clearCallingIdentity();
2856 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002857 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 if (req != mForcedAppOrientation) {
2860 changed = true;
2861 mForcedAppOrientation = req;
2862 //send a message to Policy indicating orientation change to take
2863 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002864 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 }
Romain Guy06882f82009-06-10 13:36:04 -07002866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 if (changed) {
2868 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002869 WindowManagerPolicy.USE_LAST_ROTATION,
2870 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 if (changed) {
2872 if (freezeThisOneIfNeeded != null) {
2873 AppWindowToken wtoken = findAppWindowToken(
2874 freezeThisOneIfNeeded);
2875 if (wtoken != null) {
2876 startAppFreezingScreenLocked(wtoken,
2877 ActivityInfo.CONFIG_ORIENTATION);
2878 }
2879 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002880 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 }
2882 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002883
2884 // No obvious action we need to take, but if our current
2885 // state mismatches the activity maanager's, update it
2886 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002887 mTempConfiguration.setToDefaults();
2888 if (computeNewConfigurationLocked(mTempConfiguration)) {
2889 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002890 return new Configuration(mTempConfiguration);
2891 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002892 }
2893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 } finally {
2895 Binder.restoreCallingIdentity(ident);
2896 }
Romain Guy06882f82009-06-10 13:36:04 -07002897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 return null;
2899 }
Romain Guy06882f82009-06-10 13:36:04 -07002900
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002901 int computeForcedAppOrientationLocked() {
2902 int req = getOrientationFromWindowsLocked();
2903 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2904 req = getOrientationFromAppTokensLocked();
2905 }
2906 return req;
2907 }
Romain Guy06882f82009-06-10 13:36:04 -07002908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2910 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2911 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002912 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 }
Romain Guy06882f82009-06-10 13:36:04 -07002914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 synchronized(mWindowMap) {
2916 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2917 if (wtoken == null) {
2918 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2919 return;
2920 }
Romain Guy06882f82009-06-10 13:36:04 -07002921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 wtoken.requestedOrientation = requestedOrientation;
2923 }
2924 }
Romain Guy06882f82009-06-10 13:36:04 -07002925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 public int getAppOrientation(IApplicationToken token) {
2927 synchronized(mWindowMap) {
2928 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2929 if (wtoken == null) {
2930 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2931 }
Romain Guy06882f82009-06-10 13:36:04 -07002932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 return wtoken.requestedOrientation;
2934 }
2935 }
Romain Guy06882f82009-06-10 13:36:04 -07002936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2938 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2939 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002940 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 }
2942
2943 synchronized(mWindowMap) {
2944 boolean changed = false;
2945 if (token == null) {
2946 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2947 changed = mFocusedApp != null;
2948 mFocusedApp = null;
2949 mKeyWaiter.tickle();
2950 } else {
2951 AppWindowToken newFocus = findAppWindowToken(token);
2952 if (newFocus == null) {
2953 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2954 return;
2955 }
2956 changed = mFocusedApp != newFocus;
2957 mFocusedApp = newFocus;
2958 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2959 mKeyWaiter.tickle();
2960 }
2961
2962 if (moveFocusNow && changed) {
2963 final long origId = Binder.clearCallingIdentity();
2964 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2965 Binder.restoreCallingIdentity(origId);
2966 }
2967 }
2968 }
2969
2970 public void prepareAppTransition(int transit) {
2971 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2972 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002973 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 }
Romain Guy06882f82009-06-10 13:36:04 -07002975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 synchronized(mWindowMap) {
2977 if (DEBUG_APP_TRANSITIONS) Log.v(
2978 TAG, "Prepare app transition: transit=" + transit
2979 + " mNextAppTransition=" + mNextAppTransition);
2980 if (!mDisplayFrozen) {
2981 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2982 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002983 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2984 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2985 // Opening a new task always supersedes a close for the anim.
2986 mNextAppTransition = transit;
2987 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2988 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2989 // Opening a new activity always supersedes a close for the anim.
2990 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 }
2992 mAppTransitionReady = false;
2993 mAppTransitionTimeout = false;
2994 mStartingIconInTransition = false;
2995 mSkipAppTransitionAnimation = false;
2996 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2997 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2998 5000);
2999 }
3000 }
3001 }
3002
3003 public int getPendingAppTransition() {
3004 return mNextAppTransition;
3005 }
Romain Guy06882f82009-06-10 13:36:04 -07003006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 public void executeAppTransition() {
3008 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3009 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003010 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 }
Romain Guy06882f82009-06-10 13:36:04 -07003012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 synchronized(mWindowMap) {
3014 if (DEBUG_APP_TRANSITIONS) Log.v(
3015 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
3016 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3017 mAppTransitionReady = true;
3018 final long origId = Binder.clearCallingIdentity();
3019 performLayoutAndPlaceSurfacesLocked();
3020 Binder.restoreCallingIdentity(origId);
3021 }
3022 }
3023 }
3024
3025 public void setAppStartingWindow(IBinder token, String pkg,
3026 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3027 IBinder transferFrom, boolean createIfNeeded) {
3028 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3029 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003030 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 }
3032
3033 synchronized(mWindowMap) {
3034 if (DEBUG_STARTING_WINDOW) Log.v(
3035 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3036 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 AppWindowToken wtoken = findAppWindowToken(token);
3039 if (wtoken == null) {
3040 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3041 return;
3042 }
3043
3044 // If the display is frozen, we won't do anything until the
3045 // actual window is displayed so there is no reason to put in
3046 // the starting window.
3047 if (mDisplayFrozen) {
3048 return;
3049 }
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 if (wtoken.startingData != null) {
3052 return;
3053 }
Romain Guy06882f82009-06-10 13:36:04 -07003054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 if (transferFrom != null) {
3056 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3057 if (ttoken != null) {
3058 WindowState startingWindow = ttoken.startingWindow;
3059 if (startingWindow != null) {
3060 if (mStartingIconInTransition) {
3061 // In this case, the starting icon has already
3062 // been displayed, so start letting windows get
3063 // shown immediately without any more transitions.
3064 mSkipAppTransitionAnimation = true;
3065 }
3066 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3067 "Moving existing starting from " + ttoken
3068 + " to " + wtoken);
3069 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 // Transfer the starting window over to the new
3072 // token.
3073 wtoken.startingData = ttoken.startingData;
3074 wtoken.startingView = ttoken.startingView;
3075 wtoken.startingWindow = startingWindow;
3076 ttoken.startingData = null;
3077 ttoken.startingView = null;
3078 ttoken.startingWindow = null;
3079 ttoken.startingMoved = true;
3080 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003081 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 startingWindow.mAppToken = wtoken;
3083 mWindows.remove(startingWindow);
3084 ttoken.windows.remove(startingWindow);
3085 ttoken.allAppWindows.remove(startingWindow);
3086 addWindowToListInOrderLocked(startingWindow, true);
3087 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 // Propagate other interesting state between the
3090 // tokens. If the old token is displayed, we should
3091 // immediately force the new one to be displayed. If
3092 // it is animating, we need to move that animation to
3093 // the new one.
3094 if (ttoken.allDrawn) {
3095 wtoken.allDrawn = true;
3096 }
3097 if (ttoken.firstWindowDrawn) {
3098 wtoken.firstWindowDrawn = true;
3099 }
3100 if (!ttoken.hidden) {
3101 wtoken.hidden = false;
3102 wtoken.hiddenRequested = false;
3103 wtoken.willBeHidden = false;
3104 }
3105 if (wtoken.clientHidden != ttoken.clientHidden) {
3106 wtoken.clientHidden = ttoken.clientHidden;
3107 wtoken.sendAppVisibilityToClients();
3108 }
3109 if (ttoken.animation != null) {
3110 wtoken.animation = ttoken.animation;
3111 wtoken.animating = ttoken.animating;
3112 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3113 ttoken.animation = null;
3114 ttoken.animLayerAdjustment = 0;
3115 wtoken.updateLayers();
3116 ttoken.updateLayers();
3117 }
Romain Guy06882f82009-06-10 13:36:04 -07003118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 mLayoutNeeded = true;
3121 performLayoutAndPlaceSurfacesLocked();
3122 Binder.restoreCallingIdentity(origId);
3123 return;
3124 } else if (ttoken.startingData != null) {
3125 // The previous app was getting ready to show a
3126 // starting window, but hasn't yet done so. Steal it!
3127 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3128 "Moving pending starting from " + ttoken
3129 + " to " + wtoken);
3130 wtoken.startingData = ttoken.startingData;
3131 ttoken.startingData = null;
3132 ttoken.startingMoved = true;
3133 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3134 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3135 // want to process the message ASAP, before any other queued
3136 // messages.
3137 mH.sendMessageAtFrontOfQueue(m);
3138 return;
3139 }
3140 }
3141 }
3142
3143 // There is no existing starting window, and the caller doesn't
3144 // want us to create one, so that's it!
3145 if (!createIfNeeded) {
3146 return;
3147 }
Romain Guy06882f82009-06-10 13:36:04 -07003148
Dianne Hackborn284ac932009-08-28 10:34:25 -07003149 // If this is a translucent or wallpaper window, then don't
3150 // show a starting window -- the current effect (a full-screen
3151 // opaque starting window that fades away to the real contents
3152 // when it is ready) does not work for this.
3153 if (theme != 0) {
3154 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3155 com.android.internal.R.styleable.Window);
3156 if (ent.array.getBoolean(
3157 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3158 return;
3159 }
3160 if (ent.array.getBoolean(
3161 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3162 return;
3163 }
3164 }
3165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 mStartingIconInTransition = true;
3167 wtoken.startingData = new StartingData(
3168 pkg, theme, nonLocalizedLabel,
3169 labelRes, icon);
3170 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3171 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3172 // want to process the message ASAP, before any other queued
3173 // messages.
3174 mH.sendMessageAtFrontOfQueue(m);
3175 }
3176 }
3177
3178 public void setAppWillBeHidden(IBinder token) {
3179 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3180 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003181 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 }
3183
3184 AppWindowToken wtoken;
3185
3186 synchronized(mWindowMap) {
3187 wtoken = findAppWindowToken(token);
3188 if (wtoken == null) {
3189 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3190 return;
3191 }
3192 wtoken.willBeHidden = true;
3193 }
3194 }
Romain Guy06882f82009-06-10 13:36:04 -07003195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3197 boolean visible, int transit, boolean performLayout) {
3198 boolean delayed = false;
3199
3200 if (wtoken.clientHidden == visible) {
3201 wtoken.clientHidden = !visible;
3202 wtoken.sendAppVisibilityToClients();
3203 }
Romain Guy06882f82009-06-10 13:36:04 -07003204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 wtoken.willBeHidden = false;
3206 if (wtoken.hidden == visible) {
3207 final int N = wtoken.allAppWindows.size();
3208 boolean changed = false;
3209 if (DEBUG_APP_TRANSITIONS) Log.v(
3210 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3211 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3216 if (wtoken.animation == sDummyAnimation) {
3217 wtoken.animation = null;
3218 }
3219 applyAnimationLocked(wtoken, lp, transit, visible);
3220 changed = true;
3221 if (wtoken.animation != null) {
3222 delayed = runningAppAnimation = true;
3223 }
3224 }
Romain Guy06882f82009-06-10 13:36:04 -07003225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 for (int i=0; i<N; i++) {
3227 WindowState win = wtoken.allAppWindows.get(i);
3228 if (win == wtoken.startingWindow) {
3229 continue;
3230 }
3231
3232 if (win.isAnimating()) {
3233 delayed = true;
3234 }
Romain Guy06882f82009-06-10 13:36:04 -07003235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3237 //win.dump(" ");
3238 if (visible) {
3239 if (!win.isVisibleNow()) {
3240 if (!runningAppAnimation) {
3241 applyAnimationLocked(win,
3242 WindowManagerPolicy.TRANSIT_ENTER, true);
3243 }
3244 changed = true;
3245 }
3246 } else if (win.isVisibleNow()) {
3247 if (!runningAppAnimation) {
3248 applyAnimationLocked(win,
3249 WindowManagerPolicy.TRANSIT_EXIT, false);
3250 }
3251 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3252 KeyWaiter.RETURN_NOTHING);
3253 changed = true;
3254 }
3255 }
3256
3257 wtoken.hidden = wtoken.hiddenRequested = !visible;
3258 if (!visible) {
3259 unsetAppFreezingScreenLocked(wtoken, true, true);
3260 } else {
3261 // If we are being set visible, and the starting window is
3262 // not yet displayed, then make sure it doesn't get displayed.
3263 WindowState swin = wtoken.startingWindow;
3264 if (swin != null && (swin.mDrawPending
3265 || swin.mCommitDrawPending)) {
3266 swin.mPolicyVisibility = false;
3267 swin.mPolicyVisibilityAfterAnim = false;
3268 }
3269 }
Romain Guy06882f82009-06-10 13:36:04 -07003270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3272 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3273 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 if (changed && performLayout) {
3276 mLayoutNeeded = true;
3277 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 performLayoutAndPlaceSurfacesLocked();
3279 }
3280 }
3281
3282 if (wtoken.animation != null) {
3283 delayed = true;
3284 }
Romain Guy06882f82009-06-10 13:36:04 -07003285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 return delayed;
3287 }
3288
3289 public void setAppVisibility(IBinder token, boolean visible) {
3290 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3291 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003292 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 }
3294
3295 AppWindowToken wtoken;
3296
3297 synchronized(mWindowMap) {
3298 wtoken = findAppWindowToken(token);
3299 if (wtoken == null) {
3300 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3301 return;
3302 }
3303
3304 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3305 RuntimeException e = new RuntimeException();
3306 e.fillInStackTrace();
3307 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3308 + "): mNextAppTransition=" + mNextAppTransition
3309 + " hidden=" + wtoken.hidden
3310 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3311 }
Romain Guy06882f82009-06-10 13:36:04 -07003312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 // If we are preparing an app transition, then delay changing
3314 // the visibility of this token until we execute that transition.
3315 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3316 // Already in requested state, don't do anything more.
3317 if (wtoken.hiddenRequested != visible) {
3318 return;
3319 }
3320 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 if (DEBUG_APP_TRANSITIONS) Log.v(
3323 TAG, "Setting dummy animation on: " + wtoken);
3324 wtoken.setDummyAnimation();
3325 mOpeningApps.remove(wtoken);
3326 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003327 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 wtoken.inPendingTransaction = true;
3329 if (visible) {
3330 mOpeningApps.add(wtoken);
3331 wtoken.allDrawn = false;
3332 wtoken.startingDisplayed = false;
3333 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003334 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 if (wtoken.clientHidden) {
3337 // In the case where we are making an app visible
3338 // but holding off for a transition, we still need
3339 // to tell the client to make its windows visible so
3340 // they get drawn. Otherwise, we will wait on
3341 // performing the transition until all windows have
3342 // been drawn, they never will be, and we are sad.
3343 wtoken.clientHidden = false;
3344 wtoken.sendAppVisibilityToClients();
3345 }
3346 } else {
3347 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003348 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 }
3350 return;
3351 }
Romain Guy06882f82009-06-10 13:36:04 -07003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 final long origId = Binder.clearCallingIdentity();
3354 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3355 wtoken.updateReportedVisibilityLocked();
3356 Binder.restoreCallingIdentity(origId);
3357 }
3358 }
3359
3360 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3361 boolean unfreezeSurfaceNow, boolean force) {
3362 if (wtoken.freezingScreen) {
3363 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3364 + " force=" + force);
3365 final int N = wtoken.allAppWindows.size();
3366 boolean unfrozeWindows = false;
3367 for (int i=0; i<N; i++) {
3368 WindowState w = wtoken.allAppWindows.get(i);
3369 if (w.mAppFreezing) {
3370 w.mAppFreezing = false;
3371 if (w.mSurface != null && !w.mOrientationChanging) {
3372 w.mOrientationChanging = true;
3373 }
3374 unfrozeWindows = true;
3375 }
3376 }
3377 if (force || unfrozeWindows) {
3378 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3379 wtoken.freezingScreen = false;
3380 mAppsFreezingScreen--;
3381 }
3382 if (unfreezeSurfaceNow) {
3383 if (unfrozeWindows) {
3384 mLayoutNeeded = true;
3385 performLayoutAndPlaceSurfacesLocked();
3386 }
3387 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3388 stopFreezingDisplayLocked();
3389 }
3390 }
3391 }
3392 }
Romain Guy06882f82009-06-10 13:36:04 -07003393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3395 int configChanges) {
3396 if (DEBUG_ORIENTATION) {
3397 RuntimeException e = new RuntimeException();
3398 e.fillInStackTrace();
3399 Log.i(TAG, "Set freezing of " + wtoken.appToken
3400 + ": hidden=" + wtoken.hidden + " freezing="
3401 + wtoken.freezingScreen, e);
3402 }
3403 if (!wtoken.hiddenRequested) {
3404 if (!wtoken.freezingScreen) {
3405 wtoken.freezingScreen = true;
3406 mAppsFreezingScreen++;
3407 if (mAppsFreezingScreen == 1) {
3408 startFreezingDisplayLocked();
3409 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3410 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3411 5000);
3412 }
3413 }
3414 final int N = wtoken.allAppWindows.size();
3415 for (int i=0; i<N; i++) {
3416 WindowState w = wtoken.allAppWindows.get(i);
3417 w.mAppFreezing = true;
3418 }
3419 }
3420 }
Romain Guy06882f82009-06-10 13:36:04 -07003421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 public void startAppFreezingScreen(IBinder token, int configChanges) {
3423 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3424 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003425 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 }
3427
3428 synchronized(mWindowMap) {
3429 if (configChanges == 0 && !mDisplayFrozen) {
3430 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3431 return;
3432 }
Romain Guy06882f82009-06-10 13:36:04 -07003433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 AppWindowToken wtoken = findAppWindowToken(token);
3435 if (wtoken == null || wtoken.appToken == null) {
3436 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3437 return;
3438 }
3439 final long origId = Binder.clearCallingIdentity();
3440 startAppFreezingScreenLocked(wtoken, configChanges);
3441 Binder.restoreCallingIdentity(origId);
3442 }
3443 }
Romain Guy06882f82009-06-10 13:36:04 -07003444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 public void stopAppFreezingScreen(IBinder token, boolean force) {
3446 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3447 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003448 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 }
3450
3451 synchronized(mWindowMap) {
3452 AppWindowToken wtoken = findAppWindowToken(token);
3453 if (wtoken == null || wtoken.appToken == null) {
3454 return;
3455 }
3456 final long origId = Binder.clearCallingIdentity();
3457 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3458 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3459 unsetAppFreezingScreenLocked(wtoken, true, force);
3460 Binder.restoreCallingIdentity(origId);
3461 }
3462 }
Romain Guy06882f82009-06-10 13:36:04 -07003463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 public void removeAppToken(IBinder token) {
3465 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3466 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003467 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 }
3469
3470 AppWindowToken wtoken = null;
3471 AppWindowToken startingToken = null;
3472 boolean delayed = false;
3473
3474 final long origId = Binder.clearCallingIdentity();
3475 synchronized(mWindowMap) {
3476 WindowToken basewtoken = mTokenMap.remove(token);
3477 mTokenList.remove(basewtoken);
3478 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3479 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3480 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3481 wtoken.inPendingTransaction = false;
3482 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003483 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 if (mClosingApps.contains(wtoken)) {
3485 delayed = true;
3486 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3487 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003488 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 delayed = true;
3490 }
3491 if (DEBUG_APP_TRANSITIONS) Log.v(
3492 TAG, "Removing app " + wtoken + " delayed=" + delayed
3493 + " animation=" + wtoken.animation
3494 + " animating=" + wtoken.animating);
3495 if (delayed) {
3496 // set the token aside because it has an active animation to be finished
3497 mExitingAppTokens.add(wtoken);
3498 }
3499 mAppTokens.remove(wtoken);
3500 wtoken.removed = true;
3501 if (wtoken.startingData != null) {
3502 startingToken = wtoken;
3503 }
3504 unsetAppFreezingScreenLocked(wtoken, true, true);
3505 if (mFocusedApp == wtoken) {
3506 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3507 mFocusedApp = null;
3508 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3509 mKeyWaiter.tickle();
3510 }
3511 } else {
3512 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3513 }
Romain Guy06882f82009-06-10 13:36:04 -07003514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 if (!delayed && wtoken != null) {
3516 wtoken.updateReportedVisibilityLocked();
3517 }
3518 }
3519 Binder.restoreCallingIdentity(origId);
3520
3521 if (startingToken != null) {
3522 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3523 + startingToken + ": app token removed");
3524 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3525 mH.sendMessage(m);
3526 }
3527 }
3528
3529 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3530 final int NW = token.windows.size();
3531 for (int i=0; i<NW; i++) {
3532 WindowState win = token.windows.get(i);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003533 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 mWindows.remove(win);
3535 int j = win.mChildWindows.size();
3536 while (j > 0) {
3537 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003538 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3539 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3540 "Tmp removing child window " + cwin);
3541 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 }
3543 }
3544 return NW > 0;
3545 }
3546
3547 void dumpAppTokensLocked() {
3548 for (int i=mAppTokens.size()-1; i>=0; i--) {
3549 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3550 }
3551 }
Romain Guy06882f82009-06-10 13:36:04 -07003552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 void dumpWindowsLocked() {
3554 for (int i=mWindows.size()-1; i>=0; i--) {
3555 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3556 }
3557 }
Romain Guy06882f82009-06-10 13:36:04 -07003558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 private int findWindowOffsetLocked(int tokenPos) {
3560 final int NW = mWindows.size();
3561
3562 if (tokenPos >= mAppTokens.size()) {
3563 int i = NW;
3564 while (i > 0) {
3565 i--;
3566 WindowState win = (WindowState)mWindows.get(i);
3567 if (win.getAppToken() != null) {
3568 return i+1;
3569 }
3570 }
3571 }
3572
3573 while (tokenPos > 0) {
3574 // Find the first app token below the new position that has
3575 // a window displayed.
3576 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3577 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3578 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003579 if (wtoken.sendingToBottom) {
3580 if (DEBUG_REORDER) Log.v(TAG,
3581 "Skipping token -- currently sending to bottom");
3582 tokenPos--;
3583 continue;
3584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 int i = wtoken.windows.size();
3586 while (i > 0) {
3587 i--;
3588 WindowState win = wtoken.windows.get(i);
3589 int j = win.mChildWindows.size();
3590 while (j > 0) {
3591 j--;
3592 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003593 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 for (int pos=NW-1; pos>=0; pos--) {
3595 if (mWindows.get(pos) == cwin) {
3596 if (DEBUG_REORDER) Log.v(TAG,
3597 "Found child win @" + (pos+1));
3598 return pos+1;
3599 }
3600 }
3601 }
3602 }
3603 for (int pos=NW-1; pos>=0; pos--) {
3604 if (mWindows.get(pos) == win) {
3605 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3606 return pos+1;
3607 }
3608 }
3609 }
3610 tokenPos--;
3611 }
3612
3613 return 0;
3614 }
3615
3616 private final int reAddWindowLocked(int index, WindowState win) {
3617 final int NCW = win.mChildWindows.size();
3618 boolean added = false;
3619 for (int j=0; j<NCW; j++) {
3620 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3621 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003622 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3623 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 mWindows.add(index, win);
3625 index++;
3626 added = true;
3627 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003628 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3629 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 mWindows.add(index, cwin);
3631 index++;
3632 }
3633 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003634 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3635 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 mWindows.add(index, win);
3637 index++;
3638 }
3639 return index;
3640 }
Romain Guy06882f82009-06-10 13:36:04 -07003641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3643 final int NW = token.windows.size();
3644 for (int i=0; i<NW; i++) {
3645 index = reAddWindowLocked(index, token.windows.get(i));
3646 }
3647 return index;
3648 }
3649
3650 public void moveAppToken(int index, IBinder token) {
3651 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3652 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003653 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 }
3655
3656 synchronized(mWindowMap) {
3657 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3658 if (DEBUG_REORDER) dumpAppTokensLocked();
3659 final AppWindowToken wtoken = findAppWindowToken(token);
3660 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3661 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3662 + token + " (" + wtoken + ")");
3663 return;
3664 }
3665 mAppTokens.add(index, wtoken);
3666 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3667 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 final long origId = Binder.clearCallingIdentity();
3670 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3671 if (DEBUG_REORDER) dumpWindowsLocked();
3672 if (tmpRemoveAppWindowsLocked(wtoken)) {
3673 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3674 if (DEBUG_REORDER) dumpWindowsLocked();
3675 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3676 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3677 if (DEBUG_REORDER) dumpWindowsLocked();
3678 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 mLayoutNeeded = true;
3680 performLayoutAndPlaceSurfacesLocked();
3681 }
3682 Binder.restoreCallingIdentity(origId);
3683 }
3684 }
3685
3686 private void removeAppTokensLocked(List<IBinder> tokens) {
3687 // XXX This should be done more efficiently!
3688 // (take advantage of the fact that both lists should be
3689 // ordered in the same way.)
3690 int N = tokens.size();
3691 for (int i=0; i<N; i++) {
3692 IBinder token = tokens.get(i);
3693 final AppWindowToken wtoken = findAppWindowToken(token);
3694 if (!mAppTokens.remove(wtoken)) {
3695 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3696 + token + " (" + wtoken + ")");
3697 i--;
3698 N--;
3699 }
3700 }
3701 }
3702
Dianne Hackborna8f60182009-09-01 19:01:50 -07003703 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3704 boolean updateFocusAndLayout) {
3705 // First remove all of the windows from the list.
3706 tmpRemoveAppWindowsLocked(wtoken);
3707
3708 // Where to start adding?
3709 int pos = findWindowOffsetLocked(tokenPos);
3710
3711 // And now add them back at the correct place.
3712 pos = reAddAppWindowsLocked(pos, wtoken);
3713
3714 if (updateFocusAndLayout) {
3715 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3716 assignLayersLocked();
3717 }
3718 mLayoutNeeded = true;
3719 performLayoutAndPlaceSurfacesLocked();
3720 }
3721 }
3722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3724 // First remove all of the windows from the list.
3725 final int N = tokens.size();
3726 int i;
3727 for (i=0; i<N; i++) {
3728 WindowToken token = mTokenMap.get(tokens.get(i));
3729 if (token != null) {
3730 tmpRemoveAppWindowsLocked(token);
3731 }
3732 }
3733
3734 // Where to start adding?
3735 int pos = findWindowOffsetLocked(tokenPos);
3736
3737 // And now add them back at the correct place.
3738 for (i=0; i<N; i++) {
3739 WindowToken token = mTokenMap.get(tokens.get(i));
3740 if (token != null) {
3741 pos = reAddAppWindowsLocked(pos, token);
3742 }
3743 }
3744
Dianne Hackborna8f60182009-09-01 19:01:50 -07003745 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3746 assignLayersLocked();
3747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 mLayoutNeeded = true;
3749 performLayoutAndPlaceSurfacesLocked();
3750
3751 //dump();
3752 }
3753
3754 public void moveAppTokensToTop(List<IBinder> tokens) {
3755 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3756 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003757 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 }
3759
3760 final long origId = Binder.clearCallingIdentity();
3761 synchronized(mWindowMap) {
3762 removeAppTokensLocked(tokens);
3763 final int N = tokens.size();
3764 for (int i=0; i<N; i++) {
3765 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3766 if (wt != null) {
3767 mAppTokens.add(wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003768 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3769 mToTopApps.remove(wt);
3770 mToBottomApps.remove(wt);
3771 mToTopApps.add(wt);
3772 wt.sendingToBottom = false;
3773 wt.sendingToTop = true;
3774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
3776 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003777
3778 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3779 moveAppWindowsLocked(tokens, mAppTokens.size());
3780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 }
3782 Binder.restoreCallingIdentity(origId);
3783 }
3784
3785 public void moveAppTokensToBottom(List<IBinder> tokens) {
3786 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3787 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003788 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 }
3790
3791 final long origId = Binder.clearCallingIdentity();
3792 synchronized(mWindowMap) {
3793 removeAppTokensLocked(tokens);
3794 final int N = tokens.size();
3795 int pos = 0;
3796 for (int i=0; i<N; i++) {
3797 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3798 if (wt != null) {
3799 mAppTokens.add(pos, wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003800 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3801 mToTopApps.remove(wt);
3802 mToBottomApps.remove(wt);
3803 mToBottomApps.add(i, wt);
3804 wt.sendingToTop = false;
3805 wt.sendingToBottom = true;
3806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 pos++;
3808 }
3809 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003810
3811 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3812 moveAppWindowsLocked(tokens, 0);
3813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 }
3815 Binder.restoreCallingIdentity(origId);
3816 }
3817
3818 // -------------------------------------------------------------
3819 // Misc IWindowSession methods
3820 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 public void disableKeyguard(IBinder token, String tag) {
3823 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3824 != PackageManager.PERMISSION_GRANTED) {
3825 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3826 }
3827 mKeyguardDisabled.acquire(token, tag);
3828 }
3829
3830 public void reenableKeyguard(IBinder token) {
3831 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3832 != PackageManager.PERMISSION_GRANTED) {
3833 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3834 }
3835 synchronized (mKeyguardDisabled) {
3836 mKeyguardDisabled.release(token);
3837
3838 if (!mKeyguardDisabled.isAcquired()) {
3839 // if we are the last one to reenable the keyguard wait until
3840 // we have actaully finished reenabling until returning
3841 mWaitingUntilKeyguardReenabled = true;
3842 while (mWaitingUntilKeyguardReenabled) {
3843 try {
3844 mKeyguardDisabled.wait();
3845 } catch (InterruptedException e) {
3846 Thread.currentThread().interrupt();
3847 }
3848 }
3849 }
3850 }
3851 }
3852
3853 /**
3854 * @see android.app.KeyguardManager#exitKeyguardSecurely
3855 */
3856 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3857 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3858 != PackageManager.PERMISSION_GRANTED) {
3859 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3860 }
3861 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3862 public void onKeyguardExitResult(boolean success) {
3863 try {
3864 callback.onKeyguardExitResult(success);
3865 } catch (RemoteException e) {
3866 // Client has died, we don't care.
3867 }
3868 }
3869 });
3870 }
3871
3872 public boolean inKeyguardRestrictedInputMode() {
3873 return mPolicy.inKeyguardRestrictedKeyInputMode();
3874 }
Romain Guy06882f82009-06-10 13:36:04 -07003875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 static float fixScale(float scale) {
3877 if (scale < 0) scale = 0;
3878 else if (scale > 20) scale = 20;
3879 return Math.abs(scale);
3880 }
Romain Guy06882f82009-06-10 13:36:04 -07003881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 public void setAnimationScale(int which, float scale) {
3883 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3884 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003885 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 }
3887
3888 if (scale < 0) scale = 0;
3889 else if (scale > 20) scale = 20;
3890 scale = Math.abs(scale);
3891 switch (which) {
3892 case 0: mWindowAnimationScale = fixScale(scale); break;
3893 case 1: mTransitionAnimationScale = fixScale(scale); break;
3894 }
Romain Guy06882f82009-06-10 13:36:04 -07003895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 // Persist setting
3897 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3898 }
Romain Guy06882f82009-06-10 13:36:04 -07003899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 public void setAnimationScales(float[] scales) {
3901 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3902 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003903 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
3905
3906 if (scales != null) {
3907 if (scales.length >= 1) {
3908 mWindowAnimationScale = fixScale(scales[0]);
3909 }
3910 if (scales.length >= 2) {
3911 mTransitionAnimationScale = fixScale(scales[1]);
3912 }
3913 }
Romain Guy06882f82009-06-10 13:36:04 -07003914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 // Persist setting
3916 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3917 }
Romain Guy06882f82009-06-10 13:36:04 -07003918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 public float getAnimationScale(int which) {
3920 switch (which) {
3921 case 0: return mWindowAnimationScale;
3922 case 1: return mTransitionAnimationScale;
3923 }
3924 return 0;
3925 }
Romain Guy06882f82009-06-10 13:36:04 -07003926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 public float[] getAnimationScales() {
3928 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3929 }
Romain Guy06882f82009-06-10 13:36:04 -07003930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 public int getSwitchState(int sw) {
3932 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3933 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003934 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
3936 return KeyInputQueue.getSwitchState(sw);
3937 }
Romain Guy06882f82009-06-10 13:36:04 -07003938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 public int getSwitchStateForDevice(int devid, int sw) {
3940 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3941 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003942 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 }
3944 return KeyInputQueue.getSwitchState(devid, sw);
3945 }
Romain Guy06882f82009-06-10 13:36:04 -07003946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 public int getScancodeState(int sw) {
3948 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3949 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003950 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 }
3952 return KeyInputQueue.getScancodeState(sw);
3953 }
Romain Guy06882f82009-06-10 13:36:04 -07003954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 public int getScancodeStateForDevice(int devid, int sw) {
3956 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3957 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003958 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
3960 return KeyInputQueue.getScancodeState(devid, sw);
3961 }
Romain Guy06882f82009-06-10 13:36:04 -07003962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 public int getKeycodeState(int sw) {
3964 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3965 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003966 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 }
3968 return KeyInputQueue.getKeycodeState(sw);
3969 }
Romain Guy06882f82009-06-10 13:36:04 -07003970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 public int getKeycodeStateForDevice(int devid, int sw) {
3972 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3973 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003974 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 }
3976 return KeyInputQueue.getKeycodeState(devid, sw);
3977 }
Romain Guy06882f82009-06-10 13:36:04 -07003978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3980 return KeyInputQueue.hasKeys(keycodes, keyExists);
3981 }
Romain Guy06882f82009-06-10 13:36:04 -07003982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 public void enableScreenAfterBoot() {
3984 synchronized(mWindowMap) {
3985 if (mSystemBooted) {
3986 return;
3987 }
3988 mSystemBooted = true;
3989 }
Romain Guy06882f82009-06-10 13:36:04 -07003990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 performEnableScreen();
3992 }
Romain Guy06882f82009-06-10 13:36:04 -07003993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 public void enableScreenIfNeededLocked() {
3995 if (mDisplayEnabled) {
3996 return;
3997 }
3998 if (!mSystemBooted) {
3999 return;
4000 }
4001 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4002 }
Romain Guy06882f82009-06-10 13:36:04 -07004003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 public void performEnableScreen() {
4005 synchronized(mWindowMap) {
4006 if (mDisplayEnabled) {
4007 return;
4008 }
4009 if (!mSystemBooted) {
4010 return;
4011 }
Romain Guy06882f82009-06-10 13:36:04 -07004012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 // Don't enable the screen until all existing windows
4014 // have been drawn.
4015 final int N = mWindows.size();
4016 for (int i=0; i<N; i++) {
4017 WindowState w = (WindowState)mWindows.get(i);
4018 if (w.isVisibleLw() && !w.isDisplayedLw()) {
4019 return;
4020 }
4021 }
Romain Guy06882f82009-06-10 13:36:04 -07004022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 mDisplayEnabled = true;
4024 if (false) {
4025 Log.i(TAG, "ENABLING SCREEN!");
4026 StringWriter sw = new StringWriter();
4027 PrintWriter pw = new PrintWriter(sw);
4028 this.dump(null, pw, null);
4029 Log.i(TAG, sw.toString());
4030 }
4031 try {
4032 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4033 if (surfaceFlinger != null) {
4034 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4035 Parcel data = Parcel.obtain();
4036 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4037 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4038 data, null, 0);
4039 data.recycle();
4040 }
4041 } catch (RemoteException ex) {
4042 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4043 }
4044 }
Romain Guy06882f82009-06-10 13:36:04 -07004045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004049 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4050 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 }
Romain Guy06882f82009-06-10 13:36:04 -07004052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 public void setInTouchMode(boolean mode) {
4054 synchronized(mWindowMap) {
4055 mInTouchMode = mode;
4056 }
4057 }
4058
Romain Guy06882f82009-06-10 13:36:04 -07004059 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004060 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004062 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004063 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 }
4065
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004066 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 }
Romain Guy06882f82009-06-10 13:36:04 -07004068
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004069 public void setRotationUnchecked(int rotation,
4070 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 if(DEBUG_ORIENTATION) Log.v(TAG,
4072 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 long origId = Binder.clearCallingIdentity();
4075 boolean changed;
4076 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004077 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
Romain Guy06882f82009-06-10 13:36:04 -07004079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 if (changed) {
4081 sendNewConfiguration();
4082 synchronized(mWindowMap) {
4083 mLayoutNeeded = true;
4084 performLayoutAndPlaceSurfacesLocked();
4085 }
4086 } else if (alwaysSendConfiguration) {
4087 //update configuration ignoring orientation change
4088 sendNewConfiguration();
4089 }
Romain Guy06882f82009-06-10 13:36:04 -07004090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 Binder.restoreCallingIdentity(origId);
4092 }
Romain Guy06882f82009-06-10 13:36:04 -07004093
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004094 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 boolean changed;
4096 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4097 rotation = mRequestedRotation;
4098 } else {
4099 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004100 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 }
4102 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004103 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 mRotation, mDisplayEnabled);
4105 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4106 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004109 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 "Rotation changed to " + rotation
4111 + " from " + mRotation
4112 + " (forceApp=" + mForcedAppOrientation
4113 + ", req=" + mRequestedRotation + ")");
4114 mRotation = rotation;
4115 mWindowsFreezingScreen = true;
4116 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4117 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4118 2000);
4119 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004120 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 mQueue.setOrientation(rotation);
4122 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004123 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 }
4125 for (int i=mWindows.size()-1; i>=0; i--) {
4126 WindowState w = (WindowState)mWindows.get(i);
4127 if (w.mSurface != null) {
4128 w.mOrientationChanging = true;
4129 }
4130 }
4131 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4132 try {
4133 mRotationWatchers.get(i).onRotationChanged(rotation);
4134 } catch (RemoteException e) {
4135 }
4136 }
4137 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 return changed;
4140 }
Romain Guy06882f82009-06-10 13:36:04 -07004141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 public int getRotation() {
4143 return mRotation;
4144 }
4145
4146 public int watchRotation(IRotationWatcher watcher) {
4147 final IBinder watcherBinder = watcher.asBinder();
4148 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4149 public void binderDied() {
4150 synchronized (mWindowMap) {
4151 for (int i=0; i<mRotationWatchers.size(); i++) {
4152 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004153 IRotationWatcher removed = mRotationWatchers.remove(i);
4154 if (removed != null) {
4155 removed.asBinder().unlinkToDeath(this, 0);
4156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 i--;
4158 }
4159 }
4160 }
4161 }
4162 };
Romain Guy06882f82009-06-10 13:36:04 -07004163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 synchronized (mWindowMap) {
4165 try {
4166 watcher.asBinder().linkToDeath(dr, 0);
4167 mRotationWatchers.add(watcher);
4168 } catch (RemoteException e) {
4169 // Client died, no cleanup needed.
4170 }
Romain Guy06882f82009-06-10 13:36:04 -07004171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 return mRotation;
4173 }
4174 }
4175
4176 /**
4177 * Starts the view server on the specified port.
4178 *
4179 * @param port The port to listener to.
4180 *
4181 * @return True if the server was successfully started, false otherwise.
4182 *
4183 * @see com.android.server.ViewServer
4184 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4185 */
4186 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004187 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 return false;
4189 }
4190
4191 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4192 return false;
4193 }
4194
4195 if (port < 1024) {
4196 return false;
4197 }
4198
4199 if (mViewServer != null) {
4200 if (!mViewServer.isRunning()) {
4201 try {
4202 return mViewServer.start();
4203 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004204 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 }
4206 }
4207 return false;
4208 }
4209
4210 try {
4211 mViewServer = new ViewServer(this, port);
4212 return mViewServer.start();
4213 } catch (IOException e) {
4214 Log.w(TAG, "View server did not start");
4215 }
4216 return false;
4217 }
4218
Romain Guy06882f82009-06-10 13:36:04 -07004219 private boolean isSystemSecure() {
4220 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4221 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4222 }
4223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 /**
4225 * Stops the view server if it exists.
4226 *
4227 * @return True if the server stopped, false if it wasn't started or
4228 * couldn't be stopped.
4229 *
4230 * @see com.android.server.ViewServer
4231 */
4232 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004233 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 return false;
4235 }
4236
4237 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4238 return false;
4239 }
4240
4241 if (mViewServer != null) {
4242 return mViewServer.stop();
4243 }
4244 return false;
4245 }
4246
4247 /**
4248 * Indicates whether the view server is running.
4249 *
4250 * @return True if the server is running, false otherwise.
4251 *
4252 * @see com.android.server.ViewServer
4253 */
4254 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004255 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 return false;
4257 }
4258
4259 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4260 return false;
4261 }
4262
4263 return mViewServer != null && mViewServer.isRunning();
4264 }
4265
4266 /**
4267 * Lists all availble windows in the system. The listing is written in the
4268 * specified Socket's output stream with the following syntax:
4269 * windowHashCodeInHexadecimal windowName
4270 * Each line of the ouput represents a different window.
4271 *
4272 * @param client The remote client to send the listing to.
4273 * @return False if an error occured, true otherwise.
4274 */
4275 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004276 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 return false;
4278 }
4279
4280 boolean result = true;
4281
4282 Object[] windows;
4283 synchronized (mWindowMap) {
4284 windows = new Object[mWindows.size()];
4285 //noinspection unchecked
4286 windows = mWindows.toArray(windows);
4287 }
4288
4289 BufferedWriter out = null;
4290
4291 // Any uncaught exception will crash the system process
4292 try {
4293 OutputStream clientStream = client.getOutputStream();
4294 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4295
4296 final int count = windows.length;
4297 for (int i = 0; i < count; i++) {
4298 final WindowState w = (WindowState) windows[i];
4299 out.write(Integer.toHexString(System.identityHashCode(w)));
4300 out.write(' ');
4301 out.append(w.mAttrs.getTitle());
4302 out.write('\n');
4303 }
4304
4305 out.write("DONE.\n");
4306 out.flush();
4307 } catch (Exception e) {
4308 result = false;
4309 } finally {
4310 if (out != null) {
4311 try {
4312 out.close();
4313 } catch (IOException e) {
4314 result = false;
4315 }
4316 }
4317 }
4318
4319 return result;
4320 }
4321
4322 /**
4323 * Sends a command to a target window. The result of the command, if any, will be
4324 * written in the output stream of the specified socket.
4325 *
4326 * The parameters must follow this syntax:
4327 * windowHashcode extra
4328 *
4329 * Where XX is the length in characeters of the windowTitle.
4330 *
4331 * The first parameter is the target window. The window with the specified hashcode
4332 * will be the target. If no target can be found, nothing happens. The extra parameters
4333 * will be delivered to the target window and as parameters to the command itself.
4334 *
4335 * @param client The remote client to sent the result, if any, to.
4336 * @param command The command to execute.
4337 * @param parameters The command parameters.
4338 *
4339 * @return True if the command was successfully delivered, false otherwise. This does
4340 * not indicate whether the command itself was successful.
4341 */
4342 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004343 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 return false;
4345 }
4346
4347 boolean success = true;
4348 Parcel data = null;
4349 Parcel reply = null;
4350
4351 // Any uncaught exception will crash the system process
4352 try {
4353 // Find the hashcode of the window
4354 int index = parameters.indexOf(' ');
4355 if (index == -1) {
4356 index = parameters.length();
4357 }
4358 final String code = parameters.substring(0, index);
4359 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4360
4361 // Extract the command's parameter after the window description
4362 if (index < parameters.length()) {
4363 parameters = parameters.substring(index + 1);
4364 } else {
4365 parameters = "";
4366 }
4367
4368 final WindowManagerService.WindowState window = findWindow(hashCode);
4369 if (window == null) {
4370 return false;
4371 }
4372
4373 data = Parcel.obtain();
4374 data.writeInterfaceToken("android.view.IWindow");
4375 data.writeString(command);
4376 data.writeString(parameters);
4377 data.writeInt(1);
4378 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4379
4380 reply = Parcel.obtain();
4381
4382 final IBinder binder = window.mClient.asBinder();
4383 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4384 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4385
4386 reply.readException();
4387
4388 } catch (Exception e) {
4389 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4390 success = false;
4391 } finally {
4392 if (data != null) {
4393 data.recycle();
4394 }
4395 if (reply != null) {
4396 reply.recycle();
4397 }
4398 }
4399
4400 return success;
4401 }
4402
4403 private WindowState findWindow(int hashCode) {
4404 if (hashCode == -1) {
4405 return getFocusedWindow();
4406 }
4407
4408 synchronized (mWindowMap) {
4409 final ArrayList windows = mWindows;
4410 final int count = windows.size();
4411
4412 for (int i = 0; i < count; i++) {
4413 WindowState w = (WindowState) windows.get(i);
4414 if (System.identityHashCode(w) == hashCode) {
4415 return w;
4416 }
4417 }
4418 }
4419
4420 return null;
4421 }
4422
4423 /*
4424 * Instruct the Activity Manager to fetch the current configuration and broadcast
4425 * that to config-changed listeners if appropriate.
4426 */
4427 void sendNewConfiguration() {
4428 try {
4429 mActivityManager.updateConfiguration(null);
4430 } catch (RemoteException e) {
4431 }
4432 }
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 public Configuration computeNewConfiguration() {
4435 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004436 return computeNewConfigurationLocked();
4437 }
4438 }
Romain Guy06882f82009-06-10 13:36:04 -07004439
Dianne Hackbornc485a602009-03-24 22:39:49 -07004440 Configuration computeNewConfigurationLocked() {
4441 Configuration config = new Configuration();
4442 if (!computeNewConfigurationLocked(config)) {
4443 return null;
4444 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004445 return config;
4446 }
Romain Guy06882f82009-06-10 13:36:04 -07004447
Dianne Hackbornc485a602009-03-24 22:39:49 -07004448 boolean computeNewConfigurationLocked(Configuration config) {
4449 if (mDisplay == null) {
4450 return false;
4451 }
4452 mQueue.getInputConfiguration(config);
4453 final int dw = mDisplay.getWidth();
4454 final int dh = mDisplay.getHeight();
4455 int orientation = Configuration.ORIENTATION_SQUARE;
4456 if (dw < dh) {
4457 orientation = Configuration.ORIENTATION_PORTRAIT;
4458 } else if (dw > dh) {
4459 orientation = Configuration.ORIENTATION_LANDSCAPE;
4460 }
4461 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004462
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004463 DisplayMetrics dm = new DisplayMetrics();
4464 mDisplay.getMetrics(dm);
4465 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4466
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004467 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004468 // Note we only do this once because at this point we don't
4469 // expect the screen to change in this way at runtime, and want
4470 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004471 int longSize = dw;
4472 int shortSize = dh;
4473 if (longSize < shortSize) {
4474 int tmp = longSize;
4475 longSize = shortSize;
4476 shortSize = tmp;
4477 }
4478 longSize = (int)(longSize/dm.density);
4479 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004480
Dianne Hackborn723738c2009-06-25 19:48:04 -07004481 // These semi-magic numbers define our compatibility modes for
4482 // applications with different screens. Don't change unless you
4483 // make sure to test lots and lots of apps!
4484 if (longSize < 470) {
4485 // This is shorter than an HVGA normal density screen (which
4486 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004487 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4488 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004489 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004490 // Is this a large screen?
4491 if (longSize > 640 && shortSize >= 480) {
4492 // VGA or larger screens at medium density are the point
4493 // at which we consider it to be a large screen.
4494 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4495 } else {
4496 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4497
4498 // If this screen is wider than normal HVGA, or taller
4499 // than FWVGA, then for old apps we want to run in size
4500 // compatibility mode.
4501 if (shortSize > 321 || longSize > 570) {
4502 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4503 }
4504 }
4505
4506 // Is this a long screen?
4507 if (((longSize*3)/5) >= (shortSize-1)) {
4508 // Anything wider than WVGA (5:3) is considering to be long.
4509 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4510 } else {
4511 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4512 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004513 }
4514 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004515 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004516
Dianne Hackbornc485a602009-03-24 22:39:49 -07004517 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4518 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4519 mPolicy.adjustConfigurationLw(config);
4520 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
Romain Guy06882f82009-06-10 13:36:04 -07004522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 // -------------------------------------------------------------
4524 // Input Events and Focus Management
4525 // -------------------------------------------------------------
4526
4527 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004528 long curTime = SystemClock.uptimeMillis();
4529
Michael Chane10de972009-05-18 11:24:50 -07004530 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004531 if (mLastTouchEventType == eventType &&
4532 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4533 return;
4534 }
4535 mLastUserActivityCallTime = curTime;
4536 mLastTouchEventType = eventType;
4537 }
4538
4539 if (targetWin == null
4540 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4541 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 }
4543 }
4544
4545 // tells if it's a cheek event or not -- this function is stateful
4546 private static final int EVENT_NONE = 0;
4547 private static final int EVENT_UNKNOWN = 0;
4548 private static final int EVENT_CHEEK = 0;
4549 private static final int EVENT_IGNORE_DURATION = 300; // ms
4550 private static final float CHEEK_THRESHOLD = 0.6f;
4551 private int mEventState = EVENT_NONE;
4552 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 private int eventType(MotionEvent ev) {
4555 float size = ev.getSize();
4556 switch (ev.getAction()) {
4557 case MotionEvent.ACTION_DOWN:
4558 mEventSize = size;
4559 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4560 case MotionEvent.ACTION_UP:
4561 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004562 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 case MotionEvent.ACTION_MOVE:
4564 final int N = ev.getHistorySize();
4565 if (size > mEventSize) mEventSize = size;
4566 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4567 for (int i=0; i<N; i++) {
4568 size = ev.getHistoricalSize(i);
4569 if (size > mEventSize) mEventSize = size;
4570 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4571 }
4572 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4573 return TOUCH_EVENT;
4574 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004575 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 }
4577 default:
4578 // not good
4579 return OTHER_EVENT;
4580 }
4581 }
4582
4583 /**
4584 * @return Returns true if event was dispatched, false if it was dropped for any reason
4585 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004586 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4588 "dispatchPointer " + ev);
4589
Michael Chan53071d62009-05-13 17:29:48 -07004590 if (MEASURE_LATENCY) {
4591 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4592 }
4593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004595 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596
Michael Chan53071d62009-05-13 17:29:48 -07004597 if (MEASURE_LATENCY) {
4598 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4599 }
4600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 if (action == MotionEvent.ACTION_UP) {
4604 // let go of our target
4605 mKeyWaiter.mMotionTarget = null;
4606 mPowerManager.logPointerUpEvent();
4607 } else if (action == MotionEvent.ACTION_DOWN) {
4608 mPowerManager.logPointerDownEvent();
4609 }
4610
4611 if (targetObj == null) {
4612 // In this case we are either dropping the event, or have received
4613 // a move or up without a down. It is common to receive move
4614 // events in such a way, since this means the user is moving the
4615 // pointer without actually pressing down. All other cases should
4616 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004617 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4619 }
4620 if (qev != null) {
4621 mQueue.recycleEvent(qev);
4622 }
4623 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004624 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 }
4626 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4627 if (qev != null) {
4628 mQueue.recycleEvent(qev);
4629 }
4630 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004631 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 }
Romain Guy06882f82009-06-10 13:36:04 -07004633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004634 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004637 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638
4639 //Log.i(TAG, "Sending " + ev + " to " + target);
4640
4641 if (uid != 0 && uid != target.mSession.mUid) {
4642 if (mContext.checkPermission(
4643 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4644 != PackageManager.PERMISSION_GRANTED) {
4645 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4646 + pid + " uid " + uid + " to window " + target
4647 + " owned by uid " + target.mSession.mUid);
4648 if (qev != null) {
4649 mQueue.recycleEvent(qev);
4650 }
4651 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004652 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 }
4654 }
4655
Michael Chan53071d62009-05-13 17:29:48 -07004656 if (MEASURE_LATENCY) {
4657 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4658 }
4659
Romain Guy06882f82009-06-10 13:36:04 -07004660 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4662 //target wants to ignore fat touch events
4663 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4664 //explicit flag to return without processing event further
4665 boolean returnFlag = false;
4666 if((action == MotionEvent.ACTION_DOWN)) {
4667 mFatTouch = false;
4668 if(cheekPress) {
4669 mFatTouch = true;
4670 returnFlag = true;
4671 }
4672 } else {
4673 if(action == MotionEvent.ACTION_UP) {
4674 if(mFatTouch) {
4675 //earlier even was invalid doesnt matter if current up is cheekpress or not
4676 mFatTouch = false;
4677 returnFlag = true;
4678 } else if(cheekPress) {
4679 //cancel the earlier event
4680 ev.setAction(MotionEvent.ACTION_CANCEL);
4681 action = MotionEvent.ACTION_CANCEL;
4682 }
4683 } else if(action == MotionEvent.ACTION_MOVE) {
4684 if(mFatTouch) {
4685 //two cases here
4686 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004687 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 returnFlag = true;
4689 } else if(cheekPress) {
4690 //valid down followed by invalid moves
4691 //an invalid move have to cancel earlier action
4692 ev.setAction(MotionEvent.ACTION_CANCEL);
4693 action = MotionEvent.ACTION_CANCEL;
4694 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4695 //note that the subsequent invalid moves will not get here
4696 mFatTouch = true;
4697 }
4698 }
4699 } //else if action
4700 if(returnFlag) {
4701 //recycle que, ev
4702 if (qev != null) {
4703 mQueue.recycleEvent(qev);
4704 }
4705 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004706 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 }
4708 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004709
Michael Chan9f028e62009-08-04 17:37:46 -07004710 // Enable this for testing the "right" value
4711 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004712 int max_events_per_sec = 35;
4713 try {
4714 max_events_per_sec = Integer.parseInt(SystemProperties
4715 .get("windowsmgr.max_events_per_sec"));
4716 if (max_events_per_sec < 1) {
4717 max_events_per_sec = 35;
4718 }
4719 } catch (NumberFormatException e) {
4720 }
4721 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4722 }
4723
4724 /*
4725 * Throttle events to minimize CPU usage when there's a flood of events
4726 * e.g. constant contact with the screen
4727 */
4728 if (action == MotionEvent.ACTION_MOVE) {
4729 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4730 long now = SystemClock.uptimeMillis();
4731 if (now < nextEventTime) {
4732 try {
4733 Thread.sleep(nextEventTime - now);
4734 } catch (InterruptedException e) {
4735 }
4736 mLastTouchEventTime = nextEventTime;
4737 } else {
4738 mLastTouchEventTime = now;
4739 }
4740 }
4741
Michael Chan53071d62009-05-13 17:29:48 -07004742 if (MEASURE_LATENCY) {
4743 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4744 }
4745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 synchronized(mWindowMap) {
4747 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4748 mKeyWaiter.bindTargetWindowLocked(target,
4749 KeyWaiter.RETURN_PENDING_POINTER, qev);
4750 ev = null;
4751 } else {
4752 if (action == MotionEvent.ACTION_DOWN) {
4753 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4754 if (out != null) {
4755 MotionEvent oev = MotionEvent.obtain(ev);
4756 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4757 do {
4758 final Rect frame = out.mFrame;
4759 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4760 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004761 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 } catch (android.os.RemoteException e) {
4763 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4764 }
4765 oev.offsetLocation((float)frame.left, (float)frame.top);
4766 out = out.mNextOutsideTouch;
4767 } while (out != null);
4768 mKeyWaiter.mOutsideTouchTargets = null;
4769 }
4770 }
4771 final Rect frame = target.mFrame;
4772 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4773 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004774
4775 // If we are on top of the wallpaper, then the wallpaper also
4776 // gets to see this movement.
4777 if (mWallpaperTarget == target) {
4778 sendPointerToWallpaperLocked(target, ev, eventTime);
4779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 }
4781 }
Romain Guy06882f82009-06-10 13:36:04 -07004782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 // finally offset the event to the target's coordinate system and
4784 // dispatch the event.
4785 try {
4786 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4787 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4788 }
Michael Chan53071d62009-05-13 17:29:48 -07004789
4790 if (MEASURE_LATENCY) {
4791 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4792 }
4793
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004794 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004795
4796 if (MEASURE_LATENCY) {
4797 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4798 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004799 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 } catch (android.os.RemoteException e) {
4801 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4802 mKeyWaiter.mMotionTarget = null;
4803 try {
4804 removeWindow(target.mSession, target.mClient);
4805 } catch (java.util.NoSuchElementException ex) {
4806 // This will happen if the window has already been
4807 // removed.
4808 }
4809 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004810 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 }
Romain Guy06882f82009-06-10 13:36:04 -07004812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 /**
4814 * @return Returns true if event was dispatched, false if it was dropped for any reason
4815 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004816 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817 if (DEBUG_INPUT) Log.v(
4818 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004821 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 if (focusObj == null) {
4823 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4824 if (qev != null) {
4825 mQueue.recycleEvent(qev);
4826 }
4827 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004828 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 }
4830 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4831 if (qev != null) {
4832 mQueue.recycleEvent(qev);
4833 }
4834 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004835 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 }
Romain Guy06882f82009-06-10 13:36:04 -07004837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 if (uid != 0 && uid != focus.mSession.mUid) {
4841 if (mContext.checkPermission(
4842 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4843 != PackageManager.PERMISSION_GRANTED) {
4844 Log.w(TAG, "Permission denied: injecting key event from pid "
4845 + pid + " uid " + uid + " to window " + focus
4846 + " owned by uid " + focus.mSession.mUid);
4847 if (qev != null) {
4848 mQueue.recycleEvent(qev);
4849 }
4850 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004851 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 }
4853 }
Romain Guy06882f82009-06-10 13:36:04 -07004854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 synchronized(mWindowMap) {
4858 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4859 mKeyWaiter.bindTargetWindowLocked(focus,
4860 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4861 // We don't deliver movement events to the client, we hold
4862 // them and wait for them to call back.
4863 ev = null;
4864 } else {
4865 mKeyWaiter.bindTargetWindowLocked(focus);
4866 }
4867 }
Romain Guy06882f82009-06-10 13:36:04 -07004868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004870 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004871 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 } catch (android.os.RemoteException e) {
4873 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4874 try {
4875 removeWindow(focus.mSession, focus.mClient);
4876 } catch (java.util.NoSuchElementException ex) {
4877 // This will happen if the window has already been
4878 // removed.
4879 }
4880 }
Romain Guy06882f82009-06-10 13:36:04 -07004881
Dianne Hackborncfaef692009-06-15 14:24:44 -07004882 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 }
Romain Guy06882f82009-06-10 13:36:04 -07004884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 /**
4886 * @return Returns true if event was dispatched, false if it was dropped for any reason
4887 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004888 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4890
4891 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004892 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004893 if (focusObj == null) {
4894 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004895 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 }
4897 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004898 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 }
Romain Guy06882f82009-06-10 13:36:04 -07004900
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004901 // Okay we have finished waiting for the last event to be processed.
4902 // First off, if this is a repeat event, check to see if there is
4903 // a corresponding up event in the queue. If there is, we will
4904 // just drop the repeat, because it makes no sense to repeat after
4905 // the user has released a key. (This is especially important for
4906 // long presses.)
4907 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
4908 return INJECT_SUCCEEDED;
4909 }
4910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 if (DEBUG_INPUT) Log.v(
4914 TAG, "Dispatching to " + focus + ": " + event);
4915
4916 if (uid != 0 && uid != focus.mSession.mUid) {
4917 if (mContext.checkPermission(
4918 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4919 != PackageManager.PERMISSION_GRANTED) {
4920 Log.w(TAG, "Permission denied: injecting key event from pid "
4921 + pid + " uid " + uid + " to window " + focus
4922 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004923 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 }
4925 }
Romain Guy06882f82009-06-10 13:36:04 -07004926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 synchronized(mWindowMap) {
4928 mKeyWaiter.bindTargetWindowLocked(focus);
4929 }
4930
4931 // NOSHIP extra state logging
4932 mKeyWaiter.recordDispatchState(event, focus);
4933 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 try {
4936 if (DEBUG_INPUT || DEBUG_FOCUS) {
4937 Log.v(TAG, "Delivering key " + event.getKeyCode()
4938 + " to " + focus);
4939 }
4940 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004941 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 } catch (android.os.RemoteException e) {
4943 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4944 try {
4945 removeWindow(focus.mSession, focus.mClient);
4946 } catch (java.util.NoSuchElementException ex) {
4947 // This will happen if the window has already been
4948 // removed.
4949 }
4950 }
Romain Guy06882f82009-06-10 13:36:04 -07004951
Dianne Hackborncfaef692009-06-15 14:24:44 -07004952 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 }
Romain Guy06882f82009-06-10 13:36:04 -07004954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 public void pauseKeyDispatching(IBinder _token) {
4956 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4957 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004958 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 }
4960
4961 synchronized (mWindowMap) {
4962 WindowToken token = mTokenMap.get(_token);
4963 if (token != null) {
4964 mKeyWaiter.pauseDispatchingLocked(token);
4965 }
4966 }
4967 }
4968
4969 public void resumeKeyDispatching(IBinder _token) {
4970 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4971 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004972 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 }
4974
4975 synchronized (mWindowMap) {
4976 WindowToken token = mTokenMap.get(_token);
4977 if (token != null) {
4978 mKeyWaiter.resumeDispatchingLocked(token);
4979 }
4980 }
4981 }
4982
4983 public void setEventDispatching(boolean enabled) {
4984 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4985 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004986 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 }
4988
4989 synchronized (mWindowMap) {
4990 mKeyWaiter.setEventDispatchingLocked(enabled);
4991 }
4992 }
Romain Guy06882f82009-06-10 13:36:04 -07004993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 /**
4995 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004996 *
4997 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 * {@link SystemClock#uptimeMillis()} as the timebase.)
4999 * @param sync If true, wait for the event to be completed before returning to the caller.
5000 * @return Returns true if event was dispatched, false if it was dropped for any reason
5001 */
5002 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5003 long downTime = ev.getDownTime();
5004 long eventTime = ev.getEventTime();
5005
5006 int action = ev.getAction();
5007 int code = ev.getKeyCode();
5008 int repeatCount = ev.getRepeatCount();
5009 int metaState = ev.getMetaState();
5010 int deviceId = ev.getDeviceId();
5011 int scancode = ev.getScanCode();
5012
5013 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5014 if (downTime == 0) downTime = eventTime;
5015
5016 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005017 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005019 final int pid = Binder.getCallingPid();
5020 final int uid = Binder.getCallingUid();
5021 final long ident = Binder.clearCallingIdentity();
5022 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005024 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005026 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005027 switch (result) {
5028 case INJECT_NO_PERMISSION:
5029 throw new SecurityException(
5030 "Injecting to another application requires INJECT_EVENT permission");
5031 case INJECT_SUCCEEDED:
5032 return true;
5033 }
5034 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 }
5036
5037 /**
5038 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005039 *
5040 * @param ev A motion event describing the pointer (touch) action. (As noted in
5041 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 * {@link SystemClock#uptimeMillis()} as the timebase.)
5043 * @param sync If true, wait for the event to be completed before returning to the caller.
5044 * @return Returns true if event was dispatched, false if it was dropped for any reason
5045 */
5046 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005047 final int pid = Binder.getCallingPid();
5048 final int uid = Binder.getCallingUid();
5049 final long ident = Binder.clearCallingIdentity();
5050 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005052 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005053 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005054 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005055 switch (result) {
5056 case INJECT_NO_PERMISSION:
5057 throw new SecurityException(
5058 "Injecting to another application requires INJECT_EVENT permission");
5059 case INJECT_SUCCEEDED:
5060 return true;
5061 }
5062 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 }
Romain Guy06882f82009-06-10 13:36:04 -07005064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 /**
5066 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005067 *
5068 * @param ev A motion event describing the trackball action. (As noted in
5069 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 * {@link SystemClock#uptimeMillis()} as the timebase.)
5071 * @param sync If true, wait for the event to be completed before returning to the caller.
5072 * @return Returns true if event was dispatched, false if it was dropped for any reason
5073 */
5074 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005075 final int pid = Binder.getCallingPid();
5076 final int uid = Binder.getCallingUid();
5077 final long ident = Binder.clearCallingIdentity();
5078 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005080 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005082 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005083 switch (result) {
5084 case INJECT_NO_PERMISSION:
5085 throw new SecurityException(
5086 "Injecting to another application requires INJECT_EVENT permission");
5087 case INJECT_SUCCEEDED:
5088 return true;
5089 }
5090 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005091 }
Romain Guy06882f82009-06-10 13:36:04 -07005092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 private WindowState getFocusedWindow() {
5094 synchronized (mWindowMap) {
5095 return getFocusedWindowLocked();
5096 }
5097 }
5098
5099 private WindowState getFocusedWindowLocked() {
5100 return mCurrentFocus;
5101 }
Romain Guy06882f82009-06-10 13:36:04 -07005102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 /**
5104 * This class holds the state for dispatching key events. This state
5105 * is protected by the KeyWaiter instance, NOT by the window lock. You
5106 * can be holding the main window lock while acquire the KeyWaiter lock,
5107 * but not the other way around.
5108 */
5109 final class KeyWaiter {
5110 // NOSHIP debugging
5111 public class DispatchState {
5112 private KeyEvent event;
5113 private WindowState focus;
5114 private long time;
5115 private WindowState lastWin;
5116 private IBinder lastBinder;
5117 private boolean finished;
5118 private boolean gotFirstWindow;
5119 private boolean eventDispatching;
5120 private long timeToSwitch;
5121 private boolean wasFrozen;
5122 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005123 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5126 focus = theFocus;
5127 event = theEvent;
5128 time = System.currentTimeMillis();
5129 // snapshot KeyWaiter state
5130 lastWin = mLastWin;
5131 lastBinder = mLastBinder;
5132 finished = mFinished;
5133 gotFirstWindow = mGotFirstWindow;
5134 eventDispatching = mEventDispatching;
5135 timeToSwitch = mTimeToSwitch;
5136 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005137 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 // cache the paused state at ctor time as well
5139 if (theFocus == null || theFocus.mToken == null) {
5140 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5141 focusPaused = false;
5142 } else {
5143 focusPaused = theFocus.mToken.paused;
5144 }
5145 }
Romain Guy06882f82009-06-10 13:36:04 -07005146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 public String toString() {
5148 return "{{" + event + " to " + focus + " @ " + time
5149 + " lw=" + lastWin + " lb=" + lastBinder
5150 + " fin=" + finished + " gfw=" + gotFirstWindow
5151 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005152 + " wf=" + wasFrozen + " fp=" + focusPaused
5153 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 }
5155 };
5156 private DispatchState mDispatchState = null;
5157 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5158 mDispatchState = new DispatchState(theEvent, theFocus);
5159 }
5160 // END NOSHIP
5161
5162 public static final int RETURN_NOTHING = 0;
5163 public static final int RETURN_PENDING_POINTER = 1;
5164 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 final Object SKIP_TARGET_TOKEN = new Object();
5167 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 private WindowState mLastWin = null;
5170 private IBinder mLastBinder = null;
5171 private boolean mFinished = true;
5172 private boolean mGotFirstWindow = false;
5173 private boolean mEventDispatching = true;
5174 private long mTimeToSwitch = 0;
5175 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 // Target of Motion events
5178 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 // Windows above the target who would like to receive an "outside"
5181 // touch event for any down events outside of them.
5182 WindowState mOutsideTouchTargets;
5183
5184 /**
5185 * Wait for the last event dispatch to complete, then find the next
5186 * target that should receive the given event and wait for that one
5187 * to be ready to receive it.
5188 */
5189 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5190 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005191 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 long startTime = SystemClock.uptimeMillis();
5193 long keyDispatchingTimeout = 5 * 1000;
5194 long waitedFor = 0;
5195
5196 while (true) {
5197 // Figure out which window we care about. It is either the
5198 // last window we are waiting to have process the event or,
5199 // if none, then the next window we think the event should go
5200 // to. Note: we retrieve mLastWin outside of the lock, so
5201 // it may change before we lock. Thus we must check it again.
5202 WindowState targetWin = mLastWin;
5203 boolean targetIsNew = targetWin == null;
5204 if (DEBUG_INPUT) Log.v(
5205 TAG, "waitForLastKey: mFinished=" + mFinished +
5206 ", mLastWin=" + mLastWin);
5207 if (targetIsNew) {
5208 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005209 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 if (target == SKIP_TARGET_TOKEN) {
5211 // The user has pressed a special key, and we are
5212 // dropping all pending events before it.
5213 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5214 + " " + nextMotion);
5215 return null;
5216 }
5217 if (target == CONSUMED_EVENT_TOKEN) {
5218 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5219 + " " + nextMotion);
5220 return target;
5221 }
5222 targetWin = (WindowState)target;
5223 }
Romain Guy06882f82009-06-10 13:36:04 -07005224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 // Now: is it okay to send the next event to this window?
5228 synchronized (this) {
5229 // First: did we come here based on the last window not
5230 // being null, but it changed by the time we got here?
5231 // If so, try again.
5232 if (!targetIsNew && mLastWin == null) {
5233 continue;
5234 }
Romain Guy06882f82009-06-10 13:36:04 -07005235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 // We never dispatch events if not finished with the
5237 // last one, or the display is frozen.
5238 if (mFinished && !mDisplayFrozen) {
5239 // If event dispatching is disabled, then we
5240 // just consume the events.
5241 if (!mEventDispatching) {
5242 if (DEBUG_INPUT) Log.v(TAG,
5243 "Skipping event; dispatching disabled: "
5244 + nextKey + " " + nextMotion);
5245 return null;
5246 }
5247 if (targetWin != null) {
5248 // If this is a new target, and that target is not
5249 // paused or unresponsive, then all looks good to
5250 // handle the event.
5251 if (targetIsNew && !targetWin.mToken.paused) {
5252 return targetWin;
5253 }
Romain Guy06882f82009-06-10 13:36:04 -07005254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 // If we didn't find a target window, and there is no
5256 // focused app window, then just eat the events.
5257 } else if (mFocusedApp == null) {
5258 if (DEBUG_INPUT) Log.v(TAG,
5259 "Skipping event; no focused app: "
5260 + nextKey + " " + nextMotion);
5261 return null;
5262 }
5263 }
Romain Guy06882f82009-06-10 13:36:04 -07005264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005265 if (DEBUG_INPUT) Log.v(
5266 TAG, "Waiting for last key in " + mLastBinder
5267 + " target=" + targetWin
5268 + " mFinished=" + mFinished
5269 + " mDisplayFrozen=" + mDisplayFrozen
5270 + " targetIsNew=" + targetIsNew
5271 + " paused="
5272 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005273 + " mFocusedApp=" + mFocusedApp
5274 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 targetApp = targetWin != null
5277 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 long curTimeout = keyDispatchingTimeout;
5280 if (mTimeToSwitch != 0) {
5281 long now = SystemClock.uptimeMillis();
5282 if (mTimeToSwitch <= now) {
5283 // If an app switch key has been pressed, and we have
5284 // waited too long for the current app to finish
5285 // processing keys, then wait no more!
5286 doFinishedKeyLocked(true);
5287 continue;
5288 }
5289 long switchTimeout = mTimeToSwitch - now;
5290 if (curTimeout > switchTimeout) {
5291 curTimeout = switchTimeout;
5292 }
5293 }
Romain Guy06882f82009-06-10 13:36:04 -07005294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 try {
5296 // after that continue
5297 // processing keys, so we don't get stuck.
5298 if (DEBUG_INPUT) Log.v(
5299 TAG, "Waiting for key dispatch: " + curTimeout);
5300 wait(curTimeout);
5301 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5302 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005303 + startTime + " switchTime=" + mTimeToSwitch
5304 + " target=" + targetWin + " mLW=" + mLastWin
5305 + " mLB=" + mLastBinder + " fin=" + mFinished
5306 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 } catch (InterruptedException e) {
5308 }
5309 }
5310
5311 // If we were frozen during configuration change, restart the
5312 // timeout checks from now; otherwise look at whether we timed
5313 // out before awakening.
5314 if (mWasFrozen) {
5315 waitedFor = 0;
5316 mWasFrozen = false;
5317 } else {
5318 waitedFor = SystemClock.uptimeMillis() - startTime;
5319 }
5320
5321 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5322 IApplicationToken at = null;
5323 synchronized (this) {
5324 Log.w(TAG, "Key dispatching timed out sending to " +
5325 (targetWin != null ? targetWin.mAttrs.getTitle()
5326 : "<null>"));
5327 // NOSHIP debugging
5328 Log.w(TAG, "Dispatch state: " + mDispatchState);
5329 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5330 // END NOSHIP
5331 //dump();
5332 if (targetWin != null) {
5333 at = targetWin.getAppToken();
5334 } else if (targetApp != null) {
5335 at = targetApp.appToken;
5336 }
5337 }
5338
5339 boolean abort = true;
5340 if (at != null) {
5341 try {
5342 long timeout = at.getKeyDispatchingTimeout();
5343 if (timeout > waitedFor) {
5344 // we did not wait the proper amount of time for this application.
5345 // set the timeout to be the real timeout and wait again.
5346 keyDispatchingTimeout = timeout - waitedFor;
5347 continue;
5348 } else {
5349 abort = at.keyDispatchingTimedOut();
5350 }
5351 } catch (RemoteException ex) {
5352 }
5353 }
5354
5355 synchronized (this) {
5356 if (abort && (mLastWin == targetWin || targetWin == null)) {
5357 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005358 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 if (DEBUG_INPUT) Log.v(TAG,
5360 "Window " + mLastWin +
5361 " timed out on key input");
5362 if (mLastWin.mToken.paused) {
5363 Log.w(TAG, "Un-pausing dispatching to this window");
5364 mLastWin.mToken.paused = false;
5365 }
5366 }
5367 if (mMotionTarget == targetWin) {
5368 mMotionTarget = null;
5369 }
5370 mLastWin = null;
5371 mLastBinder = null;
5372 if (failIfTimeout || targetWin == null) {
5373 return null;
5374 }
5375 } else {
5376 Log.w(TAG, "Continuing to wait for key to be dispatched");
5377 startTime = SystemClock.uptimeMillis();
5378 }
5379 }
5380 }
5381 }
5382 }
Romain Guy06882f82009-06-10 13:36:04 -07005383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005385 MotionEvent nextMotion, boolean isPointerEvent,
5386 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 if (nextKey != null) {
5390 // Find the target window for a normal key event.
5391 final int keycode = nextKey.getKeyCode();
5392 final int repeatCount = nextKey.getRepeatCount();
5393 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5394 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005397 if (callingUid == 0 ||
5398 mContext.checkPermission(
5399 android.Manifest.permission.INJECT_EVENTS,
5400 callingPid, callingUid)
5401 == PackageManager.PERMISSION_GRANTED) {
5402 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005403 nextKey.getMetaState(), down, repeatCount,
5404 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 Log.w(TAG, "Event timeout during app switch: dropping "
5407 + nextKey);
5408 return SKIP_TARGET_TOKEN;
5409 }
Romain Guy06882f82009-06-10 13:36:04 -07005410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005411 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 WindowState focus = null;
5414 synchronized(mWindowMap) {
5415 focus = getFocusedWindowLocked();
5416 }
Romain Guy06882f82009-06-10 13:36:04 -07005417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005419
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005420 if (callingUid == 0 ||
5421 (focus != null && callingUid == focus.mSession.mUid) ||
5422 mContext.checkPermission(
5423 android.Manifest.permission.INJECT_EVENTS,
5424 callingPid, callingUid)
5425 == PackageManager.PERMISSION_GRANTED) {
5426 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005427 keycode, nextKey.getMetaState(), down, repeatCount,
5428 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005429 return CONSUMED_EVENT_TOKEN;
5430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
Romain Guy06882f82009-06-10 13:36:04 -07005432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 } else if (!isPointerEvent) {
5436 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5437 if (!dispatch) {
5438 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5439 + nextMotion);
5440 return SKIP_TARGET_TOKEN;
5441 }
Romain Guy06882f82009-06-10 13:36:04 -07005442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 WindowState focus = null;
5444 synchronized(mWindowMap) {
5445 focus = getFocusedWindowLocked();
5446 }
Romain Guy06882f82009-06-10 13:36:04 -07005447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5449 return focus;
5450 }
Romain Guy06882f82009-06-10 13:36:04 -07005451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 if (nextMotion == null) {
5453 return SKIP_TARGET_TOKEN;
5454 }
Romain Guy06882f82009-06-10 13:36:04 -07005455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5457 KeyEvent.KEYCODE_UNKNOWN);
5458 if (!dispatch) {
5459 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5460 + nextMotion);
5461 return SKIP_TARGET_TOKEN;
5462 }
Romain Guy06882f82009-06-10 13:36:04 -07005463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 // Find the target window for a pointer event.
5465 int action = nextMotion.getAction();
5466 final float xf = nextMotion.getX();
5467 final float yf = nextMotion.getY();
5468 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 final boolean screenWasOff = qev != null
5471 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 synchronized(mWindowMap) {
5476 synchronized (this) {
5477 if (action == MotionEvent.ACTION_DOWN) {
5478 if (mMotionTarget != null) {
5479 // this is weird, we got a pen down, but we thought it was
5480 // already down!
5481 // XXX: We should probably send an ACTION_UP to the current
5482 // target.
5483 Log.w(TAG, "Pointer down received while already down in: "
5484 + mMotionTarget);
5485 mMotionTarget = null;
5486 }
Romain Guy06882f82009-06-10 13:36:04 -07005487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 // ACTION_DOWN is special, because we need to lock next events to
5489 // the window we'll land onto.
5490 final int x = (int)xf;
5491 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 final ArrayList windows = mWindows;
5494 final int N = windows.size();
5495 WindowState topErrWindow = null;
5496 final Rect tmpRect = mTempRect;
5497 for (int i=N-1; i>=0; i--) {
5498 WindowState child = (WindowState)windows.get(i);
5499 //Log.i(TAG, "Checking dispatch to: " + child);
5500 final int flags = child.mAttrs.flags;
5501 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5502 if (topErrWindow == null) {
5503 topErrWindow = child;
5504 }
5505 }
5506 if (!child.isVisibleLw()) {
5507 //Log.i(TAG, "Not visible!");
5508 continue;
5509 }
5510 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5511 //Log.i(TAG, "Not touchable!");
5512 if ((flags & WindowManager.LayoutParams
5513 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5514 child.mNextOutsideTouch = mOutsideTouchTargets;
5515 mOutsideTouchTargets = child;
5516 }
5517 continue;
5518 }
5519 tmpRect.set(child.mFrame);
5520 if (child.mTouchableInsets == ViewTreeObserver
5521 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5522 // The touch is inside of the window if it is
5523 // inside the frame, AND the content part of that
5524 // frame that was given by the application.
5525 tmpRect.left += child.mGivenContentInsets.left;
5526 tmpRect.top += child.mGivenContentInsets.top;
5527 tmpRect.right -= child.mGivenContentInsets.right;
5528 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5529 } else if (child.mTouchableInsets == ViewTreeObserver
5530 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5531 // The touch is inside of the window if it is
5532 // inside the frame, AND the visible part of that
5533 // frame that was given by the application.
5534 tmpRect.left += child.mGivenVisibleInsets.left;
5535 tmpRect.top += child.mGivenVisibleInsets.top;
5536 tmpRect.right -= child.mGivenVisibleInsets.right;
5537 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5538 }
5539 final int touchFlags = flags &
5540 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5541 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5542 if (tmpRect.contains(x, y) || touchFlags == 0) {
5543 //Log.i(TAG, "Using this target!");
5544 if (!screenWasOff || (flags &
5545 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5546 mMotionTarget = child;
5547 } else {
5548 //Log.i(TAG, "Waking, skip!");
5549 mMotionTarget = null;
5550 }
5551 break;
5552 }
Romain Guy06882f82009-06-10 13:36:04 -07005553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 if ((flags & WindowManager.LayoutParams
5555 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5556 child.mNextOutsideTouch = mOutsideTouchTargets;
5557 mOutsideTouchTargets = child;
5558 //Log.i(TAG, "Adding to outside target list: " + child);
5559 }
5560 }
5561
5562 // if there's an error window but it's not accepting
5563 // focus (typically because it is not yet visible) just
5564 // wait for it -- any other focused window may in fact
5565 // be in ANR state.
5566 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5567 mMotionTarget = null;
5568 }
5569 }
Romain Guy06882f82009-06-10 13:36:04 -07005570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 target = mMotionTarget;
5572 }
5573 }
Romain Guy06882f82009-06-10 13:36:04 -07005574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 // Pointer events are a little different -- if there isn't a
5578 // target found for any event, then just drop it.
5579 return target != null ? target : SKIP_TARGET_TOKEN;
5580 }
Romain Guy06882f82009-06-10 13:36:04 -07005581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 boolean checkShouldDispatchKey(int keycode) {
5583 synchronized (this) {
5584 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5585 mTimeToSwitch = 0;
5586 return true;
5587 }
5588 if (mTimeToSwitch != 0
5589 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5590 return false;
5591 }
5592 return true;
5593 }
5594 }
Romain Guy06882f82009-06-10 13:36:04 -07005595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 void bindTargetWindowLocked(WindowState win,
5597 int pendingWhat, QueuedEvent pendingMotion) {
5598 synchronized (this) {
5599 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5600 }
5601 }
Romain Guy06882f82009-06-10 13:36:04 -07005602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 void bindTargetWindowLocked(WindowState win) {
5604 synchronized (this) {
5605 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5606 }
5607 }
5608
5609 void bindTargetWindowLockedLocked(WindowState win,
5610 int pendingWhat, QueuedEvent pendingMotion) {
5611 mLastWin = win;
5612 mLastBinder = win.mClient.asBinder();
5613 mFinished = false;
5614 if (pendingMotion != null) {
5615 final Session s = win.mSession;
5616 if (pendingWhat == RETURN_PENDING_POINTER) {
5617 releasePendingPointerLocked(s);
5618 s.mPendingPointerMove = pendingMotion;
5619 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005620 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005621 "bindTargetToWindow " + s.mPendingPointerMove);
5622 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5623 releasePendingTrackballLocked(s);
5624 s.mPendingTrackballMove = pendingMotion;
5625 s.mPendingTrackballWindow = win;
5626 }
5627 }
5628 }
Romain Guy06882f82009-06-10 13:36:04 -07005629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 void releasePendingPointerLocked(Session s) {
5631 if (DEBUG_INPUT) Log.v(TAG,
5632 "releasePendingPointer " + s.mPendingPointerMove);
5633 if (s.mPendingPointerMove != null) {
5634 mQueue.recycleEvent(s.mPendingPointerMove);
5635 s.mPendingPointerMove = null;
5636 }
5637 }
Romain Guy06882f82009-06-10 13:36:04 -07005638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 void releasePendingTrackballLocked(Session s) {
5640 if (s.mPendingTrackballMove != null) {
5641 mQueue.recycleEvent(s.mPendingTrackballMove);
5642 s.mPendingTrackballMove = null;
5643 }
5644 }
Romain Guy06882f82009-06-10 13:36:04 -07005645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5647 int returnWhat) {
5648 if (DEBUG_INPUT) Log.v(
5649 TAG, "finishedKey: client=" + client + ", force=" + force);
5650
5651 if (client == null) {
5652 return null;
5653 }
5654
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005655 MotionEvent res = null;
5656 QueuedEvent qev = null;
5657 WindowState win = null;
5658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 synchronized (this) {
5660 if (DEBUG_INPUT) Log.v(
5661 TAG, "finishedKey: client=" + client.asBinder()
5662 + ", force=" + force + ", last=" + mLastBinder
5663 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 if (returnWhat == RETURN_PENDING_POINTER) {
5666 qev = session.mPendingPointerMove;
5667 win = session.mPendingPointerWindow;
5668 session.mPendingPointerMove = null;
5669 session.mPendingPointerWindow = null;
5670 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5671 qev = session.mPendingTrackballMove;
5672 win = session.mPendingTrackballWindow;
5673 session.mPendingTrackballMove = null;
5674 session.mPendingTrackballWindow = null;
5675 }
Romain Guy06882f82009-06-10 13:36:04 -07005676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 if (mLastBinder == client.asBinder()) {
5678 if (DEBUG_INPUT) Log.v(
5679 TAG, "finishedKey: last paused="
5680 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5681 if (mLastWin != null && (!mLastWin.mToken.paused || force
5682 || !mEventDispatching)) {
5683 doFinishedKeyLocked(false);
5684 } else {
5685 // Make sure to wake up anyone currently waiting to
5686 // dispatch a key, so they can re-evaluate their
5687 // current situation.
5688 mFinished = true;
5689 notifyAll();
5690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 }
Romain Guy06882f82009-06-10 13:36:04 -07005692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005694 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 if (DEBUG_INPUT) Log.v(TAG,
5696 "Returning pending motion: " + res);
5697 mQueue.recycleEvent(qev);
5698 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5699 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005703
5704 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5705 synchronized (mWindowMap) {
5706 if (mWallpaperTarget == win) {
5707 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5708 }
5709 }
5710 }
5711
5712 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 }
5714
5715 void tickle() {
5716 synchronized (this) {
5717 notifyAll();
5718 }
5719 }
Romain Guy06882f82009-06-10 13:36:04 -07005720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 void handleNewWindowLocked(WindowState newWindow) {
5722 if (!newWindow.canReceiveKeys()) {
5723 return;
5724 }
5725 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005726 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 TAG, "New key dispatch window: win="
5728 + newWindow.mClient.asBinder()
5729 + ", last=" + mLastBinder
5730 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5731 + "), finished=" + mFinished + ", paused="
5732 + newWindow.mToken.paused);
5733
5734 // Displaying a window implicitly causes dispatching to
5735 // be unpaused. (This is to protect against bugs if someone
5736 // pauses dispatching but forgets to resume.)
5737 newWindow.mToken.paused = false;
5738
5739 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740
5741 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5742 if (DEBUG_INPUT) Log.v(TAG,
5743 "New SYSTEM_ERROR window; resetting state");
5744 mLastWin = null;
5745 mLastBinder = null;
5746 mMotionTarget = null;
5747 mFinished = true;
5748 } else if (mLastWin != null) {
5749 // If the new window is above the window we are
5750 // waiting on, then stop waiting and let key dispatching
5751 // start on the new guy.
5752 if (DEBUG_INPUT) Log.v(
5753 TAG, "Last win layer=" + mLastWin.mLayer
5754 + ", new win layer=" + newWindow.mLayer);
5755 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005756 // The new window is above the old; finish pending input to the last
5757 // window and start directing it to the new one.
5758 mLastWin.mToken.paused = false;
5759 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005761 // Either the new window is lower, so there is no need to wake key waiters,
5762 // or we just finished key input to the previous window, which implicitly
5763 // notified the key waiters. In both cases, we don't need to issue the
5764 // notification here.
5765 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 }
5767
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005768 // Now that we've put a new window state in place, make the event waiter
5769 // take notice and retarget its attentions.
5770 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 }
5772 }
5773
5774 void pauseDispatchingLocked(WindowToken token) {
5775 synchronized (this)
5776 {
5777 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5778 token.paused = true;
5779
5780 /*
5781 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5782 mPaused = true;
5783 } else {
5784 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005785 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005787 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005789 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 }
5791 }
5792 */
5793 }
5794 }
5795
5796 void resumeDispatchingLocked(WindowToken token) {
5797 synchronized (this) {
5798 if (token.paused) {
5799 if (DEBUG_INPUT) Log.v(
5800 TAG, "Resuming WindowToken " + token
5801 + ", last=" + mLastBinder
5802 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5803 + "), finished=" + mFinished + ", paused="
5804 + token.paused);
5805 token.paused = false;
5806 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5807 doFinishedKeyLocked(true);
5808 } else {
5809 notifyAll();
5810 }
5811 }
5812 }
5813 }
5814
5815 void setEventDispatchingLocked(boolean enabled) {
5816 synchronized (this) {
5817 mEventDispatching = enabled;
5818 notifyAll();
5819 }
5820 }
Romain Guy06882f82009-06-10 13:36:04 -07005821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 void appSwitchComing() {
5823 synchronized (this) {
5824 // Don't wait for more than .5 seconds for app to finish
5825 // processing the pending events.
5826 long now = SystemClock.uptimeMillis() + 500;
5827 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5828 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5829 mTimeToSwitch = now;
5830 }
5831 notifyAll();
5832 }
5833 }
Romain Guy06882f82009-06-10 13:36:04 -07005834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 private final void doFinishedKeyLocked(boolean doRecycle) {
5836 if (mLastWin != null) {
5837 releasePendingPointerLocked(mLastWin.mSession);
5838 releasePendingTrackballLocked(mLastWin.mSession);
5839 }
Romain Guy06882f82009-06-10 13:36:04 -07005840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 if (mLastWin == null || !mLastWin.mToken.paused
5842 || !mLastWin.isVisibleLw()) {
5843 // If the current window has been paused, we aren't -really-
5844 // finished... so let the waiters still wait.
5845 mLastWin = null;
5846 mLastBinder = null;
5847 }
5848 mFinished = true;
5849 notifyAll();
5850 }
5851 }
5852
5853 private class KeyQ extends KeyInputQueue
5854 implements KeyInputQueue.FilterCallback {
5855 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005858 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5860 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5861 "KEEP_SCREEN_ON_FLAG");
5862 mHoldingScreen.setReferenceCounted(false);
5863 }
5864
5865 @Override
5866 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5867 if (mPolicy.preprocessInputEventTq(event)) {
5868 return true;
5869 }
Romain Guy06882f82009-06-10 13:36:04 -07005870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 switch (event.type) {
5872 case RawInputEvent.EV_KEY: {
5873 // XXX begin hack
5874 if (DEBUG) {
5875 if (event.keycode == KeyEvent.KEYCODE_G) {
5876 if (event.value != 0) {
5877 // G down
5878 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5879 }
5880 return false;
5881 }
5882 if (event.keycode == KeyEvent.KEYCODE_D) {
5883 if (event.value != 0) {
5884 //dump();
5885 }
5886 return false;
5887 }
5888 }
5889 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 boolean screenIsOff = !mPowerManager.screenIsOn();
5892 boolean screenIsDim = !mPowerManager.screenIsBright();
5893 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5896 mPowerManager.goToSleep(event.when);
5897 }
5898
5899 if (screenIsOff) {
5900 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5901 }
5902 if (screenIsDim) {
5903 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5904 }
5905 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5906 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005907 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 }
Romain Guy06882f82009-06-10 13:36:04 -07005909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5911 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5912 filterQueue(this);
5913 mKeyWaiter.appSwitchComing();
5914 }
5915 return true;
5916 } else {
5917 return false;
5918 }
5919 }
Romain Guy06882f82009-06-10 13:36:04 -07005920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 case RawInputEvent.EV_REL: {
5922 boolean screenIsOff = !mPowerManager.screenIsOn();
5923 boolean screenIsDim = !mPowerManager.screenIsBright();
5924 if (screenIsOff) {
5925 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5926 device.classes, event)) {
5927 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5928 return false;
5929 }
5930 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5931 }
5932 if (screenIsDim) {
5933 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5934 }
5935 return true;
5936 }
Romain Guy06882f82009-06-10 13:36:04 -07005937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 case RawInputEvent.EV_ABS: {
5939 boolean screenIsOff = !mPowerManager.screenIsOn();
5940 boolean screenIsDim = !mPowerManager.screenIsBright();
5941 if (screenIsOff) {
5942 if (!mPolicy.isWakeAbsMovementTq(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 default:
5956 return true;
5957 }
5958 }
5959
5960 public int filterEvent(QueuedEvent ev) {
5961 switch (ev.classType) {
5962 case RawInputEvent.CLASS_KEYBOARD:
5963 KeyEvent ke = (KeyEvent)ev.event;
5964 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5965 Log.w(TAG, "Dropping movement key during app switch: "
5966 + ke.getKeyCode() + ", action=" + ke.getAction());
5967 return FILTER_REMOVE;
5968 }
5969 return FILTER_ABORT;
5970 default:
5971 return FILTER_KEEP;
5972 }
5973 }
Romain Guy06882f82009-06-10 13:36:04 -07005974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 /**
5976 * Must be called with the main window manager lock held.
5977 */
5978 void setHoldScreenLocked(boolean holding) {
5979 boolean state = mHoldingScreen.isHeld();
5980 if (holding != state) {
5981 if (holding) {
5982 mHoldingScreen.acquire();
5983 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005984 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 mHoldingScreen.release();
5986 }
5987 }
5988 }
Michael Chan53071d62009-05-13 17:29:48 -07005989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990
5991 public boolean detectSafeMode() {
5992 mSafeMode = mPolicy.detectSafeMode();
5993 return mSafeMode;
5994 }
Romain Guy06882f82009-06-10 13:36:04 -07005995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 public void systemReady() {
5997 mPolicy.systemReady();
5998 }
Romain Guy06882f82009-06-10 13:36:04 -07005999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 private final class InputDispatcherThread extends Thread {
6001 // Time to wait when there is nothing to do: 9999 seconds.
6002 static final int LONG_WAIT=9999*1000;
6003
6004 public InputDispatcherThread() {
6005 super("InputDispatcher");
6006 }
Romain Guy06882f82009-06-10 13:36:04 -07006007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 @Override
6009 public void run() {
6010 while (true) {
6011 try {
6012 process();
6013 } catch (Exception e) {
6014 Log.e(TAG, "Exception in input dispatcher", e);
6015 }
6016 }
6017 }
Romain Guy06882f82009-06-10 13:36:04 -07006018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 private void process() {
6020 android.os.Process.setThreadPriority(
6021 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 // The last key event we saw
6024 KeyEvent lastKey = null;
6025
6026 // Last keydown time for auto-repeating keys
6027 long lastKeyTime = SystemClock.uptimeMillis();
6028 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006029 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030
Romain Guy06882f82009-06-10 13:36:04 -07006031 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 int keyRepeatCount = 0;
6033
6034 // Need to report that configuration has changed?
6035 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 while (true) {
6038 long curTime = SystemClock.uptimeMillis();
6039
6040 if (DEBUG_INPUT) Log.v(
6041 TAG, "Waiting for next key: now=" + curTime
6042 + ", repeat @ " + nextKeyTime);
6043
6044 // Retrieve next event, waiting only as long as the next
6045 // repeat timeout. If the configuration has changed, then
6046 // don't wait at all -- we'll report the change as soon as
6047 // we have processed all events.
6048 QueuedEvent ev = mQueue.getEvent(
6049 (int)((!configChanged && curTime < nextKeyTime)
6050 ? (nextKeyTime-curTime) : 0));
6051
6052 if (DEBUG_INPUT && ev != null) Log.v(
6053 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6054
Michael Chan53071d62009-05-13 17:29:48 -07006055 if (MEASURE_LATENCY) {
6056 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6057 }
6058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 try {
6060 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006061 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 int eventType;
6063 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6064 eventType = eventType((MotionEvent)ev.event);
6065 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6066 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6067 eventType = LocalPowerManager.BUTTON_EVENT;
6068 } else {
6069 eventType = LocalPowerManager.OTHER_EVENT;
6070 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006071 try {
Michael Chan53071d62009-05-13 17:29:48 -07006072 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006073 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006074 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006075 mBatteryStats.noteInputEvent();
6076 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006077 } catch (RemoteException e) {
6078 // Ignore
6079 }
Michael Chane10de972009-05-18 11:24:50 -07006080
6081 if (eventType != TOUCH_EVENT
6082 && eventType != LONG_TOUCH_EVENT
6083 && eventType != CHEEK_EVENT) {
6084 mPowerManager.userActivity(curTime, false,
6085 eventType, false);
6086 } else if (mLastTouchEventType != eventType
6087 || (curTime - mLastUserActivityCallTime)
6088 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6089 mLastUserActivityCallTime = curTime;
6090 mLastTouchEventType = eventType;
6091 mPowerManager.userActivity(curTime, false,
6092 eventType, false);
6093 }
6094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 switch (ev.classType) {
6096 case RawInputEvent.CLASS_KEYBOARD:
6097 KeyEvent ke = (KeyEvent)ev.event;
6098 if (ke.isDown()) {
6099 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006100 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 keyRepeatCount = 0;
6102 lastKeyTime = curTime;
6103 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006104 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 if (DEBUG_INPUT) Log.v(
6106 TAG, "Received key down: first repeat @ "
6107 + nextKeyTime);
6108 } else {
6109 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006110 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 // Arbitrary long timeout.
6112 lastKeyTime = curTime;
6113 nextKeyTime = curTime + LONG_WAIT;
6114 if (DEBUG_INPUT) Log.v(
6115 TAG, "Received key up: ignore repeat @ "
6116 + nextKeyTime);
6117 }
6118 dispatchKey((KeyEvent)ev.event, 0, 0);
6119 mQueue.recycleEvent(ev);
6120 break;
6121 case RawInputEvent.CLASS_TOUCHSCREEN:
6122 //Log.i(TAG, "Read next event " + ev);
6123 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6124 break;
6125 case RawInputEvent.CLASS_TRACKBALL:
6126 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6127 break;
6128 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6129 configChanged = true;
6130 break;
6131 default:
6132 mQueue.recycleEvent(ev);
6133 break;
6134 }
Romain Guy06882f82009-06-10 13:36:04 -07006135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006136 } else if (configChanged) {
6137 configChanged = false;
6138 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006140 } else if (lastKey != null) {
6141 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 // Timeout occurred while key was down. If it is at or
6144 // past the key repeat time, dispatch the repeat.
6145 if (DEBUG_INPUT) Log.v(
6146 TAG, "Key timeout: repeat=" + nextKeyTime
6147 + ", now=" + curTime);
6148 if (curTime < nextKeyTime) {
6149 continue;
6150 }
Romain Guy06882f82009-06-10 13:36:04 -07006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 lastKeyTime = nextKeyTime;
6153 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6154 keyRepeatCount++;
6155 if (DEBUG_INPUT) Log.v(
6156 TAG, "Key repeat: count=" + keyRepeatCount
6157 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006158 KeyEvent newEvent;
6159 if (downTime != 0 && (downTime
6160 + ViewConfiguration.getLongPressTimeout())
6161 <= curTime) {
6162 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6163 curTime, keyRepeatCount,
6164 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6165 downTime = 0;
6166 } else {
6167 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6168 curTime, keyRepeatCount);
6169 }
6170 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 } else {
6173 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 lastKeyTime = curTime;
6176 nextKeyTime = curTime + LONG_WAIT;
6177 }
Romain Guy06882f82009-06-10 13:36:04 -07006178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 } catch (Exception e) {
6180 Log.e(TAG,
6181 "Input thread received uncaught exception: " + e, e);
6182 }
6183 }
6184 }
6185 }
6186
6187 // -------------------------------------------------------------
6188 // Client Session State
6189 // -------------------------------------------------------------
6190
6191 private final class Session extends IWindowSession.Stub
6192 implements IBinder.DeathRecipient {
6193 final IInputMethodClient mClient;
6194 final IInputContext mInputContext;
6195 final int mUid;
6196 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006197 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006198 SurfaceSession mSurfaceSession;
6199 int mNumWindow = 0;
6200 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006202 /**
6203 * Current pointer move event being dispatched to client window... must
6204 * hold key lock to access.
6205 */
6206 QueuedEvent mPendingPointerMove;
6207 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 /**
6210 * Current trackball move event being dispatched to client window... must
6211 * hold key lock to access.
6212 */
6213 QueuedEvent mPendingTrackballMove;
6214 WindowState mPendingTrackballWindow;
6215
6216 public Session(IInputMethodClient client, IInputContext inputContext) {
6217 mClient = client;
6218 mInputContext = inputContext;
6219 mUid = Binder.getCallingUid();
6220 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006221 StringBuilder sb = new StringBuilder();
6222 sb.append("Session{");
6223 sb.append(Integer.toHexString(System.identityHashCode(this)));
6224 sb.append(" uid ");
6225 sb.append(mUid);
6226 sb.append("}");
6227 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 synchronized (mWindowMap) {
6230 if (mInputMethodManager == null && mHaveInputMethods) {
6231 IBinder b = ServiceManager.getService(
6232 Context.INPUT_METHOD_SERVICE);
6233 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6234 }
6235 }
6236 long ident = Binder.clearCallingIdentity();
6237 try {
6238 // Note: it is safe to call in to the input method manager
6239 // here because we are not holding our lock.
6240 if (mInputMethodManager != null) {
6241 mInputMethodManager.addClient(client, inputContext,
6242 mUid, mPid);
6243 } else {
6244 client.setUsingInputMethod(false);
6245 }
6246 client.asBinder().linkToDeath(this, 0);
6247 } catch (RemoteException e) {
6248 // The caller has died, so we can just forget about this.
6249 try {
6250 if (mInputMethodManager != null) {
6251 mInputMethodManager.removeClient(client);
6252 }
6253 } catch (RemoteException ee) {
6254 }
6255 } finally {
6256 Binder.restoreCallingIdentity(ident);
6257 }
6258 }
Romain Guy06882f82009-06-10 13:36:04 -07006259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 @Override
6261 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6262 throws RemoteException {
6263 try {
6264 return super.onTransact(code, data, reply, flags);
6265 } catch (RuntimeException e) {
6266 // Log all 'real' exceptions thrown to the caller
6267 if (!(e instanceof SecurityException)) {
6268 Log.e(TAG, "Window Session Crash", e);
6269 }
6270 throw e;
6271 }
6272 }
6273
6274 public void binderDied() {
6275 // Note: it is safe to call in to the input method manager
6276 // here because we are not holding our lock.
6277 try {
6278 if (mInputMethodManager != null) {
6279 mInputMethodManager.removeClient(mClient);
6280 }
6281 } catch (RemoteException e) {
6282 }
6283 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006284 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 mClientDead = true;
6286 killSessionLocked();
6287 }
6288 }
6289
6290 public int add(IWindow window, WindowManager.LayoutParams attrs,
6291 int viewVisibility, Rect outContentInsets) {
6292 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6293 }
Romain Guy06882f82009-06-10 13:36:04 -07006294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 public void remove(IWindow window) {
6296 removeWindow(this, window);
6297 }
Romain Guy06882f82009-06-10 13:36:04 -07006298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6300 int requestedWidth, int requestedHeight, int viewFlags,
6301 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6302 Rect outVisibleInsets, Surface outSurface) {
6303 return relayoutWindow(this, window, attrs,
6304 requestedWidth, requestedHeight, viewFlags, insetsPending,
6305 outFrame, outContentInsets, outVisibleInsets, outSurface);
6306 }
Romain Guy06882f82009-06-10 13:36:04 -07006307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 public void setTransparentRegion(IWindow window, Region region) {
6309 setTransparentRegionWindow(this, window, region);
6310 }
Romain Guy06882f82009-06-10 13:36:04 -07006311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 public void setInsets(IWindow window, int touchableInsets,
6313 Rect contentInsets, Rect visibleInsets) {
6314 setInsetsWindow(this, window, touchableInsets, contentInsets,
6315 visibleInsets);
6316 }
Romain Guy06882f82009-06-10 13:36:04 -07006317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6319 getWindowDisplayFrame(this, window, outDisplayFrame);
6320 }
Romain Guy06882f82009-06-10 13:36:04 -07006321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 public void finishDrawing(IWindow window) {
6323 if (localLOGV) Log.v(
6324 TAG, "IWindow finishDrawing called for " + window);
6325 finishDrawingWindow(this, window);
6326 }
6327
6328 public void finishKey(IWindow window) {
6329 if (localLOGV) Log.v(
6330 TAG, "IWindow finishKey called for " + window);
6331 mKeyWaiter.finishedKey(this, window, false,
6332 KeyWaiter.RETURN_NOTHING);
6333 }
6334
6335 public MotionEvent getPendingPointerMove(IWindow window) {
6336 if (localLOGV) Log.v(
6337 TAG, "IWindow getPendingMotionEvent called for " + window);
6338 return mKeyWaiter.finishedKey(this, window, false,
6339 KeyWaiter.RETURN_PENDING_POINTER);
6340 }
Romain Guy06882f82009-06-10 13:36:04 -07006341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 public MotionEvent getPendingTrackballMove(IWindow window) {
6343 if (localLOGV) Log.v(
6344 TAG, "IWindow getPendingMotionEvent called for " + window);
6345 return mKeyWaiter.finishedKey(this, window, false,
6346 KeyWaiter.RETURN_PENDING_TRACKBALL);
6347 }
6348
6349 public void setInTouchMode(boolean mode) {
6350 synchronized(mWindowMap) {
6351 mInTouchMode = mode;
6352 }
6353 }
6354
6355 public boolean getInTouchMode() {
6356 synchronized(mWindowMap) {
6357 return mInTouchMode;
6358 }
6359 }
6360
6361 public boolean performHapticFeedback(IWindow window, int effectId,
6362 boolean always) {
6363 synchronized(mWindowMap) {
6364 long ident = Binder.clearCallingIdentity();
6365 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006366 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 windowForClientLocked(this, window), effectId, always);
6368 } finally {
6369 Binder.restoreCallingIdentity(ident);
6370 }
6371 }
6372 }
Romain Guy06882f82009-06-10 13:36:04 -07006373
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006374 public void setWallpaperPosition(IBinder window, float x, float y) {
6375 synchronized(mWindowMap) {
6376 long ident = Binder.clearCallingIdentity();
6377 try {
6378 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6379 x, y);
6380 } finally {
6381 Binder.restoreCallingIdentity(ident);
6382 }
6383 }
6384 }
6385
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006386 public void wallpaperOffsetsComplete(IBinder window) {
6387 WindowManagerService.this.wallpaperOffsetsComplete(window);
6388 }
6389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 void windowAddedLocked() {
6391 if (mSurfaceSession == null) {
6392 if (localLOGV) Log.v(
6393 TAG, "First window added to " + this + ", creating SurfaceSession");
6394 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006395 if (SHOW_TRANSACTIONS) Log.i(
6396 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 mSessions.add(this);
6398 }
6399 mNumWindow++;
6400 }
6401
6402 void windowRemovedLocked() {
6403 mNumWindow--;
6404 killSessionLocked();
6405 }
Romain Guy06882f82009-06-10 13:36:04 -07006406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 void killSessionLocked() {
6408 if (mNumWindow <= 0 && mClientDead) {
6409 mSessions.remove(this);
6410 if (mSurfaceSession != null) {
6411 if (localLOGV) Log.v(
6412 TAG, "Last window removed from " + this
6413 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006414 if (SHOW_TRANSACTIONS) Log.i(
6415 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 try {
6417 mSurfaceSession.kill();
6418 } catch (Exception e) {
6419 Log.w(TAG, "Exception thrown when killing surface session "
6420 + mSurfaceSession + " in session " + this
6421 + ": " + e.toString());
6422 }
6423 mSurfaceSession = null;
6424 }
6425 }
6426 }
Romain Guy06882f82009-06-10 13:36:04 -07006427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006428 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006429 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6430 pw.print(" mClientDead="); pw.print(mClientDead);
6431 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6432 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6433 pw.print(prefix);
6434 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6435 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6436 }
6437 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6438 pw.print(prefix);
6439 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6440 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 }
6443
6444 @Override
6445 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006446 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 }
6448 }
6449
6450 // -------------------------------------------------------------
6451 // Client Window State
6452 // -------------------------------------------------------------
6453
6454 private final class WindowState implements WindowManagerPolicy.WindowState {
6455 final Session mSession;
6456 final IWindow mClient;
6457 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006458 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 AppWindowToken mAppToken;
6460 AppWindowToken mTargetAppToken;
6461 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6462 final DeathRecipient mDeathRecipient;
6463 final WindowState mAttachedWindow;
6464 final ArrayList mChildWindows = new ArrayList();
6465 final int mBaseLayer;
6466 final int mSubLayer;
6467 final boolean mLayoutAttached;
6468 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006469 final boolean mIsWallpaper;
6470 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 int mViewVisibility;
6472 boolean mPolicyVisibility = true;
6473 boolean mPolicyVisibilityAfterAnim = true;
6474 boolean mAppFreezing;
6475 Surface mSurface;
6476 boolean mAttachedHidden; // is our parent window hidden?
6477 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006478 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 int mRequestedWidth;
6480 int mRequestedHeight;
6481 int mLastRequestedWidth;
6482 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 int mLayer;
6484 int mAnimLayer;
6485 int mLastLayer;
6486 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006487 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006488 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489
6490 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492 // Actual frame shown on-screen (may be modified by animation)
6493 final Rect mShownFrame = new Rect();
6494 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 /**
6497 * Insets that determine the actually visible area
6498 */
6499 final Rect mVisibleInsets = new Rect();
6500 final Rect mLastVisibleInsets = new Rect();
6501 boolean mVisibleInsetsChanged;
6502
6503 /**
6504 * Insets that are covered by system windows
6505 */
6506 final Rect mContentInsets = new Rect();
6507 final Rect mLastContentInsets = new Rect();
6508 boolean mContentInsetsChanged;
6509
6510 /**
6511 * Set to true if we are waiting for this window to receive its
6512 * given internal insets before laying out other windows based on it.
6513 */
6514 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 /**
6517 * These are the content insets that were given during layout for
6518 * this window, to be applied to windows behind it.
6519 */
6520 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006522 /**
6523 * These are the visible insets that were given during layout for
6524 * this window, to be applied to windows behind it.
6525 */
6526 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 /**
6529 * Flag indicating whether the touchable region should be adjusted by
6530 * the visible insets; if false the area outside the visible insets is
6531 * NOT touchable, so we must use those to adjust the frame during hit
6532 * tests.
6533 */
6534 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 // Current transformation being applied.
6537 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6538 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6539 float mHScale=1, mVScale=1;
6540 float mLastHScale=1, mLastVScale=1;
6541 final Matrix mTmpMatrix = new Matrix();
6542
6543 // "Real" frame that the application sees.
6544 final Rect mFrame = new Rect();
6545 final Rect mLastFrame = new Rect();
6546
6547 final Rect mContainingFrame = new Rect();
6548 final Rect mDisplayFrame = new Rect();
6549 final Rect mContentFrame = new Rect();
6550 final Rect mVisibleFrame = new Rect();
6551
6552 float mShownAlpha = 1;
6553 float mAlpha = 1;
6554 float mLastAlpha = 1;
6555
6556 // Set to true if, when the window gets displayed, it should perform
6557 // an enter animation.
6558 boolean mEnterAnimationPending;
6559
6560 // Currently running animation.
6561 boolean mAnimating;
6562 boolean mLocalAnimating;
6563 Animation mAnimation;
6564 boolean mAnimationIsEntrance;
6565 boolean mHasTransformation;
6566 boolean mHasLocalTransformation;
6567 final Transformation mTransformation = new Transformation();
6568
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006569 // If a window showing a wallpaper: the requested offset for the
6570 // wallpaper; if a wallpaper window: the currently applied offset.
6571 float mWallpaperX = -1;
6572 float mWallpaperY = -1;
6573
6574 // Wallpaper windows: pixels offset based on above variables.
6575 int mXOffset;
6576 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 // This is set after IWindowSession.relayout() has been called at
6579 // least once for the window. It allows us to detect the situation
6580 // where we don't yet have a surface, but should have one soon, so
6581 // we can give the window focus before waiting for the relayout.
6582 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584 // This is set after the Surface has been created but before the
6585 // window has been drawn. During this time the surface is hidden.
6586 boolean mDrawPending;
6587
6588 // This is set after the window has finished drawing for the first
6589 // time but before its surface is shown. The surface will be
6590 // displayed when the next layout is run.
6591 boolean mCommitDrawPending;
6592
6593 // This is set during the time after the window's drawing has been
6594 // committed, and before its surface is actually shown. It is used
6595 // to delay showing the surface until all windows in a token are ready
6596 // to be shown.
6597 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 // Set when the window has been shown in the screen the first time.
6600 boolean mHasDrawn;
6601
6602 // Currently running an exit animation?
6603 boolean mExiting;
6604
6605 // Currently on the mDestroySurface list?
6606 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 // Completely remove from window manager after exit animation?
6609 boolean mRemoveOnExit;
6610
6611 // Set when the orientation is changing and this window has not yet
6612 // been updated for the new orientation.
6613 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 // Is this window now (or just being) removed?
6616 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 WindowState(Session s, IWindow c, WindowToken token,
6619 WindowState attachedWindow, WindowManager.LayoutParams a,
6620 int viewVisibility) {
6621 mSession = s;
6622 mClient = c;
6623 mToken = token;
6624 mAttrs.copyFrom(a);
6625 mViewVisibility = viewVisibility;
6626 DeathRecipient deathRecipient = new DeathRecipient();
6627 mAlpha = a.alpha;
6628 if (localLOGV) Log.v(
6629 TAG, "Window " + this + " client=" + c.asBinder()
6630 + " token=" + token + " (" + mAttrs.token + ")");
6631 try {
6632 c.asBinder().linkToDeath(deathRecipient, 0);
6633 } catch (RemoteException e) {
6634 mDeathRecipient = null;
6635 mAttachedWindow = null;
6636 mLayoutAttached = false;
6637 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006638 mIsWallpaper = false;
6639 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 mBaseLayer = 0;
6641 mSubLayer = 0;
6642 return;
6643 }
6644 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6647 mAttrs.type <= LAST_SUB_WINDOW)) {
6648 // The multiplier here is to reserve space for multiple
6649 // windows in the same type layer.
6650 mBaseLayer = mPolicy.windowTypeToLayerLw(
6651 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6652 + TYPE_LAYER_OFFSET;
6653 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6654 mAttachedWindow = attachedWindow;
6655 mAttachedWindow.mChildWindows.add(this);
6656 mLayoutAttached = mAttrs.type !=
6657 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6658 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6659 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006660 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6661 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 } else {
6663 // The multiplier here is to reserve space for multiple
6664 // windows in the same type layer.
6665 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6666 * TYPE_LAYER_MULTIPLIER
6667 + TYPE_LAYER_OFFSET;
6668 mSubLayer = 0;
6669 mAttachedWindow = null;
6670 mLayoutAttached = false;
6671 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6672 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006673 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6674 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 }
6676
6677 WindowState appWin = this;
6678 while (appWin.mAttachedWindow != null) {
6679 appWin = mAttachedWindow;
6680 }
6681 WindowToken appToken = appWin.mToken;
6682 while (appToken.appWindowToken == null) {
6683 WindowToken parent = mTokenMap.get(appToken.token);
6684 if (parent == null || appToken == parent) {
6685 break;
6686 }
6687 appToken = parent;
6688 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006689 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 mAppToken = appToken.appWindowToken;
6691
6692 mSurface = null;
6693 mRequestedWidth = 0;
6694 mRequestedHeight = 0;
6695 mLastRequestedWidth = 0;
6696 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006697 mXOffset = 0;
6698 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 mLayer = 0;
6700 mAnimLayer = 0;
6701 mLastLayer = 0;
6702 }
6703
6704 void attach() {
6705 if (localLOGV) Log.v(
6706 TAG, "Attaching " + this + " token=" + mToken
6707 + ", list=" + mToken.windows);
6708 mSession.windowAddedLocked();
6709 }
6710
6711 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6712 mHaveFrame = true;
6713
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006714 final Rect container = mContainingFrame;
6715 container.set(pf);
6716
6717 final Rect display = mDisplayFrame;
6718 display.set(df);
6719
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006720 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006721 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006722 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6723 display.intersect(mCompatibleScreenFrame);
6724 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006725 }
6726
6727 final int pw = container.right - container.left;
6728 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729
6730 int w,h;
6731 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6732 w = mAttrs.width < 0 ? pw : mAttrs.width;
6733 h = mAttrs.height< 0 ? ph : mAttrs.height;
6734 } else {
6735 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6736 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6737 }
Romain Guy06882f82009-06-10 13:36:04 -07006738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006739 final Rect content = mContentFrame;
6740 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 final Rect visible = mVisibleFrame;
6743 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006746 final int fw = frame.width();
6747 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6750 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6751
6752 Gravity.apply(mAttrs.gravity, w, h, container,
6753 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6754 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6755
6756 //System.out.println("Out: " + mFrame);
6757
6758 // Now make sure the window fits in the overall display.
6759 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 // Make sure the content and visible frames are inside of the
6762 // final window frame.
6763 if (content.left < frame.left) content.left = frame.left;
6764 if (content.top < frame.top) content.top = frame.top;
6765 if (content.right > frame.right) content.right = frame.right;
6766 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6767 if (visible.left < frame.left) visible.left = frame.left;
6768 if (visible.top < frame.top) visible.top = frame.top;
6769 if (visible.right > frame.right) visible.right = frame.right;
6770 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 final Rect contentInsets = mContentInsets;
6773 contentInsets.left = content.left-frame.left;
6774 contentInsets.top = content.top-frame.top;
6775 contentInsets.right = frame.right-content.right;
6776 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 final Rect visibleInsets = mVisibleInsets;
6779 visibleInsets.left = visible.left-frame.left;
6780 visibleInsets.top = visible.top-frame.top;
6781 visibleInsets.right = frame.right-visible.right;
6782 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006783
Dianne Hackborn284ac932009-08-28 10:34:25 -07006784 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6785 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006786 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006787 }
6788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 if (localLOGV) {
6790 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6791 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6792 Log.v(TAG, "Resolving (mRequestedWidth="
6793 + mRequestedWidth + ", mRequestedheight="
6794 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6795 + "): frame=" + mFrame.toShortString()
6796 + " ci=" + contentInsets.toShortString()
6797 + " vi=" + visibleInsets.toShortString());
6798 //}
6799 }
6800 }
Romain Guy06882f82009-06-10 13:36:04 -07006801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 public Rect getFrameLw() {
6803 return mFrame;
6804 }
6805
6806 public Rect getShownFrameLw() {
6807 return mShownFrame;
6808 }
6809
6810 public Rect getDisplayFrameLw() {
6811 return mDisplayFrame;
6812 }
6813
6814 public Rect getContentFrameLw() {
6815 return mContentFrame;
6816 }
6817
6818 public Rect getVisibleFrameLw() {
6819 return mVisibleFrame;
6820 }
6821
6822 public boolean getGivenInsetsPendingLw() {
6823 return mGivenInsetsPending;
6824 }
6825
6826 public Rect getGivenContentInsetsLw() {
6827 return mGivenContentInsets;
6828 }
Romain Guy06882f82009-06-10 13:36:04 -07006829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 public Rect getGivenVisibleInsetsLw() {
6831 return mGivenVisibleInsets;
6832 }
Romain Guy06882f82009-06-10 13:36:04 -07006833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 public WindowManager.LayoutParams getAttrs() {
6835 return mAttrs;
6836 }
6837
6838 public int getSurfaceLayer() {
6839 return mLayer;
6840 }
Romain Guy06882f82009-06-10 13:36:04 -07006841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 public IApplicationToken getAppToken() {
6843 return mAppToken != null ? mAppToken.appToken : null;
6844 }
6845
6846 public boolean hasAppShownWindows() {
6847 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6848 }
6849
6850 public boolean hasAppStartingIcon() {
6851 return mAppToken != null ? (mAppToken.startingData != null) : false;
6852 }
6853
6854 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6855 return mAppToken != null ? mAppToken.startingWindow : null;
6856 }
6857
6858 public void setAnimation(Animation anim) {
6859 if (localLOGV) Log.v(
6860 TAG, "Setting animation in " + this + ": " + anim);
6861 mAnimating = false;
6862 mLocalAnimating = false;
6863 mAnimation = anim;
6864 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6865 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6866 }
6867
6868 public void clearAnimation() {
6869 if (mAnimation != null) {
6870 mAnimating = true;
6871 mLocalAnimating = false;
6872 mAnimation = null;
6873 }
6874 }
Romain Guy06882f82009-06-10 13:36:04 -07006875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006876 Surface createSurfaceLocked() {
6877 if (mSurface == null) {
6878 mDrawPending = true;
6879 mCommitDrawPending = false;
6880 mReadyToShow = false;
6881 if (mAppToken != null) {
6882 mAppToken.allDrawn = false;
6883 }
6884
6885 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006886 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 flags |= Surface.PUSH_BUFFERS;
6888 }
6889
6890 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6891 flags |= Surface.SECURE;
6892 }
6893 if (DEBUG_VISIBILITY) Log.v(
6894 TAG, "Creating surface in session "
6895 + mSession.mSurfaceSession + " window " + this
6896 + " w=" + mFrame.width()
6897 + " h=" + mFrame.height() + " format="
6898 + mAttrs.format + " flags=" + flags);
6899
6900 int w = mFrame.width();
6901 int h = mFrame.height();
6902 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6903 // for a scaled surface, we always want the requested
6904 // size.
6905 w = mRequestedWidth;
6906 h = mRequestedHeight;
6907 }
6908
6909 try {
6910 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006911 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006913 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
6914 + mSurface + " IN SESSION "
6915 + mSession.mSurfaceSession
6916 + ": pid=" + mSession.mPid + " format="
6917 + mAttrs.format + " flags=0x"
6918 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 } catch (Surface.OutOfResourcesException e) {
6920 Log.w(TAG, "OutOfResourcesException creating surface");
6921 reclaimSomeSurfaceMemoryLocked(this, "create");
6922 return null;
6923 } catch (Exception e) {
6924 Log.e(TAG, "Exception creating surface", e);
6925 return null;
6926 }
Romain Guy06882f82009-06-10 13:36:04 -07006927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 if (localLOGV) Log.v(
6929 TAG, "Got surface: " + mSurface
6930 + ", set left=" + mFrame.left + " top=" + mFrame.top
6931 + ", animLayer=" + mAnimLayer);
6932 if (SHOW_TRANSACTIONS) {
6933 Log.i(TAG, ">>> OPEN TRANSACTION");
6934 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6935 + mAttrs.getTitle() + ") pos=(" +
6936 mFrame.left + "," + mFrame.top + ") (" +
6937 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6938 mAnimLayer + " HIDE");
6939 }
6940 Surface.openTransaction();
6941 try {
6942 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006943 mSurface.setPosition(mFrame.left + mXOffset,
6944 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 mSurface.setLayer(mAnimLayer);
6946 mSurface.hide();
6947 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006948 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
6949 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 mSurface.setFlags(Surface.SURFACE_DITHER,
6951 Surface.SURFACE_DITHER);
6952 }
6953 } catch (RuntimeException e) {
6954 Log.w(TAG, "Error creating surface in " + w, e);
6955 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6956 }
6957 mLastHidden = true;
6958 } finally {
6959 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6960 Surface.closeTransaction();
6961 }
6962 if (localLOGV) Log.v(
6963 TAG, "Created surface " + this);
6964 }
6965 return mSurface;
6966 }
Romain Guy06882f82009-06-10 13:36:04 -07006967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 void destroySurfaceLocked() {
6969 // Window is no longer on-screen, so can no longer receive
6970 // key events... if we were waiting for it to finish
6971 // handling a key event, the wait is over!
6972 mKeyWaiter.finishedKey(mSession, mClient, true,
6973 KeyWaiter.RETURN_NOTHING);
6974 mKeyWaiter.releasePendingPointerLocked(mSession);
6975 mKeyWaiter.releasePendingTrackballLocked(mSession);
6976
6977 if (mAppToken != null && this == mAppToken.startingWindow) {
6978 mAppToken.startingDisplayed = false;
6979 }
Romain Guy06882f82009-06-10 13:36:04 -07006980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 if (mSurface != null) {
6982 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006983 if (DEBUG_VISIBILITY) {
6984 RuntimeException e = new RuntimeException();
6985 e.fillInStackTrace();
6986 Log.w(TAG, "Window " + this + " destroying surface "
6987 + mSurface + ", session " + mSession, e);
6988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 if (SHOW_TRANSACTIONS) {
6990 RuntimeException ex = new RuntimeException();
6991 ex.fillInStackTrace();
6992 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6993 + mAttrs.getTitle() + ")", ex);
6994 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006995 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 } catch (RuntimeException e) {
6997 Log.w(TAG, "Exception thrown when destroying Window " + this
6998 + " surface " + mSurface + " session " + mSession
6999 + ": " + e.toString());
7000 }
7001 mSurface = null;
7002 mDrawPending = false;
7003 mCommitDrawPending = false;
7004 mReadyToShow = false;
7005
7006 int i = mChildWindows.size();
7007 while (i > 0) {
7008 i--;
7009 WindowState c = (WindowState)mChildWindows.get(i);
7010 c.mAttachedHidden = true;
7011 }
7012 }
7013 }
7014
7015 boolean finishDrawingLocked() {
7016 if (mDrawPending) {
7017 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7018 TAG, "finishDrawingLocked: " + mSurface);
7019 mCommitDrawPending = true;
7020 mDrawPending = false;
7021 return true;
7022 }
7023 return false;
7024 }
7025
7026 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007027 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007028 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7029 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007030 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 }
7032 mCommitDrawPending = false;
7033 mReadyToShow = true;
7034 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7035 final AppWindowToken atoken = mAppToken;
7036 if (atoken == null || atoken.allDrawn || starting) {
7037 performShowLocked();
7038 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007039 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007040 }
7041
7042 // This must be called while inside a transaction.
7043 boolean performShowLocked() {
7044 if (DEBUG_VISIBILITY) {
7045 RuntimeException e = new RuntimeException();
7046 e.fillInStackTrace();
7047 Log.v(TAG, "performShow on " + this
7048 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7049 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7050 }
7051 if (mReadyToShow && isReadyForDisplay()) {
7052 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7053 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7054 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7055 + " during animation: policyVis=" + mPolicyVisibility
7056 + " attHidden=" + mAttachedHidden
7057 + " tok.hiddenRequested="
7058 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007059 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 + (mAppToken != null ? mAppToken.hidden : false)
7061 + " animating=" + mAnimating
7062 + " tok animating="
7063 + (mAppToken != null ? mAppToken.animating : false));
7064 if (!showSurfaceRobustlyLocked(this)) {
7065 return false;
7066 }
7067 mLastAlpha = -1;
7068 mHasDrawn = true;
7069 mLastHidden = false;
7070 mReadyToShow = false;
7071 enableScreenIfNeededLocked();
7072
7073 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007075 int i = mChildWindows.size();
7076 while (i > 0) {
7077 i--;
7078 WindowState c = (WindowState)mChildWindows.get(i);
7079 if (c.mSurface != null && c.mAttachedHidden) {
7080 c.mAttachedHidden = false;
7081 c.performShowLocked();
7082 }
7083 }
Romain Guy06882f82009-06-10 13:36:04 -07007084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 if (mAttrs.type != TYPE_APPLICATION_STARTING
7086 && mAppToken != null) {
7087 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007088
7089 if (mAppToken.startingData != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7091 + mToken
7092 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007093 // If this initial window is animating, stop it -- we
7094 // will do an animation to reveal it from behind the
7095 // starting window, so there is no need for it to also
7096 // be doing its own stuff.
7097 if (mAnimation != null) {
7098 mAnimation = null;
7099 // Make sure we clean up the animation.
7100 mAnimating = true;
7101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 mFinishedStarting.add(mAppToken);
7103 mH.sendEmptyMessage(H.FINISHED_STARTING);
7104 }
7105 mAppToken.updateReportedVisibilityLocked();
7106 }
7107 }
7108 return true;
7109 }
Romain Guy06882f82009-06-10 13:36:04 -07007110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 // This must be called while inside a transaction. Returns true if
7112 // there is more animation to run.
7113 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7114 if (!mDisplayFrozen) {
7115 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7118 mHasTransformation = true;
7119 mHasLocalTransformation = true;
7120 if (!mLocalAnimating) {
7121 if (DEBUG_ANIM) Log.v(
7122 TAG, "Starting animation in " + this +
7123 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7124 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7125 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7126 mAnimation.setStartTime(currentTime);
7127 mLocalAnimating = true;
7128 mAnimating = true;
7129 }
7130 mTransformation.clear();
7131 final boolean more = mAnimation.getTransformation(
7132 currentTime, mTransformation);
7133 if (DEBUG_ANIM) Log.v(
7134 TAG, "Stepped animation in " + this +
7135 ": more=" + more + ", xform=" + mTransformation);
7136 if (more) {
7137 // we're not done!
7138 return true;
7139 }
7140 if (DEBUG_ANIM) Log.v(
7141 TAG, "Finished animation in " + this +
7142 " @ " + currentTime);
7143 mAnimation = null;
7144 //WindowManagerService.this.dump();
7145 }
7146 mHasLocalTransformation = false;
7147 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007148 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 // When our app token is animating, we kind-of pretend like
7150 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7151 // part of this check means that we will only do this if
7152 // our window is not currently exiting, or it is not
7153 // locally animating itself. The idea being that one that
7154 // is exiting and doing a local animation should be removed
7155 // once that animation is done.
7156 mAnimating = true;
7157 mHasTransformation = true;
7158 mTransformation.clear();
7159 return false;
7160 } else if (mHasTransformation) {
7161 // Little trick to get through the path below to act like
7162 // we have finished an animation.
7163 mAnimating = true;
7164 } else if (isAnimating()) {
7165 mAnimating = true;
7166 }
7167 } else if (mAnimation != null) {
7168 // If the display is frozen, and there is a pending animation,
7169 // clear it and make sure we run the cleanup code.
7170 mAnimating = true;
7171 mLocalAnimating = true;
7172 mAnimation = null;
7173 }
Romain Guy06882f82009-06-10 13:36:04 -07007174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007175 if (!mAnimating && !mLocalAnimating) {
7176 return false;
7177 }
7178
7179 if (DEBUG_ANIM) Log.v(
7180 TAG, "Animation done in " + this + ": exiting=" + mExiting
7181 + ", reportedVisible="
7182 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 mAnimating = false;
7185 mLocalAnimating = false;
7186 mAnimation = null;
7187 mAnimLayer = mLayer;
7188 if (mIsImWindow) {
7189 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007190 } else if (mIsWallpaper) {
7191 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 }
7193 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7194 + " anim layer: " + mAnimLayer);
7195 mHasTransformation = false;
7196 mHasLocalTransformation = false;
7197 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7198 mTransformation.clear();
7199 if (mHasDrawn
7200 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7201 && mAppToken != null
7202 && mAppToken.firstWindowDrawn
7203 && mAppToken.startingData != null) {
7204 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7205 + mToken + ": first real window done animating");
7206 mFinishedStarting.add(mAppToken);
7207 mH.sendEmptyMessage(H.FINISHED_STARTING);
7208 }
Romain Guy06882f82009-06-10 13:36:04 -07007209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 finishExit();
7211
7212 if (mAppToken != null) {
7213 mAppToken.updateReportedVisibilityLocked();
7214 }
7215
7216 return false;
7217 }
7218
7219 void finishExit() {
7220 if (DEBUG_ANIM) Log.v(
7221 TAG, "finishExit in " + this
7222 + ": exiting=" + mExiting
7223 + " remove=" + mRemoveOnExit
7224 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 final int N = mChildWindows.size();
7227 for (int i=0; i<N; i++) {
7228 ((WindowState)mChildWindows.get(i)).finishExit();
7229 }
Romain Guy06882f82009-06-10 13:36:04 -07007230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 if (!mExiting) {
7232 return;
7233 }
Romain Guy06882f82009-06-10 13:36:04 -07007234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 if (isWindowAnimating()) {
7236 return;
7237 }
7238
7239 if (localLOGV) Log.v(
7240 TAG, "Exit animation finished in " + this
7241 + ": remove=" + mRemoveOnExit);
7242 if (mSurface != null) {
7243 mDestroySurface.add(this);
7244 mDestroying = true;
7245 if (SHOW_TRANSACTIONS) Log.i(
7246 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7247 try {
7248 mSurface.hide();
7249 } catch (RuntimeException e) {
7250 Log.w(TAG, "Error hiding surface in " + this, e);
7251 }
7252 mLastHidden = true;
7253 mKeyWaiter.releasePendingPointerLocked(mSession);
7254 }
7255 mExiting = false;
7256 if (mRemoveOnExit) {
7257 mPendingRemove.add(this);
7258 mRemoveOnExit = false;
7259 }
7260 }
Romain Guy06882f82009-06-10 13:36:04 -07007261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7263 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7264 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7265 if (dtdx < -.000001f || dtdx > .000001f) return false;
7266 if (dsdy < -.000001f || dsdy > .000001f) return false;
7267 return true;
7268 }
Romain Guy06882f82009-06-10 13:36:04 -07007269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 void computeShownFrameLocked() {
7271 final boolean selfTransformation = mHasLocalTransformation;
7272 Transformation attachedTransformation =
7273 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7274 ? mAttachedWindow.mTransformation : null;
7275 Transformation appTransformation =
7276 (mAppToken != null && mAppToken.hasTransformation)
7277 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007278
7279 // Wallpapers are animated based on the "real" window they
7280 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007281 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007282 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007283 if (mWallpaperTarget.mHasLocalTransformation) {
7284 attachedTransformation = mWallpaperTarget.mTransformation;
7285 }
7286 if (mWallpaperTarget.mAppToken != null &&
7287 mWallpaperTarget.mAppToken.hasTransformation) {
7288 appTransformation = mWallpaperTarget.mAppToken.transformation;
7289 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007290 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7291 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7292 }
7293 if (DEBUG_WALLPAPER && appTransformation != null) {
7294 Log.v(TAG, "WP target app xform: " + appTransformation);
7295 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007296 }
7297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 if (selfTransformation || attachedTransformation != null
7299 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007300 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 final Rect frame = mFrame;
7302 final float tmpFloats[] = mTmpFloats;
7303 final Matrix tmpMatrix = mTmpMatrix;
7304
7305 // Compute the desired transformation.
7306 tmpMatrix.setTranslate(frame.left, frame.top);
7307 if (selfTransformation) {
7308 tmpMatrix.preConcat(mTransformation.getMatrix());
7309 }
7310 if (attachedTransformation != null) {
7311 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7312 }
7313 if (appTransformation != null) {
7314 tmpMatrix.preConcat(appTransformation.getMatrix());
7315 }
7316
7317 // "convert" it into SurfaceFlinger's format
7318 // (a 2x2 matrix + an offset)
7319 // Here we must not transform the position of the surface
7320 // since it is already included in the transformation.
7321 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007323 tmpMatrix.getValues(tmpFloats);
7324 mDsDx = tmpFloats[Matrix.MSCALE_X];
7325 mDtDx = tmpFloats[Matrix.MSKEW_X];
7326 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7327 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007328 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7329 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 int w = frame.width();
7331 int h = frame.height();
7332 mShownFrame.set(x, y, x+w, y+h);
7333
7334 // Now set the alpha... but because our current hardware
7335 // can't do alpha transformation on a non-opaque surface,
7336 // turn it off if we are running an animation that is also
7337 // transforming since it is more important to have that
7338 // animation be smooth.
7339 mShownAlpha = mAlpha;
7340 if (!mLimitedAlphaCompositing
7341 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7342 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7343 && x == frame.left && y == frame.top))) {
7344 //Log.i(TAG, "Applying alpha transform");
7345 if (selfTransformation) {
7346 mShownAlpha *= mTransformation.getAlpha();
7347 }
7348 if (attachedTransformation != null) {
7349 mShownAlpha *= attachedTransformation.getAlpha();
7350 }
7351 if (appTransformation != null) {
7352 mShownAlpha *= appTransformation.getAlpha();
7353 }
7354 } else {
7355 //Log.i(TAG, "Not applying alpha transform");
7356 }
Romain Guy06882f82009-06-10 13:36:04 -07007357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 if (localLOGV) Log.v(
7359 TAG, "Continuing animation in " + this +
7360 ": " + mShownFrame +
7361 ", alpha=" + mTransformation.getAlpha());
7362 return;
7363 }
Romain Guy06882f82009-06-10 13:36:04 -07007364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007366 if (mXOffset != 0 || mYOffset != 0) {
7367 mShownFrame.offset(mXOffset, mYOffset);
7368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 mShownAlpha = mAlpha;
7370 mDsDx = 1;
7371 mDtDx = 0;
7372 mDsDy = 0;
7373 mDtDy = 1;
7374 }
Romain Guy06882f82009-06-10 13:36:04 -07007375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 /**
7377 * Is this window visible? It is not visible if there is no
7378 * surface, or we are in the process of running an exit animation
7379 * that will remove the surface, or its app token has been hidden.
7380 */
7381 public boolean isVisibleLw() {
7382 final AppWindowToken atoken = mAppToken;
7383 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7384 && (atoken == null || !atoken.hiddenRequested)
7385 && !mExiting && !mDestroying;
7386 }
7387
7388 /**
7389 * Is this window visible, ignoring its app token? It is not visible
7390 * if there is no surface, or we are in the process of running an exit animation
7391 * that will remove the surface.
7392 */
7393 public boolean isWinVisibleLw() {
7394 final AppWindowToken atoken = mAppToken;
7395 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7396 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7397 && !mExiting && !mDestroying;
7398 }
7399
7400 /**
7401 * The same as isVisible(), but follows the current hidden state of
7402 * the associated app token, not the pending requested hidden state.
7403 */
7404 boolean isVisibleNow() {
7405 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007406 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007407 }
7408
7409 /**
7410 * Same as isVisible(), but we also count it as visible between the
7411 * call to IWindowSession.add() and the first relayout().
7412 */
7413 boolean isVisibleOrAdding() {
7414 final AppWindowToken atoken = mAppToken;
7415 return (mSurface != null
7416 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7417 && mPolicyVisibility && !mAttachedHidden
7418 && (atoken == null || !atoken.hiddenRequested)
7419 && !mExiting && !mDestroying;
7420 }
7421
7422 /**
7423 * Is this window currently on-screen? It is on-screen either if it
7424 * is visible or it is currently running an animation before no longer
7425 * being visible.
7426 */
7427 boolean isOnScreen() {
7428 final AppWindowToken atoken = mAppToken;
7429 if (atoken != null) {
7430 return mSurface != null && mPolicyVisibility && !mDestroying
7431 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007432 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 } else {
7434 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007435 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436 }
7437 }
Romain Guy06882f82009-06-10 13:36:04 -07007438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 /**
7440 * Like isOnScreen(), but we don't return true if the window is part
7441 * of a transition that has not yet been started.
7442 */
7443 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007444 if (mRootToken.waitingToShow &&
7445 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7446 return false;
7447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007449 final boolean animating = atoken != null
7450 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007452 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7453 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007454 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 }
7456
7457 /** Is the window or its container currently animating? */
7458 boolean isAnimating() {
7459 final WindowState attached = mAttachedWindow;
7460 final AppWindowToken atoken = mAppToken;
7461 return mAnimation != null
7462 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007463 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 (atoken.animation != null
7465 || atoken.inPendingTransaction));
7466 }
7467
7468 /** Is this window currently animating? */
7469 boolean isWindowAnimating() {
7470 return mAnimation != null;
7471 }
7472
7473 /**
7474 * Like isOnScreen, but returns false if the surface hasn't yet
7475 * been drawn.
7476 */
7477 public boolean isDisplayedLw() {
7478 final AppWindowToken atoken = mAppToken;
7479 return mSurface != null && mPolicyVisibility && !mDestroying
7480 && !mDrawPending && !mCommitDrawPending
7481 && ((!mAttachedHidden &&
7482 (atoken == null || !atoken.hiddenRequested))
7483 || mAnimating);
7484 }
7485
7486 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7487 boolean shownFrame, boolean onlyOpaque) {
7488 if (mSurface == null) {
7489 return false;
7490 }
7491 if (mAppToken != null && !mAppToken.appFullscreen) {
7492 return false;
7493 }
7494 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7495 return false;
7496 }
7497 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007498
7499 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7500 return frame.left <= mCompatibleScreenFrame.left &&
7501 frame.top <= mCompatibleScreenFrame.top &&
7502 frame.right >= mCompatibleScreenFrame.right &&
7503 frame.bottom >= mCompatibleScreenFrame.bottom;
7504 } else {
7505 return frame.left <= 0 && frame.top <= 0
7506 && frame.right >= screenWidth
7507 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 }
Romain Guy06882f82009-06-10 13:36:04 -07007510
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007511 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007512 * Return true if the window is opaque and fully drawn. This indicates
7513 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007514 */
7515 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007516 return (mAttrs.format == PixelFormat.OPAQUE
7517 || mAttrs.type == TYPE_WALLPAPER)
7518 && mSurface != null && mAnimation == null
7519 && (mAppToken == null || mAppToken.animation == null)
7520 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007521 }
7522
7523 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7524 return
7525 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007526 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7527 // only if it's visible
7528 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007529 // and only if the application fills the compatible screen
7530 mFrame.left <= mCompatibleScreenFrame.left &&
7531 mFrame.top <= mCompatibleScreenFrame.top &&
7532 mFrame.right >= mCompatibleScreenFrame.right &&
7533 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007534 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007535 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007536 }
7537
7538 boolean isFullscreen(int screenWidth, int screenHeight) {
7539 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007540 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007541 }
7542
7543 void removeLocked() {
7544 if (mAttachedWindow != null) {
7545 mAttachedWindow.mChildWindows.remove(this);
7546 }
7547 destroySurfaceLocked();
7548 mSession.windowRemovedLocked();
7549 try {
7550 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7551 } catch (RuntimeException e) {
7552 // Ignore if it has already been removed (usually because
7553 // we are doing this as part of processing a death note.)
7554 }
7555 }
7556
7557 private class DeathRecipient implements IBinder.DeathRecipient {
7558 public void binderDied() {
7559 try {
7560 synchronized(mWindowMap) {
7561 WindowState win = windowForClientLocked(mSession, mClient);
7562 Log.i(TAG, "WIN DEATH: " + win);
7563 if (win != null) {
7564 removeWindowLocked(mSession, win);
7565 }
7566 }
7567 } catch (IllegalArgumentException ex) {
7568 // This will happen if the window has already been
7569 // removed.
7570 }
7571 }
7572 }
7573
7574 /** Returns true if this window desires key events. */
7575 public final boolean canReceiveKeys() {
7576 return isVisibleOrAdding()
7577 && (mViewVisibility == View.VISIBLE)
7578 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7579 }
7580
7581 public boolean hasDrawnLw() {
7582 return mHasDrawn;
7583 }
7584
7585 public boolean showLw(boolean doAnimation) {
7586 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7587 mPolicyVisibility = true;
7588 mPolicyVisibilityAfterAnim = true;
7589 if (doAnimation) {
7590 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7591 }
7592 requestAnimationLocked(0);
7593 return true;
7594 }
7595 return false;
7596 }
7597
7598 public boolean hideLw(boolean doAnimation) {
7599 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7600 : mPolicyVisibility;
7601 if (current) {
7602 if (doAnimation) {
7603 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7604 if (mAnimation == null) {
7605 doAnimation = false;
7606 }
7607 }
7608 if (doAnimation) {
7609 mPolicyVisibilityAfterAnim = false;
7610 } else {
7611 mPolicyVisibilityAfterAnim = false;
7612 mPolicyVisibility = false;
7613 }
7614 requestAnimationLocked(0);
7615 return true;
7616 }
7617 return false;
7618 }
7619
7620 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007621 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007622
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007623 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7624 pw.print(" mClient="); pw.println(mClient.asBinder());
7625 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7626 if (mAttachedWindow != null || mLayoutAttached) {
7627 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7628 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7629 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007630 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7631 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7632 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007633 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7634 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007635 }
7636 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7637 pw.print(" mSubLayer="); pw.print(mSubLayer);
7638 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7639 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7640 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7641 pw.print("="); pw.print(mAnimLayer);
7642 pw.print(" mLastLayer="); pw.println(mLastLayer);
7643 if (mSurface != null) {
7644 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7645 }
7646 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7647 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7648 if (mAppToken != null) {
7649 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7650 }
7651 if (mTargetAppToken != null) {
7652 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7653 }
7654 pw.print(prefix); pw.print("mViewVisibility=0x");
7655 pw.print(Integer.toHexString(mViewVisibility));
7656 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007657 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7658 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007659 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7660 pw.print(prefix); pw.print("mPolicyVisibility=");
7661 pw.print(mPolicyVisibility);
7662 pw.print(" mPolicyVisibilityAfterAnim=");
7663 pw.print(mPolicyVisibilityAfterAnim);
7664 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7665 }
7666 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007667 pw.print(" h="); pw.println(mRequestedHeight);
7668 if (mXOffset != 0 || mYOffset != 0) {
7669 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7670 pw.print(" y="); pw.println(mYOffset);
7671 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007672 pw.print(prefix); pw.print("mGivenContentInsets=");
7673 mGivenContentInsets.printShortString(pw);
7674 pw.print(" mGivenVisibleInsets=");
7675 mGivenVisibleInsets.printShortString(pw);
7676 pw.println();
7677 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7678 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7679 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7680 }
7681 pw.print(prefix); pw.print("mShownFrame=");
7682 mShownFrame.printShortString(pw);
7683 pw.print(" last="); mLastShownFrame.printShortString(pw);
7684 pw.println();
7685 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7686 pw.print(" last="); mLastFrame.printShortString(pw);
7687 pw.println();
7688 pw.print(prefix); pw.print("mContainingFrame=");
7689 mContainingFrame.printShortString(pw);
7690 pw.print(" mDisplayFrame=");
7691 mDisplayFrame.printShortString(pw);
7692 pw.println();
7693 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7694 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7695 pw.println();
7696 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7697 pw.print(" last="); mLastContentInsets.printShortString(pw);
7698 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7699 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7700 pw.println();
7701 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7702 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7703 pw.print(" mAlpha="); pw.print(mAlpha);
7704 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7705 }
7706 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7707 || mAnimation != null) {
7708 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7709 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7710 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7711 pw.print(" mAnimation="); pw.println(mAnimation);
7712 }
7713 if (mHasTransformation || mHasLocalTransformation) {
7714 pw.print(prefix); pw.print("XForm: has=");
7715 pw.print(mHasTransformation);
7716 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7717 pw.print(" "); mTransformation.printShortString(pw);
7718 pw.println();
7719 }
7720 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7721 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7722 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7723 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7724 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7725 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7726 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7727 pw.print(" mDestroying="); pw.print(mDestroying);
7728 pw.print(" mRemoved="); pw.println(mRemoved);
7729 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007730 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007731 pw.print(prefix); pw.print("mOrientationChanging=");
7732 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007733 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7734 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007735 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007736 if (mHScale != 1 || mVScale != 1) {
7737 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7738 pw.print(" mVScale="); pw.println(mVScale);
7739 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007740 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007741 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7742 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 }
7745
7746 @Override
7747 public String toString() {
7748 return "Window{"
7749 + Integer.toHexString(System.identityHashCode(this))
7750 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7751 }
7752 }
Romain Guy06882f82009-06-10 13:36:04 -07007753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 // -------------------------------------------------------------
7755 // Window Token State
7756 // -------------------------------------------------------------
7757
7758 class WindowToken {
7759 // The actual token.
7760 final IBinder token;
7761
7762 // The type of window this token is for, as per WindowManager.LayoutParams.
7763 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 // Set if this token was explicitly added by a client, so should
7766 // not be removed when all windows are removed.
7767 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007768
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007769 // For printing.
7770 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 // If this is an AppWindowToken, this is non-null.
7773 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 // All of the windows associated with this token.
7776 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7777
7778 // Is key dispatching paused for this token?
7779 boolean paused = false;
7780
7781 // Should this token's windows be hidden?
7782 boolean hidden;
7783
7784 // Temporary for finding which tokens no longer have visible windows.
7785 boolean hasVisible;
7786
Dianne Hackborna8f60182009-09-01 19:01:50 -07007787 // Set to true when this token is in a pending transaction where it
7788 // will be shown.
7789 boolean waitingToShow;
7790
7791 // Set to true when this token is in a pending transaction where it
7792 // will be hidden.
7793 boolean waitingToHide;
7794
7795 // Set to true when this token is in a pending transaction where its
7796 // windows will be put to the bottom of the list.
7797 boolean sendingToBottom;
7798
7799 // Set to true when this token is in a pending transaction where its
7800 // windows will be put to the top of the list.
7801 boolean sendingToTop;
7802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 WindowToken(IBinder _token, int type, boolean _explicit) {
7804 token = _token;
7805 windowType = type;
7806 explicit = _explicit;
7807 }
7808
7809 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007810 pw.print(prefix); pw.print("token="); pw.println(token);
7811 pw.print(prefix); pw.print("windows="); pw.println(windows);
7812 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7813 pw.print(" hidden="); pw.print(hidden);
7814 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007815 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7816 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7817 pw.print(" waitingToHide="); pw.print(waitingToHide);
7818 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7819 pw.print(" sendingToTop="); pw.println(sendingToTop);
7820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 }
7822
7823 @Override
7824 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007825 if (stringName == null) {
7826 StringBuilder sb = new StringBuilder();
7827 sb.append("WindowToken{");
7828 sb.append(Integer.toHexString(System.identityHashCode(this)));
7829 sb.append(" token="); sb.append(token); sb.append('}');
7830 stringName = sb.toString();
7831 }
7832 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 }
7834 };
7835
7836 class AppWindowToken extends WindowToken {
7837 // Non-null only for application tokens.
7838 final IApplicationToken appToken;
7839
7840 // All of the windows and child windows that are included in this
7841 // application token. Note this list is NOT sorted!
7842 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7843
7844 int groupId = -1;
7845 boolean appFullscreen;
7846 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 // These are used for determining when all windows associated with
7849 // an activity have been drawn, so they can be made visible together
7850 // at the same time.
7851 int lastTransactionSequence = mTransactionSequence-1;
7852 int numInterestingWindows;
7853 int numDrawnWindows;
7854 boolean inPendingTransaction;
7855 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 // Is this token going to be hidden in a little while? If so, it
7858 // won't be taken into account for setting the screen orientation.
7859 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 // Is this window's surface needed? This is almost like hidden, except
7862 // it will sometimes be true a little earlier: when the token has
7863 // been shown, but is still waiting for its app transition to execute
7864 // before making its windows shown.
7865 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 // Have we told the window clients to hide themselves?
7868 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007870 // Last visibility state we reported to the app token.
7871 boolean reportedVisible;
7872
7873 // Set to true when the token has been removed from the window mgr.
7874 boolean removed;
7875
7876 // Have we been asked to have this token keep the screen frozen?
7877 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 boolean animating;
7880 Animation animation;
7881 boolean hasTransformation;
7882 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 // Offset to the window of all layers in the token, for use by
7885 // AppWindowToken animations.
7886 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 // Information about an application starting window if displayed.
7889 StartingData startingData;
7890 WindowState startingWindow;
7891 View startingView;
7892 boolean startingDisplayed;
7893 boolean startingMoved;
7894 boolean firstWindowDrawn;
7895
7896 AppWindowToken(IApplicationToken _token) {
7897 super(_token.asBinder(),
7898 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7899 appWindowToken = this;
7900 appToken = _token;
7901 }
Romain Guy06882f82009-06-10 13:36:04 -07007902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 public void setAnimation(Animation anim) {
7904 if (localLOGV) Log.v(
7905 TAG, "Setting animation in " + this + ": " + anim);
7906 animation = anim;
7907 animating = false;
7908 anim.restrictDuration(MAX_ANIMATION_DURATION);
7909 anim.scaleCurrentDuration(mTransitionAnimationScale);
7910 int zorder = anim.getZAdjustment();
7911 int adj = 0;
7912 if (zorder == Animation.ZORDER_TOP) {
7913 adj = TYPE_LAYER_OFFSET;
7914 } else if (zorder == Animation.ZORDER_BOTTOM) {
7915 adj = -TYPE_LAYER_OFFSET;
7916 }
Romain Guy06882f82009-06-10 13:36:04 -07007917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 if (animLayerAdjustment != adj) {
7919 animLayerAdjustment = adj;
7920 updateLayers();
7921 }
7922 }
Romain Guy06882f82009-06-10 13:36:04 -07007923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007924 public void setDummyAnimation() {
7925 if (animation == null) {
7926 if (localLOGV) Log.v(
7927 TAG, "Setting dummy animation in " + this);
7928 animation = sDummyAnimation;
7929 }
7930 }
7931
7932 public void clearAnimation() {
7933 if (animation != null) {
7934 animation = null;
7935 animating = true;
7936 }
7937 }
Romain Guy06882f82009-06-10 13:36:04 -07007938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007939 void updateLayers() {
7940 final int N = allAppWindows.size();
7941 final int adj = animLayerAdjustment;
7942 for (int i=0; i<N; i++) {
7943 WindowState w = allAppWindows.get(i);
7944 w.mAnimLayer = w.mLayer + adj;
7945 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7946 + w.mAnimLayer);
7947 if (w == mInputMethodTarget) {
7948 setInputMethodAnimLayerAdjustment(adj);
7949 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007950 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007951 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 }
7954 }
Romain Guy06882f82009-06-10 13:36:04 -07007955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 void sendAppVisibilityToClients() {
7957 final int N = allAppWindows.size();
7958 for (int i=0; i<N; i++) {
7959 WindowState win = allAppWindows.get(i);
7960 if (win == startingWindow && clientHidden) {
7961 // Don't hide the starting window.
7962 continue;
7963 }
7964 try {
7965 if (DEBUG_VISIBILITY) Log.v(TAG,
7966 "Setting visibility of " + win + ": " + (!clientHidden));
7967 win.mClient.dispatchAppVisibility(!clientHidden);
7968 } catch (RemoteException e) {
7969 }
7970 }
7971 }
Romain Guy06882f82009-06-10 13:36:04 -07007972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 void showAllWindowsLocked() {
7974 final int NW = allAppWindows.size();
7975 for (int i=0; i<NW; i++) {
7976 WindowState w = allAppWindows.get(i);
7977 if (DEBUG_VISIBILITY) Log.v(TAG,
7978 "performing show on: " + w);
7979 w.performShowLocked();
7980 }
7981 }
Romain Guy06882f82009-06-10 13:36:04 -07007982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 // This must be called while inside a transaction.
7984 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7985 if (!mDisplayFrozen) {
7986 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 if (animation == sDummyAnimation) {
7989 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007990 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 // when it is really time to animate, this will be set to
7992 // a real animation and the next call will execute normally.
7993 return false;
7994 }
Romain Guy06882f82009-06-10 13:36:04 -07007995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7997 if (!animating) {
7998 if (DEBUG_ANIM) Log.v(
7999 TAG, "Starting animation in " + this +
8000 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8001 + " scale=" + mTransitionAnimationScale
8002 + " allDrawn=" + allDrawn + " animating=" + animating);
8003 animation.initialize(dw, dh, dw, dh);
8004 animation.setStartTime(currentTime);
8005 animating = true;
8006 }
8007 transformation.clear();
8008 final boolean more = animation.getTransformation(
8009 currentTime, transformation);
8010 if (DEBUG_ANIM) Log.v(
8011 TAG, "Stepped animation in " + this +
8012 ": more=" + more + ", xform=" + transformation);
8013 if (more) {
8014 // we're done!
8015 hasTransformation = true;
8016 return true;
8017 }
8018 if (DEBUG_ANIM) Log.v(
8019 TAG, "Finished animation in " + this +
8020 " @ " + currentTime);
8021 animation = null;
8022 }
8023 } else if (animation != null) {
8024 // If the display is frozen, and there is a pending animation,
8025 // clear it and make sure we run the cleanup code.
8026 animating = true;
8027 animation = null;
8028 }
8029
8030 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 if (!animating) {
8033 return false;
8034 }
8035
8036 clearAnimation();
8037 animating = false;
8038 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8039 moveInputMethodWindowsIfNeededLocked(true);
8040 }
Romain Guy06882f82009-06-10 13:36:04 -07008041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 if (DEBUG_ANIM) Log.v(
8043 TAG, "Animation done in " + this
8044 + ": reportedVisible=" + reportedVisible);
8045
8046 transformation.clear();
8047 if (animLayerAdjustment != 0) {
8048 animLayerAdjustment = 0;
8049 updateLayers();
8050 }
Romain Guy06882f82009-06-10 13:36:04 -07008051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052 final int N = windows.size();
8053 for (int i=0; i<N; i++) {
8054 ((WindowState)windows.get(i)).finishExit();
8055 }
8056 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 return false;
8059 }
8060
8061 void updateReportedVisibilityLocked() {
8062 if (appToken == null) {
8063 return;
8064 }
Romain Guy06882f82009-06-10 13:36:04 -07008065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 int numInteresting = 0;
8067 int numVisible = 0;
8068 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008070 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8071 final int N = allAppWindows.size();
8072 for (int i=0; i<N; i++) {
8073 WindowState win = allAppWindows.get(i);
8074 if (win == startingWindow || win.mAppFreezing) {
8075 continue;
8076 }
8077 if (DEBUG_VISIBILITY) {
8078 Log.v(TAG, "Win " + win + ": isDisplayed="
8079 + win.isDisplayedLw()
8080 + ", isAnimating=" + win.isAnimating());
8081 if (!win.isDisplayedLw()) {
8082 Log.v(TAG, "Not displayed: s=" + win.mSurface
8083 + " pv=" + win.mPolicyVisibility
8084 + " dp=" + win.mDrawPending
8085 + " cdp=" + win.mCommitDrawPending
8086 + " ah=" + win.mAttachedHidden
8087 + " th="
8088 + (win.mAppToken != null
8089 ? win.mAppToken.hiddenRequested : false)
8090 + " a=" + win.mAnimating);
8091 }
8092 }
8093 numInteresting++;
8094 if (win.isDisplayedLw()) {
8095 if (!win.isAnimating()) {
8096 numVisible++;
8097 }
8098 nowGone = false;
8099 } else if (win.isAnimating()) {
8100 nowGone = false;
8101 }
8102 }
Romain Guy06882f82009-06-10 13:36:04 -07008103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8105 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8106 + numInteresting + " visible=" + numVisible);
8107 if (nowVisible != reportedVisible) {
8108 if (DEBUG_VISIBILITY) Log.v(
8109 TAG, "Visibility changed in " + this
8110 + ": vis=" + nowVisible);
8111 reportedVisible = nowVisible;
8112 Message m = mH.obtainMessage(
8113 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8114 nowVisible ? 1 : 0,
8115 nowGone ? 1 : 0,
8116 this);
8117 mH.sendMessage(m);
8118 }
8119 }
Romain Guy06882f82009-06-10 13:36:04 -07008120
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008121 WindowState findMainWindow() {
8122 int j = windows.size();
8123 while (j > 0) {
8124 j--;
8125 WindowState win = windows.get(j);
8126 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8127 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8128 return win;
8129 }
8130 }
8131 return null;
8132 }
8133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 void dump(PrintWriter pw, String prefix) {
8135 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008136 if (appToken != null) {
8137 pw.print(prefix); pw.println("app=true");
8138 }
8139 if (allAppWindows.size() > 0) {
8140 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8141 }
8142 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008143 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008144 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8145 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8146 pw.print(" clientHidden="); pw.print(clientHidden);
8147 pw.print(" willBeHidden="); pw.print(willBeHidden);
8148 pw.print(" reportedVisible="); pw.println(reportedVisible);
8149 if (paused || freezingScreen) {
8150 pw.print(prefix); pw.print("paused="); pw.print(paused);
8151 pw.print(" freezingScreen="); pw.println(freezingScreen);
8152 }
8153 if (numInterestingWindows != 0 || numDrawnWindows != 0
8154 || inPendingTransaction || allDrawn) {
8155 pw.print(prefix); pw.print("numInterestingWindows=");
8156 pw.print(numInterestingWindows);
8157 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8158 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8159 pw.print(" allDrawn="); pw.println(allDrawn);
8160 }
8161 if (animating || animation != null) {
8162 pw.print(prefix); pw.print("animating="); pw.print(animating);
8163 pw.print(" animation="); pw.println(animation);
8164 }
8165 if (animLayerAdjustment != 0) {
8166 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8167 }
8168 if (hasTransformation) {
8169 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8170 pw.print(" transformation="); transformation.printShortString(pw);
8171 pw.println();
8172 }
8173 if (startingData != null || removed || firstWindowDrawn) {
8174 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8175 pw.print(" removed="); pw.print(removed);
8176 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8177 }
8178 if (startingWindow != null || startingView != null
8179 || startingDisplayed || startingMoved) {
8180 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8181 pw.print(" startingView="); pw.print(startingView);
8182 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8183 pw.print(" startingMoved"); pw.println(startingMoved);
8184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008185 }
8186
8187 @Override
8188 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008189 if (stringName == null) {
8190 StringBuilder sb = new StringBuilder();
8191 sb.append("AppWindowToken{");
8192 sb.append(Integer.toHexString(System.identityHashCode(this)));
8193 sb.append(" token="); sb.append(token); sb.append('}');
8194 stringName = sb.toString();
8195 }
8196 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 }
8198 }
Romain Guy06882f82009-06-10 13:36:04 -07008199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 // -------------------------------------------------------------
8201 // DummyAnimation
8202 // -------------------------------------------------------------
8203
8204 // This is an animation that does nothing: it just immediately finishes
8205 // itself every time it is called. It is used as a stub animation in cases
8206 // where we want to synchronize multiple things that may be animating.
8207 static final class DummyAnimation extends Animation {
8208 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8209 return false;
8210 }
8211 }
8212 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 // -------------------------------------------------------------
8215 // Async Handler
8216 // -------------------------------------------------------------
8217
8218 static final class StartingData {
8219 final String pkg;
8220 final int theme;
8221 final CharSequence nonLocalizedLabel;
8222 final int labelRes;
8223 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8226 int _labelRes, int _icon) {
8227 pkg = _pkg;
8228 theme = _theme;
8229 nonLocalizedLabel = _nonLocalizedLabel;
8230 labelRes = _labelRes;
8231 icon = _icon;
8232 }
8233 }
8234
8235 private final class H extends Handler {
8236 public static final int REPORT_FOCUS_CHANGE = 2;
8237 public static final int REPORT_LOSING_FOCUS = 3;
8238 public static final int ANIMATE = 4;
8239 public static final int ADD_STARTING = 5;
8240 public static final int REMOVE_STARTING = 6;
8241 public static final int FINISHED_STARTING = 7;
8242 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8244 public static final int HOLD_SCREEN_CHANGED = 12;
8245 public static final int APP_TRANSITION_TIMEOUT = 13;
8246 public static final int PERSIST_ANIMATION_SCALE = 14;
8247 public static final int FORCE_GC = 15;
8248 public static final int ENABLE_SCREEN = 16;
8249 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008250 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 public H() {
8255 }
Romain Guy06882f82009-06-10 13:36:04 -07008256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 @Override
8258 public void handleMessage(Message msg) {
8259 switch (msg.what) {
8260 case REPORT_FOCUS_CHANGE: {
8261 WindowState lastFocus;
8262 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 synchronized(mWindowMap) {
8265 lastFocus = mLastFocus;
8266 newFocus = mCurrentFocus;
8267 if (lastFocus == newFocus) {
8268 // Focus is not changing, so nothing to do.
8269 return;
8270 }
8271 mLastFocus = newFocus;
8272 //Log.i(TAG, "Focus moving from " + lastFocus
8273 // + " to " + newFocus);
8274 if (newFocus != null && lastFocus != null
8275 && !newFocus.isDisplayedLw()) {
8276 //Log.i(TAG, "Delaying loss of focus...");
8277 mLosingFocus.add(lastFocus);
8278 lastFocus = null;
8279 }
8280 }
8281
8282 if (lastFocus != newFocus) {
8283 //System.out.println("Changing focus from " + lastFocus
8284 // + " to " + newFocus);
8285 if (newFocus != null) {
8286 try {
8287 //Log.i(TAG, "Gaining focus: " + newFocus);
8288 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8289 } catch (RemoteException e) {
8290 // Ignore if process has died.
8291 }
8292 }
8293
8294 if (lastFocus != null) {
8295 try {
8296 //Log.i(TAG, "Losing focus: " + lastFocus);
8297 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8298 } catch (RemoteException e) {
8299 // Ignore if process has died.
8300 }
8301 }
8302 }
8303 } break;
8304
8305 case REPORT_LOSING_FOCUS: {
8306 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 synchronized(mWindowMap) {
8309 losers = mLosingFocus;
8310 mLosingFocus = new ArrayList<WindowState>();
8311 }
8312
8313 final int N = losers.size();
8314 for (int i=0; i<N; i++) {
8315 try {
8316 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8317 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8318 } catch (RemoteException e) {
8319 // Ignore if process has died.
8320 }
8321 }
8322 } break;
8323
8324 case ANIMATE: {
8325 synchronized(mWindowMap) {
8326 mAnimationPending = false;
8327 performLayoutAndPlaceSurfacesLocked();
8328 }
8329 } break;
8330
8331 case ADD_STARTING: {
8332 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8333 final StartingData sd = wtoken.startingData;
8334
8335 if (sd == null) {
8336 // Animation has been canceled... do nothing.
8337 return;
8338 }
Romain Guy06882f82009-06-10 13:36:04 -07008339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8341 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008343 View view = null;
8344 try {
8345 view = mPolicy.addStartingWindow(
8346 wtoken.token, sd.pkg,
8347 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8348 sd.icon);
8349 } catch (Exception e) {
8350 Log.w(TAG, "Exception when adding starting window", e);
8351 }
8352
8353 if (view != null) {
8354 boolean abort = false;
8355
8356 synchronized(mWindowMap) {
8357 if (wtoken.removed || wtoken.startingData == null) {
8358 // If the window was successfully added, then
8359 // we need to remove it.
8360 if (wtoken.startingWindow != null) {
8361 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8362 "Aborted starting " + wtoken
8363 + ": removed=" + wtoken.removed
8364 + " startingData=" + wtoken.startingData);
8365 wtoken.startingWindow = null;
8366 wtoken.startingData = null;
8367 abort = true;
8368 }
8369 } else {
8370 wtoken.startingView = view;
8371 }
8372 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8373 "Added starting " + wtoken
8374 + ": startingWindow="
8375 + wtoken.startingWindow + " startingView="
8376 + wtoken.startingView);
8377 }
8378
8379 if (abort) {
8380 try {
8381 mPolicy.removeStartingWindow(wtoken.token, view);
8382 } catch (Exception e) {
8383 Log.w(TAG, "Exception when removing starting window", e);
8384 }
8385 }
8386 }
8387 } break;
8388
8389 case REMOVE_STARTING: {
8390 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8391 IBinder token = null;
8392 View view = null;
8393 synchronized (mWindowMap) {
8394 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8395 + wtoken + ": startingWindow="
8396 + wtoken.startingWindow + " startingView="
8397 + wtoken.startingView);
8398 if (wtoken.startingWindow != null) {
8399 view = wtoken.startingView;
8400 token = wtoken.token;
8401 wtoken.startingData = null;
8402 wtoken.startingView = null;
8403 wtoken.startingWindow = null;
8404 }
8405 }
8406 if (view != null) {
8407 try {
8408 mPolicy.removeStartingWindow(token, view);
8409 } catch (Exception e) {
8410 Log.w(TAG, "Exception when removing starting window", e);
8411 }
8412 }
8413 } break;
8414
8415 case FINISHED_STARTING: {
8416 IBinder token = null;
8417 View view = null;
8418 while (true) {
8419 synchronized (mWindowMap) {
8420 final int N = mFinishedStarting.size();
8421 if (N <= 0) {
8422 break;
8423 }
8424 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8425
8426 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8427 "Finished starting " + wtoken
8428 + ": startingWindow=" + wtoken.startingWindow
8429 + " startingView=" + wtoken.startingView);
8430
8431 if (wtoken.startingWindow == null) {
8432 continue;
8433 }
8434
8435 view = wtoken.startingView;
8436 token = wtoken.token;
8437 wtoken.startingData = null;
8438 wtoken.startingView = null;
8439 wtoken.startingWindow = null;
8440 }
8441
8442 try {
8443 mPolicy.removeStartingWindow(token, view);
8444 } catch (Exception e) {
8445 Log.w(TAG, "Exception when removing starting window", e);
8446 }
8447 }
8448 } break;
8449
8450 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8451 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8452
8453 boolean nowVisible = msg.arg1 != 0;
8454 boolean nowGone = msg.arg2 != 0;
8455
8456 try {
8457 if (DEBUG_VISIBILITY) Log.v(
8458 TAG, "Reporting visible in " + wtoken
8459 + " visible=" + nowVisible
8460 + " gone=" + nowGone);
8461 if (nowVisible) {
8462 wtoken.appToken.windowsVisible();
8463 } else {
8464 wtoken.appToken.windowsGone();
8465 }
8466 } catch (RemoteException ex) {
8467 }
8468 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 case WINDOW_FREEZE_TIMEOUT: {
8471 synchronized (mWindowMap) {
8472 Log.w(TAG, "Window freeze timeout expired.");
8473 int i = mWindows.size();
8474 while (i > 0) {
8475 i--;
8476 WindowState w = (WindowState)mWindows.get(i);
8477 if (w.mOrientationChanging) {
8478 w.mOrientationChanging = false;
8479 Log.w(TAG, "Force clearing orientation change: " + w);
8480 }
8481 }
8482 performLayoutAndPlaceSurfacesLocked();
8483 }
8484 break;
8485 }
Romain Guy06882f82009-06-10 13:36:04 -07008486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008487 case HOLD_SCREEN_CHANGED: {
8488 Session oldHold;
8489 Session newHold;
8490 synchronized (mWindowMap) {
8491 oldHold = mLastReportedHold;
8492 newHold = (Session)msg.obj;
8493 mLastReportedHold = newHold;
8494 }
Romain Guy06882f82009-06-10 13:36:04 -07008495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 if (oldHold != newHold) {
8497 try {
8498 if (oldHold != null) {
8499 mBatteryStats.noteStopWakelock(oldHold.mUid,
8500 "window",
8501 BatteryStats.WAKE_TYPE_WINDOW);
8502 }
8503 if (newHold != null) {
8504 mBatteryStats.noteStartWakelock(newHold.mUid,
8505 "window",
8506 BatteryStats.WAKE_TYPE_WINDOW);
8507 }
8508 } catch (RemoteException e) {
8509 }
8510 }
8511 break;
8512 }
Romain Guy06882f82009-06-10 13:36:04 -07008513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 case APP_TRANSITION_TIMEOUT: {
8515 synchronized (mWindowMap) {
8516 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8517 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8518 "*** APP TRANSITION TIMEOUT");
8519 mAppTransitionReady = true;
8520 mAppTransitionTimeout = true;
8521 performLayoutAndPlaceSurfacesLocked();
8522 }
8523 }
8524 break;
8525 }
Romain Guy06882f82009-06-10 13:36:04 -07008526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008527 case PERSIST_ANIMATION_SCALE: {
8528 Settings.System.putFloat(mContext.getContentResolver(),
8529 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8530 Settings.System.putFloat(mContext.getContentResolver(),
8531 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8532 break;
8533 }
Romain Guy06882f82009-06-10 13:36:04 -07008534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 case FORCE_GC: {
8536 synchronized(mWindowMap) {
8537 if (mAnimationPending) {
8538 // If we are animating, don't do the gc now but
8539 // delay a bit so we don't interrupt the animation.
8540 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8541 2000);
8542 return;
8543 }
8544 // If we are currently rotating the display, it will
8545 // schedule a new message when done.
8546 if (mDisplayFrozen) {
8547 return;
8548 }
8549 mFreezeGcPending = 0;
8550 }
8551 Runtime.getRuntime().gc();
8552 break;
8553 }
Romain Guy06882f82009-06-10 13:36:04 -07008554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 case ENABLE_SCREEN: {
8556 performEnableScreen();
8557 break;
8558 }
Romain Guy06882f82009-06-10 13:36:04 -07008559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008560 case APP_FREEZE_TIMEOUT: {
8561 synchronized (mWindowMap) {
8562 Log.w(TAG, "App freeze timeout expired.");
8563 int i = mAppTokens.size();
8564 while (i > 0) {
8565 i--;
8566 AppWindowToken tok = mAppTokens.get(i);
8567 if (tok.freezingScreen) {
8568 Log.w(TAG, "Force clearing freeze: " + tok);
8569 unsetAppFreezingScreenLocked(tok, true, true);
8570 }
8571 }
8572 }
8573 break;
8574 }
Romain Guy06882f82009-06-10 13:36:04 -07008575
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008576 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008577 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008578 sendNewConfiguration();
8579 }
8580 break;
8581 }
Romain Guy06882f82009-06-10 13:36:04 -07008582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 }
8584 }
8585 }
8586
8587 // -------------------------------------------------------------
8588 // IWindowManager API
8589 // -------------------------------------------------------------
8590
8591 public IWindowSession openSession(IInputMethodClient client,
8592 IInputContext inputContext) {
8593 if (client == null) throw new IllegalArgumentException("null client");
8594 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8595 return new Session(client, inputContext);
8596 }
8597
8598 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8599 synchronized (mWindowMap) {
8600 // The focus for the client is the window immediately below
8601 // where we would place the input method window.
8602 int idx = findDesiredInputMethodWindowIndexLocked(false);
8603 WindowState imFocus;
8604 if (idx > 0) {
8605 imFocus = (WindowState)mWindows.get(idx-1);
8606 if (imFocus != null) {
8607 if (imFocus.mSession.mClient != null &&
8608 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8609 return true;
8610 }
8611 }
8612 }
8613 }
8614 return false;
8615 }
Romain Guy06882f82009-06-10 13:36:04 -07008616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617 // -------------------------------------------------------------
8618 // Internals
8619 // -------------------------------------------------------------
8620
8621 final WindowState windowForClientLocked(Session session, IWindow client) {
8622 return windowForClientLocked(session, client.asBinder());
8623 }
Romain Guy06882f82009-06-10 13:36:04 -07008624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 final WindowState windowForClientLocked(Session session, IBinder client) {
8626 WindowState win = mWindowMap.get(client);
8627 if (localLOGV) Log.v(
8628 TAG, "Looking up client " + client + ": " + win);
8629 if (win == null) {
8630 RuntimeException ex = new RuntimeException();
8631 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8632 return null;
8633 }
8634 if (session != null && win.mSession != session) {
8635 RuntimeException ex = new RuntimeException();
8636 Log.w(TAG, "Requested window " + client + " is in session " +
8637 win.mSession + ", not " + session, ex);
8638 return null;
8639 }
8640
8641 return win;
8642 }
8643
Dianne Hackborna8f60182009-09-01 19:01:50 -07008644 final void rebuildAppWindowListLocked() {
8645 int NW = mWindows.size();
8646 int i;
8647
8648 // First remove all existing app windows.
8649 i=0;
8650 while (i < NW) {
8651 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008652 WindowState win = (WindowState)mWindows.remove(i);
8653 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8654 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008655 NW--;
8656 continue;
8657 }
8658 i++;
8659 }
8660
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008661 // First add all of the exiting app tokens... these are no longer
8662 // in the main app list, but still have windows shown. We put them
8663 // in the back because now that the animation is over we no longer
8664 // will care about them.
8665 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008666 i = 0;
8667 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008668 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8669 }
8670
8671 // And add in the still active app tokens in Z order.
8672 NT = mAppTokens.size();
8673 for (int j=0; j<NT; j++) {
8674 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008675 }
8676 }
8677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 private final void assignLayersLocked() {
8679 int N = mWindows.size();
8680 int curBaseLayer = 0;
8681 int curLayer = 0;
8682 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 for (i=0; i<N; i++) {
8685 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008686 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8687 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 curLayer += WINDOW_LAYER_MULTIPLIER;
8689 w.mLayer = curLayer;
8690 } else {
8691 curBaseLayer = curLayer = w.mBaseLayer;
8692 w.mLayer = curLayer;
8693 }
8694 if (w.mTargetAppToken != null) {
8695 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8696 } else if (w.mAppToken != null) {
8697 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8698 } else {
8699 w.mAnimLayer = w.mLayer;
8700 }
8701 if (w.mIsImWindow) {
8702 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008703 } else if (w.mIsWallpaper) {
8704 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 }
8706 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8707 + w.mAnimLayer);
8708 //System.out.println(
8709 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8710 }
8711 }
8712
8713 private boolean mInLayout = false;
8714 private final void performLayoutAndPlaceSurfacesLocked() {
8715 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008716 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 throw new RuntimeException("Recursive call!");
8718 }
8719 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8720 return;
8721 }
8722
8723 boolean recoveringMemory = false;
8724 if (mForceRemoves != null) {
8725 recoveringMemory = true;
8726 // Wait a little it for things to settle down, and off we go.
8727 for (int i=0; i<mForceRemoves.size(); i++) {
8728 WindowState ws = mForceRemoves.get(i);
8729 Log.i(TAG, "Force removing: " + ws);
8730 removeWindowInnerLocked(ws.mSession, ws);
8731 }
8732 mForceRemoves = null;
8733 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8734 Object tmp = new Object();
8735 synchronized (tmp) {
8736 try {
8737 tmp.wait(250);
8738 } catch (InterruptedException e) {
8739 }
8740 }
8741 }
Romain Guy06882f82009-06-10 13:36:04 -07008742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008743 mInLayout = true;
8744 try {
8745 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 int i = mPendingRemove.size()-1;
8748 if (i >= 0) {
8749 while (i >= 0) {
8750 WindowState w = mPendingRemove.get(i);
8751 removeWindowInnerLocked(w.mSession, w);
8752 i--;
8753 }
8754 mPendingRemove.clear();
8755
8756 mInLayout = false;
8757 assignLayersLocked();
8758 mLayoutNeeded = true;
8759 performLayoutAndPlaceSurfacesLocked();
8760
8761 } else {
8762 mInLayout = false;
8763 if (mLayoutNeeded) {
8764 requestAnimationLocked(0);
8765 }
8766 }
8767 } catch (RuntimeException e) {
8768 mInLayout = false;
8769 Log.e(TAG, "Unhandled exception while layout out windows", e);
8770 }
8771 }
8772
8773 private final void performLayoutLockedInner() {
8774 final int dw = mDisplay.getWidth();
8775 final int dh = mDisplay.getHeight();
8776
8777 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008778 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 int i;
8780
8781 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008782
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008783 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 mPolicy.beginLayoutLw(dw, dh);
8785
8786 // First perform layout of any root windows (not attached
8787 // to another window).
8788 int topAttached = -1;
8789 for (i = N-1; i >= 0; i--) {
8790 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008791
8792 // Don't do layout of a window if it is not visible, or
8793 // soon won't be visible, to avoid wasting time and funky
8794 // changes while a window is animating away.
8795 final AppWindowToken atoken = win.mAppToken;
8796 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008798 || win.mRootToken.hidden
8799 || (atoken != null && atoken.hiddenRequested)
8800 || !win.mPolicyVisibility
8801 || win.mAttachedHidden
8802 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803
8804 // If this view is GONE, then skip it -- keep the current
8805 // frame, and let the caller know so they can ignore it
8806 // if they want. (We do the normal layout for INVISIBLE
8807 // windows, since that means "perform layout as normal,
8808 // just don't display").
8809 if (!gone || !win.mHaveFrame) {
8810 if (!win.mLayoutAttached) {
8811 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8812 } else {
8813 if (topAttached < 0) topAttached = i;
8814 }
8815 }
8816 }
Romain Guy06882f82009-06-10 13:36:04 -07008817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 // Now perform layout of attached windows, which usually
8819 // depend on the position of the window they are attached to.
8820 // XXX does not deal with windows that are attached to windows
8821 // that are themselves attached.
8822 for (i = topAttached; i >= 0; i--) {
8823 WindowState win = (WindowState) mWindows.get(i);
8824
8825 // If this view is GONE, then skip it -- keep the current
8826 // frame, and let the caller know so they can ignore it
8827 // if they want. (We do the normal layout for INVISIBLE
8828 // windows, since that means "perform layout as normal,
8829 // just don't display").
8830 if (win.mLayoutAttached) {
8831 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8832 || !win.mHaveFrame) {
8833 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8834 }
8835 }
8836 }
8837
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008838 if (!mPolicy.finishLayoutLw()) {
8839 mLayoutNeeded = false;
8840 } else if (repeats > 2) {
8841 Log.w(TAG, "Layout repeat aborted after too many iterations");
8842 mLayoutNeeded = false;
8843 } else {
8844 repeats++;
8845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008846 }
8847 }
Romain Guy06882f82009-06-10 13:36:04 -07008848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008849 private final void performLayoutAndPlaceSurfacesLockedInner(
8850 boolean recoveringMemory) {
8851 final long currentTime = SystemClock.uptimeMillis();
8852 final int dw = mDisplay.getWidth();
8853 final int dh = mDisplay.getHeight();
8854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008855 int i;
8856
8857 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008858 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 if (mFxSession == null) {
8861 mFxSession = new SurfaceSession();
8862 }
Romain Guy06882f82009-06-10 13:36:04 -07008863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8865
8866 // Initialize state of exiting tokens.
8867 for (i=mExitingTokens.size()-1; i>=0; i--) {
8868 mExitingTokens.get(i).hasVisible = false;
8869 }
8870
8871 // Initialize state of exiting applications.
8872 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8873 mExitingAppTokens.get(i).hasVisible = false;
8874 }
8875
8876 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008877 boolean orientationChangeComplete = true;
8878 Session holdScreen = null;
8879 float screenBrightness = -1;
8880 boolean focusDisplayed = false;
8881 boolean animating = false;
8882
8883 Surface.openTransaction();
8884 try {
8885 boolean restart;
8886
8887 do {
8888 final int transactionSequence = ++mTransactionSequence;
8889
8890 // Update animations of all applications, including those
8891 // associated with exiting/removed apps
8892 boolean tokensAnimating = false;
8893 final int NAT = mAppTokens.size();
8894 for (i=0; i<NAT; i++) {
8895 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8896 tokensAnimating = true;
8897 }
8898 }
8899 final int NEAT = mExitingAppTokens.size();
8900 for (i=0; i<NEAT; i++) {
8901 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8902 tokensAnimating = true;
8903 }
8904 }
8905
8906 animating = tokensAnimating;
8907 restart = false;
8908
8909 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008910 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008911
8912 mPolicy.beginAnimationLw(dw, dh);
8913
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008914 final int N = mWindows.size();
8915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 for (i=N-1; i>=0; i--) {
8917 WindowState w = (WindowState)mWindows.get(i);
8918
8919 final WindowManager.LayoutParams attrs = w.mAttrs;
8920
8921 if (w.mSurface != null) {
8922 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008923 if (w.commitFinishDrawingLocked(currentTime)) {
8924 if ((w.mAttrs.flags
8925 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008926 if (DEBUG_WALLPAPER) Log.v(TAG,
8927 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008928 wallpaperMayChange = true;
8929 }
8930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008931 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8932 animating = true;
8933 //w.dump(" ");
8934 }
8935
8936 mPolicy.animatingWindowLw(w, attrs);
8937 }
8938
8939 final AppWindowToken atoken = w.mAppToken;
8940 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8941 if (atoken.lastTransactionSequence != transactionSequence) {
8942 atoken.lastTransactionSequence = transactionSequence;
8943 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8944 atoken.startingDisplayed = false;
8945 }
8946 if ((w.isOnScreen() || w.mAttrs.type
8947 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8948 && !w.mExiting && !w.mDestroying) {
8949 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8950 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8951 + w.isDisplayedLw()
8952 + ", isAnimating=" + w.isAnimating());
8953 if (!w.isDisplayedLw()) {
8954 Log.v(TAG, "Not displayed: s=" + w.mSurface
8955 + " pv=" + w.mPolicyVisibility
8956 + " dp=" + w.mDrawPending
8957 + " cdp=" + w.mCommitDrawPending
8958 + " ah=" + w.mAttachedHidden
8959 + " th=" + atoken.hiddenRequested
8960 + " a=" + w.mAnimating);
8961 }
8962 }
8963 if (w != atoken.startingWindow) {
8964 if (!atoken.freezingScreen || !w.mAppFreezing) {
8965 atoken.numInterestingWindows++;
8966 if (w.isDisplayedLw()) {
8967 atoken.numDrawnWindows++;
8968 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8969 "tokenMayBeDrawn: " + atoken
8970 + " freezingScreen=" + atoken.freezingScreen
8971 + " mAppFreezing=" + w.mAppFreezing);
8972 tokenMayBeDrawn = true;
8973 }
8974 }
8975 } else if (w.isDisplayedLw()) {
8976 atoken.startingDisplayed = true;
8977 }
8978 }
8979 } else if (w.mReadyToShow) {
8980 w.performShowLocked();
8981 }
8982 }
8983
8984 if (mPolicy.finishAnimationLw()) {
8985 restart = true;
8986 }
8987
8988 if (tokenMayBeDrawn) {
8989 // See if any windows have been drawn, so they (and others
8990 // associated with them) can now be shown.
8991 final int NT = mTokenList.size();
8992 for (i=0; i<NT; i++) {
8993 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8994 if (wtoken == null) {
8995 continue;
8996 }
8997 if (wtoken.freezingScreen) {
8998 int numInteresting = wtoken.numInterestingWindows;
8999 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9000 if (DEBUG_VISIBILITY) Log.v(TAG,
9001 "allDrawn: " + wtoken
9002 + " interesting=" + numInteresting
9003 + " drawn=" + wtoken.numDrawnWindows);
9004 wtoken.showAllWindowsLocked();
9005 unsetAppFreezingScreenLocked(wtoken, false, true);
9006 orientationChangeComplete = true;
9007 }
9008 } else if (!wtoken.allDrawn) {
9009 int numInteresting = wtoken.numInterestingWindows;
9010 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9011 if (DEBUG_VISIBILITY) Log.v(TAG,
9012 "allDrawn: " + wtoken
9013 + " interesting=" + numInteresting
9014 + " drawn=" + wtoken.numDrawnWindows);
9015 wtoken.allDrawn = true;
9016 restart = true;
9017
9018 // We can now show all of the drawn windows!
9019 if (!mOpeningApps.contains(wtoken)) {
9020 wtoken.showAllWindowsLocked();
9021 }
9022 }
9023 }
9024 }
9025 }
9026
9027 // If we are ready to perform an app transition, check through
9028 // all of the app tokens to be shown and see if they are ready
9029 // to go.
9030 if (mAppTransitionReady) {
9031 int NN = mOpeningApps.size();
9032 boolean goodToGo = true;
9033 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9034 "Checking " + NN + " opening apps (frozen="
9035 + mDisplayFrozen + " timeout="
9036 + mAppTransitionTimeout + ")...");
9037 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9038 // If the display isn't frozen, wait to do anything until
9039 // all of the apps are ready. Otherwise just go because
9040 // we'll unfreeze the display when everyone is ready.
9041 for (i=0; i<NN && goodToGo; i++) {
9042 AppWindowToken wtoken = mOpeningApps.get(i);
9043 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9044 "Check opening app" + wtoken + ": allDrawn="
9045 + wtoken.allDrawn + " startingDisplayed="
9046 + wtoken.startingDisplayed);
9047 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9048 && !wtoken.startingMoved) {
9049 goodToGo = false;
9050 }
9051 }
9052 }
9053 if (goodToGo) {
9054 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9055 int transit = mNextAppTransition;
9056 if (mSkipAppTransitionAnimation) {
9057 transit = WindowManagerPolicy.TRANSIT_NONE;
9058 }
9059 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9060 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009061 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009062 mAppTransitionTimeout = false;
9063 mStartingIconInTransition = false;
9064 mSkipAppTransitionAnimation = false;
9065
9066 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9067
Dianne Hackborna8f60182009-09-01 19:01:50 -07009068 // If there are applications waiting to come to the
9069 // top of the stack, now is the time to move their windows.
9070 // (Note that we don't do apps going to the bottom
9071 // here -- we want to keep their windows in the old
9072 // Z-order until the animation completes.)
9073 if (mToTopApps.size() > 0) {
9074 NN = mAppTokens.size();
9075 for (i=0; i<NN; i++) {
9076 AppWindowToken wtoken = mAppTokens.get(i);
9077 if (wtoken.sendingToTop) {
9078 wtoken.sendingToTop = false;
9079 moveAppWindowsLocked(wtoken, NN, false);
9080 }
9081 }
9082 mToTopApps.clear();
9083 }
9084
Dianne Hackborn25994b42009-09-04 14:21:19 -07009085 WindowState oldWallpaper = mWallpaperTarget;
9086
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009087 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009088 wallpaperMayChange = false;
9089
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009090 // The top-most window will supply the layout params,
9091 // and we will determine it below.
9092 LayoutParams animLp = null;
9093 int bestAnimLayer = -1;
9094
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009095 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009096 "New wallpaper target=" + mWallpaperTarget
9097 + ", lower target=" + mLowerWallpaperTarget
9098 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009099 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009100 // Do a first pass through the tokens for two
9101 // things:
9102 // (1) Determine if both the closing and opening
9103 // app token sets are wallpaper targets, in which
9104 // case special animations are needed
9105 // (since the wallpaper needs to stay static
9106 // behind them).
9107 // (2) Find the layout params of the top-most
9108 // application window in the tokens, which is
9109 // what will control the animation theme.
9110 final int NC = mClosingApps.size();
9111 NN = NC + mOpeningApps.size();
9112 for (i=0; i<NN; i++) {
9113 AppWindowToken wtoken;
9114 int mode;
9115 if (i < NC) {
9116 wtoken = mClosingApps.get(i);
9117 mode = 1;
9118 } else {
9119 wtoken = mOpeningApps.get(i-NC);
9120 mode = 2;
9121 }
9122 if (mLowerWallpaperTarget != null) {
9123 if (mLowerWallpaperTarget.mAppToken == wtoken
9124 || mUpperWallpaperTarget.mAppToken == wtoken) {
9125 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009126 }
9127 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009128 if (wtoken.appFullscreen) {
9129 WindowState ws = wtoken.findMainWindow();
9130 if (ws != null) {
9131 // If this is a compatibility mode
9132 // window, we will always use its anim.
9133 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9134 animLp = ws.mAttrs;
9135 bestAnimLayer = Integer.MAX_VALUE;
9136 } else if (ws.mLayer > bestAnimLayer) {
9137 animLp = ws.mAttrs;
9138 bestAnimLayer = ws.mLayer;
9139 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009140 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009141 }
9142 }
9143
Dianne Hackborn25994b42009-09-04 14:21:19 -07009144 if (foundWallpapers == 3) {
9145 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9146 "Wallpaper animation!");
9147 switch (transit) {
9148 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9149 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9150 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9151 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9152 break;
9153 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9154 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9155 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9156 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9157 break;
9158 }
9159 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9160 "New transit: " + transit);
9161 } else if (oldWallpaper != null) {
9162 // We are transitioning from an activity with
9163 // a wallpaper to one without.
9164 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9165 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9166 "New transit away from wallpaper: " + transit);
9167 } else if (mWallpaperTarget != null) {
9168 // We are transitioning from an activity without
9169 // a wallpaper to now showing the wallpaper
9170 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9171 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9172 "New transit into wallpaper: " + transit);
9173 }
9174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009175 NN = mOpeningApps.size();
9176 for (i=0; i<NN; i++) {
9177 AppWindowToken wtoken = mOpeningApps.get(i);
9178 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9179 "Now opening app" + wtoken);
9180 wtoken.reportedVisible = false;
9181 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009182 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009183 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009185 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 wtoken.showAllWindowsLocked();
9187 }
9188 NN = mClosingApps.size();
9189 for (i=0; i<NN; i++) {
9190 AppWindowToken wtoken = mClosingApps.get(i);
9191 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9192 "Now closing app" + wtoken);
9193 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009194 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009195 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009196 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009197 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009198 // Force the allDrawn flag, because we want to start
9199 // this guy's animations regardless of whether it's
9200 // gotten drawn.
9201 wtoken.allDrawn = true;
9202 }
9203
9204 mOpeningApps.clear();
9205 mClosingApps.clear();
9206
9207 // This has changed the visibility of windows, so perform
9208 // a new layout to get them all up-to-date.
9209 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009210 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9211 assignLayersLocked();
9212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009213 performLayoutLockedInner();
9214 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9215
9216 restart = true;
9217 }
9218 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009219
Dianne Hackborna8f60182009-09-01 19:01:50 -07009220 if (!animating && mAppTransitionRunning) {
9221 // We have finished the animation of an app transition. To do
9222 // this, we have delayed a lot of operations like showing and
9223 // hiding apps, moving apps in Z-order, etc. The app token list
9224 // reflects the correct Z-order, but the window list may now
9225 // be out of sync with it. So here we will just rebuild the
9226 // entire app window list. Fun!
9227 mAppTransitionRunning = false;
9228 // Clear information about apps that were moving.
9229 mToBottomApps.clear();
9230
9231 rebuildAppWindowListLocked();
9232 restart = true;
9233 moveInputMethodWindowsIfNeededLocked(false);
9234 wallpaperMayChange = true;
9235 mLayoutNeeded = true;
9236 }
9237
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009238 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009239 if (DEBUG_WALLPAPER) Log.v(TAG,
9240 "Wallpaper may change! Adjusting");
9241 int adjResult = adjustWallpaperWindowsLocked();
9242 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9243 if (DEBUG_WALLPAPER) Log.v(TAG,
9244 "Wallpaper layer changed: assigning layers + relayout");
9245 restart = true;
9246 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009247 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009248 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9249 if (DEBUG_WALLPAPER) Log.v(TAG,
9250 "Wallpaper visibility changed: relayout");
9251 restart = true;
9252 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009253 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009254 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009255 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009256 performLayoutLockedInner();
9257 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009258 }
9259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 } while (restart);
9261
9262 // THIRD LOOP: Update the surfaces of all windows.
9263
9264 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9265
9266 boolean obscured = false;
9267 boolean blurring = false;
9268 boolean dimming = false;
9269 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009270 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009271 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009272
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009273 final int N = mWindows.size();
9274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009275 for (i=N-1; i>=0; i--) {
9276 WindowState w = (WindowState)mWindows.get(i);
9277
9278 boolean displayed = false;
9279 final WindowManager.LayoutParams attrs = w.mAttrs;
9280 final int attrFlags = attrs.flags;
9281
9282 if (w.mSurface != null) {
9283 w.computeShownFrameLocked();
9284 if (localLOGV) Log.v(
9285 TAG, "Placing surface #" + i + " " + w.mSurface
9286 + ": new=" + w.mShownFrame + ", old="
9287 + w.mLastShownFrame);
9288
9289 boolean resize;
9290 int width, height;
9291 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9292 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9293 w.mLastRequestedHeight != w.mRequestedHeight;
9294 // for a scaled surface, we just want to use
9295 // the requested size.
9296 width = w.mRequestedWidth;
9297 height = w.mRequestedHeight;
9298 w.mLastRequestedWidth = width;
9299 w.mLastRequestedHeight = height;
9300 w.mLastShownFrame.set(w.mShownFrame);
9301 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009302 if (SHOW_TRANSACTIONS) Log.i(
9303 TAG, " SURFACE " + w.mSurface
9304 + ": POS " + w.mShownFrame.left
9305 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9307 } catch (RuntimeException e) {
9308 Log.w(TAG, "Error positioning surface in " + w, e);
9309 if (!recoveringMemory) {
9310 reclaimSomeSurfaceMemoryLocked(w, "position");
9311 }
9312 }
9313 } else {
9314 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9315 width = w.mShownFrame.width();
9316 height = w.mShownFrame.height();
9317 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 }
9319
9320 if (resize) {
9321 if (width < 1) width = 1;
9322 if (height < 1) height = 1;
9323 if (w.mSurface != null) {
9324 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009325 if (SHOW_TRANSACTIONS) Log.i(
9326 TAG, " SURFACE " + w.mSurface + ": POS "
9327 + w.mShownFrame.left + ","
9328 + w.mShownFrame.top + " SIZE "
9329 + w.mShownFrame.width() + "x"
9330 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 w.mSurface.setSize(width, height);
9332 w.mSurface.setPosition(w.mShownFrame.left,
9333 w.mShownFrame.top);
9334 } catch (RuntimeException e) {
9335 // If something goes wrong with the surface (such
9336 // as running out of memory), don't take down the
9337 // entire system.
9338 Log.e(TAG, "Failure updating surface of " + w
9339 + "size=(" + width + "x" + height
9340 + "), pos=(" + w.mShownFrame.left
9341 + "," + w.mShownFrame.top + ")", e);
9342 if (!recoveringMemory) {
9343 reclaimSomeSurfaceMemoryLocked(w, "size");
9344 }
9345 }
9346 }
9347 }
9348 if (!w.mAppFreezing) {
9349 w.mContentInsetsChanged =
9350 !w.mLastContentInsets.equals(w.mContentInsets);
9351 w.mVisibleInsetsChanged =
9352 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009353 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 || w.mContentInsetsChanged
9355 || w.mVisibleInsetsChanged) {
9356 w.mLastFrame.set(w.mFrame);
9357 w.mLastContentInsets.set(w.mContentInsets);
9358 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009359 // If the screen is currently frozen, then keep
9360 // it frozen until this window draws at its new
9361 // orientation.
9362 if (mDisplayFrozen) {
9363 if (DEBUG_ORIENTATION) Log.v(TAG,
9364 "Resizing while display frozen: " + w);
9365 w.mOrientationChanging = true;
9366 if (mWindowsFreezingScreen) {
9367 mWindowsFreezingScreen = true;
9368 // XXX should probably keep timeout from
9369 // when we first froze the display.
9370 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9371 mH.sendMessageDelayed(mH.obtainMessage(
9372 H.WINDOW_FREEZE_TIMEOUT), 2000);
9373 }
9374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 // If the orientation is changing, then we need to
9376 // hold off on unfreezing the display until this
9377 // window has been redrawn; to do that, we need
9378 // to go through the process of getting informed
9379 // by the application when it has finished drawing.
9380 if (w.mOrientationChanging) {
9381 if (DEBUG_ORIENTATION) Log.v(TAG,
9382 "Orientation start waiting for draw in "
9383 + w + ", surface " + w.mSurface);
9384 w.mDrawPending = true;
9385 w.mCommitDrawPending = false;
9386 w.mReadyToShow = false;
9387 if (w.mAppToken != null) {
9388 w.mAppToken.allDrawn = false;
9389 }
9390 }
Romain Guy06882f82009-06-10 13:36:04 -07009391 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392 "Resizing window " + w + " to " + w.mFrame);
9393 mResizingWindows.add(w);
9394 } else if (w.mOrientationChanging) {
9395 if (!w.mDrawPending && !w.mCommitDrawPending) {
9396 if (DEBUG_ORIENTATION) Log.v(TAG,
9397 "Orientation not waiting for draw in "
9398 + w + ", surface " + w.mSurface);
9399 w.mOrientationChanging = false;
9400 }
9401 }
9402 }
9403
9404 if (w.mAttachedHidden) {
9405 if (!w.mLastHidden) {
9406 //dump();
9407 w.mLastHidden = true;
9408 if (SHOW_TRANSACTIONS) Log.i(
9409 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9410 if (w.mSurface != null) {
9411 try {
9412 w.mSurface.hide();
9413 } catch (RuntimeException e) {
9414 Log.w(TAG, "Exception hiding surface in " + w);
9415 }
9416 }
9417 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9418 }
9419 // If we are waiting for this window to handle an
9420 // orientation change, well, it is hidden, so
9421 // doesn't really matter. Note that this does
9422 // introduce a potential glitch if the window
9423 // becomes unhidden before it has drawn for the
9424 // new orientation.
9425 if (w.mOrientationChanging) {
9426 w.mOrientationChanging = false;
9427 if (DEBUG_ORIENTATION) Log.v(TAG,
9428 "Orientation change skips hidden " + w);
9429 }
9430 } else if (!w.isReadyForDisplay()) {
9431 if (!w.mLastHidden) {
9432 //dump();
9433 w.mLastHidden = true;
9434 if (SHOW_TRANSACTIONS) Log.i(
9435 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9436 if (w.mSurface != null) {
9437 try {
9438 w.mSurface.hide();
9439 } catch (RuntimeException e) {
9440 Log.w(TAG, "Exception exception hiding surface in " + w);
9441 }
9442 }
9443 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9444 }
9445 // If we are waiting for this window to handle an
9446 // orientation change, well, it is hidden, so
9447 // doesn't really matter. Note that this does
9448 // introduce a potential glitch if the window
9449 // becomes unhidden before it has drawn for the
9450 // new orientation.
9451 if (w.mOrientationChanging) {
9452 w.mOrientationChanging = false;
9453 if (DEBUG_ORIENTATION) Log.v(TAG,
9454 "Orientation change skips hidden " + w);
9455 }
9456 } else if (w.mLastLayer != w.mAnimLayer
9457 || w.mLastAlpha != w.mShownAlpha
9458 || w.mLastDsDx != w.mDsDx
9459 || w.mLastDtDx != w.mDtDx
9460 || w.mLastDsDy != w.mDsDy
9461 || w.mLastDtDy != w.mDtDy
9462 || w.mLastHScale != w.mHScale
9463 || w.mLastVScale != w.mVScale
9464 || w.mLastHidden) {
9465 displayed = true;
9466 w.mLastAlpha = w.mShownAlpha;
9467 w.mLastLayer = w.mAnimLayer;
9468 w.mLastDsDx = w.mDsDx;
9469 w.mLastDtDx = w.mDtDx;
9470 w.mLastDsDy = w.mDsDy;
9471 w.mLastDtDy = w.mDtDy;
9472 w.mLastHScale = w.mHScale;
9473 w.mLastVScale = w.mVScale;
9474 if (SHOW_TRANSACTIONS) Log.i(
9475 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009476 + w.mShownAlpha + " layer=" + w.mAnimLayer
9477 + " matrix=[" + (w.mDsDx*w.mHScale)
9478 + "," + (w.mDtDx*w.mVScale)
9479 + "][" + (w.mDsDy*w.mHScale)
9480 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 if (w.mSurface != null) {
9482 try {
9483 w.mSurface.setAlpha(w.mShownAlpha);
9484 w.mSurface.setLayer(w.mAnimLayer);
9485 w.mSurface.setMatrix(
9486 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9487 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9488 } catch (RuntimeException e) {
9489 Log.w(TAG, "Error updating surface in " + w, e);
9490 if (!recoveringMemory) {
9491 reclaimSomeSurfaceMemoryLocked(w, "update");
9492 }
9493 }
9494 }
9495
9496 if (w.mLastHidden && !w.mDrawPending
9497 && !w.mCommitDrawPending
9498 && !w.mReadyToShow) {
9499 if (SHOW_TRANSACTIONS) Log.i(
9500 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9501 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9502 + " during relayout");
9503 if (showSurfaceRobustlyLocked(w)) {
9504 w.mHasDrawn = true;
9505 w.mLastHidden = false;
9506 } else {
9507 w.mOrientationChanging = false;
9508 }
9509 }
9510 if (w.mSurface != null) {
9511 w.mToken.hasVisible = true;
9512 }
9513 } else {
9514 displayed = true;
9515 }
9516
9517 if (displayed) {
9518 if (!covered) {
9519 if (attrs.width == LayoutParams.FILL_PARENT
9520 && attrs.height == LayoutParams.FILL_PARENT) {
9521 covered = true;
9522 }
9523 }
9524 if (w.mOrientationChanging) {
9525 if (w.mDrawPending || w.mCommitDrawPending) {
9526 orientationChangeComplete = false;
9527 if (DEBUG_ORIENTATION) Log.v(TAG,
9528 "Orientation continue waiting for draw in " + w);
9529 } else {
9530 w.mOrientationChanging = false;
9531 if (DEBUG_ORIENTATION) Log.v(TAG,
9532 "Orientation change complete in " + w);
9533 }
9534 }
9535 w.mToken.hasVisible = true;
9536 }
9537 } else if (w.mOrientationChanging) {
9538 if (DEBUG_ORIENTATION) Log.v(TAG,
9539 "Orientation change skips hidden " + w);
9540 w.mOrientationChanging = false;
9541 }
9542
9543 final boolean canBeSeen = w.isDisplayedLw();
9544
9545 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9546 focusDisplayed = true;
9547 }
9548
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009549 final boolean obscuredChanged = w.mObscured != obscured;
9550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009551 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009552 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 if (w.mSurface != null) {
9554 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9555 holdScreen = w.mSession;
9556 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009557 if (!syswin && w.mAttrs.screenBrightness >= 0
9558 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 screenBrightness = w.mAttrs.screenBrightness;
9560 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009561 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9562 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9563 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9564 syswin = true;
9565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009566 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009567
Dianne Hackborn25994b42009-09-04 14:21:19 -07009568 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9569 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009570 // This window completely covers everything behind it,
9571 // so we want to leave all of them as unblurred (for
9572 // performance reasons).
9573 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009574 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9575 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009576 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009577 obscured = true;
9578 if (mBackgroundFillerSurface == null) {
9579 try {
9580 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9581 0, dw, dh,
9582 PixelFormat.OPAQUE,
9583 Surface.FX_SURFACE_NORMAL);
9584 } catch (Exception e) {
9585 Log.e(TAG, "Exception creating filler surface", e);
9586 }
9587 }
9588 try {
9589 mBackgroundFillerSurface.setPosition(0, 0);
9590 mBackgroundFillerSurface.setSize(dw, dh);
9591 // Using the same layer as Dim because they will never be shown at the
9592 // same time.
9593 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9594 mBackgroundFillerSurface.show();
9595 } catch (RuntimeException e) {
9596 Log.e(TAG, "Exception showing filler surface");
9597 }
9598 backgroundFillerShown = true;
9599 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009600 } else if (canBeSeen && !obscured &&
9601 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9602 if (localLOGV) Log.v(TAG, "Win " + w
9603 + ": blurring=" + blurring
9604 + " obscured=" + obscured
9605 + " displayed=" + displayed);
9606 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9607 if (!dimming) {
9608 //Log.i(TAG, "DIM BEHIND: " + w);
9609 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009610 if (mDimAnimator == null) {
9611 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009613 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009614 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009615 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009616 }
9617 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9618 if (!blurring) {
9619 //Log.i(TAG, "BLUR BEHIND: " + w);
9620 blurring = true;
9621 mBlurShown = true;
9622 if (mBlurSurface == null) {
9623 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9624 + mBlurSurface + ": CREATE");
9625 try {
Romain Guy06882f82009-06-10 13:36:04 -07009626 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009627 -1, 16, 16,
9628 PixelFormat.OPAQUE,
9629 Surface.FX_SURFACE_BLUR);
9630 } catch (Exception e) {
9631 Log.e(TAG, "Exception creating Blur surface", e);
9632 }
9633 }
9634 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9635 + mBlurSurface + ": SHOW pos=(0,0) (" +
9636 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9637 if (mBlurSurface != null) {
9638 mBlurSurface.setPosition(0, 0);
9639 mBlurSurface.setSize(dw, dh);
9640 try {
9641 mBlurSurface.show();
9642 } catch (RuntimeException e) {
9643 Log.w(TAG, "Failure showing blur surface", e);
9644 }
9645 }
9646 }
9647 mBlurSurface.setLayer(w.mAnimLayer-2);
9648 }
9649 }
9650 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009651
9652 if (obscuredChanged && mWallpaperTarget == w) {
9653 // This is the wallpaper target and its obscured state
9654 // changed... make sure the current wallaper's visibility
9655 // has been updated accordingly.
9656 updateWallpaperVisibilityLocked();
9657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009658 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009659
9660 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9661 mBackgroundFillerShown = false;
9662 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9663 try {
9664 mBackgroundFillerSurface.hide();
9665 } catch (RuntimeException e) {
9666 Log.e(TAG, "Exception hiding filler surface", e);
9667 }
9668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009670 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9671 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009672 }
Romain Guy06882f82009-06-10 13:36:04 -07009673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 if (!blurring && mBlurShown) {
9675 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9676 + ": HIDE");
9677 try {
9678 mBlurSurface.hide();
9679 } catch (IllegalArgumentException e) {
9680 Log.w(TAG, "Illegal argument exception hiding blur surface");
9681 }
9682 mBlurShown = false;
9683 }
9684
9685 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9686 } catch (RuntimeException e) {
9687 Log.e(TAG, "Unhandled exception in Window Manager", e);
9688 }
9689
9690 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9693 "With display frozen, orientationChangeComplete="
9694 + orientationChangeComplete);
9695 if (orientationChangeComplete) {
9696 if (mWindowsFreezingScreen) {
9697 mWindowsFreezingScreen = false;
9698 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9699 }
9700 if (mAppsFreezingScreen == 0) {
9701 stopFreezingDisplayLocked();
9702 }
9703 }
Romain Guy06882f82009-06-10 13:36:04 -07009704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 i = mResizingWindows.size();
9706 if (i > 0) {
9707 do {
9708 i--;
9709 WindowState win = mResizingWindows.get(i);
9710 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009711 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9712 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009713 win.mClient.resized(win.mFrame.width(),
9714 win.mFrame.height(), win.mLastContentInsets,
9715 win.mLastVisibleInsets, win.mDrawPending);
9716 win.mContentInsetsChanged = false;
9717 win.mVisibleInsetsChanged = false;
9718 } catch (RemoteException e) {
9719 win.mOrientationChanging = false;
9720 }
9721 } while (i > 0);
9722 mResizingWindows.clear();
9723 }
Romain Guy06882f82009-06-10 13:36:04 -07009724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009726 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009727 i = mDestroySurface.size();
9728 if (i > 0) {
9729 do {
9730 i--;
9731 WindowState win = mDestroySurface.get(i);
9732 win.mDestroying = false;
9733 if (mInputMethodWindow == win) {
9734 mInputMethodWindow = null;
9735 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009736 if (win == mWallpaperTarget) {
9737 wallpaperDestroyed = true;
9738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 win.destroySurfaceLocked();
9740 } while (i > 0);
9741 mDestroySurface.clear();
9742 }
9743
9744 // Time to remove any exiting tokens?
9745 for (i=mExitingTokens.size()-1; i>=0; i--) {
9746 WindowToken token = mExitingTokens.get(i);
9747 if (!token.hasVisible) {
9748 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009749 if (token.windowType == TYPE_WALLPAPER) {
9750 mWallpaperTokens.remove(token);
9751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 }
9753 }
9754
9755 // Time to remove any exiting applications?
9756 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9757 AppWindowToken token = mExitingAppTokens.get(i);
9758 if (!token.hasVisible && !mClosingApps.contains(token)) {
9759 mAppTokens.remove(token);
9760 mExitingAppTokens.remove(i);
9761 }
9762 }
9763
Dianne Hackborna8f60182009-09-01 19:01:50 -07009764 boolean needRelayout = false;
9765
9766 if (!animating && mAppTransitionRunning) {
9767 // We have finished the animation of an app transition. To do
9768 // this, we have delayed a lot of operations like showing and
9769 // hiding apps, moving apps in Z-order, etc. The app token list
9770 // reflects the correct Z-order, but the window list may now
9771 // be out of sync with it. So here we will just rebuild the
9772 // entire app window list. Fun!
9773 mAppTransitionRunning = false;
9774 needRelayout = true;
9775 rebuildAppWindowListLocked();
9776 // Clear information about apps that were moving.
9777 mToBottomApps.clear();
9778 }
9779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 if (focusDisplayed) {
9781 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9782 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009783 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009784 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009785 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009786 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009787 requestAnimationLocked(0);
9788 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009789 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9790 }
9791 mQueue.setHoldScreenLocked(holdScreen != null);
9792 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9793 mPowerManager.setScreenBrightnessOverride(-1);
9794 } else {
9795 mPowerManager.setScreenBrightnessOverride((int)
9796 (screenBrightness * Power.BRIGHTNESS_ON));
9797 }
9798 if (holdScreen != mHoldingScreenOn) {
9799 mHoldingScreenOn = holdScreen;
9800 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9801 mH.sendMessage(m);
9802 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009803
9804 if (mTurnOnScreen) {
9805 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
9806 LocalPowerManager.BUTTON_EVENT, true);
9807 mTurnOnScreen = false;
9808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 }
9810
9811 void requestAnimationLocked(long delay) {
9812 if (!mAnimationPending) {
9813 mAnimationPending = true;
9814 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9815 }
9816 }
Romain Guy06882f82009-06-10 13:36:04 -07009817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 /**
9819 * Have the surface flinger show a surface, robustly dealing with
9820 * error conditions. In particular, if there is not enough memory
9821 * to show the surface, then we will try to get rid of other surfaces
9822 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009824 * @return Returns true if the surface was successfully shown.
9825 */
9826 boolean showSurfaceRobustlyLocked(WindowState win) {
9827 try {
9828 if (win.mSurface != null) {
9829 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009830 if (win.mTurnOnScreen) {
9831 win.mTurnOnScreen = false;
9832 mTurnOnScreen = true;
9833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 }
9835 return true;
9836 } catch (RuntimeException e) {
9837 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9838 }
Romain Guy06882f82009-06-10 13:36:04 -07009839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009842 return false;
9843 }
Romain Guy06882f82009-06-10 13:36:04 -07009844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9846 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009848 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9849 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 if (mForceRemoves == null) {
9852 mForceRemoves = new ArrayList<WindowState>();
9853 }
Romain Guy06882f82009-06-10 13:36:04 -07009854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 long callingIdentity = Binder.clearCallingIdentity();
9856 try {
9857 // There was some problem... first, do a sanity check of the
9858 // window list to make sure we haven't left any dangling surfaces
9859 // around.
9860 int N = mWindows.size();
9861 boolean leakedSurface = false;
9862 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9863 for (int i=0; i<N; i++) {
9864 WindowState ws = (WindowState)mWindows.get(i);
9865 if (ws.mSurface != null) {
9866 if (!mSessions.contains(ws.mSession)) {
9867 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9868 + ws + " surface=" + ws.mSurface
9869 + " token=" + win.mToken
9870 + " pid=" + ws.mSession.mPid
9871 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009872 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 ws.mSurface = null;
9874 mForceRemoves.add(ws);
9875 i--;
9876 N--;
9877 leakedSurface = true;
9878 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9879 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9880 + ws + " surface=" + ws.mSurface
9881 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009882 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 ws.mSurface = null;
9884 leakedSurface = true;
9885 }
9886 }
9887 }
Romain Guy06882f82009-06-10 13:36:04 -07009888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009889 boolean killedApps = false;
9890 if (!leakedSurface) {
9891 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9892 SparseIntArray pidCandidates = new SparseIntArray();
9893 for (int i=0; i<N; i++) {
9894 WindowState ws = (WindowState)mWindows.get(i);
9895 if (ws.mSurface != null) {
9896 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9897 }
9898 }
9899 if (pidCandidates.size() > 0) {
9900 int[] pids = new int[pidCandidates.size()];
9901 for (int i=0; i<pids.length; i++) {
9902 pids[i] = pidCandidates.keyAt(i);
9903 }
9904 try {
9905 if (mActivityManager.killPidsForMemory(pids)) {
9906 killedApps = true;
9907 }
9908 } catch (RemoteException e) {
9909 }
9910 }
9911 }
Romain Guy06882f82009-06-10 13:36:04 -07009912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 if (leakedSurface || killedApps) {
9914 // We managed to reclaim some memory, so get rid of the trouble
9915 // surface and ask the app to request another one.
9916 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9917 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009918 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 win.mSurface = null;
9920 }
Romain Guy06882f82009-06-10 13:36:04 -07009921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 try {
9923 win.mClient.dispatchGetNewSurface();
9924 } catch (RemoteException e) {
9925 }
9926 }
9927 } finally {
9928 Binder.restoreCallingIdentity(callingIdentity);
9929 }
9930 }
Romain Guy06882f82009-06-10 13:36:04 -07009931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009932 private boolean updateFocusedWindowLocked(int mode) {
9933 WindowState newFocus = computeFocusedWindowLocked();
9934 if (mCurrentFocus != newFocus) {
9935 // This check makes sure that we don't already have the focus
9936 // change message pending.
9937 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9938 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9939 if (localLOGV) Log.v(
9940 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9941 final WindowState oldFocus = mCurrentFocus;
9942 mCurrentFocus = newFocus;
9943 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 final WindowState imWindow = mInputMethodWindow;
9946 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009947 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009949 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9950 mLayoutNeeded = true;
9951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9953 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009954 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9955 // Client will do the layout, but we need to assign layers
9956 // for handleNewWindowLocked() below.
9957 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 }
9959 }
Romain Guy06882f82009-06-10 13:36:04 -07009960
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009961 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9962 mKeyWaiter.handleNewWindowLocked(newFocus);
9963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 return true;
9965 }
9966 return false;
9967 }
9968
9969 private WindowState computeFocusedWindowLocked() {
9970 WindowState result = null;
9971 WindowState win;
9972
9973 int i = mWindows.size() - 1;
9974 int nextAppIndex = mAppTokens.size()-1;
9975 WindowToken nextApp = nextAppIndex >= 0
9976 ? mAppTokens.get(nextAppIndex) : null;
9977
9978 while (i >= 0) {
9979 win = (WindowState)mWindows.get(i);
9980
9981 if (localLOGV || DEBUG_FOCUS) Log.v(
9982 TAG, "Looking for focus: " + i
9983 + " = " + win
9984 + ", flags=" + win.mAttrs.flags
9985 + ", canReceive=" + win.canReceiveKeys());
9986
9987 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 // If this window's application has been removed, just skip it.
9990 if (thisApp != null && thisApp.removed) {
9991 i--;
9992 continue;
9993 }
Romain Guy06882f82009-06-10 13:36:04 -07009994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 // If there is a focused app, don't allow focus to go to any
9996 // windows below it. If this is an application window, step
9997 // through the app tokens until we find its app.
9998 if (thisApp != null && nextApp != null && thisApp != nextApp
9999 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10000 int origAppIndex = nextAppIndex;
10001 while (nextAppIndex > 0) {
10002 if (nextApp == mFocusedApp) {
10003 // Whoops, we are below the focused app... no focus
10004 // for you!
10005 if (localLOGV || DEBUG_FOCUS) Log.v(
10006 TAG, "Reached focused app: " + mFocusedApp);
10007 return null;
10008 }
10009 nextAppIndex--;
10010 nextApp = mAppTokens.get(nextAppIndex);
10011 if (nextApp == thisApp) {
10012 break;
10013 }
10014 }
10015 if (thisApp != nextApp) {
10016 // Uh oh, the app token doesn't exist! This shouldn't
10017 // happen, but if it does we can get totally hosed...
10018 // so restart at the original app.
10019 nextAppIndex = origAppIndex;
10020 nextApp = mAppTokens.get(nextAppIndex);
10021 }
10022 }
10023
10024 // Dispatch to this window if it is wants key events.
10025 if (win.canReceiveKeys()) {
10026 if (DEBUG_FOCUS) Log.v(
10027 TAG, "Found focus @ " + i + " = " + win);
10028 result = win;
10029 break;
10030 }
10031
10032 i--;
10033 }
10034
10035 return result;
10036 }
10037
10038 private void startFreezingDisplayLocked() {
10039 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010040 // Freezing the display also suspends key event delivery, to
10041 // keep events from going astray while the display is reconfigured.
10042 // If someone has changed orientation again while the screen is
10043 // still frozen, the events will continue to be blocked while the
10044 // successive orientation change is processed. To prevent spurious
10045 // ANRs, we reset the event dispatch timeout in this case.
10046 synchronized (mKeyWaiter) {
10047 mKeyWaiter.mWasFrozen = true;
10048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010049 return;
10050 }
Romain Guy06882f82009-06-10 13:36:04 -070010051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 long now = SystemClock.uptimeMillis();
10055 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10056 if (mFreezeGcPending != 0) {
10057 if (now > (mFreezeGcPending+1000)) {
10058 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10059 mH.removeMessages(H.FORCE_GC);
10060 Runtime.getRuntime().gc();
10061 mFreezeGcPending = now;
10062 }
10063 } else {
10064 mFreezeGcPending = now;
10065 }
Romain Guy06882f82009-06-10 13:36:04 -070010066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 mDisplayFrozen = true;
10068 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
10069 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
10070 mAppTransitionReady = true;
10071 }
Romain Guy06882f82009-06-10 13:36:04 -070010072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 if (PROFILE_ORIENTATION) {
10074 File file = new File("/data/system/frozen");
10075 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10076 }
10077 Surface.freezeDisplay(0);
10078 }
Romain Guy06882f82009-06-10 13:36:04 -070010079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080 private void stopFreezingDisplayLocked() {
10081 if (!mDisplayFrozen) {
10082 return;
10083 }
Romain Guy06882f82009-06-10 13:36:04 -070010084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 mDisplayFrozen = false;
10086 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10087 if (PROFILE_ORIENTATION) {
10088 Debug.stopMethodTracing();
10089 }
10090 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010091
Chris Tate2ad63a92009-03-25 17:36:48 -070010092 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10093 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 synchronized (mKeyWaiter) {
10095 mKeyWaiter.mWasFrozen = true;
10096 mKeyWaiter.notifyAll();
10097 }
10098
10099 // A little kludge: a lot could have happened while the
10100 // display was frozen, so now that we are coming back we
10101 // do a gc so that any remote references the system
10102 // processes holds on others can be released if they are
10103 // no longer needed.
10104 mH.removeMessages(H.FORCE_GC);
10105 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10106 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 mScreenFrozenLock.release();
10109 }
Romain Guy06882f82009-06-10 13:36:04 -070010110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111 @Override
10112 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10113 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10114 != PackageManager.PERMISSION_GRANTED) {
10115 pw.println("Permission Denial: can't dump WindowManager from from pid="
10116 + Binder.getCallingPid()
10117 + ", uid=" + Binder.getCallingUid());
10118 return;
10119 }
Romain Guy06882f82009-06-10 13:36:04 -070010120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 synchronized(mWindowMap) {
10122 pw.println("Current Window Manager state:");
10123 for (int i=mWindows.size()-1; i>=0; i--) {
10124 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010125 pw.print(" Window #"); pw.print(i); pw.print(' ');
10126 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010127 w.dump(pw, " ");
10128 }
10129 if (mInputMethodDialogs.size() > 0) {
10130 pw.println(" ");
10131 pw.println(" Input method dialogs:");
10132 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10133 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010134 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 }
10136 }
10137 if (mPendingRemove.size() > 0) {
10138 pw.println(" ");
10139 pw.println(" Remove pending for:");
10140 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10141 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010142 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10143 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144 w.dump(pw, " ");
10145 }
10146 }
10147 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10148 pw.println(" ");
10149 pw.println(" Windows force removing:");
10150 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10151 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010152 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10153 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 w.dump(pw, " ");
10155 }
10156 }
10157 if (mDestroySurface.size() > 0) {
10158 pw.println(" ");
10159 pw.println(" Windows waiting to destroy their surface:");
10160 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10161 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010162 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10163 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 w.dump(pw, " ");
10165 }
10166 }
10167 if (mLosingFocus.size() > 0) {
10168 pw.println(" ");
10169 pw.println(" Windows losing focus:");
10170 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10171 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010172 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10173 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 w.dump(pw, " ");
10175 }
10176 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010177 if (mResizingWindows.size() > 0) {
10178 pw.println(" ");
10179 pw.println(" Windows waiting to resize:");
10180 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10181 WindowState w = mResizingWindows.get(i);
10182 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10183 pw.print(w); pw.println(":");
10184 w.dump(pw, " ");
10185 }
10186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 if (mSessions.size() > 0) {
10188 pw.println(" ");
10189 pw.println(" All active sessions:");
10190 Iterator<Session> it = mSessions.iterator();
10191 while (it.hasNext()) {
10192 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010193 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 s.dump(pw, " ");
10195 }
10196 }
10197 if (mTokenMap.size() > 0) {
10198 pw.println(" ");
10199 pw.println(" All tokens:");
10200 Iterator<WindowToken> it = mTokenMap.values().iterator();
10201 while (it.hasNext()) {
10202 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010203 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010204 token.dump(pw, " ");
10205 }
10206 }
10207 if (mTokenList.size() > 0) {
10208 pw.println(" ");
10209 pw.println(" Window token list:");
10210 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010211 pw.print(" #"); pw.print(i); pw.print(": ");
10212 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010213 }
10214 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010215 if (mWallpaperTokens.size() > 0) {
10216 pw.println(" ");
10217 pw.println(" Wallpaper tokens:");
10218 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10219 WindowToken token = mWallpaperTokens.get(i);
10220 pw.print(" Wallpaper #"); pw.print(i);
10221 pw.print(' '); pw.print(token); pw.println(':');
10222 token.dump(pw, " ");
10223 }
10224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 if (mAppTokens.size() > 0) {
10226 pw.println(" ");
10227 pw.println(" Application tokens in Z order:");
10228 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010229 pw.print(" App #"); pw.print(i); pw.print(": ");
10230 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 }
10232 }
10233 if (mFinishedStarting.size() > 0) {
10234 pw.println(" ");
10235 pw.println(" Finishing start of application tokens:");
10236 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10237 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010238 pw.print(" Finished Starting #"); pw.print(i);
10239 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010240 token.dump(pw, " ");
10241 }
10242 }
10243 if (mExitingTokens.size() > 0) {
10244 pw.println(" ");
10245 pw.println(" Exiting tokens:");
10246 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10247 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010248 pw.print(" Exiting #"); pw.print(i);
10249 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 token.dump(pw, " ");
10251 }
10252 }
10253 if (mExitingAppTokens.size() > 0) {
10254 pw.println(" ");
10255 pw.println(" Exiting application tokens:");
10256 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10257 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010258 pw.print(" Exiting App #"); pw.print(i);
10259 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010260 token.dump(pw, " ");
10261 }
10262 }
10263 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010264 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10265 pw.print(" mLastFocus="); pw.println(mLastFocus);
10266 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10267 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10268 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010269 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010270 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10271 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10272 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10273 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010274 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10275 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10276 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10277 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10278 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010279 if (mDimAnimator != null) {
10280 mDimAnimator.printTo(pw);
10281 } else {
10282 pw.print( " no DimAnimator ");
10283 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010284 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010285 pw.print(mInputMethodAnimLayerAdjustment);
10286 pw.print(" mWallpaperAnimLayerAdjustment=");
10287 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010288 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10289 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010290 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10291 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10292 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10293 pw.print(" mRotation="); pw.print(mRotation);
10294 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10295 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10296 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10297 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10298 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10299 pw.print(" mNextAppTransition=0x");
10300 pw.print(Integer.toHexString(mNextAppTransition));
10301 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010302 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010303 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10304 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10305 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10306 if (mOpeningApps.size() > 0) {
10307 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10308 }
10309 if (mClosingApps.size() > 0) {
10310 pw.print(" mClosingApps="); pw.println(mClosingApps);
10311 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010312 if (mToTopApps.size() > 0) {
10313 pw.print(" mToTopApps="); pw.println(mToTopApps);
10314 }
10315 if (mToBottomApps.size() > 0) {
10316 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10317 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010318 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10319 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010321 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10322 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10323 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10324 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10325 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10326 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 }
10328 }
10329
10330 public void monitor() {
10331 synchronized (mWindowMap) { }
10332 synchronized (mKeyguardDisabled) { }
10333 synchronized (mKeyWaiter) { }
10334 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010335
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010336 public void virtualKeyFeedback(KeyEvent event) {
10337 mPolicy.keyFeedbackFromInput(event);
10338 }
10339
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010340 /**
10341 * DimAnimator class that controls the dim animation. This holds the surface and
10342 * all state used for dim animation.
10343 */
10344 private static class DimAnimator {
10345 Surface mDimSurface;
10346 boolean mDimShown = false;
10347 float mDimCurrentAlpha;
10348 float mDimTargetAlpha;
10349 float mDimDeltaPerMs;
10350 long mLastDimAnimTime;
10351
10352 DimAnimator (SurfaceSession session) {
10353 if (mDimSurface == null) {
10354 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10355 + mDimSurface + ": CREATE");
10356 try {
10357 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10358 Surface.FX_SURFACE_DIM);
10359 } catch (Exception e) {
10360 Log.e(TAG, "Exception creating Dim surface", e);
10361 }
10362 }
10363 }
10364
10365 /**
10366 * Show the dim surface.
10367 */
10368 void show(int dw, int dh) {
10369 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10370 dw + "x" + dh + ")");
10371 mDimShown = true;
10372 try {
10373 mDimSurface.setPosition(0, 0);
10374 mDimSurface.setSize(dw, dh);
10375 mDimSurface.show();
10376 } catch (RuntimeException e) {
10377 Log.w(TAG, "Failure showing dim surface", e);
10378 }
10379 }
10380
10381 /**
10382 * Set's the dim surface's layer and update dim parameters that will be used in
10383 * {@link updateSurface} after all windows are examined.
10384 */
10385 void updateParameters(WindowState w, long currentTime) {
10386 mDimSurface.setLayer(w.mAnimLayer-1);
10387
10388 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010389 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10390 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010391 if (mDimTargetAlpha != target) {
10392 // If the desired dim level has changed, then
10393 // start an animation to it.
10394 mLastDimAnimTime = currentTime;
10395 long duration = (w.mAnimating && w.mAnimation != null)
10396 ? w.mAnimation.computeDurationHint()
10397 : DEFAULT_DIM_DURATION;
10398 if (target > mDimTargetAlpha) {
10399 // This is happening behind the activity UI,
10400 // so we can make it run a little longer to
10401 // give a stronger impression without disrupting
10402 // the user.
10403 duration *= DIM_DURATION_MULTIPLIER;
10404 }
10405 if (duration < 1) {
10406 // Don't divide by zero
10407 duration = 1;
10408 }
10409 mDimTargetAlpha = target;
10410 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10411 }
10412 }
10413
10414 /**
10415 * Updating the surface's alpha. Returns true if the animation continues, or returns
10416 * false when the animation is finished and the dim surface is hidden.
10417 */
10418 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10419 if (!dimming) {
10420 if (mDimTargetAlpha != 0) {
10421 mLastDimAnimTime = currentTime;
10422 mDimTargetAlpha = 0;
10423 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10424 }
10425 }
10426
10427 boolean animating = false;
10428 if (mLastDimAnimTime != 0) {
10429 mDimCurrentAlpha += mDimDeltaPerMs
10430 * (currentTime-mLastDimAnimTime);
10431 boolean more = true;
10432 if (displayFrozen) {
10433 // If the display is frozen, there is no reason to animate.
10434 more = false;
10435 } else if (mDimDeltaPerMs > 0) {
10436 if (mDimCurrentAlpha > mDimTargetAlpha) {
10437 more = false;
10438 }
10439 } else if (mDimDeltaPerMs < 0) {
10440 if (mDimCurrentAlpha < mDimTargetAlpha) {
10441 more = false;
10442 }
10443 } else {
10444 more = false;
10445 }
10446
10447 // Do we need to continue animating?
10448 if (more) {
10449 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10450 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10451 mLastDimAnimTime = currentTime;
10452 mDimSurface.setAlpha(mDimCurrentAlpha);
10453 animating = true;
10454 } else {
10455 mDimCurrentAlpha = mDimTargetAlpha;
10456 mLastDimAnimTime = 0;
10457 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10458 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10459 mDimSurface.setAlpha(mDimCurrentAlpha);
10460 if (!dimming) {
10461 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10462 + ": HIDE");
10463 try {
10464 mDimSurface.hide();
10465 } catch (RuntimeException e) {
10466 Log.w(TAG, "Illegal argument exception hiding dim surface");
10467 }
10468 mDimShown = false;
10469 }
10470 }
10471 }
10472 return animating;
10473 }
10474
10475 public void printTo(PrintWriter pw) {
10476 pw.print(" mDimShown="); pw.print(mDimShown);
10477 pw.print(" current="); pw.print(mDimCurrentAlpha);
10478 pw.print(" target="); pw.print(mDimTargetAlpha);
10479 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10480 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10481 }
10482 }
10483
10484 /**
10485 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10486 * This is used for opening/closing transition for apps in compatible mode.
10487 */
10488 private static class FadeInOutAnimation extends Animation {
10489 int mWidth;
10490 boolean mFadeIn;
10491
10492 public FadeInOutAnimation(boolean fadeIn) {
10493 setInterpolator(new AccelerateInterpolator());
10494 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10495 mFadeIn = fadeIn;
10496 }
10497
10498 @Override
10499 protected void applyTransformation(float interpolatedTime, Transformation t) {
10500 float x = interpolatedTime;
10501 if (!mFadeIn) {
10502 x = 1.0f - x; // reverse the interpolation for fade out
10503 }
10504 if (x < 0.5) {
10505 // move the window out of the screen.
10506 t.getMatrix().setTranslate(mWidth, 0);
10507 } else {
10508 t.getMatrix().setTranslate(0, 0);// show
10509 t.setAlpha((x - 0.5f) * 2);
10510 }
10511 }
10512
10513 @Override
10514 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10515 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10516 mWidth = width;
10517 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010518
10519 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010520 public int getZAdjustment() {
10521 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010522 }
10523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524}