blob: b4db7bc0e62a033699c1750f63b74681df0c5217 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
Dianne Hackborn75804932009-10-20 20:15:20 -070066import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Debug;
68import android.os.Handler;
69import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070070import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.LocalPowerManager;
72import android.os.Looper;
73import android.os.Message;
74import android.os.Parcel;
75import android.os.ParcelFileDescriptor;
76import android.os.Power;
77import android.os.PowerManager;
78import android.os.Process;
79import android.os.RemoteException;
80import android.os.ServiceManager;
81import android.os.SystemClock;
82import android.os.SystemProperties;
83import android.os.TokenWatcher;
84import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070085import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.EventLog;
87import android.util.Log;
88import android.util.SparseIntArray;
89import android.view.Display;
90import android.view.Gravity;
91import android.view.IApplicationToken;
92import android.view.IOnKeyguardExitResult;
93import android.view.IRotationWatcher;
94import android.view.IWindow;
95import android.view.IWindowManager;
96import android.view.IWindowSession;
97import android.view.KeyEvent;
98import android.view.MotionEvent;
99import android.view.RawInputEvent;
100import android.view.Surface;
101import android.view.SurfaceSession;
102import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700103import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.ViewTreeObserver;
105import android.view.WindowManager;
106import android.view.WindowManagerImpl;
107import android.view.WindowManagerPolicy;
108import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700109import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.animation.Animation;
111import android.view.animation.AnimationUtils;
112import android.view.animation.Transformation;
113
114import java.io.BufferedWriter;
115import java.io.File;
116import java.io.FileDescriptor;
117import java.io.IOException;
118import java.io.OutputStream;
119import java.io.OutputStreamWriter;
120import java.io.PrintWriter;
121import java.io.StringWriter;
122import java.net.Socket;
123import java.util.ArrayList;
124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.Iterator;
127import java.util.List;
128
129/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700130public class WindowManagerService extends IWindowManager.Stub
131 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final String TAG = "WindowManager";
133 static final boolean DEBUG = false;
134 static final boolean DEBUG_FOCUS = false;
135 static final boolean DEBUG_ANIM = false;
136 static final boolean DEBUG_LAYERS = false;
137 static final boolean DEBUG_INPUT = false;
138 static final boolean DEBUG_INPUT_METHOD = false;
139 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700140 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 static final boolean DEBUG_ORIENTATION = false;
142 static final boolean DEBUG_APP_TRANSITIONS = false;
143 static final boolean DEBUG_STARTING_WINDOW = false;
144 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700145 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700147 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700148 static final boolean MEASURE_LATENCY = false;
149 static private LatencyTimer lt;
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final boolean PROFILE_ORIENTATION = false;
152 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700153 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /** How long to wait for subsequent key repeats, in milliseconds */
158 static final int KEY_REPEAT_DELAY = 50;
159
160 /** How much to multiply the policy's type layer, to reserve room
161 * for multiple windows of the same type and Z-ordering adjustment
162 * with TYPE_LAYER_OFFSET. */
163 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
166 * or below others in the same layer. */
167 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** How much to increment the layer for each window, to reserve room
170 * for effect surfaces between them.
171 */
172 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /** The maximum length we will accept for a loaded animation duration:
175 * this is 10 seconds.
176 */
177 static final int MAX_ANIMATION_DURATION = 10*1000;
178
179 /** Amount of time (in milliseconds) to animate the dim surface from one
180 * value to another, when no window animation is driving it.
181 */
182 static final int DEFAULT_DIM_DURATION = 200;
183
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700184 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
185 * compatible windows.
186 */
187 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /** Adjustment to time to perform a dim, to make it more dramatic.
190 */
191 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700192
Dianne Hackborncfaef692009-06-15 14:24:44 -0700193 static final int INJECT_FAILED = 0;
194 static final int INJECT_SUCCEEDED = 1;
195 static final int INJECT_NO_PERMISSION = -1;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 static final int UPDATE_FOCUS_NORMAL = 0;
198 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
199 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
200 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 /** The minimum time between dispatching touch events. */
203 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
204
205 // Last touch event time
206 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700207
Michael Chane96440f2009-05-06 10:27:36 -0700208 // Last touch event type
209 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 // Time to wait before calling useractivity again. This saves CPU usage
212 // when we get a flood of touch events.
213 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
214
215 // Last time we call user activity
216 long mLastUserActivityCallTime = 0;
217
Romain Guy06882f82009-06-10 13:36:04 -0700218 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700219 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700222 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 /**
225 * Condition waited on by {@link #reenableKeyguard} to know the call to
226 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500227 * This is set to true only if mKeyguardTokenWatcher.acquired() has
228 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500230 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Mike Lockwood983ee092009-11-22 01:42:24 -0500232 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
233 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public void acquired() {
235 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500236 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
238 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700239 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500240 synchronized (mKeyguardTokenWatcher) {
241 mKeyguardDisabled = false;
242 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
244 }
245 };
246
247 final Context mContext;
248
249 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
254
255 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * All currently active sessions with clients.
261 */
262 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * Mapping from an IWindow IBinder to the server's Window object.
266 * This is also used as the lock for all of our state.
267 */
268 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
269
270 /**
271 * Mapping from a token IBinder to a WindowToken object.
272 */
273 final HashMap<IBinder, WindowToken> mTokenMap =
274 new HashMap<IBinder, WindowToken>();
275
276 /**
277 * The same tokens as mTokenMap, stored in a list for efficient iteration
278 * over them.
279 */
280 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Window tokens that are in the process of exiting, but still
284 * on screen for animations.
285 */
286 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
287
288 /**
289 * Z-ordered (bottom-most first) list of all application tokens, for
290 * controlling the ordering of windows in different applications. This
291 * contains WindowToken objects.
292 */
293 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
294
295 /**
296 * Application tokens that are in the process of exiting, but still
297 * on screen for animations.
298 */
299 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
300
301 /**
302 * List of window tokens that have finished starting their application,
303 * and now need to have the policy remove their windows.
304 */
305 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
306
307 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700308 * This was the app token that was used to retrieve the last enter
309 * animation. It will be used for the next exit animation.
310 */
311 AppWindowToken mLastEnterAnimToken;
312
313 /**
314 * These were the layout params used to retrieve the last enter animation.
315 * They will be used for the next exit animation.
316 */
317 LayoutParams mLastEnterAnimParams;
318
319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 * Z-ordered (bottom-most first) list of all Window objects.
321 */
322 final ArrayList mWindows = new ArrayList();
323
324 /**
325 * Windows that are being resized. Used so we can tell the client about
326 * the resize after closing the transaction in which we resized the
327 * underlying surface.
328 */
329 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
330
331 /**
332 * Windows whose animations have ended and now must be removed.
333 */
334 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
335
336 /**
337 * Windows whose surface should be destroyed.
338 */
339 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
340
341 /**
342 * Windows that have lost input focus and are waiting for the new
343 * focus window to be displayed before they are told about this.
344 */
345 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
346
347 /**
348 * This is set when we have run out of memory, and will either be an empty
349 * list or contain windows that need to be force removed.
350 */
351 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700356 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 Surface mBlurSurface;
358 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 final float[] mTmpFloats = new float[9];
363
364 boolean mSafeMode;
365 boolean mDisplayEnabled = false;
366 boolean mSystemBooted = false;
367 int mRotation = 0;
368 int mRequestedRotation = 0;
369 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700370 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 ArrayList<IRotationWatcher> mRotationWatchers
372 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 boolean mLayoutNeeded = true;
375 boolean mAnimationPending = false;
376 boolean mDisplayFrozen = false;
377 boolean mWindowsFreezingScreen = false;
378 long mFreezeGcPending = 0;
379 int mAppsFreezingScreen = 0;
380
381 // This is held as long as we have the screen frozen, to give us time to
382 // perform a rotation animation when turning off shows the lock screen which
383 // changes the orientation.
384 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 // State management of app transitions. When we are preparing for a
387 // transition, mNextAppTransition will be the kind of transition to
388 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
389 // mOpeningApps and mClosingApps are the lists of tokens that will be
390 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700391 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700392 String mNextAppTransitionPackage;
393 int mNextAppTransitionEnter;
394 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700396 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 boolean mAppTransitionTimeout = false;
398 boolean mStartingIconInTransition = false;
399 boolean mSkipAppTransitionAnimation = false;
400 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
401 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700402 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
403 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 //flag to detect fat touch events
406 boolean mFatTouch = false;
407 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 H mH = new H();
410
411 WindowState mCurrentFocus = null;
412 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 // This just indicates the window the input method is on top of, not
415 // necessarily the window its input is going to.
416 WindowState mInputMethodTarget = null;
417 WindowState mUpcomingInputMethodTarget = null;
418 boolean mInputMethodTargetWaitingAnim;
419 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 WindowState mInputMethodWindow = null;
422 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
423
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700424 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
425
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700426 // If non-null, this is the currently visible window that is associated
427 // with the wallpaper.
428 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700429 // If non-null, we are in the middle of animating from one wallpaper target
430 // to another, and this is the lower one in Z-order.
431 WindowState mLowerWallpaperTarget = null;
432 // If non-null, we are in the middle of animating from one wallpaper target
433 // to another, and this is the higher one in Z-order.
434 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700435 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700436 float mLastWallpaperX = -1;
437 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800438 float mLastWallpaperXStep = -1;
439 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800440 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700441 // This is set when we are waiting for a wallpaper to tell us it is done
442 // changing its scroll position.
443 WindowState mWaitingOnWallpaper;
444 // The last time we had a timeout when waiting for a wallpaper.
445 long mLastWallpaperTimeoutTime;
446 // We give a wallpaper up to 150ms to finish scrolling.
447 static final long WALLPAPER_TIMEOUT = 150;
448 // Time we wait after a timeout before trying to wait again.
449 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 AppWindowToken mFocusedApp = null;
452
453 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 float mWindowAnimationScale = 1.0f;
456 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 final KeyWaiter mKeyWaiter = new KeyWaiter();
459 final KeyQ mQueue;
460 final InputDispatcherThread mInputThread;
461
462 // Who is holding the screen on.
463 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700464
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700465 boolean mTurnOnScreen;
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
468 * Whether the UI is currently running in touch mode (not showing
469 * navigational focus because the user is directly pressing the screen).
470 */
471 boolean mInTouchMode = false;
472
473 private ViewServer mViewServer;
474
475 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700476
Dianne Hackbornc485a602009-03-24 22:39:49 -0700477 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700478 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700479
480 // The frame use to limit the size of the app running in compatibility mode.
481 Rect mCompatibleScreenFrame = new Rect();
482 // The surface used to fill the outer rim of the app running in compatibility mode.
483 Surface mBackgroundFillerSurface = null;
484 boolean mBackgroundFillerShown = false;
485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public static WindowManagerService main(Context context,
487 PowerManagerService pm, boolean haveInputMethods) {
488 WMThread thr = new WMThread(context, pm, haveInputMethods);
489 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 synchronized (thr) {
492 while (thr.mService == null) {
493 try {
494 thr.wait();
495 } catch (InterruptedException e) {
496 }
497 }
498 }
Romain Guy06882f82009-06-10 13:36:04 -0700499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 return thr.mService;
501 }
Romain Guy06882f82009-06-10 13:36:04 -0700502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 static class WMThread extends Thread {
504 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 private final Context mContext;
507 private final PowerManagerService mPM;
508 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public WMThread(Context context, PowerManagerService pm,
511 boolean haveInputMethods) {
512 super("WindowManager");
513 mContext = context;
514 mPM = pm;
515 mHaveInputMethods = haveInputMethods;
516 }
Romain Guy06882f82009-06-10 13:36:04 -0700517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 public void run() {
519 Looper.prepare();
520 WindowManagerService s = new WindowManagerService(mContext, mPM,
521 mHaveInputMethods);
522 android.os.Process.setThreadPriority(
523 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 synchronized (this) {
526 mService = s;
527 notifyAll();
528 }
Romain Guy06882f82009-06-10 13:36:04 -0700529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 Looper.loop();
531 }
532 }
533
534 static class PolicyThread extends Thread {
535 private final WindowManagerPolicy mPolicy;
536 private final WindowManagerService mService;
537 private final Context mContext;
538 private final PowerManagerService mPM;
539 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 public PolicyThread(WindowManagerPolicy policy,
542 WindowManagerService service, Context context,
543 PowerManagerService pm) {
544 super("WindowManagerPolicy");
545 mPolicy = policy;
546 mService = service;
547 mContext = context;
548 mPM = pm;
549 }
Romain Guy06882f82009-06-10 13:36:04 -0700550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 public void run() {
552 Looper.prepare();
553 //Looper.myLooper().setMessageLogging(new LogPrinter(
554 // Log.VERBOSE, "WindowManagerPolicy"));
555 android.os.Process.setThreadPriority(
556 android.os.Process.THREAD_PRIORITY_FOREGROUND);
557 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 synchronized (this) {
560 mRunning = true;
561 notifyAll();
562 }
Romain Guy06882f82009-06-10 13:36:04 -0700563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 Looper.loop();
565 }
566 }
567
568 private WindowManagerService(Context context, PowerManagerService pm,
569 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700570 if (MEASURE_LATENCY) {
571 lt = new LatencyTimer(100, 1000);
572 }
573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 mContext = context;
575 mHaveInputMethods = haveInputMethods;
576 mLimitedAlphaCompositing = context.getResources().getBoolean(
577 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 mPowerManager = pm;
580 mPowerManager.setPolicy(mPolicy);
581 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
582 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
583 "SCREEN_FROZEN");
584 mScreenFrozenLock.setReferenceCounted(false);
585
586 mActivityManager = ActivityManagerNative.getDefault();
587 mBatteryStats = BatteryStatsService.getService();
588
589 // Get persisted window scale setting
590 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
591 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
592 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
593 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700594
Michael Chan9f028e62009-08-04 17:37:46 -0700595 int max_events_per_sec = 35;
596 try {
597 max_events_per_sec = Integer.parseInt(SystemProperties
598 .get("windowsmgr.max_events_per_sec"));
599 if (max_events_per_sec < 1) {
600 max_events_per_sec = 35;
601 }
602 } catch (NumberFormatException e) {
603 }
604 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 mQueue = new KeyQ();
607
608 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
611 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 synchronized (thr) {
614 while (!thr.mRunning) {
615 try {
616 thr.wait();
617 } catch (InterruptedException e) {
618 }
619 }
620 }
Romain Guy06882f82009-06-10 13:36:04 -0700621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 // Add ourself to the Watchdog monitors.
625 Watchdog.getInstance().addMonitor(this);
626 }
627
628 @Override
629 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
630 throws RemoteException {
631 try {
632 return super.onTransact(code, data, reply, flags);
633 } catch (RuntimeException e) {
634 // The window manager only throws security exceptions, so let's
635 // log all others.
636 if (!(e instanceof SecurityException)) {
637 Log.e(TAG, "Window Manager Crash", e);
638 }
639 throw e;
640 }
641 }
642
643 private void placeWindowAfter(Object pos, WindowState window) {
644 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700645 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 TAG, "Adding window " + window + " at "
647 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
648 mWindows.add(i+1, window);
649 }
650
651 private void placeWindowBefore(Object pos, WindowState window) {
652 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700653 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 TAG, "Adding window " + window + " at "
655 + i + " of " + mWindows.size() + " (before " + pos + ")");
656 mWindows.add(i, window);
657 }
658
659 //This method finds out the index of a window that has the same app token as
660 //win. used for z ordering the windows in mWindows
661 private int findIdxBasedOnAppTokens(WindowState win) {
662 //use a local variable to cache mWindows
663 ArrayList localmWindows = mWindows;
664 int jmax = localmWindows.size();
665 if(jmax == 0) {
666 return -1;
667 }
668 for(int j = (jmax-1); j >= 0; j--) {
669 WindowState wentry = (WindowState)localmWindows.get(j);
670 if(wentry.mAppToken == win.mAppToken) {
671 return j;
672 }
673 }
674 return -1;
675 }
Romain Guy06882f82009-06-10 13:36:04 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
678 final IWindow client = win.mClient;
679 final WindowToken token = win.mToken;
680 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 final int N = localmWindows.size();
683 final WindowState attached = win.mAttachedWindow;
684 int i;
685 if (attached == null) {
686 int tokenWindowsPos = token.windows.size();
687 if (token.appWindowToken != null) {
688 int index = tokenWindowsPos-1;
689 if (index >= 0) {
690 // If this application has existing windows, we
691 // simply place the new window on top of them... but
692 // keep the starting window on top.
693 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
694 // Base windows go behind everything else.
695 placeWindowBefore(token.windows.get(0), win);
696 tokenWindowsPos = 0;
697 } else {
698 AppWindowToken atoken = win.mAppToken;
699 if (atoken != null &&
700 token.windows.get(index) == atoken.startingWindow) {
701 placeWindowBefore(token.windows.get(index), win);
702 tokenWindowsPos--;
703 } else {
704 int newIdx = findIdxBasedOnAppTokens(win);
705 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700706 //there is a window above this one associated with the same
707 //apptoken note that the window could be a floating window
708 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700710 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
711 TAG, "Adding window " + win + " at "
712 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716 }
717 } else {
718 if (localLOGV) Log.v(
719 TAG, "Figuring out where to add app window "
720 + client.asBinder() + " (token=" + token + ")");
721 // Figure out where the window should go, based on the
722 // order of applications.
723 final int NA = mAppTokens.size();
724 Object pos = null;
725 for (i=NA-1; i>=0; i--) {
726 AppWindowToken t = mAppTokens.get(i);
727 if (t == token) {
728 i--;
729 break;
730 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700731
732 // We haven't reached the token yet; if this token
733 // is not going to the bottom and has windows, we can
734 // use it as an anchor for when we do reach the token.
735 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 pos = t.windows.get(0);
737 }
738 }
739 // We now know the index into the apps. If we found
740 // an app window above, that gives us the position; else
741 // we need to look some more.
742 if (pos != null) {
743 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700744 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 mTokenMap.get(((WindowState)pos).mClient.asBinder());
746 if (atoken != null) {
747 final int NC = atoken.windows.size();
748 if (NC > 0) {
749 WindowState bottom = atoken.windows.get(0);
750 if (bottom.mSubLayer < 0) {
751 pos = bottom;
752 }
753 }
754 }
755 placeWindowBefore(pos, win);
756 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700757 // Continue looking down until we find the first
758 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 while (i >= 0) {
760 AppWindowToken t = mAppTokens.get(i);
761 final int NW = t.windows.size();
762 if (NW > 0) {
763 pos = t.windows.get(NW-1);
764 break;
765 }
766 i--;
767 }
768 if (pos != null) {
769 // Move in front of any windows attached to this
770 // one.
771 WindowToken atoken =
772 mTokenMap.get(((WindowState)pos).mClient.asBinder());
773 if (atoken != null) {
774 final int NC = atoken.windows.size();
775 if (NC > 0) {
776 WindowState top = atoken.windows.get(NC-1);
777 if (top.mSubLayer >= 0) {
778 pos = top;
779 }
780 }
781 }
782 placeWindowAfter(pos, win);
783 } else {
784 // Just search for the start of this layer.
785 final int myLayer = win.mBaseLayer;
786 for (i=0; i<N; i++) {
787 WindowState w = (WindowState)localmWindows.get(i);
788 if (w.mBaseLayer > myLayer) {
789 break;
790 }
791 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700792 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
793 TAG, "Adding window " + win + " at "
794 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 localmWindows.add(i, win);
796 }
797 }
798 }
799 } else {
800 // Figure out where window should go, based on layer.
801 final int myLayer = win.mBaseLayer;
802 for (i=N-1; i>=0; i--) {
803 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
804 i++;
805 break;
806 }
807 }
808 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700809 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
810 TAG, "Adding window " + win + " at "
811 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 localmWindows.add(i, win);
813 }
814 if (addToToken) {
815 token.windows.add(tokenWindowsPos, win);
816 }
817
818 } else {
819 // Figure out this window's ordering relative to the window
820 // it is attached to.
821 final int NA = token.windows.size();
822 final int sublayer = win.mSubLayer;
823 int largestSublayer = Integer.MIN_VALUE;
824 WindowState windowWithLargestSublayer = null;
825 for (i=0; i<NA; i++) {
826 WindowState w = token.windows.get(i);
827 final int wSublayer = w.mSubLayer;
828 if (wSublayer >= largestSublayer) {
829 largestSublayer = wSublayer;
830 windowWithLargestSublayer = w;
831 }
832 if (sublayer < 0) {
833 // For negative sublayers, we go below all windows
834 // in the same sublayer.
835 if (wSublayer >= sublayer) {
836 if (addToToken) {
837 token.windows.add(i, win);
838 }
839 placeWindowBefore(
840 wSublayer >= 0 ? attached : w, win);
841 break;
842 }
843 } else {
844 // For positive sublayers, we go above all windows
845 // in the same sublayer.
846 if (wSublayer > sublayer) {
847 if (addToToken) {
848 token.windows.add(i, win);
849 }
850 placeWindowBefore(w, win);
851 break;
852 }
853 }
854 }
855 if (i >= NA) {
856 if (addToToken) {
857 token.windows.add(win);
858 }
859 if (sublayer < 0) {
860 placeWindowBefore(attached, win);
861 } else {
862 placeWindowAfter(largestSublayer >= 0
863 ? windowWithLargestSublayer
864 : attached,
865 win);
866 }
867 }
868 }
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (win.mAppToken != null && addToToken) {
871 win.mAppToken.allAppWindows.add(win);
872 }
873 }
Romain Guy06882f82009-06-10 13:36:04 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 static boolean canBeImeTarget(WindowState w) {
876 final int fl = w.mAttrs.flags
877 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
878 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
879 return w.isVisibleOrAdding();
880 }
881 return false;
882 }
Romain Guy06882f82009-06-10 13:36:04 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
885 final ArrayList localmWindows = mWindows;
886 final int N = localmWindows.size();
887 WindowState w = null;
888 int i = N;
889 while (i > 0) {
890 i--;
891 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
894 // + Integer.toHexString(w.mAttrs.flags));
895 if (canBeImeTarget(w)) {
896 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // Yet more tricksyness! If this window is a "starting"
899 // window, we do actually want to be on top of it, but
900 // it is not -really- where input will go. So if the caller
901 // is not actually looking to move the IME, look down below
902 // for a real window to target...
903 if (!willMove
904 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
905 && i > 0) {
906 WindowState wb = (WindowState)localmWindows.get(i-1);
907 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
908 i--;
909 w = wb;
910 }
911 }
912 break;
913 }
914 }
Romain Guy06882f82009-06-10 13:36:04 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
919 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 if (willMove && w != null) {
922 final WindowState curTarget = mInputMethodTarget;
923 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 // Now some fun for dealing with window animations that
926 // modify the Z order. We need to look at all windows below
927 // the current target that are in this app, finding the highest
928 // visible one in layering.
929 AppWindowToken token = curTarget.mAppToken;
930 WindowState highestTarget = null;
931 int highestPos = 0;
932 if (token.animating || token.animation != null) {
933 int pos = 0;
934 pos = localmWindows.indexOf(curTarget);
935 while (pos >= 0) {
936 WindowState win = (WindowState)localmWindows.get(pos);
937 if (win.mAppToken != token) {
938 break;
939 }
940 if (!win.mRemoved) {
941 if (highestTarget == null || win.mAnimLayer >
942 highestTarget.mAnimLayer) {
943 highestTarget = win;
944 highestPos = pos;
945 }
946 }
947 pos--;
948 }
949 }
Romain Guy06882f82009-06-10 13:36:04 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700952 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 + mNextAppTransition + " " + highestTarget
954 + " animating=" + highestTarget.isAnimating()
955 + " layer=" + highestTarget.mAnimLayer
956 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700957
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700958 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 // If we are currently setting up for an animation,
960 // hold everything until we can find out what will happen.
961 mInputMethodTargetWaitingAnim = true;
962 mInputMethodTarget = highestTarget;
963 return highestPos + 1;
964 } else if (highestTarget.isAnimating() &&
965 highestTarget.mAnimLayer > w.mAnimLayer) {
966 // If the window we are currently targeting is involved
967 // with an animation, and it is on top of the next target
968 // we will be over, then hold off on moving until
969 // that is done.
970 mInputMethodTarget = highestTarget;
971 return highestPos + 1;
972 }
973 }
974 }
975 }
Romain Guy06882f82009-06-10 13:36:04 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 //Log.i(TAG, "Placing input method @" + (i+1));
978 if (w != null) {
979 if (willMove) {
980 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700981 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
983 + mInputMethodTarget + " to " + w, e);
984 mInputMethodTarget = w;
985 if (w.mAppToken != null) {
986 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
987 } else {
988 setInputMethodAnimLayerAdjustment(0);
989 }
990 }
991 return i+1;
992 }
993 if (willMove) {
994 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700995 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
997 + mInputMethodTarget + " to null", e);
998 mInputMethodTarget = null;
999 setInputMethodAnimLayerAdjustment(0);
1000 }
1001 return -1;
1002 }
Romain Guy06882f82009-06-10 13:36:04 -07001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 void addInputMethodWindowToListLocked(WindowState win) {
1005 int pos = findDesiredInputMethodWindowIndexLocked(true);
1006 if (pos >= 0) {
1007 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001008 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1009 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 mWindows.add(pos, win);
1011 moveInputMethodDialogsLocked(pos+1);
1012 return;
1013 }
1014 win.mTargetAppToken = null;
1015 addWindowToListInOrderLocked(win, true);
1016 moveInputMethodDialogsLocked(pos);
1017 }
Romain Guy06882f82009-06-10 13:36:04 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 void setInputMethodAnimLayerAdjustment(int adj) {
1020 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1021 mInputMethodAnimLayerAdjustment = adj;
1022 WindowState imw = mInputMethodWindow;
1023 if (imw != null) {
1024 imw.mAnimLayer = imw.mLayer + adj;
1025 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1026 + " anim layer: " + imw.mAnimLayer);
1027 int wi = imw.mChildWindows.size();
1028 while (wi > 0) {
1029 wi--;
1030 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1031 cw.mAnimLayer = cw.mLayer + adj;
1032 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1033 + " anim layer: " + cw.mAnimLayer);
1034 }
1035 }
1036 int di = mInputMethodDialogs.size();
1037 while (di > 0) {
1038 di --;
1039 imw = mInputMethodDialogs.get(di);
1040 imw.mAnimLayer = imw.mLayer + adj;
1041 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1042 + " anim layer: " + imw.mAnimLayer);
1043 }
1044 }
Romain Guy06882f82009-06-10 13:36:04 -07001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1047 int wpos = mWindows.indexOf(win);
1048 if (wpos >= 0) {
1049 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001050 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 mWindows.remove(wpos);
1052 int NC = win.mChildWindows.size();
1053 while (NC > 0) {
1054 NC--;
1055 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1056 int cpos = mWindows.indexOf(cw);
1057 if (cpos >= 0) {
1058 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001059 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1060 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 mWindows.remove(cpos);
1062 }
1063 }
1064 }
1065 return interestingPos;
1066 }
Romain Guy06882f82009-06-10 13:36:04 -07001067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 private void reAddWindowToListInOrderLocked(WindowState win) {
1069 addWindowToListInOrderLocked(win, false);
1070 // This is a hack to get all of the child windows added as well
1071 // at the right position. Child windows should be rare and
1072 // this case should be rare, so it shouldn't be that big a deal.
1073 int wpos = mWindows.indexOf(win);
1074 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001075 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1076 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 mWindows.remove(wpos);
1078 reAddWindowLocked(wpos, win);
1079 }
1080 }
Romain Guy06882f82009-06-10 13:36:04 -07001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 void logWindowList(String prefix) {
1083 int N = mWindows.size();
1084 while (N > 0) {
1085 N--;
1086 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1087 }
1088 }
Romain Guy06882f82009-06-10 13:36:04 -07001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 void moveInputMethodDialogsLocked(int pos) {
1091 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 final int N = dialogs.size();
1094 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1095 for (int i=0; i<N; i++) {
1096 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1097 }
1098 if (DEBUG_INPUT_METHOD) {
1099 Log.v(TAG, "Window list w/pos=" + pos);
1100 logWindowList(" ");
1101 }
Romain Guy06882f82009-06-10 13:36:04 -07001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (pos >= 0) {
1104 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1105 if (pos < mWindows.size()) {
1106 WindowState wp = (WindowState)mWindows.get(pos);
1107 if (wp == mInputMethodWindow) {
1108 pos++;
1109 }
1110 }
1111 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1112 for (int i=0; i<N; i++) {
1113 WindowState win = dialogs.get(i);
1114 win.mTargetAppToken = targetAppToken;
1115 pos = reAddWindowLocked(pos, win);
1116 }
1117 if (DEBUG_INPUT_METHOD) {
1118 Log.v(TAG, "Final window list:");
1119 logWindowList(" ");
1120 }
1121 return;
1122 }
1123 for (int i=0; i<N; i++) {
1124 WindowState win = dialogs.get(i);
1125 win.mTargetAppToken = null;
1126 reAddWindowToListInOrderLocked(win);
1127 if (DEBUG_INPUT_METHOD) {
1128 Log.v(TAG, "No IM target, final list:");
1129 logWindowList(" ");
1130 }
1131 }
1132 }
Romain Guy06882f82009-06-10 13:36:04 -07001133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1135 final WindowState imWin = mInputMethodWindow;
1136 final int DN = mInputMethodDialogs.size();
1137 if (imWin == null && DN == 0) {
1138 return false;
1139 }
Romain Guy06882f82009-06-10 13:36:04 -07001140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1142 if (imPos >= 0) {
1143 // In this case, the input method windows are to be placed
1144 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 // First check to see if the input method windows are already
1147 // located here, and contiguous.
1148 final int N = mWindows.size();
1149 WindowState firstImWin = imPos < N
1150 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 // Figure out the actual input method window that should be
1153 // at the bottom of their stack.
1154 WindowState baseImWin = imWin != null
1155 ? imWin : mInputMethodDialogs.get(0);
1156 if (baseImWin.mChildWindows.size() > 0) {
1157 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1158 if (cw.mSubLayer < 0) baseImWin = cw;
1159 }
Romain Guy06882f82009-06-10 13:36:04 -07001160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 if (firstImWin == baseImWin) {
1162 // The windows haven't moved... but are they still contiguous?
1163 // First find the top IM window.
1164 int pos = imPos+1;
1165 while (pos < N) {
1166 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1167 break;
1168 }
1169 pos++;
1170 }
1171 pos++;
1172 // Now there should be no more input method windows above.
1173 while (pos < N) {
1174 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1175 break;
1176 }
1177 pos++;
1178 }
1179 if (pos >= N) {
1180 // All is good!
1181 return false;
1182 }
1183 }
Romain Guy06882f82009-06-10 13:36:04 -07001184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (imWin != null) {
1186 if (DEBUG_INPUT_METHOD) {
1187 Log.v(TAG, "Moving IM from " + imPos);
1188 logWindowList(" ");
1189 }
1190 imPos = tmpRemoveWindowLocked(imPos, imWin);
1191 if (DEBUG_INPUT_METHOD) {
1192 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1193 logWindowList(" ");
1194 }
1195 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1196 reAddWindowLocked(imPos, imWin);
1197 if (DEBUG_INPUT_METHOD) {
1198 Log.v(TAG, "List after moving IM to " + imPos + ":");
1199 logWindowList(" ");
1200 }
1201 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1202 } else {
1203 moveInputMethodDialogsLocked(imPos);
1204 }
Romain Guy06882f82009-06-10 13:36:04 -07001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 } else {
1207 // In this case, the input method windows go in a fixed layer,
1208 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 if (imWin != null) {
1211 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1212 tmpRemoveWindowLocked(0, imWin);
1213 imWin.mTargetAppToken = null;
1214 reAddWindowToListInOrderLocked(imWin);
1215 if (DEBUG_INPUT_METHOD) {
1216 Log.v(TAG, "List with no IM target:");
1217 logWindowList(" ");
1218 }
1219 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1220 } else {
1221 moveInputMethodDialogsLocked(-1);;
1222 }
Romain Guy06882f82009-06-10 13:36:04 -07001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
Romain Guy06882f82009-06-10 13:36:04 -07001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 if (needAssignLayers) {
1227 assignLayersLocked();
1228 }
Romain Guy06882f82009-06-10 13:36:04 -07001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 return true;
1231 }
Romain Guy06882f82009-06-10 13:36:04 -07001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 void adjustInputMethodDialogsLocked() {
1234 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1235 }
Romain Guy06882f82009-06-10 13:36:04 -07001236
Dianne Hackborn25994b42009-09-04 14:21:19 -07001237 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1238 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1239 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1240 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1241 ? wallpaperTarget.mAppToken.animation : null)
1242 + " upper=" + mUpperWallpaperTarget
1243 + " lower=" + mLowerWallpaperTarget);
1244 return (wallpaperTarget != null
1245 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1246 && wallpaperTarget.mAppToken.animation != null)))
1247 || mUpperWallpaperTarget != null
1248 || mLowerWallpaperTarget != null;
1249 }
1250
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001251 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1252 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1253
1254 int adjustWallpaperWindowsLocked() {
1255 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001256
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001257 final int dw = mDisplay.getWidth();
1258 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001259
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001260 // First find top-most window that has asked to be on top of the
1261 // wallpaper; all wallpapers go behind it.
1262 final ArrayList localmWindows = mWindows;
1263 int N = localmWindows.size();
1264 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001265 WindowState foundW = null;
1266 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001267 WindowState topCurW = null;
1268 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001269 int i = N;
1270 while (i > 0) {
1271 i--;
1272 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001273 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1274 if (topCurW == null) {
1275 topCurW = w;
1276 topCurI = i;
1277 }
1278 continue;
1279 }
1280 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001281 if (w.mAppToken != null) {
1282 // If this window's app token is hidden and not animating,
1283 // it is of no interest to us.
1284 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1285 if (DEBUG_WALLPAPER) Log.v(TAG,
1286 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001287 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001288 continue;
1289 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001290 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001291 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1292 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1293 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001294 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001295 && (mWallpaperTarget == w
1296 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001297 if (DEBUG_WALLPAPER) Log.v(TAG,
1298 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001299 foundW = w;
1300 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001301 if (w == mWallpaperTarget && ((w.mAppToken != null
1302 && w.mAppToken.animation != null)
1303 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001304 // The current wallpaper target is animating, so we'll
1305 // look behind it for another possible target and figure
1306 // out what is going on below.
1307 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1308 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001309 continue;
1310 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001311 break;
1312 }
1313 }
1314
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001315 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001316 // If we are currently waiting for an app transition, and either
1317 // the current target or the next target are involved with it,
1318 // then hold off on doing anything with the wallpaper.
1319 // Note that we are checking here for just whether the target
1320 // is part of an app token... which is potentially overly aggressive
1321 // (the app token may not be involved in the transition), but good
1322 // enough (we'll just wait until whatever transition is pending
1323 // executes).
1324 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001325 if (DEBUG_WALLPAPER) Log.v(TAG,
1326 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001327 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001328 }
1329 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001330 if (DEBUG_WALLPAPER) Log.v(TAG,
1331 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001332 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001333 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001334 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001335
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001336 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001337 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001338 Log.v(TAG, "New wallpaper target: " + foundW
1339 + " oldTarget: " + mWallpaperTarget);
1340 }
1341
1342 mLowerWallpaperTarget = null;
1343 mUpperWallpaperTarget = null;
1344
1345 WindowState oldW = mWallpaperTarget;
1346 mWallpaperTarget = foundW;
1347
1348 // Now what is happening... if the current and new targets are
1349 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001350 if (foundW != null && oldW != null) {
1351 boolean oldAnim = oldW.mAnimation != null
1352 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1353 boolean foundAnim = foundW.mAnimation != null
1354 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001355 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001356 Log.v(TAG, "New animation: " + foundAnim
1357 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001358 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001359 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001360 int oldI = localmWindows.indexOf(oldW);
1361 if (DEBUG_WALLPAPER) {
1362 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1363 }
1364 if (oldI >= 0) {
1365 if (DEBUG_WALLPAPER) {
1366 Log.v(TAG, "Animating wallpapers: old#" + oldI
1367 + "=" + oldW + "; new#" + foundI
1368 + "=" + foundW);
1369 }
1370
1371 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001372 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001373 if (DEBUG_WALLPAPER) {
1374 Log.v(TAG, "Old wallpaper still the target.");
1375 }
1376 mWallpaperTarget = oldW;
1377 }
1378
1379 // Now set the upper and lower wallpaper targets
1380 // correctly, and make sure that we are positioning
1381 // the wallpaper below the lower.
1382 if (foundI > oldI) {
1383 // The new target is on top of the old one.
1384 if (DEBUG_WALLPAPER) {
1385 Log.v(TAG, "Found target above old target.");
1386 }
1387 mUpperWallpaperTarget = foundW;
1388 mLowerWallpaperTarget = oldW;
1389 foundW = oldW;
1390 foundI = oldI;
1391 } else {
1392 // The new target is below the old one.
1393 if (DEBUG_WALLPAPER) {
1394 Log.v(TAG, "Found target below old target.");
1395 }
1396 mUpperWallpaperTarget = oldW;
1397 mLowerWallpaperTarget = foundW;
1398 }
1399 }
1400 }
1401 }
1402
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001403 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001404 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001405 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1406 || (mLowerWallpaperTarget.mAppToken != null
1407 && mLowerWallpaperTarget.mAppToken.animation != null);
1408 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1409 || (mUpperWallpaperTarget.mAppToken != null
1410 && mUpperWallpaperTarget.mAppToken.animation != null);
1411 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001412 if (DEBUG_WALLPAPER) {
1413 Log.v(TAG, "No longer animating wallpaper targets!");
1414 }
1415 mLowerWallpaperTarget = null;
1416 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001417 }
1418 }
1419
1420 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001421 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001422 // The window is visible to the compositor... but is it visible
1423 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001424 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001425 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001426
1427 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001428 // its layer adjustment. Only do this if we are not transfering
1429 // between two wallpaper targets.
1430 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001431 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001432 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001433
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001434 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1435 * TYPE_LAYER_MULTIPLIER
1436 + TYPE_LAYER_OFFSET;
1437
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001438 // Now w is the window we are supposed to be behind... but we
1439 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001440 // AND any starting window associated with it, AND below the
1441 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001442 while (foundI > 0) {
1443 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001444 if (wb.mBaseLayer < maxLayer &&
1445 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001446 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001447 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001448 // This window is not related to the previous one in any
1449 // interesting way, so stop here.
1450 break;
1451 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001452 foundW = wb;
1453 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001454 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001455 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001456 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001457 }
1458
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001459 if (foundW == null && topCurW != null) {
1460 // There is no wallpaper target, so it goes at the bottom.
1461 // We will assume it is the same place as last time, if known.
1462 foundW = topCurW;
1463 foundI = topCurI+1;
1464 } else {
1465 // Okay i is the position immediately above the wallpaper. Look at
1466 // what is below it for later.
1467 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1468 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001469
Dianne Hackborn284ac932009-08-28 10:34:25 -07001470 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001471 if (mWallpaperTarget.mWallpaperX >= 0) {
1472 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001473 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001474 }
1475 if (mWallpaperTarget.mWallpaperY >= 0) {
1476 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001477 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001478 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001479 }
1480
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001481 // Start stepping backwards from here, ensuring that our wallpaper windows
1482 // are correctly placed.
1483 int curTokenIndex = mWallpaperTokens.size();
1484 while (curTokenIndex > 0) {
1485 curTokenIndex--;
1486 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001487 if (token.hidden == visible) {
1488 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1489 token.hidden = !visible;
1490 // Need to do a layout to ensure the wallpaper now has the
1491 // correct size.
1492 mLayoutNeeded = true;
1493 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001494
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001495 int curWallpaperIndex = token.windows.size();
1496 while (curWallpaperIndex > 0) {
1497 curWallpaperIndex--;
1498 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001499
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001500 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001501 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001502 }
1503
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001504 // First, make sure the client has the current visibility
1505 // state.
1506 if (wallpaper.mWallpaperVisible != visible) {
1507 wallpaper.mWallpaperVisible = visible;
1508 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001509 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001510 "Setting visibility of wallpaper " + wallpaper
1511 + ": " + visible);
1512 wallpaper.mClient.dispatchAppVisibility(visible);
1513 } catch (RemoteException e) {
1514 }
1515 }
1516
1517 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001518 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1519 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001520
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001521 // First, if this window is at the current index, then all
1522 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001523 if (wallpaper == foundW) {
1524 foundI--;
1525 foundW = foundI > 0
1526 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001527 continue;
1528 }
1529
1530 // The window didn't match... the current wallpaper window,
1531 // wherever it is, is in the wrong place, so make sure it is
1532 // not in the list.
1533 int oldIndex = localmWindows.indexOf(wallpaper);
1534 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001535 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1536 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001537 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001538 if (oldIndex < foundI) {
1539 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001540 }
1541 }
1542
1543 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001544 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1545 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001546 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001547
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001548 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001549 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001550 }
1551 }
1552
1553 return changed;
1554 }
1555
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001556 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001557 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1558 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001559 mWallpaperAnimLayerAdjustment = adj;
1560 int curTokenIndex = mWallpaperTokens.size();
1561 while (curTokenIndex > 0) {
1562 curTokenIndex--;
1563 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1564 int curWallpaperIndex = token.windows.size();
1565 while (curWallpaperIndex > 0) {
1566 curWallpaperIndex--;
1567 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1568 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001569 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1570 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001571 }
1572 }
1573 }
1574
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001575 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1576 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001577 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001578 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001579 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001580 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001581 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1582 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1583 changed = wallpaperWin.mXOffset != offset;
1584 if (changed) {
1585 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1586 + wallpaperWin + " x: " + offset);
1587 wallpaperWin.mXOffset = offset;
1588 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001589 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001590 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001591 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001592 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001593 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001594
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001595 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001596 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001597 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1598 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1599 if (wallpaperWin.mYOffset != offset) {
1600 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1601 + wallpaperWin + " y: " + offset);
1602 changed = true;
1603 wallpaperWin.mYOffset = offset;
1604 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001605 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001606 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001607 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001608 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001609 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001610
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001611 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001612 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001613 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1614 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1615 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001616 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001617 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001618 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001619 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001620 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1621 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001622 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001623 if (mWaitingOnWallpaper != null) {
1624 long start = SystemClock.uptimeMillis();
1625 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1626 < start) {
1627 try {
1628 if (DEBUG_WALLPAPER) Log.v(TAG,
1629 "Waiting for offset complete...");
1630 mWindowMap.wait(WALLPAPER_TIMEOUT);
1631 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001632 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001633 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1634 if ((start+WALLPAPER_TIMEOUT)
1635 < SystemClock.uptimeMillis()) {
1636 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1637 + wallpaperWin);
1638 mLastWallpaperTimeoutTime = start;
1639 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001640 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001641 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001642 }
1643 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001644 } catch (RemoteException e) {
1645 }
1646 }
1647
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001648 return changed;
1649 }
1650
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001651 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001652 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001653 if (mWaitingOnWallpaper != null &&
1654 mWaitingOnWallpaper.mClient.asBinder() == window) {
1655 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001656 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001657 }
1658 }
1659 }
1660
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001661 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001662 final int dw = mDisplay.getWidth();
1663 final int dh = mDisplay.getHeight();
1664
1665 boolean changed = false;
1666
1667 WindowState target = mWallpaperTarget;
1668 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001669 if (target.mWallpaperX >= 0) {
1670 mLastWallpaperX = target.mWallpaperX;
1671 } else if (changingTarget.mWallpaperX >= 0) {
1672 mLastWallpaperX = changingTarget.mWallpaperX;
1673 }
1674 if (target.mWallpaperY >= 0) {
1675 mLastWallpaperY = target.mWallpaperY;
1676 } else if (changingTarget.mWallpaperY >= 0) {
1677 mLastWallpaperY = changingTarget.mWallpaperY;
1678 }
1679 }
1680
1681 int curTokenIndex = mWallpaperTokens.size();
1682 while (curTokenIndex > 0) {
1683 curTokenIndex--;
1684 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1685 int curWallpaperIndex = token.windows.size();
1686 while (curWallpaperIndex > 0) {
1687 curWallpaperIndex--;
1688 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1689 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1690 wallpaper.computeShownFrameLocked();
1691 changed = true;
1692 // We only want to be synchronous with one wallpaper.
1693 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001694 }
1695 }
1696 }
1697
1698 return changed;
1699 }
1700
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001701 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001702 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001703 final int dw = mDisplay.getWidth();
1704 final int dh = mDisplay.getHeight();
1705
1706 int curTokenIndex = mWallpaperTokens.size();
1707 while (curTokenIndex > 0) {
1708 curTokenIndex--;
1709 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001710 if (token.hidden == visible) {
1711 token.hidden = !visible;
1712 // Need to do a layout to ensure the wallpaper now has the
1713 // correct size.
1714 mLayoutNeeded = true;
1715 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001716
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001717 int curWallpaperIndex = token.windows.size();
1718 while (curWallpaperIndex > 0) {
1719 curWallpaperIndex--;
1720 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1721 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001722 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001723 }
1724
1725 if (wallpaper.mWallpaperVisible != visible) {
1726 wallpaper.mWallpaperVisible = visible;
1727 try {
1728 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001729 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001730 + ": " + visible);
1731 wallpaper.mClient.dispatchAppVisibility(visible);
1732 } catch (RemoteException e) {
1733 }
1734 }
1735 }
1736 }
1737 }
1738
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001739 void sendPointerToWallpaperLocked(WindowState srcWin,
1740 MotionEvent pointer, long eventTime) {
1741 int curTokenIndex = mWallpaperTokens.size();
1742 while (curTokenIndex > 0) {
1743 curTokenIndex--;
1744 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1745 int curWallpaperIndex = token.windows.size();
1746 while (curWallpaperIndex > 0) {
1747 curWallpaperIndex--;
1748 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1749 if ((wallpaper.mAttrs.flags &
1750 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1751 continue;
1752 }
1753 try {
1754 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001755 if (srcWin != null) {
1756 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1757 srcWin.mFrame.top-wallpaper.mFrame.top);
1758 } else {
1759 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1760 }
1761 switch (pointer.getAction()) {
1762 case MotionEvent.ACTION_DOWN:
1763 mSendingPointersToWallpaper = true;
1764 break;
1765 case MotionEvent.ACTION_UP:
1766 mSendingPointersToWallpaper = false;
1767 break;
1768 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001769 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1770 } catch (RemoteException e) {
1771 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1772 }
1773 }
1774 }
1775 }
1776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 public int addWindow(Session session, IWindow client,
1778 WindowManager.LayoutParams attrs, int viewVisibility,
1779 Rect outContentInsets) {
1780 int res = mPolicy.checkAddPermission(attrs);
1781 if (res != WindowManagerImpl.ADD_OKAY) {
1782 return res;
1783 }
Romain Guy06882f82009-06-10 13:36:04 -07001784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 boolean reportNewConfig = false;
1786 WindowState attachedWindow = null;
1787 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 synchronized(mWindowMap) {
1790 // Instantiating a Display requires talking with the simulator,
1791 // so don't do it until we know the system is mostly up and
1792 // running.
1793 if (mDisplay == null) {
1794 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1795 mDisplay = wm.getDefaultDisplay();
1796 mQueue.setDisplay(mDisplay);
1797 reportNewConfig = true;
1798 }
Romain Guy06882f82009-06-10 13:36:04 -07001799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 if (mWindowMap.containsKey(client.asBinder())) {
1801 Log.w(TAG, "Window " + client + " is already added");
1802 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1803 }
1804
1805 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001806 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 if (attachedWindow == null) {
1808 Log.w(TAG, "Attempted to add window with token that is not a window: "
1809 + attrs.token + ". Aborting.");
1810 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1811 }
1812 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1813 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1814 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1815 + attrs.token + ". Aborting.");
1816 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1817 }
1818 }
1819
1820 boolean addToken = false;
1821 WindowToken token = mTokenMap.get(attrs.token);
1822 if (token == null) {
1823 if (attrs.type >= FIRST_APPLICATION_WINDOW
1824 && attrs.type <= LAST_APPLICATION_WINDOW) {
1825 Log.w(TAG, "Attempted to add application window with unknown token "
1826 + attrs.token + ". Aborting.");
1827 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1828 }
1829 if (attrs.type == TYPE_INPUT_METHOD) {
1830 Log.w(TAG, "Attempted to add input method window with unknown token "
1831 + attrs.token + ". Aborting.");
1832 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1833 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001834 if (attrs.type == TYPE_WALLPAPER) {
1835 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1836 + attrs.token + ". Aborting.");
1837 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 token = new WindowToken(attrs.token, -1, false);
1840 addToken = true;
1841 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1842 && attrs.type <= LAST_APPLICATION_WINDOW) {
1843 AppWindowToken atoken = token.appWindowToken;
1844 if (atoken == null) {
1845 Log.w(TAG, "Attempted to add window with non-application token "
1846 + token + ". Aborting.");
1847 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1848 } else if (atoken.removed) {
1849 Log.w(TAG, "Attempted to add window with exiting application token "
1850 + token + ". Aborting.");
1851 return WindowManagerImpl.ADD_APP_EXITING;
1852 }
1853 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1854 // No need for this guy!
1855 if (localLOGV) Log.v(
1856 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1857 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1858 }
1859 } else if (attrs.type == TYPE_INPUT_METHOD) {
1860 if (token.windowType != TYPE_INPUT_METHOD) {
1861 Log.w(TAG, "Attempted to add input method window with bad token "
1862 + attrs.token + ". Aborting.");
1863 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1864 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001865 } else if (attrs.type == TYPE_WALLPAPER) {
1866 if (token.windowType != TYPE_WALLPAPER) {
1867 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1868 + attrs.token + ". Aborting.");
1869 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
1872
1873 win = new WindowState(session, client, token,
1874 attachedWindow, attrs, viewVisibility);
1875 if (win.mDeathRecipient == null) {
1876 // Client has apparently died, so there is no reason to
1877 // continue.
1878 Log.w(TAG, "Adding window client " + client.asBinder()
1879 + " that is dead, aborting.");
1880 return WindowManagerImpl.ADD_APP_EXITING;
1881 }
1882
1883 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 res = mPolicy.prepareAddWindowLw(win, attrs);
1886 if (res != WindowManagerImpl.ADD_OKAY) {
1887 return res;
1888 }
1889
1890 // From now on, no exceptions or errors allowed!
1891
1892 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 if (addToken) {
1897 mTokenMap.put(attrs.token, token);
1898 mTokenList.add(token);
1899 }
1900 win.attach();
1901 mWindowMap.put(client.asBinder(), win);
1902
1903 if (attrs.type == TYPE_APPLICATION_STARTING &&
1904 token.appWindowToken != null) {
1905 token.appWindowToken.startingWindow = win;
1906 }
1907
1908 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 if (attrs.type == TYPE_INPUT_METHOD) {
1911 mInputMethodWindow = win;
1912 addInputMethodWindowToListLocked(win);
1913 imMayMove = false;
1914 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1915 mInputMethodDialogs.add(win);
1916 addWindowToListInOrderLocked(win, true);
1917 adjustInputMethodDialogsLocked();
1918 imMayMove = false;
1919 } else {
1920 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001921 if (attrs.type == TYPE_WALLPAPER) {
1922 mLastWallpaperTimeoutTime = 0;
1923 adjustWallpaperWindowsLocked();
1924 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001925 adjustWallpaperWindowsLocked();
1926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
Romain Guy06882f82009-06-10 13:36:04 -07001928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 if (mInTouchMode) {
1934 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1935 }
1936 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1937 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1938 }
Romain Guy06882f82009-06-10 13:36:04 -07001939
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001940 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001942 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1943 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 imMayMove = false;
1945 }
1946 }
Romain Guy06882f82009-06-10 13:36:04 -07001947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001949 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
Romain Guy06882f82009-06-10 13:36:04 -07001951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 assignLayersLocked();
1953 // Don't do layout here, the window must call
1954 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 //dump();
1957
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001958 if (focusChanged) {
1959 if (mCurrentFocus != null) {
1960 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1961 }
1962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 if (localLOGV) Log.v(
1964 TAG, "New client " + client.asBinder()
1965 + ": window=" + win);
1966 }
1967
1968 // sendNewConfiguration() checks caller permissions so we must call it with
1969 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1970 // identity anyway, so it's safe to just clear & restore around this whole
1971 // block.
1972 final long origId = Binder.clearCallingIdentity();
1973 if (reportNewConfig) {
1974 sendNewConfiguration();
1975 } else {
1976 // Update Orientation after adding a window, only if the window needs to be
1977 // displayed right away
1978 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001979 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 sendNewConfiguration();
1981 }
1982 }
1983 }
1984 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 return res;
1987 }
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 public void removeWindow(Session session, IWindow client) {
1990 synchronized(mWindowMap) {
1991 WindowState win = windowForClientLocked(session, client);
1992 if (win == null) {
1993 return;
1994 }
1995 removeWindowLocked(session, win);
1996 }
1997 }
Romain Guy06882f82009-06-10 13:36:04 -07001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 public void removeWindowLocked(Session session, WindowState win) {
2000
2001 if (localLOGV || DEBUG_FOCUS) Log.v(
2002 TAG, "Remove " + win + " client="
2003 + Integer.toHexString(System.identityHashCode(
2004 win.mClient.asBinder()))
2005 + ", surface=" + win.mSurface);
2006
2007 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 if (DEBUG_APP_TRANSITIONS) Log.v(
2010 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2011 + " mExiting=" + win.mExiting
2012 + " isAnimating=" + win.isAnimating()
2013 + " app-animation="
2014 + (win.mAppToken != null ? win.mAppToken.animation : null)
2015 + " inPendingTransaction="
2016 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2017 + " mDisplayFrozen=" + mDisplayFrozen);
2018 // Visibility of the removed window. Will be used later to update orientation later on.
2019 boolean wasVisible = false;
2020 // First, see if we need to run an animation. If we do, we have
2021 // to hold off on removing the window until the animation is done.
2022 // If the display is frozen, just remove immediately, since the
2023 // animation wouldn't be seen.
2024 if (win.mSurface != null && !mDisplayFrozen) {
2025 // If we are not currently running the exit animation, we
2026 // need to see about starting one.
2027 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2030 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2031 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2032 }
2033 // Try starting an animation.
2034 if (applyAnimationLocked(win, transit, false)) {
2035 win.mExiting = true;
2036 }
2037 }
2038 if (win.mExiting || win.isAnimating()) {
2039 // The exit animation is running... wait for it!
2040 //Log.i(TAG, "*** Running exit animation...");
2041 win.mExiting = true;
2042 win.mRemoveOnExit = true;
2043 mLayoutNeeded = true;
2044 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2045 performLayoutAndPlaceSurfacesLocked();
2046 if (win.mAppToken != null) {
2047 win.mAppToken.updateReportedVisibilityLocked();
2048 }
2049 //dump();
2050 Binder.restoreCallingIdentity(origId);
2051 return;
2052 }
2053 }
2054
2055 removeWindowInnerLocked(session, win);
2056 // Removing a visible window will effect the computed orientation
2057 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002058 if (wasVisible && computeForcedAppOrientationLocked()
2059 != mForcedAppOrientation) {
2060 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2063 Binder.restoreCallingIdentity(origId);
2064 }
Romain Guy06882f82009-06-10 13:36:04 -07002065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002067 mKeyWaiter.finishedKey(session, win.mClient, true,
2068 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2070 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 if (mInputMethodTarget == win) {
2075 moveInputMethodWindowsIfNeededLocked(false);
2076 }
Romain Guy06882f82009-06-10 13:36:04 -07002077
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002078 if (false) {
2079 RuntimeException e = new RuntimeException("here");
2080 e.fillInStackTrace();
2081 Log.w(TAG, "Removing window " + win, e);
2082 }
2083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 mPolicy.removeWindowLw(win);
2085 win.removeLocked();
2086
2087 mWindowMap.remove(win.mClient.asBinder());
2088 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002089 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090
2091 if (mInputMethodWindow == win) {
2092 mInputMethodWindow = null;
2093 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2094 mInputMethodDialogs.remove(win);
2095 }
Romain Guy06882f82009-06-10 13:36:04 -07002096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 final WindowToken token = win.mToken;
2098 final AppWindowToken atoken = win.mAppToken;
2099 token.windows.remove(win);
2100 if (atoken != null) {
2101 atoken.allAppWindows.remove(win);
2102 }
2103 if (localLOGV) Log.v(
2104 TAG, "**** Removing window " + win + ": count="
2105 + token.windows.size());
2106 if (token.windows.size() == 0) {
2107 if (!token.explicit) {
2108 mTokenMap.remove(token.token);
2109 mTokenList.remove(token);
2110 } else if (atoken != null) {
2111 atoken.firstWindowDrawn = false;
2112 }
2113 }
2114
2115 if (atoken != null) {
2116 if (atoken.startingWindow == win) {
2117 atoken.startingWindow = null;
2118 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2119 // If this is the last window and we had requested a starting
2120 // transition window, well there is no point now.
2121 atoken.startingData = null;
2122 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2123 // If this is the last window except for a starting transition
2124 // window, we need to get rid of the starting transition.
2125 if (DEBUG_STARTING_WINDOW) {
2126 Log.v(TAG, "Schedule remove starting " + token
2127 + ": no more real windows");
2128 }
2129 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2130 mH.sendMessage(m);
2131 }
2132 }
Romain Guy06882f82009-06-10 13:36:04 -07002133
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002134 if (win.mAttrs.type == TYPE_WALLPAPER) {
2135 mLastWallpaperTimeoutTime = 0;
2136 adjustWallpaperWindowsLocked();
2137 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002138 adjustWallpaperWindowsLocked();
2139 }
2140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 if (!mInLayout) {
2142 assignLayersLocked();
2143 mLayoutNeeded = true;
2144 performLayoutAndPlaceSurfacesLocked();
2145 if (win.mAppToken != null) {
2146 win.mAppToken.updateReportedVisibilityLocked();
2147 }
2148 }
2149 }
2150
2151 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2152 long origId = Binder.clearCallingIdentity();
2153 try {
2154 synchronized (mWindowMap) {
2155 WindowState w = windowForClientLocked(session, client);
2156 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002157 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 Surface.openTransaction();
2159 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002160 if (SHOW_TRANSACTIONS) Log.i(
2161 TAG, " SURFACE " + w.mSurface
2162 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 w.mSurface.setTransparentRegionHint(region);
2164 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002165 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 Surface.closeTransaction();
2167 }
2168 }
2169 }
2170 } finally {
2171 Binder.restoreCallingIdentity(origId);
2172 }
2173 }
2174
2175 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002176 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 Rect visibleInsets) {
2178 long origId = Binder.clearCallingIdentity();
2179 try {
2180 synchronized (mWindowMap) {
2181 WindowState w = windowForClientLocked(session, client);
2182 if (w != null) {
2183 w.mGivenInsetsPending = false;
2184 w.mGivenContentInsets.set(contentInsets);
2185 w.mGivenVisibleInsets.set(visibleInsets);
2186 w.mTouchableInsets = touchableInsets;
2187 mLayoutNeeded = true;
2188 performLayoutAndPlaceSurfacesLocked();
2189 }
2190 }
2191 } finally {
2192 Binder.restoreCallingIdentity(origId);
2193 }
2194 }
Romain Guy06882f82009-06-10 13:36:04 -07002195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 public void getWindowDisplayFrame(Session session, IWindow client,
2197 Rect outDisplayFrame) {
2198 synchronized(mWindowMap) {
2199 WindowState win = windowForClientLocked(session, client);
2200 if (win == null) {
2201 outDisplayFrame.setEmpty();
2202 return;
2203 }
2204 outDisplayFrame.set(win.mDisplayFrame);
2205 }
2206 }
2207
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002208 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2209 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002210 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2211 window.mWallpaperX = x;
2212 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002213 window.mWallpaperXStep = xStep;
2214 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002215 if (updateWallpaperOffsetLocked(window, true)) {
2216 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002217 }
2218 }
2219 }
2220
Dianne Hackborn75804932009-10-20 20:15:20 -07002221 void wallpaperCommandComplete(IBinder window, Bundle result) {
2222 synchronized (mWindowMap) {
2223 if (mWaitingOnWallpaper != null &&
2224 mWaitingOnWallpaper.mClient.asBinder() == window) {
2225 mWaitingOnWallpaper = null;
2226 mWindowMap.notifyAll();
2227 }
2228 }
2229 }
2230
2231 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2232 String action, int x, int y, int z, Bundle extras, boolean sync) {
2233 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2234 || window == mUpperWallpaperTarget) {
2235 boolean doWait = sync;
2236 int curTokenIndex = mWallpaperTokens.size();
2237 while (curTokenIndex > 0) {
2238 curTokenIndex--;
2239 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2240 int curWallpaperIndex = token.windows.size();
2241 while (curWallpaperIndex > 0) {
2242 curWallpaperIndex--;
2243 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2244 try {
2245 wallpaper.mClient.dispatchWallpaperCommand(action,
2246 x, y, z, extras, sync);
2247 // We only want to be synchronous with one wallpaper.
2248 sync = false;
2249 } catch (RemoteException e) {
2250 }
2251 }
2252 }
2253
2254 if (doWait) {
2255 // XXX Need to wait for result.
2256 }
2257 }
2258
2259 return null;
2260 }
2261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 public int relayoutWindow(Session session, IWindow client,
2263 WindowManager.LayoutParams attrs, int requestedWidth,
2264 int requestedHeight, int viewVisibility, boolean insetsPending,
2265 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2266 Surface outSurface) {
2267 boolean displayed = false;
2268 boolean inTouchMode;
2269 Configuration newConfig = null;
2270 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 synchronized(mWindowMap) {
2273 WindowState win = windowForClientLocked(session, client);
2274 if (win == null) {
2275 return 0;
2276 }
2277 win.mRequestedWidth = requestedWidth;
2278 win.mRequestedHeight = requestedHeight;
2279
2280 if (attrs != null) {
2281 mPolicy.adjustWindowParamsLw(attrs);
2282 }
Romain Guy06882f82009-06-10 13:36:04 -07002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 int attrChanges = 0;
2285 int flagChanges = 0;
2286 if (attrs != null) {
2287 flagChanges = win.mAttrs.flags ^= attrs.flags;
2288 attrChanges = win.mAttrs.copyFrom(attrs);
2289 }
2290
2291 if (localLOGV) Log.v(
2292 TAG, "Relayout given client " + client.asBinder()
2293 + " (" + win.mAttrs.getTitle() + ")");
2294
2295
2296 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2297 win.mAlpha = attrs.alpha;
2298 }
2299
2300 final boolean scaledWindow =
2301 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2302
2303 if (scaledWindow) {
2304 // requested{Width|Height} Surface's physical size
2305 // attrs.{width|height} Size on screen
2306 win.mHScale = (attrs.width != requestedWidth) ?
2307 (attrs.width / (float)requestedWidth) : 1.0f;
2308 win.mVScale = (attrs.height != requestedHeight) ?
2309 (attrs.height / (float)requestedHeight) : 1.0f;
2310 }
2311
2312 boolean imMayMove = (flagChanges&(
2313 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2314 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 boolean focusMayChange = win.mViewVisibility != viewVisibility
2317 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2318 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002319
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002320 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2321 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 win.mRelayoutCalled = true;
2324 final int oldVisibility = win.mViewVisibility;
2325 win.mViewVisibility = viewVisibility;
2326 if (viewVisibility == View.VISIBLE &&
2327 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2328 displayed = !win.isVisibleLw();
2329 if (win.mExiting) {
2330 win.mExiting = false;
2331 win.mAnimation = null;
2332 }
2333 if (win.mDestroying) {
2334 win.mDestroying = false;
2335 mDestroySurface.remove(win);
2336 }
2337 if (oldVisibility == View.GONE) {
2338 win.mEnterAnimationPending = true;
2339 }
2340 if (displayed && win.mSurface != null && !win.mDrawPending
2341 && !win.mCommitDrawPending && !mDisplayFrozen) {
2342 applyEnterAnimationLocked(win);
2343 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002344 if (displayed && (win.mAttrs.flags
2345 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2346 win.mTurnOnScreen = true;
2347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2349 // To change the format, we need to re-build the surface.
2350 win.destroySurfaceLocked();
2351 displayed = true;
2352 }
2353 try {
2354 Surface surface = win.createSurfaceLocked();
2355 if (surface != null) {
2356 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002357 win.mReportDestroySurface = false;
2358 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002359 if (SHOW_TRANSACTIONS) Log.i(TAG,
2360 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002362 // For some reason there isn't a surface. Clear the
2363 // caller's object so they see the same state.
2364 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
2366 } catch (Exception e) {
2367 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002368 + client + " (" + win.mAttrs.getTitle() + ")",
2369 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 Binder.restoreCallingIdentity(origId);
2371 return 0;
2372 }
2373 if (displayed) {
2374 focusMayChange = true;
2375 }
2376 if (win.mAttrs.type == TYPE_INPUT_METHOD
2377 && mInputMethodWindow == null) {
2378 mInputMethodWindow = win;
2379 imMayMove = true;
2380 }
2381 } else {
2382 win.mEnterAnimationPending = false;
2383 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002384 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2385 + ": mExiting=" + win.mExiting
2386 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 // If we are not currently running the exit animation, we
2388 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002389 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 // Try starting an animation; if there isn't one, we
2391 // can destroy the surface right away.
2392 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2393 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2394 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2395 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002396 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002398 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 win.mExiting = true;
2400 mKeyWaiter.finishedKey(session, client, true,
2401 KeyWaiter.RETURN_NOTHING);
2402 } else if (win.isAnimating()) {
2403 // Currently in a hide animation... turn this into
2404 // an exit.
2405 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002406 } else if (win == mWallpaperTarget) {
2407 // If the wallpaper is currently behind this
2408 // window, we need to change both of them inside
2409 // of a transaction to avoid artifacts.
2410 win.mExiting = true;
2411 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 } else {
2413 if (mInputMethodWindow == win) {
2414 mInputMethodWindow = null;
2415 }
2416 win.destroySurfaceLocked();
2417 }
2418 }
2419 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002420
2421 if (win.mSurface == null || (win.getAttrs().flags
2422 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2423 || win.mSurfacePendingDestroy) {
2424 // We are being called from a local process, which
2425 // means outSurface holds its current surface. Ensure the
2426 // surface object is cleared, but we don't want it actually
2427 // destroyed at this point.
2428 win.mSurfacePendingDestroy = false;
2429 outSurface.release();
2430 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2431 } else if (win.mSurface != null) {
2432 if (DEBUG_VISIBILITY) Log.i(TAG,
2433 "Keeping surface, will report destroy: " + win);
2434 win.mReportDestroySurface = true;
2435 outSurface.copyFrom(win.mSurface);
2436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 if (focusMayChange) {
2440 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2441 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 imMayMove = false;
2443 }
2444 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2445 }
Romain Guy06882f82009-06-10 13:36:04 -07002446
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002447 // updateFocusedWindowLocked() already assigned layers so we only need to
2448 // reassign them at this point if the IM window state gets shuffled
2449 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002452 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2453 // Little hack here -- we -should- be able to rely on the
2454 // function to return true if the IME has moved and needs
2455 // its layer recomputed. However, if the IME was hidden
2456 // and isn't actually moved in the list, its layer may be
2457 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 assignLayers = true;
2459 }
2460 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002461 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002462 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002463 assignLayers = true;
2464 }
2465 }
Romain Guy06882f82009-06-10 13:36:04 -07002466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 mLayoutNeeded = true;
2468 win.mGivenInsetsPending = insetsPending;
2469 if (assignLayers) {
2470 assignLayersLocked();
2471 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002472 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002474 if (displayed && win.mIsWallpaper) {
2475 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002476 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 if (win.mAppToken != null) {
2479 win.mAppToken.updateReportedVisibilityLocked();
2480 }
2481 outFrame.set(win.mFrame);
2482 outContentInsets.set(win.mContentInsets);
2483 outVisibleInsets.set(win.mVisibleInsets);
2484 if (localLOGV) Log.v(
2485 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002486 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 + ", requestedHeight=" + requestedHeight
2488 + ", viewVisibility=" + viewVisibility
2489 + "\nRelayout returning frame=" + outFrame
2490 + ", surface=" + outSurface);
2491
2492 if (localLOGV || DEBUG_FOCUS) Log.v(
2493 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2494
2495 inTouchMode = mInTouchMode;
2496 }
2497
2498 if (newConfig != null) {
2499 sendNewConfiguration();
2500 }
Romain Guy06882f82009-06-10 13:36:04 -07002501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2505 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2506 }
2507
2508 public void finishDrawingWindow(Session session, IWindow client) {
2509 final long origId = Binder.clearCallingIdentity();
2510 synchronized(mWindowMap) {
2511 WindowState win = windowForClientLocked(session, client);
2512 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002513 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2514 adjustWallpaperWindowsLocked();
2515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 mLayoutNeeded = true;
2517 performLayoutAndPlaceSurfacesLocked();
2518 }
2519 }
2520 Binder.restoreCallingIdentity(origId);
2521 }
2522
2523 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2524 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2525 + (lp != null ? lp.packageName : null)
2526 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2527 if (lp != null && lp.windowAnimations != 0) {
2528 // If this is a system resource, don't try to load it from the
2529 // application resources. It is nice to avoid loading application
2530 // resources if we can.
2531 String packageName = lp.packageName != null ? lp.packageName : "android";
2532 int resId = lp.windowAnimations;
2533 if ((resId&0xFF000000) == 0x01000000) {
2534 packageName = "android";
2535 }
2536 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2537 + packageName);
2538 return AttributeCache.instance().get(packageName, resId,
2539 com.android.internal.R.styleable.WindowAnimation);
2540 }
2541 return null;
2542 }
Romain Guy06882f82009-06-10 13:36:04 -07002543
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002544 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2545 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2546 + packageName + " resId=0x" + Integer.toHexString(resId));
2547 if (packageName != null) {
2548 if ((resId&0xFF000000) == 0x01000000) {
2549 packageName = "android";
2550 }
2551 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2552 + packageName);
2553 return AttributeCache.instance().get(packageName, resId,
2554 com.android.internal.R.styleable.WindowAnimation);
2555 }
2556 return null;
2557 }
2558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 private void applyEnterAnimationLocked(WindowState win) {
2560 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2561 if (win.mEnterAnimationPending) {
2562 win.mEnterAnimationPending = false;
2563 transit = WindowManagerPolicy.TRANSIT_ENTER;
2564 }
2565
2566 applyAnimationLocked(win, transit, true);
2567 }
2568
2569 private boolean applyAnimationLocked(WindowState win,
2570 int transit, boolean isEntrance) {
2571 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2572 // If we are trying to apply an animation, but already running
2573 // an animation of the same type, then just leave that one alone.
2574 return true;
2575 }
Romain Guy06882f82009-06-10 13:36:04 -07002576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 // Only apply an animation if the display isn't frozen. If it is
2578 // frozen, there is no reason to animate and it can cause strange
2579 // artifacts when we unfreeze the display if some different animation
2580 // is running.
2581 if (!mDisplayFrozen) {
2582 int anim = mPolicy.selectAnimationLw(win, transit);
2583 int attr = -1;
2584 Animation a = null;
2585 if (anim != 0) {
2586 a = AnimationUtils.loadAnimation(mContext, anim);
2587 } else {
2588 switch (transit) {
2589 case WindowManagerPolicy.TRANSIT_ENTER:
2590 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2591 break;
2592 case WindowManagerPolicy.TRANSIT_EXIT:
2593 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2594 break;
2595 case WindowManagerPolicy.TRANSIT_SHOW:
2596 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2597 break;
2598 case WindowManagerPolicy.TRANSIT_HIDE:
2599 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2600 break;
2601 }
2602 if (attr >= 0) {
2603 a = loadAnimation(win.mAttrs, attr);
2604 }
2605 }
2606 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2607 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2608 + " mAnimation=" + win.mAnimation
2609 + " isEntrance=" + isEntrance);
2610 if (a != null) {
2611 if (DEBUG_ANIM) {
2612 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002613 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2615 }
2616 win.setAnimation(a);
2617 win.mAnimationIsEntrance = isEntrance;
2618 }
2619 } else {
2620 win.clearAnimation();
2621 }
2622
2623 return win.mAnimation != null;
2624 }
2625
2626 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2627 int anim = 0;
2628 Context context = mContext;
2629 if (animAttr >= 0) {
2630 AttributeCache.Entry ent = getCachedAnimations(lp);
2631 if (ent != null) {
2632 context = ent.context;
2633 anim = ent.array.getResourceId(animAttr, 0);
2634 }
2635 }
2636 if (anim != 0) {
2637 return AnimationUtils.loadAnimation(context, anim);
2638 }
2639 return null;
2640 }
Romain Guy06882f82009-06-10 13:36:04 -07002641
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002642 private Animation loadAnimation(String packageName, int resId) {
2643 int anim = 0;
2644 Context context = mContext;
2645 if (resId >= 0) {
2646 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2647 if (ent != null) {
2648 context = ent.context;
2649 anim = resId;
2650 }
2651 }
2652 if (anim != 0) {
2653 return AnimationUtils.loadAnimation(context, anim);
2654 }
2655 return null;
2656 }
2657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 private boolean applyAnimationLocked(AppWindowToken wtoken,
2659 WindowManager.LayoutParams lp, int transit, boolean enter) {
2660 // Only apply an animation if the display isn't frozen. If it is
2661 // frozen, there is no reason to animate and it can cause strange
2662 // artifacts when we unfreeze the display if some different animation
2663 // is running.
2664 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002665 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002666 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002667 a = new FadeInOutAnimation(enter);
2668 if (DEBUG_ANIM) Log.v(TAG,
2669 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002670 } else if (mNextAppTransitionPackage != null) {
2671 a = loadAnimation(mNextAppTransitionPackage, enter ?
2672 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002673 } else {
2674 int animAttr = 0;
2675 switch (transit) {
2676 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2677 animAttr = enter
2678 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2679 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2680 break;
2681 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2682 animAttr = enter
2683 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2684 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2685 break;
2686 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2687 animAttr = enter
2688 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2689 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2690 break;
2691 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2692 animAttr = enter
2693 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2694 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2695 break;
2696 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2697 animAttr = enter
2698 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2699 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2700 break;
2701 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2702 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002703 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002704 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2705 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002706 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002707 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002708 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2709 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002710 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002711 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002712 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002713 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2714 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2715 break;
2716 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2717 animAttr = enter
2718 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2719 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2720 break;
2721 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2722 animAttr = enter
2723 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2724 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002725 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002726 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002727 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002728 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2729 + " anim=" + a
2730 + " animAttr=0x" + Integer.toHexString(animAttr)
2731 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 if (a != null) {
2734 if (DEBUG_ANIM) {
2735 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002736 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2738 }
2739 wtoken.setAnimation(a);
2740 }
2741 } else {
2742 wtoken.clearAnimation();
2743 }
2744
2745 return wtoken.animation != null;
2746 }
2747
2748 // -------------------------------------------------------------
2749 // Application Window Tokens
2750 // -------------------------------------------------------------
2751
2752 public void validateAppTokens(List tokens) {
2753 int v = tokens.size()-1;
2754 int m = mAppTokens.size()-1;
2755 while (v >= 0 && m >= 0) {
2756 AppWindowToken wtoken = mAppTokens.get(m);
2757 if (wtoken.removed) {
2758 m--;
2759 continue;
2760 }
2761 if (tokens.get(v) != wtoken.token) {
2762 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2763 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2764 }
2765 v--;
2766 m--;
2767 }
2768 while (v >= 0) {
2769 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2770 v--;
2771 }
2772 while (m >= 0) {
2773 AppWindowToken wtoken = mAppTokens.get(m);
2774 if (!wtoken.removed) {
2775 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2776 }
2777 m--;
2778 }
2779 }
2780
2781 boolean checkCallingPermission(String permission, String func) {
2782 // Quick check: if the calling permission is me, it's all okay.
2783 if (Binder.getCallingPid() == Process.myPid()) {
2784 return true;
2785 }
Romain Guy06882f82009-06-10 13:36:04 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 if (mContext.checkCallingPermission(permission)
2788 == PackageManager.PERMISSION_GRANTED) {
2789 return true;
2790 }
2791 String msg = "Permission Denial: " + func + " from pid="
2792 + Binder.getCallingPid()
2793 + ", uid=" + Binder.getCallingUid()
2794 + " requires " + permission;
2795 Log.w(TAG, msg);
2796 return false;
2797 }
Romain Guy06882f82009-06-10 13:36:04 -07002798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 AppWindowToken findAppWindowToken(IBinder token) {
2800 WindowToken wtoken = mTokenMap.get(token);
2801 if (wtoken == null) {
2802 return null;
2803 }
2804 return wtoken.appWindowToken;
2805 }
Romain Guy06882f82009-06-10 13:36:04 -07002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 public void addWindowToken(IBinder token, int type) {
2808 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2809 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002810 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Romain Guy06882f82009-06-10 13:36:04 -07002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 synchronized(mWindowMap) {
2814 WindowToken wtoken = mTokenMap.get(token);
2815 if (wtoken != null) {
2816 Log.w(TAG, "Attempted to add existing input method token: " + token);
2817 return;
2818 }
2819 wtoken = new WindowToken(token, type, true);
2820 mTokenMap.put(token, wtoken);
2821 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002822 if (type == TYPE_WALLPAPER) {
2823 mWallpaperTokens.add(wtoken);
2824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 }
2826 }
Romain Guy06882f82009-06-10 13:36:04 -07002827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 public void removeWindowToken(IBinder token) {
2829 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2830 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002831 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 }
2833
2834 final long origId = Binder.clearCallingIdentity();
2835 synchronized(mWindowMap) {
2836 WindowToken wtoken = mTokenMap.remove(token);
2837 mTokenList.remove(wtoken);
2838 if (wtoken != null) {
2839 boolean delayed = false;
2840 if (!wtoken.hidden) {
2841 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 final int N = wtoken.windows.size();
2844 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 for (int i=0; i<N; i++) {
2847 WindowState win = wtoken.windows.get(i);
2848
2849 if (win.isAnimating()) {
2850 delayed = true;
2851 }
Romain Guy06882f82009-06-10 13:36:04 -07002852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 if (win.isVisibleNow()) {
2854 applyAnimationLocked(win,
2855 WindowManagerPolicy.TRANSIT_EXIT, false);
2856 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2857 KeyWaiter.RETURN_NOTHING);
2858 changed = true;
2859 }
2860 }
2861
2862 if (changed) {
2863 mLayoutNeeded = true;
2864 performLayoutAndPlaceSurfacesLocked();
2865 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2866 }
Romain Guy06882f82009-06-10 13:36:04 -07002867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 if (delayed) {
2869 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002870 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2871 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 }
2873 }
Romain Guy06882f82009-06-10 13:36:04 -07002874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 } else {
2876 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2877 }
2878 }
2879 Binder.restoreCallingIdentity(origId);
2880 }
2881
2882 public void addAppToken(int addPos, IApplicationToken token,
2883 int groupId, int requestedOrientation, boolean fullscreen) {
2884 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2885 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002886 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 }
Romain Guy06882f82009-06-10 13:36:04 -07002888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 synchronized(mWindowMap) {
2890 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2891 if (wtoken != null) {
2892 Log.w(TAG, "Attempted to add existing app token: " + token);
2893 return;
2894 }
2895 wtoken = new AppWindowToken(token);
2896 wtoken.groupId = groupId;
2897 wtoken.appFullscreen = fullscreen;
2898 wtoken.requestedOrientation = requestedOrientation;
2899 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002900 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 mTokenMap.put(token.asBinder(), wtoken);
2902 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 // Application tokens start out hidden.
2905 wtoken.hidden = true;
2906 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 //dump();
2909 }
2910 }
Romain Guy06882f82009-06-10 13:36:04 -07002911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 public void setAppGroupId(IBinder token, int groupId) {
2913 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2914 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002915 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 }
2917
2918 synchronized(mWindowMap) {
2919 AppWindowToken wtoken = findAppWindowToken(token);
2920 if (wtoken == null) {
2921 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2922 return;
2923 }
2924 wtoken.groupId = groupId;
2925 }
2926 }
Romain Guy06882f82009-06-10 13:36:04 -07002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 public int getOrientationFromWindowsLocked() {
2929 int pos = mWindows.size() - 1;
2930 while (pos >= 0) {
2931 WindowState wtoken = (WindowState) mWindows.get(pos);
2932 pos--;
2933 if (wtoken.mAppToken != null) {
2934 // We hit an application window. so the orientation will be determined by the
2935 // app window. No point in continuing further.
2936 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2937 }
2938 if (!wtoken.isVisibleLw()) {
2939 continue;
2940 }
2941 int req = wtoken.mAttrs.screenOrientation;
2942 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2943 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2944 continue;
2945 } else {
2946 return req;
2947 }
2948 }
2949 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2950 }
Romain Guy06882f82009-06-10 13:36:04 -07002951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 public int getOrientationFromAppTokensLocked() {
2953 int pos = mAppTokens.size() - 1;
2954 int curGroup = 0;
2955 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002956 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002958 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 while (pos >= 0) {
2960 AppWindowToken wtoken = mAppTokens.get(pos);
2961 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002962 // if we're about to tear down this window and not seek for
2963 // the behind activity, don't use it for orientation
2964 if (!findingBehind
2965 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002966 continue;
2967 }
2968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 if (!haveGroup) {
2970 // We ignore any hidden applications on the top.
2971 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2972 continue;
2973 }
2974 haveGroup = true;
2975 curGroup = wtoken.groupId;
2976 lastOrientation = wtoken.requestedOrientation;
2977 } else if (curGroup != wtoken.groupId) {
2978 // If we have hit a new application group, and the bottom
2979 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002980 // the orientation behind it, and the last app was
2981 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002983 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2984 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 return lastOrientation;
2986 }
2987 }
2988 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002989 // If this application is fullscreen, and didn't explicitly say
2990 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002992 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002993 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002994 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 return or;
2996 }
2997 // If this application has requested an explicit orientation,
2998 // then use it.
2999 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3000 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3001 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3002 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3003 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3004 return or;
3005 }
Owen Lin3413b892009-05-01 17:12:32 -07003006 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 }
3008 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3009 }
Romain Guy06882f82009-06-10 13:36:04 -07003010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003012 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003013 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3014 "updateOrientationFromAppTokens()")) {
3015 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3016 }
3017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 Configuration config;
3019 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003020 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3021 freezeThisOneIfNeeded);
3022 Binder.restoreCallingIdentity(ident);
3023 return config;
3024 }
3025
3026 Configuration updateOrientationFromAppTokensUnchecked(
3027 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3028 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003030 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003031 if (config != null) {
3032 mLayoutNeeded = true;
3033 performLayoutAndPlaceSurfacesLocked();
3034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 return config;
3037 }
Romain Guy06882f82009-06-10 13:36:04 -07003038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 /*
3040 * The orientation is computed from non-application windows first. If none of
3041 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003042 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3044 * android.os.IBinder)
3045 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003046 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003047 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 long ident = Binder.clearCallingIdentity();
3050 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003051 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 if (req != mForcedAppOrientation) {
3054 changed = true;
3055 mForcedAppOrientation = req;
3056 //send a message to Policy indicating orientation change to take
3057 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003058 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
Romain Guy06882f82009-06-10 13:36:04 -07003060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 if (changed) {
3062 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003063 WindowManagerPolicy.USE_LAST_ROTATION,
3064 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 if (changed) {
3066 if (freezeThisOneIfNeeded != null) {
3067 AppWindowToken wtoken = findAppWindowToken(
3068 freezeThisOneIfNeeded);
3069 if (wtoken != null) {
3070 startAppFreezingScreenLocked(wtoken,
3071 ActivityInfo.CONFIG_ORIENTATION);
3072 }
3073 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003074 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 }
3076 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003077
3078 // No obvious action we need to take, but if our current
3079 // state mismatches the activity maanager's, update it
3080 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003081 mTempConfiguration.setToDefaults();
3082 if (computeNewConfigurationLocked(mTempConfiguration)) {
3083 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003084 return new Configuration(mTempConfiguration);
3085 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003086 }
3087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 } finally {
3089 Binder.restoreCallingIdentity(ident);
3090 }
Romain Guy06882f82009-06-10 13:36:04 -07003091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 return null;
3093 }
Romain Guy06882f82009-06-10 13:36:04 -07003094
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003095 int computeForcedAppOrientationLocked() {
3096 int req = getOrientationFromWindowsLocked();
3097 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3098 req = getOrientationFromAppTokensLocked();
3099 }
3100 return req;
3101 }
Romain Guy06882f82009-06-10 13:36:04 -07003102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3104 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3105 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003106 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 }
Romain Guy06882f82009-06-10 13:36:04 -07003108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 synchronized(mWindowMap) {
3110 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3111 if (wtoken == null) {
3112 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3113 return;
3114 }
Romain Guy06882f82009-06-10 13:36:04 -07003115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 wtoken.requestedOrientation = requestedOrientation;
3117 }
3118 }
Romain Guy06882f82009-06-10 13:36:04 -07003119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 public int getAppOrientation(IApplicationToken token) {
3121 synchronized(mWindowMap) {
3122 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3123 if (wtoken == null) {
3124 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3125 }
Romain Guy06882f82009-06-10 13:36:04 -07003126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 return wtoken.requestedOrientation;
3128 }
3129 }
Romain Guy06882f82009-06-10 13:36:04 -07003130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3132 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3133 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003134 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 }
3136
3137 synchronized(mWindowMap) {
3138 boolean changed = false;
3139 if (token == null) {
3140 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3141 changed = mFocusedApp != null;
3142 mFocusedApp = null;
3143 mKeyWaiter.tickle();
3144 } else {
3145 AppWindowToken newFocus = findAppWindowToken(token);
3146 if (newFocus == null) {
3147 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3148 return;
3149 }
3150 changed = mFocusedApp != newFocus;
3151 mFocusedApp = newFocus;
3152 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3153 mKeyWaiter.tickle();
3154 }
3155
3156 if (moveFocusNow && changed) {
3157 final long origId = Binder.clearCallingIdentity();
3158 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3159 Binder.restoreCallingIdentity(origId);
3160 }
3161 }
3162 }
3163
3164 public void prepareAppTransition(int transit) {
3165 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3166 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003167 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 }
Romain Guy06882f82009-06-10 13:36:04 -07003169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 synchronized(mWindowMap) {
3171 if (DEBUG_APP_TRANSITIONS) Log.v(
3172 TAG, "Prepare app transition: transit=" + transit
3173 + " mNextAppTransition=" + mNextAppTransition);
3174 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003175 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3176 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003178 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3179 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3180 // Opening a new task always supersedes a close for the anim.
3181 mNextAppTransition = transit;
3182 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3183 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3184 // Opening a new activity always supersedes a close for the anim.
3185 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
3187 mAppTransitionReady = false;
3188 mAppTransitionTimeout = false;
3189 mStartingIconInTransition = false;
3190 mSkipAppTransitionAnimation = false;
3191 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3192 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3193 5000);
3194 }
3195 }
3196 }
3197
3198 public int getPendingAppTransition() {
3199 return mNextAppTransition;
3200 }
Romain Guy06882f82009-06-10 13:36:04 -07003201
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003202 public void overridePendingAppTransition(String packageName,
3203 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003204 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003205 mNextAppTransitionPackage = packageName;
3206 mNextAppTransitionEnter = enterAnim;
3207 mNextAppTransitionExit = exitAnim;
3208 }
3209 }
3210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 public void executeAppTransition() {
3212 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3213 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003214 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
Romain Guy06882f82009-06-10 13:36:04 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003218 if (DEBUG_APP_TRANSITIONS) {
3219 RuntimeException e = new RuntimeException("here");
3220 e.fillInStackTrace();
3221 Log.w(TAG, "Execute app transition: mNextAppTransition="
3222 + mNextAppTransition, e);
3223 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003224 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 mAppTransitionReady = true;
3226 final long origId = Binder.clearCallingIdentity();
3227 performLayoutAndPlaceSurfacesLocked();
3228 Binder.restoreCallingIdentity(origId);
3229 }
3230 }
3231 }
3232
3233 public void setAppStartingWindow(IBinder token, String pkg,
3234 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3235 IBinder transferFrom, boolean createIfNeeded) {
3236 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3237 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003238 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 }
3240
3241 synchronized(mWindowMap) {
3242 if (DEBUG_STARTING_WINDOW) Log.v(
3243 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3244 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 AppWindowToken wtoken = findAppWindowToken(token);
3247 if (wtoken == null) {
3248 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3249 return;
3250 }
3251
3252 // If the display is frozen, we won't do anything until the
3253 // actual window is displayed so there is no reason to put in
3254 // the starting window.
3255 if (mDisplayFrozen) {
3256 return;
3257 }
Romain Guy06882f82009-06-10 13:36:04 -07003258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 if (wtoken.startingData != null) {
3260 return;
3261 }
Romain Guy06882f82009-06-10 13:36:04 -07003262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 if (transferFrom != null) {
3264 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3265 if (ttoken != null) {
3266 WindowState startingWindow = ttoken.startingWindow;
3267 if (startingWindow != null) {
3268 if (mStartingIconInTransition) {
3269 // In this case, the starting icon has already
3270 // been displayed, so start letting windows get
3271 // shown immediately without any more transitions.
3272 mSkipAppTransitionAnimation = true;
3273 }
3274 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3275 "Moving existing starting from " + ttoken
3276 + " to " + wtoken);
3277 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 // Transfer the starting window over to the new
3280 // token.
3281 wtoken.startingData = ttoken.startingData;
3282 wtoken.startingView = ttoken.startingView;
3283 wtoken.startingWindow = startingWindow;
3284 ttoken.startingData = null;
3285 ttoken.startingView = null;
3286 ttoken.startingWindow = null;
3287 ttoken.startingMoved = true;
3288 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003289 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003291 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3292 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 mWindows.remove(startingWindow);
3294 ttoken.windows.remove(startingWindow);
3295 ttoken.allAppWindows.remove(startingWindow);
3296 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 // Propagate other interesting state between the
3299 // tokens. If the old token is displayed, we should
3300 // immediately force the new one to be displayed. If
3301 // it is animating, we need to move that animation to
3302 // the new one.
3303 if (ttoken.allDrawn) {
3304 wtoken.allDrawn = true;
3305 }
3306 if (ttoken.firstWindowDrawn) {
3307 wtoken.firstWindowDrawn = true;
3308 }
3309 if (!ttoken.hidden) {
3310 wtoken.hidden = false;
3311 wtoken.hiddenRequested = false;
3312 wtoken.willBeHidden = false;
3313 }
3314 if (wtoken.clientHidden != ttoken.clientHidden) {
3315 wtoken.clientHidden = ttoken.clientHidden;
3316 wtoken.sendAppVisibilityToClients();
3317 }
3318 if (ttoken.animation != null) {
3319 wtoken.animation = ttoken.animation;
3320 wtoken.animating = ttoken.animating;
3321 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3322 ttoken.animation = null;
3323 ttoken.animLayerAdjustment = 0;
3324 wtoken.updateLayers();
3325 ttoken.updateLayers();
3326 }
Romain Guy06882f82009-06-10 13:36:04 -07003327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 mLayoutNeeded = true;
3330 performLayoutAndPlaceSurfacesLocked();
3331 Binder.restoreCallingIdentity(origId);
3332 return;
3333 } else if (ttoken.startingData != null) {
3334 // The previous app was getting ready to show a
3335 // starting window, but hasn't yet done so. Steal it!
3336 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3337 "Moving pending starting from " + ttoken
3338 + " to " + wtoken);
3339 wtoken.startingData = ttoken.startingData;
3340 ttoken.startingData = null;
3341 ttoken.startingMoved = true;
3342 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3343 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3344 // want to process the message ASAP, before any other queued
3345 // messages.
3346 mH.sendMessageAtFrontOfQueue(m);
3347 return;
3348 }
3349 }
3350 }
3351
3352 // There is no existing starting window, and the caller doesn't
3353 // want us to create one, so that's it!
3354 if (!createIfNeeded) {
3355 return;
3356 }
Romain Guy06882f82009-06-10 13:36:04 -07003357
Dianne Hackborn284ac932009-08-28 10:34:25 -07003358 // If this is a translucent or wallpaper window, then don't
3359 // show a starting window -- the current effect (a full-screen
3360 // opaque starting window that fades away to the real contents
3361 // when it is ready) does not work for this.
3362 if (theme != 0) {
3363 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3364 com.android.internal.R.styleable.Window);
3365 if (ent.array.getBoolean(
3366 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3367 return;
3368 }
3369 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003370 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3371 return;
3372 }
3373 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003374 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3375 return;
3376 }
3377 }
3378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 mStartingIconInTransition = true;
3380 wtoken.startingData = new StartingData(
3381 pkg, theme, nonLocalizedLabel,
3382 labelRes, icon);
3383 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3384 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3385 // want to process the message ASAP, before any other queued
3386 // messages.
3387 mH.sendMessageAtFrontOfQueue(m);
3388 }
3389 }
3390
3391 public void setAppWillBeHidden(IBinder token) {
3392 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3393 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003394 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 }
3396
3397 AppWindowToken wtoken;
3398
3399 synchronized(mWindowMap) {
3400 wtoken = findAppWindowToken(token);
3401 if (wtoken == null) {
3402 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3403 return;
3404 }
3405 wtoken.willBeHidden = true;
3406 }
3407 }
Romain Guy06882f82009-06-10 13:36:04 -07003408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3410 boolean visible, int transit, boolean performLayout) {
3411 boolean delayed = false;
3412
3413 if (wtoken.clientHidden == visible) {
3414 wtoken.clientHidden = !visible;
3415 wtoken.sendAppVisibilityToClients();
3416 }
Romain Guy06882f82009-06-10 13:36:04 -07003417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 wtoken.willBeHidden = false;
3419 if (wtoken.hidden == visible) {
3420 final int N = wtoken.allAppWindows.size();
3421 boolean changed = false;
3422 if (DEBUG_APP_TRANSITIONS) Log.v(
3423 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3424 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003427
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003428 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 if (wtoken.animation == sDummyAnimation) {
3430 wtoken.animation = null;
3431 }
3432 applyAnimationLocked(wtoken, lp, transit, visible);
3433 changed = true;
3434 if (wtoken.animation != null) {
3435 delayed = runningAppAnimation = true;
3436 }
3437 }
Romain Guy06882f82009-06-10 13:36:04 -07003438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 for (int i=0; i<N; i++) {
3440 WindowState win = wtoken.allAppWindows.get(i);
3441 if (win == wtoken.startingWindow) {
3442 continue;
3443 }
3444
3445 if (win.isAnimating()) {
3446 delayed = true;
3447 }
Romain Guy06882f82009-06-10 13:36:04 -07003448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3450 //win.dump(" ");
3451 if (visible) {
3452 if (!win.isVisibleNow()) {
3453 if (!runningAppAnimation) {
3454 applyAnimationLocked(win,
3455 WindowManagerPolicy.TRANSIT_ENTER, true);
3456 }
3457 changed = true;
3458 }
3459 } else if (win.isVisibleNow()) {
3460 if (!runningAppAnimation) {
3461 applyAnimationLocked(win,
3462 WindowManagerPolicy.TRANSIT_EXIT, false);
3463 }
3464 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3465 KeyWaiter.RETURN_NOTHING);
3466 changed = true;
3467 }
3468 }
3469
3470 wtoken.hidden = wtoken.hiddenRequested = !visible;
3471 if (!visible) {
3472 unsetAppFreezingScreenLocked(wtoken, true, true);
3473 } else {
3474 // If we are being set visible, and the starting window is
3475 // not yet displayed, then make sure it doesn't get displayed.
3476 WindowState swin = wtoken.startingWindow;
3477 if (swin != null && (swin.mDrawPending
3478 || swin.mCommitDrawPending)) {
3479 swin.mPolicyVisibility = false;
3480 swin.mPolicyVisibilityAfterAnim = false;
3481 }
3482 }
Romain Guy06882f82009-06-10 13:36:04 -07003483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3485 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3486 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 if (changed && performLayout) {
3489 mLayoutNeeded = true;
3490 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 performLayoutAndPlaceSurfacesLocked();
3492 }
3493 }
3494
3495 if (wtoken.animation != null) {
3496 delayed = true;
3497 }
Romain Guy06882f82009-06-10 13:36:04 -07003498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 return delayed;
3500 }
3501
3502 public void setAppVisibility(IBinder token, boolean visible) {
3503 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3504 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003505 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507
3508 AppWindowToken wtoken;
3509
3510 synchronized(mWindowMap) {
3511 wtoken = findAppWindowToken(token);
3512 if (wtoken == null) {
3513 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3514 return;
3515 }
3516
3517 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3518 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003519 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3521 + "): mNextAppTransition=" + mNextAppTransition
3522 + " hidden=" + wtoken.hidden
3523 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3524 }
Romain Guy06882f82009-06-10 13:36:04 -07003525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 // If we are preparing an app transition, then delay changing
3527 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003528 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 // Already in requested state, don't do anything more.
3530 if (wtoken.hiddenRequested != visible) {
3531 return;
3532 }
3533 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 if (DEBUG_APP_TRANSITIONS) Log.v(
3536 TAG, "Setting dummy animation on: " + wtoken);
3537 wtoken.setDummyAnimation();
3538 mOpeningApps.remove(wtoken);
3539 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003540 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 wtoken.inPendingTransaction = true;
3542 if (visible) {
3543 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 wtoken.startingDisplayed = false;
3545 wtoken.startingMoved = false;
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003546
3547 // If the token is currently hidden (should be the
3548 // common case), then we need to set up to wait for
3549 // its windows to be ready.
3550 if (wtoken.hidden) {
3551 wtoken.allDrawn = false;
3552 wtoken.waitingToShow = true;
3553
3554 if (wtoken.clientHidden) {
3555 // In the case where we are making an app visible
3556 // but holding off for a transition, we still need
3557 // to tell the client to make its windows visible so
3558 // they get drawn. Otherwise, we will wait on
3559 // performing the transition until all windows have
3560 // been drawn, they never will be, and we are sad.
3561 wtoken.clientHidden = false;
3562 wtoken.sendAppVisibilityToClients();
3563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 }
3565 } else {
3566 mClosingApps.add(wtoken);
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003567
3568 // If the token is currently visible (should be the
3569 // common case), then set up to wait for it to be hidden.
3570 if (!wtoken.hidden) {
3571 wtoken.waitingToHide = true;
3572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 }
3574 return;
3575 }
Romain Guy06882f82009-06-10 13:36:04 -07003576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003578 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 wtoken.updateReportedVisibilityLocked();
3580 Binder.restoreCallingIdentity(origId);
3581 }
3582 }
3583
3584 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3585 boolean unfreezeSurfaceNow, boolean force) {
3586 if (wtoken.freezingScreen) {
3587 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3588 + " force=" + force);
3589 final int N = wtoken.allAppWindows.size();
3590 boolean unfrozeWindows = false;
3591 for (int i=0; i<N; i++) {
3592 WindowState w = wtoken.allAppWindows.get(i);
3593 if (w.mAppFreezing) {
3594 w.mAppFreezing = false;
3595 if (w.mSurface != null && !w.mOrientationChanging) {
3596 w.mOrientationChanging = true;
3597 }
3598 unfrozeWindows = true;
3599 }
3600 }
3601 if (force || unfrozeWindows) {
3602 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3603 wtoken.freezingScreen = false;
3604 mAppsFreezingScreen--;
3605 }
3606 if (unfreezeSurfaceNow) {
3607 if (unfrozeWindows) {
3608 mLayoutNeeded = true;
3609 performLayoutAndPlaceSurfacesLocked();
3610 }
3611 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3612 stopFreezingDisplayLocked();
3613 }
3614 }
3615 }
3616 }
Romain Guy06882f82009-06-10 13:36:04 -07003617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3619 int configChanges) {
3620 if (DEBUG_ORIENTATION) {
3621 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003622 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 Log.i(TAG, "Set freezing of " + wtoken.appToken
3624 + ": hidden=" + wtoken.hidden + " freezing="
3625 + wtoken.freezingScreen, e);
3626 }
3627 if (!wtoken.hiddenRequested) {
3628 if (!wtoken.freezingScreen) {
3629 wtoken.freezingScreen = true;
3630 mAppsFreezingScreen++;
3631 if (mAppsFreezingScreen == 1) {
3632 startFreezingDisplayLocked();
3633 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3634 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3635 5000);
3636 }
3637 }
3638 final int N = wtoken.allAppWindows.size();
3639 for (int i=0; i<N; i++) {
3640 WindowState w = wtoken.allAppWindows.get(i);
3641 w.mAppFreezing = true;
3642 }
3643 }
3644 }
Romain Guy06882f82009-06-10 13:36:04 -07003645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 public void startAppFreezingScreen(IBinder token, int configChanges) {
3647 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3648 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003649 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 }
3651
3652 synchronized(mWindowMap) {
3653 if (configChanges == 0 && !mDisplayFrozen) {
3654 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3655 return;
3656 }
Romain Guy06882f82009-06-10 13:36:04 -07003657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 AppWindowToken wtoken = findAppWindowToken(token);
3659 if (wtoken == null || wtoken.appToken == null) {
3660 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3661 return;
3662 }
3663 final long origId = Binder.clearCallingIdentity();
3664 startAppFreezingScreenLocked(wtoken, configChanges);
3665 Binder.restoreCallingIdentity(origId);
3666 }
3667 }
Romain Guy06882f82009-06-10 13:36:04 -07003668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 public void stopAppFreezingScreen(IBinder token, boolean force) {
3670 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3671 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003672 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 }
3674
3675 synchronized(mWindowMap) {
3676 AppWindowToken wtoken = findAppWindowToken(token);
3677 if (wtoken == null || wtoken.appToken == null) {
3678 return;
3679 }
3680 final long origId = Binder.clearCallingIdentity();
3681 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3682 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3683 unsetAppFreezingScreenLocked(wtoken, true, force);
3684 Binder.restoreCallingIdentity(origId);
3685 }
3686 }
Romain Guy06882f82009-06-10 13:36:04 -07003687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 public void removeAppToken(IBinder token) {
3689 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3690 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003691 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 }
3693
3694 AppWindowToken wtoken = null;
3695 AppWindowToken startingToken = null;
3696 boolean delayed = false;
3697
3698 final long origId = Binder.clearCallingIdentity();
3699 synchronized(mWindowMap) {
3700 WindowToken basewtoken = mTokenMap.remove(token);
3701 mTokenList.remove(basewtoken);
3702 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3703 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003704 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 wtoken.inPendingTransaction = false;
3706 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003707 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 if (mClosingApps.contains(wtoken)) {
3709 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003710 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003712 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 delayed = true;
3714 }
3715 if (DEBUG_APP_TRANSITIONS) Log.v(
3716 TAG, "Removing app " + wtoken + " delayed=" + delayed
3717 + " animation=" + wtoken.animation
3718 + " animating=" + wtoken.animating);
3719 if (delayed) {
3720 // set the token aside because it has an active animation to be finished
3721 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003722 } else {
3723 // Make sure there is no animation running on this token,
3724 // so any windows associated with it will be removed as
3725 // soon as their animations are complete
3726 wtoken.animation = null;
3727 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 }
3729 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003730 if (mLastEnterAnimToken == wtoken) {
3731 mLastEnterAnimToken = null;
3732 mLastEnterAnimParams = null;
3733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 wtoken.removed = true;
3735 if (wtoken.startingData != null) {
3736 startingToken = wtoken;
3737 }
3738 unsetAppFreezingScreenLocked(wtoken, true, true);
3739 if (mFocusedApp == wtoken) {
3740 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3741 mFocusedApp = null;
3742 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3743 mKeyWaiter.tickle();
3744 }
3745 } else {
3746 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3747 }
Romain Guy06882f82009-06-10 13:36:04 -07003748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 if (!delayed && wtoken != null) {
3750 wtoken.updateReportedVisibilityLocked();
3751 }
3752 }
3753 Binder.restoreCallingIdentity(origId);
3754
3755 if (startingToken != null) {
3756 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3757 + startingToken + ": app token removed");
3758 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3759 mH.sendMessage(m);
3760 }
3761 }
3762
3763 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3764 final int NW = token.windows.size();
3765 for (int i=0; i<NW; i++) {
3766 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003767 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 mWindows.remove(win);
3769 int j = win.mChildWindows.size();
3770 while (j > 0) {
3771 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003772 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3773 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3774 "Tmp removing child window " + cwin);
3775 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 }
3777 }
3778 return NW > 0;
3779 }
3780
3781 void dumpAppTokensLocked() {
3782 for (int i=mAppTokens.size()-1; i>=0; i--) {
3783 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3784 }
3785 }
Romain Guy06882f82009-06-10 13:36:04 -07003786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 void dumpWindowsLocked() {
3788 for (int i=mWindows.size()-1; i>=0; i--) {
3789 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3790 }
3791 }
Romain Guy06882f82009-06-10 13:36:04 -07003792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 private int findWindowOffsetLocked(int tokenPos) {
3794 final int NW = mWindows.size();
3795
3796 if (tokenPos >= mAppTokens.size()) {
3797 int i = NW;
3798 while (i > 0) {
3799 i--;
3800 WindowState win = (WindowState)mWindows.get(i);
3801 if (win.getAppToken() != null) {
3802 return i+1;
3803 }
3804 }
3805 }
3806
3807 while (tokenPos > 0) {
3808 // Find the first app token below the new position that has
3809 // a window displayed.
3810 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3811 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3812 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003813 if (wtoken.sendingToBottom) {
3814 if (DEBUG_REORDER) Log.v(TAG,
3815 "Skipping token -- currently sending to bottom");
3816 tokenPos--;
3817 continue;
3818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 int i = wtoken.windows.size();
3820 while (i > 0) {
3821 i--;
3822 WindowState win = wtoken.windows.get(i);
3823 int j = win.mChildWindows.size();
3824 while (j > 0) {
3825 j--;
3826 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003827 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 for (int pos=NW-1; pos>=0; pos--) {
3829 if (mWindows.get(pos) == cwin) {
3830 if (DEBUG_REORDER) Log.v(TAG,
3831 "Found child win @" + (pos+1));
3832 return pos+1;
3833 }
3834 }
3835 }
3836 }
3837 for (int pos=NW-1; pos>=0; pos--) {
3838 if (mWindows.get(pos) == win) {
3839 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3840 return pos+1;
3841 }
3842 }
3843 }
3844 tokenPos--;
3845 }
3846
3847 return 0;
3848 }
3849
3850 private final int reAddWindowLocked(int index, WindowState win) {
3851 final int NCW = win.mChildWindows.size();
3852 boolean added = false;
3853 for (int j=0; j<NCW; j++) {
3854 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3855 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003856 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3857 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 mWindows.add(index, win);
3859 index++;
3860 added = true;
3861 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003862 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3863 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 mWindows.add(index, cwin);
3865 index++;
3866 }
3867 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003868 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3869 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 mWindows.add(index, win);
3871 index++;
3872 }
3873 return index;
3874 }
Romain Guy06882f82009-06-10 13:36:04 -07003875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3877 final int NW = token.windows.size();
3878 for (int i=0; i<NW; i++) {
3879 index = reAddWindowLocked(index, token.windows.get(i));
3880 }
3881 return index;
3882 }
3883
3884 public void moveAppToken(int index, IBinder token) {
3885 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3886 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003887 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
3889
3890 synchronized(mWindowMap) {
3891 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3892 if (DEBUG_REORDER) dumpAppTokensLocked();
3893 final AppWindowToken wtoken = findAppWindowToken(token);
3894 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3895 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3896 + token + " (" + wtoken + ")");
3897 return;
3898 }
3899 mAppTokens.add(index, wtoken);
3900 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3901 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 final long origId = Binder.clearCallingIdentity();
3904 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3905 if (DEBUG_REORDER) dumpWindowsLocked();
3906 if (tmpRemoveAppWindowsLocked(wtoken)) {
3907 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3908 if (DEBUG_REORDER) dumpWindowsLocked();
3909 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3910 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3911 if (DEBUG_REORDER) dumpWindowsLocked();
3912 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 mLayoutNeeded = true;
3914 performLayoutAndPlaceSurfacesLocked();
3915 }
3916 Binder.restoreCallingIdentity(origId);
3917 }
3918 }
3919
3920 private void removeAppTokensLocked(List<IBinder> tokens) {
3921 // XXX This should be done more efficiently!
3922 // (take advantage of the fact that both lists should be
3923 // ordered in the same way.)
3924 int N = tokens.size();
3925 for (int i=0; i<N; i++) {
3926 IBinder token = tokens.get(i);
3927 final AppWindowToken wtoken = findAppWindowToken(token);
3928 if (!mAppTokens.remove(wtoken)) {
3929 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3930 + token + " (" + wtoken + ")");
3931 i--;
3932 N--;
3933 }
3934 }
3935 }
3936
Dianne Hackborna8f60182009-09-01 19:01:50 -07003937 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3938 boolean updateFocusAndLayout) {
3939 // First remove all of the windows from the list.
3940 tmpRemoveAppWindowsLocked(wtoken);
3941
3942 // Where to start adding?
3943 int pos = findWindowOffsetLocked(tokenPos);
3944
3945 // And now add them back at the correct place.
3946 pos = reAddAppWindowsLocked(pos, wtoken);
3947
3948 if (updateFocusAndLayout) {
3949 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3950 assignLayersLocked();
3951 }
3952 mLayoutNeeded = true;
3953 performLayoutAndPlaceSurfacesLocked();
3954 }
3955 }
3956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3958 // First remove all of the windows from the list.
3959 final int N = tokens.size();
3960 int i;
3961 for (i=0; i<N; i++) {
3962 WindowToken token = mTokenMap.get(tokens.get(i));
3963 if (token != null) {
3964 tmpRemoveAppWindowsLocked(token);
3965 }
3966 }
3967
3968 // Where to start adding?
3969 int pos = findWindowOffsetLocked(tokenPos);
3970
3971 // And now add them back at the correct place.
3972 for (i=0; i<N; i++) {
3973 WindowToken token = mTokenMap.get(tokens.get(i));
3974 if (token != null) {
3975 pos = reAddAppWindowsLocked(pos, token);
3976 }
3977 }
3978
Dianne Hackborna8f60182009-09-01 19:01:50 -07003979 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3980 assignLayersLocked();
3981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 mLayoutNeeded = true;
3983 performLayoutAndPlaceSurfacesLocked();
3984
3985 //dump();
3986 }
3987
3988 public void moveAppTokensToTop(List<IBinder> tokens) {
3989 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3990 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003991 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 }
3993
3994 final long origId = Binder.clearCallingIdentity();
3995 synchronized(mWindowMap) {
3996 removeAppTokensLocked(tokens);
3997 final int N = tokens.size();
3998 for (int i=0; i<N; i++) {
3999 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4000 if (wt != null) {
4001 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004002 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004003 mToTopApps.remove(wt);
4004 mToBottomApps.remove(wt);
4005 mToTopApps.add(wt);
4006 wt.sendingToBottom = false;
4007 wt.sendingToTop = true;
4008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 }
4010 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004011
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004012 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004013 moveAppWindowsLocked(tokens, mAppTokens.size());
4014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 }
4016 Binder.restoreCallingIdentity(origId);
4017 }
4018
4019 public void moveAppTokensToBottom(List<IBinder> tokens) {
4020 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4021 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004022 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
4024
4025 final long origId = Binder.clearCallingIdentity();
4026 synchronized(mWindowMap) {
4027 removeAppTokensLocked(tokens);
4028 final int N = tokens.size();
4029 int pos = 0;
4030 for (int i=0; i<N; i++) {
4031 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4032 if (wt != null) {
4033 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004034 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004035 mToTopApps.remove(wt);
4036 mToBottomApps.remove(wt);
4037 mToBottomApps.add(i, wt);
4038 wt.sendingToTop = false;
4039 wt.sendingToBottom = true;
4040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 pos++;
4042 }
4043 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004044
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004045 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004046 moveAppWindowsLocked(tokens, 0);
4047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 }
4049 Binder.restoreCallingIdentity(origId);
4050 }
4051
4052 // -------------------------------------------------------------
4053 // Misc IWindowSession methods
4054 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004057 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 != PackageManager.PERMISSION_GRANTED) {
4059 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4060 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004061 synchronized (mKeyguardTokenWatcher) {
4062 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 }
4065
4066 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004067 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 != PackageManager.PERMISSION_GRANTED) {
4069 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4070 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004071 synchronized (mKeyguardTokenWatcher) {
4072 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073
Mike Lockwood983ee092009-11-22 01:42:24 -05004074 if (!mKeyguardTokenWatcher.isAcquired()) {
4075 // If we are the last one to reenable the keyguard wait until
4076 // we have actaully finished reenabling until returning.
4077 // It is possible that reenableKeyguard() can be called before
4078 // the previous disableKeyguard() is handled, in which case
4079 // neither mKeyguardTokenWatcher.acquired() or released() would
4080 // be called. In that case mKeyguardDisabled will be false here
4081 // and we have nothing to wait for.
4082 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004084 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 } catch (InterruptedException e) {
4086 Thread.currentThread().interrupt();
4087 }
4088 }
4089 }
4090 }
4091 }
4092
4093 /**
4094 * @see android.app.KeyguardManager#exitKeyguardSecurely
4095 */
4096 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004097 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 != PackageManager.PERMISSION_GRANTED) {
4099 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4100 }
4101 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4102 public void onKeyguardExitResult(boolean success) {
4103 try {
4104 callback.onKeyguardExitResult(success);
4105 } catch (RemoteException e) {
4106 // Client has died, we don't care.
4107 }
4108 }
4109 });
4110 }
4111
4112 public boolean inKeyguardRestrictedInputMode() {
4113 return mPolicy.inKeyguardRestrictedKeyInputMode();
4114 }
Romain Guy06882f82009-06-10 13:36:04 -07004115
Dianne Hackbornffa42482009-09-23 22:20:11 -07004116 public void closeSystemDialogs(String reason) {
4117 synchronized(mWindowMap) {
4118 for (int i=mWindows.size()-1; i>=0; i--) {
4119 WindowState w = (WindowState)mWindows.get(i);
4120 if (w.mSurface != null) {
4121 try {
4122 w.mClient.closeSystemDialogs(reason);
4123 } catch (RemoteException e) {
4124 }
4125 }
4126 }
4127 }
4128 }
4129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 static float fixScale(float scale) {
4131 if (scale < 0) scale = 0;
4132 else if (scale > 20) scale = 20;
4133 return Math.abs(scale);
4134 }
Romain Guy06882f82009-06-10 13:36:04 -07004135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 public void setAnimationScale(int which, float scale) {
4137 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4138 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004139 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 }
4141
4142 if (scale < 0) scale = 0;
4143 else if (scale > 20) scale = 20;
4144 scale = Math.abs(scale);
4145 switch (which) {
4146 case 0: mWindowAnimationScale = fixScale(scale); break;
4147 case 1: mTransitionAnimationScale = fixScale(scale); break;
4148 }
Romain Guy06882f82009-06-10 13:36:04 -07004149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 // Persist setting
4151 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4152 }
Romain Guy06882f82009-06-10 13:36:04 -07004153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 public void setAnimationScales(float[] scales) {
4155 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4156 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004157 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 }
4159
4160 if (scales != null) {
4161 if (scales.length >= 1) {
4162 mWindowAnimationScale = fixScale(scales[0]);
4163 }
4164 if (scales.length >= 2) {
4165 mTransitionAnimationScale = fixScale(scales[1]);
4166 }
4167 }
Romain Guy06882f82009-06-10 13:36:04 -07004168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 // Persist setting
4170 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4171 }
Romain Guy06882f82009-06-10 13:36:04 -07004172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 public float getAnimationScale(int which) {
4174 switch (which) {
4175 case 0: return mWindowAnimationScale;
4176 case 1: return mTransitionAnimationScale;
4177 }
4178 return 0;
4179 }
Romain Guy06882f82009-06-10 13:36:04 -07004180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 public float[] getAnimationScales() {
4182 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4183 }
Romain Guy06882f82009-06-10 13:36:04 -07004184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 public int getSwitchState(int sw) {
4186 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4187 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004188 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 }
4190 return KeyInputQueue.getSwitchState(sw);
4191 }
Romain Guy06882f82009-06-10 13:36:04 -07004192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 public int getSwitchStateForDevice(int devid, int sw) {
4194 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4195 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004196 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198 return KeyInputQueue.getSwitchState(devid, sw);
4199 }
Romain Guy06882f82009-06-10 13:36:04 -07004200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 public int getScancodeState(int sw) {
4202 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4203 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004204 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004206 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 }
Romain Guy06882f82009-06-10 13:36:04 -07004208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 public int getScancodeStateForDevice(int devid, int sw) {
4210 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4211 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004212 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004214 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 }
Romain Guy06882f82009-06-10 13:36:04 -07004216
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004217 public int getTrackballScancodeState(int sw) {
4218 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4219 "getTrackballScancodeState()")) {
4220 throw new SecurityException("Requires READ_INPUT_STATE permission");
4221 }
4222 return mQueue.getTrackballScancodeState(sw);
4223 }
4224
4225 public int getDPadScancodeState(int sw) {
4226 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4227 "getDPadScancodeState()")) {
4228 throw new SecurityException("Requires READ_INPUT_STATE permission");
4229 }
4230 return mQueue.getDPadScancodeState(sw);
4231 }
4232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 public int getKeycodeState(int sw) {
4234 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4235 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004236 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004238 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 }
Romain Guy06882f82009-06-10 13:36:04 -07004240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 public int getKeycodeStateForDevice(int devid, int sw) {
4242 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4243 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004244 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004246 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 }
Romain Guy06882f82009-06-10 13:36:04 -07004248
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004249 public int getTrackballKeycodeState(int sw) {
4250 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4251 "getTrackballKeycodeState()")) {
4252 throw new SecurityException("Requires READ_INPUT_STATE permission");
4253 }
4254 return mQueue.getTrackballKeycodeState(sw);
4255 }
4256
4257 public int getDPadKeycodeState(int sw) {
4258 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4259 "getDPadKeycodeState()")) {
4260 throw new SecurityException("Requires READ_INPUT_STATE permission");
4261 }
4262 return mQueue.getDPadKeycodeState(sw);
4263 }
4264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4266 return KeyInputQueue.hasKeys(keycodes, keyExists);
4267 }
Romain Guy06882f82009-06-10 13:36:04 -07004268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 public void enableScreenAfterBoot() {
4270 synchronized(mWindowMap) {
4271 if (mSystemBooted) {
4272 return;
4273 }
4274 mSystemBooted = true;
4275 }
Romain Guy06882f82009-06-10 13:36:04 -07004276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 performEnableScreen();
4278 }
Romain Guy06882f82009-06-10 13:36:04 -07004279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 public void enableScreenIfNeededLocked() {
4281 if (mDisplayEnabled) {
4282 return;
4283 }
4284 if (!mSystemBooted) {
4285 return;
4286 }
4287 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4288 }
Romain Guy06882f82009-06-10 13:36:04 -07004289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 public void performEnableScreen() {
4291 synchronized(mWindowMap) {
4292 if (mDisplayEnabled) {
4293 return;
4294 }
4295 if (!mSystemBooted) {
4296 return;
4297 }
Romain Guy06882f82009-06-10 13:36:04 -07004298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 // Don't enable the screen until all existing windows
4300 // have been drawn.
4301 final int N = mWindows.size();
4302 for (int i=0; i<N; i++) {
4303 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004304 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 return;
4306 }
4307 }
Romain Guy06882f82009-06-10 13:36:04 -07004308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 mDisplayEnabled = true;
4310 if (false) {
4311 Log.i(TAG, "ENABLING SCREEN!");
4312 StringWriter sw = new StringWriter();
4313 PrintWriter pw = new PrintWriter(sw);
4314 this.dump(null, pw, null);
4315 Log.i(TAG, sw.toString());
4316 }
4317 try {
4318 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4319 if (surfaceFlinger != null) {
4320 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4321 Parcel data = Parcel.obtain();
4322 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4323 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4324 data, null, 0);
4325 data.recycle();
4326 }
4327 } catch (RemoteException ex) {
4328 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4329 }
4330 }
Romain Guy06882f82009-06-10 13:36:04 -07004331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004335 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4336 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 }
Romain Guy06882f82009-06-10 13:36:04 -07004338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 public void setInTouchMode(boolean mode) {
4340 synchronized(mWindowMap) {
4341 mInTouchMode = mode;
4342 }
4343 }
4344
Romain Guy06882f82009-06-10 13:36:04 -07004345 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004346 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004348 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004349 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 }
4351
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004352 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 }
Romain Guy06882f82009-06-10 13:36:04 -07004354
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004355 public void setRotationUnchecked(int rotation,
4356 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 if(DEBUG_ORIENTATION) Log.v(TAG,
4358 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 long origId = Binder.clearCallingIdentity();
4361 boolean changed;
4362 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004363 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
Romain Guy06882f82009-06-10 13:36:04 -07004365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 if (changed) {
4367 sendNewConfiguration();
4368 synchronized(mWindowMap) {
4369 mLayoutNeeded = true;
4370 performLayoutAndPlaceSurfacesLocked();
4371 }
4372 } else if (alwaysSendConfiguration) {
4373 //update configuration ignoring orientation change
4374 sendNewConfiguration();
4375 }
Romain Guy06882f82009-06-10 13:36:04 -07004376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 Binder.restoreCallingIdentity(origId);
4378 }
Romain Guy06882f82009-06-10 13:36:04 -07004379
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004380 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 boolean changed;
4382 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4383 rotation = mRequestedRotation;
4384 } else {
4385 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004386 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 }
4388 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004389 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 mRotation, mDisplayEnabled);
4391 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4392 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004395 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 "Rotation changed to " + rotation
4397 + " from " + mRotation
4398 + " (forceApp=" + mForcedAppOrientation
4399 + ", req=" + mRequestedRotation + ")");
4400 mRotation = rotation;
4401 mWindowsFreezingScreen = true;
4402 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4403 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4404 2000);
4405 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004406 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 mQueue.setOrientation(rotation);
4408 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004409 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 }
4411 for (int i=mWindows.size()-1; i>=0; i--) {
4412 WindowState w = (WindowState)mWindows.get(i);
4413 if (w.mSurface != null) {
4414 w.mOrientationChanging = true;
4415 }
4416 }
4417 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4418 try {
4419 mRotationWatchers.get(i).onRotationChanged(rotation);
4420 } catch (RemoteException e) {
4421 }
4422 }
4423 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 return changed;
4426 }
Romain Guy06882f82009-06-10 13:36:04 -07004427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 public int getRotation() {
4429 return mRotation;
4430 }
4431
4432 public int watchRotation(IRotationWatcher watcher) {
4433 final IBinder watcherBinder = watcher.asBinder();
4434 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4435 public void binderDied() {
4436 synchronized (mWindowMap) {
4437 for (int i=0; i<mRotationWatchers.size(); i++) {
4438 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004439 IRotationWatcher removed = mRotationWatchers.remove(i);
4440 if (removed != null) {
4441 removed.asBinder().unlinkToDeath(this, 0);
4442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 i--;
4444 }
4445 }
4446 }
4447 }
4448 };
Romain Guy06882f82009-06-10 13:36:04 -07004449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 synchronized (mWindowMap) {
4451 try {
4452 watcher.asBinder().linkToDeath(dr, 0);
4453 mRotationWatchers.add(watcher);
4454 } catch (RemoteException e) {
4455 // Client died, no cleanup needed.
4456 }
Romain Guy06882f82009-06-10 13:36:04 -07004457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 return mRotation;
4459 }
4460 }
4461
4462 /**
4463 * Starts the view server on the specified port.
4464 *
4465 * @param port The port to listener to.
4466 *
4467 * @return True if the server was successfully started, false otherwise.
4468 *
4469 * @see com.android.server.ViewServer
4470 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4471 */
4472 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004473 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 return false;
4475 }
4476
4477 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4478 return false;
4479 }
4480
4481 if (port < 1024) {
4482 return false;
4483 }
4484
4485 if (mViewServer != null) {
4486 if (!mViewServer.isRunning()) {
4487 try {
4488 return mViewServer.start();
4489 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004490 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 }
4492 }
4493 return false;
4494 }
4495
4496 try {
4497 mViewServer = new ViewServer(this, port);
4498 return mViewServer.start();
4499 } catch (IOException e) {
4500 Log.w(TAG, "View server did not start");
4501 }
4502 return false;
4503 }
4504
Romain Guy06882f82009-06-10 13:36:04 -07004505 private boolean isSystemSecure() {
4506 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4507 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4508 }
4509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 /**
4511 * Stops the view server if it exists.
4512 *
4513 * @return True if the server stopped, false if it wasn't started or
4514 * couldn't be stopped.
4515 *
4516 * @see com.android.server.ViewServer
4517 */
4518 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004519 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 return false;
4521 }
4522
4523 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4524 return false;
4525 }
4526
4527 if (mViewServer != null) {
4528 return mViewServer.stop();
4529 }
4530 return false;
4531 }
4532
4533 /**
4534 * Indicates whether the view server is running.
4535 *
4536 * @return True if the server is running, false otherwise.
4537 *
4538 * @see com.android.server.ViewServer
4539 */
4540 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004541 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 return false;
4543 }
4544
4545 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4546 return false;
4547 }
4548
4549 return mViewServer != null && mViewServer.isRunning();
4550 }
4551
4552 /**
4553 * Lists all availble windows in the system. The listing is written in the
4554 * specified Socket's output stream with the following syntax:
4555 * windowHashCodeInHexadecimal windowName
4556 * Each line of the ouput represents a different window.
4557 *
4558 * @param client The remote client to send the listing to.
4559 * @return False if an error occured, true otherwise.
4560 */
4561 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004562 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 return false;
4564 }
4565
4566 boolean result = true;
4567
4568 Object[] windows;
4569 synchronized (mWindowMap) {
4570 windows = new Object[mWindows.size()];
4571 //noinspection unchecked
4572 windows = mWindows.toArray(windows);
4573 }
4574
4575 BufferedWriter out = null;
4576
4577 // Any uncaught exception will crash the system process
4578 try {
4579 OutputStream clientStream = client.getOutputStream();
4580 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4581
4582 final int count = windows.length;
4583 for (int i = 0; i < count; i++) {
4584 final WindowState w = (WindowState) windows[i];
4585 out.write(Integer.toHexString(System.identityHashCode(w)));
4586 out.write(' ');
4587 out.append(w.mAttrs.getTitle());
4588 out.write('\n');
4589 }
4590
4591 out.write("DONE.\n");
4592 out.flush();
4593 } catch (Exception e) {
4594 result = false;
4595 } finally {
4596 if (out != null) {
4597 try {
4598 out.close();
4599 } catch (IOException e) {
4600 result = false;
4601 }
4602 }
4603 }
4604
4605 return result;
4606 }
4607
4608 /**
4609 * Sends a command to a target window. The result of the command, if any, will be
4610 * written in the output stream of the specified socket.
4611 *
4612 * The parameters must follow this syntax:
4613 * windowHashcode extra
4614 *
4615 * Where XX is the length in characeters of the windowTitle.
4616 *
4617 * The first parameter is the target window. The window with the specified hashcode
4618 * will be the target. If no target can be found, nothing happens. The extra parameters
4619 * will be delivered to the target window and as parameters to the command itself.
4620 *
4621 * @param client The remote client to sent the result, if any, to.
4622 * @param command The command to execute.
4623 * @param parameters The command parameters.
4624 *
4625 * @return True if the command was successfully delivered, false otherwise. This does
4626 * not indicate whether the command itself was successful.
4627 */
4628 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004629 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 return false;
4631 }
4632
4633 boolean success = true;
4634 Parcel data = null;
4635 Parcel reply = null;
4636
4637 // Any uncaught exception will crash the system process
4638 try {
4639 // Find the hashcode of the window
4640 int index = parameters.indexOf(' ');
4641 if (index == -1) {
4642 index = parameters.length();
4643 }
4644 final String code = parameters.substring(0, index);
4645 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4646
4647 // Extract the command's parameter after the window description
4648 if (index < parameters.length()) {
4649 parameters = parameters.substring(index + 1);
4650 } else {
4651 parameters = "";
4652 }
4653
4654 final WindowManagerService.WindowState window = findWindow(hashCode);
4655 if (window == null) {
4656 return false;
4657 }
4658
4659 data = Parcel.obtain();
4660 data.writeInterfaceToken("android.view.IWindow");
4661 data.writeString(command);
4662 data.writeString(parameters);
4663 data.writeInt(1);
4664 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4665
4666 reply = Parcel.obtain();
4667
4668 final IBinder binder = window.mClient.asBinder();
4669 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4670 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4671
4672 reply.readException();
4673
4674 } catch (Exception e) {
4675 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4676 success = false;
4677 } finally {
4678 if (data != null) {
4679 data.recycle();
4680 }
4681 if (reply != null) {
4682 reply.recycle();
4683 }
4684 }
4685
4686 return success;
4687 }
4688
4689 private WindowState findWindow(int hashCode) {
4690 if (hashCode == -1) {
4691 return getFocusedWindow();
4692 }
4693
4694 synchronized (mWindowMap) {
4695 final ArrayList windows = mWindows;
4696 final int count = windows.size();
4697
4698 for (int i = 0; i < count; i++) {
4699 WindowState w = (WindowState) windows.get(i);
4700 if (System.identityHashCode(w) == hashCode) {
4701 return w;
4702 }
4703 }
4704 }
4705
4706 return null;
4707 }
4708
4709 /*
4710 * Instruct the Activity Manager to fetch the current configuration and broadcast
4711 * that to config-changed listeners if appropriate.
4712 */
4713 void sendNewConfiguration() {
4714 try {
4715 mActivityManager.updateConfiguration(null);
4716 } catch (RemoteException e) {
4717 }
4718 }
Romain Guy06882f82009-06-10 13:36:04 -07004719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 public Configuration computeNewConfiguration() {
4721 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004722 return computeNewConfigurationLocked();
4723 }
4724 }
Romain Guy06882f82009-06-10 13:36:04 -07004725
Dianne Hackbornc485a602009-03-24 22:39:49 -07004726 Configuration computeNewConfigurationLocked() {
4727 Configuration config = new Configuration();
4728 if (!computeNewConfigurationLocked(config)) {
4729 return null;
4730 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004731 return config;
4732 }
Romain Guy06882f82009-06-10 13:36:04 -07004733
Dianne Hackbornc485a602009-03-24 22:39:49 -07004734 boolean computeNewConfigurationLocked(Configuration config) {
4735 if (mDisplay == null) {
4736 return false;
4737 }
4738 mQueue.getInputConfiguration(config);
4739 final int dw = mDisplay.getWidth();
4740 final int dh = mDisplay.getHeight();
4741 int orientation = Configuration.ORIENTATION_SQUARE;
4742 if (dw < dh) {
4743 orientation = Configuration.ORIENTATION_PORTRAIT;
4744 } else if (dw > dh) {
4745 orientation = Configuration.ORIENTATION_LANDSCAPE;
4746 }
4747 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004748
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004749 DisplayMetrics dm = new DisplayMetrics();
4750 mDisplay.getMetrics(dm);
4751 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4752
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004753 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004754 // Note we only do this once because at this point we don't
4755 // expect the screen to change in this way at runtime, and want
4756 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004757 int longSize = dw;
4758 int shortSize = dh;
4759 if (longSize < shortSize) {
4760 int tmp = longSize;
4761 longSize = shortSize;
4762 shortSize = tmp;
4763 }
4764 longSize = (int)(longSize/dm.density);
4765 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004766
Dianne Hackborn723738c2009-06-25 19:48:04 -07004767 // These semi-magic numbers define our compatibility modes for
4768 // applications with different screens. Don't change unless you
4769 // make sure to test lots and lots of apps!
4770 if (longSize < 470) {
4771 // This is shorter than an HVGA normal density screen (which
4772 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004773 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4774 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004775 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004776 // Is this a large screen?
4777 if (longSize > 640 && shortSize >= 480) {
4778 // VGA or larger screens at medium density are the point
4779 // at which we consider it to be a large screen.
4780 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4781 } else {
4782 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4783
4784 // If this screen is wider than normal HVGA, or taller
4785 // than FWVGA, then for old apps we want to run in size
4786 // compatibility mode.
4787 if (shortSize > 321 || longSize > 570) {
4788 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4789 }
4790 }
4791
4792 // Is this a long screen?
4793 if (((longSize*3)/5) >= (shortSize-1)) {
4794 // Anything wider than WVGA (5:3) is considering to be long.
4795 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4796 } else {
4797 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4798 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004799 }
4800 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004801 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004802
Dianne Hackbornc485a602009-03-24 22:39:49 -07004803 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4804 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4805 mPolicy.adjustConfigurationLw(config);
4806 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 }
Romain Guy06882f82009-06-10 13:36:04 -07004808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 // -------------------------------------------------------------
4810 // Input Events and Focus Management
4811 // -------------------------------------------------------------
4812
4813 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004814 long curTime = SystemClock.uptimeMillis();
4815
Michael Chane10de972009-05-18 11:24:50 -07004816 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004817 if (mLastTouchEventType == eventType &&
4818 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4819 return;
4820 }
4821 mLastUserActivityCallTime = curTime;
4822 mLastTouchEventType = eventType;
4823 }
4824
4825 if (targetWin == null
4826 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4827 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 }
4829 }
4830
4831 // tells if it's a cheek event or not -- this function is stateful
4832 private static final int EVENT_NONE = 0;
4833 private static final int EVENT_UNKNOWN = 0;
4834 private static final int EVENT_CHEEK = 0;
4835 private static final int EVENT_IGNORE_DURATION = 300; // ms
4836 private static final float CHEEK_THRESHOLD = 0.6f;
4837 private int mEventState = EVENT_NONE;
4838 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 private int eventType(MotionEvent ev) {
4841 float size = ev.getSize();
4842 switch (ev.getAction()) {
4843 case MotionEvent.ACTION_DOWN:
4844 mEventSize = size;
4845 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4846 case MotionEvent.ACTION_UP:
4847 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004848 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 case MotionEvent.ACTION_MOVE:
4850 final int N = ev.getHistorySize();
4851 if (size > mEventSize) mEventSize = size;
4852 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4853 for (int i=0; i<N; i++) {
4854 size = ev.getHistoricalSize(i);
4855 if (size > mEventSize) mEventSize = size;
4856 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4857 }
4858 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4859 return TOUCH_EVENT;
4860 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004861 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 }
4863 default:
4864 // not good
4865 return OTHER_EVENT;
4866 }
4867 }
4868
4869 /**
4870 * @return Returns true if event was dispatched, false if it was dropped for any reason
4871 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004872 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4874 "dispatchPointer " + ev);
4875
Michael Chan53071d62009-05-13 17:29:48 -07004876 if (MEASURE_LATENCY) {
4877 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4878 }
4879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004881 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882
Michael Chan53071d62009-05-13 17:29:48 -07004883 if (MEASURE_LATENCY) {
4884 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4885 }
4886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 if (action == MotionEvent.ACTION_UP) {
4890 // let go of our target
4891 mKeyWaiter.mMotionTarget = null;
4892 mPowerManager.logPointerUpEvent();
4893 } else if (action == MotionEvent.ACTION_DOWN) {
4894 mPowerManager.logPointerDownEvent();
4895 }
4896
4897 if (targetObj == null) {
4898 // In this case we are either dropping the event, or have received
4899 // a move or up without a down. It is common to receive move
4900 // events in such a way, since this means the user is moving the
4901 // pointer without actually pressing down. All other cases should
4902 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004903 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4905 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004906 synchronized (mWindowMap) {
4907 if (mSendingPointersToWallpaper) {
4908 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4909 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4910 }
4911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 if (qev != null) {
4913 mQueue.recycleEvent(qev);
4914 }
4915 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004916 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 }
4918 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004919 synchronized (mWindowMap) {
4920 if (mSendingPointersToWallpaper) {
4921 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4922 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4923 }
4924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004925 if (qev != null) {
4926 mQueue.recycleEvent(qev);
4927 }
4928 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004929 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 }
Romain Guy06882f82009-06-10 13:36:04 -07004931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004935 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936
4937 //Log.i(TAG, "Sending " + ev + " to " + target);
4938
4939 if (uid != 0 && uid != target.mSession.mUid) {
4940 if (mContext.checkPermission(
4941 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4942 != PackageManager.PERMISSION_GRANTED) {
4943 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4944 + pid + " uid " + uid + " to window " + target
4945 + " owned by uid " + target.mSession.mUid);
4946 if (qev != null) {
4947 mQueue.recycleEvent(qev);
4948 }
4949 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004950 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 }
4952 }
4953
Michael Chan53071d62009-05-13 17:29:48 -07004954 if (MEASURE_LATENCY) {
4955 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4956 }
4957
Romain Guy06882f82009-06-10 13:36:04 -07004958 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4960 //target wants to ignore fat touch events
4961 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4962 //explicit flag to return without processing event further
4963 boolean returnFlag = false;
4964 if((action == MotionEvent.ACTION_DOWN)) {
4965 mFatTouch = false;
4966 if(cheekPress) {
4967 mFatTouch = true;
4968 returnFlag = true;
4969 }
4970 } else {
4971 if(action == MotionEvent.ACTION_UP) {
4972 if(mFatTouch) {
4973 //earlier even was invalid doesnt matter if current up is cheekpress or not
4974 mFatTouch = false;
4975 returnFlag = true;
4976 } else if(cheekPress) {
4977 //cancel the earlier event
4978 ev.setAction(MotionEvent.ACTION_CANCEL);
4979 action = MotionEvent.ACTION_CANCEL;
4980 }
4981 } else if(action == MotionEvent.ACTION_MOVE) {
4982 if(mFatTouch) {
4983 //two cases here
4984 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004985 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 returnFlag = true;
4987 } else if(cheekPress) {
4988 //valid down followed by invalid moves
4989 //an invalid move have to cancel earlier action
4990 ev.setAction(MotionEvent.ACTION_CANCEL);
4991 action = MotionEvent.ACTION_CANCEL;
4992 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4993 //note that the subsequent invalid moves will not get here
4994 mFatTouch = true;
4995 }
4996 }
4997 } //else if action
4998 if(returnFlag) {
4999 //recycle que, ev
5000 if (qev != null) {
5001 mQueue.recycleEvent(qev);
5002 }
5003 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005004 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 }
5006 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005007
Michael Chan9f028e62009-08-04 17:37:46 -07005008 // Enable this for testing the "right" value
5009 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005010 int max_events_per_sec = 35;
5011 try {
5012 max_events_per_sec = Integer.parseInt(SystemProperties
5013 .get("windowsmgr.max_events_per_sec"));
5014 if (max_events_per_sec < 1) {
5015 max_events_per_sec = 35;
5016 }
5017 } catch (NumberFormatException e) {
5018 }
5019 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5020 }
5021
5022 /*
5023 * Throttle events to minimize CPU usage when there's a flood of events
5024 * e.g. constant contact with the screen
5025 */
5026 if (action == MotionEvent.ACTION_MOVE) {
5027 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5028 long now = SystemClock.uptimeMillis();
5029 if (now < nextEventTime) {
5030 try {
5031 Thread.sleep(nextEventTime - now);
5032 } catch (InterruptedException e) {
5033 }
5034 mLastTouchEventTime = nextEventTime;
5035 } else {
5036 mLastTouchEventTime = now;
5037 }
5038 }
5039
Michael Chan53071d62009-05-13 17:29:48 -07005040 if (MEASURE_LATENCY) {
5041 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5042 }
5043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005045 if (!target.isVisibleLw()) {
5046 // During this motion dispatch, the target window has become
5047 // invisible.
5048 if (mSendingPointersToWallpaper) {
5049 sendPointerToWallpaperLocked(null, ev, eventTime);
5050 }
5051 if (qev != null) {
5052 mQueue.recycleEvent(qev);
5053 }
5054 ev.recycle();
5055 return INJECT_SUCCEEDED;
5056 }
5057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5059 mKeyWaiter.bindTargetWindowLocked(target,
5060 KeyWaiter.RETURN_PENDING_POINTER, qev);
5061 ev = null;
5062 } else {
5063 if (action == MotionEvent.ACTION_DOWN) {
5064 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5065 if (out != null) {
5066 MotionEvent oev = MotionEvent.obtain(ev);
5067 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5068 do {
5069 final Rect frame = out.mFrame;
5070 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5071 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005072 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 } catch (android.os.RemoteException e) {
5074 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5075 }
5076 oev.offsetLocation((float)frame.left, (float)frame.top);
5077 out = out.mNextOutsideTouch;
5078 } while (out != null);
5079 mKeyWaiter.mOutsideTouchTargets = null;
5080 }
5081 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005082
5083 // If we are on top of the wallpaper, then the wallpaper also
5084 // gets to see this movement.
Dianne Hackborn6adba242009-11-10 11:10:09 -08005085 if (mWallpaperTarget == target || mSendingPointersToWallpaper) {
5086 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005087 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005088
5089 final Rect frame = target.mFrame;
5090 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5091 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 }
5093 }
Romain Guy06882f82009-06-10 13:36:04 -07005094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 // finally offset the event to the target's coordinate system and
5096 // dispatch the event.
5097 try {
5098 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5099 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5100 }
Michael Chan53071d62009-05-13 17:29:48 -07005101
5102 if (MEASURE_LATENCY) {
5103 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5104 }
5105
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005106 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005107
5108 if (MEASURE_LATENCY) {
5109 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5110 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005111 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 } catch (android.os.RemoteException e) {
5113 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5114 mKeyWaiter.mMotionTarget = null;
5115 try {
5116 removeWindow(target.mSession, target.mClient);
5117 } catch (java.util.NoSuchElementException ex) {
5118 // This will happen if the window has already been
5119 // removed.
5120 }
5121 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005122 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 }
Romain Guy06882f82009-06-10 13:36:04 -07005124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 /**
5126 * @return Returns true if event was dispatched, false if it was dropped for any reason
5127 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005128 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 if (DEBUG_INPUT) Log.v(
5130 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005133 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 if (focusObj == null) {
5135 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5136 if (qev != null) {
5137 mQueue.recycleEvent(qev);
5138 }
5139 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005140 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 }
5142 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5143 if (qev != null) {
5144 mQueue.recycleEvent(qev);
5145 }
5146 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005147 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 }
Romain Guy06882f82009-06-10 13:36:04 -07005149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 if (uid != 0 && uid != focus.mSession.mUid) {
5153 if (mContext.checkPermission(
5154 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5155 != PackageManager.PERMISSION_GRANTED) {
5156 Log.w(TAG, "Permission denied: injecting key event from pid "
5157 + pid + " uid " + uid + " to window " + focus
5158 + " owned by uid " + focus.mSession.mUid);
5159 if (qev != null) {
5160 mQueue.recycleEvent(qev);
5161 }
5162 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005163 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 }
5165 }
Romain Guy06882f82009-06-10 13:36:04 -07005166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 synchronized(mWindowMap) {
5170 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5171 mKeyWaiter.bindTargetWindowLocked(focus,
5172 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5173 // We don't deliver movement events to the client, we hold
5174 // them and wait for them to call back.
5175 ev = null;
5176 } else {
5177 mKeyWaiter.bindTargetWindowLocked(focus);
5178 }
5179 }
Romain Guy06882f82009-06-10 13:36:04 -07005180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005182 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005183 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 } catch (android.os.RemoteException e) {
5185 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5186 try {
5187 removeWindow(focus.mSession, focus.mClient);
5188 } catch (java.util.NoSuchElementException ex) {
5189 // This will happen if the window has already been
5190 // removed.
5191 }
5192 }
Romain Guy06882f82009-06-10 13:36:04 -07005193
Dianne Hackborncfaef692009-06-15 14:24:44 -07005194 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 }
Romain Guy06882f82009-06-10 13:36:04 -07005196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 /**
5198 * @return Returns true if event was dispatched, false if it was dropped for any reason
5199 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005200 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5202
5203 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005204 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 if (focusObj == null) {
5206 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005207 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 }
5209 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005210 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 }
Romain Guy06882f82009-06-10 13:36:04 -07005212
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005213 // Okay we have finished waiting for the last event to be processed.
5214 // First off, if this is a repeat event, check to see if there is
5215 // a corresponding up event in the queue. If there is, we will
5216 // just drop the repeat, because it makes no sense to repeat after
5217 // the user has released a key. (This is especially important for
5218 // long presses.)
5219 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5220 return INJECT_SUCCEEDED;
5221 }
5222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 if (DEBUG_INPUT) Log.v(
5226 TAG, "Dispatching to " + focus + ": " + event);
5227
5228 if (uid != 0 && uid != focus.mSession.mUid) {
5229 if (mContext.checkPermission(
5230 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5231 != PackageManager.PERMISSION_GRANTED) {
5232 Log.w(TAG, "Permission denied: injecting key event from pid "
5233 + pid + " uid " + uid + " to window " + focus
5234 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005235 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 }
5237 }
Romain Guy06882f82009-06-10 13:36:04 -07005238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 synchronized(mWindowMap) {
5240 mKeyWaiter.bindTargetWindowLocked(focus);
5241 }
5242
5243 // NOSHIP extra state logging
5244 mKeyWaiter.recordDispatchState(event, focus);
5245 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 try {
5248 if (DEBUG_INPUT || DEBUG_FOCUS) {
5249 Log.v(TAG, "Delivering key " + event.getKeyCode()
5250 + " to " + focus);
5251 }
5252 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005253 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 } catch (android.os.RemoteException e) {
5255 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5256 try {
5257 removeWindow(focus.mSession, focus.mClient);
5258 } catch (java.util.NoSuchElementException ex) {
5259 // This will happen if the window has already been
5260 // removed.
5261 }
5262 }
Romain Guy06882f82009-06-10 13:36:04 -07005263
Dianne Hackborncfaef692009-06-15 14:24:44 -07005264 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005265 }
Romain Guy06882f82009-06-10 13:36:04 -07005266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 public void pauseKeyDispatching(IBinder _token) {
5268 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5269 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005270 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 }
5272
5273 synchronized (mWindowMap) {
5274 WindowToken token = mTokenMap.get(_token);
5275 if (token != null) {
5276 mKeyWaiter.pauseDispatchingLocked(token);
5277 }
5278 }
5279 }
5280
5281 public void resumeKeyDispatching(IBinder _token) {
5282 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5283 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005284 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 }
5286
5287 synchronized (mWindowMap) {
5288 WindowToken token = mTokenMap.get(_token);
5289 if (token != null) {
5290 mKeyWaiter.resumeDispatchingLocked(token);
5291 }
5292 }
5293 }
5294
5295 public void setEventDispatching(boolean enabled) {
5296 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5297 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005298 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 }
5300
5301 synchronized (mWindowMap) {
5302 mKeyWaiter.setEventDispatchingLocked(enabled);
5303 }
5304 }
Romain Guy06882f82009-06-10 13:36:04 -07005305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 /**
5307 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005308 *
5309 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 * {@link SystemClock#uptimeMillis()} as the timebase.)
5311 * @param sync If true, wait for the event to be completed before returning to the caller.
5312 * @return Returns true if event was dispatched, false if it was dropped for any reason
5313 */
5314 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5315 long downTime = ev.getDownTime();
5316 long eventTime = ev.getEventTime();
5317
5318 int action = ev.getAction();
5319 int code = ev.getKeyCode();
5320 int repeatCount = ev.getRepeatCount();
5321 int metaState = ev.getMetaState();
5322 int deviceId = ev.getDeviceId();
5323 int scancode = ev.getScanCode();
5324
5325 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5326 if (downTime == 0) downTime = eventTime;
5327
5328 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005329 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005331 final int pid = Binder.getCallingPid();
5332 final int uid = Binder.getCallingUid();
5333 final long ident = Binder.clearCallingIdentity();
5334 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005336 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005338 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005339 switch (result) {
5340 case INJECT_NO_PERMISSION:
5341 throw new SecurityException(
5342 "Injecting to another application requires INJECT_EVENT permission");
5343 case INJECT_SUCCEEDED:
5344 return true;
5345 }
5346 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 }
5348
5349 /**
5350 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005351 *
5352 * @param ev A motion event describing the pointer (touch) action. (As noted in
5353 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 * {@link SystemClock#uptimeMillis()} as the timebase.)
5355 * @param sync If true, wait for the event to be completed before returning to the caller.
5356 * @return Returns true if event was dispatched, false if it was dropped for any reason
5357 */
5358 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005359 final int pid = Binder.getCallingPid();
5360 final int uid = Binder.getCallingUid();
5361 final long ident = Binder.clearCallingIdentity();
5362 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005364 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005366 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005367 switch (result) {
5368 case INJECT_NO_PERMISSION:
5369 throw new SecurityException(
5370 "Injecting to another application requires INJECT_EVENT permission");
5371 case INJECT_SUCCEEDED:
5372 return true;
5373 }
5374 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 }
Romain Guy06882f82009-06-10 13:36:04 -07005376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 /**
5378 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005379 *
5380 * @param ev A motion event describing the trackball action. (As noted in
5381 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 * {@link SystemClock#uptimeMillis()} as the timebase.)
5383 * @param sync If true, wait for the event to be completed before returning to the caller.
5384 * @return Returns true if event was dispatched, false if it was dropped for any reason
5385 */
5386 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005387 final int pid = Binder.getCallingPid();
5388 final int uid = Binder.getCallingUid();
5389 final long ident = Binder.clearCallingIdentity();
5390 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005392 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005394 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005395 switch (result) {
5396 case INJECT_NO_PERMISSION:
5397 throw new SecurityException(
5398 "Injecting to another application requires INJECT_EVENT permission");
5399 case INJECT_SUCCEEDED:
5400 return true;
5401 }
5402 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 }
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 private WindowState getFocusedWindow() {
5406 synchronized (mWindowMap) {
5407 return getFocusedWindowLocked();
5408 }
5409 }
5410
5411 private WindowState getFocusedWindowLocked() {
5412 return mCurrentFocus;
5413 }
Romain Guy06882f82009-06-10 13:36:04 -07005414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 /**
5416 * This class holds the state for dispatching key events. This state
5417 * is protected by the KeyWaiter instance, NOT by the window lock. You
5418 * can be holding the main window lock while acquire the KeyWaiter lock,
5419 * but not the other way around.
5420 */
5421 final class KeyWaiter {
5422 // NOSHIP debugging
5423 public class DispatchState {
5424 private KeyEvent event;
5425 private WindowState focus;
5426 private long time;
5427 private WindowState lastWin;
5428 private IBinder lastBinder;
5429 private boolean finished;
5430 private boolean gotFirstWindow;
5431 private boolean eventDispatching;
5432 private long timeToSwitch;
5433 private boolean wasFrozen;
5434 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005435 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5438 focus = theFocus;
5439 event = theEvent;
5440 time = System.currentTimeMillis();
5441 // snapshot KeyWaiter state
5442 lastWin = mLastWin;
5443 lastBinder = mLastBinder;
5444 finished = mFinished;
5445 gotFirstWindow = mGotFirstWindow;
5446 eventDispatching = mEventDispatching;
5447 timeToSwitch = mTimeToSwitch;
5448 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005449 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 // cache the paused state at ctor time as well
5451 if (theFocus == null || theFocus.mToken == null) {
5452 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5453 focusPaused = false;
5454 } else {
5455 focusPaused = theFocus.mToken.paused;
5456 }
5457 }
Romain Guy06882f82009-06-10 13:36:04 -07005458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 public String toString() {
5460 return "{{" + event + " to " + focus + " @ " + time
5461 + " lw=" + lastWin + " lb=" + lastBinder
5462 + " fin=" + finished + " gfw=" + gotFirstWindow
5463 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005464 + " wf=" + wasFrozen + " fp=" + focusPaused
5465 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 }
5467 };
5468 private DispatchState mDispatchState = null;
5469 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5470 mDispatchState = new DispatchState(theEvent, theFocus);
5471 }
5472 // END NOSHIP
5473
5474 public static final int RETURN_NOTHING = 0;
5475 public static final int RETURN_PENDING_POINTER = 1;
5476 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 final Object SKIP_TARGET_TOKEN = new Object();
5479 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 private WindowState mLastWin = null;
5482 private IBinder mLastBinder = null;
5483 private boolean mFinished = true;
5484 private boolean mGotFirstWindow = false;
5485 private boolean mEventDispatching = true;
5486 private long mTimeToSwitch = 0;
5487 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 // Target of Motion events
5490 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 // Windows above the target who would like to receive an "outside"
5493 // touch event for any down events outside of them.
5494 WindowState mOutsideTouchTargets;
5495
5496 /**
5497 * Wait for the last event dispatch to complete, then find the next
5498 * target that should receive the given event and wait for that one
5499 * to be ready to receive it.
5500 */
5501 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5502 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005503 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 long startTime = SystemClock.uptimeMillis();
5505 long keyDispatchingTimeout = 5 * 1000;
5506 long waitedFor = 0;
5507
5508 while (true) {
5509 // Figure out which window we care about. It is either the
5510 // last window we are waiting to have process the event or,
5511 // if none, then the next window we think the event should go
5512 // to. Note: we retrieve mLastWin outside of the lock, so
5513 // it may change before we lock. Thus we must check it again.
5514 WindowState targetWin = mLastWin;
5515 boolean targetIsNew = targetWin == null;
5516 if (DEBUG_INPUT) Log.v(
5517 TAG, "waitForLastKey: mFinished=" + mFinished +
5518 ", mLastWin=" + mLastWin);
5519 if (targetIsNew) {
5520 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005521 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 if (target == SKIP_TARGET_TOKEN) {
5523 // The user has pressed a special key, and we are
5524 // dropping all pending events before it.
5525 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5526 + " " + nextMotion);
5527 return null;
5528 }
5529 if (target == CONSUMED_EVENT_TOKEN) {
5530 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5531 + " " + nextMotion);
5532 return target;
5533 }
5534 targetWin = (WindowState)target;
5535 }
Romain Guy06882f82009-06-10 13:36:04 -07005536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 // Now: is it okay to send the next event to this window?
5540 synchronized (this) {
5541 // First: did we come here based on the last window not
5542 // being null, but it changed by the time we got here?
5543 // If so, try again.
5544 if (!targetIsNew && mLastWin == null) {
5545 continue;
5546 }
Romain Guy06882f82009-06-10 13:36:04 -07005547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 // We never dispatch events if not finished with the
5549 // last one, or the display is frozen.
5550 if (mFinished && !mDisplayFrozen) {
5551 // If event dispatching is disabled, then we
5552 // just consume the events.
5553 if (!mEventDispatching) {
5554 if (DEBUG_INPUT) Log.v(TAG,
5555 "Skipping event; dispatching disabled: "
5556 + nextKey + " " + nextMotion);
5557 return null;
5558 }
5559 if (targetWin != null) {
5560 // If this is a new target, and that target is not
5561 // paused or unresponsive, then all looks good to
5562 // handle the event.
5563 if (targetIsNew && !targetWin.mToken.paused) {
5564 return targetWin;
5565 }
Romain Guy06882f82009-06-10 13:36:04 -07005566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 // If we didn't find a target window, and there is no
5568 // focused app window, then just eat the events.
5569 } else if (mFocusedApp == null) {
5570 if (DEBUG_INPUT) Log.v(TAG,
5571 "Skipping event; no focused app: "
5572 + nextKey + " " + nextMotion);
5573 return null;
5574 }
5575 }
Romain Guy06882f82009-06-10 13:36:04 -07005576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 if (DEBUG_INPUT) Log.v(
5578 TAG, "Waiting for last key in " + mLastBinder
5579 + " target=" + targetWin
5580 + " mFinished=" + mFinished
5581 + " mDisplayFrozen=" + mDisplayFrozen
5582 + " targetIsNew=" + targetIsNew
5583 + " paused="
5584 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005585 + " mFocusedApp=" + mFocusedApp
5586 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 targetApp = targetWin != null
5589 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 long curTimeout = keyDispatchingTimeout;
5592 if (mTimeToSwitch != 0) {
5593 long now = SystemClock.uptimeMillis();
5594 if (mTimeToSwitch <= now) {
5595 // If an app switch key has been pressed, and we have
5596 // waited too long for the current app to finish
5597 // processing keys, then wait no more!
5598 doFinishedKeyLocked(true);
5599 continue;
5600 }
5601 long switchTimeout = mTimeToSwitch - now;
5602 if (curTimeout > switchTimeout) {
5603 curTimeout = switchTimeout;
5604 }
5605 }
Romain Guy06882f82009-06-10 13:36:04 -07005606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 try {
5608 // after that continue
5609 // processing keys, so we don't get stuck.
5610 if (DEBUG_INPUT) Log.v(
5611 TAG, "Waiting for key dispatch: " + curTimeout);
5612 wait(curTimeout);
5613 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5614 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005615 + startTime + " switchTime=" + mTimeToSwitch
5616 + " target=" + targetWin + " mLW=" + mLastWin
5617 + " mLB=" + mLastBinder + " fin=" + mFinished
5618 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 } catch (InterruptedException e) {
5620 }
5621 }
5622
5623 // If we were frozen during configuration change, restart the
5624 // timeout checks from now; otherwise look at whether we timed
5625 // out before awakening.
5626 if (mWasFrozen) {
5627 waitedFor = 0;
5628 mWasFrozen = false;
5629 } else {
5630 waitedFor = SystemClock.uptimeMillis() - startTime;
5631 }
5632
5633 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5634 IApplicationToken at = null;
5635 synchronized (this) {
5636 Log.w(TAG, "Key dispatching timed out sending to " +
5637 (targetWin != null ? targetWin.mAttrs.getTitle()
5638 : "<null>"));
5639 // NOSHIP debugging
5640 Log.w(TAG, "Dispatch state: " + mDispatchState);
5641 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5642 // END NOSHIP
5643 //dump();
5644 if (targetWin != null) {
5645 at = targetWin.getAppToken();
5646 } else if (targetApp != null) {
5647 at = targetApp.appToken;
5648 }
5649 }
5650
5651 boolean abort = true;
5652 if (at != null) {
5653 try {
5654 long timeout = at.getKeyDispatchingTimeout();
5655 if (timeout > waitedFor) {
5656 // we did not wait the proper amount of time for this application.
5657 // set the timeout to be the real timeout and wait again.
5658 keyDispatchingTimeout = timeout - waitedFor;
5659 continue;
5660 } else {
5661 abort = at.keyDispatchingTimedOut();
5662 }
5663 } catch (RemoteException ex) {
5664 }
5665 }
5666
5667 synchronized (this) {
5668 if (abort && (mLastWin == targetWin || targetWin == null)) {
5669 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005670 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 if (DEBUG_INPUT) Log.v(TAG,
5672 "Window " + mLastWin +
5673 " timed out on key input");
5674 if (mLastWin.mToken.paused) {
5675 Log.w(TAG, "Un-pausing dispatching to this window");
5676 mLastWin.mToken.paused = false;
5677 }
5678 }
5679 if (mMotionTarget == targetWin) {
5680 mMotionTarget = null;
5681 }
5682 mLastWin = null;
5683 mLastBinder = null;
5684 if (failIfTimeout || targetWin == null) {
5685 return null;
5686 }
5687 } else {
5688 Log.w(TAG, "Continuing to wait for key to be dispatched");
5689 startTime = SystemClock.uptimeMillis();
5690 }
5691 }
5692 }
5693 }
5694 }
Romain Guy06882f82009-06-10 13:36:04 -07005695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005697 MotionEvent nextMotion, boolean isPointerEvent,
5698 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 if (nextKey != null) {
5702 // Find the target window for a normal key event.
5703 final int keycode = nextKey.getKeyCode();
5704 final int repeatCount = nextKey.getRepeatCount();
5705 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5706 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005709 if (callingUid == 0 ||
5710 mContext.checkPermission(
5711 android.Manifest.permission.INJECT_EVENTS,
5712 callingPid, callingUid)
5713 == PackageManager.PERMISSION_GRANTED) {
5714 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005715 nextKey.getMetaState(), down, repeatCount,
5716 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 Log.w(TAG, "Event timeout during app switch: dropping "
5719 + nextKey);
5720 return SKIP_TARGET_TOKEN;
5721 }
Romain Guy06882f82009-06-10 13:36:04 -07005722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 WindowState focus = null;
5726 synchronized(mWindowMap) {
5727 focus = getFocusedWindowLocked();
5728 }
Romain Guy06882f82009-06-10 13:36:04 -07005729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005731
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005732 if (callingUid == 0 ||
5733 (focus != null && callingUid == focus.mSession.mUid) ||
5734 mContext.checkPermission(
5735 android.Manifest.permission.INJECT_EVENTS,
5736 callingPid, callingUid)
5737 == PackageManager.PERMISSION_GRANTED) {
5738 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005739 keycode, nextKey.getMetaState(), down, repeatCount,
5740 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005741 return CONSUMED_EVENT_TOKEN;
5742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 }
Romain Guy06882f82009-06-10 13:36:04 -07005744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 } else if (!isPointerEvent) {
5748 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5749 if (!dispatch) {
5750 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5751 + nextMotion);
5752 return SKIP_TARGET_TOKEN;
5753 }
Romain Guy06882f82009-06-10 13:36:04 -07005754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 WindowState focus = null;
5756 synchronized(mWindowMap) {
5757 focus = getFocusedWindowLocked();
5758 }
Romain Guy06882f82009-06-10 13:36:04 -07005759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5761 return focus;
5762 }
Romain Guy06882f82009-06-10 13:36:04 -07005763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 if (nextMotion == null) {
5765 return SKIP_TARGET_TOKEN;
5766 }
Romain Guy06882f82009-06-10 13:36:04 -07005767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5769 KeyEvent.KEYCODE_UNKNOWN);
5770 if (!dispatch) {
5771 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5772 + nextMotion);
5773 return SKIP_TARGET_TOKEN;
5774 }
Romain Guy06882f82009-06-10 13:36:04 -07005775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 // Find the target window for a pointer event.
5777 int action = nextMotion.getAction();
5778 final float xf = nextMotion.getX();
5779 final float yf = nextMotion.getY();
5780 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 final boolean screenWasOff = qev != null
5783 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 synchronized(mWindowMap) {
5788 synchronized (this) {
5789 if (action == MotionEvent.ACTION_DOWN) {
5790 if (mMotionTarget != null) {
5791 // this is weird, we got a pen down, but we thought it was
5792 // already down!
5793 // XXX: We should probably send an ACTION_UP to the current
5794 // target.
5795 Log.w(TAG, "Pointer down received while already down in: "
5796 + mMotionTarget);
5797 mMotionTarget = null;
5798 }
Romain Guy06882f82009-06-10 13:36:04 -07005799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005800 // ACTION_DOWN is special, because we need to lock next events to
5801 // the window we'll land onto.
5802 final int x = (int)xf;
5803 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 final ArrayList windows = mWindows;
5806 final int N = windows.size();
5807 WindowState topErrWindow = null;
5808 final Rect tmpRect = mTempRect;
5809 for (int i=N-1; i>=0; i--) {
5810 WindowState child = (WindowState)windows.get(i);
5811 //Log.i(TAG, "Checking dispatch to: " + child);
5812 final int flags = child.mAttrs.flags;
5813 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5814 if (topErrWindow == null) {
5815 topErrWindow = child;
5816 }
5817 }
5818 if (!child.isVisibleLw()) {
5819 //Log.i(TAG, "Not visible!");
5820 continue;
5821 }
5822 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5823 //Log.i(TAG, "Not touchable!");
5824 if ((flags & WindowManager.LayoutParams
5825 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5826 child.mNextOutsideTouch = mOutsideTouchTargets;
5827 mOutsideTouchTargets = child;
5828 }
5829 continue;
5830 }
5831 tmpRect.set(child.mFrame);
5832 if (child.mTouchableInsets == ViewTreeObserver
5833 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5834 // The touch is inside of the window if it is
5835 // inside the frame, AND the content part of that
5836 // frame that was given by the application.
5837 tmpRect.left += child.mGivenContentInsets.left;
5838 tmpRect.top += child.mGivenContentInsets.top;
5839 tmpRect.right -= child.mGivenContentInsets.right;
5840 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5841 } else if (child.mTouchableInsets == ViewTreeObserver
5842 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5843 // The touch is inside of the window if it is
5844 // inside the frame, AND the visible part of that
5845 // frame that was given by the application.
5846 tmpRect.left += child.mGivenVisibleInsets.left;
5847 tmpRect.top += child.mGivenVisibleInsets.top;
5848 tmpRect.right -= child.mGivenVisibleInsets.right;
5849 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5850 }
5851 final int touchFlags = flags &
5852 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5853 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5854 if (tmpRect.contains(x, y) || touchFlags == 0) {
5855 //Log.i(TAG, "Using this target!");
5856 if (!screenWasOff || (flags &
5857 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5858 mMotionTarget = child;
5859 } else {
5860 //Log.i(TAG, "Waking, skip!");
5861 mMotionTarget = null;
5862 }
5863 break;
5864 }
Romain Guy06882f82009-06-10 13:36:04 -07005865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 if ((flags & WindowManager.LayoutParams
5867 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5868 child.mNextOutsideTouch = mOutsideTouchTargets;
5869 mOutsideTouchTargets = child;
5870 //Log.i(TAG, "Adding to outside target list: " + child);
5871 }
5872 }
5873
5874 // if there's an error window but it's not accepting
5875 // focus (typically because it is not yet visible) just
5876 // wait for it -- any other focused window may in fact
5877 // be in ANR state.
5878 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5879 mMotionTarget = null;
5880 }
5881 }
Romain Guy06882f82009-06-10 13:36:04 -07005882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 target = mMotionTarget;
5884 }
5885 }
Romain Guy06882f82009-06-10 13:36:04 -07005886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 // Pointer events are a little different -- if there isn't a
5890 // target found for any event, then just drop it.
5891 return target != null ? target : SKIP_TARGET_TOKEN;
5892 }
Romain Guy06882f82009-06-10 13:36:04 -07005893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 boolean checkShouldDispatchKey(int keycode) {
5895 synchronized (this) {
5896 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5897 mTimeToSwitch = 0;
5898 return true;
5899 }
5900 if (mTimeToSwitch != 0
5901 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5902 return false;
5903 }
5904 return true;
5905 }
5906 }
Romain Guy06882f82009-06-10 13:36:04 -07005907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 void bindTargetWindowLocked(WindowState win,
5909 int pendingWhat, QueuedEvent pendingMotion) {
5910 synchronized (this) {
5911 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5912 }
5913 }
Romain Guy06882f82009-06-10 13:36:04 -07005914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 void bindTargetWindowLocked(WindowState win) {
5916 synchronized (this) {
5917 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5918 }
5919 }
5920
5921 void bindTargetWindowLockedLocked(WindowState win,
5922 int pendingWhat, QueuedEvent pendingMotion) {
5923 mLastWin = win;
5924 mLastBinder = win.mClient.asBinder();
5925 mFinished = false;
5926 if (pendingMotion != null) {
5927 final Session s = win.mSession;
5928 if (pendingWhat == RETURN_PENDING_POINTER) {
5929 releasePendingPointerLocked(s);
5930 s.mPendingPointerMove = pendingMotion;
5931 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005932 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 "bindTargetToWindow " + s.mPendingPointerMove);
5934 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5935 releasePendingTrackballLocked(s);
5936 s.mPendingTrackballMove = pendingMotion;
5937 s.mPendingTrackballWindow = win;
5938 }
5939 }
5940 }
Romain Guy06882f82009-06-10 13:36:04 -07005941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 void releasePendingPointerLocked(Session s) {
5943 if (DEBUG_INPUT) Log.v(TAG,
5944 "releasePendingPointer " + s.mPendingPointerMove);
5945 if (s.mPendingPointerMove != null) {
5946 mQueue.recycleEvent(s.mPendingPointerMove);
5947 s.mPendingPointerMove = null;
5948 }
5949 }
Romain Guy06882f82009-06-10 13:36:04 -07005950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 void releasePendingTrackballLocked(Session s) {
5952 if (s.mPendingTrackballMove != null) {
5953 mQueue.recycleEvent(s.mPendingTrackballMove);
5954 s.mPendingTrackballMove = null;
5955 }
5956 }
Romain Guy06882f82009-06-10 13:36:04 -07005957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5959 int returnWhat) {
5960 if (DEBUG_INPUT) Log.v(
5961 TAG, "finishedKey: client=" + client + ", force=" + force);
5962
5963 if (client == null) {
5964 return null;
5965 }
5966
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005967 MotionEvent res = null;
5968 QueuedEvent qev = null;
5969 WindowState win = null;
5970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 synchronized (this) {
5972 if (DEBUG_INPUT) Log.v(
5973 TAG, "finishedKey: client=" + client.asBinder()
5974 + ", force=" + force + ", last=" + mLastBinder
5975 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005977 if (returnWhat == RETURN_PENDING_POINTER) {
5978 qev = session.mPendingPointerMove;
5979 win = session.mPendingPointerWindow;
5980 session.mPendingPointerMove = null;
5981 session.mPendingPointerWindow = null;
5982 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5983 qev = session.mPendingTrackballMove;
5984 win = session.mPendingTrackballWindow;
5985 session.mPendingTrackballMove = null;
5986 session.mPendingTrackballWindow = null;
5987 }
Romain Guy06882f82009-06-10 13:36:04 -07005988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 if (mLastBinder == client.asBinder()) {
5990 if (DEBUG_INPUT) Log.v(
5991 TAG, "finishedKey: last paused="
5992 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5993 if (mLastWin != null && (!mLastWin.mToken.paused || force
5994 || !mEventDispatching)) {
5995 doFinishedKeyLocked(false);
5996 } else {
5997 // Make sure to wake up anyone currently waiting to
5998 // dispatch a key, so they can re-evaluate their
5999 // current situation.
6000 mFinished = true;
6001 notifyAll();
6002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 }
Romain Guy06882f82009-06-10 13:36:04 -07006004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006006 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 if (DEBUG_INPUT) Log.v(TAG,
6008 "Returning pending motion: " + res);
6009 mQueue.recycleEvent(qev);
6010 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6011 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08006014
6015 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6016 synchronized (mWindowMap) {
6017 if (mWallpaperTarget == win || mSendingPointersToWallpaper) {
6018 sendPointerToWallpaperLocked(win, res, res.getEventTime());
6019 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006020 }
6021 }
6022 }
6023
6024 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 }
6026
6027 void tickle() {
6028 synchronized (this) {
6029 notifyAll();
6030 }
6031 }
Romain Guy06882f82009-06-10 13:36:04 -07006032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 void handleNewWindowLocked(WindowState newWindow) {
6034 if (!newWindow.canReceiveKeys()) {
6035 return;
6036 }
6037 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006038 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 TAG, "New key dispatch window: win="
6040 + newWindow.mClient.asBinder()
6041 + ", last=" + mLastBinder
6042 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6043 + "), finished=" + mFinished + ", paused="
6044 + newWindow.mToken.paused);
6045
6046 // Displaying a window implicitly causes dispatching to
6047 // be unpaused. (This is to protect against bugs if someone
6048 // pauses dispatching but forgets to resume.)
6049 newWindow.mToken.paused = false;
6050
6051 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052
6053 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6054 if (DEBUG_INPUT) Log.v(TAG,
6055 "New SYSTEM_ERROR window; resetting state");
6056 mLastWin = null;
6057 mLastBinder = null;
6058 mMotionTarget = null;
6059 mFinished = true;
6060 } else if (mLastWin != null) {
6061 // If the new window is above the window we are
6062 // waiting on, then stop waiting and let key dispatching
6063 // start on the new guy.
6064 if (DEBUG_INPUT) Log.v(
6065 TAG, "Last win layer=" + mLastWin.mLayer
6066 + ", new win layer=" + newWindow.mLayer);
6067 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006068 // The new window is above the old; finish pending input to the last
6069 // window and start directing it to the new one.
6070 mLastWin.mToken.paused = false;
6071 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006073 // Either the new window is lower, so there is no need to wake key waiters,
6074 // or we just finished key input to the previous window, which implicitly
6075 // notified the key waiters. In both cases, we don't need to issue the
6076 // notification here.
6077 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 }
6079
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006080 // Now that we've put a new window state in place, make the event waiter
6081 // take notice and retarget its attentions.
6082 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 }
6084 }
6085
6086 void pauseDispatchingLocked(WindowToken token) {
6087 synchronized (this)
6088 {
6089 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6090 token.paused = true;
6091
6092 /*
6093 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6094 mPaused = true;
6095 } else {
6096 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006097 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006099 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006101 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 }
6103 }
6104 */
6105 }
6106 }
6107
6108 void resumeDispatchingLocked(WindowToken token) {
6109 synchronized (this) {
6110 if (token.paused) {
6111 if (DEBUG_INPUT) Log.v(
6112 TAG, "Resuming WindowToken " + token
6113 + ", last=" + mLastBinder
6114 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6115 + "), finished=" + mFinished + ", paused="
6116 + token.paused);
6117 token.paused = false;
6118 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6119 doFinishedKeyLocked(true);
6120 } else {
6121 notifyAll();
6122 }
6123 }
6124 }
6125 }
6126
6127 void setEventDispatchingLocked(boolean enabled) {
6128 synchronized (this) {
6129 mEventDispatching = enabled;
6130 notifyAll();
6131 }
6132 }
Romain Guy06882f82009-06-10 13:36:04 -07006133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 void appSwitchComing() {
6135 synchronized (this) {
6136 // Don't wait for more than .5 seconds for app to finish
6137 // processing the pending events.
6138 long now = SystemClock.uptimeMillis() + 500;
6139 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6140 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6141 mTimeToSwitch = now;
6142 }
6143 notifyAll();
6144 }
6145 }
Romain Guy06882f82009-06-10 13:36:04 -07006146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006147 private final void doFinishedKeyLocked(boolean doRecycle) {
6148 if (mLastWin != null) {
6149 releasePendingPointerLocked(mLastWin.mSession);
6150 releasePendingTrackballLocked(mLastWin.mSession);
6151 }
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 if (mLastWin == null || !mLastWin.mToken.paused
6154 || !mLastWin.isVisibleLw()) {
6155 // If the current window has been paused, we aren't -really-
6156 // finished... so let the waiters still wait.
6157 mLastWin = null;
6158 mLastBinder = null;
6159 }
6160 mFinished = true;
6161 notifyAll();
6162 }
6163 }
6164
6165 private class KeyQ extends KeyInputQueue
6166 implements KeyInputQueue.FilterCallback {
6167 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006170 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6172 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6173 "KEEP_SCREEN_ON_FLAG");
6174 mHoldingScreen.setReferenceCounted(false);
6175 }
6176
6177 @Override
6178 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6179 if (mPolicy.preprocessInputEventTq(event)) {
6180 return true;
6181 }
Romain Guy06882f82009-06-10 13:36:04 -07006182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 switch (event.type) {
6184 case RawInputEvent.EV_KEY: {
6185 // XXX begin hack
6186 if (DEBUG) {
6187 if (event.keycode == KeyEvent.KEYCODE_G) {
6188 if (event.value != 0) {
6189 // G down
6190 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6191 }
6192 return false;
6193 }
6194 if (event.keycode == KeyEvent.KEYCODE_D) {
6195 if (event.value != 0) {
6196 //dump();
6197 }
6198 return false;
6199 }
6200 }
6201 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006202
Charles Mendis322591c2009-10-29 11:06:59 -07006203 boolean screenIsOff = !mPowerManager.isScreenOn();
6204 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6208 mPowerManager.goToSleep(event.when);
6209 }
6210
6211 if (screenIsOff) {
6212 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6213 }
6214 if (screenIsDim) {
6215 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6216 }
6217 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6218 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006219 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 }
Romain Guy06882f82009-06-10 13:36:04 -07006221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6223 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6224 filterQueue(this);
6225 mKeyWaiter.appSwitchComing();
6226 }
6227 return true;
6228 } else {
6229 return false;
6230 }
6231 }
Romain Guy06882f82009-06-10 13:36:04 -07006232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006234 boolean screenIsOff = !mPowerManager.isScreenOn();
6235 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006236 if (screenIsOff) {
6237 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6238 device.classes, event)) {
6239 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6240 return false;
6241 }
6242 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6243 }
6244 if (screenIsDim) {
6245 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6246 }
6247 return true;
6248 }
Romain Guy06882f82009-06-10 13:36:04 -07006249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006251 boolean screenIsOff = !mPowerManager.isScreenOn();
6252 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 if (screenIsOff) {
6254 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6255 device.classes, event)) {
6256 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6257 return false;
6258 }
6259 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6260 }
6261 if (screenIsDim) {
6262 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6263 }
6264 return true;
6265 }
Romain Guy06882f82009-06-10 13:36:04 -07006266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 default:
6268 return true;
6269 }
6270 }
6271
6272 public int filterEvent(QueuedEvent ev) {
6273 switch (ev.classType) {
6274 case RawInputEvent.CLASS_KEYBOARD:
6275 KeyEvent ke = (KeyEvent)ev.event;
6276 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6277 Log.w(TAG, "Dropping movement key during app switch: "
6278 + ke.getKeyCode() + ", action=" + ke.getAction());
6279 return FILTER_REMOVE;
6280 }
6281 return FILTER_ABORT;
6282 default:
6283 return FILTER_KEEP;
6284 }
6285 }
Romain Guy06882f82009-06-10 13:36:04 -07006286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287 /**
6288 * Must be called with the main window manager lock held.
6289 */
6290 void setHoldScreenLocked(boolean holding) {
6291 boolean state = mHoldingScreen.isHeld();
6292 if (holding != state) {
6293 if (holding) {
6294 mHoldingScreen.acquire();
6295 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006296 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006297 mHoldingScreen.release();
6298 }
6299 }
6300 }
Michael Chan53071d62009-05-13 17:29:48 -07006301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302
6303 public boolean detectSafeMode() {
6304 mSafeMode = mPolicy.detectSafeMode();
6305 return mSafeMode;
6306 }
Romain Guy06882f82009-06-10 13:36:04 -07006307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 public void systemReady() {
6309 mPolicy.systemReady();
6310 }
Romain Guy06882f82009-06-10 13:36:04 -07006311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 private final class InputDispatcherThread extends Thread {
6313 // Time to wait when there is nothing to do: 9999 seconds.
6314 static final int LONG_WAIT=9999*1000;
6315
6316 public InputDispatcherThread() {
6317 super("InputDispatcher");
6318 }
Romain Guy06882f82009-06-10 13:36:04 -07006319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 @Override
6321 public void run() {
6322 while (true) {
6323 try {
6324 process();
6325 } catch (Exception e) {
6326 Log.e(TAG, "Exception in input dispatcher", e);
6327 }
6328 }
6329 }
Romain Guy06882f82009-06-10 13:36:04 -07006330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 private void process() {
6332 android.os.Process.setThreadPriority(
6333 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 // The last key event we saw
6336 KeyEvent lastKey = null;
6337
6338 // Last keydown time for auto-repeating keys
6339 long lastKeyTime = SystemClock.uptimeMillis();
6340 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006341 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342
Romain Guy06882f82009-06-10 13:36:04 -07006343 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 int keyRepeatCount = 0;
6345
6346 // Need to report that configuration has changed?
6347 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 while (true) {
6350 long curTime = SystemClock.uptimeMillis();
6351
6352 if (DEBUG_INPUT) Log.v(
6353 TAG, "Waiting for next key: now=" + curTime
6354 + ", repeat @ " + nextKeyTime);
6355
6356 // Retrieve next event, waiting only as long as the next
6357 // repeat timeout. If the configuration has changed, then
6358 // don't wait at all -- we'll report the change as soon as
6359 // we have processed all events.
6360 QueuedEvent ev = mQueue.getEvent(
6361 (int)((!configChanged && curTime < nextKeyTime)
6362 ? (nextKeyTime-curTime) : 0));
6363
6364 if (DEBUG_INPUT && ev != null) Log.v(
6365 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6366
Michael Chan53071d62009-05-13 17:29:48 -07006367 if (MEASURE_LATENCY) {
6368 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6369 }
6370
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006371 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6372 // cancel key repeat at the request of the policy.
6373 lastKey = null;
6374 downTime = 0;
6375 lastKeyTime = curTime;
6376 nextKeyTime = curTime + LONG_WAIT;
6377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 try {
6379 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006380 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 int eventType;
6382 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6383 eventType = eventType((MotionEvent)ev.event);
6384 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6385 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6386 eventType = LocalPowerManager.BUTTON_EVENT;
6387 } else {
6388 eventType = LocalPowerManager.OTHER_EVENT;
6389 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006390 try {
Michael Chan53071d62009-05-13 17:29:48 -07006391 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006392 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006393 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006394 mBatteryStats.noteInputEvent();
6395 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006396 } catch (RemoteException e) {
6397 // Ignore
6398 }
Michael Chane10de972009-05-18 11:24:50 -07006399
6400 if (eventType != TOUCH_EVENT
6401 && eventType != LONG_TOUCH_EVENT
6402 && eventType != CHEEK_EVENT) {
6403 mPowerManager.userActivity(curTime, false,
6404 eventType, false);
6405 } else if (mLastTouchEventType != eventType
6406 || (curTime - mLastUserActivityCallTime)
6407 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6408 mLastUserActivityCallTime = curTime;
6409 mLastTouchEventType = eventType;
6410 mPowerManager.userActivity(curTime, false,
6411 eventType, false);
6412 }
6413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 switch (ev.classType) {
6415 case RawInputEvent.CLASS_KEYBOARD:
6416 KeyEvent ke = (KeyEvent)ev.event;
6417 if (ke.isDown()) {
6418 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006419 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 keyRepeatCount = 0;
6421 lastKeyTime = curTime;
6422 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006423 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 if (DEBUG_INPUT) Log.v(
6425 TAG, "Received key down: first repeat @ "
6426 + nextKeyTime);
6427 } else {
6428 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006429 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 // Arbitrary long timeout.
6431 lastKeyTime = curTime;
6432 nextKeyTime = curTime + LONG_WAIT;
6433 if (DEBUG_INPUT) Log.v(
6434 TAG, "Received key up: ignore repeat @ "
6435 + nextKeyTime);
6436 }
6437 dispatchKey((KeyEvent)ev.event, 0, 0);
6438 mQueue.recycleEvent(ev);
6439 break;
6440 case RawInputEvent.CLASS_TOUCHSCREEN:
6441 //Log.i(TAG, "Read next event " + ev);
6442 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6443 break;
6444 case RawInputEvent.CLASS_TRACKBALL:
6445 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6446 break;
6447 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6448 configChanged = true;
6449 break;
6450 default:
6451 mQueue.recycleEvent(ev);
6452 break;
6453 }
Romain Guy06882f82009-06-10 13:36:04 -07006454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 } else if (configChanged) {
6456 configChanged = false;
6457 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 } else if (lastKey != null) {
6460 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 // Timeout occurred while key was down. If it is at or
6463 // past the key repeat time, dispatch the repeat.
6464 if (DEBUG_INPUT) Log.v(
6465 TAG, "Key timeout: repeat=" + nextKeyTime
6466 + ", now=" + curTime);
6467 if (curTime < nextKeyTime) {
6468 continue;
6469 }
Romain Guy06882f82009-06-10 13:36:04 -07006470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 lastKeyTime = nextKeyTime;
6472 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6473 keyRepeatCount++;
6474 if (DEBUG_INPUT) Log.v(
6475 TAG, "Key repeat: count=" + keyRepeatCount
6476 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006477 KeyEvent newEvent;
6478 if (downTime != 0 && (downTime
6479 + ViewConfiguration.getLongPressTimeout())
6480 <= curTime) {
6481 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6482 curTime, keyRepeatCount,
6483 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6484 downTime = 0;
6485 } else {
6486 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6487 curTime, keyRepeatCount);
6488 }
6489 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 } else {
6492 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 lastKeyTime = curTime;
6495 nextKeyTime = curTime + LONG_WAIT;
6496 }
Romain Guy06882f82009-06-10 13:36:04 -07006497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 } catch (Exception e) {
6499 Log.e(TAG,
6500 "Input thread received uncaught exception: " + e, e);
6501 }
6502 }
6503 }
6504 }
6505
6506 // -------------------------------------------------------------
6507 // Client Session State
6508 // -------------------------------------------------------------
6509
6510 private final class Session extends IWindowSession.Stub
6511 implements IBinder.DeathRecipient {
6512 final IInputMethodClient mClient;
6513 final IInputContext mInputContext;
6514 final int mUid;
6515 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006516 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 SurfaceSession mSurfaceSession;
6518 int mNumWindow = 0;
6519 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 /**
6522 * Current pointer move event being dispatched to client window... must
6523 * hold key lock to access.
6524 */
6525 QueuedEvent mPendingPointerMove;
6526 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 /**
6529 * Current trackball move event being dispatched to client window... must
6530 * hold key lock to access.
6531 */
6532 QueuedEvent mPendingTrackballMove;
6533 WindowState mPendingTrackballWindow;
6534
6535 public Session(IInputMethodClient client, IInputContext inputContext) {
6536 mClient = client;
6537 mInputContext = inputContext;
6538 mUid = Binder.getCallingUid();
6539 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006540 StringBuilder sb = new StringBuilder();
6541 sb.append("Session{");
6542 sb.append(Integer.toHexString(System.identityHashCode(this)));
6543 sb.append(" uid ");
6544 sb.append(mUid);
6545 sb.append("}");
6546 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 synchronized (mWindowMap) {
6549 if (mInputMethodManager == null && mHaveInputMethods) {
6550 IBinder b = ServiceManager.getService(
6551 Context.INPUT_METHOD_SERVICE);
6552 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6553 }
6554 }
6555 long ident = Binder.clearCallingIdentity();
6556 try {
6557 // Note: it is safe to call in to the input method manager
6558 // here because we are not holding our lock.
6559 if (mInputMethodManager != null) {
6560 mInputMethodManager.addClient(client, inputContext,
6561 mUid, mPid);
6562 } else {
6563 client.setUsingInputMethod(false);
6564 }
6565 client.asBinder().linkToDeath(this, 0);
6566 } catch (RemoteException e) {
6567 // The caller has died, so we can just forget about this.
6568 try {
6569 if (mInputMethodManager != null) {
6570 mInputMethodManager.removeClient(client);
6571 }
6572 } catch (RemoteException ee) {
6573 }
6574 } finally {
6575 Binder.restoreCallingIdentity(ident);
6576 }
6577 }
Romain Guy06882f82009-06-10 13:36:04 -07006578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 @Override
6580 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6581 throws RemoteException {
6582 try {
6583 return super.onTransact(code, data, reply, flags);
6584 } catch (RuntimeException e) {
6585 // Log all 'real' exceptions thrown to the caller
6586 if (!(e instanceof SecurityException)) {
6587 Log.e(TAG, "Window Session Crash", e);
6588 }
6589 throw e;
6590 }
6591 }
6592
6593 public void binderDied() {
6594 // Note: it is safe to call in to the input method manager
6595 // here because we are not holding our lock.
6596 try {
6597 if (mInputMethodManager != null) {
6598 mInputMethodManager.removeClient(mClient);
6599 }
6600 } catch (RemoteException e) {
6601 }
6602 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006603 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 mClientDead = true;
6605 killSessionLocked();
6606 }
6607 }
6608
6609 public int add(IWindow window, WindowManager.LayoutParams attrs,
6610 int viewVisibility, Rect outContentInsets) {
6611 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6612 }
Romain Guy06882f82009-06-10 13:36:04 -07006613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 public void remove(IWindow window) {
6615 removeWindow(this, window);
6616 }
Romain Guy06882f82009-06-10 13:36:04 -07006617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6619 int requestedWidth, int requestedHeight, int viewFlags,
6620 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6621 Rect outVisibleInsets, Surface outSurface) {
6622 return relayoutWindow(this, window, attrs,
6623 requestedWidth, requestedHeight, viewFlags, insetsPending,
6624 outFrame, outContentInsets, outVisibleInsets, outSurface);
6625 }
Romain Guy06882f82009-06-10 13:36:04 -07006626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 public void setTransparentRegion(IWindow window, Region region) {
6628 setTransparentRegionWindow(this, window, region);
6629 }
Romain Guy06882f82009-06-10 13:36:04 -07006630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 public void setInsets(IWindow window, int touchableInsets,
6632 Rect contentInsets, Rect visibleInsets) {
6633 setInsetsWindow(this, window, touchableInsets, contentInsets,
6634 visibleInsets);
6635 }
Romain Guy06882f82009-06-10 13:36:04 -07006636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6638 getWindowDisplayFrame(this, window, outDisplayFrame);
6639 }
Romain Guy06882f82009-06-10 13:36:04 -07006640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 public void finishDrawing(IWindow window) {
6642 if (localLOGV) Log.v(
6643 TAG, "IWindow finishDrawing called for " + window);
6644 finishDrawingWindow(this, window);
6645 }
6646
6647 public void finishKey(IWindow window) {
6648 if (localLOGV) Log.v(
6649 TAG, "IWindow finishKey called for " + window);
6650 mKeyWaiter.finishedKey(this, window, false,
6651 KeyWaiter.RETURN_NOTHING);
6652 }
6653
6654 public MotionEvent getPendingPointerMove(IWindow window) {
6655 if (localLOGV) Log.v(
6656 TAG, "IWindow getPendingMotionEvent called for " + window);
6657 return mKeyWaiter.finishedKey(this, window, false,
6658 KeyWaiter.RETURN_PENDING_POINTER);
6659 }
Romain Guy06882f82009-06-10 13:36:04 -07006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 public MotionEvent getPendingTrackballMove(IWindow window) {
6662 if (localLOGV) Log.v(
6663 TAG, "IWindow getPendingMotionEvent called for " + window);
6664 return mKeyWaiter.finishedKey(this, window, false,
6665 KeyWaiter.RETURN_PENDING_TRACKBALL);
6666 }
6667
6668 public void setInTouchMode(boolean mode) {
6669 synchronized(mWindowMap) {
6670 mInTouchMode = mode;
6671 }
6672 }
6673
6674 public boolean getInTouchMode() {
6675 synchronized(mWindowMap) {
6676 return mInTouchMode;
6677 }
6678 }
6679
6680 public boolean performHapticFeedback(IWindow window, int effectId,
6681 boolean always) {
6682 synchronized(mWindowMap) {
6683 long ident = Binder.clearCallingIdentity();
6684 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006685 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 windowForClientLocked(this, window), effectId, always);
6687 } finally {
6688 Binder.restoreCallingIdentity(ident);
6689 }
6690 }
6691 }
Romain Guy06882f82009-06-10 13:36:04 -07006692
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006693 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006694 synchronized(mWindowMap) {
6695 long ident = Binder.clearCallingIdentity();
6696 try {
6697 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006698 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006699 } finally {
6700 Binder.restoreCallingIdentity(ident);
6701 }
6702 }
6703 }
6704
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006705 public void wallpaperOffsetsComplete(IBinder window) {
6706 WindowManagerService.this.wallpaperOffsetsComplete(window);
6707 }
6708
Dianne Hackborn75804932009-10-20 20:15:20 -07006709 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6710 int z, Bundle extras, boolean sync) {
6711 synchronized(mWindowMap) {
6712 long ident = Binder.clearCallingIdentity();
6713 try {
6714 return sendWindowWallpaperCommandLocked(
6715 windowForClientLocked(this, window),
6716 action, x, y, z, extras, sync);
6717 } finally {
6718 Binder.restoreCallingIdentity(ident);
6719 }
6720 }
6721 }
6722
6723 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6724 WindowManagerService.this.wallpaperCommandComplete(window, result);
6725 }
6726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006727 void windowAddedLocked() {
6728 if (mSurfaceSession == null) {
6729 if (localLOGV) Log.v(
6730 TAG, "First window added to " + this + ", creating SurfaceSession");
6731 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006732 if (SHOW_TRANSACTIONS) Log.i(
6733 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 mSessions.add(this);
6735 }
6736 mNumWindow++;
6737 }
6738
6739 void windowRemovedLocked() {
6740 mNumWindow--;
6741 killSessionLocked();
6742 }
Romain Guy06882f82009-06-10 13:36:04 -07006743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006744 void killSessionLocked() {
6745 if (mNumWindow <= 0 && mClientDead) {
6746 mSessions.remove(this);
6747 if (mSurfaceSession != null) {
6748 if (localLOGV) Log.v(
6749 TAG, "Last window removed from " + this
6750 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006751 if (SHOW_TRANSACTIONS) Log.i(
6752 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 try {
6754 mSurfaceSession.kill();
6755 } catch (Exception e) {
6756 Log.w(TAG, "Exception thrown when killing surface session "
6757 + mSurfaceSession + " in session " + this
6758 + ": " + e.toString());
6759 }
6760 mSurfaceSession = null;
6761 }
6762 }
6763 }
Romain Guy06882f82009-06-10 13:36:04 -07006764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006766 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6767 pw.print(" mClientDead="); pw.print(mClientDead);
6768 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6769 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6770 pw.print(prefix);
6771 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6772 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6773 }
6774 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6775 pw.print(prefix);
6776 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6777 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 }
6780
6781 @Override
6782 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006783 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006784 }
6785 }
6786
6787 // -------------------------------------------------------------
6788 // Client Window State
6789 // -------------------------------------------------------------
6790
6791 private final class WindowState implements WindowManagerPolicy.WindowState {
6792 final Session mSession;
6793 final IWindow mClient;
6794 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006795 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 AppWindowToken mAppToken;
6797 AppWindowToken mTargetAppToken;
6798 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6799 final DeathRecipient mDeathRecipient;
6800 final WindowState mAttachedWindow;
6801 final ArrayList mChildWindows = new ArrayList();
6802 final int mBaseLayer;
6803 final int mSubLayer;
6804 final boolean mLayoutAttached;
6805 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006806 final boolean mIsWallpaper;
6807 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 int mViewVisibility;
6809 boolean mPolicyVisibility = true;
6810 boolean mPolicyVisibilityAfterAnim = true;
6811 boolean mAppFreezing;
6812 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006813 boolean mReportDestroySurface;
6814 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 boolean mAttachedHidden; // is our parent window hidden?
6816 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006817 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 int mRequestedWidth;
6819 int mRequestedHeight;
6820 int mLastRequestedWidth;
6821 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 int mLayer;
6823 int mAnimLayer;
6824 int mLastLayer;
6825 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006826 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006827 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828
6829 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 // Actual frame shown on-screen (may be modified by animation)
6832 final Rect mShownFrame = new Rect();
6833 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 /**
6836 * Insets that determine the actually visible area
6837 */
6838 final Rect mVisibleInsets = new Rect();
6839 final Rect mLastVisibleInsets = new Rect();
6840 boolean mVisibleInsetsChanged;
6841
6842 /**
6843 * Insets that are covered by system windows
6844 */
6845 final Rect mContentInsets = new Rect();
6846 final Rect mLastContentInsets = new Rect();
6847 boolean mContentInsetsChanged;
6848
6849 /**
6850 * Set to true if we are waiting for this window to receive its
6851 * given internal insets before laying out other windows based on it.
6852 */
6853 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 /**
6856 * These are the content insets that were given during layout for
6857 * this window, to be applied to windows behind it.
6858 */
6859 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 /**
6862 * These are the visible insets that were given during layout for
6863 * this window, to be applied to windows behind it.
6864 */
6865 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006867 /**
6868 * Flag indicating whether the touchable region should be adjusted by
6869 * the visible insets; if false the area outside the visible insets is
6870 * NOT touchable, so we must use those to adjust the frame during hit
6871 * tests.
6872 */
6873 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 // Current transformation being applied.
6876 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6877 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6878 float mHScale=1, mVScale=1;
6879 float mLastHScale=1, mLastVScale=1;
6880 final Matrix mTmpMatrix = new Matrix();
6881
6882 // "Real" frame that the application sees.
6883 final Rect mFrame = new Rect();
6884 final Rect mLastFrame = new Rect();
6885
6886 final Rect mContainingFrame = new Rect();
6887 final Rect mDisplayFrame = new Rect();
6888 final Rect mContentFrame = new Rect();
6889 final Rect mVisibleFrame = new Rect();
6890
6891 float mShownAlpha = 1;
6892 float mAlpha = 1;
6893 float mLastAlpha = 1;
6894
6895 // Set to true if, when the window gets displayed, it should perform
6896 // an enter animation.
6897 boolean mEnterAnimationPending;
6898
6899 // Currently running animation.
6900 boolean mAnimating;
6901 boolean mLocalAnimating;
6902 Animation mAnimation;
6903 boolean mAnimationIsEntrance;
6904 boolean mHasTransformation;
6905 boolean mHasLocalTransformation;
6906 final Transformation mTransformation = new Transformation();
6907
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006908 // If a window showing a wallpaper: the requested offset for the
6909 // wallpaper; if a wallpaper window: the currently applied offset.
6910 float mWallpaperX = -1;
6911 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006912
6913 // If a window showing a wallpaper: what fraction of the offset
6914 // range corresponds to a full virtual screen.
6915 float mWallpaperXStep = -1;
6916 float mWallpaperYStep = -1;
6917
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006918 // Wallpaper windows: pixels offset based on above variables.
6919 int mXOffset;
6920 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 // This is set after IWindowSession.relayout() has been called at
6923 // least once for the window. It allows us to detect the situation
6924 // where we don't yet have a surface, but should have one soon, so
6925 // we can give the window focus before waiting for the relayout.
6926 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 // This is set after the Surface has been created but before the
6929 // window has been drawn. During this time the surface is hidden.
6930 boolean mDrawPending;
6931
6932 // This is set after the window has finished drawing for the first
6933 // time but before its surface is shown. The surface will be
6934 // displayed when the next layout is run.
6935 boolean mCommitDrawPending;
6936
6937 // This is set during the time after the window's drawing has been
6938 // committed, and before its surface is actually shown. It is used
6939 // to delay showing the surface until all windows in a token are ready
6940 // to be shown.
6941 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 // Set when the window has been shown in the screen the first time.
6944 boolean mHasDrawn;
6945
6946 // Currently running an exit animation?
6947 boolean mExiting;
6948
6949 // Currently on the mDestroySurface list?
6950 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 // Completely remove from window manager after exit animation?
6953 boolean mRemoveOnExit;
6954
6955 // Set when the orientation is changing and this window has not yet
6956 // been updated for the new orientation.
6957 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 // Is this window now (or just being) removed?
6960 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 WindowState(Session s, IWindow c, WindowToken token,
6963 WindowState attachedWindow, WindowManager.LayoutParams a,
6964 int viewVisibility) {
6965 mSession = s;
6966 mClient = c;
6967 mToken = token;
6968 mAttrs.copyFrom(a);
6969 mViewVisibility = viewVisibility;
6970 DeathRecipient deathRecipient = new DeathRecipient();
6971 mAlpha = a.alpha;
6972 if (localLOGV) Log.v(
6973 TAG, "Window " + this + " client=" + c.asBinder()
6974 + " token=" + token + " (" + mAttrs.token + ")");
6975 try {
6976 c.asBinder().linkToDeath(deathRecipient, 0);
6977 } catch (RemoteException e) {
6978 mDeathRecipient = null;
6979 mAttachedWindow = null;
6980 mLayoutAttached = false;
6981 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006982 mIsWallpaper = false;
6983 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 mBaseLayer = 0;
6985 mSubLayer = 0;
6986 return;
6987 }
6988 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6991 mAttrs.type <= LAST_SUB_WINDOW)) {
6992 // The multiplier here is to reserve space for multiple
6993 // windows in the same type layer.
6994 mBaseLayer = mPolicy.windowTypeToLayerLw(
6995 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6996 + TYPE_LAYER_OFFSET;
6997 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6998 mAttachedWindow = attachedWindow;
6999 mAttachedWindow.mChildWindows.add(this);
7000 mLayoutAttached = mAttrs.type !=
7001 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7002 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7003 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007004 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7005 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 } else {
7007 // The multiplier here is to reserve space for multiple
7008 // windows in the same type layer.
7009 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7010 * TYPE_LAYER_MULTIPLIER
7011 + TYPE_LAYER_OFFSET;
7012 mSubLayer = 0;
7013 mAttachedWindow = null;
7014 mLayoutAttached = false;
7015 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7016 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007017 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7018 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 }
7020
7021 WindowState appWin = this;
7022 while (appWin.mAttachedWindow != null) {
7023 appWin = mAttachedWindow;
7024 }
7025 WindowToken appToken = appWin.mToken;
7026 while (appToken.appWindowToken == null) {
7027 WindowToken parent = mTokenMap.get(appToken.token);
7028 if (parent == null || appToken == parent) {
7029 break;
7030 }
7031 appToken = parent;
7032 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007033 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034 mAppToken = appToken.appWindowToken;
7035
7036 mSurface = null;
7037 mRequestedWidth = 0;
7038 mRequestedHeight = 0;
7039 mLastRequestedWidth = 0;
7040 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007041 mXOffset = 0;
7042 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 mLayer = 0;
7044 mAnimLayer = 0;
7045 mLastLayer = 0;
7046 }
7047
7048 void attach() {
7049 if (localLOGV) Log.v(
7050 TAG, "Attaching " + this + " token=" + mToken
7051 + ", list=" + mToken.windows);
7052 mSession.windowAddedLocked();
7053 }
7054
7055 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7056 mHaveFrame = true;
7057
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007058 final Rect container = mContainingFrame;
7059 container.set(pf);
7060
7061 final Rect display = mDisplayFrame;
7062 display.set(df);
7063
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007064 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007065 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007066 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7067 display.intersect(mCompatibleScreenFrame);
7068 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007069 }
7070
7071 final int pw = container.right - container.left;
7072 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073
7074 int w,h;
7075 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7076 w = mAttrs.width < 0 ? pw : mAttrs.width;
7077 h = mAttrs.height< 0 ? ph : mAttrs.height;
7078 } else {
7079 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7080 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7081 }
Romain Guy06882f82009-06-10 13:36:04 -07007082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 final Rect content = mContentFrame;
7084 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 final Rect visible = mVisibleFrame;
7087 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007090 final int fw = frame.width();
7091 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7094 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7095
7096 Gravity.apply(mAttrs.gravity, w, h, container,
7097 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7098 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7099
7100 //System.out.println("Out: " + mFrame);
7101
7102 // Now make sure the window fits in the overall display.
7103 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 // Make sure the content and visible frames are inside of the
7106 // final window frame.
7107 if (content.left < frame.left) content.left = frame.left;
7108 if (content.top < frame.top) content.top = frame.top;
7109 if (content.right > frame.right) content.right = frame.right;
7110 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7111 if (visible.left < frame.left) visible.left = frame.left;
7112 if (visible.top < frame.top) visible.top = frame.top;
7113 if (visible.right > frame.right) visible.right = frame.right;
7114 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007116 final Rect contentInsets = mContentInsets;
7117 contentInsets.left = content.left-frame.left;
7118 contentInsets.top = content.top-frame.top;
7119 contentInsets.right = frame.right-content.right;
7120 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 final Rect visibleInsets = mVisibleInsets;
7123 visibleInsets.left = visible.left-frame.left;
7124 visibleInsets.top = visible.top-frame.top;
7125 visibleInsets.right = frame.right-visible.right;
7126 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007127
Dianne Hackborn284ac932009-08-28 10:34:25 -07007128 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7129 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007130 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007131 }
7132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 if (localLOGV) {
7134 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7135 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7136 Log.v(TAG, "Resolving (mRequestedWidth="
7137 + mRequestedWidth + ", mRequestedheight="
7138 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7139 + "): frame=" + mFrame.toShortString()
7140 + " ci=" + contentInsets.toShortString()
7141 + " vi=" + visibleInsets.toShortString());
7142 //}
7143 }
7144 }
Romain Guy06882f82009-06-10 13:36:04 -07007145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 public Rect getFrameLw() {
7147 return mFrame;
7148 }
7149
7150 public Rect getShownFrameLw() {
7151 return mShownFrame;
7152 }
7153
7154 public Rect getDisplayFrameLw() {
7155 return mDisplayFrame;
7156 }
7157
7158 public Rect getContentFrameLw() {
7159 return mContentFrame;
7160 }
7161
7162 public Rect getVisibleFrameLw() {
7163 return mVisibleFrame;
7164 }
7165
7166 public boolean getGivenInsetsPendingLw() {
7167 return mGivenInsetsPending;
7168 }
7169
7170 public Rect getGivenContentInsetsLw() {
7171 return mGivenContentInsets;
7172 }
Romain Guy06882f82009-06-10 13:36:04 -07007173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 public Rect getGivenVisibleInsetsLw() {
7175 return mGivenVisibleInsets;
7176 }
Romain Guy06882f82009-06-10 13:36:04 -07007177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 public WindowManager.LayoutParams getAttrs() {
7179 return mAttrs;
7180 }
7181
7182 public int getSurfaceLayer() {
7183 return mLayer;
7184 }
Romain Guy06882f82009-06-10 13:36:04 -07007185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 public IApplicationToken getAppToken() {
7187 return mAppToken != null ? mAppToken.appToken : null;
7188 }
7189
7190 public boolean hasAppShownWindows() {
7191 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7192 }
7193
7194 public boolean hasAppStartingIcon() {
7195 return mAppToken != null ? (mAppToken.startingData != null) : false;
7196 }
7197
7198 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7199 return mAppToken != null ? mAppToken.startingWindow : null;
7200 }
7201
7202 public void setAnimation(Animation anim) {
7203 if (localLOGV) Log.v(
7204 TAG, "Setting animation in " + this + ": " + anim);
7205 mAnimating = false;
7206 mLocalAnimating = false;
7207 mAnimation = anim;
7208 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7209 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7210 }
7211
7212 public void clearAnimation() {
7213 if (mAnimation != null) {
7214 mAnimating = true;
7215 mLocalAnimating = false;
7216 mAnimation = null;
7217 }
7218 }
Romain Guy06882f82009-06-10 13:36:04 -07007219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 Surface createSurfaceLocked() {
7221 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007222 mReportDestroySurface = false;
7223 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 mDrawPending = true;
7225 mCommitDrawPending = false;
7226 mReadyToShow = false;
7227 if (mAppToken != null) {
7228 mAppToken.allDrawn = false;
7229 }
7230
7231 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007232 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007233 flags |= Surface.PUSH_BUFFERS;
7234 }
7235
7236 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7237 flags |= Surface.SECURE;
7238 }
7239 if (DEBUG_VISIBILITY) Log.v(
7240 TAG, "Creating surface in session "
7241 + mSession.mSurfaceSession + " window " + this
7242 + " w=" + mFrame.width()
7243 + " h=" + mFrame.height() + " format="
7244 + mAttrs.format + " flags=" + flags);
7245
7246 int w = mFrame.width();
7247 int h = mFrame.height();
7248 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7249 // for a scaled surface, we always want the requested
7250 // size.
7251 w = mRequestedWidth;
7252 h = mRequestedHeight;
7253 }
7254
Romain Guy9825ec62009-10-01 00:58:09 -07007255 // Something is wrong and SurfaceFlinger will not like this,
7256 // try to revert to sane values
7257 if (w <= 0) w = 1;
7258 if (h <= 0) h = 1;
7259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 try {
7261 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007262 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007263 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007264 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7265 + mSurface + " IN SESSION "
7266 + mSession.mSurfaceSession
7267 + ": pid=" + mSession.mPid + " format="
7268 + mAttrs.format + " flags=0x"
7269 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 } catch (Surface.OutOfResourcesException e) {
7271 Log.w(TAG, "OutOfResourcesException creating surface");
7272 reclaimSomeSurfaceMemoryLocked(this, "create");
7273 return null;
7274 } catch (Exception e) {
7275 Log.e(TAG, "Exception creating surface", e);
7276 return null;
7277 }
Romain Guy06882f82009-06-10 13:36:04 -07007278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 if (localLOGV) Log.v(
7280 TAG, "Got surface: " + mSurface
7281 + ", set left=" + mFrame.left + " top=" + mFrame.top
7282 + ", animLayer=" + mAnimLayer);
7283 if (SHOW_TRANSACTIONS) {
7284 Log.i(TAG, ">>> OPEN TRANSACTION");
7285 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7286 + mAttrs.getTitle() + ") pos=(" +
7287 mFrame.left + "," + mFrame.top + ") (" +
7288 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7289 mAnimLayer + " HIDE");
7290 }
7291 Surface.openTransaction();
7292 try {
7293 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007294 mSurface.setPosition(mFrame.left + mXOffset,
7295 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 mSurface.setLayer(mAnimLayer);
7297 mSurface.hide();
7298 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007299 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7300 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 mSurface.setFlags(Surface.SURFACE_DITHER,
7302 Surface.SURFACE_DITHER);
7303 }
7304 } catch (RuntimeException e) {
7305 Log.w(TAG, "Error creating surface in " + w, e);
7306 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7307 }
7308 mLastHidden = true;
7309 } finally {
7310 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7311 Surface.closeTransaction();
7312 }
7313 if (localLOGV) Log.v(
7314 TAG, "Created surface " + this);
7315 }
7316 return mSurface;
7317 }
Romain Guy06882f82009-06-10 13:36:04 -07007318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 void destroySurfaceLocked() {
7320 // Window is no longer on-screen, so can no longer receive
7321 // key events... if we were waiting for it to finish
7322 // handling a key event, the wait is over!
7323 mKeyWaiter.finishedKey(mSession, mClient, true,
7324 KeyWaiter.RETURN_NOTHING);
7325 mKeyWaiter.releasePendingPointerLocked(mSession);
7326 mKeyWaiter.releasePendingTrackballLocked(mSession);
7327
7328 if (mAppToken != null && this == mAppToken.startingWindow) {
7329 mAppToken.startingDisplayed = false;
7330 }
Romain Guy06882f82009-06-10 13:36:04 -07007331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007333 mDrawPending = false;
7334 mCommitDrawPending = false;
7335 mReadyToShow = false;
7336
7337 int i = mChildWindows.size();
7338 while (i > 0) {
7339 i--;
7340 WindowState c = (WindowState)mChildWindows.get(i);
7341 c.mAttachedHidden = true;
7342 }
7343
7344 if (mReportDestroySurface) {
7345 mReportDestroySurface = false;
7346 mSurfacePendingDestroy = true;
7347 try {
7348 mClient.dispatchGetNewSurface();
7349 // We'll really destroy on the next time around.
7350 return;
7351 } catch (RemoteException e) {
7352 }
7353 }
7354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007355 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007356 if (DEBUG_VISIBILITY) {
7357 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007358 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007359 Log.w(TAG, "Window " + this + " destroying surface "
7360 + mSurface + ", session " + mSession, e);
7361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 if (SHOW_TRANSACTIONS) {
7363 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007364 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7366 + mAttrs.getTitle() + ")", ex);
7367 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007368 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 } catch (RuntimeException e) {
7370 Log.w(TAG, "Exception thrown when destroying Window " + this
7371 + " surface " + mSurface + " session " + mSession
7372 + ": " + e.toString());
7373 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 }
7377 }
7378
7379 boolean finishDrawingLocked() {
7380 if (mDrawPending) {
7381 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7382 TAG, "finishDrawingLocked: " + mSurface);
7383 mCommitDrawPending = true;
7384 mDrawPending = false;
7385 return true;
7386 }
7387 return false;
7388 }
7389
7390 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007391 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7393 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007394 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 }
7396 mCommitDrawPending = false;
7397 mReadyToShow = true;
7398 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7399 final AppWindowToken atoken = mAppToken;
7400 if (atoken == null || atoken.allDrawn || starting) {
7401 performShowLocked();
7402 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007403 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007404 }
7405
7406 // This must be called while inside a transaction.
7407 boolean performShowLocked() {
7408 if (DEBUG_VISIBILITY) {
7409 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007410 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 Log.v(TAG, "performShow on " + this
7412 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7413 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7414 }
7415 if (mReadyToShow && isReadyForDisplay()) {
7416 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7417 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7418 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7419 + " during animation: policyVis=" + mPolicyVisibility
7420 + " attHidden=" + mAttachedHidden
7421 + " tok.hiddenRequested="
7422 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007423 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 + (mAppToken != null ? mAppToken.hidden : false)
7425 + " animating=" + mAnimating
7426 + " tok animating="
7427 + (mAppToken != null ? mAppToken.animating : false));
7428 if (!showSurfaceRobustlyLocked(this)) {
7429 return false;
7430 }
7431 mLastAlpha = -1;
7432 mHasDrawn = true;
7433 mLastHidden = false;
7434 mReadyToShow = false;
7435 enableScreenIfNeededLocked();
7436
7437 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 int i = mChildWindows.size();
7440 while (i > 0) {
7441 i--;
7442 WindowState c = (WindowState)mChildWindows.get(i);
7443 if (c.mSurface != null && c.mAttachedHidden) {
7444 c.mAttachedHidden = false;
7445 c.performShowLocked();
7446 }
7447 }
Romain Guy06882f82009-06-10 13:36:04 -07007448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007449 if (mAttrs.type != TYPE_APPLICATION_STARTING
7450 && mAppToken != null) {
7451 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007452
7453 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007454 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7455 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007456 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007457 // If this initial window is animating, stop it -- we
7458 // will do an animation to reveal it from behind the
7459 // starting window, so there is no need for it to also
7460 // be doing its own stuff.
7461 if (mAnimation != null) {
7462 mAnimation = null;
7463 // Make sure we clean up the animation.
7464 mAnimating = true;
7465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 mFinishedStarting.add(mAppToken);
7467 mH.sendEmptyMessage(H.FINISHED_STARTING);
7468 }
7469 mAppToken.updateReportedVisibilityLocked();
7470 }
7471 }
7472 return true;
7473 }
Romain Guy06882f82009-06-10 13:36:04 -07007474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 // This must be called while inside a transaction. Returns true if
7476 // there is more animation to run.
7477 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7478 if (!mDisplayFrozen) {
7479 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7482 mHasTransformation = true;
7483 mHasLocalTransformation = true;
7484 if (!mLocalAnimating) {
7485 if (DEBUG_ANIM) Log.v(
7486 TAG, "Starting animation in " + this +
7487 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7488 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7489 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7490 mAnimation.setStartTime(currentTime);
7491 mLocalAnimating = true;
7492 mAnimating = true;
7493 }
7494 mTransformation.clear();
7495 final boolean more = mAnimation.getTransformation(
7496 currentTime, mTransformation);
7497 if (DEBUG_ANIM) Log.v(
7498 TAG, "Stepped animation in " + this +
7499 ": more=" + more + ", xform=" + mTransformation);
7500 if (more) {
7501 // we're not done!
7502 return true;
7503 }
7504 if (DEBUG_ANIM) Log.v(
7505 TAG, "Finished animation in " + this +
7506 " @ " + currentTime);
7507 mAnimation = null;
7508 //WindowManagerService.this.dump();
7509 }
7510 mHasLocalTransformation = false;
7511 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007512 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 // When our app token is animating, we kind-of pretend like
7514 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7515 // part of this check means that we will only do this if
7516 // our window is not currently exiting, or it is not
7517 // locally animating itself. The idea being that one that
7518 // is exiting and doing a local animation should be removed
7519 // once that animation is done.
7520 mAnimating = true;
7521 mHasTransformation = true;
7522 mTransformation.clear();
7523 return false;
7524 } else if (mHasTransformation) {
7525 // Little trick to get through the path below to act like
7526 // we have finished an animation.
7527 mAnimating = true;
7528 } else if (isAnimating()) {
7529 mAnimating = true;
7530 }
7531 } else if (mAnimation != null) {
7532 // If the display is frozen, and there is a pending animation,
7533 // clear it and make sure we run the cleanup code.
7534 mAnimating = true;
7535 mLocalAnimating = true;
7536 mAnimation = null;
7537 }
Romain Guy06882f82009-06-10 13:36:04 -07007538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 if (!mAnimating && !mLocalAnimating) {
7540 return false;
7541 }
7542
7543 if (DEBUG_ANIM) Log.v(
7544 TAG, "Animation done in " + this + ": exiting=" + mExiting
7545 + ", reportedVisible="
7546 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 mAnimating = false;
7549 mLocalAnimating = false;
7550 mAnimation = null;
7551 mAnimLayer = mLayer;
7552 if (mIsImWindow) {
7553 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007554 } else if (mIsWallpaper) {
7555 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 }
7557 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7558 + " anim layer: " + mAnimLayer);
7559 mHasTransformation = false;
7560 mHasLocalTransformation = false;
7561 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7562 mTransformation.clear();
7563 if (mHasDrawn
7564 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7565 && mAppToken != null
7566 && mAppToken.firstWindowDrawn
7567 && mAppToken.startingData != null) {
7568 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7569 + mToken + ": first real window done animating");
7570 mFinishedStarting.add(mAppToken);
7571 mH.sendEmptyMessage(H.FINISHED_STARTING);
7572 }
Romain Guy06882f82009-06-10 13:36:04 -07007573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 finishExit();
7575
7576 if (mAppToken != null) {
7577 mAppToken.updateReportedVisibilityLocked();
7578 }
7579
7580 return false;
7581 }
7582
7583 void finishExit() {
7584 if (DEBUG_ANIM) Log.v(
7585 TAG, "finishExit in " + this
7586 + ": exiting=" + mExiting
7587 + " remove=" + mRemoveOnExit
7588 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007590 final int N = mChildWindows.size();
7591 for (int i=0; i<N; i++) {
7592 ((WindowState)mChildWindows.get(i)).finishExit();
7593 }
Romain Guy06882f82009-06-10 13:36:04 -07007594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 if (!mExiting) {
7596 return;
7597 }
Romain Guy06882f82009-06-10 13:36:04 -07007598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 if (isWindowAnimating()) {
7600 return;
7601 }
7602
7603 if (localLOGV) Log.v(
7604 TAG, "Exit animation finished in " + this
7605 + ": remove=" + mRemoveOnExit);
7606 if (mSurface != null) {
7607 mDestroySurface.add(this);
7608 mDestroying = true;
7609 if (SHOW_TRANSACTIONS) Log.i(
7610 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7611 try {
7612 mSurface.hide();
7613 } catch (RuntimeException e) {
7614 Log.w(TAG, "Error hiding surface in " + this, e);
7615 }
7616 mLastHidden = true;
7617 mKeyWaiter.releasePendingPointerLocked(mSession);
7618 }
7619 mExiting = false;
7620 if (mRemoveOnExit) {
7621 mPendingRemove.add(this);
7622 mRemoveOnExit = false;
7623 }
7624 }
Romain Guy06882f82009-06-10 13:36:04 -07007625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7627 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7628 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7629 if (dtdx < -.000001f || dtdx > .000001f) return false;
7630 if (dsdy < -.000001f || dsdy > .000001f) return false;
7631 return true;
7632 }
Romain Guy06882f82009-06-10 13:36:04 -07007633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 void computeShownFrameLocked() {
7635 final boolean selfTransformation = mHasLocalTransformation;
7636 Transformation attachedTransformation =
7637 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7638 ? mAttachedWindow.mTransformation : null;
7639 Transformation appTransformation =
7640 (mAppToken != null && mAppToken.hasTransformation)
7641 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007642
7643 // Wallpapers are animated based on the "real" window they
7644 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007645 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007646 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007647 if (mWallpaperTarget.mHasLocalTransformation &&
7648 mWallpaperTarget.mAnimation != null &&
7649 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007650 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007651 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7652 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7653 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007654 }
7655 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007656 mWallpaperTarget.mAppToken.hasTransformation &&
7657 mWallpaperTarget.mAppToken.animation != null &&
7658 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007659 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007660 if (DEBUG_WALLPAPER && appTransformation != null) {
7661 Log.v(TAG, "WP target app xform: " + appTransformation);
7662 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007663 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007664 }
7665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 if (selfTransformation || attachedTransformation != null
7667 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007668 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 final Rect frame = mFrame;
7670 final float tmpFloats[] = mTmpFloats;
7671 final Matrix tmpMatrix = mTmpMatrix;
7672
7673 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007674 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007675 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007676 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007678 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007680 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 }
7682 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007683 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 }
7685
7686 // "convert" it into SurfaceFlinger's format
7687 // (a 2x2 matrix + an offset)
7688 // Here we must not transform the position of the surface
7689 // since it is already included in the transformation.
7690 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 tmpMatrix.getValues(tmpFloats);
7693 mDsDx = tmpFloats[Matrix.MSCALE_X];
7694 mDtDx = tmpFloats[Matrix.MSKEW_X];
7695 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7696 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007697 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7698 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 int w = frame.width();
7700 int h = frame.height();
7701 mShownFrame.set(x, y, x+w, y+h);
7702
7703 // Now set the alpha... but because our current hardware
7704 // can't do alpha transformation on a non-opaque surface,
7705 // turn it off if we are running an animation that is also
7706 // transforming since it is more important to have that
7707 // animation be smooth.
7708 mShownAlpha = mAlpha;
7709 if (!mLimitedAlphaCompositing
7710 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7711 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7712 && x == frame.left && y == frame.top))) {
7713 //Log.i(TAG, "Applying alpha transform");
7714 if (selfTransformation) {
7715 mShownAlpha *= mTransformation.getAlpha();
7716 }
7717 if (attachedTransformation != null) {
7718 mShownAlpha *= attachedTransformation.getAlpha();
7719 }
7720 if (appTransformation != null) {
7721 mShownAlpha *= appTransformation.getAlpha();
7722 }
7723 } else {
7724 //Log.i(TAG, "Not applying alpha transform");
7725 }
Romain Guy06882f82009-06-10 13:36:04 -07007726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 if (localLOGV) Log.v(
7728 TAG, "Continuing animation in " + this +
7729 ": " + mShownFrame +
7730 ", alpha=" + mTransformation.getAlpha());
7731 return;
7732 }
Romain Guy06882f82009-06-10 13:36:04 -07007733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007735 if (mXOffset != 0 || mYOffset != 0) {
7736 mShownFrame.offset(mXOffset, mYOffset);
7737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 mShownAlpha = mAlpha;
7739 mDsDx = 1;
7740 mDtDx = 0;
7741 mDsDy = 0;
7742 mDtDy = 1;
7743 }
Romain Guy06882f82009-06-10 13:36:04 -07007744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 /**
7746 * Is this window visible? It is not visible if there is no
7747 * surface, or we are in the process of running an exit animation
7748 * that will remove the surface, or its app token has been hidden.
7749 */
7750 public boolean isVisibleLw() {
7751 final AppWindowToken atoken = mAppToken;
7752 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7753 && (atoken == null || !atoken.hiddenRequested)
7754 && !mExiting && !mDestroying;
7755 }
7756
7757 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007758 * Like {@link #isVisibleLw}, but also counts a window that is currently
7759 * "hidden" behind the keyguard as visible. This allows us to apply
7760 * things like window flags that impact the keyguard.
7761 * XXX I am starting to think we need to have ANOTHER visibility flag
7762 * for this "hidden behind keyguard" state rather than overloading
7763 * mPolicyVisibility. Ungh.
7764 */
7765 public boolean isVisibleOrBehindKeyguardLw() {
7766 final AppWindowToken atoken = mAppToken;
7767 return mSurface != null && !mAttachedHidden
7768 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7769 && !mExiting && !mDestroying;
7770 }
7771
7772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 * Is this window visible, ignoring its app token? It is not visible
7774 * if there is no surface, or we are in the process of running an exit animation
7775 * that will remove the surface.
7776 */
7777 public boolean isWinVisibleLw() {
7778 final AppWindowToken atoken = mAppToken;
7779 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7780 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7781 && !mExiting && !mDestroying;
7782 }
7783
7784 /**
7785 * The same as isVisible(), but follows the current hidden state of
7786 * the associated app token, not the pending requested hidden state.
7787 */
7788 boolean isVisibleNow() {
7789 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007790 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 }
7792
7793 /**
7794 * Same as isVisible(), but we also count it as visible between the
7795 * call to IWindowSession.add() and the first relayout().
7796 */
7797 boolean isVisibleOrAdding() {
7798 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007799 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7801 && mPolicyVisibility && !mAttachedHidden
7802 && (atoken == null || !atoken.hiddenRequested)
7803 && !mExiting && !mDestroying;
7804 }
7805
7806 /**
7807 * Is this window currently on-screen? It is on-screen either if it
7808 * is visible or it is currently running an animation before no longer
7809 * being visible.
7810 */
7811 boolean isOnScreen() {
7812 final AppWindowToken atoken = mAppToken;
7813 if (atoken != null) {
7814 return mSurface != null && mPolicyVisibility && !mDestroying
7815 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007816 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 } else {
7818 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007819 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 }
7821 }
Romain Guy06882f82009-06-10 13:36:04 -07007822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 /**
7824 * Like isOnScreen(), but we don't return true if the window is part
7825 * of a transition that has not yet been started.
7826 */
7827 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007828 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007829 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007830 return false;
7831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007833 final boolean animating = atoken != null
7834 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007836 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7837 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007838 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 }
7840
7841 /** Is the window or its container currently animating? */
7842 boolean isAnimating() {
7843 final WindowState attached = mAttachedWindow;
7844 final AppWindowToken atoken = mAppToken;
7845 return mAnimation != null
7846 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007847 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 (atoken.animation != null
7849 || atoken.inPendingTransaction));
7850 }
7851
7852 /** Is this window currently animating? */
7853 boolean isWindowAnimating() {
7854 return mAnimation != null;
7855 }
7856
7857 /**
7858 * Like isOnScreen, but returns false if the surface hasn't yet
7859 * been drawn.
7860 */
7861 public boolean isDisplayedLw() {
7862 final AppWindowToken atoken = mAppToken;
7863 return mSurface != null && mPolicyVisibility && !mDestroying
7864 && !mDrawPending && !mCommitDrawPending
7865 && ((!mAttachedHidden &&
7866 (atoken == null || !atoken.hiddenRequested))
7867 || mAnimating);
7868 }
7869
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007870 /**
7871 * Returns true if the window has a surface that it has drawn a
7872 * complete UI in to.
7873 */
7874 public boolean isDrawnLw() {
7875 final AppWindowToken atoken = mAppToken;
7876 return mSurface != null && !mDestroying
7877 && !mDrawPending && !mCommitDrawPending;
7878 }
7879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7881 boolean shownFrame, boolean onlyOpaque) {
7882 if (mSurface == null) {
7883 return false;
7884 }
7885 if (mAppToken != null && !mAppToken.appFullscreen) {
7886 return false;
7887 }
7888 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7889 return false;
7890 }
7891 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007892
7893 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7894 return frame.left <= mCompatibleScreenFrame.left &&
7895 frame.top <= mCompatibleScreenFrame.top &&
7896 frame.right >= mCompatibleScreenFrame.right &&
7897 frame.bottom >= mCompatibleScreenFrame.bottom;
7898 } else {
7899 return frame.left <= 0 && frame.top <= 0
7900 && frame.right >= screenWidth
7901 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 }
Romain Guy06882f82009-06-10 13:36:04 -07007904
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007905 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007906 * Return true if the window is opaque and fully drawn. This indicates
7907 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007908 */
7909 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007910 return (mAttrs.format == PixelFormat.OPAQUE
7911 || mAttrs.type == TYPE_WALLPAPER)
7912 && mSurface != null && mAnimation == null
7913 && (mAppToken == null || mAppToken.animation == null)
7914 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007915 }
7916
7917 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7918 return
7919 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007920 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7921 // only if it's visible
7922 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007923 // and only if the application fills the compatible screen
7924 mFrame.left <= mCompatibleScreenFrame.left &&
7925 mFrame.top <= mCompatibleScreenFrame.top &&
7926 mFrame.right >= mCompatibleScreenFrame.right &&
7927 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007928 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007929 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007930 }
7931
7932 boolean isFullscreen(int screenWidth, int screenHeight) {
7933 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007934 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 }
7936
7937 void removeLocked() {
7938 if (mAttachedWindow != null) {
7939 mAttachedWindow.mChildWindows.remove(this);
7940 }
7941 destroySurfaceLocked();
7942 mSession.windowRemovedLocked();
7943 try {
7944 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7945 } catch (RuntimeException e) {
7946 // Ignore if it has already been removed (usually because
7947 // we are doing this as part of processing a death note.)
7948 }
7949 }
7950
7951 private class DeathRecipient implements IBinder.DeathRecipient {
7952 public void binderDied() {
7953 try {
7954 synchronized(mWindowMap) {
7955 WindowState win = windowForClientLocked(mSession, mClient);
7956 Log.i(TAG, "WIN DEATH: " + win);
7957 if (win != null) {
7958 removeWindowLocked(mSession, win);
7959 }
7960 }
7961 } catch (IllegalArgumentException ex) {
7962 // This will happen if the window has already been
7963 // removed.
7964 }
7965 }
7966 }
7967
7968 /** Returns true if this window desires key events. */
7969 public final boolean canReceiveKeys() {
7970 return isVisibleOrAdding()
7971 && (mViewVisibility == View.VISIBLE)
7972 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7973 }
7974
7975 public boolean hasDrawnLw() {
7976 return mHasDrawn;
7977 }
7978
7979 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007980 return showLw(doAnimation, true);
7981 }
7982
7983 boolean showLw(boolean doAnimation, boolean requestAnim) {
7984 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7985 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007987 mPolicyVisibility = true;
7988 mPolicyVisibilityAfterAnim = true;
7989 if (doAnimation) {
7990 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7991 }
7992 if (requestAnim) {
7993 requestAnimationLocked(0);
7994 }
7995 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 }
7997
7998 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007999 return hideLw(doAnimation, true);
8000 }
8001
8002 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008003 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8004 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008005 if (!current) {
8006 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008008 if (doAnimation) {
8009 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8010 if (mAnimation == null) {
8011 doAnimation = false;
8012 }
8013 }
8014 if (doAnimation) {
8015 mPolicyVisibilityAfterAnim = false;
8016 } else {
8017 mPolicyVisibilityAfterAnim = false;
8018 mPolicyVisibility = false;
8019 }
8020 if (requestAnim) {
8021 requestAnimationLocked(0);
8022 }
8023 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 }
8025
8026 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008027 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008028
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008029 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8030 pw.print(" mClient="); pw.println(mClient.asBinder());
8031 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8032 if (mAttachedWindow != null || mLayoutAttached) {
8033 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8034 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8035 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008036 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8037 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8038 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008039 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8040 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008041 }
8042 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8043 pw.print(" mSubLayer="); pw.print(mSubLayer);
8044 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8045 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8046 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8047 pw.print("="); pw.print(mAnimLayer);
8048 pw.print(" mLastLayer="); pw.println(mLastLayer);
8049 if (mSurface != null) {
8050 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8051 }
8052 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8053 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8054 if (mAppToken != null) {
8055 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8056 }
8057 if (mTargetAppToken != null) {
8058 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8059 }
8060 pw.print(prefix); pw.print("mViewVisibility=0x");
8061 pw.print(Integer.toHexString(mViewVisibility));
8062 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008063 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8064 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008065 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8066 pw.print(prefix); pw.print("mPolicyVisibility=");
8067 pw.print(mPolicyVisibility);
8068 pw.print(" mPolicyVisibilityAfterAnim=");
8069 pw.print(mPolicyVisibilityAfterAnim);
8070 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8071 }
8072 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008073 pw.print(" h="); pw.println(mRequestedHeight);
8074 if (mXOffset != 0 || mYOffset != 0) {
8075 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8076 pw.print(" y="); pw.println(mYOffset);
8077 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008078 pw.print(prefix); pw.print("mGivenContentInsets=");
8079 mGivenContentInsets.printShortString(pw);
8080 pw.print(" mGivenVisibleInsets=");
8081 mGivenVisibleInsets.printShortString(pw);
8082 pw.println();
8083 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8084 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8085 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8086 }
8087 pw.print(prefix); pw.print("mShownFrame=");
8088 mShownFrame.printShortString(pw);
8089 pw.print(" last="); mLastShownFrame.printShortString(pw);
8090 pw.println();
8091 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8092 pw.print(" last="); mLastFrame.printShortString(pw);
8093 pw.println();
8094 pw.print(prefix); pw.print("mContainingFrame=");
8095 mContainingFrame.printShortString(pw);
8096 pw.print(" mDisplayFrame=");
8097 mDisplayFrame.printShortString(pw);
8098 pw.println();
8099 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8100 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8101 pw.println();
8102 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8103 pw.print(" last="); mLastContentInsets.printShortString(pw);
8104 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8105 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8106 pw.println();
8107 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8108 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8109 pw.print(" mAlpha="); pw.print(mAlpha);
8110 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8111 }
8112 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8113 || mAnimation != null) {
8114 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8115 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8116 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8117 pw.print(" mAnimation="); pw.println(mAnimation);
8118 }
8119 if (mHasTransformation || mHasLocalTransformation) {
8120 pw.print(prefix); pw.print("XForm: has=");
8121 pw.print(mHasTransformation);
8122 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8123 pw.print(" "); mTransformation.printShortString(pw);
8124 pw.println();
8125 }
8126 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8127 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8128 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8129 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8130 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8131 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8132 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8133 pw.print(" mDestroying="); pw.print(mDestroying);
8134 pw.print(" mRemoved="); pw.println(mRemoved);
8135 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008136 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008137 pw.print(prefix); pw.print("mOrientationChanging=");
8138 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008139 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8140 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008141 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008142 if (mHScale != 1 || mVScale != 1) {
8143 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8144 pw.print(" mVScale="); pw.println(mVScale);
8145 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008146 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008147 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8148 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8149 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008150 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8151 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8152 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 }
8155
8156 @Override
8157 public String toString() {
8158 return "Window{"
8159 + Integer.toHexString(System.identityHashCode(this))
8160 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8161 }
8162 }
Romain Guy06882f82009-06-10 13:36:04 -07008163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164 // -------------------------------------------------------------
8165 // Window Token State
8166 // -------------------------------------------------------------
8167
8168 class WindowToken {
8169 // The actual token.
8170 final IBinder token;
8171
8172 // The type of window this token is for, as per WindowManager.LayoutParams.
8173 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 // Set if this token was explicitly added by a client, so should
8176 // not be removed when all windows are removed.
8177 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008178
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008179 // For printing.
8180 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 // If this is an AppWindowToken, this is non-null.
8183 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008185 // All of the windows associated with this token.
8186 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8187
8188 // Is key dispatching paused for this token?
8189 boolean paused = false;
8190
8191 // Should this token's windows be hidden?
8192 boolean hidden;
8193
8194 // Temporary for finding which tokens no longer have visible windows.
8195 boolean hasVisible;
8196
Dianne Hackborna8f60182009-09-01 19:01:50 -07008197 // Set to true when this token is in a pending transaction where it
8198 // will be shown.
8199 boolean waitingToShow;
8200
8201 // Set to true when this token is in a pending transaction where it
8202 // will be hidden.
8203 boolean waitingToHide;
8204
8205 // Set to true when this token is in a pending transaction where its
8206 // windows will be put to the bottom of the list.
8207 boolean sendingToBottom;
8208
8209 // Set to true when this token is in a pending transaction where its
8210 // windows will be put to the top of the list.
8211 boolean sendingToTop;
8212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 WindowToken(IBinder _token, int type, boolean _explicit) {
8214 token = _token;
8215 windowType = type;
8216 explicit = _explicit;
8217 }
8218
8219 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008220 pw.print(prefix); pw.print("token="); pw.println(token);
8221 pw.print(prefix); pw.print("windows="); pw.println(windows);
8222 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8223 pw.print(" hidden="); pw.print(hidden);
8224 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008225 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8226 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8227 pw.print(" waitingToHide="); pw.print(waitingToHide);
8228 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8229 pw.print(" sendingToTop="); pw.println(sendingToTop);
8230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 }
8232
8233 @Override
8234 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008235 if (stringName == null) {
8236 StringBuilder sb = new StringBuilder();
8237 sb.append("WindowToken{");
8238 sb.append(Integer.toHexString(System.identityHashCode(this)));
8239 sb.append(" token="); sb.append(token); sb.append('}');
8240 stringName = sb.toString();
8241 }
8242 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 }
8244 };
8245
8246 class AppWindowToken extends WindowToken {
8247 // Non-null only for application tokens.
8248 final IApplicationToken appToken;
8249
8250 // All of the windows and child windows that are included in this
8251 // application token. Note this list is NOT sorted!
8252 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8253
8254 int groupId = -1;
8255 boolean appFullscreen;
8256 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 // These are used for determining when all windows associated with
8259 // an activity have been drawn, so they can be made visible together
8260 // at the same time.
8261 int lastTransactionSequence = mTransactionSequence-1;
8262 int numInterestingWindows;
8263 int numDrawnWindows;
8264 boolean inPendingTransaction;
8265 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 // Is this token going to be hidden in a little while? If so, it
8268 // won't be taken into account for setting the screen orientation.
8269 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008271 // Is this window's surface needed? This is almost like hidden, except
8272 // it will sometimes be true a little earlier: when the token has
8273 // been shown, but is still waiting for its app transition to execute
8274 // before making its windows shown.
8275 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008277 // Have we told the window clients to hide themselves?
8278 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 // Last visibility state we reported to the app token.
8281 boolean reportedVisible;
8282
8283 // Set to true when the token has been removed from the window mgr.
8284 boolean removed;
8285
8286 // Have we been asked to have this token keep the screen frozen?
8287 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008289 boolean animating;
8290 Animation animation;
8291 boolean hasTransformation;
8292 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 // Offset to the window of all layers in the token, for use by
8295 // AppWindowToken animations.
8296 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 // Information about an application starting window if displayed.
8299 StartingData startingData;
8300 WindowState startingWindow;
8301 View startingView;
8302 boolean startingDisplayed;
8303 boolean startingMoved;
8304 boolean firstWindowDrawn;
8305
8306 AppWindowToken(IApplicationToken _token) {
8307 super(_token.asBinder(),
8308 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8309 appWindowToken = this;
8310 appToken = _token;
8311 }
Romain Guy06882f82009-06-10 13:36:04 -07008312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008313 public void setAnimation(Animation anim) {
8314 if (localLOGV) Log.v(
8315 TAG, "Setting animation in " + this + ": " + anim);
8316 animation = anim;
8317 animating = false;
8318 anim.restrictDuration(MAX_ANIMATION_DURATION);
8319 anim.scaleCurrentDuration(mTransitionAnimationScale);
8320 int zorder = anim.getZAdjustment();
8321 int adj = 0;
8322 if (zorder == Animation.ZORDER_TOP) {
8323 adj = TYPE_LAYER_OFFSET;
8324 } else if (zorder == Animation.ZORDER_BOTTOM) {
8325 adj = -TYPE_LAYER_OFFSET;
8326 }
Romain Guy06882f82009-06-10 13:36:04 -07008327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 if (animLayerAdjustment != adj) {
8329 animLayerAdjustment = adj;
8330 updateLayers();
8331 }
8332 }
Romain Guy06882f82009-06-10 13:36:04 -07008333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008334 public void setDummyAnimation() {
8335 if (animation == null) {
8336 if (localLOGV) Log.v(
8337 TAG, "Setting dummy animation in " + this);
8338 animation = sDummyAnimation;
8339 }
8340 }
8341
8342 public void clearAnimation() {
8343 if (animation != null) {
8344 animation = null;
8345 animating = true;
8346 }
8347 }
Romain Guy06882f82009-06-10 13:36:04 -07008348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349 void updateLayers() {
8350 final int N = allAppWindows.size();
8351 final int adj = animLayerAdjustment;
8352 for (int i=0; i<N; i++) {
8353 WindowState w = allAppWindows.get(i);
8354 w.mAnimLayer = w.mLayer + adj;
8355 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8356 + w.mAnimLayer);
8357 if (w == mInputMethodTarget) {
8358 setInputMethodAnimLayerAdjustment(adj);
8359 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008360 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008361 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 }
8364 }
Romain Guy06882f82009-06-10 13:36:04 -07008365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 void sendAppVisibilityToClients() {
8367 final int N = allAppWindows.size();
8368 for (int i=0; i<N; i++) {
8369 WindowState win = allAppWindows.get(i);
8370 if (win == startingWindow && clientHidden) {
8371 // Don't hide the starting window.
8372 continue;
8373 }
8374 try {
8375 if (DEBUG_VISIBILITY) Log.v(TAG,
8376 "Setting visibility of " + win + ": " + (!clientHidden));
8377 win.mClient.dispatchAppVisibility(!clientHidden);
8378 } catch (RemoteException e) {
8379 }
8380 }
8381 }
Romain Guy06882f82009-06-10 13:36:04 -07008382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008383 void showAllWindowsLocked() {
8384 final int NW = allAppWindows.size();
8385 for (int i=0; i<NW; i++) {
8386 WindowState w = allAppWindows.get(i);
8387 if (DEBUG_VISIBILITY) Log.v(TAG,
8388 "performing show on: " + w);
8389 w.performShowLocked();
8390 }
8391 }
Romain Guy06882f82009-06-10 13:36:04 -07008392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008393 // This must be called while inside a transaction.
8394 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8395 if (!mDisplayFrozen) {
8396 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 if (animation == sDummyAnimation) {
8399 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008400 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 // when it is really time to animate, this will be set to
8402 // a real animation and the next call will execute normally.
8403 return false;
8404 }
Romain Guy06882f82009-06-10 13:36:04 -07008405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008406 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8407 if (!animating) {
8408 if (DEBUG_ANIM) Log.v(
8409 TAG, "Starting animation in " + this +
8410 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8411 + " scale=" + mTransitionAnimationScale
8412 + " allDrawn=" + allDrawn + " animating=" + animating);
8413 animation.initialize(dw, dh, dw, dh);
8414 animation.setStartTime(currentTime);
8415 animating = true;
8416 }
8417 transformation.clear();
8418 final boolean more = animation.getTransformation(
8419 currentTime, transformation);
8420 if (DEBUG_ANIM) Log.v(
8421 TAG, "Stepped animation in " + this +
8422 ": more=" + more + ", xform=" + transformation);
8423 if (more) {
8424 // we're done!
8425 hasTransformation = true;
8426 return true;
8427 }
8428 if (DEBUG_ANIM) Log.v(
8429 TAG, "Finished animation in " + this +
8430 " @ " + currentTime);
8431 animation = null;
8432 }
8433 } else if (animation != null) {
8434 // If the display is frozen, and there is a pending animation,
8435 // clear it and make sure we run the cleanup code.
8436 animating = true;
8437 animation = null;
8438 }
8439
8440 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 if (!animating) {
8443 return false;
8444 }
8445
8446 clearAnimation();
8447 animating = false;
8448 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8449 moveInputMethodWindowsIfNeededLocked(true);
8450 }
Romain Guy06882f82009-06-10 13:36:04 -07008451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 if (DEBUG_ANIM) Log.v(
8453 TAG, "Animation done in " + this
8454 + ": reportedVisible=" + reportedVisible);
8455
8456 transformation.clear();
8457 if (animLayerAdjustment != 0) {
8458 animLayerAdjustment = 0;
8459 updateLayers();
8460 }
Romain Guy06882f82009-06-10 13:36:04 -07008461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008462 final int N = windows.size();
8463 for (int i=0; i<N; i++) {
8464 ((WindowState)windows.get(i)).finishExit();
8465 }
8466 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 return false;
8469 }
8470
8471 void updateReportedVisibilityLocked() {
8472 if (appToken == null) {
8473 return;
8474 }
Romain Guy06882f82009-06-10 13:36:04 -07008475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 int numInteresting = 0;
8477 int numVisible = 0;
8478 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8481 final int N = allAppWindows.size();
8482 for (int i=0; i<N; i++) {
8483 WindowState win = allAppWindows.get(i);
8484 if (win == startingWindow || win.mAppFreezing) {
8485 continue;
8486 }
8487 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008488 Log.v(TAG, "Win " + win + ": isDrawn="
8489 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008491 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 Log.v(TAG, "Not displayed: s=" + win.mSurface
8493 + " pv=" + win.mPolicyVisibility
8494 + " dp=" + win.mDrawPending
8495 + " cdp=" + win.mCommitDrawPending
8496 + " ah=" + win.mAttachedHidden
8497 + " th="
8498 + (win.mAppToken != null
8499 ? win.mAppToken.hiddenRequested : false)
8500 + " a=" + win.mAnimating);
8501 }
8502 }
8503 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008504 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 if (!win.isAnimating()) {
8506 numVisible++;
8507 }
8508 nowGone = false;
8509 } else if (win.isAnimating()) {
8510 nowGone = false;
8511 }
8512 }
Romain Guy06882f82009-06-10 13:36:04 -07008513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8515 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8516 + numInteresting + " visible=" + numVisible);
8517 if (nowVisible != reportedVisible) {
8518 if (DEBUG_VISIBILITY) Log.v(
8519 TAG, "Visibility changed in " + this
8520 + ": vis=" + nowVisible);
8521 reportedVisible = nowVisible;
8522 Message m = mH.obtainMessage(
8523 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8524 nowVisible ? 1 : 0,
8525 nowGone ? 1 : 0,
8526 this);
8527 mH.sendMessage(m);
8528 }
8529 }
Romain Guy06882f82009-06-10 13:36:04 -07008530
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008531 WindowState findMainWindow() {
8532 int j = windows.size();
8533 while (j > 0) {
8534 j--;
8535 WindowState win = windows.get(j);
8536 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8537 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8538 return win;
8539 }
8540 }
8541 return null;
8542 }
8543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008544 void dump(PrintWriter pw, String prefix) {
8545 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008546 if (appToken != null) {
8547 pw.print(prefix); pw.println("app=true");
8548 }
8549 if (allAppWindows.size() > 0) {
8550 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8551 }
8552 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008553 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008554 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8555 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8556 pw.print(" clientHidden="); pw.print(clientHidden);
8557 pw.print(" willBeHidden="); pw.print(willBeHidden);
8558 pw.print(" reportedVisible="); pw.println(reportedVisible);
8559 if (paused || freezingScreen) {
8560 pw.print(prefix); pw.print("paused="); pw.print(paused);
8561 pw.print(" freezingScreen="); pw.println(freezingScreen);
8562 }
8563 if (numInterestingWindows != 0 || numDrawnWindows != 0
8564 || inPendingTransaction || allDrawn) {
8565 pw.print(prefix); pw.print("numInterestingWindows=");
8566 pw.print(numInterestingWindows);
8567 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8568 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8569 pw.print(" allDrawn="); pw.println(allDrawn);
8570 }
8571 if (animating || animation != null) {
8572 pw.print(prefix); pw.print("animating="); pw.print(animating);
8573 pw.print(" animation="); pw.println(animation);
8574 }
8575 if (animLayerAdjustment != 0) {
8576 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8577 }
8578 if (hasTransformation) {
8579 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8580 pw.print(" transformation="); transformation.printShortString(pw);
8581 pw.println();
8582 }
8583 if (startingData != null || removed || firstWindowDrawn) {
8584 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8585 pw.print(" removed="); pw.print(removed);
8586 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8587 }
8588 if (startingWindow != null || startingView != null
8589 || startingDisplayed || startingMoved) {
8590 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8591 pw.print(" startingView="); pw.print(startingView);
8592 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8593 pw.print(" startingMoved"); pw.println(startingMoved);
8594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008595 }
8596
8597 @Override
8598 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008599 if (stringName == null) {
8600 StringBuilder sb = new StringBuilder();
8601 sb.append("AppWindowToken{");
8602 sb.append(Integer.toHexString(System.identityHashCode(this)));
8603 sb.append(" token="); sb.append(token); sb.append('}');
8604 stringName = sb.toString();
8605 }
8606 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 }
8608 }
Romain Guy06882f82009-06-10 13:36:04 -07008609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610 // -------------------------------------------------------------
8611 // DummyAnimation
8612 // -------------------------------------------------------------
8613
8614 // This is an animation that does nothing: it just immediately finishes
8615 // itself every time it is called. It is used as a stub animation in cases
8616 // where we want to synchronize multiple things that may be animating.
8617 static final class DummyAnimation extends Animation {
8618 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8619 return false;
8620 }
8621 }
8622 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 // -------------------------------------------------------------
8625 // Async Handler
8626 // -------------------------------------------------------------
8627
8628 static final class StartingData {
8629 final String pkg;
8630 final int theme;
8631 final CharSequence nonLocalizedLabel;
8632 final int labelRes;
8633 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8636 int _labelRes, int _icon) {
8637 pkg = _pkg;
8638 theme = _theme;
8639 nonLocalizedLabel = _nonLocalizedLabel;
8640 labelRes = _labelRes;
8641 icon = _icon;
8642 }
8643 }
8644
8645 private final class H extends Handler {
8646 public static final int REPORT_FOCUS_CHANGE = 2;
8647 public static final int REPORT_LOSING_FOCUS = 3;
8648 public static final int ANIMATE = 4;
8649 public static final int ADD_STARTING = 5;
8650 public static final int REMOVE_STARTING = 6;
8651 public static final int FINISHED_STARTING = 7;
8652 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8654 public static final int HOLD_SCREEN_CHANGED = 12;
8655 public static final int APP_TRANSITION_TIMEOUT = 13;
8656 public static final int PERSIST_ANIMATION_SCALE = 14;
8657 public static final int FORCE_GC = 15;
8658 public static final int ENABLE_SCREEN = 16;
8659 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008660 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008662 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 public H() {
8665 }
Romain Guy06882f82009-06-10 13:36:04 -07008666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667 @Override
8668 public void handleMessage(Message msg) {
8669 switch (msg.what) {
8670 case REPORT_FOCUS_CHANGE: {
8671 WindowState lastFocus;
8672 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 synchronized(mWindowMap) {
8675 lastFocus = mLastFocus;
8676 newFocus = mCurrentFocus;
8677 if (lastFocus == newFocus) {
8678 // Focus is not changing, so nothing to do.
8679 return;
8680 }
8681 mLastFocus = newFocus;
8682 //Log.i(TAG, "Focus moving from " + lastFocus
8683 // + " to " + newFocus);
8684 if (newFocus != null && lastFocus != null
8685 && !newFocus.isDisplayedLw()) {
8686 //Log.i(TAG, "Delaying loss of focus...");
8687 mLosingFocus.add(lastFocus);
8688 lastFocus = null;
8689 }
8690 }
8691
8692 if (lastFocus != newFocus) {
8693 //System.out.println("Changing focus from " + lastFocus
8694 // + " to " + newFocus);
8695 if (newFocus != null) {
8696 try {
8697 //Log.i(TAG, "Gaining focus: " + newFocus);
8698 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8699 } catch (RemoteException e) {
8700 // Ignore if process has died.
8701 }
8702 }
8703
8704 if (lastFocus != null) {
8705 try {
8706 //Log.i(TAG, "Losing focus: " + lastFocus);
8707 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8708 } catch (RemoteException e) {
8709 // Ignore if process has died.
8710 }
8711 }
8712 }
8713 } break;
8714
8715 case REPORT_LOSING_FOCUS: {
8716 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008718 synchronized(mWindowMap) {
8719 losers = mLosingFocus;
8720 mLosingFocus = new ArrayList<WindowState>();
8721 }
8722
8723 final int N = losers.size();
8724 for (int i=0; i<N; i++) {
8725 try {
8726 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8727 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8728 } catch (RemoteException e) {
8729 // Ignore if process has died.
8730 }
8731 }
8732 } break;
8733
8734 case ANIMATE: {
8735 synchronized(mWindowMap) {
8736 mAnimationPending = false;
8737 performLayoutAndPlaceSurfacesLocked();
8738 }
8739 } break;
8740
8741 case ADD_STARTING: {
8742 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8743 final StartingData sd = wtoken.startingData;
8744
8745 if (sd == null) {
8746 // Animation has been canceled... do nothing.
8747 return;
8748 }
Romain Guy06882f82009-06-10 13:36:04 -07008749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8751 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 View view = null;
8754 try {
8755 view = mPolicy.addStartingWindow(
8756 wtoken.token, sd.pkg,
8757 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8758 sd.icon);
8759 } catch (Exception e) {
8760 Log.w(TAG, "Exception when adding starting window", e);
8761 }
8762
8763 if (view != null) {
8764 boolean abort = false;
8765
8766 synchronized(mWindowMap) {
8767 if (wtoken.removed || wtoken.startingData == null) {
8768 // If the window was successfully added, then
8769 // we need to remove it.
8770 if (wtoken.startingWindow != null) {
8771 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8772 "Aborted starting " + wtoken
8773 + ": removed=" + wtoken.removed
8774 + " startingData=" + wtoken.startingData);
8775 wtoken.startingWindow = null;
8776 wtoken.startingData = null;
8777 abort = true;
8778 }
8779 } else {
8780 wtoken.startingView = view;
8781 }
8782 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8783 "Added starting " + wtoken
8784 + ": startingWindow="
8785 + wtoken.startingWindow + " startingView="
8786 + wtoken.startingView);
8787 }
8788
8789 if (abort) {
8790 try {
8791 mPolicy.removeStartingWindow(wtoken.token, view);
8792 } catch (Exception e) {
8793 Log.w(TAG, "Exception when removing starting window", e);
8794 }
8795 }
8796 }
8797 } break;
8798
8799 case REMOVE_STARTING: {
8800 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8801 IBinder token = null;
8802 View view = null;
8803 synchronized (mWindowMap) {
8804 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8805 + wtoken + ": startingWindow="
8806 + wtoken.startingWindow + " startingView="
8807 + wtoken.startingView);
8808 if (wtoken.startingWindow != null) {
8809 view = wtoken.startingView;
8810 token = wtoken.token;
8811 wtoken.startingData = null;
8812 wtoken.startingView = null;
8813 wtoken.startingWindow = null;
8814 }
8815 }
8816 if (view != null) {
8817 try {
8818 mPolicy.removeStartingWindow(token, view);
8819 } catch (Exception e) {
8820 Log.w(TAG, "Exception when removing starting window", e);
8821 }
8822 }
8823 } break;
8824
8825 case FINISHED_STARTING: {
8826 IBinder token = null;
8827 View view = null;
8828 while (true) {
8829 synchronized (mWindowMap) {
8830 final int N = mFinishedStarting.size();
8831 if (N <= 0) {
8832 break;
8833 }
8834 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8835
8836 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8837 "Finished starting " + wtoken
8838 + ": startingWindow=" + wtoken.startingWindow
8839 + " startingView=" + wtoken.startingView);
8840
8841 if (wtoken.startingWindow == null) {
8842 continue;
8843 }
8844
8845 view = wtoken.startingView;
8846 token = wtoken.token;
8847 wtoken.startingData = null;
8848 wtoken.startingView = null;
8849 wtoken.startingWindow = null;
8850 }
8851
8852 try {
8853 mPolicy.removeStartingWindow(token, view);
8854 } catch (Exception e) {
8855 Log.w(TAG, "Exception when removing starting window", e);
8856 }
8857 }
8858 } break;
8859
8860 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8861 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8862
8863 boolean nowVisible = msg.arg1 != 0;
8864 boolean nowGone = msg.arg2 != 0;
8865
8866 try {
8867 if (DEBUG_VISIBILITY) Log.v(
8868 TAG, "Reporting visible in " + wtoken
8869 + " visible=" + nowVisible
8870 + " gone=" + nowGone);
8871 if (nowVisible) {
8872 wtoken.appToken.windowsVisible();
8873 } else {
8874 wtoken.appToken.windowsGone();
8875 }
8876 } catch (RemoteException ex) {
8877 }
8878 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 case WINDOW_FREEZE_TIMEOUT: {
8881 synchronized (mWindowMap) {
8882 Log.w(TAG, "Window freeze timeout expired.");
8883 int i = mWindows.size();
8884 while (i > 0) {
8885 i--;
8886 WindowState w = (WindowState)mWindows.get(i);
8887 if (w.mOrientationChanging) {
8888 w.mOrientationChanging = false;
8889 Log.w(TAG, "Force clearing orientation change: " + w);
8890 }
8891 }
8892 performLayoutAndPlaceSurfacesLocked();
8893 }
8894 break;
8895 }
Romain Guy06882f82009-06-10 13:36:04 -07008896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008897 case HOLD_SCREEN_CHANGED: {
8898 Session oldHold;
8899 Session newHold;
8900 synchronized (mWindowMap) {
8901 oldHold = mLastReportedHold;
8902 newHold = (Session)msg.obj;
8903 mLastReportedHold = newHold;
8904 }
Romain Guy06882f82009-06-10 13:36:04 -07008905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 if (oldHold != newHold) {
8907 try {
8908 if (oldHold != null) {
8909 mBatteryStats.noteStopWakelock(oldHold.mUid,
8910 "window",
8911 BatteryStats.WAKE_TYPE_WINDOW);
8912 }
8913 if (newHold != null) {
8914 mBatteryStats.noteStartWakelock(newHold.mUid,
8915 "window",
8916 BatteryStats.WAKE_TYPE_WINDOW);
8917 }
8918 } catch (RemoteException e) {
8919 }
8920 }
8921 break;
8922 }
Romain Guy06882f82009-06-10 13:36:04 -07008923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008924 case APP_TRANSITION_TIMEOUT: {
8925 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008926 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8928 "*** APP TRANSITION TIMEOUT");
8929 mAppTransitionReady = true;
8930 mAppTransitionTimeout = true;
8931 performLayoutAndPlaceSurfacesLocked();
8932 }
8933 }
8934 break;
8935 }
Romain Guy06882f82009-06-10 13:36:04 -07008936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 case PERSIST_ANIMATION_SCALE: {
8938 Settings.System.putFloat(mContext.getContentResolver(),
8939 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8940 Settings.System.putFloat(mContext.getContentResolver(),
8941 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8942 break;
8943 }
Romain Guy06882f82009-06-10 13:36:04 -07008944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008945 case FORCE_GC: {
8946 synchronized(mWindowMap) {
8947 if (mAnimationPending) {
8948 // If we are animating, don't do the gc now but
8949 // delay a bit so we don't interrupt the animation.
8950 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8951 2000);
8952 return;
8953 }
8954 // If we are currently rotating the display, it will
8955 // schedule a new message when done.
8956 if (mDisplayFrozen) {
8957 return;
8958 }
8959 mFreezeGcPending = 0;
8960 }
8961 Runtime.getRuntime().gc();
8962 break;
8963 }
Romain Guy06882f82009-06-10 13:36:04 -07008964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 case ENABLE_SCREEN: {
8966 performEnableScreen();
8967 break;
8968 }
Romain Guy06882f82009-06-10 13:36:04 -07008969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008970 case APP_FREEZE_TIMEOUT: {
8971 synchronized (mWindowMap) {
8972 Log.w(TAG, "App freeze timeout expired.");
8973 int i = mAppTokens.size();
8974 while (i > 0) {
8975 i--;
8976 AppWindowToken tok = mAppTokens.get(i);
8977 if (tok.freezingScreen) {
8978 Log.w(TAG, "Force clearing freeze: " + tok);
8979 unsetAppFreezingScreenLocked(tok, true, true);
8980 }
8981 }
8982 }
8983 break;
8984 }
Romain Guy06882f82009-06-10 13:36:04 -07008985
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008986 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008987 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008988 sendNewConfiguration();
8989 }
8990 break;
8991 }
Romain Guy06882f82009-06-10 13:36:04 -07008992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008993 }
8994 }
8995 }
8996
8997 // -------------------------------------------------------------
8998 // IWindowManager API
8999 // -------------------------------------------------------------
9000
9001 public IWindowSession openSession(IInputMethodClient client,
9002 IInputContext inputContext) {
9003 if (client == null) throw new IllegalArgumentException("null client");
9004 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9005 return new Session(client, inputContext);
9006 }
9007
9008 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9009 synchronized (mWindowMap) {
9010 // The focus for the client is the window immediately below
9011 // where we would place the input method window.
9012 int idx = findDesiredInputMethodWindowIndexLocked(false);
9013 WindowState imFocus;
9014 if (idx > 0) {
9015 imFocus = (WindowState)mWindows.get(idx-1);
9016 if (imFocus != null) {
9017 if (imFocus.mSession.mClient != null &&
9018 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9019 return true;
9020 }
9021 }
9022 }
9023 }
9024 return false;
9025 }
Romain Guy06882f82009-06-10 13:36:04 -07009026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 // -------------------------------------------------------------
9028 // Internals
9029 // -------------------------------------------------------------
9030
9031 final WindowState windowForClientLocked(Session session, IWindow client) {
9032 return windowForClientLocked(session, client.asBinder());
9033 }
Romain Guy06882f82009-06-10 13:36:04 -07009034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009035 final WindowState windowForClientLocked(Session session, IBinder client) {
9036 WindowState win = mWindowMap.get(client);
9037 if (localLOGV) Log.v(
9038 TAG, "Looking up client " + client + ": " + win);
9039 if (win == null) {
9040 RuntimeException ex = new RuntimeException();
9041 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9042 return null;
9043 }
9044 if (session != null && win.mSession != session) {
9045 RuntimeException ex = new RuntimeException();
9046 Log.w(TAG, "Requested window " + client + " is in session " +
9047 win.mSession + ", not " + session, ex);
9048 return null;
9049 }
9050
9051 return win;
9052 }
9053
Dianne Hackborna8f60182009-09-01 19:01:50 -07009054 final void rebuildAppWindowListLocked() {
9055 int NW = mWindows.size();
9056 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009057 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009058 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009059
9060 // First remove all existing app windows.
9061 i=0;
9062 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009063 WindowState w = (WindowState)mWindows.get(i);
9064 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009065 WindowState win = (WindowState)mWindows.remove(i);
9066 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9067 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009068 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009069 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009070 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009071 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9072 && lastWallpaper == i-1) {
9073 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009074 }
9075 i++;
9076 }
9077
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009078 // The wallpaper window(s) typically live at the bottom of the stack,
9079 // so skip them before adding app tokens.
9080 lastWallpaper++;
9081 i = lastWallpaper;
9082
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009083 // First add all of the exiting app tokens... these are no longer
9084 // in the main app list, but still have windows shown. We put them
9085 // in the back because now that the animation is over we no longer
9086 // will care about them.
9087 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009088 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009089 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9090 }
9091
9092 // And add in the still active app tokens in Z order.
9093 NT = mAppTokens.size();
9094 for (int j=0; j<NT; j++) {
9095 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009096 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009097
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009098 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009099 if (i != numRemoved) {
9100 Log.w(TAG, "Rebuild removed " + numRemoved
9101 + " windows but added " + i);
9102 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009103 }
9104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 private final void assignLayersLocked() {
9106 int N = mWindows.size();
9107 int curBaseLayer = 0;
9108 int curLayer = 0;
9109 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 for (i=0; i<N; i++) {
9112 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009113 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9114 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 curLayer += WINDOW_LAYER_MULTIPLIER;
9116 w.mLayer = curLayer;
9117 } else {
9118 curBaseLayer = curLayer = w.mBaseLayer;
9119 w.mLayer = curLayer;
9120 }
9121 if (w.mTargetAppToken != null) {
9122 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9123 } else if (w.mAppToken != null) {
9124 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9125 } else {
9126 w.mAnimLayer = w.mLayer;
9127 }
9128 if (w.mIsImWindow) {
9129 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009130 } else if (w.mIsWallpaper) {
9131 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009132 }
9133 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9134 + w.mAnimLayer);
9135 //System.out.println(
9136 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9137 }
9138 }
9139
9140 private boolean mInLayout = false;
9141 private final void performLayoutAndPlaceSurfacesLocked() {
9142 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009143 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009144 throw new RuntimeException("Recursive call!");
9145 }
9146 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9147 return;
9148 }
9149
9150 boolean recoveringMemory = false;
9151 if (mForceRemoves != null) {
9152 recoveringMemory = true;
9153 // Wait a little it for things to settle down, and off we go.
9154 for (int i=0; i<mForceRemoves.size(); i++) {
9155 WindowState ws = mForceRemoves.get(i);
9156 Log.i(TAG, "Force removing: " + ws);
9157 removeWindowInnerLocked(ws.mSession, ws);
9158 }
9159 mForceRemoves = null;
9160 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9161 Object tmp = new Object();
9162 synchronized (tmp) {
9163 try {
9164 tmp.wait(250);
9165 } catch (InterruptedException e) {
9166 }
9167 }
9168 }
Romain Guy06882f82009-06-10 13:36:04 -07009169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 mInLayout = true;
9171 try {
9172 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 int i = mPendingRemove.size()-1;
9175 if (i >= 0) {
9176 while (i >= 0) {
9177 WindowState w = mPendingRemove.get(i);
9178 removeWindowInnerLocked(w.mSession, w);
9179 i--;
9180 }
9181 mPendingRemove.clear();
9182
9183 mInLayout = false;
9184 assignLayersLocked();
9185 mLayoutNeeded = true;
9186 performLayoutAndPlaceSurfacesLocked();
9187
9188 } else {
9189 mInLayout = false;
9190 if (mLayoutNeeded) {
9191 requestAnimationLocked(0);
9192 }
9193 }
9194 } catch (RuntimeException e) {
9195 mInLayout = false;
9196 Log.e(TAG, "Unhandled exception while layout out windows", e);
9197 }
9198 }
9199
9200 private final void performLayoutLockedInner() {
9201 final int dw = mDisplay.getWidth();
9202 final int dh = mDisplay.getHeight();
9203
9204 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009205 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 int i;
9207
9208 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009209
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009210 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009211 mPolicy.beginLayoutLw(dw, dh);
9212
9213 // First perform layout of any root windows (not attached
9214 // to another window).
9215 int topAttached = -1;
9216 for (i = N-1; i >= 0; i--) {
9217 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009218
9219 // Don't do layout of a window if it is not visible, or
9220 // soon won't be visible, to avoid wasting time and funky
9221 // changes while a window is animating away.
9222 final AppWindowToken atoken = win.mAppToken;
9223 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009224 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009225 || win.mRootToken.hidden
9226 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009227 || win.mAttachedHidden
9228 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009229
9230 // If this view is GONE, then skip it -- keep the current
9231 // frame, and let the caller know so they can ignore it
9232 // if they want. (We do the normal layout for INVISIBLE
9233 // windows, since that means "perform layout as normal,
9234 // just don't display").
9235 if (!gone || !win.mHaveFrame) {
9236 if (!win.mLayoutAttached) {
9237 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9238 } else {
9239 if (topAttached < 0) topAttached = i;
9240 }
9241 }
9242 }
Romain Guy06882f82009-06-10 13:36:04 -07009243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009244 // Now perform layout of attached windows, which usually
9245 // depend on the position of the window they are attached to.
9246 // XXX does not deal with windows that are attached to windows
9247 // that are themselves attached.
9248 for (i = topAttached; i >= 0; i--) {
9249 WindowState win = (WindowState) mWindows.get(i);
9250
9251 // If this view is GONE, then skip it -- keep the current
9252 // frame, and let the caller know so they can ignore it
9253 // if they want. (We do the normal layout for INVISIBLE
9254 // windows, since that means "perform layout as normal,
9255 // just don't display").
9256 if (win.mLayoutAttached) {
9257 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9258 || !win.mHaveFrame) {
9259 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9260 }
9261 }
9262 }
9263
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009264 int changes = mPolicy.finishLayoutLw();
9265 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9266 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9267 assignLayersLocked();
9268 }
9269 }
9270 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009271 mLayoutNeeded = false;
9272 } else if (repeats > 2) {
9273 Log.w(TAG, "Layout repeat aborted after too many iterations");
9274 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009275 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9276 Configuration newConfig = updateOrientationFromAppTokensLocked(
9277 null, null);
9278 if (newConfig != null) {
9279 mLayoutNeeded = true;
9280 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9281 }
9282 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009283 } else {
9284 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009285 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9286 Configuration newConfig = updateOrientationFromAppTokensLocked(
9287 null, null);
9288 if (newConfig != null) {
9289 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9290 }
9291 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 }
9294 }
Romain Guy06882f82009-06-10 13:36:04 -07009295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 private final void performLayoutAndPlaceSurfacesLockedInner(
9297 boolean recoveringMemory) {
9298 final long currentTime = SystemClock.uptimeMillis();
9299 final int dw = mDisplay.getWidth();
9300 final int dh = mDisplay.getHeight();
9301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009302 int i;
9303
9304 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 if (mFxSession == null) {
9308 mFxSession = new SurfaceSession();
9309 }
Romain Guy06882f82009-06-10 13:36:04 -07009310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9312
9313 // Initialize state of exiting tokens.
9314 for (i=mExitingTokens.size()-1; i>=0; i--) {
9315 mExitingTokens.get(i).hasVisible = false;
9316 }
9317
9318 // Initialize state of exiting applications.
9319 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9320 mExitingAppTokens.get(i).hasVisible = false;
9321 }
9322
9323 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009324 boolean orientationChangeComplete = true;
9325 Session holdScreen = null;
9326 float screenBrightness = -1;
9327 boolean focusDisplayed = false;
9328 boolean animating = false;
9329
9330 Surface.openTransaction();
9331 try {
9332 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009333 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009334
9335 do {
9336 final int transactionSequence = ++mTransactionSequence;
9337
9338 // Update animations of all applications, including those
9339 // associated with exiting/removed apps
9340 boolean tokensAnimating = false;
9341 final int NAT = mAppTokens.size();
9342 for (i=0; i<NAT; i++) {
9343 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9344 tokensAnimating = true;
9345 }
9346 }
9347 final int NEAT = mExitingAppTokens.size();
9348 for (i=0; i<NEAT; i++) {
9349 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9350 tokensAnimating = true;
9351 }
9352 }
9353
9354 animating = tokensAnimating;
9355 restart = false;
9356
9357 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009358 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009359 boolean focusMayChange = false;
9360 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361
9362 mPolicy.beginAnimationLw(dw, dh);
9363
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009364 final int N = mWindows.size();
9365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 for (i=N-1; i>=0; i--) {
9367 WindowState w = (WindowState)mWindows.get(i);
9368
9369 final WindowManager.LayoutParams attrs = w.mAttrs;
9370
9371 if (w.mSurface != null) {
9372 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009373 if (w.commitFinishDrawingLocked(currentTime)) {
9374 if ((w.mAttrs.flags
9375 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009376 if (DEBUG_WALLPAPER) Log.v(TAG,
9377 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009378 wallpaperMayChange = true;
9379 }
9380 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009381
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009382 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9384 animating = true;
9385 //w.dump(" ");
9386 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009387 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9388 wallpaperMayChange = true;
9389 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009390
9391 if (mPolicy.doesForceHide(w, attrs)) {
9392 if (!wasAnimating && animating) {
9393 wallpaperForceHidingChanged = true;
9394 focusMayChange = true;
9395 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9396 forceHiding = true;
9397 }
9398 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9399 boolean changed;
9400 if (forceHiding) {
9401 changed = w.hideLw(false, false);
9402 } else {
9403 changed = w.showLw(false, false);
9404 if (changed && wallpaperForceHidingChanged
9405 && w.isReadyForDisplay()) {
9406 // Assume we will need to animate. If
9407 // we don't (because the wallpaper will
9408 // stay with the lock screen), then we will
9409 // clean up later.
9410 Animation a = mPolicy.createForceHideEnterAnimation();
9411 if (a != null) {
9412 w.setAnimation(a);
9413 }
9414 }
9415 }
9416 if (changed && (attrs.flags
9417 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9418 wallpaperMayChange = true;
9419 }
Christopher Tate405d0892009-10-27 20:23:28 -07009420 if (changed && !forceHiding
9421 && (mCurrentFocus == null)
9422 && (mFocusedApp != null)) {
9423 // It's possible that the last focus recalculation left no
9424 // current focused window even though the app has come to the
9425 // foreground already. In this case, we make sure to recalculate
9426 // focus when we show a window.
9427 focusMayChange = true;
9428 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009429 }
9430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 mPolicy.animatingWindowLw(w, attrs);
9432 }
9433
9434 final AppWindowToken atoken = w.mAppToken;
9435 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9436 if (atoken.lastTransactionSequence != transactionSequence) {
9437 atoken.lastTransactionSequence = transactionSequence;
9438 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9439 atoken.startingDisplayed = false;
9440 }
9441 if ((w.isOnScreen() || w.mAttrs.type
9442 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9443 && !w.mExiting && !w.mDestroying) {
9444 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009445 Log.v(TAG, "Eval win " + w + ": isDrawn="
9446 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009448 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009449 Log.v(TAG, "Not displayed: s=" + w.mSurface
9450 + " pv=" + w.mPolicyVisibility
9451 + " dp=" + w.mDrawPending
9452 + " cdp=" + w.mCommitDrawPending
9453 + " ah=" + w.mAttachedHidden
9454 + " th=" + atoken.hiddenRequested
9455 + " a=" + w.mAnimating);
9456 }
9457 }
9458 if (w != atoken.startingWindow) {
9459 if (!atoken.freezingScreen || !w.mAppFreezing) {
9460 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009461 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 atoken.numDrawnWindows++;
9463 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9464 "tokenMayBeDrawn: " + atoken
9465 + " freezingScreen=" + atoken.freezingScreen
9466 + " mAppFreezing=" + w.mAppFreezing);
9467 tokenMayBeDrawn = true;
9468 }
9469 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009470 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 atoken.startingDisplayed = true;
9472 }
9473 }
9474 } else if (w.mReadyToShow) {
9475 w.performShowLocked();
9476 }
9477 }
9478
9479 if (mPolicy.finishAnimationLw()) {
9480 restart = true;
9481 }
9482
9483 if (tokenMayBeDrawn) {
9484 // See if any windows have been drawn, so they (and others
9485 // associated with them) can now be shown.
9486 final int NT = mTokenList.size();
9487 for (i=0; i<NT; i++) {
9488 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9489 if (wtoken == null) {
9490 continue;
9491 }
9492 if (wtoken.freezingScreen) {
9493 int numInteresting = wtoken.numInterestingWindows;
9494 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9495 if (DEBUG_VISIBILITY) Log.v(TAG,
9496 "allDrawn: " + wtoken
9497 + " interesting=" + numInteresting
9498 + " drawn=" + wtoken.numDrawnWindows);
9499 wtoken.showAllWindowsLocked();
9500 unsetAppFreezingScreenLocked(wtoken, false, true);
9501 orientationChangeComplete = true;
9502 }
9503 } else if (!wtoken.allDrawn) {
9504 int numInteresting = wtoken.numInterestingWindows;
9505 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9506 if (DEBUG_VISIBILITY) Log.v(TAG,
9507 "allDrawn: " + wtoken
9508 + " interesting=" + numInteresting
9509 + " drawn=" + wtoken.numDrawnWindows);
9510 wtoken.allDrawn = true;
9511 restart = true;
9512
9513 // We can now show all of the drawn windows!
9514 if (!mOpeningApps.contains(wtoken)) {
9515 wtoken.showAllWindowsLocked();
9516 }
9517 }
9518 }
9519 }
9520 }
9521
9522 // If we are ready to perform an app transition, check through
9523 // all of the app tokens to be shown and see if they are ready
9524 // to go.
9525 if (mAppTransitionReady) {
9526 int NN = mOpeningApps.size();
9527 boolean goodToGo = true;
9528 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9529 "Checking " + NN + " opening apps (frozen="
9530 + mDisplayFrozen + " timeout="
9531 + mAppTransitionTimeout + ")...");
9532 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9533 // If the display isn't frozen, wait to do anything until
9534 // all of the apps are ready. Otherwise just go because
9535 // we'll unfreeze the display when everyone is ready.
9536 for (i=0; i<NN && goodToGo; i++) {
9537 AppWindowToken wtoken = mOpeningApps.get(i);
9538 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9539 "Check opening app" + wtoken + ": allDrawn="
9540 + wtoken.allDrawn + " startingDisplayed="
9541 + wtoken.startingDisplayed);
9542 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9543 && !wtoken.startingMoved) {
9544 goodToGo = false;
9545 }
9546 }
9547 }
9548 if (goodToGo) {
9549 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9550 int transit = mNextAppTransition;
9551 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009552 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009554 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009555 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009556 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 mAppTransitionTimeout = false;
9558 mStartingIconInTransition = false;
9559 mSkipAppTransitionAnimation = false;
9560
9561 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9562
Dianne Hackborna8f60182009-09-01 19:01:50 -07009563 // If there are applications waiting to come to the
9564 // top of the stack, now is the time to move their windows.
9565 // (Note that we don't do apps going to the bottom
9566 // here -- we want to keep their windows in the old
9567 // Z-order until the animation completes.)
9568 if (mToTopApps.size() > 0) {
9569 NN = mAppTokens.size();
9570 for (i=0; i<NN; i++) {
9571 AppWindowToken wtoken = mAppTokens.get(i);
9572 if (wtoken.sendingToTop) {
9573 wtoken.sendingToTop = false;
9574 moveAppWindowsLocked(wtoken, NN, false);
9575 }
9576 }
9577 mToTopApps.clear();
9578 }
9579
Dianne Hackborn25994b42009-09-04 14:21:19 -07009580 WindowState oldWallpaper = mWallpaperTarget;
9581
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009582 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009583 wallpaperMayChange = false;
9584
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009585 // The top-most window will supply the layout params,
9586 // and we will determine it below.
9587 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009588 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009589 int bestAnimLayer = -1;
9590
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009591 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009592 "New wallpaper target=" + mWallpaperTarget
9593 + ", lower target=" + mLowerWallpaperTarget
9594 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009595 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009596 // Do a first pass through the tokens for two
9597 // things:
9598 // (1) Determine if both the closing and opening
9599 // app token sets are wallpaper targets, in which
9600 // case special animations are needed
9601 // (since the wallpaper needs to stay static
9602 // behind them).
9603 // (2) Find the layout params of the top-most
9604 // application window in the tokens, which is
9605 // what will control the animation theme.
9606 final int NC = mClosingApps.size();
9607 NN = NC + mOpeningApps.size();
9608 for (i=0; i<NN; i++) {
9609 AppWindowToken wtoken;
9610 int mode;
9611 if (i < NC) {
9612 wtoken = mClosingApps.get(i);
9613 mode = 1;
9614 } else {
9615 wtoken = mOpeningApps.get(i-NC);
9616 mode = 2;
9617 }
9618 if (mLowerWallpaperTarget != null) {
9619 if (mLowerWallpaperTarget.mAppToken == wtoken
9620 || mUpperWallpaperTarget.mAppToken == wtoken) {
9621 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009622 }
9623 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009624 if (wtoken.appFullscreen) {
9625 WindowState ws = wtoken.findMainWindow();
9626 if (ws != null) {
9627 // If this is a compatibility mode
9628 // window, we will always use its anim.
9629 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9630 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009631 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009632 bestAnimLayer = Integer.MAX_VALUE;
9633 } else if (ws.mLayer > bestAnimLayer) {
9634 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009635 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009636 bestAnimLayer = ws.mLayer;
9637 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009638 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009639 }
9640 }
9641
Dianne Hackborn25994b42009-09-04 14:21:19 -07009642 if (foundWallpapers == 3) {
9643 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9644 "Wallpaper animation!");
9645 switch (transit) {
9646 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9647 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9648 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9649 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9650 break;
9651 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9652 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9653 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9654 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9655 break;
9656 }
9657 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9658 "New transit: " + transit);
9659 } else if (oldWallpaper != null) {
9660 // We are transitioning from an activity with
9661 // a wallpaper to one without.
9662 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9663 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9664 "New transit away from wallpaper: " + transit);
9665 } else if (mWallpaperTarget != null) {
9666 // We are transitioning from an activity without
9667 // a wallpaper to now showing the wallpaper
9668 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9669 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9670 "New transit into wallpaper: " + transit);
9671 }
9672
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009673 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9674 mLastEnterAnimToken = animToken;
9675 mLastEnterAnimParams = animLp;
9676 } else if (mLastEnterAnimParams != null) {
9677 animLp = mLastEnterAnimParams;
9678 mLastEnterAnimToken = null;
9679 mLastEnterAnimParams = null;
9680 }
9681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009682 NN = mOpeningApps.size();
9683 for (i=0; i<NN; i++) {
9684 AppWindowToken wtoken = mOpeningApps.get(i);
9685 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9686 "Now opening app" + wtoken);
9687 wtoken.reportedVisible = false;
9688 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009689 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009690 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009691 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009692 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 wtoken.showAllWindowsLocked();
9694 }
9695 NN = mClosingApps.size();
9696 for (i=0; i<NN; i++) {
9697 AppWindowToken wtoken = mClosingApps.get(i);
9698 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9699 "Now closing app" + wtoken);
9700 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009701 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009702 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009704 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 // Force the allDrawn flag, because we want to start
9706 // this guy's animations regardless of whether it's
9707 // gotten drawn.
9708 wtoken.allDrawn = true;
9709 }
9710
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009711 mNextAppTransitionPackage = null;
9712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009713 mOpeningApps.clear();
9714 mClosingApps.clear();
9715
9716 // This has changed the visibility of windows, so perform
9717 // a new layout to get them all up-to-date.
9718 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009719 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9720 assignLayersLocked();
9721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009722 performLayoutLockedInner();
9723 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009724 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725
9726 restart = true;
9727 }
9728 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009729
Dianne Hackborna8f60182009-09-01 19:01:50 -07009730 if (!animating && mAppTransitionRunning) {
9731 // We have finished the animation of an app transition. To do
9732 // this, we have delayed a lot of operations like showing and
9733 // hiding apps, moving apps in Z-order, etc. The app token list
9734 // reflects the correct Z-order, but the window list may now
9735 // be out of sync with it. So here we will just rebuild the
9736 // entire app window list. Fun!
9737 mAppTransitionRunning = false;
9738 // Clear information about apps that were moving.
9739 mToBottomApps.clear();
9740
9741 rebuildAppWindowListLocked();
9742 restart = true;
9743 moveInputMethodWindowsIfNeededLocked(false);
9744 wallpaperMayChange = true;
9745 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009746 // Since the window list has been rebuilt, focus might
9747 // have to be recomputed since the actual order of windows
9748 // might have changed again.
9749 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009750 }
9751
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009752 int adjResult = 0;
9753
9754 if (wallpaperForceHidingChanged) {
9755 // At this point, there was a window with a wallpaper that
9756 // was force hiding other windows behind it, but now it
9757 // is going away. This may be simple -- just animate
9758 // away the wallpaper and its window -- or it may be
9759 // hard -- the wallpaper now needs to be shown behind
9760 // something that was hidden.
9761 WindowState oldWallpaper = mWallpaperTarget;
9762 adjResult = adjustWallpaperWindowsLocked();
9763 wallpaperMayChange = false;
9764 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9765 + " NEW: " + mWallpaperTarget);
9766 if (mLowerWallpaperTarget == null) {
9767 // Whoops, we don't need a special wallpaper animation.
9768 // Clear them out.
9769 forceHiding = false;
9770 for (i=N-1; i>=0; i--) {
9771 WindowState w = (WindowState)mWindows.get(i);
9772 if (w.mSurface != null) {
9773 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009774 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9775 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009776 forceHiding = true;
9777 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9778 if (!w.mAnimating) {
9779 // We set the animation above so it
9780 // is not yet running.
9781 w.clearAnimation();
9782 }
9783 }
9784 }
9785 }
9786 }
9787 }
9788
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009789 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009790 if (DEBUG_WALLPAPER) Log.v(TAG,
9791 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009792 adjResult = adjustWallpaperWindowsLocked();
9793 }
9794
9795 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9796 if (DEBUG_WALLPAPER) Log.v(TAG,
9797 "Wallpaper layer changed: assigning layers + relayout");
9798 restart = true;
9799 mLayoutNeeded = true;
9800 assignLayersLocked();
9801 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9802 if (DEBUG_WALLPAPER) Log.v(TAG,
9803 "Wallpaper visibility changed: relayout");
9804 restart = true;
9805 mLayoutNeeded = true;
9806 }
9807
9808 if (focusMayChange) {
9809 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009810 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009811 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009812 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009813 }
9814
9815 if (mLayoutNeeded) {
9816 restart = true;
9817 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009818 }
9819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 } while (restart);
9821
9822 // THIRD LOOP: Update the surfaces of all windows.
9823
9824 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9825
9826 boolean obscured = false;
9827 boolean blurring = false;
9828 boolean dimming = false;
9829 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009830 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009831 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009832
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009833 final int N = mWindows.size();
9834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 for (i=N-1; i>=0; i--) {
9836 WindowState w = (WindowState)mWindows.get(i);
9837
9838 boolean displayed = false;
9839 final WindowManager.LayoutParams attrs = w.mAttrs;
9840 final int attrFlags = attrs.flags;
9841
9842 if (w.mSurface != null) {
9843 w.computeShownFrameLocked();
9844 if (localLOGV) Log.v(
9845 TAG, "Placing surface #" + i + " " + w.mSurface
9846 + ": new=" + w.mShownFrame + ", old="
9847 + w.mLastShownFrame);
9848
9849 boolean resize;
9850 int width, height;
9851 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9852 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9853 w.mLastRequestedHeight != w.mRequestedHeight;
9854 // for a scaled surface, we just want to use
9855 // the requested size.
9856 width = w.mRequestedWidth;
9857 height = w.mRequestedHeight;
9858 w.mLastRequestedWidth = width;
9859 w.mLastRequestedHeight = height;
9860 w.mLastShownFrame.set(w.mShownFrame);
9861 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009862 if (SHOW_TRANSACTIONS) Log.i(
9863 TAG, " SURFACE " + w.mSurface
9864 + ": POS " + w.mShownFrame.left
9865 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9867 } catch (RuntimeException e) {
9868 Log.w(TAG, "Error positioning surface in " + w, e);
9869 if (!recoveringMemory) {
9870 reclaimSomeSurfaceMemoryLocked(w, "position");
9871 }
9872 }
9873 } else {
9874 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9875 width = w.mShownFrame.width();
9876 height = w.mShownFrame.height();
9877 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 }
9879
9880 if (resize) {
9881 if (width < 1) width = 1;
9882 if (height < 1) height = 1;
9883 if (w.mSurface != null) {
9884 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009885 if (SHOW_TRANSACTIONS) Log.i(
9886 TAG, " SURFACE " + w.mSurface + ": POS "
9887 + w.mShownFrame.left + ","
9888 + w.mShownFrame.top + " SIZE "
9889 + w.mShownFrame.width() + "x"
9890 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 w.mSurface.setSize(width, height);
9892 w.mSurface.setPosition(w.mShownFrame.left,
9893 w.mShownFrame.top);
9894 } catch (RuntimeException e) {
9895 // If something goes wrong with the surface (such
9896 // as running out of memory), don't take down the
9897 // entire system.
9898 Log.e(TAG, "Failure updating surface of " + w
9899 + "size=(" + width + "x" + height
9900 + "), pos=(" + w.mShownFrame.left
9901 + "," + w.mShownFrame.top + ")", e);
9902 if (!recoveringMemory) {
9903 reclaimSomeSurfaceMemoryLocked(w, "size");
9904 }
9905 }
9906 }
9907 }
9908 if (!w.mAppFreezing) {
9909 w.mContentInsetsChanged =
9910 !w.mLastContentInsets.equals(w.mContentInsets);
9911 w.mVisibleInsetsChanged =
9912 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009913 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009914 || w.mContentInsetsChanged
9915 || w.mVisibleInsetsChanged) {
9916 w.mLastFrame.set(w.mFrame);
9917 w.mLastContentInsets.set(w.mContentInsets);
9918 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009919 // If the screen is currently frozen, then keep
9920 // it frozen until this window draws at its new
9921 // orientation.
9922 if (mDisplayFrozen) {
9923 if (DEBUG_ORIENTATION) Log.v(TAG,
9924 "Resizing while display frozen: " + w);
9925 w.mOrientationChanging = true;
9926 if (mWindowsFreezingScreen) {
9927 mWindowsFreezingScreen = true;
9928 // XXX should probably keep timeout from
9929 // when we first froze the display.
9930 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9931 mH.sendMessageDelayed(mH.obtainMessage(
9932 H.WINDOW_FREEZE_TIMEOUT), 2000);
9933 }
9934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 // If the orientation is changing, then we need to
9936 // hold off on unfreezing the display until this
9937 // window has been redrawn; to do that, we need
9938 // to go through the process of getting informed
9939 // by the application when it has finished drawing.
9940 if (w.mOrientationChanging) {
9941 if (DEBUG_ORIENTATION) Log.v(TAG,
9942 "Orientation start waiting for draw in "
9943 + w + ", surface " + w.mSurface);
9944 w.mDrawPending = true;
9945 w.mCommitDrawPending = false;
9946 w.mReadyToShow = false;
9947 if (w.mAppToken != null) {
9948 w.mAppToken.allDrawn = false;
9949 }
9950 }
Romain Guy06882f82009-06-10 13:36:04 -07009951 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952 "Resizing window " + w + " to " + w.mFrame);
9953 mResizingWindows.add(w);
9954 } else if (w.mOrientationChanging) {
9955 if (!w.mDrawPending && !w.mCommitDrawPending) {
9956 if (DEBUG_ORIENTATION) Log.v(TAG,
9957 "Orientation not waiting for draw in "
9958 + w + ", surface " + w.mSurface);
9959 w.mOrientationChanging = false;
9960 }
9961 }
9962 }
9963
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009964 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009965 if (!w.mLastHidden) {
9966 //dump();
9967 w.mLastHidden = true;
9968 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009969 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009970 if (w.mSurface != null) {
9971 try {
9972 w.mSurface.hide();
9973 } catch (RuntimeException e) {
9974 Log.w(TAG, "Exception hiding surface in " + w);
9975 }
9976 }
9977 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9978 }
9979 // If we are waiting for this window to handle an
9980 // orientation change, well, it is hidden, so
9981 // doesn't really matter. Note that this does
9982 // introduce a potential glitch if the window
9983 // becomes unhidden before it has drawn for the
9984 // new orientation.
9985 if (w.mOrientationChanging) {
9986 w.mOrientationChanging = false;
9987 if (DEBUG_ORIENTATION) Log.v(TAG,
9988 "Orientation change skips hidden " + w);
9989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009990 } else if (w.mLastLayer != w.mAnimLayer
9991 || w.mLastAlpha != w.mShownAlpha
9992 || w.mLastDsDx != w.mDsDx
9993 || w.mLastDtDx != w.mDtDx
9994 || w.mLastDsDy != w.mDsDy
9995 || w.mLastDtDy != w.mDtDy
9996 || w.mLastHScale != w.mHScale
9997 || w.mLastVScale != w.mVScale
9998 || w.mLastHidden) {
9999 displayed = true;
10000 w.mLastAlpha = w.mShownAlpha;
10001 w.mLastLayer = w.mAnimLayer;
10002 w.mLastDsDx = w.mDsDx;
10003 w.mLastDtDx = w.mDtDx;
10004 w.mLastDsDy = w.mDsDy;
10005 w.mLastDtDy = w.mDtDy;
10006 w.mLastHScale = w.mHScale;
10007 w.mLastVScale = w.mVScale;
10008 if (SHOW_TRANSACTIONS) Log.i(
10009 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010010 + w.mShownAlpha + " layer=" + w.mAnimLayer
10011 + " matrix=[" + (w.mDsDx*w.mHScale)
10012 + "," + (w.mDtDx*w.mVScale)
10013 + "][" + (w.mDsDy*w.mHScale)
10014 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 if (w.mSurface != null) {
10016 try {
10017 w.mSurface.setAlpha(w.mShownAlpha);
10018 w.mSurface.setLayer(w.mAnimLayer);
10019 w.mSurface.setMatrix(
10020 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10021 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10022 } catch (RuntimeException e) {
10023 Log.w(TAG, "Error updating surface in " + w, e);
10024 if (!recoveringMemory) {
10025 reclaimSomeSurfaceMemoryLocked(w, "update");
10026 }
10027 }
10028 }
10029
10030 if (w.mLastHidden && !w.mDrawPending
10031 && !w.mCommitDrawPending
10032 && !w.mReadyToShow) {
10033 if (SHOW_TRANSACTIONS) Log.i(
10034 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10035 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10036 + " during relayout");
10037 if (showSurfaceRobustlyLocked(w)) {
10038 w.mHasDrawn = true;
10039 w.mLastHidden = false;
10040 } else {
10041 w.mOrientationChanging = false;
10042 }
10043 }
10044 if (w.mSurface != null) {
10045 w.mToken.hasVisible = true;
10046 }
10047 } else {
10048 displayed = true;
10049 }
10050
10051 if (displayed) {
10052 if (!covered) {
10053 if (attrs.width == LayoutParams.FILL_PARENT
10054 && attrs.height == LayoutParams.FILL_PARENT) {
10055 covered = true;
10056 }
10057 }
10058 if (w.mOrientationChanging) {
10059 if (w.mDrawPending || w.mCommitDrawPending) {
10060 orientationChangeComplete = false;
10061 if (DEBUG_ORIENTATION) Log.v(TAG,
10062 "Orientation continue waiting for draw in " + w);
10063 } else {
10064 w.mOrientationChanging = false;
10065 if (DEBUG_ORIENTATION) Log.v(TAG,
10066 "Orientation change complete in " + w);
10067 }
10068 }
10069 w.mToken.hasVisible = true;
10070 }
10071 } else if (w.mOrientationChanging) {
10072 if (DEBUG_ORIENTATION) Log.v(TAG,
10073 "Orientation change skips hidden " + w);
10074 w.mOrientationChanging = false;
10075 }
10076
10077 final boolean canBeSeen = w.isDisplayedLw();
10078
10079 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10080 focusDisplayed = true;
10081 }
10082
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010083 final boolean obscuredChanged = w.mObscured != obscured;
10084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010086 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010087 if (w.mSurface != null) {
10088 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10089 holdScreen = w.mSession;
10090 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010091 if (!syswin && w.mAttrs.screenBrightness >= 0
10092 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010093 screenBrightness = w.mAttrs.screenBrightness;
10094 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010095 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10096 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10097 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10098 syswin = true;
10099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010101
Dianne Hackborn25994b42009-09-04 14:21:19 -070010102 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10103 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 // This window completely covers everything behind it,
10105 // so we want to leave all of them as unblurred (for
10106 // performance reasons).
10107 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010108 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10109 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010110 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010111 obscured = true;
10112 if (mBackgroundFillerSurface == null) {
10113 try {
10114 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10115 0, dw, dh,
10116 PixelFormat.OPAQUE,
10117 Surface.FX_SURFACE_NORMAL);
10118 } catch (Exception e) {
10119 Log.e(TAG, "Exception creating filler surface", e);
10120 }
10121 }
10122 try {
10123 mBackgroundFillerSurface.setPosition(0, 0);
10124 mBackgroundFillerSurface.setSize(dw, dh);
10125 // Using the same layer as Dim because they will never be shown at the
10126 // same time.
10127 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10128 mBackgroundFillerSurface.show();
10129 } catch (RuntimeException e) {
10130 Log.e(TAG, "Exception showing filler surface");
10131 }
10132 backgroundFillerShown = true;
10133 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010134 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10136 if (localLOGV) Log.v(TAG, "Win " + w
10137 + ": blurring=" + blurring
10138 + " obscured=" + obscured
10139 + " displayed=" + displayed);
10140 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10141 if (!dimming) {
10142 //Log.i(TAG, "DIM BEHIND: " + w);
10143 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010144 if (mDimAnimator == null) {
10145 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010147 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010149 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 }
10151 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10152 if (!blurring) {
10153 //Log.i(TAG, "BLUR BEHIND: " + w);
10154 blurring = true;
10155 mBlurShown = true;
10156 if (mBlurSurface == null) {
10157 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10158 + mBlurSurface + ": CREATE");
10159 try {
Romain Guy06882f82009-06-10 13:36:04 -070010160 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 -1, 16, 16,
10162 PixelFormat.OPAQUE,
10163 Surface.FX_SURFACE_BLUR);
10164 } catch (Exception e) {
10165 Log.e(TAG, "Exception creating Blur surface", e);
10166 }
10167 }
10168 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10169 + mBlurSurface + ": SHOW pos=(0,0) (" +
10170 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10171 if (mBlurSurface != null) {
10172 mBlurSurface.setPosition(0, 0);
10173 mBlurSurface.setSize(dw, dh);
10174 try {
10175 mBlurSurface.show();
10176 } catch (RuntimeException e) {
10177 Log.w(TAG, "Failure showing blur surface", e);
10178 }
10179 }
10180 }
10181 mBlurSurface.setLayer(w.mAnimLayer-2);
10182 }
10183 }
10184 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010185
10186 if (obscuredChanged && mWallpaperTarget == w) {
10187 // This is the wallpaper target and its obscured state
10188 // changed... make sure the current wallaper's visibility
10189 // has been updated accordingly.
10190 updateWallpaperVisibilityLocked();
10191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010193
10194 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10195 mBackgroundFillerShown = false;
10196 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10197 try {
10198 mBackgroundFillerSurface.hide();
10199 } catch (RuntimeException e) {
10200 Log.e(TAG, "Exception hiding filler surface", e);
10201 }
10202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010204 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10205 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010206 }
Romain Guy06882f82009-06-10 13:36:04 -070010207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 if (!blurring && mBlurShown) {
10209 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10210 + ": HIDE");
10211 try {
10212 mBlurSurface.hide();
10213 } catch (IllegalArgumentException e) {
10214 Log.w(TAG, "Illegal argument exception hiding blur surface");
10215 }
10216 mBlurShown = false;
10217 }
10218
10219 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10220 } catch (RuntimeException e) {
10221 Log.e(TAG, "Unhandled exception in Window Manager", e);
10222 }
10223
10224 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010226 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10227 "With display frozen, orientationChangeComplete="
10228 + orientationChangeComplete);
10229 if (orientationChangeComplete) {
10230 if (mWindowsFreezingScreen) {
10231 mWindowsFreezingScreen = false;
10232 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10233 }
10234 if (mAppsFreezingScreen == 0) {
10235 stopFreezingDisplayLocked();
10236 }
10237 }
Romain Guy06882f82009-06-10 13:36:04 -070010238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010239 i = mResizingWindows.size();
10240 if (i > 0) {
10241 do {
10242 i--;
10243 WindowState win = mResizingWindows.get(i);
10244 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010245 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10246 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 win.mClient.resized(win.mFrame.width(),
10248 win.mFrame.height(), win.mLastContentInsets,
10249 win.mLastVisibleInsets, win.mDrawPending);
10250 win.mContentInsetsChanged = false;
10251 win.mVisibleInsetsChanged = false;
10252 } catch (RemoteException e) {
10253 win.mOrientationChanging = false;
10254 }
10255 } while (i > 0);
10256 mResizingWindows.clear();
10257 }
Romain Guy06882f82009-06-10 13:36:04 -070010258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010260 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 i = mDestroySurface.size();
10262 if (i > 0) {
10263 do {
10264 i--;
10265 WindowState win = mDestroySurface.get(i);
10266 win.mDestroying = false;
10267 if (mInputMethodWindow == win) {
10268 mInputMethodWindow = null;
10269 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010270 if (win == mWallpaperTarget) {
10271 wallpaperDestroyed = true;
10272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 win.destroySurfaceLocked();
10274 } while (i > 0);
10275 mDestroySurface.clear();
10276 }
10277
10278 // Time to remove any exiting tokens?
10279 for (i=mExitingTokens.size()-1; i>=0; i--) {
10280 WindowToken token = mExitingTokens.get(i);
10281 if (!token.hasVisible) {
10282 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010283 if (token.windowType == TYPE_WALLPAPER) {
10284 mWallpaperTokens.remove(token);
10285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 }
10287 }
10288
10289 // Time to remove any exiting applications?
10290 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10291 AppWindowToken token = mExitingAppTokens.get(i);
10292 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010293 // Make sure there is no animation running on this token,
10294 // so any windows associated with it will be removed as
10295 // soon as their animations are complete
10296 token.animation = null;
10297 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 mAppTokens.remove(token);
10299 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010300 if (mLastEnterAnimToken == token) {
10301 mLastEnterAnimToken = null;
10302 mLastEnterAnimParams = null;
10303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 }
10305 }
10306
Dianne Hackborna8f60182009-09-01 19:01:50 -070010307 boolean needRelayout = false;
10308
10309 if (!animating && mAppTransitionRunning) {
10310 // We have finished the animation of an app transition. To do
10311 // this, we have delayed a lot of operations like showing and
10312 // hiding apps, moving apps in Z-order, etc. The app token list
10313 // reflects the correct Z-order, but the window list may now
10314 // be out of sync with it. So here we will just rebuild the
10315 // entire app window list. Fun!
10316 mAppTransitionRunning = false;
10317 needRelayout = true;
10318 rebuildAppWindowListLocked();
10319 // Clear information about apps that were moving.
10320 mToBottomApps.clear();
10321 }
10322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 if (focusDisplayed) {
10324 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10325 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010326 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010327 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010328 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010329 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010330 requestAnimationLocked(0);
10331 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10333 }
10334 mQueue.setHoldScreenLocked(holdScreen != null);
10335 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10336 mPowerManager.setScreenBrightnessOverride(-1);
10337 } else {
10338 mPowerManager.setScreenBrightnessOverride((int)
10339 (screenBrightness * Power.BRIGHTNESS_ON));
10340 }
10341 if (holdScreen != mHoldingScreenOn) {
10342 mHoldingScreenOn = holdScreen;
10343 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10344 mH.sendMessage(m);
10345 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010346
10347 if (mTurnOnScreen) {
10348 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10349 LocalPowerManager.BUTTON_EVENT, true);
10350 mTurnOnScreen = false;
10351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 }
10353
10354 void requestAnimationLocked(long delay) {
10355 if (!mAnimationPending) {
10356 mAnimationPending = true;
10357 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10358 }
10359 }
Romain Guy06882f82009-06-10 13:36:04 -070010360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 /**
10362 * Have the surface flinger show a surface, robustly dealing with
10363 * error conditions. In particular, if there is not enough memory
10364 * to show the surface, then we will try to get rid of other surfaces
10365 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 * @return Returns true if the surface was successfully shown.
10368 */
10369 boolean showSurfaceRobustlyLocked(WindowState win) {
10370 try {
10371 if (win.mSurface != null) {
10372 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010373 if (win.mTurnOnScreen) {
10374 win.mTurnOnScreen = false;
10375 mTurnOnScreen = true;
10376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 }
10378 return true;
10379 } catch (RuntimeException e) {
10380 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10381 }
Romain Guy06882f82009-06-10 13:36:04 -070010382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 return false;
10386 }
Romain Guy06882f82009-06-10 13:36:04 -070010387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010388 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10389 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010391 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10392 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 if (mForceRemoves == null) {
10395 mForceRemoves = new ArrayList<WindowState>();
10396 }
Romain Guy06882f82009-06-10 13:36:04 -070010397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 long callingIdentity = Binder.clearCallingIdentity();
10399 try {
10400 // There was some problem... first, do a sanity check of the
10401 // window list to make sure we haven't left any dangling surfaces
10402 // around.
10403 int N = mWindows.size();
10404 boolean leakedSurface = false;
10405 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10406 for (int i=0; i<N; i++) {
10407 WindowState ws = (WindowState)mWindows.get(i);
10408 if (ws.mSurface != null) {
10409 if (!mSessions.contains(ws.mSession)) {
10410 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10411 + ws + " surface=" + ws.mSurface
10412 + " token=" + win.mToken
10413 + " pid=" + ws.mSession.mPid
10414 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010415 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010416 ws.mSurface = null;
10417 mForceRemoves.add(ws);
10418 i--;
10419 N--;
10420 leakedSurface = true;
10421 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10422 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10423 + ws + " surface=" + ws.mSurface
10424 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010425 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 ws.mSurface = null;
10427 leakedSurface = true;
10428 }
10429 }
10430 }
Romain Guy06882f82009-06-10 13:36:04 -070010431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 boolean killedApps = false;
10433 if (!leakedSurface) {
10434 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10435 SparseIntArray pidCandidates = new SparseIntArray();
10436 for (int i=0; i<N; i++) {
10437 WindowState ws = (WindowState)mWindows.get(i);
10438 if (ws.mSurface != null) {
10439 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10440 }
10441 }
10442 if (pidCandidates.size() > 0) {
10443 int[] pids = new int[pidCandidates.size()];
10444 for (int i=0; i<pids.length; i++) {
10445 pids[i] = pidCandidates.keyAt(i);
10446 }
10447 try {
10448 if (mActivityManager.killPidsForMemory(pids)) {
10449 killedApps = true;
10450 }
10451 } catch (RemoteException e) {
10452 }
10453 }
10454 }
Romain Guy06882f82009-06-10 13:36:04 -070010455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456 if (leakedSurface || killedApps) {
10457 // We managed to reclaim some memory, so get rid of the trouble
10458 // surface and ask the app to request another one.
10459 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10460 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010461 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 win.mSurface = null;
10463 }
Romain Guy06882f82009-06-10 13:36:04 -070010464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 try {
10466 win.mClient.dispatchGetNewSurface();
10467 } catch (RemoteException e) {
10468 }
10469 }
10470 } finally {
10471 Binder.restoreCallingIdentity(callingIdentity);
10472 }
10473 }
Romain Guy06882f82009-06-10 13:36:04 -070010474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 private boolean updateFocusedWindowLocked(int mode) {
10476 WindowState newFocus = computeFocusedWindowLocked();
10477 if (mCurrentFocus != newFocus) {
10478 // This check makes sure that we don't already have the focus
10479 // change message pending.
10480 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10481 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10482 if (localLOGV) Log.v(
10483 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10484 final WindowState oldFocus = mCurrentFocus;
10485 mCurrentFocus = newFocus;
10486 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488 final WindowState imWindow = mInputMethodWindow;
10489 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010490 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010492 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10493 mLayoutNeeded = true;
10494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010495 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10496 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010497 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10498 // Client will do the layout, but we need to assign layers
10499 // for handleNewWindowLocked() below.
10500 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 }
10502 }
Romain Guy06882f82009-06-10 13:36:04 -070010503
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010504 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10505 mKeyWaiter.handleNewWindowLocked(newFocus);
10506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 return true;
10508 }
10509 return false;
10510 }
10511
10512 private WindowState computeFocusedWindowLocked() {
10513 WindowState result = null;
10514 WindowState win;
10515
10516 int i = mWindows.size() - 1;
10517 int nextAppIndex = mAppTokens.size()-1;
10518 WindowToken nextApp = nextAppIndex >= 0
10519 ? mAppTokens.get(nextAppIndex) : null;
10520
10521 while (i >= 0) {
10522 win = (WindowState)mWindows.get(i);
10523
10524 if (localLOGV || DEBUG_FOCUS) Log.v(
10525 TAG, "Looking for focus: " + i
10526 + " = " + win
10527 + ", flags=" + win.mAttrs.flags
10528 + ", canReceive=" + win.canReceiveKeys());
10529
10530 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010532 // If this window's application has been removed, just skip it.
10533 if (thisApp != null && thisApp.removed) {
10534 i--;
10535 continue;
10536 }
Romain Guy06882f82009-06-10 13:36:04 -070010537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010538 // If there is a focused app, don't allow focus to go to any
10539 // windows below it. If this is an application window, step
10540 // through the app tokens until we find its app.
10541 if (thisApp != null && nextApp != null && thisApp != nextApp
10542 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10543 int origAppIndex = nextAppIndex;
10544 while (nextAppIndex > 0) {
10545 if (nextApp == mFocusedApp) {
10546 // Whoops, we are below the focused app... no focus
10547 // for you!
10548 if (localLOGV || DEBUG_FOCUS) Log.v(
10549 TAG, "Reached focused app: " + mFocusedApp);
10550 return null;
10551 }
10552 nextAppIndex--;
10553 nextApp = mAppTokens.get(nextAppIndex);
10554 if (nextApp == thisApp) {
10555 break;
10556 }
10557 }
10558 if (thisApp != nextApp) {
10559 // Uh oh, the app token doesn't exist! This shouldn't
10560 // happen, but if it does we can get totally hosed...
10561 // so restart at the original app.
10562 nextAppIndex = origAppIndex;
10563 nextApp = mAppTokens.get(nextAppIndex);
10564 }
10565 }
10566
10567 // Dispatch to this window if it is wants key events.
10568 if (win.canReceiveKeys()) {
10569 if (DEBUG_FOCUS) Log.v(
10570 TAG, "Found focus @ " + i + " = " + win);
10571 result = win;
10572 break;
10573 }
10574
10575 i--;
10576 }
10577
10578 return result;
10579 }
10580
10581 private void startFreezingDisplayLocked() {
10582 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010583 // Freezing the display also suspends key event delivery, to
10584 // keep events from going astray while the display is reconfigured.
10585 // If someone has changed orientation again while the screen is
10586 // still frozen, the events will continue to be blocked while the
10587 // successive orientation change is processed. To prevent spurious
10588 // ANRs, we reset the event dispatch timeout in this case.
10589 synchronized (mKeyWaiter) {
10590 mKeyWaiter.mWasFrozen = true;
10591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 return;
10593 }
Romain Guy06882f82009-06-10 13:36:04 -070010594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 long now = SystemClock.uptimeMillis();
10598 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10599 if (mFreezeGcPending != 0) {
10600 if (now > (mFreezeGcPending+1000)) {
10601 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10602 mH.removeMessages(H.FORCE_GC);
10603 Runtime.getRuntime().gc();
10604 mFreezeGcPending = now;
10605 }
10606 } else {
10607 mFreezeGcPending = now;
10608 }
Romain Guy06882f82009-06-10 13:36:04 -070010609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010611 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10612 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010613 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 mAppTransitionReady = true;
10615 }
Romain Guy06882f82009-06-10 13:36:04 -070010616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010617 if (PROFILE_ORIENTATION) {
10618 File file = new File("/data/system/frozen");
10619 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10620 }
10621 Surface.freezeDisplay(0);
10622 }
Romain Guy06882f82009-06-10 13:36:04 -070010623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 private void stopFreezingDisplayLocked() {
10625 if (!mDisplayFrozen) {
10626 return;
10627 }
Romain Guy06882f82009-06-10 13:36:04 -070010628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010629 mDisplayFrozen = false;
10630 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10631 if (PROFILE_ORIENTATION) {
10632 Debug.stopMethodTracing();
10633 }
10634 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010635
Chris Tate2ad63a92009-03-25 17:36:48 -070010636 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10637 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 synchronized (mKeyWaiter) {
10639 mKeyWaiter.mWasFrozen = true;
10640 mKeyWaiter.notifyAll();
10641 }
10642
10643 // A little kludge: a lot could have happened while the
10644 // display was frozen, so now that we are coming back we
10645 // do a gc so that any remote references the system
10646 // processes holds on others can be released if they are
10647 // no longer needed.
10648 mH.removeMessages(H.FORCE_GC);
10649 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10650 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652 mScreenFrozenLock.release();
10653 }
Romain Guy06882f82009-06-10 13:36:04 -070010654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 @Override
10656 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10657 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10658 != PackageManager.PERMISSION_GRANTED) {
10659 pw.println("Permission Denial: can't dump WindowManager from from pid="
10660 + Binder.getCallingPid()
10661 + ", uid=" + Binder.getCallingUid());
10662 return;
10663 }
Romain Guy06882f82009-06-10 13:36:04 -070010664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 synchronized(mWindowMap) {
10666 pw.println("Current Window Manager state:");
10667 for (int i=mWindows.size()-1; i>=0; i--) {
10668 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010669 pw.print(" Window #"); pw.print(i); pw.print(' ');
10670 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671 w.dump(pw, " ");
10672 }
10673 if (mInputMethodDialogs.size() > 0) {
10674 pw.println(" ");
10675 pw.println(" Input method dialogs:");
10676 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10677 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010678 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 }
10680 }
10681 if (mPendingRemove.size() > 0) {
10682 pw.println(" ");
10683 pw.println(" Remove pending for:");
10684 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10685 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010686 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10687 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 w.dump(pw, " ");
10689 }
10690 }
10691 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10692 pw.println(" ");
10693 pw.println(" Windows force removing:");
10694 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10695 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010696 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10697 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 w.dump(pw, " ");
10699 }
10700 }
10701 if (mDestroySurface.size() > 0) {
10702 pw.println(" ");
10703 pw.println(" Windows waiting to destroy their surface:");
10704 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10705 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010706 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10707 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 w.dump(pw, " ");
10709 }
10710 }
10711 if (mLosingFocus.size() > 0) {
10712 pw.println(" ");
10713 pw.println(" Windows losing focus:");
10714 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10715 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010716 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10717 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010718 w.dump(pw, " ");
10719 }
10720 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010721 if (mResizingWindows.size() > 0) {
10722 pw.println(" ");
10723 pw.println(" Windows waiting to resize:");
10724 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10725 WindowState w = mResizingWindows.get(i);
10726 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10727 pw.print(w); pw.println(":");
10728 w.dump(pw, " ");
10729 }
10730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 if (mSessions.size() > 0) {
10732 pw.println(" ");
10733 pw.println(" All active sessions:");
10734 Iterator<Session> it = mSessions.iterator();
10735 while (it.hasNext()) {
10736 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010737 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010738 s.dump(pw, " ");
10739 }
10740 }
10741 if (mTokenMap.size() > 0) {
10742 pw.println(" ");
10743 pw.println(" All tokens:");
10744 Iterator<WindowToken> it = mTokenMap.values().iterator();
10745 while (it.hasNext()) {
10746 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010747 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 token.dump(pw, " ");
10749 }
10750 }
10751 if (mTokenList.size() > 0) {
10752 pw.println(" ");
10753 pw.println(" Window token list:");
10754 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010755 pw.print(" #"); pw.print(i); pw.print(": ");
10756 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 }
10758 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010759 if (mWallpaperTokens.size() > 0) {
10760 pw.println(" ");
10761 pw.println(" Wallpaper tokens:");
10762 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10763 WindowToken token = mWallpaperTokens.get(i);
10764 pw.print(" Wallpaper #"); pw.print(i);
10765 pw.print(' '); pw.print(token); pw.println(':');
10766 token.dump(pw, " ");
10767 }
10768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 if (mAppTokens.size() > 0) {
10770 pw.println(" ");
10771 pw.println(" Application tokens in Z order:");
10772 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010773 pw.print(" App #"); pw.print(i); pw.print(": ");
10774 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 }
10776 }
10777 if (mFinishedStarting.size() > 0) {
10778 pw.println(" ");
10779 pw.println(" Finishing start of application tokens:");
10780 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10781 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010782 pw.print(" Finished Starting #"); pw.print(i);
10783 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010784 token.dump(pw, " ");
10785 }
10786 }
10787 if (mExitingTokens.size() > 0) {
10788 pw.println(" ");
10789 pw.println(" Exiting tokens:");
10790 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10791 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010792 pw.print(" Exiting #"); pw.print(i);
10793 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 token.dump(pw, " ");
10795 }
10796 }
10797 if (mExitingAppTokens.size() > 0) {
10798 pw.println(" ");
10799 pw.println(" Exiting application tokens:");
10800 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10801 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010802 pw.print(" Exiting App #"); pw.print(i);
10803 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 token.dump(pw, " ");
10805 }
10806 }
10807 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010808 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10809 pw.print(" mLastFocus="); pw.println(mLastFocus);
10810 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10811 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10812 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010813 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010814 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10815 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10816 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10817 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010818 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10819 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10820 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10821 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10822 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010823 if (mDimAnimator != null) {
10824 mDimAnimator.printTo(pw);
10825 } else {
10826 pw.print( " no DimAnimator ");
10827 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010828 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010829 pw.print(mInputMethodAnimLayerAdjustment);
10830 pw.print(" mWallpaperAnimLayerAdjustment=");
10831 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010832 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10833 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010834 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10835 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10836 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10837 pw.print(" mRotation="); pw.print(mRotation);
10838 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10839 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10840 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10841 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10842 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10843 pw.print(" mNextAppTransition=0x");
10844 pw.print(Integer.toHexString(mNextAppTransition));
10845 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010846 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010847 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010848 if (mNextAppTransitionPackage != null) {
10849 pw.print(" mNextAppTransitionPackage=");
10850 pw.print(mNextAppTransitionPackage);
10851 pw.print(", mNextAppTransitionEnter=0x");
10852 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10853 pw.print(", mNextAppTransitionExit=0x");
10854 pw.print(Integer.toHexString(mNextAppTransitionExit));
10855 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010856 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10857 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010858 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10859 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10860 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10861 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010862 if (mOpeningApps.size() > 0) {
10863 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10864 }
10865 if (mClosingApps.size() > 0) {
10866 pw.print(" mClosingApps="); pw.println(mClosingApps);
10867 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010868 if (mToTopApps.size() > 0) {
10869 pw.print(" mToTopApps="); pw.println(mToTopApps);
10870 }
10871 if (mToBottomApps.size() > 0) {
10872 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10873 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010874 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10875 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010876 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010877 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10878 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10879 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10880 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10881 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10882 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883 }
10884 }
10885
10886 public void monitor() {
10887 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010888 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 synchronized (mKeyWaiter) { }
10890 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010891
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010892 public void virtualKeyFeedback(KeyEvent event) {
10893 mPolicy.keyFeedbackFromInput(event);
10894 }
10895
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010896 /**
10897 * DimAnimator class that controls the dim animation. This holds the surface and
10898 * all state used for dim animation.
10899 */
10900 private static class DimAnimator {
10901 Surface mDimSurface;
10902 boolean mDimShown = false;
10903 float mDimCurrentAlpha;
10904 float mDimTargetAlpha;
10905 float mDimDeltaPerMs;
10906 long mLastDimAnimTime;
10907
10908 DimAnimator (SurfaceSession session) {
10909 if (mDimSurface == null) {
10910 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10911 + mDimSurface + ": CREATE");
10912 try {
10913 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10914 Surface.FX_SURFACE_DIM);
10915 } catch (Exception e) {
10916 Log.e(TAG, "Exception creating Dim surface", e);
10917 }
10918 }
10919 }
10920
10921 /**
10922 * Show the dim surface.
10923 */
10924 void show(int dw, int dh) {
10925 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10926 dw + "x" + dh + ")");
10927 mDimShown = true;
10928 try {
10929 mDimSurface.setPosition(0, 0);
10930 mDimSurface.setSize(dw, dh);
10931 mDimSurface.show();
10932 } catch (RuntimeException e) {
10933 Log.w(TAG, "Failure showing dim surface", e);
10934 }
10935 }
10936
10937 /**
10938 * Set's the dim surface's layer and update dim parameters that will be used in
10939 * {@link updateSurface} after all windows are examined.
10940 */
10941 void updateParameters(WindowState w, long currentTime) {
10942 mDimSurface.setLayer(w.mAnimLayer-1);
10943
10944 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010945 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10946 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010947 if (mDimTargetAlpha != target) {
10948 // If the desired dim level has changed, then
10949 // start an animation to it.
10950 mLastDimAnimTime = currentTime;
10951 long duration = (w.mAnimating && w.mAnimation != null)
10952 ? w.mAnimation.computeDurationHint()
10953 : DEFAULT_DIM_DURATION;
10954 if (target > mDimTargetAlpha) {
10955 // This is happening behind the activity UI,
10956 // so we can make it run a little longer to
10957 // give a stronger impression without disrupting
10958 // the user.
10959 duration *= DIM_DURATION_MULTIPLIER;
10960 }
10961 if (duration < 1) {
10962 // Don't divide by zero
10963 duration = 1;
10964 }
10965 mDimTargetAlpha = target;
10966 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10967 }
10968 }
10969
10970 /**
10971 * Updating the surface's alpha. Returns true if the animation continues, or returns
10972 * false when the animation is finished and the dim surface is hidden.
10973 */
10974 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10975 if (!dimming) {
10976 if (mDimTargetAlpha != 0) {
10977 mLastDimAnimTime = currentTime;
10978 mDimTargetAlpha = 0;
10979 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10980 }
10981 }
10982
10983 boolean animating = false;
10984 if (mLastDimAnimTime != 0) {
10985 mDimCurrentAlpha += mDimDeltaPerMs
10986 * (currentTime-mLastDimAnimTime);
10987 boolean more = true;
10988 if (displayFrozen) {
10989 // If the display is frozen, there is no reason to animate.
10990 more = false;
10991 } else if (mDimDeltaPerMs > 0) {
10992 if (mDimCurrentAlpha > mDimTargetAlpha) {
10993 more = false;
10994 }
10995 } else if (mDimDeltaPerMs < 0) {
10996 if (mDimCurrentAlpha < mDimTargetAlpha) {
10997 more = false;
10998 }
10999 } else {
11000 more = false;
11001 }
11002
11003 // Do we need to continue animating?
11004 if (more) {
11005 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11006 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11007 mLastDimAnimTime = currentTime;
11008 mDimSurface.setAlpha(mDimCurrentAlpha);
11009 animating = true;
11010 } else {
11011 mDimCurrentAlpha = mDimTargetAlpha;
11012 mLastDimAnimTime = 0;
11013 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11014 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11015 mDimSurface.setAlpha(mDimCurrentAlpha);
11016 if (!dimming) {
11017 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11018 + ": HIDE");
11019 try {
11020 mDimSurface.hide();
11021 } catch (RuntimeException e) {
11022 Log.w(TAG, "Illegal argument exception hiding dim surface");
11023 }
11024 mDimShown = false;
11025 }
11026 }
11027 }
11028 return animating;
11029 }
11030
11031 public void printTo(PrintWriter pw) {
11032 pw.print(" mDimShown="); pw.print(mDimShown);
11033 pw.print(" current="); pw.print(mDimCurrentAlpha);
11034 pw.print(" target="); pw.print(mDimTargetAlpha);
11035 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11036 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11037 }
11038 }
11039
11040 /**
11041 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11042 * This is used for opening/closing transition for apps in compatible mode.
11043 */
11044 private static class FadeInOutAnimation extends Animation {
11045 int mWidth;
11046 boolean mFadeIn;
11047
11048 public FadeInOutAnimation(boolean fadeIn) {
11049 setInterpolator(new AccelerateInterpolator());
11050 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11051 mFadeIn = fadeIn;
11052 }
11053
11054 @Override
11055 protected void applyTransformation(float interpolatedTime, Transformation t) {
11056 float x = interpolatedTime;
11057 if (!mFadeIn) {
11058 x = 1.0f - x; // reverse the interpolation for fade out
11059 }
11060 if (x < 0.5) {
11061 // move the window out of the screen.
11062 t.getMatrix().setTranslate(mWidth, 0);
11063 } else {
11064 t.getMatrix().setTranslate(0, 0);// show
11065 t.setAlpha((x - 0.5f) * 2);
11066 }
11067 }
11068
11069 @Override
11070 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11071 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11072 mWidth = width;
11073 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011074
11075 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011076 public int getZAdjustment() {
11077 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011078 }
11079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011080}