blob: 8e85a6a4b14e4688700bf876f5bdb35520caddbe [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
446 * Whether the UI is currently running in touch mode (not showing
447 * navigational focus because the user is directly pressing the screen).
448 */
449 boolean mInTouchMode = false;
450
451 private ViewServer mViewServer;
452
453 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700454
Dianne Hackbornc485a602009-03-24 22:39:49 -0700455 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700456 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700457
458 // The frame use to limit the size of the app running in compatibility mode.
459 Rect mCompatibleScreenFrame = new Rect();
460 // The surface used to fill the outer rim of the app running in compatibility mode.
461 Surface mBackgroundFillerSurface = null;
462 boolean mBackgroundFillerShown = false;
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 public static WindowManagerService main(Context context,
465 PowerManagerService pm, boolean haveInputMethods) {
466 WMThread thr = new WMThread(context, pm, haveInputMethods);
467 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 synchronized (thr) {
470 while (thr.mService == null) {
471 try {
472 thr.wait();
473 } catch (InterruptedException e) {
474 }
475 }
476 }
Romain Guy06882f82009-06-10 13:36:04 -0700477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 return thr.mService;
479 }
Romain Guy06882f82009-06-10 13:36:04 -0700480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 static class WMThread extends Thread {
482 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 private final Context mContext;
485 private final PowerManagerService mPM;
486 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 public WMThread(Context context, PowerManagerService pm,
489 boolean haveInputMethods) {
490 super("WindowManager");
491 mContext = context;
492 mPM = pm;
493 mHaveInputMethods = haveInputMethods;
494 }
Romain Guy06882f82009-06-10 13:36:04 -0700495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 public void run() {
497 Looper.prepare();
498 WindowManagerService s = new WindowManagerService(mContext, mPM,
499 mHaveInputMethods);
500 android.os.Process.setThreadPriority(
501 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 synchronized (this) {
504 mService = s;
505 notifyAll();
506 }
Romain Guy06882f82009-06-10 13:36:04 -0700507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 Looper.loop();
509 }
510 }
511
512 static class PolicyThread extends Thread {
513 private final WindowManagerPolicy mPolicy;
514 private final WindowManagerService mService;
515 private final Context mContext;
516 private final PowerManagerService mPM;
517 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 public PolicyThread(WindowManagerPolicy policy,
520 WindowManagerService service, Context context,
521 PowerManagerService pm) {
522 super("WindowManagerPolicy");
523 mPolicy = policy;
524 mService = service;
525 mContext = context;
526 mPM = pm;
527 }
Romain Guy06882f82009-06-10 13:36:04 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 public void run() {
530 Looper.prepare();
531 //Looper.myLooper().setMessageLogging(new LogPrinter(
532 // Log.VERBOSE, "WindowManagerPolicy"));
533 android.os.Process.setThreadPriority(
534 android.os.Process.THREAD_PRIORITY_FOREGROUND);
535 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 synchronized (this) {
538 mRunning = true;
539 notifyAll();
540 }
Romain Guy06882f82009-06-10 13:36:04 -0700541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 Looper.loop();
543 }
544 }
545
546 private WindowManagerService(Context context, PowerManagerService pm,
547 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700548 if (MEASURE_LATENCY) {
549 lt = new LatencyTimer(100, 1000);
550 }
551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 mContext = context;
553 mHaveInputMethods = haveInputMethods;
554 mLimitedAlphaCompositing = context.getResources().getBoolean(
555 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 mPowerManager = pm;
558 mPowerManager.setPolicy(mPolicy);
559 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
560 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
561 "SCREEN_FROZEN");
562 mScreenFrozenLock.setReferenceCounted(false);
563
564 mActivityManager = ActivityManagerNative.getDefault();
565 mBatteryStats = BatteryStatsService.getService();
566
567 // Get persisted window scale setting
568 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
569 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
570 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
571 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700572
Michael Chan9f028e62009-08-04 17:37:46 -0700573 int max_events_per_sec = 35;
574 try {
575 max_events_per_sec = Integer.parseInt(SystemProperties
576 .get("windowsmgr.max_events_per_sec"));
577 if (max_events_per_sec < 1) {
578 max_events_per_sec = 35;
579 }
580 } catch (NumberFormatException e) {
581 }
582 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 mQueue = new KeyQ();
585
586 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
589 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 synchronized (thr) {
592 while (!thr.mRunning) {
593 try {
594 thr.wait();
595 } catch (InterruptedException e) {
596 }
597 }
598 }
Romain Guy06882f82009-06-10 13:36:04 -0700599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 // Add ourself to the Watchdog monitors.
603 Watchdog.getInstance().addMonitor(this);
604 }
605
606 @Override
607 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
608 throws RemoteException {
609 try {
610 return super.onTransact(code, data, reply, flags);
611 } catch (RuntimeException e) {
612 // The window manager only throws security exceptions, so let's
613 // log all others.
614 if (!(e instanceof SecurityException)) {
615 Log.e(TAG, "Window Manager Crash", e);
616 }
617 throw e;
618 }
619 }
620
621 private void placeWindowAfter(Object pos, WindowState window) {
622 final int i = mWindows.indexOf(pos);
623 if (localLOGV || DEBUG_FOCUS) Log.v(
624 TAG, "Adding window " + window + " at "
625 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
626 mWindows.add(i+1, window);
627 }
628
629 private void placeWindowBefore(Object pos, WindowState window) {
630 final int i = mWindows.indexOf(pos);
631 if (localLOGV || DEBUG_FOCUS) Log.v(
632 TAG, "Adding window " + window + " at "
633 + i + " of " + mWindows.size() + " (before " + pos + ")");
634 mWindows.add(i, window);
635 }
636
637 //This method finds out the index of a window that has the same app token as
638 //win. used for z ordering the windows in mWindows
639 private int findIdxBasedOnAppTokens(WindowState win) {
640 //use a local variable to cache mWindows
641 ArrayList localmWindows = mWindows;
642 int jmax = localmWindows.size();
643 if(jmax == 0) {
644 return -1;
645 }
646 for(int j = (jmax-1); j >= 0; j--) {
647 WindowState wentry = (WindowState)localmWindows.get(j);
648 if(wentry.mAppToken == win.mAppToken) {
649 return j;
650 }
651 }
652 return -1;
653 }
Romain Guy06882f82009-06-10 13:36:04 -0700654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
656 final IWindow client = win.mClient;
657 final WindowToken token = win.mToken;
658 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 final int N = localmWindows.size();
661 final WindowState attached = win.mAttachedWindow;
662 int i;
663 if (attached == null) {
664 int tokenWindowsPos = token.windows.size();
665 if (token.appWindowToken != null) {
666 int index = tokenWindowsPos-1;
667 if (index >= 0) {
668 // If this application has existing windows, we
669 // simply place the new window on top of them... but
670 // keep the starting window on top.
671 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
672 // Base windows go behind everything else.
673 placeWindowBefore(token.windows.get(0), win);
674 tokenWindowsPos = 0;
675 } else {
676 AppWindowToken atoken = win.mAppToken;
677 if (atoken != null &&
678 token.windows.get(index) == atoken.startingWindow) {
679 placeWindowBefore(token.windows.get(index), win);
680 tokenWindowsPos--;
681 } else {
682 int newIdx = findIdxBasedOnAppTokens(win);
683 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700684 //there is a window above this one associated with the same
685 //apptoken note that the window could be a floating window
686 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 //windows associated with this token.
688 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691 }
692 } else {
693 if (localLOGV) Log.v(
694 TAG, "Figuring out where to add app window "
695 + client.asBinder() + " (token=" + token + ")");
696 // Figure out where the window should go, based on the
697 // order of applications.
698 final int NA = mAppTokens.size();
699 Object pos = null;
700 for (i=NA-1; i>=0; i--) {
701 AppWindowToken t = mAppTokens.get(i);
702 if (t == token) {
703 i--;
704 break;
705 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700706
707 // We haven't reached the token yet; if this token
708 // is not going to the bottom and has windows, we can
709 // use it as an anchor for when we do reach the token.
710 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 pos = t.windows.get(0);
712 }
713 }
714 // We now know the index into the apps. If we found
715 // an app window above, that gives us the position; else
716 // we need to look some more.
717 if (pos != null) {
718 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700719 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 mTokenMap.get(((WindowState)pos).mClient.asBinder());
721 if (atoken != null) {
722 final int NC = atoken.windows.size();
723 if (NC > 0) {
724 WindowState bottom = atoken.windows.get(0);
725 if (bottom.mSubLayer < 0) {
726 pos = bottom;
727 }
728 }
729 }
730 placeWindowBefore(pos, win);
731 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700732 // Continue looking down until we find the first
733 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 while (i >= 0) {
735 AppWindowToken t = mAppTokens.get(i);
736 final int NW = t.windows.size();
737 if (NW > 0) {
738 pos = t.windows.get(NW-1);
739 break;
740 }
741 i--;
742 }
743 if (pos != null) {
744 // Move in front of any windows attached to this
745 // one.
746 WindowToken atoken =
747 mTokenMap.get(((WindowState)pos).mClient.asBinder());
748 if (atoken != null) {
749 final int NC = atoken.windows.size();
750 if (NC > 0) {
751 WindowState top = atoken.windows.get(NC-1);
752 if (top.mSubLayer >= 0) {
753 pos = top;
754 }
755 }
756 }
757 placeWindowAfter(pos, win);
758 } else {
759 // Just search for the start of this layer.
760 final int myLayer = win.mBaseLayer;
761 for (i=0; i<N; i++) {
762 WindowState w = (WindowState)localmWindows.get(i);
763 if (w.mBaseLayer > myLayer) {
764 break;
765 }
766 }
767 if (localLOGV || DEBUG_FOCUS) Log.v(
768 TAG, "Adding window " + win + " at "
769 + i + " of " + N);
770 localmWindows.add(i, win);
771 }
772 }
773 }
774 } else {
775 // Figure out where window should go, based on layer.
776 final int myLayer = win.mBaseLayer;
777 for (i=N-1; i>=0; i--) {
778 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
779 i++;
780 break;
781 }
782 }
783 if (i < 0) i = 0;
784 if (localLOGV || DEBUG_FOCUS) Log.v(
785 TAG, "Adding window " + win + " at "
786 + i + " of " + N);
787 localmWindows.add(i, win);
788 }
789 if (addToToken) {
790 token.windows.add(tokenWindowsPos, win);
791 }
792
793 } else {
794 // Figure out this window's ordering relative to the window
795 // it is attached to.
796 final int NA = token.windows.size();
797 final int sublayer = win.mSubLayer;
798 int largestSublayer = Integer.MIN_VALUE;
799 WindowState windowWithLargestSublayer = null;
800 for (i=0; i<NA; i++) {
801 WindowState w = token.windows.get(i);
802 final int wSublayer = w.mSubLayer;
803 if (wSublayer >= largestSublayer) {
804 largestSublayer = wSublayer;
805 windowWithLargestSublayer = w;
806 }
807 if (sublayer < 0) {
808 // For negative sublayers, we go below all windows
809 // in the same sublayer.
810 if (wSublayer >= sublayer) {
811 if (addToToken) {
812 token.windows.add(i, win);
813 }
814 placeWindowBefore(
815 wSublayer >= 0 ? attached : w, win);
816 break;
817 }
818 } else {
819 // For positive sublayers, we go above all windows
820 // in the same sublayer.
821 if (wSublayer > sublayer) {
822 if (addToToken) {
823 token.windows.add(i, win);
824 }
825 placeWindowBefore(w, win);
826 break;
827 }
828 }
829 }
830 if (i >= NA) {
831 if (addToToken) {
832 token.windows.add(win);
833 }
834 if (sublayer < 0) {
835 placeWindowBefore(attached, win);
836 } else {
837 placeWindowAfter(largestSublayer >= 0
838 ? windowWithLargestSublayer
839 : attached,
840 win);
841 }
842 }
843 }
Romain Guy06882f82009-06-10 13:36:04 -0700844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 if (win.mAppToken != null && addToToken) {
846 win.mAppToken.allAppWindows.add(win);
847 }
848 }
Romain Guy06882f82009-06-10 13:36:04 -0700849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 static boolean canBeImeTarget(WindowState w) {
851 final int fl = w.mAttrs.flags
852 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
853 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
854 return w.isVisibleOrAdding();
855 }
856 return false;
857 }
Romain Guy06882f82009-06-10 13:36:04 -0700858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
860 final ArrayList localmWindows = mWindows;
861 final int N = localmWindows.size();
862 WindowState w = null;
863 int i = N;
864 while (i > 0) {
865 i--;
866 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
869 // + Integer.toHexString(w.mAttrs.flags));
870 if (canBeImeTarget(w)) {
871 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 // Yet more tricksyness! If this window is a "starting"
874 // window, we do actually want to be on top of it, but
875 // it is not -really- where input will go. So if the caller
876 // is not actually looking to move the IME, look down below
877 // for a real window to target...
878 if (!willMove
879 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
880 && i > 0) {
881 WindowState wb = (WindowState)localmWindows.get(i-1);
882 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
883 i--;
884 w = wb;
885 }
886 }
887 break;
888 }
889 }
Romain Guy06882f82009-06-10 13:36:04 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
894 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 if (willMove && w != null) {
897 final WindowState curTarget = mInputMethodTarget;
898 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 // Now some fun for dealing with window animations that
901 // modify the Z order. We need to look at all windows below
902 // the current target that are in this app, finding the highest
903 // visible one in layering.
904 AppWindowToken token = curTarget.mAppToken;
905 WindowState highestTarget = null;
906 int highestPos = 0;
907 if (token.animating || token.animation != null) {
908 int pos = 0;
909 pos = localmWindows.indexOf(curTarget);
910 while (pos >= 0) {
911 WindowState win = (WindowState)localmWindows.get(pos);
912 if (win.mAppToken != token) {
913 break;
914 }
915 if (!win.mRemoved) {
916 if (highestTarget == null || win.mAnimLayer >
917 highestTarget.mAnimLayer) {
918 highestTarget = win;
919 highestPos = pos;
920 }
921 }
922 pos--;
923 }
924 }
Romain Guy06882f82009-06-10 13:36:04 -0700925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700927 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 + mNextAppTransition + " " + highestTarget
929 + " animating=" + highestTarget.isAnimating()
930 + " layer=" + highestTarget.mAnimLayer
931 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
934 // If we are currently setting up for an animation,
935 // hold everything until we can find out what will happen.
936 mInputMethodTargetWaitingAnim = true;
937 mInputMethodTarget = highestTarget;
938 return highestPos + 1;
939 } else if (highestTarget.isAnimating() &&
940 highestTarget.mAnimLayer > w.mAnimLayer) {
941 // If the window we are currently targeting is involved
942 // with an animation, and it is on top of the next target
943 // we will be over, then hold off on moving until
944 // that is done.
945 mInputMethodTarget = highestTarget;
946 return highestPos + 1;
947 }
948 }
949 }
950 }
Romain Guy06882f82009-06-10 13:36:04 -0700951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 //Log.i(TAG, "Placing input method @" + (i+1));
953 if (w != null) {
954 if (willMove) {
955 RuntimeException e = new RuntimeException();
956 e.fillInStackTrace();
957 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
958 + mInputMethodTarget + " to " + w, e);
959 mInputMethodTarget = w;
960 if (w.mAppToken != null) {
961 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
962 } else {
963 setInputMethodAnimLayerAdjustment(0);
964 }
965 }
966 return i+1;
967 }
968 if (willMove) {
969 RuntimeException e = new RuntimeException();
970 e.fillInStackTrace();
971 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
972 + mInputMethodTarget + " to null", e);
973 mInputMethodTarget = null;
974 setInputMethodAnimLayerAdjustment(0);
975 }
976 return -1;
977 }
Romain Guy06882f82009-06-10 13:36:04 -0700978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 void addInputMethodWindowToListLocked(WindowState win) {
980 int pos = findDesiredInputMethodWindowIndexLocked(true);
981 if (pos >= 0) {
982 win.mTargetAppToken = mInputMethodTarget.mAppToken;
983 mWindows.add(pos, win);
984 moveInputMethodDialogsLocked(pos+1);
985 return;
986 }
987 win.mTargetAppToken = null;
988 addWindowToListInOrderLocked(win, true);
989 moveInputMethodDialogsLocked(pos);
990 }
Romain Guy06882f82009-06-10 13:36:04 -0700991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 void setInputMethodAnimLayerAdjustment(int adj) {
993 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
994 mInputMethodAnimLayerAdjustment = adj;
995 WindowState imw = mInputMethodWindow;
996 if (imw != null) {
997 imw.mAnimLayer = imw.mLayer + adj;
998 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
999 + " anim layer: " + imw.mAnimLayer);
1000 int wi = imw.mChildWindows.size();
1001 while (wi > 0) {
1002 wi--;
1003 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1004 cw.mAnimLayer = cw.mLayer + adj;
1005 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1006 + " anim layer: " + cw.mAnimLayer);
1007 }
1008 }
1009 int di = mInputMethodDialogs.size();
1010 while (di > 0) {
1011 di --;
1012 imw = mInputMethodDialogs.get(di);
1013 imw.mAnimLayer = imw.mLayer + adj;
1014 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1015 + " anim layer: " + imw.mAnimLayer);
1016 }
1017 }
Romain Guy06882f82009-06-10 13:36:04 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1020 int wpos = mWindows.indexOf(win);
1021 if (wpos >= 0) {
1022 if (wpos < interestingPos) interestingPos--;
1023 mWindows.remove(wpos);
1024 int NC = win.mChildWindows.size();
1025 while (NC > 0) {
1026 NC--;
1027 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1028 int cpos = mWindows.indexOf(cw);
1029 if (cpos >= 0) {
1030 if (cpos < interestingPos) interestingPos--;
1031 mWindows.remove(cpos);
1032 }
1033 }
1034 }
1035 return interestingPos;
1036 }
Romain Guy06882f82009-06-10 13:36:04 -07001037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 private void reAddWindowToListInOrderLocked(WindowState win) {
1039 addWindowToListInOrderLocked(win, false);
1040 // This is a hack to get all of the child windows added as well
1041 // at the right position. Child windows should be rare and
1042 // this case should be rare, so it shouldn't be that big a deal.
1043 int wpos = mWindows.indexOf(win);
1044 if (wpos >= 0) {
1045 mWindows.remove(wpos);
1046 reAddWindowLocked(wpos, win);
1047 }
1048 }
Romain Guy06882f82009-06-10 13:36:04 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 void logWindowList(String prefix) {
1051 int N = mWindows.size();
1052 while (N > 0) {
1053 N--;
1054 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1055 }
1056 }
Romain Guy06882f82009-06-10 13:36:04 -07001057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 void moveInputMethodDialogsLocked(int pos) {
1059 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 final int N = dialogs.size();
1062 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1063 for (int i=0; i<N; i++) {
1064 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1065 }
1066 if (DEBUG_INPUT_METHOD) {
1067 Log.v(TAG, "Window list w/pos=" + pos);
1068 logWindowList(" ");
1069 }
Romain Guy06882f82009-06-10 13:36:04 -07001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 if (pos >= 0) {
1072 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1073 if (pos < mWindows.size()) {
1074 WindowState wp = (WindowState)mWindows.get(pos);
1075 if (wp == mInputMethodWindow) {
1076 pos++;
1077 }
1078 }
1079 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1080 for (int i=0; i<N; i++) {
1081 WindowState win = dialogs.get(i);
1082 win.mTargetAppToken = targetAppToken;
1083 pos = reAddWindowLocked(pos, win);
1084 }
1085 if (DEBUG_INPUT_METHOD) {
1086 Log.v(TAG, "Final window list:");
1087 logWindowList(" ");
1088 }
1089 return;
1090 }
1091 for (int i=0; i<N; i++) {
1092 WindowState win = dialogs.get(i);
1093 win.mTargetAppToken = null;
1094 reAddWindowToListInOrderLocked(win);
1095 if (DEBUG_INPUT_METHOD) {
1096 Log.v(TAG, "No IM target, final list:");
1097 logWindowList(" ");
1098 }
1099 }
1100 }
Romain Guy06882f82009-06-10 13:36:04 -07001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1103 final WindowState imWin = mInputMethodWindow;
1104 final int DN = mInputMethodDialogs.size();
1105 if (imWin == null && DN == 0) {
1106 return false;
1107 }
Romain Guy06882f82009-06-10 13:36:04 -07001108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1110 if (imPos >= 0) {
1111 // In this case, the input method windows are to be placed
1112 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 // First check to see if the input method windows are already
1115 // located here, and contiguous.
1116 final int N = mWindows.size();
1117 WindowState firstImWin = imPos < N
1118 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 // Figure out the actual input method window that should be
1121 // at the bottom of their stack.
1122 WindowState baseImWin = imWin != null
1123 ? imWin : mInputMethodDialogs.get(0);
1124 if (baseImWin.mChildWindows.size() > 0) {
1125 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1126 if (cw.mSubLayer < 0) baseImWin = cw;
1127 }
Romain Guy06882f82009-06-10 13:36:04 -07001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 if (firstImWin == baseImWin) {
1130 // The windows haven't moved... but are they still contiguous?
1131 // First find the top IM window.
1132 int pos = imPos+1;
1133 while (pos < N) {
1134 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1135 break;
1136 }
1137 pos++;
1138 }
1139 pos++;
1140 // Now there should be no more input method windows above.
1141 while (pos < N) {
1142 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1143 break;
1144 }
1145 pos++;
1146 }
1147 if (pos >= N) {
1148 // All is good!
1149 return false;
1150 }
1151 }
Romain Guy06882f82009-06-10 13:36:04 -07001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 if (imWin != null) {
1154 if (DEBUG_INPUT_METHOD) {
1155 Log.v(TAG, "Moving IM from " + imPos);
1156 logWindowList(" ");
1157 }
1158 imPos = tmpRemoveWindowLocked(imPos, imWin);
1159 if (DEBUG_INPUT_METHOD) {
1160 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1161 logWindowList(" ");
1162 }
1163 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1164 reAddWindowLocked(imPos, imWin);
1165 if (DEBUG_INPUT_METHOD) {
1166 Log.v(TAG, "List after moving IM to " + imPos + ":");
1167 logWindowList(" ");
1168 }
1169 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1170 } else {
1171 moveInputMethodDialogsLocked(imPos);
1172 }
Romain Guy06882f82009-06-10 13:36:04 -07001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 } else {
1175 // In this case, the input method windows go in a fixed layer,
1176 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 if (imWin != null) {
1179 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1180 tmpRemoveWindowLocked(0, imWin);
1181 imWin.mTargetAppToken = null;
1182 reAddWindowToListInOrderLocked(imWin);
1183 if (DEBUG_INPUT_METHOD) {
1184 Log.v(TAG, "List with no IM target:");
1185 logWindowList(" ");
1186 }
1187 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1188 } else {
1189 moveInputMethodDialogsLocked(-1);;
1190 }
Romain Guy06882f82009-06-10 13:36:04 -07001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
Romain Guy06882f82009-06-10 13:36:04 -07001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 if (needAssignLayers) {
1195 assignLayersLocked();
1196 }
Romain Guy06882f82009-06-10 13:36:04 -07001197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 return true;
1199 }
Romain Guy06882f82009-06-10 13:36:04 -07001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 void adjustInputMethodDialogsLocked() {
1202 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1203 }
Romain Guy06882f82009-06-10 13:36:04 -07001204
Dianne Hackborn25994b42009-09-04 14:21:19 -07001205 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1206 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1207 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1208 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1209 ? wallpaperTarget.mAppToken.animation : null)
1210 + " upper=" + mUpperWallpaperTarget
1211 + " lower=" + mLowerWallpaperTarget);
1212 return (wallpaperTarget != null
1213 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1214 && wallpaperTarget.mAppToken.animation != null)))
1215 || mUpperWallpaperTarget != null
1216 || mLowerWallpaperTarget != null;
1217 }
1218
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001219 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1220 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1221
1222 int adjustWallpaperWindowsLocked() {
1223 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001224
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001225 final int dw = mDisplay.getWidth();
1226 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001227
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001228 // First find top-most window that has asked to be on top of the
1229 // wallpaper; all wallpapers go behind it.
1230 final ArrayList localmWindows = mWindows;
1231 int N = localmWindows.size();
1232 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001233 WindowState foundW = null;
1234 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001235 int i = N;
1236 while (i > 0) {
1237 i--;
1238 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001239 if (w.mAppToken != null) {
1240 // If this window's app token is hidden and not animating,
1241 // it is of no interest to us.
1242 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1243 if (DEBUG_WALLPAPER) Log.v(TAG,
1244 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001245 continue;
1246 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001247 // If this window's app token is ot fullscreen, also irrelevant.
1248 if (!w.mAppToken.appFullscreen) {
1249 if (DEBUG_WALLPAPER) Log.v(TAG,
1250 "Skipping non-fullscreen token: " + w);
1251 continue;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001252 }
1253 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001254 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1255 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1256 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001257 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001258 && (mWallpaperTarget == w
1259 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001260 if (DEBUG_WALLPAPER) Log.v(TAG,
1261 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001262 foundW = w;
1263 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001264 if (w == mWallpaperTarget && w.mAppToken != null
1265 && w.mAppToken.animation != null) {
1266 // The current wallpaper target is animating, so we'll
1267 // look behind it for another possible target and figure
1268 // out what is going on below.
1269 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1270 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001271 continue;
1272 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001273 break;
1274 }
1275 }
1276
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001277 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1278 // If we are currently waiting for an app transition, and either
1279 // the current target or the next target are involved with it,
1280 // then hold off on doing anything with the wallpaper.
1281 // Note that we are checking here for just whether the target
1282 // is part of an app token... which is potentially overly aggressive
1283 // (the app token may not be involved in the transition), but good
1284 // enough (we'll just wait until whatever transition is pending
1285 // executes).
1286 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001287 if (DEBUG_WALLPAPER) Log.v(TAG,
1288 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001289 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001290 }
1291 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001292 if (DEBUG_WALLPAPER) Log.v(TAG,
1293 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001294 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001295 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001296 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001297
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001298 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001299 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001300 Log.v(TAG, "New wallpaper target: " + foundW
1301 + " oldTarget: " + mWallpaperTarget);
1302 }
1303
1304 mLowerWallpaperTarget = null;
1305 mUpperWallpaperTarget = null;
1306
1307 WindowState oldW = mWallpaperTarget;
1308 mWallpaperTarget = foundW;
1309
1310 // Now what is happening... if the current and new targets are
1311 // animating, then we are in our super special mode!
1312 if (foundW != null && foundW.mAppToken != null && oldW != null
1313 && oldW.mAppToken != null) {
1314 if (DEBUG_WALLPAPER) {
1315 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1316 + " old animation: " + oldW.mAppToken.animation);
1317 }
1318 if (foundW.mAppToken.animation != null
1319 && oldW.mAppToken.animation != null) {
1320 int oldI = localmWindows.indexOf(oldW);
1321 if (DEBUG_WALLPAPER) {
1322 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1323 }
1324 if (oldI >= 0) {
1325 if (DEBUG_WALLPAPER) {
1326 Log.v(TAG, "Animating wallpapers: old#" + oldI
1327 + "=" + oldW + "; new#" + foundI
1328 + "=" + foundW);
1329 }
1330
1331 // Set the new target correctly.
1332 if (foundW.mAppToken.hiddenRequested) {
1333 if (DEBUG_WALLPAPER) {
1334 Log.v(TAG, "Old wallpaper still the target.");
1335 }
1336 mWallpaperTarget = oldW;
1337 }
1338
1339 // Now set the upper and lower wallpaper targets
1340 // correctly, and make sure that we are positioning
1341 // the wallpaper below the lower.
1342 if (foundI > oldI) {
1343 // The new target is on top of the old one.
1344 if (DEBUG_WALLPAPER) {
1345 Log.v(TAG, "Found target above old target.");
1346 }
1347 mUpperWallpaperTarget = foundW;
1348 mLowerWallpaperTarget = oldW;
1349 foundW = oldW;
1350 foundI = oldI;
1351 } else {
1352 // The new target is below the old one.
1353 if (DEBUG_WALLPAPER) {
1354 Log.v(TAG, "Found target below old target.");
1355 }
1356 mUpperWallpaperTarget = oldW;
1357 mLowerWallpaperTarget = foundW;
1358 }
1359 }
1360 }
1361 }
1362
1363 } else {
1364 // Is it time to stop animating?
1365 if (mLowerWallpaperTarget == null
1366 || mLowerWallpaperTarget.mAppToken.animation == null
1367 || mUpperWallpaperTarget == null
1368 || mUpperWallpaperTarget.mAppToken.animation == null) {
1369 if (DEBUG_WALLPAPER) {
1370 Log.v(TAG, "No longer animating wallpaper targets!");
1371 }
1372 mLowerWallpaperTarget = null;
1373 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001374 }
1375 }
1376
1377 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001378 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001379 // The window is visible to the compositor... but is it visible
1380 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001381 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001382 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001383
1384 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001385 // its layer adjustment. Only do this if we are not transfering
1386 // between two wallpaper targets.
1387 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001388 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001389 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001390
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001391 // Now w is the window we are supposed to be behind... but we
1392 // need to be sure to also be behind any of its attached windows,
1393 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001394 while (foundI > 0) {
1395 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1396 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001397 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001398 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001399 // This window is not related to the previous one in any
1400 // interesting way, so stop here.
1401 break;
1402 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001403 foundW = wb;
1404 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001405 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001406 } else {
1407 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001408 }
1409
1410 // Okay i is the position immediately above the wallpaper. Look at
1411 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001412 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001413
Dianne Hackborn284ac932009-08-28 10:34:25 -07001414 if (visible) {
1415 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1416 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1417 }
1418
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001419 // Start stepping backwards from here, ensuring that our wallpaper windows
1420 // are correctly placed.
1421 int curTokenIndex = mWallpaperTokens.size();
1422 while (curTokenIndex > 0) {
1423 curTokenIndex--;
1424 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001425 if (token.hidden == visible) {
1426 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1427 token.hidden = !visible;
1428 // Need to do a layout to ensure the wallpaper now has the
1429 // correct size.
1430 mLayoutNeeded = true;
1431 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001432
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001433 int curWallpaperIndex = token.windows.size();
1434 while (curWallpaperIndex > 0) {
1435 curWallpaperIndex--;
1436 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001437
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001438 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001439 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001440 }
1441
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001442 // First, make sure the client has the current visibility
1443 // state.
1444 if (wallpaper.mWallpaperVisible != visible) {
1445 wallpaper.mWallpaperVisible = visible;
1446 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001447 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001448 "Setting visibility of wallpaper " + wallpaper
1449 + ": " + visible);
1450 wallpaper.mClient.dispatchAppVisibility(visible);
1451 } catch (RemoteException e) {
1452 }
1453 }
1454
1455 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001456 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1457 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001458
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001459 // First, if this window is at the current index, then all
1460 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001461 if (wallpaper == foundW) {
1462 foundI--;
1463 foundW = foundI > 0
1464 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001465 continue;
1466 }
1467
1468 // The window didn't match... the current wallpaper window,
1469 // wherever it is, is in the wrong place, so make sure it is
1470 // not in the list.
1471 int oldIndex = localmWindows.indexOf(wallpaper);
1472 if (oldIndex >= 0) {
1473 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001474 if (oldIndex < foundI) {
1475 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001476 }
1477 }
1478
1479 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001480 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001481 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001482
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001483 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001484 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001485 }
1486 }
1487
1488 return changed;
1489 }
1490
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001491 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001492 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1493 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001494 mWallpaperAnimLayerAdjustment = adj;
1495 int curTokenIndex = mWallpaperTokens.size();
1496 while (curTokenIndex > 0) {
1497 curTokenIndex--;
1498 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1499 int curWallpaperIndex = token.windows.size();
1500 while (curWallpaperIndex > 0) {
1501 curWallpaperIndex--;
1502 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1503 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001504 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1505 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001506 }
1507 }
1508 }
1509
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001510 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1511 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001512 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001513 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001514 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001515 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001516 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001517 changed = wallpaperWin.mXOffset != offset;
1518 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001519 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1520 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001521 wallpaperWin.mXOffset = offset;
1522 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001523 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1524 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001525 rawChanged = true;
1526 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001527 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001528
Dianne Hackborn284ac932009-08-28 10:34:25 -07001529 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001530 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001531 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001532 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001533 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1534 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001535 changed = true;
1536 wallpaperWin.mYOffset = offset;
1537 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001538 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1539 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001540 rawChanged = true;
1541 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001542 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001543
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001544 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001545 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001546 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1547 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1548 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001549 if (sync) {
1550 synchronized (mWaitingOnWallpaperLock) {
1551 mWaitingOnWallpaper = wallpaperWin;
1552 }
1553 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001554 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001555 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1556 if (sync) {
1557 synchronized (mWaitingOnWallpaperLock) {
1558 if (mWaitingOnWallpaper != null) {
1559 long start = SystemClock.uptimeMillis();
1560 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1561 < start) {
1562 try {
1563 if (DEBUG_WALLPAPER) Log.v(TAG,
1564 "Waiting for offset complete...");
1565 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1566 } catch (InterruptedException e) {
1567 }
1568 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1569 if ((start+WALLPAPER_TIMEOUT)
1570 < SystemClock.uptimeMillis()) {
1571 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1572 + wallpaperWin);
1573 mLastWallpaperTimeoutTime = start;
1574 }
1575 }
1576 mWaitingOnWallpaper = null;
1577 }
1578 }
1579 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001580 } catch (RemoteException e) {
1581 }
1582 }
1583
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001584 return changed;
1585 }
1586
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001587 void wallpaperOffsetsComplete(IBinder window) {
1588 synchronized (mWaitingOnWallpaperLock) {
1589 if (mWaitingOnWallpaper != null &&
1590 mWaitingOnWallpaper.mClient.asBinder() == window) {
1591 mWaitingOnWallpaper = null;
1592 mWaitingOnWallpaperLock.notifyAll();
1593 }
1594 }
1595 }
1596
1597 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001598 final int dw = mDisplay.getWidth();
1599 final int dh = mDisplay.getHeight();
1600
1601 boolean changed = false;
1602
1603 WindowState target = mWallpaperTarget;
1604 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001605 mLastWallpaperX = target.mWallpaperX;
1606 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001607 int curTokenIndex = mWallpaperTokens.size();
1608 while (curTokenIndex > 0) {
1609 curTokenIndex--;
1610 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1611 int curWallpaperIndex = token.windows.size();
1612 while (curWallpaperIndex > 0) {
1613 curWallpaperIndex--;
1614 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001615 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001616 wallpaper.computeShownFrameLocked();
1617 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001618 // We only want to be synchronous with one wallpaper.
1619 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001620 }
1621 }
1622 }
1623 }
1624
1625 return changed;
1626 }
1627
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001628 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001629 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001630 final int dw = mDisplay.getWidth();
1631 final int dh = mDisplay.getHeight();
1632
1633 int curTokenIndex = mWallpaperTokens.size();
1634 while (curTokenIndex > 0) {
1635 curTokenIndex--;
1636 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001637 if (token.hidden == visible) {
1638 token.hidden = !visible;
1639 // Need to do a layout to ensure the wallpaper now has the
1640 // correct size.
1641 mLayoutNeeded = true;
1642 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001643
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001644 int curWallpaperIndex = token.windows.size();
1645 while (curWallpaperIndex > 0) {
1646 curWallpaperIndex--;
1647 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1648 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001649 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001650 }
1651
1652 if (wallpaper.mWallpaperVisible != visible) {
1653 wallpaper.mWallpaperVisible = visible;
1654 try {
1655 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001656 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001657 + ": " + visible);
1658 wallpaper.mClient.dispatchAppVisibility(visible);
1659 } catch (RemoteException e) {
1660 }
1661 }
1662 }
1663 }
1664 }
1665
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001666 void sendPointerToWallpaperLocked(WindowState srcWin,
1667 MotionEvent pointer, long eventTime) {
1668 int curTokenIndex = mWallpaperTokens.size();
1669 while (curTokenIndex > 0) {
1670 curTokenIndex--;
1671 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1672 int curWallpaperIndex = token.windows.size();
1673 while (curWallpaperIndex > 0) {
1674 curWallpaperIndex--;
1675 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1676 if ((wallpaper.mAttrs.flags &
1677 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1678 continue;
1679 }
1680 try {
1681 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1682 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1683 srcWin.mFrame.top-wallpaper.mFrame.top);
1684 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1685 } catch (RemoteException e) {
1686 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1687 }
1688 }
1689 }
1690 }
1691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 public int addWindow(Session session, IWindow client,
1693 WindowManager.LayoutParams attrs, int viewVisibility,
1694 Rect outContentInsets) {
1695 int res = mPolicy.checkAddPermission(attrs);
1696 if (res != WindowManagerImpl.ADD_OKAY) {
1697 return res;
1698 }
Romain Guy06882f82009-06-10 13:36:04 -07001699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 boolean reportNewConfig = false;
1701 WindowState attachedWindow = null;
1702 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 synchronized(mWindowMap) {
1705 // Instantiating a Display requires talking with the simulator,
1706 // so don't do it until we know the system is mostly up and
1707 // running.
1708 if (mDisplay == null) {
1709 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1710 mDisplay = wm.getDefaultDisplay();
1711 mQueue.setDisplay(mDisplay);
1712 reportNewConfig = true;
1713 }
Romain Guy06882f82009-06-10 13:36:04 -07001714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 if (mWindowMap.containsKey(client.asBinder())) {
1716 Log.w(TAG, "Window " + client + " is already added");
1717 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1718 }
1719
1720 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001721 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 if (attachedWindow == null) {
1723 Log.w(TAG, "Attempted to add window with token that is not a window: "
1724 + attrs.token + ". Aborting.");
1725 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1726 }
1727 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1728 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1729 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1730 + attrs.token + ". Aborting.");
1731 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1732 }
1733 }
1734
1735 boolean addToken = false;
1736 WindowToken token = mTokenMap.get(attrs.token);
1737 if (token == null) {
1738 if (attrs.type >= FIRST_APPLICATION_WINDOW
1739 && attrs.type <= LAST_APPLICATION_WINDOW) {
1740 Log.w(TAG, "Attempted to add application window with unknown token "
1741 + attrs.token + ". Aborting.");
1742 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1743 }
1744 if (attrs.type == TYPE_INPUT_METHOD) {
1745 Log.w(TAG, "Attempted to add input method window with unknown token "
1746 + attrs.token + ". Aborting.");
1747 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1748 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001749 if (attrs.type == TYPE_WALLPAPER) {
1750 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1751 + attrs.token + ". Aborting.");
1752 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 token = new WindowToken(attrs.token, -1, false);
1755 addToken = true;
1756 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1757 && attrs.type <= LAST_APPLICATION_WINDOW) {
1758 AppWindowToken atoken = token.appWindowToken;
1759 if (atoken == null) {
1760 Log.w(TAG, "Attempted to add window with non-application token "
1761 + token + ". Aborting.");
1762 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1763 } else if (atoken.removed) {
1764 Log.w(TAG, "Attempted to add window with exiting application token "
1765 + token + ". Aborting.");
1766 return WindowManagerImpl.ADD_APP_EXITING;
1767 }
1768 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1769 // No need for this guy!
1770 if (localLOGV) Log.v(
1771 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1772 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1773 }
1774 } else if (attrs.type == TYPE_INPUT_METHOD) {
1775 if (token.windowType != TYPE_INPUT_METHOD) {
1776 Log.w(TAG, "Attempted to add input method window with bad token "
1777 + attrs.token + ". Aborting.");
1778 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1779 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001780 } else if (attrs.type == TYPE_WALLPAPER) {
1781 if (token.windowType != TYPE_WALLPAPER) {
1782 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1783 + attrs.token + ". Aborting.");
1784 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787
1788 win = new WindowState(session, client, token,
1789 attachedWindow, attrs, viewVisibility);
1790 if (win.mDeathRecipient == null) {
1791 // Client has apparently died, so there is no reason to
1792 // continue.
1793 Log.w(TAG, "Adding window client " + client.asBinder()
1794 + " that is dead, aborting.");
1795 return WindowManagerImpl.ADD_APP_EXITING;
1796 }
1797
1798 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 res = mPolicy.prepareAddWindowLw(win, attrs);
1801 if (res != WindowManagerImpl.ADD_OKAY) {
1802 return res;
1803 }
1804
1805 // From now on, no exceptions or errors allowed!
1806
1807 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 if (addToken) {
1812 mTokenMap.put(attrs.token, token);
1813 mTokenList.add(token);
1814 }
1815 win.attach();
1816 mWindowMap.put(client.asBinder(), win);
1817
1818 if (attrs.type == TYPE_APPLICATION_STARTING &&
1819 token.appWindowToken != null) {
1820 token.appWindowToken.startingWindow = win;
1821 }
1822
1823 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 if (attrs.type == TYPE_INPUT_METHOD) {
1826 mInputMethodWindow = win;
1827 addInputMethodWindowToListLocked(win);
1828 imMayMove = false;
1829 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1830 mInputMethodDialogs.add(win);
1831 addWindowToListInOrderLocked(win, true);
1832 adjustInputMethodDialogsLocked();
1833 imMayMove = false;
1834 } else {
1835 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001836 if (attrs.type == TYPE_WALLPAPER) {
1837 mLastWallpaperTimeoutTime = 0;
1838 adjustWallpaperWindowsLocked();
1839 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001840 adjustWallpaperWindowsLocked();
1841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
Romain Guy06882f82009-06-10 13:36:04 -07001843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 if (mInTouchMode) {
1849 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1850 }
1851 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1852 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1853 }
Romain Guy06882f82009-06-10 13:36:04 -07001854
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001855 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001857 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1858 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 imMayMove = false;
1860 }
1861 }
Romain Guy06882f82009-06-10 13:36:04 -07001862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001864 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
Romain Guy06882f82009-06-10 13:36:04 -07001866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 assignLayersLocked();
1868 // Don't do layout here, the window must call
1869 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 //dump();
1872
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001873 if (focusChanged) {
1874 if (mCurrentFocus != null) {
1875 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1876 }
1877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 if (localLOGV) Log.v(
1879 TAG, "New client " + client.asBinder()
1880 + ": window=" + win);
1881 }
1882
1883 // sendNewConfiguration() checks caller permissions so we must call it with
1884 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1885 // identity anyway, so it's safe to just clear & restore around this whole
1886 // block.
1887 final long origId = Binder.clearCallingIdentity();
1888 if (reportNewConfig) {
1889 sendNewConfiguration();
1890 } else {
1891 // Update Orientation after adding a window, only if the window needs to be
1892 // displayed right away
1893 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001894 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 sendNewConfiguration();
1896 }
1897 }
1898 }
1899 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 return res;
1902 }
Romain Guy06882f82009-06-10 13:36:04 -07001903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 public void removeWindow(Session session, IWindow client) {
1905 synchronized(mWindowMap) {
1906 WindowState win = windowForClientLocked(session, client);
1907 if (win == null) {
1908 return;
1909 }
1910 removeWindowLocked(session, win);
1911 }
1912 }
Romain Guy06882f82009-06-10 13:36:04 -07001913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 public void removeWindowLocked(Session session, WindowState win) {
1915
1916 if (localLOGV || DEBUG_FOCUS) Log.v(
1917 TAG, "Remove " + win + " client="
1918 + Integer.toHexString(System.identityHashCode(
1919 win.mClient.asBinder()))
1920 + ", surface=" + win.mSurface);
1921
1922 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 if (DEBUG_APP_TRANSITIONS) Log.v(
1925 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1926 + " mExiting=" + win.mExiting
1927 + " isAnimating=" + win.isAnimating()
1928 + " app-animation="
1929 + (win.mAppToken != null ? win.mAppToken.animation : null)
1930 + " inPendingTransaction="
1931 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1932 + " mDisplayFrozen=" + mDisplayFrozen);
1933 // Visibility of the removed window. Will be used later to update orientation later on.
1934 boolean wasVisible = false;
1935 // First, see if we need to run an animation. If we do, we have
1936 // to hold off on removing the window until the animation is done.
1937 // If the display is frozen, just remove immediately, since the
1938 // animation wouldn't be seen.
1939 if (win.mSurface != null && !mDisplayFrozen) {
1940 // If we are not currently running the exit animation, we
1941 // need to see about starting one.
1942 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1945 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1946 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1947 }
1948 // Try starting an animation.
1949 if (applyAnimationLocked(win, transit, false)) {
1950 win.mExiting = true;
1951 }
1952 }
1953 if (win.mExiting || win.isAnimating()) {
1954 // The exit animation is running... wait for it!
1955 //Log.i(TAG, "*** Running exit animation...");
1956 win.mExiting = true;
1957 win.mRemoveOnExit = true;
1958 mLayoutNeeded = true;
1959 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1960 performLayoutAndPlaceSurfacesLocked();
1961 if (win.mAppToken != null) {
1962 win.mAppToken.updateReportedVisibilityLocked();
1963 }
1964 //dump();
1965 Binder.restoreCallingIdentity(origId);
1966 return;
1967 }
1968 }
1969
1970 removeWindowInnerLocked(session, win);
1971 // Removing a visible window will effect the computed orientation
1972 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001973 if (wasVisible && computeForcedAppOrientationLocked()
1974 != mForcedAppOrientation) {
1975 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
1977 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1978 Binder.restoreCallingIdentity(origId);
1979 }
Romain Guy06882f82009-06-10 13:36:04 -07001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001982 mKeyWaiter.finishedKey(session, win.mClient, true,
1983 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1985 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 if (mInputMethodTarget == win) {
1990 moveInputMethodWindowsIfNeededLocked(false);
1991 }
Romain Guy06882f82009-06-10 13:36:04 -07001992
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001993 if (false) {
1994 RuntimeException e = new RuntimeException("here");
1995 e.fillInStackTrace();
1996 Log.w(TAG, "Removing window " + win, e);
1997 }
1998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 mPolicy.removeWindowLw(win);
2000 win.removeLocked();
2001
2002 mWindowMap.remove(win.mClient.asBinder());
2003 mWindows.remove(win);
2004
2005 if (mInputMethodWindow == win) {
2006 mInputMethodWindow = null;
2007 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2008 mInputMethodDialogs.remove(win);
2009 }
Romain Guy06882f82009-06-10 13:36:04 -07002010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 final WindowToken token = win.mToken;
2012 final AppWindowToken atoken = win.mAppToken;
2013 token.windows.remove(win);
2014 if (atoken != null) {
2015 atoken.allAppWindows.remove(win);
2016 }
2017 if (localLOGV) Log.v(
2018 TAG, "**** Removing window " + win + ": count="
2019 + token.windows.size());
2020 if (token.windows.size() == 0) {
2021 if (!token.explicit) {
2022 mTokenMap.remove(token.token);
2023 mTokenList.remove(token);
2024 } else if (atoken != null) {
2025 atoken.firstWindowDrawn = false;
2026 }
2027 }
2028
2029 if (atoken != null) {
2030 if (atoken.startingWindow == win) {
2031 atoken.startingWindow = null;
2032 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2033 // If this is the last window and we had requested a starting
2034 // transition window, well there is no point now.
2035 atoken.startingData = null;
2036 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2037 // If this is the last window except for a starting transition
2038 // window, we need to get rid of the starting transition.
2039 if (DEBUG_STARTING_WINDOW) {
2040 Log.v(TAG, "Schedule remove starting " + token
2041 + ": no more real windows");
2042 }
2043 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2044 mH.sendMessage(m);
2045 }
2046 }
Romain Guy06882f82009-06-10 13:36:04 -07002047
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002048 if (win.mAttrs.type == TYPE_WALLPAPER) {
2049 mLastWallpaperTimeoutTime = 0;
2050 adjustWallpaperWindowsLocked();
2051 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002052 adjustWallpaperWindowsLocked();
2053 }
2054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 if (!mInLayout) {
2056 assignLayersLocked();
2057 mLayoutNeeded = true;
2058 performLayoutAndPlaceSurfacesLocked();
2059 if (win.mAppToken != null) {
2060 win.mAppToken.updateReportedVisibilityLocked();
2061 }
2062 }
2063 }
2064
2065 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2066 long origId = Binder.clearCallingIdentity();
2067 try {
2068 synchronized (mWindowMap) {
2069 WindowState w = windowForClientLocked(session, client);
2070 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002071 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 Surface.openTransaction();
2073 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002074 if (SHOW_TRANSACTIONS) Log.i(
2075 TAG, " SURFACE " + w.mSurface
2076 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 w.mSurface.setTransparentRegionHint(region);
2078 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002079 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 Surface.closeTransaction();
2081 }
2082 }
2083 }
2084 } finally {
2085 Binder.restoreCallingIdentity(origId);
2086 }
2087 }
2088
2089 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002090 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 Rect visibleInsets) {
2092 long origId = Binder.clearCallingIdentity();
2093 try {
2094 synchronized (mWindowMap) {
2095 WindowState w = windowForClientLocked(session, client);
2096 if (w != null) {
2097 w.mGivenInsetsPending = false;
2098 w.mGivenContentInsets.set(contentInsets);
2099 w.mGivenVisibleInsets.set(visibleInsets);
2100 w.mTouchableInsets = touchableInsets;
2101 mLayoutNeeded = true;
2102 performLayoutAndPlaceSurfacesLocked();
2103 }
2104 }
2105 } finally {
2106 Binder.restoreCallingIdentity(origId);
2107 }
2108 }
Romain Guy06882f82009-06-10 13:36:04 -07002109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 public void getWindowDisplayFrame(Session session, IWindow client,
2111 Rect outDisplayFrame) {
2112 synchronized(mWindowMap) {
2113 WindowState win = windowForClientLocked(session, client);
2114 if (win == null) {
2115 outDisplayFrame.setEmpty();
2116 return;
2117 }
2118 outDisplayFrame.set(win.mDisplayFrame);
2119 }
2120 }
2121
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002122 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2123 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2124 window.mWallpaperX = x;
2125 window.mWallpaperY = y;
2126
2127 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002128 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002129 performLayoutAndPlaceSurfacesLocked();
2130 }
2131 }
2132 }
2133 }
2134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 public int relayoutWindow(Session session, IWindow client,
2136 WindowManager.LayoutParams attrs, int requestedWidth,
2137 int requestedHeight, int viewVisibility, boolean insetsPending,
2138 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2139 Surface outSurface) {
2140 boolean displayed = false;
2141 boolean inTouchMode;
2142 Configuration newConfig = null;
2143 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 synchronized(mWindowMap) {
2146 WindowState win = windowForClientLocked(session, client);
2147 if (win == null) {
2148 return 0;
2149 }
2150 win.mRequestedWidth = requestedWidth;
2151 win.mRequestedHeight = requestedHeight;
2152
2153 if (attrs != null) {
2154 mPolicy.adjustWindowParamsLw(attrs);
2155 }
Romain Guy06882f82009-06-10 13:36:04 -07002156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 int attrChanges = 0;
2158 int flagChanges = 0;
2159 if (attrs != null) {
2160 flagChanges = win.mAttrs.flags ^= attrs.flags;
2161 attrChanges = win.mAttrs.copyFrom(attrs);
2162 }
2163
2164 if (localLOGV) Log.v(
2165 TAG, "Relayout given client " + client.asBinder()
2166 + " (" + win.mAttrs.getTitle() + ")");
2167
2168
2169 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2170 win.mAlpha = attrs.alpha;
2171 }
2172
2173 final boolean scaledWindow =
2174 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2175
2176 if (scaledWindow) {
2177 // requested{Width|Height} Surface's physical size
2178 // attrs.{width|height} Size on screen
2179 win.mHScale = (attrs.width != requestedWidth) ?
2180 (attrs.width / (float)requestedWidth) : 1.0f;
2181 win.mVScale = (attrs.height != requestedHeight) ?
2182 (attrs.height / (float)requestedHeight) : 1.0f;
2183 }
2184
2185 boolean imMayMove = (flagChanges&(
2186 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2187 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 boolean focusMayChange = win.mViewVisibility != viewVisibility
2190 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2191 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002192
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002193 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2194 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 win.mRelayoutCalled = true;
2197 final int oldVisibility = win.mViewVisibility;
2198 win.mViewVisibility = viewVisibility;
2199 if (viewVisibility == View.VISIBLE &&
2200 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2201 displayed = !win.isVisibleLw();
2202 if (win.mExiting) {
2203 win.mExiting = false;
2204 win.mAnimation = null;
2205 }
2206 if (win.mDestroying) {
2207 win.mDestroying = false;
2208 mDestroySurface.remove(win);
2209 }
2210 if (oldVisibility == View.GONE) {
2211 win.mEnterAnimationPending = true;
2212 }
2213 if (displayed && win.mSurface != null && !win.mDrawPending
2214 && !win.mCommitDrawPending && !mDisplayFrozen) {
2215 applyEnterAnimationLocked(win);
2216 }
2217 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488
6489 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 // Actual frame shown on-screen (may be modified by animation)
6492 final Rect mShownFrame = new Rect();
6493 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 /**
6496 * Insets that determine the actually visible area
6497 */
6498 final Rect mVisibleInsets = new Rect();
6499 final Rect mLastVisibleInsets = new Rect();
6500 boolean mVisibleInsetsChanged;
6501
6502 /**
6503 * Insets that are covered by system windows
6504 */
6505 final Rect mContentInsets = new Rect();
6506 final Rect mLastContentInsets = new Rect();
6507 boolean mContentInsetsChanged;
6508
6509 /**
6510 * Set to true if we are waiting for this window to receive its
6511 * given internal insets before laying out other windows based on it.
6512 */
6513 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 /**
6516 * These are the content insets that were given during layout for
6517 * this window, to be applied to windows behind it.
6518 */
6519 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 /**
6522 * These are the visible insets that were given during layout for
6523 * this window, to be applied to windows behind it.
6524 */
6525 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 /**
6528 * Flag indicating whether the touchable region should be adjusted by
6529 * the visible insets; if false the area outside the visible insets is
6530 * NOT touchable, so we must use those to adjust the frame during hit
6531 * tests.
6532 */
6533 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 // Current transformation being applied.
6536 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6537 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6538 float mHScale=1, mVScale=1;
6539 float mLastHScale=1, mLastVScale=1;
6540 final Matrix mTmpMatrix = new Matrix();
6541
6542 // "Real" frame that the application sees.
6543 final Rect mFrame = new Rect();
6544 final Rect mLastFrame = new Rect();
6545
6546 final Rect mContainingFrame = new Rect();
6547 final Rect mDisplayFrame = new Rect();
6548 final Rect mContentFrame = new Rect();
6549 final Rect mVisibleFrame = new Rect();
6550
6551 float mShownAlpha = 1;
6552 float mAlpha = 1;
6553 float mLastAlpha = 1;
6554
6555 // Set to true if, when the window gets displayed, it should perform
6556 // an enter animation.
6557 boolean mEnterAnimationPending;
6558
6559 // Currently running animation.
6560 boolean mAnimating;
6561 boolean mLocalAnimating;
6562 Animation mAnimation;
6563 boolean mAnimationIsEntrance;
6564 boolean mHasTransformation;
6565 boolean mHasLocalTransformation;
6566 final Transformation mTransformation = new Transformation();
6567
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006568 // If a window showing a wallpaper: the requested offset for the
6569 // wallpaper; if a wallpaper window: the currently applied offset.
6570 float mWallpaperX = -1;
6571 float mWallpaperY = -1;
6572
6573 // Wallpaper windows: pixels offset based on above variables.
6574 int mXOffset;
6575 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 // This is set after IWindowSession.relayout() has been called at
6578 // least once for the window. It allows us to detect the situation
6579 // where we don't yet have a surface, but should have one soon, so
6580 // we can give the window focus before waiting for the relayout.
6581 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583 // This is set after the Surface has been created but before the
6584 // window has been drawn. During this time the surface is hidden.
6585 boolean mDrawPending;
6586
6587 // This is set after the window has finished drawing for the first
6588 // time but before its surface is shown. The surface will be
6589 // displayed when the next layout is run.
6590 boolean mCommitDrawPending;
6591
6592 // This is set during the time after the window's drawing has been
6593 // committed, and before its surface is actually shown. It is used
6594 // to delay showing the surface until all windows in a token are ready
6595 // to be shown.
6596 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 // Set when the window has been shown in the screen the first time.
6599 boolean mHasDrawn;
6600
6601 // Currently running an exit animation?
6602 boolean mExiting;
6603
6604 // Currently on the mDestroySurface list?
6605 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 // Completely remove from window manager after exit animation?
6608 boolean mRemoveOnExit;
6609
6610 // Set when the orientation is changing and this window has not yet
6611 // been updated for the new orientation.
6612 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 // Is this window now (or just being) removed?
6615 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617 WindowState(Session s, IWindow c, WindowToken token,
6618 WindowState attachedWindow, WindowManager.LayoutParams a,
6619 int viewVisibility) {
6620 mSession = s;
6621 mClient = c;
6622 mToken = token;
6623 mAttrs.copyFrom(a);
6624 mViewVisibility = viewVisibility;
6625 DeathRecipient deathRecipient = new DeathRecipient();
6626 mAlpha = a.alpha;
6627 if (localLOGV) Log.v(
6628 TAG, "Window " + this + " client=" + c.asBinder()
6629 + " token=" + token + " (" + mAttrs.token + ")");
6630 try {
6631 c.asBinder().linkToDeath(deathRecipient, 0);
6632 } catch (RemoteException e) {
6633 mDeathRecipient = null;
6634 mAttachedWindow = null;
6635 mLayoutAttached = false;
6636 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006637 mIsWallpaper = false;
6638 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 mBaseLayer = 0;
6640 mSubLayer = 0;
6641 return;
6642 }
6643 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6646 mAttrs.type <= LAST_SUB_WINDOW)) {
6647 // The multiplier here is to reserve space for multiple
6648 // windows in the same type layer.
6649 mBaseLayer = mPolicy.windowTypeToLayerLw(
6650 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6651 + TYPE_LAYER_OFFSET;
6652 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6653 mAttachedWindow = attachedWindow;
6654 mAttachedWindow.mChildWindows.add(this);
6655 mLayoutAttached = mAttrs.type !=
6656 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6657 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6658 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006659 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6660 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 } else {
6662 // The multiplier here is to reserve space for multiple
6663 // windows in the same type layer.
6664 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6665 * TYPE_LAYER_MULTIPLIER
6666 + TYPE_LAYER_OFFSET;
6667 mSubLayer = 0;
6668 mAttachedWindow = null;
6669 mLayoutAttached = false;
6670 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6671 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006672 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6673 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674 }
6675
6676 WindowState appWin = this;
6677 while (appWin.mAttachedWindow != null) {
6678 appWin = mAttachedWindow;
6679 }
6680 WindowToken appToken = appWin.mToken;
6681 while (appToken.appWindowToken == null) {
6682 WindowToken parent = mTokenMap.get(appToken.token);
6683 if (parent == null || appToken == parent) {
6684 break;
6685 }
6686 appToken = parent;
6687 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006688 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 mAppToken = appToken.appWindowToken;
6690
6691 mSurface = null;
6692 mRequestedWidth = 0;
6693 mRequestedHeight = 0;
6694 mLastRequestedWidth = 0;
6695 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006696 mXOffset = 0;
6697 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 mLayer = 0;
6699 mAnimLayer = 0;
6700 mLastLayer = 0;
6701 }
6702
6703 void attach() {
6704 if (localLOGV) Log.v(
6705 TAG, "Attaching " + this + " token=" + mToken
6706 + ", list=" + mToken.windows);
6707 mSession.windowAddedLocked();
6708 }
6709
6710 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6711 mHaveFrame = true;
6712
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006713 final Rect container = mContainingFrame;
6714 container.set(pf);
6715
6716 final Rect display = mDisplayFrame;
6717 display.set(df);
6718
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006719 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006720 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006721 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6722 display.intersect(mCompatibleScreenFrame);
6723 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006724 }
6725
6726 final int pw = container.right - container.left;
6727 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728
6729 int w,h;
6730 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6731 w = mAttrs.width < 0 ? pw : mAttrs.width;
6732 h = mAttrs.height< 0 ? ph : mAttrs.height;
6733 } else {
6734 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6735 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6736 }
Romain Guy06882f82009-06-10 13:36:04 -07006737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006738 final Rect content = mContentFrame;
6739 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 final Rect visible = mVisibleFrame;
6742 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006744 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006745 final int fw = frame.width();
6746 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006748 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6749 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6750
6751 Gravity.apply(mAttrs.gravity, w, h, container,
6752 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6753 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6754
6755 //System.out.println("Out: " + mFrame);
6756
6757 // Now make sure the window fits in the overall display.
6758 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 // Make sure the content and visible frames are inside of the
6761 // final window frame.
6762 if (content.left < frame.left) content.left = frame.left;
6763 if (content.top < frame.top) content.top = frame.top;
6764 if (content.right > frame.right) content.right = frame.right;
6765 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6766 if (visible.left < frame.left) visible.left = frame.left;
6767 if (visible.top < frame.top) visible.top = frame.top;
6768 if (visible.right > frame.right) visible.right = frame.right;
6769 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 final Rect contentInsets = mContentInsets;
6772 contentInsets.left = content.left-frame.left;
6773 contentInsets.top = content.top-frame.top;
6774 contentInsets.right = frame.right-content.right;
6775 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 final Rect visibleInsets = mVisibleInsets;
6778 visibleInsets.left = visible.left-frame.left;
6779 visibleInsets.top = visible.top-frame.top;
6780 visibleInsets.right = frame.right-visible.right;
6781 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006782
Dianne Hackborn284ac932009-08-28 10:34:25 -07006783 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6784 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006785 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006786 }
6787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 if (localLOGV) {
6789 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6790 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6791 Log.v(TAG, "Resolving (mRequestedWidth="
6792 + mRequestedWidth + ", mRequestedheight="
6793 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6794 + "): frame=" + mFrame.toShortString()
6795 + " ci=" + contentInsets.toShortString()
6796 + " vi=" + visibleInsets.toShortString());
6797 //}
6798 }
6799 }
Romain Guy06882f82009-06-10 13:36:04 -07006800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 public Rect getFrameLw() {
6802 return mFrame;
6803 }
6804
6805 public Rect getShownFrameLw() {
6806 return mShownFrame;
6807 }
6808
6809 public Rect getDisplayFrameLw() {
6810 return mDisplayFrame;
6811 }
6812
6813 public Rect getContentFrameLw() {
6814 return mContentFrame;
6815 }
6816
6817 public Rect getVisibleFrameLw() {
6818 return mVisibleFrame;
6819 }
6820
6821 public boolean getGivenInsetsPendingLw() {
6822 return mGivenInsetsPending;
6823 }
6824
6825 public Rect getGivenContentInsetsLw() {
6826 return mGivenContentInsets;
6827 }
Romain Guy06882f82009-06-10 13:36:04 -07006828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 public Rect getGivenVisibleInsetsLw() {
6830 return mGivenVisibleInsets;
6831 }
Romain Guy06882f82009-06-10 13:36:04 -07006832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 public WindowManager.LayoutParams getAttrs() {
6834 return mAttrs;
6835 }
6836
6837 public int getSurfaceLayer() {
6838 return mLayer;
6839 }
Romain Guy06882f82009-06-10 13:36:04 -07006840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 public IApplicationToken getAppToken() {
6842 return mAppToken != null ? mAppToken.appToken : null;
6843 }
6844
6845 public boolean hasAppShownWindows() {
6846 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6847 }
6848
6849 public boolean hasAppStartingIcon() {
6850 return mAppToken != null ? (mAppToken.startingData != null) : false;
6851 }
6852
6853 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6854 return mAppToken != null ? mAppToken.startingWindow : null;
6855 }
6856
6857 public void setAnimation(Animation anim) {
6858 if (localLOGV) Log.v(
6859 TAG, "Setting animation in " + this + ": " + anim);
6860 mAnimating = false;
6861 mLocalAnimating = false;
6862 mAnimation = anim;
6863 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6864 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6865 }
6866
6867 public void clearAnimation() {
6868 if (mAnimation != null) {
6869 mAnimating = true;
6870 mLocalAnimating = false;
6871 mAnimation = null;
6872 }
6873 }
Romain Guy06882f82009-06-10 13:36:04 -07006874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 Surface createSurfaceLocked() {
6876 if (mSurface == null) {
6877 mDrawPending = true;
6878 mCommitDrawPending = false;
6879 mReadyToShow = false;
6880 if (mAppToken != null) {
6881 mAppToken.allDrawn = false;
6882 }
6883
6884 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006885 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006886 flags |= Surface.PUSH_BUFFERS;
6887 }
6888
6889 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6890 flags |= Surface.SECURE;
6891 }
6892 if (DEBUG_VISIBILITY) Log.v(
6893 TAG, "Creating surface in session "
6894 + mSession.mSurfaceSession + " window " + this
6895 + " w=" + mFrame.width()
6896 + " h=" + mFrame.height() + " format="
6897 + mAttrs.format + " flags=" + flags);
6898
6899 int w = mFrame.width();
6900 int h = mFrame.height();
6901 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6902 // for a scaled surface, we always want the requested
6903 // size.
6904 w = mRequestedWidth;
6905 h = mRequestedHeight;
6906 }
6907
6908 try {
6909 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006910 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006912 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
6913 + mSurface + " IN SESSION "
6914 + mSession.mSurfaceSession
6915 + ": pid=" + mSession.mPid + " format="
6916 + mAttrs.format + " flags=0x"
6917 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 } catch (Surface.OutOfResourcesException e) {
6919 Log.w(TAG, "OutOfResourcesException creating surface");
6920 reclaimSomeSurfaceMemoryLocked(this, "create");
6921 return null;
6922 } catch (Exception e) {
6923 Log.e(TAG, "Exception creating surface", e);
6924 return null;
6925 }
Romain Guy06882f82009-06-10 13:36:04 -07006926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 if (localLOGV) Log.v(
6928 TAG, "Got surface: " + mSurface
6929 + ", set left=" + mFrame.left + " top=" + mFrame.top
6930 + ", animLayer=" + mAnimLayer);
6931 if (SHOW_TRANSACTIONS) {
6932 Log.i(TAG, ">>> OPEN TRANSACTION");
6933 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6934 + mAttrs.getTitle() + ") pos=(" +
6935 mFrame.left + "," + mFrame.top + ") (" +
6936 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6937 mAnimLayer + " HIDE");
6938 }
6939 Surface.openTransaction();
6940 try {
6941 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006942 mSurface.setPosition(mFrame.left + mXOffset,
6943 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 mSurface.setLayer(mAnimLayer);
6945 mSurface.hide();
6946 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006947 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
6948 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 mSurface.setFlags(Surface.SURFACE_DITHER,
6950 Surface.SURFACE_DITHER);
6951 }
6952 } catch (RuntimeException e) {
6953 Log.w(TAG, "Error creating surface in " + w, e);
6954 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6955 }
6956 mLastHidden = true;
6957 } finally {
6958 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6959 Surface.closeTransaction();
6960 }
6961 if (localLOGV) Log.v(
6962 TAG, "Created surface " + this);
6963 }
6964 return mSurface;
6965 }
Romain Guy06882f82009-06-10 13:36:04 -07006966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 void destroySurfaceLocked() {
6968 // Window is no longer on-screen, so can no longer receive
6969 // key events... if we were waiting for it to finish
6970 // handling a key event, the wait is over!
6971 mKeyWaiter.finishedKey(mSession, mClient, true,
6972 KeyWaiter.RETURN_NOTHING);
6973 mKeyWaiter.releasePendingPointerLocked(mSession);
6974 mKeyWaiter.releasePendingTrackballLocked(mSession);
6975
6976 if (mAppToken != null && this == mAppToken.startingWindow) {
6977 mAppToken.startingDisplayed = false;
6978 }
Romain Guy06882f82009-06-10 13:36:04 -07006979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 if (mSurface != null) {
6981 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006982 if (DEBUG_VISIBILITY) {
6983 RuntimeException e = new RuntimeException();
6984 e.fillInStackTrace();
6985 Log.w(TAG, "Window " + this + " destroying surface "
6986 + mSurface + ", session " + mSession, e);
6987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 if (SHOW_TRANSACTIONS) {
6989 RuntimeException ex = new RuntimeException();
6990 ex.fillInStackTrace();
6991 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6992 + mAttrs.getTitle() + ")", ex);
6993 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006994 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 } catch (RuntimeException e) {
6996 Log.w(TAG, "Exception thrown when destroying Window " + this
6997 + " surface " + mSurface + " session " + mSession
6998 + ": " + e.toString());
6999 }
7000 mSurface = null;
7001 mDrawPending = false;
7002 mCommitDrawPending = false;
7003 mReadyToShow = false;
7004
7005 int i = mChildWindows.size();
7006 while (i > 0) {
7007 i--;
7008 WindowState c = (WindowState)mChildWindows.get(i);
7009 c.mAttachedHidden = true;
7010 }
7011 }
7012 }
7013
7014 boolean finishDrawingLocked() {
7015 if (mDrawPending) {
7016 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7017 TAG, "finishDrawingLocked: " + mSurface);
7018 mCommitDrawPending = true;
7019 mDrawPending = false;
7020 return true;
7021 }
7022 return false;
7023 }
7024
7025 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007026 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7028 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007029 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007030 }
7031 mCommitDrawPending = false;
7032 mReadyToShow = true;
7033 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7034 final AppWindowToken atoken = mAppToken;
7035 if (atoken == null || atoken.allDrawn || starting) {
7036 performShowLocked();
7037 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007038 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 }
7040
7041 // This must be called while inside a transaction.
7042 boolean performShowLocked() {
7043 if (DEBUG_VISIBILITY) {
7044 RuntimeException e = new RuntimeException();
7045 e.fillInStackTrace();
7046 Log.v(TAG, "performShow on " + this
7047 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7048 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7049 }
7050 if (mReadyToShow && isReadyForDisplay()) {
7051 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7052 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7053 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7054 + " during animation: policyVis=" + mPolicyVisibility
7055 + " attHidden=" + mAttachedHidden
7056 + " tok.hiddenRequested="
7057 + (mAppToken != null ? mAppToken.hiddenRequested : false)
7058 + " tok.idden="
7059 + (mAppToken != null ? mAppToken.hidden : false)
7060 + " animating=" + mAnimating
7061 + " tok animating="
7062 + (mAppToken != null ? mAppToken.animating : false));
7063 if (!showSurfaceRobustlyLocked(this)) {
7064 return false;
7065 }
7066 mLastAlpha = -1;
7067 mHasDrawn = true;
7068 mLastHidden = false;
7069 mReadyToShow = false;
7070 enableScreenIfNeededLocked();
7071
7072 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 int i = mChildWindows.size();
7075 while (i > 0) {
7076 i--;
7077 WindowState c = (WindowState)mChildWindows.get(i);
7078 if (c.mSurface != null && c.mAttachedHidden) {
7079 c.mAttachedHidden = false;
7080 c.performShowLocked();
7081 }
7082 }
Romain Guy06882f82009-06-10 13:36:04 -07007083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 if (mAttrs.type != TYPE_APPLICATION_STARTING
7085 && mAppToken != null) {
7086 mAppToken.firstWindowDrawn = true;
7087 if (mAnimation == null && mAppToken.startingData != null) {
7088 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7089 + mToken
7090 + ": first real window is shown, no animation");
7091 mFinishedStarting.add(mAppToken);
7092 mH.sendEmptyMessage(H.FINISHED_STARTING);
7093 }
7094 mAppToken.updateReportedVisibilityLocked();
7095 }
7096 }
7097 return true;
7098 }
Romain Guy06882f82009-06-10 13:36:04 -07007099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 // This must be called while inside a transaction. Returns true if
7101 // there is more animation to run.
7102 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7103 if (!mDisplayFrozen) {
7104 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7107 mHasTransformation = true;
7108 mHasLocalTransformation = true;
7109 if (!mLocalAnimating) {
7110 if (DEBUG_ANIM) Log.v(
7111 TAG, "Starting animation in " + this +
7112 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7113 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7114 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7115 mAnimation.setStartTime(currentTime);
7116 mLocalAnimating = true;
7117 mAnimating = true;
7118 }
7119 mTransformation.clear();
7120 final boolean more = mAnimation.getTransformation(
7121 currentTime, mTransformation);
7122 if (DEBUG_ANIM) Log.v(
7123 TAG, "Stepped animation in " + this +
7124 ": more=" + more + ", xform=" + mTransformation);
7125 if (more) {
7126 // we're not done!
7127 return true;
7128 }
7129 if (DEBUG_ANIM) Log.v(
7130 TAG, "Finished animation in " + this +
7131 " @ " + currentTime);
7132 mAnimation = null;
7133 //WindowManagerService.this.dump();
7134 }
7135 mHasLocalTransformation = false;
7136 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007137 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 // When our app token is animating, we kind-of pretend like
7139 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7140 // part of this check means that we will only do this if
7141 // our window is not currently exiting, or it is not
7142 // locally animating itself. The idea being that one that
7143 // is exiting and doing a local animation should be removed
7144 // once that animation is done.
7145 mAnimating = true;
7146 mHasTransformation = true;
7147 mTransformation.clear();
7148 return false;
7149 } else if (mHasTransformation) {
7150 // Little trick to get through the path below to act like
7151 // we have finished an animation.
7152 mAnimating = true;
7153 } else if (isAnimating()) {
7154 mAnimating = true;
7155 }
7156 } else if (mAnimation != null) {
7157 // If the display is frozen, and there is a pending animation,
7158 // clear it and make sure we run the cleanup code.
7159 mAnimating = true;
7160 mLocalAnimating = true;
7161 mAnimation = null;
7162 }
Romain Guy06882f82009-06-10 13:36:04 -07007163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 if (!mAnimating && !mLocalAnimating) {
7165 return false;
7166 }
7167
7168 if (DEBUG_ANIM) Log.v(
7169 TAG, "Animation done in " + this + ": exiting=" + mExiting
7170 + ", reportedVisible="
7171 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007173 mAnimating = false;
7174 mLocalAnimating = false;
7175 mAnimation = null;
7176 mAnimLayer = mLayer;
7177 if (mIsImWindow) {
7178 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007179 } else if (mIsWallpaper) {
7180 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007181 }
7182 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7183 + " anim layer: " + mAnimLayer);
7184 mHasTransformation = false;
7185 mHasLocalTransformation = false;
7186 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7187 mTransformation.clear();
7188 if (mHasDrawn
7189 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7190 && mAppToken != null
7191 && mAppToken.firstWindowDrawn
7192 && mAppToken.startingData != null) {
7193 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7194 + mToken + ": first real window done animating");
7195 mFinishedStarting.add(mAppToken);
7196 mH.sendEmptyMessage(H.FINISHED_STARTING);
7197 }
Romain Guy06882f82009-06-10 13:36:04 -07007198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 finishExit();
7200
7201 if (mAppToken != null) {
7202 mAppToken.updateReportedVisibilityLocked();
7203 }
7204
7205 return false;
7206 }
7207
7208 void finishExit() {
7209 if (DEBUG_ANIM) Log.v(
7210 TAG, "finishExit in " + this
7211 + ": exiting=" + mExiting
7212 + " remove=" + mRemoveOnExit
7213 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007215 final int N = mChildWindows.size();
7216 for (int i=0; i<N; i++) {
7217 ((WindowState)mChildWindows.get(i)).finishExit();
7218 }
Romain Guy06882f82009-06-10 13:36:04 -07007219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 if (!mExiting) {
7221 return;
7222 }
Romain Guy06882f82009-06-10 13:36:04 -07007223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 if (isWindowAnimating()) {
7225 return;
7226 }
7227
7228 if (localLOGV) Log.v(
7229 TAG, "Exit animation finished in " + this
7230 + ": remove=" + mRemoveOnExit);
7231 if (mSurface != null) {
7232 mDestroySurface.add(this);
7233 mDestroying = true;
7234 if (SHOW_TRANSACTIONS) Log.i(
7235 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7236 try {
7237 mSurface.hide();
7238 } catch (RuntimeException e) {
7239 Log.w(TAG, "Error hiding surface in " + this, e);
7240 }
7241 mLastHidden = true;
7242 mKeyWaiter.releasePendingPointerLocked(mSession);
7243 }
7244 mExiting = false;
7245 if (mRemoveOnExit) {
7246 mPendingRemove.add(this);
7247 mRemoveOnExit = false;
7248 }
7249 }
Romain Guy06882f82009-06-10 13:36:04 -07007250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007251 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7252 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7253 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7254 if (dtdx < -.000001f || dtdx > .000001f) return false;
7255 if (dsdy < -.000001f || dsdy > .000001f) return false;
7256 return true;
7257 }
Romain Guy06882f82009-06-10 13:36:04 -07007258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 void computeShownFrameLocked() {
7260 final boolean selfTransformation = mHasLocalTransformation;
7261 Transformation attachedTransformation =
7262 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7263 ? mAttachedWindow.mTransformation : null;
7264 Transformation appTransformation =
7265 (mAppToken != null && mAppToken.hasTransformation)
7266 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007267
7268 // Wallpapers are animated based on the "real" window they
7269 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007270 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007271 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007272 if (mWallpaperTarget.mHasLocalTransformation) {
7273 attachedTransformation = mWallpaperTarget.mTransformation;
7274 }
7275 if (mWallpaperTarget.mAppToken != null &&
7276 mWallpaperTarget.mAppToken.hasTransformation) {
7277 appTransformation = mWallpaperTarget.mAppToken.transformation;
7278 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007279 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7280 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7281 }
7282 if (DEBUG_WALLPAPER && appTransformation != null) {
7283 Log.v(TAG, "WP target app xform: " + appTransformation);
7284 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007285 }
7286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 if (selfTransformation || attachedTransformation != null
7288 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007289 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 final Rect frame = mFrame;
7291 final float tmpFloats[] = mTmpFloats;
7292 final Matrix tmpMatrix = mTmpMatrix;
7293
7294 // Compute the desired transformation.
7295 tmpMatrix.setTranslate(frame.left, frame.top);
7296 if (selfTransformation) {
7297 tmpMatrix.preConcat(mTransformation.getMatrix());
7298 }
7299 if (attachedTransformation != null) {
7300 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7301 }
7302 if (appTransformation != null) {
7303 tmpMatrix.preConcat(appTransformation.getMatrix());
7304 }
7305
7306 // "convert" it into SurfaceFlinger's format
7307 // (a 2x2 matrix + an offset)
7308 // Here we must not transform the position of the surface
7309 // since it is already included in the transformation.
7310 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 tmpMatrix.getValues(tmpFloats);
7313 mDsDx = tmpFloats[Matrix.MSCALE_X];
7314 mDtDx = tmpFloats[Matrix.MSKEW_X];
7315 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7316 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007317 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7318 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 int w = frame.width();
7320 int h = frame.height();
7321 mShownFrame.set(x, y, x+w, y+h);
7322
7323 // Now set the alpha... but because our current hardware
7324 // can't do alpha transformation on a non-opaque surface,
7325 // turn it off if we are running an animation that is also
7326 // transforming since it is more important to have that
7327 // animation be smooth.
7328 mShownAlpha = mAlpha;
7329 if (!mLimitedAlphaCompositing
7330 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7331 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7332 && x == frame.left && y == frame.top))) {
7333 //Log.i(TAG, "Applying alpha transform");
7334 if (selfTransformation) {
7335 mShownAlpha *= mTransformation.getAlpha();
7336 }
7337 if (attachedTransformation != null) {
7338 mShownAlpha *= attachedTransformation.getAlpha();
7339 }
7340 if (appTransformation != null) {
7341 mShownAlpha *= appTransformation.getAlpha();
7342 }
7343 } else {
7344 //Log.i(TAG, "Not applying alpha transform");
7345 }
Romain Guy06882f82009-06-10 13:36:04 -07007346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 if (localLOGV) Log.v(
7348 TAG, "Continuing animation in " + this +
7349 ": " + mShownFrame +
7350 ", alpha=" + mTransformation.getAlpha());
7351 return;
7352 }
Romain Guy06882f82009-06-10 13:36:04 -07007353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007355 if (mXOffset != 0 || mYOffset != 0) {
7356 mShownFrame.offset(mXOffset, mYOffset);
7357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 mShownAlpha = mAlpha;
7359 mDsDx = 1;
7360 mDtDx = 0;
7361 mDsDy = 0;
7362 mDtDy = 1;
7363 }
Romain Guy06882f82009-06-10 13:36:04 -07007364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 /**
7366 * Is this window visible? It is not visible if there is no
7367 * surface, or we are in the process of running an exit animation
7368 * that will remove the surface, or its app token has been hidden.
7369 */
7370 public boolean isVisibleLw() {
7371 final AppWindowToken atoken = mAppToken;
7372 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7373 && (atoken == null || !atoken.hiddenRequested)
7374 && !mExiting && !mDestroying;
7375 }
7376
7377 /**
7378 * Is this window visible, ignoring its app token? It is not visible
7379 * if there is no surface, or we are in the process of running an exit animation
7380 * that will remove the surface.
7381 */
7382 public boolean isWinVisibleLw() {
7383 final AppWindowToken atoken = mAppToken;
7384 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7385 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7386 && !mExiting && !mDestroying;
7387 }
7388
7389 /**
7390 * The same as isVisible(), but follows the current hidden state of
7391 * the associated app token, not the pending requested hidden state.
7392 */
7393 boolean isVisibleNow() {
7394 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007395 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 }
7397
7398 /**
7399 * Same as isVisible(), but we also count it as visible between the
7400 * call to IWindowSession.add() and the first relayout().
7401 */
7402 boolean isVisibleOrAdding() {
7403 final AppWindowToken atoken = mAppToken;
7404 return (mSurface != null
7405 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7406 && mPolicyVisibility && !mAttachedHidden
7407 && (atoken == null || !atoken.hiddenRequested)
7408 && !mExiting && !mDestroying;
7409 }
7410
7411 /**
7412 * Is this window currently on-screen? It is on-screen either if it
7413 * is visible or it is currently running an animation before no longer
7414 * being visible.
7415 */
7416 boolean isOnScreen() {
7417 final AppWindowToken atoken = mAppToken;
7418 if (atoken != null) {
7419 return mSurface != null && mPolicyVisibility && !mDestroying
7420 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007421 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007422 } else {
7423 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007424 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 }
7426 }
Romain Guy06882f82009-06-10 13:36:04 -07007427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 /**
7429 * Like isOnScreen(), but we don't return true if the window is part
7430 * of a transition that has not yet been started.
7431 */
7432 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007433 if (mRootToken.waitingToShow &&
7434 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7435 return false;
7436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007438 final boolean animating = atoken != null
7439 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007441 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7442 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007443 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007444 }
7445
7446 /** Is the window or its container currently animating? */
7447 boolean isAnimating() {
7448 final WindowState attached = mAttachedWindow;
7449 final AppWindowToken atoken = mAppToken;
7450 return mAnimation != null
7451 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007452 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 (atoken.animation != null
7454 || atoken.inPendingTransaction));
7455 }
7456
7457 /** Is this window currently animating? */
7458 boolean isWindowAnimating() {
7459 return mAnimation != null;
7460 }
7461
7462 /**
7463 * Like isOnScreen, but returns false if the surface hasn't yet
7464 * been drawn.
7465 */
7466 public boolean isDisplayedLw() {
7467 final AppWindowToken atoken = mAppToken;
7468 return mSurface != null && mPolicyVisibility && !mDestroying
7469 && !mDrawPending && !mCommitDrawPending
7470 && ((!mAttachedHidden &&
7471 (atoken == null || !atoken.hiddenRequested))
7472 || mAnimating);
7473 }
7474
7475 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7476 boolean shownFrame, boolean onlyOpaque) {
7477 if (mSurface == null) {
7478 return false;
7479 }
7480 if (mAppToken != null && !mAppToken.appFullscreen) {
7481 return false;
7482 }
7483 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7484 return false;
7485 }
7486 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007487
7488 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7489 return frame.left <= mCompatibleScreenFrame.left &&
7490 frame.top <= mCompatibleScreenFrame.top &&
7491 frame.right >= mCompatibleScreenFrame.right &&
7492 frame.bottom >= mCompatibleScreenFrame.bottom;
7493 } else {
7494 return frame.left <= 0 && frame.top <= 0
7495 && frame.right >= screenWidth
7496 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007498 }
Romain Guy06882f82009-06-10 13:36:04 -07007499
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007500 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007501 * Return true if the window is opaque and fully drawn. This indicates
7502 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007503 */
7504 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007505 return (mAttrs.format == PixelFormat.OPAQUE
7506 || mAttrs.type == TYPE_WALLPAPER)
7507 && mSurface != null && mAnimation == null
7508 && (mAppToken == null || mAppToken.animation == null)
7509 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007510 }
7511
7512 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7513 return
7514 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007515 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7516 // only if it's visible
7517 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007518 // and only if the application fills the compatible screen
7519 mFrame.left <= mCompatibleScreenFrame.left &&
7520 mFrame.top <= mCompatibleScreenFrame.top &&
7521 mFrame.right >= mCompatibleScreenFrame.right &&
7522 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007523 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007524 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007525 }
7526
7527 boolean isFullscreen(int screenWidth, int screenHeight) {
7528 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007529 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530 }
7531
7532 void removeLocked() {
7533 if (mAttachedWindow != null) {
7534 mAttachedWindow.mChildWindows.remove(this);
7535 }
7536 destroySurfaceLocked();
7537 mSession.windowRemovedLocked();
7538 try {
7539 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7540 } catch (RuntimeException e) {
7541 // Ignore if it has already been removed (usually because
7542 // we are doing this as part of processing a death note.)
7543 }
7544 }
7545
7546 private class DeathRecipient implements IBinder.DeathRecipient {
7547 public void binderDied() {
7548 try {
7549 synchronized(mWindowMap) {
7550 WindowState win = windowForClientLocked(mSession, mClient);
7551 Log.i(TAG, "WIN DEATH: " + win);
7552 if (win != null) {
7553 removeWindowLocked(mSession, win);
7554 }
7555 }
7556 } catch (IllegalArgumentException ex) {
7557 // This will happen if the window has already been
7558 // removed.
7559 }
7560 }
7561 }
7562
7563 /** Returns true if this window desires key events. */
7564 public final boolean canReceiveKeys() {
7565 return isVisibleOrAdding()
7566 && (mViewVisibility == View.VISIBLE)
7567 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7568 }
7569
7570 public boolean hasDrawnLw() {
7571 return mHasDrawn;
7572 }
7573
7574 public boolean showLw(boolean doAnimation) {
7575 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7576 mPolicyVisibility = true;
7577 mPolicyVisibilityAfterAnim = true;
7578 if (doAnimation) {
7579 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7580 }
7581 requestAnimationLocked(0);
7582 return true;
7583 }
7584 return false;
7585 }
7586
7587 public boolean hideLw(boolean doAnimation) {
7588 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7589 : mPolicyVisibility;
7590 if (current) {
7591 if (doAnimation) {
7592 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7593 if (mAnimation == null) {
7594 doAnimation = false;
7595 }
7596 }
7597 if (doAnimation) {
7598 mPolicyVisibilityAfterAnim = false;
7599 } else {
7600 mPolicyVisibilityAfterAnim = false;
7601 mPolicyVisibility = false;
7602 }
7603 requestAnimationLocked(0);
7604 return true;
7605 }
7606 return false;
7607 }
7608
7609 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007610 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007611
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007612 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7613 pw.print(" mClient="); pw.println(mClient.asBinder());
7614 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7615 if (mAttachedWindow != null || mLayoutAttached) {
7616 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7617 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7618 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007619 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7620 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7621 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007622 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7623 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007624 }
7625 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7626 pw.print(" mSubLayer="); pw.print(mSubLayer);
7627 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7628 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7629 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7630 pw.print("="); pw.print(mAnimLayer);
7631 pw.print(" mLastLayer="); pw.println(mLastLayer);
7632 if (mSurface != null) {
7633 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7634 }
7635 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7636 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7637 if (mAppToken != null) {
7638 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7639 }
7640 if (mTargetAppToken != null) {
7641 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7642 }
7643 pw.print(prefix); pw.print("mViewVisibility=0x");
7644 pw.print(Integer.toHexString(mViewVisibility));
7645 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007646 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7647 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007648 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7649 pw.print(prefix); pw.print("mPolicyVisibility=");
7650 pw.print(mPolicyVisibility);
7651 pw.print(" mPolicyVisibilityAfterAnim=");
7652 pw.print(mPolicyVisibilityAfterAnim);
7653 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7654 }
7655 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007656 pw.print(" h="); pw.println(mRequestedHeight);
7657 if (mXOffset != 0 || mYOffset != 0) {
7658 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7659 pw.print(" y="); pw.println(mYOffset);
7660 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007661 pw.print(prefix); pw.print("mGivenContentInsets=");
7662 mGivenContentInsets.printShortString(pw);
7663 pw.print(" mGivenVisibleInsets=");
7664 mGivenVisibleInsets.printShortString(pw);
7665 pw.println();
7666 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7667 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7668 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7669 }
7670 pw.print(prefix); pw.print("mShownFrame=");
7671 mShownFrame.printShortString(pw);
7672 pw.print(" last="); mLastShownFrame.printShortString(pw);
7673 pw.println();
7674 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7675 pw.print(" last="); mLastFrame.printShortString(pw);
7676 pw.println();
7677 pw.print(prefix); pw.print("mContainingFrame=");
7678 mContainingFrame.printShortString(pw);
7679 pw.print(" mDisplayFrame=");
7680 mDisplayFrame.printShortString(pw);
7681 pw.println();
7682 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7683 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7684 pw.println();
7685 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7686 pw.print(" last="); mLastContentInsets.printShortString(pw);
7687 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7688 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7689 pw.println();
7690 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7691 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7692 pw.print(" mAlpha="); pw.print(mAlpha);
7693 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7694 }
7695 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7696 || mAnimation != null) {
7697 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7698 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7699 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7700 pw.print(" mAnimation="); pw.println(mAnimation);
7701 }
7702 if (mHasTransformation || mHasLocalTransformation) {
7703 pw.print(prefix); pw.print("XForm: has=");
7704 pw.print(mHasTransformation);
7705 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7706 pw.print(" "); mTransformation.printShortString(pw);
7707 pw.println();
7708 }
7709 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7710 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7711 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7712 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7713 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7714 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7715 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7716 pw.print(" mDestroying="); pw.print(mDestroying);
7717 pw.print(" mRemoved="); pw.println(mRemoved);
7718 }
7719 if (mOrientationChanging || mAppFreezing) {
7720 pw.print(prefix); pw.print("mOrientationChanging=");
7721 pw.print(mOrientationChanging);
7722 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7723 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007724 if (mHScale != 1 || mVScale != 1) {
7725 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7726 pw.print(" mVScale="); pw.println(mVScale);
7727 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007728 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007729 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7730 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732 }
7733
7734 @Override
7735 public String toString() {
7736 return "Window{"
7737 + Integer.toHexString(System.identityHashCode(this))
7738 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7739 }
7740 }
Romain Guy06882f82009-06-10 13:36:04 -07007741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007742 // -------------------------------------------------------------
7743 // Window Token State
7744 // -------------------------------------------------------------
7745
7746 class WindowToken {
7747 // The actual token.
7748 final IBinder token;
7749
7750 // The type of window this token is for, as per WindowManager.LayoutParams.
7751 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 // Set if this token was explicitly added by a client, so should
7754 // not be removed when all windows are removed.
7755 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007756
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007757 // For printing.
7758 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007760 // If this is an AppWindowToken, this is non-null.
7761 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 // All of the windows associated with this token.
7764 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7765
7766 // Is key dispatching paused for this token?
7767 boolean paused = false;
7768
7769 // Should this token's windows be hidden?
7770 boolean hidden;
7771
7772 // Temporary for finding which tokens no longer have visible windows.
7773 boolean hasVisible;
7774
Dianne Hackborna8f60182009-09-01 19:01:50 -07007775 // Set to true when this token is in a pending transaction where it
7776 // will be shown.
7777 boolean waitingToShow;
7778
7779 // Set to true when this token is in a pending transaction where it
7780 // will be hidden.
7781 boolean waitingToHide;
7782
7783 // Set to true when this token is in a pending transaction where its
7784 // windows will be put to the bottom of the list.
7785 boolean sendingToBottom;
7786
7787 // Set to true when this token is in a pending transaction where its
7788 // windows will be put to the top of the list.
7789 boolean sendingToTop;
7790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 WindowToken(IBinder _token, int type, boolean _explicit) {
7792 token = _token;
7793 windowType = type;
7794 explicit = _explicit;
7795 }
7796
7797 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007798 pw.print(prefix); pw.print("token="); pw.println(token);
7799 pw.print(prefix); pw.print("windows="); pw.println(windows);
7800 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7801 pw.print(" hidden="); pw.print(hidden);
7802 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007803 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7804 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7805 pw.print(" waitingToHide="); pw.print(waitingToHide);
7806 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7807 pw.print(" sendingToTop="); pw.println(sendingToTop);
7808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 }
7810
7811 @Override
7812 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007813 if (stringName == null) {
7814 StringBuilder sb = new StringBuilder();
7815 sb.append("WindowToken{");
7816 sb.append(Integer.toHexString(System.identityHashCode(this)));
7817 sb.append(" token="); sb.append(token); sb.append('}');
7818 stringName = sb.toString();
7819 }
7820 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 }
7822 };
7823
7824 class AppWindowToken extends WindowToken {
7825 // Non-null only for application tokens.
7826 final IApplicationToken appToken;
7827
7828 // All of the windows and child windows that are included in this
7829 // application token. Note this list is NOT sorted!
7830 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7831
7832 int groupId = -1;
7833 boolean appFullscreen;
7834 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 // These are used for determining when all windows associated with
7837 // an activity have been drawn, so they can be made visible together
7838 // at the same time.
7839 int lastTransactionSequence = mTransactionSequence-1;
7840 int numInterestingWindows;
7841 int numDrawnWindows;
7842 boolean inPendingTransaction;
7843 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 // Is this token going to be hidden in a little while? If so, it
7846 // won't be taken into account for setting the screen orientation.
7847 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 // Is this window's surface needed? This is almost like hidden, except
7850 // it will sometimes be true a little earlier: when the token has
7851 // been shown, but is still waiting for its app transition to execute
7852 // before making its windows shown.
7853 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007855 // Have we told the window clients to hide themselves?
7856 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 // Last visibility state we reported to the app token.
7859 boolean reportedVisible;
7860
7861 // Set to true when the token has been removed from the window mgr.
7862 boolean removed;
7863
7864 // Have we been asked to have this token keep the screen frozen?
7865 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 boolean animating;
7868 Animation animation;
7869 boolean hasTransformation;
7870 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 // Offset to the window of all layers in the token, for use by
7873 // AppWindowToken animations.
7874 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876 // Information about an application starting window if displayed.
7877 StartingData startingData;
7878 WindowState startingWindow;
7879 View startingView;
7880 boolean startingDisplayed;
7881 boolean startingMoved;
7882 boolean firstWindowDrawn;
7883
7884 AppWindowToken(IApplicationToken _token) {
7885 super(_token.asBinder(),
7886 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7887 appWindowToken = this;
7888 appToken = _token;
7889 }
Romain Guy06882f82009-06-10 13:36:04 -07007890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 public void setAnimation(Animation anim) {
7892 if (localLOGV) Log.v(
7893 TAG, "Setting animation in " + this + ": " + anim);
7894 animation = anim;
7895 animating = false;
7896 anim.restrictDuration(MAX_ANIMATION_DURATION);
7897 anim.scaleCurrentDuration(mTransitionAnimationScale);
7898 int zorder = anim.getZAdjustment();
7899 int adj = 0;
7900 if (zorder == Animation.ZORDER_TOP) {
7901 adj = TYPE_LAYER_OFFSET;
7902 } else if (zorder == Animation.ZORDER_BOTTOM) {
7903 adj = -TYPE_LAYER_OFFSET;
7904 }
Romain Guy06882f82009-06-10 13:36:04 -07007905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007906 if (animLayerAdjustment != adj) {
7907 animLayerAdjustment = adj;
7908 updateLayers();
7909 }
7910 }
Romain Guy06882f82009-06-10 13:36:04 -07007911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 public void setDummyAnimation() {
7913 if (animation == null) {
7914 if (localLOGV) Log.v(
7915 TAG, "Setting dummy animation in " + this);
7916 animation = sDummyAnimation;
7917 }
7918 }
7919
7920 public void clearAnimation() {
7921 if (animation != null) {
7922 animation = null;
7923 animating = true;
7924 }
7925 }
Romain Guy06882f82009-06-10 13:36:04 -07007926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007927 void updateLayers() {
7928 final int N = allAppWindows.size();
7929 final int adj = animLayerAdjustment;
7930 for (int i=0; i<N; i++) {
7931 WindowState w = allAppWindows.get(i);
7932 w.mAnimLayer = w.mLayer + adj;
7933 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7934 + w.mAnimLayer);
7935 if (w == mInputMethodTarget) {
7936 setInputMethodAnimLayerAdjustment(adj);
7937 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007938 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007939 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 }
7942 }
Romain Guy06882f82009-06-10 13:36:04 -07007943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 void sendAppVisibilityToClients() {
7945 final int N = allAppWindows.size();
7946 for (int i=0; i<N; i++) {
7947 WindowState win = allAppWindows.get(i);
7948 if (win == startingWindow && clientHidden) {
7949 // Don't hide the starting window.
7950 continue;
7951 }
7952 try {
7953 if (DEBUG_VISIBILITY) Log.v(TAG,
7954 "Setting visibility of " + win + ": " + (!clientHidden));
7955 win.mClient.dispatchAppVisibility(!clientHidden);
7956 } catch (RemoteException e) {
7957 }
7958 }
7959 }
Romain Guy06882f82009-06-10 13:36:04 -07007960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 void showAllWindowsLocked() {
7962 final int NW = allAppWindows.size();
7963 for (int i=0; i<NW; i++) {
7964 WindowState w = allAppWindows.get(i);
7965 if (DEBUG_VISIBILITY) Log.v(TAG,
7966 "performing show on: " + w);
7967 w.performShowLocked();
7968 }
7969 }
Romain Guy06882f82009-06-10 13:36:04 -07007970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 // This must be called while inside a transaction.
7972 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7973 if (!mDisplayFrozen) {
7974 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 if (animation == sDummyAnimation) {
7977 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007978 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007979 // when it is really time to animate, this will be set to
7980 // a real animation and the next call will execute normally.
7981 return false;
7982 }
Romain Guy06882f82009-06-10 13:36:04 -07007983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7985 if (!animating) {
7986 if (DEBUG_ANIM) Log.v(
7987 TAG, "Starting animation in " + this +
7988 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7989 + " scale=" + mTransitionAnimationScale
7990 + " allDrawn=" + allDrawn + " animating=" + animating);
7991 animation.initialize(dw, dh, dw, dh);
7992 animation.setStartTime(currentTime);
7993 animating = true;
7994 }
7995 transformation.clear();
7996 final boolean more = animation.getTransformation(
7997 currentTime, transformation);
7998 if (DEBUG_ANIM) Log.v(
7999 TAG, "Stepped animation in " + this +
8000 ": more=" + more + ", xform=" + transformation);
8001 if (more) {
8002 // we're done!
8003 hasTransformation = true;
8004 return true;
8005 }
8006 if (DEBUG_ANIM) Log.v(
8007 TAG, "Finished animation in " + this +
8008 " @ " + currentTime);
8009 animation = null;
8010 }
8011 } else if (animation != null) {
8012 // If the display is frozen, and there is a pending animation,
8013 // clear it and make sure we run the cleanup code.
8014 animating = true;
8015 animation = null;
8016 }
8017
8018 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 if (!animating) {
8021 return false;
8022 }
8023
8024 clearAnimation();
8025 animating = false;
8026 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8027 moveInputMethodWindowsIfNeededLocked(true);
8028 }
Romain Guy06882f82009-06-10 13:36:04 -07008029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 if (DEBUG_ANIM) Log.v(
8031 TAG, "Animation done in " + this
8032 + ": reportedVisible=" + reportedVisible);
8033
8034 transformation.clear();
8035 if (animLayerAdjustment != 0) {
8036 animLayerAdjustment = 0;
8037 updateLayers();
8038 }
Romain Guy06882f82009-06-10 13:36:04 -07008039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 final int N = windows.size();
8041 for (int i=0; i<N; i++) {
8042 ((WindowState)windows.get(i)).finishExit();
8043 }
8044 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 return false;
8047 }
8048
8049 void updateReportedVisibilityLocked() {
8050 if (appToken == null) {
8051 return;
8052 }
Romain Guy06882f82009-06-10 13:36:04 -07008053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 int numInteresting = 0;
8055 int numVisible = 0;
8056 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8059 final int N = allAppWindows.size();
8060 for (int i=0; i<N; i++) {
8061 WindowState win = allAppWindows.get(i);
8062 if (win == startingWindow || win.mAppFreezing) {
8063 continue;
8064 }
8065 if (DEBUG_VISIBILITY) {
8066 Log.v(TAG, "Win " + win + ": isDisplayed="
8067 + win.isDisplayedLw()
8068 + ", isAnimating=" + win.isAnimating());
8069 if (!win.isDisplayedLw()) {
8070 Log.v(TAG, "Not displayed: s=" + win.mSurface
8071 + " pv=" + win.mPolicyVisibility
8072 + " dp=" + win.mDrawPending
8073 + " cdp=" + win.mCommitDrawPending
8074 + " ah=" + win.mAttachedHidden
8075 + " th="
8076 + (win.mAppToken != null
8077 ? win.mAppToken.hiddenRequested : false)
8078 + " a=" + win.mAnimating);
8079 }
8080 }
8081 numInteresting++;
8082 if (win.isDisplayedLw()) {
8083 if (!win.isAnimating()) {
8084 numVisible++;
8085 }
8086 nowGone = false;
8087 } else if (win.isAnimating()) {
8088 nowGone = false;
8089 }
8090 }
Romain Guy06882f82009-06-10 13:36:04 -07008091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8093 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8094 + numInteresting + " visible=" + numVisible);
8095 if (nowVisible != reportedVisible) {
8096 if (DEBUG_VISIBILITY) Log.v(
8097 TAG, "Visibility changed in " + this
8098 + ": vis=" + nowVisible);
8099 reportedVisible = nowVisible;
8100 Message m = mH.obtainMessage(
8101 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8102 nowVisible ? 1 : 0,
8103 nowGone ? 1 : 0,
8104 this);
8105 mH.sendMessage(m);
8106 }
8107 }
Romain Guy06882f82009-06-10 13:36:04 -07008108
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008109 WindowState findMainWindow() {
8110 int j = windows.size();
8111 while (j > 0) {
8112 j--;
8113 WindowState win = windows.get(j);
8114 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8115 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8116 return win;
8117 }
8118 }
8119 return null;
8120 }
8121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 void dump(PrintWriter pw, String prefix) {
8123 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008124 if (appToken != null) {
8125 pw.print(prefix); pw.println("app=true");
8126 }
8127 if (allAppWindows.size() > 0) {
8128 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8129 }
8130 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008131 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008132 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8133 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8134 pw.print(" clientHidden="); pw.print(clientHidden);
8135 pw.print(" willBeHidden="); pw.print(willBeHidden);
8136 pw.print(" reportedVisible="); pw.println(reportedVisible);
8137 if (paused || freezingScreen) {
8138 pw.print(prefix); pw.print("paused="); pw.print(paused);
8139 pw.print(" freezingScreen="); pw.println(freezingScreen);
8140 }
8141 if (numInterestingWindows != 0 || numDrawnWindows != 0
8142 || inPendingTransaction || allDrawn) {
8143 pw.print(prefix); pw.print("numInterestingWindows=");
8144 pw.print(numInterestingWindows);
8145 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8146 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8147 pw.print(" allDrawn="); pw.println(allDrawn);
8148 }
8149 if (animating || animation != null) {
8150 pw.print(prefix); pw.print("animating="); pw.print(animating);
8151 pw.print(" animation="); pw.println(animation);
8152 }
8153 if (animLayerAdjustment != 0) {
8154 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8155 }
8156 if (hasTransformation) {
8157 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8158 pw.print(" transformation="); transformation.printShortString(pw);
8159 pw.println();
8160 }
8161 if (startingData != null || removed || firstWindowDrawn) {
8162 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8163 pw.print(" removed="); pw.print(removed);
8164 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8165 }
8166 if (startingWindow != null || startingView != null
8167 || startingDisplayed || startingMoved) {
8168 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8169 pw.print(" startingView="); pw.print(startingView);
8170 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8171 pw.print(" startingMoved"); pw.println(startingMoved);
8172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 }
8174
8175 @Override
8176 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008177 if (stringName == null) {
8178 StringBuilder sb = new StringBuilder();
8179 sb.append("AppWindowToken{");
8180 sb.append(Integer.toHexString(System.identityHashCode(this)));
8181 sb.append(" token="); sb.append(token); sb.append('}');
8182 stringName = sb.toString();
8183 }
8184 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008185 }
8186 }
Romain Guy06882f82009-06-10 13:36:04 -07008187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008188 // -------------------------------------------------------------
8189 // DummyAnimation
8190 // -------------------------------------------------------------
8191
8192 // This is an animation that does nothing: it just immediately finishes
8193 // itself every time it is called. It is used as a stub animation in cases
8194 // where we want to synchronize multiple things that may be animating.
8195 static final class DummyAnimation extends Animation {
8196 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8197 return false;
8198 }
8199 }
8200 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202 // -------------------------------------------------------------
8203 // Async Handler
8204 // -------------------------------------------------------------
8205
8206 static final class StartingData {
8207 final String pkg;
8208 final int theme;
8209 final CharSequence nonLocalizedLabel;
8210 final int labelRes;
8211 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8214 int _labelRes, int _icon) {
8215 pkg = _pkg;
8216 theme = _theme;
8217 nonLocalizedLabel = _nonLocalizedLabel;
8218 labelRes = _labelRes;
8219 icon = _icon;
8220 }
8221 }
8222
8223 private final class H extends Handler {
8224 public static final int REPORT_FOCUS_CHANGE = 2;
8225 public static final int REPORT_LOSING_FOCUS = 3;
8226 public static final int ANIMATE = 4;
8227 public static final int ADD_STARTING = 5;
8228 public static final int REMOVE_STARTING = 6;
8229 public static final int FINISHED_STARTING = 7;
8230 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8232 public static final int HOLD_SCREEN_CHANGED = 12;
8233 public static final int APP_TRANSITION_TIMEOUT = 13;
8234 public static final int PERSIST_ANIMATION_SCALE = 14;
8235 public static final int FORCE_GC = 15;
8236 public static final int ENABLE_SCREEN = 16;
8237 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008238 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 public H() {
8243 }
Romain Guy06882f82009-06-10 13:36:04 -07008244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 @Override
8246 public void handleMessage(Message msg) {
8247 switch (msg.what) {
8248 case REPORT_FOCUS_CHANGE: {
8249 WindowState lastFocus;
8250 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 synchronized(mWindowMap) {
8253 lastFocus = mLastFocus;
8254 newFocus = mCurrentFocus;
8255 if (lastFocus == newFocus) {
8256 // Focus is not changing, so nothing to do.
8257 return;
8258 }
8259 mLastFocus = newFocus;
8260 //Log.i(TAG, "Focus moving from " + lastFocus
8261 // + " to " + newFocus);
8262 if (newFocus != null && lastFocus != null
8263 && !newFocus.isDisplayedLw()) {
8264 //Log.i(TAG, "Delaying loss of focus...");
8265 mLosingFocus.add(lastFocus);
8266 lastFocus = null;
8267 }
8268 }
8269
8270 if (lastFocus != newFocus) {
8271 //System.out.println("Changing focus from " + lastFocus
8272 // + " to " + newFocus);
8273 if (newFocus != null) {
8274 try {
8275 //Log.i(TAG, "Gaining focus: " + newFocus);
8276 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8277 } catch (RemoteException e) {
8278 // Ignore if process has died.
8279 }
8280 }
8281
8282 if (lastFocus != null) {
8283 try {
8284 //Log.i(TAG, "Losing focus: " + lastFocus);
8285 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8286 } catch (RemoteException e) {
8287 // Ignore if process has died.
8288 }
8289 }
8290 }
8291 } break;
8292
8293 case REPORT_LOSING_FOCUS: {
8294 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008296 synchronized(mWindowMap) {
8297 losers = mLosingFocus;
8298 mLosingFocus = new ArrayList<WindowState>();
8299 }
8300
8301 final int N = losers.size();
8302 for (int i=0; i<N; i++) {
8303 try {
8304 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8305 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8306 } catch (RemoteException e) {
8307 // Ignore if process has died.
8308 }
8309 }
8310 } break;
8311
8312 case ANIMATE: {
8313 synchronized(mWindowMap) {
8314 mAnimationPending = false;
8315 performLayoutAndPlaceSurfacesLocked();
8316 }
8317 } break;
8318
8319 case ADD_STARTING: {
8320 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8321 final StartingData sd = wtoken.startingData;
8322
8323 if (sd == null) {
8324 // Animation has been canceled... do nothing.
8325 return;
8326 }
Romain Guy06882f82009-06-10 13:36:04 -07008327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8329 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 View view = null;
8332 try {
8333 view = mPolicy.addStartingWindow(
8334 wtoken.token, sd.pkg,
8335 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8336 sd.icon);
8337 } catch (Exception e) {
8338 Log.w(TAG, "Exception when adding starting window", e);
8339 }
8340
8341 if (view != null) {
8342 boolean abort = false;
8343
8344 synchronized(mWindowMap) {
8345 if (wtoken.removed || wtoken.startingData == null) {
8346 // If the window was successfully added, then
8347 // we need to remove it.
8348 if (wtoken.startingWindow != null) {
8349 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8350 "Aborted starting " + wtoken
8351 + ": removed=" + wtoken.removed
8352 + " startingData=" + wtoken.startingData);
8353 wtoken.startingWindow = null;
8354 wtoken.startingData = null;
8355 abort = true;
8356 }
8357 } else {
8358 wtoken.startingView = view;
8359 }
8360 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8361 "Added starting " + wtoken
8362 + ": startingWindow="
8363 + wtoken.startingWindow + " startingView="
8364 + wtoken.startingView);
8365 }
8366
8367 if (abort) {
8368 try {
8369 mPolicy.removeStartingWindow(wtoken.token, view);
8370 } catch (Exception e) {
8371 Log.w(TAG, "Exception when removing starting window", e);
8372 }
8373 }
8374 }
8375 } break;
8376
8377 case REMOVE_STARTING: {
8378 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8379 IBinder token = null;
8380 View view = null;
8381 synchronized (mWindowMap) {
8382 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8383 + wtoken + ": startingWindow="
8384 + wtoken.startingWindow + " startingView="
8385 + wtoken.startingView);
8386 if (wtoken.startingWindow != null) {
8387 view = wtoken.startingView;
8388 token = wtoken.token;
8389 wtoken.startingData = null;
8390 wtoken.startingView = null;
8391 wtoken.startingWindow = null;
8392 }
8393 }
8394 if (view != null) {
8395 try {
8396 mPolicy.removeStartingWindow(token, view);
8397 } catch (Exception e) {
8398 Log.w(TAG, "Exception when removing starting window", e);
8399 }
8400 }
8401 } break;
8402
8403 case FINISHED_STARTING: {
8404 IBinder token = null;
8405 View view = null;
8406 while (true) {
8407 synchronized (mWindowMap) {
8408 final int N = mFinishedStarting.size();
8409 if (N <= 0) {
8410 break;
8411 }
8412 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8413
8414 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8415 "Finished starting " + wtoken
8416 + ": startingWindow=" + wtoken.startingWindow
8417 + " startingView=" + wtoken.startingView);
8418
8419 if (wtoken.startingWindow == null) {
8420 continue;
8421 }
8422
8423 view = wtoken.startingView;
8424 token = wtoken.token;
8425 wtoken.startingData = null;
8426 wtoken.startingView = null;
8427 wtoken.startingWindow = null;
8428 }
8429
8430 try {
8431 mPolicy.removeStartingWindow(token, view);
8432 } catch (Exception e) {
8433 Log.w(TAG, "Exception when removing starting window", e);
8434 }
8435 }
8436 } break;
8437
8438 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8439 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8440
8441 boolean nowVisible = msg.arg1 != 0;
8442 boolean nowGone = msg.arg2 != 0;
8443
8444 try {
8445 if (DEBUG_VISIBILITY) Log.v(
8446 TAG, "Reporting visible in " + wtoken
8447 + " visible=" + nowVisible
8448 + " gone=" + nowGone);
8449 if (nowVisible) {
8450 wtoken.appToken.windowsVisible();
8451 } else {
8452 wtoken.appToken.windowsGone();
8453 }
8454 } catch (RemoteException ex) {
8455 }
8456 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 case WINDOW_FREEZE_TIMEOUT: {
8459 synchronized (mWindowMap) {
8460 Log.w(TAG, "Window freeze timeout expired.");
8461 int i = mWindows.size();
8462 while (i > 0) {
8463 i--;
8464 WindowState w = (WindowState)mWindows.get(i);
8465 if (w.mOrientationChanging) {
8466 w.mOrientationChanging = false;
8467 Log.w(TAG, "Force clearing orientation change: " + w);
8468 }
8469 }
8470 performLayoutAndPlaceSurfacesLocked();
8471 }
8472 break;
8473 }
Romain Guy06882f82009-06-10 13:36:04 -07008474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 case HOLD_SCREEN_CHANGED: {
8476 Session oldHold;
8477 Session newHold;
8478 synchronized (mWindowMap) {
8479 oldHold = mLastReportedHold;
8480 newHold = (Session)msg.obj;
8481 mLastReportedHold = newHold;
8482 }
Romain Guy06882f82009-06-10 13:36:04 -07008483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 if (oldHold != newHold) {
8485 try {
8486 if (oldHold != null) {
8487 mBatteryStats.noteStopWakelock(oldHold.mUid,
8488 "window",
8489 BatteryStats.WAKE_TYPE_WINDOW);
8490 }
8491 if (newHold != null) {
8492 mBatteryStats.noteStartWakelock(newHold.mUid,
8493 "window",
8494 BatteryStats.WAKE_TYPE_WINDOW);
8495 }
8496 } catch (RemoteException e) {
8497 }
8498 }
8499 break;
8500 }
Romain Guy06882f82009-06-10 13:36:04 -07008501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008502 case APP_TRANSITION_TIMEOUT: {
8503 synchronized (mWindowMap) {
8504 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8505 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8506 "*** APP TRANSITION TIMEOUT");
8507 mAppTransitionReady = true;
8508 mAppTransitionTimeout = true;
8509 performLayoutAndPlaceSurfacesLocked();
8510 }
8511 }
8512 break;
8513 }
Romain Guy06882f82009-06-10 13:36:04 -07008514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 case PERSIST_ANIMATION_SCALE: {
8516 Settings.System.putFloat(mContext.getContentResolver(),
8517 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8518 Settings.System.putFloat(mContext.getContentResolver(),
8519 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8520 break;
8521 }
Romain Guy06882f82009-06-10 13:36:04 -07008522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 case FORCE_GC: {
8524 synchronized(mWindowMap) {
8525 if (mAnimationPending) {
8526 // If we are animating, don't do the gc now but
8527 // delay a bit so we don't interrupt the animation.
8528 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8529 2000);
8530 return;
8531 }
8532 // If we are currently rotating the display, it will
8533 // schedule a new message when done.
8534 if (mDisplayFrozen) {
8535 return;
8536 }
8537 mFreezeGcPending = 0;
8538 }
8539 Runtime.getRuntime().gc();
8540 break;
8541 }
Romain Guy06882f82009-06-10 13:36:04 -07008542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 case ENABLE_SCREEN: {
8544 performEnableScreen();
8545 break;
8546 }
Romain Guy06882f82009-06-10 13:36:04 -07008547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 case APP_FREEZE_TIMEOUT: {
8549 synchronized (mWindowMap) {
8550 Log.w(TAG, "App freeze timeout expired.");
8551 int i = mAppTokens.size();
8552 while (i > 0) {
8553 i--;
8554 AppWindowToken tok = mAppTokens.get(i);
8555 if (tok.freezingScreen) {
8556 Log.w(TAG, "Force clearing freeze: " + tok);
8557 unsetAppFreezingScreenLocked(tok, true, true);
8558 }
8559 }
8560 }
8561 break;
8562 }
Romain Guy06882f82009-06-10 13:36:04 -07008563
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008564 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008565 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008566 sendNewConfiguration();
8567 }
8568 break;
8569 }
Romain Guy06882f82009-06-10 13:36:04 -07008570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 }
8572 }
8573 }
8574
8575 // -------------------------------------------------------------
8576 // IWindowManager API
8577 // -------------------------------------------------------------
8578
8579 public IWindowSession openSession(IInputMethodClient client,
8580 IInputContext inputContext) {
8581 if (client == null) throw new IllegalArgumentException("null client");
8582 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8583 return new Session(client, inputContext);
8584 }
8585
8586 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8587 synchronized (mWindowMap) {
8588 // The focus for the client is the window immediately below
8589 // where we would place the input method window.
8590 int idx = findDesiredInputMethodWindowIndexLocked(false);
8591 WindowState imFocus;
8592 if (idx > 0) {
8593 imFocus = (WindowState)mWindows.get(idx-1);
8594 if (imFocus != null) {
8595 if (imFocus.mSession.mClient != null &&
8596 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8597 return true;
8598 }
8599 }
8600 }
8601 }
8602 return false;
8603 }
Romain Guy06882f82009-06-10 13:36:04 -07008604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008605 // -------------------------------------------------------------
8606 // Internals
8607 // -------------------------------------------------------------
8608
8609 final WindowState windowForClientLocked(Session session, IWindow client) {
8610 return windowForClientLocked(session, client.asBinder());
8611 }
Romain Guy06882f82009-06-10 13:36:04 -07008612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 final WindowState windowForClientLocked(Session session, IBinder client) {
8614 WindowState win = mWindowMap.get(client);
8615 if (localLOGV) Log.v(
8616 TAG, "Looking up client " + client + ": " + win);
8617 if (win == null) {
8618 RuntimeException ex = new RuntimeException();
8619 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8620 return null;
8621 }
8622 if (session != null && win.mSession != session) {
8623 RuntimeException ex = new RuntimeException();
8624 Log.w(TAG, "Requested window " + client + " is in session " +
8625 win.mSession + ", not " + session, ex);
8626 return null;
8627 }
8628
8629 return win;
8630 }
8631
Dianne Hackborna8f60182009-09-01 19:01:50 -07008632 final void rebuildAppWindowListLocked() {
8633 int NW = mWindows.size();
8634 int i;
8635
8636 // First remove all existing app windows.
8637 i=0;
8638 while (i < NW) {
8639 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008640 WindowState win = (WindowState)mWindows.remove(i);
8641 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8642 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008643 NW--;
8644 continue;
8645 }
8646 i++;
8647 }
8648
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008649 // First add all of the exiting app tokens... these are no longer
8650 // in the main app list, but still have windows shown. We put them
8651 // in the back because now that the animation is over we no longer
8652 // will care about them.
8653 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008654 i = 0;
8655 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008656 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8657 }
8658
8659 // And add in the still active app tokens in Z order.
8660 NT = mAppTokens.size();
8661 for (int j=0; j<NT; j++) {
8662 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008663 }
8664 }
8665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 private final void assignLayersLocked() {
8667 int N = mWindows.size();
8668 int curBaseLayer = 0;
8669 int curLayer = 0;
8670 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008672 for (i=0; i<N; i++) {
8673 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008674 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8675 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 curLayer += WINDOW_LAYER_MULTIPLIER;
8677 w.mLayer = curLayer;
8678 } else {
8679 curBaseLayer = curLayer = w.mBaseLayer;
8680 w.mLayer = curLayer;
8681 }
8682 if (w.mTargetAppToken != null) {
8683 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8684 } else if (w.mAppToken != null) {
8685 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8686 } else {
8687 w.mAnimLayer = w.mLayer;
8688 }
8689 if (w.mIsImWindow) {
8690 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008691 } else if (w.mIsWallpaper) {
8692 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 }
8694 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8695 + w.mAnimLayer);
8696 //System.out.println(
8697 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8698 }
8699 }
8700
8701 private boolean mInLayout = false;
8702 private final void performLayoutAndPlaceSurfacesLocked() {
8703 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008704 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 throw new RuntimeException("Recursive call!");
8706 }
8707 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8708 return;
8709 }
8710
8711 boolean recoveringMemory = false;
8712 if (mForceRemoves != null) {
8713 recoveringMemory = true;
8714 // Wait a little it for things to settle down, and off we go.
8715 for (int i=0; i<mForceRemoves.size(); i++) {
8716 WindowState ws = mForceRemoves.get(i);
8717 Log.i(TAG, "Force removing: " + ws);
8718 removeWindowInnerLocked(ws.mSession, ws);
8719 }
8720 mForceRemoves = null;
8721 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8722 Object tmp = new Object();
8723 synchronized (tmp) {
8724 try {
8725 tmp.wait(250);
8726 } catch (InterruptedException e) {
8727 }
8728 }
8729 }
Romain Guy06882f82009-06-10 13:36:04 -07008730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 mInLayout = true;
8732 try {
8733 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008735 int i = mPendingRemove.size()-1;
8736 if (i >= 0) {
8737 while (i >= 0) {
8738 WindowState w = mPendingRemove.get(i);
8739 removeWindowInnerLocked(w.mSession, w);
8740 i--;
8741 }
8742 mPendingRemove.clear();
8743
8744 mInLayout = false;
8745 assignLayersLocked();
8746 mLayoutNeeded = true;
8747 performLayoutAndPlaceSurfacesLocked();
8748
8749 } else {
8750 mInLayout = false;
8751 if (mLayoutNeeded) {
8752 requestAnimationLocked(0);
8753 }
8754 }
8755 } catch (RuntimeException e) {
8756 mInLayout = false;
8757 Log.e(TAG, "Unhandled exception while layout out windows", e);
8758 }
8759 }
8760
8761 private final void performLayoutLockedInner() {
8762 final int dw = mDisplay.getWidth();
8763 final int dh = mDisplay.getHeight();
8764
8765 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008766 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008767 int i;
8768
8769 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008770
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008771 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 mPolicy.beginLayoutLw(dw, dh);
8773
8774 // First perform layout of any root windows (not attached
8775 // to another window).
8776 int topAttached = -1;
8777 for (i = N-1; i >= 0; i--) {
8778 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008779
8780 // Don't do layout of a window if it is not visible, or
8781 // soon won't be visible, to avoid wasting time and funky
8782 // changes while a window is animating away.
8783 final AppWindowToken atoken = win.mAppToken;
8784 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008785 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008786 || win.mRootToken.hidden
8787 || (atoken != null && atoken.hiddenRequested)
8788 || !win.mPolicyVisibility
8789 || win.mAttachedHidden
8790 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791
8792 // If this view is GONE, then skip it -- keep the current
8793 // frame, and let the caller know so they can ignore it
8794 // if they want. (We do the normal layout for INVISIBLE
8795 // windows, since that means "perform layout as normal,
8796 // just don't display").
8797 if (!gone || !win.mHaveFrame) {
8798 if (!win.mLayoutAttached) {
8799 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8800 } else {
8801 if (topAttached < 0) topAttached = i;
8802 }
8803 }
8804 }
Romain Guy06882f82009-06-10 13:36:04 -07008805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008806 // Now perform layout of attached windows, which usually
8807 // depend on the position of the window they are attached to.
8808 // XXX does not deal with windows that are attached to windows
8809 // that are themselves attached.
8810 for (i = topAttached; i >= 0; i--) {
8811 WindowState win = (WindowState) mWindows.get(i);
8812
8813 // If this view is GONE, then skip it -- keep the current
8814 // frame, and let the caller know so they can ignore it
8815 // if they want. (We do the normal layout for INVISIBLE
8816 // windows, since that means "perform layout as normal,
8817 // just don't display").
8818 if (win.mLayoutAttached) {
8819 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8820 || !win.mHaveFrame) {
8821 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8822 }
8823 }
8824 }
8825
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008826 if (!mPolicy.finishLayoutLw()) {
8827 mLayoutNeeded = false;
8828 } else if (repeats > 2) {
8829 Log.w(TAG, "Layout repeat aborted after too many iterations");
8830 mLayoutNeeded = false;
8831 } else {
8832 repeats++;
8833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008834 }
8835 }
Romain Guy06882f82009-06-10 13:36:04 -07008836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008837 private final void performLayoutAndPlaceSurfacesLockedInner(
8838 boolean recoveringMemory) {
8839 final long currentTime = SystemClock.uptimeMillis();
8840 final int dw = mDisplay.getWidth();
8841 final int dh = mDisplay.getHeight();
8842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008843 int i;
8844
8845 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008846 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 if (mFxSession == null) {
8849 mFxSession = new SurfaceSession();
8850 }
Romain Guy06882f82009-06-10 13:36:04 -07008851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008852 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8853
8854 // Initialize state of exiting tokens.
8855 for (i=mExitingTokens.size()-1; i>=0; i--) {
8856 mExitingTokens.get(i).hasVisible = false;
8857 }
8858
8859 // Initialize state of exiting applications.
8860 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8861 mExitingAppTokens.get(i).hasVisible = false;
8862 }
8863
8864 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 boolean orientationChangeComplete = true;
8866 Session holdScreen = null;
8867 float screenBrightness = -1;
8868 boolean focusDisplayed = false;
8869 boolean animating = false;
8870
8871 Surface.openTransaction();
8872 try {
8873 boolean restart;
8874
8875 do {
8876 final int transactionSequence = ++mTransactionSequence;
8877
8878 // Update animations of all applications, including those
8879 // associated with exiting/removed apps
8880 boolean tokensAnimating = false;
8881 final int NAT = mAppTokens.size();
8882 for (i=0; i<NAT; i++) {
8883 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8884 tokensAnimating = true;
8885 }
8886 }
8887 final int NEAT = mExitingAppTokens.size();
8888 for (i=0; i<NEAT; i++) {
8889 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8890 tokensAnimating = true;
8891 }
8892 }
8893
8894 animating = tokensAnimating;
8895 restart = false;
8896
8897 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008898 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008899
8900 mPolicy.beginAnimationLw(dw, dh);
8901
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008902 final int N = mWindows.size();
8903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 for (i=N-1; i>=0; i--) {
8905 WindowState w = (WindowState)mWindows.get(i);
8906
8907 final WindowManager.LayoutParams attrs = w.mAttrs;
8908
8909 if (w.mSurface != null) {
8910 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008911 if (w.commitFinishDrawingLocked(currentTime)) {
8912 if ((w.mAttrs.flags
8913 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008914 if (DEBUG_WALLPAPER) Log.v(TAG,
8915 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008916 wallpaperMayChange = true;
8917 }
8918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8920 animating = true;
8921 //w.dump(" ");
8922 }
8923
8924 mPolicy.animatingWindowLw(w, attrs);
8925 }
8926
8927 final AppWindowToken atoken = w.mAppToken;
8928 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8929 if (atoken.lastTransactionSequence != transactionSequence) {
8930 atoken.lastTransactionSequence = transactionSequence;
8931 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8932 atoken.startingDisplayed = false;
8933 }
8934 if ((w.isOnScreen() || w.mAttrs.type
8935 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8936 && !w.mExiting && !w.mDestroying) {
8937 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8938 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8939 + w.isDisplayedLw()
8940 + ", isAnimating=" + w.isAnimating());
8941 if (!w.isDisplayedLw()) {
8942 Log.v(TAG, "Not displayed: s=" + w.mSurface
8943 + " pv=" + w.mPolicyVisibility
8944 + " dp=" + w.mDrawPending
8945 + " cdp=" + w.mCommitDrawPending
8946 + " ah=" + w.mAttachedHidden
8947 + " th=" + atoken.hiddenRequested
8948 + " a=" + w.mAnimating);
8949 }
8950 }
8951 if (w != atoken.startingWindow) {
8952 if (!atoken.freezingScreen || !w.mAppFreezing) {
8953 atoken.numInterestingWindows++;
8954 if (w.isDisplayedLw()) {
8955 atoken.numDrawnWindows++;
8956 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8957 "tokenMayBeDrawn: " + atoken
8958 + " freezingScreen=" + atoken.freezingScreen
8959 + " mAppFreezing=" + w.mAppFreezing);
8960 tokenMayBeDrawn = true;
8961 }
8962 }
8963 } else if (w.isDisplayedLw()) {
8964 atoken.startingDisplayed = true;
8965 }
8966 }
8967 } else if (w.mReadyToShow) {
8968 w.performShowLocked();
8969 }
8970 }
8971
8972 if (mPolicy.finishAnimationLw()) {
8973 restart = true;
8974 }
8975
8976 if (tokenMayBeDrawn) {
8977 // See if any windows have been drawn, so they (and others
8978 // associated with them) can now be shown.
8979 final int NT = mTokenList.size();
8980 for (i=0; i<NT; i++) {
8981 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8982 if (wtoken == null) {
8983 continue;
8984 }
8985 if (wtoken.freezingScreen) {
8986 int numInteresting = wtoken.numInterestingWindows;
8987 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8988 if (DEBUG_VISIBILITY) Log.v(TAG,
8989 "allDrawn: " + wtoken
8990 + " interesting=" + numInteresting
8991 + " drawn=" + wtoken.numDrawnWindows);
8992 wtoken.showAllWindowsLocked();
8993 unsetAppFreezingScreenLocked(wtoken, false, true);
8994 orientationChangeComplete = true;
8995 }
8996 } else if (!wtoken.allDrawn) {
8997 int numInteresting = wtoken.numInterestingWindows;
8998 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8999 if (DEBUG_VISIBILITY) Log.v(TAG,
9000 "allDrawn: " + wtoken
9001 + " interesting=" + numInteresting
9002 + " drawn=" + wtoken.numDrawnWindows);
9003 wtoken.allDrawn = true;
9004 restart = true;
9005
9006 // We can now show all of the drawn windows!
9007 if (!mOpeningApps.contains(wtoken)) {
9008 wtoken.showAllWindowsLocked();
9009 }
9010 }
9011 }
9012 }
9013 }
9014
9015 // If we are ready to perform an app transition, check through
9016 // all of the app tokens to be shown and see if they are ready
9017 // to go.
9018 if (mAppTransitionReady) {
9019 int NN = mOpeningApps.size();
9020 boolean goodToGo = true;
9021 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9022 "Checking " + NN + " opening apps (frozen="
9023 + mDisplayFrozen + " timeout="
9024 + mAppTransitionTimeout + ")...");
9025 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9026 // If the display isn't frozen, wait to do anything until
9027 // all of the apps are ready. Otherwise just go because
9028 // we'll unfreeze the display when everyone is ready.
9029 for (i=0; i<NN && goodToGo; i++) {
9030 AppWindowToken wtoken = mOpeningApps.get(i);
9031 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9032 "Check opening app" + wtoken + ": allDrawn="
9033 + wtoken.allDrawn + " startingDisplayed="
9034 + wtoken.startingDisplayed);
9035 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9036 && !wtoken.startingMoved) {
9037 goodToGo = false;
9038 }
9039 }
9040 }
9041 if (goodToGo) {
9042 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9043 int transit = mNextAppTransition;
9044 if (mSkipAppTransitionAnimation) {
9045 transit = WindowManagerPolicy.TRANSIT_NONE;
9046 }
9047 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9048 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009049 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 mAppTransitionTimeout = false;
9051 mStartingIconInTransition = false;
9052 mSkipAppTransitionAnimation = false;
9053
9054 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9055
Dianne Hackborna8f60182009-09-01 19:01:50 -07009056 // If there are applications waiting to come to the
9057 // top of the stack, now is the time to move their windows.
9058 // (Note that we don't do apps going to the bottom
9059 // here -- we want to keep their windows in the old
9060 // Z-order until the animation completes.)
9061 if (mToTopApps.size() > 0) {
9062 NN = mAppTokens.size();
9063 for (i=0; i<NN; i++) {
9064 AppWindowToken wtoken = mAppTokens.get(i);
9065 if (wtoken.sendingToTop) {
9066 wtoken.sendingToTop = false;
9067 moveAppWindowsLocked(wtoken, NN, false);
9068 }
9069 }
9070 mToTopApps.clear();
9071 }
9072
Dianne Hackborn25994b42009-09-04 14:21:19 -07009073 WindowState oldWallpaper = mWallpaperTarget;
9074
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009075 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009076 wallpaperMayChange = false;
9077
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009078 // The top-most window will supply the layout params,
9079 // and we will determine it below.
9080 LayoutParams animLp = null;
9081 int bestAnimLayer = -1;
9082
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009083 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009084 "New wallpaper target=" + mWallpaperTarget
9085 + ", lower target=" + mLowerWallpaperTarget
9086 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009087 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009088 // Do a first pass through the tokens for two
9089 // things:
9090 // (1) Determine if both the closing and opening
9091 // app token sets are wallpaper targets, in which
9092 // case special animations are needed
9093 // (since the wallpaper needs to stay static
9094 // behind them).
9095 // (2) Find the layout params of the top-most
9096 // application window in the tokens, which is
9097 // what will control the animation theme.
9098 final int NC = mClosingApps.size();
9099 NN = NC + mOpeningApps.size();
9100 for (i=0; i<NN; i++) {
9101 AppWindowToken wtoken;
9102 int mode;
9103 if (i < NC) {
9104 wtoken = mClosingApps.get(i);
9105 mode = 1;
9106 } else {
9107 wtoken = mOpeningApps.get(i-NC);
9108 mode = 2;
9109 }
9110 if (mLowerWallpaperTarget != null) {
9111 if (mLowerWallpaperTarget.mAppToken == wtoken
9112 || mUpperWallpaperTarget.mAppToken == wtoken) {
9113 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009114 }
9115 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009116 if (wtoken.appFullscreen) {
9117 WindowState ws = wtoken.findMainWindow();
9118 if (ws != null) {
9119 // If this is a compatibility mode
9120 // window, we will always use its anim.
9121 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9122 animLp = ws.mAttrs;
9123 bestAnimLayer = Integer.MAX_VALUE;
9124 } else if (ws.mLayer > bestAnimLayer) {
9125 animLp = ws.mAttrs;
9126 bestAnimLayer = ws.mLayer;
9127 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009128 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009129 }
9130 }
9131
Dianne Hackborn25994b42009-09-04 14:21:19 -07009132 if (foundWallpapers == 3) {
9133 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9134 "Wallpaper animation!");
9135 switch (transit) {
9136 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9137 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9138 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9139 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9140 break;
9141 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9142 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9143 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9144 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9145 break;
9146 }
9147 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9148 "New transit: " + transit);
9149 } else if (oldWallpaper != null) {
9150 // We are transitioning from an activity with
9151 // a wallpaper to one without.
9152 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9153 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9154 "New transit away from wallpaper: " + transit);
9155 } else if (mWallpaperTarget != null) {
9156 // We are transitioning from an activity without
9157 // a wallpaper to now showing the wallpaper
9158 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9159 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9160 "New transit into wallpaper: " + transit);
9161 }
9162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009163 NN = mOpeningApps.size();
9164 for (i=0; i<NN; i++) {
9165 AppWindowToken wtoken = mOpeningApps.get(i);
9166 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9167 "Now opening app" + wtoken);
9168 wtoken.reportedVisible = false;
9169 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009170 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009171 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009173 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 wtoken.showAllWindowsLocked();
9175 }
9176 NN = mClosingApps.size();
9177 for (i=0; i<NN; i++) {
9178 AppWindowToken wtoken = mClosingApps.get(i);
9179 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9180 "Now closing app" + wtoken);
9181 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009182 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009183 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009185 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 // Force the allDrawn flag, because we want to start
9187 // this guy's animations regardless of whether it's
9188 // gotten drawn.
9189 wtoken.allDrawn = true;
9190 }
9191
9192 mOpeningApps.clear();
9193 mClosingApps.clear();
9194
9195 // This has changed the visibility of windows, so perform
9196 // a new layout to get them all up-to-date.
9197 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009198 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9199 assignLayersLocked();
9200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009201 performLayoutLockedInner();
9202 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9203
9204 restart = true;
9205 }
9206 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009207
Dianne Hackborna8f60182009-09-01 19:01:50 -07009208 if (!animating && mAppTransitionRunning) {
9209 // We have finished the animation of an app transition. To do
9210 // this, we have delayed a lot of operations like showing and
9211 // hiding apps, moving apps in Z-order, etc. The app token list
9212 // reflects the correct Z-order, but the window list may now
9213 // be out of sync with it. So here we will just rebuild the
9214 // entire app window list. Fun!
9215 mAppTransitionRunning = false;
9216 // Clear information about apps that were moving.
9217 mToBottomApps.clear();
9218
9219 rebuildAppWindowListLocked();
9220 restart = true;
9221 moveInputMethodWindowsIfNeededLocked(false);
9222 wallpaperMayChange = true;
9223 mLayoutNeeded = true;
9224 }
9225
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009226 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009227 if (DEBUG_WALLPAPER) Log.v(TAG,
9228 "Wallpaper may change! Adjusting");
9229 int adjResult = adjustWallpaperWindowsLocked();
9230 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9231 if (DEBUG_WALLPAPER) Log.v(TAG,
9232 "Wallpaper layer changed: assigning layers + relayout");
9233 restart = true;
9234 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009235 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009236 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9237 if (DEBUG_WALLPAPER) Log.v(TAG,
9238 "Wallpaper visibility changed: relayout");
9239 restart = true;
9240 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009241 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009242 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009243 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009244 performLayoutLockedInner();
9245 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009246 }
9247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009248 } while (restart);
9249
9250 // THIRD LOOP: Update the surfaces of all windows.
9251
9252 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9253
9254 boolean obscured = false;
9255 boolean blurring = false;
9256 boolean dimming = false;
9257 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009258 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009259 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009261 final int N = mWindows.size();
9262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 for (i=N-1; i>=0; i--) {
9264 WindowState w = (WindowState)mWindows.get(i);
9265
9266 boolean displayed = false;
9267 final WindowManager.LayoutParams attrs = w.mAttrs;
9268 final int attrFlags = attrs.flags;
9269
9270 if (w.mSurface != null) {
9271 w.computeShownFrameLocked();
9272 if (localLOGV) Log.v(
9273 TAG, "Placing surface #" + i + " " + w.mSurface
9274 + ": new=" + w.mShownFrame + ", old="
9275 + w.mLastShownFrame);
9276
9277 boolean resize;
9278 int width, height;
9279 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9280 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9281 w.mLastRequestedHeight != w.mRequestedHeight;
9282 // for a scaled surface, we just want to use
9283 // the requested size.
9284 width = w.mRequestedWidth;
9285 height = w.mRequestedHeight;
9286 w.mLastRequestedWidth = width;
9287 w.mLastRequestedHeight = height;
9288 w.mLastShownFrame.set(w.mShownFrame);
9289 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009290 if (SHOW_TRANSACTIONS) Log.i(
9291 TAG, " SURFACE " + w.mSurface
9292 + ": POS " + w.mShownFrame.left
9293 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9295 } catch (RuntimeException e) {
9296 Log.w(TAG, "Error positioning surface in " + w, e);
9297 if (!recoveringMemory) {
9298 reclaimSomeSurfaceMemoryLocked(w, "position");
9299 }
9300 }
9301 } else {
9302 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9303 width = w.mShownFrame.width();
9304 height = w.mShownFrame.height();
9305 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 }
9307
9308 if (resize) {
9309 if (width < 1) width = 1;
9310 if (height < 1) height = 1;
9311 if (w.mSurface != null) {
9312 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009313 if (SHOW_TRANSACTIONS) Log.i(
9314 TAG, " SURFACE " + w.mSurface + ": POS "
9315 + w.mShownFrame.left + ","
9316 + w.mShownFrame.top + " SIZE "
9317 + w.mShownFrame.width() + "x"
9318 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 w.mSurface.setSize(width, height);
9320 w.mSurface.setPosition(w.mShownFrame.left,
9321 w.mShownFrame.top);
9322 } catch (RuntimeException e) {
9323 // If something goes wrong with the surface (such
9324 // as running out of memory), don't take down the
9325 // entire system.
9326 Log.e(TAG, "Failure updating surface of " + w
9327 + "size=(" + width + "x" + height
9328 + "), pos=(" + w.mShownFrame.left
9329 + "," + w.mShownFrame.top + ")", e);
9330 if (!recoveringMemory) {
9331 reclaimSomeSurfaceMemoryLocked(w, "size");
9332 }
9333 }
9334 }
9335 }
9336 if (!w.mAppFreezing) {
9337 w.mContentInsetsChanged =
9338 !w.mLastContentInsets.equals(w.mContentInsets);
9339 w.mVisibleInsetsChanged =
9340 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009341 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009342 || w.mContentInsetsChanged
9343 || w.mVisibleInsetsChanged) {
9344 w.mLastFrame.set(w.mFrame);
9345 w.mLastContentInsets.set(w.mContentInsets);
9346 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009347 // If the screen is currently frozen, then keep
9348 // it frozen until this window draws at its new
9349 // orientation.
9350 if (mDisplayFrozen) {
9351 if (DEBUG_ORIENTATION) Log.v(TAG,
9352 "Resizing while display frozen: " + w);
9353 w.mOrientationChanging = true;
9354 if (mWindowsFreezingScreen) {
9355 mWindowsFreezingScreen = true;
9356 // XXX should probably keep timeout from
9357 // when we first froze the display.
9358 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9359 mH.sendMessageDelayed(mH.obtainMessage(
9360 H.WINDOW_FREEZE_TIMEOUT), 2000);
9361 }
9362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 // If the orientation is changing, then we need to
9364 // hold off on unfreezing the display until this
9365 // window has been redrawn; to do that, we need
9366 // to go through the process of getting informed
9367 // by the application when it has finished drawing.
9368 if (w.mOrientationChanging) {
9369 if (DEBUG_ORIENTATION) Log.v(TAG,
9370 "Orientation start waiting for draw in "
9371 + w + ", surface " + w.mSurface);
9372 w.mDrawPending = true;
9373 w.mCommitDrawPending = false;
9374 w.mReadyToShow = false;
9375 if (w.mAppToken != null) {
9376 w.mAppToken.allDrawn = false;
9377 }
9378 }
Romain Guy06882f82009-06-10 13:36:04 -07009379 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 "Resizing window " + w + " to " + w.mFrame);
9381 mResizingWindows.add(w);
9382 } else if (w.mOrientationChanging) {
9383 if (!w.mDrawPending && !w.mCommitDrawPending) {
9384 if (DEBUG_ORIENTATION) Log.v(TAG,
9385 "Orientation not waiting for draw in "
9386 + w + ", surface " + w.mSurface);
9387 w.mOrientationChanging = false;
9388 }
9389 }
9390 }
9391
9392 if (w.mAttachedHidden) {
9393 if (!w.mLastHidden) {
9394 //dump();
9395 w.mLastHidden = true;
9396 if (SHOW_TRANSACTIONS) Log.i(
9397 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9398 if (w.mSurface != null) {
9399 try {
9400 w.mSurface.hide();
9401 } catch (RuntimeException e) {
9402 Log.w(TAG, "Exception hiding surface in " + w);
9403 }
9404 }
9405 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9406 }
9407 // If we are waiting for this window to handle an
9408 // orientation change, well, it is hidden, so
9409 // doesn't really matter. Note that this does
9410 // introduce a potential glitch if the window
9411 // becomes unhidden before it has drawn for the
9412 // new orientation.
9413 if (w.mOrientationChanging) {
9414 w.mOrientationChanging = false;
9415 if (DEBUG_ORIENTATION) Log.v(TAG,
9416 "Orientation change skips hidden " + w);
9417 }
9418 } else if (!w.isReadyForDisplay()) {
9419 if (!w.mLastHidden) {
9420 //dump();
9421 w.mLastHidden = true;
9422 if (SHOW_TRANSACTIONS) Log.i(
9423 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9424 if (w.mSurface != null) {
9425 try {
9426 w.mSurface.hide();
9427 } catch (RuntimeException e) {
9428 Log.w(TAG, "Exception exception hiding surface in " + w);
9429 }
9430 }
9431 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9432 }
9433 // If we are waiting for this window to handle an
9434 // orientation change, well, it is hidden, so
9435 // doesn't really matter. Note that this does
9436 // introduce a potential glitch if the window
9437 // becomes unhidden before it has drawn for the
9438 // new orientation.
9439 if (w.mOrientationChanging) {
9440 w.mOrientationChanging = false;
9441 if (DEBUG_ORIENTATION) Log.v(TAG,
9442 "Orientation change skips hidden " + w);
9443 }
9444 } else if (w.mLastLayer != w.mAnimLayer
9445 || w.mLastAlpha != w.mShownAlpha
9446 || w.mLastDsDx != w.mDsDx
9447 || w.mLastDtDx != w.mDtDx
9448 || w.mLastDsDy != w.mDsDy
9449 || w.mLastDtDy != w.mDtDy
9450 || w.mLastHScale != w.mHScale
9451 || w.mLastVScale != w.mVScale
9452 || w.mLastHidden) {
9453 displayed = true;
9454 w.mLastAlpha = w.mShownAlpha;
9455 w.mLastLayer = w.mAnimLayer;
9456 w.mLastDsDx = w.mDsDx;
9457 w.mLastDtDx = w.mDtDx;
9458 w.mLastDsDy = w.mDsDy;
9459 w.mLastDtDy = w.mDtDy;
9460 w.mLastHScale = w.mHScale;
9461 w.mLastVScale = w.mVScale;
9462 if (SHOW_TRANSACTIONS) Log.i(
9463 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009464 + w.mShownAlpha + " layer=" + w.mAnimLayer
9465 + " matrix=[" + (w.mDsDx*w.mHScale)
9466 + "," + (w.mDtDx*w.mVScale)
9467 + "][" + (w.mDsDy*w.mHScale)
9468 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009469 if (w.mSurface != null) {
9470 try {
9471 w.mSurface.setAlpha(w.mShownAlpha);
9472 w.mSurface.setLayer(w.mAnimLayer);
9473 w.mSurface.setMatrix(
9474 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9475 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9476 } catch (RuntimeException e) {
9477 Log.w(TAG, "Error updating surface in " + w, e);
9478 if (!recoveringMemory) {
9479 reclaimSomeSurfaceMemoryLocked(w, "update");
9480 }
9481 }
9482 }
9483
9484 if (w.mLastHidden && !w.mDrawPending
9485 && !w.mCommitDrawPending
9486 && !w.mReadyToShow) {
9487 if (SHOW_TRANSACTIONS) Log.i(
9488 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9489 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9490 + " during relayout");
9491 if (showSurfaceRobustlyLocked(w)) {
9492 w.mHasDrawn = true;
9493 w.mLastHidden = false;
9494 } else {
9495 w.mOrientationChanging = false;
9496 }
9497 }
9498 if (w.mSurface != null) {
9499 w.mToken.hasVisible = true;
9500 }
9501 } else {
9502 displayed = true;
9503 }
9504
9505 if (displayed) {
9506 if (!covered) {
9507 if (attrs.width == LayoutParams.FILL_PARENT
9508 && attrs.height == LayoutParams.FILL_PARENT) {
9509 covered = true;
9510 }
9511 }
9512 if (w.mOrientationChanging) {
9513 if (w.mDrawPending || w.mCommitDrawPending) {
9514 orientationChangeComplete = false;
9515 if (DEBUG_ORIENTATION) Log.v(TAG,
9516 "Orientation continue waiting for draw in " + w);
9517 } else {
9518 w.mOrientationChanging = false;
9519 if (DEBUG_ORIENTATION) Log.v(TAG,
9520 "Orientation change complete in " + w);
9521 }
9522 }
9523 w.mToken.hasVisible = true;
9524 }
9525 } else if (w.mOrientationChanging) {
9526 if (DEBUG_ORIENTATION) Log.v(TAG,
9527 "Orientation change skips hidden " + w);
9528 w.mOrientationChanging = false;
9529 }
9530
9531 final boolean canBeSeen = w.isDisplayedLw();
9532
9533 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9534 focusDisplayed = true;
9535 }
9536
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009537 final boolean obscuredChanged = w.mObscured != obscured;
9538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009540 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541 if (w.mSurface != null) {
9542 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9543 holdScreen = w.mSession;
9544 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009545 if (!syswin && w.mAttrs.screenBrightness >= 0
9546 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 screenBrightness = w.mAttrs.screenBrightness;
9548 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009549 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9550 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9551 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9552 syswin = true;
9553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009554 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009555
Dianne Hackborn25994b42009-09-04 14:21:19 -07009556 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9557 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 // This window completely covers everything behind it,
9559 // so we want to leave all of them as unblurred (for
9560 // performance reasons).
9561 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009562 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9563 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009564 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009565 obscured = true;
9566 if (mBackgroundFillerSurface == null) {
9567 try {
9568 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9569 0, dw, dh,
9570 PixelFormat.OPAQUE,
9571 Surface.FX_SURFACE_NORMAL);
9572 } catch (Exception e) {
9573 Log.e(TAG, "Exception creating filler surface", e);
9574 }
9575 }
9576 try {
9577 mBackgroundFillerSurface.setPosition(0, 0);
9578 mBackgroundFillerSurface.setSize(dw, dh);
9579 // Using the same layer as Dim because they will never be shown at the
9580 // same time.
9581 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9582 mBackgroundFillerSurface.show();
9583 } catch (RuntimeException e) {
9584 Log.e(TAG, "Exception showing filler surface");
9585 }
9586 backgroundFillerShown = true;
9587 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 } else if (canBeSeen && !obscured &&
9589 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9590 if (localLOGV) Log.v(TAG, "Win " + w
9591 + ": blurring=" + blurring
9592 + " obscured=" + obscured
9593 + " displayed=" + displayed);
9594 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9595 if (!dimming) {
9596 //Log.i(TAG, "DIM BEHIND: " + w);
9597 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009598 if (mDimAnimator == null) {
9599 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009600 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009601 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009603 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009604 }
9605 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9606 if (!blurring) {
9607 //Log.i(TAG, "BLUR BEHIND: " + w);
9608 blurring = true;
9609 mBlurShown = true;
9610 if (mBlurSurface == null) {
9611 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9612 + mBlurSurface + ": CREATE");
9613 try {
Romain Guy06882f82009-06-10 13:36:04 -07009614 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 -1, 16, 16,
9616 PixelFormat.OPAQUE,
9617 Surface.FX_SURFACE_BLUR);
9618 } catch (Exception e) {
9619 Log.e(TAG, "Exception creating Blur surface", e);
9620 }
9621 }
9622 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9623 + mBlurSurface + ": SHOW pos=(0,0) (" +
9624 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9625 if (mBlurSurface != null) {
9626 mBlurSurface.setPosition(0, 0);
9627 mBlurSurface.setSize(dw, dh);
9628 try {
9629 mBlurSurface.show();
9630 } catch (RuntimeException e) {
9631 Log.w(TAG, "Failure showing blur surface", e);
9632 }
9633 }
9634 }
9635 mBlurSurface.setLayer(w.mAnimLayer-2);
9636 }
9637 }
9638 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009639
9640 if (obscuredChanged && mWallpaperTarget == w) {
9641 // This is the wallpaper target and its obscured state
9642 // changed... make sure the current wallaper's visibility
9643 // has been updated accordingly.
9644 updateWallpaperVisibilityLocked();
9645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009647
9648 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9649 mBackgroundFillerShown = false;
9650 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9651 try {
9652 mBackgroundFillerSurface.hide();
9653 } catch (RuntimeException e) {
9654 Log.e(TAG, "Exception hiding filler surface", e);
9655 }
9656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009657
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009658 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9659 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 }
Romain Guy06882f82009-06-10 13:36:04 -07009661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 if (!blurring && mBlurShown) {
9663 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9664 + ": HIDE");
9665 try {
9666 mBlurSurface.hide();
9667 } catch (IllegalArgumentException e) {
9668 Log.w(TAG, "Illegal argument exception hiding blur surface");
9669 }
9670 mBlurShown = false;
9671 }
9672
9673 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9674 } catch (RuntimeException e) {
9675 Log.e(TAG, "Unhandled exception in Window Manager", e);
9676 }
9677
9678 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9681 "With display frozen, orientationChangeComplete="
9682 + orientationChangeComplete);
9683 if (orientationChangeComplete) {
9684 if (mWindowsFreezingScreen) {
9685 mWindowsFreezingScreen = false;
9686 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9687 }
9688 if (mAppsFreezingScreen == 0) {
9689 stopFreezingDisplayLocked();
9690 }
9691 }
Romain Guy06882f82009-06-10 13:36:04 -07009692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 i = mResizingWindows.size();
9694 if (i > 0) {
9695 do {
9696 i--;
9697 WindowState win = mResizingWindows.get(i);
9698 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009699 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9700 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009701 win.mClient.resized(win.mFrame.width(),
9702 win.mFrame.height(), win.mLastContentInsets,
9703 win.mLastVisibleInsets, win.mDrawPending);
9704 win.mContentInsetsChanged = false;
9705 win.mVisibleInsetsChanged = false;
9706 } catch (RemoteException e) {
9707 win.mOrientationChanging = false;
9708 }
9709 } while (i > 0);
9710 mResizingWindows.clear();
9711 }
Romain Guy06882f82009-06-10 13:36:04 -07009712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009713 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009714 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 i = mDestroySurface.size();
9716 if (i > 0) {
9717 do {
9718 i--;
9719 WindowState win = mDestroySurface.get(i);
9720 win.mDestroying = false;
9721 if (mInputMethodWindow == win) {
9722 mInputMethodWindow = null;
9723 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009724 if (win == mWallpaperTarget) {
9725 wallpaperDestroyed = true;
9726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009727 win.destroySurfaceLocked();
9728 } while (i > 0);
9729 mDestroySurface.clear();
9730 }
9731
9732 // Time to remove any exiting tokens?
9733 for (i=mExitingTokens.size()-1; i>=0; i--) {
9734 WindowToken token = mExitingTokens.get(i);
9735 if (!token.hasVisible) {
9736 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009737 if (token.windowType == TYPE_WALLPAPER) {
9738 mWallpaperTokens.remove(token);
9739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009740 }
9741 }
9742
9743 // Time to remove any exiting applications?
9744 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9745 AppWindowToken token = mExitingAppTokens.get(i);
9746 if (!token.hasVisible && !mClosingApps.contains(token)) {
9747 mAppTokens.remove(token);
9748 mExitingAppTokens.remove(i);
9749 }
9750 }
9751
Dianne Hackborna8f60182009-09-01 19:01:50 -07009752 boolean needRelayout = false;
9753
9754 if (!animating && mAppTransitionRunning) {
9755 // We have finished the animation of an app transition. To do
9756 // this, we have delayed a lot of operations like showing and
9757 // hiding apps, moving apps in Z-order, etc. The app token list
9758 // reflects the correct Z-order, but the window list may now
9759 // be out of sync with it. So here we will just rebuild the
9760 // entire app window list. Fun!
9761 mAppTransitionRunning = false;
9762 needRelayout = true;
9763 rebuildAppWindowListLocked();
9764 // Clear information about apps that were moving.
9765 mToBottomApps.clear();
9766 }
9767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009768 if (focusDisplayed) {
9769 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9770 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009771 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009772 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009773 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009774 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009775 requestAnimationLocked(0);
9776 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9778 }
9779 mQueue.setHoldScreenLocked(holdScreen != null);
9780 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9781 mPowerManager.setScreenBrightnessOverride(-1);
9782 } else {
9783 mPowerManager.setScreenBrightnessOverride((int)
9784 (screenBrightness * Power.BRIGHTNESS_ON));
9785 }
9786 if (holdScreen != mHoldingScreenOn) {
9787 mHoldingScreenOn = holdScreen;
9788 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9789 mH.sendMessage(m);
9790 }
9791 }
9792
9793 void requestAnimationLocked(long delay) {
9794 if (!mAnimationPending) {
9795 mAnimationPending = true;
9796 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9797 }
9798 }
Romain Guy06882f82009-06-10 13:36:04 -07009799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009800 /**
9801 * Have the surface flinger show a surface, robustly dealing with
9802 * error conditions. In particular, if there is not enough memory
9803 * to show the surface, then we will try to get rid of other surfaces
9804 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009805 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 * @return Returns true if the surface was successfully shown.
9807 */
9808 boolean showSurfaceRobustlyLocked(WindowState win) {
9809 try {
9810 if (win.mSurface != null) {
9811 win.mSurface.show();
9812 }
9813 return true;
9814 } catch (RuntimeException e) {
9815 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9816 }
Romain Guy06882f82009-06-10 13:36:04 -07009817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 return false;
9821 }
Romain Guy06882f82009-06-10 13:36:04 -07009822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009823 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9824 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009826 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9827 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009829 if (mForceRemoves == null) {
9830 mForceRemoves = new ArrayList<WindowState>();
9831 }
Romain Guy06882f82009-06-10 13:36:04 -07009832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 long callingIdentity = Binder.clearCallingIdentity();
9834 try {
9835 // There was some problem... first, do a sanity check of the
9836 // window list to make sure we haven't left any dangling surfaces
9837 // around.
9838 int N = mWindows.size();
9839 boolean leakedSurface = false;
9840 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9841 for (int i=0; i<N; i++) {
9842 WindowState ws = (WindowState)mWindows.get(i);
9843 if (ws.mSurface != null) {
9844 if (!mSessions.contains(ws.mSession)) {
9845 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9846 + ws + " surface=" + ws.mSurface
9847 + " token=" + win.mToken
9848 + " pid=" + ws.mSession.mPid
9849 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009850 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 ws.mSurface = null;
9852 mForceRemoves.add(ws);
9853 i--;
9854 N--;
9855 leakedSurface = true;
9856 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9857 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9858 + ws + " surface=" + ws.mSurface
9859 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009860 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 ws.mSurface = null;
9862 leakedSurface = true;
9863 }
9864 }
9865 }
Romain Guy06882f82009-06-10 13:36:04 -07009866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 boolean killedApps = false;
9868 if (!leakedSurface) {
9869 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9870 SparseIntArray pidCandidates = new SparseIntArray();
9871 for (int i=0; i<N; i++) {
9872 WindowState ws = (WindowState)mWindows.get(i);
9873 if (ws.mSurface != null) {
9874 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9875 }
9876 }
9877 if (pidCandidates.size() > 0) {
9878 int[] pids = new int[pidCandidates.size()];
9879 for (int i=0; i<pids.length; i++) {
9880 pids[i] = pidCandidates.keyAt(i);
9881 }
9882 try {
9883 if (mActivityManager.killPidsForMemory(pids)) {
9884 killedApps = true;
9885 }
9886 } catch (RemoteException e) {
9887 }
9888 }
9889 }
Romain Guy06882f82009-06-10 13:36:04 -07009890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 if (leakedSurface || killedApps) {
9892 // We managed to reclaim some memory, so get rid of the trouble
9893 // surface and ask the app to request another one.
9894 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9895 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009896 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 win.mSurface = null;
9898 }
Romain Guy06882f82009-06-10 13:36:04 -07009899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 try {
9901 win.mClient.dispatchGetNewSurface();
9902 } catch (RemoteException e) {
9903 }
9904 }
9905 } finally {
9906 Binder.restoreCallingIdentity(callingIdentity);
9907 }
9908 }
Romain Guy06882f82009-06-10 13:36:04 -07009909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 private boolean updateFocusedWindowLocked(int mode) {
9911 WindowState newFocus = computeFocusedWindowLocked();
9912 if (mCurrentFocus != newFocus) {
9913 // This check makes sure that we don't already have the focus
9914 // change message pending.
9915 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9916 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9917 if (localLOGV) Log.v(
9918 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9919 final WindowState oldFocus = mCurrentFocus;
9920 mCurrentFocus = newFocus;
9921 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 final WindowState imWindow = mInputMethodWindow;
9924 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009925 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009927 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9928 mLayoutNeeded = true;
9929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9931 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009932 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9933 // Client will do the layout, but we need to assign layers
9934 // for handleNewWindowLocked() below.
9935 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 }
9937 }
Romain Guy06882f82009-06-10 13:36:04 -07009938
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009939 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9940 mKeyWaiter.handleNewWindowLocked(newFocus);
9941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 return true;
9943 }
9944 return false;
9945 }
9946
9947 private WindowState computeFocusedWindowLocked() {
9948 WindowState result = null;
9949 WindowState win;
9950
9951 int i = mWindows.size() - 1;
9952 int nextAppIndex = mAppTokens.size()-1;
9953 WindowToken nextApp = nextAppIndex >= 0
9954 ? mAppTokens.get(nextAppIndex) : null;
9955
9956 while (i >= 0) {
9957 win = (WindowState)mWindows.get(i);
9958
9959 if (localLOGV || DEBUG_FOCUS) Log.v(
9960 TAG, "Looking for focus: " + i
9961 + " = " + win
9962 + ", flags=" + win.mAttrs.flags
9963 + ", canReceive=" + win.canReceiveKeys());
9964
9965 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 // If this window's application has been removed, just skip it.
9968 if (thisApp != null && thisApp.removed) {
9969 i--;
9970 continue;
9971 }
Romain Guy06882f82009-06-10 13:36:04 -07009972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009973 // If there is a focused app, don't allow focus to go to any
9974 // windows below it. If this is an application window, step
9975 // through the app tokens until we find its app.
9976 if (thisApp != null && nextApp != null && thisApp != nextApp
9977 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9978 int origAppIndex = nextAppIndex;
9979 while (nextAppIndex > 0) {
9980 if (nextApp == mFocusedApp) {
9981 // Whoops, we are below the focused app... no focus
9982 // for you!
9983 if (localLOGV || DEBUG_FOCUS) Log.v(
9984 TAG, "Reached focused app: " + mFocusedApp);
9985 return null;
9986 }
9987 nextAppIndex--;
9988 nextApp = mAppTokens.get(nextAppIndex);
9989 if (nextApp == thisApp) {
9990 break;
9991 }
9992 }
9993 if (thisApp != nextApp) {
9994 // Uh oh, the app token doesn't exist! This shouldn't
9995 // happen, but if it does we can get totally hosed...
9996 // so restart at the original app.
9997 nextAppIndex = origAppIndex;
9998 nextApp = mAppTokens.get(nextAppIndex);
9999 }
10000 }
10001
10002 // Dispatch to this window if it is wants key events.
10003 if (win.canReceiveKeys()) {
10004 if (DEBUG_FOCUS) Log.v(
10005 TAG, "Found focus @ " + i + " = " + win);
10006 result = win;
10007 break;
10008 }
10009
10010 i--;
10011 }
10012
10013 return result;
10014 }
10015
10016 private void startFreezingDisplayLocked() {
10017 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010018 // Freezing the display also suspends key event delivery, to
10019 // keep events from going astray while the display is reconfigured.
10020 // If someone has changed orientation again while the screen is
10021 // still frozen, the events will continue to be blocked while the
10022 // successive orientation change is processed. To prevent spurious
10023 // ANRs, we reset the event dispatch timeout in this case.
10024 synchronized (mKeyWaiter) {
10025 mKeyWaiter.mWasFrozen = true;
10026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027 return;
10028 }
Romain Guy06882f82009-06-10 13:36:04 -070010029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 long now = SystemClock.uptimeMillis();
10033 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10034 if (mFreezeGcPending != 0) {
10035 if (now > (mFreezeGcPending+1000)) {
10036 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10037 mH.removeMessages(H.FORCE_GC);
10038 Runtime.getRuntime().gc();
10039 mFreezeGcPending = now;
10040 }
10041 } else {
10042 mFreezeGcPending = now;
10043 }
Romain Guy06882f82009-06-10 13:36:04 -070010044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 mDisplayFrozen = true;
10046 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
10047 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
10048 mAppTransitionReady = true;
10049 }
Romain Guy06882f82009-06-10 13:36:04 -070010050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051 if (PROFILE_ORIENTATION) {
10052 File file = new File("/data/system/frozen");
10053 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10054 }
10055 Surface.freezeDisplay(0);
10056 }
Romain Guy06882f82009-06-10 13:36:04 -070010057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 private void stopFreezingDisplayLocked() {
10059 if (!mDisplayFrozen) {
10060 return;
10061 }
Romain Guy06882f82009-06-10 13:36:04 -070010062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 mDisplayFrozen = false;
10064 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10065 if (PROFILE_ORIENTATION) {
10066 Debug.stopMethodTracing();
10067 }
10068 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010069
Chris Tate2ad63a92009-03-25 17:36:48 -070010070 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10071 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 synchronized (mKeyWaiter) {
10073 mKeyWaiter.mWasFrozen = true;
10074 mKeyWaiter.notifyAll();
10075 }
10076
10077 // A little kludge: a lot could have happened while the
10078 // display was frozen, so now that we are coming back we
10079 // do a gc so that any remote references the system
10080 // processes holds on others can be released if they are
10081 // no longer needed.
10082 mH.removeMessages(H.FORCE_GC);
10083 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10084 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010086 mScreenFrozenLock.release();
10087 }
Romain Guy06882f82009-06-10 13:36:04 -070010088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010089 @Override
10090 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10091 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10092 != PackageManager.PERMISSION_GRANTED) {
10093 pw.println("Permission Denial: can't dump WindowManager from from pid="
10094 + Binder.getCallingPid()
10095 + ", uid=" + Binder.getCallingUid());
10096 return;
10097 }
Romain Guy06882f82009-06-10 13:36:04 -070010098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010099 synchronized(mWindowMap) {
10100 pw.println("Current Window Manager state:");
10101 for (int i=mWindows.size()-1; i>=0; i--) {
10102 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010103 pw.print(" Window #"); pw.print(i); pw.print(' ');
10104 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010105 w.dump(pw, " ");
10106 }
10107 if (mInputMethodDialogs.size() > 0) {
10108 pw.println(" ");
10109 pw.println(" Input method dialogs:");
10110 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10111 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010112 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010113 }
10114 }
10115 if (mPendingRemove.size() > 0) {
10116 pw.println(" ");
10117 pw.println(" Remove pending for:");
10118 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10119 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010120 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10121 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 w.dump(pw, " ");
10123 }
10124 }
10125 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10126 pw.println(" ");
10127 pw.println(" Windows force removing:");
10128 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10129 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010130 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10131 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 w.dump(pw, " ");
10133 }
10134 }
10135 if (mDestroySurface.size() > 0) {
10136 pw.println(" ");
10137 pw.println(" Windows waiting to destroy their surface:");
10138 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10139 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010140 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10141 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 w.dump(pw, " ");
10143 }
10144 }
10145 if (mLosingFocus.size() > 0) {
10146 pw.println(" ");
10147 pw.println(" Windows losing focus:");
10148 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10149 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010150 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10151 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 w.dump(pw, " ");
10153 }
10154 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010155 if (mResizingWindows.size() > 0) {
10156 pw.println(" ");
10157 pw.println(" Windows waiting to resize:");
10158 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10159 WindowState w = mResizingWindows.get(i);
10160 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10161 pw.print(w); pw.println(":");
10162 w.dump(pw, " ");
10163 }
10164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010165 if (mSessions.size() > 0) {
10166 pw.println(" ");
10167 pw.println(" All active sessions:");
10168 Iterator<Session> it = mSessions.iterator();
10169 while (it.hasNext()) {
10170 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010171 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172 s.dump(pw, " ");
10173 }
10174 }
10175 if (mTokenMap.size() > 0) {
10176 pw.println(" ");
10177 pw.println(" All tokens:");
10178 Iterator<WindowToken> it = mTokenMap.values().iterator();
10179 while (it.hasNext()) {
10180 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010181 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 token.dump(pw, " ");
10183 }
10184 }
10185 if (mTokenList.size() > 0) {
10186 pw.println(" ");
10187 pw.println(" Window token list:");
10188 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010189 pw.print(" #"); pw.print(i); pw.print(": ");
10190 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 }
10192 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010193 if (mWallpaperTokens.size() > 0) {
10194 pw.println(" ");
10195 pw.println(" Wallpaper tokens:");
10196 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10197 WindowToken token = mWallpaperTokens.get(i);
10198 pw.print(" Wallpaper #"); pw.print(i);
10199 pw.print(' '); pw.print(token); pw.println(':');
10200 token.dump(pw, " ");
10201 }
10202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 if (mAppTokens.size() > 0) {
10204 pw.println(" ");
10205 pw.println(" Application tokens in Z order:");
10206 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010207 pw.print(" App #"); pw.print(i); pw.print(": ");
10208 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010209 }
10210 }
10211 if (mFinishedStarting.size() > 0) {
10212 pw.println(" ");
10213 pw.println(" Finishing start of application tokens:");
10214 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10215 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010216 pw.print(" Finished Starting #"); pw.print(i);
10217 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218 token.dump(pw, " ");
10219 }
10220 }
10221 if (mExitingTokens.size() > 0) {
10222 pw.println(" ");
10223 pw.println(" Exiting tokens:");
10224 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10225 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010226 pw.print(" Exiting #"); pw.print(i);
10227 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010228 token.dump(pw, " ");
10229 }
10230 }
10231 if (mExitingAppTokens.size() > 0) {
10232 pw.println(" ");
10233 pw.println(" Exiting application tokens:");
10234 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10235 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010236 pw.print(" Exiting App #"); pw.print(i);
10237 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010238 token.dump(pw, " ");
10239 }
10240 }
10241 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010242 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10243 pw.print(" mLastFocus="); pw.println(mLastFocus);
10244 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10245 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10246 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010247 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010248 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10249 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10250 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10251 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010252 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10253 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10254 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10255 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10256 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010257 if (mDimAnimator != null) {
10258 mDimAnimator.printTo(pw);
10259 } else {
10260 pw.print( " no DimAnimator ");
10261 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010262 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010263 pw.print(mInputMethodAnimLayerAdjustment);
10264 pw.print(" mWallpaperAnimLayerAdjustment=");
10265 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010266 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10267 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010268 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10269 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10270 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10271 pw.print(" mRotation="); pw.print(mRotation);
10272 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10273 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10274 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10275 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10276 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10277 pw.print(" mNextAppTransition=0x");
10278 pw.print(Integer.toHexString(mNextAppTransition));
10279 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010280 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010281 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10282 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10283 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10284 if (mOpeningApps.size() > 0) {
10285 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10286 }
10287 if (mClosingApps.size() > 0) {
10288 pw.print(" mClosingApps="); pw.println(mClosingApps);
10289 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010290 if (mToTopApps.size() > 0) {
10291 pw.print(" mToTopApps="); pw.println(mToTopApps);
10292 }
10293 if (mToBottomApps.size() > 0) {
10294 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10295 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010296 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10297 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010299 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10300 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10301 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10302 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10303 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10304 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 }
10306 }
10307
10308 public void monitor() {
10309 synchronized (mWindowMap) { }
10310 synchronized (mKeyguardDisabled) { }
10311 synchronized (mKeyWaiter) { }
10312 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010313
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010314 public void virtualKeyFeedback(KeyEvent event) {
10315 mPolicy.keyFeedbackFromInput(event);
10316 }
10317
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010318 /**
10319 * DimAnimator class that controls the dim animation. This holds the surface and
10320 * all state used for dim animation.
10321 */
10322 private static class DimAnimator {
10323 Surface mDimSurface;
10324 boolean mDimShown = false;
10325 float mDimCurrentAlpha;
10326 float mDimTargetAlpha;
10327 float mDimDeltaPerMs;
10328 long mLastDimAnimTime;
10329
10330 DimAnimator (SurfaceSession session) {
10331 if (mDimSurface == null) {
10332 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10333 + mDimSurface + ": CREATE");
10334 try {
10335 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10336 Surface.FX_SURFACE_DIM);
10337 } catch (Exception e) {
10338 Log.e(TAG, "Exception creating Dim surface", e);
10339 }
10340 }
10341 }
10342
10343 /**
10344 * Show the dim surface.
10345 */
10346 void show(int dw, int dh) {
10347 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10348 dw + "x" + dh + ")");
10349 mDimShown = true;
10350 try {
10351 mDimSurface.setPosition(0, 0);
10352 mDimSurface.setSize(dw, dh);
10353 mDimSurface.show();
10354 } catch (RuntimeException e) {
10355 Log.w(TAG, "Failure showing dim surface", e);
10356 }
10357 }
10358
10359 /**
10360 * Set's the dim surface's layer and update dim parameters that will be used in
10361 * {@link updateSurface} after all windows are examined.
10362 */
10363 void updateParameters(WindowState w, long currentTime) {
10364 mDimSurface.setLayer(w.mAnimLayer-1);
10365
10366 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010367 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10368 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010369 if (mDimTargetAlpha != target) {
10370 // If the desired dim level has changed, then
10371 // start an animation to it.
10372 mLastDimAnimTime = currentTime;
10373 long duration = (w.mAnimating && w.mAnimation != null)
10374 ? w.mAnimation.computeDurationHint()
10375 : DEFAULT_DIM_DURATION;
10376 if (target > mDimTargetAlpha) {
10377 // This is happening behind the activity UI,
10378 // so we can make it run a little longer to
10379 // give a stronger impression without disrupting
10380 // the user.
10381 duration *= DIM_DURATION_MULTIPLIER;
10382 }
10383 if (duration < 1) {
10384 // Don't divide by zero
10385 duration = 1;
10386 }
10387 mDimTargetAlpha = target;
10388 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10389 }
10390 }
10391
10392 /**
10393 * Updating the surface's alpha. Returns true if the animation continues, or returns
10394 * false when the animation is finished and the dim surface is hidden.
10395 */
10396 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10397 if (!dimming) {
10398 if (mDimTargetAlpha != 0) {
10399 mLastDimAnimTime = currentTime;
10400 mDimTargetAlpha = 0;
10401 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10402 }
10403 }
10404
10405 boolean animating = false;
10406 if (mLastDimAnimTime != 0) {
10407 mDimCurrentAlpha += mDimDeltaPerMs
10408 * (currentTime-mLastDimAnimTime);
10409 boolean more = true;
10410 if (displayFrozen) {
10411 // If the display is frozen, there is no reason to animate.
10412 more = false;
10413 } else if (mDimDeltaPerMs > 0) {
10414 if (mDimCurrentAlpha > mDimTargetAlpha) {
10415 more = false;
10416 }
10417 } else if (mDimDeltaPerMs < 0) {
10418 if (mDimCurrentAlpha < mDimTargetAlpha) {
10419 more = false;
10420 }
10421 } else {
10422 more = false;
10423 }
10424
10425 // Do we need to continue animating?
10426 if (more) {
10427 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10428 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10429 mLastDimAnimTime = currentTime;
10430 mDimSurface.setAlpha(mDimCurrentAlpha);
10431 animating = true;
10432 } else {
10433 mDimCurrentAlpha = mDimTargetAlpha;
10434 mLastDimAnimTime = 0;
10435 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10436 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10437 mDimSurface.setAlpha(mDimCurrentAlpha);
10438 if (!dimming) {
10439 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10440 + ": HIDE");
10441 try {
10442 mDimSurface.hide();
10443 } catch (RuntimeException e) {
10444 Log.w(TAG, "Illegal argument exception hiding dim surface");
10445 }
10446 mDimShown = false;
10447 }
10448 }
10449 }
10450 return animating;
10451 }
10452
10453 public void printTo(PrintWriter pw) {
10454 pw.print(" mDimShown="); pw.print(mDimShown);
10455 pw.print(" current="); pw.print(mDimCurrentAlpha);
10456 pw.print(" target="); pw.print(mDimTargetAlpha);
10457 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10458 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10459 }
10460 }
10461
10462 /**
10463 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10464 * This is used for opening/closing transition for apps in compatible mode.
10465 */
10466 private static class FadeInOutAnimation extends Animation {
10467 int mWidth;
10468 boolean mFadeIn;
10469
10470 public FadeInOutAnimation(boolean fadeIn) {
10471 setInterpolator(new AccelerateInterpolator());
10472 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10473 mFadeIn = fadeIn;
10474 }
10475
10476 @Override
10477 protected void applyTransformation(float interpolatedTime, Transformation t) {
10478 float x = interpolatedTime;
10479 if (!mFadeIn) {
10480 x = 1.0f - x; // reverse the interpolation for fade out
10481 }
10482 if (x < 0.5) {
10483 // move the window out of the screen.
10484 t.getMatrix().setTranslate(mWidth, 0);
10485 } else {
10486 t.getMatrix().setTranslate(0, 0);// show
10487 t.setAlpha((x - 0.5f) * 2);
10488 }
10489 }
10490
10491 @Override
10492 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10493 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10494 mWidth = width;
10495 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010496
10497 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010498 public int getZAdjustment() {
10499 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010500 }
10501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502}