blob: 60496d67fe5418789dfbd5b228bcd3452f43e36d [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)
7059 + " tok.idden="
7060 + (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;
7088 if (mAnimation == null && mAppToken.startingData != null) {
7089 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7090 + mToken
7091 + ": first real window is shown, no animation");
7092 mFinishedStarting.add(mAppToken);
7093 mH.sendEmptyMessage(H.FINISHED_STARTING);
7094 }
7095 mAppToken.updateReportedVisibilityLocked();
7096 }
7097 }
7098 return true;
7099 }
Romain Guy06882f82009-06-10 13:36:04 -07007100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 // This must be called while inside a transaction. Returns true if
7102 // there is more animation to run.
7103 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7104 if (!mDisplayFrozen) {
7105 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7108 mHasTransformation = true;
7109 mHasLocalTransformation = true;
7110 if (!mLocalAnimating) {
7111 if (DEBUG_ANIM) Log.v(
7112 TAG, "Starting animation in " + this +
7113 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7114 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7115 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7116 mAnimation.setStartTime(currentTime);
7117 mLocalAnimating = true;
7118 mAnimating = true;
7119 }
7120 mTransformation.clear();
7121 final boolean more = mAnimation.getTransformation(
7122 currentTime, mTransformation);
7123 if (DEBUG_ANIM) Log.v(
7124 TAG, "Stepped animation in " + this +
7125 ": more=" + more + ", xform=" + mTransformation);
7126 if (more) {
7127 // we're not done!
7128 return true;
7129 }
7130 if (DEBUG_ANIM) Log.v(
7131 TAG, "Finished animation in " + this +
7132 " @ " + currentTime);
7133 mAnimation = null;
7134 //WindowManagerService.this.dump();
7135 }
7136 mHasLocalTransformation = false;
7137 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007138 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 // When our app token is animating, we kind-of pretend like
7140 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7141 // part of this check means that we will only do this if
7142 // our window is not currently exiting, or it is not
7143 // locally animating itself. The idea being that one that
7144 // is exiting and doing a local animation should be removed
7145 // once that animation is done.
7146 mAnimating = true;
7147 mHasTransformation = true;
7148 mTransformation.clear();
7149 return false;
7150 } else if (mHasTransformation) {
7151 // Little trick to get through the path below to act like
7152 // we have finished an animation.
7153 mAnimating = true;
7154 } else if (isAnimating()) {
7155 mAnimating = true;
7156 }
7157 } else if (mAnimation != null) {
7158 // If the display is frozen, and there is a pending animation,
7159 // clear it and make sure we run the cleanup code.
7160 mAnimating = true;
7161 mLocalAnimating = true;
7162 mAnimation = null;
7163 }
Romain Guy06882f82009-06-10 13:36:04 -07007164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 if (!mAnimating && !mLocalAnimating) {
7166 return false;
7167 }
7168
7169 if (DEBUG_ANIM) Log.v(
7170 TAG, "Animation done in " + this + ": exiting=" + mExiting
7171 + ", reportedVisible="
7172 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 mAnimating = false;
7175 mLocalAnimating = false;
7176 mAnimation = null;
7177 mAnimLayer = mLayer;
7178 if (mIsImWindow) {
7179 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007180 } else if (mIsWallpaper) {
7181 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 }
7183 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7184 + " anim layer: " + mAnimLayer);
7185 mHasTransformation = false;
7186 mHasLocalTransformation = false;
7187 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7188 mTransformation.clear();
7189 if (mHasDrawn
7190 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7191 && mAppToken != null
7192 && mAppToken.firstWindowDrawn
7193 && mAppToken.startingData != null) {
7194 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7195 + mToken + ": first real window done animating");
7196 mFinishedStarting.add(mAppToken);
7197 mH.sendEmptyMessage(H.FINISHED_STARTING);
7198 }
Romain Guy06882f82009-06-10 13:36:04 -07007199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 finishExit();
7201
7202 if (mAppToken != null) {
7203 mAppToken.updateReportedVisibilityLocked();
7204 }
7205
7206 return false;
7207 }
7208
7209 void finishExit() {
7210 if (DEBUG_ANIM) Log.v(
7211 TAG, "finishExit in " + this
7212 + ": exiting=" + mExiting
7213 + " remove=" + mRemoveOnExit
7214 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 final int N = mChildWindows.size();
7217 for (int i=0; i<N; i++) {
7218 ((WindowState)mChildWindows.get(i)).finishExit();
7219 }
Romain Guy06882f82009-06-10 13:36:04 -07007220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007221 if (!mExiting) {
7222 return;
7223 }
Romain Guy06882f82009-06-10 13:36:04 -07007224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 if (isWindowAnimating()) {
7226 return;
7227 }
7228
7229 if (localLOGV) Log.v(
7230 TAG, "Exit animation finished in " + this
7231 + ": remove=" + mRemoveOnExit);
7232 if (mSurface != null) {
7233 mDestroySurface.add(this);
7234 mDestroying = true;
7235 if (SHOW_TRANSACTIONS) Log.i(
7236 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7237 try {
7238 mSurface.hide();
7239 } catch (RuntimeException e) {
7240 Log.w(TAG, "Error hiding surface in " + this, e);
7241 }
7242 mLastHidden = true;
7243 mKeyWaiter.releasePendingPointerLocked(mSession);
7244 }
7245 mExiting = false;
7246 if (mRemoveOnExit) {
7247 mPendingRemove.add(this);
7248 mRemoveOnExit = false;
7249 }
7250 }
Romain Guy06882f82009-06-10 13:36:04 -07007251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7253 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7254 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7255 if (dtdx < -.000001f || dtdx > .000001f) return false;
7256 if (dsdy < -.000001f || dsdy > .000001f) return false;
7257 return true;
7258 }
Romain Guy06882f82009-06-10 13:36:04 -07007259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 void computeShownFrameLocked() {
7261 final boolean selfTransformation = mHasLocalTransformation;
7262 Transformation attachedTransformation =
7263 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7264 ? mAttachedWindow.mTransformation : null;
7265 Transformation appTransformation =
7266 (mAppToken != null && mAppToken.hasTransformation)
7267 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007268
7269 // Wallpapers are animated based on the "real" window they
7270 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007271 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007272 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007273 if (mWallpaperTarget.mHasLocalTransformation) {
7274 attachedTransformation = mWallpaperTarget.mTransformation;
7275 }
7276 if (mWallpaperTarget.mAppToken != null &&
7277 mWallpaperTarget.mAppToken.hasTransformation) {
7278 appTransformation = mWallpaperTarget.mAppToken.transformation;
7279 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007280 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7281 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7282 }
7283 if (DEBUG_WALLPAPER && appTransformation != null) {
7284 Log.v(TAG, "WP target app xform: " + appTransformation);
7285 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007286 }
7287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 if (selfTransformation || attachedTransformation != null
7289 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007290 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007291 final Rect frame = mFrame;
7292 final float tmpFloats[] = mTmpFloats;
7293 final Matrix tmpMatrix = mTmpMatrix;
7294
7295 // Compute the desired transformation.
7296 tmpMatrix.setTranslate(frame.left, frame.top);
7297 if (selfTransformation) {
7298 tmpMatrix.preConcat(mTransformation.getMatrix());
7299 }
7300 if (attachedTransformation != null) {
7301 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7302 }
7303 if (appTransformation != null) {
7304 tmpMatrix.preConcat(appTransformation.getMatrix());
7305 }
7306
7307 // "convert" it into SurfaceFlinger's format
7308 // (a 2x2 matrix + an offset)
7309 // Here we must not transform the position of the surface
7310 // since it is already included in the transformation.
7311 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 tmpMatrix.getValues(tmpFloats);
7314 mDsDx = tmpFloats[Matrix.MSCALE_X];
7315 mDtDx = tmpFloats[Matrix.MSKEW_X];
7316 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7317 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007318 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7319 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 int w = frame.width();
7321 int h = frame.height();
7322 mShownFrame.set(x, y, x+w, y+h);
7323
7324 // Now set the alpha... but because our current hardware
7325 // can't do alpha transformation on a non-opaque surface,
7326 // turn it off if we are running an animation that is also
7327 // transforming since it is more important to have that
7328 // animation be smooth.
7329 mShownAlpha = mAlpha;
7330 if (!mLimitedAlphaCompositing
7331 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7332 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7333 && x == frame.left && y == frame.top))) {
7334 //Log.i(TAG, "Applying alpha transform");
7335 if (selfTransformation) {
7336 mShownAlpha *= mTransformation.getAlpha();
7337 }
7338 if (attachedTransformation != null) {
7339 mShownAlpha *= attachedTransformation.getAlpha();
7340 }
7341 if (appTransformation != null) {
7342 mShownAlpha *= appTransformation.getAlpha();
7343 }
7344 } else {
7345 //Log.i(TAG, "Not applying alpha transform");
7346 }
Romain Guy06882f82009-06-10 13:36:04 -07007347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007348 if (localLOGV) Log.v(
7349 TAG, "Continuing animation in " + this +
7350 ": " + mShownFrame +
7351 ", alpha=" + mTransformation.getAlpha());
7352 return;
7353 }
Romain Guy06882f82009-06-10 13:36:04 -07007354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007355 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007356 if (mXOffset != 0 || mYOffset != 0) {
7357 mShownFrame.offset(mXOffset, mYOffset);
7358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 mShownAlpha = mAlpha;
7360 mDsDx = 1;
7361 mDtDx = 0;
7362 mDsDy = 0;
7363 mDtDy = 1;
7364 }
Romain Guy06882f82009-06-10 13:36:04 -07007365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 /**
7367 * Is this window visible? It is not visible if there is no
7368 * surface, or we are in the process of running an exit animation
7369 * that will remove the surface, or its app token has been hidden.
7370 */
7371 public boolean isVisibleLw() {
7372 final AppWindowToken atoken = mAppToken;
7373 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7374 && (atoken == null || !atoken.hiddenRequested)
7375 && !mExiting && !mDestroying;
7376 }
7377
7378 /**
7379 * Is this window visible, ignoring its app token? It is not visible
7380 * if there is no surface, or we are in the process of running an exit animation
7381 * that will remove the surface.
7382 */
7383 public boolean isWinVisibleLw() {
7384 final AppWindowToken atoken = mAppToken;
7385 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7386 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7387 && !mExiting && !mDestroying;
7388 }
7389
7390 /**
7391 * The same as isVisible(), but follows the current hidden state of
7392 * the associated app token, not the pending requested hidden state.
7393 */
7394 boolean isVisibleNow() {
7395 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007396 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 }
7398
7399 /**
7400 * Same as isVisible(), but we also count it as visible between the
7401 * call to IWindowSession.add() and the first relayout().
7402 */
7403 boolean isVisibleOrAdding() {
7404 final AppWindowToken atoken = mAppToken;
7405 return (mSurface != null
7406 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7407 && mPolicyVisibility && !mAttachedHidden
7408 && (atoken == null || !atoken.hiddenRequested)
7409 && !mExiting && !mDestroying;
7410 }
7411
7412 /**
7413 * Is this window currently on-screen? It is on-screen either if it
7414 * is visible or it is currently running an animation before no longer
7415 * being visible.
7416 */
7417 boolean isOnScreen() {
7418 final AppWindowToken atoken = mAppToken;
7419 if (atoken != null) {
7420 return mSurface != null && mPolicyVisibility && !mDestroying
7421 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007422 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 } else {
7424 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007425 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 }
7427 }
Romain Guy06882f82009-06-10 13:36:04 -07007428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 /**
7430 * Like isOnScreen(), but we don't return true if the window is part
7431 * of a transition that has not yet been started.
7432 */
7433 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007434 if (mRootToken.waitingToShow &&
7435 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7436 return false;
7437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007439 final boolean animating = atoken != null
7440 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007442 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7443 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007444 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 }
7446
7447 /** Is the window or its container currently animating? */
7448 boolean isAnimating() {
7449 final WindowState attached = mAttachedWindow;
7450 final AppWindowToken atoken = mAppToken;
7451 return mAnimation != null
7452 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007453 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 (atoken.animation != null
7455 || atoken.inPendingTransaction));
7456 }
7457
7458 /** Is this window currently animating? */
7459 boolean isWindowAnimating() {
7460 return mAnimation != null;
7461 }
7462
7463 /**
7464 * Like isOnScreen, but returns false if the surface hasn't yet
7465 * been drawn.
7466 */
7467 public boolean isDisplayedLw() {
7468 final AppWindowToken atoken = mAppToken;
7469 return mSurface != null && mPolicyVisibility && !mDestroying
7470 && !mDrawPending && !mCommitDrawPending
7471 && ((!mAttachedHidden &&
7472 (atoken == null || !atoken.hiddenRequested))
7473 || mAnimating);
7474 }
7475
7476 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7477 boolean shownFrame, boolean onlyOpaque) {
7478 if (mSurface == null) {
7479 return false;
7480 }
7481 if (mAppToken != null && !mAppToken.appFullscreen) {
7482 return false;
7483 }
7484 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7485 return false;
7486 }
7487 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007488
7489 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7490 return frame.left <= mCompatibleScreenFrame.left &&
7491 frame.top <= mCompatibleScreenFrame.top &&
7492 frame.right >= mCompatibleScreenFrame.right &&
7493 frame.bottom >= mCompatibleScreenFrame.bottom;
7494 } else {
7495 return frame.left <= 0 && frame.top <= 0
7496 && frame.right >= screenWidth
7497 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 }
Romain Guy06882f82009-06-10 13:36:04 -07007500
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007501 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007502 * Return true if the window is opaque and fully drawn. This indicates
7503 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007504 */
7505 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007506 return (mAttrs.format == PixelFormat.OPAQUE
7507 || mAttrs.type == TYPE_WALLPAPER)
7508 && mSurface != null && mAnimation == null
7509 && (mAppToken == null || mAppToken.animation == null)
7510 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007511 }
7512
7513 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7514 return
7515 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007516 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7517 // only if it's visible
7518 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007519 // and only if the application fills the compatible screen
7520 mFrame.left <= mCompatibleScreenFrame.left &&
7521 mFrame.top <= mCompatibleScreenFrame.top &&
7522 mFrame.right >= mCompatibleScreenFrame.right &&
7523 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007524 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007525 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007526 }
7527
7528 boolean isFullscreen(int screenWidth, int screenHeight) {
7529 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007530 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531 }
7532
7533 void removeLocked() {
7534 if (mAttachedWindow != null) {
7535 mAttachedWindow.mChildWindows.remove(this);
7536 }
7537 destroySurfaceLocked();
7538 mSession.windowRemovedLocked();
7539 try {
7540 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7541 } catch (RuntimeException e) {
7542 // Ignore if it has already been removed (usually because
7543 // we are doing this as part of processing a death note.)
7544 }
7545 }
7546
7547 private class DeathRecipient implements IBinder.DeathRecipient {
7548 public void binderDied() {
7549 try {
7550 synchronized(mWindowMap) {
7551 WindowState win = windowForClientLocked(mSession, mClient);
7552 Log.i(TAG, "WIN DEATH: " + win);
7553 if (win != null) {
7554 removeWindowLocked(mSession, win);
7555 }
7556 }
7557 } catch (IllegalArgumentException ex) {
7558 // This will happen if the window has already been
7559 // removed.
7560 }
7561 }
7562 }
7563
7564 /** Returns true if this window desires key events. */
7565 public final boolean canReceiveKeys() {
7566 return isVisibleOrAdding()
7567 && (mViewVisibility == View.VISIBLE)
7568 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7569 }
7570
7571 public boolean hasDrawnLw() {
7572 return mHasDrawn;
7573 }
7574
7575 public boolean showLw(boolean doAnimation) {
7576 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7577 mPolicyVisibility = true;
7578 mPolicyVisibilityAfterAnim = true;
7579 if (doAnimation) {
7580 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7581 }
7582 requestAnimationLocked(0);
7583 return true;
7584 }
7585 return false;
7586 }
7587
7588 public boolean hideLw(boolean doAnimation) {
7589 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7590 : mPolicyVisibility;
7591 if (current) {
7592 if (doAnimation) {
7593 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7594 if (mAnimation == null) {
7595 doAnimation = false;
7596 }
7597 }
7598 if (doAnimation) {
7599 mPolicyVisibilityAfterAnim = false;
7600 } else {
7601 mPolicyVisibilityAfterAnim = false;
7602 mPolicyVisibility = false;
7603 }
7604 requestAnimationLocked(0);
7605 return true;
7606 }
7607 return false;
7608 }
7609
7610 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007611 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007612
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007613 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7614 pw.print(" mClient="); pw.println(mClient.asBinder());
7615 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7616 if (mAttachedWindow != null || mLayoutAttached) {
7617 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7618 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7619 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007620 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7621 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7622 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007623 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7624 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007625 }
7626 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7627 pw.print(" mSubLayer="); pw.print(mSubLayer);
7628 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7629 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7630 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7631 pw.print("="); pw.print(mAnimLayer);
7632 pw.print(" mLastLayer="); pw.println(mLastLayer);
7633 if (mSurface != null) {
7634 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7635 }
7636 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7637 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7638 if (mAppToken != null) {
7639 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7640 }
7641 if (mTargetAppToken != null) {
7642 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7643 }
7644 pw.print(prefix); pw.print("mViewVisibility=0x");
7645 pw.print(Integer.toHexString(mViewVisibility));
7646 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007647 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7648 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007649 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7650 pw.print(prefix); pw.print("mPolicyVisibility=");
7651 pw.print(mPolicyVisibility);
7652 pw.print(" mPolicyVisibilityAfterAnim=");
7653 pw.print(mPolicyVisibilityAfterAnim);
7654 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7655 }
7656 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007657 pw.print(" h="); pw.println(mRequestedHeight);
7658 if (mXOffset != 0 || mYOffset != 0) {
7659 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7660 pw.print(" y="); pw.println(mYOffset);
7661 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007662 pw.print(prefix); pw.print("mGivenContentInsets=");
7663 mGivenContentInsets.printShortString(pw);
7664 pw.print(" mGivenVisibleInsets=");
7665 mGivenVisibleInsets.printShortString(pw);
7666 pw.println();
7667 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7668 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7669 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7670 }
7671 pw.print(prefix); pw.print("mShownFrame=");
7672 mShownFrame.printShortString(pw);
7673 pw.print(" last="); mLastShownFrame.printShortString(pw);
7674 pw.println();
7675 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7676 pw.print(" last="); mLastFrame.printShortString(pw);
7677 pw.println();
7678 pw.print(prefix); pw.print("mContainingFrame=");
7679 mContainingFrame.printShortString(pw);
7680 pw.print(" mDisplayFrame=");
7681 mDisplayFrame.printShortString(pw);
7682 pw.println();
7683 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7684 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7685 pw.println();
7686 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7687 pw.print(" last="); mLastContentInsets.printShortString(pw);
7688 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7689 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7690 pw.println();
7691 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7692 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7693 pw.print(" mAlpha="); pw.print(mAlpha);
7694 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7695 }
7696 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7697 || mAnimation != null) {
7698 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7699 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7700 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7701 pw.print(" mAnimation="); pw.println(mAnimation);
7702 }
7703 if (mHasTransformation || mHasLocalTransformation) {
7704 pw.print(prefix); pw.print("XForm: has=");
7705 pw.print(mHasTransformation);
7706 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7707 pw.print(" "); mTransformation.printShortString(pw);
7708 pw.println();
7709 }
7710 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7711 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7712 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7713 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7714 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7715 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7716 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7717 pw.print(" mDestroying="); pw.print(mDestroying);
7718 pw.print(" mRemoved="); pw.println(mRemoved);
7719 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007720 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007721 pw.print(prefix); pw.print("mOrientationChanging=");
7722 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007723 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
7724 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007725 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007726 if (mHScale != 1 || mVScale != 1) {
7727 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7728 pw.print(" mVScale="); pw.println(mVScale);
7729 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007730 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007731 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7732 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 }
7735
7736 @Override
7737 public String toString() {
7738 return "Window{"
7739 + Integer.toHexString(System.identityHashCode(this))
7740 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7741 }
7742 }
Romain Guy06882f82009-06-10 13:36:04 -07007743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 // -------------------------------------------------------------
7745 // Window Token State
7746 // -------------------------------------------------------------
7747
7748 class WindowToken {
7749 // The actual token.
7750 final IBinder token;
7751
7752 // The type of window this token is for, as per WindowManager.LayoutParams.
7753 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 // Set if this token was explicitly added by a client, so should
7756 // not be removed when all windows are removed.
7757 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007758
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007759 // For printing.
7760 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 // If this is an AppWindowToken, this is non-null.
7763 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 // All of the windows associated with this token.
7766 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7767
7768 // Is key dispatching paused for this token?
7769 boolean paused = false;
7770
7771 // Should this token's windows be hidden?
7772 boolean hidden;
7773
7774 // Temporary for finding which tokens no longer have visible windows.
7775 boolean hasVisible;
7776
Dianne Hackborna8f60182009-09-01 19:01:50 -07007777 // Set to true when this token is in a pending transaction where it
7778 // will be shown.
7779 boolean waitingToShow;
7780
7781 // Set to true when this token is in a pending transaction where it
7782 // will be hidden.
7783 boolean waitingToHide;
7784
7785 // Set to true when this token is in a pending transaction where its
7786 // windows will be put to the bottom of the list.
7787 boolean sendingToBottom;
7788
7789 // Set to true when this token is in a pending transaction where its
7790 // windows will be put to the top of the list.
7791 boolean sendingToTop;
7792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 WindowToken(IBinder _token, int type, boolean _explicit) {
7794 token = _token;
7795 windowType = type;
7796 explicit = _explicit;
7797 }
7798
7799 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007800 pw.print(prefix); pw.print("token="); pw.println(token);
7801 pw.print(prefix); pw.print("windows="); pw.println(windows);
7802 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7803 pw.print(" hidden="); pw.print(hidden);
7804 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007805 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7806 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7807 pw.print(" waitingToHide="); pw.print(waitingToHide);
7808 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7809 pw.print(" sendingToTop="); pw.println(sendingToTop);
7810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 }
7812
7813 @Override
7814 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007815 if (stringName == null) {
7816 StringBuilder sb = new StringBuilder();
7817 sb.append("WindowToken{");
7818 sb.append(Integer.toHexString(System.identityHashCode(this)));
7819 sb.append(" token="); sb.append(token); sb.append('}');
7820 stringName = sb.toString();
7821 }
7822 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 }
7824 };
7825
7826 class AppWindowToken extends WindowToken {
7827 // Non-null only for application tokens.
7828 final IApplicationToken appToken;
7829
7830 // All of the windows and child windows that are included in this
7831 // application token. Note this list is NOT sorted!
7832 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7833
7834 int groupId = -1;
7835 boolean appFullscreen;
7836 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 // These are used for determining when all windows associated with
7839 // an activity have been drawn, so they can be made visible together
7840 // at the same time.
7841 int lastTransactionSequence = mTransactionSequence-1;
7842 int numInterestingWindows;
7843 int numDrawnWindows;
7844 boolean inPendingTransaction;
7845 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 // Is this token going to be hidden in a little while? If so, it
7848 // won't be taken into account for setting the screen orientation.
7849 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 // Is this window's surface needed? This is almost like hidden, except
7852 // it will sometimes be true a little earlier: when the token has
7853 // been shown, but is still waiting for its app transition to execute
7854 // before making its windows shown.
7855 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 // Have we told the window clients to hide themselves?
7858 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 // Last visibility state we reported to the app token.
7861 boolean reportedVisible;
7862
7863 // Set to true when the token has been removed from the window mgr.
7864 boolean removed;
7865
7866 // Have we been asked to have this token keep the screen frozen?
7867 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 boolean animating;
7870 Animation animation;
7871 boolean hasTransformation;
7872 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 // Offset to the window of all layers in the token, for use by
7875 // AppWindowToken animations.
7876 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 // Information about an application starting window if displayed.
7879 StartingData startingData;
7880 WindowState startingWindow;
7881 View startingView;
7882 boolean startingDisplayed;
7883 boolean startingMoved;
7884 boolean firstWindowDrawn;
7885
7886 AppWindowToken(IApplicationToken _token) {
7887 super(_token.asBinder(),
7888 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7889 appWindowToken = this;
7890 appToken = _token;
7891 }
Romain Guy06882f82009-06-10 13:36:04 -07007892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893 public void setAnimation(Animation anim) {
7894 if (localLOGV) Log.v(
7895 TAG, "Setting animation in " + this + ": " + anim);
7896 animation = anim;
7897 animating = false;
7898 anim.restrictDuration(MAX_ANIMATION_DURATION);
7899 anim.scaleCurrentDuration(mTransitionAnimationScale);
7900 int zorder = anim.getZAdjustment();
7901 int adj = 0;
7902 if (zorder == Animation.ZORDER_TOP) {
7903 adj = TYPE_LAYER_OFFSET;
7904 } else if (zorder == Animation.ZORDER_BOTTOM) {
7905 adj = -TYPE_LAYER_OFFSET;
7906 }
Romain Guy06882f82009-06-10 13:36:04 -07007907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 if (animLayerAdjustment != adj) {
7909 animLayerAdjustment = adj;
7910 updateLayers();
7911 }
7912 }
Romain Guy06882f82009-06-10 13:36:04 -07007913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 public void setDummyAnimation() {
7915 if (animation == null) {
7916 if (localLOGV) Log.v(
7917 TAG, "Setting dummy animation in " + this);
7918 animation = sDummyAnimation;
7919 }
7920 }
7921
7922 public void clearAnimation() {
7923 if (animation != null) {
7924 animation = null;
7925 animating = true;
7926 }
7927 }
Romain Guy06882f82009-06-10 13:36:04 -07007928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 void updateLayers() {
7930 final int N = allAppWindows.size();
7931 final int adj = animLayerAdjustment;
7932 for (int i=0; i<N; i++) {
7933 WindowState w = allAppWindows.get(i);
7934 w.mAnimLayer = w.mLayer + adj;
7935 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7936 + w.mAnimLayer);
7937 if (w == mInputMethodTarget) {
7938 setInputMethodAnimLayerAdjustment(adj);
7939 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007940 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007941 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 }
7944 }
Romain Guy06882f82009-06-10 13:36:04 -07007945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 void sendAppVisibilityToClients() {
7947 final int N = allAppWindows.size();
7948 for (int i=0; i<N; i++) {
7949 WindowState win = allAppWindows.get(i);
7950 if (win == startingWindow && clientHidden) {
7951 // Don't hide the starting window.
7952 continue;
7953 }
7954 try {
7955 if (DEBUG_VISIBILITY) Log.v(TAG,
7956 "Setting visibility of " + win + ": " + (!clientHidden));
7957 win.mClient.dispatchAppVisibility(!clientHidden);
7958 } catch (RemoteException e) {
7959 }
7960 }
7961 }
Romain Guy06882f82009-06-10 13:36:04 -07007962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 void showAllWindowsLocked() {
7964 final int NW = allAppWindows.size();
7965 for (int i=0; i<NW; i++) {
7966 WindowState w = allAppWindows.get(i);
7967 if (DEBUG_VISIBILITY) Log.v(TAG,
7968 "performing show on: " + w);
7969 w.performShowLocked();
7970 }
7971 }
Romain Guy06882f82009-06-10 13:36:04 -07007972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 // This must be called while inside a transaction.
7974 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7975 if (!mDisplayFrozen) {
7976 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 if (animation == sDummyAnimation) {
7979 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007980 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 // when it is really time to animate, this will be set to
7982 // a real animation and the next call will execute normally.
7983 return false;
7984 }
Romain Guy06882f82009-06-10 13:36:04 -07007985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7987 if (!animating) {
7988 if (DEBUG_ANIM) Log.v(
7989 TAG, "Starting animation in " + this +
7990 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7991 + " scale=" + mTransitionAnimationScale
7992 + " allDrawn=" + allDrawn + " animating=" + animating);
7993 animation.initialize(dw, dh, dw, dh);
7994 animation.setStartTime(currentTime);
7995 animating = true;
7996 }
7997 transformation.clear();
7998 final boolean more = animation.getTransformation(
7999 currentTime, transformation);
8000 if (DEBUG_ANIM) Log.v(
8001 TAG, "Stepped animation in " + this +
8002 ": more=" + more + ", xform=" + transformation);
8003 if (more) {
8004 // we're done!
8005 hasTransformation = true;
8006 return true;
8007 }
8008 if (DEBUG_ANIM) Log.v(
8009 TAG, "Finished animation in " + this +
8010 " @ " + currentTime);
8011 animation = null;
8012 }
8013 } else if (animation != null) {
8014 // If the display is frozen, and there is a pending animation,
8015 // clear it and make sure we run the cleanup code.
8016 animating = true;
8017 animation = null;
8018 }
8019
8020 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 if (!animating) {
8023 return false;
8024 }
8025
8026 clearAnimation();
8027 animating = false;
8028 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8029 moveInputMethodWindowsIfNeededLocked(true);
8030 }
Romain Guy06882f82009-06-10 13:36:04 -07008031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 if (DEBUG_ANIM) Log.v(
8033 TAG, "Animation done in " + this
8034 + ": reportedVisible=" + reportedVisible);
8035
8036 transformation.clear();
8037 if (animLayerAdjustment != 0) {
8038 animLayerAdjustment = 0;
8039 updateLayers();
8040 }
Romain Guy06882f82009-06-10 13:36:04 -07008041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 final int N = windows.size();
8043 for (int i=0; i<N; i++) {
8044 ((WindowState)windows.get(i)).finishExit();
8045 }
8046 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 return false;
8049 }
8050
8051 void updateReportedVisibilityLocked() {
8052 if (appToken == null) {
8053 return;
8054 }
Romain Guy06882f82009-06-10 13:36:04 -07008055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 int numInteresting = 0;
8057 int numVisible = 0;
8058 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8061 final int N = allAppWindows.size();
8062 for (int i=0; i<N; i++) {
8063 WindowState win = allAppWindows.get(i);
8064 if (win == startingWindow || win.mAppFreezing) {
8065 continue;
8066 }
8067 if (DEBUG_VISIBILITY) {
8068 Log.v(TAG, "Win " + win + ": isDisplayed="
8069 + win.isDisplayedLw()
8070 + ", isAnimating=" + win.isAnimating());
8071 if (!win.isDisplayedLw()) {
8072 Log.v(TAG, "Not displayed: s=" + win.mSurface
8073 + " pv=" + win.mPolicyVisibility
8074 + " dp=" + win.mDrawPending
8075 + " cdp=" + win.mCommitDrawPending
8076 + " ah=" + win.mAttachedHidden
8077 + " th="
8078 + (win.mAppToken != null
8079 ? win.mAppToken.hiddenRequested : false)
8080 + " a=" + win.mAnimating);
8081 }
8082 }
8083 numInteresting++;
8084 if (win.isDisplayedLw()) {
8085 if (!win.isAnimating()) {
8086 numVisible++;
8087 }
8088 nowGone = false;
8089 } else if (win.isAnimating()) {
8090 nowGone = false;
8091 }
8092 }
Romain Guy06882f82009-06-10 13:36:04 -07008093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8095 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8096 + numInteresting + " visible=" + numVisible);
8097 if (nowVisible != reportedVisible) {
8098 if (DEBUG_VISIBILITY) Log.v(
8099 TAG, "Visibility changed in " + this
8100 + ": vis=" + nowVisible);
8101 reportedVisible = nowVisible;
8102 Message m = mH.obtainMessage(
8103 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8104 nowVisible ? 1 : 0,
8105 nowGone ? 1 : 0,
8106 this);
8107 mH.sendMessage(m);
8108 }
8109 }
Romain Guy06882f82009-06-10 13:36:04 -07008110
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008111 WindowState findMainWindow() {
8112 int j = windows.size();
8113 while (j > 0) {
8114 j--;
8115 WindowState win = windows.get(j);
8116 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8117 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8118 return win;
8119 }
8120 }
8121 return null;
8122 }
8123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008124 void dump(PrintWriter pw, String prefix) {
8125 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008126 if (appToken != null) {
8127 pw.print(prefix); pw.println("app=true");
8128 }
8129 if (allAppWindows.size() > 0) {
8130 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8131 }
8132 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008133 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008134 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8135 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8136 pw.print(" clientHidden="); pw.print(clientHidden);
8137 pw.print(" willBeHidden="); pw.print(willBeHidden);
8138 pw.print(" reportedVisible="); pw.println(reportedVisible);
8139 if (paused || freezingScreen) {
8140 pw.print(prefix); pw.print("paused="); pw.print(paused);
8141 pw.print(" freezingScreen="); pw.println(freezingScreen);
8142 }
8143 if (numInterestingWindows != 0 || numDrawnWindows != 0
8144 || inPendingTransaction || allDrawn) {
8145 pw.print(prefix); pw.print("numInterestingWindows=");
8146 pw.print(numInterestingWindows);
8147 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8148 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8149 pw.print(" allDrawn="); pw.println(allDrawn);
8150 }
8151 if (animating || animation != null) {
8152 pw.print(prefix); pw.print("animating="); pw.print(animating);
8153 pw.print(" animation="); pw.println(animation);
8154 }
8155 if (animLayerAdjustment != 0) {
8156 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8157 }
8158 if (hasTransformation) {
8159 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8160 pw.print(" transformation="); transformation.printShortString(pw);
8161 pw.println();
8162 }
8163 if (startingData != null || removed || firstWindowDrawn) {
8164 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8165 pw.print(" removed="); pw.print(removed);
8166 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8167 }
8168 if (startingWindow != null || startingView != null
8169 || startingDisplayed || startingMoved) {
8170 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8171 pw.print(" startingView="); pw.print(startingView);
8172 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8173 pw.print(" startingMoved"); pw.println(startingMoved);
8174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 }
8176
8177 @Override
8178 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008179 if (stringName == null) {
8180 StringBuilder sb = new StringBuilder();
8181 sb.append("AppWindowToken{");
8182 sb.append(Integer.toHexString(System.identityHashCode(this)));
8183 sb.append(" token="); sb.append(token); sb.append('}');
8184 stringName = sb.toString();
8185 }
8186 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008187 }
8188 }
Romain Guy06882f82009-06-10 13:36:04 -07008189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 // -------------------------------------------------------------
8191 // DummyAnimation
8192 // -------------------------------------------------------------
8193
8194 // This is an animation that does nothing: it just immediately finishes
8195 // itself every time it is called. It is used as a stub animation in cases
8196 // where we want to synchronize multiple things that may be animating.
8197 static final class DummyAnimation extends Animation {
8198 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8199 return false;
8200 }
8201 }
8202 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008204 // -------------------------------------------------------------
8205 // Async Handler
8206 // -------------------------------------------------------------
8207
8208 static final class StartingData {
8209 final String pkg;
8210 final int theme;
8211 final CharSequence nonLocalizedLabel;
8212 final int labelRes;
8213 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8216 int _labelRes, int _icon) {
8217 pkg = _pkg;
8218 theme = _theme;
8219 nonLocalizedLabel = _nonLocalizedLabel;
8220 labelRes = _labelRes;
8221 icon = _icon;
8222 }
8223 }
8224
8225 private final class H extends Handler {
8226 public static final int REPORT_FOCUS_CHANGE = 2;
8227 public static final int REPORT_LOSING_FOCUS = 3;
8228 public static final int ANIMATE = 4;
8229 public static final int ADD_STARTING = 5;
8230 public static final int REMOVE_STARTING = 6;
8231 public static final int FINISHED_STARTING = 7;
8232 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8234 public static final int HOLD_SCREEN_CHANGED = 12;
8235 public static final int APP_TRANSITION_TIMEOUT = 13;
8236 public static final int PERSIST_ANIMATION_SCALE = 14;
8237 public static final int FORCE_GC = 15;
8238 public static final int ENABLE_SCREEN = 16;
8239 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008240 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008244 public H() {
8245 }
Romain Guy06882f82009-06-10 13:36:04 -07008246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008247 @Override
8248 public void handleMessage(Message msg) {
8249 switch (msg.what) {
8250 case REPORT_FOCUS_CHANGE: {
8251 WindowState lastFocus;
8252 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 synchronized(mWindowMap) {
8255 lastFocus = mLastFocus;
8256 newFocus = mCurrentFocus;
8257 if (lastFocus == newFocus) {
8258 // Focus is not changing, so nothing to do.
8259 return;
8260 }
8261 mLastFocus = newFocus;
8262 //Log.i(TAG, "Focus moving from " + lastFocus
8263 // + " to " + newFocus);
8264 if (newFocus != null && lastFocus != null
8265 && !newFocus.isDisplayedLw()) {
8266 //Log.i(TAG, "Delaying loss of focus...");
8267 mLosingFocus.add(lastFocus);
8268 lastFocus = null;
8269 }
8270 }
8271
8272 if (lastFocus != newFocus) {
8273 //System.out.println("Changing focus from " + lastFocus
8274 // + " to " + newFocus);
8275 if (newFocus != null) {
8276 try {
8277 //Log.i(TAG, "Gaining focus: " + newFocus);
8278 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8279 } catch (RemoteException e) {
8280 // Ignore if process has died.
8281 }
8282 }
8283
8284 if (lastFocus != null) {
8285 try {
8286 //Log.i(TAG, "Losing focus: " + lastFocus);
8287 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8288 } catch (RemoteException e) {
8289 // Ignore if process has died.
8290 }
8291 }
8292 }
8293 } break;
8294
8295 case REPORT_LOSING_FOCUS: {
8296 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 synchronized(mWindowMap) {
8299 losers = mLosingFocus;
8300 mLosingFocus = new ArrayList<WindowState>();
8301 }
8302
8303 final int N = losers.size();
8304 for (int i=0; i<N; i++) {
8305 try {
8306 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8307 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8308 } catch (RemoteException e) {
8309 // Ignore if process has died.
8310 }
8311 }
8312 } break;
8313
8314 case ANIMATE: {
8315 synchronized(mWindowMap) {
8316 mAnimationPending = false;
8317 performLayoutAndPlaceSurfacesLocked();
8318 }
8319 } break;
8320
8321 case ADD_STARTING: {
8322 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8323 final StartingData sd = wtoken.startingData;
8324
8325 if (sd == null) {
8326 // Animation has been canceled... do nothing.
8327 return;
8328 }
Romain Guy06882f82009-06-10 13:36:04 -07008329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008330 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8331 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333 View view = null;
8334 try {
8335 view = mPolicy.addStartingWindow(
8336 wtoken.token, sd.pkg,
8337 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8338 sd.icon);
8339 } catch (Exception e) {
8340 Log.w(TAG, "Exception when adding starting window", e);
8341 }
8342
8343 if (view != null) {
8344 boolean abort = false;
8345
8346 synchronized(mWindowMap) {
8347 if (wtoken.removed || wtoken.startingData == null) {
8348 // If the window was successfully added, then
8349 // we need to remove it.
8350 if (wtoken.startingWindow != null) {
8351 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8352 "Aborted starting " + wtoken
8353 + ": removed=" + wtoken.removed
8354 + " startingData=" + wtoken.startingData);
8355 wtoken.startingWindow = null;
8356 wtoken.startingData = null;
8357 abort = true;
8358 }
8359 } else {
8360 wtoken.startingView = view;
8361 }
8362 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8363 "Added starting " + wtoken
8364 + ": startingWindow="
8365 + wtoken.startingWindow + " startingView="
8366 + wtoken.startingView);
8367 }
8368
8369 if (abort) {
8370 try {
8371 mPolicy.removeStartingWindow(wtoken.token, view);
8372 } catch (Exception e) {
8373 Log.w(TAG, "Exception when removing starting window", e);
8374 }
8375 }
8376 }
8377 } break;
8378
8379 case REMOVE_STARTING: {
8380 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8381 IBinder token = null;
8382 View view = null;
8383 synchronized (mWindowMap) {
8384 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8385 + wtoken + ": startingWindow="
8386 + wtoken.startingWindow + " startingView="
8387 + wtoken.startingView);
8388 if (wtoken.startingWindow != null) {
8389 view = wtoken.startingView;
8390 token = wtoken.token;
8391 wtoken.startingData = null;
8392 wtoken.startingView = null;
8393 wtoken.startingWindow = null;
8394 }
8395 }
8396 if (view != null) {
8397 try {
8398 mPolicy.removeStartingWindow(token, view);
8399 } catch (Exception e) {
8400 Log.w(TAG, "Exception when removing starting window", e);
8401 }
8402 }
8403 } break;
8404
8405 case FINISHED_STARTING: {
8406 IBinder token = null;
8407 View view = null;
8408 while (true) {
8409 synchronized (mWindowMap) {
8410 final int N = mFinishedStarting.size();
8411 if (N <= 0) {
8412 break;
8413 }
8414 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8415
8416 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8417 "Finished starting " + wtoken
8418 + ": startingWindow=" + wtoken.startingWindow
8419 + " startingView=" + wtoken.startingView);
8420
8421 if (wtoken.startingWindow == null) {
8422 continue;
8423 }
8424
8425 view = wtoken.startingView;
8426 token = wtoken.token;
8427 wtoken.startingData = null;
8428 wtoken.startingView = null;
8429 wtoken.startingWindow = null;
8430 }
8431
8432 try {
8433 mPolicy.removeStartingWindow(token, view);
8434 } catch (Exception e) {
8435 Log.w(TAG, "Exception when removing starting window", e);
8436 }
8437 }
8438 } break;
8439
8440 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8441 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8442
8443 boolean nowVisible = msg.arg1 != 0;
8444 boolean nowGone = msg.arg2 != 0;
8445
8446 try {
8447 if (DEBUG_VISIBILITY) Log.v(
8448 TAG, "Reporting visible in " + wtoken
8449 + " visible=" + nowVisible
8450 + " gone=" + nowGone);
8451 if (nowVisible) {
8452 wtoken.appToken.windowsVisible();
8453 } else {
8454 wtoken.appToken.windowsGone();
8455 }
8456 } catch (RemoteException ex) {
8457 }
8458 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 case WINDOW_FREEZE_TIMEOUT: {
8461 synchronized (mWindowMap) {
8462 Log.w(TAG, "Window freeze timeout expired.");
8463 int i = mWindows.size();
8464 while (i > 0) {
8465 i--;
8466 WindowState w = (WindowState)mWindows.get(i);
8467 if (w.mOrientationChanging) {
8468 w.mOrientationChanging = false;
8469 Log.w(TAG, "Force clearing orientation change: " + w);
8470 }
8471 }
8472 performLayoutAndPlaceSurfacesLocked();
8473 }
8474 break;
8475 }
Romain Guy06882f82009-06-10 13:36:04 -07008476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477 case HOLD_SCREEN_CHANGED: {
8478 Session oldHold;
8479 Session newHold;
8480 synchronized (mWindowMap) {
8481 oldHold = mLastReportedHold;
8482 newHold = (Session)msg.obj;
8483 mLastReportedHold = newHold;
8484 }
Romain Guy06882f82009-06-10 13:36:04 -07008485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008486 if (oldHold != newHold) {
8487 try {
8488 if (oldHold != null) {
8489 mBatteryStats.noteStopWakelock(oldHold.mUid,
8490 "window",
8491 BatteryStats.WAKE_TYPE_WINDOW);
8492 }
8493 if (newHold != null) {
8494 mBatteryStats.noteStartWakelock(newHold.mUid,
8495 "window",
8496 BatteryStats.WAKE_TYPE_WINDOW);
8497 }
8498 } catch (RemoteException e) {
8499 }
8500 }
8501 break;
8502 }
Romain Guy06882f82009-06-10 13:36:04 -07008503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 case APP_TRANSITION_TIMEOUT: {
8505 synchronized (mWindowMap) {
8506 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8507 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8508 "*** APP TRANSITION TIMEOUT");
8509 mAppTransitionReady = true;
8510 mAppTransitionTimeout = true;
8511 performLayoutAndPlaceSurfacesLocked();
8512 }
8513 }
8514 break;
8515 }
Romain Guy06882f82009-06-10 13:36:04 -07008516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008517 case PERSIST_ANIMATION_SCALE: {
8518 Settings.System.putFloat(mContext.getContentResolver(),
8519 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8520 Settings.System.putFloat(mContext.getContentResolver(),
8521 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8522 break;
8523 }
Romain Guy06882f82009-06-10 13:36:04 -07008524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 case FORCE_GC: {
8526 synchronized(mWindowMap) {
8527 if (mAnimationPending) {
8528 // If we are animating, don't do the gc now but
8529 // delay a bit so we don't interrupt the animation.
8530 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8531 2000);
8532 return;
8533 }
8534 // If we are currently rotating the display, it will
8535 // schedule a new message when done.
8536 if (mDisplayFrozen) {
8537 return;
8538 }
8539 mFreezeGcPending = 0;
8540 }
8541 Runtime.getRuntime().gc();
8542 break;
8543 }
Romain Guy06882f82009-06-10 13:36:04 -07008544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008545 case ENABLE_SCREEN: {
8546 performEnableScreen();
8547 break;
8548 }
Romain Guy06882f82009-06-10 13:36:04 -07008549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 case APP_FREEZE_TIMEOUT: {
8551 synchronized (mWindowMap) {
8552 Log.w(TAG, "App freeze timeout expired.");
8553 int i = mAppTokens.size();
8554 while (i > 0) {
8555 i--;
8556 AppWindowToken tok = mAppTokens.get(i);
8557 if (tok.freezingScreen) {
8558 Log.w(TAG, "Force clearing freeze: " + tok);
8559 unsetAppFreezingScreenLocked(tok, true, true);
8560 }
8561 }
8562 }
8563 break;
8564 }
Romain Guy06882f82009-06-10 13:36:04 -07008565
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008566 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008567 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008568 sendNewConfiguration();
8569 }
8570 break;
8571 }
Romain Guy06882f82009-06-10 13:36:04 -07008572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 }
8574 }
8575 }
8576
8577 // -------------------------------------------------------------
8578 // IWindowManager API
8579 // -------------------------------------------------------------
8580
8581 public IWindowSession openSession(IInputMethodClient client,
8582 IInputContext inputContext) {
8583 if (client == null) throw new IllegalArgumentException("null client");
8584 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8585 return new Session(client, inputContext);
8586 }
8587
8588 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8589 synchronized (mWindowMap) {
8590 // The focus for the client is the window immediately below
8591 // where we would place the input method window.
8592 int idx = findDesiredInputMethodWindowIndexLocked(false);
8593 WindowState imFocus;
8594 if (idx > 0) {
8595 imFocus = (WindowState)mWindows.get(idx-1);
8596 if (imFocus != null) {
8597 if (imFocus.mSession.mClient != null &&
8598 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8599 return true;
8600 }
8601 }
8602 }
8603 }
8604 return false;
8605 }
Romain Guy06882f82009-06-10 13:36:04 -07008606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 // -------------------------------------------------------------
8608 // Internals
8609 // -------------------------------------------------------------
8610
8611 final WindowState windowForClientLocked(Session session, IWindow client) {
8612 return windowForClientLocked(session, client.asBinder());
8613 }
Romain Guy06882f82009-06-10 13:36:04 -07008614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 final WindowState windowForClientLocked(Session session, IBinder client) {
8616 WindowState win = mWindowMap.get(client);
8617 if (localLOGV) Log.v(
8618 TAG, "Looking up client " + client + ": " + win);
8619 if (win == null) {
8620 RuntimeException ex = new RuntimeException();
8621 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8622 return null;
8623 }
8624 if (session != null && win.mSession != session) {
8625 RuntimeException ex = new RuntimeException();
8626 Log.w(TAG, "Requested window " + client + " is in session " +
8627 win.mSession + ", not " + session, ex);
8628 return null;
8629 }
8630
8631 return win;
8632 }
8633
Dianne Hackborna8f60182009-09-01 19:01:50 -07008634 final void rebuildAppWindowListLocked() {
8635 int NW = mWindows.size();
8636 int i;
8637
8638 // First remove all existing app windows.
8639 i=0;
8640 while (i < NW) {
8641 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008642 WindowState win = (WindowState)mWindows.remove(i);
8643 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8644 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008645 NW--;
8646 continue;
8647 }
8648 i++;
8649 }
8650
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008651 // First add all of the exiting app tokens... these are no longer
8652 // in the main app list, but still have windows shown. We put them
8653 // in the back because now that the animation is over we no longer
8654 // will care about them.
8655 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008656 i = 0;
8657 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008658 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8659 }
8660
8661 // And add in the still active app tokens in Z order.
8662 NT = mAppTokens.size();
8663 for (int j=0; j<NT; j++) {
8664 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008665 }
8666 }
8667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 private final void assignLayersLocked() {
8669 int N = mWindows.size();
8670 int curBaseLayer = 0;
8671 int curLayer = 0;
8672 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 for (i=0; i<N; i++) {
8675 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008676 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8677 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 curLayer += WINDOW_LAYER_MULTIPLIER;
8679 w.mLayer = curLayer;
8680 } else {
8681 curBaseLayer = curLayer = w.mBaseLayer;
8682 w.mLayer = curLayer;
8683 }
8684 if (w.mTargetAppToken != null) {
8685 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8686 } else if (w.mAppToken != null) {
8687 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8688 } else {
8689 w.mAnimLayer = w.mLayer;
8690 }
8691 if (w.mIsImWindow) {
8692 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008693 } else if (w.mIsWallpaper) {
8694 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 }
8696 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8697 + w.mAnimLayer);
8698 //System.out.println(
8699 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8700 }
8701 }
8702
8703 private boolean mInLayout = false;
8704 private final void performLayoutAndPlaceSurfacesLocked() {
8705 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008706 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 throw new RuntimeException("Recursive call!");
8708 }
8709 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8710 return;
8711 }
8712
8713 boolean recoveringMemory = false;
8714 if (mForceRemoves != null) {
8715 recoveringMemory = true;
8716 // Wait a little it for things to settle down, and off we go.
8717 for (int i=0; i<mForceRemoves.size(); i++) {
8718 WindowState ws = mForceRemoves.get(i);
8719 Log.i(TAG, "Force removing: " + ws);
8720 removeWindowInnerLocked(ws.mSession, ws);
8721 }
8722 mForceRemoves = null;
8723 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8724 Object tmp = new Object();
8725 synchronized (tmp) {
8726 try {
8727 tmp.wait(250);
8728 } catch (InterruptedException e) {
8729 }
8730 }
8731 }
Romain Guy06882f82009-06-10 13:36:04 -07008732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 mInLayout = true;
8734 try {
8735 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008737 int i = mPendingRemove.size()-1;
8738 if (i >= 0) {
8739 while (i >= 0) {
8740 WindowState w = mPendingRemove.get(i);
8741 removeWindowInnerLocked(w.mSession, w);
8742 i--;
8743 }
8744 mPendingRemove.clear();
8745
8746 mInLayout = false;
8747 assignLayersLocked();
8748 mLayoutNeeded = true;
8749 performLayoutAndPlaceSurfacesLocked();
8750
8751 } else {
8752 mInLayout = false;
8753 if (mLayoutNeeded) {
8754 requestAnimationLocked(0);
8755 }
8756 }
8757 } catch (RuntimeException e) {
8758 mInLayout = false;
8759 Log.e(TAG, "Unhandled exception while layout out windows", e);
8760 }
8761 }
8762
8763 private final void performLayoutLockedInner() {
8764 final int dw = mDisplay.getWidth();
8765 final int dh = mDisplay.getHeight();
8766
8767 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008768 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008769 int i;
8770
8771 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008772
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008773 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 mPolicy.beginLayoutLw(dw, dh);
8775
8776 // First perform layout of any root windows (not attached
8777 // to another window).
8778 int topAttached = -1;
8779 for (i = N-1; i >= 0; i--) {
8780 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008781
8782 // Don't do layout of a window if it is not visible, or
8783 // soon won't be visible, to avoid wasting time and funky
8784 // changes while a window is animating away.
8785 final AppWindowToken atoken = win.mAppToken;
8786 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008788 || win.mRootToken.hidden
8789 || (atoken != null && atoken.hiddenRequested)
8790 || !win.mPolicyVisibility
8791 || win.mAttachedHidden
8792 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793
8794 // If this view is GONE, then skip it -- keep the current
8795 // frame, and let the caller know so they can ignore it
8796 // if they want. (We do the normal layout for INVISIBLE
8797 // windows, since that means "perform layout as normal,
8798 // just don't display").
8799 if (!gone || !win.mHaveFrame) {
8800 if (!win.mLayoutAttached) {
8801 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8802 } else {
8803 if (topAttached < 0) topAttached = i;
8804 }
8805 }
8806 }
Romain Guy06882f82009-06-10 13:36:04 -07008807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008808 // Now perform layout of attached windows, which usually
8809 // depend on the position of the window they are attached to.
8810 // XXX does not deal with windows that are attached to windows
8811 // that are themselves attached.
8812 for (i = topAttached; i >= 0; i--) {
8813 WindowState win = (WindowState) mWindows.get(i);
8814
8815 // If this view is GONE, then skip it -- keep the current
8816 // frame, and let the caller know so they can ignore it
8817 // if they want. (We do the normal layout for INVISIBLE
8818 // windows, since that means "perform layout as normal,
8819 // just don't display").
8820 if (win.mLayoutAttached) {
8821 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8822 || !win.mHaveFrame) {
8823 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8824 }
8825 }
8826 }
8827
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008828 if (!mPolicy.finishLayoutLw()) {
8829 mLayoutNeeded = false;
8830 } else if (repeats > 2) {
8831 Log.w(TAG, "Layout repeat aborted after too many iterations");
8832 mLayoutNeeded = false;
8833 } else {
8834 repeats++;
8835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 }
8837 }
Romain Guy06882f82009-06-10 13:36:04 -07008838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008839 private final void performLayoutAndPlaceSurfacesLockedInner(
8840 boolean recoveringMemory) {
8841 final long currentTime = SystemClock.uptimeMillis();
8842 final int dw = mDisplay.getWidth();
8843 final int dh = mDisplay.getHeight();
8844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008845 int i;
8846
8847 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 if (mFxSession == null) {
8851 mFxSession = new SurfaceSession();
8852 }
Romain Guy06882f82009-06-10 13:36:04 -07008853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008854 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8855
8856 // Initialize state of exiting tokens.
8857 for (i=mExitingTokens.size()-1; i>=0; i--) {
8858 mExitingTokens.get(i).hasVisible = false;
8859 }
8860
8861 // Initialize state of exiting applications.
8862 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8863 mExitingAppTokens.get(i).hasVisible = false;
8864 }
8865
8866 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008867 boolean orientationChangeComplete = true;
8868 Session holdScreen = null;
8869 float screenBrightness = -1;
8870 boolean focusDisplayed = false;
8871 boolean animating = false;
8872
8873 Surface.openTransaction();
8874 try {
8875 boolean restart;
8876
8877 do {
8878 final int transactionSequence = ++mTransactionSequence;
8879
8880 // Update animations of all applications, including those
8881 // associated with exiting/removed apps
8882 boolean tokensAnimating = false;
8883 final int NAT = mAppTokens.size();
8884 for (i=0; i<NAT; i++) {
8885 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8886 tokensAnimating = true;
8887 }
8888 }
8889 final int NEAT = mExitingAppTokens.size();
8890 for (i=0; i<NEAT; i++) {
8891 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8892 tokensAnimating = true;
8893 }
8894 }
8895
8896 animating = tokensAnimating;
8897 restart = false;
8898
8899 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008900 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008901
8902 mPolicy.beginAnimationLw(dw, dh);
8903
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008904 final int N = mWindows.size();
8905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 for (i=N-1; i>=0; i--) {
8907 WindowState w = (WindowState)mWindows.get(i);
8908
8909 final WindowManager.LayoutParams attrs = w.mAttrs;
8910
8911 if (w.mSurface != null) {
8912 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008913 if (w.commitFinishDrawingLocked(currentTime)) {
8914 if ((w.mAttrs.flags
8915 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008916 if (DEBUG_WALLPAPER) Log.v(TAG,
8917 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008918 wallpaperMayChange = true;
8919 }
8920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8922 animating = true;
8923 //w.dump(" ");
8924 }
8925
8926 mPolicy.animatingWindowLw(w, attrs);
8927 }
8928
8929 final AppWindowToken atoken = w.mAppToken;
8930 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8931 if (atoken.lastTransactionSequence != transactionSequence) {
8932 atoken.lastTransactionSequence = transactionSequence;
8933 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8934 atoken.startingDisplayed = false;
8935 }
8936 if ((w.isOnScreen() || w.mAttrs.type
8937 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8938 && !w.mExiting && !w.mDestroying) {
8939 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8940 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8941 + w.isDisplayedLw()
8942 + ", isAnimating=" + w.isAnimating());
8943 if (!w.isDisplayedLw()) {
8944 Log.v(TAG, "Not displayed: s=" + w.mSurface
8945 + " pv=" + w.mPolicyVisibility
8946 + " dp=" + w.mDrawPending
8947 + " cdp=" + w.mCommitDrawPending
8948 + " ah=" + w.mAttachedHidden
8949 + " th=" + atoken.hiddenRequested
8950 + " a=" + w.mAnimating);
8951 }
8952 }
8953 if (w != atoken.startingWindow) {
8954 if (!atoken.freezingScreen || !w.mAppFreezing) {
8955 atoken.numInterestingWindows++;
8956 if (w.isDisplayedLw()) {
8957 atoken.numDrawnWindows++;
8958 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8959 "tokenMayBeDrawn: " + atoken
8960 + " freezingScreen=" + atoken.freezingScreen
8961 + " mAppFreezing=" + w.mAppFreezing);
8962 tokenMayBeDrawn = true;
8963 }
8964 }
8965 } else if (w.isDisplayedLw()) {
8966 atoken.startingDisplayed = true;
8967 }
8968 }
8969 } else if (w.mReadyToShow) {
8970 w.performShowLocked();
8971 }
8972 }
8973
8974 if (mPolicy.finishAnimationLw()) {
8975 restart = true;
8976 }
8977
8978 if (tokenMayBeDrawn) {
8979 // See if any windows have been drawn, so they (and others
8980 // associated with them) can now be shown.
8981 final int NT = mTokenList.size();
8982 for (i=0; i<NT; i++) {
8983 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8984 if (wtoken == null) {
8985 continue;
8986 }
8987 if (wtoken.freezingScreen) {
8988 int numInteresting = wtoken.numInterestingWindows;
8989 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8990 if (DEBUG_VISIBILITY) Log.v(TAG,
8991 "allDrawn: " + wtoken
8992 + " interesting=" + numInteresting
8993 + " drawn=" + wtoken.numDrawnWindows);
8994 wtoken.showAllWindowsLocked();
8995 unsetAppFreezingScreenLocked(wtoken, false, true);
8996 orientationChangeComplete = true;
8997 }
8998 } else if (!wtoken.allDrawn) {
8999 int numInteresting = wtoken.numInterestingWindows;
9000 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9001 if (DEBUG_VISIBILITY) Log.v(TAG,
9002 "allDrawn: " + wtoken
9003 + " interesting=" + numInteresting
9004 + " drawn=" + wtoken.numDrawnWindows);
9005 wtoken.allDrawn = true;
9006 restart = true;
9007
9008 // We can now show all of the drawn windows!
9009 if (!mOpeningApps.contains(wtoken)) {
9010 wtoken.showAllWindowsLocked();
9011 }
9012 }
9013 }
9014 }
9015 }
9016
9017 // If we are ready to perform an app transition, check through
9018 // all of the app tokens to be shown and see if they are ready
9019 // to go.
9020 if (mAppTransitionReady) {
9021 int NN = mOpeningApps.size();
9022 boolean goodToGo = true;
9023 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9024 "Checking " + NN + " opening apps (frozen="
9025 + mDisplayFrozen + " timeout="
9026 + mAppTransitionTimeout + ")...");
9027 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9028 // If the display isn't frozen, wait to do anything until
9029 // all of the apps are ready. Otherwise just go because
9030 // we'll unfreeze the display when everyone is ready.
9031 for (i=0; i<NN && goodToGo; i++) {
9032 AppWindowToken wtoken = mOpeningApps.get(i);
9033 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9034 "Check opening app" + wtoken + ": allDrawn="
9035 + wtoken.allDrawn + " startingDisplayed="
9036 + wtoken.startingDisplayed);
9037 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9038 && !wtoken.startingMoved) {
9039 goodToGo = false;
9040 }
9041 }
9042 }
9043 if (goodToGo) {
9044 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9045 int transit = mNextAppTransition;
9046 if (mSkipAppTransitionAnimation) {
9047 transit = WindowManagerPolicy.TRANSIT_NONE;
9048 }
9049 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9050 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009051 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 mAppTransitionTimeout = false;
9053 mStartingIconInTransition = false;
9054 mSkipAppTransitionAnimation = false;
9055
9056 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9057
Dianne Hackborna8f60182009-09-01 19:01:50 -07009058 // If there are applications waiting to come to the
9059 // top of the stack, now is the time to move their windows.
9060 // (Note that we don't do apps going to the bottom
9061 // here -- we want to keep their windows in the old
9062 // Z-order until the animation completes.)
9063 if (mToTopApps.size() > 0) {
9064 NN = mAppTokens.size();
9065 for (i=0; i<NN; i++) {
9066 AppWindowToken wtoken = mAppTokens.get(i);
9067 if (wtoken.sendingToTop) {
9068 wtoken.sendingToTop = false;
9069 moveAppWindowsLocked(wtoken, NN, false);
9070 }
9071 }
9072 mToTopApps.clear();
9073 }
9074
Dianne Hackborn25994b42009-09-04 14:21:19 -07009075 WindowState oldWallpaper = mWallpaperTarget;
9076
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009077 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009078 wallpaperMayChange = false;
9079
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009080 // The top-most window will supply the layout params,
9081 // and we will determine it below.
9082 LayoutParams animLp = null;
9083 int bestAnimLayer = -1;
9084
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009085 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009086 "New wallpaper target=" + mWallpaperTarget
9087 + ", lower target=" + mLowerWallpaperTarget
9088 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009089 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009090 // Do a first pass through the tokens for two
9091 // things:
9092 // (1) Determine if both the closing and opening
9093 // app token sets are wallpaper targets, in which
9094 // case special animations are needed
9095 // (since the wallpaper needs to stay static
9096 // behind them).
9097 // (2) Find the layout params of the top-most
9098 // application window in the tokens, which is
9099 // what will control the animation theme.
9100 final int NC = mClosingApps.size();
9101 NN = NC + mOpeningApps.size();
9102 for (i=0; i<NN; i++) {
9103 AppWindowToken wtoken;
9104 int mode;
9105 if (i < NC) {
9106 wtoken = mClosingApps.get(i);
9107 mode = 1;
9108 } else {
9109 wtoken = mOpeningApps.get(i-NC);
9110 mode = 2;
9111 }
9112 if (mLowerWallpaperTarget != null) {
9113 if (mLowerWallpaperTarget.mAppToken == wtoken
9114 || mUpperWallpaperTarget.mAppToken == wtoken) {
9115 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009116 }
9117 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009118 if (wtoken.appFullscreen) {
9119 WindowState ws = wtoken.findMainWindow();
9120 if (ws != null) {
9121 // If this is a compatibility mode
9122 // window, we will always use its anim.
9123 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9124 animLp = ws.mAttrs;
9125 bestAnimLayer = Integer.MAX_VALUE;
9126 } else if (ws.mLayer > bestAnimLayer) {
9127 animLp = ws.mAttrs;
9128 bestAnimLayer = ws.mLayer;
9129 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009130 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009131 }
9132 }
9133
Dianne Hackborn25994b42009-09-04 14:21:19 -07009134 if (foundWallpapers == 3) {
9135 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9136 "Wallpaper animation!");
9137 switch (transit) {
9138 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9139 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9140 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9141 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9142 break;
9143 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9144 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9145 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9146 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9147 break;
9148 }
9149 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9150 "New transit: " + transit);
9151 } else if (oldWallpaper != null) {
9152 // We are transitioning from an activity with
9153 // a wallpaper to one without.
9154 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9155 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9156 "New transit away from wallpaper: " + transit);
9157 } else if (mWallpaperTarget != null) {
9158 // We are transitioning from an activity without
9159 // a wallpaper to now showing the wallpaper
9160 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9161 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9162 "New transit into wallpaper: " + transit);
9163 }
9164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009165 NN = mOpeningApps.size();
9166 for (i=0; i<NN; i++) {
9167 AppWindowToken wtoken = mOpeningApps.get(i);
9168 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9169 "Now opening app" + wtoken);
9170 wtoken.reportedVisible = false;
9171 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009172 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009173 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009175 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 wtoken.showAllWindowsLocked();
9177 }
9178 NN = mClosingApps.size();
9179 for (i=0; i<NN; i++) {
9180 AppWindowToken wtoken = mClosingApps.get(i);
9181 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9182 "Now closing app" + wtoken);
9183 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009184 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009185 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009187 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009188 // Force the allDrawn flag, because we want to start
9189 // this guy's animations regardless of whether it's
9190 // gotten drawn.
9191 wtoken.allDrawn = true;
9192 }
9193
9194 mOpeningApps.clear();
9195 mClosingApps.clear();
9196
9197 // This has changed the visibility of windows, so perform
9198 // a new layout to get them all up-to-date.
9199 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009200 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9201 assignLayersLocked();
9202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009203 performLayoutLockedInner();
9204 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9205
9206 restart = true;
9207 }
9208 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009209
Dianne Hackborna8f60182009-09-01 19:01:50 -07009210 if (!animating && mAppTransitionRunning) {
9211 // We have finished the animation of an app transition. To do
9212 // this, we have delayed a lot of operations like showing and
9213 // hiding apps, moving apps in Z-order, etc. The app token list
9214 // reflects the correct Z-order, but the window list may now
9215 // be out of sync with it. So here we will just rebuild the
9216 // entire app window list. Fun!
9217 mAppTransitionRunning = false;
9218 // Clear information about apps that were moving.
9219 mToBottomApps.clear();
9220
9221 rebuildAppWindowListLocked();
9222 restart = true;
9223 moveInputMethodWindowsIfNeededLocked(false);
9224 wallpaperMayChange = true;
9225 mLayoutNeeded = true;
9226 }
9227
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009228 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009229 if (DEBUG_WALLPAPER) Log.v(TAG,
9230 "Wallpaper may change! Adjusting");
9231 int adjResult = adjustWallpaperWindowsLocked();
9232 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9233 if (DEBUG_WALLPAPER) Log.v(TAG,
9234 "Wallpaper layer changed: assigning layers + relayout");
9235 restart = true;
9236 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009237 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009238 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9239 if (DEBUG_WALLPAPER) Log.v(TAG,
9240 "Wallpaper visibility changed: relayout");
9241 restart = true;
9242 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009243 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009244 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009245 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009246 performLayoutLockedInner();
9247 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009248 }
9249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 } while (restart);
9251
9252 // THIRD LOOP: Update the surfaces of all windows.
9253
9254 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9255
9256 boolean obscured = false;
9257 boolean blurring = false;
9258 boolean dimming = false;
9259 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009260 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009261 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009263 final int N = mWindows.size();
9264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 for (i=N-1; i>=0; i--) {
9266 WindowState w = (WindowState)mWindows.get(i);
9267
9268 boolean displayed = false;
9269 final WindowManager.LayoutParams attrs = w.mAttrs;
9270 final int attrFlags = attrs.flags;
9271
9272 if (w.mSurface != null) {
9273 w.computeShownFrameLocked();
9274 if (localLOGV) Log.v(
9275 TAG, "Placing surface #" + i + " " + w.mSurface
9276 + ": new=" + w.mShownFrame + ", old="
9277 + w.mLastShownFrame);
9278
9279 boolean resize;
9280 int width, height;
9281 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9282 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9283 w.mLastRequestedHeight != w.mRequestedHeight;
9284 // for a scaled surface, we just want to use
9285 // the requested size.
9286 width = w.mRequestedWidth;
9287 height = w.mRequestedHeight;
9288 w.mLastRequestedWidth = width;
9289 w.mLastRequestedHeight = height;
9290 w.mLastShownFrame.set(w.mShownFrame);
9291 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009292 if (SHOW_TRANSACTIONS) Log.i(
9293 TAG, " SURFACE " + w.mSurface
9294 + ": POS " + w.mShownFrame.left
9295 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9297 } catch (RuntimeException e) {
9298 Log.w(TAG, "Error positioning surface in " + w, e);
9299 if (!recoveringMemory) {
9300 reclaimSomeSurfaceMemoryLocked(w, "position");
9301 }
9302 }
9303 } else {
9304 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9305 width = w.mShownFrame.width();
9306 height = w.mShownFrame.height();
9307 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009308 }
9309
9310 if (resize) {
9311 if (width < 1) width = 1;
9312 if (height < 1) height = 1;
9313 if (w.mSurface != null) {
9314 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009315 if (SHOW_TRANSACTIONS) Log.i(
9316 TAG, " SURFACE " + w.mSurface + ": POS "
9317 + w.mShownFrame.left + ","
9318 + w.mShownFrame.top + " SIZE "
9319 + w.mShownFrame.width() + "x"
9320 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009321 w.mSurface.setSize(width, height);
9322 w.mSurface.setPosition(w.mShownFrame.left,
9323 w.mShownFrame.top);
9324 } catch (RuntimeException e) {
9325 // If something goes wrong with the surface (such
9326 // as running out of memory), don't take down the
9327 // entire system.
9328 Log.e(TAG, "Failure updating surface of " + w
9329 + "size=(" + width + "x" + height
9330 + "), pos=(" + w.mShownFrame.left
9331 + "," + w.mShownFrame.top + ")", e);
9332 if (!recoveringMemory) {
9333 reclaimSomeSurfaceMemoryLocked(w, "size");
9334 }
9335 }
9336 }
9337 }
9338 if (!w.mAppFreezing) {
9339 w.mContentInsetsChanged =
9340 !w.mLastContentInsets.equals(w.mContentInsets);
9341 w.mVisibleInsetsChanged =
9342 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009343 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 || w.mContentInsetsChanged
9345 || w.mVisibleInsetsChanged) {
9346 w.mLastFrame.set(w.mFrame);
9347 w.mLastContentInsets.set(w.mContentInsets);
9348 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009349 // If the screen is currently frozen, then keep
9350 // it frozen until this window draws at its new
9351 // orientation.
9352 if (mDisplayFrozen) {
9353 if (DEBUG_ORIENTATION) Log.v(TAG,
9354 "Resizing while display frozen: " + w);
9355 w.mOrientationChanging = true;
9356 if (mWindowsFreezingScreen) {
9357 mWindowsFreezingScreen = true;
9358 // XXX should probably keep timeout from
9359 // when we first froze the display.
9360 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9361 mH.sendMessageDelayed(mH.obtainMessage(
9362 H.WINDOW_FREEZE_TIMEOUT), 2000);
9363 }
9364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009365 // If the orientation is changing, then we need to
9366 // hold off on unfreezing the display until this
9367 // window has been redrawn; to do that, we need
9368 // to go through the process of getting informed
9369 // by the application when it has finished drawing.
9370 if (w.mOrientationChanging) {
9371 if (DEBUG_ORIENTATION) Log.v(TAG,
9372 "Orientation start waiting for draw in "
9373 + w + ", surface " + w.mSurface);
9374 w.mDrawPending = true;
9375 w.mCommitDrawPending = false;
9376 w.mReadyToShow = false;
9377 if (w.mAppToken != null) {
9378 w.mAppToken.allDrawn = false;
9379 }
9380 }
Romain Guy06882f82009-06-10 13:36:04 -07009381 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382 "Resizing window " + w + " to " + w.mFrame);
9383 mResizingWindows.add(w);
9384 } else if (w.mOrientationChanging) {
9385 if (!w.mDrawPending && !w.mCommitDrawPending) {
9386 if (DEBUG_ORIENTATION) Log.v(TAG,
9387 "Orientation not waiting for draw in "
9388 + w + ", surface " + w.mSurface);
9389 w.mOrientationChanging = false;
9390 }
9391 }
9392 }
9393
9394 if (w.mAttachedHidden) {
9395 if (!w.mLastHidden) {
9396 //dump();
9397 w.mLastHidden = true;
9398 if (SHOW_TRANSACTIONS) Log.i(
9399 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9400 if (w.mSurface != null) {
9401 try {
9402 w.mSurface.hide();
9403 } catch (RuntimeException e) {
9404 Log.w(TAG, "Exception hiding surface in " + w);
9405 }
9406 }
9407 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9408 }
9409 // If we are waiting for this window to handle an
9410 // orientation change, well, it is hidden, so
9411 // doesn't really matter. Note that this does
9412 // introduce a potential glitch if the window
9413 // becomes unhidden before it has drawn for the
9414 // new orientation.
9415 if (w.mOrientationChanging) {
9416 w.mOrientationChanging = false;
9417 if (DEBUG_ORIENTATION) Log.v(TAG,
9418 "Orientation change skips hidden " + w);
9419 }
9420 } else if (!w.isReadyForDisplay()) {
9421 if (!w.mLastHidden) {
9422 //dump();
9423 w.mLastHidden = true;
9424 if (SHOW_TRANSACTIONS) Log.i(
9425 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9426 if (w.mSurface != null) {
9427 try {
9428 w.mSurface.hide();
9429 } catch (RuntimeException e) {
9430 Log.w(TAG, "Exception exception hiding surface in " + w);
9431 }
9432 }
9433 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9434 }
9435 // If we are waiting for this window to handle an
9436 // orientation change, well, it is hidden, so
9437 // doesn't really matter. Note that this does
9438 // introduce a potential glitch if the window
9439 // becomes unhidden before it has drawn for the
9440 // new orientation.
9441 if (w.mOrientationChanging) {
9442 w.mOrientationChanging = false;
9443 if (DEBUG_ORIENTATION) Log.v(TAG,
9444 "Orientation change skips hidden " + w);
9445 }
9446 } else if (w.mLastLayer != w.mAnimLayer
9447 || w.mLastAlpha != w.mShownAlpha
9448 || w.mLastDsDx != w.mDsDx
9449 || w.mLastDtDx != w.mDtDx
9450 || w.mLastDsDy != w.mDsDy
9451 || w.mLastDtDy != w.mDtDy
9452 || w.mLastHScale != w.mHScale
9453 || w.mLastVScale != w.mVScale
9454 || w.mLastHidden) {
9455 displayed = true;
9456 w.mLastAlpha = w.mShownAlpha;
9457 w.mLastLayer = w.mAnimLayer;
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 if (SHOW_TRANSACTIONS) Log.i(
9465 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009466 + w.mShownAlpha + " layer=" + w.mAnimLayer
9467 + " matrix=[" + (w.mDsDx*w.mHScale)
9468 + "," + (w.mDtDx*w.mVScale)
9469 + "][" + (w.mDsDy*w.mHScale)
9470 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 if (w.mSurface != null) {
9472 try {
9473 w.mSurface.setAlpha(w.mShownAlpha);
9474 w.mSurface.setLayer(w.mAnimLayer);
9475 w.mSurface.setMatrix(
9476 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9477 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9478 } catch (RuntimeException e) {
9479 Log.w(TAG, "Error updating surface in " + w, e);
9480 if (!recoveringMemory) {
9481 reclaimSomeSurfaceMemoryLocked(w, "update");
9482 }
9483 }
9484 }
9485
9486 if (w.mLastHidden && !w.mDrawPending
9487 && !w.mCommitDrawPending
9488 && !w.mReadyToShow) {
9489 if (SHOW_TRANSACTIONS) Log.i(
9490 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9491 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9492 + " during relayout");
9493 if (showSurfaceRobustlyLocked(w)) {
9494 w.mHasDrawn = true;
9495 w.mLastHidden = false;
9496 } else {
9497 w.mOrientationChanging = false;
9498 }
9499 }
9500 if (w.mSurface != null) {
9501 w.mToken.hasVisible = true;
9502 }
9503 } else {
9504 displayed = true;
9505 }
9506
9507 if (displayed) {
9508 if (!covered) {
9509 if (attrs.width == LayoutParams.FILL_PARENT
9510 && attrs.height == LayoutParams.FILL_PARENT) {
9511 covered = true;
9512 }
9513 }
9514 if (w.mOrientationChanging) {
9515 if (w.mDrawPending || w.mCommitDrawPending) {
9516 orientationChangeComplete = false;
9517 if (DEBUG_ORIENTATION) Log.v(TAG,
9518 "Orientation continue waiting for draw in " + w);
9519 } else {
9520 w.mOrientationChanging = false;
9521 if (DEBUG_ORIENTATION) Log.v(TAG,
9522 "Orientation change complete in " + w);
9523 }
9524 }
9525 w.mToken.hasVisible = true;
9526 }
9527 } else if (w.mOrientationChanging) {
9528 if (DEBUG_ORIENTATION) Log.v(TAG,
9529 "Orientation change skips hidden " + w);
9530 w.mOrientationChanging = false;
9531 }
9532
9533 final boolean canBeSeen = w.isDisplayedLw();
9534
9535 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9536 focusDisplayed = true;
9537 }
9538
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009539 final boolean obscuredChanged = w.mObscured != obscured;
9540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009542 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009543 if (w.mSurface != null) {
9544 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9545 holdScreen = w.mSession;
9546 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009547 if (!syswin && w.mAttrs.screenBrightness >= 0
9548 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 screenBrightness = w.mAttrs.screenBrightness;
9550 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009551 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9552 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9553 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9554 syswin = true;
9555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009557
Dianne Hackborn25994b42009-09-04 14:21:19 -07009558 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9559 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560 // This window completely covers everything behind it,
9561 // so we want to leave all of them as unblurred (for
9562 // performance reasons).
9563 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009564 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9565 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009566 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009567 obscured = true;
9568 if (mBackgroundFillerSurface == null) {
9569 try {
9570 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9571 0, dw, dh,
9572 PixelFormat.OPAQUE,
9573 Surface.FX_SURFACE_NORMAL);
9574 } catch (Exception e) {
9575 Log.e(TAG, "Exception creating filler surface", e);
9576 }
9577 }
9578 try {
9579 mBackgroundFillerSurface.setPosition(0, 0);
9580 mBackgroundFillerSurface.setSize(dw, dh);
9581 // Using the same layer as Dim because they will never be shown at the
9582 // same time.
9583 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9584 mBackgroundFillerSurface.show();
9585 } catch (RuntimeException e) {
9586 Log.e(TAG, "Exception showing filler surface");
9587 }
9588 backgroundFillerShown = true;
9589 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009590 } else if (canBeSeen && !obscured &&
9591 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9592 if (localLOGV) Log.v(TAG, "Win " + w
9593 + ": blurring=" + blurring
9594 + " obscured=" + obscured
9595 + " displayed=" + displayed);
9596 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9597 if (!dimming) {
9598 //Log.i(TAG, "DIM BEHIND: " + w);
9599 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009600 if (mDimAnimator == null) {
9601 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009603 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009604 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009605 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009606 }
9607 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9608 if (!blurring) {
9609 //Log.i(TAG, "BLUR BEHIND: " + w);
9610 blurring = true;
9611 mBlurShown = true;
9612 if (mBlurSurface == null) {
9613 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9614 + mBlurSurface + ": CREATE");
9615 try {
Romain Guy06882f82009-06-10 13:36:04 -07009616 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009617 -1, 16, 16,
9618 PixelFormat.OPAQUE,
9619 Surface.FX_SURFACE_BLUR);
9620 } catch (Exception e) {
9621 Log.e(TAG, "Exception creating Blur surface", e);
9622 }
9623 }
9624 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9625 + mBlurSurface + ": SHOW pos=(0,0) (" +
9626 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9627 if (mBlurSurface != null) {
9628 mBlurSurface.setPosition(0, 0);
9629 mBlurSurface.setSize(dw, dh);
9630 try {
9631 mBlurSurface.show();
9632 } catch (RuntimeException e) {
9633 Log.w(TAG, "Failure showing blur surface", e);
9634 }
9635 }
9636 }
9637 mBlurSurface.setLayer(w.mAnimLayer-2);
9638 }
9639 }
9640 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009641
9642 if (obscuredChanged && mWallpaperTarget == w) {
9643 // This is the wallpaper target and its obscured state
9644 // changed... make sure the current wallaper's visibility
9645 // has been updated accordingly.
9646 updateWallpaperVisibilityLocked();
9647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009649
9650 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9651 mBackgroundFillerShown = false;
9652 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9653 try {
9654 mBackgroundFillerSurface.hide();
9655 } catch (RuntimeException e) {
9656 Log.e(TAG, "Exception hiding filler surface", e);
9657 }
9658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009659
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009660 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9661 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 }
Romain Guy06882f82009-06-10 13:36:04 -07009663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009664 if (!blurring && mBlurShown) {
9665 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9666 + ": HIDE");
9667 try {
9668 mBlurSurface.hide();
9669 } catch (IllegalArgumentException e) {
9670 Log.w(TAG, "Illegal argument exception hiding blur surface");
9671 }
9672 mBlurShown = false;
9673 }
9674
9675 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9676 } catch (RuntimeException e) {
9677 Log.e(TAG, "Unhandled exception in Window Manager", e);
9678 }
9679
9680 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009682 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9683 "With display frozen, orientationChangeComplete="
9684 + orientationChangeComplete);
9685 if (orientationChangeComplete) {
9686 if (mWindowsFreezingScreen) {
9687 mWindowsFreezingScreen = false;
9688 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9689 }
9690 if (mAppsFreezingScreen == 0) {
9691 stopFreezingDisplayLocked();
9692 }
9693 }
Romain Guy06882f82009-06-10 13:36:04 -07009694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009695 i = mResizingWindows.size();
9696 if (i > 0) {
9697 do {
9698 i--;
9699 WindowState win = mResizingWindows.get(i);
9700 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009701 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9702 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 win.mClient.resized(win.mFrame.width(),
9704 win.mFrame.height(), win.mLastContentInsets,
9705 win.mLastVisibleInsets, win.mDrawPending);
9706 win.mContentInsetsChanged = false;
9707 win.mVisibleInsetsChanged = false;
9708 } catch (RemoteException e) {
9709 win.mOrientationChanging = false;
9710 }
9711 } while (i > 0);
9712 mResizingWindows.clear();
9713 }
Romain Guy06882f82009-06-10 13:36:04 -07009714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009716 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 i = mDestroySurface.size();
9718 if (i > 0) {
9719 do {
9720 i--;
9721 WindowState win = mDestroySurface.get(i);
9722 win.mDestroying = false;
9723 if (mInputMethodWindow == win) {
9724 mInputMethodWindow = null;
9725 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009726 if (win == mWallpaperTarget) {
9727 wallpaperDestroyed = true;
9728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009729 win.destroySurfaceLocked();
9730 } while (i > 0);
9731 mDestroySurface.clear();
9732 }
9733
9734 // Time to remove any exiting tokens?
9735 for (i=mExitingTokens.size()-1; i>=0; i--) {
9736 WindowToken token = mExitingTokens.get(i);
9737 if (!token.hasVisible) {
9738 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009739 if (token.windowType == TYPE_WALLPAPER) {
9740 mWallpaperTokens.remove(token);
9741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009742 }
9743 }
9744
9745 // Time to remove any exiting applications?
9746 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9747 AppWindowToken token = mExitingAppTokens.get(i);
9748 if (!token.hasVisible && !mClosingApps.contains(token)) {
9749 mAppTokens.remove(token);
9750 mExitingAppTokens.remove(i);
9751 }
9752 }
9753
Dianne Hackborna8f60182009-09-01 19:01:50 -07009754 boolean needRelayout = false;
9755
9756 if (!animating && mAppTransitionRunning) {
9757 // We have finished the animation of an app transition. To do
9758 // this, we have delayed a lot of operations like showing and
9759 // hiding apps, moving apps in Z-order, etc. The app token list
9760 // reflects the correct Z-order, but the window list may now
9761 // be out of sync with it. So here we will just rebuild the
9762 // entire app window list. Fun!
9763 mAppTransitionRunning = false;
9764 needRelayout = true;
9765 rebuildAppWindowListLocked();
9766 // Clear information about apps that were moving.
9767 mToBottomApps.clear();
9768 }
9769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 if (focusDisplayed) {
9771 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9772 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009773 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009774 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009775 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009776 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009777 requestAnimationLocked(0);
9778 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9780 }
9781 mQueue.setHoldScreenLocked(holdScreen != null);
9782 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9783 mPowerManager.setScreenBrightnessOverride(-1);
9784 } else {
9785 mPowerManager.setScreenBrightnessOverride((int)
9786 (screenBrightness * Power.BRIGHTNESS_ON));
9787 }
9788 if (holdScreen != mHoldingScreenOn) {
9789 mHoldingScreenOn = holdScreen;
9790 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9791 mH.sendMessage(m);
9792 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009793
9794 if (mTurnOnScreen) {
9795 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
9796 LocalPowerManager.BUTTON_EVENT, true);
9797 mTurnOnScreen = false;
9798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 }
9800
9801 void requestAnimationLocked(long delay) {
9802 if (!mAnimationPending) {
9803 mAnimationPending = true;
9804 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9805 }
9806 }
Romain Guy06882f82009-06-10 13:36:04 -07009807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 /**
9809 * Have the surface flinger show a surface, robustly dealing with
9810 * error conditions. In particular, if there is not enough memory
9811 * to show the surface, then we will try to get rid of other surfaces
9812 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009813 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009814 * @return Returns true if the surface was successfully shown.
9815 */
9816 boolean showSurfaceRobustlyLocked(WindowState win) {
9817 try {
9818 if (win.mSurface != null) {
9819 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -07009820 if (win.mTurnOnScreen) {
9821 win.mTurnOnScreen = false;
9822 mTurnOnScreen = true;
9823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009824 }
9825 return true;
9826 } catch (RuntimeException e) {
9827 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9828 }
Romain Guy06882f82009-06-10 13:36:04 -07009829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009832 return false;
9833 }
Romain Guy06882f82009-06-10 13:36:04 -07009834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9836 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9839 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 if (mForceRemoves == null) {
9842 mForceRemoves = new ArrayList<WindowState>();
9843 }
Romain Guy06882f82009-06-10 13:36:04 -07009844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 long callingIdentity = Binder.clearCallingIdentity();
9846 try {
9847 // There was some problem... first, do a sanity check of the
9848 // window list to make sure we haven't left any dangling surfaces
9849 // around.
9850 int N = mWindows.size();
9851 boolean leakedSurface = false;
9852 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9853 for (int i=0; i<N; i++) {
9854 WindowState ws = (WindowState)mWindows.get(i);
9855 if (ws.mSurface != null) {
9856 if (!mSessions.contains(ws.mSession)) {
9857 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9858 + ws + " surface=" + ws.mSurface
9859 + " token=" + win.mToken
9860 + " pid=" + ws.mSession.mPid
9861 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009862 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009863 ws.mSurface = null;
9864 mForceRemoves.add(ws);
9865 i--;
9866 N--;
9867 leakedSurface = true;
9868 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9869 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9870 + ws + " surface=" + ws.mSurface
9871 + " token=" + win.mAppToken);
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 leakedSurface = true;
9875 }
9876 }
9877 }
Romain Guy06882f82009-06-10 13:36:04 -07009878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 boolean killedApps = false;
9880 if (!leakedSurface) {
9881 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9882 SparseIntArray pidCandidates = new SparseIntArray();
9883 for (int i=0; i<N; i++) {
9884 WindowState ws = (WindowState)mWindows.get(i);
9885 if (ws.mSurface != null) {
9886 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9887 }
9888 }
9889 if (pidCandidates.size() > 0) {
9890 int[] pids = new int[pidCandidates.size()];
9891 for (int i=0; i<pids.length; i++) {
9892 pids[i] = pidCandidates.keyAt(i);
9893 }
9894 try {
9895 if (mActivityManager.killPidsForMemory(pids)) {
9896 killedApps = true;
9897 }
9898 } catch (RemoteException e) {
9899 }
9900 }
9901 }
Romain Guy06882f82009-06-10 13:36:04 -07009902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 if (leakedSurface || killedApps) {
9904 // We managed to reclaim some memory, so get rid of the trouble
9905 // surface and ask the app to request another one.
9906 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9907 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009908 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 win.mSurface = null;
9910 }
Romain Guy06882f82009-06-10 13:36:04 -07009911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 try {
9913 win.mClient.dispatchGetNewSurface();
9914 } catch (RemoteException e) {
9915 }
9916 }
9917 } finally {
9918 Binder.restoreCallingIdentity(callingIdentity);
9919 }
9920 }
Romain Guy06882f82009-06-10 13:36:04 -07009921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 private boolean updateFocusedWindowLocked(int mode) {
9923 WindowState newFocus = computeFocusedWindowLocked();
9924 if (mCurrentFocus != newFocus) {
9925 // This check makes sure that we don't already have the focus
9926 // change message pending.
9927 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9928 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9929 if (localLOGV) Log.v(
9930 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9931 final WindowState oldFocus = mCurrentFocus;
9932 mCurrentFocus = newFocus;
9933 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 final WindowState imWindow = mInputMethodWindow;
9936 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009937 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009939 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9940 mLayoutNeeded = true;
9941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9943 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009944 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9945 // Client will do the layout, but we need to assign layers
9946 // for handleNewWindowLocked() below.
9947 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 }
9949 }
Romain Guy06882f82009-06-10 13:36:04 -07009950
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009951 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9952 mKeyWaiter.handleNewWindowLocked(newFocus);
9953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 return true;
9955 }
9956 return false;
9957 }
9958
9959 private WindowState computeFocusedWindowLocked() {
9960 WindowState result = null;
9961 WindowState win;
9962
9963 int i = mWindows.size() - 1;
9964 int nextAppIndex = mAppTokens.size()-1;
9965 WindowToken nextApp = nextAppIndex >= 0
9966 ? mAppTokens.get(nextAppIndex) : null;
9967
9968 while (i >= 0) {
9969 win = (WindowState)mWindows.get(i);
9970
9971 if (localLOGV || DEBUG_FOCUS) Log.v(
9972 TAG, "Looking for focus: " + i
9973 + " = " + win
9974 + ", flags=" + win.mAttrs.flags
9975 + ", canReceive=" + win.canReceiveKeys());
9976
9977 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 // If this window's application has been removed, just skip it.
9980 if (thisApp != null && thisApp.removed) {
9981 i--;
9982 continue;
9983 }
Romain Guy06882f82009-06-10 13:36:04 -07009984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 // If there is a focused app, don't allow focus to go to any
9986 // windows below it. If this is an application window, step
9987 // through the app tokens until we find its app.
9988 if (thisApp != null && nextApp != null && thisApp != nextApp
9989 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9990 int origAppIndex = nextAppIndex;
9991 while (nextAppIndex > 0) {
9992 if (nextApp == mFocusedApp) {
9993 // Whoops, we are below the focused app... no focus
9994 // for you!
9995 if (localLOGV || DEBUG_FOCUS) Log.v(
9996 TAG, "Reached focused app: " + mFocusedApp);
9997 return null;
9998 }
9999 nextAppIndex--;
10000 nextApp = mAppTokens.get(nextAppIndex);
10001 if (nextApp == thisApp) {
10002 break;
10003 }
10004 }
10005 if (thisApp != nextApp) {
10006 // Uh oh, the app token doesn't exist! This shouldn't
10007 // happen, but if it does we can get totally hosed...
10008 // so restart at the original app.
10009 nextAppIndex = origAppIndex;
10010 nextApp = mAppTokens.get(nextAppIndex);
10011 }
10012 }
10013
10014 // Dispatch to this window if it is wants key events.
10015 if (win.canReceiveKeys()) {
10016 if (DEBUG_FOCUS) Log.v(
10017 TAG, "Found focus @ " + i + " = " + win);
10018 result = win;
10019 break;
10020 }
10021
10022 i--;
10023 }
10024
10025 return result;
10026 }
10027
10028 private void startFreezingDisplayLocked() {
10029 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010030 // Freezing the display also suspends key event delivery, to
10031 // keep events from going astray while the display is reconfigured.
10032 // If someone has changed orientation again while the screen is
10033 // still frozen, the events will continue to be blocked while the
10034 // successive orientation change is processed. To prevent spurious
10035 // ANRs, we reset the event dispatch timeout in this case.
10036 synchronized (mKeyWaiter) {
10037 mKeyWaiter.mWasFrozen = true;
10038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 return;
10040 }
Romain Guy06882f82009-06-10 13:36:04 -070010041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010044 long now = SystemClock.uptimeMillis();
10045 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10046 if (mFreezeGcPending != 0) {
10047 if (now > (mFreezeGcPending+1000)) {
10048 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10049 mH.removeMessages(H.FORCE_GC);
10050 Runtime.getRuntime().gc();
10051 mFreezeGcPending = now;
10052 }
10053 } else {
10054 mFreezeGcPending = now;
10055 }
Romain Guy06882f82009-06-10 13:36:04 -070010056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010057 mDisplayFrozen = true;
10058 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
10059 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
10060 mAppTransitionReady = true;
10061 }
Romain Guy06882f82009-06-10 13:36:04 -070010062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 if (PROFILE_ORIENTATION) {
10064 File file = new File("/data/system/frozen");
10065 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10066 }
10067 Surface.freezeDisplay(0);
10068 }
Romain Guy06882f82009-06-10 13:36:04 -070010069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010070 private void stopFreezingDisplayLocked() {
10071 if (!mDisplayFrozen) {
10072 return;
10073 }
Romain Guy06882f82009-06-10 13:36:04 -070010074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010075 mDisplayFrozen = false;
10076 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10077 if (PROFILE_ORIENTATION) {
10078 Debug.stopMethodTracing();
10079 }
10080 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010081
Chris Tate2ad63a92009-03-25 17:36:48 -070010082 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10083 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010084 synchronized (mKeyWaiter) {
10085 mKeyWaiter.mWasFrozen = true;
10086 mKeyWaiter.notifyAll();
10087 }
10088
10089 // A little kludge: a lot could have happened while the
10090 // display was frozen, so now that we are coming back we
10091 // do a gc so that any remote references the system
10092 // processes holds on others can be released if they are
10093 // no longer needed.
10094 mH.removeMessages(H.FORCE_GC);
10095 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10096 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 mScreenFrozenLock.release();
10099 }
Romain Guy06882f82009-06-10 13:36:04 -070010100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 @Override
10102 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10103 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10104 != PackageManager.PERMISSION_GRANTED) {
10105 pw.println("Permission Denial: can't dump WindowManager from from pid="
10106 + Binder.getCallingPid()
10107 + ", uid=" + Binder.getCallingUid());
10108 return;
10109 }
Romain Guy06882f82009-06-10 13:36:04 -070010110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111 synchronized(mWindowMap) {
10112 pw.println("Current Window Manager state:");
10113 for (int i=mWindows.size()-1; i>=0; i--) {
10114 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010115 pw.print(" Window #"); pw.print(i); pw.print(' ');
10116 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010117 w.dump(pw, " ");
10118 }
10119 if (mInputMethodDialogs.size() > 0) {
10120 pw.println(" ");
10121 pw.println(" Input method dialogs:");
10122 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10123 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010124 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010125 }
10126 }
10127 if (mPendingRemove.size() > 0) {
10128 pw.println(" ");
10129 pw.println(" Remove pending for:");
10130 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10131 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010132 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10133 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010134 w.dump(pw, " ");
10135 }
10136 }
10137 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10138 pw.println(" ");
10139 pw.println(" Windows force removing:");
10140 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10141 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010142 pw.print(" Removing #"); 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 (mDestroySurface.size() > 0) {
10148 pw.println(" ");
10149 pw.println(" Windows waiting to destroy their surface:");
10150 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10151 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010152 pw.print(" Destroy #"); 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 (mLosingFocus.size() > 0) {
10158 pw.println(" ");
10159 pw.println(" Windows losing focus:");
10160 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10161 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010162 pw.print(" Losing #"); 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 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010167 if (mResizingWindows.size() > 0) {
10168 pw.println(" ");
10169 pw.println(" Windows waiting to resize:");
10170 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10171 WindowState w = mResizingWindows.get(i);
10172 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10173 pw.print(w); pw.println(":");
10174 w.dump(pw, " ");
10175 }
10176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010177 if (mSessions.size() > 0) {
10178 pw.println(" ");
10179 pw.println(" All active sessions:");
10180 Iterator<Session> it = mSessions.iterator();
10181 while (it.hasNext()) {
10182 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010183 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010184 s.dump(pw, " ");
10185 }
10186 }
10187 if (mTokenMap.size() > 0) {
10188 pw.println(" ");
10189 pw.println(" All tokens:");
10190 Iterator<WindowToken> it = mTokenMap.values().iterator();
10191 while (it.hasNext()) {
10192 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010193 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 token.dump(pw, " ");
10195 }
10196 }
10197 if (mTokenList.size() > 0) {
10198 pw.println(" ");
10199 pw.println(" Window token list:");
10200 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010201 pw.print(" #"); pw.print(i); pw.print(": ");
10202 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 }
10204 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010205 if (mWallpaperTokens.size() > 0) {
10206 pw.println(" ");
10207 pw.println(" Wallpaper tokens:");
10208 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10209 WindowToken token = mWallpaperTokens.get(i);
10210 pw.print(" Wallpaper #"); pw.print(i);
10211 pw.print(' '); pw.print(token); pw.println(':');
10212 token.dump(pw, " ");
10213 }
10214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010215 if (mAppTokens.size() > 0) {
10216 pw.println(" ");
10217 pw.println(" Application tokens in Z order:");
10218 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010219 pw.print(" App #"); pw.print(i); pw.print(": ");
10220 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 }
10222 }
10223 if (mFinishedStarting.size() > 0) {
10224 pw.println(" ");
10225 pw.println(" Finishing start of application tokens:");
10226 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10227 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010228 pw.print(" Finished Starting #"); pw.print(i);
10229 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010230 token.dump(pw, " ");
10231 }
10232 }
10233 if (mExitingTokens.size() > 0) {
10234 pw.println(" ");
10235 pw.println(" Exiting tokens:");
10236 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10237 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010238 pw.print(" Exiting #"); 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 (mExitingAppTokens.size() > 0) {
10244 pw.println(" ");
10245 pw.println(" Exiting application tokens:");
10246 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10247 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010248 pw.print(" Exiting App #"); 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 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010254 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10255 pw.print(" mLastFocus="); pw.println(mLastFocus);
10256 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10257 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10258 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010259 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010260 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10261 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10262 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10263 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010264 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10265 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10266 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10267 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10268 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010269 if (mDimAnimator != null) {
10270 mDimAnimator.printTo(pw);
10271 } else {
10272 pw.print( " no DimAnimator ");
10273 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010274 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010275 pw.print(mInputMethodAnimLayerAdjustment);
10276 pw.print(" mWallpaperAnimLayerAdjustment=");
10277 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010278 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10279 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010280 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10281 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10282 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10283 pw.print(" mRotation="); pw.print(mRotation);
10284 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10285 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10286 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10287 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10288 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10289 pw.print(" mNextAppTransition=0x");
10290 pw.print(Integer.toHexString(mNextAppTransition));
10291 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010292 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010293 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10294 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10295 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10296 if (mOpeningApps.size() > 0) {
10297 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10298 }
10299 if (mClosingApps.size() > 0) {
10300 pw.print(" mClosingApps="); pw.println(mClosingApps);
10301 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010302 if (mToTopApps.size() > 0) {
10303 pw.print(" mToTopApps="); pw.println(mToTopApps);
10304 }
10305 if (mToBottomApps.size() > 0) {
10306 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10307 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010308 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10309 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010311 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10312 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10313 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10314 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10315 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10316 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 }
10318 }
10319
10320 public void monitor() {
10321 synchronized (mWindowMap) { }
10322 synchronized (mKeyguardDisabled) { }
10323 synchronized (mKeyWaiter) { }
10324 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010325
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010326 public void virtualKeyFeedback(KeyEvent event) {
10327 mPolicy.keyFeedbackFromInput(event);
10328 }
10329
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010330 /**
10331 * DimAnimator class that controls the dim animation. This holds the surface and
10332 * all state used for dim animation.
10333 */
10334 private static class DimAnimator {
10335 Surface mDimSurface;
10336 boolean mDimShown = false;
10337 float mDimCurrentAlpha;
10338 float mDimTargetAlpha;
10339 float mDimDeltaPerMs;
10340 long mLastDimAnimTime;
10341
10342 DimAnimator (SurfaceSession session) {
10343 if (mDimSurface == null) {
10344 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10345 + mDimSurface + ": CREATE");
10346 try {
10347 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10348 Surface.FX_SURFACE_DIM);
10349 } catch (Exception e) {
10350 Log.e(TAG, "Exception creating Dim surface", e);
10351 }
10352 }
10353 }
10354
10355 /**
10356 * Show the dim surface.
10357 */
10358 void show(int dw, int dh) {
10359 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10360 dw + "x" + dh + ")");
10361 mDimShown = true;
10362 try {
10363 mDimSurface.setPosition(0, 0);
10364 mDimSurface.setSize(dw, dh);
10365 mDimSurface.show();
10366 } catch (RuntimeException e) {
10367 Log.w(TAG, "Failure showing dim surface", e);
10368 }
10369 }
10370
10371 /**
10372 * Set's the dim surface's layer and update dim parameters that will be used in
10373 * {@link updateSurface} after all windows are examined.
10374 */
10375 void updateParameters(WindowState w, long currentTime) {
10376 mDimSurface.setLayer(w.mAnimLayer-1);
10377
10378 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010379 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10380 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010381 if (mDimTargetAlpha != target) {
10382 // If the desired dim level has changed, then
10383 // start an animation to it.
10384 mLastDimAnimTime = currentTime;
10385 long duration = (w.mAnimating && w.mAnimation != null)
10386 ? w.mAnimation.computeDurationHint()
10387 : DEFAULT_DIM_DURATION;
10388 if (target > mDimTargetAlpha) {
10389 // This is happening behind the activity UI,
10390 // so we can make it run a little longer to
10391 // give a stronger impression without disrupting
10392 // the user.
10393 duration *= DIM_DURATION_MULTIPLIER;
10394 }
10395 if (duration < 1) {
10396 // Don't divide by zero
10397 duration = 1;
10398 }
10399 mDimTargetAlpha = target;
10400 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10401 }
10402 }
10403
10404 /**
10405 * Updating the surface's alpha. Returns true if the animation continues, or returns
10406 * false when the animation is finished and the dim surface is hidden.
10407 */
10408 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10409 if (!dimming) {
10410 if (mDimTargetAlpha != 0) {
10411 mLastDimAnimTime = currentTime;
10412 mDimTargetAlpha = 0;
10413 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10414 }
10415 }
10416
10417 boolean animating = false;
10418 if (mLastDimAnimTime != 0) {
10419 mDimCurrentAlpha += mDimDeltaPerMs
10420 * (currentTime-mLastDimAnimTime);
10421 boolean more = true;
10422 if (displayFrozen) {
10423 // If the display is frozen, there is no reason to animate.
10424 more = false;
10425 } else if (mDimDeltaPerMs > 0) {
10426 if (mDimCurrentAlpha > mDimTargetAlpha) {
10427 more = false;
10428 }
10429 } else if (mDimDeltaPerMs < 0) {
10430 if (mDimCurrentAlpha < mDimTargetAlpha) {
10431 more = false;
10432 }
10433 } else {
10434 more = false;
10435 }
10436
10437 // Do we need to continue animating?
10438 if (more) {
10439 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10440 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10441 mLastDimAnimTime = currentTime;
10442 mDimSurface.setAlpha(mDimCurrentAlpha);
10443 animating = true;
10444 } else {
10445 mDimCurrentAlpha = mDimTargetAlpha;
10446 mLastDimAnimTime = 0;
10447 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10448 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10449 mDimSurface.setAlpha(mDimCurrentAlpha);
10450 if (!dimming) {
10451 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10452 + ": HIDE");
10453 try {
10454 mDimSurface.hide();
10455 } catch (RuntimeException e) {
10456 Log.w(TAG, "Illegal argument exception hiding dim surface");
10457 }
10458 mDimShown = false;
10459 }
10460 }
10461 }
10462 return animating;
10463 }
10464
10465 public void printTo(PrintWriter pw) {
10466 pw.print(" mDimShown="); pw.print(mDimShown);
10467 pw.print(" current="); pw.print(mDimCurrentAlpha);
10468 pw.print(" target="); pw.print(mDimTargetAlpha);
10469 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10470 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10471 }
10472 }
10473
10474 /**
10475 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10476 * This is used for opening/closing transition for apps in compatible mode.
10477 */
10478 private static class FadeInOutAnimation extends Animation {
10479 int mWidth;
10480 boolean mFadeIn;
10481
10482 public FadeInOutAnimation(boolean fadeIn) {
10483 setInterpolator(new AccelerateInterpolator());
10484 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10485 mFadeIn = fadeIn;
10486 }
10487
10488 @Override
10489 protected void applyTransformation(float interpolatedTime, Transformation t) {
10490 float x = interpolatedTime;
10491 if (!mFadeIn) {
10492 x = 1.0f - x; // reverse the interpolation for fade out
10493 }
10494 if (x < 0.5) {
10495 // move the window out of the screen.
10496 t.getMatrix().setTranslate(mWidth, 0);
10497 } else {
10498 t.getMatrix().setTranslate(0, 0);// show
10499 t.setAlpha((x - 0.5f) * 2);
10500 }
10501 }
10502
10503 @Override
10504 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10505 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10506 mWidth = width;
10507 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010508
10509 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010510 public int getZAdjustment() {
10511 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010512 }
10513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514}